blob: 9239823326fd85b631e71b268458d54b7c4925c0 [file] [log] [blame]
Willy Tarreaubaaee002006-06-26 02:48:02 +02001/*
2 * HTTP protocol analyzer
3 *
Willy Tarreauf68a15a2011-01-06 16:53:21 +01004 * Copyright 2000-2011 Willy Tarreau <w@1wt.eu>
Willy Tarreaubaaee002006-06-26 02:48:02 +02005 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version
9 * 2 of the License, or (at your option) any later version.
10 *
11 */
12
13#include <ctype.h>
14#include <errno.h>
15#include <fcntl.h>
16#include <stdio.h>
17#include <stdlib.h>
18#include <string.h>
19#include <syslog.h>
Willy Tarreau42250582007-04-01 01:30:43 +020020#include <time.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020021
22#include <sys/socket.h>
23#include <sys/stat.h>
24#include <sys/types.h>
25
Willy Tarreaub05405a2012-01-23 15:35:52 +010026#include <netinet/tcp.h>
27
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010028#include <common/base64.h>
Willy Tarreauc7e42382012-08-24 19:22:53 +020029#include <common/chunk.h>
Willy Tarreau2dd0d472006-06-29 17:53:05 +020030#include <common/compat.h>
31#include <common/config.h>
Willy Tarreaua4cd1f52006-12-16 19:57:26 +010032#include <common/debug.h>
Willy Tarreau2dd0d472006-06-29 17:53:05 +020033#include <common/memory.h>
34#include <common/mini-clist.h>
35#include <common/standard.h>
Willy Tarreau0c303ee2008-07-07 00:09:58 +020036#include <common/ticks.h>
Willy Tarreau2dd0d472006-06-29 17:53:05 +020037#include <common/time.h>
38#include <common/uri_auth.h>
39#include <common/version.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020040
41#include <types/capture.h>
Willy Tarreau12207b32016-11-22 19:48:51 +010042#include <types/cli.h>
Christopher Fauletd7c91962015-04-30 11:48:27 +020043#include <types/filters.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020044#include <types/global.h>
William Lallemand9ed62032016-11-21 17:49:11 +010045#include <types/stats.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020046
Willy Tarreau8797c062007-05-07 00:55:35 +020047#include <proto/acl.h>
Thierry FOURNIER322a1242015-08-19 09:07:47 +020048#include <proto/action.h>
Willy Tarreau61612d42012-04-19 18:42:05 +020049#include <proto/arg.h>
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010050#include <proto/auth.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020051#include <proto/backend.h>
Willy Tarreauc7e42382012-08-24 19:22:53 +020052#include <proto/channel.h>
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +010053#include <proto/checks.h>
Willy Tarreau12207b32016-11-22 19:48:51 +010054#include <proto/cli.h>
William Lallemand82fe75c2012-10-23 10:25:10 +020055#include <proto/compression.h>
William Lallemand9ed62032016-11-21 17:49:11 +010056#include <proto/stats.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020057#include <proto/fd.h>
Christopher Fauletd7c91962015-04-30 11:48:27 +020058#include <proto/filters.h>
Willy Tarreau03fa5df2010-05-24 21:02:37 +020059#include <proto/frontend.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020060#include <proto/log.h>
Willy Tarreau58f10d72006-12-04 02:26:12 +010061#include <proto/hdr_idx.h>
Thierry FOURNIERed66c292013-11-28 11:05:19 +010062#include <proto/pattern.h>
Willy Tarreaub6866442008-07-14 23:54:42 +020063#include <proto/proto_tcp.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020064#include <proto/proto_http.h>
Willy Tarreau7f062c42009-03-05 18:43:00 +010065#include <proto/proxy.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020066#include <proto/queue.h>
Willy Tarreaucd3b0942012-04-27 21:52:18 +020067#include <proto/sample.h>
Willy Tarreau7f062c42009-03-05 18:43:00 +010068#include <proto/server.h>
Willy Tarreau87b09662015-04-03 00:22:06 +020069#include <proto/stream.h>
Willy Tarreaucff64112008-11-03 06:26:53 +010070#include <proto/stream_interface.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020071#include <proto/task.h>
Baptiste Assmannfabcbe02014-04-24 22:16:59 +020072#include <proto/pattern.h>
Thierry FOURNIER4834bc72015-06-06 19:29:07 +020073#include <proto/vars.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020074
Willy Tarreau522d6c02009-12-06 18:49:18 +010075const char HTTP_100[] =
76 "HTTP/1.1 100 Continue\r\n\r\n";
77
78const struct chunk http_100_chunk = {
79 .str = (char *)&HTTP_100,
80 .len = sizeof(HTTP_100)-1
81};
82
Willy Tarreaua9679ac2010-01-03 17:32:57 +010083/* Warning: no "connection" header is provided with the 3xx messages below */
Willy Tarreaub463dfb2008-06-07 23:08:56 +020084const char *HTTP_301 =
Willy Tarreaubc5aa192010-01-03 15:09:36 +010085 "HTTP/1.1 301 Moved Permanently\r\n"
Willy Tarreaubc5aa192010-01-03 15:09:36 +010086 "Content-length: 0\r\n"
Willy Tarreaub463dfb2008-06-07 23:08:56 +020087 "Location: "; /* not terminated since it will be concatenated with the URL */
88
Willy Tarreau0f772532006-12-23 20:51:41 +010089const char *HTTP_302 =
Willy Tarreaubc5aa192010-01-03 15:09:36 +010090 "HTTP/1.1 302 Found\r\n"
Willy Tarreau0f772532006-12-23 20:51:41 +010091 "Cache-Control: no-cache\r\n"
Willy Tarreaubc5aa192010-01-03 15:09:36 +010092 "Content-length: 0\r\n"
Willy Tarreau0f772532006-12-23 20:51:41 +010093 "Location: "; /* not terminated since it will be concatenated with the URL */
94
95/* same as 302 except that the browser MUST retry with the GET method */
96const char *HTTP_303 =
Willy Tarreaubc5aa192010-01-03 15:09:36 +010097 "HTTP/1.1 303 See Other\r\n"
Willy Tarreau0f772532006-12-23 20:51:41 +010098 "Cache-Control: no-cache\r\n"
Willy Tarreaubc5aa192010-01-03 15:09:36 +010099 "Content-length: 0\r\n"
Willy Tarreau0f772532006-12-23 20:51:41 +0100100 "Location: "; /* not terminated since it will be concatenated with the URL */
101
Yves Lafon3e8d1ae2013-03-11 11:06:05 -0400102
103/* same as 302 except that the browser MUST retry with the same method */
104const char *HTTP_307 =
105 "HTTP/1.1 307 Temporary Redirect\r\n"
106 "Cache-Control: no-cache\r\n"
107 "Content-length: 0\r\n"
108 "Location: "; /* not terminated since it will be concatenated with the URL */
109
110/* same as 301 except that the browser MUST retry with the same method */
111const char *HTTP_308 =
112 "HTTP/1.1 308 Permanent Redirect\r\n"
113 "Content-length: 0\r\n"
114 "Location: "; /* not terminated since it will be concatenated with the URL */
115
Willy Tarreaubaaee002006-06-26 02:48:02 +0200116/* Warning: this one is an sprintf() fmt string, with <realm> as its only argument */
117const char *HTTP_401_fmt =
118 "HTTP/1.0 401 Unauthorized\r\n"
119 "Cache-Control: no-cache\r\n"
120 "Connection: close\r\n"
Willy Tarreau791d66d2006-07-08 16:53:38 +0200121 "Content-Type: text/html\r\n"
Willy Tarreaubaaee002006-06-26 02:48:02 +0200122 "WWW-Authenticate: Basic realm=\"%s\"\r\n"
123 "\r\n"
124 "<html><body><h1>401 Unauthorized</h1>\nYou need a valid user and password to access this content.\n</body></html>\n";
125
Willy Tarreau844a7e72010-01-31 21:46:18 +0100126const char *HTTP_407_fmt =
127 "HTTP/1.0 407 Unauthorized\r\n"
128 "Cache-Control: no-cache\r\n"
129 "Connection: close\r\n"
130 "Content-Type: text/html\r\n"
131 "Proxy-Authenticate: Basic realm=\"%s\"\r\n"
132 "\r\n"
Godbach1f1fae62014-12-17 16:32:05 +0800133 "<html><body><h1>407 Unauthorized</h1>\nYou need a valid user and password to access this content.\n</body></html>\n";
Willy Tarreau844a7e72010-01-31 21:46:18 +0100134
Willy Tarreau0f772532006-12-23 20:51:41 +0100135
136const int http_err_codes[HTTP_ERR_SIZE] = {
Willy Tarreauae94d4d2011-05-11 16:28:49 +0200137 [HTTP_ERR_200] = 200, /* used by "monitor-uri" */
Willy Tarreau0f772532006-12-23 20:51:41 +0100138 [HTTP_ERR_400] = 400,
139 [HTTP_ERR_403] = 403,
CJ Ess108b1dd2015-04-07 12:03:37 -0400140 [HTTP_ERR_405] = 405,
Willy Tarreau0f772532006-12-23 20:51:41 +0100141 [HTTP_ERR_408] = 408,
CJ Ess108b1dd2015-04-07 12:03:37 -0400142 [HTTP_ERR_429] = 429,
Willy Tarreau0f772532006-12-23 20:51:41 +0100143 [HTTP_ERR_500] = 500,
144 [HTTP_ERR_502] = 502,
145 [HTTP_ERR_503] = 503,
146 [HTTP_ERR_504] = 504,
147};
148
Willy Tarreau80587432006-12-24 17:47:20 +0100149static const char *http_err_msgs[HTTP_ERR_SIZE] = {
Willy Tarreauae94d4d2011-05-11 16:28:49 +0200150 [HTTP_ERR_200] =
151 "HTTP/1.0 200 OK\r\n"
152 "Cache-Control: no-cache\r\n"
153 "Connection: close\r\n"
154 "Content-Type: text/html\r\n"
155 "\r\n"
156 "<html><body><h1>200 OK</h1>\nService ready.\n</body></html>\n",
157
Willy Tarreau0f772532006-12-23 20:51:41 +0100158 [HTTP_ERR_400] =
Willy Tarreau80587432006-12-24 17:47:20 +0100159 "HTTP/1.0 400 Bad request\r\n"
Willy Tarreau0f772532006-12-23 20:51:41 +0100160 "Cache-Control: no-cache\r\n"
161 "Connection: close\r\n"
162 "Content-Type: text/html\r\n"
163 "\r\n"
164 "<html><body><h1>400 Bad request</h1>\nYour browser sent an invalid request.\n</body></html>\n",
165
166 [HTTP_ERR_403] =
167 "HTTP/1.0 403 Forbidden\r\n"
168 "Cache-Control: no-cache\r\n"
169 "Connection: close\r\n"
170 "Content-Type: text/html\r\n"
171 "\r\n"
172 "<html><body><h1>403 Forbidden</h1>\nRequest forbidden by administrative rules.\n</body></html>\n",
173
CJ Ess108b1dd2015-04-07 12:03:37 -0400174 [HTTP_ERR_405] =
175 "HTTP/1.0 405 Method Not Allowed\r\n"
176 "Cache-Control: no-cache\r\n"
177 "Connection: close\r\n"
178 "Content-Type: text/html\r\n"
179 "\r\n"
180 "<html><body><h1>405 Method Not Allowed</h1>\nA request was made of a resource using a request method not supported by that resource\n</body></html>\n",
181
Willy Tarreau0f772532006-12-23 20:51:41 +0100182 [HTTP_ERR_408] =
183 "HTTP/1.0 408 Request Time-out\r\n"
184 "Cache-Control: no-cache\r\n"
185 "Connection: close\r\n"
186 "Content-Type: text/html\r\n"
187 "\r\n"
188 "<html><body><h1>408 Request Time-out</h1>\nYour browser didn't send a complete request in time.\n</body></html>\n",
189
CJ Ess108b1dd2015-04-07 12:03:37 -0400190 [HTTP_ERR_429] =
191 "HTTP/1.0 429 Too Many Requests\r\n"
192 "Cache-Control: no-cache\r\n"
193 "Connection: close\r\n"
194 "Content-Type: text/html\r\n"
195 "\r\n"
196 "<html><body><h1>429 Too Many Requests</h1>\nYou have sent too many requests in a given amount of time.\n</body></html>\n",
197
Willy Tarreau0f772532006-12-23 20:51:41 +0100198 [HTTP_ERR_500] =
Jarno Huuskonen16ad94a2017-01-09 14:17:10 +0200199 "HTTP/1.0 500 Internal Server Error\r\n"
Willy Tarreau0f772532006-12-23 20:51:41 +0100200 "Cache-Control: no-cache\r\n"
201 "Connection: close\r\n"
202 "Content-Type: text/html\r\n"
203 "\r\n"
Jarno Huuskonen16ad94a2017-01-09 14:17:10 +0200204 "<html><body><h1>500 Internal Server Error</h1>\nAn internal server error occured.\n</body></html>\n",
Willy Tarreau0f772532006-12-23 20:51:41 +0100205
206 [HTTP_ERR_502] =
207 "HTTP/1.0 502 Bad Gateway\r\n"
208 "Cache-Control: no-cache\r\n"
209 "Connection: close\r\n"
210 "Content-Type: text/html\r\n"
211 "\r\n"
212 "<html><body><h1>502 Bad Gateway</h1>\nThe server returned an invalid or incomplete response.\n</body></html>\n",
213
214 [HTTP_ERR_503] =
215 "HTTP/1.0 503 Service Unavailable\r\n"
216 "Cache-Control: no-cache\r\n"
217 "Connection: close\r\n"
218 "Content-Type: text/html\r\n"
219 "\r\n"
220 "<html><body><h1>503 Service Unavailable</h1>\nNo server is available to handle this request.\n</body></html>\n",
221
222 [HTTP_ERR_504] =
223 "HTTP/1.0 504 Gateway Time-out\r\n"
224 "Cache-Control: no-cache\r\n"
225 "Connection: close\r\n"
226 "Content-Type: text/html\r\n"
227 "\r\n"
228 "<html><body><h1>504 Gateway Time-out</h1>\nThe server didn't respond in time.\n</body></html>\n",
229
230};
231
Cyril Bonté19979e12012-04-04 12:57:21 +0200232/* status codes available for the stats admin page (strictly 4 chars length) */
233const char *stat_status_codes[STAT_STATUS_SIZE] = {
234 [STAT_STATUS_DENY] = "DENY",
235 [STAT_STATUS_DONE] = "DONE",
236 [STAT_STATUS_ERRP] = "ERRP",
237 [STAT_STATUS_EXCD] = "EXCD",
238 [STAT_STATUS_NONE] = "NONE",
239 [STAT_STATUS_PART] = "PART",
240 [STAT_STATUS_UNKN] = "UNKN",
241};
242
243
William Lallemand73025dd2014-04-24 14:38:37 +0200244/* List head of all known action keywords for "http-request" */
Thierry FOURNIER36481b82015-08-19 09:01:53 +0200245struct action_kw_list http_req_keywords = {
William Lallemand73025dd2014-04-24 14:38:37 +0200246 .list = LIST_HEAD_INIT(http_req_keywords.list)
247};
248
249/* List head of all known action keywords for "http-response" */
Thierry FOURNIER36481b82015-08-19 09:01:53 +0200250struct action_kw_list http_res_keywords = {
William Lallemand73025dd2014-04-24 14:38:37 +0200251 .list = LIST_HEAD_INIT(http_res_keywords.list)
252};
253
Willy Tarreau80587432006-12-24 17:47:20 +0100254/* We must put the messages here since GCC cannot initialize consts depending
255 * on strlen().
256 */
257struct chunk http_err_chunks[HTTP_ERR_SIZE];
258
Willy Tarreaua890d072013-04-02 12:01:06 +0200259/* this struct is used between calls to smp_fetch_hdr() or smp_fetch_cookie() */
260static struct hdr_ctx static_hdr_ctx;
261
Willy Tarreau42250582007-04-01 01:30:43 +0200262#define FD_SETS_ARE_BITFIELDS
263#ifdef FD_SETS_ARE_BITFIELDS
264/*
265 * This map is used with all the FD_* macros to check whether a particular bit
266 * is set or not. Each bit represents an ACSII code. FD_SET() sets those bytes
267 * which should be encoded. When FD_ISSET() returns non-zero, it means that the
268 * byte should be encoded. Be careful to always pass bytes from 0 to 255
269 * exclusively to the macros.
270 */
271fd_set hdr_encode_map[(sizeof(fd_set) > (256/8)) ? 1 : ((256/8) / sizeof(fd_set))];
272fd_set url_encode_map[(sizeof(fd_set) > (256/8)) ? 1 : ((256/8) / sizeof(fd_set))];
Thierry FOURNIERd048d8b2014-03-13 16:46:18 +0100273fd_set http_encode_map[(sizeof(fd_set) > (256/8)) ? 1 : ((256/8) / sizeof(fd_set))];
Willy Tarreau42250582007-04-01 01:30:43 +0200274
275#else
276#error "Check if your OS uses bitfields for fd_sets"
277#endif
278
Willy Tarreau87b09662015-04-03 00:22:06 +0200279static int http_apply_redirect_rule(struct redirect_rule *rule, struct stream *s, struct http_txn *txn);
Willy Tarreau0b748332014-04-29 00:13:29 +0200280
David Carlier7365f7d2016-04-04 11:54:42 +0100281static inline int http_msg_forward_body(struct stream *s, struct http_msg *msg);
282static inline int http_msg_forward_chunked_body(struct stream *s, struct http_msg *msg);
Christopher Fauletdbe34eb2015-12-02 10:01:17 +0100283
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +0200284/* This function returns a reason associated with the HTTP status.
285 * This function never fails, a message is always returned.
286 */
287const char *get_reason(unsigned int status)
288{
289 switch (status) {
290 case 100: return "Continue";
291 case 101: return "Switching Protocols";
292 case 102: return "Processing";
293 case 200: return "OK";
294 case 201: return "Created";
295 case 202: return "Accepted";
296 case 203: return "Non-Authoritative Information";
297 case 204: return "No Content";
298 case 205: return "Reset Content";
299 case 206: return "Partial Content";
300 case 207: return "Multi-Status";
301 case 210: return "Content Different";
302 case 226: return "IM Used";
303 case 300: return "Multiple Choices";
304 case 301: return "Moved Permanently";
305 case 302: return "Moved Temporarily";
306 case 303: return "See Other";
307 case 304: return "Not Modified";
308 case 305: return "Use Proxy";
309 case 307: return "Temporary Redirect";
310 case 308: return "Permanent Redirect";
311 case 310: return "Too many Redirects";
312 case 400: return "Bad Request";
313 case 401: return "Unauthorized";
314 case 402: return "Payment Required";
315 case 403: return "Forbidden";
316 case 404: return "Not Found";
317 case 405: return "Method Not Allowed";
318 case 406: return "Not Acceptable";
319 case 407: return "Proxy Authentication Required";
320 case 408: return "Request Time-out";
321 case 409: return "Conflict";
322 case 410: return "Gone";
323 case 411: return "Length Required";
324 case 412: return "Precondition Failed";
325 case 413: return "Request Entity Too Large";
326 case 414: return "Request-URI Too Long";
327 case 415: return "Unsupported Media Type";
328 case 416: return "Requested range unsatisfiable";
329 case 417: return "Expectation failed";
330 case 418: return "I'm a teapot";
331 case 422: return "Unprocessable entity";
332 case 423: return "Locked";
333 case 424: return "Method failure";
334 case 425: return "Unordered Collection";
335 case 426: return "Upgrade Required";
336 case 428: return "Precondition Required";
337 case 429: return "Too Many Requests";
338 case 431: return "Request Header Fields Too Large";
339 case 449: return "Retry With";
340 case 450: return "Blocked by Windows Parental Controls";
341 case 451: return "Unavailable For Legal Reasons";
342 case 456: return "Unrecoverable Error";
343 case 499: return "client has closed connection";
344 case 500: return "Internal Server Error";
345 case 501: return "Not Implemented";
Jarno Huuskonen59af2df2016-12-28 10:49:01 +0200346 case 502: return "Bad Gateway or Proxy Error";
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +0200347 case 503: return "Service Unavailable";
348 case 504: return "Gateway Time-out";
349 case 505: return "HTTP Version not supported";
350 case 506: return "Variant also negociate";
351 case 507: return "Insufficient storage";
352 case 508: return "Loop detected";
353 case 509: return "Bandwidth Limit Exceeded";
354 case 510: return "Not extended";
355 case 511: return "Network authentication required";
356 case 520: return "Web server is returning an unknown error";
357 default:
358 switch (status) {
359 case 100 ... 199: return "Informational";
360 case 200 ... 299: return "Success";
361 case 300 ... 399: return "Redirection";
362 case 400 ... 499: return "Client Error";
363 case 500 ... 599: return "Server Error";
364 default: return "Other";
365 }
366 }
367}
368
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +0200369/* This function returns HTTP_ERR_<num> (enum) matching http status code.
370 * Returned value should match codes from http_err_codes.
371 */
372static const int http_get_status_idx(unsigned int status)
373{
374 switch (status) {
375 case 200: return HTTP_ERR_200;
376 case 400: return HTTP_ERR_400;
377 case 403: return HTTP_ERR_403;
378 case 405: return HTTP_ERR_405;
379 case 408: return HTTP_ERR_408;
380 case 429: return HTTP_ERR_429;
381 case 500: return HTTP_ERR_500;
382 case 502: return HTTP_ERR_502;
383 case 503: return HTTP_ERR_503;
384 case 504: return HTTP_ERR_504;
385 default: return HTTP_ERR_500;
386 }
387}
388
Willy Tarreau80587432006-12-24 17:47:20 +0100389void init_proto_http()
390{
Willy Tarreau42250582007-04-01 01:30:43 +0200391 int i;
392 char *tmp;
Willy Tarreau80587432006-12-24 17:47:20 +0100393 int msg;
Willy Tarreau42250582007-04-01 01:30:43 +0200394
Willy Tarreau80587432006-12-24 17:47:20 +0100395 for (msg = 0; msg < HTTP_ERR_SIZE; msg++) {
396 if (!http_err_msgs[msg]) {
397 Alert("Internal error: no message defined for HTTP return code %d. Aborting.\n", msg);
398 abort();
399 }
400
401 http_err_chunks[msg].str = (char *)http_err_msgs[msg];
402 http_err_chunks[msg].len = strlen(http_err_msgs[msg]);
403 }
Willy Tarreau42250582007-04-01 01:30:43 +0200404
405 /* initialize the log header encoding map : '{|}"#' should be encoded with
406 * '#' as prefix, as well as non-printable characters ( <32 or >= 127 ).
407 * URL encoding only requires '"', '#' to be encoded as well as non-
408 * printable characters above.
409 */
410 memset(hdr_encode_map, 0, sizeof(hdr_encode_map));
411 memset(url_encode_map, 0, sizeof(url_encode_map));
Thierry FOURNIERd048d8b2014-03-13 16:46:18 +0100412 memset(http_encode_map, 0, sizeof(url_encode_map));
Willy Tarreau42250582007-04-01 01:30:43 +0200413 for (i = 0; i < 32; i++) {
414 FD_SET(i, hdr_encode_map);
415 FD_SET(i, url_encode_map);
416 }
417 for (i = 127; i < 256; i++) {
418 FD_SET(i, hdr_encode_map);
419 FD_SET(i, url_encode_map);
420 }
421
422 tmp = "\"#{|}";
423 while (*tmp) {
424 FD_SET(*tmp, hdr_encode_map);
425 tmp++;
426 }
427
428 tmp = "\"#";
429 while (*tmp) {
430 FD_SET(*tmp, url_encode_map);
431 tmp++;
432 }
Willy Tarreau332f8bf2007-05-13 21:36:56 +0200433
Thierry FOURNIERd048d8b2014-03-13 16:46:18 +0100434 /* initialize the http header encoding map. The draft httpbis define the
435 * header content as:
436 *
437 * HTTP-message = start-line
438 * *( header-field CRLF )
439 * CRLF
440 * [ message-body ]
441 * header-field = field-name ":" OWS field-value OWS
442 * field-value = *( field-content / obs-fold )
443 * field-content = field-vchar [ 1*( SP / HTAB ) field-vchar ]
444 * obs-fold = CRLF 1*( SP / HTAB )
445 * field-vchar = VCHAR / obs-text
446 * VCHAR = %x21-7E
447 * obs-text = %x80-FF
448 *
449 * All the chars are encoded except "VCHAR", "obs-text", SP and HTAB.
450 * The encoded chars are form 0x00 to 0x08, 0x0a to 0x1f and 0x7f. The
451 * "obs-fold" is volontary forgotten because haproxy remove this.
452 */
453 memset(http_encode_map, 0, sizeof(http_encode_map));
454 for (i = 0x00; i <= 0x08; i++)
455 FD_SET(i, http_encode_map);
456 for (i = 0x0a; i <= 0x1f; i++)
457 FD_SET(i, http_encode_map);
458 FD_SET(0x7f, http_encode_map);
459
Willy Tarreau332f8bf2007-05-13 21:36:56 +0200460 /* memory allocations */
Willy Tarreau63986c72015-04-03 22:55:33 +0200461 pool2_http_txn = create_pool("http_txn", sizeof(struct http_txn), MEM_F_SHARED);
Willy Tarreau332f8bf2007-05-13 21:36:56 +0200462 pool2_requri = create_pool("requri", REQURI_LEN, MEM_F_SHARED);
William Lallemanda73203e2012-03-12 12:48:57 +0100463 pool2_uniqueid = create_pool("uniqueid", UNIQUEID_LEN, MEM_F_SHARED);
Willy Tarreau80587432006-12-24 17:47:20 +0100464}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200465
Willy Tarreau53b6c742006-12-17 13:37:46 +0100466/*
467 * We have 26 list of methods (1 per first letter), each of which can have
468 * up to 3 entries (2 valid, 1 null).
469 */
470struct http_method_desc {
Willy Tarreauc8987b32013-12-06 23:43:17 +0100471 enum http_meth_t meth;
Willy Tarreau53b6c742006-12-17 13:37:46 +0100472 int len;
473 const char text[8];
474};
475
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100476const struct http_method_desc http_methods[26][3] = {
Willy Tarreau53b6c742006-12-17 13:37:46 +0100477 ['C' - 'A'] = {
478 [0] = { .meth = HTTP_METH_CONNECT , .len=7, .text="CONNECT" },
479 },
480 ['D' - 'A'] = {
481 [0] = { .meth = HTTP_METH_DELETE , .len=6, .text="DELETE" },
482 },
483 ['G' - 'A'] = {
484 [0] = { .meth = HTTP_METH_GET , .len=3, .text="GET" },
485 },
486 ['H' - 'A'] = {
487 [0] = { .meth = HTTP_METH_HEAD , .len=4, .text="HEAD" },
488 },
Christopher Fauletd57ad642015-07-31 14:26:57 +0200489 ['O' - 'A'] = {
490 [0] = { .meth = HTTP_METH_OPTIONS , .len=7, .text="OPTIONS" },
491 },
Willy Tarreau53b6c742006-12-17 13:37:46 +0100492 ['P' - 'A'] = {
493 [0] = { .meth = HTTP_METH_POST , .len=4, .text="POST" },
494 [1] = { .meth = HTTP_METH_PUT , .len=3, .text="PUT" },
495 },
496 ['T' - 'A'] = {
497 [0] = { .meth = HTTP_METH_TRACE , .len=5, .text="TRACE" },
498 },
499 /* rest is empty like this :
Willy Tarreaub7ce4242015-09-03 17:15:21 +0200500 * [0] = { .meth = HTTP_METH_OTHER , .len=0, .text="" },
Willy Tarreau53b6c742006-12-17 13:37:46 +0100501 */
502};
503
Thierry FOURNIERd4373142013-12-17 01:10:10 +0100504const struct http_method_name http_known_methods[HTTP_METH_OTHER] = {
Thierry FOURNIERd4373142013-12-17 01:10:10 +0100505 [HTTP_METH_OPTIONS] = { "OPTIONS", 7 },
506 [HTTP_METH_GET] = { "GET", 3 },
507 [HTTP_METH_HEAD] = { "HEAD", 4 },
508 [HTTP_METH_POST] = { "POST", 4 },
509 [HTTP_METH_PUT] = { "PUT", 3 },
510 [HTTP_METH_DELETE] = { "DELETE", 6 },
511 [HTTP_METH_TRACE] = { "TRACE", 5 },
512 [HTTP_METH_CONNECT] = { "CONNECT", 7 },
513};
514
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100515/* It is about twice as fast on recent architectures to lookup a byte in a
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200516 * table than to perform a boolean AND or OR between two tests. Refer to
Willy Tarreau2235b262016-11-05 15:50:20 +0100517 * RFC2616/RFC5234/RFC7230 for those chars. A token is any ASCII char that is
518 * neither a separator nor a CTL char. An http ver_token is any ASCII which can
519 * be found in an HTTP version, which includes 'H', 'T', 'P', '/', '.' and any
520 * digit. Note: please do not overwrite values in assignment since gcc-2.95
521 * will not handle them correctly. It's worth noting that chars 128..255 are
522 * nothing, not even control chars.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100523 */
Willy Tarreau2235b262016-11-05 15:50:20 +0100524const unsigned char http_char_classes[256] = {
525 [ 0] = HTTP_FLG_CTL,
526 [ 1] = HTTP_FLG_CTL,
527 [ 2] = HTTP_FLG_CTL,
528 [ 3] = HTTP_FLG_CTL,
529 [ 4] = HTTP_FLG_CTL,
530 [ 5] = HTTP_FLG_CTL,
531 [ 6] = HTTP_FLG_CTL,
532 [ 7] = HTTP_FLG_CTL,
533 [ 8] = HTTP_FLG_CTL,
534 [ 9] = HTTP_FLG_SPHT | HTTP_FLG_LWS | HTTP_FLG_SEP | HTTP_FLG_CTL,
535 [ 10] = HTTP_FLG_CRLF | HTTP_FLG_LWS | HTTP_FLG_CTL,
536 [ 11] = HTTP_FLG_CTL,
537 [ 12] = HTTP_FLG_CTL,
538 [ 13] = HTTP_FLG_CRLF | HTTP_FLG_LWS | HTTP_FLG_CTL,
539 [ 14] = HTTP_FLG_CTL,
540 [ 15] = HTTP_FLG_CTL,
541 [ 16] = HTTP_FLG_CTL,
542 [ 17] = HTTP_FLG_CTL,
543 [ 18] = HTTP_FLG_CTL,
544 [ 19] = HTTP_FLG_CTL,
545 [ 20] = HTTP_FLG_CTL,
546 [ 21] = HTTP_FLG_CTL,
547 [ 22] = HTTP_FLG_CTL,
548 [ 23] = HTTP_FLG_CTL,
549 [ 24] = HTTP_FLG_CTL,
550 [ 25] = HTTP_FLG_CTL,
551 [ 26] = HTTP_FLG_CTL,
552 [ 27] = HTTP_FLG_CTL,
553 [ 28] = HTTP_FLG_CTL,
554 [ 29] = HTTP_FLG_CTL,
555 [ 30] = HTTP_FLG_CTL,
556 [ 31] = HTTP_FLG_CTL,
557 [' '] = HTTP_FLG_SPHT | HTTP_FLG_LWS | HTTP_FLG_SEP,
558 ['!'] = HTTP_FLG_TOK,
559 ['"'] = HTTP_FLG_SEP,
560 ['#'] = HTTP_FLG_TOK,
561 ['$'] = HTTP_FLG_TOK,
562 ['%'] = HTTP_FLG_TOK,
563 ['&'] = HTTP_FLG_TOK,
564 [ 39] = HTTP_FLG_TOK,
565 ['('] = HTTP_FLG_SEP,
566 [')'] = HTTP_FLG_SEP,
567 ['*'] = HTTP_FLG_TOK,
568 ['+'] = HTTP_FLG_TOK,
569 [','] = HTTP_FLG_SEP,
570 ['-'] = HTTP_FLG_TOK,
571 ['.'] = HTTP_FLG_TOK | HTTP_FLG_VER,
572 ['/'] = HTTP_FLG_SEP | HTTP_FLG_VER,
573 ['0'] = HTTP_FLG_TOK | HTTP_FLG_VER,
574 ['1'] = HTTP_FLG_TOK | HTTP_FLG_VER,
575 ['2'] = HTTP_FLG_TOK | HTTP_FLG_VER,
576 ['3'] = HTTP_FLG_TOK | HTTP_FLG_VER,
577 ['4'] = HTTP_FLG_TOK | HTTP_FLG_VER,
578 ['5'] = HTTP_FLG_TOK | HTTP_FLG_VER,
579 ['6'] = HTTP_FLG_TOK | HTTP_FLG_VER,
580 ['7'] = HTTP_FLG_TOK | HTTP_FLG_VER,
581 ['8'] = HTTP_FLG_TOK | HTTP_FLG_VER,
582 ['9'] = HTTP_FLG_TOK | HTTP_FLG_VER,
583 [':'] = HTTP_FLG_SEP,
584 [';'] = HTTP_FLG_SEP,
585 ['<'] = HTTP_FLG_SEP,
586 ['='] = HTTP_FLG_SEP,
587 ['>'] = HTTP_FLG_SEP,
588 ['?'] = HTTP_FLG_SEP,
589 ['@'] = HTTP_FLG_SEP,
590 ['A'] = HTTP_FLG_TOK,
591 ['B'] = HTTP_FLG_TOK,
592 ['C'] = HTTP_FLG_TOK,
593 ['D'] = HTTP_FLG_TOK,
594 ['E'] = HTTP_FLG_TOK,
595 ['F'] = HTTP_FLG_TOK,
596 ['G'] = HTTP_FLG_TOK,
597 ['H'] = HTTP_FLG_TOK | HTTP_FLG_VER,
598 ['I'] = HTTP_FLG_TOK,
599 ['J'] = HTTP_FLG_TOK,
600 ['K'] = HTTP_FLG_TOK,
601 ['L'] = HTTP_FLG_TOK,
602 ['M'] = HTTP_FLG_TOK,
603 ['N'] = HTTP_FLG_TOK,
604 ['O'] = HTTP_FLG_TOK,
605 ['P'] = HTTP_FLG_TOK | HTTP_FLG_VER,
606 ['Q'] = HTTP_FLG_TOK,
607 ['R'] = HTTP_FLG_TOK | HTTP_FLG_VER,
608 ['S'] = HTTP_FLG_TOK | HTTP_FLG_VER,
609 ['T'] = HTTP_FLG_TOK | HTTP_FLG_VER,
610 ['U'] = HTTP_FLG_TOK,
611 ['V'] = HTTP_FLG_TOK,
612 ['W'] = HTTP_FLG_TOK,
613 ['X'] = HTTP_FLG_TOK,
614 ['Y'] = HTTP_FLG_TOK,
615 ['Z'] = HTTP_FLG_TOK,
616 ['['] = HTTP_FLG_SEP,
617 [ 92] = HTTP_FLG_SEP,
618 [']'] = HTTP_FLG_SEP,
619 ['^'] = HTTP_FLG_TOK,
620 ['_'] = HTTP_FLG_TOK,
621 ['`'] = HTTP_FLG_TOK,
622 ['a'] = HTTP_FLG_TOK,
623 ['b'] = HTTP_FLG_TOK,
624 ['c'] = HTTP_FLG_TOK,
625 ['d'] = HTTP_FLG_TOK,
626 ['e'] = HTTP_FLG_TOK,
627 ['f'] = HTTP_FLG_TOK,
628 ['g'] = HTTP_FLG_TOK,
629 ['h'] = HTTP_FLG_TOK,
630 ['i'] = HTTP_FLG_TOK,
631 ['j'] = HTTP_FLG_TOK,
632 ['k'] = HTTP_FLG_TOK,
633 ['l'] = HTTP_FLG_TOK,
634 ['m'] = HTTP_FLG_TOK,
635 ['n'] = HTTP_FLG_TOK,
636 ['o'] = HTTP_FLG_TOK,
637 ['p'] = HTTP_FLG_TOK,
638 ['q'] = HTTP_FLG_TOK,
639 ['r'] = HTTP_FLG_TOK,
640 ['s'] = HTTP_FLG_TOK,
641 ['t'] = HTTP_FLG_TOK,
642 ['u'] = HTTP_FLG_TOK,
643 ['v'] = HTTP_FLG_TOK,
644 ['w'] = HTTP_FLG_TOK,
645 ['x'] = HTTP_FLG_TOK,
646 ['y'] = HTTP_FLG_TOK,
647 ['z'] = HTTP_FLG_TOK,
648 ['{'] = HTTP_FLG_SEP,
649 ['|'] = HTTP_FLG_TOK,
650 ['}'] = HTTP_FLG_SEP,
651 ['~'] = HTTP_FLG_TOK,
652 [127] = HTTP_FLG_CTL,
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100653};
654
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100655/*
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100656 * Adds a header and its CRLF at the tail of the message's buffer, just before
657 * the last CRLF. Text length is measured first, so it cannot be NULL.
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100658 * The header is also automatically added to the index <hdr_idx>, and the end
659 * of headers is automatically adjusted. The number of bytes added is returned
660 * on success, otherwise <0 is returned indicating an error.
661 */
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100662int http_header_add_tail(struct http_msg *msg, struct hdr_idx *hdr_idx, const char *text)
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100663{
664 int bytes, len;
665
666 len = strlen(text);
Willy Tarreau9b28e032012-10-12 23:49:43 +0200667 bytes = buffer_insert_line2(msg->chn->buf, msg->chn->buf->p + msg->eoh, text, len);
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100668 if (!bytes)
669 return -1;
Willy Tarreaufa355d42009-11-29 18:12:29 +0100670 http_msg_move_end(msg, bytes);
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100671 return hdr_idx_add(len, 1, hdr_idx, hdr_idx->tail);
672}
673
674/*
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100675 * Adds a header and its CRLF at the tail of the message's buffer, just before
676 * the last CRLF. <len> bytes are copied, not counting the CRLF. If <text> is NULL, then
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100677 * the buffer is only opened and the space reserved, but nothing is copied.
678 * The header is also automatically added to the index <hdr_idx>, and the end
679 * of headers is automatically adjusted. The number of bytes added is returned
680 * on success, otherwise <0 is returned indicating an error.
681 */
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100682int http_header_add_tail2(struct http_msg *msg,
683 struct hdr_idx *hdr_idx, const char *text, int len)
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100684{
685 int bytes;
686
Willy Tarreau9b28e032012-10-12 23:49:43 +0200687 bytes = buffer_insert_line2(msg->chn->buf, msg->chn->buf->p + msg->eoh, text, len);
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100688 if (!bytes)
689 return -1;
Willy Tarreaufa355d42009-11-29 18:12:29 +0100690 http_msg_move_end(msg, bytes);
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100691 return hdr_idx_add(len, 1, hdr_idx, hdr_idx->tail);
692}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200693
694/*
Willy Tarreauaa9dce32007-03-18 23:50:16 +0100695 * Checks if <hdr> is exactly <name> for <len> chars, and ends with a colon.
696 * If so, returns the position of the first non-space character relative to
697 * <hdr>, or <end>-<hdr> if not found before. If no value is found, it tries
698 * to return a pointer to the place after the first space. Returns 0 if the
699 * header name does not match. Checks are case-insensitive.
700 */
701int http_header_match2(const char *hdr, const char *end,
702 const char *name, int len)
703{
704 const char *val;
705
706 if (hdr + len >= end)
707 return 0;
708 if (hdr[len] != ':')
709 return 0;
710 if (strncasecmp(hdr, name, len) != 0)
711 return 0;
712 val = hdr + len + 1;
713 while (val < end && HTTP_IS_SPHT(*val))
714 val++;
715 if ((val >= end) && (len + 2 <= end - hdr))
716 return len + 2; /* we may replace starting from second space */
717 return val - hdr;
718}
719
Willy Tarreau04ff9f12013-06-10 18:39:42 +0200720/* Find the first or next occurrence of header <name> in message buffer <sol>
721 * using headers index <idx>, and return it in the <ctx> structure. This
722 * structure holds everything necessary to use the header and find next
723 * occurrence. If its <idx> member is 0, the header is searched from the
724 * beginning. Otherwise, the next occurrence is returned. The function returns
725 * 1 when it finds a value, and 0 when there is no more. It is very similar to
726 * http_find_header2() except that it is designed to work with full-line headers
727 * whose comma is not a delimiter but is part of the syntax. As a special case,
728 * if ctx->val is NULL when searching for a new values of a header, the current
729 * header is rescanned. This allows rescanning after a header deletion.
730 */
731int http_find_full_header2(const char *name, int len,
732 char *sol, struct hdr_idx *idx,
733 struct hdr_ctx *ctx)
734{
735 char *eol, *sov;
736 int cur_idx, old_idx;
737
738 cur_idx = ctx->idx;
739 if (cur_idx) {
740 /* We have previously returned a header, let's search another one */
741 sol = ctx->line;
742 eol = sol + idx->v[cur_idx].len;
743 goto next_hdr;
744 }
745
746 /* first request for this header */
747 sol += hdr_idx_first_pos(idx);
748 old_idx = 0;
749 cur_idx = hdr_idx_first_idx(idx);
750 while (cur_idx) {
751 eol = sol + idx->v[cur_idx].len;
752
753 if (len == 0) {
754 /* No argument was passed, we want any header.
755 * To achieve this, we simply build a fake request. */
756 while (sol + len < eol && sol[len] != ':')
757 len++;
758 name = sol;
759 }
760
761 if ((len < eol - sol) &&
762 (sol[len] == ':') &&
763 (strncasecmp(sol, name, len) == 0)) {
764 ctx->del = len;
765 sov = sol + len + 1;
Willy Tarreau2235b262016-11-05 15:50:20 +0100766 while (sov < eol && HTTP_IS_LWS(*sov))
Willy Tarreau04ff9f12013-06-10 18:39:42 +0200767 sov++;
768
769 ctx->line = sol;
770 ctx->prev = old_idx;
771 ctx->idx = cur_idx;
772 ctx->val = sov - sol;
773 ctx->tws = 0;
Willy Tarreau2235b262016-11-05 15:50:20 +0100774 while (eol > sov && HTTP_IS_LWS(*(eol - 1))) {
Willy Tarreau04ff9f12013-06-10 18:39:42 +0200775 eol--;
776 ctx->tws++;
777 }
778 ctx->vlen = eol - sov;
779 return 1;
780 }
781 next_hdr:
782 sol = eol + idx->v[cur_idx].cr + 1;
783 old_idx = cur_idx;
784 cur_idx = idx->v[cur_idx].next;
785 }
786 return 0;
787}
788
Willy Tarreauc90dc232015-02-20 13:51:36 +0100789/* Find the first or next header field in message buffer <sol> using headers
790 * index <idx>, and return it in the <ctx> structure. This structure holds
791 * everything necessary to use the header and find next occurrence. If its
792 * <idx> member is 0, the first header is retrieved. Otherwise, the next
793 * occurrence is returned. The function returns 1 when it finds a value, and
794 * 0 when there is no more. It is equivalent to http_find_full_header2() with
795 * no header name.
796 */
797int http_find_next_header(char *sol, struct hdr_idx *idx, struct hdr_ctx *ctx)
798{
799 char *eol, *sov;
800 int cur_idx, old_idx;
801 int len;
802
803 cur_idx = ctx->idx;
804 if (cur_idx) {
805 /* We have previously returned a header, let's search another one */
806 sol = ctx->line;
807 eol = sol + idx->v[cur_idx].len;
808 goto next_hdr;
809 }
810
811 /* first request for this header */
812 sol += hdr_idx_first_pos(idx);
813 old_idx = 0;
814 cur_idx = hdr_idx_first_idx(idx);
815 while (cur_idx) {
816 eol = sol + idx->v[cur_idx].len;
817
818 len = 0;
819 while (1) {
820 if (len >= eol - sol)
821 goto next_hdr;
822 if (sol[len] == ':')
823 break;
824 len++;
825 }
826
827 ctx->del = len;
828 sov = sol + len + 1;
Willy Tarreau2235b262016-11-05 15:50:20 +0100829 while (sov < eol && HTTP_IS_LWS(*sov))
Willy Tarreauc90dc232015-02-20 13:51:36 +0100830 sov++;
831
832 ctx->line = sol;
833 ctx->prev = old_idx;
834 ctx->idx = cur_idx;
835 ctx->val = sov - sol;
836 ctx->tws = 0;
837
Willy Tarreau2235b262016-11-05 15:50:20 +0100838 while (eol > sov && HTTP_IS_LWS(*(eol - 1))) {
Willy Tarreauc90dc232015-02-20 13:51:36 +0100839 eol--;
840 ctx->tws++;
841 }
842 ctx->vlen = eol - sov;
843 return 1;
844
845 next_hdr:
846 sol = eol + idx->v[cur_idx].cr + 1;
847 old_idx = cur_idx;
848 cur_idx = idx->v[cur_idx].next;
849 }
850 return 0;
851}
852
Willy Tarreau68085d82010-01-18 14:54:04 +0100853/* Find the end of the header value contained between <s> and <e>. See RFC2616,
854 * par 2.2 for more information. Note that it requires a valid header to return
855 * a valid result. This works for headers defined as comma-separated lists.
Willy Tarreau33a7e692007-06-10 19:45:56 +0200856 */
Willy Tarreau68085d82010-01-18 14:54:04 +0100857char *find_hdr_value_end(char *s, const char *e)
Willy Tarreau33a7e692007-06-10 19:45:56 +0200858{
859 int quoted, qdpair;
860
861 quoted = qdpair = 0;
Willy Tarreaue6d9c212016-11-05 18:23:38 +0100862
863#if defined(__x86_64__) || \
864 defined(__i386__) || defined(__i486__) || defined(__i586__) || defined(__i686__) || \
865 defined(__ARM_ARCH_7A__)
866 /* speedup: skip everything not a comma nor a double quote */
867 for (; s <= e - sizeof(int); s += sizeof(int)) {
868 unsigned int c = *(int *)s; // comma
869 unsigned int q = c; // quote
870
871 c ^= 0x2c2c2c2c; // contains one zero on a comma
872 q ^= 0x22222222; // contains one zero on a quote
873
874 c = (c - 0x01010101) & ~c; // contains 0x80 below a comma
875 q = (q - 0x01010101) & ~q; // contains 0x80 below a quote
876
877 if ((c | q) & 0x80808080)
878 break; // found a comma or a quote
879 }
880#endif
Willy Tarreau33a7e692007-06-10 19:45:56 +0200881 for (; s < e; s++) {
882 if (qdpair) qdpair = 0;
Willy Tarreau0f7f51f2010-08-30 11:06:34 +0200883 else if (quoted) {
884 if (*s == '\\') qdpair = 1;
885 else if (*s == '"') quoted = 0;
886 }
Willy Tarreau33a7e692007-06-10 19:45:56 +0200887 else if (*s == '"') quoted = 1;
888 else if (*s == ',') return s;
889 }
890 return s;
891}
892
893/* Find the first or next occurrence of header <name> in message buffer <sol>
894 * using headers index <idx>, and return it in the <ctx> structure. This
895 * structure holds everything necessary to use the header and find next
896 * occurrence. If its <idx> member is 0, the header is searched from the
897 * beginning. Otherwise, the next occurrence is returned. The function returns
Willy Tarreau68085d82010-01-18 14:54:04 +0100898 * 1 when it finds a value, and 0 when there is no more. It is designed to work
899 * with headers defined as comma-separated lists. As a special case, if ctx->val
900 * is NULL when searching for a new values of a header, the current header is
901 * rescanned. This allows rescanning after a header deletion.
Willy Tarreau33a7e692007-06-10 19:45:56 +0200902 */
903int http_find_header2(const char *name, int len,
Willy Tarreau68085d82010-01-18 14:54:04 +0100904 char *sol, struct hdr_idx *idx,
Willy Tarreau33a7e692007-06-10 19:45:56 +0200905 struct hdr_ctx *ctx)
906{
Willy Tarreau68085d82010-01-18 14:54:04 +0100907 char *eol, *sov;
908 int cur_idx, old_idx;
Willy Tarreau33a7e692007-06-10 19:45:56 +0200909
Willy Tarreau68085d82010-01-18 14:54:04 +0100910 cur_idx = ctx->idx;
911 if (cur_idx) {
Willy Tarreau33a7e692007-06-10 19:45:56 +0200912 /* We have previously returned a value, let's search
913 * another one on the same line.
914 */
Willy Tarreau33a7e692007-06-10 19:45:56 +0200915 sol = ctx->line;
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200916 ctx->del = ctx->val + ctx->vlen + ctx->tws;
Willy Tarreau68085d82010-01-18 14:54:04 +0100917 sov = sol + ctx->del;
Willy Tarreau33a7e692007-06-10 19:45:56 +0200918 eol = sol + idx->v[cur_idx].len;
919
920 if (sov >= eol)
921 /* no more values in this header */
922 goto next_hdr;
923
Willy Tarreau68085d82010-01-18 14:54:04 +0100924 /* values remaining for this header, skip the comma but save it
925 * for later use (eg: for header deletion).
926 */
Willy Tarreau33a7e692007-06-10 19:45:56 +0200927 sov++;
Willy Tarreau2235b262016-11-05 15:50:20 +0100928 while (sov < eol && HTTP_IS_LWS((*sov)))
Willy Tarreau33a7e692007-06-10 19:45:56 +0200929 sov++;
930
931 goto return_hdr;
932 }
933
934 /* first request for this header */
935 sol += hdr_idx_first_pos(idx);
Willy Tarreau68085d82010-01-18 14:54:04 +0100936 old_idx = 0;
Willy Tarreau33a7e692007-06-10 19:45:56 +0200937 cur_idx = hdr_idx_first_idx(idx);
Willy Tarreau33a7e692007-06-10 19:45:56 +0200938 while (cur_idx) {
939 eol = sol + idx->v[cur_idx].len;
940
Willy Tarreau1ad7c6d2007-06-10 21:42:55 +0200941 if (len == 0) {
942 /* No argument was passed, we want any header.
943 * To achieve this, we simply build a fake request. */
944 while (sol + len < eol && sol[len] != ':')
945 len++;
946 name = sol;
947 }
948
Willy Tarreau33a7e692007-06-10 19:45:56 +0200949 if ((len < eol - sol) &&
950 (sol[len] == ':') &&
951 (strncasecmp(sol, name, len) == 0)) {
Willy Tarreau68085d82010-01-18 14:54:04 +0100952 ctx->del = len;
Willy Tarreau33a7e692007-06-10 19:45:56 +0200953 sov = sol + len + 1;
Willy Tarreau2235b262016-11-05 15:50:20 +0100954 while (sov < eol && HTTP_IS_LWS(*sov))
Willy Tarreau33a7e692007-06-10 19:45:56 +0200955 sov++;
Willy Tarreau68085d82010-01-18 14:54:04 +0100956
Willy Tarreau33a7e692007-06-10 19:45:56 +0200957 ctx->line = sol;
Willy Tarreau68085d82010-01-18 14:54:04 +0100958 ctx->prev = old_idx;
959 return_hdr:
Willy Tarreau33a7e692007-06-10 19:45:56 +0200960 ctx->idx = cur_idx;
961 ctx->val = sov - sol;
962
963 eol = find_hdr_value_end(sov, eol);
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200964 ctx->tws = 0;
Willy Tarreau2235b262016-11-05 15:50:20 +0100965 while (eol > sov && HTTP_IS_LWS(*(eol - 1))) {
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200966 eol--;
967 ctx->tws++;
968 }
Willy Tarreau33a7e692007-06-10 19:45:56 +0200969 ctx->vlen = eol - sov;
970 return 1;
971 }
972 next_hdr:
973 sol = eol + idx->v[cur_idx].cr + 1;
Willy Tarreau68085d82010-01-18 14:54:04 +0100974 old_idx = cur_idx;
Willy Tarreau33a7e692007-06-10 19:45:56 +0200975 cur_idx = idx->v[cur_idx].next;
976 }
977 return 0;
978}
979
980int http_find_header(const char *name,
Willy Tarreau68085d82010-01-18 14:54:04 +0100981 char *sol, struct hdr_idx *idx,
Willy Tarreau33a7e692007-06-10 19:45:56 +0200982 struct hdr_ctx *ctx)
983{
984 return http_find_header2(name, strlen(name), sol, idx, ctx);
985}
986
Willy Tarreau68085d82010-01-18 14:54:04 +0100987/* Remove one value of a header. This only works on a <ctx> returned by one of
988 * the http_find_header functions. The value is removed, as well as surrounding
989 * commas if any. If the removed value was alone, the whole header is removed.
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100990 * The ctx is always updated accordingly, as well as the buffer and HTTP
Willy Tarreau68085d82010-01-18 14:54:04 +0100991 * message <msg>. The new index is returned. If it is zero, it means there is
992 * no more header, so any processing may stop. The ctx is always left in a form
993 * that can be handled by http_find_header2() to find next occurrence.
994 */
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100995int http_remove_header2(struct http_msg *msg, struct hdr_idx *idx, struct hdr_ctx *ctx)
Willy Tarreau68085d82010-01-18 14:54:04 +0100996{
997 int cur_idx = ctx->idx;
998 char *sol = ctx->line;
999 struct hdr_idx_elem *hdr;
1000 int delta, skip_comma;
1001
1002 if (!cur_idx)
1003 return 0;
1004
1005 hdr = &idx->v[cur_idx];
Willy Tarreau588bd4f2011-09-01 22:22:28 +02001006 if (sol[ctx->del] == ':' && ctx->val + ctx->vlen + ctx->tws == hdr->len) {
Willy Tarreau68085d82010-01-18 14:54:04 +01001007 /* This was the only value of the header, we must now remove it entirely. */
Willy Tarreau9b28e032012-10-12 23:49:43 +02001008 delta = buffer_replace2(msg->chn->buf, sol, sol + hdr->len + hdr->cr + 1, NULL, 0);
Willy Tarreau68085d82010-01-18 14:54:04 +01001009 http_msg_move_end(msg, delta);
1010 idx->used--;
1011 hdr->len = 0; /* unused entry */
1012 idx->v[ctx->prev].next = idx->v[ctx->idx].next;
Willy Tarreau5c4784f2011-02-12 13:07:35 +01001013 if (idx->tail == ctx->idx)
1014 idx->tail = ctx->prev;
Willy Tarreau68085d82010-01-18 14:54:04 +01001015 ctx->idx = ctx->prev; /* walk back to the end of previous header */
Willy Tarreau7c1c2172015-01-07 17:23:50 +01001016 ctx->line -= idx->v[ctx->idx].len + idx->v[ctx->idx].cr + 1;
Willy Tarreau68085d82010-01-18 14:54:04 +01001017 ctx->val = idx->v[ctx->idx].len; /* point to end of previous header */
Willy Tarreau588bd4f2011-09-01 22:22:28 +02001018 ctx->tws = ctx->vlen = 0;
Willy Tarreau68085d82010-01-18 14:54:04 +01001019 return ctx->idx;
1020 }
1021
1022 /* This was not the only value of this header. We have to remove between
Willy Tarreau588bd4f2011-09-01 22:22:28 +02001023 * ctx->del+1 and ctx->val+ctx->vlen+ctx->tws+1 included. If it is the
1024 * last entry of the list, we remove the last separator.
Willy Tarreau68085d82010-01-18 14:54:04 +01001025 */
1026
Willy Tarreau588bd4f2011-09-01 22:22:28 +02001027 skip_comma = (ctx->val + ctx->vlen + ctx->tws == hdr->len) ? 0 : 1;
Willy Tarreau9b28e032012-10-12 23:49:43 +02001028 delta = buffer_replace2(msg->chn->buf, sol + ctx->del + skip_comma,
Willy Tarreau588bd4f2011-09-01 22:22:28 +02001029 sol + ctx->val + ctx->vlen + ctx->tws + skip_comma,
Willy Tarreau68085d82010-01-18 14:54:04 +01001030 NULL, 0);
1031 hdr->len += delta;
1032 http_msg_move_end(msg, delta);
1033 ctx->val = ctx->del;
Willy Tarreau588bd4f2011-09-01 22:22:28 +02001034 ctx->tws = ctx->vlen = 0;
Willy Tarreau68085d82010-01-18 14:54:04 +01001035 return ctx->idx;
1036}
1037
Willy Tarreau2d3d94c2008-11-30 20:20:08 +01001038/* This function handles a server error at the stream interface level. The
1039 * stream interface is assumed to be already in a closed state. An optional
1040 * message is copied into the input buffer, and an HTTP status code stored.
1041 * The error flags are set to the values in arguments. Any pending request
Willy Tarreau6f0aa472009-03-08 20:33:29 +01001042 * in this buffer will be lost.
Willy Tarreaubaaee002006-06-26 02:48:02 +02001043 */
Willy Tarreau87b09662015-04-03 00:22:06 +02001044static void http_server_error(struct stream *s, struct stream_interface *si,
Willy Tarreau2d3d94c2008-11-30 20:20:08 +01001045 int err, int finst, int status, const struct chunk *msg)
Willy Tarreaubaaee002006-06-26 02:48:02 +02001046{
Christopher Faulet3e344292015-11-24 16:24:13 +01001047 FLT_STRM_CB(s, flt_http_reply(s, status, msg));
Willy Tarreau2bb4a962014-11-28 11:11:05 +01001048 channel_auto_read(si_oc(si));
1049 channel_abort(si_oc(si));
1050 channel_auto_close(si_oc(si));
1051 channel_erase(si_oc(si));
1052 channel_auto_close(si_ic(si));
1053 channel_auto_read(si_ic(si));
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02001054 if (status > 0)
Willy Tarreaueee5b512015-04-03 23:46:31 +02001055 s->txn->status = status;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02001056 if (msg)
Willy Tarreau2bb4a962014-11-28 11:11:05 +01001057 bo_inject(si_ic(si), msg->str, msg->len);
Willy Tarreaue7dff022015-04-03 01:14:29 +02001058 if (!(s->flags & SF_ERR_MASK))
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02001059 s->flags |= err;
Willy Tarreaue7dff022015-04-03 01:14:29 +02001060 if (!(s->flags & SF_FINST_MASK))
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02001061 s->flags |= finst;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001062}
1063
Willy Tarreau87b09662015-04-03 00:22:06 +02001064/* This function returns the appropriate error location for the given stream
Willy Tarreau80587432006-12-24 17:47:20 +01001065 * and message.
1066 */
1067
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02001068struct chunk *http_error_message(struct stream *s)
Willy Tarreau80587432006-12-24 17:47:20 +01001069{
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02001070 const int msgnum = http_get_status_idx(s->txn->status);
1071
Willy Tarreaue2e27a52007-04-01 00:01:37 +02001072 if (s->be->errmsg[msgnum].str)
1073 return &s->be->errmsg[msgnum];
Willy Tarreaud0d8da92015-04-04 02:10:38 +02001074 else if (strm_fe(s)->errmsg[msgnum].str)
1075 return &strm_fe(s)->errmsg[msgnum];
Willy Tarreau80587432006-12-24 17:47:20 +01001076 else
1077 return &http_err_chunks[msgnum];
1078}
Willy Tarreaubaaee002006-06-26 02:48:02 +02001079
Christopher Fauleta94e5a52015-12-09 15:55:06 +01001080void
1081http_reply_and_close(struct stream *s, short status, struct chunk *msg)
1082{
Christopher Fauletd7c91962015-04-30 11:48:27 +02001083 s->txn->flags &= ~TX_WAIT_NEXT_RQ;
Christopher Faulet3e344292015-11-24 16:24:13 +01001084 FLT_STRM_CB(s, flt_http_reply(s, status, msg));
Christopher Fauleta94e5a52015-12-09 15:55:06 +01001085 stream_int_retnclose(&s->si[0], msg);
1086}
1087
Willy Tarreau53b6c742006-12-17 13:37:46 +01001088/*
Willy Tarreaub7ce4242015-09-03 17:15:21 +02001089 * returns a known method among HTTP_METH_* or HTTP_METH_OTHER for all unknown
1090 * ones.
Willy Tarreau53b6c742006-12-17 13:37:46 +01001091 */
Thierry FOURNIERd4373142013-12-17 01:10:10 +01001092enum http_meth_t find_http_meth(const char *str, const int len)
Willy Tarreau53b6c742006-12-17 13:37:46 +01001093{
1094 unsigned char m;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001095 const struct http_method_desc *h;
Willy Tarreau53b6c742006-12-17 13:37:46 +01001096
1097 m = ((unsigned)*str - 'A');
1098
1099 if (m < 26) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001100 for (h = http_methods[m]; h->len > 0; h++) {
1101 if (unlikely(h->len != len))
Willy Tarreau53b6c742006-12-17 13:37:46 +01001102 continue;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001103 if (likely(memcmp(str, h->text, h->len) == 0))
Willy Tarreau53b6c742006-12-17 13:37:46 +01001104 return h->meth;
Willy Tarreau53b6c742006-12-17 13:37:46 +01001105 };
Willy Tarreau53b6c742006-12-17 13:37:46 +01001106 }
Willy Tarreaub7ce4242015-09-03 17:15:21 +02001107 return HTTP_METH_OTHER;
Willy Tarreau53b6c742006-12-17 13:37:46 +01001108}
1109
Willy Tarreau21d2af32008-02-14 20:25:24 +01001110/* Parse the URI from the given transaction (which is assumed to be in request
1111 * phase) and look for the "/" beginning the PATH. If not found, return NULL.
1112 * It is returned otherwise.
1113 */
Thierry FOURNIER3c331782015-09-17 19:33:35 +02001114char *http_get_path(struct http_txn *txn)
Willy Tarreau21d2af32008-02-14 20:25:24 +01001115{
1116 char *ptr, *end;
1117
Willy Tarreau9b28e032012-10-12 23:49:43 +02001118 ptr = txn->req.chn->buf->p + txn->req.sl.rq.u;
Willy Tarreau21d2af32008-02-14 20:25:24 +01001119 end = ptr + txn->req.sl.rq.u_l;
1120
1121 if (ptr >= end)
1122 return NULL;
1123
1124 /* RFC2616, par. 5.1.2 :
1125 * Request-URI = "*" | absuri | abspath | authority
1126 */
1127
1128 if (*ptr == '*')
1129 return NULL;
1130
1131 if (isalpha((unsigned char)*ptr)) {
1132 /* this is a scheme as described by RFC3986, par. 3.1 */
1133 ptr++;
1134 while (ptr < end &&
1135 (isalnum((unsigned char)*ptr) || *ptr == '+' || *ptr == '-' || *ptr == '.'))
1136 ptr++;
1137 /* skip '://' */
1138 if (ptr == end || *ptr++ != ':')
1139 return NULL;
1140 if (ptr == end || *ptr++ != '/')
1141 return NULL;
1142 if (ptr == end || *ptr++ != '/')
1143 return NULL;
1144 }
1145 /* skip [user[:passwd]@]host[:[port]] */
1146
1147 while (ptr < end && *ptr != '/')
1148 ptr++;
1149
1150 if (ptr == end)
1151 return NULL;
1152
1153 /* OK, we got the '/' ! */
1154 return ptr;
1155}
1156
William Lallemand65ad6e12014-01-31 15:08:02 +01001157/* Parse the URI from the given string and look for the "/" beginning the PATH.
1158 * If not found, return NULL. It is returned otherwise.
1159 */
1160static char *
1161http_get_path_from_string(char *str)
1162{
1163 char *ptr = str;
1164
1165 /* RFC2616, par. 5.1.2 :
1166 * Request-URI = "*" | absuri | abspath | authority
1167 */
1168
1169 if (*ptr == '*')
1170 return NULL;
1171
1172 if (isalpha((unsigned char)*ptr)) {
1173 /* this is a scheme as described by RFC3986, par. 3.1 */
1174 ptr++;
1175 while (isalnum((unsigned char)*ptr) || *ptr == '+' || *ptr == '-' || *ptr == '.')
1176 ptr++;
1177 /* skip '://' */
1178 if (*ptr == '\0' || *ptr++ != ':')
1179 return NULL;
1180 if (*ptr == '\0' || *ptr++ != '/')
1181 return NULL;
1182 if (*ptr == '\0' || *ptr++ != '/')
1183 return NULL;
1184 }
1185 /* skip [user[:passwd]@]host[:[port]] */
1186
1187 while (*ptr != '\0' && *ptr != ' ' && *ptr != '/')
1188 ptr++;
1189
1190 if (*ptr == '\0' || *ptr == ' ')
1191 return NULL;
1192
1193 /* OK, we got the '/' ! */
1194 return ptr;
1195}
1196
Willy Tarreau71241ab2012-12-27 11:30:54 +01001197/* Returns a 302 for a redirectable request that reaches a server working in
1198 * in redirect mode. This may only be called just after the stream interface
1199 * has moved to SI_ST_ASS. Unprocessable requests are left unchanged and will
1200 * follow normal proxy processing. NOTE: this function is designed to support
1201 * being called once data are scheduled for forwarding.
Willy Tarreauefb453c2008-10-26 20:49:47 +01001202 */
Willy Tarreau87b09662015-04-03 00:22:06 +02001203void http_perform_server_redirect(struct stream *s, struct stream_interface *si)
Willy Tarreauefb453c2008-10-26 20:49:47 +01001204{
1205 struct http_txn *txn;
Willy Tarreau827aee92011-03-10 16:55:02 +01001206 struct server *srv;
Willy Tarreauefb453c2008-10-26 20:49:47 +01001207 char *path;
Willy Tarreaucde18fc2012-05-30 07:59:54 +02001208 int len, rewind;
Willy Tarreauefb453c2008-10-26 20:49:47 +01001209
1210 /* 1: create the response header */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01001211 trash.len = strlen(HTTP_302);
1212 memcpy(trash.str, HTTP_302, trash.len);
Willy Tarreauefb453c2008-10-26 20:49:47 +01001213
Willy Tarreau3fdb3662012-11-12 00:42:33 +01001214 srv = objt_server(s->target);
Willy Tarreau827aee92011-03-10 16:55:02 +01001215
Willy Tarreauefb453c2008-10-26 20:49:47 +01001216 /* 2: add the server's prefix */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01001217 if (trash.len + srv->rdr_len > trash.size)
Willy Tarreauefb453c2008-10-26 20:49:47 +01001218 return;
1219
Willy Tarreaudcb75c42010-01-10 00:24:22 +01001220 /* special prefix "/" means don't change URL */
Willy Tarreau827aee92011-03-10 16:55:02 +01001221 if (srv->rdr_len != 1 || *srv->rdr_pfx != '/') {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01001222 memcpy(trash.str + trash.len, srv->rdr_pfx, srv->rdr_len);
1223 trash.len += srv->rdr_len;
Willy Tarreaudcb75c42010-01-10 00:24:22 +01001224 }
Willy Tarreauefb453c2008-10-26 20:49:47 +01001225
Willy Tarreaucde18fc2012-05-30 07:59:54 +02001226 /* 3: add the request URI. Since it was already forwarded, we need
1227 * to temporarily rewind the buffer.
1228 */
Willy Tarreaueee5b512015-04-03 23:46:31 +02001229 txn = s->txn;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01001230 b_rew(s->req.buf, rewind = http_hdr_rewind(&txn->req));
Willy Tarreaucde18fc2012-05-30 07:59:54 +02001231
Willy Tarreauefb453c2008-10-26 20:49:47 +01001232 path = http_get_path(txn);
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01001233 len = buffer_count(s->req.buf, path, b_ptr(s->req.buf, txn->req.sl.rq.u + txn->req.sl.rq.u_l));
Willy Tarreaucde18fc2012-05-30 07:59:54 +02001234
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01001235 b_adv(s->req.buf, rewind);
Willy Tarreaucde18fc2012-05-30 07:59:54 +02001236
Willy Tarreauefb453c2008-10-26 20:49:47 +01001237 if (!path)
1238 return;
1239
Willy Tarreau19d14ef2012-10-29 16:51:55 +01001240 if (trash.len + len > trash.size - 4) /* 4 for CRLF-CRLF */
Willy Tarreauefb453c2008-10-26 20:49:47 +01001241 return;
1242
Willy Tarreau19d14ef2012-10-29 16:51:55 +01001243 memcpy(trash.str + trash.len, path, len);
1244 trash.len += len;
Willy Tarreau88d349d2010-01-25 12:15:43 +01001245
1246 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01001247 memcpy(trash.str + trash.len, "\r\nProxy-Connection: close\r\n\r\n", 29);
1248 trash.len += 29;
Willy Tarreau88d349d2010-01-25 12:15:43 +01001249 } else {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01001250 memcpy(trash.str + trash.len, "\r\nConnection: close\r\n\r\n", 23);
1251 trash.len += 23;
Willy Tarreau88d349d2010-01-25 12:15:43 +01001252 }
Willy Tarreauefb453c2008-10-26 20:49:47 +01001253
1254 /* prepare to return without error. */
Willy Tarreau73b013b2012-05-21 16:31:45 +02001255 si_shutr(si);
1256 si_shutw(si);
Willy Tarreauefb453c2008-10-26 20:49:47 +01001257 si->err_type = SI_ET_NONE;
Willy Tarreauefb453c2008-10-26 20:49:47 +01001258 si->state = SI_ST_CLO;
1259
1260 /* send the message */
Willy Tarreaue7dff022015-04-03 01:14:29 +02001261 http_server_error(s, si, SF_ERR_LOCAL, SF_FINST_C, 302, &trash);
Willy Tarreauefb453c2008-10-26 20:49:47 +01001262
1263 /* FIXME: we should increase a counter of redirects per server and per backend. */
Willy Tarreau4521ba62013-01-24 01:25:25 +01001264 srv_inc_sess_ctr(srv);
Bhaskar Maddalaa20cb852014-02-03 16:26:46 -05001265 srv_set_sess_last(srv);
Willy Tarreauefb453c2008-10-26 20:49:47 +01001266}
1267
Willy Tarreau0cac36f2008-11-30 20:44:17 +01001268/* Return the error message corresponding to si->err_type. It is assumed
Willy Tarreauefb453c2008-10-26 20:49:47 +01001269 * that the server side is closed. Note that err_type is actually a
1270 * bitmask, where almost only aborts may be cumulated with other
1271 * values. We consider that aborted operations are more important
1272 * than timeouts or errors due to the fact that nobody else in the
1273 * logs might explain incomplete retries. All others should avoid
1274 * being cumulated. It should normally not be possible to have multiple
1275 * aborts at once, but just in case, the first one in sequence is reported.
Willy Tarreau6b726ad2013-12-15 19:31:37 +01001276 * Note that connection errors appearing on the second request of a keep-alive
1277 * connection are not reported since this allows the client to retry.
Willy Tarreauefb453c2008-10-26 20:49:47 +01001278 */
Willy Tarreau87b09662015-04-03 00:22:06 +02001279void http_return_srv_error(struct stream *s, struct stream_interface *si)
Willy Tarreauefb453c2008-10-26 20:49:47 +01001280{
Willy Tarreau0cac36f2008-11-30 20:44:17 +01001281 int err_type = si->err_type;
Willy Tarreauefb453c2008-10-26 20:49:47 +01001282
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02001283 /* set s->txn->status for http_error_message(s) */
1284 s->txn->status = 503;
1285
Willy Tarreauefb453c2008-10-26 20:49:47 +01001286 if (err_type & SI_ET_QUEUE_ABRT)
Willy Tarreaue7dff022015-04-03 01:14:29 +02001287 http_server_error(s, si, SF_ERR_CLICL, SF_FINST_Q,
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02001288 503, http_error_message(s));
Willy Tarreauefb453c2008-10-26 20:49:47 +01001289 else if (err_type & SI_ET_CONN_ABRT)
Willy Tarreaue7dff022015-04-03 01:14:29 +02001290 http_server_error(s, si, SF_ERR_CLICL, SF_FINST_C,
Willy Tarreaueee5b512015-04-03 23:46:31 +02001291 503, (s->txn->flags & TX_NOT_FIRST) ? NULL :
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02001292 http_error_message(s));
Willy Tarreauefb453c2008-10-26 20:49:47 +01001293 else if (err_type & SI_ET_QUEUE_TO)
Willy Tarreaue7dff022015-04-03 01:14:29 +02001294 http_server_error(s, si, SF_ERR_SRVTO, SF_FINST_Q,
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02001295 503, http_error_message(s));
Willy Tarreauefb453c2008-10-26 20:49:47 +01001296 else if (err_type & SI_ET_QUEUE_ERR)
Willy Tarreaue7dff022015-04-03 01:14:29 +02001297 http_server_error(s, si, SF_ERR_SRVCL, SF_FINST_Q,
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02001298 503, http_error_message(s));
Willy Tarreauefb453c2008-10-26 20:49:47 +01001299 else if (err_type & SI_ET_CONN_TO)
Willy Tarreaue7dff022015-04-03 01:14:29 +02001300 http_server_error(s, si, SF_ERR_SRVTO, SF_FINST_C,
Willy Tarreaueee5b512015-04-03 23:46:31 +02001301 503, (s->txn->flags & TX_NOT_FIRST) ? NULL :
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02001302 http_error_message(s));
Willy Tarreauefb453c2008-10-26 20:49:47 +01001303 else if (err_type & SI_ET_CONN_ERR)
Willy Tarreaue7dff022015-04-03 01:14:29 +02001304 http_server_error(s, si, SF_ERR_SRVCL, SF_FINST_C,
1305 503, (s->flags & SF_SRV_REUSED) ? NULL :
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02001306 http_error_message(s));
Willy Tarreau2d400bb2012-05-14 12:11:47 +02001307 else if (err_type & SI_ET_CONN_RES)
Willy Tarreaue7dff022015-04-03 01:14:29 +02001308 http_server_error(s, si, SF_ERR_RESOURCE, SF_FINST_C,
Willy Tarreaueee5b512015-04-03 23:46:31 +02001309 503, (s->txn->flags & TX_NOT_FIRST) ? NULL :
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02001310 http_error_message(s));
1311 else { /* SI_ET_CONN_OTHER and others */
1312 s->txn->status = 500;
Willy Tarreaue7dff022015-04-03 01:14:29 +02001313 http_server_error(s, si, SF_ERR_INTERNAL, SF_FINST_C,
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02001314 500, http_error_message(s));
1315 }
Willy Tarreauefb453c2008-10-26 20:49:47 +01001316}
1317
Willy Tarreau42250582007-04-01 01:30:43 +02001318extern const char sess_term_cond[8];
1319extern const char sess_fin_state[8];
1320extern const char *monthname[12];
Willy Tarreau63986c72015-04-03 22:55:33 +02001321struct pool_head *pool2_http_txn;
Willy Tarreau332f8bf2007-05-13 21:36:56 +02001322struct pool_head *pool2_requri;
Willy Tarreau193b8c62012-11-22 00:17:38 +01001323struct pool_head *pool2_capture = NULL;
William Lallemanda73203e2012-03-12 12:48:57 +01001324struct pool_head *pool2_uniqueid;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001325
Willy Tarreau117f59e2007-03-04 18:17:17 +01001326/*
1327 * Capture headers from message starting at <som> according to header list
Willy Tarreau54da8db2014-06-13 16:11:48 +02001328 * <cap_hdr>, and fill the <cap> pointers appropriately.
Willy Tarreau117f59e2007-03-04 18:17:17 +01001329 */
1330void capture_headers(char *som, struct hdr_idx *idx,
1331 char **cap, struct cap_hdr *cap_hdr)
1332{
1333 char *eol, *sol, *col, *sov;
1334 int cur_idx;
1335 struct cap_hdr *h;
1336 int len;
1337
1338 sol = som + hdr_idx_first_pos(idx);
1339 cur_idx = hdr_idx_first_idx(idx);
1340
1341 while (cur_idx) {
1342 eol = sol + idx->v[cur_idx].len;
1343
1344 col = sol;
1345 while (col < eol && *col != ':')
1346 col++;
1347
1348 sov = col + 1;
Willy Tarreau2235b262016-11-05 15:50:20 +01001349 while (sov < eol && HTTP_IS_LWS(*sov))
Willy Tarreau117f59e2007-03-04 18:17:17 +01001350 sov++;
1351
1352 for (h = cap_hdr; h; h = h->next) {
Willy Tarreau54da8db2014-06-13 16:11:48 +02001353 if (h->namelen && (h->namelen == col - sol) &&
Willy Tarreau117f59e2007-03-04 18:17:17 +01001354 (strncasecmp(sol, h->name, h->namelen) == 0)) {
1355 if (cap[h->index] == NULL)
1356 cap[h->index] =
Willy Tarreaucf7f3202007-05-13 22:46:04 +02001357 pool_alloc2(h->pool);
Willy Tarreau117f59e2007-03-04 18:17:17 +01001358
1359 if (cap[h->index] == NULL) {
1360 Alert("HTTP capture : out of memory.\n");
1361 continue;
1362 }
1363
1364 len = eol - sov;
1365 if (len > h->len)
1366 len = h->len;
1367
1368 memcpy(cap[h->index], sov, len);
1369 cap[h->index][len]=0;
1370 }
1371 }
1372 sol = eol + idx->v[cur_idx].cr + 1;
1373 cur_idx = idx->v[cur_idx].next;
1374 }
1375}
1376
1377
Willy Tarreau42250582007-04-01 01:30:43 +02001378/* either we find an LF at <ptr> or we jump to <bad>.
1379 */
Willy Tarreau10e61cb2017-01-04 14:51:22 +01001380#define EXPECT_LF_HERE(ptr, bad, st) do { if (unlikely(*(ptr) != '\n')) { state = st; goto bad;}; } while (0)
Willy Tarreau42250582007-04-01 01:30:43 +02001381
1382/* plays with variables <ptr>, <end> and <state>. Jumps to <good> if OK,
1383 * otherwise to <http_msg_ood> with <state> set to <st>.
1384 */
1385#define EAT_AND_JUMP_OR_RETURN(good, st) do { \
1386 ptr++; \
1387 if (likely(ptr < end)) \
1388 goto good; \
1389 else { \
1390 state = (st); \
1391 goto http_msg_ood; \
1392 } \
1393 } while (0)
1394
1395
Willy Tarreaubaaee002006-06-26 02:48:02 +02001396/*
Willy Tarreaua15645d2007-03-18 16:22:39 +01001397 * This function parses a status line between <ptr> and <end>, starting with
Willy Tarreau8973c702007-01-21 23:58:29 +01001398 * parser state <state>. Only states HTTP_MSG_RPVER, HTTP_MSG_RPVER_SP,
1399 * HTTP_MSG_RPCODE, HTTP_MSG_RPCODE_SP and HTTP_MSG_RPREASON are handled. Others
1400 * will give undefined results.
1401 * Note that it is upon the caller's responsibility to ensure that ptr < end,
1402 * and that msg->sol points to the beginning of the response.
1403 * If a complete line is found (which implies that at least one CR or LF is
1404 * found before <end>, the updated <ptr> is returned, otherwise NULL is
1405 * returned indicating an incomplete line (which does not mean that parts have
1406 * not been updated). In the incomplete case, if <ret_ptr> or <ret_state> are
1407 * non-NULL, they are fed with the new <ptr> and <state> values to be passed
1408 * upon next call.
1409 *
Willy Tarreau9cdde232007-05-02 20:58:19 +02001410 * This function was intentionally designed to be called from
Willy Tarreau8973c702007-01-21 23:58:29 +01001411 * http_msg_analyzer() with the lowest overhead. It should integrate perfectly
1412 * within its state machine and use the same macros, hence the need for same
Willy Tarreau9cdde232007-05-02 20:58:19 +02001413 * labels and variable names. Note that msg->sol is left unchanged.
Willy Tarreau8973c702007-01-21 23:58:29 +01001414 */
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02001415const char *http_parse_stsline(struct http_msg *msg,
Willy Tarreau3770f232013-12-07 00:01:53 +01001416 enum ht_state state, const char *ptr, const char *end,
1417 unsigned int *ret_ptr, enum ht_state *ret_state)
Willy Tarreau8973c702007-01-21 23:58:29 +01001418{
Willy Tarreau9b28e032012-10-12 23:49:43 +02001419 const char *msg_start = msg->chn->buf->p;
Willy Tarreau62f791e2012-03-09 11:32:30 +01001420
Willy Tarreau8973c702007-01-21 23:58:29 +01001421 switch (state) {
Willy Tarreau8973c702007-01-21 23:58:29 +01001422 case HTTP_MSG_RPVER:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001423 http_msg_rpver:
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001424 if (likely(HTTP_IS_VER_TOKEN(*ptr)))
Willy Tarreau8973c702007-01-21 23:58:29 +01001425 EAT_AND_JUMP_OR_RETURN(http_msg_rpver, HTTP_MSG_RPVER);
1426
1427 if (likely(HTTP_IS_SPHT(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001428 msg->sl.st.v_l = ptr - msg_start;
Willy Tarreau8973c702007-01-21 23:58:29 +01001429 EAT_AND_JUMP_OR_RETURN(http_msg_rpver_sp, HTTP_MSG_RPVER_SP);
1430 }
Willy Tarreau10e61cb2017-01-04 14:51:22 +01001431 msg->err_state = HTTP_MSG_RPVER;
Willy Tarreau7552c032009-03-01 11:10:40 +01001432 state = HTTP_MSG_ERROR;
1433 break;
1434
Willy Tarreau8973c702007-01-21 23:58:29 +01001435 case HTTP_MSG_RPVER_SP:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001436 http_msg_rpver_sp:
Willy Tarreau8973c702007-01-21 23:58:29 +01001437 if (likely(!HTTP_IS_LWS(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001438 msg->sl.st.c = ptr - msg_start;
Willy Tarreau8973c702007-01-21 23:58:29 +01001439 goto http_msg_rpcode;
1440 }
1441 if (likely(HTTP_IS_SPHT(*ptr)))
1442 EAT_AND_JUMP_OR_RETURN(http_msg_rpver_sp, HTTP_MSG_RPVER_SP);
1443 /* so it's a CR/LF, this is invalid */
Willy Tarreau10e61cb2017-01-04 14:51:22 +01001444 msg->err_state = HTTP_MSG_RPVER_SP;
Willy Tarreau7552c032009-03-01 11:10:40 +01001445 state = HTTP_MSG_ERROR;
1446 break;
Willy Tarreau8973c702007-01-21 23:58:29 +01001447
Willy Tarreau8973c702007-01-21 23:58:29 +01001448 case HTTP_MSG_RPCODE:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001449 http_msg_rpcode:
Willy Tarreau8973c702007-01-21 23:58:29 +01001450 if (likely(!HTTP_IS_LWS(*ptr)))
1451 EAT_AND_JUMP_OR_RETURN(http_msg_rpcode, HTTP_MSG_RPCODE);
1452
1453 if (likely(HTTP_IS_SPHT(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001454 msg->sl.st.c_l = ptr - msg_start - msg->sl.st.c;
Willy Tarreau8973c702007-01-21 23:58:29 +01001455 EAT_AND_JUMP_OR_RETURN(http_msg_rpcode_sp, HTTP_MSG_RPCODE_SP);
1456 }
1457
1458 /* so it's a CR/LF, so there is no reason phrase */
Willy Tarreauea1175a2012-03-05 15:52:30 +01001459 msg->sl.st.c_l = ptr - msg_start - msg->sl.st.c;
Willy Tarreau8973c702007-01-21 23:58:29 +01001460 http_msg_rsp_reason:
1461 /* FIXME: should we support HTTP responses without any reason phrase ? */
Willy Tarreauea1175a2012-03-05 15:52:30 +01001462 msg->sl.st.r = ptr - msg_start;
Willy Tarreau8973c702007-01-21 23:58:29 +01001463 msg->sl.st.r_l = 0;
1464 goto http_msg_rpline_eol;
1465
Willy Tarreau8973c702007-01-21 23:58:29 +01001466 case HTTP_MSG_RPCODE_SP:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001467 http_msg_rpcode_sp:
Willy Tarreau8973c702007-01-21 23:58:29 +01001468 if (likely(!HTTP_IS_LWS(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001469 msg->sl.st.r = ptr - msg_start;
Willy Tarreau8973c702007-01-21 23:58:29 +01001470 goto http_msg_rpreason;
1471 }
1472 if (likely(HTTP_IS_SPHT(*ptr)))
1473 EAT_AND_JUMP_OR_RETURN(http_msg_rpcode_sp, HTTP_MSG_RPCODE_SP);
1474 /* so it's a CR/LF, so there is no reason phrase */
1475 goto http_msg_rsp_reason;
1476
Willy Tarreau8973c702007-01-21 23:58:29 +01001477 case HTTP_MSG_RPREASON:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001478 http_msg_rpreason:
Willy Tarreau8973c702007-01-21 23:58:29 +01001479 if (likely(!HTTP_IS_CRLF(*ptr)))
1480 EAT_AND_JUMP_OR_RETURN(http_msg_rpreason, HTTP_MSG_RPREASON);
Willy Tarreauea1175a2012-03-05 15:52:30 +01001481 msg->sl.st.r_l = ptr - msg_start - msg->sl.st.r;
Willy Tarreau8973c702007-01-21 23:58:29 +01001482 http_msg_rpline_eol:
1483 /* We have seen the end of line. Note that we do not
1484 * necessarily have the \n yet, but at least we know that we
1485 * have EITHER \r OR \n, otherwise the response would not be
1486 * complete. We can then record the response length and return
1487 * to the caller which will be able to register it.
1488 */
Willy Tarreau3a215be2012-03-09 21:39:51 +01001489 msg->sl.st.l = ptr - msg_start - msg->sol;
Willy Tarreau8973c702007-01-21 23:58:29 +01001490 return ptr;
1491
Willy Tarreau8973c702007-01-21 23:58:29 +01001492 default:
Willy Tarreau3770f232013-12-07 00:01:53 +01001493#ifdef DEBUG_FULL
Willy Tarreau8973c702007-01-21 23:58:29 +01001494 fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state);
1495 exit(1);
1496#endif
Willy Tarreau3770f232013-12-07 00:01:53 +01001497 ;
Willy Tarreau8973c702007-01-21 23:58:29 +01001498 }
1499
1500 http_msg_ood:
Willy Tarreau7552c032009-03-01 11:10:40 +01001501 /* out of valid data */
Willy Tarreau8973c702007-01-21 23:58:29 +01001502 if (ret_state)
1503 *ret_state = state;
1504 if (ret_ptr)
Willy Tarreaua458b672012-03-05 11:17:50 +01001505 *ret_ptr = ptr - msg_start;
Willy Tarreau8973c702007-01-21 23:58:29 +01001506 return NULL;
Willy Tarreau8973c702007-01-21 23:58:29 +01001507}
1508
Willy Tarreau8973c702007-01-21 23:58:29 +01001509/*
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001510 * This function parses a request line between <ptr> and <end>, starting with
1511 * parser state <state>. Only states HTTP_MSG_RQMETH, HTTP_MSG_RQMETH_SP,
1512 * HTTP_MSG_RQURI, HTTP_MSG_RQURI_SP and HTTP_MSG_RQVER are handled. Others
1513 * will give undefined results.
1514 * Note that it is upon the caller's responsibility to ensure that ptr < end,
1515 * and that msg->sol points to the beginning of the request.
1516 * If a complete line is found (which implies that at least one CR or LF is
1517 * found before <end>, the updated <ptr> is returned, otherwise NULL is
1518 * returned indicating an incomplete line (which does not mean that parts have
1519 * not been updated). In the incomplete case, if <ret_ptr> or <ret_state> are
1520 * non-NULL, they are fed with the new <ptr> and <state> values to be passed
1521 * upon next call.
1522 *
Willy Tarreau9cdde232007-05-02 20:58:19 +02001523 * This function was intentionally designed to be called from
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001524 * http_msg_analyzer() with the lowest overhead. It should integrate perfectly
1525 * within its state machine and use the same macros, hence the need for same
Willy Tarreau9cdde232007-05-02 20:58:19 +02001526 * labels and variable names. Note that msg->sol is left unchanged.
Willy Tarreaubaaee002006-06-26 02:48:02 +02001527 */
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02001528const char *http_parse_reqline(struct http_msg *msg,
Willy Tarreau3770f232013-12-07 00:01:53 +01001529 enum ht_state state, const char *ptr, const char *end,
1530 unsigned int *ret_ptr, enum ht_state *ret_state)
Willy Tarreaubaaee002006-06-26 02:48:02 +02001531{
Willy Tarreau9b28e032012-10-12 23:49:43 +02001532 const char *msg_start = msg->chn->buf->p;
Willy Tarreau62f791e2012-03-09 11:32:30 +01001533
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001534 switch (state) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001535 case HTTP_MSG_RQMETH:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001536 http_msg_rqmeth:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001537 if (likely(HTTP_IS_TOKEN(*ptr)))
1538 EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth, HTTP_MSG_RQMETH);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001539
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001540 if (likely(HTTP_IS_SPHT(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001541 msg->sl.rq.m_l = ptr - msg_start;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001542 EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth_sp, HTTP_MSG_RQMETH_SP);
1543 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001544
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001545 if (likely(HTTP_IS_CRLF(*ptr))) {
1546 /* HTTP 0.9 request */
Willy Tarreauea1175a2012-03-05 15:52:30 +01001547 msg->sl.rq.m_l = ptr - msg_start;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001548 http_msg_req09_uri:
Willy Tarreauea1175a2012-03-05 15:52:30 +01001549 msg->sl.rq.u = ptr - msg_start;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001550 http_msg_req09_uri_e:
Willy Tarreauea1175a2012-03-05 15:52:30 +01001551 msg->sl.rq.u_l = ptr - msg_start - msg->sl.rq.u;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001552 http_msg_req09_ver:
Willy Tarreauea1175a2012-03-05 15:52:30 +01001553 msg->sl.rq.v = ptr - msg_start;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001554 msg->sl.rq.v_l = 0;
1555 goto http_msg_rqline_eol;
1556 }
Willy Tarreau10e61cb2017-01-04 14:51:22 +01001557 msg->err_state = HTTP_MSG_RQMETH;
Willy Tarreau7552c032009-03-01 11:10:40 +01001558 state = HTTP_MSG_ERROR;
1559 break;
1560
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001561 case HTTP_MSG_RQMETH_SP:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001562 http_msg_rqmeth_sp:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001563 if (likely(!HTTP_IS_LWS(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001564 msg->sl.rq.u = ptr - msg_start;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001565 goto http_msg_rquri;
1566 }
1567 if (likely(HTTP_IS_SPHT(*ptr)))
1568 EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth_sp, HTTP_MSG_RQMETH_SP);
1569 /* so it's a CR/LF, meaning an HTTP 0.9 request */
1570 goto http_msg_req09_uri;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001571
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001572 case HTTP_MSG_RQURI:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001573 http_msg_rquri:
Willy Tarreau5f10ea32016-11-05 17:52:06 +01001574#if defined(__x86_64__) || \
1575 defined(__i386__) || defined(__i486__) || defined(__i586__) || defined(__i686__) || \
1576 defined(__ARM_ARCH_7A__)
1577 /* speedup: skip bytes not between 0x21 and 0x7e inclusive */
1578 while (ptr <= end - sizeof(int)) {
1579 int x = *(int *)ptr - 0x21212121;
1580 if (x & 0x80808080)
1581 break;
1582
1583 x -= 0x5e5e5e5e;
1584 if (!(x & 0x80808080))
1585 break;
1586
1587 ptr += sizeof(int);
1588 }
1589#endif
Willy Tarreau2afff9c2017-01-04 14:44:46 +01001590 if (ptr >= end) {
1591 state = HTTP_MSG_RQURI;
1592 goto http_msg_ood;
1593 }
Willy Tarreau5f10ea32016-11-05 17:52:06 +01001594 http_msg_rquri2:
Willy Tarreau2e9506d2012-01-07 23:22:31 +01001595 if (likely((unsigned char)(*ptr - 33) <= 93)) /* 33 to 126 included */
Willy Tarreau5f10ea32016-11-05 17:52:06 +01001596 EAT_AND_JUMP_OR_RETURN(http_msg_rquri2, HTTP_MSG_RQURI);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001597
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001598 if (likely(HTTP_IS_SPHT(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001599 msg->sl.rq.u_l = ptr - msg_start - msg->sl.rq.u;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001600 EAT_AND_JUMP_OR_RETURN(http_msg_rquri_sp, HTTP_MSG_RQURI_SP);
1601 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001602
Willy Tarreau2e9506d2012-01-07 23:22:31 +01001603 if (likely((unsigned char)*ptr >= 128)) {
Willy Tarreau422246e2012-01-07 23:54:13 +01001604 /* non-ASCII chars are forbidden unless option
1605 * accept-invalid-http-request is enabled in the frontend.
1606 * In any case, we capture the faulty char.
Willy Tarreau2e9506d2012-01-07 23:22:31 +01001607 */
Willy Tarreau422246e2012-01-07 23:54:13 +01001608 if (msg->err_pos < -1)
1609 goto invalid_char;
1610 if (msg->err_pos == -1)
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02001611 msg->err_pos = ptr - msg_start;
Willy Tarreau2e9506d2012-01-07 23:22:31 +01001612 EAT_AND_JUMP_OR_RETURN(http_msg_rquri, HTTP_MSG_RQURI);
1613 }
1614
1615 if (likely(HTTP_IS_CRLF(*ptr))) {
1616 /* so it's a CR/LF, meaning an HTTP 0.9 request */
1617 goto http_msg_req09_uri_e;
1618 }
1619
1620 /* OK forbidden chars, 0..31 or 127 */
Willy Tarreau422246e2012-01-07 23:54:13 +01001621 invalid_char:
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02001622 msg->err_pos = ptr - msg_start;
Willy Tarreau10e61cb2017-01-04 14:51:22 +01001623 msg->err_state = HTTP_MSG_RQURI;
Willy Tarreau2e9506d2012-01-07 23:22:31 +01001624 state = HTTP_MSG_ERROR;
1625 break;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001626
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001627 case HTTP_MSG_RQURI_SP:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001628 http_msg_rquri_sp:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001629 if (likely(!HTTP_IS_LWS(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001630 msg->sl.rq.v = ptr - msg_start;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001631 goto http_msg_rqver;
1632 }
1633 if (likely(HTTP_IS_SPHT(*ptr)))
1634 EAT_AND_JUMP_OR_RETURN(http_msg_rquri_sp, HTTP_MSG_RQURI_SP);
1635 /* so it's a CR/LF, meaning an HTTP 0.9 request */
1636 goto http_msg_req09_ver;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001637
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001638 case HTTP_MSG_RQVER:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001639 http_msg_rqver:
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001640 if (likely(HTTP_IS_VER_TOKEN(*ptr)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001641 EAT_AND_JUMP_OR_RETURN(http_msg_rqver, HTTP_MSG_RQVER);
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001642
1643 if (likely(HTTP_IS_CRLF(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001644 msg->sl.rq.v_l = ptr - msg_start - msg->sl.rq.v;
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001645 http_msg_rqline_eol:
1646 /* We have seen the end of line. Note that we do not
1647 * necessarily have the \n yet, but at least we know that we
1648 * have EITHER \r OR \n, otherwise the request would not be
1649 * complete. We can then record the request length and return
1650 * to the caller which will be able to register it.
1651 */
Willy Tarreau3a215be2012-03-09 21:39:51 +01001652 msg->sl.rq.l = ptr - msg_start - msg->sol;
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001653 return ptr;
1654 }
1655
1656 /* neither an HTTP_VER token nor a CRLF */
Willy Tarreau10e61cb2017-01-04 14:51:22 +01001657 msg->err_state = HTTP_MSG_RQVER;
Willy Tarreau7552c032009-03-01 11:10:40 +01001658 state = HTTP_MSG_ERROR;
1659 break;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001660
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001661 default:
Willy Tarreau3770f232013-12-07 00:01:53 +01001662#ifdef DEBUG_FULL
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001663 fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state);
1664 exit(1);
1665#endif
Willy Tarreau3770f232013-12-07 00:01:53 +01001666 ;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001667 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001668
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001669 http_msg_ood:
Willy Tarreau7552c032009-03-01 11:10:40 +01001670 /* out of valid data */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001671 if (ret_state)
1672 *ret_state = state;
1673 if (ret_ptr)
Willy Tarreaua458b672012-03-05 11:17:50 +01001674 *ret_ptr = ptr - msg_start;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001675 return NULL;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001676}
Willy Tarreau58f10d72006-12-04 02:26:12 +01001677
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001678/*
1679 * Returns the data from Authorization header. Function may be called more
1680 * than once so data is stored in txn->auth_data. When no header is found
1681 * or auth method is unknown auth_method is set to HTTP_AUTH_WRONG to avoid
Thierry FOURNIER98d96952014-01-23 12:13:02 +01001682 * searching again for something we are unable to find anyway. However, if
1683 * the result if valid, the cache is not reused because we would risk to
Willy Tarreau87b09662015-04-03 00:22:06 +02001684 * have the credentials overwritten by another stream in parallel.
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001685 */
1686
Thierry FOURNIER9eec0a62014-01-22 18:38:02 +01001687/* This bufffer is initialized in the file 'src/haproxy.c'. This length is
1688 * set according to global.tune.bufsize.
1689 */
Willy Tarreau7e2c6472012-10-29 20:44:36 +01001690char *get_http_auth_buff;
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001691
1692int
Willy Tarreau87b09662015-04-03 00:22:06 +02001693get_http_auth(struct stream *s)
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001694{
1695
Willy Tarreaueee5b512015-04-03 23:46:31 +02001696 struct http_txn *txn = s->txn;
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001697 struct chunk auth_method;
1698 struct hdr_ctx ctx;
1699 char *h, *p;
1700 int len;
1701
1702#ifdef DEBUG_AUTH
Willy Tarreau87b09662015-04-03 00:22:06 +02001703 printf("Auth for stream %p: %d\n", s, txn->auth.method);
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001704#endif
1705
1706 if (txn->auth.method == HTTP_AUTH_WRONG)
1707 return 0;
1708
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001709 txn->auth.method = HTTP_AUTH_WRONG;
1710
1711 ctx.idx = 0;
Willy Tarreau844a7e72010-01-31 21:46:18 +01001712
1713 if (txn->flags & TX_USE_PX_CONN) {
1714 h = "Proxy-Authorization";
1715 len = strlen(h);
1716 } else {
1717 h = "Authorization";
1718 len = strlen(h);
1719 }
1720
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01001721 if (!http_find_header2(h, len, s->req.buf->p, &txn->hdr_idx, &ctx))
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001722 return 0;
1723
1724 h = ctx.line + ctx.val;
1725
1726 p = memchr(h, ' ', ctx.vlen);
Willy Tarreau5c557d12016-03-13 08:17:02 +01001727 len = p - h;
1728 if (!p || len <= 0)
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001729 return 0;
1730
David Carlier7365f7d2016-04-04 11:54:42 +01001731 if (chunk_initlen(&auth_method, h, 0, len) != 1)
1732 return 0;
1733
Willy Tarreau5c557d12016-03-13 08:17:02 +01001734 chunk_initlen(&txn->auth.method_data, p + 1, 0, ctx.vlen - len - 1);
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001735
1736 if (!strncasecmp("Basic", auth_method.str, auth_method.len)) {
1737
1738 len = base64dec(txn->auth.method_data.str, txn->auth.method_data.len,
Willy Tarreau7e2c6472012-10-29 20:44:36 +01001739 get_http_auth_buff, global.tune.bufsize - 1);
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001740
1741 if (len < 0)
1742 return 0;
1743
1744
1745 get_http_auth_buff[len] = '\0';
1746
1747 p = strchr(get_http_auth_buff, ':');
1748
1749 if (!p)
1750 return 0;
1751
1752 txn->auth.user = get_http_auth_buff;
1753 *p = '\0';
1754 txn->auth.pass = p+1;
1755
1756 txn->auth.method = HTTP_AUTH_BASIC;
1757 return 1;
1758 }
1759
1760 return 0;
1761}
1762
Willy Tarreau58f10d72006-12-04 02:26:12 +01001763
Willy Tarreau8973c702007-01-21 23:58:29 +01001764/*
1765 * This function parses an HTTP message, either a request or a response,
Willy Tarreau8b1323e2012-03-09 14:46:19 +01001766 * depending on the initial msg->msg_state. The caller is responsible for
1767 * ensuring that the message does not wrap. The function can be preempted
1768 * everywhere when data are missing and recalled at the exact same location
1769 * with no information loss. The message may even be realigned between two
1770 * calls. The header index is re-initialized when switching from
Willy Tarreau9cdde232007-05-02 20:58:19 +02001771 * MSG_R[PQ]BEFORE to MSG_RPVER|MSG_RQMETH. It modifies msg->sol among other
Willy Tarreau26927362012-05-18 23:22:52 +02001772 * fields. Note that msg->sol will be initialized after completing the first
1773 * state, so that none of the msg pointers has to be initialized prior to the
1774 * first call.
Willy Tarreau8973c702007-01-21 23:58:29 +01001775 */
Willy Tarreaua560c212012-03-09 13:50:57 +01001776void http_msg_analyzer(struct http_msg *msg, struct hdr_idx *idx)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001777{
Willy Tarreau3770f232013-12-07 00:01:53 +01001778 enum ht_state state; /* updated only when leaving the FSM */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001779 register char *ptr, *end; /* request pointers, to avoid dereferences */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001780 struct buffer *buf;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001781
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001782 state = msg->msg_state;
Willy Tarreau9b28e032012-10-12 23:49:43 +02001783 buf = msg->chn->buf;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001784 ptr = buf->p + msg->next;
1785 end = buf->p + buf->i;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001786
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001787 if (unlikely(ptr >= end))
1788 goto http_msg_ood;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001789
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001790 switch (state) {
Willy Tarreau8973c702007-01-21 23:58:29 +01001791 /*
1792 * First, states that are specific to the response only.
1793 * We check them first so that request and headers are
1794 * closer to each other (accessed more often).
1795 */
Willy Tarreau8973c702007-01-21 23:58:29 +01001796 case HTTP_MSG_RPBEFORE:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001797 http_msg_rpbefore:
Willy Tarreau8973c702007-01-21 23:58:29 +01001798 if (likely(HTTP_IS_TOKEN(*ptr))) {
Willy Tarreau15de77e2010-01-02 21:59:16 +01001799 /* we have a start of message, but we have to check
1800 * first if we need to remove some CRLF. We can only
Willy Tarreau2e046c62012-03-01 16:08:30 +01001801 * do this when o=0.
Willy Tarreau15de77e2010-01-02 21:59:16 +01001802 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001803 if (unlikely(ptr != buf->p)) {
1804 if (buf->o)
Willy Tarreau15de77e2010-01-02 21:59:16 +01001805 goto http_msg_ood;
Willy Tarreau1d3bcce2009-12-27 15:50:06 +01001806 /* Remove empty leading lines, as recommended by RFC2616. */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001807 bi_fast_delete(buf, ptr - buf->p);
Willy Tarreau8973c702007-01-21 23:58:29 +01001808 }
Willy Tarreau26927362012-05-18 23:22:52 +02001809 msg->sol = 0;
Willy Tarreaue92693a2012-09-24 21:13:39 +02001810 msg->sl.st.l = 0; /* used in debug mode */
Willy Tarreau8973c702007-01-21 23:58:29 +01001811 hdr_idx_init(idx);
1812 state = HTTP_MSG_RPVER;
1813 goto http_msg_rpver;
1814 }
1815
Willy Tarreau10e61cb2017-01-04 14:51:22 +01001816 if (unlikely(!HTTP_IS_CRLF(*ptr))) {
1817 state = HTTP_MSG_RPBEFORE;
Willy Tarreau8973c702007-01-21 23:58:29 +01001818 goto http_msg_invalid;
Willy Tarreau10e61cb2017-01-04 14:51:22 +01001819 }
Willy Tarreau8973c702007-01-21 23:58:29 +01001820
1821 if (unlikely(*ptr == '\n'))
1822 EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore, HTTP_MSG_RPBEFORE);
1823 EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore_cr, HTTP_MSG_RPBEFORE_CR);
1824 /* stop here */
1825
Willy Tarreau8973c702007-01-21 23:58:29 +01001826 case HTTP_MSG_RPBEFORE_CR:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001827 http_msg_rpbefore_cr:
Willy Tarreau10e61cb2017-01-04 14:51:22 +01001828 EXPECT_LF_HERE(ptr, http_msg_invalid, HTTP_MSG_RPBEFORE_CR);
Willy Tarreau8973c702007-01-21 23:58:29 +01001829 EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore, HTTP_MSG_RPBEFORE);
1830 /* stop here */
1831
Willy Tarreau8973c702007-01-21 23:58:29 +01001832 case HTTP_MSG_RPVER:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001833 http_msg_rpver:
Willy Tarreau8973c702007-01-21 23:58:29 +01001834 case HTTP_MSG_RPVER_SP:
1835 case HTTP_MSG_RPCODE:
1836 case HTTP_MSG_RPCODE_SP:
1837 case HTTP_MSG_RPREASON:
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02001838 ptr = (char *)http_parse_stsline(msg,
Willy Tarreaua458b672012-03-05 11:17:50 +01001839 state, ptr, end,
1840 &msg->next, &msg->msg_state);
Willy Tarreau8973c702007-01-21 23:58:29 +01001841 if (unlikely(!ptr))
1842 return;
1843
1844 /* we have a full response and we know that we have either a CR
1845 * or an LF at <ptr>.
1846 */
Willy Tarreau8973c702007-01-21 23:58:29 +01001847 hdr_idx_set_start(idx, msg->sl.st.l, *ptr == '\r');
1848
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001849 msg->sol = ptr - buf->p;
Willy Tarreau8973c702007-01-21 23:58:29 +01001850 if (likely(*ptr == '\r'))
1851 EAT_AND_JUMP_OR_RETURN(http_msg_rpline_end, HTTP_MSG_RPLINE_END);
1852 goto http_msg_rpline_end;
1853
Willy Tarreau8973c702007-01-21 23:58:29 +01001854 case HTTP_MSG_RPLINE_END:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001855 http_msg_rpline_end:
Willy Tarreau8973c702007-01-21 23:58:29 +01001856 /* msg->sol must point to the first of CR or LF. */
Willy Tarreau10e61cb2017-01-04 14:51:22 +01001857 EXPECT_LF_HERE(ptr, http_msg_invalid, HTTP_MSG_RPLINE_END);
Willy Tarreau8973c702007-01-21 23:58:29 +01001858 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_first, HTTP_MSG_HDR_FIRST);
1859 /* stop here */
1860
1861 /*
1862 * Second, states that are specific to the request only
1863 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001864 case HTTP_MSG_RQBEFORE:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001865 http_msg_rqbefore:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001866 if (likely(HTTP_IS_TOKEN(*ptr))) {
Willy Tarreau15de77e2010-01-02 21:59:16 +01001867 /* we have a start of message, but we have to check
1868 * first if we need to remove some CRLF. We can only
Willy Tarreau2e046c62012-03-01 16:08:30 +01001869 * do this when o=0.
Willy Tarreau15de77e2010-01-02 21:59:16 +01001870 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001871 if (likely(ptr != buf->p)) {
1872 if (buf->o)
Willy Tarreau15de77e2010-01-02 21:59:16 +01001873 goto http_msg_ood;
Willy Tarreau1d3bcce2009-12-27 15:50:06 +01001874 /* Remove empty leading lines, as recommended by RFC2616. */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001875 bi_fast_delete(buf, ptr - buf->p);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001876 }
Willy Tarreau26927362012-05-18 23:22:52 +02001877 msg->sol = 0;
Willy Tarreaue92693a2012-09-24 21:13:39 +02001878 msg->sl.rq.l = 0; /* used in debug mode */
Willy Tarreau8973c702007-01-21 23:58:29 +01001879 state = HTTP_MSG_RQMETH;
1880 goto http_msg_rqmeth;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001881 }
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001882
Willy Tarreau10e61cb2017-01-04 14:51:22 +01001883 if (unlikely(!HTTP_IS_CRLF(*ptr))) {
1884 state = HTTP_MSG_RQBEFORE;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001885 goto http_msg_invalid;
Willy Tarreau10e61cb2017-01-04 14:51:22 +01001886 }
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001887
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001888 if (unlikely(*ptr == '\n'))
1889 EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore, HTTP_MSG_RQBEFORE);
1890 EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore_cr, HTTP_MSG_RQBEFORE_CR);
Willy Tarreau8973c702007-01-21 23:58:29 +01001891 /* stop here */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001892
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001893 case HTTP_MSG_RQBEFORE_CR:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001894 http_msg_rqbefore_cr:
Willy Tarreau10e61cb2017-01-04 14:51:22 +01001895 EXPECT_LF_HERE(ptr, http_msg_invalid, HTTP_MSG_RQBEFORE_CR);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001896 EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore, HTTP_MSG_RQBEFORE);
Willy Tarreau8973c702007-01-21 23:58:29 +01001897 /* stop here */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001898
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001899 case HTTP_MSG_RQMETH:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001900 http_msg_rqmeth:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001901 case HTTP_MSG_RQMETH_SP:
1902 case HTTP_MSG_RQURI:
1903 case HTTP_MSG_RQURI_SP:
1904 case HTTP_MSG_RQVER:
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02001905 ptr = (char *)http_parse_reqline(msg,
Willy Tarreaua458b672012-03-05 11:17:50 +01001906 state, ptr, end,
1907 &msg->next, &msg->msg_state);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001908 if (unlikely(!ptr))
1909 return;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001910
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001911 /* we have a full request and we know that we have either a CR
1912 * or an LF at <ptr>.
1913 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001914 hdr_idx_set_start(idx, msg->sl.rq.l, *ptr == '\r');
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001915
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001916 msg->sol = ptr - buf->p;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001917 if (likely(*ptr == '\r'))
1918 EAT_AND_JUMP_OR_RETURN(http_msg_rqline_end, HTTP_MSG_RQLINE_END);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001919 goto http_msg_rqline_end;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001920
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001921 case HTTP_MSG_RQLINE_END:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001922 http_msg_rqline_end:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001923 /* check for HTTP/0.9 request : no version information available.
1924 * msg->sol must point to the first of CR or LF.
1925 */
1926 if (unlikely(msg->sl.rq.v_l == 0))
1927 goto http_msg_last_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001928
Willy Tarreau10e61cb2017-01-04 14:51:22 +01001929 EXPECT_LF_HERE(ptr, http_msg_invalid, HTTP_MSG_RQLINE_END);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001930 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_first, HTTP_MSG_HDR_FIRST);
Willy Tarreau8973c702007-01-21 23:58:29 +01001931 /* stop here */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001932
Willy Tarreau8973c702007-01-21 23:58:29 +01001933 /*
1934 * Common states below
1935 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001936 case HTTP_MSG_HDR_FIRST:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001937 http_msg_hdr_first:
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001938 msg->sol = ptr - buf->p;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001939 if (likely(!HTTP_IS_CRLF(*ptr))) {
1940 goto http_msg_hdr_name;
1941 }
1942
1943 if (likely(*ptr == '\r'))
1944 EAT_AND_JUMP_OR_RETURN(http_msg_last_lf, HTTP_MSG_LAST_LF);
1945 goto http_msg_last_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001946
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001947 case HTTP_MSG_HDR_NAME:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001948 http_msg_hdr_name:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001949 /* assumes msg->sol points to the first char */
1950 if (likely(HTTP_IS_TOKEN(*ptr)))
1951 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_name, HTTP_MSG_HDR_NAME);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001952
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01001953 if (likely(*ptr == ':'))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001954 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_sp, HTTP_MSG_HDR_L1_SP);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001955
Willy Tarreau10e61cb2017-01-04 14:51:22 +01001956 if (likely(msg->err_pos < -1) || *ptr == '\n') {
1957 state = HTTP_MSG_HDR_NAME;
Willy Tarreau32a4ec02009-04-02 11:35:18 +02001958 goto http_msg_invalid;
Willy Tarreau10e61cb2017-01-04 14:51:22 +01001959 }
Willy Tarreau32a4ec02009-04-02 11:35:18 +02001960
1961 if (msg->err_pos == -1) /* capture error pointer */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001962 msg->err_pos = ptr - buf->p; /* >= 0 now */
Willy Tarreau32a4ec02009-04-02 11:35:18 +02001963
1964 /* and we still accept this non-token character */
1965 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_name, HTTP_MSG_HDR_NAME);
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001966
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001967 case HTTP_MSG_HDR_L1_SP:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001968 http_msg_hdr_l1_sp:
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01001969 /* assumes msg->sol points to the first char */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001970 if (likely(HTTP_IS_SPHT(*ptr)))
1971 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_sp, HTTP_MSG_HDR_L1_SP);
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001972
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001973 /* header value can be basically anything except CR/LF */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001974 msg->sov = ptr - buf->p;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001975
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001976 if (likely(!HTTP_IS_CRLF(*ptr))) {
1977 goto http_msg_hdr_val;
1978 }
1979
1980 if (likely(*ptr == '\r'))
1981 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_lf, HTTP_MSG_HDR_L1_LF);
1982 goto http_msg_hdr_l1_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001983
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001984 case HTTP_MSG_HDR_L1_LF:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001985 http_msg_hdr_l1_lf:
Willy Tarreau10e61cb2017-01-04 14:51:22 +01001986 EXPECT_LF_HERE(ptr, http_msg_invalid, HTTP_MSG_HDR_L1_LF);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001987 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_lws, HTTP_MSG_HDR_L1_LWS);
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001988
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001989 case HTTP_MSG_HDR_L1_LWS:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001990 http_msg_hdr_l1_lws:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001991 if (likely(HTTP_IS_SPHT(*ptr))) {
1992 /* replace HT,CR,LF with spaces */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001993 for (; buf->p + msg->sov < ptr; msg->sov++)
1994 buf->p[msg->sov] = ' ';
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001995 goto http_msg_hdr_l1_sp;
1996 }
Willy Tarreauaa9dce32007-03-18 23:50:16 +01001997 /* we had a header consisting only in spaces ! */
Willy Tarreau12e48b32012-03-05 16:57:34 +01001998 msg->eol = msg->sov;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001999 goto http_msg_complete_header;
2000
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002001 case HTTP_MSG_HDR_VAL:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02002002 http_msg_hdr_val:
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01002003 /* assumes msg->sol points to the first char, and msg->sov
2004 * points to the first character of the value.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002005 */
Willy Tarreau0431f9d2016-11-05 17:35:40 +01002006
2007 /* speedup: we'll skip packs of 4 or 8 bytes not containing bytes 0x0D
2008 * and lower. In fact since most of the time is spent in the loop, we
2009 * also remove the sign bit test so that bytes 0x8e..0x0d break the
2010 * loop, but we don't care since they're very rare in header values.
2011 */
2012#if defined(__x86_64__)
2013 while (ptr <= end - sizeof(long)) {
2014 if ((*(long *)ptr - 0x0e0e0e0e0e0e0e0eULL) & 0x8080808080808080ULL)
2015 goto http_msg_hdr_val2;
2016 ptr += sizeof(long);
2017 }
2018#endif
2019#if defined(__x86_64__) || \
2020 defined(__i386__) || defined(__i486__) || defined(__i586__) || defined(__i686__) || \
2021 defined(__ARM_ARCH_7A__)
2022 while (ptr <= end - sizeof(int)) {
2023 if ((*(int*)ptr - 0x0e0e0e0e) & 0x80808080)
2024 goto http_msg_hdr_val2;
2025 ptr += sizeof(int);
2026 }
2027#endif
Willy Tarreau2afff9c2017-01-04 14:44:46 +01002028 if (ptr >= end) {
2029 state = HTTP_MSG_HDR_VAL;
2030 goto http_msg_ood;
2031 }
Willy Tarreau0431f9d2016-11-05 17:35:40 +01002032 http_msg_hdr_val2:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002033 if (likely(!HTTP_IS_CRLF(*ptr)))
Willy Tarreau0431f9d2016-11-05 17:35:40 +01002034 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_val2, HTTP_MSG_HDR_VAL);
Willy Tarreau976f1ee2006-12-17 10:06:03 +01002035
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002036 msg->eol = ptr - buf->p;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002037 /* Note: we could also copy eol into ->eoh so that we have the
2038 * real header end in case it ends with lots of LWS, but is this
2039 * really needed ?
2040 */
2041 if (likely(*ptr == '\r'))
2042 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l2_lf, HTTP_MSG_HDR_L2_LF);
2043 goto http_msg_hdr_l2_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01002044
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002045 case HTTP_MSG_HDR_L2_LF:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02002046 http_msg_hdr_l2_lf:
Willy Tarreau10e61cb2017-01-04 14:51:22 +01002047 EXPECT_LF_HERE(ptr, http_msg_invalid, HTTP_MSG_HDR_L2_LF);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002048 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l2_lws, HTTP_MSG_HDR_L2_LWS);
Willy Tarreau976f1ee2006-12-17 10:06:03 +01002049
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002050 case HTTP_MSG_HDR_L2_LWS:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02002051 http_msg_hdr_l2_lws:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002052 if (unlikely(HTTP_IS_SPHT(*ptr))) {
2053 /* LWS: replace HT,CR,LF with spaces */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002054 for (; buf->p + msg->eol < ptr; msg->eol++)
2055 buf->p[msg->eol] = ' ';
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002056 goto http_msg_hdr_val;
2057 }
2058 http_msg_complete_header:
2059 /*
2060 * It was a new header, so the last one is finished.
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01002061 * Assumes msg->sol points to the first char, msg->sov points
2062 * to the first character of the value and msg->eol to the
2063 * first CR or LF so we know how the line ends. We insert last
2064 * header into the index.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002065 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002066 if (unlikely(hdr_idx_add(msg->eol - msg->sol, buf->p[msg->eol] == '\r',
Willy Tarreau10e61cb2017-01-04 14:51:22 +01002067 idx, idx->tail) < 0)) {
2068 state = HTTP_MSG_HDR_L2_LWS;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002069 goto http_msg_invalid;
Willy Tarreau10e61cb2017-01-04 14:51:22 +01002070 }
Willy Tarreau230fd0b2006-12-17 12:05:00 +01002071
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002072 msg->sol = ptr - buf->p;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002073 if (likely(!HTTP_IS_CRLF(*ptr))) {
2074 goto http_msg_hdr_name;
2075 }
2076
2077 if (likely(*ptr == '\r'))
2078 EAT_AND_JUMP_OR_RETURN(http_msg_last_lf, HTTP_MSG_LAST_LF);
2079 goto http_msg_last_lf;
Willy Tarreau230fd0b2006-12-17 12:05:00 +01002080
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002081 case HTTP_MSG_LAST_LF:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02002082 http_msg_last_lf:
Willy Tarreau0558a022014-03-13 15:48:45 +01002083 /* Assumes msg->sol points to the first of either CR or LF.
2084 * Sets ->sov and ->next to the total header length, ->eoh to
2085 * the last CRLF, and ->eol to the last CRLF length (1 or 2).
2086 */
Willy Tarreau10e61cb2017-01-04 14:51:22 +01002087 EXPECT_LF_HERE(ptr, http_msg_invalid, HTTP_MSG_LAST_LF);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002088 ptr++;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002089 msg->sov = msg->next = ptr - buf->p;
Willy Tarreau3a215be2012-03-09 21:39:51 +01002090 msg->eoh = msg->sol;
2091 msg->sol = 0;
Willy Tarreau0558a022014-03-13 15:48:45 +01002092 msg->eol = msg->sov - msg->eoh;
Willy Tarreaub326fcc2007-03-03 13:54:32 +01002093 msg->msg_state = HTTP_MSG_BODY;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002094 return;
Willy Tarreaub56928a2012-04-16 14:51:55 +02002095
2096 case HTTP_MSG_ERROR:
2097 /* this may only happen if we call http_msg_analyser() twice with an error */
2098 break;
2099
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002100 default:
Willy Tarreau3770f232013-12-07 00:01:53 +01002101#ifdef DEBUG_FULL
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002102 fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state);
2103 exit(1);
Willy Tarreau230fd0b2006-12-17 12:05:00 +01002104#endif
Willy Tarreau3770f232013-12-07 00:01:53 +01002105 ;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002106 }
2107 http_msg_ood:
2108 /* out of data */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01002109 msg->msg_state = state;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002110 msg->next = ptr - buf->p;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002111 return;
Willy Tarreau58f10d72006-12-04 02:26:12 +01002112
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002113 http_msg_invalid:
2114 /* invalid message */
Willy Tarreau10e61cb2017-01-04 14:51:22 +01002115 msg->err_state = state;
Willy Tarreaub326fcc2007-03-03 13:54:32 +01002116 msg->msg_state = HTTP_MSG_ERROR;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002117 msg->next = ptr - buf->p;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002118 return;
2119}
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01002120
Willy Tarreau2492d5b2009-07-11 00:06:00 +02002121/* convert an HTTP/0.9 request into an HTTP/1.0 request. Returns 1 if the
2122 * conversion succeeded, 0 in case of error. If the request was already 1.X,
2123 * nothing is done and 1 is returned.
2124 */
Willy Tarreau418bfcc2012-03-09 13:56:20 +01002125static int http_upgrade_v09_to_v10(struct http_txn *txn)
Willy Tarreau2492d5b2009-07-11 00:06:00 +02002126{
2127 int delta;
2128 char *cur_end;
Willy Tarreau418bfcc2012-03-09 13:56:20 +01002129 struct http_msg *msg = &txn->req;
Willy Tarreau2492d5b2009-07-11 00:06:00 +02002130
2131 if (msg->sl.rq.v_l != 0)
2132 return 1;
2133
Apollon Oikonomopoulos25a15222014-04-06 02:46:00 +03002134 /* RFC 1945 allows only GET for HTTP/0.9 requests */
2135 if (txn->meth != HTTP_METH_GET)
2136 return 0;
2137
Willy Tarreau9b28e032012-10-12 23:49:43 +02002138 cur_end = msg->chn->buf->p + msg->sl.rq.l;
Willy Tarreau2492d5b2009-07-11 00:06:00 +02002139
2140 if (msg->sl.rq.u_l == 0) {
Apollon Oikonomopoulos25a15222014-04-06 02:46:00 +03002141 /* HTTP/0.9 requests *must* have a request URI, per RFC 1945 */
2142 return 0;
Willy Tarreau2492d5b2009-07-11 00:06:00 +02002143 }
2144 /* add HTTP version */
Willy Tarreau9b28e032012-10-12 23:49:43 +02002145 delta = buffer_replace2(msg->chn->buf, cur_end, cur_end, " HTTP/1.0\r\n", 11);
Willy Tarreaufa355d42009-11-29 18:12:29 +01002146 http_msg_move_end(msg, delta);
Willy Tarreau2492d5b2009-07-11 00:06:00 +02002147 cur_end += delta;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02002148 cur_end = (char *)http_parse_reqline(msg,
Willy Tarreau2492d5b2009-07-11 00:06:00 +02002149 HTTP_MSG_RQMETH,
Willy Tarreau9b28e032012-10-12 23:49:43 +02002150 msg->chn->buf->p, cur_end + 1,
Willy Tarreau2492d5b2009-07-11 00:06:00 +02002151 NULL, NULL);
2152 if (unlikely(!cur_end))
2153 return 0;
2154
2155 /* we have a full HTTP/1.0 request now and we know that
2156 * we have either a CR or an LF at <ptr>.
2157 */
2158 hdr_idx_set_start(&txn->hdr_idx, msg->sl.rq.l, *cur_end == '\r');
2159 return 1;
2160}
2161
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002162/* Parse the Connection: header of an HTTP request, looking for both "close"
Willy Tarreau6acf7c92012-03-09 13:30:45 +01002163 * and "keep-alive" values. If we already know that some headers may safely
2164 * be removed, we remove them now. The <to_del> flags are used for that :
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002165 * - bit 0 means remove "close" headers (in HTTP/1.0 requests/responses)
2166 * - bit 1 means remove "keep-alive" headers (in HTTP/1.1 reqs/resp to 1.1).
Willy Tarreau50fc7772012-11-11 22:19:57 +01002167 * Presence of the "Upgrade" token is also checked and reported.
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002168 * The TX_HDR_CONN_* flags are adjusted in txn->flags depending on what was
2169 * found, and TX_CON_*_SET is adjusted depending on what is left so only
2170 * harmless combinations may be removed. Do not call that after changes have
Willy Tarreau6acf7c92012-03-09 13:30:45 +01002171 * been processed.
Willy Tarreau5b154472009-12-21 20:11:07 +01002172 */
Willy Tarreau6acf7c92012-03-09 13:30:45 +01002173void http_parse_connection_header(struct http_txn *txn, struct http_msg *msg, int to_del)
Willy Tarreau5b154472009-12-21 20:11:07 +01002174{
Willy Tarreau5b154472009-12-21 20:11:07 +01002175 struct hdr_ctx ctx;
Willy Tarreau88d349d2010-01-25 12:15:43 +01002176 const char *hdr_val = "Connection";
2177 int hdr_len = 10;
Willy Tarreau5b154472009-12-21 20:11:07 +01002178
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002179 if (txn->flags & TX_HDR_CONN_PRS)
Willy Tarreau5b154472009-12-21 20:11:07 +01002180 return;
2181
Willy Tarreau88d349d2010-01-25 12:15:43 +01002182 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
2183 hdr_val = "Proxy-Connection";
2184 hdr_len = 16;
2185 }
2186
Willy Tarreau5b154472009-12-21 20:11:07 +01002187 ctx.idx = 0;
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002188 txn->flags &= ~(TX_CON_KAL_SET|TX_CON_CLO_SET);
Willy Tarreau9b28e032012-10-12 23:49:43 +02002189 while (http_find_header2(hdr_val, hdr_len, msg->chn->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002190 if (ctx.vlen >= 10 && word_match(ctx.line + ctx.val, ctx.vlen, "keep-alive", 10)) {
2191 txn->flags |= TX_HDR_CONN_KAL;
Willy Tarreau6acf7c92012-03-09 13:30:45 +01002192 if (to_del & 2)
2193 http_remove_header2(msg, &txn->hdr_idx, &ctx);
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002194 else
2195 txn->flags |= TX_CON_KAL_SET;
2196 }
2197 else if (ctx.vlen >= 5 && word_match(ctx.line + ctx.val, ctx.vlen, "close", 5)) {
2198 txn->flags |= TX_HDR_CONN_CLO;
Willy Tarreau6acf7c92012-03-09 13:30:45 +01002199 if (to_del & 1)
2200 http_remove_header2(msg, &txn->hdr_idx, &ctx);
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002201 else
2202 txn->flags |= TX_CON_CLO_SET;
2203 }
Willy Tarreau50fc7772012-11-11 22:19:57 +01002204 else if (ctx.vlen >= 7 && word_match(ctx.line + ctx.val, ctx.vlen, "upgrade", 7)) {
2205 txn->flags |= TX_HDR_CONN_UPG;
2206 }
Willy Tarreau5b154472009-12-21 20:11:07 +01002207 }
2208
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002209 txn->flags |= TX_HDR_CONN_PRS;
2210 return;
2211}
Willy Tarreau5b154472009-12-21 20:11:07 +01002212
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002213/* Apply desired changes on the Connection: header. Values may be removed and/or
2214 * added depending on the <wanted> flags, which are exclusively composed of
2215 * TX_CON_CLO_SET and TX_CON_KAL_SET, depending on what flags are desired. The
2216 * TX_CON_*_SET flags are adjusted in txn->flags depending on what is left.
2217 */
Willy Tarreau6acf7c92012-03-09 13:30:45 +01002218void http_change_connection_header(struct http_txn *txn, struct http_msg *msg, int wanted)
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002219{
2220 struct hdr_ctx ctx;
Willy Tarreau88d349d2010-01-25 12:15:43 +01002221 const char *hdr_val = "Connection";
2222 int hdr_len = 10;
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002223
2224 ctx.idx = 0;
2225
Willy Tarreau88d349d2010-01-25 12:15:43 +01002226
2227 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
2228 hdr_val = "Proxy-Connection";
2229 hdr_len = 16;
2230 }
2231
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002232 txn->flags &= ~(TX_CON_CLO_SET | TX_CON_KAL_SET);
Willy Tarreau9b28e032012-10-12 23:49:43 +02002233 while (http_find_header2(hdr_val, hdr_len, msg->chn->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002234 if (ctx.vlen >= 10 && word_match(ctx.line + ctx.val, ctx.vlen, "keep-alive", 10)) {
2235 if (wanted & TX_CON_KAL_SET)
2236 txn->flags |= TX_CON_KAL_SET;
2237 else
Willy Tarreau6acf7c92012-03-09 13:30:45 +01002238 http_remove_header2(msg, &txn->hdr_idx, &ctx);
Willy Tarreau5b154472009-12-21 20:11:07 +01002239 }
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002240 else if (ctx.vlen >= 5 && word_match(ctx.line + ctx.val, ctx.vlen, "close", 5)) {
2241 if (wanted & TX_CON_CLO_SET)
2242 txn->flags |= TX_CON_CLO_SET;
2243 else
Willy Tarreau6acf7c92012-03-09 13:30:45 +01002244 http_remove_header2(msg, &txn->hdr_idx, &ctx);
Willy Tarreau0dfdf192010-01-05 11:33:11 +01002245 }
Willy Tarreau5b154472009-12-21 20:11:07 +01002246 }
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002247
2248 if (wanted == (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET)))
2249 return;
2250
2251 if ((wanted & TX_CON_CLO_SET) && !(txn->flags & TX_CON_CLO_SET)) {
2252 txn->flags |= TX_CON_CLO_SET;
Willy Tarreau88d349d2010-01-25 12:15:43 +01002253 hdr_val = "Connection: close";
2254 hdr_len = 17;
2255 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
2256 hdr_val = "Proxy-Connection: close";
2257 hdr_len = 23;
2258 }
Willy Tarreau6acf7c92012-03-09 13:30:45 +01002259 http_header_add_tail2(msg, &txn->hdr_idx, hdr_val, hdr_len);
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002260 }
2261
2262 if ((wanted & TX_CON_KAL_SET) && !(txn->flags & TX_CON_KAL_SET)) {
2263 txn->flags |= TX_CON_KAL_SET;
Willy Tarreau88d349d2010-01-25 12:15:43 +01002264 hdr_val = "Connection: keep-alive";
2265 hdr_len = 22;
2266 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
2267 hdr_val = "Proxy-Connection: keep-alive";
2268 hdr_len = 28;
2269 }
Willy Tarreau6acf7c92012-03-09 13:30:45 +01002270 http_header_add_tail2(msg, &txn->hdr_idx, hdr_val, hdr_len);
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002271 }
2272 return;
Willy Tarreau5b154472009-12-21 20:11:07 +01002273}
2274
Christopher Faulet113f7de2015-12-14 14:52:13 +01002275/* Parse the chunk size at msg->next. Once done, caller should adjust ->next to
2276 * point to the first byte of data after the chunk size, so that we know we can
2277 * forward exactly msg->next bytes. msg->sol contains the exact number of bytes
2278 * forming the chunk size. That way it is always possible to differentiate
2279 * between the start of the body and the start of the data. Return the number
2280 * of byte parsed on success, 0 when some data is missing, <0 on error. Note:
2281 * this function is designed to parse wrapped CRLF at the end of the buffer.
Willy Tarreau115acb92009-12-26 13:56:06 +01002282 */
Willy Tarreau24e6d972012-10-26 00:49:52 +02002283static inline int http_parse_chunk_size(struct http_msg *msg)
Willy Tarreau115acb92009-12-26 13:56:06 +01002284{
Willy Tarreau9b28e032012-10-12 23:49:43 +02002285 const struct buffer *buf = msg->chn->buf;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002286 const char *ptr = b_ptr(buf, msg->next);
Willy Tarreau4baf44b2012-03-09 14:10:20 +01002287 const char *ptr_old = ptr;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002288 const char *end = buf->data + buf->size;
2289 const char *stop = bi_end(buf);
Willy Tarreau115acb92009-12-26 13:56:06 +01002290 unsigned int chunk = 0;
2291
2292 /* The chunk size is in the following form, though we are only
2293 * interested in the size and CRLF :
2294 * 1*HEXDIGIT *WSP *[ ';' extensions ] CRLF
2295 */
2296 while (1) {
2297 int c;
Willy Tarreau363a5bb2012-03-02 20:14:45 +01002298 if (ptr == stop)
Willy Tarreau115acb92009-12-26 13:56:06 +01002299 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002300 c = hex2i(*ptr);
Willy Tarreau115acb92009-12-26 13:56:06 +01002301 if (c < 0) /* not a hex digit anymore */
2302 break;
Willy Tarreau0161d622013-04-02 01:26:55 +02002303 if (unlikely(++ptr >= end))
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002304 ptr = buf->data;
Willy Tarreau431946e2012-02-24 19:20:12 +01002305 if (chunk & 0xF8000000) /* integer overflow will occur if result >= 2GB */
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002306 goto error;
Willy Tarreau115acb92009-12-26 13:56:06 +01002307 chunk = (chunk << 4) + c;
2308 }
2309
Willy Tarreaud98cf932009-12-27 22:54:55 +01002310 /* empty size not allowed */
Willy Tarreau0161d622013-04-02 01:26:55 +02002311 if (unlikely(ptr == ptr_old))
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002312 goto error;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002313
Willy Tarreau2235b262016-11-05 15:50:20 +01002314 while (HTTP_IS_SPHT(*ptr)) {
Willy Tarreaud98cf932009-12-27 22:54:55 +01002315 if (++ptr >= end)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002316 ptr = buf->data;
Willy Tarreau0161d622013-04-02 01:26:55 +02002317 if (unlikely(ptr == stop))
Willy Tarreau115acb92009-12-26 13:56:06 +01002318 return 0;
Willy Tarreau115acb92009-12-26 13:56:06 +01002319 }
2320
Willy Tarreaud98cf932009-12-27 22:54:55 +01002321 /* Up to there, we know that at least one byte is present at *ptr. Check
2322 * for the end of chunk size.
2323 */
2324 while (1) {
2325 if (likely(HTTP_IS_CRLF(*ptr))) {
2326 /* we now have a CR or an LF at ptr */
2327 if (likely(*ptr == '\r')) {
2328 if (++ptr >= end)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002329 ptr = buf->data;
Willy Tarreau363a5bb2012-03-02 20:14:45 +01002330 if (ptr == stop)
Willy Tarreaud98cf932009-12-27 22:54:55 +01002331 return 0;
2332 }
Willy Tarreau115acb92009-12-26 13:56:06 +01002333
Willy Tarreaud98cf932009-12-27 22:54:55 +01002334 if (*ptr != '\n')
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002335 goto error;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002336 if (++ptr >= end)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002337 ptr = buf->data;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002338 /* done */
2339 break;
2340 }
2341 else if (*ptr == ';') {
2342 /* chunk extension, ends at next CRLF */
2343 if (++ptr >= end)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002344 ptr = buf->data;
Willy Tarreau363a5bb2012-03-02 20:14:45 +01002345 if (ptr == stop)
Willy Tarreau115acb92009-12-26 13:56:06 +01002346 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002347
2348 while (!HTTP_IS_CRLF(*ptr)) {
2349 if (++ptr >= end)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002350 ptr = buf->data;
Willy Tarreau363a5bb2012-03-02 20:14:45 +01002351 if (ptr == stop)
Willy Tarreaud98cf932009-12-27 22:54:55 +01002352 return 0;
2353 }
2354 /* we have a CRLF now, loop above */
2355 continue;
Willy Tarreau115acb92009-12-26 13:56:06 +01002356 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01002357 else
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002358 goto error;
Willy Tarreau115acb92009-12-26 13:56:06 +01002359 }
2360
Christopher Faulet113f7de2015-12-14 14:52:13 +01002361 /* OK we found our CRLF and now <ptr> points to the next byte, which may
2362 * or may not be present. We save the number of bytes parsed into
2363 * msg->sol.
Willy Tarreau115acb92009-12-26 13:56:06 +01002364 */
Willy Tarreauc24715e2014-04-17 15:21:20 +02002365 msg->sol = ptr - ptr_old;
Willy Tarreau0161d622013-04-02 01:26:55 +02002366 if (unlikely(ptr < ptr_old))
Willy Tarreauc24715e2014-04-17 15:21:20 +02002367 msg->sol += buf->size;
Willy Tarreau124d9912011-03-01 20:30:48 +01002368 msg->chunk_len = chunk;
2369 msg->body_len += chunk;
Christopher Faulet113f7de2015-12-14 14:52:13 +01002370 return msg->sol;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002371 error:
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002372 msg->err_pos = buffer_count(buf, buf->p, ptr);
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002373 return -1;
Willy Tarreau115acb92009-12-26 13:56:06 +01002374}
2375
Christopher Faulet113f7de2015-12-14 14:52:13 +01002376/* This function skips trailers in the buffer associated with HTTP message
2377 * <msg>. The first visited position is msg->next. If the end of the trailers is
2378 * found, the function returns >0. So, the caller can automatically schedul it
2379 * to be forwarded, and switch msg->msg_state to HTTP_MSG_DONE. If not enough
2380 * data are available, the function does not change anything except maybe
2381 * msg->sol if it could parse some lines, and returns zero. If a parse error
2382 * is encountered, the function returns < 0 and does not change anything except
2383 * maybe msg->sol. Note that the message must already be in HTTP_MSG_TRAILERS
2384 * state before calling this function, which implies that all non-trailers data
2385 * have already been scheduled for forwarding, and that msg->next exactly
2386 * matches the length of trailers already parsed and not forwarded. It is also
2387 * important to note that this function is designed to be able to parse wrapped
2388 * headers at end of buffer.
Willy Tarreaud98cf932009-12-27 22:54:55 +01002389 */
Willy Tarreau24e6d972012-10-26 00:49:52 +02002390static int http_forward_trailers(struct http_msg *msg)
Willy Tarreaud98cf932009-12-27 22:54:55 +01002391{
Willy Tarreau9b28e032012-10-12 23:49:43 +02002392 const struct buffer *buf = msg->chn->buf;
Willy Tarreau4baf44b2012-03-09 14:10:20 +01002393
Christopher Faulet113f7de2015-12-14 14:52:13 +01002394 /* we have msg->next which points to next line. Look for CRLF. But
2395 * first, we reset msg->sol */
2396 msg->sol = 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002397 while (1) {
Willy Tarreau4baf44b2012-03-09 14:10:20 +01002398 const char *p1 = NULL, *p2 = NULL;
Christopher Faulet2fb28802015-12-01 10:40:57 +01002399 const char *start = b_ptr(buf, msg->next + msg->sol);
2400 const char *stop = bi_end(buf);
2401 const char *ptr = start;
2402 int bytes = 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002403
2404 /* scan current line and stop at LF or CRLF */
2405 while (1) {
Willy Tarreau363a5bb2012-03-02 20:14:45 +01002406 if (ptr == stop)
Willy Tarreaud98cf932009-12-27 22:54:55 +01002407 return 0;
2408
2409 if (*ptr == '\n') {
2410 if (!p1)
2411 p1 = ptr;
2412 p2 = ptr;
2413 break;
2414 }
2415
2416 if (*ptr == '\r') {
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002417 if (p1) {
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002418 msg->err_pos = buffer_count(buf, buf->p, ptr);
Willy Tarreaud98cf932009-12-27 22:54:55 +01002419 return -1;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002420 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01002421 p1 = ptr;
2422 }
2423
2424 ptr++;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002425 if (ptr >= buf->data + buf->size)
2426 ptr = buf->data;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002427 }
2428
2429 /* after LF; point to beginning of next line */
2430 p2++;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002431 if (p2 >= buf->data + buf->size)
2432 p2 = buf->data;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002433
Christopher Faulet2fb28802015-12-01 10:40:57 +01002434 bytes = p2 - start;
Willy Tarreau638cd022010-01-03 07:42:04 +01002435 if (bytes < 0)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002436 bytes += buf->size;
Christopher Faulet2fb28802015-12-01 10:40:57 +01002437 msg->sol += bytes;
Willy Tarreau638cd022010-01-03 07:42:04 +01002438
Christopher Fauletd7c91962015-04-30 11:48:27 +02002439 /* LF/CRLF at beginning of line => end of trailers at p2.
2440 * Everything was scheduled for forwarding, there's nothing left
Christopher Faulet2fb28802015-12-01 10:40:57 +01002441 * from this message. */
2442 if (p1 == start)
Willy Tarreaud98cf932009-12-27 22:54:55 +01002443 return 1;
Christopher Faulet2fb28802015-12-01 10:40:57 +01002444
Willy Tarreaud98cf932009-12-27 22:54:55 +01002445 /* OK, next line then */
Willy Tarreaud98cf932009-12-27 22:54:55 +01002446 }
2447}
2448
Christopher Faulet113f7de2015-12-14 14:52:13 +01002449/* This function may be called only in HTTP_MSG_CHUNK_CRLF. It reads the CRLF or
2450 * a possible LF alone at the end of a chunk. The caller should adjust msg->next
2451 * in order to include this part into the next forwarding phase. Note that the
2452 * caller must ensure that ->p points to the first byte to parse. It returns
2453 * the number of bytes parsed on success, so the caller can set msg_state to
2454 * HTTP_MSG_CHUNK_SIZE. If not enough data are available, the function does not
2455 * change anything and returns zero. If a parse error is encountered, the
2456 * function returns < 0. Note: this function is designed to parse wrapped CRLF
2457 * at the end of the buffer.
Willy Tarreaud98cf932009-12-27 22:54:55 +01002458 */
Willy Tarreau24e6d972012-10-26 00:49:52 +02002459static inline int http_skip_chunk_crlf(struct http_msg *msg)
Willy Tarreaud98cf932009-12-27 22:54:55 +01002460{
Willy Tarreau9b28e032012-10-12 23:49:43 +02002461 const struct buffer *buf = msg->chn->buf;
Willy Tarreau4baf44b2012-03-09 14:10:20 +01002462 const char *ptr;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002463 int bytes;
2464
2465 /* NB: we'll check data availabilty at the end. It's not a
2466 * problem because whatever we match first will be checked
2467 * against the correct length.
2468 */
2469 bytes = 1;
Willy Tarreau0669d7d2014-04-17 11:40:10 +02002470 ptr = b_ptr(buf, msg->next);
Willy Tarreaud98cf932009-12-27 22:54:55 +01002471 if (*ptr == '\r') {
2472 bytes++;
2473 ptr++;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002474 if (ptr >= buf->data + buf->size)
2475 ptr = buf->data;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002476 }
2477
Willy Tarreau0669d7d2014-04-17 11:40:10 +02002478 if (msg->next + bytes > buf->i)
Willy Tarreaud98cf932009-12-27 22:54:55 +01002479 return 0;
2480
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002481 if (*ptr != '\n') {
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002482 msg->err_pos = buffer_count(buf, buf->p, ptr);
Willy Tarreaud98cf932009-12-27 22:54:55 +01002483 return -1;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002484 }
Christopher Faulet113f7de2015-12-14 14:52:13 +01002485 return bytes;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002486}
Willy Tarreau5b154472009-12-21 20:11:07 +01002487
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002488/* Parses a qvalue and returns it multipled by 1000, from 0 to 1000. If the
2489 * value is larger than 1000, it is bound to 1000. The parser consumes up to
2490 * 1 digit, one dot and 3 digits and stops on the first invalid character.
2491 * Unparsable qvalues return 1000 as "q=1.000".
2492 */
Thierry FOURNIERad903512014-04-11 17:51:01 +02002493int parse_qvalue(const char *qvalue, const char **end)
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002494{
2495 int q = 1000;
2496
Willy Tarreau506c69a2014-07-08 00:59:48 +02002497 if (!isdigit((unsigned char)*qvalue))
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002498 goto out;
2499 q = (*qvalue++ - '0') * 1000;
2500
2501 if (*qvalue++ != '.')
2502 goto out;
2503
Willy Tarreau506c69a2014-07-08 00:59:48 +02002504 if (!isdigit((unsigned char)*qvalue))
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002505 goto out;
2506 q += (*qvalue++ - '0') * 100;
2507
Willy Tarreau506c69a2014-07-08 00:59:48 +02002508 if (!isdigit((unsigned char)*qvalue))
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002509 goto out;
2510 q += (*qvalue++ - '0') * 10;
2511
Willy Tarreau506c69a2014-07-08 00:59:48 +02002512 if (!isdigit((unsigned char)*qvalue))
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002513 goto out;
2514 q += (*qvalue++ - '0') * 1;
2515 out:
2516 if (q > 1000)
2517 q = 1000;
Willy Tarreau38b3aa52014-04-22 23:32:05 +02002518 if (end)
Thierry FOURNIERad903512014-04-11 17:51:01 +02002519 *end = qvalue;
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002520 return q;
2521}
William Lallemand82fe75c2012-10-23 10:25:10 +02002522
Willy Tarreau87b09662015-04-03 00:22:06 +02002523void http_adjust_conn_mode(struct stream *s, struct http_txn *txn, struct http_msg *msg)
Willy Tarreau4e21ff92014-09-30 18:44:22 +02002524{
Willy Tarreaud0d8da92015-04-04 02:10:38 +02002525 struct proxy *fe = strm_fe(s);
Willy Tarreau4e21ff92014-09-30 18:44:22 +02002526 int tmp = TX_CON_WANT_KAL;
2527
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002528 if (!((fe->options2|s->be->options2) & PR_O2_FAKE_KA)) {
2529 if ((fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_TUN ||
Willy Tarreau4e21ff92014-09-30 18:44:22 +02002530 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_TUN)
2531 tmp = TX_CON_WANT_TUN;
2532
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002533 if ((fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
Willy Tarreau4e21ff92014-09-30 18:44:22 +02002534 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL)
2535 tmp = TX_CON_WANT_TUN;
2536 }
2537
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002538 if ((fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_SCL ||
Willy Tarreau4e21ff92014-09-30 18:44:22 +02002539 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_SCL) {
2540 /* option httpclose + server_close => forceclose */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002541 if ((fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
Willy Tarreau4e21ff92014-09-30 18:44:22 +02002542 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL)
2543 tmp = TX_CON_WANT_CLO;
2544 else
2545 tmp = TX_CON_WANT_SCL;
2546 }
2547
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002548 if ((fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_FCL ||
Willy Tarreau4e21ff92014-09-30 18:44:22 +02002549 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_FCL)
2550 tmp = TX_CON_WANT_CLO;
2551
2552 if ((txn->flags & TX_CON_WANT_MSK) < tmp)
2553 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | tmp;
2554
2555 if (!(txn->flags & TX_HDR_CONN_PRS) &&
2556 (txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) {
2557 /* parse the Connection header and possibly clean it */
2558 int to_del = 0;
2559 if ((msg->flags & HTTP_MSGF_VER_11) ||
2560 ((txn->flags & TX_CON_WANT_MSK) >= TX_CON_WANT_SCL &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002561 !((fe->options2|s->be->options2) & PR_O2_FAKE_KA)))
Willy Tarreau4e21ff92014-09-30 18:44:22 +02002562 to_del |= 2; /* remove "keep-alive" */
2563 if (!(msg->flags & HTTP_MSGF_VER_11))
2564 to_del |= 1; /* remove "close" */
2565 http_parse_connection_header(txn, msg, to_del);
2566 }
2567
2568 /* check if client or config asks for explicit close in KAL/SCL */
2569 if (((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
2570 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) &&
2571 ((txn->flags & TX_HDR_CONN_CLO) || /* "connection: close" */
2572 (!(msg->flags & HTTP_MSGF_VER_11) && !(txn->flags & TX_HDR_CONN_KAL)) || /* no "connection: k-a" in 1.0 */
2573 !(msg->flags & HTTP_MSGF_XFER_LEN) || /* no length known => close */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002574 fe->state == PR_STSTOPPED)) /* frontend is stopping */
Willy Tarreau4e21ff92014-09-30 18:44:22 +02002575 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO;
2576}
William Lallemand82fe75c2012-10-23 10:25:10 +02002577
Willy Tarreaud787e662009-07-07 10:14:51 +02002578/* This stream analyser waits for a complete HTTP request. It returns 1 if the
2579 * processing can continue on next analysers, or zero if it either needs more
2580 * data or wants to immediately abort the request (eg: timeout, error, ...). It
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01002581 * is tied to AN_REQ_WAIT_HTTP and may may remove itself from s->req.analysers
Willy Tarreaud787e662009-07-07 10:14:51 +02002582 * when it has nothing left to do, and may remove any analyser when it wants to
2583 * abort.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002584 */
Willy Tarreau87b09662015-04-03 00:22:06 +02002585int http_wait_for_request(struct stream *s, struct channel *req, int an_bit)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002586{
Willy Tarreau59234e92008-11-30 23:51:27 +01002587 /*
2588 * We will parse the partial (or complete) lines.
2589 * We will check the request syntax, and also join multi-line
2590 * headers. An index of all the lines will be elaborated while
2591 * parsing.
2592 *
2593 * For the parsing, we use a 28 states FSM.
2594 *
2595 * Here is the information we currently have :
Willy Tarreau9b28e032012-10-12 23:49:43 +02002596 * req->buf->p = beginning of request
2597 * req->buf->p + msg->eoh = end of processed headers / start of current one
2598 * req->buf->p + req->buf->i = end of input data
Willy Tarreau26927362012-05-18 23:22:52 +02002599 * msg->eol = end of current header or line (LF or CRLF)
2600 * msg->next = first non-visited byte
Willy Tarreaud787e662009-07-07 10:14:51 +02002601 *
2602 * At end of parsing, we may perform a capture of the error (if any), and
Willy Tarreaue7dff022015-04-03 01:14:29 +02002603 * we will set a few fields (txn->meth, sn->flags/SF_REDIRECTABLE).
Willy Tarreaud787e662009-07-07 10:14:51 +02002604 * We also check for monitor-uri, logging, HTTP/0.9 to 1.0 conversion, and
2605 * finally headers capture.
Willy Tarreau59234e92008-11-30 23:51:27 +01002606 */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01002607
Willy Tarreau59234e92008-11-30 23:51:27 +01002608 int cur_idx;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02002609 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02002610 struct http_txn *txn = s->txn;
Willy Tarreau59234e92008-11-30 23:51:27 +01002611 struct http_msg *msg = &txn->req;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002612 struct hdr_ctx ctx;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01002613
Willy Tarreau87b09662015-04-03 00:22:06 +02002614 DPRINTF(stderr,"[%u] %s: stream=%p b=%p, exp(r,w)=%u,%u bf=%08x bh=%d analysers=%02x\n",
Willy Tarreau6bf17362009-02-24 10:48:35 +01002615 now_ms, __FUNCTION__,
2616 s,
2617 req,
2618 req->rex, req->wex,
2619 req->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02002620 req->buf->i,
Willy Tarreau6bf17362009-02-24 10:48:35 +01002621 req->analysers);
2622
Willy Tarreau52a0c602009-08-16 22:45:38 +02002623 /* we're speaking HTTP here, so let's speak HTTP to the client */
2624 s->srv_error = http_return_srv_error;
2625
Willy Tarreau83e3af02009-12-28 17:39:57 +01002626 /* There's a protected area at the end of the buffer for rewriting
2627 * purposes. We don't want to start to parse the request if the
2628 * protected area is affected, because we may have to move processed
2629 * data later, which is much more complicated.
2630 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02002631 if (buffer_not_empty(req->buf) && msg->msg_state < HTTP_MSG_ERROR) {
Thierry FOURNIER / OZON.IO4cac3592016-07-28 17:19:45 +02002632
2633 /* This point is executed when some data is avalaible for analysis,
2634 * so we log the end of the idle time. */
2635 if (s->logs.t_idle == -1)
2636 s->logs.t_idle = tv_ms_elapsed(&s->logs.tv_accept, &now) - s->logs.t_handshake;
2637
Willy Tarreau379357a2013-06-08 12:55:46 +02002638 if (txn->flags & TX_NOT_FIRST) {
Willy Tarreauba0902e2015-01-13 14:39:16 +01002639 if (unlikely(!channel_is_rewritable(req))) {
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002640 if (req->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_WRITE_ERROR|CF_WRITE_TIMEOUT))
Willy Tarreau64648412010-03-05 10:41:54 +01002641 goto failed_keep_alive;
Willy Tarreau2ab6eb12010-01-02 22:04:45 +01002642 /* some data has still not left the buffer, wake us once that's done */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02002643 channel_dont_connect(req);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002644 req->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
Willy Tarreaud7ad9f52013-12-31 17:26:25 +01002645 req->flags |= CF_WAKE_WRITE;
Willy Tarreau2ab6eb12010-01-02 22:04:45 +01002646 return 0;
2647 }
Willy Tarreau379357a2013-06-08 12:55:46 +02002648 if (unlikely(bi_end(req->buf) < b_ptr(req->buf, msg->next) ||
2649 bi_end(req->buf) > req->buf->data + req->buf->size - global.tune.maxrewrite))
2650 buffer_slow_realign(req->buf);
Willy Tarreau83e3af02009-12-28 17:39:57 +01002651 }
2652
Willy Tarreau065e8332010-01-08 00:30:20 +01002653 /* Note that we have the same problem with the response ; we
2654 * may want to send a redirect, error or anything which requires
2655 * some spare space. So we'll ensure that we have at least
2656 * maxrewrite bytes available in the response buffer before
2657 * processing that one. This will only affect pipelined
2658 * keep-alive requests.
2659 */
2660 if ((txn->flags & TX_NOT_FIRST) &&
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01002661 unlikely(!channel_is_rewritable(&s->res) ||
2662 bi_end(s->res.buf) < b_ptr(s->res.buf, txn->rsp.next) ||
2663 bi_end(s->res.buf) > s->res.buf->data + s->res.buf->size - global.tune.maxrewrite)) {
2664 if (s->res.buf->o) {
2665 if (s->res.flags & (CF_SHUTW|CF_SHUTW_NOW|CF_WRITE_ERROR|CF_WRITE_TIMEOUT))
Willy Tarreau64648412010-03-05 10:41:54 +01002666 goto failed_keep_alive;
Willy Tarreau065e8332010-01-08 00:30:20 +01002667 /* don't let a connection request be initiated */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02002668 channel_dont_connect(req);
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01002669 s->res.flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */
2670 s->res.flags |= CF_WAKE_WRITE;
2671 s->res.analysers |= an_bit; /* wake us up once it changes */
Willy Tarreau065e8332010-01-08 00:30:20 +01002672 return 0;
2673 }
2674 }
2675
Willy Tarreau9b28e032012-10-12 23:49:43 +02002676 if (likely(msg->next < req->buf->i)) /* some unparsed data are available */
Willy Tarreaua560c212012-03-09 13:50:57 +01002677 http_msg_analyzer(msg, &txn->hdr_idx);
Willy Tarreau83e3af02009-12-28 17:39:57 +01002678 }
2679
Willy Tarreau59234e92008-11-30 23:51:27 +01002680 /* 1: we might have to print this header in debug mode */
2681 if (unlikely((global.mode & MODE_DEBUG) &&
2682 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) &&
Willy Tarreau7d59e902014-10-21 19:36:09 +02002683 msg->msg_state >= HTTP_MSG_BODY)) {
Willy Tarreau59234e92008-11-30 23:51:27 +01002684 char *eol, *sol;
Willy Tarreau58f10d72006-12-04 02:26:12 +01002685
Willy Tarreau9b28e032012-10-12 23:49:43 +02002686 sol = req->buf->p;
Willy Tarreaue92693a2012-09-24 21:13:39 +02002687 /* this is a bit complex : in case of error on the request line,
2688 * we know that rq.l is still zero, so we display only the part
2689 * up to the end of the line (truncated by debug_hdr).
2690 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02002691 eol = sol + (msg->sl.rq.l ? msg->sl.rq.l : req->buf->i);
Willy Tarreau59234e92008-11-30 23:51:27 +01002692 debug_hdr("clireq", s, sol, eol);
Willy Tarreau45e73e32006-12-17 00:05:15 +01002693
Willy Tarreau59234e92008-11-30 23:51:27 +01002694 sol += hdr_idx_first_pos(&txn->hdr_idx);
2695 cur_idx = hdr_idx_first_idx(&txn->hdr_idx);
Willy Tarreau58f10d72006-12-04 02:26:12 +01002696
Willy Tarreau59234e92008-11-30 23:51:27 +01002697 while (cur_idx) {
2698 eol = sol + txn->hdr_idx.v[cur_idx].len;
2699 debug_hdr("clihdr", s, sol, eol);
2700 sol = eol + txn->hdr_idx.v[cur_idx].cr + 1;
2701 cur_idx = txn->hdr_idx.v[cur_idx].next;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002702 }
Willy Tarreau59234e92008-11-30 23:51:27 +01002703 }
2704
Willy Tarreau58f10d72006-12-04 02:26:12 +01002705
Willy Tarreau59234e92008-11-30 23:51:27 +01002706 /*
2707 * Now we quickly check if we have found a full valid request.
2708 * If not so, we check the FD and buffer states before leaving.
2709 * A full request is indicated by the fact that we have seen
Willy Tarreau655dce92009-11-08 13:10:58 +01002710 * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid
Willy Tarreaud3c343f2010-01-16 10:26:19 +01002711 * requests are checked first. When waiting for a second request
Willy Tarreau87b09662015-04-03 00:22:06 +02002712 * on a keep-alive stream, if we encounter and error, close, t/o,
2713 * we note the error in the stream flags but don't set any state.
Willy Tarreaud3c343f2010-01-16 10:26:19 +01002714 * Since the error will be noted there, it will not be counted by
Willy Tarreau87b09662015-04-03 00:22:06 +02002715 * process_stream() as a frontend error.
Willy Tarreauda7ff642010-06-23 11:44:09 +02002716 * Last, we may increase some tracked counters' http request errors on
2717 * the cases that are deliberately the client's fault. For instance,
2718 * a timeout or connection reset is not counted as an error. However
2719 * a bad request is.
Willy Tarreau59234e92008-11-30 23:51:27 +01002720 */
Willy Tarreau58f10d72006-12-04 02:26:12 +01002721
Willy Tarreau655dce92009-11-08 13:10:58 +01002722 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01002723 /*
Willy Tarreau59234e92008-11-30 23:51:27 +01002724 * First, let's catch bad requests.
Willy Tarreau58f10d72006-12-04 02:26:12 +01002725 */
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002726 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
Willy Tarreau87b09662015-04-03 00:22:06 +02002727 stream_inc_http_req_ctr(s);
2728 stream_inc_http_err_ctr(s);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002729 proxy_inc_fe_req_ctr(sess->fe);
Willy Tarreau59234e92008-11-30 23:51:27 +01002730 goto return_bad_req;
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002731 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002732
Willy Tarreau59234e92008-11-30 23:51:27 +01002733 /* 1: Since we are in header mode, if there's no space
2734 * left for headers, we won't be able to free more
Willy Tarreau87b09662015-04-03 00:22:06 +02002735 * later, so the stream will never terminate. We
Willy Tarreau59234e92008-11-30 23:51:27 +01002736 * must terminate it now.
2737 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02002738 if (unlikely(buffer_full(req->buf, global.tune.maxrewrite))) {
Willy Tarreau59234e92008-11-30 23:51:27 +01002739 /* FIXME: check if URI is set and return Status
2740 * 414 Request URI too long instead.
Willy Tarreau58f10d72006-12-04 02:26:12 +01002741 */
Willy Tarreau87b09662015-04-03 00:22:06 +02002742 stream_inc_http_req_ctr(s);
2743 stream_inc_http_err_ctr(s);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002744 proxy_inc_fe_req_ctr(sess->fe);
Willy Tarreaufec4d892011-09-02 20:04:57 +02002745 if (msg->err_pos < 0)
Willy Tarreau9b28e032012-10-12 23:49:43 +02002746 msg->err_pos = req->buf->i;
Willy Tarreau59234e92008-11-30 23:51:27 +01002747 goto return_bad_req;
2748 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002749
Willy Tarreau59234e92008-11-30 23:51:27 +01002750 /* 2: have we encountered a read error ? */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002751 else if (req->flags & CF_READ_ERROR) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02002752 if (!(s->flags & SF_ERR_MASK))
2753 s->flags |= SF_ERR_CLICL;
Willy Tarreaud3c343f2010-01-16 10:26:19 +01002754
Willy Tarreaufcffa692010-01-10 14:21:19 +01002755 if (txn->flags & TX_WAIT_NEXT_RQ)
Willy Tarreaub608feb2010-01-02 22:47:18 +01002756 goto failed_keep_alive;
2757
Willy Tarreau0f228a02015-05-01 15:37:53 +02002758 if (sess->fe->options & PR_O_IGNORE_PRB)
2759 goto failed_keep_alive;
2760
Willy Tarreau59234e92008-11-30 23:51:27 +01002761 /* we cannot return any message on error */
Willy Tarreauda7ff642010-06-23 11:44:09 +02002762 if (msg->err_pos >= 0) {
Willy Tarreau10e61cb2017-01-04 14:51:22 +01002763 http_capture_bad_message(&sess->fe->invalid_req, s, msg, msg->err_state, sess->fe);
Willy Tarreau87b09662015-04-03 00:22:06 +02002764 stream_inc_http_err_ctr(s);
Willy Tarreauda7ff642010-06-23 11:44:09 +02002765 }
2766
Willy Tarreaudc979f22012-12-04 10:39:01 +01002767 txn->status = 400;
Willy Tarreau10e61cb2017-01-04 14:51:22 +01002768 msg->err_state = msg->msg_state;
Willy Tarreau59234e92008-11-30 23:51:27 +01002769 msg->msg_state = HTTP_MSG_ERROR;
Christopher Fauleta94e5a52015-12-09 15:55:06 +01002770 http_reply_and_close(s, txn->status, NULL);
Christopher Faulet0184ea72017-01-05 14:06:34 +01002771 req->analysers &= AN_REQ_FLT_END;
Willy Tarreau87b09662015-04-03 00:22:06 +02002772 stream_inc_http_req_ctr(s);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002773 proxy_inc_fe_req_ctr(sess->fe);
2774 sess->fe->fe_counters.failed_req++;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02002775 if (sess->listener->counters)
2776 sess->listener->counters->failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002777
Willy Tarreaue7dff022015-04-03 01:14:29 +02002778 if (!(s->flags & SF_FINST_MASK))
2779 s->flags |= SF_FINST_R;
Willy Tarreau59234e92008-11-30 23:51:27 +01002780 return 0;
2781 }
Willy Tarreauf9839bd2008-08-27 23:57:16 +02002782
Willy Tarreau59234e92008-11-30 23:51:27 +01002783 /* 3: has the read timeout expired ? */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002784 else if (req->flags & CF_READ_TIMEOUT || tick_is_expired(req->analyse_exp, now_ms)) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02002785 if (!(s->flags & SF_ERR_MASK))
2786 s->flags |= SF_ERR_CLITO;
Willy Tarreaud3c343f2010-01-16 10:26:19 +01002787
Willy Tarreaufcffa692010-01-10 14:21:19 +01002788 if (txn->flags & TX_WAIT_NEXT_RQ)
Willy Tarreaub608feb2010-01-02 22:47:18 +01002789 goto failed_keep_alive;
2790
Willy Tarreau0f228a02015-05-01 15:37:53 +02002791 if (sess->fe->options & PR_O_IGNORE_PRB)
2792 goto failed_keep_alive;
2793
Willy Tarreau59234e92008-11-30 23:51:27 +01002794 /* read timeout : give up with an error message. */
Willy Tarreauda7ff642010-06-23 11:44:09 +02002795 if (msg->err_pos >= 0) {
Willy Tarreau10e61cb2017-01-04 14:51:22 +01002796 http_capture_bad_message(&sess->fe->invalid_req, s, msg, msg->err_state, sess->fe);
Willy Tarreau87b09662015-04-03 00:22:06 +02002797 stream_inc_http_err_ctr(s);
Willy Tarreauda7ff642010-06-23 11:44:09 +02002798 }
Willy Tarreau59234e92008-11-30 23:51:27 +01002799 txn->status = 408;
Willy Tarreau10e61cb2017-01-04 14:51:22 +01002800 msg->err_state = msg->msg_state;
Willy Tarreau59234e92008-11-30 23:51:27 +01002801 msg->msg_state = HTTP_MSG_ERROR;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02002802 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulet0184ea72017-01-05 14:06:34 +01002803 req->analysers &= AN_REQ_FLT_END;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002804
Willy Tarreau87b09662015-04-03 00:22:06 +02002805 stream_inc_http_req_ctr(s);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002806 proxy_inc_fe_req_ctr(sess->fe);
2807 sess->fe->fe_counters.failed_req++;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02002808 if (sess->listener->counters)
2809 sess->listener->counters->failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002810
Willy Tarreaue7dff022015-04-03 01:14:29 +02002811 if (!(s->flags & SF_FINST_MASK))
2812 s->flags |= SF_FINST_R;
Willy Tarreau59234e92008-11-30 23:51:27 +01002813 return 0;
2814 }
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02002815
Willy Tarreau59234e92008-11-30 23:51:27 +01002816 /* 4: have we encountered a close ? */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002817 else if (req->flags & CF_SHUTR) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02002818 if (!(s->flags & SF_ERR_MASK))
2819 s->flags |= SF_ERR_CLICL;
Willy Tarreaud3c343f2010-01-16 10:26:19 +01002820
Willy Tarreaufcffa692010-01-10 14:21:19 +01002821 if (txn->flags & TX_WAIT_NEXT_RQ)
Willy Tarreaub608feb2010-01-02 22:47:18 +01002822 goto failed_keep_alive;
2823
Willy Tarreau0f228a02015-05-01 15:37:53 +02002824 if (sess->fe->options & PR_O_IGNORE_PRB)
2825 goto failed_keep_alive;
2826
Willy Tarreau4076a152009-04-02 15:18:36 +02002827 if (msg->err_pos >= 0)
Willy Tarreau10e61cb2017-01-04 14:51:22 +01002828 http_capture_bad_message(&sess->fe->invalid_req, s, msg, msg->err_state, sess->fe);
Willy Tarreau59234e92008-11-30 23:51:27 +01002829 txn->status = 400;
Willy Tarreau10e61cb2017-01-04 14:51:22 +01002830 msg->err_state = msg->msg_state;
Willy Tarreau59234e92008-11-30 23:51:27 +01002831 msg->msg_state = HTTP_MSG_ERROR;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02002832 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulet0184ea72017-01-05 14:06:34 +01002833 req->analysers &= AN_REQ_FLT_END;
Willy Tarreau87b09662015-04-03 00:22:06 +02002834 stream_inc_http_err_ctr(s);
2835 stream_inc_http_req_ctr(s);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002836 proxy_inc_fe_req_ctr(sess->fe);
2837 sess->fe->fe_counters.failed_req++;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02002838 if (sess->listener->counters)
2839 sess->listener->counters->failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002840
Willy Tarreaue7dff022015-04-03 01:14:29 +02002841 if (!(s->flags & SF_FINST_MASK))
2842 s->flags |= SF_FINST_R;
Willy Tarreaudafde432008-08-17 01:00:46 +02002843 return 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01002844 }
2845
Willy Tarreau8263d2b2012-08-28 00:06:31 +02002846 channel_dont_connect(req);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002847 req->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01002848 s->res.flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */
Willy Tarreau5e205522011-12-17 16:34:27 +01002849#ifdef TCP_QUICKACK
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02002850 if (sess->listener->options & LI_O_NOQUICKACK && req->buf->i &&
2851 objt_conn(sess->origin) && conn_ctrl_ready(__objt_conn(sess->origin))) {
Willy Tarreau5e205522011-12-17 16:34:27 +01002852 /* We need more data, we have to re-enable quick-ack in case we
2853 * previously disabled it, otherwise we might cause the client
2854 * to delay next data.
2855 */
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02002856 setsockopt(__objt_conn(sess->origin)->t.sock.fd, IPPROTO_TCP, TCP_QUICKACK, &one, sizeof(one));
Willy Tarreau5e205522011-12-17 16:34:27 +01002857 }
2858#endif
Willy Tarreau1b194fe2009-03-21 21:10:04 +01002859
Willy Tarreaufcffa692010-01-10 14:21:19 +01002860 if ((msg->msg_state != HTTP_MSG_RQBEFORE) && (txn->flags & TX_WAIT_NEXT_RQ)) {
2861 /* If the client starts to talk, let's fall back to
2862 * request timeout processing.
2863 */
2864 txn->flags &= ~TX_WAIT_NEXT_RQ;
Willy Tarreaub16a5742010-01-10 14:46:16 +01002865 req->analyse_exp = TICK_ETERNITY;
Willy Tarreaufcffa692010-01-10 14:21:19 +01002866 }
2867
Willy Tarreau59234e92008-11-30 23:51:27 +01002868 /* just set the request timeout once at the beginning of the request */
Willy Tarreaub16a5742010-01-10 14:46:16 +01002869 if (!tick_isset(req->analyse_exp)) {
2870 if ((msg->msg_state == HTTP_MSG_RQBEFORE) &&
2871 (txn->flags & TX_WAIT_NEXT_RQ) &&
2872 tick_isset(s->be->timeout.httpka))
2873 req->analyse_exp = tick_add(now_ms, s->be->timeout.httpka);
2874 else
2875 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.httpreq);
2876 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002877
Willy Tarreau59234e92008-11-30 23:51:27 +01002878 /* we're not ready yet */
2879 return 0;
Willy Tarreaub608feb2010-01-02 22:47:18 +01002880
2881 failed_keep_alive:
2882 /* Here we process low-level errors for keep-alive requests. In
2883 * short, if the request is not the first one and it experiences
2884 * a timeout, read error or shutdown, we just silently close so
2885 * that the client can try again.
2886 */
2887 txn->status = 0;
2888 msg->msg_state = HTTP_MSG_RQBEFORE;
Christopher Faulet0184ea72017-01-05 14:06:34 +01002889 req->analysers &= AN_REQ_FLT_END;
Willy Tarreaub608feb2010-01-02 22:47:18 +01002890 s->logs.logwait = 0;
Willy Tarreauabcd5142013-06-11 17:18:02 +02002891 s->logs.level = 0;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01002892 s->res.flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */
Christopher Fauleta94e5a52015-12-09 15:55:06 +01002893 http_reply_and_close(s, txn->status, NULL);
Willy Tarreaub608feb2010-01-02 22:47:18 +01002894 return 0;
Willy Tarreau59234e92008-11-30 23:51:27 +01002895 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002896
Willy Tarreaud787e662009-07-07 10:14:51 +02002897 /* OK now we have a complete HTTP request with indexed headers. Let's
2898 * complete the request parsing by setting a few fields we will need
Willy Tarreau9b28e032012-10-12 23:49:43 +02002899 * later. At this point, we have the last CRLF at req->buf->data + msg->eoh.
Willy Tarreaufa355d42009-11-29 18:12:29 +01002900 * If the request is in HTTP/0.9 form, the rule is still true, and eoh
Willy Tarreaua458b672012-03-05 11:17:50 +01002901 * points to the CRLF of the request line. msg->next points to the first
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01002902 * byte after the last LF. msg->sov points to the first byte of data.
2903 * msg->eol cannot be trusted because it may have been left uninitialized
2904 * (for instance in the absence of headers).
Willy Tarreaud787e662009-07-07 10:14:51 +02002905 */
Willy Tarreau9cdde232007-05-02 20:58:19 +02002906
Willy Tarreau87b09662015-04-03 00:22:06 +02002907 stream_inc_http_req_ctr(s);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002908 proxy_inc_fe_req_ctr(sess->fe); /* one more valid request for this FE */
Willy Tarreaud9b587f2010-02-26 10:05:55 +01002909
Willy Tarreaub16a5742010-01-10 14:46:16 +01002910 if (txn->flags & TX_WAIT_NEXT_RQ) {
2911 /* kill the pending keep-alive timeout */
2912 txn->flags &= ~TX_WAIT_NEXT_RQ;
2913 req->analyse_exp = TICK_ETERNITY;
2914 }
2915
2916
Willy Tarreaud787e662009-07-07 10:14:51 +02002917 /* Maybe we found in invalid header name while we were configured not
2918 * to block on that, so we have to capture it now.
2919 */
2920 if (unlikely(msg->err_pos >= 0))
Willy Tarreau10e61cb2017-01-04 14:51:22 +01002921 http_capture_bad_message(&sess->fe->invalid_req, s, msg, msg->err_state, sess->fe);
Willy Tarreau4076a152009-04-02 15:18:36 +02002922
Willy Tarreau59234e92008-11-30 23:51:27 +01002923 /*
2924 * 1: identify the method
2925 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02002926 txn->meth = find_http_meth(req->buf->p, msg->sl.rq.m_l);
Willy Tarreau59234e92008-11-30 23:51:27 +01002927
2928 /* we can make use of server redirect on GET and HEAD */
2929 if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)
Willy Tarreaue7dff022015-04-03 01:14:29 +02002930 s->flags |= SF_REDIRECTABLE;
Willy Tarreaufa7e1022008-10-19 07:30:41 +02002931
Willy Tarreau59234e92008-11-30 23:51:27 +01002932 /*
2933 * 2: check if the URI matches the monitor_uri.
2934 * We have to do this for every request which gets in, because
2935 * the monitor-uri is defined by the frontend.
2936 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002937 if (unlikely((sess->fe->monitor_uri_len != 0) &&
2938 (sess->fe->monitor_uri_len == msg->sl.rq.u_l) &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02002939 !memcmp(req->buf->p + msg->sl.rq.u,
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002940 sess->fe->monitor_uri,
2941 sess->fe->monitor_uri_len))) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01002942 /*
Willy Tarreau59234e92008-11-30 23:51:27 +01002943 * We have found the monitor URI
Willy Tarreau58f10d72006-12-04 02:26:12 +01002944 */
Willy Tarreau59234e92008-11-30 23:51:27 +01002945 struct acl_cond *cond;
Willy Tarreaub80c2302007-11-30 20:51:32 +01002946
Willy Tarreaue7dff022015-04-03 01:14:29 +02002947 s->flags |= SF_MONITOR;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002948 sess->fe->fe_counters.intercepted_req++;
Willy Tarreaub80c2302007-11-30 20:51:32 +01002949
Willy Tarreau59234e92008-11-30 23:51:27 +01002950 /* Check if we want to fail this monitor request or not */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002951 list_for_each_entry(cond, &sess->fe->mon_fail_cond, list) {
Willy Tarreau192252e2015-04-04 01:47:55 +02002952 int ret = acl_exec_cond(cond, sess->fe, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreau11382812008-07-09 16:18:21 +02002953
Willy Tarreau59234e92008-11-30 23:51:27 +01002954 ret = acl_pass(ret);
2955 if (cond->pol == ACL_COND_UNLESS)
2956 ret = !ret;
Willy Tarreaub80c2302007-11-30 20:51:32 +01002957
Willy Tarreau59234e92008-11-30 23:51:27 +01002958 if (ret) {
2959 /* we fail this request, let's return 503 service unavail */
2960 txn->status = 503;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02002961 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreaue7dff022015-04-03 01:14:29 +02002962 if (!(s->flags & SF_ERR_MASK))
2963 s->flags |= SF_ERR_LOCAL; /* we don't want a real error here */
Willy Tarreau59234e92008-11-30 23:51:27 +01002964 goto return_prx_cond;
Willy Tarreaub80c2302007-11-30 20:51:32 +01002965 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002966 }
Willy Tarreaua5555ec2008-11-30 19:02:32 +01002967
Willy Tarreau59234e92008-11-30 23:51:27 +01002968 /* nothing to fail, let's reply normaly */
2969 txn->status = 200;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02002970 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreaue7dff022015-04-03 01:14:29 +02002971 if (!(s->flags & SF_ERR_MASK))
2972 s->flags |= SF_ERR_LOCAL; /* we don't want a real error here */
Willy Tarreau59234e92008-11-30 23:51:27 +01002973 goto return_prx_cond;
2974 }
2975
2976 /*
2977 * 3: Maybe we have to copy the original REQURI for the logs ?
2978 * Note: we cannot log anymore if the request has been
2979 * classified as invalid.
2980 */
2981 if (unlikely(s->logs.logwait & LW_REQ)) {
2982 /* we have a complete HTTP request that we must log */
2983 if ((txn->uri = pool_alloc2(pool2_requri)) != NULL) {
2984 int urilen = msg->sl.rq.l;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002985
Willy Tarreau59234e92008-11-30 23:51:27 +01002986 if (urilen >= REQURI_LEN)
2987 urilen = REQURI_LEN - 1;
Willy Tarreau9b28e032012-10-12 23:49:43 +02002988 memcpy(txn->uri, req->buf->p, urilen);
Willy Tarreau59234e92008-11-30 23:51:27 +01002989 txn->uri[urilen] = 0;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002990
Willy Tarreaud79a3b22012-12-28 09:40:16 +01002991 if (!(s->logs.logwait &= ~(LW_REQ|LW_INIT)))
Willy Tarreau59234e92008-11-30 23:51:27 +01002992 s->do_log(s);
2993 } else {
2994 Alert("HTTP logging : out of memory.\n");
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002995 }
Willy Tarreau59234e92008-11-30 23:51:27 +01002996 }
Willy Tarreau06619262006-12-17 08:37:22 +01002997
Willy Tarreau91852eb2015-05-01 13:26:00 +02002998 /* RFC7230#2.6 has enforced the format of the HTTP version string to be
2999 * exactly one digit "." one digit. This check may be disabled using
3000 * option accept-invalid-http-request.
3001 */
3002 if (!(sess->fe->options2 & PR_O2_REQBUG_OK)) {
3003 if (msg->sl.rq.v_l != 8) {
3004 msg->err_pos = msg->sl.rq.v;
3005 goto return_bad_req;
3006 }
3007
3008 if (req->buf->p[msg->sl.rq.v + 4] != '/' ||
3009 !isdigit((unsigned char)req->buf->p[msg->sl.rq.v + 5]) ||
3010 req->buf->p[msg->sl.rq.v + 6] != '.' ||
3011 !isdigit((unsigned char)req->buf->p[msg->sl.rq.v + 7])) {
3012 msg->err_pos = msg->sl.rq.v + 4;
3013 goto return_bad_req;
3014 }
3015 }
Willy Tarreau13317662015-05-01 13:47:08 +02003016 else {
3017 /* 4. We may have to convert HTTP/0.9 requests to HTTP/1.0 */
3018 if (unlikely(msg->sl.rq.v_l == 0) && !http_upgrade_v09_to_v10(txn))
3019 goto return_bad_req;
3020 }
Willy Tarreau91852eb2015-05-01 13:26:00 +02003021
Willy Tarreau5b154472009-12-21 20:11:07 +01003022 /* ... and check if the request is HTTP/1.1 or above */
3023 if ((msg->sl.rq.v_l == 8) &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02003024 ((req->buf->p[msg->sl.rq.v + 5] > '1') ||
3025 ((req->buf->p[msg->sl.rq.v + 5] == '1') &&
3026 (req->buf->p[msg->sl.rq.v + 7] >= '1'))))
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003027 msg->flags |= HTTP_MSGF_VER_11;
Willy Tarreau5b154472009-12-21 20:11:07 +01003028
3029 /* "connection" has not been parsed yet */
Willy Tarreau50fc7772012-11-11 22:19:57 +01003030 txn->flags &= ~(TX_HDR_CONN_PRS | TX_HDR_CONN_CLO | TX_HDR_CONN_KAL | TX_HDR_CONN_UPG);
Willy Tarreau5b154472009-12-21 20:11:07 +01003031
Willy Tarreau88d349d2010-01-25 12:15:43 +01003032 /* if the frontend has "option http-use-proxy-header", we'll check if
3033 * we have what looks like a proxied connection instead of a connection,
3034 * and in this case set the TX_USE_PX_CONN flag to use Proxy-connection.
3035 * Note that this is *not* RFC-compliant, however browsers and proxies
3036 * happen to do that despite being non-standard :-(
3037 * We consider that a request not beginning with either '/' or '*' is
3038 * a proxied connection, which covers both "scheme://location" and
3039 * CONNECT ip:port.
3040 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003041 if ((sess->fe->options2 & PR_O2_USE_PXHDR) &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02003042 req->buf->p[msg->sl.rq.u] != '/' && req->buf->p[msg->sl.rq.u] != '*')
Willy Tarreau88d349d2010-01-25 12:15:43 +01003043 txn->flags |= TX_USE_PX_CONN;
3044
Willy Tarreaue8e785b2009-12-26 15:34:26 +01003045 /* transfer length unknown*/
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003046 msg->flags &= ~HTTP_MSGF_XFER_LEN;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01003047
Willy Tarreau59234e92008-11-30 23:51:27 +01003048 /* 5: we may need to capture headers */
Willy Tarreaucb7dd012015-04-03 22:16:32 +02003049 if (unlikely((s->logs.logwait & LW_REQHDR) && s->req_cap))
Willy Tarreau9b28e032012-10-12 23:49:43 +02003050 capture_headers(req->buf->p, &txn->hdr_idx,
Willy Tarreaucb7dd012015-04-03 22:16:32 +02003051 s->req_cap, sess->fe->req_cap);
Willy Tarreau11382812008-07-09 16:18:21 +02003052
Willy Tarreau557f1992015-05-01 10:05:17 +02003053 /* 6: determine the transfer-length according to RFC2616 #4.4, updated
3054 * by RFC7230#3.3.3 :
Willy Tarreaue8e785b2009-12-26 15:34:26 +01003055 *
Willy Tarreau557f1992015-05-01 10:05:17 +02003056 * The length of a message body is determined by one of the following
3057 * (in order of precedence):
Willy Tarreau32b47f42009-10-18 20:55:02 +02003058 *
Willy Tarreau557f1992015-05-01 10:05:17 +02003059 * 1. Any response to a HEAD request and any response with a 1xx
3060 * (Informational), 204 (No Content), or 304 (Not Modified) status
3061 * code is always terminated by the first empty line after the
3062 * header fields, regardless of the header fields present in the
3063 * message, and thus cannot contain a message body.
Willy Tarreau32b47f42009-10-18 20:55:02 +02003064 *
Willy Tarreau557f1992015-05-01 10:05:17 +02003065 * 2. Any 2xx (Successful) response to a CONNECT request implies that
3066 * the connection will become a tunnel immediately after the empty
3067 * line that concludes the header fields. A client MUST ignore any
3068 * Content-Length or Transfer-Encoding header fields received in
3069 * such a message.
Willy Tarreaue8e785b2009-12-26 15:34:26 +01003070 *
Willy Tarreau557f1992015-05-01 10:05:17 +02003071 * 3. If a Transfer-Encoding header field is present and the chunked
3072 * transfer coding (Section 4.1) is the final encoding, the message
3073 * body length is determined by reading and decoding the chunked
3074 * data until the transfer coding indicates the data is complete.
3075 *
3076 * If a Transfer-Encoding header field is present in a response and
3077 * the chunked transfer coding is not the final encoding, the
3078 * message body length is determined by reading the connection until
3079 * it is closed by the server. If a Transfer-Encoding header field
3080 * is present in a request and the chunked transfer coding is not
3081 * the final encoding, the message body length cannot be determined
3082 * reliably; the server MUST respond with the 400 (Bad Request)
3083 * status code and then close the connection.
3084 *
3085 * If a message is received with both a Transfer-Encoding and a
3086 * Content-Length header field, the Transfer-Encoding overrides the
3087 * Content-Length. Such a message might indicate an attempt to
3088 * perform request smuggling (Section 9.5) or response splitting
3089 * (Section 9.4) and ought to be handled as an error. A sender MUST
3090 * remove the received Content-Length field prior to forwarding such
3091 * a message downstream.
3092 *
3093 * 4. If a message is received without Transfer-Encoding and with
3094 * either multiple Content-Length header fields having differing
3095 * field-values or a single Content-Length header field having an
3096 * invalid value, then the message framing is invalid and the
3097 * recipient MUST treat it as an unrecoverable error. If this is a
3098 * request message, the server MUST respond with a 400 (Bad Request)
3099 * status code and then close the connection. If this is a response
3100 * message received by a proxy, the proxy MUST close the connection
3101 * to the server, discard the received response, and send a 502 (Bad
3102 * Gateway) response to the client. If this is a response message
3103 * received by a user agent, the user agent MUST close the
3104 * connection to the server and discard the received response.
3105 *
3106 * 5. If a valid Content-Length header field is present without
3107 * Transfer-Encoding, its decimal value defines the expected message
3108 * body length in octets. If the sender closes the connection or
3109 * the recipient times out before the indicated number of octets are
3110 * received, the recipient MUST consider the message to be
3111 * incomplete and close the connection.
3112 *
3113 * 6. If this is a request message and none of the above are true, then
3114 * the message body length is zero (no message body is present).
3115 *
3116 * 7. Otherwise, this is a response message without a declared message
3117 * body length, so the message body length is determined by the
3118 * number of octets received prior to the server closing the
3119 * connection.
Willy Tarreau32b47f42009-10-18 20:55:02 +02003120 */
3121
Willy Tarreau32b47f42009-10-18 20:55:02 +02003122 ctx.idx = 0;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01003123 /* set TE_CHNK and XFER_LEN only if "chunked" is seen last */
Willy Tarreau4979d5c2015-05-01 10:06:30 +02003124 while (http_find_header2("Transfer-Encoding", 17, req->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01003125 if (ctx.vlen == 7 && strncasecmp(ctx.line + ctx.val, "chunked", 7) == 0)
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003126 msg->flags |= (HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN);
3127 else if (msg->flags & HTTP_MSGF_TE_CHNK) {
Willy Tarreau34dfc602015-05-01 10:09:49 +02003128 /* chunked not last, return badreq */
3129 goto return_bad_req;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01003130 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02003131 }
3132
Willy Tarreau1c913912015-04-30 10:57:51 +02003133 /* Chunked requests must have their content-length removed */
Willy Tarreau32b47f42009-10-18 20:55:02 +02003134 ctx.idx = 0;
Willy Tarreau1c913912015-04-30 10:57:51 +02003135 if (msg->flags & HTTP_MSGF_TE_CHNK) {
3136 while (http_find_header2("Content-Length", 14, req->buf->p, &txn->hdr_idx, &ctx))
3137 http_remove_header2(msg, &txn->hdr_idx, &ctx);
3138 }
Willy Tarreau34dfc602015-05-01 10:09:49 +02003139 else while (http_find_header2("Content-Length", 14, req->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreau32b47f42009-10-18 20:55:02 +02003140 signed long long cl;
3141
Willy Tarreauad14f752011-09-02 20:33:27 +02003142 if (!ctx.vlen) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02003143 msg->err_pos = ctx.line + ctx.val - req->buf->p;
Willy Tarreau32b47f42009-10-18 20:55:02 +02003144 goto return_bad_req;
Willy Tarreauad14f752011-09-02 20:33:27 +02003145 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02003146
Willy Tarreauad14f752011-09-02 20:33:27 +02003147 if (strl2llrc(ctx.line + ctx.val, ctx.vlen, &cl)) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02003148 msg->err_pos = ctx.line + ctx.val - req->buf->p;
Willy Tarreau32b47f42009-10-18 20:55:02 +02003149 goto return_bad_req; /* parse failure */
Willy Tarreauad14f752011-09-02 20:33:27 +02003150 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02003151
Willy Tarreauad14f752011-09-02 20:33:27 +02003152 if (cl < 0) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02003153 msg->err_pos = ctx.line + ctx.val - req->buf->p;
Willy Tarreau32b47f42009-10-18 20:55:02 +02003154 goto return_bad_req;
Willy Tarreauad14f752011-09-02 20:33:27 +02003155 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02003156
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003157 if ((msg->flags & HTTP_MSGF_CNT_LEN) && (msg->chunk_len != cl)) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02003158 msg->err_pos = ctx.line + ctx.val - req->buf->p;
Willy Tarreau32b47f42009-10-18 20:55:02 +02003159 goto return_bad_req; /* already specified, was different */
Willy Tarreauad14f752011-09-02 20:33:27 +02003160 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02003161
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003162 msg->flags |= HTTP_MSGF_CNT_LEN | HTTP_MSGF_XFER_LEN;
Willy Tarreau124d9912011-03-01 20:30:48 +01003163 msg->body_len = msg->chunk_len = cl;
Willy Tarreau32b47f42009-10-18 20:55:02 +02003164 }
3165
Willy Tarreau34dfc602015-05-01 10:09:49 +02003166 /* even bodyless requests have a known length */
3167 msg->flags |= HTTP_MSGF_XFER_LEN;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01003168
Willy Tarreau179085c2014-04-28 16:48:56 +02003169 /* Until set to anything else, the connection mode is set as Keep-Alive. It will
3170 * only change if both the request and the config reference something else.
3171 * Option httpclose by itself sets tunnel mode where headers are mangled.
3172 * However, if another mode is set, it will affect it (eg: server-close/
3173 * keep-alive + httpclose = close). Note that we avoid to redo the same work
3174 * if FE and BE have the same settings (common). The method consists in
3175 * checking if options changed between the two calls (implying that either
3176 * one is non-null, or one of them is non-null and we are there for the first
3177 * time.
3178 */
3179 if (!(txn->flags & TX_HDR_CONN_PRS) ||
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003180 ((sess->fe->options & PR_O_HTTP_MODE) != (s->be->options & PR_O_HTTP_MODE)))
Willy Tarreau4e21ff92014-09-30 18:44:22 +02003181 http_adjust_conn_mode(s, txn, msg);
Willy Tarreau179085c2014-04-28 16:48:56 +02003182
Willy Tarreau9fbe18e2015-05-01 22:42:08 +02003183 /* we may have to wait for the request's body */
3184 if ((s->be->options & PR_O_WREQ_BODY) &&
3185 (msg->body_len || (msg->flags & HTTP_MSGF_TE_CHNK)))
3186 req->analysers |= AN_REQ_HTTP_BODY;
3187
Willy Tarreaud787e662009-07-07 10:14:51 +02003188 /* end of job, return OK */
Willy Tarreau3a816292009-07-07 10:55:49 +02003189 req->analysers &= ~an_bit;
Willy Tarreaud787e662009-07-07 10:14:51 +02003190 req->analyse_exp = TICK_ETERNITY;
3191 return 1;
3192
3193 return_bad_req:
3194 /* We centralize bad requests processing here */
3195 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
3196 /* we detected a parsing error. We want to archive this request
3197 * in the dedicated proxy area for later troubleshooting.
3198 */
Willy Tarreau10e61cb2017-01-04 14:51:22 +01003199 http_capture_bad_message(&sess->fe->invalid_req, s, msg, msg->err_state, sess->fe);
Willy Tarreaud787e662009-07-07 10:14:51 +02003200 }
3201
Willy Tarreau10e61cb2017-01-04 14:51:22 +01003202 txn->req.err_state = txn->req.msg_state;
Willy Tarreaud787e662009-07-07 10:14:51 +02003203 txn->req.msg_state = HTTP_MSG_ERROR;
3204 txn->status = 400;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02003205 http_reply_and_close(s, txn->status, http_error_message(s));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003206
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003207 sess->fe->fe_counters.failed_req++;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02003208 if (sess->listener->counters)
3209 sess->listener->counters->failed_req++;
Willy Tarreaud787e662009-07-07 10:14:51 +02003210
3211 return_prx_cond:
Willy Tarreaue7dff022015-04-03 01:14:29 +02003212 if (!(s->flags & SF_ERR_MASK))
3213 s->flags |= SF_ERR_PRXCOND;
3214 if (!(s->flags & SF_FINST_MASK))
3215 s->flags |= SF_FINST_R;
Willy Tarreaud787e662009-07-07 10:14:51 +02003216
Christopher Faulet0184ea72017-01-05 14:06:34 +01003217 req->analysers &= AN_REQ_FLT_END;
Willy Tarreaud787e662009-07-07 10:14:51 +02003218 req->analyse_exp = TICK_ETERNITY;
3219 return 0;
3220}
3221
Willy Tarreau4f8a83c2012-06-04 00:26:23 +02003222
Willy Tarreau347a35d2013-11-22 17:51:09 +01003223/* This function prepares an applet to handle the stats. It can deal with the
3224 * "100-continue" expectation, check that admin rules are met for POST requests,
3225 * and program a response message if something was unexpected. It cannot fail
3226 * and always relies on the stats applet to complete the job. It does not touch
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003227 * analysers nor counters, which are left to the caller. It does not touch
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003228 * s->target which is supposed to already point to the stats applet. The caller
Willy Tarreau87b09662015-04-03 00:22:06 +02003229 * is expected to have already assigned an appctx to the stream.
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003230 */
Willy Tarreau87b09662015-04-03 00:22:06 +02003231int http_handle_stats(struct stream *s, struct channel *req)
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003232{
3233 struct stats_admin_rule *stats_admin_rule;
Willy Tarreau350f4872014-11-28 14:42:25 +01003234 struct stream_interface *si = &s->si[1];
Willy Tarreau192252e2015-04-04 01:47:55 +02003235 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02003236 struct http_txn *txn = s->txn;
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003237 struct http_msg *msg = &txn->req;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003238 struct uri_auth *uri_auth = s->be->uri_auth;
3239 const char *uri, *h, *lookup;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003240 struct appctx *appctx;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003241
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003242 appctx = si_appctx(si);
3243 memset(&appctx->ctx.stats, 0, sizeof(appctx->ctx.stats));
3244 appctx->st1 = appctx->st2 = 0;
3245 appctx->ctx.stats.st_code = STAT_STATUS_INIT;
3246 appctx->ctx.stats.flags |= STAT_FMT_HTML; /* assume HTML mode by default */
Willy Tarreaueee5b512015-04-03 23:46:31 +02003247 if ((msg->flags & HTTP_MSGF_VER_11) && (s->txn->meth != HTTP_METH_HEAD))
Willy Tarreauaf3cf702014-04-22 22:19:53 +02003248 appctx->ctx.stats.flags |= STAT_CHUNKED;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003249
3250 uri = msg->chn->buf->p + msg->sl.rq.u;
3251 lookup = uri + uri_auth->uri_len;
3252
3253 for (h = lookup; h <= uri + msg->sl.rq.u_l - 3; h++) {
3254 if (memcmp(h, ";up", 3) == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003255 appctx->ctx.stats.flags |= STAT_HIDE_DOWN;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003256 break;
3257 }
3258 }
3259
3260 if (uri_auth->refresh) {
3261 for (h = lookup; h <= uri + msg->sl.rq.u_l - 10; h++) {
3262 if (memcmp(h, ";norefresh", 10) == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003263 appctx->ctx.stats.flags |= STAT_NO_REFRESH;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003264 break;
3265 }
3266 }
3267 }
3268
3269 for (h = lookup; h <= uri + msg->sl.rq.u_l - 4; h++) {
3270 if (memcmp(h, ";csv", 4) == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003271 appctx->ctx.stats.flags &= ~STAT_FMT_HTML;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003272 break;
3273 }
3274 }
3275
Willy Tarreau1e62df92016-01-11 18:57:53 +01003276 for (h = lookup; h <= uri + msg->sl.rq.u_l - 6; h++) {
3277 if (memcmp(h, ";typed", 6) == 0) {
3278 appctx->ctx.stats.flags &= ~STAT_FMT_HTML;
3279 appctx->ctx.stats.flags |= STAT_FMT_TYPED;
3280 break;
3281 }
3282 }
3283
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003284 for (h = lookup; h <= uri + msg->sl.rq.u_l - 8; h++) {
3285 if (memcmp(h, ";st=", 4) == 0) {
3286 int i;
3287 h += 4;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003288 appctx->ctx.stats.st_code = STAT_STATUS_UNKN;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003289 for (i = STAT_STATUS_INIT + 1; i < STAT_STATUS_SIZE; i++) {
3290 if (strncmp(stat_status_codes[i], h, 4) == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003291 appctx->ctx.stats.st_code = i;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003292 break;
3293 }
3294 }
3295 break;
3296 }
3297 }
3298
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003299 appctx->ctx.stats.scope_str = 0;
3300 appctx->ctx.stats.scope_len = 0;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003301 for (h = lookup; h <= uri + msg->sl.rq.u_l - 8; h++) {
3302 if (memcmp(h, STAT_SCOPE_INPUT_NAME "=", strlen(STAT_SCOPE_INPUT_NAME) + 1) == 0) {
3303 int itx = 0;
3304 const char *h2;
3305 char scope_txt[STAT_SCOPE_TXT_MAXLEN + 1];
3306 const char *err;
3307
3308 h += strlen(STAT_SCOPE_INPUT_NAME) + 1;
3309 h2 = h;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003310 appctx->ctx.stats.scope_str = h2 - msg->chn->buf->p;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003311 while (*h != ';' && *h != '\0' && *h != '&' && *h != ' ' && *h != '\n') {
3312 itx++;
3313 h++;
3314 }
3315
3316 if (itx > STAT_SCOPE_TXT_MAXLEN)
3317 itx = STAT_SCOPE_TXT_MAXLEN;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003318 appctx->ctx.stats.scope_len = itx;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003319
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003320 /* scope_txt = search query, appctx->ctx.stats.scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003321 memcpy(scope_txt, h2, itx);
3322 scope_txt[itx] = '\0';
3323 err = invalid_char(scope_txt);
3324 if (err) {
3325 /* bad char in search text => clear scope */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003326 appctx->ctx.stats.scope_str = 0;
3327 appctx->ctx.stats.scope_len = 0;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003328 }
3329 break;
3330 }
3331 }
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003332
3333 /* now check whether we have some admin rules for this request */
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003334 list_for_each_entry(stats_admin_rule, &uri_auth->admin_rules, list) {
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003335 int ret = 1;
3336
3337 if (stats_admin_rule->cond) {
Willy Tarreau192252e2015-04-04 01:47:55 +02003338 ret = acl_exec_cond(stats_admin_rule->cond, s->be, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003339 ret = acl_pass(ret);
3340 if (stats_admin_rule->cond->pol == ACL_COND_UNLESS)
3341 ret = !ret;
3342 }
3343
3344 if (ret) {
3345 /* no rule, or the rule matches */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003346 appctx->ctx.stats.flags |= STAT_ADMIN;
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003347 break;
3348 }
3349 }
3350
3351 /* Was the status page requested with a POST ? */
Willy Tarreaub8cdf522015-05-29 01:09:15 +02003352 if (unlikely(txn->meth == HTTP_METH_POST && txn->req.body_len > 0)) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003353 if (appctx->ctx.stats.flags & STAT_ADMIN) {
Willy Tarreaucfe7fdd2014-04-26 22:08:32 +02003354 /* we'll need the request body, possibly after sending 100-continue */
Willy Tarreaub8cdf522015-05-29 01:09:15 +02003355 if (msg->msg_state < HTTP_MSG_CHUNK_SIZE)
3356 req->analysers |= AN_REQ_HTTP_BODY;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003357 appctx->st0 = STAT_HTTP_POST;
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003358 }
Willy Tarreau347a35d2013-11-22 17:51:09 +01003359 else {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003360 appctx->ctx.stats.st_code = STAT_STATUS_DENY;
3361 appctx->st0 = STAT_HTTP_LAST;
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003362 }
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003363 }
Willy Tarreau347a35d2013-11-22 17:51:09 +01003364 else {
3365 /* So it was another method (GET/HEAD) */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003366 appctx->st0 = STAT_HTTP_HEAD;
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003367 }
3368
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003369 s->task->nice = -32; /* small boost for HTTP statistics */
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003370 return 1;
3371}
3372
Lukas Tribus67db8df2013-06-23 17:37:13 +02003373/* Sets the TOS header in IPv4 and the traffic class header in IPv6 packets
3374 * (as per RFC3260 #4 and BCP37 #4.2 and #5.2).
3375 */
Vincent Bernat6e615892016-05-18 16:17:44 +02003376void inet_set_tos(int fd, const struct sockaddr_storage *from, int tos)
Lukas Tribus67db8df2013-06-23 17:37:13 +02003377{
3378#ifdef IP_TOS
Vincent Bernat6e615892016-05-18 16:17:44 +02003379 if (from->ss_family == AF_INET)
Lukas Tribus67db8df2013-06-23 17:37:13 +02003380 setsockopt(fd, IPPROTO_IP, IP_TOS, &tos, sizeof(tos));
3381#endif
3382#ifdef IPV6_TCLASS
Vincent Bernat6e615892016-05-18 16:17:44 +02003383 if (from->ss_family == AF_INET6) {
3384 if (IN6_IS_ADDR_V4MAPPED(&((struct sockaddr_in6 *)from)->sin6_addr))
Lukas Tribus67db8df2013-06-23 17:37:13 +02003385 /* v4-mapped addresses need IP_TOS */
3386 setsockopt(fd, IPPROTO_IP, IP_TOS, &tos, sizeof(tos));
3387 else
3388 setsockopt(fd, IPPROTO_IPV6, IPV6_TCLASS, &tos, sizeof(tos));
3389 }
3390#endif
3391}
3392
Willy Tarreau87b09662015-04-03 00:22:06 +02003393int http_transform_header_str(struct stream* s, struct http_msg *msg,
Thierry FOURNIER5531f872015-03-16 11:15:50 +01003394 const char* name, unsigned int name_len,
3395 const char *str, struct my_regex *re,
3396 int action)
Sasha Pachev218f0642014-06-16 12:05:59 -06003397{
Thierry FOURNIER5a33ac72015-03-16 23:54:35 +01003398 struct hdr_ctx ctx;
3399 char *buf = msg->chn->buf->p;
Willy Tarreaueee5b512015-04-03 23:46:31 +02003400 struct hdr_idx *idx = &s->txn->hdr_idx;
Thierry FOURNIER5531f872015-03-16 11:15:50 +01003401 int (*http_find_hdr_func)(const char *name, int len, char *sol,
3402 struct hdr_idx *idx, struct hdr_ctx *ctx);
Thierry FOURNIER191f9ef2015-03-16 23:23:53 +01003403 struct chunk *output = get_trash_chunk();
3404
Thierry FOURNIER5a33ac72015-03-16 23:54:35 +01003405 ctx.idx = 0;
Sasha Pachev218f0642014-06-16 12:05:59 -06003406
Thierry FOURNIER191f9ef2015-03-16 23:23:53 +01003407 /* Choose the header browsing function. */
3408 switch (action) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003409 case ACT_HTTP_REPLACE_VAL:
Thierry FOURNIER191f9ef2015-03-16 23:23:53 +01003410 http_find_hdr_func = http_find_header2;
3411 break;
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003412 case ACT_HTTP_REPLACE_HDR:
Thierry FOURNIER191f9ef2015-03-16 23:23:53 +01003413 http_find_hdr_func = http_find_full_header2;
3414 break;
3415 default: /* impossible */
3416 return -1;
3417 }
3418
Thierry FOURNIER5a33ac72015-03-16 23:54:35 +01003419 while (http_find_hdr_func(name, name_len, buf, idx, &ctx)) {
3420 struct hdr_idx_elem *hdr = idx->v + ctx.idx;
Sasha Pachev218f0642014-06-16 12:05:59 -06003421 int delta;
Thierry FOURNIER5a33ac72015-03-16 23:54:35 +01003422 char *val = ctx.line + ctx.val;
3423 char* val_end = val + ctx.vlen;
Sasha Pachev218f0642014-06-16 12:05:59 -06003424
Thierry FOURNIER191f9ef2015-03-16 23:23:53 +01003425 if (!regex_exec_match2(re, val, val_end-val, MAX_MATCH, pmatch, 0))
3426 continue;
Sasha Pachev218f0642014-06-16 12:05:59 -06003427
Thierry FOURNIER5531f872015-03-16 11:15:50 +01003428 output->len = exp_replace(output->str, output->size, val, str, pmatch);
Thierry FOURNIER191f9ef2015-03-16 23:23:53 +01003429 if (output->len == -1)
Sasha Pachev218f0642014-06-16 12:05:59 -06003430 return -1;
Sasha Pachev218f0642014-06-16 12:05:59 -06003431
Thierry FOURNIER191f9ef2015-03-16 23:23:53 +01003432 delta = buffer_replace2(msg->chn->buf, val, val_end, output->str, output->len);
Sasha Pachev218f0642014-06-16 12:05:59 -06003433
3434 hdr->len += delta;
3435 http_msg_move_end(msg, delta);
Thierry FOURNIER191f9ef2015-03-16 23:23:53 +01003436
3437 /* Adjust the length of the current value of the index. */
Thierry FOURNIER5a33ac72015-03-16 23:54:35 +01003438 ctx.vlen += delta;
Sasha Pachev218f0642014-06-16 12:05:59 -06003439 }
3440
3441 return 0;
3442}
3443
Willy Tarreau87b09662015-04-03 00:22:06 +02003444static int http_transform_header(struct stream* s, struct http_msg *msg,
Thierry FOURNIER5531f872015-03-16 11:15:50 +01003445 const char* name, unsigned int name_len,
3446 struct list *fmt, struct my_regex *re,
3447 int action)
3448{
Christopher Faulet07a0fec2017-02-08 12:17:07 +01003449 struct chunk *replace;
3450 int ret = -1;
3451
3452 replace = alloc_trash_chunk();
3453 if (!replace)
3454 goto leave;
Thierry FOURNIER5531f872015-03-16 11:15:50 +01003455
3456 replace->len = build_logline(s, replace->str, replace->size, fmt);
3457 if (replace->len >= replace->size - 1)
Christopher Faulet07a0fec2017-02-08 12:17:07 +01003458 goto leave;
3459
3460 ret = http_transform_header_str(s, msg, name, name_len, replace->str, re, action);
Thierry FOURNIER5531f872015-03-16 11:15:50 +01003461
Christopher Faulet07a0fec2017-02-08 12:17:07 +01003462 leave:
3463 free_trash_chunk(replace);
3464 return ret;
Thierry FOURNIER5531f872015-03-16 11:15:50 +01003465}
3466
Willy Tarreau87b09662015-04-03 00:22:06 +02003467/* Executes the http-request rules <rules> for stream <s>, proxy <px> and
Willy Tarreau0b748332014-04-29 00:13:29 +02003468 * transaction <txn>. Returns the verdict of the first rule that prevents
3469 * further processing of the request (auth, deny, ...), and defaults to
3470 * HTTP_RULE_RES_STOP if it executed all rules or stopped on an allow, or
3471 * HTTP_RULE_RES_CONT if the last rule was reached. It may set the TX_CLTARPIT
Willy Tarreau58727ec2016-05-25 16:23:59 +02003472 * on txn->flags if it encounters a tarpit rule. If <deny_status> is not NULL
3473 * and a deny/tarpit rule is matched, it will be filled with this rule's deny
3474 * status.
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003475 */
Willy Tarreau0b748332014-04-29 00:13:29 +02003476enum rule_result
Willy Tarreau58727ec2016-05-25 16:23:59 +02003477http_req_get_intercept_rule(struct proxy *px, struct list *rules, struct stream *s, int *deny_status)
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003478{
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02003479 struct session *sess = strm_sess(s);
Willy Tarreau987e3fb2015-04-04 01:09:08 +02003480 struct http_txn *txn = s->txn;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003481 struct connection *cli_conn;
Thierry FOURNIERa28a9422015-08-04 19:35:46 +02003482 struct act_rule *rule;
Willy Tarreau20b0de52012-12-24 15:45:22 +01003483 struct hdr_ctx ctx;
Willy Tarreauae3c0102014-04-28 23:22:08 +02003484 const char *auth_realm;
Willy Tarreauacc98002015-09-27 23:34:39 +02003485 int act_flags = 0;
Thierry Fournier4b788f72016-06-01 13:35:36 +02003486 int len;
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003487
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01003488 /* If "the current_rule_list" match the executed rule list, we are in
3489 * resume condition. If a resume is needed it is always in the action
3490 * and never in the ACL or converters. In this case, we initialise the
3491 * current rule, and go to the action execution point.
3492 */
Willy Tarreau152b81e2015-04-20 13:26:17 +02003493 if (s->current_rule) {
3494 rule = s->current_rule;
3495 s->current_rule = NULL;
3496 if (s->current_rule_list == rules)
3497 goto resume_execution;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01003498 }
3499 s->current_rule_list = rules;
Willy Tarreau152b81e2015-04-20 13:26:17 +02003500
Willy Tarreauff011f22011-01-06 17:51:27 +01003501 list_for_each_entry(rule, rules, list) {
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003502
Willy Tarreau96257ec2012-12-27 10:46:37 +01003503 /* check optional condition */
Willy Tarreauff011f22011-01-06 17:51:27 +01003504 if (rule->cond) {
Willy Tarreau96257ec2012-12-27 10:46:37 +01003505 int ret;
3506
Willy Tarreau192252e2015-04-04 01:47:55 +02003507 ret = acl_exec_cond(rule->cond, px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003508 ret = acl_pass(ret);
3509
Willy Tarreauff011f22011-01-06 17:51:27 +01003510 if (rule->cond->pol == ACL_COND_UNLESS)
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003511 ret = !ret;
Willy Tarreau96257ec2012-12-27 10:46:37 +01003512
3513 if (!ret) /* condition not matched */
3514 continue;
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003515 }
3516
Willy Tarreauacc98002015-09-27 23:34:39 +02003517 act_flags |= ACT_FLAG_FIRST;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01003518resume_execution:
Willy Tarreau96257ec2012-12-27 10:46:37 +01003519 switch (rule->action) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003520 case ACT_ACTION_ALLOW:
Willy Tarreau0b748332014-04-29 00:13:29 +02003521 return HTTP_RULE_RES_STOP;
Willy Tarreau96257ec2012-12-27 10:46:37 +01003522
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003523 case ACT_ACTION_DENY:
Willy Tarreau58727ec2016-05-25 16:23:59 +02003524 if (deny_status)
3525 *deny_status = rule->deny_status;
Willy Tarreau0b748332014-04-29 00:13:29 +02003526 return HTTP_RULE_RES_DENY;
Willy Tarreau96257ec2012-12-27 10:46:37 +01003527
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003528 case ACT_HTTP_REQ_TARPIT:
Willy Tarreauccbcc372012-12-27 12:37:57 +01003529 txn->flags |= TX_CLTARPIT;
Willy Tarreau58727ec2016-05-25 16:23:59 +02003530 if (deny_status)
3531 *deny_status = rule->deny_status;
Willy Tarreau0b748332014-04-29 00:13:29 +02003532 return HTTP_RULE_RES_DENY;
Willy Tarreauccbcc372012-12-27 12:37:57 +01003533
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003534 case ACT_HTTP_REQ_AUTH:
Willy Tarreauae3c0102014-04-28 23:22:08 +02003535 /* Auth might be performed on regular http-req rules as well as on stats */
3536 auth_realm = rule->arg.auth.realm;
3537 if (!auth_realm) {
3538 if (px->uri_auth && rules == &px->uri_auth->http_req_rules)
3539 auth_realm = STATS_DEFAULT_REALM;
3540 else
3541 auth_realm = px->id;
3542 }
3543 /* send 401/407 depending on whether we use a proxy or not. We still
3544 * count one error, because normal browsing won't significantly
3545 * increase the counter but brute force attempts will.
3546 */
3547 chunk_printf(&trash, (txn->flags & TX_USE_PX_CONN) ? HTTP_407_fmt : HTTP_401_fmt, auth_realm);
3548 txn->status = (txn->flags & TX_USE_PX_CONN) ? 407 : 401;
Christopher Fauleta94e5a52015-12-09 15:55:06 +01003549 http_reply_and_close(s, txn->status, &trash);
Willy Tarreau87b09662015-04-03 00:22:06 +02003550 stream_inc_http_err_ctr(s);
Willy Tarreau0b748332014-04-29 00:13:29 +02003551 return HTTP_RULE_RES_ABRT;
Willy Tarreau96257ec2012-12-27 10:46:37 +01003552
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003553 case ACT_HTTP_REDIR:
Willy Tarreau0b748332014-04-29 00:13:29 +02003554 if (!http_apply_redirect_rule(rule->arg.redir, s, txn))
3555 return HTTP_RULE_RES_BADREQ;
3556 return HTTP_RULE_RES_DONE;
Willy Tarreau81499eb2012-12-27 12:19:02 +01003557
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003558 case ACT_HTTP_SET_NICE:
Willy Tarreauf4c43c12013-06-11 17:01:13 +02003559 s->task->nice = rule->arg.nice;
3560 break;
3561
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003562 case ACT_HTTP_SET_TOS:
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02003563 if ((cli_conn = objt_conn(sess->origin)) && conn_ctrl_ready(cli_conn))
Vincent Bernat6e615892016-05-18 16:17:44 +02003564 inet_set_tos(cli_conn->t.sock.fd, &cli_conn->addr.from, rule->arg.tos);
Willy Tarreau42cf39e2013-06-11 18:51:32 +02003565 break;
3566
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003567 case ACT_HTTP_SET_MARK:
Willy Tarreau51347ed2013-06-11 19:34:13 +02003568#ifdef SO_MARK
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02003569 if ((cli_conn = objt_conn(sess->origin)) && conn_ctrl_ready(cli_conn))
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003570 setsockopt(cli_conn->t.sock.fd, SOL_SOCKET, SO_MARK, &rule->arg.mark, sizeof(rule->arg.mark));
Willy Tarreau51347ed2013-06-11 19:34:13 +02003571#endif
3572 break;
3573
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003574 case ACT_HTTP_SET_LOGL:
Willy Tarreau9a355ec2013-06-11 17:45:46 +02003575 s->logs.level = rule->arg.loglevel;
3576 break;
3577
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003578 case ACT_HTTP_REPLACE_HDR:
3579 case ACT_HTTP_REPLACE_VAL:
Thierry FOURNIER5a33ac72015-03-16 23:54:35 +01003580 if (http_transform_header(s, &txn->req, rule->arg.hdr_add.name,
3581 rule->arg.hdr_add.name_len,
3582 &rule->arg.hdr_add.fmt,
3583 &rule->arg.hdr_add.re, rule->action))
Sasha Pachev218f0642014-06-16 12:05:59 -06003584 return HTTP_RULE_RES_BADREQ;
3585 break;
3586
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003587 case ACT_HTTP_DEL_HDR:
Willy Tarreau96257ec2012-12-27 10:46:37 +01003588 ctx.idx = 0;
3589 /* remove all occurrences of the header */
3590 while (http_find_header2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len,
3591 txn->req.chn->buf->p, &txn->hdr_idx, &ctx)) {
3592 http_remove_header2(&txn->req, &txn->hdr_idx, &ctx);
Willy Tarreau20b0de52012-12-24 15:45:22 +01003593 }
Willy Tarreau85603282015-01-21 20:39:27 +01003594 break;
Willy Tarreau96257ec2012-12-27 10:46:37 +01003595
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003596 case ACT_HTTP_SET_HDR:
3597 case ACT_HTTP_ADD_HDR:
Thierry Fournier4b788f72016-06-01 13:35:36 +02003598 /* The scope of the trash buffer must be limited to this function. The
3599 * build_logline() function can execute a lot of other function which
3600 * can use the trash buffer. So for limiting the scope of this global
3601 * buffer, we build first the header value using build_logline, and
3602 * after we store the header name.
3603 */
3604 len = rule->arg.hdr_add.name_len + 2,
3605 len += build_logline(s, trash.str + len, trash.size - len, &rule->arg.hdr_add.fmt);
Willy Tarreau96257ec2012-12-27 10:46:37 +01003606 memcpy(trash.str, rule->arg.hdr_add.name, rule->arg.hdr_add.name_len);
Thierry Fournier4b788f72016-06-01 13:35:36 +02003607 trash.str[rule->arg.hdr_add.name_len] = ':';
3608 trash.str[rule->arg.hdr_add.name_len + 1] = ' ';
3609 trash.len = len;
Willy Tarreau85603282015-01-21 20:39:27 +01003610
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003611 if (rule->action == ACT_HTTP_SET_HDR) {
Willy Tarreau85603282015-01-21 20:39:27 +01003612 /* remove all occurrences of the header */
3613 ctx.idx = 0;
3614 while (http_find_header2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len,
3615 txn->req.chn->buf->p, &txn->hdr_idx, &ctx)) {
3616 http_remove_header2(&txn->req, &txn->hdr_idx, &ctx);
3617 }
3618 }
3619
Willy Tarreau96257ec2012-12-27 10:46:37 +01003620 http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, trash.len);
3621 break;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003622
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003623 case ACT_HTTP_DEL_ACL:
3624 case ACT_HTTP_DEL_MAP: {
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003625 struct pat_ref *ref;
3626 char *key;
3627 int len;
3628
3629 /* collect reference */
3630 ref = pat_ref_lookup(rule->arg.map.ref);
3631 if (!ref)
3632 continue;
3633
3634 /* collect key */
3635 len = build_logline(s, trash.str, trash.size, &rule->arg.map.key);
3636 key = trash.str;
3637 key[len] = '\0';
3638
3639 /* perform update */
3640 /* returned code: 1=ok, 0=ko */
3641 pat_ref_delete(ref, key);
3642
3643 break;
3644 }
3645
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003646 case ACT_HTTP_ADD_ACL: {
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003647 struct pat_ref *ref;
3648 char *key;
3649 struct chunk *trash_key;
3650 int len;
3651
3652 trash_key = get_trash_chunk();
3653
3654 /* collect reference */
3655 ref = pat_ref_lookup(rule->arg.map.ref);
3656 if (!ref)
3657 continue;
3658
3659 /* collect key */
3660 len = build_logline(s, trash_key->str, trash_key->size, &rule->arg.map.key);
3661 key = trash_key->str;
3662 key[len] = '\0';
3663
3664 /* perform update */
3665 /* add entry only if it does not already exist */
3666 if (pat_ref_find_elt(ref, key) == NULL)
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02003667 pat_ref_add(ref, key, NULL, NULL);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003668
3669 break;
3670 }
3671
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003672 case ACT_HTTP_SET_MAP: {
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003673 struct pat_ref *ref;
3674 char *key, *value;
3675 struct chunk *trash_key, *trash_value;
3676 int len;
3677
3678 trash_key = get_trash_chunk();
3679 trash_value = get_trash_chunk();
3680
3681 /* collect reference */
3682 ref = pat_ref_lookup(rule->arg.map.ref);
3683 if (!ref)
3684 continue;
3685
3686 /* collect key */
3687 len = build_logline(s, trash_key->str, trash_key->size, &rule->arg.map.key);
3688 key = trash_key->str;
3689 key[len] = '\0';
3690
3691 /* collect value */
3692 len = build_logline(s, trash_value->str, trash_value->size, &rule->arg.map.value);
3693 value = trash_value->str;
3694 value[len] = '\0';
3695
3696 /* perform update */
3697 if (pat_ref_find_elt(ref, key) != NULL)
3698 /* update entry if it exists */
3699 pat_ref_set(ref, key, value, NULL);
3700 else
3701 /* insert a new entry */
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02003702 pat_ref_add(ref, key, value, NULL);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003703
3704 break;
3705 }
William Lallemand73025dd2014-04-24 14:38:37 +02003706
Thierry FOURNIER42148732015-09-02 17:17:33 +02003707 case ACT_CUSTOM:
Willy Tarreauacc98002015-09-27 23:34:39 +02003708 if ((px->options & PR_O_ABRT_CLOSE) && (s->req.flags & (CF_SHUTR|CF_READ_NULL|CF_READ_ERROR)))
3709 act_flags |= ACT_FLAG_FINAL;
Willy Tarreau39458682015-09-27 10:33:15 +02003710
Willy Tarreauacc98002015-09-27 23:34:39 +02003711 switch (rule->action_ptr(rule, px, s->sess, s, act_flags)) {
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +02003712 case ACT_RET_ERR:
3713 case ACT_RET_CONT:
3714 break;
Thierry FOURNIER42148732015-09-02 17:17:33 +02003715 case ACT_RET_STOP:
3716 return HTTP_RULE_RES_DONE;
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +02003717 case ACT_RET_YIELD:
Willy Tarreau152b81e2015-04-20 13:26:17 +02003718 s->current_rule = rule;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01003719 return HTTP_RULE_RES_YIELD;
3720 }
William Lallemand73025dd2014-04-24 14:38:37 +02003721 break;
3722
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003723 case ACT_ACTION_TRK_SC0 ... ACT_ACTION_TRK_SCMAX:
Willy Tarreau09448f72014-06-25 18:12:15 +02003724 /* Note: only the first valid tracking parameter of each
3725 * applies.
3726 */
3727
Ruoshan Huange4edc6b2016-07-14 15:07:45 +08003728 if (stkctr_entry(&s->stkctr[http_trk_idx(rule->action)]) == NULL) {
Willy Tarreau09448f72014-06-25 18:12:15 +02003729 struct stktable *t;
3730 struct stksess *ts;
3731 struct stktable_key *key;
3732 void *ptr;
3733
Thierry FOURNIER5ec63e02015-08-04 09:09:48 +02003734 t = rule->arg.trk_ctr.table.t;
3735 key = stktable_fetch_key(t, s->be, sess, s, SMP_OPT_DIR_REQ | SMP_OPT_FINAL, rule->arg.trk_ctr.expr, NULL);
Willy Tarreau09448f72014-06-25 18:12:15 +02003736
3737 if (key && (ts = stktable_get_entry(t, key))) {
Ruoshan Huange4edc6b2016-07-14 15:07:45 +08003738 stream_track_stkctr(&s->stkctr[http_trk_idx(rule->action)], t, ts);
Willy Tarreau09448f72014-06-25 18:12:15 +02003739
3740 /* let's count a new HTTP request as it's the first time we do it */
3741 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_REQ_CNT);
3742 if (ptr)
3743 stktable_data_cast(ptr, http_req_cnt)++;
3744
3745 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_REQ_RATE);
3746 if (ptr)
3747 update_freq_ctr_period(&stktable_data_cast(ptr, http_req_rate),
3748 t->data_arg[STKTABLE_DT_HTTP_REQ_RATE].u, 1);
3749
Ruoshan Huange4edc6b2016-07-14 15:07:45 +08003750 stkctr_set_flags(&s->stkctr[http_trk_idx(rule->action)], STKCTR_TRACK_CONTENT);
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02003751 if (sess->fe != s->be)
Ruoshan Huange4edc6b2016-07-14 15:07:45 +08003752 stkctr_set_flags(&s->stkctr[http_trk_idx(rule->action)], STKCTR_TRACK_BACKEND);
Willy Tarreau09448f72014-06-25 18:12:15 +02003753 }
3754 }
Adis Nezirovic2fbcafc2015-07-06 15:44:30 +02003755 break;
3756
Thierry FOURNIER22e49012015-08-05 19:13:48 +02003757 /* other flags exists, but normaly, they never be matched. */
3758 default:
3759 break;
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003760 }
3761 }
Willy Tarreau96257ec2012-12-27 10:46:37 +01003762
3763 /* we reached the end of the rules, nothing to report */
Willy Tarreau0b748332014-04-29 00:13:29 +02003764 return HTTP_RULE_RES_CONT;
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003765}
3766
Willy Tarreau71241ab2012-12-27 11:30:54 +01003767
Willy Tarreau51d861a2015-05-22 17:30:48 +02003768/* Executes the http-response rules <rules> for stream <s> and proxy <px>. It
3769 * returns one of 5 possible statuses: HTTP_RULE_RES_CONT, HTTP_RULE_RES_STOP,
3770 * HTTP_RULE_RES_DONE, HTTP_RULE_RES_YIELD, or HTTP_RULE_RES_BADREQ. If *CONT
3771 * is returned, the process can continue the evaluation of next rule list. If
3772 * *STOP or *DONE is returned, the process must stop the evaluation. If *BADREQ
3773 * is returned, it means the operation could not be processed and a server error
3774 * must be returned. It may set the TX_SVDENY on txn->flags if it encounters a
3775 * deny rule. If *YIELD is returned, the caller must call again the function
3776 * with the same context.
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003777 */
Thierry FOURNIER9e2ef992015-02-25 13:51:19 +01003778static enum rule_result
Willy Tarreau987e3fb2015-04-04 01:09:08 +02003779http_res_get_intercept_rule(struct proxy *px, struct list *rules, struct stream *s)
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003780{
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02003781 struct session *sess = strm_sess(s);
Willy Tarreau987e3fb2015-04-04 01:09:08 +02003782 struct http_txn *txn = s->txn;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003783 struct connection *cli_conn;
Thierry FOURNIERa28a9422015-08-04 19:35:46 +02003784 struct act_rule *rule;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003785 struct hdr_ctx ctx;
Willy Tarreauacc98002015-09-27 23:34:39 +02003786 int act_flags = 0;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003787
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01003788 /* If "the current_rule_list" match the executed rule list, we are in
3789 * resume condition. If a resume is needed it is always in the action
3790 * and never in the ACL or converters. In this case, we initialise the
3791 * current rule, and go to the action execution point.
3792 */
Willy Tarreau152b81e2015-04-20 13:26:17 +02003793 if (s->current_rule) {
3794 rule = s->current_rule;
3795 s->current_rule = NULL;
3796 if (s->current_rule_list == rules)
3797 goto resume_execution;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01003798 }
3799 s->current_rule_list = rules;
Willy Tarreau152b81e2015-04-20 13:26:17 +02003800
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003801 list_for_each_entry(rule, rules, list) {
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003802
3803 /* check optional condition */
3804 if (rule->cond) {
3805 int ret;
3806
Willy Tarreau192252e2015-04-04 01:47:55 +02003807 ret = acl_exec_cond(rule->cond, px, sess, s, SMP_OPT_DIR_RES|SMP_OPT_FINAL);
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003808 ret = acl_pass(ret);
3809
3810 if (rule->cond->pol == ACL_COND_UNLESS)
3811 ret = !ret;
3812
3813 if (!ret) /* condition not matched */
3814 continue;
3815 }
3816
Willy Tarreauacc98002015-09-27 23:34:39 +02003817 act_flags |= ACT_FLAG_FIRST;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01003818resume_execution:
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003819 switch (rule->action) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003820 case ACT_ACTION_ALLOW:
Thierry FOURNIER9e2ef992015-02-25 13:51:19 +01003821 return HTTP_RULE_RES_STOP; /* "allow" rules are OK */
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003822
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003823 case ACT_ACTION_DENY:
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003824 txn->flags |= TX_SVDENY;
Thierry FOURNIER9e2ef992015-02-25 13:51:19 +01003825 return HTTP_RULE_RES_STOP;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003826
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003827 case ACT_HTTP_SET_NICE:
Willy Tarreauf4c43c12013-06-11 17:01:13 +02003828 s->task->nice = rule->arg.nice;
3829 break;
3830
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003831 case ACT_HTTP_SET_TOS:
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02003832 if ((cli_conn = objt_conn(sess->origin)) && conn_ctrl_ready(cli_conn))
Vincent Bernat6e615892016-05-18 16:17:44 +02003833 inet_set_tos(cli_conn->t.sock.fd, &cli_conn->addr.from, rule->arg.tos);
Willy Tarreau42cf39e2013-06-11 18:51:32 +02003834 break;
3835
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003836 case ACT_HTTP_SET_MARK:
Willy Tarreau51347ed2013-06-11 19:34:13 +02003837#ifdef SO_MARK
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02003838 if ((cli_conn = objt_conn(sess->origin)) && conn_ctrl_ready(cli_conn))
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003839 setsockopt(cli_conn->t.sock.fd, SOL_SOCKET, SO_MARK, &rule->arg.mark, sizeof(rule->arg.mark));
Willy Tarreau51347ed2013-06-11 19:34:13 +02003840#endif
3841 break;
3842
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003843 case ACT_HTTP_SET_LOGL:
Willy Tarreau9a355ec2013-06-11 17:45:46 +02003844 s->logs.level = rule->arg.loglevel;
3845 break;
3846
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003847 case ACT_HTTP_REPLACE_HDR:
3848 case ACT_HTTP_REPLACE_VAL:
Thierry FOURNIER5a33ac72015-03-16 23:54:35 +01003849 if (http_transform_header(s, &txn->rsp, rule->arg.hdr_add.name,
3850 rule->arg.hdr_add.name_len,
3851 &rule->arg.hdr_add.fmt,
3852 &rule->arg.hdr_add.re, rule->action))
Christopher Fauletcdade942017-02-08 12:41:31 +01003853 return HTTP_RULE_RES_BADREQ;
Sasha Pachev218f0642014-06-16 12:05:59 -06003854 break;
3855
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003856 case ACT_HTTP_DEL_HDR:
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003857 ctx.idx = 0;
3858 /* remove all occurrences of the header */
3859 while (http_find_header2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len,
3860 txn->rsp.chn->buf->p, &txn->hdr_idx, &ctx)) {
3861 http_remove_header2(&txn->rsp, &txn->hdr_idx, &ctx);
3862 }
Willy Tarreau85603282015-01-21 20:39:27 +01003863 break;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003864
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003865 case ACT_HTTP_SET_HDR:
3866 case ACT_HTTP_ADD_HDR:
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003867 chunk_printf(&trash, "%s: ", rule->arg.hdr_add.name);
3868 memcpy(trash.str, rule->arg.hdr_add.name, rule->arg.hdr_add.name_len);
3869 trash.len = rule->arg.hdr_add.name_len;
3870 trash.str[trash.len++] = ':';
3871 trash.str[trash.len++] = ' ';
3872 trash.len += build_logline(s, trash.str + trash.len, trash.size - trash.len, &rule->arg.hdr_add.fmt);
Willy Tarreau85603282015-01-21 20:39:27 +01003873
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003874 if (rule->action == ACT_HTTP_SET_HDR) {
Willy Tarreau85603282015-01-21 20:39:27 +01003875 /* remove all occurrences of the header */
3876 ctx.idx = 0;
3877 while (http_find_header2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len,
3878 txn->rsp.chn->buf->p, &txn->hdr_idx, &ctx)) {
3879 http_remove_header2(&txn->rsp, &txn->hdr_idx, &ctx);
3880 }
3881 }
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003882 http_header_add_tail2(&txn->rsp, &txn->hdr_idx, trash.str, trash.len);
3883 break;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003884
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003885 case ACT_HTTP_DEL_ACL:
3886 case ACT_HTTP_DEL_MAP: {
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003887 struct pat_ref *ref;
3888 char *key;
3889 int len;
3890
3891 /* collect reference */
3892 ref = pat_ref_lookup(rule->arg.map.ref);
3893 if (!ref)
3894 continue;
3895
3896 /* collect key */
3897 len = build_logline(s, trash.str, trash.size, &rule->arg.map.key);
3898 key = trash.str;
3899 key[len] = '\0';
3900
3901 /* perform update */
3902 /* returned code: 1=ok, 0=ko */
3903 pat_ref_delete(ref, key);
3904
3905 break;
3906 }
3907
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003908 case ACT_HTTP_ADD_ACL: {
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003909 struct pat_ref *ref;
3910 char *key;
3911 struct chunk *trash_key;
3912 int len;
3913
3914 trash_key = get_trash_chunk();
3915
3916 /* collect reference */
3917 ref = pat_ref_lookup(rule->arg.map.ref);
3918 if (!ref)
3919 continue;
3920
3921 /* collect key */
3922 len = build_logline(s, trash_key->str, trash_key->size, &rule->arg.map.key);
3923 key = trash_key->str;
3924 key[len] = '\0';
3925
3926 /* perform update */
3927 /* check if the entry already exists */
3928 if (pat_ref_find_elt(ref, key) == NULL)
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02003929 pat_ref_add(ref, key, NULL, NULL);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003930
3931 break;
3932 }
3933
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003934 case ACT_HTTP_SET_MAP: {
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003935 struct pat_ref *ref;
3936 char *key, *value;
3937 struct chunk *trash_key, *trash_value;
3938 int len;
3939
3940 trash_key = get_trash_chunk();
3941 trash_value = get_trash_chunk();
3942
3943 /* collect reference */
3944 ref = pat_ref_lookup(rule->arg.map.ref);
3945 if (!ref)
3946 continue;
3947
3948 /* collect key */
3949 len = build_logline(s, trash_key->str, trash_key->size, &rule->arg.map.key);
3950 key = trash_key->str;
3951 key[len] = '\0';
3952
3953 /* collect value */
3954 len = build_logline(s, trash_value->str, trash_value->size, &rule->arg.map.value);
3955 value = trash_value->str;
3956 value[len] = '\0';
3957
3958 /* perform update */
3959 if (pat_ref_find_elt(ref, key) != NULL)
3960 /* update entry if it exists */
3961 pat_ref_set(ref, key, value, NULL);
3962 else
3963 /* insert a new entry */
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02003964 pat_ref_add(ref, key, value, NULL);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003965
3966 break;
3967 }
William Lallemand73025dd2014-04-24 14:38:37 +02003968
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003969 case ACT_HTTP_REDIR:
Willy Tarreau51d861a2015-05-22 17:30:48 +02003970 if (!http_apply_redirect_rule(rule->arg.redir, s, txn))
3971 return HTTP_RULE_RES_BADREQ;
3972 return HTTP_RULE_RES_DONE;
3973
Ruoshan Huange4edc6b2016-07-14 15:07:45 +08003974 case ACT_ACTION_TRK_SC0 ... ACT_ACTION_TRK_SCMAX:
3975 /* Note: only the first valid tracking parameter of each
3976 * applies.
3977 */
3978
3979 if (stkctr_entry(&s->stkctr[http_trk_idx(rule->action)]) == NULL) {
3980 struct stktable *t;
3981 struct stksess *ts;
3982 struct stktable_key *key;
3983 void *ptr;
3984
3985 t = rule->arg.trk_ctr.table.t;
3986 key = stktable_fetch_key(t, s->be, sess, s, SMP_OPT_DIR_RES | SMP_OPT_FINAL, rule->arg.trk_ctr.expr, NULL);
3987
3988 if (key && (ts = stktable_get_entry(t, key))) {
3989 stream_track_stkctr(&s->stkctr[http_trk_idx(rule->action)], t, ts);
3990
3991 /* let's count a new HTTP request as it's the first time we do it */
3992 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_REQ_CNT);
3993 if (ptr)
3994 stktable_data_cast(ptr, http_req_cnt)++;
3995
3996 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_REQ_RATE);
3997 if (ptr)
3998 update_freq_ctr_period(&stktable_data_cast(ptr, http_req_rate),
3999 t->data_arg[STKTABLE_DT_HTTP_REQ_RATE].u, 1);
4000
4001 stkctr_set_flags(&s->stkctr[http_trk_idx(rule->action)], STKCTR_TRACK_CONTENT);
4002 if (sess->fe != s->be)
4003 stkctr_set_flags(&s->stkctr[http_trk_idx(rule->action)], STKCTR_TRACK_BACKEND);
4004
4005 /* When the client triggers a 4xx from the server, it's most often due
4006 * to a missing object or permission. These events should be tracked
4007 * because if they happen often, it may indicate a brute force or a
4008 * vulnerability scan. Normally this is done when receiving the response
4009 * but here we're tracking after this ought to have been done so we have
4010 * to do it on purpose.
4011 */
Willy Tarreau3146a4c2016-07-26 15:22:33 +02004012 if ((unsigned)(txn->status - 400) < 100) {
4013 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_ERR_CNT);
4014 if (ptr)
4015 stktable_data_cast(ptr, http_err_cnt)++;
4016
4017 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_ERR_RATE);
4018 if (ptr)
4019 update_freq_ctr_period(&stktable_data_cast(ptr, http_err_rate),
4020 t->data_arg[STKTABLE_DT_HTTP_ERR_RATE].u, 1);
4021 }
Ruoshan Huange4edc6b2016-07-14 15:07:45 +08004022 }
4023 }
4024 break;
4025
Thierry FOURNIER42148732015-09-02 17:17:33 +02004026 case ACT_CUSTOM:
Willy Tarreauacc98002015-09-27 23:34:39 +02004027 if ((px->options & PR_O_ABRT_CLOSE) && (s->req.flags & (CF_SHUTR|CF_READ_NULL|CF_READ_ERROR)))
4028 act_flags |= ACT_FLAG_FINAL;
Willy Tarreau39458682015-09-27 10:33:15 +02004029
Willy Tarreauacc98002015-09-27 23:34:39 +02004030 switch (rule->action_ptr(rule, px, s->sess, s, act_flags)) {
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +02004031 case ACT_RET_ERR:
4032 case ACT_RET_CONT:
4033 break;
Thierry FOURNIER42148732015-09-02 17:17:33 +02004034 case ACT_RET_STOP:
4035 return HTTP_RULE_RES_STOP;
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +02004036 case ACT_RET_YIELD:
Willy Tarreau152b81e2015-04-20 13:26:17 +02004037 s->current_rule = rule;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01004038 return HTTP_RULE_RES_YIELD;
4039 }
William Lallemand73025dd2014-04-24 14:38:37 +02004040 break;
4041
Thierry FOURNIER22e49012015-08-05 19:13:48 +02004042 /* other flags exists, but normaly, they never be matched. */
4043 default:
4044 break;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02004045 }
4046 }
4047
4048 /* we reached the end of the rules, nothing to report */
Thierry FOURNIER9e2ef992015-02-25 13:51:19 +01004049 return HTTP_RULE_RES_CONT;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02004050}
4051
4052
Willy Tarreau71241ab2012-12-27 11:30:54 +01004053/* Perform an HTTP redirect based on the information in <rule>. The function
4054 * returns non-zero on success, or zero in case of a, irrecoverable error such
4055 * as too large a request to build a valid response.
4056 */
Willy Tarreau87b09662015-04-03 00:22:06 +02004057static int http_apply_redirect_rule(struct redirect_rule *rule, struct stream *s, struct http_txn *txn)
Willy Tarreau71241ab2012-12-27 11:30:54 +01004058{
Willy Tarreaub329a312015-05-22 16:27:37 +02004059 struct http_msg *req = &txn->req;
4060 struct http_msg *res = &txn->rsp;
Willy Tarreau71241ab2012-12-27 11:30:54 +01004061 const char *msg_fmt;
Thierry FOURNIER0d945762017-01-28 07:39:53 +01004062 struct chunk *chunk;
4063 int ret = 0;
4064
4065 chunk = alloc_trash_chunk();
4066 if (!chunk)
4067 goto leave;
Willy Tarreau71241ab2012-12-27 11:30:54 +01004068
4069 /* build redirect message */
4070 switch(rule->code) {
Yves Lafon3e8d1ae2013-03-11 11:06:05 -04004071 case 308:
4072 msg_fmt = HTTP_308;
4073 break;
4074 case 307:
4075 msg_fmt = HTTP_307;
4076 break;
Willy Tarreau71241ab2012-12-27 11:30:54 +01004077 case 303:
4078 msg_fmt = HTTP_303;
4079 break;
4080 case 301:
4081 msg_fmt = HTTP_301;
4082 break;
4083 case 302:
4084 default:
4085 msg_fmt = HTTP_302;
4086 break;
4087 }
4088
Thierry FOURNIER0d945762017-01-28 07:39:53 +01004089 if (unlikely(!chunk_strcpy(chunk, msg_fmt)))
4090 goto leave;
Willy Tarreau71241ab2012-12-27 11:30:54 +01004091
4092 switch(rule->type) {
4093 case REDIRECT_TYPE_SCHEME: {
4094 const char *path;
4095 const char *host;
4096 struct hdr_ctx ctx;
4097 int pathlen;
4098 int hostlen;
4099
4100 host = "";
4101 hostlen = 0;
4102 ctx.idx = 0;
Willy Tarreaub329a312015-05-22 16:27:37 +02004103 if (http_find_header2("Host", 4, req->chn->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreau71241ab2012-12-27 11:30:54 +01004104 host = ctx.line + ctx.val;
4105 hostlen = ctx.vlen;
4106 }
4107
4108 path = http_get_path(txn);
4109 /* build message using path */
4110 if (path) {
Willy Tarreaub329a312015-05-22 16:27:37 +02004111 pathlen = req->sl.rq.u_l + (req->chn->buf->p + req->sl.rq.u) - path;
Willy Tarreau71241ab2012-12-27 11:30:54 +01004112 if (rule->flags & REDIRECT_FLAG_DROP_QS) {
4113 int qs = 0;
4114 while (qs < pathlen) {
4115 if (path[qs] == '?') {
4116 pathlen = qs;
4117 break;
4118 }
4119 qs++;
4120 }
4121 }
4122 } else {
4123 path = "/";
4124 pathlen = 1;
4125 }
4126
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01004127 if (rule->rdr_str) { /* this is an old "redirect" rule */
4128 /* check if we can add scheme + "://" + host + path */
Thierry FOURNIER0d945762017-01-28 07:39:53 +01004129 if (chunk->len + rule->rdr_len + 3 + hostlen + pathlen > chunk->size - 4)
4130 goto leave;
Willy Tarreau71241ab2012-12-27 11:30:54 +01004131
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01004132 /* add scheme */
Thierry FOURNIER0d945762017-01-28 07:39:53 +01004133 memcpy(chunk->str + chunk->len, rule->rdr_str, rule->rdr_len);
4134 chunk->len += rule->rdr_len;
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01004135 }
4136 else {
4137 /* add scheme with executing log format */
Thierry FOURNIER0d945762017-01-28 07:39:53 +01004138 chunk->len += build_logline(s, chunk->str + chunk->len, chunk->size - chunk->len, &rule->rdr_fmt);
Willy Tarreau71241ab2012-12-27 11:30:54 +01004139
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01004140 /* check if we can add scheme + "://" + host + path */
Thierry FOURNIER0d945762017-01-28 07:39:53 +01004141 if (chunk->len + 3 + hostlen + pathlen > chunk->size - 4)
4142 goto leave;
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01004143 }
Willy Tarreau71241ab2012-12-27 11:30:54 +01004144 /* add "://" */
Thierry FOURNIER0d945762017-01-28 07:39:53 +01004145 memcpy(chunk->str + chunk->len, "://", 3);
4146 chunk->len += 3;
Willy Tarreau71241ab2012-12-27 11:30:54 +01004147
4148 /* add host */
Thierry FOURNIER0d945762017-01-28 07:39:53 +01004149 memcpy(chunk->str + chunk->len, host, hostlen);
4150 chunk->len += hostlen;
Willy Tarreau71241ab2012-12-27 11:30:54 +01004151
4152 /* add path */
Thierry FOURNIER0d945762017-01-28 07:39:53 +01004153 memcpy(chunk->str + chunk->len, path, pathlen);
4154 chunk->len += pathlen;
Willy Tarreau71241ab2012-12-27 11:30:54 +01004155
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01004156 /* append a slash at the end of the location if needed and missing */
Thierry FOURNIER0d945762017-01-28 07:39:53 +01004157 if (chunk->len && chunk->str[chunk->len - 1] != '/' &&
Willy Tarreau71241ab2012-12-27 11:30:54 +01004158 (rule->flags & REDIRECT_FLAG_APPEND_SLASH)) {
Thierry FOURNIER0d945762017-01-28 07:39:53 +01004159 if (chunk->len > chunk->size - 5)
4160 goto leave;
4161 chunk->str[chunk->len] = '/';
4162 chunk->len++;
Willy Tarreau71241ab2012-12-27 11:30:54 +01004163 }
4164
4165 break;
4166 }
4167 case REDIRECT_TYPE_PREFIX: {
4168 const char *path;
4169 int pathlen;
4170
4171 path = http_get_path(txn);
4172 /* build message using path */
4173 if (path) {
Willy Tarreaub329a312015-05-22 16:27:37 +02004174 pathlen = req->sl.rq.u_l + (req->chn->buf->p + req->sl.rq.u) - path;
Willy Tarreau71241ab2012-12-27 11:30:54 +01004175 if (rule->flags & REDIRECT_FLAG_DROP_QS) {
4176 int qs = 0;
4177 while (qs < pathlen) {
4178 if (path[qs] == '?') {
4179 pathlen = qs;
4180 break;
4181 }
4182 qs++;
4183 }
4184 }
4185 } else {
4186 path = "/";
4187 pathlen = 1;
4188 }
4189
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01004190 if (rule->rdr_str) { /* this is an old "redirect" rule */
Thierry FOURNIER0d945762017-01-28 07:39:53 +01004191 if (chunk->len + rule->rdr_len + pathlen > chunk->size - 4)
4192 goto leave;
Willy Tarreau71241ab2012-12-27 11:30:54 +01004193
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01004194 /* add prefix. Note that if prefix == "/", we don't want to
4195 * add anything, otherwise it makes it hard for the user to
4196 * configure a self-redirection.
4197 */
4198 if (rule->rdr_len != 1 || *rule->rdr_str != '/') {
Thierry FOURNIER0d945762017-01-28 07:39:53 +01004199 memcpy(chunk->str + chunk->len, rule->rdr_str, rule->rdr_len);
4200 chunk->len += rule->rdr_len;
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01004201 }
4202 }
4203 else {
4204 /* add prefix with executing log format */
Thierry FOURNIER0d945762017-01-28 07:39:53 +01004205 chunk->len += build_logline(s, chunk->str + chunk->len, chunk->size - chunk->len, &rule->rdr_fmt);
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01004206
4207 /* Check length */
Thierry FOURNIER0d945762017-01-28 07:39:53 +01004208 if (chunk->len + pathlen > chunk->size - 4)
4209 goto leave;
Willy Tarreau71241ab2012-12-27 11:30:54 +01004210 }
4211
4212 /* add path */
Thierry FOURNIER0d945762017-01-28 07:39:53 +01004213 memcpy(chunk->str + chunk->len, path, pathlen);
4214 chunk->len += pathlen;
Willy Tarreau71241ab2012-12-27 11:30:54 +01004215
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01004216 /* append a slash at the end of the location if needed and missing */
Thierry FOURNIER0d945762017-01-28 07:39:53 +01004217 if (chunk->len && chunk->str[chunk->len - 1] != '/' &&
Willy Tarreau71241ab2012-12-27 11:30:54 +01004218 (rule->flags & REDIRECT_FLAG_APPEND_SLASH)) {
Thierry FOURNIER0d945762017-01-28 07:39:53 +01004219 if (chunk->len > chunk->size - 5)
4220 goto leave;
4221 chunk->str[chunk->len] = '/';
4222 chunk->len++;
Willy Tarreau71241ab2012-12-27 11:30:54 +01004223 }
4224
4225 break;
4226 }
4227 case REDIRECT_TYPE_LOCATION:
4228 default:
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01004229 if (rule->rdr_str) { /* this is an old "redirect" rule */
Thierry FOURNIER0d945762017-01-28 07:39:53 +01004230 if (chunk->len + rule->rdr_len > chunk->size - 4)
4231 goto leave;
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01004232
4233 /* add location */
Thierry FOURNIER0d945762017-01-28 07:39:53 +01004234 memcpy(chunk->str + chunk->len, rule->rdr_str, rule->rdr_len);
4235 chunk->len += rule->rdr_len;
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01004236 }
4237 else {
4238 /* add location with executing log format */
Thierry FOURNIER0d945762017-01-28 07:39:53 +01004239 chunk->len += build_logline(s, chunk->str + chunk->len, chunk->size - chunk->len, &rule->rdr_fmt);
Willy Tarreau71241ab2012-12-27 11:30:54 +01004240
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01004241 /* Check left length */
Thierry FOURNIER0d945762017-01-28 07:39:53 +01004242 if (chunk->len > chunk->size - 4)
4243 goto leave;
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01004244 }
Willy Tarreau71241ab2012-12-27 11:30:54 +01004245 break;
4246 }
4247
4248 if (rule->cookie_len) {
Thierry FOURNIER0d945762017-01-28 07:39:53 +01004249 memcpy(chunk->str + chunk->len, "\r\nSet-Cookie: ", 14);
4250 chunk->len += 14;
4251 memcpy(chunk->str + chunk->len, rule->cookie_str, rule->cookie_len);
4252 chunk->len += rule->cookie_len;
Willy Tarreau71241ab2012-12-27 11:30:54 +01004253 }
4254
Willy Tarreau19b14122017-02-28 09:48:11 +01004255 /* add end of headers and the keep-alive/close status. */
Willy Tarreau71241ab2012-12-27 11:30:54 +01004256 txn->status = rule->code;
4257 /* let's log the request time */
4258 s->logs.tv_request = now;
4259
Willy Tarreau19b14122017-02-28 09:48:11 +01004260 if ((req->flags & HTTP_MSGF_XFER_LEN) &&
Willy Tarreau2de8a502015-05-28 17:23:54 +02004261 ((!(req->flags & HTTP_MSGF_TE_CHNK) && !req->body_len) || (req->msg_state == HTTP_MSG_DONE)) &&
Willy Tarreau71241ab2012-12-27 11:30:54 +01004262 ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL ||
4263 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL)) {
4264 /* keep-alive possible */
Willy Tarreaub329a312015-05-22 16:27:37 +02004265 if (!(req->flags & HTTP_MSGF_VER_11)) {
Willy Tarreau71241ab2012-12-27 11:30:54 +01004266 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
Thierry FOURNIER0d945762017-01-28 07:39:53 +01004267 memcpy(chunk->str + chunk->len, "\r\nProxy-Connection: keep-alive", 30);
4268 chunk->len += 30;
Willy Tarreau71241ab2012-12-27 11:30:54 +01004269 } else {
Thierry FOURNIER0d945762017-01-28 07:39:53 +01004270 memcpy(chunk->str + chunk->len, "\r\nConnection: keep-alive", 24);
4271 chunk->len += 24;
Willy Tarreau71241ab2012-12-27 11:30:54 +01004272 }
4273 }
Thierry FOURNIER0d945762017-01-28 07:39:53 +01004274 memcpy(chunk->str + chunk->len, "\r\n\r\n", 4);
4275 chunk->len += 4;
4276 FLT_STRM_CB(s, flt_http_reply(s, txn->status, chunk));
4277 bo_inject(res->chn, chunk->str, chunk->len);
Willy Tarreau71241ab2012-12-27 11:30:54 +01004278 /* "eat" the request */
Willy Tarreaub329a312015-05-22 16:27:37 +02004279 bi_fast_delete(req->chn->buf, req->sov);
4280 req->next -= req->sov;
4281 req->sov = 0;
Christopher Faulet0184ea72017-01-05 14:06:34 +01004282 s->req.analysers = AN_REQ_HTTP_XFER_BODY | (s->req.analysers & AN_REQ_FLT_END);
4283 s->res.analysers = AN_RES_HTTP_XFER_BODY | (s->req.analysers & AN_RES_FLT_END);
Willy Tarreaub329a312015-05-22 16:27:37 +02004284 req->msg_state = HTTP_MSG_CLOSED;
4285 res->msg_state = HTTP_MSG_DONE;
Willy Tarreau51d861a2015-05-22 17:30:48 +02004286 /* Trim any possible response */
4287 res->chn->buf->i = 0;
4288 res->next = res->sov = 0;
Willy Tarreau71241ab2012-12-27 11:30:54 +01004289 } else {
4290 /* keep-alive not possible */
4291 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
Thierry FOURNIER0d945762017-01-28 07:39:53 +01004292 memcpy(chunk->str + chunk->len, "\r\nProxy-Connection: close\r\n\r\n", 29);
4293 chunk->len += 29;
Willy Tarreau71241ab2012-12-27 11:30:54 +01004294 } else {
Thierry FOURNIER0d945762017-01-28 07:39:53 +01004295 memcpy(chunk->str + chunk->len, "\r\nConnection: close\r\n\r\n", 23);
4296 chunk->len += 23;
Willy Tarreau71241ab2012-12-27 11:30:54 +01004297 }
Thierry FOURNIER0d945762017-01-28 07:39:53 +01004298 http_reply_and_close(s, txn->status, chunk);
Christopher Faulet0184ea72017-01-05 14:06:34 +01004299 req->chn->analysers &= AN_REQ_FLT_END;
Willy Tarreau71241ab2012-12-27 11:30:54 +01004300 }
4301
Willy Tarreaue7dff022015-04-03 01:14:29 +02004302 if (!(s->flags & SF_ERR_MASK))
4303 s->flags |= SF_ERR_LOCAL;
4304 if (!(s->flags & SF_FINST_MASK))
4305 s->flags |= SF_FINST_R;
Willy Tarreau71241ab2012-12-27 11:30:54 +01004306
Thierry FOURNIER0d945762017-01-28 07:39:53 +01004307 ret = 1;
4308 leave:
4309 free_trash_chunk(chunk);
4310 return ret;
Willy Tarreau71241ab2012-12-27 11:30:54 +01004311}
4312
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004313/* This stream analyser runs all HTTP request processing which is common to
4314 * frontends and backends, which means blocking ACLs, filters, connection-close,
4315 * reqadd, stats and redirects. This is performed for the designated proxy.
Willy Tarreaud787e662009-07-07 10:14:51 +02004316 * It returns 1 if the processing can continue on next analysers, or zero if it
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004317 * either needs more data or wants to immediately abort the request (eg: deny,
4318 * error, ...).
Willy Tarreaud787e662009-07-07 10:14:51 +02004319 */
Willy Tarreau87b09662015-04-03 00:22:06 +02004320int http_process_req_common(struct stream *s, struct channel *req, int an_bit, struct proxy *px)
Willy Tarreaud787e662009-07-07 10:14:51 +02004321{
Willy Tarreaufb0afa72015-04-03 14:46:27 +02004322 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02004323 struct http_txn *txn = s->txn;
Willy Tarreaud787e662009-07-07 10:14:51 +02004324 struct http_msg *msg = &txn->req;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004325 struct redirect_rule *rule;
Willy Tarreauf4f04122010-01-28 18:10:50 +01004326 struct cond_wordlist *wl;
Willy Tarreau0b748332014-04-29 00:13:29 +02004327 enum rule_result verdict;
Willy Tarreau58727ec2016-05-25 16:23:59 +02004328 int deny_status = HTTP_ERR_403;
Willy Tarreaud787e662009-07-07 10:14:51 +02004329
Willy Tarreau655dce92009-11-08 13:10:58 +01004330 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau51aecc72009-07-12 09:47:04 +02004331 /* we need more data */
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01004332 goto return_prx_yield;
Willy Tarreau51aecc72009-07-12 09:47:04 +02004333 }
4334
Willy Tarreau87b09662015-04-03 00:22:06 +02004335 DPRINTF(stderr,"[%u] %s: stream=%p b=%p, exp(r,w)=%u,%u bf=%08x bh=%d analysers=%02x\n",
Willy Tarreaud787e662009-07-07 10:14:51 +02004336 now_ms, __FUNCTION__,
4337 s,
4338 req,
4339 req->rex, req->wex,
4340 req->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02004341 req->buf->i,
Willy Tarreaud787e662009-07-07 10:14:51 +02004342 req->analysers);
4343
Willy Tarreau65410832014-04-28 21:25:43 +02004344 /* just in case we have some per-backend tracking */
Willy Tarreau87b09662015-04-03 00:22:06 +02004345 stream_inc_be_http_req_ctr(s);
Willy Tarreau65410832014-04-28 21:25:43 +02004346
Willy Tarreauf68a15a2011-01-06 16:53:21 +01004347 /* evaluate http-request rules */
Willy Tarreau0b748332014-04-29 00:13:29 +02004348 if (!LIST_ISEMPTY(&px->http_req_rules)) {
Willy Tarreau58727ec2016-05-25 16:23:59 +02004349 verdict = http_req_get_intercept_rule(px, &px->http_req_rules, s, &deny_status);
Willy Tarreau51425942010-02-01 10:40:19 +01004350
Willy Tarreau0b748332014-04-29 00:13:29 +02004351 switch (verdict) {
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01004352 case HTTP_RULE_RES_YIELD: /* some data miss, call the function later. */
4353 goto return_prx_yield;
4354
Willy Tarreau0b748332014-04-29 00:13:29 +02004355 case HTTP_RULE_RES_CONT:
4356 case HTTP_RULE_RES_STOP: /* nothing to do */
4357 break;
Willy Tarreau52542592014-04-28 18:33:26 +02004358
Willy Tarreau0b748332014-04-29 00:13:29 +02004359 case HTTP_RULE_RES_DENY: /* deny or tarpit */
4360 if (txn->flags & TX_CLTARPIT)
4361 goto tarpit;
4362 goto deny;
Willy Tarreau52542592014-04-28 18:33:26 +02004363
Willy Tarreau0b748332014-04-29 00:13:29 +02004364 case HTTP_RULE_RES_ABRT: /* abort request, response already sent. Eg: auth */
4365 goto return_prx_cond;
Willy Tarreau52542592014-04-28 18:33:26 +02004366
Willy Tarreau0b748332014-04-29 00:13:29 +02004367 case HTTP_RULE_RES_DONE: /* OK, but terminate request processing (eg: redirect) */
Willy Tarreau52542592014-04-28 18:33:26 +02004368 goto done;
4369
Willy Tarreau0b748332014-04-29 00:13:29 +02004370 case HTTP_RULE_RES_BADREQ: /* failed with a bad request */
4371 goto return_bad_req;
4372 }
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004373 }
4374
Willy Tarreau52542592014-04-28 18:33:26 +02004375 /* OK at this stage, we know that the request was accepted according to
4376 * the http-request rules, we can check for the stats. Note that the
4377 * URI is detected *before* the req* rules in order not to be affected
4378 * by a possible reqrep, while they are processed *after* so that a
4379 * reqdeny can still block them. This clearly needs to change in 1.6!
4380 */
Willy Tarreau350f4872014-11-28 14:42:25 +01004381 if (stats_check_uri(&s->si[1], txn, px)) {
Willy Tarreau52542592014-04-28 18:33:26 +02004382 s->target = &http_stats_applet.obj_type;
Willy Tarreau350f4872014-11-28 14:42:25 +01004383 if (unlikely(!stream_int_register_handler(&s->si[1], objt_applet(s->target)))) {
Willy Tarreau52542592014-04-28 18:33:26 +02004384 txn->status = 500;
4385 s->logs.tv_request = now;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02004386 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004387
Willy Tarreaue7dff022015-04-03 01:14:29 +02004388 if (!(s->flags & SF_ERR_MASK))
4389 s->flags |= SF_ERR_RESOURCE;
Willy Tarreau52542592014-04-28 18:33:26 +02004390 goto return_prx_cond;
4391 }
4392
4393 /* parse the whole stats request and extract the relevant information */
4394 http_handle_stats(s, req);
Willy Tarreau58727ec2016-05-25 16:23:59 +02004395 verdict = http_req_get_intercept_rule(px, &px->uri_auth->http_req_rules, s, &deny_status);
Willy Tarreau0b748332014-04-29 00:13:29 +02004396 /* not all actions implemented: deny, allow, auth */
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004397
Willy Tarreau0b748332014-04-29 00:13:29 +02004398 if (verdict == HTTP_RULE_RES_DENY) /* stats http-request deny */
4399 goto deny;
Willy Tarreau52542592014-04-28 18:33:26 +02004400
Willy Tarreau0b748332014-04-29 00:13:29 +02004401 if (verdict == HTTP_RULE_RES_ABRT) /* stats auth / stats http-request auth */
4402 goto return_prx_cond;
Krzysztof Piotr Oledzki59bb2182010-01-29 17:58:21 +01004403 }
4404
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004405 /* evaluate the req* rules except reqadd */
4406 if (px->req_exp != NULL) {
Willy Tarreau6c123b12010-01-28 20:22:06 +01004407 if (apply_filters_to_request(s, req, px) < 0)
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004408 goto return_bad_req;
Willy Tarreau06619262006-12-17 08:37:22 +01004409
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004410 if (txn->flags & TX_CLDENY)
4411 goto deny;
Willy Tarreauc465fd72009-08-31 00:17:18 +02004412
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004413 if (txn->flags & TX_CLTARPIT)
4414 goto tarpit;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004415 }
Willy Tarreau06619262006-12-17 08:37:22 +01004416
Willy Tarreauf68a15a2011-01-06 16:53:21 +01004417 /* add request headers from the rule sets in the same order */
4418 list_for_each_entry(wl, &px->req_add, list) {
4419 if (wl->cond) {
Willy Tarreau192252e2015-04-04 01:47:55 +02004420 int ret = acl_exec_cond(wl->cond, px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreauf68a15a2011-01-06 16:53:21 +01004421 ret = acl_pass(ret);
4422 if (((struct acl_cond *)wl->cond)->pol == ACL_COND_UNLESS)
4423 ret = !ret;
4424 if (!ret)
4425 continue;
4426 }
4427
Willy Tarreau6acf7c92012-03-09 13:30:45 +01004428 if (unlikely(http_header_add_tail(&txn->req, &txn->hdr_idx, wl->s) < 0))
Willy Tarreauf68a15a2011-01-06 16:53:21 +01004429 goto return_bad_req;
Willy Tarreau81499eb2012-12-27 12:19:02 +01004430 }
4431
Willy Tarreau52542592014-04-28 18:33:26 +02004432
4433 /* Proceed with the stats now. */
Willy Tarreau414e9bb2013-11-23 00:30:38 +01004434 if (unlikely(objt_applet(s->target) == &http_stats_applet)) {
Willy Tarreau1facd6d2012-12-22 22:03:39 +01004435 /* process the stats request now */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004436 if (sess->fe == s->be) /* report it if the request was intercepted by the frontend */
4437 sess->fe->fe_counters.intercepted_req++;
Willy Tarreau347a35d2013-11-22 17:51:09 +01004438
Willy Tarreaue7dff022015-04-03 01:14:29 +02004439 if (!(s->flags & SF_ERR_MASK)) // this is not really an error but it is
4440 s->flags |= SF_ERR_LOCAL; // to mark that it comes from the proxy
4441 if (!(s->flags & SF_FINST_MASK))
4442 s->flags |= SF_FINST_R;
Willy Tarreau347a35d2013-11-22 17:51:09 +01004443
Willy Tarreau70730dd2014-04-24 18:06:27 +02004444 /* enable the minimally required analyzers to handle keep-alive and compression on the HTTP response */
Christopher Faulet0184ea72017-01-05 14:06:34 +01004445 req->analysers &= (AN_REQ_HTTP_BODY | AN_REQ_FLT_HTTP_HDRS | AN_REQ_FLT_END);
4446 req->analysers &= ~AN_REQ_FLT_XFER_DATA;
Christopher Fauletd7c91962015-04-30 11:48:27 +02004447 req->analysers |= AN_REQ_HTTP_XFER_BODY;
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004448 goto done;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004449 }
Willy Tarreaub2513902006-12-17 14:52:38 +01004450
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004451 /* check whether we have some ACLs set to redirect this request */
4452 list_for_each_entry(rule, &px->redirect_rules, list) {
Willy Tarreauf285f542010-01-03 20:03:03 +01004453 if (rule->cond) {
Willy Tarreau71241ab2012-12-27 11:30:54 +01004454 int ret;
4455
Willy Tarreau192252e2015-04-04 01:47:55 +02004456 ret = acl_exec_cond(rule->cond, px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreauf285f542010-01-03 20:03:03 +01004457 ret = acl_pass(ret);
4458 if (rule->cond->pol == ACL_COND_UNLESS)
4459 ret = !ret;
Willy Tarreau71241ab2012-12-27 11:30:54 +01004460 if (!ret)
4461 continue;
Willy Tarreauf285f542010-01-03 20:03:03 +01004462 }
Willy Tarreau71241ab2012-12-27 11:30:54 +01004463 if (!http_apply_redirect_rule(rule, s, txn))
4464 goto return_bad_req;
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004465 goto done;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004466 }
Willy Tarreau55ea7572007-06-17 19:56:27 +02004467
Willy Tarreau2be39392010-01-03 17:24:51 +01004468 /* POST requests may be accompanied with an "Expect: 100-Continue" header.
4469 * If this happens, then the data will not come immediately, so we must
4470 * send all what we have without waiting. Note that due to the small gain
4471 * in waiting for the body of the request, it's easier to simply put the
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004472 * CF_SEND_DONTWAIT flag any time. It's a one-shot flag so it will remove
Willy Tarreau2be39392010-01-03 17:24:51 +01004473 * itself once used.
4474 */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004475 req->flags |= CF_SEND_DONTWAIT;
Willy Tarreau2be39392010-01-03 17:24:51 +01004476
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004477 done: /* done with this analyser, continue with next ones that the calling
4478 * points will have set, if any.
4479 */
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004480 req->analyse_exp = TICK_ETERNITY;
Thierry FOURNIER7566e302014-08-22 06:55:26 +02004481 done_without_exp: /* done with this analyser, but dont reset the analyse_exp. */
4482 req->analysers &= ~an_bit;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004483 return 1;
Willy Tarreau11382812008-07-09 16:18:21 +02004484
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004485 tarpit:
4486 /* When a connection is tarpitted, we use the tarpit timeout,
4487 * which may be the same as the connect timeout if unspecified.
4488 * If unset, then set it to zero because we really want it to
4489 * eventually expire. We build the tarpit as an analyser.
4490 */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004491 channel_erase(&s->req);
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004492
4493 /* wipe the request out so that we can drop the connection early
4494 * if the client closes first.
4495 */
4496 channel_dont_connect(req);
Bertrand Paquet83a2c3d2016-04-06 11:58:31 +02004497
4498 /* Allow cookie logging
4499 */
4500 if (s->be->cookie_name || sess->fe->capture_name)
4501 manage_client_side_cookies(s, req);
4502
Christopher Faulet0184ea72017-01-05 14:06:34 +01004503 req->analysers &= AN_REQ_FLT_END; /* remove switching rules etc... */
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004504 req->analysers |= AN_REQ_HTTP_TARPIT;
4505 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.tarpit);
4506 if (!req->analyse_exp)
4507 req->analyse_exp = tick_add(now_ms, 0);
Willy Tarreau87b09662015-04-03 00:22:06 +02004508 stream_inc_http_err_ctr(s);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004509 sess->fe->fe_counters.denied_req++;
4510 if (sess->fe != s->be)
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004511 s->be->be_counters.denied_req++;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02004512 if (sess->listener->counters)
4513 sess->listener->counters->denied_req++;
Thierry FOURNIER7566e302014-08-22 06:55:26 +02004514 goto done_without_exp;
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004515
4516 deny: /* this request was blocked (denied) */
Bertrand Paquet83a2c3d2016-04-06 11:58:31 +02004517
4518 /* Allow cookie logging
4519 */
4520 if (s->be->cookie_name || sess->fe->capture_name)
4521 manage_client_side_cookies(s, req);
4522
Willy Tarreau0b748332014-04-29 00:13:29 +02004523 txn->flags |= TX_CLDENY;
Willy Tarreau58727ec2016-05-25 16:23:59 +02004524 txn->status = http_err_codes[deny_status];
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004525 s->logs.tv_request = now;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02004526 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreau87b09662015-04-03 00:22:06 +02004527 stream_inc_http_err_ctr(s);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004528 sess->fe->fe_counters.denied_req++;
4529 if (sess->fe != s->be)
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004530 s->be->be_counters.denied_req++;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02004531 if (sess->listener->counters)
4532 sess->listener->counters->denied_req++;
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004533 goto return_prx_cond;
4534
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004535 return_bad_req:
4536 /* We centralize bad requests processing here */
4537 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
4538 /* we detected a parsing error. We want to archive this request
4539 * in the dedicated proxy area for later troubleshooting.
4540 */
Willy Tarreau10e61cb2017-01-04 14:51:22 +01004541 http_capture_bad_message(&sess->fe->invalid_req, s, msg, msg->err_state, sess->fe);
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004542 }
Willy Tarreau55ea7572007-06-17 19:56:27 +02004543
Willy Tarreau10e61cb2017-01-04 14:51:22 +01004544 txn->req.err_state = txn->req.msg_state;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004545 txn->req.msg_state = HTTP_MSG_ERROR;
4546 txn->status = 400;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02004547 http_reply_and_close(s, txn->status, http_error_message(s));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004548
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004549 sess->fe->fe_counters.failed_req++;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02004550 if (sess->listener->counters)
4551 sess->listener->counters->failed_req++;
Willy Tarreau6e4261e2007-09-18 18:36:05 +02004552
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004553 return_prx_cond:
Willy Tarreaue7dff022015-04-03 01:14:29 +02004554 if (!(s->flags & SF_ERR_MASK))
4555 s->flags |= SF_ERR_PRXCOND;
4556 if (!(s->flags & SF_FINST_MASK))
4557 s->flags |= SF_FINST_R;
Willy Tarreauf1221aa2006-12-17 22:14:12 +01004558
Christopher Faulet0184ea72017-01-05 14:06:34 +01004559 req->analysers &= AN_REQ_FLT_END;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004560 req->analyse_exp = TICK_ETERNITY;
4561 return 0;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01004562
4563 return_prx_yield:
4564 channel_dont_connect(req);
4565 return 0;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004566}
Willy Tarreau58f10d72006-12-04 02:26:12 +01004567
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004568/* This function performs all the processing enabled for the current request.
4569 * It returns 1 if the processing can continue on next analysers, or zero if it
4570 * needs more data, encounters an error, or wants to immediately abort the
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004571 * request. It relies on buffers flags, and updates s->req.analysers.
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004572 */
Willy Tarreau87b09662015-04-03 00:22:06 +02004573int http_process_request(struct stream *s, struct channel *req, int an_bit)
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004574{
Willy Tarreaufb0afa72015-04-03 14:46:27 +02004575 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02004576 struct http_txn *txn = s->txn;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004577 struct http_msg *msg = &txn->req;
Willy Tarreauee335e62015-04-21 18:15:13 +02004578 struct connection *cli_conn = objt_conn(strm_sess(s)->origin);
Willy Tarreau58f10d72006-12-04 02:26:12 +01004579
Willy Tarreau655dce92009-11-08 13:10:58 +01004580 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau51aecc72009-07-12 09:47:04 +02004581 /* we need more data */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004582 channel_dont_connect(req);
Willy Tarreau51aecc72009-07-12 09:47:04 +02004583 return 0;
4584 }
4585
Willy Tarreau87b09662015-04-03 00:22:06 +02004586 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 +02004587 now_ms, __FUNCTION__,
4588 s,
4589 req,
4590 req->rex, req->wex,
4591 req->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02004592 req->buf->i,
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004593 req->analysers);
Willy Tarreau06619262006-12-17 08:37:22 +01004594
Willy Tarreau59234e92008-11-30 23:51:27 +01004595 /*
4596 * Right now, we know that we have processed the entire headers
4597 * and that unwanted requests have been filtered out. We can do
4598 * whatever we want with the remaining request. Also, now we
4599 * may have separate values for ->fe, ->be.
4600 */
Willy Tarreau06619262006-12-17 08:37:22 +01004601
Willy Tarreau59234e92008-11-30 23:51:27 +01004602 /*
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004603 * If HTTP PROXY is set we simply get remote server address parsing
4604 * incoming request. Note that this requires that a connection is
4605 * allocated on the server side.
Willy Tarreau59234e92008-11-30 23:51:27 +01004606 */
Willy Tarreaue7dff022015-04-03 01:14:29 +02004607 if ((s->be->options & PR_O_HTTP_PROXY) && !(s->flags & SF_ADDR_SET)) {
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02004608 struct connection *conn;
Willy Tarreaue8df1e12013-12-16 14:30:55 +01004609 char *path;
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02004610
Willy Tarreau9471b8c2013-12-15 13:31:35 +01004611 /* Note that for now we don't reuse existing proxy connections */
Willy Tarreau973a5422015-08-05 21:47:23 +02004612 if (unlikely((conn = si_alloc_conn(&s->si[1])) == NULL)) {
Willy Tarreau10e61cb2017-01-04 14:51:22 +01004613 txn->req.err_state = txn->req.msg_state;
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02004614 txn->req.msg_state = HTTP_MSG_ERROR;
4615 txn->status = 500;
Christopher Faulet0184ea72017-01-05 14:06:34 +01004616 req->analysers &= AN_REQ_FLT_END;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02004617 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02004618
Willy Tarreaue7dff022015-04-03 01:14:29 +02004619 if (!(s->flags & SF_ERR_MASK))
4620 s->flags |= SF_ERR_RESOURCE;
4621 if (!(s->flags & SF_FINST_MASK))
4622 s->flags |= SF_FINST_R;
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02004623
4624 return 0;
4625 }
Willy Tarreaue8df1e12013-12-16 14:30:55 +01004626
4627 path = http_get_path(txn);
4628 url2sa(req->buf->p + msg->sl.rq.u,
4629 path ? path - (req->buf->p + msg->sl.rq.u) : msg->sl.rq.u_l,
Thierry FOURNIER9f95e402014-03-21 14:51:46 +01004630 &conn->addr.to, NULL);
Willy Tarreaue8df1e12013-12-16 14:30:55 +01004631 /* if the path was found, we have to remove everything between
4632 * req->buf->p + msg->sl.rq.u and path (excluded). If it was not
4633 * found, we need to replace from req->buf->p + msg->sl.rq.u for
4634 * u_l characters by a single "/".
4635 */
4636 if (path) {
4637 char *cur_ptr = req->buf->p;
4638 char *cur_end = cur_ptr + txn->req.sl.rq.l;
4639 int delta;
4640
4641 delta = buffer_replace2(req->buf, req->buf->p + msg->sl.rq.u, path, NULL, 0);
4642 http_msg_move_end(&txn->req, delta);
4643 cur_end += delta;
4644 if (http_parse_reqline(&txn->req, HTTP_MSG_RQMETH, cur_ptr, cur_end + 1, NULL, NULL) == NULL)
4645 goto return_bad_req;
4646 }
4647 else {
4648 char *cur_ptr = req->buf->p;
4649 char *cur_end = cur_ptr + txn->req.sl.rq.l;
4650 int delta;
4651
4652 delta = buffer_replace2(req->buf, req->buf->p + msg->sl.rq.u,
4653 req->buf->p + msg->sl.rq.u + msg->sl.rq.u_l, "/", 1);
4654 http_msg_move_end(&txn->req, delta);
4655 cur_end += delta;
4656 if (http_parse_reqline(&txn->req, HTTP_MSG_RQMETH, cur_ptr, cur_end + 1, NULL, NULL) == NULL)
4657 goto return_bad_req;
4658 }
Willy Tarreau59234e92008-11-30 23:51:27 +01004659 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01004660
Willy Tarreau59234e92008-11-30 23:51:27 +01004661 /*
Cyril Bontéb21570a2009-11-29 20:04:48 +01004662 * 7: Now we can work with the cookies.
Willy Tarreau59234e92008-11-30 23:51:27 +01004663 * Note that doing so might move headers in the request, but
4664 * the fields will stay coherent and the URI will not move.
4665 * This should only be performed in the backend.
4666 */
Bertrand Paquet83a2c3d2016-04-06 11:58:31 +02004667 if (s->be->cookie_name || sess->fe->capture_name)
Willy Tarreau59234e92008-11-30 23:51:27 +01004668 manage_client_side_cookies(s, req);
Willy Tarreau7ac51f62007-03-25 16:00:04 +02004669
William Lallemanda73203e2012-03-12 12:48:57 +01004670 /* add unique-id if "header-unique-id" is specified */
4671
Thierry Fournierf4011dd2016-03-29 17:23:51 +02004672 if (!LIST_ISEMPTY(&sess->fe->format_unique_id) && !s->unique_id) {
William Lallemand5b7ea3a2013-08-28 15:44:19 +02004673 if ((s->unique_id = pool_alloc2(pool2_uniqueid)) == NULL)
4674 goto return_bad_req;
4675 s->unique_id[0] = '\0';
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004676 build_logline(s, s->unique_id, UNIQUEID_LEN, &sess->fe->format_unique_id);
William Lallemand5b7ea3a2013-08-28 15:44:19 +02004677 }
William Lallemanda73203e2012-03-12 12:48:57 +01004678
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004679 if (sess->fe->header_unique_id && s->unique_id) {
4680 chunk_printf(&trash, "%s: %s", sess->fe->header_unique_id, s->unique_id);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004681 if (trash.len < 0)
William Lallemanda73203e2012-03-12 12:48:57 +01004682 goto return_bad_req;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004683 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, trash.len) < 0))
William Lallemanda73203e2012-03-12 12:48:57 +01004684 goto return_bad_req;
4685 }
4686
Cyril Bontéb21570a2009-11-29 20:04:48 +01004687 /*
Willy Tarreau59234e92008-11-30 23:51:27 +01004688 * 9: add X-Forwarded-For if either the frontend or the backend
4689 * asks for it.
4690 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004691 if ((sess->fe->options | s->be->options) & PR_O_FWDFOR) {
Willy Tarreau87cf5142011-08-19 22:57:24 +02004692 struct hdr_ctx ctx = { .idx = 0 };
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004693 if (!((sess->fe->options | s->be->options) & PR_O_FF_ALWAYS) &&
4694 http_find_header2(s->be->fwdfor_hdr_len ? s->be->fwdfor_hdr_name : sess->fe->fwdfor_hdr_name,
4695 s->be->fwdfor_hdr_len ? s->be->fwdfor_hdr_len : sess->fe->fwdfor_hdr_len,
Willy Tarreau9b28e032012-10-12 23:49:43 +02004696 req->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreau87cf5142011-08-19 22:57:24 +02004697 /* The header is set to be added only if none is present
4698 * and we found it, so don't do anything.
4699 */
4700 }
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004701 else if (cli_conn && cli_conn->addr.from.ss_family == AF_INET) {
Willy Tarreau59234e92008-11-30 23:51:27 +01004702 /* Add an X-Forwarded-For header unless the source IP is
4703 * in the 'except' network range.
4704 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004705 if ((!sess->fe->except_mask.s_addr ||
4706 (((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr.s_addr & sess->fe->except_mask.s_addr)
4707 != sess->fe->except_net.s_addr) &&
Willy Tarreau59234e92008-11-30 23:51:27 +01004708 (!s->be->except_mask.s_addr ||
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004709 (((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr.s_addr & s->be->except_mask.s_addr)
Willy Tarreau59234e92008-11-30 23:51:27 +01004710 != s->be->except_net.s_addr)) {
Willy Tarreau2a324282006-12-05 00:05:46 +01004711 int len;
Willy Tarreau59234e92008-11-30 23:51:27 +01004712 unsigned char *pn;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004713 pn = (unsigned char *)&((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr;
Ross Westaf72a1d2008-08-03 10:51:45 +02004714
4715 /* Note: we rely on the backend to get the header name to be used for
4716 * x-forwarded-for, because the header is really meant for the backends.
4717 * However, if the backend did not specify any option, we have to rely
4718 * on the frontend's header name.
4719 */
Willy Tarreau59234e92008-11-30 23:51:27 +01004720 if (s->be->fwdfor_hdr_len) {
4721 len = s->be->fwdfor_hdr_len;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004722 memcpy(trash.str, s->be->fwdfor_hdr_name, len);
Ross Westaf72a1d2008-08-03 10:51:45 +02004723 } else {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004724 len = sess->fe->fwdfor_hdr_len;
4725 memcpy(trash.str, sess->fe->fwdfor_hdr_name, len);
Willy Tarreaub86db342009-11-30 11:50:16 +01004726 }
Willy Tarreaue9187f82014-04-14 15:27:14 +02004727 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 +01004728
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004729 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, len) < 0))
Willy Tarreau06619262006-12-17 08:37:22 +01004730 goto return_bad_req;
Willy Tarreau2a324282006-12-05 00:05:46 +01004731 }
4732 }
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004733 else if (cli_conn && cli_conn->addr.from.ss_family == AF_INET6) {
Willy Tarreau59234e92008-11-30 23:51:27 +01004734 /* FIXME: for the sake of completeness, we should also support
4735 * 'except' here, although it is mostly useless in this case.
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02004736 */
Willy Tarreau59234e92008-11-30 23:51:27 +01004737 int len;
4738 char pn[INET6_ADDRSTRLEN];
4739 inet_ntop(AF_INET6,
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004740 (const void *)&((struct sockaddr_in6 *)(&cli_conn->addr.from))->sin6_addr,
Willy Tarreau59234e92008-11-30 23:51:27 +01004741 pn, sizeof(pn));
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02004742
Willy Tarreau59234e92008-11-30 23:51:27 +01004743 /* Note: we rely on the backend to get the header name to be used for
4744 * x-forwarded-for, because the header is really meant for the backends.
4745 * However, if the backend did not specify any option, we have to rely
4746 * on the frontend's header name.
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02004747 */
Willy Tarreau59234e92008-11-30 23:51:27 +01004748 if (s->be->fwdfor_hdr_len) {
4749 len = s->be->fwdfor_hdr_len;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004750 memcpy(trash.str, s->be->fwdfor_hdr_name, len);
Willy Tarreau59234e92008-11-30 23:51:27 +01004751 } else {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004752 len = sess->fe->fwdfor_hdr_len;
4753 memcpy(trash.str, sess->fe->fwdfor_hdr_name, len);
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02004754 }
Willy Tarreaue9187f82014-04-14 15:27:14 +02004755 len += snprintf(trash.str + len, trash.size - len, ": %s", pn);
Willy Tarreauadfb8562008-08-11 15:24:42 +02004756
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004757 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, len) < 0))
Willy Tarreau59234e92008-11-30 23:51:27 +01004758 goto return_bad_req;
4759 }
4760 }
4761
4762 /*
Maik Broemme2850cb42009-04-17 18:53:21 +02004763 * 10: add X-Original-To if either the frontend or the backend
4764 * asks for it.
4765 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004766 if ((sess->fe->options | s->be->options) & PR_O_ORGTO) {
Maik Broemme2850cb42009-04-17 18:53:21 +02004767
4768 /* FIXME: don't know if IPv6 can handle that case too. */
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004769 if (cli_conn && cli_conn->addr.from.ss_family == AF_INET) {
Maik Broemme2850cb42009-04-17 18:53:21 +02004770 /* Add an X-Original-To header unless the destination IP is
4771 * in the 'except' network range.
4772 */
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004773 conn_get_to_addr(cli_conn);
Maik Broemme2850cb42009-04-17 18:53:21 +02004774
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004775 if (cli_conn->addr.to.ss_family == AF_INET &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004776 ((!sess->fe->except_mask_to.s_addr ||
4777 (((struct sockaddr_in *)&cli_conn->addr.to)->sin_addr.s_addr & sess->fe->except_mask_to.s_addr)
4778 != sess->fe->except_to.s_addr) &&
Emeric Brun5bd86a82010-10-22 17:23:04 +02004779 (!s->be->except_mask_to.s_addr ||
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004780 (((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 +02004781 != s->be->except_to.s_addr))) {
Maik Broemme2850cb42009-04-17 18:53:21 +02004782 int len;
4783 unsigned char *pn;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004784 pn = (unsigned char *)&((struct sockaddr_in *)&cli_conn->addr.to)->sin_addr;
Maik Broemme2850cb42009-04-17 18:53:21 +02004785
4786 /* Note: we rely on the backend to get the header name to be used for
4787 * x-original-to, because the header is really meant for the backends.
4788 * However, if the backend did not specify any option, we have to rely
4789 * on the frontend's header name.
4790 */
4791 if (s->be->orgto_hdr_len) {
4792 len = s->be->orgto_hdr_len;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004793 memcpy(trash.str, s->be->orgto_hdr_name, len);
Maik Broemme2850cb42009-04-17 18:53:21 +02004794 } else {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004795 len = sess->fe->orgto_hdr_len;
4796 memcpy(trash.str, sess->fe->orgto_hdr_name, len);
Willy Tarreaub86db342009-11-30 11:50:16 +01004797 }
Willy Tarreaue9187f82014-04-14 15:27:14 +02004798 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 +02004799
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004800 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, len) < 0))
Maik Broemme2850cb42009-04-17 18:53:21 +02004801 goto return_bad_req;
4802 }
4803 }
4804 }
4805
Willy Tarreau50fc7772012-11-11 22:19:57 +01004806 /* 11: add "Connection: close" or "Connection: keep-alive" if needed and not yet set.
4807 * If an "Upgrade" token is found, the header is left untouched in order not to have
4808 * to deal with some servers bugs : some of them fail an Upgrade if anything but
4809 * "Upgrade" is present in the Connection header.
4810 */
4811 if (!(txn->flags & TX_HDR_CONN_UPG) &&
4812 (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) ||
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004813 ((sess->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
Willy Tarreau02bce8b2014-01-30 00:15:28 +01004814 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL))) {
Willy Tarreaubbf0b372010-01-18 16:54:40 +01004815 unsigned int want_flags = 0;
4816
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004817 if (msg->flags & HTTP_MSGF_VER_11) {
Willy Tarreau22a95342010-09-29 14:31:41 +02004818 if (((txn->flags & TX_CON_WANT_MSK) >= TX_CON_WANT_SCL ||
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004819 ((sess->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
Willy Tarreau02bce8b2014-01-30 00:15:28 +01004820 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL)) &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004821 !((sess->fe->options2|s->be->options2) & PR_O2_FAKE_KA))
Willy Tarreaubbf0b372010-01-18 16:54:40 +01004822 want_flags |= TX_CON_CLO_SET;
4823 } else {
Willy Tarreau22a95342010-09-29 14:31:41 +02004824 if (((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004825 ((sess->fe->options & PR_O_HTTP_MODE) != PR_O_HTTP_PCL &&
Willy Tarreau02bce8b2014-01-30 00:15:28 +01004826 (s->be->options & PR_O_HTTP_MODE) != PR_O_HTTP_PCL)) ||
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004827 ((sess->fe->options2|s->be->options2) & PR_O2_FAKE_KA))
Willy Tarreaubbf0b372010-01-18 16:54:40 +01004828 want_flags |= TX_CON_KAL_SET;
4829 }
4830
4831 if (want_flags != (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET)))
Willy Tarreau6acf7c92012-03-09 13:30:45 +01004832 http_change_connection_header(txn, msg, want_flags);
Willy Tarreau59234e92008-11-30 23:51:27 +01004833 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01004834
Willy Tarreaubbf0b372010-01-18 16:54:40 +01004835
Willy Tarreau522d6c02009-12-06 18:49:18 +01004836 /* If we have no server assigned yet and we're balancing on url_param
4837 * with a POST request, we may be interested in checking the body for
4838 * that parameter. This will be done in another analyser.
Willy Tarreau59234e92008-11-30 23:51:27 +01004839 */
Willy Tarreaue7dff022015-04-03 01:14:29 +02004840 if (!(s->flags & (SF_ASSIGNED|SF_DIRECT)) &&
Willy Tarreaueee5b512015-04-03 23:46:31 +02004841 s->txn->meth == HTTP_METH_POST && s->be->url_param_name != NULL &&
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004842 (msg->flags & (HTTP_MSGF_CNT_LEN|HTTP_MSGF_TE_CHNK))) {
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004843 channel_dont_connect(req);
Willy Tarreau522d6c02009-12-06 18:49:18 +01004844 req->analysers |= AN_REQ_HTTP_BODY;
Willy Tarreau59234e92008-11-30 23:51:27 +01004845 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02004846
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004847 if (msg->flags & HTTP_MSGF_XFER_LEN) {
Christopher Faulet0184ea72017-01-05 14:06:34 +01004848 req->analysers &= ~AN_REQ_FLT_XFER_DATA;
Willy Tarreaud98cf932009-12-27 22:54:55 +01004849 req->analysers |= AN_REQ_HTTP_XFER_BODY;
Willy Tarreau5e205522011-12-17 16:34:27 +01004850#ifdef TCP_QUICKACK
4851 /* We expect some data from the client. Unless we know for sure
4852 * we already have a full request, we have to re-enable quick-ack
4853 * in case we previously disabled it, otherwise we might cause
4854 * the client to delay further data.
4855 */
Willy Tarreaufb0afa72015-04-03 14:46:27 +02004856 if ((sess->listener->options & LI_O_NOQUICKACK) &&
Willy Tarreau3c728722014-01-23 13:50:42 +01004857 cli_conn && conn_ctrl_ready(cli_conn) &&
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004858 ((msg->flags & HTTP_MSGF_TE_CHNK) ||
Willy Tarreau9b28e032012-10-12 23:49:43 +02004859 (msg->body_len > req->buf->i - txn->req.eoh - 2)))
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004860 setsockopt(cli_conn->t.sock.fd, IPPROTO_TCP, TCP_QUICKACK, &one, sizeof(one));
Willy Tarreau5e205522011-12-17 16:34:27 +01004861#endif
4862 }
Willy Tarreau03945942009-12-22 16:50:27 +01004863
Willy Tarreau59234e92008-11-30 23:51:27 +01004864 /*************************************************************
4865 * OK, that's finished for the headers. We have done what we *
4866 * could. Let's switch to the DATA state. *
4867 ************************************************************/
Willy Tarreau522d6c02009-12-06 18:49:18 +01004868 req->analyse_exp = TICK_ETERNITY;
4869 req->analysers &= ~an_bit;
Willy Tarreaubaaee002006-06-26 02:48:02 +02004870
Willy Tarreau7bb68ab2012-05-13 14:48:59 +02004871 /* if the server closes the connection, we want to immediately react
4872 * and close the socket to save packets and syscalls.
4873 */
Willy Tarreau40f151a2012-12-20 12:10:09 +01004874 if (!(req->analysers & AN_REQ_HTTP_XFER_BODY))
Willy Tarreau350f4872014-11-28 14:42:25 +01004875 s->si[1].flags |= SI_FL_NOHALF;
Willy Tarreau7bb68ab2012-05-13 14:48:59 +02004876
Willy Tarreau59234e92008-11-30 23:51:27 +01004877 s->logs.tv_request = now;
Willy Tarreau59234e92008-11-30 23:51:27 +01004878 /* OK let's go on with the BODY now */
4879 return 1;
Willy Tarreau06619262006-12-17 08:37:22 +01004880
Willy Tarreau59234e92008-11-30 23:51:27 +01004881 return_bad_req: /* let's centralize all bad requests */
Willy Tarreau4076a152009-04-02 15:18:36 +02004882 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
Willy Tarreauf073a832009-03-01 23:21:47 +01004883 /* we detected a parsing error. We want to archive this request
4884 * in the dedicated proxy area for later troubleshooting.
4885 */
Willy Tarreau10e61cb2017-01-04 14:51:22 +01004886 http_capture_bad_message(&sess->fe->invalid_req, s, msg, msg->err_state, sess->fe);
Willy Tarreauf073a832009-03-01 23:21:47 +01004887 }
Willy Tarreau4076a152009-04-02 15:18:36 +02004888
Willy Tarreau10e61cb2017-01-04 14:51:22 +01004889 txn->req.err_state = txn->req.msg_state;
Willy Tarreau59234e92008-11-30 23:51:27 +01004890 txn->req.msg_state = HTTP_MSG_ERROR;
4891 txn->status = 400;
Christopher Faulet0184ea72017-01-05 14:06:34 +01004892 req->analysers &= AN_REQ_FLT_END;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02004893 http_reply_and_close(s, txn->status, http_error_message(s));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004894
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004895 sess->fe->fe_counters.failed_req++;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02004896 if (sess->listener->counters)
4897 sess->listener->counters->failed_req++;
Willy Tarreauadfb8562008-08-11 15:24:42 +02004898
Willy Tarreaue7dff022015-04-03 01:14:29 +02004899 if (!(s->flags & SF_ERR_MASK))
4900 s->flags |= SF_ERR_PRXCOND;
4901 if (!(s->flags & SF_FINST_MASK))
4902 s->flags |= SF_FINST_R;
Willy Tarreaudafde432008-08-17 01:00:46 +02004903 return 0;
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02004904}
Willy Tarreauadfb8562008-08-11 15:24:42 +02004905
Willy Tarreau60b85b02008-11-30 23:28:40 +01004906/* This function is an analyser which processes the HTTP tarpit. It always
4907 * returns zero, at the beginning because it prevents any other processing
4908 * from occurring, and at the end because it terminates the request.
4909 */
Willy Tarreau87b09662015-04-03 00:22:06 +02004910int http_process_tarpit(struct stream *s, struct channel *req, int an_bit)
Willy Tarreau60b85b02008-11-30 23:28:40 +01004911{
Willy Tarreaueee5b512015-04-03 23:46:31 +02004912 struct http_txn *txn = s->txn;
Willy Tarreau60b85b02008-11-30 23:28:40 +01004913
4914 /* This connection is being tarpitted. The CLIENT side has
4915 * already set the connect expiration date to the right
4916 * timeout. We just have to check that the client is still
4917 * there and that the timeout has not expired.
4918 */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004919 channel_dont_connect(req);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004920 if ((req->flags & (CF_SHUTR|CF_READ_ERROR)) == 0 &&
Willy Tarreau60b85b02008-11-30 23:28:40 +01004921 !tick_is_expired(req->analyse_exp, now_ms))
4922 return 0;
4923
4924 /* We will set the queue timer to the time spent, just for
4925 * logging purposes. We fake a 500 server error, so that the
4926 * attacker will not suspect his connection has been tarpitted.
4927 * It will not cause trouble to the logs because we can exclude
4928 * the tarpitted connections by filtering on the 'PT' status flags.
4929 */
Willy Tarreau60b85b02008-11-30 23:28:40 +01004930 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
4931
4932 txn->status = 500;
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004933 if (!(req->flags & CF_READ_ERROR))
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02004934 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreau60b85b02008-11-30 23:28:40 +01004935
Christopher Faulet0184ea72017-01-05 14:06:34 +01004936 req->analysers &= AN_REQ_FLT_END;
Willy Tarreau60b85b02008-11-30 23:28:40 +01004937 req->analyse_exp = TICK_ETERNITY;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004938
Willy Tarreaue7dff022015-04-03 01:14:29 +02004939 if (!(s->flags & SF_ERR_MASK))
4940 s->flags |= SF_ERR_PRXCOND;
4941 if (!(s->flags & SF_FINST_MASK))
4942 s->flags |= SF_FINST_T;
Willy Tarreau60b85b02008-11-30 23:28:40 +01004943 return 0;
4944}
4945
Willy Tarreau5a8f9472014-04-10 11:16:06 +02004946/* This function is an analyser which waits for the HTTP request body. It waits
4947 * for either the buffer to be full, or the full advertised contents to have
4948 * reached the buffer. It must only be called after the standard HTTP request
4949 * processing has occurred, because it expects the request to be parsed and will
4950 * look for the Expect header. It may send a 100-Continue interim response. It
4951 * takes in input any state starting from HTTP_MSG_BODY and leaves with one of
4952 * HTTP_MSG_CHK_SIZE, HTTP_MSG_DATA or HTTP_MSG_TRAILERS. It returns zero if it
4953 * needs to read more data, or 1 once it has completed its analysis.
Willy Tarreaud34af782008-11-30 23:36:37 +01004954 */
Willy Tarreau87b09662015-04-03 00:22:06 +02004955int http_wait_for_request_body(struct stream *s, struct channel *req, int an_bit)
Willy Tarreaud34af782008-11-30 23:36:37 +01004956{
Willy Tarreaufb0afa72015-04-03 14:46:27 +02004957 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02004958 struct http_txn *txn = s->txn;
4959 struct http_msg *msg = &s->txn->req;
Willy Tarreaud34af782008-11-30 23:36:37 +01004960
4961 /* We have to parse the HTTP request body to find any required data.
4962 * "balance url_param check_post" should have been the only way to get
4963 * into this. We were brought here after HTTP header analysis, so all
4964 * related structures are ready.
4965 */
4966
Willy Tarreau890988f2014-04-10 11:59:33 +02004967 if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) {
4968 /* This is the first call */
4969 if (msg->msg_state < HTTP_MSG_BODY)
4970 goto missing_data;
Willy Tarreau522d6c02009-12-06 18:49:18 +01004971
Willy Tarreau890988f2014-04-10 11:59:33 +02004972 if (msg->msg_state < HTTP_MSG_100_SENT) {
4973 /* If we have HTTP/1.1 and Expect: 100-continue, then we must
4974 * send an HTTP/1.1 100 Continue intermediate response.
4975 */
4976 if (msg->flags & HTTP_MSGF_VER_11) {
4977 struct hdr_ctx ctx;
4978 ctx.idx = 0;
4979 /* Expect is allowed in 1.1, look for it */
4980 if (http_find_header2("Expect", 6, req->buf->p, &txn->hdr_idx, &ctx) &&
4981 unlikely(ctx.vlen == 12 && strncasecmp(ctx.line+ctx.val, "100-continue", 12) == 0)) {
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004982 bo_inject(&s->res, http_100_chunk.str, http_100_chunk.len);
Thierry FOURNIER / OZON.IO43ad11d2016-12-12 15:19:58 +01004983 http_remove_header2(&txn->req, &txn->hdr_idx, &ctx);
Willy Tarreau890988f2014-04-10 11:59:33 +02004984 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01004985 }
Willy Tarreau890988f2014-04-10 11:59:33 +02004986 msg->msg_state = HTTP_MSG_100_SENT;
Willy Tarreau522d6c02009-12-06 18:49:18 +01004987 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01004988
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01004989 /* we have msg->sov which points to the first byte of message body.
Willy Tarreau877e78d2013-04-07 18:48:08 +02004990 * req->buf->p still points to the beginning of the message. We
4991 * must save the body in msg->next because it survives buffer
4992 * re-alignments.
Willy Tarreaud98cf932009-12-27 22:54:55 +01004993 */
Willy Tarreauea1175a2012-03-05 15:52:30 +01004994 msg->next = msg->sov;
Willy Tarreaua458b672012-03-05 11:17:50 +01004995
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004996 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreau522d6c02009-12-06 18:49:18 +01004997 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
4998 else
4999 msg->msg_state = HTTP_MSG_DATA;
5000 }
5001
Willy Tarreau890988f2014-04-10 11:59:33 +02005002 if (!(msg->flags & HTTP_MSGF_TE_CHNK)) {
5003 /* We're in content-length mode, we just have to wait for enough data. */
Willy Tarreaue115b492015-05-01 23:05:14 +02005004 if (http_body_bytes(msg) < msg->body_len)
Willy Tarreau890988f2014-04-10 11:59:33 +02005005 goto missing_data;
5006
5007 /* OK we have everything we need now */
5008 goto http_end;
5009 }
5010
5011 /* OK here we're parsing a chunked-encoded message */
5012
Willy Tarreau522d6c02009-12-06 18:49:18 +01005013 if (msg->msg_state == HTTP_MSG_CHUNK_SIZE) {
Willy Tarreau124d9912011-03-01 20:30:48 +01005014 /* read the chunk size and assign it to ->chunk_len, then
Willy Tarreaua458b672012-03-05 11:17:50 +01005015 * set ->sov and ->next to point to the body and switch to DATA or
Willy Tarreaud98cf932009-12-27 22:54:55 +01005016 * TRAILERS state.
Willy Tarreau115acb92009-12-26 13:56:06 +01005017 */
Willy Tarreau4baf44b2012-03-09 14:10:20 +01005018 int ret = http_parse_chunk_size(msg);
Willy Tarreaud34af782008-11-30 23:36:37 +01005019
Willy Tarreau115acb92009-12-26 13:56:06 +01005020 if (!ret)
5021 goto missing_data;
Willy Tarreauda7ff642010-06-23 11:44:09 +02005022 else if (ret < 0) {
Willy Tarreau87b09662015-04-03 00:22:06 +02005023 stream_inc_http_err_ctr(s);
Willy Tarreau522d6c02009-12-06 18:49:18 +01005024 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02005025 }
Christopher Faulet113f7de2015-12-14 14:52:13 +01005026 msg->next += ret;
Christopher Fauletd7c91962015-04-30 11:48:27 +02005027 msg->msg_state = msg->chunk_len ? HTTP_MSG_DATA : HTTP_MSG_TRAILERS;
Willy Tarreaud34af782008-11-30 23:36:37 +01005028 }
5029
Willy Tarreaud98cf932009-12-27 22:54:55 +01005030 /* Now we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state.
Willy Tarreaue115b492015-05-01 23:05:14 +02005031 * We have the first data byte is in msg->sov + msg->sol. We're waiting
5032 * for at least a whole chunk or the whole content length bytes after
5033 * msg->sov + msg->sol.
Willy Tarreaud34af782008-11-30 23:36:37 +01005034 */
Willy Tarreau890988f2014-04-10 11:59:33 +02005035 if (msg->msg_state == HTTP_MSG_TRAILERS)
5036 goto http_end;
5037
Willy Tarreaue115b492015-05-01 23:05:14 +02005038 if (http_body_bytes(msg) >= msg->body_len) /* we have enough bytes now */
Willy Tarreau522d6c02009-12-06 18:49:18 +01005039 goto http_end;
5040
5041 missing_data:
Willy Tarreau31a19952014-04-10 11:50:37 +02005042 /* we get here if we need to wait for more data. If the buffer is full,
5043 * we have the maximum we can expect.
5044 */
5045 if (buffer_full(req->buf, global.tune.maxrewrite))
5046 goto http_end;
Willy Tarreau115acb92009-12-26 13:56:06 +01005047
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005048 if ((req->flags & CF_READ_TIMEOUT) || tick_is_expired(req->analyse_exp, now_ms)) {
Willy Tarreau522d6c02009-12-06 18:49:18 +01005049 txn->status = 408;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02005050 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreau79ebac62010-06-07 13:47:49 +02005051
Willy Tarreaue7dff022015-04-03 01:14:29 +02005052 if (!(s->flags & SF_ERR_MASK))
5053 s->flags |= SF_ERR_CLITO;
5054 if (!(s->flags & SF_FINST_MASK))
5055 s->flags |= SF_FINST_D;
Willy Tarreau522d6c02009-12-06 18:49:18 +01005056 goto return_err_msg;
Willy Tarreaud34af782008-11-30 23:36:37 +01005057 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01005058
5059 /* we get here if we need to wait for more data */
Willy Tarreau31a19952014-04-10 11:50:37 +02005060 if (!(req->flags & (CF_SHUTR | CF_READ_ERROR))) {
Willy Tarreaud34af782008-11-30 23:36:37 +01005061 /* Not enough data. We'll re-use the http-request
5062 * timeout here. Ideally, we should set the timeout
5063 * relative to the accept() date. We just set the
5064 * request timeout once at the beginning of the
5065 * request.
5066 */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005067 channel_dont_connect(req);
Willy Tarreaud34af782008-11-30 23:36:37 +01005068 if (!tick_isset(req->analyse_exp))
Willy Tarreaucd7afc02009-07-12 10:03:17 +02005069 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.httpreq);
Willy Tarreaud34af782008-11-30 23:36:37 +01005070 return 0;
5071 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01005072
5073 http_end:
5074 /* The situation will not evolve, so let's give up on the analysis. */
5075 s->logs.tv_request = now; /* update the request timer to reflect full request */
5076 req->analysers &= ~an_bit;
5077 req->analyse_exp = TICK_ETERNITY;
5078 return 1;
5079
5080 return_bad_req: /* let's centralize all bad requests */
Willy Tarreau10e61cb2017-01-04 14:51:22 +01005081 txn->req.err_state = txn->req.msg_state;
Willy Tarreau522d6c02009-12-06 18:49:18 +01005082 txn->req.msg_state = HTTP_MSG_ERROR;
5083 txn->status = 400;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02005084 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreau522d6c02009-12-06 18:49:18 +01005085
Willy Tarreaue7dff022015-04-03 01:14:29 +02005086 if (!(s->flags & SF_ERR_MASK))
5087 s->flags |= SF_ERR_PRXCOND;
5088 if (!(s->flags & SF_FINST_MASK))
5089 s->flags |= SF_FINST_R;
Willy Tarreau79ebac62010-06-07 13:47:49 +02005090
Willy Tarreau522d6c02009-12-06 18:49:18 +01005091 return_err_msg:
Christopher Faulet0184ea72017-01-05 14:06:34 +01005092 req->analysers &= AN_REQ_FLT_END;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005093 sess->fe->fe_counters.failed_req++;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02005094 if (sess->listener->counters)
5095 sess->listener->counters->failed_req++;
Willy Tarreau522d6c02009-12-06 18:49:18 +01005096 return 0;
Willy Tarreaud34af782008-11-30 23:36:37 +01005097}
5098
Willy Tarreaud1de8af2012-05-18 22:12:14 +02005099/* send a server's name with an outgoing request over an established connection.
5100 * Note: this function is designed to be called once the request has been scheduled
5101 * for being forwarded. This is the reason why it rewinds the buffer before
5102 * proceeding.
5103 */
Willy Tarreau45c0d982012-03-09 12:11:57 +01005104int http_send_name_header(struct http_txn *txn, struct proxy* be, const char* srv_name) {
Mark Lamourinec2247f02012-01-04 13:02:01 -05005105
5106 struct hdr_ctx ctx;
5107
Mark Lamourinec2247f02012-01-04 13:02:01 -05005108 char *hdr_name = be->server_id_hdr_name;
5109 int hdr_name_len = be->server_id_hdr_len;
Willy Tarreau394db372012-10-12 22:40:39 +02005110 struct channel *chn = txn->req.chn;
Mark Lamourinec2247f02012-01-04 13:02:01 -05005111 char *hdr_val;
Willy Tarreaud1de8af2012-05-18 22:12:14 +02005112 unsigned int old_o, old_i;
Mark Lamourinec2247f02012-01-04 13:02:01 -05005113
William Lallemandd9e90662012-01-30 17:27:17 +01005114 ctx.idx = 0;
5115
Willy Tarreau211cdec2014-04-17 20:18:08 +02005116 old_o = http_hdr_rewind(&txn->req);
Willy Tarreaud1de8af2012-05-18 22:12:14 +02005117 if (old_o) {
5118 /* The request was already skipped, let's restore it */
Willy Tarreau9b28e032012-10-12 23:49:43 +02005119 b_rew(chn->buf, old_o);
Willy Tarreau877e78d2013-04-07 18:48:08 +02005120 txn->req.next += old_o;
Christopher Fauletd7c91962015-04-30 11:48:27 +02005121 txn->req.sov += old_o;
Willy Tarreaud1de8af2012-05-18 22:12:14 +02005122 }
5123
Willy Tarreau9b28e032012-10-12 23:49:43 +02005124 old_i = chn->buf->i;
5125 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 -05005126 /* remove any existing values from the header */
Willy Tarreau6acf7c92012-03-09 13:30:45 +01005127 http_remove_header2(&txn->req, &txn->hdr_idx, &ctx);
Mark Lamourinec2247f02012-01-04 13:02:01 -05005128 }
5129
5130 /* Add the new header requested with the server value */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005131 hdr_val = trash.str;
Mark Lamourinec2247f02012-01-04 13:02:01 -05005132 memcpy(hdr_val, hdr_name, hdr_name_len);
5133 hdr_val += hdr_name_len;
5134 *hdr_val++ = ':';
5135 *hdr_val++ = ' ';
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005136 hdr_val += strlcpy2(hdr_val, srv_name, trash.str + trash.size - hdr_val);
5137 http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, hdr_val - trash.str);
Mark Lamourinec2247f02012-01-04 13:02:01 -05005138
Willy Tarreaud1de8af2012-05-18 22:12:14 +02005139 if (old_o) {
5140 /* If this was a forwarded request, we must readjust the amount of
5141 * data to be forwarded in order to take into account the size
Willy Tarreau877e78d2013-04-07 18:48:08 +02005142 * variations. Note that the current state is >= HTTP_MSG_BODY,
5143 * so we don't have to adjust ->sol.
Willy Tarreaud1de8af2012-05-18 22:12:14 +02005144 */
Willy Tarreau877e78d2013-04-07 18:48:08 +02005145 old_o += chn->buf->i - old_i;
5146 b_adv(chn->buf, old_o);
5147 txn->req.next -= old_o;
5148 txn->req.sov -= old_o;
Willy Tarreaud1de8af2012-05-18 22:12:14 +02005149 }
5150
Mark Lamourinec2247f02012-01-04 13:02:01 -05005151 return 0;
5152}
5153
Willy Tarreau610ecce2010-01-04 21:15:02 +01005154/* Terminate current transaction and prepare a new one. This is very tricky
5155 * right now but it works.
5156 */
Willy Tarreau87b09662015-04-03 00:22:06 +02005157void http_end_txn_clean_session(struct stream *s)
Willy Tarreau610ecce2010-01-04 21:15:02 +01005158{
Willy Tarreaueee5b512015-04-03 23:46:31 +02005159 int prev_status = s->txn->status;
Willy Tarreaud0d8da92015-04-04 02:10:38 +02005160 struct proxy *fe = strm_fe(s);
Willy Tarreau858b1032015-12-07 17:04:59 +01005161 struct proxy *be = s->be;
Willy Tarreau323a2d92015-08-04 19:00:17 +02005162 struct connection *srv_conn;
5163 struct server *srv;
Willy Tarreau449d74a2015-08-05 17:16:33 +02005164 unsigned int prev_flags = s->txn->flags;
Willy Tarreau068621e2013-12-23 15:11:25 +01005165
Willy Tarreau610ecce2010-01-04 21:15:02 +01005166 /* FIXME: We need a more portable way of releasing a backend's and a
5167 * server's connections. We need a safer way to reinitialize buffer
5168 * flags. We also need a more accurate method for computing per-request
5169 * data.
5170 */
Willy Tarreau323a2d92015-08-04 19:00:17 +02005171 srv_conn = objt_conn(s->si[1].end);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005172
Willy Tarreau4213a112013-12-15 10:25:42 +01005173 /* unless we're doing keep-alive, we want to quickly close the connection
5174 * to the server.
5175 */
Willy Tarreaueee5b512015-04-03 23:46:31 +02005176 if (((s->txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) ||
Willy Tarreau350f4872014-11-28 14:42:25 +01005177 !si_conn_ready(&s->si[1])) {
5178 s->si[1].flags |= SI_FL_NOLINGER | SI_FL_NOHALF;
5179 si_shutr(&s->si[1]);
5180 si_shutw(&s->si[1]);
Willy Tarreau4213a112013-12-15 10:25:42 +01005181 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01005182
Willy Tarreaue7dff022015-04-03 01:14:29 +02005183 if (s->flags & SF_BE_ASSIGNED) {
Willy Tarreau858b1032015-12-07 17:04:59 +01005184 be->beconn--;
Willy Tarreau2d5cd472012-03-01 23:34:37 +01005185 if (unlikely(s->srv_conn))
5186 sess_change_server(s, NULL);
5187 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01005188
5189 s->logs.t_close = tv_ms_elapsed(&s->logs.tv_accept, &now);
Willy Tarreau87b09662015-04-03 00:22:06 +02005190 stream_process_counters(s);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005191
Willy Tarreaueee5b512015-04-03 23:46:31 +02005192 if (s->txn->status) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005193 int n;
5194
Willy Tarreaueee5b512015-04-03 23:46:31 +02005195 n = s->txn->status / 100;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005196 if (n < 1 || n > 5)
5197 n = 0;
5198
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005199 if (fe->mode == PR_MODE_HTTP) {
5200 fe->fe_counters.p.http.rsp[n]++;
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01005201 }
Willy Tarreaue7dff022015-04-03 01:14:29 +02005202 if ((s->flags & SF_BE_ASSIGNED) &&
Willy Tarreau858b1032015-12-07 17:04:59 +01005203 (be->mode == PR_MODE_HTTP)) {
5204 be->be_counters.p.http.rsp[n]++;
5205 be->be_counters.p.http.cum_req++;
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01005206 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01005207 }
5208
5209 /* don't count other requests' data */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005210 s->logs.bytes_in -= s->req.buf->i;
5211 s->logs.bytes_out -= s->res.buf->i;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005212
5213 /* let's do a final log if we need it */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005214 if (!LIST_ISEMPTY(&fe->logformat) && s->logs.logwait &&
Willy Tarreaue7dff022015-04-03 01:14:29 +02005215 !(s->flags & SF_MONITOR) &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005216 (!(fe->options & PR_O_NULLNOLOG) || s->req.total)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005217 s->do_log(s);
5218 }
5219
Willy Tarreaud713bcc2014-06-25 15:36:04 +02005220 /* stop tracking content-based counters */
Willy Tarreau87b09662015-04-03 00:22:06 +02005221 stream_stop_content_counters(s);
5222 stream_update_time_stats(s);
Willy Tarreau4bfc5802014-06-17 12:19:18 +02005223
Willy Tarreau610ecce2010-01-04 21:15:02 +01005224 s->logs.accept_date = date; /* user-visible date for logging */
5225 s->logs.tv_accept = now; /* corrected date for internal use */
Thierry FOURNIER / OZON.IO4cac3592016-07-28 17:19:45 +02005226 s->logs.t_handshake = 0; /* There are no handshake in keep alive connection. */
5227 s->logs.t_idle = -1;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005228 tv_zero(&s->logs.tv_request);
5229 s->logs.t_queue = -1;
5230 s->logs.t_connect = -1;
5231 s->logs.t_data = -1;
5232 s->logs.t_close = 0;
5233 s->logs.prx_queue_size = 0; /* we get the number of pending conns before us */
5234 s->logs.srv_queue_size = 0; /* we will get this number soon */
5235
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005236 s->logs.bytes_in = s->req.total = s->req.buf->i;
5237 s->logs.bytes_out = s->res.total = s->res.buf->i;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005238
5239 if (s->pend_pos)
5240 pendconn_free(s->pend_pos);
5241
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005242 if (objt_server(s->target)) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02005243 if (s->flags & SF_CURR_SESS) {
5244 s->flags &= ~SF_CURR_SESS;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005245 objt_server(s->target)->cur_sess--;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005246 }
Willy Tarreau858b1032015-12-07 17:04:59 +01005247 if (may_dequeue_tasks(objt_server(s->target), be))
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005248 process_srv_queue(objt_server(s->target));
Willy Tarreau610ecce2010-01-04 21:15:02 +01005249 }
5250
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005251 s->target = NULL;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005252
Willy Tarreau4213a112013-12-15 10:25:42 +01005253 /* only release our endpoint if we don't intend to reuse the
5254 * connection.
5255 */
Willy Tarreaueee5b512015-04-03 23:46:31 +02005256 if (((s->txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) ||
Willy Tarreau350f4872014-11-28 14:42:25 +01005257 !si_conn_ready(&s->si[1])) {
5258 si_release_endpoint(&s->si[1]);
Willy Tarreau323a2d92015-08-04 19:00:17 +02005259 srv_conn = NULL;
Willy Tarreau4213a112013-12-15 10:25:42 +01005260 }
5261
Willy Tarreau350f4872014-11-28 14:42:25 +01005262 s->si[1].state = s->si[1].prev_state = SI_ST_INI;
5263 s->si[1].err_type = SI_ET_NONE;
5264 s->si[1].conn_retries = 0; /* used for logging too */
5265 s->si[1].exp = TICK_ETERNITY;
Willy Tarreau87b09662015-04-03 00:22:06 +02005266 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 +01005267 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);
5268 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 +02005269 s->flags &= ~(SF_DIRECT|SF_ASSIGNED|SF_ADDR_SET|SF_BE_ASSIGNED|SF_FORCE_PRST|SF_IGNORE_PRST);
5270 s->flags &= ~(SF_CURR_SESS|SF_REDIRECTABLE|SF_SRV_REUSED);
5271 s->flags &= ~(SF_ERR_MASK|SF_FINST_MASK|SF_REDISP);
Willy Tarreau543db622012-11-15 16:41:22 +01005272
Willy Tarreaueee5b512015-04-03 23:46:31 +02005273 s->txn->meth = 0;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005274 http_reset_txn(s);
Willy Tarreaueee5b512015-04-03 23:46:31 +02005275 s->txn->flags |= TX_NOT_FIRST | TX_WAIT_NEXT_RQ;
Willy Tarreau068621e2013-12-23 15:11:25 +01005276
5277 if (prev_status == 401 || prev_status == 407) {
5278 /* In HTTP keep-alive mode, if we receive a 401, we still have
5279 * a chance of being able to send the visitor again to the same
5280 * server over the same connection. This is required by some
5281 * broken protocols such as NTLM, and anyway whenever there is
5282 * an opportunity for sending the challenge to the proper place,
5283 * it's better to do it (at least it helps with debugging).
5284 */
Willy Tarreaueee5b512015-04-03 23:46:31 +02005285 s->txn->flags |= TX_PREFER_LAST;
Willy Tarreaubd99d582015-09-02 10:40:43 +02005286 if (srv_conn)
5287 srv_conn->flags |= CO_FL_PRIVATE;
Willy Tarreau068621e2013-12-23 15:11:25 +01005288 }
5289
Willy Tarreau53f96852016-02-02 18:50:47 +01005290 /* Never ever allow to reuse a connection from a non-reuse backend */
5291 if (srv_conn && (be->options & PR_O_REUSE_MASK) == PR_O_REUSE_NEVR)
5292 srv_conn->flags |= CO_FL_PRIVATE;
5293
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005294 if (fe->options2 & PR_O2_INDEPSTR)
Willy Tarreau350f4872014-11-28 14:42:25 +01005295 s->si[1].flags |= SI_FL_INDEP_STR;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005296
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005297 if (fe->options2 & PR_O2_NODELAY) {
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005298 s->req.flags |= CF_NEVER_WAIT;
5299 s->res.flags |= CF_NEVER_WAIT;
Willy Tarreau96e31212011-05-30 18:10:30 +02005300 }
5301
Willy Tarreau610ecce2010-01-04 21:15:02 +01005302 /* if the request buffer is not empty, it means we're
5303 * about to process another request, so send pending
5304 * data with MSG_MORE to merge TCP packets when possible.
Willy Tarreau065e8332010-01-08 00:30:20 +01005305 * Just don't do this if the buffer is close to be full,
5306 * because the request will wait for it to flush a little
5307 * bit before proceeding.
Willy Tarreau610ecce2010-01-04 21:15:02 +01005308 */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005309 if (s->req.buf->i) {
5310 if (s->res.buf->o &&
5311 !buffer_full(s->res.buf, global.tune.maxrewrite) &&
5312 bi_end(s->res.buf) <= s->res.buf->data + s->res.buf->size - global.tune.maxrewrite)
5313 s->res.flags |= CF_EXPECT_MORE;
Willy Tarreau065e8332010-01-08 00:30:20 +01005314 }
Willy Tarreau90deb182010-01-07 00:20:41 +01005315
Willy Tarreau714ea782015-11-25 20:11:11 +01005316 /* we're removing the analysers, we MUST re-enable events detection.
5317 * We don't enable close on the response channel since it's either
5318 * already closed, or in keep-alive with an idle connection handler.
5319 */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005320 channel_auto_read(&s->req);
5321 channel_auto_close(&s->req);
5322 channel_auto_read(&s->res);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005323
Willy Tarreau1c59bd52015-11-02 20:20:11 +01005324 /* we're in keep-alive with an idle connection, monitor it if not already done */
5325 if (srv_conn && LIST_ISEMPTY(&srv_conn->list)) {
Willy Tarreau323a2d92015-08-04 19:00:17 +02005326 srv = objt_server(srv_conn->target);
Willy Tarreau8dff9982015-08-04 20:45:52 +02005327 if (!srv)
5328 si_idle_conn(&s->si[1], NULL);
Willy Tarreau53f96852016-02-02 18:50:47 +01005329 else if (srv_conn->flags & CO_FL_PRIVATE)
Willy Tarreau8dff9982015-08-04 20:45:52 +02005330 si_idle_conn(&s->si[1], &srv->priv_conns);
Willy Tarreau449d74a2015-08-05 17:16:33 +02005331 else if (prev_flags & TX_NOT_FIRST)
5332 /* note: we check the request, not the connection, but
5333 * this is valid for strategies SAFE and AGGR, and in
5334 * case of ALWS, we don't care anyway.
5335 */
5336 si_idle_conn(&s->si[1], &srv->safe_conns);
Willy Tarreau8dff9982015-08-04 20:45:52 +02005337 else
5338 si_idle_conn(&s->si[1], &srv->idle_conns);
Willy Tarreau4320eaa2015-08-05 11:08:30 +02005339 }
Willy Tarreau27375622013-12-17 00:00:28 +01005340
Christopher Faulet3e344292015-11-24 16:24:13 +01005341 if (HAS_FILTERS(s)) {
Christopher Faulet0184ea72017-01-05 14:06:34 +01005342 s->req.analysers &= AN_REQ_FLT_END;
5343 s->res.analysers &= AN_RES_FLT_END;
Christopher Fauletd7c91962015-04-30 11:48:27 +02005344 }
Christopher Faulet3e344292015-11-24 16:24:13 +01005345 else {
5346 s->req.analysers = strm_li(s) ? strm_li(s)->analysers : 0;
5347 s->res.analysers = 0;
5348 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01005349}
5350
5351
5352/* This function updates the request state machine according to the response
5353 * state machine and buffer flags. It returns 1 if it changes anything (flag
5354 * or state), otherwise zero. It ignores any state before HTTP_MSG_DONE, as
5355 * it is only used to find when a request/response couple is complete. Both
5356 * this function and its equivalent should loop until both return zero. It
5357 * can set its own state to DONE, CLOSING, CLOSED, TUNNEL, ERROR.
5358 */
Willy Tarreau87b09662015-04-03 00:22:06 +02005359int http_sync_req_state(struct stream *s)
Willy Tarreau610ecce2010-01-04 21:15:02 +01005360{
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005361 struct channel *chn = &s->req;
Willy Tarreaueee5b512015-04-03 23:46:31 +02005362 struct http_txn *txn = s->txn;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005363 unsigned int old_flags = chn->flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005364 unsigned int old_state = txn->req.msg_state;
5365
Willy Tarreau610ecce2010-01-04 21:15:02 +01005366 if (unlikely(txn->req.msg_state < HTTP_MSG_BODY))
5367 return 0;
5368
5369 if (txn->req.msg_state == HTTP_MSG_DONE) {
Willy Tarreau90deb182010-01-07 00:20:41 +01005370 /* No need to read anymore, the request was completely parsed.
Willy Tarreau58bd8fd2010-09-28 14:16:41 +02005371 * We can shut the read side unless we want to abort_on_close,
5372 * or we have a POST request. The issue with POST requests is
5373 * that some browsers still send a CRLF after the request, and
5374 * this CRLF must be read so that it does not remain in the kernel
5375 * buffers, otherwise a close could cause an RST on some systems
5376 * (eg: Linux).
Willy Tarreau3988d932013-12-27 23:03:08 +01005377 * Note that if we're using keep-alive on the client side, we'd
5378 * rather poll now and keep the polling enabled for the whole
Willy Tarreau87b09662015-04-03 00:22:06 +02005379 * stream's life than enabling/disabling it between each
Willy Tarreau3988d932013-12-27 23:03:08 +01005380 * response and next request.
Willy Tarreau90deb182010-01-07 00:20:41 +01005381 */
Willy Tarreau3988d932013-12-27 23:03:08 +01005382 if (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_SCL) &&
5383 ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) &&
5384 !(s->be->options & PR_O_ABRT_CLOSE) &&
5385 txn->meth != HTTP_METH_POST)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005386 channel_dont_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005387
Willy Tarreau40f151a2012-12-20 12:10:09 +01005388 /* if the server closes the connection, we want to immediately react
5389 * and close the socket to save packets and syscalls.
5390 */
Willy Tarreau350f4872014-11-28 14:42:25 +01005391 s->si[1].flags |= SI_FL_NOHALF;
Willy Tarreau40f151a2012-12-20 12:10:09 +01005392
Willy Tarreau7f876a12015-11-18 11:59:55 +01005393 /* In any case we've finished parsing the request so we must
5394 * disable Nagle when sending data because 1) we're not going
5395 * to shut this side, and 2) the server is waiting for us to
5396 * send pending data.
5397 */
5398 chn->flags |= CF_NEVER_WAIT;
5399
Willy Tarreau610ecce2010-01-04 21:15:02 +01005400 if (txn->rsp.msg_state == HTTP_MSG_ERROR)
5401 goto wait_other_side;
5402
5403 if (txn->rsp.msg_state < HTTP_MSG_DONE) {
5404 /* The server has not finished to respond, so we
5405 * don't want to move in order not to upset it.
5406 */
5407 goto wait_other_side;
5408 }
5409
5410 if (txn->rsp.msg_state == HTTP_MSG_TUNNEL) {
5411 /* if any side switches to tunnel mode, the other one does too */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005412 channel_auto_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005413 txn->req.msg_state = HTTP_MSG_TUNNEL;
5414 goto wait_other_side;
5415 }
5416
5417 /* When we get here, it means that both the request and the
5418 * response have finished receiving. Depending on the connection
5419 * mode, we'll have to wait for the last bytes to leave in either
5420 * direction, and sometimes for a close to be effective.
5421 */
5422
Willy Tarreaucce7fa42010-01-16 23:19:39 +01005423 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
5424 /* Server-close mode : queue a connection close to the server */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005425 if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW)))
5426 channel_shutw_now(chn);
Willy Tarreaucce7fa42010-01-16 23:19:39 +01005427 }
5428 else if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) {
5429 /* Option forceclose is set, or either side wants to close,
5430 * let's enforce it now that we're not expecting any new
Willy Tarreau87b09662015-04-03 00:22:06 +02005431 * data to come. The caller knows the stream is complete
Willy Tarreaucce7fa42010-01-16 23:19:39 +01005432 * once both states are CLOSED.
5433 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005434 if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW))) {
5435 channel_shutr_now(chn);
5436 channel_shutw_now(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005437 }
Willy Tarreaucce7fa42010-01-16 23:19:39 +01005438 }
5439 else {
Willy Tarreau4213a112013-12-15 10:25:42 +01005440 /* The last possible modes are keep-alive and tunnel. Tunnel mode
5441 * will not have any analyser so it needs to poll for reads.
Willy Tarreaucce7fa42010-01-16 23:19:39 +01005442 */
Willy Tarreau4213a112013-12-15 10:25:42 +01005443 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN) {
5444 channel_auto_read(chn);
5445 txn->req.msg_state = HTTP_MSG_TUNNEL;
Willy Tarreau4213a112013-12-15 10:25:42 +01005446 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01005447 }
5448
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005449 if (chn->flags & (CF_SHUTW|CF_SHUTW_NOW)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005450 /* if we've just closed an output, let's switch */
Willy Tarreau350f4872014-11-28 14:42:25 +01005451 s->si[1].flags |= SI_FL_NOLINGER; /* we want to close ASAP */
Willy Tarreaucce7fa42010-01-16 23:19:39 +01005452
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005453 if (!channel_is_empty(chn)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005454 txn->req.msg_state = HTTP_MSG_CLOSING;
5455 goto http_msg_closing;
5456 }
5457 else {
5458 txn->req.msg_state = HTTP_MSG_CLOSED;
5459 goto http_msg_closed;
5460 }
5461 }
Willy Tarreaucce7fa42010-01-16 23:19:39 +01005462 goto wait_other_side;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005463 }
5464
5465 if (txn->req.msg_state == HTTP_MSG_CLOSING) {
5466 http_msg_closing:
5467 /* nothing else to forward, just waiting for the output buffer
5468 * to be empty and for the shutw_now to take effect.
5469 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005470 if (channel_is_empty(chn)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005471 txn->req.msg_state = HTTP_MSG_CLOSED;
5472 goto http_msg_closed;
5473 }
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005474 else if (chn->flags & CF_SHUTW) {
Willy Tarreau10e61cb2017-01-04 14:51:22 +01005475 txn->req.err_state = txn->req.msg_state;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005476 txn->req.msg_state = HTTP_MSG_ERROR;
5477 goto wait_other_side;
5478 }
5479 }
5480
5481 if (txn->req.msg_state == HTTP_MSG_CLOSED) {
5482 http_msg_closed:
Willy Tarreau3988d932013-12-27 23:03:08 +01005483 /* see above in MSG_DONE why we only do this in these states */
5484 if (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_SCL) &&
5485 ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) &&
5486 !(s->be->options & PR_O_ABRT_CLOSE))
Willy Tarreau2e7a1652013-12-15 15:32:10 +01005487 channel_dont_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005488 goto wait_other_side;
5489 }
5490
5491 wait_other_side:
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005492 return txn->req.msg_state != old_state || chn->flags != old_flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005493}
5494
5495
5496/* This function updates the response state machine according to the request
5497 * state machine and buffer flags. It returns 1 if it changes anything (flag
5498 * or state), otherwise zero. It ignores any state before HTTP_MSG_DONE, as
5499 * it is only used to find when a request/response couple is complete. Both
5500 * this function and its equivalent should loop until both return zero. It
5501 * can set its own state to DONE, CLOSING, CLOSED, TUNNEL, ERROR.
5502 */
Willy Tarreau87b09662015-04-03 00:22:06 +02005503int http_sync_res_state(struct stream *s)
Willy Tarreau610ecce2010-01-04 21:15:02 +01005504{
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005505 struct channel *chn = &s->res;
Willy Tarreaueee5b512015-04-03 23:46:31 +02005506 struct http_txn *txn = s->txn;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005507 unsigned int old_flags = chn->flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005508 unsigned int old_state = txn->rsp.msg_state;
5509
Willy Tarreau610ecce2010-01-04 21:15:02 +01005510 if (unlikely(txn->rsp.msg_state < HTTP_MSG_BODY))
5511 return 0;
5512
5513 if (txn->rsp.msg_state == HTTP_MSG_DONE) {
5514 /* In theory, we don't need to read anymore, but we must
Willy Tarreau90deb182010-01-07 00:20:41 +01005515 * still monitor the server connection for a possible close
5516 * while the request is being uploaded, so we don't disable
5517 * reading.
Willy Tarreau610ecce2010-01-04 21:15:02 +01005518 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005519 /* channel_dont_read(chn); */
Willy Tarreau610ecce2010-01-04 21:15:02 +01005520
5521 if (txn->req.msg_state == HTTP_MSG_ERROR)
5522 goto wait_other_side;
5523
5524 if (txn->req.msg_state < HTTP_MSG_DONE) {
5525 /* The client seems to still be sending data, probably
5526 * because we got an error response during an upload.
5527 * We have the choice of either breaking the connection
5528 * or letting it pass through. Let's do the later.
5529 */
5530 goto wait_other_side;
5531 }
5532
5533 if (txn->req.msg_state == HTTP_MSG_TUNNEL) {
5534 /* if any side switches to tunnel mode, the other one does too */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005535 channel_auto_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005536 txn->rsp.msg_state = HTTP_MSG_TUNNEL;
Willy Tarreaufc47f912012-10-20 10:38:09 +02005537 chn->flags |= CF_NEVER_WAIT;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005538 goto wait_other_side;
5539 }
5540
5541 /* When we get here, it means that both the request and the
5542 * response have finished receiving. Depending on the connection
5543 * mode, we'll have to wait for the last bytes to leave in either
5544 * direction, and sometimes for a close to be effective.
5545 */
5546
5547 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
5548 /* Server-close mode : shut read and wait for the request
5549 * side to close its output buffer. The caller will detect
5550 * when we're in DONE and the other is in CLOSED and will
5551 * catch that for the final cleanup.
5552 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005553 if (!(chn->flags & (CF_SHUTR|CF_SHUTR_NOW)))
5554 channel_shutr_now(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005555 }
Willy Tarreaucce7fa42010-01-16 23:19:39 +01005556 else if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) {
5557 /* Option forceclose is set, or either side wants to close,
5558 * let's enforce it now that we're not expecting any new
Willy Tarreau87b09662015-04-03 00:22:06 +02005559 * data to come. The caller knows the stream is complete
Willy Tarreaucce7fa42010-01-16 23:19:39 +01005560 * once both states are CLOSED.
Willy Tarreau610ecce2010-01-04 21:15:02 +01005561 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005562 if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW))) {
5563 channel_shutr_now(chn);
5564 channel_shutw_now(chn);
Willy Tarreaucce7fa42010-01-16 23:19:39 +01005565 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01005566 }
5567 else {
Willy Tarreau4213a112013-12-15 10:25:42 +01005568 /* The last possible modes are keep-alive and tunnel. Tunnel will
5569 * need to forward remaining data. Keep-alive will need to monitor
5570 * for connection closing.
Willy Tarreau610ecce2010-01-04 21:15:02 +01005571 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005572 channel_auto_read(chn);
Willy Tarreaufc47f912012-10-20 10:38:09 +02005573 chn->flags |= CF_NEVER_WAIT;
Willy Tarreau4213a112013-12-15 10:25:42 +01005574 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN)
5575 txn->rsp.msg_state = HTTP_MSG_TUNNEL;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005576 }
5577
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005578 if (chn->flags & (CF_SHUTW|CF_SHUTW_NOW)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005579 /* if we've just closed an output, let's switch */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005580 if (!channel_is_empty(chn)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005581 txn->rsp.msg_state = HTTP_MSG_CLOSING;
5582 goto http_msg_closing;
5583 }
5584 else {
5585 txn->rsp.msg_state = HTTP_MSG_CLOSED;
5586 goto http_msg_closed;
5587 }
5588 }
5589 goto wait_other_side;
5590 }
5591
5592 if (txn->rsp.msg_state == HTTP_MSG_CLOSING) {
5593 http_msg_closing:
5594 /* nothing else to forward, just waiting for the output buffer
5595 * to be empty and for the shutw_now to take effect.
5596 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005597 if (channel_is_empty(chn)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005598 txn->rsp.msg_state = HTTP_MSG_CLOSED;
5599 goto http_msg_closed;
5600 }
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005601 else if (chn->flags & CF_SHUTW) {
Willy Tarreau10e61cb2017-01-04 14:51:22 +01005602 txn->req.err_state = txn->req.msg_state;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005603 txn->rsp.msg_state = HTTP_MSG_ERROR;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005604 s->be->be_counters.cli_aborts++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005605 if (objt_server(s->target))
5606 objt_server(s->target)->counters.cli_aborts++;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005607 goto wait_other_side;
5608 }
5609 }
5610
5611 if (txn->rsp.msg_state == HTTP_MSG_CLOSED) {
5612 http_msg_closed:
5613 /* drop any pending data */
Willy Tarreau319f7452015-01-14 20:32:59 +01005614 channel_truncate(chn);
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005615 channel_auto_close(chn);
5616 channel_auto_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005617 goto wait_other_side;
5618 }
5619
5620 wait_other_side:
Willy Tarreaufc47f912012-10-20 10:38:09 +02005621 /* We force the response to leave immediately if we're waiting for the
5622 * other side, since there is no pending shutdown to push it out.
5623 */
5624 if (!channel_is_empty(chn))
5625 chn->flags |= CF_SEND_DONTWAIT;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005626 return txn->rsp.msg_state != old_state || chn->flags != old_flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005627}
5628
5629
5630/* Resync the request and response state machines. Return 1 if either state
5631 * changes.
5632 */
Willy Tarreau87b09662015-04-03 00:22:06 +02005633int http_resync_states(struct stream *s)
Willy Tarreau610ecce2010-01-04 21:15:02 +01005634{
Willy Tarreaueee5b512015-04-03 23:46:31 +02005635 struct http_txn *txn = s->txn;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005636 int old_req_state = txn->req.msg_state;
5637 int old_res_state = txn->rsp.msg_state;
5638
Willy Tarreau610ecce2010-01-04 21:15:02 +01005639 http_sync_req_state(s);
5640 while (1) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005641 if (!http_sync_res_state(s))
5642 break;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005643 if (!http_sync_req_state(s))
5644 break;
5645 }
Willy Tarreau3ce10ff2014-04-22 08:24:38 +02005646
Willy Tarreau610ecce2010-01-04 21:15:02 +01005647 /* OK, both state machines agree on a compatible state.
5648 * There are a few cases we're interested in :
5649 * - HTTP_MSG_TUNNEL on either means we have to disable both analysers
5650 * - HTTP_MSG_CLOSED on both sides means we've reached the end in both
5651 * directions, so let's simply disable both analysers.
5652 * - HTTP_MSG_CLOSED on the response only means we must abort the
5653 * request.
5654 * - HTTP_MSG_CLOSED on the request and HTTP_MSG_DONE on the response
5655 * with server-close mode means we've completed one request and we
5656 * must re-initialize the server connection.
5657 */
5658
5659 if (txn->req.msg_state == HTTP_MSG_TUNNEL ||
5660 txn->rsp.msg_state == HTTP_MSG_TUNNEL ||
5661 (txn->req.msg_state == HTTP_MSG_CLOSED &&
5662 txn->rsp.msg_state == HTTP_MSG_CLOSED)) {
Christopher Faulet0184ea72017-01-05 14:06:34 +01005663 s->req.analysers &= AN_REQ_FLT_END;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005664 channel_auto_close(&s->req);
5665 channel_auto_read(&s->req);
Christopher Faulet0184ea72017-01-05 14:06:34 +01005666 s->res.analysers &= AN_RES_FLT_END;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005667 channel_auto_close(&s->res);
5668 channel_auto_read(&s->res);
Christopher Faulet6962f4e2016-11-28 14:21:27 +01005669 if (txn->req.msg_state == HTTP_MSG_TUNNEL && HAS_REQ_DATA_FILTERS(s))
Christopher Faulet0184ea72017-01-05 14:06:34 +01005670 s->req.analysers |= AN_REQ_FLT_XFER_DATA;
Christopher Faulet6962f4e2016-11-28 14:21:27 +01005671 if (txn->rsp.msg_state == HTTP_MSG_TUNNEL && HAS_RSP_DATA_FILTERS(s))
Christopher Faulet0184ea72017-01-05 14:06:34 +01005672 s->res.analysers |= AN_RES_FLT_XFER_DATA;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005673 }
Willy Tarreau40f151a2012-12-20 12:10:09 +01005674 else if ((txn->req.msg_state >= HTTP_MSG_DONE &&
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005675 (txn->rsp.msg_state == HTTP_MSG_CLOSED || (s->res.flags & CF_SHUTW))) ||
Willy Tarreau2fa144c2010-01-04 23:13:26 +01005676 txn->rsp.msg_state == HTTP_MSG_ERROR ||
Willy Tarreau40f151a2012-12-20 12:10:09 +01005677 txn->req.msg_state == HTTP_MSG_ERROR) {
Christopher Faulet0184ea72017-01-05 14:06:34 +01005678 s->res.analysers &= AN_RES_FLT_END;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005679 channel_auto_close(&s->res);
5680 channel_auto_read(&s->res);
Christopher Faulet0184ea72017-01-05 14:06:34 +01005681 s->req.analysers &= AN_REQ_FLT_END;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005682 channel_abort(&s->req);
5683 channel_auto_close(&s->req);
5684 channel_auto_read(&s->req);
5685 channel_truncate(&s->req);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005686 }
Willy Tarreau4213a112013-12-15 10:25:42 +01005687 else if ((txn->req.msg_state == HTTP_MSG_DONE ||
5688 txn->req.msg_state == HTTP_MSG_CLOSED) &&
Willy Tarreau610ecce2010-01-04 21:15:02 +01005689 txn->rsp.msg_state == HTTP_MSG_DONE &&
Willy Tarreau4213a112013-12-15 10:25:42 +01005690 ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL ||
5691 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL)) {
5692 /* server-close/keep-alive: terminate this transaction,
5693 * possibly killing the server connection and reinitialize
Willy Tarreau3de5bd62016-05-02 16:07:07 +02005694 * a fresh-new transaction, but only once we're sure there's
5695 * enough room in the request and response buffer to process
5696 * another request. The request buffer must not hold any
5697 * pending output data and the request buffer must not have
5698 * output data occupying the reserve.
Willy Tarreau610ecce2010-01-04 21:15:02 +01005699 */
Willy Tarreau3de5bd62016-05-02 16:07:07 +02005700 if (s->req.buf->o)
5701 s->req.flags |= CF_WAKE_WRITE;
5702 else if (channel_congested(&s->res))
5703 s->res.flags |= CF_WAKE_WRITE;
5704 else
5705 http_end_txn_clean_session(s);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005706 }
5707
Willy Tarreau610ecce2010-01-04 21:15:02 +01005708 return txn->req.msg_state != old_req_state ||
5709 txn->rsp.msg_state != old_res_state;
5710}
5711
Willy Tarreaud98cf932009-12-27 22:54:55 +01005712/* This function is an analyser which forwards request body (including chunk
5713 * sizes if any). It is called as soon as we must forward, even if we forward
5714 * zero byte. The only situation where it must not be called is when we're in
5715 * tunnel mode and we want to forward till the close. It's used both to forward
5716 * remaining data and to resync after end of body. It expects the msg_state to
5717 * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to
Willy Tarreau87b09662015-04-03 00:22:06 +02005718 * read more data, or 1 once we can go on with next request or end the stream.
Willy Tarreau124d9912011-03-01 20:30:48 +01005719 * When in MSG_DATA or MSG_TRAILERS, it will automatically forward chunk_len
Willy Tarreauc24715e2014-04-17 15:21:20 +02005720 * bytes of pending data + the headers if not already done.
Willy Tarreaud98cf932009-12-27 22:54:55 +01005721 */
Willy Tarreau87b09662015-04-03 00:22:06 +02005722int http_request_forward_body(struct stream *s, struct channel *req, int an_bit)
Willy Tarreaud98cf932009-12-27 22:54:55 +01005723{
Willy Tarreaufb0afa72015-04-03 14:46:27 +02005724 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02005725 struct http_txn *txn = s->txn;
5726 struct http_msg *msg = &s->txn->req;
Christopher Faulet3e344292015-11-24 16:24:13 +01005727 int ret;
Willy Tarreaud98cf932009-12-27 22:54:55 +01005728
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005729 if (unlikely(msg->msg_state < HTTP_MSG_BODY))
5730 return 0;
5731
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005732 if ((req->flags & (CF_READ_ERROR|CF_READ_TIMEOUT|CF_WRITE_ERROR|CF_WRITE_TIMEOUT)) ||
Willy Tarreau9b28e032012-10-12 23:49:43 +02005733 ((req->flags & CF_SHUTW) && (req->to_forward || req->buf->o))) {
Willy Tarreau4fe41902010-06-07 22:27:41 +02005734 /* Output closed while we were sending data. We must abort and
5735 * wake the other side up.
5736 */
Willy Tarreau10e61cb2017-01-04 14:51:22 +01005737 msg->err_state = msg->msg_state;
Willy Tarreau4fe41902010-06-07 22:27:41 +02005738 msg->msg_state = HTTP_MSG_ERROR;
5739 http_resync_states(s);
Willy Tarreau082b01c2010-01-02 23:58:04 +01005740 return 1;
5741 }
5742
Willy Tarreaud98cf932009-12-27 22:54:55 +01005743 /* Note that we don't have to send 100-continue back because we don't
5744 * need the data to complete our job, and it's up to the server to
5745 * decide whether to return 100, 417 or anything else in return of
5746 * an "Expect: 100-continue" header.
5747 */
Christopher Fauletd7c91962015-04-30 11:48:27 +02005748 if (msg->msg_state == HTTP_MSG_BODY) {
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005749 msg->msg_state = ((msg->flags & HTTP_MSGF_TE_CHNK)
5750 ? HTTP_MSG_CHUNK_SIZE
5751 : HTTP_MSG_DATA);
Christopher Fauletd7c91962015-04-30 11:48:27 +02005752
5753 /* TODO/filters: when http-buffer-request option is set or if a
5754 * rule on url_param exists, the first chunk size could be
5755 * already parsed. In that case, msg->next is after the chunk
5756 * size (including the CRLF after the size). So this case should
5757 * be handled to */
Willy Tarreaud98cf932009-12-27 22:54:55 +01005758 }
5759
Willy Tarreau7ba23542014-04-17 21:50:00 +02005760 /* Some post-connect processing might want us to refrain from starting to
5761 * forward data. Currently, the only reason for this is "balance url_param"
5762 * whichs need to parse/process the request after we've enabled forwarding.
5763 */
5764 if (unlikely(msg->flags & HTTP_MSGF_WAIT_CONN)) {
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005765 if (!(s->res.flags & CF_READ_ATTACHED)) {
Willy Tarreau7ba23542014-04-17 21:50:00 +02005766 channel_auto_connect(req);
Willy Tarreau644c1012014-04-30 18:11:11 +02005767 req->flags |= CF_WAKE_CONNECT;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005768 goto missing_data_or_waiting;
Willy Tarreau7ba23542014-04-17 21:50:00 +02005769 }
5770 msg->flags &= ~HTTP_MSGF_WAIT_CONN;
5771 }
5772
Willy Tarreau80a92c02014-03-12 10:41:13 +01005773 /* in most states, we should abort in case of early close */
5774 channel_auto_close(req);
5775
Willy Tarreauefdf0942014-04-24 20:08:57 +02005776 if (req->to_forward) {
5777 /* We can't process the buffer's contents yet */
5778 req->flags |= CF_WAKE_WRITE;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005779 goto missing_data_or_waiting;
Willy Tarreauefdf0942014-04-24 20:08:57 +02005780 }
5781
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005782 if (msg->msg_state < HTTP_MSG_DONE) {
5783 ret = ((msg->flags & HTTP_MSGF_TE_CHNK)
5784 ? http_msg_forward_chunked_body(s, msg)
5785 : http_msg_forward_body(s, msg));
5786 if (!ret)
5787 goto missing_data_or_waiting;
5788 if (ret < 0)
5789 goto return_bad_req;
5790 }
Christopher Fauletd7c91962015-04-30 11:48:27 +02005791
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005792 /* other states, DONE...TUNNEL */
5793 /* we don't want to forward closes on DONE except in tunnel mode. */
5794 if ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN)
5795 channel_dont_close(req);
Willy Tarreau5c54c712010-07-17 08:02:58 +02005796
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005797 if (http_resync_states(s)) {
5798 /* some state changes occurred, maybe the analyser
5799 * was disabled too. */
5800 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
5801 if (req->flags & CF_SHUTW) {
5802 /* request errors are most likely due to the
5803 * server aborting the transfer. */
5804 goto aborted_xfer;
Willy Tarreau58bd8fd2010-09-28 14:16:41 +02005805 }
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005806 if (msg->err_pos >= 0)
Willy Tarreau10e61cb2017-01-04 14:51:22 +01005807 http_capture_bad_message(&sess->fe->invalid_req, s, msg, msg->err_state, s->be);
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005808 goto return_bad_req;
Willy Tarreaud98cf932009-12-27 22:54:55 +01005809 }
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005810 return 1;
Willy Tarreaud98cf932009-12-27 22:54:55 +01005811 }
5812
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005813 /* If "option abortonclose" is set on the backend, we want to monitor
5814 * the client's connection and forward any shutdown notification to the
5815 * server, which will decide whether to close or to go on processing the
5816 * request. We only do that in tunnel mode, and not in other modes since
5817 * it can be abused to exhaust source ports. */
5818 if (s->be->options & PR_O_ABRT_CLOSE) {
5819 channel_auto_read(req);
5820 if ((req->flags & (CF_SHUTR|CF_READ_NULL)) &&
5821 ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN))
5822 s->si[1].flags |= SI_FL_NOLINGER;
5823 channel_auto_close(req);
5824 }
5825 else if (s->txn->meth == HTTP_METH_POST) {
5826 /* POST requests may require to read extra CRLF sent by broken
5827 * browsers and which could cause an RST to be sent upon close
5828 * on some systems (eg: Linux). */
5829 channel_auto_read(req);
5830 }
5831 return 0;
Willy Tarreaubed410e2014-04-22 08:19:34 +02005832
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005833 missing_data_or_waiting:
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005834 /* stop waiting for data if the input is closed before the end */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005835 if (req->flags & CF_SHUTR) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02005836 if (!(s->flags & SF_ERR_MASK))
5837 s->flags |= SF_ERR_CLICL;
5838 if (!(s->flags & SF_FINST_MASK)) {
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005839 if (txn->rsp.msg_state < HTTP_MSG_ERROR)
Willy Tarreaue7dff022015-04-03 01:14:29 +02005840 s->flags |= SF_FINST_H;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005841 else
Willy Tarreaue7dff022015-04-03 01:14:29 +02005842 s->flags |= SF_FINST_D;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005843 }
5844
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005845 sess->fe->fe_counters.cli_aborts++;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005846 s->be->be_counters.cli_aborts++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005847 if (objt_server(s->target))
5848 objt_server(s->target)->counters.cli_aborts++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005849
5850 goto return_bad_req_stats_ok;
Willy Tarreau79ebac62010-06-07 13:47:49 +02005851 }
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005852
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005853 /* waiting for the last bits to leave the buffer */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005854 if (req->flags & CF_SHUTW)
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005855 goto aborted_xfer;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005856
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02005857 /* When TE: chunked is used, we need to get there again to parse remaining
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005858 * chunks even if the client has closed, so we don't want to set CF_DONTCLOSE.
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02005859 */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005860 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005861 channel_dont_close(req);
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02005862
Willy Tarreau5c620922011-05-11 19:56:11 +02005863 /* We know that more data are expected, but we couldn't send more that
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005864 * what we did. So we always set the CF_EXPECT_MORE flag so that the
Willy Tarreau07293032011-05-30 18:29:28 +02005865 * system knows it must not set a PUSH on this first part. Interactive
Willy Tarreau869fc1e2012-03-05 08:29:20 +01005866 * modes are already handled by the stream sock layer. We must not do
5867 * this in content-length mode because it could present the MSG_MORE
5868 * flag with the last block of forwarded data, which would cause an
5869 * additional delay to be observed by the receiver.
Willy Tarreau5c620922011-05-11 19:56:11 +02005870 */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005871 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005872 req->flags |= CF_EXPECT_MORE;
Willy Tarreau5c620922011-05-11 19:56:11 +02005873
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005874 return 0;
5875
Willy Tarreaud98cf932009-12-27 22:54:55 +01005876 return_bad_req: /* let's centralize all bad requests */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005877 sess->fe->fe_counters.failed_req++;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02005878 if (sess->listener->counters)
5879 sess->listener->counters->failed_req++;
Willy Tarreaubed410e2014-04-22 08:19:34 +02005880
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005881 return_bad_req_stats_ok:
Willy Tarreau10e61cb2017-01-04 14:51:22 +01005882 txn->req.err_state = txn->req.msg_state;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005883 txn->req.msg_state = HTTP_MSG_ERROR;
5884 if (txn->status) {
5885 /* Note: we don't send any error if some data were already sent */
Christopher Fauleta94e5a52015-12-09 15:55:06 +01005886 http_reply_and_close(s, txn->status, NULL);
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005887 } else {
5888 txn->status = 400;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02005889 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005890 }
Christopher Faulet0184ea72017-01-05 14:06:34 +01005891 req->analysers &= AN_REQ_FLT_END;
5892 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 +01005893
Willy Tarreaue7dff022015-04-03 01:14:29 +02005894 if (!(s->flags & SF_ERR_MASK))
5895 s->flags |= SF_ERR_PRXCOND;
5896 if (!(s->flags & SF_FINST_MASK)) {
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005897 if (txn->rsp.msg_state < HTTP_MSG_ERROR)
Willy Tarreaue7dff022015-04-03 01:14:29 +02005898 s->flags |= SF_FINST_H;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005899 else
Willy Tarreaue7dff022015-04-03 01:14:29 +02005900 s->flags |= SF_FINST_D;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005901 }
5902 return 0;
5903
5904 aborted_xfer:
Willy Tarreau10e61cb2017-01-04 14:51:22 +01005905 txn->req.err_state = txn->req.msg_state;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005906 txn->req.msg_state = HTTP_MSG_ERROR;
5907 if (txn->status) {
5908 /* Note: we don't send any error if some data were already sent */
Christopher Fauleta94e5a52015-12-09 15:55:06 +01005909 http_reply_and_close(s, txn->status, NULL);
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005910 } else {
5911 txn->status = 502;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02005912 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005913 }
Christopher Faulet0184ea72017-01-05 14:06:34 +01005914 req->analysers &= AN_REQ_FLT_END;
5915 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 +01005916
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005917 sess->fe->fe_counters.srv_aborts++;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005918 s->be->be_counters.srv_aborts++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005919 if (objt_server(s->target))
5920 objt_server(s->target)->counters.srv_aborts++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005921
Willy Tarreaue7dff022015-04-03 01:14:29 +02005922 if (!(s->flags & SF_ERR_MASK))
5923 s->flags |= SF_ERR_SRVCL;
5924 if (!(s->flags & SF_FINST_MASK)) {
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005925 if (txn->rsp.msg_state < HTTP_MSG_ERROR)
Willy Tarreaue7dff022015-04-03 01:14:29 +02005926 s->flags |= SF_FINST_H;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005927 else
Willy Tarreaue7dff022015-04-03 01:14:29 +02005928 s->flags |= SF_FINST_D;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005929 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01005930 return 0;
5931}
5932
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005933/* This stream analyser waits for a complete HTTP response. It returns 1 if the
5934 * processing can continue on next analysers, or zero if it either needs more
5935 * data or wants to immediately abort the response (eg: timeout, error, ...). It
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005936 * is tied to AN_RES_WAIT_HTTP and may may remove itself from s->res.analysers
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005937 * when it has nothing left to do, and may remove any analyser when it wants to
5938 * abort.
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02005939 */
Willy Tarreau87b09662015-04-03 00:22:06 +02005940int http_wait_for_response(struct stream *s, struct channel *rep, int an_bit)
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02005941{
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005942 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02005943 struct http_txn *txn = s->txn;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005944 struct http_msg *msg = &txn->rsp;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005945 struct hdr_ctx ctx;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005946 int use_close_only;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005947 int cur_idx;
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02005948 int n;
Willy Tarreauadfb8562008-08-11 15:24:42 +02005949
Willy Tarreau87b09662015-04-03 00:22:06 +02005950 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 +02005951 now_ms, __FUNCTION__,
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005952 s,
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02005953 rep,
5954 rep->rex, rep->wex,
5955 rep->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02005956 rep->buf->i,
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02005957 rep->analysers);
Willy Tarreau67f0eea2008-08-10 22:55:22 +02005958
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005959 /*
5960 * Now parse the partial (or complete) lines.
5961 * We will check the response syntax, and also join multi-line
5962 * headers. An index of all the lines will be elaborated while
5963 * parsing.
5964 *
5965 * For the parsing, we use a 28 states FSM.
5966 *
5967 * Here is the information we currently have :
Willy Tarreau9b28e032012-10-12 23:49:43 +02005968 * rep->buf->p = beginning of response
5969 * rep->buf->p + msg->eoh = end of processed headers / start of current one
5970 * rep->buf->p + rep->buf->i = end of input data
Willy Tarreau26927362012-05-18 23:22:52 +02005971 * msg->eol = end of current header or line (LF or CRLF)
5972 * msg->next = first non-visited byte
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005973 */
5974
Willy Tarreau628c40c2014-04-24 19:11:26 +02005975 next_one:
Willy Tarreau83e3af02009-12-28 17:39:57 +01005976 /* There's a protected area at the end of the buffer for rewriting
5977 * purposes. We don't want to start to parse the request if the
5978 * protected area is affected, because we may have to move processed
5979 * data later, which is much more complicated.
5980 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02005981 if (buffer_not_empty(rep->buf) && msg->msg_state < HTTP_MSG_ERROR) {
Willy Tarreauba0902e2015-01-13 14:39:16 +01005982 if (unlikely(!channel_is_rewritable(rep))) {
Willy Tarreau379357a2013-06-08 12:55:46 +02005983 /* some data has still not left the buffer, wake us once that's done */
5984 if (rep->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_WRITE_ERROR|CF_WRITE_TIMEOUT))
5985 goto abort_response;
5986 channel_dont_close(rep);
5987 rep->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
Willy Tarreaud7ad9f52013-12-31 17:26:25 +01005988 rep->flags |= CF_WAKE_WRITE;
Willy Tarreau379357a2013-06-08 12:55:46 +02005989 return 0;
Willy Tarreau83e3af02009-12-28 17:39:57 +01005990 }
5991
Willy Tarreau379357a2013-06-08 12:55:46 +02005992 if (unlikely(bi_end(rep->buf) < b_ptr(rep->buf, msg->next) ||
5993 bi_end(rep->buf) > rep->buf->data + rep->buf->size - global.tune.maxrewrite))
5994 buffer_slow_realign(rep->buf);
5995
Willy Tarreau9b28e032012-10-12 23:49:43 +02005996 if (likely(msg->next < rep->buf->i))
Willy Tarreaua560c212012-03-09 13:50:57 +01005997 http_msg_analyzer(msg, &txn->hdr_idx);
Willy Tarreau83e3af02009-12-28 17:39:57 +01005998 }
5999
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006000 /* 1: we might have to print this header in debug mode */
6001 if (unlikely((global.mode & MODE_DEBUG) &&
6002 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) &&
Willy Tarreau7d59e902014-10-21 19:36:09 +02006003 msg->msg_state >= HTTP_MSG_BODY)) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006004 char *eol, *sol;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006005
Willy Tarreau9b28e032012-10-12 23:49:43 +02006006 sol = rep->buf->p;
6007 eol = sol + (msg->sl.st.l ? msg->sl.st.l : rep->buf->i);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006008 debug_hdr("srvrep", s, sol, eol);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006009
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006010 sol += hdr_idx_first_pos(&txn->hdr_idx);
6011 cur_idx = hdr_idx_first_idx(&txn->hdr_idx);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006012
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006013 while (cur_idx) {
6014 eol = sol + txn->hdr_idx.v[cur_idx].len;
6015 debug_hdr("srvhdr", s, sol, eol);
6016 sol = eol + txn->hdr_idx.v[cur_idx].cr + 1;
6017 cur_idx = txn->hdr_idx.v[cur_idx].next;
6018 }
6019 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006020
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006021 /*
6022 * Now we quickly check if we have found a full valid response.
6023 * If not so, we check the FD and buffer states before leaving.
6024 * A full response is indicated by the fact that we have seen
Willy Tarreau655dce92009-11-08 13:10:58 +01006025 * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006026 * responses are checked first.
6027 *
6028 * Depending on whether the client is still there or not, we
6029 * may send an error response back or not. Note that normally
6030 * we should only check for HTTP status there, and check I/O
6031 * errors somewhere else.
6032 */
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006033
Willy Tarreau655dce92009-11-08 13:10:58 +01006034 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006035 /* Invalid response */
6036 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
6037 /* we detected a parsing error. We want to archive this response
6038 * in the dedicated proxy area for later troubleshooting.
6039 */
6040 hdr_response_bad:
6041 if (msg->msg_state == HTTP_MSG_ERROR || msg->err_pos >= 0)
Willy Tarreau10e61cb2017-01-04 14:51:22 +01006042 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->err_state, sess->fe);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006043
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01006044 s->be->be_counters.failed_resp++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006045 if (objt_server(s->target)) {
6046 objt_server(s->target)->counters.failed_resp++;
6047 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_HDRRSP);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01006048 }
Willy Tarreau64648412010-03-05 10:41:54 +01006049 abort_response:
Willy Tarreau8263d2b2012-08-28 00:06:31 +02006050 channel_auto_close(rep);
Christopher Faulet0184ea72017-01-05 14:06:34 +01006051 rep->analysers &= AN_RES_FLT_END;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006052 txn->status = 502;
Willy Tarreau350f4872014-11-28 14:42:25 +01006053 s->si[1].flags |= SI_FL_NOLINGER;
Willy Tarreau319f7452015-01-14 20:32:59 +01006054 channel_truncate(rep);
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02006055 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006056
Willy Tarreaue7dff022015-04-03 01:14:29 +02006057 if (!(s->flags & SF_ERR_MASK))
6058 s->flags |= SF_ERR_PRXCOND;
6059 if (!(s->flags & SF_FINST_MASK))
6060 s->flags |= SF_FINST_H;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006061
6062 return 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02006063 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02006064
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006065 /* too large response does not fit in buffer. */
Willy Tarreau9b28e032012-10-12 23:49:43 +02006066 else if (buffer_full(rep->buf, global.tune.maxrewrite)) {
Willy Tarreaufec4d892011-09-02 20:04:57 +02006067 if (msg->err_pos < 0)
Willy Tarreau9b28e032012-10-12 23:49:43 +02006068 msg->err_pos = rep->buf->i;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006069 goto hdr_response_bad;
6070 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006071
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006072 /* read error */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02006073 else if (rep->flags & CF_READ_ERROR) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006074 if (msg->err_pos >= 0)
Willy Tarreau10e61cb2017-01-04 14:51:22 +01006075 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->err_state, sess->fe);
Willy Tarreau6b726ad2013-12-15 19:31:37 +01006076 else if (txn->flags & TX_NOT_FIRST)
6077 goto abort_keep_alive;
Willy Tarreau4076a152009-04-02 15:18:36 +02006078
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01006079 s->be->be_counters.failed_resp++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006080 if (objt_server(s->target)) {
6081 objt_server(s->target)->counters.failed_resp++;
6082 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_READ_ERROR);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01006083 }
Willy Tarreau461f6622008-08-15 23:43:19 +02006084
Willy Tarreau8263d2b2012-08-28 00:06:31 +02006085 channel_auto_close(rep);
Christopher Faulet0184ea72017-01-05 14:06:34 +01006086 rep->analysers &= AN_RES_FLT_END;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006087 txn->status = 502;
Willy Tarreau350f4872014-11-28 14:42:25 +01006088 s->si[1].flags |= SI_FL_NOLINGER;
Willy Tarreau319f7452015-01-14 20:32:59 +01006089 channel_truncate(rep);
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02006090 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreau816b9792009-09-15 21:25:21 +02006091
Willy Tarreaue7dff022015-04-03 01:14:29 +02006092 if (!(s->flags & SF_ERR_MASK))
6093 s->flags |= SF_ERR_SRVCL;
6094 if (!(s->flags & SF_FINST_MASK))
6095 s->flags |= SF_FINST_H;
Willy Tarreaucebf57e2008-08-15 18:16:37 +02006096 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006097 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02006098
Willy Tarreau6f0a7ba2014-06-23 15:22:31 +02006099 /* read timeout : return a 504 to the client. */
6100 else if (rep->flags & CF_READ_TIMEOUT) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006101 if (msg->err_pos >= 0)
Willy Tarreau10e61cb2017-01-04 14:51:22 +01006102 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->err_state, sess->fe);
Willy Tarreau21d2af32008-02-14 20:25:24 +01006103
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01006104 s->be->be_counters.failed_resp++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006105 if (objt_server(s->target)) {
6106 objt_server(s->target)->counters.failed_resp++;
6107 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_READ_TIMEOUT);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01006108 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01006109
Willy Tarreau8263d2b2012-08-28 00:06:31 +02006110 channel_auto_close(rep);
Christopher Faulet0184ea72017-01-05 14:06:34 +01006111 rep->analysers &= AN_RES_FLT_END;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006112 txn->status = 504;
Willy Tarreau350f4872014-11-28 14:42:25 +01006113 s->si[1].flags |= SI_FL_NOLINGER;
Willy Tarreau319f7452015-01-14 20:32:59 +01006114 channel_truncate(rep);
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02006115 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreau4076a152009-04-02 15:18:36 +02006116
Willy Tarreaue7dff022015-04-03 01:14:29 +02006117 if (!(s->flags & SF_ERR_MASK))
6118 s->flags |= SF_ERR_SRVTO;
6119 if (!(s->flags & SF_FINST_MASK))
6120 s->flags |= SF_FINST_H;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006121 return 0;
6122 }
Willy Tarreaua7c52762008-08-16 18:40:18 +02006123
Willy Tarreauf003d372012-11-26 13:35:37 +01006124 /* client abort with an abortonclose */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006125 else if ((rep->flags & CF_SHUTR) && ((s->req.flags & (CF_SHUTR|CF_SHUTW)) == (CF_SHUTR|CF_SHUTW))) {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006126 sess->fe->fe_counters.cli_aborts++;
Willy Tarreauf003d372012-11-26 13:35:37 +01006127 s->be->be_counters.cli_aborts++;
6128 if (objt_server(s->target))
6129 objt_server(s->target)->counters.cli_aborts++;
6130
Christopher Faulet0184ea72017-01-05 14:06:34 +01006131 rep->analysers &= AN_RES_FLT_END;
Willy Tarreauf003d372012-11-26 13:35:37 +01006132 channel_auto_close(rep);
6133
6134 txn->status = 400;
Willy Tarreau319f7452015-01-14 20:32:59 +01006135 channel_truncate(rep);
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02006136 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreauf003d372012-11-26 13:35:37 +01006137
Willy Tarreaue7dff022015-04-03 01:14:29 +02006138 if (!(s->flags & SF_ERR_MASK))
6139 s->flags |= SF_ERR_CLICL;
6140 if (!(s->flags & SF_FINST_MASK))
6141 s->flags |= SF_FINST_H;
Willy Tarreauf003d372012-11-26 13:35:37 +01006142
Willy Tarreau87b09662015-04-03 00:22:06 +02006143 /* process_stream() will take care of the error */
Willy Tarreauf003d372012-11-26 13:35:37 +01006144 return 0;
6145 }
6146
Willy Tarreau3b8c08a2011-09-02 20:16:24 +02006147 /* close from server, capture the response if the server has started to respond */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02006148 else if (rep->flags & CF_SHUTR) {
Willy Tarreau3b8c08a2011-09-02 20:16:24 +02006149 if (msg->msg_state >= HTTP_MSG_RPVER || msg->err_pos >= 0)
Willy Tarreau10e61cb2017-01-04 14:51:22 +01006150 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->err_state, sess->fe);
Willy Tarreau6b726ad2013-12-15 19:31:37 +01006151 else if (txn->flags & TX_NOT_FIRST)
6152 goto abort_keep_alive;
Willy Tarreau21d2af32008-02-14 20:25:24 +01006153
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01006154 s->be->be_counters.failed_resp++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006155 if (objt_server(s->target)) {
6156 objt_server(s->target)->counters.failed_resp++;
6157 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_BROKEN_PIPE);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01006158 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01006159
Willy Tarreau8263d2b2012-08-28 00:06:31 +02006160 channel_auto_close(rep);
Christopher Faulet0184ea72017-01-05 14:06:34 +01006161 rep->analysers &= AN_RES_FLT_END;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006162 txn->status = 502;
Willy Tarreau350f4872014-11-28 14:42:25 +01006163 s->si[1].flags |= SI_FL_NOLINGER;
Willy Tarreau319f7452015-01-14 20:32:59 +01006164 channel_truncate(rep);
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02006165 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreau21d2af32008-02-14 20:25:24 +01006166
Willy Tarreaue7dff022015-04-03 01:14:29 +02006167 if (!(s->flags & SF_ERR_MASK))
6168 s->flags |= SF_ERR_SRVCL;
6169 if (!(s->flags & SF_FINST_MASK))
6170 s->flags |= SF_FINST_H;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006171 return 0;
6172 }
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02006173
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006174 /* write error to client (we don't send any message then) */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02006175 else if (rep->flags & CF_WRITE_ERROR) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006176 if (msg->err_pos >= 0)
Willy Tarreau10e61cb2017-01-04 14:51:22 +01006177 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->err_state, sess->fe);
Willy Tarreau6b726ad2013-12-15 19:31:37 +01006178 else if (txn->flags & TX_NOT_FIRST)
6179 goto abort_keep_alive;
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02006180
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01006181 s->be->be_counters.failed_resp++;
Christopher Faulet0184ea72017-01-05 14:06:34 +01006182 rep->analysers &= AN_RES_FLT_END;
Willy Tarreau8263d2b2012-08-28 00:06:31 +02006183 channel_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006184
Willy Tarreaue7dff022015-04-03 01:14:29 +02006185 if (!(s->flags & SF_ERR_MASK))
6186 s->flags |= SF_ERR_CLICL;
6187 if (!(s->flags & SF_FINST_MASK))
6188 s->flags |= SF_FINST_H;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006189
Willy Tarreau87b09662015-04-03 00:22:06 +02006190 /* process_stream() will take care of the error */
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006191 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006192 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01006193
Willy Tarreau8263d2b2012-08-28 00:06:31 +02006194 channel_dont_close(rep);
Willy Tarreau3f3997e2013-12-15 15:21:32 +01006195 rep->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006196 return 0;
6197 }
6198
6199 /* More interesting part now : we know that we have a complete
6200 * response which at least looks like HTTP. We have an indicator
6201 * of each header's length, so we can parse them quickly.
6202 */
6203
6204 if (unlikely(msg->err_pos >= 0))
Willy Tarreau10e61cb2017-01-04 14:51:22 +01006205 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->err_state, sess->fe);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006206
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006207 /*
6208 * 1: get the status code
6209 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02006210 n = rep->buf->p[msg->sl.st.c] - '0';
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006211 if (n < 1 || n > 5)
6212 n = 0;
Willy Tarreauda7ff642010-06-23 11:44:09 +02006213 /* when the client triggers a 4xx from the server, it's most often due
6214 * to a missing object or permission. These events should be tracked
6215 * because if they happen often, it may indicate a brute force or a
6216 * vulnerability scan.
6217 */
6218 if (n == 4)
Willy Tarreau87b09662015-04-03 00:22:06 +02006219 stream_inc_http_err_ctr(s);
Willy Tarreauda7ff642010-06-23 11:44:09 +02006220
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006221 if (objt_server(s->target))
6222 objt_server(s->target)->counters.p.http.rsp[n]++;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006223
Willy Tarreau91852eb2015-05-01 13:26:00 +02006224 /* RFC7230#2.6 has enforced the format of the HTTP version string to be
6225 * exactly one digit "." one digit. This check may be disabled using
6226 * option accept-invalid-http-response.
6227 */
6228 if (!(s->be->options2 & PR_O2_RSPBUG_OK)) {
6229 if (msg->sl.st.v_l != 8) {
6230 msg->err_pos = 0;
6231 goto hdr_response_bad;
6232 }
6233
6234 if (rep->buf->p[4] != '/' ||
6235 !isdigit((unsigned char)rep->buf->p[5]) ||
6236 rep->buf->p[6] != '.' ||
6237 !isdigit((unsigned char)rep->buf->p[7])) {
6238 msg->err_pos = 4;
6239 goto hdr_response_bad;
6240 }
6241 }
6242
Willy Tarreau5b154472009-12-21 20:11:07 +01006243 /* check if the response is HTTP/1.1 or above */
6244 if ((msg->sl.st.v_l == 8) &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02006245 ((rep->buf->p[5] > '1') ||
6246 ((rep->buf->p[5] == '1') && (rep->buf->p[7] >= '1'))))
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006247 msg->flags |= HTTP_MSGF_VER_11;
Willy Tarreau5b154472009-12-21 20:11:07 +01006248
6249 /* "connection" has not been parsed yet */
Willy Tarreau50fc7772012-11-11 22:19:57 +01006250 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 +01006251
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006252 /* transfer length unknown*/
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006253 msg->flags &= ~HTTP_MSGF_XFER_LEN;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006254
Willy Tarreau9b28e032012-10-12 23:49:43 +02006255 txn->status = strl2ui(rep->buf->p + msg->sl.st.c, msg->sl.st.c_l);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006256
Willy Tarreau39650402010-03-15 19:44:39 +01006257 /* Adjust server's health based on status code. Note: status codes 501
6258 * and 505 are triggered on demand by client request, so we must not
6259 * count them as server failures.
6260 */
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006261 if (objt_server(s->target)) {
Willy Tarreaud45b3d52010-05-20 11:49:03 +02006262 if (txn->status >= 100 && (txn->status < 500 || txn->status == 501 || txn->status == 505))
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006263 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_OK);
Willy Tarreaud45b3d52010-05-20 11:49:03 +02006264 else
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006265 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_STS);
Willy Tarreaud45b3d52010-05-20 11:49:03 +02006266 }
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01006267
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006268 /*
6269 * 2: check for cacheability.
6270 */
6271
6272 switch (txn->status) {
Willy Tarreau628c40c2014-04-24 19:11:26 +02006273 case 100:
6274 /*
6275 * We may be facing a 100-continue response, in which case this
6276 * is not the right response, and we're waiting for the next one.
6277 * Let's allow this response to go to the client and wait for the
6278 * next one.
6279 */
6280 hdr_idx_init(&txn->hdr_idx);
6281 msg->next -= channel_forward(rep, msg->next);
6282 msg->msg_state = HTTP_MSG_RPBEFORE;
6283 txn->status = 0;
6284 s->logs.t_data = -1; /* was not a response yet */
Christopher Faulet3e344292015-11-24 16:24:13 +01006285 FLT_STRM_CB(s, flt_http_reset(s, msg));
Willy Tarreau628c40c2014-04-24 19:11:26 +02006286 goto next_one;
6287
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006288 case 200:
6289 case 203:
6290 case 206:
6291 case 300:
6292 case 301:
6293 case 410:
6294 /* RFC2616 @13.4:
6295 * "A response received with a status code of
6296 * 200, 203, 206, 300, 301 or 410 MAY be stored
6297 * by a cache (...) unless a cache-control
6298 * directive prohibits caching."
6299 *
6300 * RFC2616 @9.5: POST method :
6301 * "Responses to this method are not cacheable,
6302 * unless the response includes appropriate
6303 * Cache-Control or Expires header fields."
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006304 */
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006305 if (likely(txn->meth != HTTP_METH_POST) &&
Willy Tarreau67402132012-05-31 20:40:20 +02006306 ((s->be->options & PR_O_CHK_CACHE) || (s->be->ck_opts & PR_CK_NOC)))
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006307 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
6308 break;
6309 default:
6310 break;
6311 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006312
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006313 /*
6314 * 3: we may need to capture headers
6315 */
6316 s->logs.logwait &= ~LW_RESP;
Willy Tarreaucb7dd012015-04-03 22:16:32 +02006317 if (unlikely((s->logs.logwait & LW_RSPHDR) && s->res_cap))
Willy Tarreau9b28e032012-10-12 23:49:43 +02006318 capture_headers(rep->buf->p, &txn->hdr_idx,
Willy Tarreaucb7dd012015-04-03 22:16:32 +02006319 s->res_cap, sess->fe->rsp_cap);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006320
Willy Tarreau557f1992015-05-01 10:05:17 +02006321 /* 4: determine the transfer-length according to RFC2616 #4.4, updated
6322 * by RFC7230#3.3.3 :
6323 *
6324 * The length of a message body is determined by one of the following
6325 * (in order of precedence):
6326 *
Christopher Fauletd1cd2092016-11-28 10:14:03 +01006327 * 1. Any 2xx (Successful) response to a CONNECT request implies that
6328 * the connection will become a tunnel immediately after the empty
6329 * line that concludes the header fields. A client MUST ignore
6330 * any Content-Length or Transfer-Encoding header fields received
6331 * in such a message. Any 101 response (Switching Protocols) is
6332 * managed in the same manner.
6333 *
6334 * 2. Any response to a HEAD request and any response with a 1xx
Willy Tarreau557f1992015-05-01 10:05:17 +02006335 * (Informational), 204 (No Content), or 304 (Not Modified) status
6336 * code is always terminated by the first empty line after the
6337 * header fields, regardless of the header fields present in the
6338 * message, and thus cannot contain a message body.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006339 *
Willy Tarreau557f1992015-05-01 10:05:17 +02006340 * 3. If a Transfer-Encoding header field is present and the chunked
6341 * transfer coding (Section 4.1) is the final encoding, the message
6342 * body length is determined by reading and decoding the chunked
6343 * data until the transfer coding indicates the data is complete.
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006344 *
Willy Tarreau557f1992015-05-01 10:05:17 +02006345 * If a Transfer-Encoding header field is present in a response and
6346 * the chunked transfer coding is not the final encoding, the
6347 * message body length is determined by reading the connection until
6348 * it is closed by the server. If a Transfer-Encoding header field
6349 * is present in a request and the chunked transfer coding is not
6350 * the final encoding, the message body length cannot be determined
6351 * reliably; the server MUST respond with the 400 (Bad Request)
6352 * status code and then close the connection.
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006353 *
Willy Tarreau557f1992015-05-01 10:05:17 +02006354 * If a message is received with both a Transfer-Encoding and a
6355 * Content-Length header field, the Transfer-Encoding overrides the
6356 * Content-Length. Such a message might indicate an attempt to
6357 * perform request smuggling (Section 9.5) or response splitting
6358 * (Section 9.4) and ought to be handled as an error. A sender MUST
6359 * remove the received Content-Length field prior to forwarding such
6360 * a message downstream.
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006361 *
Willy Tarreau557f1992015-05-01 10:05:17 +02006362 * 4. If a message is received without Transfer-Encoding and with
6363 * either multiple Content-Length header fields having differing
6364 * field-values or a single Content-Length header field having an
6365 * invalid value, then the message framing is invalid and the
6366 * recipient MUST treat it as an unrecoverable error. If this is a
6367 * request message, the server MUST respond with a 400 (Bad Request)
6368 * status code and then close the connection. If this is a response
6369 * message received by a proxy, the proxy MUST close the connection
6370 * to the server, discard the received response, and send a 502 (Bad
6371 * Gateway) response to the client. If this is a response message
6372 * received by a user agent, the user agent MUST close the
6373 * connection to the server and discard the received response.
6374 *
6375 * 5. If a valid Content-Length header field is present without
6376 * Transfer-Encoding, its decimal value defines the expected message
6377 * body length in octets. If the sender closes the connection or
6378 * the recipient times out before the indicated number of octets are
6379 * received, the recipient MUST consider the message to be
6380 * incomplete and close the connection.
6381 *
6382 * 6. If this is a request message and none of the above are true, then
6383 * the message body length is zero (no message body is present).
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006384 *
Willy Tarreau557f1992015-05-01 10:05:17 +02006385 * 7. Otherwise, this is a response message without a declared message
6386 * body length, so the message body length is determined by the
6387 * number of octets received prior to the server closing the
6388 * connection.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006389 */
6390
6391 /* Skip parsing if no content length is possible. The response flags
Willy Tarreau124d9912011-03-01 20:30:48 +01006392 * remain 0 as well as the chunk_len, which may or may not mirror
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006393 * the real header value, and we note that we know the response's length.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006394 * FIXME: should we parse anyway and return an error on chunked encoding ?
6395 */
Christopher Fauletd1cd2092016-11-28 10:14:03 +01006396 if (unlikely((txn->meth == HTTP_METH_CONNECT && txn->status == 200) ||
6397 txn->status == 101)) {
6398 /* Either we've established an explicit tunnel, or we're
6399 * switching the protocol. In both cases, we're very unlikely
6400 * to understand the next protocols. We have to switch to tunnel
6401 * mode, so that we transfer the request and responses then let
6402 * this protocol pass unmodified. When we later implement specific
6403 * parsers for such protocols, we'll want to check the Upgrade
6404 * header which contains information about that protocol for
6405 * responses with status 101 (eg: see RFC2817 about TLS).
6406 */
6407 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_TUN;
6408 msg->flags |= HTTP_MSGF_XFER_LEN;
6409 goto end;
6410 }
6411
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006412 if (txn->meth == HTTP_METH_HEAD ||
6413 (txn->status >= 100 && txn->status < 200) ||
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006414 txn->status == 204 || txn->status == 304) {
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006415 msg->flags |= HTTP_MSGF_XFER_LEN;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006416 goto skip_content_length;
6417 }
6418
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006419 use_close_only = 0;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006420 ctx.idx = 0;
Willy Tarreau4979d5c2015-05-01 10:06:30 +02006421 while (http_find_header2("Transfer-Encoding", 17, rep->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006422 if (ctx.vlen == 7 && strncasecmp(ctx.line + ctx.val, "chunked", 7) == 0)
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006423 msg->flags |= (HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN);
6424 else if (msg->flags & HTTP_MSGF_TE_CHNK) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006425 /* bad transfer-encoding (chunked followed by something else) */
6426 use_close_only = 1;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006427 msg->flags &= ~(HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN);
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006428 break;
6429 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006430 }
6431
Willy Tarreau1c913912015-04-30 10:57:51 +02006432 /* Chunked responses must have their content-length removed */
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006433 ctx.idx = 0;
Willy Tarreaub4d0c032015-05-01 10:25:45 +02006434 if (use_close_only || (msg->flags & HTTP_MSGF_TE_CHNK)) {
Willy Tarreau1c913912015-04-30 10:57:51 +02006435 while (http_find_header2("Content-Length", 14, rep->buf->p, &txn->hdr_idx, &ctx))
6436 http_remove_header2(msg, &txn->hdr_idx, &ctx);
6437 }
Willy Tarreaub4d0c032015-05-01 10:25:45 +02006438 else while (http_find_header2("Content-Length", 14, rep->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006439 signed long long cl;
6440
Willy Tarreauad14f752011-09-02 20:33:27 +02006441 if (!ctx.vlen) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02006442 msg->err_pos = ctx.line + ctx.val - rep->buf->p;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006443 goto hdr_response_bad;
Willy Tarreauad14f752011-09-02 20:33:27 +02006444 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006445
Willy Tarreauad14f752011-09-02 20:33:27 +02006446 if (strl2llrc(ctx.line + ctx.val, ctx.vlen, &cl)) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02006447 msg->err_pos = ctx.line + ctx.val - rep->buf->p;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006448 goto hdr_response_bad; /* parse failure */
Willy Tarreauad14f752011-09-02 20:33:27 +02006449 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006450
Willy Tarreauad14f752011-09-02 20:33:27 +02006451 if (cl < 0) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02006452 msg->err_pos = ctx.line + ctx.val - rep->buf->p;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006453 goto hdr_response_bad;
Willy Tarreauad14f752011-09-02 20:33:27 +02006454 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006455
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006456 if ((msg->flags & HTTP_MSGF_CNT_LEN) && (msg->chunk_len != cl)) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02006457 msg->err_pos = ctx.line + ctx.val - rep->buf->p;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006458 goto hdr_response_bad; /* already specified, was different */
Willy Tarreauad14f752011-09-02 20:33:27 +02006459 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006460
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006461 msg->flags |= HTTP_MSGF_CNT_LEN | HTTP_MSGF_XFER_LEN;
Willy Tarreau124d9912011-03-01 20:30:48 +01006462 msg->body_len = msg->chunk_len = cl;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006463 }
6464
Christopher Fauletd1cd2092016-11-28 10:14:03 +01006465 skip_content_length:
Willy Tarreau5b154472009-12-21 20:11:07 +01006466 /* Now we have to check if we need to modify the Connection header.
6467 * This is more difficult on the response than it is on the request,
6468 * because we can have two different HTTP versions and we don't know
6469 * how the client will interprete a response. For instance, let's say
6470 * that the client sends a keep-alive request in HTTP/1.0 and gets an
6471 * HTTP/1.1 response without any header. Maybe it will bound itself to
6472 * HTTP/1.0 because it only knows about it, and will consider the lack
6473 * of header as a close, or maybe it knows HTTP/1.1 and can consider
6474 * the lack of header as a keep-alive. Thus we will use two flags
6475 * indicating how a request MAY be understood by the client. In case
6476 * of multiple possibilities, we'll fix the header to be explicit. If
6477 * ambiguous cases such as both close and keepalive are seen, then we
6478 * will fall back to explicit close. Note that we won't take risks with
6479 * HTTP/1.0 clients which may not necessarily understand keep-alive.
Willy Tarreau60466522010-01-18 19:08:45 +01006480 * See doc/internals/connection-header.txt for the complete matrix.
Willy Tarreau5b154472009-12-21 20:11:07 +01006481 */
Christopher Fauletd1cd2092016-11-28 10:14:03 +01006482 if ((txn->status >= 200) && !(txn->flags & TX_HDR_CONN_PRS) &&
6483 ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN ||
6484 ((sess->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
6485 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL))) {
Willy Tarreau60466522010-01-18 19:08:45 +01006486 int to_del = 0;
Willy Tarreau5b154472009-12-21 20:11:07 +01006487
Willy Tarreau70dffda2014-01-30 03:07:23 +01006488 /* this situation happens when combining pretend-keepalive with httpclose. */
6489 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006490 ((sess->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006491 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL))
Willy Tarreau70dffda2014-01-30 03:07:23 +01006492 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO;
6493
Willy Tarreau60466522010-01-18 19:08:45 +01006494 /* on unknown transfer length, we must close */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006495 if (!(msg->flags & HTTP_MSGF_XFER_LEN) &&
Willy Tarreau60466522010-01-18 19:08:45 +01006496 (txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN)
6497 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO;
Willy Tarreau5b154472009-12-21 20:11:07 +01006498
Willy Tarreau60466522010-01-18 19:08:45 +01006499 /* now adjust header transformations depending on current state */
6500 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN ||
6501 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) {
6502 to_del |= 2; /* remove "keep-alive" on any response */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006503 if (!(msg->flags & HTTP_MSGF_VER_11))
Willy Tarreau60466522010-01-18 19:08:45 +01006504 to_del |= 1; /* remove "close" for HTTP/1.0 responses */
Willy Tarreau5b154472009-12-21 20:11:07 +01006505 }
Willy Tarreau60466522010-01-18 19:08:45 +01006506 else { /* SCL / KAL */
6507 to_del |= 1; /* remove "close" on any response */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006508 if (txn->req.flags & msg->flags & HTTP_MSGF_VER_11)
Willy Tarreau60466522010-01-18 19:08:45 +01006509 to_del |= 2; /* remove "keep-alive" on pure 1.1 responses */
Willy Tarreau5b154472009-12-21 20:11:07 +01006510 }
Willy Tarreau5b154472009-12-21 20:11:07 +01006511
Willy Tarreau60466522010-01-18 19:08:45 +01006512 /* Parse and remove some headers from the connection header */
Willy Tarreau6acf7c92012-03-09 13:30:45 +01006513 http_parse_connection_header(txn, msg, to_del);
Willy Tarreau5b154472009-12-21 20:11:07 +01006514
Willy Tarreau60466522010-01-18 19:08:45 +01006515 /* Some keep-alive responses are converted to Server-close if
6516 * the server wants to close.
Willy Tarreau5b154472009-12-21 20:11:07 +01006517 */
Willy Tarreau60466522010-01-18 19:08:45 +01006518 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL) {
6519 if ((txn->flags & TX_HDR_CONN_CLO) ||
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006520 (!(txn->flags & TX_HDR_CONN_KAL) && !(msg->flags & HTTP_MSGF_VER_11)))
Willy Tarreau60466522010-01-18 19:08:45 +01006521 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_SCL;
Willy Tarreaub608feb2010-01-02 22:47:18 +01006522 }
Willy Tarreau5b154472009-12-21 20:11:07 +01006523 }
6524
Christopher Fauletd1cd2092016-11-28 10:14:03 +01006525 end:
Willy Tarreau7959a552013-09-23 16:44:27 +02006526 /* we want to have the response time before we start processing it */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006527 s->logs.t_data = tv_ms_elapsed(&s->logs.tv_accept, &now);
Willy Tarreau7959a552013-09-23 16:44:27 +02006528
Willy Tarreauf118d9f2014-04-24 18:26:08 +02006529 /* end of job, return OK */
6530 rep->analysers &= ~an_bit;
6531 rep->analyse_exp = TICK_ETERNITY;
6532 channel_auto_close(rep);
6533 return 1;
6534
6535 abort_keep_alive:
6536 /* A keep-alive request to the server failed on a network error.
6537 * The client is required to retry. We need to close without returning
6538 * any other information so that the client retries.
6539 */
6540 txn->status = 0;
Christopher Faulet0184ea72017-01-05 14:06:34 +01006541 rep->analysers &= AN_RES_FLT_END;
6542 s->req.analysers &= AN_REQ_FLT_END;
Willy Tarreauf118d9f2014-04-24 18:26:08 +02006543 channel_auto_close(rep);
6544 s->logs.logwait = 0;
6545 s->logs.level = 0;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006546 s->res.flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */
Willy Tarreau319f7452015-01-14 20:32:59 +01006547 channel_truncate(rep);
Christopher Fauleta94e5a52015-12-09 15:55:06 +01006548 http_reply_and_close(s, txn->status, NULL);
Willy Tarreauf118d9f2014-04-24 18:26:08 +02006549 return 0;
6550}
6551
6552/* This function performs all the processing enabled for the current response.
6553 * It normally returns 1 unless it wants to break. It relies on buffers flags,
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006554 * and updates s->res.analysers. It might make sense to explode it into several
Willy Tarreauf118d9f2014-04-24 18:26:08 +02006555 * other functions. It works like process_request (see indications above).
6556 */
Willy Tarreau87b09662015-04-03 00:22:06 +02006557int http_process_res_common(struct stream *s, struct channel *rep, int an_bit, struct proxy *px)
Willy Tarreauf118d9f2014-04-24 18:26:08 +02006558{
Willy Tarreaufb0afa72015-04-03 14:46:27 +02006559 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02006560 struct http_txn *txn = s->txn;
Willy Tarreauf118d9f2014-04-24 18:26:08 +02006561 struct http_msg *msg = &txn->rsp;
6562 struct proxy *cur_proxy;
6563 struct cond_wordlist *wl;
Thierry FOURNIER9e2ef992015-02-25 13:51:19 +01006564 enum rule_result ret = HTTP_RULE_RES_CONT;
Willy Tarreauf118d9f2014-04-24 18:26:08 +02006565
Willy Tarreau87b09662015-04-03 00:22:06 +02006566 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 +02006567 now_ms, __FUNCTION__,
6568 s,
6569 rep,
6570 rep->rex, rep->wex,
6571 rep->flags,
6572 rep->buf->i,
6573 rep->analysers);
6574
6575 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) /* we need more data */
6576 return 0;
6577
Willy Tarreau70730dd2014-04-24 18:06:27 +02006578 /* The stats applet needs to adjust the Connection header but we don't
6579 * apply any filter there.
6580 */
Willy Tarreau612adb82015-03-10 15:25:54 +01006581 if (unlikely(objt_applet(s->target) == &http_stats_applet)) {
6582 rep->analysers &= ~an_bit;
6583 rep->analyse_exp = TICK_ETERNITY;
Willy Tarreau70730dd2014-04-24 18:06:27 +02006584 goto skip_filters;
Willy Tarreau612adb82015-03-10 15:25:54 +01006585 }
Willy Tarreau70730dd2014-04-24 18:06:27 +02006586
Willy Tarreau58975672014-04-24 21:13:57 +02006587 /*
6588 * We will have to evaluate the filters.
6589 * As opposed to version 1.2, now they will be evaluated in the
6590 * filters order and not in the header order. This means that
6591 * each filter has to be validated among all headers.
6592 *
6593 * Filters are tried with ->be first, then with ->fe if it is
6594 * different from ->be.
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01006595 *
6596 * Maybe we are in resume condiion. In this case I choose the
6597 * "struct proxy" which contains the rule list matching the resume
6598 * pointer. If none of theses "struct proxy" match, I initialise
6599 * the process with the first one.
6600 *
6601 * In fact, I check only correspondance betwwen the current list
6602 * pointer and the ->fe rule list. If it doesn't match, I initialize
6603 * the loop with the ->be.
Willy Tarreau58975672014-04-24 21:13:57 +02006604 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006605 if (s->current_rule_list == &sess->fe->http_res_rules)
6606 cur_proxy = sess->fe;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01006607 else
6608 cur_proxy = s->be;
Willy Tarreau58975672014-04-24 21:13:57 +02006609 while (1) {
6610 struct proxy *rule_set = cur_proxy;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006611
Willy Tarreau58975672014-04-24 21:13:57 +02006612 /* evaluate http-response rules */
Willy Tarreau51d861a2015-05-22 17:30:48 +02006613 if (ret == HTTP_RULE_RES_CONT) {
Willy Tarreau987e3fb2015-04-04 01:09:08 +02006614 ret = http_res_get_intercept_rule(cur_proxy, &cur_proxy->http_res_rules, s);
Willy Tarreaue365c0b2013-06-11 16:06:12 +02006615
Willy Tarreau51d861a2015-05-22 17:30:48 +02006616 if (ret == HTTP_RULE_RES_BADREQ)
6617 goto return_srv_prx_502;
6618
6619 if (ret == HTTP_RULE_RES_DONE) {
6620 rep->analysers &= ~an_bit;
6621 rep->analyse_exp = TICK_ETERNITY;
6622 return 1;
6623 }
6624 }
6625
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01006626 /* we need to be called again. */
6627 if (ret == HTTP_RULE_RES_YIELD) {
6628 channel_dont_close(rep);
6629 return 0;
6630 }
6631
Willy Tarreau58975672014-04-24 21:13:57 +02006632 /* try headers filters */
6633 if (rule_set->rsp_exp != NULL) {
6634 if (apply_filters_to_response(s, rep, rule_set) < 0) {
6635 return_bad_resp:
6636 if (objt_server(s->target)) {
6637 objt_server(s->target)->counters.failed_resp++;
6638 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_RSP);
Willy Tarreau21d2af32008-02-14 20:25:24 +01006639 }
Willy Tarreau58975672014-04-24 21:13:57 +02006640 s->be->be_counters.failed_resp++;
6641 return_srv_prx_502:
Christopher Faulet0184ea72017-01-05 14:06:34 +01006642 rep->analysers &= AN_RES_FLT_END;
Willy Tarreau58975672014-04-24 21:13:57 +02006643 txn->status = 502;
6644 s->logs.t_data = -1; /* was not a valid response */
Willy Tarreau350f4872014-11-28 14:42:25 +01006645 s->si[1].flags |= SI_FL_NOLINGER;
Willy Tarreau319f7452015-01-14 20:32:59 +01006646 channel_truncate(rep);
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02006647 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreaue7dff022015-04-03 01:14:29 +02006648 if (!(s->flags & SF_ERR_MASK))
6649 s->flags |= SF_ERR_PRXCOND;
6650 if (!(s->flags & SF_FINST_MASK))
6651 s->flags |= SF_FINST_H;
Willy Tarreau58975672014-04-24 21:13:57 +02006652 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006653 }
Willy Tarreau58975672014-04-24 21:13:57 +02006654 }
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02006655
Willy Tarreau58975672014-04-24 21:13:57 +02006656 /* has the response been denied ? */
6657 if (txn->flags & TX_SVDENY) {
6658 if (objt_server(s->target))
6659 objt_server(s->target)->counters.failed_secu++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02006660
Willy Tarreau58975672014-04-24 21:13:57 +02006661 s->be->be_counters.denied_resp++;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006662 sess->fe->fe_counters.denied_resp++;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02006663 if (sess->listener->counters)
6664 sess->listener->counters->denied_resp++;
Willy Tarreaubaaee002006-06-26 02:48:02 +02006665
Willy Tarreau58975672014-04-24 21:13:57 +02006666 goto return_srv_prx_502;
6667 }
Willy Tarreau0bbc3cf2006-10-15 14:26:02 +02006668
Willy Tarreau58975672014-04-24 21:13:57 +02006669 /* add response headers from the rule sets in the same order */
6670 list_for_each_entry(wl, &rule_set->rsp_add, list) {
Willy Tarreauce730de2014-09-16 10:40:38 +02006671 if (txn->status < 200 && txn->status != 101)
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006672 break;
Willy Tarreau58975672014-04-24 21:13:57 +02006673 if (wl->cond) {
Willy Tarreau192252e2015-04-04 01:47:55 +02006674 int ret = acl_exec_cond(wl->cond, px, sess, s, SMP_OPT_DIR_RES|SMP_OPT_FINAL);
Willy Tarreau58975672014-04-24 21:13:57 +02006675 ret = acl_pass(ret);
6676 if (((struct acl_cond *)wl->cond)->pol == ACL_COND_UNLESS)
6677 ret = !ret;
6678 if (!ret)
6679 continue;
6680 }
6681 if (unlikely(http_header_add_tail(&txn->rsp, &txn->hdr_idx, wl->s) < 0))
6682 goto return_bad_resp;
Willy Tarreaubaaee002006-06-26 02:48:02 +02006683 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02006684
Willy Tarreau58975672014-04-24 21:13:57 +02006685 /* check whether we're already working on the frontend */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006686 if (cur_proxy == sess->fe)
Willy Tarreau58975672014-04-24 21:13:57 +02006687 break;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006688 cur_proxy = sess->fe;
Willy Tarreau58975672014-04-24 21:13:57 +02006689 }
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01006690
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01006691 /* After this point, this anayzer can't return yield, so we can
6692 * remove the bit corresponding to this analyzer from the list.
6693 *
6694 * Note that the intermediate returns and goto found previously
6695 * reset the analyzers.
6696 */
6697 rep->analysers &= ~an_bit;
6698 rep->analyse_exp = TICK_ETERNITY;
6699
Willy Tarreau58975672014-04-24 21:13:57 +02006700 /* OK that's all we can do for 1xx responses */
Willy Tarreauce730de2014-09-16 10:40:38 +02006701 if (unlikely(txn->status < 200 && txn->status != 101))
Willy Tarreau58975672014-04-24 21:13:57 +02006702 goto skip_header_mangling;
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01006703
Willy Tarreau58975672014-04-24 21:13:57 +02006704 /*
6705 * Now check for a server cookie.
6706 */
Willy Tarreau53a09d52015-08-10 18:59:40 +02006707 if (s->be->cookie_name || sess->fe->capture_name || (s->be->options & PR_O_CHK_CACHE))
Willy Tarreau58975672014-04-24 21:13:57 +02006708 manage_server_side_cookies(s, rep);
Willy Tarreaubaaee002006-06-26 02:48:02 +02006709
Willy Tarreau58975672014-04-24 21:13:57 +02006710 /*
6711 * Check for cache-control or pragma headers if required.
6712 */
Willy Tarreauce730de2014-09-16 10:40:38 +02006713 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 +02006714 check_response_for_cacheability(s, rep);
Willy Tarreaubaaee002006-06-26 02:48:02 +02006715
Willy Tarreau58975672014-04-24 21:13:57 +02006716 /*
6717 * Add server cookie in the response if needed
6718 */
6719 if (objt_server(s->target) && (s->be->ck_opts & PR_CK_INS) &&
6720 !((txn->flags & TX_SCK_FOUND) && (s->be->ck_opts & PR_CK_PSV)) &&
Willy Tarreaue7dff022015-04-03 01:14:29 +02006721 (!(s->flags & SF_DIRECT) ||
Willy Tarreau58975672014-04-24 21:13:57 +02006722 ((s->be->cookie_maxidle || txn->cookie_last_date) &&
6723 (!txn->cookie_last_date || (txn->cookie_last_date - date.tv_sec) < 0)) ||
6724 (s->be->cookie_maxlife && !txn->cookie_first_date) || // set the first_date
6725 (!s->be->cookie_maxlife && txn->cookie_first_date)) && // remove the first_date
6726 (!(s->be->ck_opts & PR_CK_POST) || (txn->meth == HTTP_METH_POST)) &&
Willy Tarreaue7dff022015-04-03 01:14:29 +02006727 !(s->flags & SF_IGNORE_PRST)) {
Willy Tarreau58975672014-04-24 21:13:57 +02006728 /* the server is known, it's not the one the client requested, or the
6729 * cookie's last seen date needs to be refreshed. We have to
6730 * insert a set-cookie here, except if we want to insert only on POST
6731 * requests and this one isn't. Note that servers which don't have cookies
6732 * (eg: some backup servers) will return a full cookie removal request.
Willy Tarreaua15645d2007-03-18 16:22:39 +01006733 */
Willy Tarreau58975672014-04-24 21:13:57 +02006734 if (!objt_server(s->target)->cookie) {
6735 chunk_printf(&trash,
6736 "Set-Cookie: %s=; Expires=Thu, 01-Jan-1970 00:00:01 GMT; path=/",
6737 s->be->cookie_name);
6738 }
6739 else {
6740 chunk_printf(&trash, "Set-Cookie: %s=%s", s->be->cookie_name, objt_server(s->target)->cookie);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006741
Willy Tarreau58975672014-04-24 21:13:57 +02006742 if (s->be->cookie_maxidle || s->be->cookie_maxlife) {
6743 /* emit last_date, which is mandatory */
6744 trash.str[trash.len++] = COOKIE_DELIM_DATE;
6745 s30tob64((date.tv_sec+3) >> 2, trash.str + trash.len);
6746 trash.len += 5;
Willy Tarreauef4f3912010-10-07 21:00:29 +02006747
Willy Tarreau58975672014-04-24 21:13:57 +02006748 if (s->be->cookie_maxlife) {
6749 /* emit first_date, which is either the original one or
6750 * the current date.
6751 */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006752 trash.str[trash.len++] = COOKIE_DELIM_DATE;
Willy Tarreau58975672014-04-24 21:13:57 +02006753 s30tob64(txn->cookie_first_date ?
6754 txn->cookie_first_date >> 2 :
6755 (date.tv_sec+3) >> 2, trash.str + trash.len);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006756 trash.len += 5;
Willy Tarreauef4f3912010-10-07 21:00:29 +02006757 }
Willy Tarreauef4f3912010-10-07 21:00:29 +02006758 }
Willy Tarreau58975672014-04-24 21:13:57 +02006759 chunk_appendf(&trash, "; path=/");
6760 }
Willy Tarreau4992dd22012-05-31 21:02:17 +02006761
Willy Tarreau58975672014-04-24 21:13:57 +02006762 if (s->be->cookie_domain)
6763 chunk_appendf(&trash, "; domain=%s", s->be->cookie_domain);
Willy Tarreauef4f3912010-10-07 21:00:29 +02006764
Willy Tarreau58975672014-04-24 21:13:57 +02006765 if (s->be->ck_opts & PR_CK_HTTPONLY)
6766 chunk_appendf(&trash, "; HttpOnly");
Willy Tarreaubaaee002006-06-26 02:48:02 +02006767
Willy Tarreau58975672014-04-24 21:13:57 +02006768 if (s->be->ck_opts & PR_CK_SECURE)
6769 chunk_appendf(&trash, "; Secure");
Willy Tarreaubaaee002006-06-26 02:48:02 +02006770
Willy Tarreau58975672014-04-24 21:13:57 +02006771 if (unlikely(http_header_add_tail2(&txn->rsp, &txn->hdr_idx, trash.str, trash.len) < 0))
6772 goto return_bad_resp;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006773
Willy Tarreau58975672014-04-24 21:13:57 +02006774 txn->flags &= ~TX_SCK_MASK;
Willy Tarreaue7dff022015-04-03 01:14:29 +02006775 if (objt_server(s->target)->cookie && (s->flags & SF_DIRECT))
Willy Tarreau58975672014-04-24 21:13:57 +02006776 /* the server did not change, only the date was updated */
6777 txn->flags |= TX_SCK_UPDATED;
6778 else
6779 txn->flags |= TX_SCK_INSERTED;
Willy Tarreaubaaee002006-06-26 02:48:02 +02006780
Willy Tarreau58975672014-04-24 21:13:57 +02006781 /* Here, we will tell an eventual cache on the client side that we don't
6782 * want it to cache this reply because HTTP/1.0 caches also cache cookies !
6783 * Some caches understand the correct form: 'no-cache="set-cookie"', but
6784 * others don't (eg: apache <= 1.3.26). So we use 'private' instead.
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006785 */
Willy Tarreau58975672014-04-24 21:13:57 +02006786 if ((s->be->ck_opts & PR_CK_NOC) && (txn->flags & TX_CACHEABLE)) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006787
Willy Tarreau58975672014-04-24 21:13:57 +02006788 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02006789
Willy Tarreau58975672014-04-24 21:13:57 +02006790 if (unlikely(http_header_add_tail2(&txn->rsp, &txn->hdr_idx,
6791 "Cache-control: private", 22) < 0))
6792 goto return_bad_resp;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006793 }
Willy Tarreau58975672014-04-24 21:13:57 +02006794 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01006795
Willy Tarreau58975672014-04-24 21:13:57 +02006796 /*
6797 * Check if result will be cacheable with a cookie.
6798 * We'll block the response if security checks have caught
6799 * nasty things such as a cacheable cookie.
6800 */
6801 if (((txn->flags & (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) ==
6802 (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) &&
6803 (s->be->options & PR_O_CHK_CACHE)) {
6804 /* we're in presence of a cacheable response containing
6805 * a set-cookie header. We'll block it as requested by
6806 * the 'checkcache' option, and send an alert.
Willy Tarreaua15645d2007-03-18 16:22:39 +01006807 */
Willy Tarreau58975672014-04-24 21:13:57 +02006808 if (objt_server(s->target))
6809 objt_server(s->target)->counters.failed_secu++;
Willy Tarreau60466522010-01-18 19:08:45 +01006810
Willy Tarreau58975672014-04-24 21:13:57 +02006811 s->be->be_counters.denied_resp++;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006812 sess->fe->fe_counters.denied_resp++;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02006813 if (sess->listener->counters)
6814 sess->listener->counters->denied_resp++;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006815
Willy Tarreau58975672014-04-24 21:13:57 +02006816 Alert("Blocking cacheable cookie in response from instance %s, server %s.\n",
6817 s->be->id, objt_server(s->target) ? objt_server(s->target)->id : "<dispatch>");
6818 send_log(s->be, LOG_ALERT,
6819 "Blocking cacheable cookie in response from instance %s, server %s.\n",
6820 s->be->id, objt_server(s->target) ? objt_server(s->target)->id : "<dispatch>");
6821 goto return_srv_prx_502;
6822 }
Willy Tarreau03945942009-12-22 16:50:27 +01006823
Willy Tarreau70730dd2014-04-24 18:06:27 +02006824 skip_filters:
Willy Tarreau58975672014-04-24 21:13:57 +02006825 /*
6826 * Adjust "Connection: close" or "Connection: keep-alive" if needed.
6827 * If an "Upgrade" token is found, the header is left untouched in order
6828 * not to have to deal with some client bugs : some of them fail an upgrade
Willy Tarreauce730de2014-09-16 10:40:38 +02006829 * if anything but "Upgrade" is present in the Connection header. We don't
6830 * want to touch any 101 response either since it's switching to another
6831 * protocol.
Willy Tarreau58975672014-04-24 21:13:57 +02006832 */
Willy Tarreauce730de2014-09-16 10:40:38 +02006833 if ((txn->status != 101) && !(txn->flags & TX_HDR_CONN_UPG) &&
Willy Tarreau58975672014-04-24 21:13:57 +02006834 (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) ||
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006835 ((sess->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
Willy Tarreau58975672014-04-24 21:13:57 +02006836 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL))) {
6837 unsigned int want_flags = 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02006838
Willy Tarreau58975672014-04-24 21:13:57 +02006839 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
6840 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
6841 /* we want a keep-alive response here. Keep-alive header
6842 * required if either side is not 1.1.
6843 */
6844 if (!(txn->req.flags & msg->flags & HTTP_MSGF_VER_11))
6845 want_flags |= TX_CON_KAL_SET;
6846 }
6847 else {
6848 /* we want a close response here. Close header required if
6849 * the server is 1.1, regardless of the client.
6850 */
6851 if (msg->flags & HTTP_MSGF_VER_11)
6852 want_flags |= TX_CON_CLO_SET;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006853 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01006854
Willy Tarreau58975672014-04-24 21:13:57 +02006855 if (want_flags != (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET)))
6856 http_change_connection_header(txn, msg, want_flags);
6857 }
6858
6859 skip_header_mangling:
Christopher Fauletf1cc5d02017-02-08 09:45:13 +01006860 if ((msg->flags & HTTP_MSGF_XFER_LEN) || HAS_DATA_FILTERS(s, rep) ||
Christopher Fauletd7c91962015-04-30 11:48:27 +02006861 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN) {
Christopher Faulet0184ea72017-01-05 14:06:34 +01006862 rep->analysers &= ~AN_RES_FLT_XFER_DATA;
Willy Tarreau58975672014-04-24 21:13:57 +02006863 rep->analysers |= AN_RES_HTTP_XFER_BODY;
Christopher Fauletd7c91962015-04-30 11:48:27 +02006864 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01006865
Willy Tarreau58975672014-04-24 21:13:57 +02006866 /* if the user wants to log as soon as possible, without counting
6867 * bytes from the server, then this is the right moment. We have
6868 * to temporarily assign bytes_out to log what we currently have.
6869 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006870 if (!LIST_ISEMPTY(&sess->fe->logformat) && !(s->logs.logwait & LW_BYTES)) {
Willy Tarreau58975672014-04-24 21:13:57 +02006871 s->logs.t_close = s->logs.t_data; /* to get a valid end date */
6872 s->logs.bytes_out = txn->rsp.eoh;
6873 s->do_log(s);
6874 s->logs.bytes_out = 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006875 }
Willy Tarreaue3fa6e52010-01-04 22:57:43 +01006876 return 1;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006877}
Willy Tarreaua15645d2007-03-18 16:22:39 +01006878
Willy Tarreaud98cf932009-12-27 22:54:55 +01006879/* This function is an analyser which forwards response body (including chunk
6880 * sizes if any). It is called as soon as we must forward, even if we forward
6881 * zero byte. The only situation where it must not be called is when we're in
6882 * tunnel mode and we want to forward till the close. It's used both to forward
6883 * remaining data and to resync after end of body. It expects the msg_state to
6884 * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to
Willy Tarreau87b09662015-04-03 00:22:06 +02006885 * read more data, or 1 once we can go on with next request or end the stream.
Willy Tarreaud3510212014-04-21 11:24:13 +02006886 *
6887 * It is capable of compressing response data both in content-length mode and
6888 * in chunked mode. The state machines follows different flows depending on
6889 * whether content-length and chunked modes are used, since there are no
6890 * trailers in content-length :
6891 *
6892 * chk-mode cl-mode
6893 * ,----- BODY -----.
6894 * / \
6895 * V size > 0 V chk-mode
6896 * .--> SIZE -------------> DATA -------------> CRLF
6897 * | | size == 0 | last byte |
6898 * | v final crlf v inspected |
6899 * | TRAILERS -----------> DONE |
6900 * | |
6901 * `----------------------------------------------'
6902 *
6903 * Compression only happens in the DATA state, and must be flushed in final
6904 * states (TRAILERS/DONE) or when leaving on missing data. Normal forwarding
6905 * is performed at once on final states for all bytes parsed, or when leaving
6906 * on missing data.
Willy Tarreaud98cf932009-12-27 22:54:55 +01006907 */
Willy Tarreau87b09662015-04-03 00:22:06 +02006908int http_response_forward_body(struct stream *s, struct channel *res, int an_bit)
Willy Tarreaud98cf932009-12-27 22:54:55 +01006909{
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006910 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02006911 struct http_txn *txn = s->txn;
6912 struct http_msg *msg = &s->txn->rsp;
Christopher Faulet3e344292015-11-24 16:24:13 +01006913 int ret;
Willy Tarreaud98cf932009-12-27 22:54:55 +01006914
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01006915 if (unlikely(msg->msg_state < HTTP_MSG_BODY))
6916 return 0;
6917
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02006918 if ((res->flags & (CF_READ_ERROR|CF_READ_TIMEOUT|CF_WRITE_ERROR|CF_WRITE_TIMEOUT)) ||
Willy Tarreau9b28e032012-10-12 23:49:43 +02006919 ((res->flags & CF_SHUTW) && (res->to_forward || res->buf->o)) ||
Christopher Fauletd7c91962015-04-30 11:48:27 +02006920 !s->req.analysers) {
Willy Tarreau4fe41902010-06-07 22:27:41 +02006921 /* Output closed while we were sending data. We must abort and
6922 * wake the other side up.
6923 */
Willy Tarreau10e61cb2017-01-04 14:51:22 +01006924 msg->err_state = msg->msg_state;
Willy Tarreau4fe41902010-06-07 22:27:41 +02006925 msg->msg_state = HTTP_MSG_ERROR;
6926 http_resync_states(s);
Willy Tarreau082b01c2010-01-02 23:58:04 +01006927 return 1;
6928 }
6929
Willy Tarreau4fe41902010-06-07 22:27:41 +02006930 /* in most states, we should abort in case of early close */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02006931 channel_auto_close(res);
Willy Tarreaub608feb2010-01-02 22:47:18 +01006932
Christopher Fauletd7c91962015-04-30 11:48:27 +02006933 if (msg->msg_state == HTTP_MSG_BODY) {
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006934 msg->msg_state = ((msg->flags & HTTP_MSGF_TE_CHNK)
6935 ? HTTP_MSG_CHUNK_SIZE
6936 : HTTP_MSG_DATA);
Willy Tarreaud98cf932009-12-27 22:54:55 +01006937 }
6938
Willy Tarreauefdf0942014-04-24 20:08:57 +02006939 if (res->to_forward) {
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006940 /* We can't process the buffer's contents yet */
Willy Tarreauefdf0942014-04-24 20:08:57 +02006941 res->flags |= CF_WAKE_WRITE;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006942 goto missing_data_or_waiting;
Willy Tarreauefdf0942014-04-24 20:08:57 +02006943 }
6944
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006945 if (msg->msg_state < HTTP_MSG_DONE) {
6946 ret = ((msg->flags & HTTP_MSGF_TE_CHNK)
6947 ? http_msg_forward_chunked_body(s, msg)
6948 : http_msg_forward_body(s, msg));
6949 if (!ret)
6950 goto missing_data_or_waiting;
6951 if (ret < 0)
6952 goto return_bad_res;
6953 }
Christopher Fauletd7c91962015-04-30 11:48:27 +02006954
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006955 /* other states, DONE...TUNNEL */
6956 /* for keep-alive we don't want to forward closes on DONE */
6957 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
6958 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL)
6959 channel_dont_close(res);
Willy Tarreau3ce10ff2014-04-22 08:24:38 +02006960
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006961 if (http_resync_states(s)) {
6962 /* some state changes occurred, maybe the analyser was disabled
6963 * too. */
6964 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
6965 if (res->flags & CF_SHUTW) {
6966 /* response errors are most likely due to the
6967 * client aborting the transfer. */
6968 goto aborted_xfer;
Willy Tarreau5523b322009-12-29 12:05:52 +01006969 }
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006970 if (msg->err_pos >= 0)
Willy Tarreau10e61cb2017-01-04 14:51:22 +01006971 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->err_state, strm_fe(s));
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006972 goto return_bad_res;
Willy Tarreaud98cf932009-12-27 22:54:55 +01006973 }
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006974 return 1;
Willy Tarreaud98cf932009-12-27 22:54:55 +01006975 }
Willy Tarreauf51d03c2016-05-02 15:25:15 +02006976 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01006977
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006978 missing_data_or_waiting:
Willy Tarreauf003d372012-11-26 13:35:37 +01006979 if (res->flags & CF_SHUTW)
6980 goto aborted_xfer;
6981
6982 /* stop waiting for data if the input is closed before the end. If the
6983 * client side was already closed, it means that the client has aborted,
6984 * so we don't want to count this as a server abort. Otherwise it's a
6985 * server abort.
6986 */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02006987 if (res->flags & CF_SHUTR) {
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006988 if ((s->req.flags & (CF_SHUTR|CF_SHUTW)) == (CF_SHUTR|CF_SHUTW))
Willy Tarreauf003d372012-11-26 13:35:37 +01006989 goto aborted_xfer;
Christopher Fauleta46bbd82015-06-19 09:00:58 +02006990 /* If we have some pending data, we continue the processing */
6991 if (!buffer_pending(res->buf)) {
6992 if (!(s->flags & SF_ERR_MASK))
6993 s->flags |= SF_ERR_SRVCL;
6994 s->be->be_counters.srv_aborts++;
6995 if (objt_server(s->target))
6996 objt_server(s->target)->counters.srv_aborts++;
6997 goto return_bad_res_stats_ok;
6998 }
Willy Tarreau40dba092010-03-04 18:14:51 +01006999 }
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01007000
Willy Tarreau40dba092010-03-04 18:14:51 +01007001 /* we need to obey the req analyser, so if it leaves, we must too */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01007002 if (!s->req.analysers)
Willy Tarreau610ecce2010-01-04 21:15:02 +01007003 goto return_bad_res;
7004
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01007005 /* When TE: chunked is used, we need to get there again to parse
7006 * remaining chunks even if the server has closed, so we don't want to
7007 * set CF_DONTCLOSE. Similarly, if the body length is undefined, if
7008 * keep-alive is set on the client side or if there are filters
7009 * registered on the stream, we don't want to forward a close
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02007010 */
Christopher Fauletf1cc5d02017-02-08 09:45:13 +01007011 if ((msg->flags & HTTP_MSGF_TE_CHNK) || !(msg->flags & HTTP_MSGF_XFER_LEN) ||
7012 HAS_DATA_FILTERS(s, res) ||
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02007013 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
7014 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL)
Willy Tarreau8263d2b2012-08-28 00:06:31 +02007015 channel_dont_close(res);
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02007016
Willy Tarreau5c620922011-05-11 19:56:11 +02007017 /* We know that more data are expected, but we couldn't send more that
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02007018 * what we did. So we always set the CF_EXPECT_MORE flag so that the
Willy Tarreau07293032011-05-30 18:29:28 +02007019 * system knows it must not set a PUSH on this first part. Interactive
Willy Tarreau869fc1e2012-03-05 08:29:20 +01007020 * modes are already handled by the stream sock layer. We must not do
7021 * this in content-length mode because it could present the MSG_MORE
7022 * flag with the last block of forwarded data, which would cause an
7023 * additional delay to be observed by the receiver.
Willy Tarreau5c620922011-05-11 19:56:11 +02007024 */
Christopher Faulet92d36382015-11-05 13:35:03 +01007025 if ((msg->flags & HTTP_MSGF_TE_CHNK) || (msg->flags & HTTP_MSGF_COMPRESSING))
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02007026 res->flags |= CF_EXPECT_MORE;
Willy Tarreau5c620922011-05-11 19:56:11 +02007027
Willy Tarreau87b09662015-04-03 00:22:06 +02007028 /* the stream handler will take care of timeouts and errors */
Willy Tarreaud98cf932009-12-27 22:54:55 +01007029 return 0;
7030
Willy Tarreau40dba092010-03-04 18:14:51 +01007031 return_bad_res: /* let's centralize all bad responses */
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01007032 s->be->be_counters.failed_resp++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01007033 if (objt_server(s->target))
7034 objt_server(s->target)->counters.failed_resp++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01007035
7036 return_bad_res_stats_ok:
Willy Tarreau10e61cb2017-01-04 14:51:22 +01007037 txn->rsp.err_state = txn->rsp.msg_state;
Willy Tarreaud98cf932009-12-27 22:54:55 +01007038 txn->rsp.msg_state = HTTP_MSG_ERROR;
Willy Tarreau148d0992010-01-10 10:21:21 +01007039 /* 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 Tarreau3fdb3662012-11-12 00:42:33 +01007043 if (objt_server(s->target))
7044 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_HDRRSP);
Willy Tarreaud98cf932009-12-27 22:54:55 +01007045
Willy Tarreaue7dff022015-04-03 01:14:29 +02007046 if (!(s->flags & SF_ERR_MASK))
7047 s->flags |= SF_ERR_PRXCOND;
7048 if (!(s->flags & SF_FINST_MASK))
7049 s->flags |= SF_FINST_D;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01007050 return 0;
7051
7052 aborted_xfer:
Willy Tarreau10e61cb2017-01-04 14:51:22 +01007053 txn->rsp.err_state = txn->rsp.msg_state;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01007054 txn->rsp.msg_state = HTTP_MSG_ERROR;
7055 /* don't send any error message as we're in the body */
Christopher Fauleta94e5a52015-12-09 15:55:06 +01007056 http_reply_and_close(s, txn->status, NULL);
Christopher Faulet0184ea72017-01-05 14:06:34 +01007057 res->analysers &= AN_RES_FLT_END;
7058 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 +01007059
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02007060 sess->fe->fe_counters.cli_aborts++;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01007061 s->be->be_counters.cli_aborts++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01007062 if (objt_server(s->target))
7063 objt_server(s->target)->counters.cli_aborts++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01007064
Willy Tarreaue7dff022015-04-03 01:14:29 +02007065 if (!(s->flags & SF_ERR_MASK))
7066 s->flags |= SF_ERR_CLICL;
7067 if (!(s->flags & SF_FINST_MASK))
7068 s->flags |= SF_FINST_D;
Willy Tarreaud98cf932009-12-27 22:54:55 +01007069 return 0;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01007070}
7071
7072
7073static inline int
7074http_msg_forward_body(struct stream *s, struct http_msg *msg)
7075{
7076 struct channel *chn = msg->chn;
7077 int ret;
7078
7079 /* Here we have the guarantee to be in HTTP_MSG_DATA or HTTP_MSG_ENDING state */
7080
7081 if (msg->msg_state == HTTP_MSG_ENDING)
7082 goto ending;
7083
7084 /* Neither content-length, nor transfer-encoding was found, so we must
7085 * read the body until the server connection is closed. In that case, we
7086 * eat data as they come. Of course, this happens for response only. */
7087 if (!(msg->flags & HTTP_MSGF_XFER_LEN)) {
7088 unsigned long long len = (chn->buf->i - msg->next);
7089 msg->chunk_len += len;
7090 msg->body_len += len;
7091 }
Christopher Fauletda02e172015-12-04 09:25:05 +01007092 ret = FLT_STRM_DATA_CB(s, chn, flt_http_data(s, msg),
7093 /* default_ret */ MIN(msg->chunk_len, chn->buf->i - msg->next),
7094 /* on_error */ goto error);
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01007095 msg->next += ret;
7096 msg->chunk_len -= ret;
7097 if (msg->chunk_len) {
7098 /* input empty or output full */
7099 if (chn->buf->i > msg->next)
7100 chn->flags |= CF_WAKE_WRITE;
7101 goto missing_data_or_waiting;
7102 }
7103
Christopher Fauletf1cc5d02017-02-08 09:45:13 +01007104 /* The server still sending data that should be filtered */
7105 if (!(msg->flags & HTTP_MSGF_XFER_LEN) && !(chn->flags & CF_SHUTR))
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01007106 goto missing_data_or_waiting;
Christopher Fauletf1cc5d02017-02-08 09:45:13 +01007107
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01007108 msg->msg_state = HTTP_MSG_ENDING;
7109
7110 ending:
7111 /* we may have some pending data starting at res->buf->p such as a last
7112 * chunk of data or trailers. */
Christopher Fauletda02e172015-12-04 09:25:05 +01007113 ret = FLT_STRM_DATA_CB(s, chn, flt_http_forward_data(s, msg, msg->next),
7114 /* default_ret */ msg->next,
7115 /* on_error */ goto error);
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01007116 b_adv(chn->buf, ret);
7117 msg->next -= ret;
Willy Tarreau9962f8f2016-06-28 11:52:08 +02007118 if (unlikely(!(chn->flags & CF_WROTE_DATA) || msg->sov > 0))
7119 msg->sov -= ret;
Christopher Fauleta9300a32016-06-28 15:54:44 +02007120 if (msg->next)
7121 goto waiting;
Willy Tarreau9962f8f2016-06-28 11:52:08 +02007122
Christopher Fauletda02e172015-12-04 09:25:05 +01007123 FLT_STRM_DATA_CB(s, chn, flt_http_end(s, msg),
7124 /* default_ret */ 1,
7125 /* on_error */ goto error,
7126 /* on_wait */ goto waiting);
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01007127 msg->msg_state = HTTP_MSG_DONE;
7128 return 1;
7129
7130 missing_data_or_waiting:
7131 /* we may have some pending data starting at chn->buf->p */
Christopher Fauletda02e172015-12-04 09:25:05 +01007132 ret = FLT_STRM_DATA_CB(s, chn, flt_http_forward_data(s, msg, msg->next),
7133 /* default_ret */ msg->next,
7134 /* on_error */ goto error);
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01007135 b_adv(chn->buf, ret);
7136 msg->next -= ret;
7137 if (!(chn->flags & CF_WROTE_DATA) || msg->sov > 0)
7138 msg->sov -= ret;
Christopher Faulet75e2eb62015-12-15 10:41:47 +01007139 if (!HAS_DATA_FILTERS(s, chn))
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01007140 msg->chunk_len -= channel_forward(chn, msg->chunk_len);
Christopher Fauleta9300a32016-06-28 15:54:44 +02007141 waiting:
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01007142 return 0;
7143 error:
7144 return -1;
7145}
7146
7147static inline int
7148http_msg_forward_chunked_body(struct stream *s, struct http_msg *msg)
7149{
7150 struct channel *chn = msg->chn;
7151 int ret;
7152
7153 /* Here we have the guarantee to be in one of the following state:
7154 * HTTP_MSG_DATA, HTTP_MSG_CHUNK_SIZE, HTTP_MSG_CHUNK_CRLF,
7155 * HTTP_MSG_TRAILERS or HTTP_MSG_ENDING. */
7156
7157 switch_states:
7158 switch (msg->msg_state) {
7159 case HTTP_MSG_DATA:
Christopher Fauletda02e172015-12-04 09:25:05 +01007160 ret = FLT_STRM_DATA_CB(s, chn, flt_http_data(s, msg),
7161 /* default_ret */ MIN(msg->chunk_len, chn->buf->i - msg->next),
7162 /* on_error */ goto error);
7163 msg->next += ret;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01007164 msg->chunk_len -= ret;
7165 if (msg->chunk_len) {
7166 /* input empty or output full */
7167 if (chn->buf->i > msg->next)
7168 chn->flags |= CF_WAKE_WRITE;
7169 goto missing_data_or_waiting;
7170 }
7171
7172 /* nothing left to forward for this chunk*/
7173 msg->msg_state = HTTP_MSG_CHUNK_CRLF;
7174 /* fall through for HTTP_MSG_CHUNK_CRLF */
7175
7176 case HTTP_MSG_CHUNK_CRLF:
7177 /* we want the CRLF after the data */
7178 ret = http_skip_chunk_crlf(msg);
7179 if (ret == 0)
7180 goto missing_data_or_waiting;
7181 if (ret < 0)
7182 goto chunk_parsing_error;
Christopher Faulet113f7de2015-12-14 14:52:13 +01007183 msg->next += ret;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01007184 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
7185 /* fall through for HTTP_MSG_CHUNK_SIZE */
7186
7187 case HTTP_MSG_CHUNK_SIZE:
7188 /* read the chunk size and assign it to ->chunk_len,
7189 * then set ->next to point to the body and switch to
7190 * DATA or TRAILERS state.
7191 */
7192 ret = http_parse_chunk_size(msg);
7193 if (ret == 0)
7194 goto missing_data_or_waiting;
7195 if (ret < 0)
7196 goto chunk_parsing_error;
Christopher Faulet113f7de2015-12-14 14:52:13 +01007197 msg->next += ret;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01007198 if (msg->chunk_len) {
7199 msg->msg_state = HTTP_MSG_DATA;
7200 goto switch_states;
7201 }
7202 msg->msg_state = HTTP_MSG_TRAILERS;
7203 /* fall through for HTTP_MSG_TRAILERS */
7204
7205 case HTTP_MSG_TRAILERS:
7206 ret = http_forward_trailers(msg);
7207 if (ret < 0)
7208 goto chunk_parsing_error;
Christopher Fauletda02e172015-12-04 09:25:05 +01007209 FLT_STRM_DATA_CB(s, chn, flt_http_chunk_trailers(s, msg),
7210 /* default_ret */ 1,
7211 /* on_error */ goto error);
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01007212 msg->next += msg->sol;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01007213 if (!ret)
7214 goto missing_data_or_waiting;
7215 break;
7216
7217 case HTTP_MSG_ENDING:
7218 goto ending;
7219
7220 default:
7221 /* This should no happen in this function */
7222 goto error;
7223 }
7224
7225 msg->msg_state = HTTP_MSG_ENDING;
7226 ending:
7227 /* we may have some pending data starting at res->buf->p such as a last
7228 * chunk of data or trailers. */
Christopher Fauletda02e172015-12-04 09:25:05 +01007229 ret = FLT_STRM_DATA_CB(s, chn, flt_http_forward_data(s, msg, msg->next),
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01007230 /* default_ret */ msg->next,
7231 /* on_error */ goto error);
7232 b_adv(chn->buf, ret);
7233 msg->next -= ret;
Willy Tarreau9962f8f2016-06-28 11:52:08 +02007234 if (unlikely(!(chn->flags & CF_WROTE_DATA) || msg->sov > 0))
7235 msg->sov -= ret;
Christopher Fauleta9300a32016-06-28 15:54:44 +02007236 if (msg->next)
7237 goto waiting;
Willy Tarreau9962f8f2016-06-28 11:52:08 +02007238
Christopher Fauletda02e172015-12-04 09:25:05 +01007239 FLT_STRM_DATA_CB(s, chn, flt_http_end(s, msg),
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01007240 /* default_ret */ 1,
7241 /* on_error */ goto error,
7242 /* on_wait */ goto waiting);
7243 msg->msg_state = HTTP_MSG_DONE;
7244 return 1;
7245
7246 missing_data_or_waiting:
7247 /* we may have some pending data starting at chn->buf->p */
Christopher Fauletda02e172015-12-04 09:25:05 +01007248 ret = FLT_STRM_DATA_CB(s, chn, flt_http_forward_data(s, msg, msg->next),
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01007249 /* default_ret */ msg->next,
7250 /* on_error */ goto error);
7251 b_adv(chn->buf, ret);
7252 msg->next -= ret;
7253 if (!(chn->flags & CF_WROTE_DATA) || msg->sov > 0)
7254 msg->sov -= ret;
Christopher Faulet75e2eb62015-12-15 10:41:47 +01007255 if (!HAS_DATA_FILTERS(s, chn))
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01007256 msg->chunk_len -= channel_forward(chn, msg->chunk_len);
Christopher Fauleta9300a32016-06-28 15:54:44 +02007257 waiting:
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01007258 return 0;
7259
7260 chunk_parsing_error:
7261 if (msg->err_pos >= 0) {
7262 if (chn->flags & CF_ISRESP)
7263 http_capture_bad_message(&s->be->invalid_rep, s, msg,
7264 msg->msg_state, strm_fe(s));
7265 else
7266 http_capture_bad_message(&strm_fe(s)->invalid_req, s,
7267 msg, msg->msg_state, s->be);
7268 }
7269 error:
7270 return -1;
Willy Tarreaud98cf932009-12-27 22:54:55 +01007271}
7272
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01007273
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007274/* Iterate the same filter through all request headers.
7275 * Returns 1 if this filter can be stopped upon return, otherwise 0.
Willy Tarreaua15645d2007-03-18 16:22:39 +01007276 * Since it can manage the switch to another backend, it updates the per-proxy
7277 * DENY stats.
Willy Tarreau58f10d72006-12-04 02:26:12 +01007278 */
Willy Tarreau87b09662015-04-03 00:22:06 +02007279int apply_filter_to_req_headers(struct stream *s, struct channel *req, struct hdr_exp *exp)
Willy Tarreau58f10d72006-12-04 02:26:12 +01007280{
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007281 char *cur_ptr, *cur_end, *cur_next;
7282 int cur_idx, old_idx, last_hdr;
Willy Tarreaueee5b512015-04-03 23:46:31 +02007283 struct http_txn *txn = s->txn;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007284 struct hdr_idx_elem *cur_hdr;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007285 int delta;
Willy Tarreau0f7562b2007-01-07 15:46:13 +01007286
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007287 last_hdr = 0;
7288
Willy Tarreau9b28e032012-10-12 23:49:43 +02007289 cur_next = req->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007290 old_idx = 0;
7291
7292 while (!last_hdr) {
Willy Tarreau3d300592007-03-18 18:34:41 +01007293 if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007294 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01007295 else if (unlikely(txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007296 (exp->action == ACT_ALLOW ||
7297 exp->action == ACT_DENY ||
7298 exp->action == ACT_TARPIT))
7299 return 0;
7300
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01007301 cur_idx = txn->hdr_idx.v[old_idx].next;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007302 if (!cur_idx)
7303 break;
7304
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01007305 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007306 cur_ptr = cur_next;
7307 cur_end = cur_ptr + cur_hdr->len;
7308 cur_next = cur_end + cur_hdr->cr + 1;
7309
7310 /* Now we have one header between cur_ptr and cur_end,
7311 * and the next header starts at cur_next.
Willy Tarreau58f10d72006-12-04 02:26:12 +01007312 */
7313
Willy Tarreau15a53a42015-01-21 13:39:42 +01007314 if (regex_exec_match2(exp->preg, cur_ptr, cur_end-cur_ptr, MAX_MATCH, pmatch, 0)) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007315 switch (exp->action) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007316 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01007317 txn->flags |= TX_CLALLOW;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007318 last_hdr = 1;
7319 break;
7320
7321 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01007322 txn->flags |= TX_CLDENY;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007323 last_hdr = 1;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007324 break;
7325
7326 case ACT_TARPIT:
Willy Tarreau3d300592007-03-18 18:34:41 +01007327 txn->flags |= TX_CLTARPIT;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007328 last_hdr = 1;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007329 break;
7330
7331 case ACT_REPLACE:
Sasha Pachevc6002042014-05-26 12:33:48 -06007332 trash.len = exp_replace(trash.str, trash.size, cur_ptr, exp->replace, pmatch);
7333 if (trash.len < 0)
7334 return -1;
7335
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007336 delta = buffer_replace2(req->buf, cur_ptr, cur_end, trash.str, trash.len);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007337 /* FIXME: if the user adds a newline in the replacement, the
7338 * index will not be recalculated for now, and the new line
7339 * will not be counted as a new header.
7340 */
7341
7342 cur_end += delta;
7343 cur_next += delta;
7344 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01007345 http_msg_move_end(&txn->req, delta);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007346 break;
7347
7348 case ACT_REMOVE:
Willy Tarreau9b28e032012-10-12 23:49:43 +02007349 delta = buffer_replace2(req->buf, cur_ptr, cur_next, NULL, 0);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007350 cur_next += delta;
7351
Willy Tarreaufa355d42009-11-29 18:12:29 +01007352 http_msg_move_end(&txn->req, delta);
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01007353 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
7354 txn->hdr_idx.used--;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007355 cur_hdr->len = 0;
7356 cur_end = NULL; /* null-term has been rewritten */
Willy Tarreau26db59e2010-11-28 06:57:24 +01007357 cur_idx = old_idx;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007358 break;
7359
7360 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007361 }
7362
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007363 /* keep the link from this header to next one in case of later
7364 * removal of next header.
Willy Tarreau58f10d72006-12-04 02:26:12 +01007365 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007366 old_idx = cur_idx;
7367 }
7368 return 0;
7369}
7370
7371
7372/* Apply the filter to the request line.
7373 * Returns 0 if nothing has been done, 1 if the filter has been applied,
7374 * or -1 if a replacement resulted in an invalid request line.
Willy Tarreaua15645d2007-03-18 16:22:39 +01007375 * Since it can manage the switch to another backend, it updates the per-proxy
7376 * DENY stats.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007377 */
Willy Tarreau87b09662015-04-03 00:22:06 +02007378int apply_filter_to_req_line(struct stream *s, struct channel *req, struct hdr_exp *exp)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007379{
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007380 char *cur_ptr, *cur_end;
7381 int done;
Willy Tarreaueee5b512015-04-03 23:46:31 +02007382 struct http_txn *txn = s->txn;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007383 int delta;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007384
Willy Tarreau3d300592007-03-18 18:34:41 +01007385 if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007386 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01007387 else if (unlikely(txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007388 (exp->action == ACT_ALLOW ||
7389 exp->action == ACT_DENY ||
7390 exp->action == ACT_TARPIT))
7391 return 0;
7392 else if (exp->action == ACT_REMOVE)
7393 return 0;
7394
7395 done = 0;
7396
Willy Tarreau9b28e032012-10-12 23:49:43 +02007397 cur_ptr = req->buf->p;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01007398 cur_end = cur_ptr + txn->req.sl.rq.l;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007399
7400 /* Now we have the request line between cur_ptr and cur_end */
7401
Willy Tarreau15a53a42015-01-21 13:39:42 +01007402 if (regex_exec_match2(exp->preg, cur_ptr, cur_end-cur_ptr, MAX_MATCH, pmatch, 0)) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007403 switch (exp->action) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007404 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01007405 txn->flags |= TX_CLALLOW;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007406 done = 1;
7407 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01007408
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007409 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01007410 txn->flags |= TX_CLDENY;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007411 done = 1;
7412 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01007413
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007414 case ACT_TARPIT:
Willy Tarreau3d300592007-03-18 18:34:41 +01007415 txn->flags |= TX_CLTARPIT;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007416 done = 1;
7417 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01007418
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007419 case ACT_REPLACE:
Sasha Pachevc6002042014-05-26 12:33:48 -06007420 trash.len = exp_replace(trash.str, trash.size, cur_ptr, exp->replace, pmatch);
7421 if (trash.len < 0)
7422 return -1;
7423
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007424 delta = buffer_replace2(req->buf, cur_ptr, cur_end, trash.str, trash.len);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007425 /* FIXME: if the user adds a newline in the replacement, the
7426 * index will not be recalculated for now, and the new line
7427 * will not be counted as a new header.
7428 */
Willy Tarreaua496b602006-12-17 23:15:24 +01007429
Willy Tarreaufa355d42009-11-29 18:12:29 +01007430 http_msg_move_end(&txn->req, delta);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007431 cur_end += delta;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02007432 cur_end = (char *)http_parse_reqline(&txn->req,
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007433 HTTP_MSG_RQMETH,
7434 cur_ptr, cur_end + 1,
7435 NULL, NULL);
7436 if (unlikely(!cur_end))
7437 return -1;
Willy Tarreaua496b602006-12-17 23:15:24 +01007438
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007439 /* we have a full request and we know that we have either a CR
7440 * or an LF at <ptr>.
7441 */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01007442 txn->meth = find_http_meth(cur_ptr, txn->req.sl.rq.m_l);
7443 hdr_idx_set_start(&txn->hdr_idx, txn->req.sl.rq.l, *cur_end == '\r');
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007444 /* there is no point trying this regex on headers */
7445 return 1;
7446 }
7447 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007448 return done;
7449}
Willy Tarreau97de6242006-12-27 17:18:38 +01007450
Willy Tarreau58f10d72006-12-04 02:26:12 +01007451
Willy Tarreau58f10d72006-12-04 02:26:12 +01007452
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007453/*
Willy Tarreau87b09662015-04-03 00:22:06 +02007454 * Apply all the req filters of proxy <px> to all headers in buffer <req> of stream <s>.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007455 * Returns 0 if everything is alright, or -1 in case a replacement lead to an
Willy Tarreaua15645d2007-03-18 16:22:39 +01007456 * unparsable request. Since it can manage the switch to another backend, it
7457 * updates the per-proxy DENY stats.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007458 */
Willy Tarreau87b09662015-04-03 00:22:06 +02007459int apply_filters_to_request(struct stream *s, struct channel *req, struct proxy *px)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007460{
Willy Tarreau192252e2015-04-04 01:47:55 +02007461 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02007462 struct http_txn *txn = s->txn;
Willy Tarreau6c123b12010-01-28 20:22:06 +01007463 struct hdr_exp *exp;
7464
7465 for (exp = px->req_exp; exp; exp = exp->next) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007466 int ret;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007467
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007468 /*
7469 * The interleaving of transformations and verdicts
7470 * makes it difficult to decide to continue or stop
7471 * the evaluation.
7472 */
7473
Willy Tarreau6c123b12010-01-28 20:22:06 +01007474 if (txn->flags & (TX_CLDENY|TX_CLTARPIT))
7475 break;
7476
Willy Tarreau3d300592007-03-18 18:34:41 +01007477 if ((txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007478 (exp->action == ACT_ALLOW || exp->action == ACT_DENY ||
Willy Tarreau6c123b12010-01-28 20:22:06 +01007479 exp->action == ACT_TARPIT || exp->action == ACT_PASS))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007480 continue;
Willy Tarreau6c123b12010-01-28 20:22:06 +01007481
7482 /* if this filter had a condition, evaluate it now and skip to
7483 * next filter if the condition does not match.
7484 */
7485 if (exp->cond) {
Willy Tarreau192252e2015-04-04 01:47:55 +02007486 ret = acl_exec_cond(exp->cond, px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreau6c123b12010-01-28 20:22:06 +01007487 ret = acl_pass(ret);
7488 if (((struct acl_cond *)exp->cond)->pol == ACL_COND_UNLESS)
7489 ret = !ret;
7490
7491 if (!ret)
7492 continue;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007493 }
7494
7495 /* Apply the filter to the request line. */
Willy Tarreau6c123b12010-01-28 20:22:06 +01007496 ret = apply_filter_to_req_line(s, req, exp);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007497 if (unlikely(ret < 0))
7498 return -1;
7499
7500 if (likely(ret == 0)) {
7501 /* The filter did not match the request, it can be
7502 * iterated through all headers.
7503 */
Willy Tarreau34d4c3c2015-01-30 20:58:58 +01007504 if (unlikely(apply_filter_to_req_headers(s, req, exp) < 0))
7505 return -1;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007506 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007507 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007508 return 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007509}
7510
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007511
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007512/* Find the end of a cookie value contained between <s> and <e>. It works the
7513 * same way as with headers above except that the semi-colon also ends a token.
7514 * See RFC2965 for more information. Note that it requires a valid header to
7515 * return a valid result.
7516 */
7517char *find_cookie_value_end(char *s, const char *e)
7518{
7519 int quoted, qdpair;
7520
7521 quoted = qdpair = 0;
7522 for (; s < e; s++) {
7523 if (qdpair) qdpair = 0;
7524 else if (quoted) {
7525 if (*s == '\\') qdpair = 1;
7526 else if (*s == '"') quoted = 0;
7527 }
7528 else if (*s == '"') quoted = 1;
7529 else if (*s == ',' || *s == ';') return s;
7530 }
7531 return s;
7532}
7533
7534/* Delete a value in a header between delimiters <from> and <next> in buffer
7535 * <buf>. The number of characters displaced is returned, and the pointer to
7536 * the first delimiter is updated if required. The function tries as much as
7537 * possible to respect the following principles :
7538 * - replace <from> delimiter by the <next> one unless <from> points to a
7539 * colon, in which case <next> is simply removed
7540 * - set exactly one space character after the new first delimiter, unless
7541 * there are not enough characters in the block being moved to do so.
7542 * - remove unneeded spaces before the previous delimiter and after the new
7543 * one.
7544 *
7545 * It is the caller's responsibility to ensure that :
7546 * - <from> points to a valid delimiter or the colon ;
7547 * - <next> points to a valid delimiter or the final CR/LF ;
7548 * - there are non-space chars before <from> ;
7549 * - there is a CR/LF at or after <next>.
7550 */
Willy Tarreauaf819352012-08-27 22:08:00 +02007551int del_hdr_value(struct buffer *buf, char **from, char *next)
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007552{
7553 char *prev = *from;
7554
7555 if (*prev == ':') {
7556 /* We're removing the first value, preserve the colon and add a
7557 * space if possible.
7558 */
Willy Tarreau2235b262016-11-05 15:50:20 +01007559 if (!HTTP_IS_CRLF(*next))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007560 next++;
7561 prev++;
7562 if (prev < next)
7563 *prev++ = ' ';
7564
Willy Tarreau2235b262016-11-05 15:50:20 +01007565 while (HTTP_IS_SPHT(*next))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007566 next++;
7567 } else {
7568 /* Remove useless spaces before the old delimiter. */
Willy Tarreau2235b262016-11-05 15:50:20 +01007569 while (HTTP_IS_SPHT(*(prev-1)))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007570 prev--;
7571 *from = prev;
7572
7573 /* copy the delimiter and if possible a space if we're
7574 * not at the end of the line.
7575 */
Willy Tarreau2235b262016-11-05 15:50:20 +01007576 if (!HTTP_IS_CRLF(*next)) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007577 *prev++ = *next++;
7578 if (prev + 1 < next)
7579 *prev++ = ' ';
Willy Tarreau2235b262016-11-05 15:50:20 +01007580 while (HTTP_IS_SPHT(*next))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007581 next++;
7582 }
7583 }
7584 return buffer_replace2(buf, prev, next, NULL, 0);
7585}
7586
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007587/*
Willy Tarreau396d2c62007-11-04 19:30:00 +01007588 * Manage client-side cookie. It can impact performance by about 2% so it is
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007589 * desirable to call it only when needed. This code is quite complex because
7590 * of the multiple very crappy and ambiguous syntaxes we have to support. it
7591 * highly recommended not to touch this part without a good reason !
Willy Tarreau58f10d72006-12-04 02:26:12 +01007592 */
Willy Tarreau87b09662015-04-03 00:22:06 +02007593void manage_client_side_cookies(struct stream *s, struct channel *req)
Willy Tarreau58f10d72006-12-04 02:26:12 +01007594{
Willy Tarreaueee5b512015-04-03 23:46:31 +02007595 struct http_txn *txn = s->txn;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02007596 struct session *sess = s->sess;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007597 int preserve_hdr;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007598 int cur_idx, old_idx;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007599 char *hdr_beg, *hdr_end, *hdr_next, *del_from;
7600 char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007601
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007602 /* Iterate through the headers, we start with the start line. */
Willy Tarreau83969f42007-01-22 08:55:47 +01007603 old_idx = 0;
Willy Tarreau9b28e032012-10-12 23:49:43 +02007604 hdr_next = req->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau58f10d72006-12-04 02:26:12 +01007605
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01007606 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01007607 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007608 int val;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007609
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01007610 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007611 hdr_beg = hdr_next;
7612 hdr_end = hdr_beg + cur_hdr->len;
7613 hdr_next = hdr_end + cur_hdr->cr + 1;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007614
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007615 /* We have one full header between hdr_beg and hdr_end, and the
7616 * next header starts at hdr_next. We're only interested in
Willy Tarreau58f10d72006-12-04 02:26:12 +01007617 * "Cookie:" headers.
7618 */
7619
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007620 val = http_header_match2(hdr_beg, hdr_end, "Cookie", 6);
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007621 if (!val) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01007622 old_idx = cur_idx;
7623 continue;
7624 }
7625
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007626 del_from = NULL; /* nothing to be deleted */
7627 preserve_hdr = 0; /* assume we may kill the whole header */
7628
Willy Tarreau58f10d72006-12-04 02:26:12 +01007629 /* Now look for cookies. Conforming to RFC2109, we have to support
7630 * attributes whose name begin with a '$', and associate them with
7631 * the right cookie, if we want to delete this cookie.
7632 * So there are 3 cases for each cookie read :
7633 * 1) it's a special attribute, beginning with a '$' : ignore it.
7634 * 2) it's a server id cookie that we *MAY* want to delete : save
7635 * some pointers on it (last semi-colon, beginning of cookie...)
7636 * 3) it's an application cookie : we *MAY* have to delete a previous
7637 * "special" cookie.
7638 * At the end of loop, if a "special" cookie remains, we may have to
7639 * remove it. If no application cookie persists in the header, we
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007640 * *MUST* delete it.
7641 *
7642 * Note: RFC2965 is unclear about the processing of spaces around
7643 * the equal sign in the ATTR=VALUE form. A careful inspection of
7644 * the RFC explicitly allows spaces before it, and not within the
7645 * tokens (attrs or values). An inspection of RFC2109 allows that
7646 * too but section 10.1.3 lets one think that spaces may be allowed
7647 * after the equal sign too, resulting in some (rare) buggy
7648 * implementations trying to do that. So let's do what servers do.
7649 * Latest ietf draft forbids spaces all around. Also, earlier RFCs
7650 * allowed quoted strings in values, with any possible character
7651 * after a backslash, including control chars and delimitors, which
7652 * causes parsing to become ambiguous. Browsers also allow spaces
7653 * within values even without quotes.
7654 *
7655 * We have to keep multiple pointers in order to support cookie
7656 * removal at the beginning, middle or end of header without
7657 * corrupting the header. All of these headers are valid :
7658 *
7659 * Cookie:NAME1=VALUE1;NAME2=VALUE2;NAME3=VALUE3\r\n
7660 * Cookie:NAME1=VALUE1;NAME2_ONLY ;NAME3=VALUE3\r\n
7661 * Cookie: NAME1 = VALUE 1 ; NAME2 = VALUE2 ; NAME3 = VALUE3\r\n
7662 * | | | | | | | | |
7663 * | | | | | | | | hdr_end <--+
7664 * | | | | | | | +--> next
7665 * | | | | | | +----> val_end
7666 * | | | | | +-----------> val_beg
7667 * | | | | +--------------> equal
7668 * | | | +----------------> att_end
7669 * | | +---------------------> att_beg
7670 * | +--------------------------> prev
7671 * +--------------------------------> hdr_beg
Willy Tarreau58f10d72006-12-04 02:26:12 +01007672 */
7673
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007674 for (prev = hdr_beg + 6; prev < hdr_end; prev = next) {
7675 /* Iterate through all cookies on this line */
7676
7677 /* find att_beg */
7678 att_beg = prev + 1;
Willy Tarreau2235b262016-11-05 15:50:20 +01007679 while (att_beg < hdr_end && HTTP_IS_SPHT(*att_beg))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007680 att_beg++;
7681
7682 /* find att_end : this is the first character after the last non
7683 * space before the equal. It may be equal to hdr_end.
7684 */
7685 equal = att_end = att_beg;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007686
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007687 while (equal < hdr_end) {
7688 if (*equal == '=' || *equal == ',' || *equal == ';')
Willy Tarreau58f10d72006-12-04 02:26:12 +01007689 break;
Willy Tarreau2235b262016-11-05 15:50:20 +01007690 if (HTTP_IS_SPHT(*equal++))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007691 continue;
7692 att_end = equal;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007693 }
7694
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007695 /* here, <equal> points to '=', a delimitor or the end. <att_end>
7696 * is between <att_beg> and <equal>, both may be identical.
7697 */
7698
7699 /* look for end of cookie if there is an equal sign */
7700 if (equal < hdr_end && *equal == '=') {
7701 /* look for the beginning of the value */
7702 val_beg = equal + 1;
Willy Tarreau2235b262016-11-05 15:50:20 +01007703 while (val_beg < hdr_end && HTTP_IS_SPHT(*val_beg))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007704 val_beg++;
7705
7706 /* find the end of the value, respecting quotes */
7707 next = find_cookie_value_end(val_beg, hdr_end);
7708
7709 /* make val_end point to the first white space or delimitor after the value */
7710 val_end = next;
Willy Tarreau2235b262016-11-05 15:50:20 +01007711 while (val_end > val_beg && HTTP_IS_SPHT(*(val_end - 1)))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007712 val_end--;
7713 } else {
7714 val_beg = val_end = next = equal;
Willy Tarreau305ae852010-01-03 19:45:54 +01007715 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007716
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007717 /* We have nothing to do with attributes beginning with '$'. However,
7718 * they will automatically be removed if a header before them is removed,
7719 * since they're supposed to be linked together.
7720 */
7721 if (*att_beg == '$')
7722 continue;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007723
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007724 /* Ignore cookies with no equal sign */
7725 if (equal == next) {
7726 /* This is not our cookie, so we must preserve it. But if we already
7727 * scheduled another cookie for removal, we cannot remove the
7728 * complete header, but we can remove the previous block itself.
7729 */
7730 preserve_hdr = 1;
7731 if (del_from != NULL) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007732 int delta = del_hdr_value(req->buf, &del_from, prev);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007733 val_end += delta;
7734 next += delta;
7735 hdr_end += delta;
7736 hdr_next += delta;
7737 cur_hdr->len += delta;
7738 http_msg_move_end(&txn->req, delta);
7739 prev = del_from;
7740 del_from = NULL;
7741 }
7742 continue;
Willy Tarreau305ae852010-01-03 19:45:54 +01007743 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007744
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007745 /* if there are spaces around the equal sign, we need to
7746 * strip them otherwise we'll get trouble for cookie captures,
7747 * or even for rewrites. Since this happens extremely rarely,
7748 * it does not hurt performance.
Willy Tarreau58f10d72006-12-04 02:26:12 +01007749 */
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007750 if (unlikely(att_end != equal || val_beg > equal + 1)) {
7751 int stripped_before = 0;
7752 int stripped_after = 0;
7753
7754 if (att_end != equal) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007755 stripped_before = buffer_replace2(req->buf, att_end, equal, NULL, 0);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007756 equal += stripped_before;
7757 val_beg += stripped_before;
7758 }
7759
7760 if (val_beg > equal + 1) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007761 stripped_after = buffer_replace2(req->buf, equal + 1, val_beg, NULL, 0);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007762 val_beg += stripped_after;
7763 stripped_before += stripped_after;
7764 }
7765
7766 val_end += stripped_before;
7767 next += stripped_before;
7768 hdr_end += stripped_before;
7769 hdr_next += stripped_before;
7770 cur_hdr->len += stripped_before;
7771 http_msg_move_end(&txn->req, stripped_before);
Willy Tarreau58f10d72006-12-04 02:26:12 +01007772 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007773 /* now everything is as on the diagram above */
Willy Tarreau58f10d72006-12-04 02:26:12 +01007774
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007775 /* First, let's see if we want to capture this cookie. We check
7776 * that we don't already have a client side cookie, because we
7777 * can only capture one. Also as an optimisation, we ignore
7778 * cookies shorter than the declared name.
7779 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02007780 if (sess->fe->capture_name != NULL && txn->cli_cookie == NULL &&
7781 (val_end - att_beg >= sess->fe->capture_namelen) &&
7782 memcmp(att_beg, sess->fe->capture_name, sess->fe->capture_namelen) == 0) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007783 int log_len = val_end - att_beg;
7784
7785 if ((txn->cli_cookie = pool_alloc2(pool2_capture)) == NULL) {
7786 Alert("HTTP logging : out of memory.\n");
7787 } else {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02007788 if (log_len > sess->fe->capture_len)
7789 log_len = sess->fe->capture_len;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007790 memcpy(txn->cli_cookie, att_beg, log_len);
7791 txn->cli_cookie[log_len] = 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007792 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007793 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007794
Willy Tarreaubca99692010-10-06 19:25:55 +02007795 /* Persistence cookies in passive, rewrite or insert mode have the
7796 * following form :
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007797 *
Willy Tarreaubca99692010-10-06 19:25:55 +02007798 * Cookie: NAME=SRV[|<lastseen>[|<firstseen>]]
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007799 *
Willy Tarreaubca99692010-10-06 19:25:55 +02007800 * For cookies in prefix mode, the form is :
7801 *
7802 * Cookie: NAME=SRV~VALUE
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007803 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007804 if ((att_end - att_beg == s->be->cookie_len) && (s->be->cookie_name != NULL) &&
7805 (memcmp(att_beg, s->be->cookie_name, att_end - att_beg) == 0)) {
7806 struct server *srv = s->be->srv;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007807 char *delim;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007808
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007809 /* if we're in cookie prefix mode, we'll search the delimitor so that we
7810 * have the server ID between val_beg and delim, and the original cookie between
7811 * delim+1 and val_end. Otherwise, delim==val_end :
7812 *
7813 * Cookie: NAME=SRV; # in all but prefix modes
7814 * Cookie: NAME=SRV~OPAQUE ; # in prefix mode
7815 * | || || | |+-> next
7816 * | || || | +--> val_end
7817 * | || || +---------> delim
7818 * | || |+------------> val_beg
7819 * | || +-------------> att_end = equal
7820 * | |+-----------------> att_beg
7821 * | +------------------> prev
7822 * +-------------------------> hdr_beg
7823 */
Willy Tarreau58f10d72006-12-04 02:26:12 +01007824
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007825 if (s->be->ck_opts & PR_CK_PFX) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007826 for (delim = val_beg; delim < val_end; delim++)
7827 if (*delim == COOKIE_DELIM)
7828 break;
Willy Tarreaubca99692010-10-06 19:25:55 +02007829 } else {
7830 char *vbar1;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007831 delim = val_end;
Willy Tarreaubca99692010-10-06 19:25:55 +02007832 /* Now check if the cookie contains a date field, which would
7833 * appear after a vertical bar ('|') just after the server name
7834 * and before the delimiter.
7835 */
7836 vbar1 = memchr(val_beg, COOKIE_DELIM_DATE, val_end - val_beg);
7837 if (vbar1) {
7838 /* OK, so left of the bar is the server's cookie and
Willy Tarreauf64d1412010-10-07 20:06:11 +02007839 * right is the last seen date. It is a base64 encoded
7840 * 30-bit value representing the UNIX date since the
7841 * epoch in 4-second quantities.
Willy Tarreaubca99692010-10-06 19:25:55 +02007842 */
Willy Tarreauf64d1412010-10-07 20:06:11 +02007843 int val;
Willy Tarreaubca99692010-10-06 19:25:55 +02007844 delim = vbar1++;
Willy Tarreauf64d1412010-10-07 20:06:11 +02007845 if (val_end - vbar1 >= 5) {
7846 val = b64tos30(vbar1);
7847 if (val > 0)
7848 txn->cookie_last_date = val << 2;
7849 }
7850 /* look for a second vertical bar */
7851 vbar1 = memchr(vbar1, COOKIE_DELIM_DATE, val_end - vbar1);
7852 if (vbar1 && (val_end - vbar1 > 5)) {
7853 val = b64tos30(vbar1 + 1);
7854 if (val > 0)
7855 txn->cookie_first_date = val << 2;
7856 }
Willy Tarreaubca99692010-10-06 19:25:55 +02007857 }
7858 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007859
Willy Tarreauf64d1412010-10-07 20:06:11 +02007860 /* if the cookie has an expiration date and the proxy wants to check
7861 * it, then we do that now. We first check if the cookie is too old,
7862 * then only if it has expired. We detect strict overflow because the
7863 * time resolution here is not great (4 seconds). Cookies with dates
7864 * in the future are ignored if their offset is beyond one day. This
7865 * allows an admin to fix timezone issues without expiring everyone
7866 * and at the same time avoids keeping unwanted side effects for too
7867 * long.
7868 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007869 if (txn->cookie_first_date && s->be->cookie_maxlife &&
7870 (((signed)(date.tv_sec - txn->cookie_first_date) > (signed)s->be->cookie_maxlife) ||
Willy Tarreauef4f3912010-10-07 21:00:29 +02007871 ((signed)(txn->cookie_first_date - date.tv_sec) > 86400))) {
Willy Tarreauf64d1412010-10-07 20:06:11 +02007872 txn->flags &= ~TX_CK_MASK;
7873 txn->flags |= TX_CK_OLD;
7874 delim = val_beg; // let's pretend we have not found the cookie
7875 txn->cookie_first_date = 0;
7876 txn->cookie_last_date = 0;
7877 }
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007878 else if (txn->cookie_last_date && s->be->cookie_maxidle &&
7879 (((signed)(date.tv_sec - txn->cookie_last_date) > (signed)s->be->cookie_maxidle) ||
Willy Tarreauef4f3912010-10-07 21:00:29 +02007880 ((signed)(txn->cookie_last_date - date.tv_sec) > 86400))) {
Willy Tarreauf64d1412010-10-07 20:06:11 +02007881 txn->flags &= ~TX_CK_MASK;
7882 txn->flags |= TX_CK_EXPIRED;
7883 delim = val_beg; // let's pretend we have not found the cookie
7884 txn->cookie_first_date = 0;
7885 txn->cookie_last_date = 0;
7886 }
7887
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007888 /* Here, we'll look for the first running server which supports the cookie.
7889 * This allows to share a same cookie between several servers, for example
7890 * to dedicate backup servers to specific servers only.
7891 * However, to prevent clients from sticking to cookie-less backup server
7892 * when they have incidentely learned an empty cookie, we simply ignore
7893 * empty cookies and mark them as invalid.
7894 * The same behaviour is applied when persistence must be ignored.
7895 */
Willy Tarreaue7dff022015-04-03 01:14:29 +02007896 if ((delim == val_beg) || (s->flags & (SF_IGNORE_PRST | SF_ASSIGNED)))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007897 srv = NULL;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007898
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007899 while (srv) {
7900 if (srv->cookie && (srv->cklen == delim - val_beg) &&
7901 !memcmp(val_beg, srv->cookie, delim - val_beg)) {
Willy Tarreau892337c2014-05-13 23:41:20 +02007902 if ((srv->state != SRV_ST_STOPPED) ||
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007903 (s->be->options & PR_O_PERSIST) ||
Willy Tarreaue7dff022015-04-03 01:14:29 +02007904 (s->flags & SF_FORCE_PRST)) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007905 /* we found the server and we can use it */
7906 txn->flags &= ~TX_CK_MASK;
Willy Tarreau892337c2014-05-13 23:41:20 +02007907 txn->flags |= (srv->state != SRV_ST_STOPPED) ? TX_CK_VALID : TX_CK_DOWN;
Willy Tarreaue7dff022015-04-03 01:14:29 +02007908 s->flags |= SF_DIRECT | SF_ASSIGNED;
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007909 s->target = &srv->obj_type;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007910 break;
7911 } else {
7912 /* we found a server, but it's down,
7913 * mark it as such and go on in case
7914 * another one is available.
7915 */
7916 txn->flags &= ~TX_CK_MASK;
7917 txn->flags |= TX_CK_DOWN;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007918 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007919 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007920 srv = srv->next;
7921 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007922
Willy Tarreauf64d1412010-10-07 20:06:11 +02007923 if (!srv && !(txn->flags & (TX_CK_DOWN|TX_CK_EXPIRED|TX_CK_OLD))) {
Willy Tarreauc89ccb62012-04-05 21:18:22 +02007924 /* no server matched this cookie or we deliberately skipped it */
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007925 txn->flags &= ~TX_CK_MASK;
Willy Tarreaue7dff022015-04-03 01:14:29 +02007926 if ((s->flags & (SF_IGNORE_PRST | SF_ASSIGNED)))
Willy Tarreauc89ccb62012-04-05 21:18:22 +02007927 txn->flags |= TX_CK_UNUSED;
7928 else
7929 txn->flags |= TX_CK_INVALID;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007930 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007931
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007932 /* depending on the cookie mode, we may have to either :
7933 * - delete the complete cookie if we're in insert+indirect mode, so that
7934 * the server never sees it ;
7935 * - remove the server id from the cookie value, and tag the cookie as an
7936 * application cookie so that it does not get accidentely removed later,
7937 * if we're in cookie prefix mode
7938 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007939 if ((s->be->ck_opts & PR_CK_PFX) && (delim != val_end)) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007940 int delta; /* negative */
Willy Tarreau58f10d72006-12-04 02:26:12 +01007941
Willy Tarreau9b28e032012-10-12 23:49:43 +02007942 delta = buffer_replace2(req->buf, val_beg, delim + 1, NULL, 0);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007943 val_end += delta;
7944 next += delta;
7945 hdr_end += delta;
7946 hdr_next += delta;
7947 cur_hdr->len += delta;
7948 http_msg_move_end(&txn->req, delta);
Willy Tarreau58f10d72006-12-04 02:26:12 +01007949
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007950 del_from = NULL;
7951 preserve_hdr = 1; /* we want to keep this cookie */
7952 }
7953 else if (del_from == NULL &&
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007954 (s->be->ck_opts & (PR_CK_INS | PR_CK_IND)) == (PR_CK_INS | PR_CK_IND)) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007955 del_from = prev;
7956 }
7957 } else {
7958 /* This is not our cookie, so we must preserve it. But if we already
7959 * scheduled another cookie for removal, we cannot remove the
7960 * complete header, but we can remove the previous block itself.
7961 */
7962 preserve_hdr = 1;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007963
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007964 if (del_from != NULL) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007965 int delta = del_hdr_value(req->buf, &del_from, prev);
Willy Tarreaub8105542010-11-24 18:31:28 +01007966 if (att_beg >= del_from)
7967 att_beg += delta;
7968 if (att_end >= del_from)
7969 att_end += delta;
7970 val_beg += delta;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007971 val_end += delta;
7972 next += delta;
7973 hdr_end += delta;
7974 hdr_next += delta;
7975 cur_hdr->len += delta;
7976 http_msg_move_end(&txn->req, delta);
7977 prev = del_from;
7978 del_from = NULL;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007979 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007980 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007981
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007982 /* continue with next cookie on this header line */
7983 att_beg = next;
7984 } /* for each cookie */
Willy Tarreau58f10d72006-12-04 02:26:12 +01007985
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007986 /* There are no more cookies on this line.
7987 * We may still have one (or several) marked for deletion at the
7988 * end of the line. We must do this now in two ways :
7989 * - if some cookies must be preserved, we only delete from the
7990 * mark to the end of line ;
7991 * - if nothing needs to be preserved, simply delete the whole header
Willy Tarreau58f10d72006-12-04 02:26:12 +01007992 */
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007993 if (del_from) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01007994 int delta;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007995 if (preserve_hdr) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007996 delta = del_hdr_value(req->buf, &del_from, hdr_end);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007997 hdr_end = del_from;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007998 cur_hdr->len += delta;
7999 } else {
Willy Tarreau9b28e032012-10-12 23:49:43 +02008000 delta = buffer_replace2(req->buf, hdr_beg, hdr_next, NULL, 0);
Willy Tarreau58f10d72006-12-04 02:26:12 +01008001
8002 /* FIXME: this should be a separate function */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01008003 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
8004 txn->hdr_idx.used--;
Willy Tarreau58f10d72006-12-04 02:26:12 +01008005 cur_hdr->len = 0;
Willy Tarreau26db59e2010-11-28 06:57:24 +01008006 cur_idx = old_idx;
Willy Tarreau58f10d72006-12-04 02:26:12 +01008007 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02008008 hdr_next += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01008009 http_msg_move_end(&txn->req, delta);
Willy Tarreau58f10d72006-12-04 02:26:12 +01008010 }
8011
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02008012 /* check next header */
Willy Tarreau58f10d72006-12-04 02:26:12 +01008013 old_idx = cur_idx;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02008014 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01008015}
8016
8017
Willy Tarreaua15645d2007-03-18 16:22:39 +01008018/* Iterate the same filter through all response headers contained in <rtr>.
8019 * Returns 1 if this filter can be stopped upon return, otherwise 0.
8020 */
Willy Tarreau87b09662015-04-03 00:22:06 +02008021int apply_filter_to_resp_headers(struct stream *s, struct channel *rtr, struct hdr_exp *exp)
Willy Tarreaua15645d2007-03-18 16:22:39 +01008022{
Willy Tarreaua15645d2007-03-18 16:22:39 +01008023 char *cur_ptr, *cur_end, *cur_next;
8024 int cur_idx, old_idx, last_hdr;
Willy Tarreaueee5b512015-04-03 23:46:31 +02008025 struct http_txn *txn = s->txn;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008026 struct hdr_idx_elem *cur_hdr;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01008027 int delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008028
8029 last_hdr = 0;
8030
Willy Tarreau9b28e032012-10-12 23:49:43 +02008031 cur_next = rtr->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01008032 old_idx = 0;
8033
8034 while (!last_hdr) {
Willy Tarreau3d300592007-03-18 18:34:41 +01008035 if (unlikely(txn->flags & TX_SVDENY))
Willy Tarreaua15645d2007-03-18 16:22:39 +01008036 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01008037 else if (unlikely(txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01008038 (exp->action == ACT_ALLOW ||
8039 exp->action == ACT_DENY))
8040 return 0;
8041
8042 cur_idx = txn->hdr_idx.v[old_idx].next;
8043 if (!cur_idx)
8044 break;
8045
8046 cur_hdr = &txn->hdr_idx.v[cur_idx];
8047 cur_ptr = cur_next;
8048 cur_end = cur_ptr + cur_hdr->len;
8049 cur_next = cur_end + cur_hdr->cr + 1;
8050
8051 /* Now we have one header between cur_ptr and cur_end,
8052 * and the next header starts at cur_next.
8053 */
8054
Willy Tarreau15a53a42015-01-21 13:39:42 +01008055 if (regex_exec_match2(exp->preg, cur_ptr, cur_end-cur_ptr, MAX_MATCH, pmatch, 0)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01008056 switch (exp->action) {
8057 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01008058 txn->flags |= TX_SVALLOW;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008059 last_hdr = 1;
8060 break;
8061
8062 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01008063 txn->flags |= TX_SVDENY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008064 last_hdr = 1;
8065 break;
8066
8067 case ACT_REPLACE:
Sasha Pachevc6002042014-05-26 12:33:48 -06008068 trash.len = exp_replace(trash.str, trash.size, cur_ptr, exp->replace, pmatch);
8069 if (trash.len < 0)
8070 return -1;
8071
Willy Tarreau19d14ef2012-10-29 16:51:55 +01008072 delta = buffer_replace2(rtr->buf, cur_ptr, cur_end, trash.str, trash.len);
Willy Tarreaua15645d2007-03-18 16:22:39 +01008073 /* FIXME: if the user adds a newline in the replacement, the
8074 * index will not be recalculated for now, and the new line
8075 * will not be counted as a new header.
8076 */
8077
8078 cur_end += delta;
8079 cur_next += delta;
8080 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01008081 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01008082 break;
8083
8084 case ACT_REMOVE:
Willy Tarreau9b28e032012-10-12 23:49:43 +02008085 delta = buffer_replace2(rtr->buf, cur_ptr, cur_next, NULL, 0);
Willy Tarreaua15645d2007-03-18 16:22:39 +01008086 cur_next += delta;
8087
Willy Tarreaufa355d42009-11-29 18:12:29 +01008088 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01008089 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
8090 txn->hdr_idx.used--;
8091 cur_hdr->len = 0;
8092 cur_end = NULL; /* null-term has been rewritten */
Willy Tarreau26db59e2010-11-28 06:57:24 +01008093 cur_idx = old_idx;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008094 break;
8095
8096 }
8097 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01008098
8099 /* keep the link from this header to next one in case of later
8100 * removal of next header.
8101 */
8102 old_idx = cur_idx;
8103 }
8104 return 0;
8105}
8106
8107
8108/* Apply the filter to the status line in the response buffer <rtr>.
8109 * Returns 0 if nothing has been done, 1 if the filter has been applied,
8110 * or -1 if a replacement resulted in an invalid status line.
8111 */
Willy Tarreau87b09662015-04-03 00:22:06 +02008112int apply_filter_to_sts_line(struct stream *s, struct channel *rtr, struct hdr_exp *exp)
Willy Tarreaua15645d2007-03-18 16:22:39 +01008113{
Willy Tarreaua15645d2007-03-18 16:22:39 +01008114 char *cur_ptr, *cur_end;
8115 int done;
Willy Tarreaueee5b512015-04-03 23:46:31 +02008116 struct http_txn *txn = s->txn;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01008117 int delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008118
8119
Willy Tarreau3d300592007-03-18 18:34:41 +01008120 if (unlikely(txn->flags & TX_SVDENY))
Willy Tarreaua15645d2007-03-18 16:22:39 +01008121 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01008122 else if (unlikely(txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01008123 (exp->action == ACT_ALLOW ||
8124 exp->action == ACT_DENY))
8125 return 0;
8126 else if (exp->action == ACT_REMOVE)
8127 return 0;
8128
8129 done = 0;
8130
Willy Tarreau9b28e032012-10-12 23:49:43 +02008131 cur_ptr = rtr->buf->p;
Willy Tarreau1ba0e5f2010-06-07 13:57:32 +02008132 cur_end = cur_ptr + txn->rsp.sl.st.l;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008133
8134 /* Now we have the status line between cur_ptr and cur_end */
8135
Willy Tarreau15a53a42015-01-21 13:39:42 +01008136 if (regex_exec_match2(exp->preg, cur_ptr, cur_end-cur_ptr, MAX_MATCH, pmatch, 0)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01008137 switch (exp->action) {
8138 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01008139 txn->flags |= TX_SVALLOW;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008140 done = 1;
8141 break;
8142
8143 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01008144 txn->flags |= TX_SVDENY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008145 done = 1;
8146 break;
8147
8148 case ACT_REPLACE:
Sasha Pachevc6002042014-05-26 12:33:48 -06008149 trash.len = exp_replace(trash.str, trash.size, cur_ptr, exp->replace, pmatch);
8150 if (trash.len < 0)
8151 return -1;
8152
Willy Tarreau19d14ef2012-10-29 16:51:55 +01008153 delta = buffer_replace2(rtr->buf, cur_ptr, cur_end, trash.str, trash.len);
Willy Tarreaua15645d2007-03-18 16:22:39 +01008154 /* FIXME: if the user adds a newline in the replacement, the
8155 * index will not be recalculated for now, and the new line
8156 * will not be counted as a new header.
8157 */
8158
Willy Tarreaufa355d42009-11-29 18:12:29 +01008159 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01008160 cur_end += delta;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02008161 cur_end = (char *)http_parse_stsline(&txn->rsp,
Willy Tarreau02785762007-04-03 14:45:44 +02008162 HTTP_MSG_RPVER,
Willy Tarreaua15645d2007-03-18 16:22:39 +01008163 cur_ptr, cur_end + 1,
8164 NULL, NULL);
8165 if (unlikely(!cur_end))
8166 return -1;
8167
8168 /* we have a full respnse and we know that we have either a CR
8169 * or an LF at <ptr>.
8170 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02008171 txn->status = strl2ui(rtr->buf->p + txn->rsp.sl.st.c, txn->rsp.sl.st.c_l);
Willy Tarreau1ba0e5f2010-06-07 13:57:32 +02008172 hdr_idx_set_start(&txn->hdr_idx, txn->rsp.sl.st.l, *cur_end == '\r');
Willy Tarreaua15645d2007-03-18 16:22:39 +01008173 /* there is no point trying this regex on headers */
8174 return 1;
8175 }
8176 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01008177 return done;
8178}
8179
8180
8181
8182/*
Willy Tarreau87b09662015-04-03 00:22:06 +02008183 * Apply all the resp filters of proxy <px> to all headers in buffer <rtr> of stream <s>.
Willy Tarreaua15645d2007-03-18 16:22:39 +01008184 * Returns 0 if everything is alright, or -1 in case a replacement lead to an
8185 * unparsable response.
8186 */
Willy Tarreau87b09662015-04-03 00:22:06 +02008187int apply_filters_to_response(struct stream *s, struct channel *rtr, struct proxy *px)
Willy Tarreaua15645d2007-03-18 16:22:39 +01008188{
Willy Tarreau192252e2015-04-04 01:47:55 +02008189 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02008190 struct http_txn *txn = s->txn;
Willy Tarreaufdb563c2010-01-31 15:43:27 +01008191 struct hdr_exp *exp;
8192
8193 for (exp = px->rsp_exp; exp; exp = exp->next) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01008194 int ret;
8195
8196 /*
8197 * The interleaving of transformations and verdicts
8198 * makes it difficult to decide to continue or stop
8199 * the evaluation.
8200 */
8201
Willy Tarreaufdb563c2010-01-31 15:43:27 +01008202 if (txn->flags & TX_SVDENY)
8203 break;
8204
Willy Tarreau3d300592007-03-18 18:34:41 +01008205 if ((txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01008206 (exp->action == ACT_ALLOW || exp->action == ACT_DENY ||
8207 exp->action == ACT_PASS)) {
8208 exp = exp->next;
8209 continue;
8210 }
8211
Willy Tarreaufdb563c2010-01-31 15:43:27 +01008212 /* if this filter had a condition, evaluate it now and skip to
8213 * next filter if the condition does not match.
8214 */
8215 if (exp->cond) {
Willy Tarreau192252e2015-04-04 01:47:55 +02008216 ret = acl_exec_cond(exp->cond, px, sess, s, SMP_OPT_DIR_RES|SMP_OPT_FINAL);
Willy Tarreaufdb563c2010-01-31 15:43:27 +01008217 ret = acl_pass(ret);
8218 if (((struct acl_cond *)exp->cond)->pol == ACL_COND_UNLESS)
8219 ret = !ret;
8220 if (!ret)
8221 continue;
8222 }
8223
Willy Tarreaua15645d2007-03-18 16:22:39 +01008224 /* Apply the filter to the status line. */
Willy Tarreaufdb563c2010-01-31 15:43:27 +01008225 ret = apply_filter_to_sts_line(s, rtr, exp);
Willy Tarreaua15645d2007-03-18 16:22:39 +01008226 if (unlikely(ret < 0))
8227 return -1;
8228
8229 if (likely(ret == 0)) {
8230 /* The filter did not match the response, it can be
8231 * iterated through all headers.
8232 */
Sasha Pachevc6002042014-05-26 12:33:48 -06008233 if (unlikely(apply_filter_to_resp_headers(s, rtr, exp) < 0))
8234 return -1;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008235 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01008236 }
8237 return 0;
8238}
8239
8240
Willy Tarreaua15645d2007-03-18 16:22:39 +01008241/*
Willy Tarreau396d2c62007-11-04 19:30:00 +01008242 * Manage server-side cookies. It can impact performance by about 2% so it is
Willy Tarreau24581ba2010-08-31 22:39:35 +02008243 * desirable to call it only when needed. This function is also used when we
8244 * just need to know if there is a cookie (eg: for check-cache).
Willy Tarreaua15645d2007-03-18 16:22:39 +01008245 */
Willy Tarreau87b09662015-04-03 00:22:06 +02008246void manage_server_side_cookies(struct stream *s, struct channel *res)
Willy Tarreaua15645d2007-03-18 16:22:39 +01008247{
Willy Tarreaueee5b512015-04-03 23:46:31 +02008248 struct http_txn *txn = s->txn;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02008249 struct session *sess = s->sess;
Willy Tarreau827aee92011-03-10 16:55:02 +01008250 struct server *srv;
Willy Tarreau24581ba2010-08-31 22:39:35 +02008251 int is_cookie2;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008252 int cur_idx, old_idx, delta;
Willy Tarreau24581ba2010-08-31 22:39:35 +02008253 char *hdr_beg, *hdr_end, *hdr_next;
8254 char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008255
Willy Tarreaua15645d2007-03-18 16:22:39 +01008256 /* Iterate through the headers.
8257 * we start with the start line.
8258 */
8259 old_idx = 0;
Willy Tarreau9b28e032012-10-12 23:49:43 +02008260 hdr_next = res->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01008261
8262 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
8263 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01008264 int val;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008265
8266 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreau24581ba2010-08-31 22:39:35 +02008267 hdr_beg = hdr_next;
8268 hdr_end = hdr_beg + cur_hdr->len;
8269 hdr_next = hdr_end + cur_hdr->cr + 1;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008270
Willy Tarreau24581ba2010-08-31 22:39:35 +02008271 /* We have one full header between hdr_beg and hdr_end, and the
8272 * next header starts at hdr_next. We're only interested in
8273 * "Set-Cookie" and "Set-Cookie2" headers.
Willy Tarreaua15645d2007-03-18 16:22:39 +01008274 */
8275
Willy Tarreau24581ba2010-08-31 22:39:35 +02008276 is_cookie2 = 0;
8277 prev = hdr_beg + 10;
8278 val = http_header_match2(hdr_beg, hdr_end, "Set-Cookie", 10);
Willy Tarreauaa9dce32007-03-18 23:50:16 +01008279 if (!val) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02008280 val = http_header_match2(hdr_beg, hdr_end, "Set-Cookie2", 11);
8281 if (!val) {
8282 old_idx = cur_idx;
8283 continue;
8284 }
8285 is_cookie2 = 1;
8286 prev = hdr_beg + 11;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008287 }
8288
Willy Tarreau24581ba2010-08-31 22:39:35 +02008289 /* OK, right now we know we have a Set-Cookie* at hdr_beg, and
8290 * <prev> points to the colon.
8291 */
Willy Tarreauf1348312010-10-07 15:54:11 +02008292 txn->flags |= TX_SCK_PRESENT;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008293
Willy Tarreau24581ba2010-08-31 22:39:35 +02008294 /* Maybe we only wanted to see if there was a Set-Cookie (eg:
8295 * check-cache is enabled) and we are not interested in checking
8296 * them. Warning, the cookie capture is declared in the frontend.
Willy Tarreaufd39dda2008-10-17 12:01:58 +02008297 */
Willy Tarreau53a09d52015-08-10 18:59:40 +02008298 if (s->be->cookie_name == NULL && sess->fe->capture_name == NULL)
Willy Tarreaua15645d2007-03-18 16:22:39 +01008299 return;
8300
Willy Tarreau24581ba2010-08-31 22:39:35 +02008301 /* OK so now we know we have to process this response cookie.
8302 * The format of the Set-Cookie header is slightly different
8303 * from the format of the Cookie header in that it does not
8304 * support the comma as a cookie delimiter (thus the header
8305 * cannot be folded) because the Expires attribute described in
8306 * the original Netscape's spec may contain an unquoted date
8307 * with a comma inside. We have to live with this because
8308 * many browsers don't support Max-Age and some browsers don't
8309 * support quoted strings. However the Set-Cookie2 header is
8310 * clean.
8311 *
8312 * We have to keep multiple pointers in order to support cookie
8313 * removal at the beginning, middle or end of header without
8314 * corrupting the header (in case of set-cookie2). A special
8315 * pointer, <scav> points to the beginning of the set-cookie-av
8316 * fields after the first semi-colon. The <next> pointer points
8317 * either to the end of line (set-cookie) or next unquoted comma
8318 * (set-cookie2). All of these headers are valid :
8319 *
8320 * Set-Cookie: NAME1 = VALUE 1 ; Secure; Path="/"\r\n
8321 * Set-Cookie:NAME=VALUE; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT\r\n
8322 * Set-Cookie: NAME = VALUE ; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT\r\n
8323 * Set-Cookie2: NAME1 = VALUE 1 ; Max-Age=0, NAME2=VALUE2; Discard\r\n
8324 * | | | | | | | | | |
8325 * | | | | | | | | +-> next hdr_end <--+
8326 * | | | | | | | +------------> scav
8327 * | | | | | | +--------------> val_end
8328 * | | | | | +--------------------> val_beg
8329 * | | | | +----------------------> equal
8330 * | | | +------------------------> att_end
8331 * | | +----------------------------> att_beg
8332 * | +------------------------------> prev
8333 * +-----------------------------------------> hdr_beg
8334 */
Willy Tarreaua15645d2007-03-18 16:22:39 +01008335
Willy Tarreau24581ba2010-08-31 22:39:35 +02008336 for (; prev < hdr_end; prev = next) {
8337 /* Iterate through all cookies on this line */
Willy Tarreaua15645d2007-03-18 16:22:39 +01008338
Willy Tarreau24581ba2010-08-31 22:39:35 +02008339 /* find att_beg */
8340 att_beg = prev + 1;
Willy Tarreau2235b262016-11-05 15:50:20 +01008341 while (att_beg < hdr_end && HTTP_IS_SPHT(*att_beg))
Willy Tarreau24581ba2010-08-31 22:39:35 +02008342 att_beg++;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008343
Willy Tarreau24581ba2010-08-31 22:39:35 +02008344 /* find att_end : this is the first character after the last non
8345 * space before the equal. It may be equal to hdr_end.
8346 */
8347 equal = att_end = att_beg;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008348
Willy Tarreau24581ba2010-08-31 22:39:35 +02008349 while (equal < hdr_end) {
8350 if (*equal == '=' || *equal == ';' || (is_cookie2 && *equal == ','))
8351 break;
Willy Tarreau2235b262016-11-05 15:50:20 +01008352 if (HTTP_IS_SPHT(*equal++))
Willy Tarreau24581ba2010-08-31 22:39:35 +02008353 continue;
8354 att_end = equal;
8355 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01008356
Willy Tarreau24581ba2010-08-31 22:39:35 +02008357 /* here, <equal> points to '=', a delimitor or the end. <att_end>
8358 * is between <att_beg> and <equal>, both may be identical.
8359 */
8360
8361 /* look for end of cookie if there is an equal sign */
8362 if (equal < hdr_end && *equal == '=') {
8363 /* look for the beginning of the value */
8364 val_beg = equal + 1;
Willy Tarreau2235b262016-11-05 15:50:20 +01008365 while (val_beg < hdr_end && HTTP_IS_SPHT(*val_beg))
Willy Tarreau24581ba2010-08-31 22:39:35 +02008366 val_beg++;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008367
Willy Tarreau24581ba2010-08-31 22:39:35 +02008368 /* find the end of the value, respecting quotes */
8369 next = find_cookie_value_end(val_beg, hdr_end);
8370
8371 /* make val_end point to the first white space or delimitor after the value */
8372 val_end = next;
Willy Tarreau2235b262016-11-05 15:50:20 +01008373 while (val_end > val_beg && HTTP_IS_SPHT(*(val_end - 1)))
Willy Tarreau24581ba2010-08-31 22:39:35 +02008374 val_end--;
8375 } else {
8376 /* <equal> points to next comma, semi-colon or EOL */
8377 val_beg = val_end = next = equal;
8378 }
8379
8380 if (next < hdr_end) {
8381 /* Set-Cookie2 supports multiple cookies, and <next> points to
8382 * a colon or semi-colon before the end. So skip all attr-value
8383 * pairs and look for the next comma. For Set-Cookie, since
8384 * commas are permitted in values, skip to the end.
8385 */
8386 if (is_cookie2)
8387 next = find_hdr_value_end(next, hdr_end);
8388 else
8389 next = hdr_end;
8390 }
8391
8392 /* Now everything is as on the diagram above */
8393
8394 /* Ignore cookies with no equal sign */
8395 if (equal == val_end)
8396 continue;
8397
8398 /* If there are spaces around the equal sign, we need to
8399 * strip them otherwise we'll get trouble for cookie captures,
8400 * or even for rewrites. Since this happens extremely rarely,
8401 * it does not hurt performance.
Willy Tarreaua15645d2007-03-18 16:22:39 +01008402 */
Willy Tarreau24581ba2010-08-31 22:39:35 +02008403 if (unlikely(att_end != equal || val_beg > equal + 1)) {
8404 int stripped_before = 0;
8405 int stripped_after = 0;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008406
Willy Tarreau24581ba2010-08-31 22:39:35 +02008407 if (att_end != equal) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02008408 stripped_before = buffer_replace2(res->buf, att_end, equal, NULL, 0);
Willy Tarreau24581ba2010-08-31 22:39:35 +02008409 equal += stripped_before;
8410 val_beg += stripped_before;
8411 }
8412
8413 if (val_beg > equal + 1) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02008414 stripped_after = buffer_replace2(res->buf, equal + 1, val_beg, NULL, 0);
Willy Tarreau24581ba2010-08-31 22:39:35 +02008415 val_beg += stripped_after;
8416 stripped_before += stripped_after;
8417 }
8418
8419 val_end += stripped_before;
8420 next += stripped_before;
8421 hdr_end += stripped_before;
8422 hdr_next += stripped_before;
8423 cur_hdr->len += stripped_before;
Willy Tarreau1fc1f452011-04-07 22:35:37 +02008424 http_msg_move_end(&txn->rsp, stripped_before);
Willy Tarreau24581ba2010-08-31 22:39:35 +02008425 }
8426
8427 /* First, let's see if we want to capture this cookie. We check
8428 * that we don't already have a server side cookie, because we
8429 * can only capture one. Also as an optimisation, we ignore
8430 * cookies shorter than the declared name.
8431 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02008432 if (sess->fe->capture_name != NULL &&
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01008433 txn->srv_cookie == NULL &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02008434 (val_end - att_beg >= sess->fe->capture_namelen) &&
8435 memcmp(att_beg, sess->fe->capture_name, sess->fe->capture_namelen) == 0) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02008436 int log_len = val_end - att_beg;
Willy Tarreau086b3b42007-05-13 21:45:51 +02008437 if ((txn->srv_cookie = pool_alloc2(pool2_capture)) == NULL) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01008438 Alert("HTTP logging : out of memory.\n");
8439 }
Willy Tarreauf70fc752010-11-19 11:27:18 +01008440 else {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02008441 if (log_len > sess->fe->capture_len)
8442 log_len = sess->fe->capture_len;
Willy Tarreauf70fc752010-11-19 11:27:18 +01008443 memcpy(txn->srv_cookie, att_beg, log_len);
8444 txn->srv_cookie[log_len] = 0;
8445 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01008446 }
8447
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008448 srv = objt_server(s->target);
Willy Tarreaua15645d2007-03-18 16:22:39 +01008449 /* now check if we need to process it for persistence */
Willy Tarreaue7dff022015-04-03 01:14:29 +02008450 if (!(s->flags & SF_IGNORE_PRST) &&
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008451 (att_end - att_beg == s->be->cookie_len) && (s->be->cookie_name != NULL) &&
8452 (memcmp(att_beg, s->be->cookie_name, att_end - att_beg) == 0)) {
Willy Tarreauf1348312010-10-07 15:54:11 +02008453 /* assume passive cookie by default */
8454 txn->flags &= ~TX_SCK_MASK;
8455 txn->flags |= TX_SCK_FOUND;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008456
8457 /* If the cookie is in insert mode on a known server, we'll delete
8458 * this occurrence because we'll insert another one later.
8459 * We'll delete it too if the "indirect" option is set and we're in
Willy Tarreau24581ba2010-08-31 22:39:35 +02008460 * a direct access.
8461 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008462 if (s->be->ck_opts & PR_CK_PSV) {
Willy Tarreauba4c5be2010-10-23 12:46:42 +02008463 /* The "preserve" flag was set, we don't want to touch the
8464 * server's cookie.
8465 */
8466 }
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008467 else if ((srv && (s->be->ck_opts & PR_CK_INS)) ||
Willy Tarreaue7dff022015-04-03 01:14:29 +02008468 ((s->flags & SF_DIRECT) && (s->be->ck_opts & PR_CK_IND))) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02008469 /* this cookie must be deleted */
8470 if (*prev == ':' && next == hdr_end) {
8471 /* whole header */
Willy Tarreau9b28e032012-10-12 23:49:43 +02008472 delta = buffer_replace2(res->buf, hdr_beg, hdr_next, NULL, 0);
Willy Tarreau24581ba2010-08-31 22:39:35 +02008473 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
8474 txn->hdr_idx.used--;
8475 cur_hdr->len = 0;
Willy Tarreau26db59e2010-11-28 06:57:24 +01008476 cur_idx = old_idx;
Willy Tarreau24581ba2010-08-31 22:39:35 +02008477 hdr_next += delta;
8478 http_msg_move_end(&txn->rsp, delta);
8479 /* note: while both invalid now, <next> and <hdr_end>
8480 * are still equal, so the for() will stop as expected.
8481 */
8482 } else {
8483 /* just remove the value */
Willy Tarreau9b28e032012-10-12 23:49:43 +02008484 int delta = del_hdr_value(res->buf, &prev, next);
Willy Tarreau24581ba2010-08-31 22:39:35 +02008485 next = prev;
8486 hdr_end += delta;
8487 hdr_next += delta;
8488 cur_hdr->len += delta;
8489 http_msg_move_end(&txn->rsp, delta);
8490 }
Willy Tarreauf1348312010-10-07 15:54:11 +02008491 txn->flags &= ~TX_SCK_MASK;
Willy Tarreau3d300592007-03-18 18:34:41 +01008492 txn->flags |= TX_SCK_DELETED;
Willy Tarreau24581ba2010-08-31 22:39:35 +02008493 /* and go on with next cookie */
Willy Tarreaua15645d2007-03-18 16:22:39 +01008494 }
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008495 else if (srv && srv->cookie && (s->be->ck_opts & PR_CK_RW)) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02008496 /* replace bytes val_beg->val_end with the cookie name associated
Willy Tarreaua15645d2007-03-18 16:22:39 +01008497 * with this server since we know it.
8498 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02008499 delta = buffer_replace2(res->buf, val_beg, val_end, srv->cookie, srv->cklen);
Willy Tarreau24581ba2010-08-31 22:39:35 +02008500 next += delta;
8501 hdr_end += delta;
8502 hdr_next += delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008503 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01008504 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01008505
Willy Tarreauf1348312010-10-07 15:54:11 +02008506 txn->flags &= ~TX_SCK_MASK;
8507 txn->flags |= TX_SCK_REPLACED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008508 }
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008509 else if (srv && srv->cookie && (s->be->ck_opts & PR_CK_PFX)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01008510 /* insert the cookie name associated with this server
Willy Tarreau24581ba2010-08-31 22:39:35 +02008511 * before existing cookie, and insert a delimiter between them..
Willy Tarreaua15645d2007-03-18 16:22:39 +01008512 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02008513 delta = buffer_replace2(res->buf, val_beg, val_beg, srv->cookie, srv->cklen + 1);
Willy Tarreau24581ba2010-08-31 22:39:35 +02008514 next += delta;
8515 hdr_end += delta;
8516 hdr_next += delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008517 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01008518 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01008519
Willy Tarreau827aee92011-03-10 16:55:02 +01008520 val_beg[srv->cklen] = COOKIE_DELIM;
Willy Tarreauf1348312010-10-07 15:54:11 +02008521 txn->flags &= ~TX_SCK_MASK;
8522 txn->flags |= TX_SCK_REPLACED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008523 }
8524 }
Willy Tarreau24581ba2010-08-31 22:39:35 +02008525 /* that's done for this cookie, check the next one on the same
8526 * line when next != hdr_end (only if is_cookie2).
8527 */
8528 }
8529 /* check next header */
Willy Tarreaua15645d2007-03-18 16:22:39 +01008530 old_idx = cur_idx;
Willy Tarreau24581ba2010-08-31 22:39:35 +02008531 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01008532}
8533
8534
Willy Tarreaua15645d2007-03-18 16:22:39 +01008535/*
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008536 * Check if response is cacheable or not. Updates s->flags.
Willy Tarreaua15645d2007-03-18 16:22:39 +01008537 */
Willy Tarreau87b09662015-04-03 00:22:06 +02008538void check_response_for_cacheability(struct stream *s, struct channel *rtr)
Willy Tarreaua15645d2007-03-18 16:22:39 +01008539{
Willy Tarreaueee5b512015-04-03 23:46:31 +02008540 struct http_txn *txn = s->txn;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008541 char *p1, *p2;
8542
8543 char *cur_ptr, *cur_end, *cur_next;
8544 int cur_idx;
8545
Willy Tarreau5df51872007-11-25 16:20:08 +01008546 if (!(txn->flags & TX_CACHEABLE))
Willy Tarreaua15645d2007-03-18 16:22:39 +01008547 return;
8548
8549 /* Iterate through the headers.
8550 * we start with the start line.
8551 */
8552 cur_idx = 0;
Willy Tarreau9b28e032012-10-12 23:49:43 +02008553 cur_next = rtr->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01008554
8555 while ((cur_idx = txn->hdr_idx.v[cur_idx].next)) {
8556 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01008557 int val;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008558
8559 cur_hdr = &txn->hdr_idx.v[cur_idx];
8560 cur_ptr = cur_next;
8561 cur_end = cur_ptr + cur_hdr->len;
8562 cur_next = cur_end + cur_hdr->cr + 1;
8563
8564 /* We have one full header between cur_ptr and cur_end, and the
8565 * next header starts at cur_next. We're only interested in
8566 * "Cookie:" headers.
8567 */
8568
Willy Tarreauaa9dce32007-03-18 23:50:16 +01008569 val = http_header_match2(cur_ptr, cur_end, "Pragma", 6);
8570 if (val) {
8571 if ((cur_end - (cur_ptr + val) >= 8) &&
8572 strncasecmp(cur_ptr + val, "no-cache", 8) == 0) {
8573 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
8574 return;
8575 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01008576 }
8577
Willy Tarreauaa9dce32007-03-18 23:50:16 +01008578 val = http_header_match2(cur_ptr, cur_end, "Cache-control", 13);
8579 if (!val)
Willy Tarreaua15645d2007-03-18 16:22:39 +01008580 continue;
8581
8582 /* OK, right now we know we have a cache-control header at cur_ptr */
8583
Willy Tarreauaa9dce32007-03-18 23:50:16 +01008584 p1 = cur_ptr + val; /* first non-space char after 'cache-control:' */
Willy Tarreaua15645d2007-03-18 16:22:39 +01008585
8586 if (p1 >= cur_end) /* no more info */
8587 continue;
8588
8589 /* p1 is at the beginning of the value */
8590 p2 = p1;
8591
Willy Tarreau8f8e6452007-06-17 21:51:38 +02008592 while (p2 < cur_end && *p2 != '=' && *p2 != ',' && !isspace((unsigned char)*p2))
Willy Tarreaua15645d2007-03-18 16:22:39 +01008593 p2++;
8594
8595 /* we have a complete value between p1 and p2 */
8596 if (p2 < cur_end && *p2 == '=') {
8597 /* we have something of the form no-cache="set-cookie" */
8598 if ((cur_end - p1 >= 21) &&
8599 strncasecmp(p1, "no-cache=\"set-cookie", 20) == 0
8600 && (p1[20] == '"' || p1[20] == ','))
Willy Tarreau3d300592007-03-18 18:34:41 +01008601 txn->flags &= ~TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008602 continue;
8603 }
8604
8605 /* OK, so we know that either p2 points to the end of string or to a comma */
8606 if (((p2 - p1 == 7) && strncasecmp(p1, "private", 7) == 0) ||
Willy Tarreau5b15f902013-07-04 12:46:56 +02008607 ((p2 - p1 == 8) && strncasecmp(p1, "no-cache", 8) == 0) ||
Willy Tarreaua15645d2007-03-18 16:22:39 +01008608 ((p2 - p1 == 8) && strncasecmp(p1, "no-store", 8) == 0) ||
8609 ((p2 - p1 == 9) && strncasecmp(p1, "max-age=0", 9) == 0) ||
8610 ((p2 - p1 == 10) && strncasecmp(p1, "s-maxage=0", 10) == 0)) {
Willy Tarreau3d300592007-03-18 18:34:41 +01008611 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008612 return;
8613 }
8614
8615 if ((p2 - p1 == 6) && strncasecmp(p1, "public", 6) == 0) {
Willy Tarreau3d300592007-03-18 18:34:41 +01008616 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008617 continue;
8618 }
8619 }
8620}
8621
Willy Tarreau58f10d72006-12-04 02:26:12 +01008622
Willy Tarreaub2513902006-12-17 14:52:38 +01008623/*
Cyril Bonté70be45d2010-10-12 00:14:35 +02008624 * In a GET, HEAD or POST request, check if the requested URI matches the stats uri
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01008625 * for the current backend.
Willy Tarreaub2513902006-12-17 14:52:38 +01008626 *
Cyril Bonté70be45d2010-10-12 00:14:35 +02008627 * It is assumed that the request is either a HEAD, GET, or POST and that the
Willy Tarreau295a8372011-03-10 11:25:07 +01008628 * uri_auth field is valid.
Willy Tarreaub2513902006-12-17 14:52:38 +01008629 *
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01008630 * Returns 1 if stats should be provided, otherwise 0.
Willy Tarreaub2513902006-12-17 14:52:38 +01008631 */
Willy Tarreau295a8372011-03-10 11:25:07 +01008632int stats_check_uri(struct stream_interface *si, struct http_txn *txn, struct proxy *backend)
Willy Tarreaub2513902006-12-17 14:52:38 +01008633{
8634 struct uri_auth *uri_auth = backend->uri_auth;
Willy Tarreau3a215be2012-03-09 21:39:51 +01008635 struct http_msg *msg = &txn->req;
Willy Tarreau9b28e032012-10-12 23:49:43 +02008636 const char *uri = msg->chn->buf->p+ msg->sl.rq.u;
Willy Tarreaub2513902006-12-17 14:52:38 +01008637
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01008638 if (!uri_auth)
8639 return 0;
8640
Cyril Bonté70be45d2010-10-12 00:14:35 +02008641 if (txn->meth != HTTP_METH_GET && txn->meth != HTTP_METH_HEAD && txn->meth != HTTP_METH_POST)
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01008642 return 0;
8643
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01008644 /* check URI size */
Willy Tarreau3a215be2012-03-09 21:39:51 +01008645 if (uri_auth->uri_len > msg->sl.rq.u_l)
Willy Tarreaub2513902006-12-17 14:52:38 +01008646 return 0;
8647
Willy Tarreau414e9bb2013-11-23 00:30:38 +01008648 if (memcmp(uri, uri_auth->uri_prefix, uri_auth->uri_len) != 0)
Willy Tarreaub2513902006-12-17 14:52:38 +01008649 return 0;
8650
Willy Tarreaub2513902006-12-17 14:52:38 +01008651 return 1;
8652}
8653
Willy Tarreau4076a152009-04-02 15:18:36 +02008654/*
8655 * Capture a bad request or response and archive it in the proxy's structure.
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02008656 * By default it tries to report the error position as msg->err_pos. However if
8657 * this one is not set, it will then report msg->next, which is the last known
8658 * parsing point. The function is able to deal with wrapping buffers. It always
Willy Tarreaucdbdd522012-10-12 22:51:15 +02008659 * displays buffers as a contiguous area starting at buf->p.
Willy Tarreau4076a152009-04-02 15:18:36 +02008660 */
Willy Tarreau87b09662015-04-03 00:22:06 +02008661void http_capture_bad_message(struct error_snapshot *es, struct stream *s,
Willy Tarreau8a0cef22012-03-09 13:39:23 +01008662 struct http_msg *msg,
Willy Tarreau3770f232013-12-07 00:01:53 +01008663 enum ht_state state, struct proxy *other_end)
Willy Tarreau4076a152009-04-02 15:18:36 +02008664{
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02008665 struct session *sess = strm_sess(s);
Willy Tarreaucdbdd522012-10-12 22:51:15 +02008666 struct channel *chn = msg->chn;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02008667 int len1, len2;
Willy Tarreau8a0cef22012-03-09 13:39:23 +01008668
Willy Tarreauf3764b72016-03-31 13:45:10 +02008669 es->len = MIN(chn->buf->i, global.tune.bufsize);
Willy Tarreau9b28e032012-10-12 23:49:43 +02008670 len1 = chn->buf->data + chn->buf->size - chn->buf->p;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02008671 len1 = MIN(len1, es->len);
8672 len2 = es->len - len1; /* remaining data if buffer wraps */
8673
Willy Tarreauf3764b72016-03-31 13:45:10 +02008674 if (!es->buf)
8675 es->buf = malloc(global.tune.bufsize);
8676
8677 if (es->buf) {
8678 memcpy(es->buf, chn->buf->p, len1);
8679 if (len2)
8680 memcpy(es->buf + len1, chn->buf->data, len2);
8681 }
Willy Tarreau81f2fb92010-12-12 13:09:08 +01008682
Willy Tarreau4076a152009-04-02 15:18:36 +02008683 if (msg->err_pos >= 0)
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02008684 es->pos = msg->err_pos;
Willy Tarreau81f2fb92010-12-12 13:09:08 +01008685 else
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02008686 es->pos = msg->next;
Willy Tarreau81f2fb92010-12-12 13:09:08 +01008687
Willy Tarreau4076a152009-04-02 15:18:36 +02008688 es->when = date; // user-visible date
8689 es->sid = s->uniq_id;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01008690 es->srv = objt_server(s->target);
Willy Tarreau4076a152009-04-02 15:18:36 +02008691 es->oe = other_end;
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02008692 if (objt_conn(sess->origin))
8693 es->src = __objt_conn(sess->origin)->addr.from;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008694 else
8695 memset(&es->src, 0, sizeof(es->src));
8696
Willy Tarreau078272e2010-12-12 12:46:33 +01008697 es->state = state;
Willy Tarreau10479e42010-12-12 14:00:34 +01008698 es->ev_id = error_snapshot_id++;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02008699 es->b_flags = chn->flags;
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02008700 es->s_flags = s->flags;
Willy Tarreaueee5b512015-04-03 23:46:31 +02008701 es->t_flags = s->txn->flags;
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02008702 es->m_flags = msg->flags;
Willy Tarreau9b28e032012-10-12 23:49:43 +02008703 es->b_out = chn->buf->o;
8704 es->b_wrap = chn->buf->data + chn->buf->size - chn->buf->p;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02008705 es->b_tot = chn->total;
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02008706 es->m_clen = msg->chunk_len;
8707 es->m_blen = msg->body_len;
Willy Tarreau4076a152009-04-02 15:18:36 +02008708}
Willy Tarreaub2513902006-12-17 14:52:38 +01008709
Willy Tarreau294c4732011-12-16 21:35:50 +01008710/* Return in <vptr> and <vlen> the pointer and length of occurrence <occ> of
8711 * header whose name is <hname> of length <hlen>. If <ctx> is null, lookup is
8712 * performed over the whole headers. Otherwise it must contain a valid header
8713 * context, initialised with ctx->idx=0 for the first lookup in a series. If
8714 * <occ> is positive or null, occurrence #occ from the beginning (or last ctx)
8715 * is returned. Occ #0 and #1 are equivalent. If <occ> is negative (and no less
8716 * than -MAX_HDR_HISTORY), the occurrence is counted from the last one which is
Willy Tarreau04ff9f12013-06-10 18:39:42 +02008717 * -1. The value fetch stops at commas, so this function is suited for use with
8718 * list headers.
Willy Tarreau294c4732011-12-16 21:35:50 +01008719 * The return value is 0 if nothing was found, or non-zero otherwise.
Willy Tarreaubce70882009-09-07 11:51:47 +02008720 */
Willy Tarreau185b5c42012-04-26 15:11:51 +02008721unsigned int http_get_hdr(const struct http_msg *msg, const char *hname, int hlen,
Willy Tarreau294c4732011-12-16 21:35:50 +01008722 struct hdr_idx *idx, int occ,
8723 struct hdr_ctx *ctx, char **vptr, int *vlen)
Willy Tarreaubce70882009-09-07 11:51:47 +02008724{
Willy Tarreau294c4732011-12-16 21:35:50 +01008725 struct hdr_ctx local_ctx;
8726 char *ptr_hist[MAX_HDR_HISTORY];
8727 int len_hist[MAX_HDR_HISTORY];
Willy Tarreaubce70882009-09-07 11:51:47 +02008728 unsigned int hist_ptr;
Willy Tarreau294c4732011-12-16 21:35:50 +01008729 int found;
Willy Tarreaubce70882009-09-07 11:51:47 +02008730
Willy Tarreau294c4732011-12-16 21:35:50 +01008731 if (!ctx) {
8732 local_ctx.idx = 0;
8733 ctx = &local_ctx;
8734 }
8735
Willy Tarreaubce70882009-09-07 11:51:47 +02008736 if (occ >= 0) {
Willy Tarreau294c4732011-12-16 21:35:50 +01008737 /* search from the beginning */
Willy Tarreau9b28e032012-10-12 23:49:43 +02008738 while (http_find_header2(hname, hlen, msg->chn->buf->p, idx, ctx)) {
Willy Tarreaubce70882009-09-07 11:51:47 +02008739 occ--;
8740 if (occ <= 0) {
Willy Tarreau294c4732011-12-16 21:35:50 +01008741 *vptr = ctx->line + ctx->val;
8742 *vlen = ctx->vlen;
8743 return 1;
Willy Tarreaubce70882009-09-07 11:51:47 +02008744 }
8745 }
Willy Tarreau294c4732011-12-16 21:35:50 +01008746 return 0;
Willy Tarreaubce70882009-09-07 11:51:47 +02008747 }
8748
8749 /* negative occurrence, we scan all the list then walk back */
8750 if (-occ > MAX_HDR_HISTORY)
8751 return 0;
8752
Willy Tarreau294c4732011-12-16 21:35:50 +01008753 found = hist_ptr = 0;
Willy Tarreau9b28e032012-10-12 23:49:43 +02008754 while (http_find_header2(hname, hlen, msg->chn->buf->p, idx, ctx)) {
Willy Tarreau294c4732011-12-16 21:35:50 +01008755 ptr_hist[hist_ptr] = ctx->line + ctx->val;
8756 len_hist[hist_ptr] = ctx->vlen;
8757 if (++hist_ptr >= MAX_HDR_HISTORY)
Willy Tarreaubce70882009-09-07 11:51:47 +02008758 hist_ptr = 0;
8759 found++;
8760 }
8761 if (-occ > found)
8762 return 0;
8763 /* OK now we have the last occurrence in [hist_ptr-1], and we need to
Willy Tarreau67dad272013-06-12 22:27:44 +02008764 * find occurrence -occ. 0 <= hist_ptr < MAX_HDR_HISTORY, and we have
8765 * -10 <= occ <= -1. So we have to check [hist_ptr%MAX_HDR_HISTORY+occ]
8766 * to remain in the 0..9 range.
Willy Tarreaubce70882009-09-07 11:51:47 +02008767 */
Willy Tarreau67dad272013-06-12 22:27:44 +02008768 hist_ptr += occ + MAX_HDR_HISTORY;
Willy Tarreaubce70882009-09-07 11:51:47 +02008769 if (hist_ptr >= MAX_HDR_HISTORY)
8770 hist_ptr -= MAX_HDR_HISTORY;
Willy Tarreau294c4732011-12-16 21:35:50 +01008771 *vptr = ptr_hist[hist_ptr];
8772 *vlen = len_hist[hist_ptr];
8773 return 1;
Willy Tarreaubce70882009-09-07 11:51:47 +02008774}
8775
Willy Tarreau04ff9f12013-06-10 18:39:42 +02008776/* Return in <vptr> and <vlen> the pointer and length of occurrence <occ> of
8777 * header whose name is <hname> of length <hlen>. If <ctx> is null, lookup is
8778 * performed over the whole headers. Otherwise it must contain a valid header
8779 * context, initialised with ctx->idx=0 for the first lookup in a series. If
8780 * <occ> is positive or null, occurrence #occ from the beginning (or last ctx)
8781 * is returned. Occ #0 and #1 are equivalent. If <occ> is negative (and no less
8782 * than -MAX_HDR_HISTORY), the occurrence is counted from the last one which is
8783 * -1. This function differs from http_get_hdr() in that it only returns full
8784 * line header values and does not stop at commas.
8785 * The return value is 0 if nothing was found, or non-zero otherwise.
8786 */
8787unsigned int http_get_fhdr(const struct http_msg *msg, const char *hname, int hlen,
8788 struct hdr_idx *idx, int occ,
8789 struct hdr_ctx *ctx, char **vptr, int *vlen)
8790{
8791 struct hdr_ctx local_ctx;
8792 char *ptr_hist[MAX_HDR_HISTORY];
8793 int len_hist[MAX_HDR_HISTORY];
8794 unsigned int hist_ptr;
8795 int found;
8796
8797 if (!ctx) {
8798 local_ctx.idx = 0;
8799 ctx = &local_ctx;
8800 }
8801
8802 if (occ >= 0) {
8803 /* search from the beginning */
8804 while (http_find_full_header2(hname, hlen, msg->chn->buf->p, idx, ctx)) {
8805 occ--;
8806 if (occ <= 0) {
8807 *vptr = ctx->line + ctx->val;
8808 *vlen = ctx->vlen;
8809 return 1;
8810 }
8811 }
8812 return 0;
8813 }
8814
8815 /* negative occurrence, we scan all the list then walk back */
8816 if (-occ > MAX_HDR_HISTORY)
8817 return 0;
8818
8819 found = hist_ptr = 0;
8820 while (http_find_full_header2(hname, hlen, msg->chn->buf->p, idx, ctx)) {
8821 ptr_hist[hist_ptr] = ctx->line + ctx->val;
8822 len_hist[hist_ptr] = ctx->vlen;
8823 if (++hist_ptr >= MAX_HDR_HISTORY)
8824 hist_ptr = 0;
8825 found++;
8826 }
8827 if (-occ > found)
8828 return 0;
Nenad Merdanovic69ad4b92016-03-29 13:14:30 +02008829
Willy Tarreau04ff9f12013-06-10 18:39:42 +02008830 /* OK now we have the last occurrence in [hist_ptr-1], and we need to
Nenad Merdanovic69ad4b92016-03-29 13:14:30 +02008831 * find occurrence -occ. 0 <= hist_ptr < MAX_HDR_HISTORY, and we have
8832 * -10 <= occ <= -1. So we have to check [hist_ptr%MAX_HDR_HISTORY+occ]
8833 * to remain in the 0..9 range.
Willy Tarreau04ff9f12013-06-10 18:39:42 +02008834 */
Nenad Merdanovic69ad4b92016-03-29 13:14:30 +02008835 hist_ptr += occ + MAX_HDR_HISTORY;
Willy Tarreau04ff9f12013-06-10 18:39:42 +02008836 if (hist_ptr >= MAX_HDR_HISTORY)
8837 hist_ptr -= MAX_HDR_HISTORY;
8838 *vptr = ptr_hist[hist_ptr];
8839 *vlen = len_hist[hist_ptr];
8840 return 1;
8841}
8842
Willy Tarreaubaaee002006-06-26 02:48:02 +02008843/*
Willy Tarreaue92693a2012-09-24 21:13:39 +02008844 * Print a debug line with a header. Always stop at the first CR or LF char,
8845 * so it is safe to pass it a full buffer if needed. If <err> is not NULL, an
8846 * arrow is printed after the line which contains the pointer.
Willy Tarreau58f10d72006-12-04 02:26:12 +01008847 */
Willy Tarreau87b09662015-04-03 00:22:06 +02008848void debug_hdr(const char *dir, struct stream *s, const char *start, const char *end)
Willy Tarreau58f10d72006-12-04 02:26:12 +01008849{
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02008850 struct session *sess = strm_sess(s);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01008851 int max;
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02008852
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008853 chunk_printf(&trash, "%08x:%s.%s[%04x:%04x]: ", s->uniq_id, s->be->id,
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008854 dir,
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02008855 objt_conn(sess->origin) ? (unsigned short)objt_conn(sess->origin)->t.sock.fd : -1,
Willy Tarreau350f4872014-11-28 14:42:25 +01008856 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 +02008857
8858 for (max = 0; start + max < end; max++)
8859 if (start[max] == '\r' || start[max] == '\n')
8860 break;
8861
Willy Tarreau19d14ef2012-10-29 16:51:55 +01008862 UBOUND(max, trash.size - trash.len - 3);
8863 trash.len += strlcpy2(trash.str + trash.len, start, max + 1);
8864 trash.str[trash.len++] = '\n';
Willy Tarreau89efaed2013-12-13 15:14:55 +01008865 shut_your_big_mouth_gcc(write(1, trash.str, trash.len));
Willy Tarreau58f10d72006-12-04 02:26:12 +01008866}
8867
Willy Tarreaueee5b512015-04-03 23:46:31 +02008868
8869/* Allocate a new HTTP transaction for stream <s> unless there is one already.
8870 * The hdr_idx is allocated as well. In case of allocation failure, everything
8871 * allocated is freed and NULL is returned. Otherwise the new transaction is
8872 * assigned to the stream and returned.
8873 */
8874struct http_txn *http_alloc_txn(struct stream *s)
8875{
8876 struct http_txn *txn = s->txn;
8877
8878 if (txn)
8879 return txn;
8880
8881 txn = pool_alloc2(pool2_http_txn);
8882 if (!txn)
8883 return txn;
8884
8885 txn->hdr_idx.size = global.tune.max_http_hdr;
8886 txn->hdr_idx.v = pool_alloc2(pool2_hdr_idx);
8887 if (!txn->hdr_idx.v) {
8888 pool_free2(pool2_http_txn, txn);
8889 return NULL;
8890 }
8891
8892 s->txn = txn;
8893 return txn;
8894}
8895
Thierry FOURNIERfd50f0b2015-09-25 18:53:18 +02008896void http_txn_reset_req(struct http_txn *txn)
8897{
8898 txn->req.flags = 0;
8899 txn->req.sol = txn->req.eol = txn->req.eoh = 0; /* relative to the buffer */
8900 txn->req.next = 0;
8901 txn->req.chunk_len = 0LL;
8902 txn->req.body_len = 0LL;
8903 txn->req.msg_state = HTTP_MSG_RQBEFORE; /* at the very beginning of the request */
8904}
8905
8906void http_txn_reset_res(struct http_txn *txn)
8907{
8908 txn->rsp.flags = 0;
8909 txn->rsp.sol = txn->rsp.eol = txn->rsp.eoh = 0; /* relative to the buffer */
8910 txn->rsp.next = 0;
8911 txn->rsp.chunk_len = 0LL;
8912 txn->rsp.body_len = 0LL;
8913 txn->rsp.msg_state = HTTP_MSG_RPBEFORE; /* at the very beginning of the response */
8914}
8915
Willy Tarreau0937bc42009-12-22 15:03:09 +01008916/*
Willy Tarreau87b09662015-04-03 00:22:06 +02008917 * Initialize a new HTTP transaction for stream <s>. It is assumed that all
Willy Tarreau0937bc42009-12-22 15:03:09 +01008918 * the required fields are properly allocated and that we only need to (re)init
8919 * them. This should be used before processing any new request.
8920 */
Willy Tarreau87b09662015-04-03 00:22:06 +02008921void http_init_txn(struct stream *s)
Willy Tarreau0937bc42009-12-22 15:03:09 +01008922{
Willy Tarreaueee5b512015-04-03 23:46:31 +02008923 struct http_txn *txn = s->txn;
Willy Tarreaud0d8da92015-04-04 02:10:38 +02008924 struct proxy *fe = strm_fe(s);
Willy Tarreau0937bc42009-12-22 15:03:09 +01008925
8926 txn->flags = 0;
8927 txn->status = -1;
8928
Willy Tarreauf64d1412010-10-07 20:06:11 +02008929 txn->cookie_first_date = 0;
8930 txn->cookie_last_date = 0;
8931
Willy Tarreaueee5b512015-04-03 23:46:31 +02008932 txn->srv_cookie = NULL;
8933 txn->cli_cookie = NULL;
8934 txn->uri = NULL;
8935
Thierry FOURNIERfd50f0b2015-09-25 18:53:18 +02008936 http_txn_reset_req(txn);
8937 http_txn_reset_res(txn);
8938
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01008939 txn->req.chn = &s->req;
8940 txn->rsp.chn = &s->res;
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01008941
8942 txn->auth.method = HTTP_AUTH_UNKNOWN;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008943
8944 txn->req.err_pos = txn->rsp.err_pos = -2; /* block buggy requests/responses */
8945 if (fe->options2 & PR_O2_REQBUG_OK)
8946 txn->req.err_pos = -1; /* let buggy requests pass */
8947
Willy Tarreau0937bc42009-12-22 15:03:09 +01008948 if (txn->hdr_idx.v)
8949 hdr_idx_init(&txn->hdr_idx);
Thierry FOURNIER4834bc72015-06-06 19:29:07 +02008950
8951 vars_init(&s->vars_txn, SCOPE_TXN);
8952 vars_init(&s->vars_reqres, SCOPE_REQ);
Willy Tarreau0937bc42009-12-22 15:03:09 +01008953}
8954
8955/* to be used at the end of a transaction */
Willy Tarreau87b09662015-04-03 00:22:06 +02008956void http_end_txn(struct stream *s)
Willy Tarreau0937bc42009-12-22 15:03:09 +01008957{
Willy Tarreaueee5b512015-04-03 23:46:31 +02008958 struct http_txn *txn = s->txn;
Willy Tarreaud0d8da92015-04-04 02:10:38 +02008959 struct proxy *fe = strm_fe(s);
Willy Tarreau0937bc42009-12-22 15:03:09 +01008960
8961 /* these ones will have been dynamically allocated */
8962 pool_free2(pool2_requri, txn->uri);
8963 pool_free2(pool2_capture, txn->cli_cookie);
8964 pool_free2(pool2_capture, txn->srv_cookie);
William Lallemanda73203e2012-03-12 12:48:57 +01008965 pool_free2(pool2_uniqueid, s->unique_id);
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01008966
William Lallemanda73203e2012-03-12 12:48:57 +01008967 s->unique_id = NULL;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008968 txn->uri = NULL;
8969 txn->srv_cookie = NULL;
8970 txn->cli_cookie = NULL;
Willy Tarreau46023632010-01-07 22:51:47 +01008971
Willy Tarreaucb7dd012015-04-03 22:16:32 +02008972 if (s->req_cap) {
Willy Tarreau46023632010-01-07 22:51:47 +01008973 struct cap_hdr *h;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02008974 for (h = fe->req_cap; h; h = h->next)
Willy Tarreaucb7dd012015-04-03 22:16:32 +02008975 pool_free2(h->pool, s->req_cap[h->index]);
8976 memset(s->req_cap, 0, fe->nb_req_cap * sizeof(void *));
Willy Tarreau46023632010-01-07 22:51:47 +01008977 }
8978
Willy Tarreaucb7dd012015-04-03 22:16:32 +02008979 if (s->res_cap) {
Willy Tarreau46023632010-01-07 22:51:47 +01008980 struct cap_hdr *h;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02008981 for (h = fe->rsp_cap; h; h = h->next)
Willy Tarreaucb7dd012015-04-03 22:16:32 +02008982 pool_free2(h->pool, s->res_cap[h->index]);
8983 memset(s->res_cap, 0, fe->nb_rsp_cap * sizeof(void *));
Willy Tarreau46023632010-01-07 22:51:47 +01008984 }
8985
Willy Tarreau6204cd92016-03-10 16:33:04 +01008986 vars_prune(&s->vars_txn, s->sess, s);
8987 vars_prune(&s->vars_reqres, s->sess, s);
Willy Tarreau0937bc42009-12-22 15:03:09 +01008988}
8989
8990/* to be used at the end of a transaction to prepare a new one */
Willy Tarreau87b09662015-04-03 00:22:06 +02008991void http_reset_txn(struct stream *s)
Willy Tarreau0937bc42009-12-22 15:03:09 +01008992{
8993 http_end_txn(s);
8994 http_init_txn(s);
8995
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01008996 /* reinitialise the current rule list pointer to NULL. We are sure that
8997 * any rulelist match the NULL pointer.
8998 */
8999 s->current_rule_list = NULL;
9000
Willy Tarreaud0d8da92015-04-04 02:10:38 +02009001 s->be = strm_fe(s);
9002 s->logs.logwait = strm_fe(s)->to_log;
Willy Tarreauabcd5142013-06-11 17:18:02 +02009003 s->logs.level = 0;
Willy Tarreau87b09662015-04-03 00:22:06 +02009004 stream_del_srv_conn(s);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01009005 s->target = NULL;
Emeric Brunb982a3d2010-01-04 15:45:53 +01009006 /* re-init store persistence */
9007 s->store_count = 0;
Willy Tarreau1f0da242014-01-25 11:01:50 +01009008 s->uniq_id = global.req_count++;
Emeric Brunb982a3d2010-01-04 15:45:53 +01009009
Willy Tarreau0937bc42009-12-22 15:03:09 +01009010 s->pend_pos = NULL;
Willy Tarreau0937bc42009-12-22 15:03:09 +01009011
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01009012 s->req.flags |= CF_READ_DONTWAIT; /* one read is usually enough */
Willy Tarreau0937bc42009-12-22 15:03:09 +01009013
Willy Tarreau739cfba2010-01-25 23:11:14 +01009014 /* We must trim any excess data from the response buffer, because we
9015 * may have blocked an invalid response from a server that we don't
9016 * want to accidentely forward once we disable the analysers, nor do
9017 * we want those data to come along with next response. A typical
9018 * example of such data would be from a buggy server responding to
9019 * a HEAD with some data, or sending more than the advertised
9020 * content-length.
9021 */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01009022 if (unlikely(s->res.buf->i))
9023 s->res.buf->i = 0;
Willy Tarreau739cfba2010-01-25 23:11:14 +01009024
Willy Tarreaud0d8da92015-04-04 02:10:38 +02009025 s->req.rto = strm_fe(s)->timeout.client;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01009026 s->req.wto = TICK_ETERNITY;
Willy Tarreau0937bc42009-12-22 15:03:09 +01009027
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01009028 s->res.rto = TICK_ETERNITY;
Willy Tarreaud0d8da92015-04-04 02:10:38 +02009029 s->res.wto = strm_fe(s)->timeout.client;
Willy Tarreau0937bc42009-12-22 15:03:09 +01009030
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01009031 s->req.rex = TICK_ETERNITY;
9032 s->req.wex = TICK_ETERNITY;
9033 s->req.analyse_exp = TICK_ETERNITY;
9034 s->res.rex = TICK_ETERNITY;
9035 s->res.wex = TICK_ETERNITY;
9036 s->res.analyse_exp = TICK_ETERNITY;
Willy Tarreau0937bc42009-12-22 15:03:09 +01009037}
Willy Tarreau58f10d72006-12-04 02:26:12 +01009038
Sasha Pachev218f0642014-06-16 12:05:59 -06009039void free_http_res_rules(struct list *r)
9040{
Thierry FOURNIERa28a9422015-08-04 19:35:46 +02009041 struct act_rule *tr, *pr;
Sasha Pachev218f0642014-06-16 12:05:59 -06009042
9043 list_for_each_entry_safe(pr, tr, r, list) {
9044 LIST_DEL(&pr->list);
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02009045 regex_free(&pr->arg.hdr_add.re);
Sasha Pachev218f0642014-06-16 12:05:59 -06009046 free(pr);
9047 }
9048}
9049
9050void free_http_req_rules(struct list *r)
9051{
Thierry FOURNIERa28a9422015-08-04 19:35:46 +02009052 struct act_rule *tr, *pr;
Willy Tarreauff011f22011-01-06 17:51:27 +01009053
9054 list_for_each_entry_safe(pr, tr, r, list) {
9055 LIST_DEL(&pr->list);
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009056 if (pr->action == ACT_HTTP_REQ_AUTH)
Willy Tarreau5c2e1982012-12-24 12:00:25 +01009057 free(pr->arg.auth.realm);
Willy Tarreauff011f22011-01-06 17:51:27 +01009058
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02009059 regex_free(&pr->arg.hdr_add.re);
Willy Tarreauff011f22011-01-06 17:51:27 +01009060 free(pr);
9061 }
9062}
9063
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009064/* parse an "http-request" rule */
Thierry FOURNIERa28a9422015-08-04 19:35:46 +02009065struct act_rule *parse_http_req_cond(const char **args, const char *file, int linenum, struct proxy *proxy)
Willy Tarreauff011f22011-01-06 17:51:27 +01009066{
Thierry FOURNIERa28a9422015-08-04 19:35:46 +02009067 struct act_rule *rule;
Thierry FOURNIER36481b82015-08-19 09:01:53 +02009068 struct action_kw *custom = NULL;
Willy Tarreauff011f22011-01-06 17:51:27 +01009069 int cur_arg;
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02009070 char *error;
Willy Tarreauff011f22011-01-06 17:51:27 +01009071
Vincent Bernat02779b62016-04-03 13:48:43 +02009072 rule = calloc(1, sizeof(*rule));
Willy Tarreauff011f22011-01-06 17:51:27 +01009073 if (!rule) {
9074 Alert("parsing [%s:%d]: out of memory.\n", file, linenum);
Willy Tarreau81499eb2012-12-27 12:19:02 +01009075 goto out_err;
Willy Tarreauff011f22011-01-06 17:51:27 +01009076 }
9077
CJ Ess108b1dd2015-04-07 12:03:37 -04009078 rule->deny_status = HTTP_ERR_403;
Willy Tarreau5c2e1982012-12-24 12:00:25 +01009079 if (!strcmp(args[0], "allow")) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009080 rule->action = ACT_ACTION_ALLOW;
Willy Tarreauff011f22011-01-06 17:51:27 +01009081 cur_arg = 1;
Willy Tarreau5bd67592014-04-28 22:00:46 +02009082 } else if (!strcmp(args[0], "deny") || !strcmp(args[0], "block")) {
CJ Ess108b1dd2015-04-07 12:03:37 -04009083 int code;
9084 int hc;
9085
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009086 rule->action = ACT_ACTION_DENY;
Willy Tarreauff011f22011-01-06 17:51:27 +01009087 cur_arg = 1;
CJ Ess108b1dd2015-04-07 12:03:37 -04009088 if (strcmp(args[cur_arg], "deny_status") == 0) {
9089 cur_arg++;
9090 if (!args[cur_arg]) {
9091 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : missing status code.\n",
9092 file, linenum, proxy_type_str(proxy), proxy->id, args[0]);
9093 goto out_err;
9094 }
9095
9096 code = atol(args[cur_arg]);
9097 cur_arg++;
9098 for (hc = 0; hc < HTTP_ERR_SIZE; hc++) {
9099 if (http_err_codes[hc] == code) {
9100 rule->deny_status = hc;
9101 break;
9102 }
9103 }
9104
9105 if (hc >= HTTP_ERR_SIZE) {
9106 Warning("parsing [%s:%d] : status code %d not handled, using default code 403.\n",
9107 file, linenum, code);
9108 }
9109 }
Willy Tarreauccbcc372012-12-27 12:37:57 +01009110 } else if (!strcmp(args[0], "tarpit")) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009111 rule->action = ACT_HTTP_REQ_TARPIT;
Willy Tarreauccbcc372012-12-27 12:37:57 +01009112 cur_arg = 1;
Willy Tarreauff011f22011-01-06 17:51:27 +01009113 } else if (!strcmp(args[0], "auth")) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009114 rule->action = ACT_HTTP_REQ_AUTH;
Willy Tarreauff011f22011-01-06 17:51:27 +01009115 cur_arg = 1;
9116
9117 while(*args[cur_arg]) {
9118 if (!strcmp(args[cur_arg], "realm")) {
Willy Tarreau5c2e1982012-12-24 12:00:25 +01009119 rule->arg.auth.realm = strdup(args[cur_arg + 1]);
Willy Tarreauff011f22011-01-06 17:51:27 +01009120 cur_arg+=2;
9121 continue;
9122 } else
9123 break;
9124 }
Willy Tarreauf4c43c12013-06-11 17:01:13 +02009125 } else if (!strcmp(args[0], "set-nice")) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009126 rule->action = ACT_HTTP_SET_NICE;
Willy Tarreauf4c43c12013-06-11 17:01:13 +02009127 cur_arg = 1;
9128
9129 if (!*args[cur_arg] ||
9130 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
9131 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (integer value).\n",
9132 file, linenum, args[0]);
9133 goto out_err;
9134 }
9135 rule->arg.nice = atoi(args[cur_arg]);
9136 if (rule->arg.nice < -1024)
9137 rule->arg.nice = -1024;
9138 else if (rule->arg.nice > 1024)
9139 rule->arg.nice = 1024;
9140 cur_arg++;
Willy Tarreau42cf39e2013-06-11 18:51:32 +02009141 } else if (!strcmp(args[0], "set-tos")) {
9142#ifdef IP_TOS
9143 char *err;
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009144 rule->action = ACT_HTTP_SET_TOS;
Willy Tarreau42cf39e2013-06-11 18:51:32 +02009145 cur_arg = 1;
9146
9147 if (!*args[cur_arg] ||
9148 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
9149 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (integer/hex value).\n",
9150 file, linenum, args[0]);
9151 goto out_err;
9152 }
9153
9154 rule->arg.tos = strtol(args[cur_arg], &err, 0);
9155 if (err && *err != '\0') {
9156 Alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-request %s' (integer/hex value expected).\n",
9157 file, linenum, err, args[0]);
9158 goto out_err;
9159 }
9160 cur_arg++;
9161#else
9162 Alert("parsing [%s:%d]: 'http-request %s' is not supported on this platform (IP_TOS undefined).\n", file, linenum, args[0]);
9163 goto out_err;
9164#endif
Willy Tarreau51347ed2013-06-11 19:34:13 +02009165 } else if (!strcmp(args[0], "set-mark")) {
9166#ifdef SO_MARK
9167 char *err;
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009168 rule->action = ACT_HTTP_SET_MARK;
Willy Tarreau51347ed2013-06-11 19:34:13 +02009169 cur_arg = 1;
9170
9171 if (!*args[cur_arg] ||
9172 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
9173 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (integer/hex value).\n",
9174 file, linenum, args[0]);
9175 goto out_err;
9176 }
9177
9178 rule->arg.mark = strtoul(args[cur_arg], &err, 0);
9179 if (err && *err != '\0') {
9180 Alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-request %s' (integer/hex value expected).\n",
9181 file, linenum, err, args[0]);
9182 goto out_err;
9183 }
9184 cur_arg++;
9185 global.last_checks |= LSTCHK_NETADM;
9186#else
9187 Alert("parsing [%s:%d]: 'http-request %s' is not supported on this platform (SO_MARK undefined).\n", file, linenum, args[0]);
9188 goto out_err;
9189#endif
Willy Tarreau9a355ec2013-06-11 17:45:46 +02009190 } else if (!strcmp(args[0], "set-log-level")) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009191 rule->action = ACT_HTTP_SET_LOGL;
Willy Tarreau9a355ec2013-06-11 17:45:46 +02009192 cur_arg = 1;
9193
9194 if (!*args[cur_arg] ||
9195 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
9196 bad_log_level:
9197 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (log level name or 'silent').\n",
9198 file, linenum, args[0]);
9199 goto out_err;
9200 }
9201 if (strcmp(args[cur_arg], "silent") == 0)
9202 rule->arg.loglevel = -1;
9203 else if ((rule->arg.loglevel = get_log_level(args[cur_arg]) + 1) == 0)
9204 goto bad_log_level;
9205 cur_arg++;
Willy Tarreau20b0de52012-12-24 15:45:22 +01009206 } else if (strcmp(args[0], "add-header") == 0 || strcmp(args[0], "set-header") == 0) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009207 rule->action = *args[0] == 'a' ? ACT_HTTP_ADD_HDR : ACT_HTTP_SET_HDR;
Willy Tarreau20b0de52012-12-24 15:45:22 +01009208 cur_arg = 1;
9209
Willy Tarreau8d1c5162013-04-03 14:13:58 +02009210 if (!*args[cur_arg] || !*args[cur_arg+1] ||
9211 (*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 +01009212 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 2 arguments.\n",
9213 file, linenum, args[0]);
Willy Tarreau81499eb2012-12-27 12:19:02 +01009214 goto out_err;
Willy Tarreau20b0de52012-12-24 15:45:22 +01009215 }
9216
9217 rule->arg.hdr_add.name = strdup(args[cur_arg]);
9218 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
9219 LIST_INIT(&rule->arg.hdr_add.fmt);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02009220
Thierry FOURNIER1c0054f2013-11-20 15:09:52 +01009221 proxy->conf.args.ctx = ARGC_HRQ;
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01009222 error = NULL;
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01009223 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 +01009224 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR, &error)) {
9225 Alert("parsing [%s:%d]: 'http-request %s': %s.\n",
9226 file, linenum, args[0], error);
9227 free(error);
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01009228 goto out_err;
9229 }
Willy Tarreau59ad1a22014-01-29 14:39:58 +01009230 free(proxy->conf.lfs_file);
9231 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9232 proxy->conf.lfs_line = proxy->conf.args.line;
Willy Tarreau20b0de52012-12-24 15:45:22 +01009233 cur_arg += 2;
Willy Tarreaub8543922014-06-17 18:58:26 +02009234 } else if (strcmp(args[0], "replace-header") == 0 || strcmp(args[0], "replace-value") == 0) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009235 rule->action = args[0][8] == 'h' ? ACT_HTTP_REPLACE_HDR : ACT_HTTP_REPLACE_VAL;
Sasha Pachev218f0642014-06-16 12:05:59 -06009236 cur_arg = 1;
9237
9238 if (!*args[cur_arg] || !*args[cur_arg+1] || !*args[cur_arg+2] ||
Baptiste Assmann92df3702014-06-24 11:10:00 +02009239 (*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 -06009240 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 3 arguments.\n",
9241 file, linenum, args[0]);
9242 goto out_err;
9243 }
9244
9245 rule->arg.hdr_add.name = strdup(args[cur_arg]);
9246 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
9247 LIST_INIT(&rule->arg.hdr_add.fmt);
9248
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02009249 error = NULL;
9250 if (!regex_comp(args[cur_arg + 1], &rule->arg.hdr_add.re, 1, 1, &error)) {
9251 Alert("parsing [%s:%d] : '%s' : %s.\n", file, linenum,
9252 args[cur_arg + 1], error);
9253 free(error);
Sasha Pachev218f0642014-06-16 12:05:59 -06009254 goto out_err;
9255 }
9256
9257 proxy->conf.args.ctx = ARGC_HRQ;
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01009258 error = NULL;
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01009259 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 +01009260 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR, &error)) {
9261 Alert("parsing [%s:%d]: 'http-request %s': %s.\n",
9262 file, linenum, args[0], error);
9263 free(error);
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01009264 goto out_err;
9265 }
Sasha Pachev218f0642014-06-16 12:05:59 -06009266
9267 free(proxy->conf.lfs_file);
9268 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9269 proxy->conf.lfs_line = proxy->conf.args.line;
9270 cur_arg += 3;
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02009271 } else if (strcmp(args[0], "del-header") == 0) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009272 rule->action = ACT_HTTP_DEL_HDR;
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02009273 cur_arg = 1;
9274
9275 if (!*args[cur_arg] ||
9276 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
9277 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument.\n",
9278 file, linenum, args[0]);
9279 goto out_err;
9280 }
9281
9282 rule->arg.hdr_add.name = strdup(args[cur_arg]);
9283 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
9284
9285 proxy->conf.args.ctx = ARGC_HRQ;
9286 free(proxy->conf.lfs_file);
9287 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9288 proxy->conf.lfs_line = proxy->conf.args.line;
9289 cur_arg += 1;
Willy Tarreau09448f72014-06-25 18:12:15 +02009290 } else if (strncmp(args[0], "track-sc", 8) == 0 &&
9291 args[0][9] == '\0' && args[0][8] >= '0' &&
Willy Tarreaue1cfc1f2014-10-17 11:53:05 +02009292 args[0][8] < '0' + MAX_SESS_STKCTR) { /* track-sc 0..9 */
Willy Tarreau09448f72014-06-25 18:12:15 +02009293 struct sample_expr *expr;
9294 unsigned int where;
9295 char *err = NULL;
9296
9297 cur_arg = 1;
9298 proxy->conf.args.ctx = ARGC_TRK;
9299
9300 expr = sample_parse_expr((char **)args, &cur_arg, file, linenum, &err, &proxy->conf.args);
9301 if (!expr) {
9302 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : %s.\n",
9303 file, linenum, proxy_type_str(proxy), proxy->id, args[0], err);
9304 free(err);
9305 goto out_err;
9306 }
9307
9308 where = 0;
9309 if (proxy->cap & PR_CAP_FE)
9310 where |= SMP_VAL_FE_HRQ_HDR;
9311 if (proxy->cap & PR_CAP_BE)
9312 where |= SMP_VAL_BE_HRQ_HDR;
9313
9314 if (!(expr->fetch->val & where)) {
9315 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule :"
9316 " fetch method '%s' extracts information from '%s', none of which is available here.\n",
9317 file, linenum, proxy_type_str(proxy), proxy->id, args[0],
9318 args[cur_arg-1], sample_src_names(expr->fetch->use));
9319 free(expr);
9320 goto out_err;
9321 }
9322
9323 if (strcmp(args[cur_arg], "table") == 0) {
9324 cur_arg++;
9325 if (!args[cur_arg]) {
9326 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : missing table name.\n",
9327 file, linenum, proxy_type_str(proxy), proxy->id, args[0]);
9328 free(expr);
9329 goto out_err;
9330 }
9331 /* we copy the table name for now, it will be resolved later */
Thierry FOURNIER5ec63e02015-08-04 09:09:48 +02009332 rule->arg.trk_ctr.table.n = strdup(args[cur_arg]);
Willy Tarreau09448f72014-06-25 18:12:15 +02009333 cur_arg++;
9334 }
Thierry FOURNIER5ec63e02015-08-04 09:09:48 +02009335 rule->arg.trk_ctr.expr = expr;
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009336 rule->action = ACT_ACTION_TRK_SC0 + args[0][8] - '0';
Willy Tarreau81499eb2012-12-27 12:19:02 +01009337 } else if (strcmp(args[0], "redirect") == 0) {
9338 struct redirect_rule *redir;
Willy Tarreau6d4890c2013-11-18 18:04:25 +01009339 char *errmsg = NULL;
Willy Tarreau81499eb2012-12-27 12:19:02 +01009340
Willy Tarreaube4653b2015-05-28 15:26:58 +02009341 if ((redir = http_parse_redirect_rule(file, linenum, proxy, (const char **)args + 1, &errmsg, 1, 0)) == NULL) {
Willy Tarreau81499eb2012-12-27 12:19:02 +01009342 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : %s.\n",
9343 file, linenum, proxy_type_str(proxy), proxy->id, args[0], errmsg);
9344 goto out_err;
9345 }
9346
9347 /* this redirect rule might already contain a parsed condition which
9348 * we'll pass to the http-request rule.
9349 */
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009350 rule->action = ACT_HTTP_REDIR;
Willy Tarreau81499eb2012-12-27 12:19:02 +01009351 rule->arg.redir = redir;
9352 rule->cond = redir->cond;
9353 redir->cond = NULL;
9354 cur_arg = 2;
9355 return rule;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009356 } else if (strncmp(args[0], "add-acl", 7) == 0) {
9357 /* http-request add-acl(<reference (acl name)>) <key pattern> */
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009358 rule->action = ACT_HTTP_ADD_ACL;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009359 /*
9360 * '+ 8' for 'add-acl('
9361 * '- 9' for 'add-acl(' + trailing ')'
9362 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02009363 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009364
9365 cur_arg = 1;
9366
9367 if (!*args[cur_arg] ||
9368 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
9369 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument.\n",
9370 file, linenum, args[0]);
9371 goto out_err;
9372 }
9373
9374 LIST_INIT(&rule->arg.map.key);
9375 proxy->conf.args.ctx = ARGC_HRQ;
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01009376 error = NULL;
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01009377 if (!parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01009378 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR, &error)) {
9379 Alert("parsing [%s:%d]: 'http-request %s': %s.\n",
9380 file, linenum, args[0], error);
9381 free(error);
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01009382 goto out_err;
9383 }
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009384 free(proxy->conf.lfs_file);
9385 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9386 proxy->conf.lfs_line = proxy->conf.args.line;
9387 cur_arg += 1;
9388 } else if (strncmp(args[0], "del-acl", 7) == 0) {
9389 /* http-request del-acl(<reference (acl name)>) <key pattern> */
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009390 rule->action = ACT_HTTP_DEL_ACL;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009391 /*
9392 * '+ 8' for 'del-acl('
9393 * '- 9' for 'del-acl(' + trailing ')'
9394 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02009395 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009396
9397 cur_arg = 1;
9398
9399 if (!*args[cur_arg] ||
9400 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
9401 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument.\n",
9402 file, linenum, args[0]);
9403 goto out_err;
9404 }
9405
9406 LIST_INIT(&rule->arg.map.key);
9407 proxy->conf.args.ctx = ARGC_HRQ;
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01009408 error = NULL;
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01009409 if (!parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01009410 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR, &error)) {
9411 Alert("parsing [%s:%d]: 'http-request %s': %s.\n",
9412 file, linenum, args[0], error);
9413 free(error);
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01009414 goto out_err;
9415 }
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009416 free(proxy->conf.lfs_file);
9417 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9418 proxy->conf.lfs_line = proxy->conf.args.line;
9419 cur_arg += 1;
9420 } else if (strncmp(args[0], "del-map", 7) == 0) {
9421 /* http-request del-map(<reference (map name)>) <key pattern> */
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009422 rule->action = ACT_HTTP_DEL_MAP;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009423 /*
9424 * '+ 8' for 'del-map('
9425 * '- 9' for 'del-map(' + trailing ')'
9426 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02009427 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009428
9429 cur_arg = 1;
9430
9431 if (!*args[cur_arg] ||
9432 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
9433 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument.\n",
9434 file, linenum, args[0]);
9435 goto out_err;
9436 }
9437
9438 LIST_INIT(&rule->arg.map.key);
9439 proxy->conf.args.ctx = ARGC_HRQ;
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01009440 error = NULL;
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01009441 if (!parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01009442 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR, &error)) {
9443 Alert("parsing [%s:%d]: 'http-request %s': %s.\n",
9444 file, linenum, args[0], error);
9445 free(error);
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01009446 goto out_err;
9447 }
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009448 free(proxy->conf.lfs_file);
9449 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9450 proxy->conf.lfs_line = proxy->conf.args.line;
9451 cur_arg += 1;
9452 } else if (strncmp(args[0], "set-map", 7) == 0) {
9453 /* http-request set-map(<reference (map name)>) <key pattern> <value pattern> */
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009454 rule->action = ACT_HTTP_SET_MAP;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009455 /*
9456 * '+ 8' for 'set-map('
9457 * '- 9' for 'set-map(' + trailing ')'
9458 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02009459 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009460
9461 cur_arg = 1;
9462
9463 if (!*args[cur_arg] || !*args[cur_arg+1] ||
9464 (*args[cur_arg+2] && strcmp(args[cur_arg+2], "if") != 0 && strcmp(args[cur_arg+2], "unless") != 0)) {
9465 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 2 arguments.\n",
9466 file, linenum, args[0]);
9467 goto out_err;
9468 }
9469
9470 LIST_INIT(&rule->arg.map.key);
9471 LIST_INIT(&rule->arg.map.value);
9472 proxy->conf.args.ctx = ARGC_HRQ;
9473
9474 /* key pattern */
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01009475 error = NULL;
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01009476 if (!parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01009477 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR, &error)) {
9478 Alert("parsing [%s:%d]: 'http-request %s' key: %s.\n",
9479 file, linenum, args[0], error);
9480 free(error);
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01009481 goto out_err;
9482 }
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009483
9484 /* value pattern */
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01009485 error = NULL;
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01009486 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 +01009487 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR, &error)) {
9488 Alert("parsing [%s:%d]: 'http-request %s' pattern: %s.\n",
9489 file, linenum, args[0], error);
9490 free(error);
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01009491 goto out_err;
9492 }
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009493 free(proxy->conf.lfs_file);
9494 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9495 proxy->conf.lfs_line = proxy->conf.args.line;
9496
9497 cur_arg += 2;
William Lallemand73025dd2014-04-24 14:38:37 +02009498 } else if (((custom = action_http_req_custom(args[0])) != NULL)) {
9499 char *errmsg = NULL;
9500 cur_arg = 1;
9501 /* try in the module list */
Thierry FOURNIER5563e4b2015-08-14 19:20:07 +02009502 rule->from = ACT_F_HTTP_REQ;
Thierry FOURNIER85c6c972015-09-22 19:14:35 +02009503 rule->kw = custom;
Thierry FOURNIERafa80492015-08-19 09:04:15 +02009504 if (custom->parse(args, &cur_arg, proxy, rule, &errmsg) == ACT_RET_PRS_ERR) {
William Lallemand73025dd2014-04-24 14:38:37 +02009505 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : %s.\n",
9506 file, linenum, proxy_type_str(proxy), proxy->id, args[0], errmsg);
9507 free(errmsg);
9508 goto out_err;
9509 }
Willy Tarreauff011f22011-01-06 17:51:27 +01009510 } else {
Thierry FOURNIERab95e652015-10-02 08:24:51 +02009511 action_build_list(&http_req_keywords.list, &trash);
9512 Alert("parsing [%s:%d]: 'http-request' expects 'allow', 'deny', 'auth', 'redirect', "
9513 "'tarpit', 'add-header', 'set-header', 'replace-header', 'replace-value', 'set-nice', "
Ruoshan Huange4edc6b2016-07-14 15:07:45 +08009514 "'set-tos', 'set-mark', 'set-log-level', 'add-acl', 'del-acl', 'del-map', 'set-map', 'track-sc*'"
William Lallemand2e785f22016-05-25 01:48:42 +02009515 "%s%s, but got '%s'%s.\n",
Thierry FOURNIERab95e652015-10-02 08:24:51 +02009516 file, linenum, *trash.str ? ", " : "", trash.str, args[0], *args[0] ? "" : " (missing argument)");
Willy Tarreau81499eb2012-12-27 12:19:02 +01009517 goto out_err;
Willy Tarreauff011f22011-01-06 17:51:27 +01009518 }
9519
9520 if (strcmp(args[cur_arg], "if") == 0 || strcmp(args[cur_arg], "unless") == 0) {
9521 struct acl_cond *cond;
Willy Tarreaub7451bb2012-04-27 12:38:15 +02009522 char *errmsg = NULL;
Willy Tarreauff011f22011-01-06 17:51:27 +01009523
Willy Tarreaub7451bb2012-04-27 12:38:15 +02009524 if ((cond = build_acl_cond(file, linenum, proxy, args+cur_arg, &errmsg)) == NULL) {
9525 Alert("parsing [%s:%d] : error detected while parsing an 'http-request %s' condition : %s.\n",
9526 file, linenum, args[0], errmsg);
9527 free(errmsg);
Willy Tarreau81499eb2012-12-27 12:19:02 +01009528 goto out_err;
Willy Tarreauff011f22011-01-06 17:51:27 +01009529 }
9530 rule->cond = cond;
9531 }
9532 else if (*args[cur_arg]) {
9533 Alert("parsing [%s:%d]: 'http-request %s' expects 'realm' for 'auth' or"
9534 " either 'if' or 'unless' followed by a condition but found '%s'.\n",
9535 file, linenum, args[0], args[cur_arg]);
Willy Tarreau81499eb2012-12-27 12:19:02 +01009536 goto out_err;
Willy Tarreauff011f22011-01-06 17:51:27 +01009537 }
9538
9539 return rule;
Willy Tarreau81499eb2012-12-27 12:19:02 +01009540 out_err:
9541 free(rule);
9542 return NULL;
Willy Tarreauff011f22011-01-06 17:51:27 +01009543}
9544
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009545/* parse an "http-respose" rule */
Thierry FOURNIERa28a9422015-08-04 19:35:46 +02009546struct act_rule *parse_http_res_cond(const char **args, const char *file, int linenum, struct proxy *proxy)
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009547{
Thierry FOURNIERa28a9422015-08-04 19:35:46 +02009548 struct act_rule *rule;
Thierry FOURNIER36481b82015-08-19 09:01:53 +02009549 struct action_kw *custom = NULL;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009550 int cur_arg;
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02009551 char *error;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009552
9553 rule = calloc(1, sizeof(*rule));
9554 if (!rule) {
9555 Alert("parsing [%s:%d]: out of memory.\n", file, linenum);
9556 goto out_err;
9557 }
9558
9559 if (!strcmp(args[0], "allow")) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009560 rule->action = ACT_ACTION_ALLOW;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009561 cur_arg = 1;
9562 } else if (!strcmp(args[0], "deny")) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009563 rule->action = ACT_ACTION_DENY;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009564 cur_arg = 1;
Willy Tarreauf4c43c12013-06-11 17:01:13 +02009565 } else if (!strcmp(args[0], "set-nice")) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009566 rule->action = ACT_HTTP_SET_NICE;
Willy Tarreauf4c43c12013-06-11 17:01:13 +02009567 cur_arg = 1;
9568
9569 if (!*args[cur_arg] ||
9570 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
9571 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (integer value).\n",
9572 file, linenum, args[0]);
9573 goto out_err;
9574 }
9575 rule->arg.nice = atoi(args[cur_arg]);
9576 if (rule->arg.nice < -1024)
9577 rule->arg.nice = -1024;
9578 else if (rule->arg.nice > 1024)
9579 rule->arg.nice = 1024;
9580 cur_arg++;
Willy Tarreau42cf39e2013-06-11 18:51:32 +02009581 } else if (!strcmp(args[0], "set-tos")) {
9582#ifdef IP_TOS
9583 char *err;
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009584 rule->action = ACT_HTTP_SET_TOS;
Willy Tarreau42cf39e2013-06-11 18:51:32 +02009585 cur_arg = 1;
9586
9587 if (!*args[cur_arg] ||
9588 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
9589 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (integer/hex value).\n",
9590 file, linenum, args[0]);
9591 goto out_err;
9592 }
9593
9594 rule->arg.tos = strtol(args[cur_arg], &err, 0);
9595 if (err && *err != '\0') {
9596 Alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-response %s' (integer/hex value expected).\n",
9597 file, linenum, err, args[0]);
9598 goto out_err;
9599 }
9600 cur_arg++;
9601#else
9602 Alert("parsing [%s:%d]: 'http-response %s' is not supported on this platform (IP_TOS undefined).\n", file, linenum, args[0]);
9603 goto out_err;
9604#endif
Willy Tarreau51347ed2013-06-11 19:34:13 +02009605 } else if (!strcmp(args[0], "set-mark")) {
9606#ifdef SO_MARK
9607 char *err;
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009608 rule->action = ACT_HTTP_SET_MARK;
Willy Tarreau51347ed2013-06-11 19:34:13 +02009609 cur_arg = 1;
9610
9611 if (!*args[cur_arg] ||
9612 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
9613 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (integer/hex value).\n",
9614 file, linenum, args[0]);
9615 goto out_err;
9616 }
9617
9618 rule->arg.mark = strtoul(args[cur_arg], &err, 0);
9619 if (err && *err != '\0') {
9620 Alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-response %s' (integer/hex value expected).\n",
9621 file, linenum, err, args[0]);
9622 goto out_err;
9623 }
9624 cur_arg++;
9625 global.last_checks |= LSTCHK_NETADM;
9626#else
9627 Alert("parsing [%s:%d]: 'http-response %s' is not supported on this platform (SO_MARK undefined).\n", file, linenum, args[0]);
9628 goto out_err;
9629#endif
Willy Tarreau9a355ec2013-06-11 17:45:46 +02009630 } else if (!strcmp(args[0], "set-log-level")) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009631 rule->action = ACT_HTTP_SET_LOGL;
Willy Tarreau9a355ec2013-06-11 17:45:46 +02009632 cur_arg = 1;
9633
9634 if (!*args[cur_arg] ||
9635 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
9636 bad_log_level:
9637 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (log level name or 'silent').\n",
9638 file, linenum, args[0]);
9639 goto out_err;
9640 }
9641 if (strcmp(args[cur_arg], "silent") == 0)
9642 rule->arg.loglevel = -1;
Ruoshan Huangdd016782016-06-15 22:16:03 +08009643 else if ((rule->arg.loglevel = get_log_level(args[cur_arg]) + 1) == 0)
Willy Tarreau9a355ec2013-06-11 17:45:46 +02009644 goto bad_log_level;
9645 cur_arg++;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009646 } else if (strcmp(args[0], "add-header") == 0 || strcmp(args[0], "set-header") == 0) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009647 rule->action = *args[0] == 'a' ? ACT_HTTP_ADD_HDR : ACT_HTTP_SET_HDR;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009648 cur_arg = 1;
9649
9650 if (!*args[cur_arg] || !*args[cur_arg+1] ||
9651 (*args[cur_arg+2] && strcmp(args[cur_arg+2], "if") != 0 && strcmp(args[cur_arg+2], "unless") != 0)) {
9652 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 2 arguments.\n",
9653 file, linenum, args[0]);
9654 goto out_err;
9655 }
9656
9657 rule->arg.hdr_add.name = strdup(args[cur_arg]);
9658 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
9659 LIST_INIT(&rule->arg.hdr_add.fmt);
9660
Thierry FOURNIER1c0054f2013-11-20 15:09:52 +01009661 proxy->conf.args.ctx = ARGC_HRS;
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01009662 error = NULL;
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01009663 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 +01009664 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR, &error)) {
9665 Alert("parsing [%s:%d]: 'http-response %s': %s.\n",
9666 file, linenum, args[0], error);
9667 free(error);
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01009668 goto out_err;
9669 }
Willy Tarreau59ad1a22014-01-29 14:39:58 +01009670 free(proxy->conf.lfs_file);
9671 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9672 proxy->conf.lfs_line = proxy->conf.args.line;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009673 cur_arg += 2;
Sasha Pachev218f0642014-06-16 12:05:59 -06009674 } else if (strcmp(args[0], "replace-header") == 0 || strcmp(args[0], "replace-value") == 0) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009675 rule->action = args[0][8] == 'h' ? ACT_HTTP_REPLACE_HDR : ACT_HTTP_REPLACE_VAL;
Sasha Pachev218f0642014-06-16 12:05:59 -06009676 cur_arg = 1;
9677
9678 if (!*args[cur_arg] || !*args[cur_arg+1] || !*args[cur_arg+2] ||
Baptiste Assmann12cb00b2014-08-08 17:29:06 +02009679 (*args[cur_arg+3] && strcmp(args[cur_arg+3], "if") != 0 && strcmp(args[cur_arg+3], "unless") != 0)) {
9680 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 3 arguments.\n",
Sasha Pachev218f0642014-06-16 12:05:59 -06009681 file, linenum, args[0]);
9682 goto out_err;
9683 }
9684
9685 rule->arg.hdr_add.name = strdup(args[cur_arg]);
9686 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
9687 LIST_INIT(&rule->arg.hdr_add.fmt);
9688
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02009689 error = NULL;
9690 if (!regex_comp(args[cur_arg + 1], &rule->arg.hdr_add.re, 1, 1, &error)) {
9691 Alert("parsing [%s:%d] : '%s' : %s.\n", file, linenum,
9692 args[cur_arg + 1], error);
9693 free(error);
Sasha Pachev218f0642014-06-16 12:05:59 -06009694 goto out_err;
9695 }
9696
9697 proxy->conf.args.ctx = ARGC_HRQ;
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01009698 error = NULL;
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01009699 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 +01009700 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR, &error)) {
9701 Alert("parsing [%s:%d]: 'http-response %s': %s.\n",
9702 file, linenum, args[0], error);
9703 free(error);
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01009704 goto out_err;
9705 }
Sasha Pachev218f0642014-06-16 12:05:59 -06009706
9707 free(proxy->conf.lfs_file);
9708 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9709 proxy->conf.lfs_line = proxy->conf.args.line;
9710 cur_arg += 3;
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02009711 } else if (strcmp(args[0], "del-header") == 0) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009712 rule->action = ACT_HTTP_DEL_HDR;
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02009713 cur_arg = 1;
9714
9715 if (!*args[cur_arg] ||
9716 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
9717 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument.\n",
9718 file, linenum, args[0]);
9719 goto out_err;
9720 }
9721
9722 rule->arg.hdr_add.name = strdup(args[cur_arg]);
9723 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
9724
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009725 proxy->conf.args.ctx = ARGC_HRS;
9726 free(proxy->conf.lfs_file);
9727 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9728 proxy->conf.lfs_line = proxy->conf.args.line;
9729 cur_arg += 1;
9730 } else if (strncmp(args[0], "add-acl", 7) == 0) {
9731 /* http-request add-acl(<reference (acl name)>) <key pattern> */
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009732 rule->action = ACT_HTTP_ADD_ACL;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009733 /*
9734 * '+ 8' for 'add-acl('
9735 * '- 9' for 'add-acl(' + trailing ')'
9736 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02009737 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009738
9739 cur_arg = 1;
9740
9741 if (!*args[cur_arg] ||
9742 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
9743 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument.\n",
9744 file, linenum, args[0]);
9745 goto out_err;
9746 }
9747
9748 LIST_INIT(&rule->arg.map.key);
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02009749 proxy->conf.args.ctx = ARGC_HRS;
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01009750 error = NULL;
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01009751 if (!parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01009752 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR, &error)) {
9753 Alert("parsing [%s:%d]: 'http-response %s': %s.\n",
9754 file, linenum, args[0], error);
9755 free(error);
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01009756 goto out_err;
9757 }
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02009758 free(proxy->conf.lfs_file);
9759 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9760 proxy->conf.lfs_line = proxy->conf.args.line;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009761
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02009762 cur_arg += 1;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009763 } else if (strncmp(args[0], "del-acl", 7) == 0) {
9764 /* http-response del-acl(<reference (acl name)>) <key pattern> */
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009765 rule->action = ACT_HTTP_DEL_ACL;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009766 /*
9767 * '+ 8' for 'del-acl('
9768 * '- 9' for 'del-acl(' + trailing ')'
9769 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02009770 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009771
9772 cur_arg = 1;
9773
9774 if (!*args[cur_arg] ||
9775 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
9776 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument.\n",
9777 file, linenum, args[0]);
9778 goto out_err;
9779 }
9780
9781 LIST_INIT(&rule->arg.map.key);
9782 proxy->conf.args.ctx = ARGC_HRS;
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01009783 error = NULL;
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01009784 if (!parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01009785 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR, &error)) {
9786 Alert("parsing [%s:%d]: 'http-response %s': %s.\n",
9787 file, linenum, args[0], error);
9788 free(error);
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01009789 goto out_err;
9790 }
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009791 free(proxy->conf.lfs_file);
9792 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9793 proxy->conf.lfs_line = proxy->conf.args.line;
9794 cur_arg += 1;
9795 } else if (strncmp(args[0], "del-map", 7) == 0) {
9796 /* http-response del-map(<reference (map name)>) <key pattern> */
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009797 rule->action = ACT_HTTP_DEL_MAP;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009798 /*
9799 * '+ 8' for 'del-map('
9800 * '- 9' for 'del-map(' + trailing ')'
9801 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02009802 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009803
9804 cur_arg = 1;
9805
9806 if (!*args[cur_arg] ||
9807 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
9808 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument.\n",
9809 file, linenum, args[0]);
9810 goto out_err;
9811 }
9812
9813 LIST_INIT(&rule->arg.map.key);
9814 proxy->conf.args.ctx = ARGC_HRS;
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01009815 error = NULL;
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01009816 if (!parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01009817 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR, &error)) {
9818 Alert("parsing [%s:%d]: 'http-response %s' %s.\n",
9819 file, linenum, args[0], error);
9820 free(error);
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01009821 goto out_err;
9822 }
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009823 free(proxy->conf.lfs_file);
9824 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9825 proxy->conf.lfs_line = proxy->conf.args.line;
9826 cur_arg += 1;
9827 } else if (strncmp(args[0], "set-map", 7) == 0) {
9828 /* http-response set-map(<reference (map name)>) <key pattern> <value pattern> */
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009829 rule->action = ACT_HTTP_SET_MAP;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009830 /*
9831 * '+ 8' for 'set-map('
9832 * '- 9' for 'set-map(' + trailing ')'
9833 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02009834 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009835
9836 cur_arg = 1;
9837
9838 if (!*args[cur_arg] || !*args[cur_arg+1] ||
9839 (*args[cur_arg+2] && strcmp(args[cur_arg+2], "if") != 0 && strcmp(args[cur_arg+2], "unless") != 0)) {
9840 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 2 arguments.\n",
9841 file, linenum, args[0]);
9842 goto out_err;
9843 }
9844
9845 LIST_INIT(&rule->arg.map.key);
9846 LIST_INIT(&rule->arg.map.value);
9847
9848 proxy->conf.args.ctx = ARGC_HRS;
9849
9850 /* key pattern */
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01009851 error = NULL;
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01009852 if (!parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01009853 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR, &error)) {
9854 Alert("parsing [%s:%d]: 'http-response %s' name: %s.\n",
9855 file, linenum, args[0], error);
9856 free(error);
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01009857 goto out_err;
9858 }
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009859
9860 /* value pattern */
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01009861 error = NULL;
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01009862 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 +01009863 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR, &error)) {
9864 Alert("parsing [%s:%d]: 'http-response %s' value: %s.\n",
9865 file, linenum, args[0], error);
9866 free(error);
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01009867 goto out_err;
9868 }
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009869
9870 free(proxy->conf.lfs_file);
9871 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9872 proxy->conf.lfs_line = proxy->conf.args.line;
9873
9874 cur_arg += 2;
Willy Tarreau51d861a2015-05-22 17:30:48 +02009875 } else if (strcmp(args[0], "redirect") == 0) {
9876 struct redirect_rule *redir;
9877 char *errmsg = NULL;
9878
9879 if ((redir = http_parse_redirect_rule(file, linenum, proxy, (const char **)args + 1, &errmsg, 1, 1)) == NULL) {
9880 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-response %s' rule : %s.\n",
9881 file, linenum, proxy_type_str(proxy), proxy->id, args[0], errmsg);
9882 goto out_err;
9883 }
9884
9885 /* this redirect rule might already contain a parsed condition which
9886 * we'll pass to the http-request rule.
9887 */
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009888 rule->action = ACT_HTTP_REDIR;
Willy Tarreau51d861a2015-05-22 17:30:48 +02009889 rule->arg.redir = redir;
9890 rule->cond = redir->cond;
9891 redir->cond = NULL;
9892 cur_arg = 2;
9893 return rule;
Ruoshan Huange4edc6b2016-07-14 15:07:45 +08009894 } else if (strncmp(args[0], "track-sc", 8) == 0 &&
9895 args[0][9] == '\0' && args[0][8] >= '0' &&
9896 args[0][8] < '0' + MAX_SESS_STKCTR) { /* track-sc 0..9 */
9897 struct sample_expr *expr;
9898 unsigned int where;
9899 char *err = NULL;
9900
9901 cur_arg = 1;
9902 proxy->conf.args.ctx = ARGC_TRK;
9903
9904 expr = sample_parse_expr((char **)args, &cur_arg, file, linenum, &err, &proxy->conf.args);
9905 if (!expr) {
9906 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-response %s' rule : %s.\n",
9907 file, linenum, proxy_type_str(proxy), proxy->id, args[0], err);
9908 free(err);
9909 goto out_err;
9910 }
9911
9912 where = 0;
9913 if (proxy->cap & PR_CAP_FE)
9914 where |= SMP_VAL_FE_HRS_HDR;
9915 if (proxy->cap & PR_CAP_BE)
9916 where |= SMP_VAL_BE_HRS_HDR;
9917
9918 if (!(expr->fetch->val & where)) {
9919 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-response %s' rule :"
9920 " fetch method '%s' extracts information from '%s', none of which is available here.\n",
9921 file, linenum, proxy_type_str(proxy), proxy->id, args[0],
9922 args[cur_arg-1], sample_src_names(expr->fetch->use));
9923 free(expr);
9924 goto out_err;
9925 }
9926
9927 if (strcmp(args[cur_arg], "table") == 0) {
9928 cur_arg++;
9929 if (!args[cur_arg]) {
9930 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-response %s' rule : missing table name.\n",
9931 file, linenum, proxy_type_str(proxy), proxy->id, args[0]);
9932 free(expr);
9933 goto out_err;
9934 }
9935 /* we copy the table name for now, it will be resolved later */
9936 rule->arg.trk_ctr.table.n = strdup(args[cur_arg]);
9937 cur_arg++;
9938 }
9939 rule->arg.trk_ctr.expr = expr;
9940 rule->action = ACT_ACTION_TRK_SC0 + args[0][8] - '0';
William Lallemand73025dd2014-04-24 14:38:37 +02009941 } else if (((custom = action_http_res_custom(args[0])) != NULL)) {
9942 char *errmsg = NULL;
9943 cur_arg = 1;
9944 /* try in the module list */
Thierry FOURNIER5563e4b2015-08-14 19:20:07 +02009945 rule->from = ACT_F_HTTP_RES;
Thierry FOURNIER85c6c972015-09-22 19:14:35 +02009946 rule->kw = custom;
Thierry FOURNIERafa80492015-08-19 09:04:15 +02009947 if (custom->parse(args, &cur_arg, proxy, rule, &errmsg) == ACT_RET_PRS_ERR) {
William Lallemand73025dd2014-04-24 14:38:37 +02009948 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-response %s' rule : %s.\n",
9949 file, linenum, proxy_type_str(proxy), proxy->id, args[0], errmsg);
9950 free(errmsg);
9951 goto out_err;
9952 }
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009953 } else {
Thierry FOURNIERab95e652015-10-02 08:24:51 +02009954 action_build_list(&http_res_keywords.list, &trash);
9955 Alert("parsing [%s:%d]: 'http-response' expects 'allow', 'deny', 'redirect', "
9956 "'add-header', 'del-header', 'set-header', 'replace-header', 'replace-value', 'set-nice', "
Ruoshan Huange4edc6b2016-07-14 15:07:45 +08009957 "'set-tos', 'set-mark', 'set-log-level', 'add-acl', 'del-acl', 'del-map', 'set-map', 'track-sc*'"
William Lallemand2e785f22016-05-25 01:48:42 +02009958 "%s%s, but got '%s'%s.\n",
Thierry FOURNIERab95e652015-10-02 08:24:51 +02009959 file, linenum, *trash.str ? ", " : "", trash.str, args[0], *args[0] ? "" : " (missing argument)");
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009960 goto out_err;
9961 }
9962
9963 if (strcmp(args[cur_arg], "if") == 0 || strcmp(args[cur_arg], "unless") == 0) {
9964 struct acl_cond *cond;
9965 char *errmsg = NULL;
9966
9967 if ((cond = build_acl_cond(file, linenum, proxy, args+cur_arg, &errmsg)) == NULL) {
9968 Alert("parsing [%s:%d] : error detected while parsing an 'http-response %s' condition : %s.\n",
9969 file, linenum, args[0], errmsg);
9970 free(errmsg);
9971 goto out_err;
9972 }
9973 rule->cond = cond;
9974 }
9975 else if (*args[cur_arg]) {
9976 Alert("parsing [%s:%d]: 'http-response %s' expects"
9977 " either 'if' or 'unless' followed by a condition but found '%s'.\n",
9978 file, linenum, args[0], args[cur_arg]);
9979 goto out_err;
9980 }
9981
9982 return rule;
9983 out_err:
9984 free(rule);
9985 return NULL;
9986}
9987
Willy Tarreau4baae242012-12-27 12:00:31 +01009988/* Parses a redirect rule. Returns the redirect rule on success or NULL on error,
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01009989 * with <err> filled with the error message. If <use_fmt> is not null, builds a
Willy Tarreaube4653b2015-05-28 15:26:58 +02009990 * dynamic log-format rule instead of a static string. Parameter <dir> indicates
9991 * the direction of the rule, and equals 0 for request, non-zero for responses.
Willy Tarreau4baae242012-12-27 12:00:31 +01009992 */
9993struct redirect_rule *http_parse_redirect_rule(const char *file, int linenum, struct proxy *curproxy,
Willy Tarreaube4653b2015-05-28 15:26:58 +02009994 const char **args, char **errmsg, int use_fmt, int dir)
Willy Tarreau4baae242012-12-27 12:00:31 +01009995{
9996 struct redirect_rule *rule;
9997 int cur_arg;
9998 int type = REDIRECT_TYPE_NONE;
9999 int code = 302;
10000 const char *destination = NULL;
10001 const char *cookie = NULL;
10002 int cookie_set = 0;
10003 unsigned int flags = REDIRECT_FLAG_NONE;
10004 struct acl_cond *cond = NULL;
10005
10006 cur_arg = 0;
10007 while (*(args[cur_arg])) {
10008 if (strcmp(args[cur_arg], "location") == 0) {
10009 if (!*args[cur_arg + 1])
10010 goto missing_arg;
10011
10012 type = REDIRECT_TYPE_LOCATION;
10013 cur_arg++;
10014 destination = args[cur_arg];
10015 }
10016 else if (strcmp(args[cur_arg], "prefix") == 0) {
10017 if (!*args[cur_arg + 1])
10018 goto missing_arg;
Willy Tarreau4baae242012-12-27 12:00:31 +010010019 type = REDIRECT_TYPE_PREFIX;
10020 cur_arg++;
10021 destination = args[cur_arg];
10022 }
10023 else if (strcmp(args[cur_arg], "scheme") == 0) {
10024 if (!*args[cur_arg + 1])
10025 goto missing_arg;
10026
10027 type = REDIRECT_TYPE_SCHEME;
10028 cur_arg++;
10029 destination = args[cur_arg];
10030 }
10031 else if (strcmp(args[cur_arg], "set-cookie") == 0) {
10032 if (!*args[cur_arg + 1])
10033 goto missing_arg;
10034
10035 cur_arg++;
10036 cookie = args[cur_arg];
10037 cookie_set = 1;
10038 }
10039 else if (strcmp(args[cur_arg], "clear-cookie") == 0) {
10040 if (!*args[cur_arg + 1])
10041 goto missing_arg;
10042
10043 cur_arg++;
10044 cookie = args[cur_arg];
10045 cookie_set = 0;
10046 }
10047 else if (strcmp(args[cur_arg], "code") == 0) {
10048 if (!*args[cur_arg + 1])
10049 goto missing_arg;
10050
10051 cur_arg++;
10052 code = atol(args[cur_arg]);
Yves Lafon3e8d1ae2013-03-11 11:06:05 -040010053 if (code < 301 || code > 308 || (code > 303 && code < 307)) {
Willy Tarreau4baae242012-12-27 12:00:31 +010010054 memprintf(errmsg,
Yves Lafon3e8d1ae2013-03-11 11:06:05 -040010055 "'%s': unsupported HTTP code '%s' (must be one of 301, 302, 303, 307 or 308)",
Willy Tarreau4baae242012-12-27 12:00:31 +010010056 args[cur_arg - 1], args[cur_arg]);
10057 return NULL;
10058 }
10059 }
10060 else if (!strcmp(args[cur_arg],"drop-query")) {
10061 flags |= REDIRECT_FLAG_DROP_QS;
10062 }
10063 else if (!strcmp(args[cur_arg],"append-slash")) {
10064 flags |= REDIRECT_FLAG_APPEND_SLASH;
10065 }
10066 else if (strcmp(args[cur_arg], "if") == 0 ||
10067 strcmp(args[cur_arg], "unless") == 0) {
10068 cond = build_acl_cond(file, linenum, curproxy, (const char **)args + cur_arg, errmsg);
10069 if (!cond) {
10070 memprintf(errmsg, "error in condition: %s", *errmsg);
10071 return NULL;
10072 }
10073 break;
10074 }
10075 else {
10076 memprintf(errmsg,
10077 "expects 'code', 'prefix', 'location', 'scheme', 'set-cookie', 'clear-cookie', 'drop-query' or 'append-slash' (was '%s')",
10078 args[cur_arg]);
10079 return NULL;
10080 }
10081 cur_arg++;
10082 }
10083
10084 if (type == REDIRECT_TYPE_NONE) {
10085 memprintf(errmsg, "redirection type expected ('prefix', 'location', or 'scheme')");
10086 return NULL;
10087 }
10088
Willy Tarreaube4653b2015-05-28 15:26:58 +020010089 if (dir && type != REDIRECT_TYPE_LOCATION) {
10090 memprintf(errmsg, "response only supports redirect type 'location'");
10091 return NULL;
10092 }
10093
Vincent Bernat3c2f2f22016-04-03 13:48:42 +020010094 rule = calloc(1, sizeof(*rule));
Willy Tarreau4baae242012-12-27 12:00:31 +010010095 rule->cond = cond;
Willy Tarreau60e08382013-12-03 00:48:45 +010010096 LIST_INIT(&rule->rdr_fmt);
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +010010097
10098 if (!use_fmt) {
10099 /* old-style static redirect rule */
10100 rule->rdr_str = strdup(destination);
10101 rule->rdr_len = strlen(destination);
10102 }
10103 else {
10104 /* log-format based redirect rule */
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +010010105
10106 /* Parse destination. Note that in the REDIRECT_TYPE_PREFIX case,
10107 * if prefix == "/", we don't want to add anything, otherwise it
10108 * makes it hard for the user to configure a self-redirection.
10109 */
Godbachd9722032014-12-18 15:44:58 +080010110 curproxy->conf.args.ctx = ARGC_RDR;
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +010010111 if (!(type == REDIRECT_TYPE_PREFIX && destination[0] == '/' && destination[1] == '\0')) {
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +010010112 if (!parse_logformat_string(destination, curproxy, &rule->rdr_fmt, LOG_OPT_HTTP,
10113 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 +010010114 : (curproxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR,
10115 errmsg)) {
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +010010116 return NULL;
10117 }
Willy Tarreau59ad1a22014-01-29 14:39:58 +010010118 free(curproxy->conf.lfs_file);
10119 curproxy->conf.lfs_file = strdup(curproxy->conf.args.file);
10120 curproxy->conf.lfs_line = curproxy->conf.args.line;
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +010010121 }
10122 }
10123
Willy Tarreau4baae242012-12-27 12:00:31 +010010124 if (cookie) {
10125 /* depending on cookie_set, either we want to set the cookie, or to clear it.
10126 * a clear consists in appending "; path=/; Max-Age=0;" at the end.
10127 */
10128 rule->cookie_len = strlen(cookie);
10129 if (cookie_set) {
10130 rule->cookie_str = malloc(rule->cookie_len + 10);
10131 memcpy(rule->cookie_str, cookie, rule->cookie_len);
10132 memcpy(rule->cookie_str + rule->cookie_len, "; path=/;", 10);
10133 rule->cookie_len += 9;
10134 } else {
10135 rule->cookie_str = malloc(rule->cookie_len + 21);
10136 memcpy(rule->cookie_str, cookie, rule->cookie_len);
10137 memcpy(rule->cookie_str + rule->cookie_len, "; path=/; Max-Age=0;", 21);
10138 rule->cookie_len += 20;
10139 }
10140 }
10141 rule->type = type;
10142 rule->code = code;
10143 rule->flags = flags;
10144 LIST_INIT(&rule->list);
10145 return rule;
10146
10147 missing_arg:
10148 memprintf(errmsg, "missing argument for '%s'", args[cur_arg]);
10149 return NULL;
10150}
10151
Willy Tarreau8797c062007-05-07 00:55:35 +020010152/************************************************************************/
10153/* The code below is dedicated to ACL parsing and matching */
10154/************************************************************************/
10155
10156
Willy Tarreau14174bc2012-04-16 14:34:04 +020010157/* This function ensures that the prerequisites for an L7 fetch are ready,
10158 * which means that a request or response is ready. If some data is missing,
10159 * a parsing attempt is made. This is useful in TCP-based ACLs which are able
Willy Tarreau24e32d82012-04-23 23:55:44 +020010160 * to extract data from L7. If <req_vol> is non-null during a request prefetch,
10161 * another test is made to ensure the required information is not gone.
Willy Tarreau14174bc2012-04-16 14:34:04 +020010162 *
10163 * The function returns :
Willy Tarreau506d0502013-07-06 13:29:24 +020010164 * 0 with SMP_F_MAY_CHANGE in the sample flags if some data is missing to
10165 * decide whether or not an HTTP message is present ;
10166 * 0 if the requested data cannot be fetched or if it is certain that
10167 * we'll never have any HTTP message there ;
Willy Tarreau14174bc2012-04-16 14:34:04 +020010168 * 1 if an HTTP message is ready
10169 */
James Rosewell91a41cb2015-09-18 17:11:16 +010010170int smp_prefetch_http(struct proxy *px, struct stream *s, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +020010171 const struct arg *args, struct sample *smp, int req_vol)
Willy Tarreau14174bc2012-04-16 14:34:04 +020010172{
Willy Tarreau192252e2015-04-04 01:47:55 +020010173 struct http_txn *txn;
10174 struct http_msg *msg;
Willy Tarreau14174bc2012-04-16 14:34:04 +020010175
Willy Tarreaube508f12016-03-10 11:47:01 +010010176 /* Note: it is possible that <s> is NULL when called before stream
10177 * initialization (eg: tcp-request connection), so this function is the
10178 * one responsible for guarding against this case for all HTTP users.
Willy Tarreau14174bc2012-04-16 14:34:04 +020010179 */
Willy Tarreau192252e2015-04-04 01:47:55 +020010180 if (!s)
10181 return 0;
Willy Tarreaube508f12016-03-10 11:47:01 +010010182
Thierry FOURNIERed08d6a2015-09-24 08:40:18 +020010183 if (!s->txn) {
10184 if (unlikely(!http_alloc_txn(s)))
10185 return 0; /* not enough memory */
10186 http_init_txn(s);
10187 }
Willy Tarreau192252e2015-04-04 01:47:55 +020010188 txn = s->txn;
Willy Tarreau192252e2015-04-04 01:47:55 +020010189 msg = &txn->req;
Willy Tarreau14174bc2012-04-16 14:34:04 +020010190
10191 /* Check for a dependency on a request */
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010192 smp->data.type = SMP_T_BOOL;
Willy Tarreau14174bc2012-04-16 14:34:04 +020010193
Willy Tarreau32a6f2e2012-04-25 10:13:36 +020010194 if ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) {
Willy Tarreauaae75e32013-03-29 12:31:49 +010010195 /* If the buffer does not leave enough free space at the end,
10196 * we must first realign it.
10197 */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +010010198 if (s->req.buf->p > s->req.buf->data &&
10199 s->req.buf->i + s->req.buf->p > s->req.buf->data + s->req.buf->size - global.tune.maxrewrite)
10200 buffer_slow_realign(s->req.buf);
Willy Tarreauaae75e32013-03-29 12:31:49 +010010201
Willy Tarreau14174bc2012-04-16 14:34:04 +020010202 if (unlikely(txn->req.msg_state < HTTP_MSG_BODY)) {
Willy Tarreau472b1ee2013-10-14 22:41:30 +020010203 if (msg->msg_state == HTTP_MSG_ERROR)
Willy Tarreau506d0502013-07-06 13:29:24 +020010204 return 0;
Willy Tarreau14174bc2012-04-16 14:34:04 +020010205
10206 /* Try to decode HTTP request */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +010010207 if (likely(msg->next < s->req.buf->i))
Willy Tarreau14174bc2012-04-16 14:34:04 +020010208 http_msg_analyzer(msg, &txn->hdr_idx);
10209
10210 /* Still no valid request ? */
10211 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau3bf1b2b2012-08-27 20:46:07 +020010212 if ((msg->msg_state == HTTP_MSG_ERROR) ||
Willy Tarreau22ec1ea2014-11-27 20:45:39 +010010213 buffer_full(s->req.buf, global.tune.maxrewrite)) {
Willy Tarreau506d0502013-07-06 13:29:24 +020010214 return 0;
Willy Tarreau14174bc2012-04-16 14:34:04 +020010215 }
10216 /* wait for final state */
Willy Tarreau37406352012-04-23 16:16:37 +020010217 smp->flags |= SMP_F_MAY_CHANGE;
Willy Tarreau14174bc2012-04-16 14:34:04 +020010218 return 0;
10219 }
10220
10221 /* OK we just got a valid HTTP request. We have some minor
10222 * preparation to perform so that further checks can rely
10223 * on HTTP tests.
10224 */
Willy Tarreauaae75e32013-03-29 12:31:49 +010010225
10226 /* If the request was parsed but was too large, we must absolutely
10227 * return an error so that it is not processed. At the moment this
10228 * cannot happen, but if the parsers are to change in the future,
10229 * we want this check to be maintained.
10230 */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +010010231 if (unlikely(s->req.buf->i + s->req.buf->p >
10232 s->req.buf->data + s->req.buf->size - global.tune.maxrewrite)) {
Willy Tarreau10e61cb2017-01-04 14:51:22 +010010233 msg->err_state = msg->msg_state;
Willy Tarreauaae75e32013-03-29 12:31:49 +010010234 msg->msg_state = HTTP_MSG_ERROR;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010235 smp->data.u.sint = 1;
Willy Tarreauaae75e32013-03-29 12:31:49 +010010236 return 1;
10237 }
10238
Willy Tarreau9b28e032012-10-12 23:49:43 +020010239 txn->meth = find_http_meth(msg->chn->buf->p, msg->sl.rq.m_l);
Willy Tarreau14174bc2012-04-16 14:34:04 +020010240 if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)
Willy Tarreaue7dff022015-04-03 01:14:29 +020010241 s->flags |= SF_REDIRECTABLE;
Willy Tarreau14174bc2012-04-16 14:34:04 +020010242
Willy Tarreau506d0502013-07-06 13:29:24 +020010243 if (unlikely(msg->sl.rq.v_l == 0) && !http_upgrade_v09_to_v10(txn))
10244 return 0;
Willy Tarreau14174bc2012-04-16 14:34:04 +020010245 }
10246
Willy Tarreau506d0502013-07-06 13:29:24 +020010247 if (req_vol && txn->rsp.msg_state != HTTP_MSG_RPBEFORE) {
Willy Tarreau14174bc2012-04-16 14:34:04 +020010248 return 0; /* data might have moved and indexes changed */
Willy Tarreau506d0502013-07-06 13:29:24 +020010249 }
Willy Tarreau14174bc2012-04-16 14:34:04 +020010250
10251 /* otherwise everything's ready for the request */
10252 }
Willy Tarreau24e32d82012-04-23 23:55:44 +020010253 else {
10254 /* Check for a dependency on a response */
Willy Tarreau506d0502013-07-06 13:29:24 +020010255 if (txn->rsp.msg_state < HTTP_MSG_BODY) {
10256 smp->flags |= SMP_F_MAY_CHANGE;
Willy Tarreau14174bc2012-04-16 14:34:04 +020010257 return 0;
Willy Tarreau506d0502013-07-06 13:29:24 +020010258 }
Willy Tarreau14174bc2012-04-16 14:34:04 +020010259 }
10260
10261 /* everything's OK */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010262 smp->data.u.sint = 1;
Willy Tarreau14174bc2012-04-16 14:34:04 +020010263 return 1;
10264}
Willy Tarreau8797c062007-05-07 00:55:35 +020010265
Willy Tarreau8797c062007-05-07 00:55:35 +020010266/* 1. Check on METHOD
10267 * We use the pre-parsed method if it is known, and store its number as an
10268 * integer. If it is unknown, we use the pointer and the length.
10269 */
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +020010270static int pat_parse_meth(const char *text, struct pattern *pattern, int mflags, char **err)
Willy Tarreau8797c062007-05-07 00:55:35 +020010271{
10272 int len, meth;
10273
Thierry FOURNIER580c32c2014-01-24 10:58:12 +010010274 len = strlen(text);
10275 meth = find_http_meth(text, len);
Willy Tarreau8797c062007-05-07 00:55:35 +020010276
10277 pattern->val.i = meth;
10278 if (meth == HTTP_METH_OTHER) {
Willy Tarreau912c1192014-08-29 15:15:50 +020010279 pattern->ptr.str = (char *)text;
Willy Tarreau8797c062007-05-07 00:55:35 +020010280 pattern->len = len;
10281 }
Thierry FOURNIERd4373142013-12-17 01:10:10 +010010282 else {
10283 pattern->ptr.str = NULL;
10284 pattern->len = 0;
Thierry FOURNIERd4373142013-12-17 01:10:10 +010010285 }
Willy Tarreau8797c062007-05-07 00:55:35 +020010286 return 1;
10287}
10288
Willy Tarreau8e5e9552011-12-16 15:38:49 +010010289/* This function fetches the method of current HTTP request and stores
10290 * it in the global pattern struct as a chunk. There are two possibilities :
10291 * - if the method is known (not HTTP_METH_OTHER), its identifier is stored
10292 * in <len> and <ptr> is NULL ;
10293 * - if the method is unknown (HTTP_METH_OTHER), <ptr> points to the text and
10294 * <len> to its length.
Thierry FOURNIERa65b3432013-11-28 18:22:00 +010010295 * This is intended to be used with pat_match_meth() only.
Willy Tarreau8e5e9552011-12-16 15:38:49 +010010296 */
Willy Tarreaud41f8d82007-06-10 10:06:18 +020010297static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010298smp_fetch_meth(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau8797c062007-05-07 00:55:35 +020010299{
10300 int meth;
Willy Tarreaube508f12016-03-10 11:47:01 +010010301 struct http_txn *txn;
Willy Tarreau8797c062007-05-07 00:55:35 +020010302
Willy Tarreau24e32d82012-04-23 23:55:44 +020010303 CHECK_HTTP_MESSAGE_FIRST_PERM();
Willy Tarreauc11416f2007-06-17 16:58:38 +020010304
Willy Tarreaube508f12016-03-10 11:47:01 +010010305 txn = smp->strm->txn;
Willy Tarreau8797c062007-05-07 00:55:35 +020010306 meth = txn->meth;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010307 smp->data.type = SMP_T_METH;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010308 smp->data.u.meth.meth = meth;
Willy Tarreau8797c062007-05-07 00:55:35 +020010309 if (meth == HTTP_METH_OTHER) {
Willy Tarreauc11416f2007-06-17 16:58:38 +020010310 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
10311 /* ensure the indexes are not affected */
10312 return 0;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010313 smp->flags |= SMP_F_CONST;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010314 smp->data.u.meth.str.len = txn->req.sl.rq.m_l;
10315 smp->data.u.meth.str.str = txn->req.chn->buf->p;
Willy Tarreau8797c062007-05-07 00:55:35 +020010316 }
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010317 smp->flags |= SMP_F_VOL_1ST;
Willy Tarreau8797c062007-05-07 00:55:35 +020010318 return 1;
10319}
10320
Willy Tarreau8e5e9552011-12-16 15:38:49 +010010321/* See above how the method is stored in the global pattern */
Thierry FOURNIER5338eea2013-12-16 14:22:13 +010010322static struct pattern *pat_match_meth(struct sample *smp, struct pattern_expr *expr, int fill)
Willy Tarreau8797c062007-05-07 00:55:35 +020010323{
Willy Tarreauc8d7c962007-06-17 08:20:33 +020010324 int icase;
Thierry FOURNIER5338eea2013-12-16 14:22:13 +010010325 struct pattern_list *lst;
10326 struct pattern *pattern;
Willy Tarreauc8d7c962007-06-17 08:20:33 +020010327
Thierry FOURNIER5338eea2013-12-16 14:22:13 +010010328 list_for_each_entry(lst, &expr->patterns, list) {
10329 pattern = &lst->pat;
Willy Tarreau8797c062007-05-07 00:55:35 +020010330
Thierry FOURNIER5338eea2013-12-16 14:22:13 +010010331 /* well-known method */
10332 if (pattern->val.i != HTTP_METH_OTHER) {
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010333 if (smp->data.u.meth.meth == pattern->val.i)
Thierry FOURNIER5338eea2013-12-16 14:22:13 +010010334 return pattern;
10335 else
10336 continue;
10337 }
Willy Tarreauc8d7c962007-06-17 08:20:33 +020010338
Thierry FOURNIER5338eea2013-12-16 14:22:13 +010010339 /* Other method, we must compare the strings */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010340 if (pattern->len != smp->data.u.meth.str.len)
Thierry FOURNIER5338eea2013-12-16 14:22:13 +010010341 continue;
10342
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +020010343 icase = expr->mflags & PAT_MF_IGNORE_CASE;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010344 if ((icase && strncasecmp(pattern->ptr.str, smp->data.u.meth.str.str, smp->data.u.meth.str.len) == 0) ||
10345 (!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 +010010346 return pattern;
10347 }
10348 return NULL;
Willy Tarreau8797c062007-05-07 00:55:35 +020010349}
10350
Willy Tarreaud41f8d82007-06-10 10:06:18 +020010351static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010352smp_fetch_rqver(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau8797c062007-05-07 00:55:35 +020010353{
Willy Tarreaube508f12016-03-10 11:47:01 +010010354 struct http_txn *txn;
Willy Tarreau8797c062007-05-07 00:55:35 +020010355 char *ptr;
10356 int len;
10357
Willy Tarreauc0239e02012-04-16 14:42:55 +020010358 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +020010359
Willy Tarreaube508f12016-03-10 11:47:01 +010010360 txn = smp->strm->txn;
Willy Tarreau8797c062007-05-07 00:55:35 +020010361 len = txn->req.sl.rq.v_l;
Willy Tarreau9b28e032012-10-12 23:49:43 +020010362 ptr = txn->req.chn->buf->p + txn->req.sl.rq.v;
Willy Tarreau8797c062007-05-07 00:55:35 +020010363
10364 while ((len-- > 0) && (*ptr++ != '/'));
10365 if (len <= 0)
10366 return 0;
10367
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010368 smp->data.type = SMP_T_STR;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010369 smp->data.u.str.str = ptr;
10370 smp->data.u.str.len = len;
Willy Tarreau8797c062007-05-07 00:55:35 +020010371
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010372 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
Willy Tarreau8797c062007-05-07 00:55:35 +020010373 return 1;
10374}
10375
Willy Tarreaud41f8d82007-06-10 10:06:18 +020010376static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010377smp_fetch_stver(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau8797c062007-05-07 00:55:35 +020010378{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010379 struct http_txn *txn;
Willy Tarreau8797c062007-05-07 00:55:35 +020010380 char *ptr;
10381 int len;
10382
Willy Tarreauc0239e02012-04-16 14:42:55 +020010383 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +020010384
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010385 txn = smp->strm->txn;
Willy Tarreauf26b2522012-12-14 08:33:14 +010010386 if (txn->rsp.msg_state < HTTP_MSG_BODY)
10387 return 0;
10388
Willy Tarreau8797c062007-05-07 00:55:35 +020010389 len = txn->rsp.sl.st.v_l;
Willy Tarreau9b28e032012-10-12 23:49:43 +020010390 ptr = txn->rsp.chn->buf->p;
Willy Tarreau8797c062007-05-07 00:55:35 +020010391
10392 while ((len-- > 0) && (*ptr++ != '/'));
10393 if (len <= 0)
10394 return 0;
10395
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010396 smp->data.type = SMP_T_STR;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010397 smp->data.u.str.str = ptr;
10398 smp->data.u.str.len = len;
Willy Tarreau8797c062007-05-07 00:55:35 +020010399
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010400 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
Willy Tarreau8797c062007-05-07 00:55:35 +020010401 return 1;
10402}
10403
10404/* 3. Check on Status Code. We manipulate integers here. */
Willy Tarreaud41f8d82007-06-10 10:06:18 +020010405static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010406smp_fetch_stcode(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau8797c062007-05-07 00:55:35 +020010407{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010408 struct http_txn *txn;
Willy Tarreau8797c062007-05-07 00:55:35 +020010409 char *ptr;
10410 int len;
10411
Willy Tarreauc0239e02012-04-16 14:42:55 +020010412 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +020010413
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010414 txn = smp->strm->txn;
Willy Tarreauf26b2522012-12-14 08:33:14 +010010415 if (txn->rsp.msg_state < HTTP_MSG_BODY)
10416 return 0;
10417
Willy Tarreau8797c062007-05-07 00:55:35 +020010418 len = txn->rsp.sl.st.c_l;
Willy Tarreau9b28e032012-10-12 23:49:43 +020010419 ptr = txn->rsp.chn->buf->p + txn->rsp.sl.st.c;
Willy Tarreau8797c062007-05-07 00:55:35 +020010420
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010421 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010422 smp->data.u.sint = __strl2ui(ptr, len);
Willy Tarreau37406352012-04-23 16:16:37 +020010423 smp->flags = SMP_F_VOL_1ST;
Willy Tarreau8797c062007-05-07 00:55:35 +020010424 return 1;
10425}
10426
Thierry Fournierf4011dd2016-03-29 17:23:51 +020010427static int
10428smp_fetch_uniqueid(const struct arg *args, struct sample *smp, const char *kw, void *private)
10429{
10430 if (LIST_ISEMPTY(&smp->sess->fe->format_unique_id))
10431 return 0;
10432
10433 if (!smp->strm->unique_id) {
10434 if ((smp->strm->unique_id = pool_alloc2(pool2_uniqueid)) == NULL)
10435 return 0;
10436 smp->strm->unique_id[0] = '\0';
10437 }
10438 smp->data.u.str.len = build_logline(smp->strm, smp->strm->unique_id,
10439 UNIQUEID_LEN, &smp->sess->fe->format_unique_id);
10440
10441 smp->data.type = SMP_T_STR;
10442 smp->data.u.str.str = smp->strm->unique_id;
10443 smp->flags = SMP_F_CONST;
10444 return 1;
10445}
10446
Willy Tarreaua5910cc2015-05-02 00:46:08 +020010447/* returns the longest available part of the body. This requires that the body
10448 * has been waited for using http-buffer-request.
10449 */
10450static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010451smp_fetch_body(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreaua5910cc2015-05-02 00:46:08 +020010452{
Willy Tarreaua5910cc2015-05-02 00:46:08 +020010453 struct http_msg *msg;
10454 unsigned long len;
10455 unsigned long block1;
10456 char *body;
10457 struct chunk *temp;
10458
10459 CHECK_HTTP_MESSAGE_FIRST();
10460
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010461 if ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ)
Willy Tarreaube508f12016-03-10 11:47:01 +010010462 msg = &smp->strm->txn->req;
Willy Tarreaua5910cc2015-05-02 00:46:08 +020010463 else
Willy Tarreaube508f12016-03-10 11:47:01 +010010464 msg = &smp->strm->txn->rsp;
Willy Tarreaua5910cc2015-05-02 00:46:08 +020010465
10466 len = http_body_bytes(msg);
10467 body = b_ptr(msg->chn->buf, -http_data_rewind(msg));
10468
10469 block1 = len;
10470 if (block1 > msg->chn->buf->data + msg->chn->buf->size - body)
10471 block1 = msg->chn->buf->data + msg->chn->buf->size - body;
10472
10473 if (block1 == len) {
10474 /* buffer is not wrapped (or empty) */
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010475 smp->data.type = SMP_T_BIN;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010476 smp->data.u.str.str = body;
10477 smp->data.u.str.len = len;
Willy Tarreaua5910cc2015-05-02 00:46:08 +020010478 smp->flags = SMP_F_VOL_TEST | SMP_F_CONST;
10479 }
10480 else {
10481 /* buffer is wrapped, we need to defragment it */
10482 temp = get_trash_chunk();
10483 memcpy(temp->str, body, block1);
10484 memcpy(temp->str + block1, msg->chn->buf->data, len - block1);
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010485 smp->data.type = SMP_T_BIN;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010486 smp->data.u.str.str = temp->str;
10487 smp->data.u.str.len = len;
Willy Tarreaua5910cc2015-05-02 00:46:08 +020010488 smp->flags = SMP_F_VOL_TEST;
10489 }
10490 return 1;
10491}
10492
10493
10494/* returns the available length of the body. This requires that the body
10495 * has been waited for using http-buffer-request.
10496 */
10497static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010498smp_fetch_body_len(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreaua5910cc2015-05-02 00:46:08 +020010499{
Willy Tarreaua5910cc2015-05-02 00:46:08 +020010500 struct http_msg *msg;
10501
10502 CHECK_HTTP_MESSAGE_FIRST();
10503
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010504 if ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ)
Willy Tarreaube508f12016-03-10 11:47:01 +010010505 msg = &smp->strm->txn->req;
Willy Tarreaua5910cc2015-05-02 00:46:08 +020010506 else
Willy Tarreaube508f12016-03-10 11:47:01 +010010507 msg = &smp->strm->txn->rsp;
Willy Tarreaua5910cc2015-05-02 00:46:08 +020010508
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010509 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010510 smp->data.u.sint = http_body_bytes(msg);
Willy Tarreaua5910cc2015-05-02 00:46:08 +020010511
10512 smp->flags = SMP_F_VOL_TEST;
10513 return 1;
10514}
10515
10516
10517/* returns the advertised length of the body, or the advertised size of the
10518 * chunks available in the buffer. This requires that the body has been waited
10519 * for using http-buffer-request.
10520 */
10521static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010522smp_fetch_body_size(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreaua5910cc2015-05-02 00:46:08 +020010523{
Willy Tarreaua5910cc2015-05-02 00:46:08 +020010524 struct http_msg *msg;
10525
10526 CHECK_HTTP_MESSAGE_FIRST();
10527
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010528 if ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ)
Willy Tarreaube508f12016-03-10 11:47:01 +010010529 msg = &smp->strm->txn->req;
Willy Tarreaua5910cc2015-05-02 00:46:08 +020010530 else
Willy Tarreaube508f12016-03-10 11:47:01 +010010531 msg = &smp->strm->txn->rsp;
Willy Tarreaua5910cc2015-05-02 00:46:08 +020010532
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010533 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010534 smp->data.u.sint = msg->body_len;
Willy Tarreaua5910cc2015-05-02 00:46:08 +020010535
10536 smp->flags = SMP_F_VOL_TEST;
10537 return 1;
10538}
10539
10540
Willy Tarreau8797c062007-05-07 00:55:35 +020010541/* 4. Check on URL/URI. A pointer to the URI is stored. */
Willy Tarreaud41f8d82007-06-10 10:06:18 +020010542static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010543smp_fetch_url(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau8797c062007-05-07 00:55:35 +020010544{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010545 struct http_txn *txn;
Willy Tarreau8797c062007-05-07 00:55:35 +020010546
Willy Tarreauc0239e02012-04-16 14:42:55 +020010547 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +020010548
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010549 txn = smp->strm->txn;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010550 smp->data.type = SMP_T_STR;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010551 smp->data.u.str.len = txn->req.sl.rq.u_l;
10552 smp->data.u.str.str = txn->req.chn->buf->p + txn->req.sl.rq.u;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010553 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
Willy Tarreau8797c062007-05-07 00:55:35 +020010554 return 1;
10555}
10556
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010557static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010558smp_fetch_url_ip(const struct arg *args, struct sample *smp, const char *kw, void *private)
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010559{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010560 struct http_txn *txn;
Willy Tarreau4c804ec2013-09-30 14:37:14 +020010561 struct sockaddr_storage addr;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010562
Willy Tarreauc0239e02012-04-16 14:42:55 +020010563 CHECK_HTTP_MESSAGE_FIRST();
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010564
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010565 txn = smp->strm->txn;
Thierry FOURNIER9f95e402014-03-21 14:51:46 +010010566 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 +020010567 if (((struct sockaddr_in *)&addr)->sin_family != AF_INET)
Willy Tarreauf4362b32011-12-16 17:49:52 +010010568 return 0;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010569
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010570 smp->data.type = SMP_T_IPV4;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010571 smp->data.u.ipv4 = ((struct sockaddr_in *)&addr)->sin_addr;
Willy Tarreau37406352012-04-23 16:16:37 +020010572 smp->flags = 0;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010573 return 1;
10574}
10575
10576static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010577smp_fetch_url_port(const struct arg *args, struct sample *smp, const char *kw, void *private)
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010578{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010579 struct http_txn *txn;
Willy Tarreau4c804ec2013-09-30 14:37:14 +020010580 struct sockaddr_storage addr;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010581
Willy Tarreauc0239e02012-04-16 14:42:55 +020010582 CHECK_HTTP_MESSAGE_FIRST();
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010583
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010584 txn = smp->strm->txn;
Thierry FOURNIER9f95e402014-03-21 14:51:46 +010010585 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 +020010586 if (((struct sockaddr_in *)&addr)->sin_family != AF_INET)
10587 return 0;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010588
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010589 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010590 smp->data.u.sint = ntohs(((struct sockaddr_in *)&addr)->sin_port);
Willy Tarreau21e5b0e2012-04-23 19:25:44 +020010591 smp->flags = 0;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010592 return 1;
10593}
10594
Willy Tarreau185b5c42012-04-26 15:11:51 +020010595/* Fetch an HTTP header. A pointer to the beginning of the value is returned.
10596 * Accepts an optional argument of type string containing the header field name,
10597 * and an optional argument of type signed or unsigned integer to request an
10598 * explicit occurrence of the header. Note that in the event of a missing name,
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010599 * headers are considered from the first one. It does not stop on commas and
10600 * returns full lines instead (useful for User-Agent or Date for example).
10601 */
10602static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010603smp_fetch_fhdr(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010604{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010605 struct hdr_idx *idx;
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010606 struct hdr_ctx *ctx = smp->ctx.a[0];
Willy Tarreau15e91e12015-04-04 00:52:09 +020010607 const struct http_msg *msg;
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010608 int occ = 0;
10609 const char *name_str = NULL;
10610 int name_len = 0;
10611
10612 if (!ctx) {
10613 /* first call */
10614 ctx = &static_hdr_ctx;
10615 ctx->idx = 0;
10616 smp->ctx.a[0] = ctx;
10617 }
10618
10619 if (args) {
10620 if (args[0].type != ARGT_STR)
10621 return 0;
10622 name_str = args[0].data.str.str;
10623 name_len = args[0].data.str.len;
10624
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020010625 if (args[1].type == ARGT_SINT)
10626 occ = args[1].data.sint;
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010627 }
10628
10629 CHECK_HTTP_MESSAGE_FIRST();
10630
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010631 idx = &smp->strm->txn->hdr_idx;
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010632 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 +020010633
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010634 if (ctx && !(smp->flags & SMP_F_NOT_LAST))
10635 /* search for header from the beginning */
10636 ctx->idx = 0;
10637
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010638 if (!occ && !(smp->opt & SMP_OPT_ITERATE))
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010639 /* no explicit occurrence and single fetch => last header by default */
10640 occ = -1;
10641
10642 if (!occ)
10643 /* prepare to report multiple occurrences for ACL fetches */
10644 smp->flags |= SMP_F_NOT_LAST;
10645
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010646 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010647 smp->flags |= SMP_F_VOL_HDR | SMP_F_CONST;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010648 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 +020010649 return 1;
10650
10651 smp->flags &= ~SMP_F_NOT_LAST;
10652 return 0;
10653}
10654
10655/* 6. Check on HTTP header count. The number of occurrences is returned.
10656 * Accepts exactly 1 argument of type string. It does not stop on commas and
10657 * returns full lines instead (useful for User-Agent or Date for example).
10658 */
10659static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010660smp_fetch_fhdr_cnt(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010661{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010662 struct hdr_idx *idx;
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010663 struct hdr_ctx ctx;
Willy Tarreau15e91e12015-04-04 00:52:09 +020010664 const struct http_msg *msg;
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010665 int cnt;
Willy Tarreau601a4d12015-04-01 19:16:09 +020010666 const char *name = NULL;
10667 int len = 0;
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010668
Willy Tarreau601a4d12015-04-01 19:16:09 +020010669 if (args && args->type == ARGT_STR) {
10670 name = args->data.str.str;
10671 len = args->data.str.len;
10672 }
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010673
10674 CHECK_HTTP_MESSAGE_FIRST();
10675
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010676 idx = &smp->strm->txn->hdr_idx;
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010677 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 +020010678
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010679 ctx.idx = 0;
10680 cnt = 0;
Willy Tarreau601a4d12015-04-01 19:16:09 +020010681 while (http_find_full_header2(name, len, msg->chn->buf->p, idx, &ctx))
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010682 cnt++;
10683
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010684 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010685 smp->data.u.sint = cnt;
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010686 smp->flags = SMP_F_VOL_HDR;
10687 return 1;
10688}
10689
Willy Tarreaueb27ec72015-02-20 13:55:29 +010010690static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010691smp_fetch_hdr_names(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreaueb27ec72015-02-20 13:55:29 +010010692{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010693 struct hdr_idx *idx;
Willy Tarreaueb27ec72015-02-20 13:55:29 +010010694 struct hdr_ctx ctx;
Willy Tarreau15e91e12015-04-04 00:52:09 +020010695 const struct http_msg *msg;
Willy Tarreaueb27ec72015-02-20 13:55:29 +010010696 struct chunk *temp;
10697 char del = ',';
10698
10699 if (args && args->type == ARGT_STR)
10700 del = *args[0].data.str.str;
10701
10702 CHECK_HTTP_MESSAGE_FIRST();
10703
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010704 idx = &smp->strm->txn->hdr_idx;
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010705 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 +020010706
Willy Tarreaueb27ec72015-02-20 13:55:29 +010010707 temp = get_trash_chunk();
10708
10709 ctx.idx = 0;
10710 while (http_find_next_header(msg->chn->buf->p, idx, &ctx)) {
10711 if (temp->len)
10712 temp->str[temp->len++] = del;
10713 memcpy(temp->str + temp->len, ctx.line, ctx.del);
10714 temp->len += ctx.del;
10715 }
10716
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010717 smp->data.type = SMP_T_STR;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010718 smp->data.u.str.str = temp->str;
10719 smp->data.u.str.len = temp->len;
Willy Tarreaueb27ec72015-02-20 13:55:29 +010010720 smp->flags = SMP_F_VOL_HDR;
10721 return 1;
10722}
10723
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010724/* Fetch an HTTP header. A pointer to the beginning of the value is returned.
10725 * Accepts an optional argument of type string containing the header field name,
10726 * and an optional argument of type signed or unsigned integer to request an
10727 * explicit occurrence of the header. Note that in the event of a missing name,
Willy Tarreau185b5c42012-04-26 15:11:51 +020010728 * headers are considered from the first one.
Willy Tarreauc11416f2007-06-17 16:58:38 +020010729 */
Willy Tarreau33a7e692007-06-10 19:45:56 +020010730static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010731smp_fetch_hdr(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau33a7e692007-06-10 19:45:56 +020010732{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010733 struct hdr_idx *idx;
Willy Tarreaua890d072013-04-02 12:01:06 +020010734 struct hdr_ctx *ctx = smp->ctx.a[0];
Willy Tarreau15e91e12015-04-04 00:52:09 +020010735 const struct http_msg *msg;
Willy Tarreau185b5c42012-04-26 15:11:51 +020010736 int occ = 0;
10737 const char *name_str = NULL;
10738 int name_len = 0;
Willy Tarreaue333ec92012-04-16 16:26:40 +020010739
Willy Tarreaua890d072013-04-02 12:01:06 +020010740 if (!ctx) {
10741 /* first call */
10742 ctx = &static_hdr_ctx;
10743 ctx->idx = 0;
Willy Tarreauffb6f082013-04-02 23:16:53 +020010744 smp->ctx.a[0] = ctx;
Willy Tarreaua890d072013-04-02 12:01:06 +020010745 }
10746
Willy Tarreau185b5c42012-04-26 15:11:51 +020010747 if (args) {
10748 if (args[0].type != ARGT_STR)
10749 return 0;
10750 name_str = args[0].data.str.str;
10751 name_len = args[0].data.str.len;
10752
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020010753 if (args[1].type == ARGT_SINT)
10754 occ = args[1].data.sint;
Willy Tarreau185b5c42012-04-26 15:11:51 +020010755 }
Willy Tarreau34db1082012-04-19 17:16:54 +020010756
Willy Tarreaue333ec92012-04-16 16:26:40 +020010757 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreau33a7e692007-06-10 19:45:56 +020010758
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010759 idx = &smp->strm->txn->hdr_idx;
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010760 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 +020010761
Willy Tarreau185b5c42012-04-26 15:11:51 +020010762 if (ctx && !(smp->flags & SMP_F_NOT_LAST))
Willy Tarreau33a7e692007-06-10 19:45:56 +020010763 /* search for header from the beginning */
10764 ctx->idx = 0;
10765
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010766 if (!occ && !(smp->opt & SMP_OPT_ITERATE))
Willy Tarreau185b5c42012-04-26 15:11:51 +020010767 /* no explicit occurrence and single fetch => last header by default */
10768 occ = -1;
10769
10770 if (!occ)
10771 /* prepare to report multiple occurrences for ACL fetches */
Willy Tarreau37406352012-04-23 16:16:37 +020010772 smp->flags |= SMP_F_NOT_LAST;
Willy Tarreau664092c2011-12-16 19:11:42 +010010773
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010774 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010775 smp->flags |= SMP_F_VOL_HDR | SMP_F_CONST;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010776 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 +020010777 return 1;
Willy Tarreau33a7e692007-06-10 19:45:56 +020010778
Willy Tarreau37406352012-04-23 16:16:37 +020010779 smp->flags &= ~SMP_F_NOT_LAST;
Willy Tarreau33a7e692007-06-10 19:45:56 +020010780 return 0;
10781}
10782
Willy Tarreauc11416f2007-06-17 16:58:38 +020010783/* 6. Check on HTTP header count. The number of occurrences is returned.
Willy Tarreau34db1082012-04-19 17:16:54 +020010784 * Accepts exactly 1 argument of type string.
Willy Tarreauc11416f2007-06-17 16:58:38 +020010785 */
10786static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010787smp_fetch_hdr_cnt(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau33a7e692007-06-10 19:45:56 +020010788{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010789 struct hdr_idx *idx;
Willy Tarreau33a7e692007-06-10 19:45:56 +020010790 struct hdr_ctx ctx;
Willy Tarreau15e91e12015-04-04 00:52:09 +020010791 const struct http_msg *msg;
Willy Tarreau33a7e692007-06-10 19:45:56 +020010792 int cnt;
Willy Tarreau601a4d12015-04-01 19:16:09 +020010793 const char *name = NULL;
10794 int len = 0;
Willy Tarreau8797c062007-05-07 00:55:35 +020010795
Willy Tarreau601a4d12015-04-01 19:16:09 +020010796 if (args && args->type == ARGT_STR) {
10797 name = args->data.str.str;
10798 len = args->data.str.len;
10799 }
Willy Tarreau34db1082012-04-19 17:16:54 +020010800
Willy Tarreaue333ec92012-04-16 16:26:40 +020010801 CHECK_HTTP_MESSAGE_FIRST();
10802
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010803 idx = &smp->strm->txn->hdr_idx;
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010804 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 +020010805
Willy Tarreau33a7e692007-06-10 19:45:56 +020010806 ctx.idx = 0;
10807 cnt = 0;
Willy Tarreau601a4d12015-04-01 19:16:09 +020010808 while (http_find_header2(name, len, msg->chn->buf->p, idx, &ctx))
Willy Tarreau33a7e692007-06-10 19:45:56 +020010809 cnt++;
10810
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010811 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010812 smp->data.u.sint = cnt;
Willy Tarreau37406352012-04-23 16:16:37 +020010813 smp->flags = SMP_F_VOL_HDR;
Willy Tarreau33a7e692007-06-10 19:45:56 +020010814 return 1;
10815}
10816
Willy Tarreau185b5c42012-04-26 15:11:51 +020010817/* Fetch an HTTP header's integer value. The integer value is returned. It
10818 * takes a mandatory argument of type string and an optional one of type int
10819 * to designate a specific occurrence. It returns an unsigned integer, which
10820 * may or may not be appropriate for everything.
Willy Tarreau33a7e692007-06-10 19:45:56 +020010821 */
10822static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010823smp_fetch_hdr_val(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau33a7e692007-06-10 19:45:56 +020010824{
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010825 int ret = smp_fetch_hdr(args, smp, kw, private);
Willy Tarreaue333ec92012-04-16 16:26:40 +020010826
Willy Tarreauf853c462012-04-23 18:53:56 +020010827 if (ret > 0) {
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010828 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010829 smp->data.u.sint = strl2ic(smp->data.u.str.str, smp->data.u.str.len);
Willy Tarreauf853c462012-04-23 18:53:56 +020010830 }
Willy Tarreau33a7e692007-06-10 19:45:56 +020010831
Willy Tarreaud53e2422012-04-16 17:21:11 +020010832 return ret;
Willy Tarreau33a7e692007-06-10 19:45:56 +020010833}
10834
Cyril Bonté69fa9922012-10-25 00:01:06 +020010835/* Fetch an HTTP header's IP value. takes a mandatory argument of type string
10836 * and an optional one of type int to designate a specific occurrence.
10837 * It returns an IPv4 or IPv6 address.
Willy Tarreau106f9792009-09-19 07:54:16 +020010838 */
10839static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010840smp_fetch_hdr_ip(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau106f9792009-09-19 07:54:16 +020010841{
Willy Tarreaud53e2422012-04-16 17:21:11 +020010842 int ret;
Willy Tarreaue333ec92012-04-16 16:26:40 +020010843
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010844 while ((ret = smp_fetch_hdr(args, smp, kw, private)) > 0) {
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010845 if (url2ipv4((char *)smp->data.u.str.str, &smp->data.u.ipv4)) {
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010846 smp->data.type = SMP_T_IPV4;
Willy Tarreaud53e2422012-04-16 17:21:11 +020010847 break;
Cyril Bonté69fa9922012-10-25 00:01:06 +020010848 } else {
Willy Tarreau47ca5452012-12-23 20:22:19 +010010849 struct chunk *temp = get_trash_chunk();
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010850 if (smp->data.u.str.len < temp->size - 1) {
10851 memcpy(temp->str, smp->data.u.str.str, smp->data.u.str.len);
10852 temp->str[smp->data.u.str.len] = '\0';
10853 if (inet_pton(AF_INET6, temp->str, &smp->data.u.ipv6)) {
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010854 smp->data.type = SMP_T_IPV6;
Cyril Bonté69fa9922012-10-25 00:01:06 +020010855 break;
10856 }
10857 }
10858 }
10859
Willy Tarreaud53e2422012-04-16 17:21:11 +020010860 /* if the header doesn't match an IP address, fetch next one */
Willy Tarreau185b5c42012-04-26 15:11:51 +020010861 if (!(smp->flags & SMP_F_NOT_LAST))
10862 return 0;
Willy Tarreau106f9792009-09-19 07:54:16 +020010863 }
Willy Tarreaud53e2422012-04-16 17:21:11 +020010864 return ret;
Willy Tarreau106f9792009-09-19 07:54:16 +020010865}
10866
Willy Tarreau737b0c12007-06-10 21:28:46 +020010867/* 8. Check on URI PATH. A pointer to the PATH is stored. The path starts at
10868 * the first '/' after the possible hostname, and ends before the possible '?'.
10869 */
10870static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010871smp_fetch_path(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau737b0c12007-06-10 21:28:46 +020010872{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010873 struct http_txn *txn;
Willy Tarreau737b0c12007-06-10 21:28:46 +020010874 char *ptr, *end;
Willy Tarreau33a7e692007-06-10 19:45:56 +020010875
Willy Tarreauc0239e02012-04-16 14:42:55 +020010876 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +020010877
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010878 txn = smp->strm->txn;
Willy Tarreau9b28e032012-10-12 23:49:43 +020010879 end = txn->req.chn->buf->p + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
Willy Tarreau21d2af32008-02-14 20:25:24 +010010880 ptr = http_get_path(txn);
10881 if (!ptr)
Willy Tarreau737b0c12007-06-10 21:28:46 +020010882 return 0;
10883
10884 /* OK, we got the '/' ! */
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010885 smp->data.type = SMP_T_STR;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010886 smp->data.u.str.str = ptr;
Willy Tarreau737b0c12007-06-10 21:28:46 +020010887
10888 while (ptr < end && *ptr != '?')
10889 ptr++;
10890
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010891 smp->data.u.str.len = ptr - smp->data.u.str.str;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010892 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
Willy Tarreau737b0c12007-06-10 21:28:46 +020010893 return 1;
10894}
10895
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010896/* This produces a concatenation of the first occurrence of the Host header
10897 * followed by the path component if it begins with a slash ('/'). This means
10898 * that '*' will not be added, resulting in exactly the first Host entry.
10899 * If no Host header is found, then the path is returned as-is. The returned
10900 * value is stored in the trash so it does not need to be marked constant.
Willy Tarreaub169eba2013-12-16 15:14:43 +010010901 * The returned sample is of type string.
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010902 */
10903static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010904smp_fetch_base(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010905{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010906 struct http_txn *txn;
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010907 char *ptr, *end, *beg;
10908 struct hdr_ctx ctx;
Willy Tarreau3caf2af2014-06-24 17:27:02 +020010909 struct chunk *temp;
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010910
10911 CHECK_HTTP_MESSAGE_FIRST();
10912
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010913 txn = smp->strm->txn;
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010914 ctx.idx = 0;
Willy Tarreau877e78d2013-04-07 18:48:08 +020010915 if (!http_find_header2("Host", 4, txn->req.chn->buf->p, &txn->hdr_idx, &ctx) || !ctx.vlen)
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010916 return smp_fetch_path(args, smp, kw, private);
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010917
10918 /* OK we have the header value in ctx.line+ctx.val for ctx.vlen bytes */
Willy Tarreau3caf2af2014-06-24 17:27:02 +020010919 temp = get_trash_chunk();
10920 memcpy(temp->str, ctx.line + ctx.val, ctx.vlen);
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010921 smp->data.type = SMP_T_STR;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010922 smp->data.u.str.str = temp->str;
10923 smp->data.u.str.len = ctx.vlen;
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010924
10925 /* now retrieve the path */
Willy Tarreau877e78d2013-04-07 18:48:08 +020010926 end = txn->req.chn->buf->p + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010927 beg = http_get_path(txn);
10928 if (!beg)
10929 beg = end;
10930
10931 for (ptr = beg; ptr < end && *ptr != '?'; ptr++);
10932
10933 if (beg < ptr && *beg == '/') {
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010934 memcpy(smp->data.u.str.str + smp->data.u.str.len, beg, ptr - beg);
10935 smp->data.u.str.len += ptr - beg;
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010936 }
10937
10938 smp->flags = SMP_F_VOL_1ST;
10939 return 1;
10940}
10941
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010942/* This produces a 32-bit hash of the concatenation of the first occurrence of
10943 * the Host header followed by the path component if it begins with a slash ('/').
10944 * This means that '*' will not be added, resulting in exactly the first Host
10945 * entry. If no Host header is found, then the path is used. The resulting value
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000010946 * is hashed using the path hash followed by a full avalanche hash and provides a
10947 * 32-bit integer value. This fetch is useful for tracking per-path activity on
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010948 * high-traffic sites without having to store whole paths.
10949 */
Thierry FOURNIER055b9d52014-07-15 16:11:07 +020010950int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010951smp_fetch_base32(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010952{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010953 struct http_txn *txn;
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010954 struct hdr_ctx ctx;
10955 unsigned int hash = 0;
10956 char *ptr, *beg, *end;
10957 int len;
10958
10959 CHECK_HTTP_MESSAGE_FIRST();
10960
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010961 txn = smp->strm->txn;
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010962 ctx.idx = 0;
Willy Tarreau877e78d2013-04-07 18:48:08 +020010963 if (http_find_header2("Host", 4, txn->req.chn->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010964 /* OK we have the header value in ctx.line+ctx.val for ctx.vlen bytes */
10965 ptr = ctx.line + ctx.val;
10966 len = ctx.vlen;
10967 while (len--)
10968 hash = *(ptr++) + (hash << 6) + (hash << 16) - hash;
10969 }
10970
10971 /* now retrieve the path */
Willy Tarreau877e78d2013-04-07 18:48:08 +020010972 end = txn->req.chn->buf->p + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010973 beg = http_get_path(txn);
10974 if (!beg)
10975 beg = end;
10976
10977 for (ptr = beg; ptr < end && *ptr != '?'; ptr++);
10978
10979 if (beg < ptr && *beg == '/') {
10980 while (beg < ptr)
10981 hash = *(beg++) + (hash << 6) + (hash << 16) - hash;
10982 }
10983 hash = full_hash(hash);
10984
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010985 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010986 smp->data.u.sint = hash;
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010987 smp->flags = SMP_F_VOL_1ST;
10988 return 1;
10989}
10990
Willy Tarreau4a550602012-12-09 14:53:32 +010010991/* This concatenates the source address with the 32-bit hash of the Host and
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000010992 * path as returned by smp_fetch_base32(). The idea is to have per-source and
10993 * per-path counters. The result is a binary block from 8 to 20 bytes depending
10994 * on the source address length. The path hash is stored before the address so
Willy Tarreau4a550602012-12-09 14:53:32 +010010995 * that in environments where IPv6 is insignificant, truncating the output to
10996 * 8 bytes would still work.
10997 */
10998static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010999smp_fetch_base32_src(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau4a550602012-12-09 14:53:32 +010011000{
Willy Tarreau47ca5452012-12-23 20:22:19 +010011001 struct chunk *temp;
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011002 struct connection *cli_conn = objt_conn(smp->sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +020011003
11004 if (!cli_conn)
11005 return 0;
Willy Tarreau4a550602012-12-09 14:53:32 +010011006
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011007 if (!smp_fetch_base32(args, smp, kw, private))
Willy Tarreau4a550602012-12-09 14:53:32 +010011008 return 0;
11009
Willy Tarreau47ca5452012-12-23 20:22:19 +010011010 temp = get_trash_chunk();
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011011 *(unsigned int *)temp->str = htonl(smp->data.u.sint);
Willy Tarreau5ad6e1d2014-07-15 21:34:06 +020011012 temp->len += sizeof(unsigned int);
Willy Tarreau4a550602012-12-09 14:53:32 +010011013
Willy Tarreaub363a1f2013-10-01 10:45:07 +020011014 switch (cli_conn->addr.from.ss_family) {
Willy Tarreau4a550602012-12-09 14:53:32 +010011015 case AF_INET:
Willy Tarreaub363a1f2013-10-01 10:45:07 +020011016 memcpy(temp->str + temp->len, &((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr, 4);
Willy Tarreau4a550602012-12-09 14:53:32 +010011017 temp->len += 4;
11018 break;
11019 case AF_INET6:
Willy Tarreaub363a1f2013-10-01 10:45:07 +020011020 memcpy(temp->str + temp->len, &((struct sockaddr_in6 *)&cli_conn->addr.from)->sin6_addr, 16);
Willy Tarreau4a550602012-12-09 14:53:32 +010011021 temp->len += 16;
11022 break;
11023 default:
11024 return 0;
11025 }
11026
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011027 smp->data.u.str = *temp;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011028 smp->data.type = SMP_T_BIN;
Willy Tarreau4a550602012-12-09 14:53:32 +010011029 return 1;
11030}
11031
Willy Tarreau49ad95c2015-01-19 15:06:26 +010011032/* Extracts the query string, which comes after the question mark '?'. If no
11033 * question mark is found, nothing is returned. Otherwise it returns a sample
11034 * of type string carrying the whole query string.
11035 */
11036static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011037smp_fetch_query(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau49ad95c2015-01-19 15:06:26 +010011038{
Willy Tarreau15e91e12015-04-04 00:52:09 +020011039 struct http_txn *txn;
Willy Tarreau49ad95c2015-01-19 15:06:26 +010011040 char *ptr, *end;
11041
11042 CHECK_HTTP_MESSAGE_FIRST();
11043
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011044 txn = smp->strm->txn;
Willy Tarreau49ad95c2015-01-19 15:06:26 +010011045 ptr = txn->req.chn->buf->p + txn->req.sl.rq.u;
11046 end = ptr + txn->req.sl.rq.u_l;
11047
11048 /* look up the '?' */
11049 do {
11050 if (ptr == end)
11051 return 0;
11052 } while (*ptr++ != '?');
11053
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011054 smp->data.type = SMP_T_STR;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011055 smp->data.u.str.str = ptr;
11056 smp->data.u.str.len = end - ptr;
Willy Tarreau49ad95c2015-01-19 15:06:26 +010011057 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
11058 return 1;
11059}
11060
Willy Tarreau2492d5b2009-07-11 00:06:00 +020011061static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011062smp_fetch_proto_http(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau2492d5b2009-07-11 00:06:00 +020011063{
Willy Tarreau2492d5b2009-07-11 00:06:00 +020011064 /* Note: hdr_idx.v cannot be NULL in this ACL because the ACL is tagged
11065 * as a layer7 ACL, which involves automatic allocation of hdr_idx.
11066 */
Willy Tarreau2492d5b2009-07-11 00:06:00 +020011067
Willy Tarreau24e32d82012-04-23 23:55:44 +020011068 CHECK_HTTP_MESSAGE_FIRST_PERM();
Willy Tarreau2492d5b2009-07-11 00:06:00 +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 = 1;
Willy Tarreau2492d5b2009-07-11 00:06:00 +020011072 return 1;
11073}
11074
Willy Tarreau7f18e522010-10-22 20:04:13 +020011075/* return a valid test if the current request is the first one on the connection */
11076static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011077smp_fetch_http_first_req(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau7f18e522010-10-22 20:04:13 +020011078{
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011079 smp->data.type = SMP_T_BOOL;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011080 smp->data.u.sint = !(smp->strm->txn->flags & TX_NOT_FIRST);
Willy Tarreau7f18e522010-10-22 20:04:13 +020011081 return 1;
11082}
11083
Willy Tarreau34db1082012-04-19 17:16:54 +020011084/* Accepts exactly 1 argument of type userlist */
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010011085static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011086smp_fetch_http_auth(const struct arg *args, struct sample *smp, const char *kw, void *private)
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010011087{
11088
Willy Tarreau24e32d82012-04-23 23:55:44 +020011089 if (!args || args->type != ARGT_USR)
Willy Tarreau34db1082012-04-19 17:16:54 +020011090 return 0;
11091
Willy Tarreauc0239e02012-04-16 14:42:55 +020011092 CHECK_HTTP_MESSAGE_FIRST();
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010011093
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011094 if (!get_http_auth(smp->strm))
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010011095 return 0;
11096
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011097 smp->data.type = SMP_T_BOOL;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011098 smp->data.u.sint = check_user(args->data.usr, smp->strm->txn->auth.user,
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011099 smp->strm->txn->auth.pass);
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010011100 return 1;
11101}
Willy Tarreau8797c062007-05-07 00:55:35 +020011102
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +020011103/* Accepts exactly 1 argument of type userlist */
11104static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011105smp_fetch_http_auth_grp(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +020011106{
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +020011107 if (!args || args->type != ARGT_USR)
11108 return 0;
11109
11110 CHECK_HTTP_MESSAGE_FIRST();
11111
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011112 if (!get_http_auth(smp->strm))
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +020011113 return 0;
11114
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +020011115 /* if the user does not belong to the userlist or has a wrong password,
11116 * report that it unconditionally does not match. Otherwise we return
Thierry FOURNIER9eec0a62014-01-22 18:38:02 +010011117 * a string containing the username.
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +020011118 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011119 if (!check_user(args->data.usr, smp->strm->txn->auth.user,
11120 smp->strm->txn->auth.pass))
Thierry FOURNIER9eec0a62014-01-22 18:38:02 +010011121 return 0;
11122
11123 /* pat_match_auth() will need the user list */
11124 smp->ctx.a[0] = args->data.usr;
11125
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011126 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011127 smp->flags = SMP_F_CONST;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011128 smp->data.u.str.str = smp->strm->txn->auth.user;
11129 smp->data.u.str.len = strlen(smp->strm->txn->auth.user);
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +020011130
11131 return 1;
11132}
11133
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011134/* Try to find the next occurrence of a cookie name in a cookie header value.
11135 * The lookup begins at <hdr>. The pointer and size of the next occurrence of
11136 * the cookie value is returned into *value and *value_l, and the function
11137 * returns a pointer to the next pointer to search from if the value was found.
11138 * Otherwise if the cookie was not found, NULL is returned and neither value
11139 * nor value_l are touched. The input <hdr> string should first point to the
11140 * header's value, and the <hdr_end> pointer must point to the first character
11141 * not part of the value. <list> must be non-zero if value may represent a list
11142 * of values (cookie headers). This makes it faster to abort parsing when no
11143 * list is expected.
11144 */
David Carlier4686f792015-09-25 14:10:50 +010011145char *
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011146extract_cookie_value(char *hdr, const char *hdr_end,
11147 char *cookie_name, size_t cookie_name_l, int list,
Willy Tarreau3fb818c2012-04-11 17:21:08 +020011148 char **value, int *value_l)
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011149{
11150 char *equal, *att_end, *att_beg, *val_beg, *val_end;
11151 char *next;
11152
11153 /* we search at least a cookie name followed by an equal, and more
11154 * generally something like this :
11155 * Cookie: NAME1 = VALUE 1 ; NAME2 = VALUE2 ; NAME3 = VALUE3\r\n
11156 */
11157 for (att_beg = hdr; att_beg + cookie_name_l + 1 < hdr_end; att_beg = next + 1) {
11158 /* Iterate through all cookies on this line */
11159
Willy Tarreau2235b262016-11-05 15:50:20 +010011160 while (att_beg < hdr_end && HTTP_IS_SPHT(*att_beg))
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011161 att_beg++;
11162
11163 /* find att_end : this is the first character after the last non
11164 * space before the equal. It may be equal to hdr_end.
11165 */
11166 equal = att_end = att_beg;
11167
11168 while (equal < hdr_end) {
11169 if (*equal == '=' || *equal == ';' || (list && *equal == ','))
11170 break;
Willy Tarreau2235b262016-11-05 15:50:20 +010011171 if (HTTP_IS_SPHT(*equal++))
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011172 continue;
11173 att_end = equal;
11174 }
11175
11176 /* here, <equal> points to '=', a delimitor or the end. <att_end>
11177 * is between <att_beg> and <equal>, both may be identical.
11178 */
11179
11180 /* look for end of cookie if there is an equal sign */
11181 if (equal < hdr_end && *equal == '=') {
11182 /* look for the beginning of the value */
11183 val_beg = equal + 1;
Willy Tarreau2235b262016-11-05 15:50:20 +010011184 while (val_beg < hdr_end && HTTP_IS_SPHT(*val_beg))
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011185 val_beg++;
11186
11187 /* find the end of the value, respecting quotes */
11188 next = find_cookie_value_end(val_beg, hdr_end);
11189
11190 /* make val_end point to the first white space or delimitor after the value */
11191 val_end = next;
Willy Tarreau2235b262016-11-05 15:50:20 +010011192 while (val_end > val_beg && HTTP_IS_SPHT(*(val_end - 1)))
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011193 val_end--;
11194 } else {
11195 val_beg = val_end = next = equal;
11196 }
11197
11198 /* We have nothing to do with attributes beginning with '$'. However,
11199 * they will automatically be removed if a header before them is removed,
11200 * since they're supposed to be linked together.
11201 */
11202 if (*att_beg == '$')
11203 continue;
11204
11205 /* Ignore cookies with no equal sign */
11206 if (equal == next)
11207 continue;
11208
11209 /* Now we have the cookie name between att_beg and att_end, and
11210 * its value between val_beg and val_end.
11211 */
11212
11213 if (att_end - att_beg == cookie_name_l &&
11214 memcmp(att_beg, cookie_name, cookie_name_l) == 0) {
11215 /* let's return this value and indicate where to go on from */
11216 *value = val_beg;
11217 *value_l = val_end - val_beg;
11218 return next + 1;
11219 }
11220
11221 /* Set-Cookie headers only have the name in the first attr=value part */
11222 if (!list)
11223 break;
11224 }
11225
11226 return NULL;
11227}
11228
William Lallemanda43ba4e2014-01-28 18:14:25 +010011229/* Fetch a captured HTTP request header. The index is the position of
11230 * the "capture" option in the configuration file
11231 */
11232static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011233smp_fetch_capture_header_req(const struct arg *args, struct sample *smp, const char *kw, void *private)
William Lallemanda43ba4e2014-01-28 18:14:25 +010011234{
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011235 struct proxy *fe = strm_fe(smp->strm);
William Lallemanda43ba4e2014-01-28 18:14:25 +010011236 int idx;
11237
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020011238 if (!args || args->type != ARGT_SINT)
William Lallemanda43ba4e2014-01-28 18:14:25 +010011239 return 0;
11240
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020011241 idx = args->data.sint;
William Lallemanda43ba4e2014-01-28 18:14:25 +010011242
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011243 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 +010011244 return 0;
11245
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011246 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011247 smp->flags |= SMP_F_CONST;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011248 smp->data.u.str.str = smp->strm->req_cap[idx];
11249 smp->data.u.str.len = strlen(smp->strm->req_cap[idx]);
William Lallemanda43ba4e2014-01-28 18:14:25 +010011250
11251 return 1;
11252}
11253
11254/* Fetch a captured HTTP response header. The index is the position of
11255 * the "capture" option in the configuration file
11256 */
11257static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011258smp_fetch_capture_header_res(const struct arg *args, struct sample *smp, const char *kw, void *private)
William Lallemanda43ba4e2014-01-28 18:14:25 +010011259{
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011260 struct proxy *fe = strm_fe(smp->strm);
William Lallemanda43ba4e2014-01-28 18:14:25 +010011261 int idx;
11262
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020011263 if (!args || args->type != ARGT_SINT)
William Lallemanda43ba4e2014-01-28 18:14:25 +010011264 return 0;
11265
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020011266 idx = args->data.sint;
William Lallemanda43ba4e2014-01-28 18:14:25 +010011267
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011268 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 +010011269 return 0;
11270
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011271 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011272 smp->flags |= SMP_F_CONST;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011273 smp->data.u.str.str = smp->strm->res_cap[idx];
11274 smp->data.u.str.len = strlen(smp->strm->res_cap[idx]);
William Lallemanda43ba4e2014-01-28 18:14:25 +010011275
11276 return 1;
11277}
11278
William Lallemand65ad6e12014-01-31 15:08:02 +010011279/* Extracts the METHOD in the HTTP request, the txn->uri should be filled before the call */
11280static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011281smp_fetch_capture_req_method(const struct arg *args, struct sample *smp, const char *kw, void *private)
William Lallemand65ad6e12014-01-31 15:08:02 +010011282{
11283 struct chunk *temp;
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011284 struct http_txn *txn = smp->strm->txn;
William Lallemand96a77852014-02-05 00:30:02 +010011285 char *ptr;
William Lallemand65ad6e12014-01-31 15:08:02 +010011286
Willy Tarreau15e91e12015-04-04 00:52:09 +020011287 if (!txn || !txn->uri)
William Lallemand65ad6e12014-01-31 15:08:02 +010011288 return 0;
11289
William Lallemand96a77852014-02-05 00:30:02 +010011290 ptr = txn->uri;
William Lallemand65ad6e12014-01-31 15:08:02 +010011291
William Lallemand96a77852014-02-05 00:30:02 +010011292 while (*ptr != ' ' && *ptr != '\0') /* find first space */
11293 ptr++;
William Lallemand65ad6e12014-01-31 15:08:02 +010011294
William Lallemand96a77852014-02-05 00:30:02 +010011295 temp = get_trash_chunk();
11296 temp->str = txn->uri;
11297 temp->len = ptr - txn->uri;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011298 smp->data.u.str = *temp;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011299 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011300 smp->flags = SMP_F_CONST;
William Lallemand65ad6e12014-01-31 15:08:02 +010011301
11302 return 1;
11303
11304}
11305
11306/* Extracts the path in the HTTP request, the txn->uri should be filled before the call */
11307static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011308smp_fetch_capture_req_uri(const struct arg *args, struct sample *smp, const char *kw, void *private)
William Lallemand65ad6e12014-01-31 15:08:02 +010011309{
11310 struct chunk *temp;
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011311 struct http_txn *txn = smp->strm->txn;
William Lallemand65ad6e12014-01-31 15:08:02 +010011312 char *ptr;
William Lallemand65ad6e12014-01-31 15:08:02 +010011313
Willy Tarreau15e91e12015-04-04 00:52:09 +020011314 if (!txn || !txn->uri)
William Lallemand65ad6e12014-01-31 15:08:02 +010011315 return 0;
William Lallemand96a77852014-02-05 00:30:02 +010011316
William Lallemand65ad6e12014-01-31 15:08:02 +010011317 ptr = txn->uri;
11318
11319 while (*ptr != ' ' && *ptr != '\0') /* find first space */
11320 ptr++;
William Lallemand96a77852014-02-05 00:30:02 +010011321
William Lallemand65ad6e12014-01-31 15:08:02 +010011322 if (!*ptr)
11323 return 0;
11324
11325 ptr++; /* skip the space */
11326
11327 temp = get_trash_chunk();
William Lallemand96a77852014-02-05 00:30:02 +010011328 ptr = temp->str = http_get_path_from_string(ptr);
William Lallemand65ad6e12014-01-31 15:08:02 +010011329 if (!ptr)
11330 return 0;
11331 while (*ptr != ' ' && *ptr != '\0') /* find space after URI */
11332 ptr++;
William Lallemand65ad6e12014-01-31 15:08:02 +010011333
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011334 smp->data.u.str = *temp;
11335 smp->data.u.str.len = ptr - temp->str;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011336 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011337 smp->flags = SMP_F_CONST;
William Lallemand65ad6e12014-01-31 15:08:02 +010011338
11339 return 1;
11340}
11341
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020011342/* Retrieves the HTTP version from the request (either 1.0 or 1.1) and emits it
11343 * as a string (either "HTTP/1.0" or "HTTP/1.1").
11344 */
11345static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011346smp_fetch_capture_req_ver(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020011347{
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011348 struct http_txn *txn = smp->strm->txn;
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020011349
Willy Tarreau15e91e12015-04-04 00:52:09 +020011350 if (!txn || txn->req.msg_state < HTTP_MSG_HDR_FIRST)
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020011351 return 0;
11352
11353 if (txn->req.flags & HTTP_MSGF_VER_11)
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011354 smp->data.u.str.str = "HTTP/1.1";
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020011355 else
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011356 smp->data.u.str.str = "HTTP/1.0";
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020011357
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011358 smp->data.u.str.len = 8;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011359 smp->data.type = SMP_T_STR;
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020011360 smp->flags = SMP_F_CONST;
11361 return 1;
11362
11363}
11364
11365/* Retrieves the HTTP version from the response (either 1.0 or 1.1) and emits it
11366 * as a string (either "HTTP/1.0" or "HTTP/1.1").
11367 */
11368static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011369smp_fetch_capture_res_ver(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020011370{
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011371 struct http_txn *txn = smp->strm->txn;
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020011372
Willy Tarreau15e91e12015-04-04 00:52:09 +020011373 if (!txn || txn->rsp.msg_state < HTTP_MSG_HDR_FIRST)
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020011374 return 0;
11375
11376 if (txn->rsp.flags & HTTP_MSGF_VER_11)
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011377 smp->data.u.str.str = "HTTP/1.1";
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020011378 else
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011379 smp->data.u.str.str = "HTTP/1.0";
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020011380
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011381 smp->data.u.str.len = 8;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011382 smp->data.type = SMP_T_STR;
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020011383 smp->flags = SMP_F_CONST;
11384 return 1;
11385
11386}
11387
William Lallemand65ad6e12014-01-31 15:08:02 +010011388
Willy Tarreaue333ec92012-04-16 16:26:40 +020011389/* Iterate over all cookies present in a message. The context is stored in
Willy Tarreau37406352012-04-23 16:16:37 +020011390 * smp->ctx.a[0] for the in-header position, smp->ctx.a[1] for the
Willy Tarreaua890d072013-04-02 12:01:06 +020011391 * end-of-header-value, and smp->ctx.a[2] for the hdr_ctx. Depending on
Willy Tarreaue333ec92012-04-16 16:26:40 +020011392 * the direction, multiple cookies may be parsed on the same line or not.
Willy Tarreau24e32d82012-04-23 23:55:44 +020011393 * The cookie name is in args and the name length in args->data.str.len.
Willy Tarreau28376d62012-04-26 21:26:10 +020011394 * Accepts exactly 1 argument of type string. If the input options indicate
11395 * that no iterating is desired, then only last value is fetched if any.
William Lallemand07c8b242014-05-02 17:11:07 +020011396 * The returned sample is of type CSTR. Can be used to parse cookies in other
11397 * files.
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011398 */
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011399int smp_fetch_cookie(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011400{
Willy Tarreau15e91e12015-04-04 00:52:09 +020011401 struct http_txn *txn;
11402 struct hdr_idx *idx;
Willy Tarreaua890d072013-04-02 12:01:06 +020011403 struct hdr_ctx *ctx = smp->ctx.a[2];
Willy Tarreaue333ec92012-04-16 16:26:40 +020011404 const struct http_msg *msg;
11405 const char *hdr_name;
11406 int hdr_name_len;
11407 char *sol;
Willy Tarreau28376d62012-04-26 21:26:10 +020011408 int occ = 0;
11409 int found = 0;
Willy Tarreaue333ec92012-04-16 16:26:40 +020011410
Willy Tarreau24e32d82012-04-23 23:55:44 +020011411 if (!args || args->type != ARGT_STR)
Willy Tarreau34db1082012-04-19 17:16:54 +020011412 return 0;
11413
Willy Tarreaua890d072013-04-02 12:01:06 +020011414 if (!ctx) {
11415 /* first call */
11416 ctx = &static_hdr_ctx;
11417 ctx->idx = 0;
11418 smp->ctx.a[2] = ctx;
11419 }
11420
Willy Tarreaue333ec92012-04-16 16:26:40 +020011421 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011422
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011423 txn = smp->strm->txn;
11424 idx = &smp->strm->txn->hdr_idx;
Willy Tarreau15e91e12015-04-04 00:52:09 +020011425
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011426 if ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) {
Willy Tarreaue333ec92012-04-16 16:26:40 +020011427 msg = &txn->req;
11428 hdr_name = "Cookie";
11429 hdr_name_len = 6;
11430 } else {
11431 msg = &txn->rsp;
11432 hdr_name = "Set-Cookie";
11433 hdr_name_len = 10;
11434 }
11435
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011436 if (!occ && !(smp->opt & SMP_OPT_ITERATE))
Willy Tarreau28376d62012-04-26 21:26:10 +020011437 /* no explicit occurrence and single fetch => last cookie by default */
11438 occ = -1;
11439
11440 /* OK so basically here, either we want only one value and it's the
11441 * last one, or we want to iterate over all of them and we fetch the
11442 * next one.
11443 */
11444
Willy Tarreau9b28e032012-10-12 23:49:43 +020011445 sol = msg->chn->buf->p;
Willy Tarreau37406352012-04-23 16:16:37 +020011446 if (!(smp->flags & SMP_F_NOT_LAST)) {
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011447 /* search for the header from the beginning, we must first initialize
11448 * the search parameters.
11449 */
Willy Tarreau37406352012-04-23 16:16:37 +020011450 smp->ctx.a[0] = NULL;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011451 ctx->idx = 0;
11452 }
11453
Willy Tarreau28376d62012-04-26 21:26:10 +020011454 smp->flags |= SMP_F_VOL_HDR;
11455
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011456 while (1) {
Willy Tarreau37406352012-04-23 16:16:37 +020011457 /* Note: smp->ctx.a[0] == NULL every time we need to fetch a new header */
11458 if (!smp->ctx.a[0]) {
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011459 if (!http_find_header2(hdr_name, hdr_name_len, sol, idx, ctx))
11460 goto out;
11461
Willy Tarreau24e32d82012-04-23 23:55:44 +020011462 if (ctx->vlen < args->data.str.len + 1)
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011463 continue;
11464
Willy Tarreau37406352012-04-23 16:16:37 +020011465 smp->ctx.a[0] = ctx->line + ctx->val;
11466 smp->ctx.a[1] = smp->ctx.a[0] + ctx->vlen;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011467 }
11468
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011469 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011470 smp->flags |= SMP_F_CONST;
Willy Tarreau37406352012-04-23 16:16:37 +020011471 smp->ctx.a[0] = extract_cookie_value(smp->ctx.a[0], smp->ctx.a[1],
Willy Tarreau24e32d82012-04-23 23:55:44 +020011472 args->data.str.str, args->data.str.len,
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011473 (smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ,
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011474 &smp->data.u.str.str,
11475 &smp->data.u.str.len);
Willy Tarreau37406352012-04-23 16:16:37 +020011476 if (smp->ctx.a[0]) {
Willy Tarreau28376d62012-04-26 21:26:10 +020011477 found = 1;
11478 if (occ >= 0) {
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011479 /* one value was returned into smp->data.u.str.{str,len} */
Willy Tarreau28376d62012-04-26 21:26:10 +020011480 smp->flags |= SMP_F_NOT_LAST;
11481 return 1;
11482 }
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011483 }
Willy Tarreau28376d62012-04-26 21:26:10 +020011484 /* if we're looking for last occurrence, let's loop */
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011485 }
Willy Tarreau28376d62012-04-26 21:26:10 +020011486 /* all cookie headers and values were scanned. If we're looking for the
11487 * last occurrence, we may return it now.
11488 */
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011489 out:
Willy Tarreau37406352012-04-23 16:16:37 +020011490 smp->flags &= ~SMP_F_NOT_LAST;
Willy Tarreau28376d62012-04-26 21:26:10 +020011491 return found;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011492}
11493
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011494/* Iterate over all cookies present in a request to count how many occurrences
Willy Tarreau24e32d82012-04-23 23:55:44 +020011495 * match the name in args and args->data.str.len. If <multi> is non-null, then
Willy Tarreaub169eba2013-12-16 15:14:43 +010011496 * multiple cookies may be parsed on the same line. The returned sample is of
11497 * type UINT. Accepts exactly 1 argument of type string.
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011498 */
11499static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011500smp_fetch_cookie_cnt(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011501{
Willy Tarreau15e91e12015-04-04 00:52:09 +020011502 struct http_txn *txn;
11503 struct hdr_idx *idx;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011504 struct hdr_ctx ctx;
Willy Tarreaue333ec92012-04-16 16:26:40 +020011505 const struct http_msg *msg;
11506 const char *hdr_name;
11507 int hdr_name_len;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011508 int cnt;
11509 char *val_beg, *val_end;
Willy Tarreaue333ec92012-04-16 16:26:40 +020011510 char *sol;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011511
Willy Tarreau24e32d82012-04-23 23:55:44 +020011512 if (!args || args->type != ARGT_STR)
Willy Tarreau34db1082012-04-19 17:16:54 +020011513 return 0;
11514
Willy Tarreaue333ec92012-04-16 16:26:40 +020011515 CHECK_HTTP_MESSAGE_FIRST();
11516
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011517 txn = smp->strm->txn;
11518 idx = &smp->strm->txn->hdr_idx;
Willy Tarreau15e91e12015-04-04 00:52:09 +020011519
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011520 if ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) {
Willy Tarreaue333ec92012-04-16 16:26:40 +020011521 msg = &txn->req;
11522 hdr_name = "Cookie";
11523 hdr_name_len = 6;
11524 } else {
11525 msg = &txn->rsp;
11526 hdr_name = "Set-Cookie";
11527 hdr_name_len = 10;
11528 }
11529
Willy Tarreau9b28e032012-10-12 23:49:43 +020011530 sol = msg->chn->buf->p;
Willy Tarreau46787ed2012-04-11 17:28:40 +020011531 val_end = val_beg = NULL;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011532 ctx.idx = 0;
11533 cnt = 0;
11534
11535 while (1) {
11536 /* Note: val_beg == NULL every time we need to fetch a new header */
11537 if (!val_beg) {
11538 if (!http_find_header2(hdr_name, hdr_name_len, sol, idx, &ctx))
11539 break;
11540
Willy Tarreau24e32d82012-04-23 23:55:44 +020011541 if (ctx.vlen < args->data.str.len + 1)
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011542 continue;
11543
11544 val_beg = ctx.line + ctx.val;
11545 val_end = val_beg + ctx.vlen;
11546 }
11547
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011548 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011549 smp->flags |= SMP_F_CONST;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011550 while ((val_beg = extract_cookie_value(val_beg, val_end,
Willy Tarreau24e32d82012-04-23 23:55:44 +020011551 args->data.str.str, args->data.str.len,
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011552 (smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ,
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011553 &smp->data.u.str.str,
11554 &smp->data.u.str.len))) {
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011555 cnt++;
11556 }
11557 }
11558
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011559 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011560 smp->data.u.sint = cnt;
Willy Tarreau37406352012-04-23 16:16:37 +020011561 smp->flags |= SMP_F_VOL_HDR;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011562 return 1;
11563}
11564
Willy Tarreau51539362012-05-08 12:46:28 +020011565/* Fetch an cookie's integer value. The integer value is returned. It
11566 * takes a mandatory argument of type string. It relies on smp_fetch_cookie().
11567 */
11568static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011569smp_fetch_cookie_val(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau51539362012-05-08 12:46:28 +020011570{
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011571 int ret = smp_fetch_cookie(args, smp, kw, private);
Willy Tarreau51539362012-05-08 12:46:28 +020011572
11573 if (ret > 0) {
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011574 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011575 smp->data.u.sint = strl2ic(smp->data.u.str.str, smp->data.u.str.len);
Willy Tarreau51539362012-05-08 12:46:28 +020011576 }
11577
11578 return ret;
11579}
11580
Willy Tarreau8797c062007-05-07 00:55:35 +020011581/************************************************************************/
Willy Tarreau12785782012-04-27 21:37:17 +020011582/* The code below is dedicated to sample fetches */
Willy Tarreau4a568972010-05-12 08:08:50 +020011583/************************************************************************/
11584
David Cournapeau16023ee2010-12-23 20:55:41 +090011585/*
11586 * Given a path string and its length, find the position of beginning of the
11587 * query string. Returns NULL if no query string is found in the path.
11588 *
11589 * Example: if path = "/foo/bar/fubar?yo=mama;ye=daddy", and n = 22:
11590 *
Willy Tarreau1ede1da2015-05-07 16:06:18 +020011591 * find_query_string(path, n, '?') points to "yo=mama;ye=daddy" string.
David Cournapeau16023ee2010-12-23 20:55:41 +090011592 */
bedis4c75cca2012-10-05 08:38:24 +020011593static inline char *find_param_list(char *path, size_t path_l, char delim)
David Cournapeau16023ee2010-12-23 20:55:41 +090011594{
11595 char *p;
Emeric Brun485479d2010-09-23 18:02:19 +020011596
bedis4c75cca2012-10-05 08:38:24 +020011597 p = memchr(path, delim, path_l);
David Cournapeau16023ee2010-12-23 20:55:41 +090011598 return p ? p + 1 : NULL;
11599}
11600
bedis4c75cca2012-10-05 08:38:24 +020011601static inline int is_param_delimiter(char c, char delim)
David Cournapeau16023ee2010-12-23 20:55:41 +090011602{
bedis4c75cca2012-10-05 08:38:24 +020011603 return c == '&' || c == ';' || c == delim;
David Cournapeau16023ee2010-12-23 20:55:41 +090011604}
11605
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011606/* after increasing a pointer value, it can exceed the first buffer
11607 * size. This function transform the value of <ptr> according with
11608 * the expected position. <chunks> is an array of the one or two
11609 * avalaible chunks. The first value is the start of the first chunk,
11610 * the second value if the end+1 of the first chunks. The third value
11611 * is NULL or the start of the second chunk and the fourth value is
11612 * the end+1 of the second chunk. The function returns 1 if does a
11613 * wrap, else returns 0.
11614 */
11615static inline int fix_pointer_if_wrap(const char **chunks, const char **ptr)
11616{
11617 if (*ptr < chunks[1])
11618 return 0;
11619 if (!chunks[2])
11620 return 0;
11621 *ptr = chunks[2] + ( *ptr - chunks[1] );
11622 return 1;
11623}
11624
David Cournapeau16023ee2010-12-23 20:55:41 +090011625/*
11626 * Given a url parameter, find the starting position of the first occurence,
11627 * or NULL if the parameter is not found.
11628 *
11629 * Example: if query_string is "yo=mama;ye=daddy" and url_param_name is "ye",
11630 * the function will return query_string+8.
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011631 *
Willy Tarreauf6625822015-12-27 14:51:01 +010011632 * Warning: this function returns a pointer that can point to the first chunk
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011633 * or the second chunk. The caller must be check the position before using the
11634 * result.
David Cournapeau16023ee2010-12-23 20:55:41 +090011635 */
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011636static const char *
11637find_url_param_pos(const char **chunks,
Willy Tarreau1ede1da2015-05-07 16:06:18 +020011638 const char* url_param_name, size_t url_param_name_l,
bedis4c75cca2012-10-05 08:38:24 +020011639 char delim)
David Cournapeau16023ee2010-12-23 20:55:41 +090011640{
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011641 const char *pos, *last, *equal;
11642 const char **bufs = chunks;
11643 int l1, l2;
David Cournapeau16023ee2010-12-23 20:55:41 +090011644
David Cournapeau16023ee2010-12-23 20:55:41 +090011645
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011646 pos = bufs[0];
11647 last = bufs[1];
Willy Tarreauf6625822015-12-27 14:51:01 +010011648 while (pos < last) {
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011649 /* Check the equal. */
11650 equal = pos + url_param_name_l;
11651 if (fix_pointer_if_wrap(chunks, &equal)) {
11652 if (equal >= chunks[3])
11653 return NULL;
11654 } else {
11655 if (equal >= chunks[1])
11656 return NULL;
11657 }
11658 if (*equal == '=') {
11659 if (pos + url_param_name_l > last) {
11660 /* process wrap case, we detect a wrap. In this case, the
11661 * comparison is performed in two parts.
11662 */
11663
11664 /* This is the end, we dont have any other chunk. */
11665 if (bufs != chunks || !bufs[2])
11666 return NULL;
11667
11668 /* Compute the length of each part of the comparison. */
11669 l1 = last - pos;
11670 l2 = url_param_name_l - l1;
11671
11672 /* The second buffer is too short to contain the compared string. */
11673 if (bufs[2] + l2 > bufs[3])
11674 return NULL;
11675
11676 if (memcmp(pos, url_param_name, l1) == 0 &&
11677 memcmp(bufs[2], url_param_name+l1, l2) == 0)
11678 return pos;
11679
11680 /* Perform wrapping and jump the string who fail the comparison. */
11681 bufs += 2;
11682 pos = bufs[0] + l2;
11683 last = bufs[1];
11684
11685 } else {
11686 /* process a simple comparison. */
Willy Tarreauf6625822015-12-27 14:51:01 +010011687 if (memcmp(pos, url_param_name, url_param_name_l) == 0)
11688 return pos;
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011689 pos += url_param_name_l + 1;
11690 if (fix_pointer_if_wrap(chunks, &pos))
11691 last = bufs[2];
11692 }
11693 }
11694
11695 while (1) {
11696 /* Look for the next delimiter. */
Willy Tarreauf6625822015-12-27 14:51:01 +010011697 while (pos < last && !is_param_delimiter(*pos, delim))
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011698 pos++;
11699 if (pos < last)
11700 break;
11701 /* process buffer wrapping. */
11702 if (bufs != chunks || !bufs[2])
11703 return NULL;
11704 bufs += 2;
11705 pos = bufs[0];
11706 last = bufs[1];
David Cournapeau16023ee2010-12-23 20:55:41 +090011707 }
David Cournapeau16023ee2010-12-23 20:55:41 +090011708 pos++;
11709 }
11710 return NULL;
11711}
11712
11713/*
Cyril Bontéce1ef4d2015-11-26 21:39:56 +010011714 * Given a url parameter name and a query string, find the next value.
11715 * An empty url_param_name matches the first available parameter.
11716 * If the parameter is found, 1 is returned and *vstart / *vend are updated to
11717 * respectively provide a pointer to the value and its end.
11718 * Otherwise, 0 is returned and vstart/vend are not modified.
David Cournapeau16023ee2010-12-23 20:55:41 +090011719 */
11720static int
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011721find_next_url_param(const char **chunks,
Willy Tarreau1ede1da2015-05-07 16:06:18 +020011722 const char* url_param_name, size_t url_param_name_l,
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011723 const char **vstart, const char **vend, char delim)
David Cournapeau16023ee2010-12-23 20:55:41 +090011724{
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011725 const char *arg_start, *qs_end;
11726 const char *value_start, *value_end;
David Cournapeau16023ee2010-12-23 20:55:41 +090011727
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011728 arg_start = chunks[0];
11729 qs_end = chunks[1];
Willy Tarreau1ede1da2015-05-07 16:06:18 +020011730 if (url_param_name_l) {
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011731 /* Looks for an argument name. */
11732 arg_start = find_url_param_pos(chunks,
Willy Tarreau1ede1da2015-05-07 16:06:18 +020011733 url_param_name, url_param_name_l,
11734 delim);
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011735 /* Check for wrapping. */
Willy Tarreauf6625822015-12-27 14:51:01 +010011736 if (arg_start >= qs_end)
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011737 qs_end = chunks[3];
Willy Tarreau1ede1da2015-05-07 16:06:18 +020011738 }
David Cournapeau16023ee2010-12-23 20:55:41 +090011739 if (!arg_start)
11740 return 0;
11741
Willy Tarreau1ede1da2015-05-07 16:06:18 +020011742 if (!url_param_name_l) {
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011743 while (1) {
11744 /* looks for the first argument. */
11745 value_start = memchr(arg_start, '=', qs_end - arg_start);
11746 if (!value_start) {
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011747 /* Check for wrapping. */
11748 if (arg_start >= chunks[0] &&
Willy Tarreauf6625822015-12-27 14:51:01 +010011749 arg_start < chunks[1] &&
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011750 chunks[2]) {
11751 arg_start = chunks[2];
11752 qs_end = chunks[3];
11753 continue;
11754 }
11755 return 0;
11756 }
11757 break;
11758 }
Willy Tarreau1ede1da2015-05-07 16:06:18 +020011759 value_start++;
11760 }
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011761 else {
11762 /* Jump the argument length. */
Willy Tarreau1ede1da2015-05-07 16:06:18 +020011763 value_start = arg_start + url_param_name_l + 1;
11764
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011765 /* Check for pointer wrapping. */
11766 if (fix_pointer_if_wrap(chunks, &value_start)) {
11767 /* Update the end pointer. */
11768 qs_end = chunks[3];
11769
11770 /* Check for overflow. */
Willy Tarreauf6625822015-12-27 14:51:01 +010011771 if (value_start >= qs_end)
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011772 return 0;
11773 }
11774 }
11775
David Cournapeau16023ee2010-12-23 20:55:41 +090011776 value_end = value_start;
11777
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011778 while (1) {
11779 while ((value_end < qs_end) && !is_param_delimiter(*value_end, delim))
11780 value_end++;
11781 if (value_end < qs_end)
11782 break;
11783 /* process buffer wrapping. */
11784 if (value_end >= chunks[0] &&
Willy Tarreauf6625822015-12-27 14:51:01 +010011785 value_end < chunks[1] &&
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011786 chunks[2]) {
11787 value_end = chunks[2];
11788 qs_end = chunks[3];
11789 continue;
11790 }
11791 break;
11792 }
David Cournapeau16023ee2010-12-23 20:55:41 +090011793
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011794 *vstart = value_start;
11795 *vend = value_end;
Cyril Bontéce1ef4d2015-11-26 21:39:56 +010011796 return 1;
David Cournapeau16023ee2010-12-23 20:55:41 +090011797}
11798
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011799/* This scans a URL-encoded query string. It takes an optionally wrapping
11800 * string whose first contigous chunk has its beginning in ctx->a[0] and end
11801 * in ctx->a[1], and the optional second part in (ctx->a[2]..ctx->a[3]). The
11802 * pointers are updated for next iteration before leaving.
Thierry FOURNIER4fdc74c2015-05-19 14:46:23 +020011803 */
David Cournapeau16023ee2010-12-23 20:55:41 +090011804static int
Thierry FOURNIER4fdc74c2015-05-19 14:46:23 +020011805smp_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 +090011806{
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011807 const char *vstart, *vend;
11808 struct chunk *temp;
11809 const char **chunks = (const char **)smp->ctx.a;
bedis4c75cca2012-10-05 08:38:24 +020011810
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011811 if (!find_next_url_param(chunks,
Thierry FOURNIER0948d412015-05-20 15:22:37 +020011812 name, name_len,
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011813 &vstart, &vend,
Thierry FOURNIER0948d412015-05-20 15:22:37 +020011814 delim))
David Cournapeau16023ee2010-12-23 20:55:41 +090011815 return 0;
11816
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011817 /* Create sample. If the value is contiguous, return the pointer as CONST,
11818 * if the value is wrapped, copy-it in a buffer.
11819 */
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011820 smp->data.type = SMP_T_STR;
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011821 if (chunks[2] &&
11822 vstart >= chunks[0] && vstart <= chunks[1] &&
11823 vend >= chunks[2] && vend <= chunks[3]) {
11824 /* Wrapped case. */
11825 temp = get_trash_chunk();
11826 memcpy(temp->str, vstart, chunks[1] - vstart);
11827 memcpy(temp->str + ( chunks[1] - vstart ), chunks[2], vend - chunks[2]);
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011828 smp->data.u.str.str = temp->str;
11829 smp->data.u.str.len = ( chunks[1] - vstart ) + ( vend - chunks[2] );
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011830 } else {
11831 /* Contiguous case. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011832 smp->data.u.str.str = (char *)vstart;
11833 smp->data.u.str.len = vend - vstart;
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011834 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
11835 }
11836
11837 /* Update context, check wrapping. */
11838 chunks[0] = vend;
11839 if (chunks[2] && vend >= chunks[2] && vend <= chunks[3]) {
11840 chunks[1] = chunks[3];
11841 chunks[2] = NULL;
11842 }
Willy Tarreau1ede1da2015-05-07 16:06:18 +020011843
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011844 if (chunks[0] < chunks[1])
Willy Tarreau1ede1da2015-05-07 16:06:18 +020011845 smp->flags |= SMP_F_NOT_LAST;
11846
David Cournapeau16023ee2010-12-23 20:55:41 +090011847 return 1;
11848}
11849
Thierry FOURNIER4fdc74c2015-05-19 14:46:23 +020011850/* This function iterates over each parameter of the query string. It uses
11851 * ctx->a[0] and ctx->a[1] to store the beginning and end of the current
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011852 * parameter. Since it uses smp_fetch_param(), ctx->a[2..3] are both NULL.
11853 * An optional parameter name is passed in args[0], otherwise any parameter is
11854 * considered. It supports an optional delimiter argument for the beginning of
11855 * the string in args[1], which defaults to "?".
Thierry FOURNIER4fdc74c2015-05-19 14:46:23 +020011856 */
11857static int
11858smp_fetch_url_param(const struct arg *args, struct sample *smp, const char *kw, void *private)
11859{
11860 struct http_msg *msg;
11861 char delim = '?';
11862 const char *name;
11863 int name_len;
11864
Dragan Dosen26f77e52015-05-25 10:02:11 +020011865 if (!args ||
11866 (args[0].type && args[0].type != ARGT_STR) ||
11867 (args[1].type && args[1].type != ARGT_STR))
11868 return 0;
Thierry FOURNIER4fdc74c2015-05-19 14:46:23 +020011869
Dragan Dosen26f77e52015-05-25 10:02:11 +020011870 name = "";
11871 name_len = 0;
11872 if (args->type == ARGT_STR) {
11873 name = args->data.str.str;
11874 name_len = args->data.str.len;
11875 }
Thierry FOURNIER4fdc74c2015-05-19 14:46:23 +020011876
Dragan Dosen26f77e52015-05-25 10:02:11 +020011877 if (args[1].type)
11878 delim = *args[1].data.str.str;
Thierry FOURNIER4fdc74c2015-05-19 14:46:23 +020011879
Dragan Dosen26f77e52015-05-25 10:02:11 +020011880 if (!smp->ctx.a[0]) { // first call, find the query string
Thierry FOURNIER4fdc74c2015-05-19 14:46:23 +020011881 CHECK_HTTP_MESSAGE_FIRST();
11882
11883 msg = &smp->strm->txn->req;
11884
11885 smp->ctx.a[0] = find_param_list(msg->chn->buf->p + msg->sl.rq.u,
11886 msg->sl.rq.u_l, delim);
11887 if (!smp->ctx.a[0])
11888 return 0;
11889
11890 smp->ctx.a[1] = msg->chn->buf->p + msg->sl.rq.u + msg->sl.rq.u_l;
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011891
11892 /* Assume that the context is filled with NULL pointer
11893 * before the first call.
11894 * smp->ctx.a[2] = NULL;
11895 * smp->ctx.a[3] = NULL;
11896 */
Thierry FOURNIER4fdc74c2015-05-19 14:46:23 +020011897 }
11898
11899 return smp_fetch_param(delim, name, name_len, args, smp, kw, private);
11900}
11901
Thierry FOURNIERe28c4992015-05-19 14:45:09 +020011902/* This function iterates over each parameter of the body. This requires
11903 * that the body has been waited for using http-buffer-request. It uses
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011904 * ctx->a[0] and ctx->a[1] to store the beginning and end of the first
11905 * contigous part of the body, and optionally ctx->a[2..3] to reference the
11906 * optional second part if the body wraps at the end of the buffer. An optional
11907 * parameter name is passed in args[0], otherwise any parameter is considered.
Thierry FOURNIERe28c4992015-05-19 14:45:09 +020011908 */
11909static int
11910smp_fetch_body_param(const struct arg *args, struct sample *smp, const char *kw, void *private)
11911{
Thierry FOURNIERe28c4992015-05-19 14:45:09 +020011912 struct http_msg *msg;
11913 unsigned long len;
11914 unsigned long block1;
11915 char *body;
11916 const char *name;
11917 int name_len;
11918
11919 if (!args || (args[0].type && args[0].type != ARGT_STR))
11920 return 0;
11921
11922 name = "";
11923 name_len = 0;
11924 if (args[0].type == ARGT_STR) {
11925 name = args[0].data.str.str;
11926 name_len = args[0].data.str.len;
11927 }
11928
11929 if (!smp->ctx.a[0]) { // first call, find the query string
11930 CHECK_HTTP_MESSAGE_FIRST();
11931
11932 if ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ)
Willy Tarreaube508f12016-03-10 11:47:01 +010011933 msg = &smp->strm->txn->req;
Thierry FOURNIERe28c4992015-05-19 14:45:09 +020011934 else
Willy Tarreaube508f12016-03-10 11:47:01 +010011935 msg = &smp->strm->txn->rsp;
Thierry FOURNIERe28c4992015-05-19 14:45:09 +020011936
11937 len = http_body_bytes(msg);
11938 body = b_ptr(msg->chn->buf, -http_data_rewind(msg));
11939
11940 block1 = len;
11941 if (block1 > msg->chn->buf->data + msg->chn->buf->size - body)
11942 block1 = msg->chn->buf->data + msg->chn->buf->size - body;
11943
11944 if (block1 == len) {
11945 /* buffer is not wrapped (or empty) */
11946 smp->ctx.a[0] = body;
11947 smp->ctx.a[1] = body + len;
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011948
11949 /* Assume that the context is filled with NULL pointer
11950 * before the first call.
11951 * smp->ctx.a[2] = NULL;
11952 * smp->ctx.a[3] = NULL;
11953 */
Thierry FOURNIERe28c4992015-05-19 14:45:09 +020011954 }
11955 else {
11956 /* buffer is wrapped, we need to defragment it */
11957 smp->ctx.a[0] = body;
11958 smp->ctx.a[1] = body + block1;
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011959 smp->ctx.a[2] = msg->chn->buf->data;
11960 smp->ctx.a[3] = msg->chn->buf->data + ( len - block1 );
Thierry FOURNIERe28c4992015-05-19 14:45:09 +020011961 }
11962 }
11963 return smp_fetch_param('&', name, name_len, args, smp, kw, private);
11964}
11965
Willy Tarreaua9fddca2012-07-31 07:51:48 +020011966/* Return the signed integer value for the specified url parameter (see url_param
11967 * above).
11968 */
11969static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011970smp_fetch_url_param_val(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreaua9fddca2012-07-31 07:51:48 +020011971{
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011972 int ret = smp_fetch_url_param(args, smp, kw, private);
Willy Tarreaua9fddca2012-07-31 07:51:48 +020011973
11974 if (ret > 0) {
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011975 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011976 smp->data.u.sint = strl2ic(smp->data.u.str.str, smp->data.u.str.len);
Willy Tarreaua9fddca2012-07-31 07:51:48 +020011977 }
11978
11979 return ret;
11980}
11981
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011982/* This produces a 32-bit hash of the concatenation of the first occurrence of
11983 * the Host header followed by the path component if it begins with a slash ('/').
11984 * This means that '*' will not be added, resulting in exactly the first Host
11985 * entry. If no Host header is found, then the path is used. The resulting value
11986 * is hashed using the url hash followed by a full avalanche hash and provides a
11987 * 32-bit integer value. This fetch is useful for tracking per-URL activity on
11988 * high-traffic sites without having to store whole paths.
11989 * this differs from the base32 functions in that it includes the url parameters
11990 * as well as the path
11991 */
11992static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011993smp_fetch_url32(const struct arg *args, struct sample *smp, const char *kw, void *private)
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011994{
Willy Tarreau15e91e12015-04-04 00:52:09 +020011995 struct http_txn *txn;
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011996 struct hdr_ctx ctx;
11997 unsigned int hash = 0;
11998 char *ptr, *beg, *end;
11999 int len;
12000
12001 CHECK_HTTP_MESSAGE_FIRST();
12002
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020012003 txn = smp->strm->txn;
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000012004 ctx.idx = 0;
Willy Tarreau877e78d2013-04-07 18:48:08 +020012005 if (http_find_header2("Host", 4, txn->req.chn->buf->p, &txn->hdr_idx, &ctx)) {
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000012006 /* OK we have the header value in ctx.line+ctx.val for ctx.vlen bytes */
12007 ptr = ctx.line + ctx.val;
12008 len = ctx.vlen;
12009 while (len--)
12010 hash = *(ptr++) + (hash << 6) + (hash << 16) - hash;
12011 }
12012
12013 /* now retrieve the path */
Willy Tarreau877e78d2013-04-07 18:48:08 +020012014 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 +000012015 beg = http_get_path(txn);
12016 if (!beg)
12017 beg = end;
12018
12019 for (ptr = beg; ptr < end ; ptr++);
12020
12021 if (beg < ptr && *beg == '/') {
12022 while (beg < ptr)
12023 hash = *(beg++) + (hash << 6) + (hash << 16) - hash;
12024 }
12025 hash = full_hash(hash);
12026
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020012027 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012028 smp->data.u.sint = hash;
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000012029 smp->flags = SMP_F_VOL_1ST;
12030 return 1;
12031}
12032
12033/* This concatenates the source address with the 32-bit hash of the Host and
12034 * URL as returned by smp_fetch_base32(). The idea is to have per-source and
12035 * per-url counters. The result is a binary block from 8 to 20 bytes depending
12036 * on the source address length. The URL hash is stored before the address so
12037 * that in environments where IPv6 is insignificant, truncating the output to
12038 * 8 bytes would still work.
12039 */
12040static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020012041smp_fetch_url32_src(const struct arg *args, struct sample *smp, const char *kw, void *private)
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000012042{
12043 struct chunk *temp;
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020012044 struct connection *cli_conn = objt_conn(smp->sess->origin);
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000012045
Dragan Dosendb5af612016-06-16 11:23:01 +020012046 if (!cli_conn)
12047 return 0;
12048
Thierry FOURNIER0786d052015-05-11 15:42:45 +020012049 if (!smp_fetch_url32(args, smp, kw, private))
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000012050 return 0;
12051
12052 temp = get_trash_chunk();
Dragan Dosene5f41332016-06-16 11:08:08 +020012053 *(unsigned int *)temp->str = htonl(smp->data.u.sint);
12054 temp->len += sizeof(unsigned int);
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000012055
Willy Tarreaub363a1f2013-10-01 10:45:07 +020012056 switch (cli_conn->addr.from.ss_family) {
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000012057 case AF_INET:
Willy Tarreaub363a1f2013-10-01 10:45:07 +020012058 memcpy(temp->str + temp->len, &((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr, 4);
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000012059 temp->len += 4;
12060 break;
12061 case AF_INET6:
Willy Tarreaub363a1f2013-10-01 10:45:07 +020012062 memcpy(temp->str + temp->len, &((struct sockaddr_in6 *)&cli_conn->addr.from)->sin6_addr, 16);
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000012063 temp->len += 16;
12064 break;
12065 default:
12066 return 0;
12067 }
12068
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012069 smp->data.u.str = *temp;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020012070 smp->data.type = SMP_T_BIN;
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000012071 return 1;
12072}
12073
Willy Tarreau185b5c42012-04-26 15:11:51 +020012074/* This function is used to validate the arguments passed to any "hdr" fetch
12075 * keyword. These keywords support an optional positive or negative occurrence
12076 * number. We must ensure that the number is greater than -MAX_HDR_HISTORY. It
12077 * is assumed that the types are already the correct ones. Returns 0 on error,
12078 * non-zero if OK. If <err> is not NULL, it will be filled with a pointer to an
12079 * error message in case of error, that the caller is responsible for freeing.
12080 * The initial location must either be freeable or NULL.
12081 */
Thierry FOURNIER49f45af2014-12-08 19:50:43 +010012082int val_hdr(struct arg *arg, char **err_msg)
Willy Tarreau185b5c42012-04-26 15:11:51 +020012083{
12084 if (arg && arg[1].type == ARGT_SINT && arg[1].data.sint < -MAX_HDR_HISTORY) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +020012085 memprintf(err_msg, "header occurrence must be >= %d", -MAX_HDR_HISTORY);
Willy Tarreau185b5c42012-04-26 15:11:51 +020012086 return 0;
12087 }
12088 return 1;
12089}
12090
Willy Tarreau276fae92013-07-25 14:36:01 +020012091/* takes an UINT value on input supposed to represent the time since EPOCH,
12092 * adds an optional offset found in args[0] and emits a string representing
12093 * the date in RFC-1123/5322 format.
12094 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020012095static int sample_conv_http_date(const struct arg *args, struct sample *smp, void *private)
Willy Tarreau276fae92013-07-25 14:36:01 +020012096{
Cyril Bontéf78d8962016-01-22 19:40:28 +010012097 const char day[7][4] = { "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" };
Willy Tarreau276fae92013-07-25 14:36:01 +020012098 const char mon[12][4] = { "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" };
12099 struct chunk *temp;
12100 struct tm *tm;
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020012101 /* With high numbers, the date returned can be negative, the 55 bits mask prevent this. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012102 time_t curr_date = smp->data.u.sint & 0x007fffffffffffffLL;
Willy Tarreau276fae92013-07-25 14:36:01 +020012103
12104 /* add offset */
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020012105 if (args && (args[0].type == ARGT_SINT))
Willy Tarreau276fae92013-07-25 14:36:01 +020012106 curr_date += args[0].data.sint;
12107
12108 tm = gmtime(&curr_date);
Thierry FOURNIERfac9ccf2015-07-08 00:15:20 +020012109 if (!tm)
12110 return 0;
Willy Tarreau276fae92013-07-25 14:36:01 +020012111
12112 temp = get_trash_chunk();
12113 temp->len = snprintf(temp->str, temp->size - temp->len,
12114 "%s, %02d %s %04d %02d:%02d:%02d GMT",
12115 day[tm->tm_wday], tm->tm_mday, mon[tm->tm_mon], 1900+tm->tm_year,
12116 tm->tm_hour, tm->tm_min, tm->tm_sec);
12117
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012118 smp->data.u.str = *temp;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020012119 smp->data.type = SMP_T_STR;
Willy Tarreau276fae92013-07-25 14:36:01 +020012120 return 1;
12121}
12122
Thierry FOURNIERad903512014-04-11 17:51:01 +020012123/* Match language range with language tag. RFC2616 14.4:
12124 *
12125 * A language-range matches a language-tag if it exactly equals
12126 * the tag, or if it exactly equals a prefix of the tag such
12127 * that the first tag character following the prefix is "-".
12128 *
12129 * Return 1 if the strings match, else return 0.
12130 */
12131static inline int language_range_match(const char *range, int range_len,
12132 const char *tag, int tag_len)
12133{
12134 const char *end = range + range_len;
12135 const char *tend = tag + tag_len;
12136 while (range < end) {
12137 if (*range == '-' && tag == tend)
12138 return 1;
12139 if (*range != *tag || tag == tend)
12140 return 0;
12141 range++;
12142 tag++;
12143 }
12144 /* Return true only if the last char of the tag is matched. */
12145 return tag == tend;
12146}
12147
12148/* Arguments: The list of expected value, the number of parts returned and the separator */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020012149static int sample_conv_q_prefered(const struct arg *args, struct sample *smp, void *private)
Thierry FOURNIERad903512014-04-11 17:51:01 +020012150{
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012151 const char *al = smp->data.u.str.str;
12152 const char *end = al + smp->data.u.str.len;
Thierry FOURNIERad903512014-04-11 17:51:01 +020012153 const char *token;
12154 int toklen;
12155 int qvalue;
12156 const char *str;
12157 const char *w;
12158 int best_q = 0;
12159
12160 /* Set the constant to the sample, because the output of the
12161 * function will be peek in the constant configuration string.
12162 */
12163 smp->flags |= SMP_F_CONST;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012164 smp->data.u.str.size = 0;
12165 smp->data.u.str.str = "";
12166 smp->data.u.str.len = 0;
Thierry FOURNIERad903512014-04-11 17:51:01 +020012167
12168 /* Parse the accept language */
12169 while (1) {
12170
12171 /* Jump spaces, quit if the end is detected. */
Willy Tarreau506c69a2014-07-08 00:59:48 +020012172 while (al < end && isspace((unsigned char)*al))
Thierry FOURNIERad903512014-04-11 17:51:01 +020012173 al++;
12174 if (al >= end)
12175 break;
12176
12177 /* Start of the fisrt word. */
12178 token = al;
12179
12180 /* Look for separator: isspace(), ',' or ';'. Next value if 0 length word. */
Willy Tarreau506c69a2014-07-08 00:59:48 +020012181 while (al < end && *al != ';' && *al != ',' && !isspace((unsigned char)*al))
Thierry FOURNIERad903512014-04-11 17:51:01 +020012182 al++;
12183 if (al == token)
12184 goto expect_comma;
12185
12186 /* Length of the token. */
12187 toklen = al - token;
12188 qvalue = 1000;
12189
12190 /* Check if the token exists in the list. If the token not exists,
12191 * jump to the next token.
12192 */
12193 str = args[0].data.str.str;
12194 w = str;
12195 while (1) {
12196 if (*str == ';' || *str == '\0') {
12197 if (language_range_match(token, toklen, w, str-w))
12198 goto look_for_q;
12199 if (*str == '\0')
12200 goto expect_comma;
12201 w = str + 1;
12202 }
12203 str++;
12204 }
12205 goto expect_comma;
12206
12207look_for_q:
12208
12209 /* Jump spaces, quit if the end is detected. */
Willy Tarreau506c69a2014-07-08 00:59:48 +020012210 while (al < end && isspace((unsigned char)*al))
Thierry FOURNIERad903512014-04-11 17:51:01 +020012211 al++;
12212 if (al >= end)
12213 goto process_value;
12214
12215 /* If ',' is found, process the result */
12216 if (*al == ',')
12217 goto process_value;
12218
12219 /* If the character is different from ';', look
12220 * for the end of the header part in best effort.
12221 */
12222 if (*al != ';')
12223 goto expect_comma;
12224
12225 /* Assumes that the char is ';', now expect "q=". */
12226 al++;
12227
12228 /* Jump spaces, process value if the end is detected. */
Willy Tarreau506c69a2014-07-08 00:59:48 +020012229 while (al < end && isspace((unsigned char)*al))
Thierry FOURNIERad903512014-04-11 17:51:01 +020012230 al++;
12231 if (al >= end)
12232 goto process_value;
12233
12234 /* Expect 'q'. If no 'q', continue in best effort */
12235 if (*al != 'q')
12236 goto process_value;
12237 al++;
12238
12239 /* Jump spaces, process value if the end is detected. */
Willy Tarreau506c69a2014-07-08 00:59:48 +020012240 while (al < end && isspace((unsigned char)*al))
Thierry FOURNIERad903512014-04-11 17:51:01 +020012241 al++;
12242 if (al >= end)
12243 goto process_value;
12244
12245 /* Expect '='. If no '=', continue in best effort */
12246 if (*al != '=')
12247 goto process_value;
12248 al++;
12249
12250 /* Jump spaces, process value if the end is detected. */
Willy Tarreau506c69a2014-07-08 00:59:48 +020012251 while (al < end && isspace((unsigned char)*al))
Thierry FOURNIERad903512014-04-11 17:51:01 +020012252 al++;
12253 if (al >= end)
12254 goto process_value;
12255
12256 /* Parse the q value. */
12257 qvalue = parse_qvalue(al, &al);
12258
12259process_value:
12260
12261 /* If the new q value is the best q value, then store the associated
12262 * language in the response. If qvalue is the biggest value (1000),
12263 * break the process.
12264 */
12265 if (qvalue > best_q) {
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012266 smp->data.u.str.str = (char *)w;
12267 smp->data.u.str.len = str - w;
Thierry FOURNIERad903512014-04-11 17:51:01 +020012268 if (qvalue >= 1000)
12269 break;
12270 best_q = qvalue;
12271 }
12272
12273expect_comma:
12274
12275 /* Expect comma or end. If the end is detected, quit the loop. */
12276 while (al < end && *al != ',')
12277 al++;
12278 if (al >= end)
12279 break;
12280
12281 /* Comma is found, jump it and restart the analyzer. */
12282 al++;
12283 }
12284
12285 /* Set default value if required. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012286 if (smp->data.u.str.len == 0 && args[1].type == ARGT_STR) {
12287 smp->data.u.str.str = args[1].data.str.str;
12288 smp->data.u.str.len = args[1].data.str.len;
Thierry FOURNIERad903512014-04-11 17:51:01 +020012289 }
12290
12291 /* Return true only if a matching language was found. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012292 return smp->data.u.str.len != 0;
Thierry FOURNIERad903512014-04-11 17:51:01 +020012293}
12294
Thierry FOURNIER82ff3c92015-05-07 15:46:20 +020012295/* This fetch url-decode any input string. */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020012296static int sample_conv_url_dec(const struct arg *args, struct sample *smp, void *private)
Thierry FOURNIER82ff3c92015-05-07 15:46:20 +020012297{
12298 /* If the constant flag is set or if not size is avalaible at
12299 * the end of the buffer, copy the string in other buffer
12300 * before decoding.
12301 */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012302 if (smp->flags & SMP_F_CONST || smp->data.u.str.size <= smp->data.u.str.len) {
Thierry FOURNIER82ff3c92015-05-07 15:46:20 +020012303 struct chunk *str = get_trash_chunk();
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012304 memcpy(str->str, smp->data.u.str.str, smp->data.u.str.len);
12305 smp->data.u.str.str = str->str;
12306 smp->data.u.str.size = str->size;
Thierry FOURNIER82ff3c92015-05-07 15:46:20 +020012307 smp->flags &= ~SMP_F_CONST;
12308 }
12309
12310 /* Add final \0 required by url_decode(), and convert the input string. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012311 smp->data.u.str.str[smp->data.u.str.len] = '\0';
12312 smp->data.u.str.len = url_decode(smp->data.u.str.str);
Thierry FOURNIER82ff3c92015-05-07 15:46:20 +020012313 return 1;
12314}
12315
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020012316static int smp_conv_req_capture(const struct arg *args, struct sample *smp, void *private)
12317{
12318 struct proxy *fe = strm_fe(smp->strm);
12319 int idx, i;
12320 struct cap_hdr *hdr;
12321 int len;
12322
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020012323 if (!args || args->type != ARGT_SINT)
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020012324 return 0;
12325
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020012326 idx = args->data.sint;
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020012327
12328 /* Check the availibity of the capture id. */
12329 if (idx > fe->nb_req_cap - 1)
12330 return 0;
12331
12332 /* Look for the original configuration. */
12333 for (hdr = fe->req_cap, i = fe->nb_req_cap - 1;
12334 hdr != NULL && i != idx ;
12335 i--, hdr = hdr->next);
12336 if (!hdr)
12337 return 0;
12338
12339 /* check for the memory allocation */
12340 if (smp->strm->req_cap[hdr->index] == NULL)
12341 smp->strm->req_cap[hdr->index] = pool_alloc2(hdr->pool);
12342 if (smp->strm->req_cap[hdr->index] == NULL)
12343 return 0;
12344
12345 /* Check length. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012346 len = smp->data.u.str.len;
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020012347 if (len > hdr->len)
12348 len = hdr->len;
12349
12350 /* Capture input data. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012351 memcpy(smp->strm->req_cap[idx], smp->data.u.str.str, len);
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020012352 smp->strm->req_cap[idx][len] = '\0';
12353
12354 return 1;
12355}
12356
12357static int smp_conv_res_capture(const struct arg *args, struct sample *smp, void *private)
12358{
12359 struct proxy *fe = strm_fe(smp->strm);
12360 int idx, i;
12361 struct cap_hdr *hdr;
12362 int len;
12363
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020012364 if (!args || args->type != ARGT_SINT)
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020012365 return 0;
12366
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020012367 idx = args->data.sint;
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020012368
12369 /* Check the availibity of the capture id. */
12370 if (idx > fe->nb_rsp_cap - 1)
12371 return 0;
12372
12373 /* Look for the original configuration. */
12374 for (hdr = fe->rsp_cap, i = fe->nb_rsp_cap - 1;
12375 hdr != NULL && i != idx ;
12376 i--, hdr = hdr->next);
12377 if (!hdr)
12378 return 0;
12379
12380 /* check for the memory allocation */
12381 if (smp->strm->res_cap[hdr->index] == NULL)
12382 smp->strm->res_cap[hdr->index] = pool_alloc2(hdr->pool);
12383 if (smp->strm->res_cap[hdr->index] == NULL)
12384 return 0;
12385
12386 /* Check length. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012387 len = smp->data.u.str.len;
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020012388 if (len > hdr->len)
12389 len = hdr->len;
12390
12391 /* Capture input data. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012392 memcpy(smp->strm->res_cap[idx], smp->data.u.str.str, len);
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020012393 smp->strm->res_cap[idx][len] = '\0';
12394
12395 return 1;
12396}
12397
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012398/* This function executes one of the set-{method,path,query,uri} actions. It
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012399 * takes the string from the variable 'replace' with length 'len', then modifies
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012400 * the relevant part of the request line accordingly. Then it updates various
12401 * pointers to the next elements which were moved, and the total buffer length.
12402 * It finds the action to be performed in p[2], previously filled by function
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012403 * parse_set_req_line(). It returns 0 in case of success, -1 in case of internal
12404 * error, though this can be revisited when this code is finally exploited.
12405 *
12406 * 'action' can be '0' to replace method, '1' to replace path, '2' to replace
12407 * query string and 3 to replace uri.
12408 *
12409 * In query string case, the mark question '?' must be set at the start of the
12410 * string by the caller, event if the replacement query string is empty.
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012411 */
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012412int http_replace_req_line(int action, const char *replace, int len,
Willy Tarreau987e3fb2015-04-04 01:09:08 +020012413 struct proxy *px, struct stream *s)
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012414{
Willy Tarreau987e3fb2015-04-04 01:09:08 +020012415 struct http_txn *txn = s->txn;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012416 char *cur_ptr, *cur_end;
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012417 int offset = 0;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012418 int delta;
12419
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012420 switch (action) {
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012421 case 0: // method
Willy Tarreau22ec1ea2014-11-27 20:45:39 +010012422 cur_ptr = s->req.buf->p;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012423 cur_end = cur_ptr + txn->req.sl.rq.m_l;
12424
12425 /* adjust req line offsets and lengths */
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012426 delta = len - offset - (cur_end - cur_ptr);
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012427 txn->req.sl.rq.m_l += delta;
12428 txn->req.sl.rq.u += delta;
12429 txn->req.sl.rq.v += delta;
12430 break;
12431
12432 case 1: // path
12433 cur_ptr = http_get_path(txn);
12434 if (!cur_ptr)
Willy Tarreau22ec1ea2014-11-27 20:45:39 +010012435 cur_ptr = s->req.buf->p + txn->req.sl.rq.u;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012436
12437 cur_end = cur_ptr;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +010012438 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 +010012439 cur_end++;
12440
12441 /* adjust req line offsets and lengths */
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012442 delta = len - offset - (cur_end - cur_ptr);
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012443 txn->req.sl.rq.u_l += delta;
12444 txn->req.sl.rq.v += delta;
12445 break;
12446
12447 case 2: // query
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012448 offset = 1;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +010012449 cur_ptr = s->req.buf->p + txn->req.sl.rq.u;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012450 cur_end = cur_ptr + txn->req.sl.rq.u_l;
12451 while (cur_ptr < cur_end && *cur_ptr != '?')
12452 cur_ptr++;
12453
12454 /* skip the question mark or indicate that we must insert it
12455 * (but only if the format string is not empty then).
12456 */
12457 if (cur_ptr < cur_end)
12458 cur_ptr++;
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012459 else if (len > 1)
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012460 offset = 0;
12461
12462 /* adjust req line offsets and lengths */
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012463 delta = len - offset - (cur_end - cur_ptr);
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012464 txn->req.sl.rq.u_l += delta;
12465 txn->req.sl.rq.v += delta;
12466 break;
12467
12468 case 3: // uri
Willy Tarreau22ec1ea2014-11-27 20:45:39 +010012469 cur_ptr = s->req.buf->p + txn->req.sl.rq.u;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012470 cur_end = cur_ptr + txn->req.sl.rq.u_l;
12471
12472 /* adjust req line offsets and lengths */
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012473 delta = len - offset - (cur_end - cur_ptr);
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012474 txn->req.sl.rq.u_l += delta;
12475 txn->req.sl.rq.v += delta;
12476 break;
12477
12478 default:
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012479 return -1;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012480 }
12481
12482 /* commit changes and adjust end of message */
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012483 delta = buffer_replace2(s->req.buf, cur_ptr, cur_end, replace + offset, len - offset);
Thierry FOURNIER7f6192c2015-04-26 18:01:40 +020012484 txn->req.sl.rq.l += delta;
12485 txn->hdr_idx.v[0].len += delta;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012486 http_msg_move_end(&txn->req, delta);
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012487 return 0;
12488}
12489
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020012490/* This function replace the HTTP status code and the associated message. The
12491 * variable <status> contains the new status code. This function never fails.
12492 */
Robin H. Johnson52f5db22017-01-01 13:10:52 -080012493void http_set_status(unsigned int status, const char *reason, struct stream *s)
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020012494{
12495 struct http_txn *txn = s->txn;
12496 char *cur_ptr, *cur_end;
12497 int delta;
12498 char *res;
12499 int c_l;
Robin H. Johnson52f5db22017-01-01 13:10:52 -080012500 const char *msg = reason;
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020012501 int msg_len;
12502
12503 chunk_reset(&trash);
12504
12505 res = ultoa_o(status, trash.str, trash.size);
12506 c_l = res - trash.str;
12507
12508 trash.str[c_l] = ' ';
12509 trash.len = c_l + 1;
12510
Robin H. Johnson52f5db22017-01-01 13:10:52 -080012511 /* Do we have a custom reason format string? */
12512 if (msg == NULL)
12513 msg = get_reason(status);
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020012514 msg_len = strlen(msg);
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020012515 strncpy(&trash.str[trash.len], msg, trash.size - trash.len);
12516 trash.len += msg_len;
12517
12518 cur_ptr = s->res.buf->p + txn->rsp.sl.st.c;
12519 cur_end = s->res.buf->p + txn->rsp.sl.st.r + txn->rsp.sl.st.r_l;
12520
12521 /* commit changes and adjust message */
12522 delta = buffer_replace2(s->res.buf, cur_ptr, cur_end, trash.str, trash.len);
12523
12524 /* adjust res line offsets and lengths */
12525 txn->rsp.sl.st.r += c_l - txn->rsp.sl.st.c_l;
12526 txn->rsp.sl.st.c_l = c_l;
12527 txn->rsp.sl.st.r_l = msg_len;
12528
12529 delta = trash.len - (cur_end - cur_ptr);
12530 txn->rsp.sl.st.l += delta;
12531 txn->hdr_idx.v[0].len += delta;
12532 http_msg_move_end(&txn->rsp, delta);
12533}
12534
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012535/* This function executes one of the set-{method,path,query,uri} actions. It
12536 * builds a string in the trash from the specified format string. It finds
Thierry FOURNIER3f4bc652015-08-26 16:23:34 +020012537 * the action to be performed in <http.action>, previously filled by function
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012538 * parse_set_req_line(). The replacement action is excuted by the function
Thierry FOURNIER3f4bc652015-08-26 16:23:34 +020012539 * http_action_set_req_line(). It always returns ACT_RET_CONT. If an error
12540 * occurs the action is canceled, but the rule processing continue.
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012541 */
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012542enum act_return http_action_set_req_line(struct act_rule *rule, struct proxy *px,
Willy Tarreau658b85b2015-09-27 10:00:49 +020012543 struct session *sess, struct stream *s, int flags)
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012544{
12545 chunk_reset(&trash);
12546
12547 /* If we have to create a query string, prepare a '?'. */
Thierry FOURNIER8855a922015-07-31 08:54:25 +020012548 if (rule->arg.http.action == 2)
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012549 trash.str[trash.len++] = '?';
Thierry FOURNIER8855a922015-07-31 08:54:25 +020012550 trash.len += build_logline(s, trash.str + trash.len, trash.size - trash.len, &rule->arg.http.logfmt);
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012551
Thierry FOURNIER8855a922015-07-31 08:54:25 +020012552 http_replace_req_line(rule->arg.http.action, trash.str, trash.len, px, s);
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012553 return ACT_RET_CONT;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012554}
12555
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020012556/* This function is just a compliant action wrapper for "set-status". */
12557enum act_return action_http_set_status(struct act_rule *rule, struct proxy *px,
Willy Tarreau658b85b2015-09-27 10:00:49 +020012558 struct session *sess, struct stream *s, int flags)
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020012559{
Robin H. Johnson52f5db22017-01-01 13:10:52 -080012560 http_set_status(rule->arg.status.code, rule->arg.status.reason, s);
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020012561 return ACT_RET_CONT;
12562}
12563
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012564/* parse an http-request action among :
12565 * set-method
12566 * set-path
12567 * set-query
12568 * set-uri
12569 *
12570 * All of them accept a single argument of type string representing a log-format.
12571 * The resulting rule makes use of arg->act.p[0..1] to store the log-format list
12572 * 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 +020012573 * It returns ACT_RET_PRS_OK on success, ACT_RET_PRS_ERR on error.
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012574 */
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012575enum act_parse_ret parse_set_req_line(const char **args, int *orig_arg, struct proxy *px,
12576 struct act_rule *rule, char **err)
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012577{
12578 int cur_arg = *orig_arg;
12579
Thierry FOURNIER42148732015-09-02 17:17:33 +020012580 rule->action = ACT_CUSTOM;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012581
12582 switch (args[0][4]) {
12583 case 'm' :
Thierry FOURNIER8855a922015-07-31 08:54:25 +020012584 rule->arg.http.action = 0;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012585 rule->action_ptr = http_action_set_req_line;
12586 break;
12587 case 'p' :
Thierry FOURNIER8855a922015-07-31 08:54:25 +020012588 rule->arg.http.action = 1;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012589 rule->action_ptr = http_action_set_req_line;
12590 break;
12591 case 'q' :
Thierry FOURNIER8855a922015-07-31 08:54:25 +020012592 rule->arg.http.action = 2;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012593 rule->action_ptr = http_action_set_req_line;
12594 break;
12595 case 'u' :
Thierry FOURNIER8855a922015-07-31 08:54:25 +020012596 rule->arg.http.action = 3;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012597 rule->action_ptr = http_action_set_req_line;
12598 break;
12599 default:
12600 memprintf(err, "internal error: unhandled action '%s'", args[0]);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012601 return ACT_RET_PRS_ERR;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012602 }
12603
12604 if (!*args[cur_arg] ||
12605 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
12606 memprintf(err, "expects exactly 1 argument <format>");
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012607 return ACT_RET_PRS_ERR;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012608 }
12609
Thierry FOURNIER8855a922015-07-31 08:54:25 +020012610 LIST_INIT(&rule->arg.http.logfmt);
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012611 proxy->conf.args.ctx = ARGC_HRQ;
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +010012612 if (!parse_logformat_string(args[cur_arg], proxy, &rule->arg.http.logfmt, LOG_OPT_HTTP,
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +010012613 (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 +010012614 return ACT_RET_PRS_ERR;
12615 }
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012616
12617 (*orig_arg)++;
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012618 return ACT_RET_PRS_OK;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012619}
12620
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020012621/* parse set-status action:
12622 * This action accepts a single argument of type int representing
12623 * an http status code. It returns ACT_RET_PRS_OK on success,
12624 * ACT_RET_PRS_ERR on error.
12625 */
12626enum act_parse_ret parse_http_set_status(const char **args, int *orig_arg, struct proxy *px,
12627 struct act_rule *rule, char **err)
12628{
12629 char *error;
12630
Thierry FOURNIER42148732015-09-02 17:17:33 +020012631 rule->action = ACT_CUSTOM;
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020012632 rule->action_ptr = action_http_set_status;
12633
12634 /* Check if an argument is available */
12635 if (!*args[*orig_arg]) {
Robin H. Johnson52f5db22017-01-01 13:10:52 -080012636 memprintf(err, "expects 1 argument: <status>; or 3 arguments: <status> reason <fmt>");
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020012637 return ACT_RET_PRS_ERR;
12638 }
12639
12640 /* convert status code as integer */
12641 rule->arg.status.code = strtol(args[*orig_arg], &error, 10);
12642 if (*error != '\0' || rule->arg.status.code < 100 || rule->arg.status.code > 999) {
12643 memprintf(err, "expects an integer status code between 100 and 999");
12644 return ACT_RET_PRS_ERR;
12645 }
12646
12647 (*orig_arg)++;
Robin H. Johnson52f5db22017-01-01 13:10:52 -080012648
12649 /* set custom reason string */
12650 rule->arg.status.reason = NULL; // If null, we use the default reason for the status code.
12651 if (*args[*orig_arg] && strcmp(args[*orig_arg], "reason") == 0 &&
12652 (*args[*orig_arg + 1] && strcmp(args[*orig_arg + 1], "if") != 0 && strcmp(args[*orig_arg + 1], "unless") != 0)) {
12653 (*orig_arg)++;
12654 rule->arg.status.reason = strdup(args[*orig_arg]);
12655 (*orig_arg)++;
12656 }
12657
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020012658 return ACT_RET_PRS_OK;
12659}
12660
Willy Tarreaua9083d02015-05-08 15:27:59 +020012661/* This function executes the "capture" action. It executes a fetch expression,
12662 * turns the result into a string and puts it in a capture slot. It always
12663 * returns 1. If an error occurs the action is cancelled, but the rule
12664 * processing continues.
12665 */
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012666enum act_return http_action_req_capture(struct act_rule *rule, struct proxy *px,
Willy Tarreau658b85b2015-09-27 10:00:49 +020012667 struct session *sess, struct stream *s, int flags)
Willy Tarreaua9083d02015-05-08 15:27:59 +020012668{
Willy Tarreaua9083d02015-05-08 15:27:59 +020012669 struct sample *key;
Thierry FOURNIER32b15002015-07-31 08:56:16 +020012670 struct cap_hdr *h = rule->arg.cap.hdr;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012671 char **cap = s->req_cap;
12672 int len;
12673
Thierry FOURNIER32b15002015-07-31 08:56:16 +020012674 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 +020012675 if (!key)
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012676 return ACT_RET_CONT;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012677
12678 if (cap[h->index] == NULL)
12679 cap[h->index] = pool_alloc2(h->pool);
12680
12681 if (cap[h->index] == NULL) /* no more capture memory */
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012682 return ACT_RET_CONT;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012683
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012684 len = key->data.u.str.len;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012685 if (len > h->len)
12686 len = h->len;
12687
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012688 memcpy(cap[h->index], key->data.u.str.str, len);
Willy Tarreaua9083d02015-05-08 15:27:59 +020012689 cap[h->index][len] = 0;
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012690 return ACT_RET_CONT;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012691}
12692
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012693/* This function executes the "capture" action and store the result in a
12694 * capture slot if exists. It executes a fetch expression, turns the result
12695 * into a string and puts it in a capture slot. It always returns 1. If an
12696 * error occurs the action is cancelled, but the rule processing continues.
12697 */
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012698enum act_return http_action_req_capture_by_id(struct act_rule *rule, struct proxy *px,
Willy Tarreau658b85b2015-09-27 10:00:49 +020012699 struct session *sess, struct stream *s, int flags)
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012700{
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012701 struct sample *key;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012702 struct cap_hdr *h;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012703 char **cap = s->req_cap;
12704 struct proxy *fe = strm_fe(s);
12705 int len;
12706 int i;
12707
12708 /* Look for the original configuration. */
12709 for (h = fe->req_cap, i = fe->nb_req_cap - 1;
Thierry FOURNIERe2097972015-07-31 08:56:35 +020012710 h != NULL && i != rule->arg.capid.idx ;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012711 i--, h = h->next);
12712 if (!h)
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012713 return ACT_RET_CONT;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012714
Thierry FOURNIERe2097972015-07-31 08:56:35 +020012715 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 +020012716 if (!key)
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012717 return ACT_RET_CONT;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012718
12719 if (cap[h->index] == NULL)
12720 cap[h->index] = pool_alloc2(h->pool);
12721
12722 if (cap[h->index] == NULL) /* no more capture memory */
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012723 return ACT_RET_CONT;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012724
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012725 len = key->data.u.str.len;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012726 if (len > h->len)
12727 len = h->len;
12728
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012729 memcpy(cap[h->index], key->data.u.str.str, len);
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012730 cap[h->index][len] = 0;
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012731 return ACT_RET_CONT;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012732}
12733
Willy Tarreaua9083d02015-05-08 15:27:59 +020012734/* parse an "http-request capture" action. It takes a single argument which is
12735 * a sample fetch expression. It stores the expression into arg->act.p[0] and
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012736 * the allocated hdr_cap struct or the preallocated "id" into arg->act.p[1].
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012737 * It returns ACT_RET_PRS_OK on success, ACT_RET_PRS_ERR on error.
Willy Tarreaua9083d02015-05-08 15:27:59 +020012738 */
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012739enum act_parse_ret parse_http_req_capture(const char **args, int *orig_arg, struct proxy *px,
12740 struct act_rule *rule, char **err)
Willy Tarreaua9083d02015-05-08 15:27:59 +020012741{
12742 struct sample_expr *expr;
12743 struct cap_hdr *hdr;
12744 int cur_arg;
Willy Tarreau3986ac12015-05-08 16:13:42 +020012745 int len = 0;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012746
12747 for (cur_arg = *orig_arg; cur_arg < *orig_arg + 3 && *args[cur_arg]; cur_arg++)
12748 if (strcmp(args[cur_arg], "if") == 0 ||
12749 strcmp(args[cur_arg], "unless") == 0)
12750 break;
12751
12752 if (cur_arg < *orig_arg + 3) {
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012753 memprintf(err, "expects <expression> [ 'len' <length> | id <idx> ]");
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012754 return ACT_RET_PRS_ERR;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012755 }
12756
Willy Tarreaua9083d02015-05-08 15:27:59 +020012757 cur_arg = *orig_arg;
12758 expr = sample_parse_expr((char **)args, &cur_arg, px->conf.args.file, px->conf.args.line, err, &px->conf.args);
12759 if (!expr)
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012760 return ACT_RET_PRS_ERR;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012761
12762 if (!(expr->fetch->val & SMP_VAL_FE_HRQ_HDR)) {
12763 memprintf(err,
12764 "fetch method '%s' extracts information from '%s', none of which is available here",
12765 args[cur_arg-1], sample_src_names(expr->fetch->use));
12766 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012767 return ACT_RET_PRS_ERR;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012768 }
12769
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012770 if (!args[cur_arg] || !*args[cur_arg]) {
12771 memprintf(err, "expects 'len or 'id'");
12772 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012773 return ACT_RET_PRS_ERR;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012774 }
12775
Willy Tarreaua9083d02015-05-08 15:27:59 +020012776 if (strcmp(args[cur_arg], "len") == 0) {
12777 cur_arg++;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012778
12779 if (!(px->cap & PR_CAP_FE)) {
12780 memprintf(err, "proxy '%s' has no frontend capability", px->id);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012781 return ACT_RET_PRS_ERR;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012782 }
12783
12784 proxy->conf.args.ctx = ARGC_CAP;
12785
Willy Tarreaua9083d02015-05-08 15:27:59 +020012786 if (!args[cur_arg]) {
12787 memprintf(err, "missing length value");
12788 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012789 return ACT_RET_PRS_ERR;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012790 }
12791 /* we copy the table name for now, it will be resolved later */
12792 len = atoi(args[cur_arg]);
12793 if (len <= 0) {
12794 memprintf(err, "length must be > 0");
12795 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012796 return ACT_RET_PRS_ERR;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012797 }
12798 cur_arg++;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012799
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012800 if (!len) {
12801 memprintf(err, "a positive 'len' argument is mandatory");
12802 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012803 return ACT_RET_PRS_ERR;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012804 }
12805
Vincent Bernat02779b62016-04-03 13:48:43 +020012806 hdr = calloc(1, sizeof(*hdr));
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012807 hdr->next = px->req_cap;
12808 hdr->name = NULL; /* not a header capture */
12809 hdr->namelen = 0;
12810 hdr->len = len;
12811 hdr->pool = create_pool("caphdr", hdr->len + 1, MEM_F_SHARED);
12812 hdr->index = px->nb_req_cap++;
12813
12814 px->req_cap = hdr;
12815 px->to_log |= LW_REQHDR;
12816
Thierry FOURNIER42148732015-09-02 17:17:33 +020012817 rule->action = ACT_CUSTOM;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012818 rule->action_ptr = http_action_req_capture;
Thierry FOURNIER32b15002015-07-31 08:56:16 +020012819 rule->arg.cap.expr = expr;
12820 rule->arg.cap.hdr = hdr;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012821 }
12822
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012823 else if (strcmp(args[cur_arg], "id") == 0) {
12824 int id;
12825 char *error;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012826
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012827 cur_arg++;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012828
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012829 if (!args[cur_arg]) {
12830 memprintf(err, "missing id value");
12831 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012832 return ACT_RET_PRS_ERR;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012833 }
Willy Tarreaua9083d02015-05-08 15:27:59 +020012834
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012835 id = strtol(args[cur_arg], &error, 10);
12836 if (*error != '\0') {
12837 memprintf(err, "cannot parse id '%s'", args[cur_arg]);
12838 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012839 return ACT_RET_PRS_ERR;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012840 }
12841 cur_arg++;
12842
12843 proxy->conf.args.ctx = ARGC_CAP;
12844
Thierry FOURNIER42148732015-09-02 17:17:33 +020012845 rule->action = ACT_CUSTOM;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012846 rule->action_ptr = http_action_req_capture_by_id;
Thierry FOURNIERe2097972015-07-31 08:56:35 +020012847 rule->arg.capid.expr = expr;
12848 rule->arg.capid.idx = id;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012849 }
12850
12851 else {
12852 memprintf(err, "expects 'len' or 'id', found '%s'", args[cur_arg]);
12853 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012854 return ACT_RET_PRS_ERR;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012855 }
Willy Tarreaua9083d02015-05-08 15:27:59 +020012856
12857 *orig_arg = cur_arg;
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012858 return ACT_RET_PRS_OK;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012859}
12860
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012861/* This function executes the "capture" action and store the result in a
12862 * capture slot if exists. It executes a fetch expression, turns the result
12863 * into a string and puts it in a capture slot. It always returns 1. If an
12864 * error occurs the action is cancelled, but the rule processing continues.
12865 */
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012866enum act_return http_action_res_capture_by_id(struct act_rule *rule, struct proxy *px,
Willy Tarreau658b85b2015-09-27 10:00:49 +020012867 struct session *sess, struct stream *s, int flags)
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012868{
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012869 struct sample *key;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012870 struct cap_hdr *h;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012871 char **cap = s->res_cap;
12872 struct proxy *fe = strm_fe(s);
12873 int len;
12874 int i;
12875
12876 /* Look for the original configuration. */
12877 for (h = fe->rsp_cap, i = fe->nb_rsp_cap - 1;
Thierry FOURNIERe2097972015-07-31 08:56:35 +020012878 h != NULL && i != rule->arg.capid.idx ;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012879 i--, h = h->next);
12880 if (!h)
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012881 return ACT_RET_CONT;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012882
Thierry FOURNIERe2097972015-07-31 08:56:35 +020012883 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 +020012884 if (!key)
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012885 return ACT_RET_CONT;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012886
12887 if (cap[h->index] == NULL)
12888 cap[h->index] = pool_alloc2(h->pool);
12889
12890 if (cap[h->index] == NULL) /* no more capture memory */
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012891 return ACT_RET_CONT;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012892
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012893 len = key->data.u.str.len;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012894 if (len > h->len)
12895 len = h->len;
12896
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012897 memcpy(cap[h->index], key->data.u.str.str, len);
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012898 cap[h->index][len] = 0;
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012899 return ACT_RET_CONT;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012900}
12901
12902/* parse an "http-response capture" action. It takes a single argument which is
12903 * a sample fetch expression. It stores the expression into arg->act.p[0] and
12904 * the allocated hdr_cap struct od the preallocated id into arg->act.p[1].
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012905 * It returns ACT_RET_PRS_OK on success, ACT_RET_PRS_ERR on error.
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012906 */
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012907enum act_parse_ret parse_http_res_capture(const char **args, int *orig_arg, struct proxy *px,
12908 struct act_rule *rule, char **err)
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012909{
12910 struct sample_expr *expr;
12911 int cur_arg;
12912 int id;
12913 char *error;
12914
12915 for (cur_arg = *orig_arg; cur_arg < *orig_arg + 3 && *args[cur_arg]; cur_arg++)
12916 if (strcmp(args[cur_arg], "if") == 0 ||
12917 strcmp(args[cur_arg], "unless") == 0)
12918 break;
12919
12920 if (cur_arg < *orig_arg + 3) {
Willy Tarreau29bdb1c2016-06-24 15:36:34 +020012921 memprintf(err, "expects <expression> id <idx>");
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012922 return ACT_RET_PRS_ERR;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012923 }
12924
12925 cur_arg = *orig_arg;
12926 expr = sample_parse_expr((char **)args, &cur_arg, px->conf.args.file, px->conf.args.line, err, &px->conf.args);
12927 if (!expr)
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012928 return ACT_RET_PRS_ERR;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012929
12930 if (!(expr->fetch->val & SMP_VAL_FE_HRS_HDR)) {
12931 memprintf(err,
12932 "fetch method '%s' extracts information from '%s', none of which is available here",
12933 args[cur_arg-1], sample_src_names(expr->fetch->use));
12934 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012935 return ACT_RET_PRS_ERR;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012936 }
12937
12938 if (!args[cur_arg] || !*args[cur_arg]) {
Willy Tarreau29bdb1c2016-06-24 15:36:34 +020012939 memprintf(err, "expects 'id'");
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012940 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012941 return ACT_RET_PRS_ERR;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012942 }
12943
12944 if (strcmp(args[cur_arg], "id") != 0) {
12945 memprintf(err, "expects 'id', found '%s'", args[cur_arg]);
12946 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012947 return ACT_RET_PRS_ERR;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012948 }
12949
12950 cur_arg++;
12951
12952 if (!args[cur_arg]) {
12953 memprintf(err, "missing id value");
12954 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012955 return ACT_RET_PRS_ERR;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012956 }
12957
12958 id = strtol(args[cur_arg], &error, 10);
12959 if (*error != '\0') {
12960 memprintf(err, "cannot parse id '%s'", args[cur_arg]);
12961 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012962 return ACT_RET_PRS_ERR;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012963 }
12964 cur_arg++;
12965
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012966 proxy->conf.args.ctx = ARGC_CAP;
12967
Thierry FOURNIER42148732015-09-02 17:17:33 +020012968 rule->action = ACT_CUSTOM;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012969 rule->action_ptr = http_action_res_capture_by_id;
Thierry FOURNIERe2097972015-07-31 08:56:35 +020012970 rule->arg.capid.expr = expr;
12971 rule->arg.capid.idx = id;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012972
12973 *orig_arg = cur_arg;
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012974 return ACT_RET_PRS_OK;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012975}
12976
William Lallemand73025dd2014-04-24 14:38:37 +020012977/*
12978 * Return the struct http_req_action_kw associated to a keyword.
12979 */
Thierry FOURNIER36481b82015-08-19 09:01:53 +020012980struct action_kw *action_http_req_custom(const char *kw)
William Lallemand73025dd2014-04-24 14:38:37 +020012981{
Thierry FOURNIER322a1242015-08-19 09:07:47 +020012982 return action_lookup(&http_req_keywords.list, kw);
William Lallemand73025dd2014-04-24 14:38:37 +020012983}
12984
12985/*
12986 * Return the struct http_res_action_kw associated to a keyword.
12987 */
Thierry FOURNIER36481b82015-08-19 09:01:53 +020012988struct action_kw *action_http_res_custom(const char *kw)
William Lallemand73025dd2014-04-24 14:38:37 +020012989{
Thierry FOURNIER322a1242015-08-19 09:07:47 +020012990 return action_lookup(&http_res_keywords.list, kw);
William Lallemand73025dd2014-04-24 14:38:37 +020012991}
12992
Willy Tarreau12207b32016-11-22 19:48:51 +010012993
12994/* "show errors" handler for the CLI. Returns 0 if wants to continue, 1 to stop
12995 * now.
12996 */
12997static int cli_parse_show_errors(char **args, struct appctx *appctx, void *private)
12998{
12999 if (!cli_has_level(appctx, ACCESS_LVL_OPER))
13000 return 1;
13001
Willy Tarreau234ba2d2016-11-25 08:39:10 +010013002 if (*args[2]) {
13003 struct proxy *px;
13004
13005 px = proxy_find_by_name(args[2], 0, 0);
13006 if (px)
13007 appctx->ctx.errors.iid = px->uuid;
13008 else
13009 appctx->ctx.errors.iid = atoi(args[2]);
13010
13011 if (!appctx->ctx.errors.iid) {
13012 appctx->ctx.cli.msg = "No such proxy.\n";
13013 appctx->st0 = CLI_ST_PRINT;
13014 return 1;
13015 }
13016 }
Willy Tarreau12207b32016-11-22 19:48:51 +010013017 else
Willy Tarreau234ba2d2016-11-25 08:39:10 +010013018 appctx->ctx.errors.iid = -1; // dump all proxies
13019
Willy Tarreau35069f82016-11-25 09:16:37 +010013020 appctx->ctx.errors.flag = 0;
13021 if (strcmp(args[3], "request") == 0)
13022 appctx->ctx.errors.flag |= 4; // ignore response
13023 else if (strcmp(args[3], "response") == 0)
13024 appctx->ctx.errors.flag |= 2; // ignore request
Willy Tarreau12207b32016-11-22 19:48:51 +010013025 appctx->ctx.errors.px = NULL;
Willy Tarreau12207b32016-11-22 19:48:51 +010013026 return 0;
13027}
13028
13029/* This function dumps all captured errors onto the stream interface's
13030 * read buffer. It returns 0 if the output buffer is full and it needs
13031 * to be called again, otherwise non-zero.
13032 */
13033static int cli_io_handler_show_errors(struct appctx *appctx)
13034{
13035 struct stream_interface *si = appctx->owner;
13036 extern const char *monthname[12];
13037
13038 if (unlikely(si_ic(si)->flags & (CF_WRITE_ERROR|CF_SHUTW)))
13039 return 1;
13040
13041 chunk_reset(&trash);
13042
13043 if (!appctx->ctx.errors.px) {
13044 /* the function had not been called yet, let's prepare the
13045 * buffer for a response.
13046 */
13047 struct tm tm;
13048
13049 get_localtime(date.tv_sec, &tm);
13050 chunk_appendf(&trash, "Total events captured on [%02d/%s/%04d:%02d:%02d:%02d.%03d] : %u\n",
13051 tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900,
13052 tm.tm_hour, tm.tm_min, tm.tm_sec, (int)(date.tv_usec/1000),
13053 error_snapshot_id);
13054
13055 if (bi_putchk(si_ic(si), &trash) == -1) {
13056 /* Socket buffer full. Let's try again later from the same point */
13057 si_applet_cant_put(si);
13058 return 0;
13059 }
13060
13061 appctx->ctx.errors.px = proxy;
Willy Tarreau12207b32016-11-22 19:48:51 +010013062 appctx->ctx.errors.bol = 0;
13063 appctx->ctx.errors.ptr = -1;
13064 }
13065
13066 /* we have two inner loops here, one for the proxy, the other one for
13067 * the buffer.
13068 */
13069 while (appctx->ctx.errors.px) {
13070 struct error_snapshot *es;
13071
Willy Tarreau35069f82016-11-25 09:16:37 +010013072 if ((appctx->ctx.errors.flag & 1) == 0) {
Willy Tarreau12207b32016-11-22 19:48:51 +010013073 es = &appctx->ctx.errors.px->invalid_req;
Willy Tarreau35069f82016-11-25 09:16:37 +010013074 if (appctx->ctx.errors.flag & 2) // skip req
13075 goto next;
13076 }
13077 else {
Willy Tarreau12207b32016-11-22 19:48:51 +010013078 es = &appctx->ctx.errors.px->invalid_rep;
Willy Tarreau35069f82016-11-25 09:16:37 +010013079 if (appctx->ctx.errors.flag & 4) // skip resp
13080 goto next;
13081 }
Willy Tarreau12207b32016-11-22 19:48:51 +010013082
13083 if (!es->when.tv_sec)
13084 goto next;
13085
13086 if (appctx->ctx.errors.iid >= 0 &&
13087 appctx->ctx.errors.px->uuid != appctx->ctx.errors.iid &&
13088 es->oe->uuid != appctx->ctx.errors.iid)
13089 goto next;
13090
13091 if (appctx->ctx.errors.ptr < 0) {
13092 /* just print headers now */
13093
13094 char pn[INET6_ADDRSTRLEN];
13095 struct tm tm;
13096 int port;
13097
13098 get_localtime(es->when.tv_sec, &tm);
13099 chunk_appendf(&trash, " \n[%02d/%s/%04d:%02d:%02d:%02d.%03d]",
13100 tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900,
13101 tm.tm_hour, tm.tm_min, tm.tm_sec, (int)(es->when.tv_usec/1000));
13102
13103 switch (addr_to_str(&es->src, pn, sizeof(pn))) {
13104 case AF_INET:
13105 case AF_INET6:
13106 port = get_host_port(&es->src);
13107 break;
13108 default:
13109 port = 0;
13110 }
13111
Willy Tarreau35069f82016-11-25 09:16:37 +010013112 switch (appctx->ctx.errors.flag & 1) {
Willy Tarreau12207b32016-11-22 19:48:51 +010013113 case 0:
13114 chunk_appendf(&trash,
13115 " frontend %s (#%d): invalid request\n"
13116 " backend %s (#%d)",
13117 appctx->ctx.errors.px->id, appctx->ctx.errors.px->uuid,
13118 (es->oe->cap & PR_CAP_BE) ? es->oe->id : "<NONE>",
13119 (es->oe->cap & PR_CAP_BE) ? es->oe->uuid : -1);
13120 break;
13121 case 1:
13122 chunk_appendf(&trash,
13123 " backend %s (#%d): invalid response\n"
13124 " frontend %s (#%d)",
13125 appctx->ctx.errors.px->id, appctx->ctx.errors.px->uuid,
13126 es->oe->id, es->oe->uuid);
13127 break;
13128 }
13129
13130 chunk_appendf(&trash,
13131 ", server %s (#%d), event #%u\n"
13132 " src %s:%d, session #%d, session flags 0x%08x\n"
Willy Tarreau10e61cb2017-01-04 14:51:22 +010013133 " HTTP msg state %s(%d), msg flags 0x%08x, tx flags 0x%08x\n"
Willy Tarreau12207b32016-11-22 19:48:51 +010013134 " HTTP chunk len %lld bytes, HTTP body len %lld bytes\n"
13135 " buffer flags 0x%08x, out %d bytes, total %lld bytes\n"
13136 " pending %d bytes, wrapping at %d, error at position %d:\n \n",
13137 es->srv ? es->srv->id : "<NONE>", es->srv ? es->srv->puid : -1,
13138 es->ev_id,
13139 pn, port, es->sid, es->s_flags,
Willy Tarreau10e61cb2017-01-04 14:51:22 +010013140 http_msg_state_str(es->state), es->state, es->m_flags, es->t_flags,
Willy Tarreau12207b32016-11-22 19:48:51 +010013141 es->m_clen, es->m_blen,
13142 es->b_flags, es->b_out, es->b_tot,
13143 es->len, es->b_wrap, es->pos);
13144
13145 if (bi_putchk(si_ic(si), &trash) == -1) {
13146 /* Socket buffer full. Let's try again later from the same point */
13147 si_applet_cant_put(si);
13148 return 0;
13149 }
13150 appctx->ctx.errors.ptr = 0;
13151 appctx->ctx.errors.sid = es->sid;
13152 }
13153
13154 if (appctx->ctx.errors.sid != es->sid) {
13155 /* the snapshot changed while we were dumping it */
13156 chunk_appendf(&trash,
13157 " WARNING! update detected on this snapshot, dump interrupted. Please re-check!\n");
13158 if (bi_putchk(si_ic(si), &trash) == -1) {
13159 si_applet_cant_put(si);
13160 return 0;
13161 }
13162 goto next;
13163 }
13164
13165 /* OK, ptr >= 0, so we have to dump the current line */
13166 while (es->buf && appctx->ctx.errors.ptr < es->len && appctx->ctx.errors.ptr < global.tune.bufsize) {
13167 int newptr;
13168 int newline;
13169
13170 newline = appctx->ctx.errors.bol;
13171 newptr = dump_text_line(&trash, es->buf, global.tune.bufsize, es->len, &newline, appctx->ctx.errors.ptr);
13172 if (newptr == appctx->ctx.errors.ptr)
13173 return 0;
13174
13175 if (bi_putchk(si_ic(si), &trash) == -1) {
13176 /* Socket buffer full. Let's try again later from the same point */
13177 si_applet_cant_put(si);
13178 return 0;
13179 }
13180 appctx->ctx.errors.ptr = newptr;
13181 appctx->ctx.errors.bol = newline;
13182 };
13183 next:
13184 appctx->ctx.errors.bol = 0;
13185 appctx->ctx.errors.ptr = -1;
Willy Tarreau35069f82016-11-25 09:16:37 +010013186 appctx->ctx.errors.flag ^= 1;
13187 if (!(appctx->ctx.errors.flag & 1))
Willy Tarreau12207b32016-11-22 19:48:51 +010013188 appctx->ctx.errors.px = appctx->ctx.errors.px->next;
Willy Tarreau12207b32016-11-22 19:48:51 +010013189 }
13190
13191 /* dump complete */
13192 return 1;
13193}
13194
13195/* register cli keywords */
13196static struct cli_kw_list cli_kws = {{ },{
13197 { { "show", "errors", NULL },
13198 "show errors : report last request and response errors for each proxy",
13199 cli_parse_show_errors, cli_io_handler_show_errors, NULL,
13200 },
13201 {{},}
13202}};
13203
Willy Tarreau4a568972010-05-12 08:08:50 +020013204/************************************************************************/
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020013205/* All supported ACL keywords must be declared here. */
13206/************************************************************************/
13207
13208/* Note: must not be declared <const> as its list will be overwritten.
13209 * Please take care of keeping this list alphabetically sorted.
13210 */
Willy Tarreaudc13c112013-06-21 23:16:39 +020013211static struct acl_kw_list acl_kws = {ILH, {
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010013212 { "base", "base", PAT_MATCH_STR },
13213 { "base_beg", "base", PAT_MATCH_BEG },
13214 { "base_dir", "base", PAT_MATCH_DIR },
13215 { "base_dom", "base", PAT_MATCH_DOM },
13216 { "base_end", "base", PAT_MATCH_END },
13217 { "base_len", "base", PAT_MATCH_LEN },
13218 { "base_reg", "base", PAT_MATCH_REG },
13219 { "base_sub", "base", PAT_MATCH_SUB },
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020013220
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010013221 { "cook", "req.cook", PAT_MATCH_STR },
13222 { "cook_beg", "req.cook", PAT_MATCH_BEG },
13223 { "cook_dir", "req.cook", PAT_MATCH_DIR },
13224 { "cook_dom", "req.cook", PAT_MATCH_DOM },
13225 { "cook_end", "req.cook", PAT_MATCH_END },
13226 { "cook_len", "req.cook", PAT_MATCH_LEN },
13227 { "cook_reg", "req.cook", PAT_MATCH_REG },
13228 { "cook_sub", "req.cook", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020013229
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010013230 { "hdr", "req.hdr", PAT_MATCH_STR },
13231 { "hdr_beg", "req.hdr", PAT_MATCH_BEG },
13232 { "hdr_dir", "req.hdr", PAT_MATCH_DIR },
13233 { "hdr_dom", "req.hdr", PAT_MATCH_DOM },
13234 { "hdr_end", "req.hdr", PAT_MATCH_END },
13235 { "hdr_len", "req.hdr", PAT_MATCH_LEN },
13236 { "hdr_reg", "req.hdr", PAT_MATCH_REG },
13237 { "hdr_sub", "req.hdr", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020013238
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010013239 /* these two declarations uses strings with list storage (in place
13240 * of tree storage). The basic match is PAT_MATCH_STR, but the indexation
13241 * and delete functions are relative to the list management. The parse
13242 * and match method are related to the corresponding fetch methods. This
13243 * is very particular ACL declaration mode.
13244 */
13245 { "http_auth_group", NULL, PAT_MATCH_STR, NULL, pat_idx_list_str, pat_del_list_ptr, NULL, pat_match_auth },
13246 { "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 +020013247
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010013248 { "path", "path", PAT_MATCH_STR },
13249 { "path_beg", "path", PAT_MATCH_BEG },
13250 { "path_dir", "path", PAT_MATCH_DIR },
13251 { "path_dom", "path", PAT_MATCH_DOM },
13252 { "path_end", "path", PAT_MATCH_END },
13253 { "path_len", "path", PAT_MATCH_LEN },
13254 { "path_reg", "path", PAT_MATCH_REG },
13255 { "path_sub", "path", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020013256
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010013257 { "req_ver", "req.ver", PAT_MATCH_STR },
13258 { "resp_ver", "res.ver", PAT_MATCH_STR },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020013259
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010013260 { "scook", "res.cook", PAT_MATCH_STR },
13261 { "scook_beg", "res.cook", PAT_MATCH_BEG },
13262 { "scook_dir", "res.cook", PAT_MATCH_DIR },
13263 { "scook_dom", "res.cook", PAT_MATCH_DOM },
13264 { "scook_end", "res.cook", PAT_MATCH_END },
13265 { "scook_len", "res.cook", PAT_MATCH_LEN },
13266 { "scook_reg", "res.cook", PAT_MATCH_REG },
13267 { "scook_sub", "res.cook", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020013268
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010013269 { "shdr", "res.hdr", PAT_MATCH_STR },
13270 { "shdr_beg", "res.hdr", PAT_MATCH_BEG },
13271 { "shdr_dir", "res.hdr", PAT_MATCH_DIR },
13272 { "shdr_dom", "res.hdr", PAT_MATCH_DOM },
13273 { "shdr_end", "res.hdr", PAT_MATCH_END },
13274 { "shdr_len", "res.hdr", PAT_MATCH_LEN },
13275 { "shdr_reg", "res.hdr", PAT_MATCH_REG },
13276 { "shdr_sub", "res.hdr", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020013277
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010013278 { "url", "url", PAT_MATCH_STR },
13279 { "url_beg", "url", PAT_MATCH_BEG },
13280 { "url_dir", "url", PAT_MATCH_DIR },
13281 { "url_dom", "url", PAT_MATCH_DOM },
13282 { "url_end", "url", PAT_MATCH_END },
13283 { "url_len", "url", PAT_MATCH_LEN },
13284 { "url_reg", "url", PAT_MATCH_REG },
13285 { "url_sub", "url", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020013286
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010013287 { "urlp", "urlp", PAT_MATCH_STR },
13288 { "urlp_beg", "urlp", PAT_MATCH_BEG },
13289 { "urlp_dir", "urlp", PAT_MATCH_DIR },
13290 { "urlp_dom", "urlp", PAT_MATCH_DOM },
13291 { "urlp_end", "urlp", PAT_MATCH_END },
13292 { "urlp_len", "urlp", PAT_MATCH_LEN },
13293 { "urlp_reg", "urlp", PAT_MATCH_REG },
13294 { "urlp_sub", "urlp", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020013295
Willy Tarreau8ed669b2013-01-11 15:49:37 +010013296 { /* END */ },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020013297}};
13298
13299/************************************************************************/
13300/* All supported pattern keywords must be declared here. */
Willy Tarreau4a568972010-05-12 08:08:50 +020013301/************************************************************************/
13302/* Note: must not be declared <const> as its list will be overwritten */
Willy Tarreaudc13c112013-06-21 23:16:39 +020013303static struct sample_fetch_kw_list sample_fetch_keywords = {ILH, {
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010013304 { "base", smp_fetch_base, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020013305 { "base32", smp_fetch_base32, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010013306 { "base32+src", smp_fetch_base32_src, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV },
13307
Willy Tarreau87b09662015-04-03 00:22:06 +020013308 /* capture are allocated and are permanent in the stream */
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020013309 { "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 +020013310
13311 /* retrieve these captures from the HTTP logs */
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020013312 { "capture.req.method", smp_fetch_capture_req_method, 0, NULL, SMP_T_STR, SMP_USE_HRQHP },
13313 { "capture.req.uri", smp_fetch_capture_req_uri, 0, NULL, SMP_T_STR, SMP_USE_HRQHP },
13314 { "capture.req.ver", smp_fetch_capture_req_ver, 0, NULL, SMP_T_STR, SMP_USE_HRQHP },
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020013315
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020013316 { "capture.res.hdr", smp_fetch_capture_header_res, ARG1(1,SINT), NULL, SMP_T_STR, SMP_USE_HRSHP },
13317 { "capture.res.ver", smp_fetch_capture_res_ver, 0, NULL, SMP_T_STR, SMP_USE_HRQHP },
William Lallemanda43ba4e2014-01-28 18:14:25 +010013318
Willy Tarreau409bcde2013-01-08 00:31:00 +010013319 /* cookie is valid in both directions (eg: for "stick ...") but cook*
13320 * are only here to match the ACL's name, are request-only and are used
13321 * for ACL compatibility only.
13322 */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010013323 { "cook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
13324 { "cookie", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRQHV|SMP_USE_HRSHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020013325 { "cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
13326 { "cook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010013327
13328 /* hdr is valid in both directions (eg: for "stick ...") but hdr_* are
13329 * only here to match the ACL's name, are request-only and are used for
13330 * ACL compatibility only.
13331 */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010013332 { "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 +020013333 { "hdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010013334 { "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 +020013335 { "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 +010013336
Willy Tarreau0a0daec2013-04-02 22:44:58 +020013337 { "http_auth", smp_fetch_http_auth, ARG1(1,USR), NULL, SMP_T_BOOL, SMP_USE_HRQHV },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010013338 { "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 +010013339 { "http_first_req", smp_fetch_http_first_req, 0, NULL, SMP_T_BOOL, SMP_USE_HRQHP },
Thierry FOURNIERd4373142013-12-17 01:10:10 +010013340 { "method", smp_fetch_meth, 0, NULL, SMP_T_METH, SMP_USE_HRQHP },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010013341 { "path", smp_fetch_path, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
Willy Tarreau49ad95c2015-01-19 15:06:26 +010013342 { "query", smp_fetch_query, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010013343
13344 /* HTTP protocol on the request path */
13345 { "req.proto_http", smp_fetch_proto_http, 0, NULL, SMP_T_BOOL, SMP_USE_HRQHP },
Willy Tarreau409bcde2013-01-08 00:31:00 +010013346 { "req_proto_http", smp_fetch_proto_http, 0, NULL, SMP_T_BOOL, SMP_USE_HRQHP },
Willy Tarreau18ed2562013-01-14 15:56:36 +010013347
13348 /* HTTP version on the request path */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010013349 { "req.ver", smp_fetch_rqver, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
13350 { "req_ver", smp_fetch_rqver, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010013351
Willy Tarreaua5910cc2015-05-02 00:46:08 +020013352 { "req.body", smp_fetch_body, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020013353 { "req.body_len", smp_fetch_body_len, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV },
13354 { "req.body_size", smp_fetch_body_size, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV },
Thierry FOURNIERe28c4992015-05-19 14:45:09 +020013355 { "req.body_param", smp_fetch_body_param, ARG1(0,STR), NULL, SMP_T_BIN, SMP_USE_HRQHV },
Willy Tarreaua5910cc2015-05-02 00:46:08 +020013356
Willy Tarreau18ed2562013-01-14 15:56:36 +010013357 /* HTTP version on the response path */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010013358 { "res.ver", smp_fetch_stver, 0, NULL, SMP_T_STR, SMP_USE_HRSHV },
13359 { "resp_ver", smp_fetch_stver, 0, NULL, SMP_T_STR, SMP_USE_HRSHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010013360
Willy Tarreau18ed2562013-01-14 15:56:36 +010013361 /* explicit req.{cook,hdr} are used to force the fetch direction to be request-only */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010013362 { "req.cook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020013363 { "req.cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
13364 { "req.cook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010013365
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010013366 { "req.fhdr", smp_fetch_fhdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRQHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020013367 { "req.fhdr_cnt", smp_fetch_fhdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010013368 { "req.hdr", smp_fetch_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRQHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020013369 { "req.hdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010013370 { "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 +010013371 { "req.hdr_names", smp_fetch_hdr_names, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020013372 { "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 +010013373
13374 /* explicit req.{cook,hdr} are used to force the fetch direction to be response-only */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010013375 { "res.cook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRSHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020013376 { "res.cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
13377 { "res.cook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010013378
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010013379 { "res.fhdr", smp_fetch_fhdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRSHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020013380 { "res.fhdr_cnt", smp_fetch_fhdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010013381 { "res.hdr", smp_fetch_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRSHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020013382 { "res.hdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010013383 { "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 +010013384 { "res.hdr_names", smp_fetch_hdr_names, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRSHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020013385 { "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 +010013386
Willy Tarreau409bcde2013-01-08 00:31:00 +010013387 /* scook is valid only on the response and is used for ACL compatibility */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010013388 { "scook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRSHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020013389 { "scook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
13390 { "scook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010013391 { "set-cookie", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRSHV }, /* deprecated */
Willy Tarreau409bcde2013-01-08 00:31:00 +010013392
13393 /* shdr is valid only on the response and is used for ACL compatibility */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010013394 { "shdr", smp_fetch_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRSHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020013395 { "shdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010013396 { "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 +020013397 { "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 +010013398
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020013399 { "status", smp_fetch_stcode, 0, NULL, SMP_T_SINT, SMP_USE_HRSHP },
Thierry Fournier0e00dca2016-04-07 15:47:40 +020013400 { "unique-id", smp_fetch_uniqueid, 0, NULL, SMP_T_STR, SMP_SRC_L4SRV },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010013401 { "url", smp_fetch_url, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020013402 { "url32", smp_fetch_url32, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV },
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000013403 { "url32+src", smp_fetch_url32_src, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010013404 { "url_ip", smp_fetch_url_ip, 0, NULL, SMP_T_IPV4, SMP_USE_HRQHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020013405 { "url_port", smp_fetch_url_port, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV },
Willy Tarreau1ede1da2015-05-07 16:06:18 +020013406 { "url_param", smp_fetch_url_param, ARG2(0,STR,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
13407 { "urlp" , smp_fetch_url_param, ARG2(0,STR,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020013408 { "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 +010013409 { /* END */ },
Willy Tarreau4a568972010-05-12 08:08:50 +020013410}};
13411
Willy Tarreau8797c062007-05-07 00:55:35 +020013412
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010013413/************************************************************************/
13414/* All supported converter keywords must be declared here. */
13415/************************************************************************/
Willy Tarreau276fae92013-07-25 14:36:01 +020013416/* Note: must not be declared <const> as its list will be overwritten */
13417static struct sample_conv_kw_list sample_conv_kws = {ILH, {
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020013418 { "http_date", sample_conv_http_date, ARG1(0,SINT), NULL, SMP_T_SINT, SMP_T_STR},
Thierry FOURNIERad903512014-04-11 17:51:01 +020013419 { "language", sample_conv_q_prefered, ARG2(1,STR,STR), NULL, SMP_T_STR, SMP_T_STR},
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020013420 { "capture-req", smp_conv_req_capture, ARG1(1,SINT), NULL, SMP_T_STR, SMP_T_STR},
13421 { "capture-res", smp_conv_res_capture, ARG1(1,SINT), NULL, SMP_T_STR, SMP_T_STR},
Thierry FOURNIER82ff3c92015-05-07 15:46:20 +020013422 { "url_dec", sample_conv_url_dec, 0, NULL, SMP_T_STR, SMP_T_STR},
Willy Tarreau276fae92013-07-25 14:36:01 +020013423 { NULL, NULL, 0, 0, 0 },
13424}};
13425
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020013426
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010013427/************************************************************************/
13428/* All supported http-request action keywords must be declared here. */
13429/************************************************************************/
Thierry FOURNIER36481b82015-08-19 09:01:53 +020013430struct action_kw_list http_req_actions = {
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010013431 .kw = {
Willy Tarreaua9083d02015-05-08 15:27:59 +020013432 { "capture", parse_http_req_capture },
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010013433 { "set-method", parse_set_req_line },
13434 { "set-path", parse_set_req_line },
13435 { "set-query", parse_set_req_line },
13436 { "set-uri", parse_set_req_line },
Willy Tarreaucb703b02015-04-03 09:52:01 +020013437 { NULL, NULL }
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010013438 }
13439};
13440
Thierry FOURNIER36481b82015-08-19 09:01:53 +020013441struct action_kw_list http_res_actions = {
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020013442 .kw = {
13443 { "capture", parse_http_res_capture },
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020013444 { "set-status", parse_http_set_status },
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020013445 { NULL, NULL }
13446 }
13447};
13448
Willy Tarreau8797c062007-05-07 00:55:35 +020013449__attribute__((constructor))
13450static void __http_protocol_init(void)
13451{
13452 acl_register_keywords(&acl_kws);
Willy Tarreau12785782012-04-27 21:37:17 +020013453 sample_register_fetches(&sample_fetch_keywords);
Willy Tarreau276fae92013-07-25 14:36:01 +020013454 sample_register_convs(&sample_conv_kws);
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010013455 http_req_keywords_register(&http_req_actions);
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020013456 http_res_keywords_register(&http_res_actions);
Willy Tarreau12207b32016-11-22 19:48:51 +010013457 cli_register_kw(&cli_kws);
Willy Tarreau8797c062007-05-07 00:55:35 +020013458}
13459
13460
Willy Tarreau58f10d72006-12-04 02:26:12 +010013461/*
Willy Tarreaubaaee002006-06-26 02:48:02 +020013462 * Local variables:
13463 * c-indent-level: 8
13464 * c-basic-offset: 8
13465 * End:
13466 */