Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1 | /* |
| 2 | * HTTP protocol analyzer |
| 3 | * |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 4 | * Copyright 2000-2011 Willy Tarreau <w@1wt.eu> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 5 | * |
| 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 Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 20 | #include <time.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 21 | |
| 22 | #include <sys/socket.h> |
| 23 | #include <sys/stat.h> |
| 24 | #include <sys/types.h> |
| 25 | |
Willy Tarreau | b05405a | 2012-01-23 15:35:52 +0100 | [diff] [blame] | 26 | #include <netinet/tcp.h> |
| 27 | |
Willy Tarreau | 2dd0d47 | 2006-06-29 17:53:05 +0200 | [diff] [blame] | 28 | #include <common/appsession.h> |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 29 | #include <common/base64.h> |
Willy Tarreau | c7e4238 | 2012-08-24 19:22:53 +0200 | [diff] [blame] | 30 | #include <common/chunk.h> |
Willy Tarreau | 2dd0d47 | 2006-06-29 17:53:05 +0200 | [diff] [blame] | 31 | #include <common/compat.h> |
| 32 | #include <common/config.h> |
Willy Tarreau | a4cd1f5 | 2006-12-16 19:57:26 +0100 | [diff] [blame] | 33 | #include <common/debug.h> |
Willy Tarreau | 2dd0d47 | 2006-06-29 17:53:05 +0200 | [diff] [blame] | 34 | #include <common/memory.h> |
| 35 | #include <common/mini-clist.h> |
| 36 | #include <common/standard.h> |
Willy Tarreau | 0c303ee | 2008-07-07 00:09:58 +0200 | [diff] [blame] | 37 | #include <common/ticks.h> |
Willy Tarreau | 2dd0d47 | 2006-06-29 17:53:05 +0200 | [diff] [blame] | 38 | #include <common/time.h> |
| 39 | #include <common/uri_auth.h> |
| 40 | #include <common/version.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 41 | |
| 42 | #include <types/capture.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 43 | #include <types/global.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 44 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 45 | #include <proto/acl.h> |
Willy Tarreau | 61612d4 | 2012-04-19 18:42:05 +0200 | [diff] [blame] | 46 | #include <proto/arg.h> |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 47 | #include <proto/auth.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 48 | #include <proto/backend.h> |
Willy Tarreau | c7e4238 | 2012-08-24 19:22:53 +0200 | [diff] [blame] | 49 | #include <proto/channel.h> |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 50 | #include <proto/checks.h> |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 51 | #include <proto/compression.h> |
Willy Tarreau | 9186126 | 2007-10-17 17:06:05 +0200 | [diff] [blame] | 52 | #include <proto/dumpstats.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 53 | #include <proto/fd.h> |
Willy Tarreau | 03fa5df | 2010-05-24 21:02:37 +0200 | [diff] [blame] | 54 | #include <proto/frontend.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 55 | #include <proto/log.h> |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 56 | #include <proto/hdr_idx.h> |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 57 | #include <proto/pattern.h> |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 58 | #include <proto/proto_tcp.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 59 | #include <proto/proto_http.h> |
Willy Tarreau | 7f062c4 | 2009-03-05 18:43:00 +0100 | [diff] [blame] | 60 | #include <proto/proxy.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 61 | #include <proto/queue.h> |
Willy Tarreau | cd3b094 | 2012-04-27 21:52:18 +0200 | [diff] [blame] | 62 | #include <proto/sample.h> |
Willy Tarreau | 7f062c4 | 2009-03-05 18:43:00 +0100 | [diff] [blame] | 63 | #include <proto/server.h> |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 64 | #include <proto/stream.h> |
Willy Tarreau | cff6411 | 2008-11-03 06:26:53 +0100 | [diff] [blame] | 65 | #include <proto/stream_interface.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 66 | #include <proto/task.h> |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 67 | #include <proto/pattern.h> |
Thierry FOURNIER | 4834bc7 | 2015-06-06 19:29:07 +0200 | [diff] [blame] | 68 | #include <proto/vars.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 69 | |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 70 | const char HTTP_100[] = |
| 71 | "HTTP/1.1 100 Continue\r\n\r\n"; |
| 72 | |
| 73 | const struct chunk http_100_chunk = { |
| 74 | .str = (char *)&HTTP_100, |
| 75 | .len = sizeof(HTTP_100)-1 |
| 76 | }; |
| 77 | |
Willy Tarreau | a9679ac | 2010-01-03 17:32:57 +0100 | [diff] [blame] | 78 | /* Warning: no "connection" header is provided with the 3xx messages below */ |
Willy Tarreau | b463dfb | 2008-06-07 23:08:56 +0200 | [diff] [blame] | 79 | const char *HTTP_301 = |
Willy Tarreau | bc5aa19 | 2010-01-03 15:09:36 +0100 | [diff] [blame] | 80 | "HTTP/1.1 301 Moved Permanently\r\n" |
Willy Tarreau | bc5aa19 | 2010-01-03 15:09:36 +0100 | [diff] [blame] | 81 | "Content-length: 0\r\n" |
Willy Tarreau | b463dfb | 2008-06-07 23:08:56 +0200 | [diff] [blame] | 82 | "Location: "; /* not terminated since it will be concatenated with the URL */ |
| 83 | |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 84 | const char *HTTP_302 = |
Willy Tarreau | bc5aa19 | 2010-01-03 15:09:36 +0100 | [diff] [blame] | 85 | "HTTP/1.1 302 Found\r\n" |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 86 | "Cache-Control: no-cache\r\n" |
Willy Tarreau | bc5aa19 | 2010-01-03 15:09:36 +0100 | [diff] [blame] | 87 | "Content-length: 0\r\n" |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 88 | "Location: "; /* not terminated since it will be concatenated with the URL */ |
| 89 | |
| 90 | /* same as 302 except that the browser MUST retry with the GET method */ |
| 91 | const char *HTTP_303 = |
Willy Tarreau | bc5aa19 | 2010-01-03 15:09:36 +0100 | [diff] [blame] | 92 | "HTTP/1.1 303 See Other\r\n" |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 93 | "Cache-Control: no-cache\r\n" |
Willy Tarreau | bc5aa19 | 2010-01-03 15:09:36 +0100 | [diff] [blame] | 94 | "Content-length: 0\r\n" |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 95 | "Location: "; /* not terminated since it will be concatenated with the URL */ |
| 96 | |
Yves Lafon | 3e8d1ae | 2013-03-11 11:06:05 -0400 | [diff] [blame] | 97 | |
| 98 | /* same as 302 except that the browser MUST retry with the same method */ |
| 99 | const char *HTTP_307 = |
| 100 | "HTTP/1.1 307 Temporary Redirect\r\n" |
| 101 | "Cache-Control: no-cache\r\n" |
| 102 | "Content-length: 0\r\n" |
| 103 | "Location: "; /* not terminated since it will be concatenated with the URL */ |
| 104 | |
| 105 | /* same as 301 except that the browser MUST retry with the same method */ |
| 106 | const char *HTTP_308 = |
| 107 | "HTTP/1.1 308 Permanent Redirect\r\n" |
| 108 | "Content-length: 0\r\n" |
| 109 | "Location: "; /* not terminated since it will be concatenated with the URL */ |
| 110 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 111 | /* Warning: this one is an sprintf() fmt string, with <realm> as its only argument */ |
| 112 | const char *HTTP_401_fmt = |
| 113 | "HTTP/1.0 401 Unauthorized\r\n" |
| 114 | "Cache-Control: no-cache\r\n" |
| 115 | "Connection: close\r\n" |
Willy Tarreau | 791d66d | 2006-07-08 16:53:38 +0200 | [diff] [blame] | 116 | "Content-Type: text/html\r\n" |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 117 | "WWW-Authenticate: Basic realm=\"%s\"\r\n" |
| 118 | "\r\n" |
| 119 | "<html><body><h1>401 Unauthorized</h1>\nYou need a valid user and password to access this content.\n</body></html>\n"; |
| 120 | |
Willy Tarreau | 844a7e7 | 2010-01-31 21:46:18 +0100 | [diff] [blame] | 121 | const char *HTTP_407_fmt = |
| 122 | "HTTP/1.0 407 Unauthorized\r\n" |
| 123 | "Cache-Control: no-cache\r\n" |
| 124 | "Connection: close\r\n" |
| 125 | "Content-Type: text/html\r\n" |
| 126 | "Proxy-Authenticate: Basic realm=\"%s\"\r\n" |
| 127 | "\r\n" |
Godbach | 1f1fae6 | 2014-12-17 16:32:05 +0800 | [diff] [blame] | 128 | "<html><body><h1>407 Unauthorized</h1>\nYou need a valid user and password to access this content.\n</body></html>\n"; |
Willy Tarreau | 844a7e7 | 2010-01-31 21:46:18 +0100 | [diff] [blame] | 129 | |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 130 | |
| 131 | const int http_err_codes[HTTP_ERR_SIZE] = { |
Willy Tarreau | ae94d4d | 2011-05-11 16:28:49 +0200 | [diff] [blame] | 132 | [HTTP_ERR_200] = 200, /* used by "monitor-uri" */ |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 133 | [HTTP_ERR_400] = 400, |
| 134 | [HTTP_ERR_403] = 403, |
CJ Ess | 108b1dd | 2015-04-07 12:03:37 -0400 | [diff] [blame] | 135 | [HTTP_ERR_405] = 405, |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 136 | [HTTP_ERR_408] = 408, |
CJ Ess | 108b1dd | 2015-04-07 12:03:37 -0400 | [diff] [blame] | 137 | [HTTP_ERR_429] = 429, |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 138 | [HTTP_ERR_500] = 500, |
| 139 | [HTTP_ERR_502] = 502, |
| 140 | [HTTP_ERR_503] = 503, |
| 141 | [HTTP_ERR_504] = 504, |
| 142 | }; |
| 143 | |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 144 | static const char *http_err_msgs[HTTP_ERR_SIZE] = { |
Willy Tarreau | ae94d4d | 2011-05-11 16:28:49 +0200 | [diff] [blame] | 145 | [HTTP_ERR_200] = |
| 146 | "HTTP/1.0 200 OK\r\n" |
| 147 | "Cache-Control: no-cache\r\n" |
| 148 | "Connection: close\r\n" |
| 149 | "Content-Type: text/html\r\n" |
| 150 | "\r\n" |
| 151 | "<html><body><h1>200 OK</h1>\nService ready.\n</body></html>\n", |
| 152 | |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 153 | [HTTP_ERR_400] = |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 154 | "HTTP/1.0 400 Bad request\r\n" |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 155 | "Cache-Control: no-cache\r\n" |
| 156 | "Connection: close\r\n" |
| 157 | "Content-Type: text/html\r\n" |
| 158 | "\r\n" |
| 159 | "<html><body><h1>400 Bad request</h1>\nYour browser sent an invalid request.\n</body></html>\n", |
| 160 | |
| 161 | [HTTP_ERR_403] = |
| 162 | "HTTP/1.0 403 Forbidden\r\n" |
| 163 | "Cache-Control: no-cache\r\n" |
| 164 | "Connection: close\r\n" |
| 165 | "Content-Type: text/html\r\n" |
| 166 | "\r\n" |
| 167 | "<html><body><h1>403 Forbidden</h1>\nRequest forbidden by administrative rules.\n</body></html>\n", |
| 168 | |
CJ Ess | 108b1dd | 2015-04-07 12:03:37 -0400 | [diff] [blame] | 169 | [HTTP_ERR_405] = |
| 170 | "HTTP/1.0 405 Method Not Allowed\r\n" |
| 171 | "Cache-Control: no-cache\r\n" |
| 172 | "Connection: close\r\n" |
| 173 | "Content-Type: text/html\r\n" |
| 174 | "\r\n" |
| 175 | "<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", |
| 176 | |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 177 | [HTTP_ERR_408] = |
| 178 | "HTTP/1.0 408 Request Time-out\r\n" |
| 179 | "Cache-Control: no-cache\r\n" |
| 180 | "Connection: close\r\n" |
| 181 | "Content-Type: text/html\r\n" |
| 182 | "\r\n" |
| 183 | "<html><body><h1>408 Request Time-out</h1>\nYour browser didn't send a complete request in time.\n</body></html>\n", |
| 184 | |
CJ Ess | 108b1dd | 2015-04-07 12:03:37 -0400 | [diff] [blame] | 185 | [HTTP_ERR_429] = |
| 186 | "HTTP/1.0 429 Too Many Requests\r\n" |
| 187 | "Cache-Control: no-cache\r\n" |
| 188 | "Connection: close\r\n" |
| 189 | "Content-Type: text/html\r\n" |
| 190 | "\r\n" |
| 191 | "<html><body><h1>429 Too Many Requests</h1>\nYou have sent too many requests in a given amount of time.\n</body></html>\n", |
| 192 | |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 193 | [HTTP_ERR_500] = |
| 194 | "HTTP/1.0 500 Server Error\r\n" |
| 195 | "Cache-Control: no-cache\r\n" |
| 196 | "Connection: close\r\n" |
| 197 | "Content-Type: text/html\r\n" |
| 198 | "\r\n" |
| 199 | "<html><body><h1>500 Server Error</h1>\nAn internal server error occured.\n</body></html>\n", |
| 200 | |
| 201 | [HTTP_ERR_502] = |
| 202 | "HTTP/1.0 502 Bad Gateway\r\n" |
| 203 | "Cache-Control: no-cache\r\n" |
| 204 | "Connection: close\r\n" |
| 205 | "Content-Type: text/html\r\n" |
| 206 | "\r\n" |
| 207 | "<html><body><h1>502 Bad Gateway</h1>\nThe server returned an invalid or incomplete response.\n</body></html>\n", |
| 208 | |
| 209 | [HTTP_ERR_503] = |
| 210 | "HTTP/1.0 503 Service Unavailable\r\n" |
| 211 | "Cache-Control: no-cache\r\n" |
| 212 | "Connection: close\r\n" |
| 213 | "Content-Type: text/html\r\n" |
| 214 | "\r\n" |
| 215 | "<html><body><h1>503 Service Unavailable</h1>\nNo server is available to handle this request.\n</body></html>\n", |
| 216 | |
| 217 | [HTTP_ERR_504] = |
| 218 | "HTTP/1.0 504 Gateway Time-out\r\n" |
| 219 | "Cache-Control: no-cache\r\n" |
| 220 | "Connection: close\r\n" |
| 221 | "Content-Type: text/html\r\n" |
| 222 | "\r\n" |
| 223 | "<html><body><h1>504 Gateway Time-out</h1>\nThe server didn't respond in time.\n</body></html>\n", |
| 224 | |
| 225 | }; |
| 226 | |
Cyril Bonté | 19979e1 | 2012-04-04 12:57:21 +0200 | [diff] [blame] | 227 | /* status codes available for the stats admin page (strictly 4 chars length) */ |
| 228 | const char *stat_status_codes[STAT_STATUS_SIZE] = { |
| 229 | [STAT_STATUS_DENY] = "DENY", |
| 230 | [STAT_STATUS_DONE] = "DONE", |
| 231 | [STAT_STATUS_ERRP] = "ERRP", |
| 232 | [STAT_STATUS_EXCD] = "EXCD", |
| 233 | [STAT_STATUS_NONE] = "NONE", |
| 234 | [STAT_STATUS_PART] = "PART", |
| 235 | [STAT_STATUS_UNKN] = "UNKN", |
| 236 | }; |
| 237 | |
| 238 | |
William Lallemand | 73025dd | 2014-04-24 14:38:37 +0200 | [diff] [blame] | 239 | /* List head of all known action keywords for "http-request" */ |
| 240 | struct http_req_action_kw_list http_req_keywords = { |
| 241 | .list = LIST_HEAD_INIT(http_req_keywords.list) |
| 242 | }; |
| 243 | |
| 244 | /* List head of all known action keywords for "http-response" */ |
| 245 | struct http_res_action_kw_list http_res_keywords = { |
| 246 | .list = LIST_HEAD_INIT(http_res_keywords.list) |
| 247 | }; |
| 248 | |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 249 | /* We must put the messages here since GCC cannot initialize consts depending |
| 250 | * on strlen(). |
| 251 | */ |
| 252 | struct chunk http_err_chunks[HTTP_ERR_SIZE]; |
| 253 | |
Willy Tarreau | a890d07 | 2013-04-02 12:01:06 +0200 | [diff] [blame] | 254 | /* this struct is used between calls to smp_fetch_hdr() or smp_fetch_cookie() */ |
| 255 | static struct hdr_ctx static_hdr_ctx; |
| 256 | |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 257 | #define FD_SETS_ARE_BITFIELDS |
| 258 | #ifdef FD_SETS_ARE_BITFIELDS |
| 259 | /* |
| 260 | * This map is used with all the FD_* macros to check whether a particular bit |
| 261 | * is set or not. Each bit represents an ACSII code. FD_SET() sets those bytes |
| 262 | * which should be encoded. When FD_ISSET() returns non-zero, it means that the |
| 263 | * byte should be encoded. Be careful to always pass bytes from 0 to 255 |
| 264 | * exclusively to the macros. |
| 265 | */ |
| 266 | fd_set hdr_encode_map[(sizeof(fd_set) > (256/8)) ? 1 : ((256/8) / sizeof(fd_set))]; |
| 267 | fd_set url_encode_map[(sizeof(fd_set) > (256/8)) ? 1 : ((256/8) / sizeof(fd_set))]; |
Thierry FOURNIER | d048d8b | 2014-03-13 16:46:18 +0100 | [diff] [blame] | 268 | fd_set http_encode_map[(sizeof(fd_set) > (256/8)) ? 1 : ((256/8) / sizeof(fd_set))]; |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 269 | |
| 270 | #else |
| 271 | #error "Check if your OS uses bitfields for fd_sets" |
| 272 | #endif |
| 273 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 274 | static int http_apply_redirect_rule(struct redirect_rule *rule, struct stream *s, struct http_txn *txn); |
Willy Tarreau | 0b74833 | 2014-04-29 00:13:29 +0200 | [diff] [blame] | 275 | |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 276 | void init_proto_http() |
| 277 | { |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 278 | int i; |
| 279 | char *tmp; |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 280 | int msg; |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 281 | |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 282 | for (msg = 0; msg < HTTP_ERR_SIZE; msg++) { |
| 283 | if (!http_err_msgs[msg]) { |
| 284 | Alert("Internal error: no message defined for HTTP return code %d. Aborting.\n", msg); |
| 285 | abort(); |
| 286 | } |
| 287 | |
| 288 | http_err_chunks[msg].str = (char *)http_err_msgs[msg]; |
| 289 | http_err_chunks[msg].len = strlen(http_err_msgs[msg]); |
| 290 | } |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 291 | |
| 292 | /* initialize the log header encoding map : '{|}"#' should be encoded with |
| 293 | * '#' as prefix, as well as non-printable characters ( <32 or >= 127 ). |
| 294 | * URL encoding only requires '"', '#' to be encoded as well as non- |
| 295 | * printable characters above. |
| 296 | */ |
| 297 | memset(hdr_encode_map, 0, sizeof(hdr_encode_map)); |
| 298 | memset(url_encode_map, 0, sizeof(url_encode_map)); |
Thierry FOURNIER | d048d8b | 2014-03-13 16:46:18 +0100 | [diff] [blame] | 299 | memset(http_encode_map, 0, sizeof(url_encode_map)); |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 300 | for (i = 0; i < 32; i++) { |
| 301 | FD_SET(i, hdr_encode_map); |
| 302 | FD_SET(i, url_encode_map); |
| 303 | } |
| 304 | for (i = 127; i < 256; i++) { |
| 305 | FD_SET(i, hdr_encode_map); |
| 306 | FD_SET(i, url_encode_map); |
| 307 | } |
| 308 | |
| 309 | tmp = "\"#{|}"; |
| 310 | while (*tmp) { |
| 311 | FD_SET(*tmp, hdr_encode_map); |
| 312 | tmp++; |
| 313 | } |
| 314 | |
| 315 | tmp = "\"#"; |
| 316 | while (*tmp) { |
| 317 | FD_SET(*tmp, url_encode_map); |
| 318 | tmp++; |
| 319 | } |
Willy Tarreau | 332f8bf | 2007-05-13 21:36:56 +0200 | [diff] [blame] | 320 | |
Thierry FOURNIER | d048d8b | 2014-03-13 16:46:18 +0100 | [diff] [blame] | 321 | /* initialize the http header encoding map. The draft httpbis define the |
| 322 | * header content as: |
| 323 | * |
| 324 | * HTTP-message = start-line |
| 325 | * *( header-field CRLF ) |
| 326 | * CRLF |
| 327 | * [ message-body ] |
| 328 | * header-field = field-name ":" OWS field-value OWS |
| 329 | * field-value = *( field-content / obs-fold ) |
| 330 | * field-content = field-vchar [ 1*( SP / HTAB ) field-vchar ] |
| 331 | * obs-fold = CRLF 1*( SP / HTAB ) |
| 332 | * field-vchar = VCHAR / obs-text |
| 333 | * VCHAR = %x21-7E |
| 334 | * obs-text = %x80-FF |
| 335 | * |
| 336 | * All the chars are encoded except "VCHAR", "obs-text", SP and HTAB. |
| 337 | * The encoded chars are form 0x00 to 0x08, 0x0a to 0x1f and 0x7f. The |
| 338 | * "obs-fold" is volontary forgotten because haproxy remove this. |
| 339 | */ |
| 340 | memset(http_encode_map, 0, sizeof(http_encode_map)); |
| 341 | for (i = 0x00; i <= 0x08; i++) |
| 342 | FD_SET(i, http_encode_map); |
| 343 | for (i = 0x0a; i <= 0x1f; i++) |
| 344 | FD_SET(i, http_encode_map); |
| 345 | FD_SET(0x7f, http_encode_map); |
| 346 | |
Willy Tarreau | 332f8bf | 2007-05-13 21:36:56 +0200 | [diff] [blame] | 347 | /* memory allocations */ |
Willy Tarreau | 63986c7 | 2015-04-03 22:55:33 +0200 | [diff] [blame] | 348 | pool2_http_txn = create_pool("http_txn", sizeof(struct http_txn), MEM_F_SHARED); |
Willy Tarreau | 332f8bf | 2007-05-13 21:36:56 +0200 | [diff] [blame] | 349 | pool2_requri = create_pool("requri", REQURI_LEN, MEM_F_SHARED); |
William Lallemand | a73203e | 2012-03-12 12:48:57 +0100 | [diff] [blame] | 350 | pool2_uniqueid = create_pool("uniqueid", UNIQUEID_LEN, MEM_F_SHARED); |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 351 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 352 | |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 353 | /* |
| 354 | * We have 26 list of methods (1 per first letter), each of which can have |
| 355 | * up to 3 entries (2 valid, 1 null). |
| 356 | */ |
| 357 | struct http_method_desc { |
Willy Tarreau | c8987b3 | 2013-12-06 23:43:17 +0100 | [diff] [blame] | 358 | enum http_meth_t meth; |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 359 | int len; |
| 360 | const char text[8]; |
| 361 | }; |
| 362 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 363 | const struct http_method_desc http_methods[26][3] = { |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 364 | ['C' - 'A'] = { |
| 365 | [0] = { .meth = HTTP_METH_CONNECT , .len=7, .text="CONNECT" }, |
| 366 | }, |
| 367 | ['D' - 'A'] = { |
| 368 | [0] = { .meth = HTTP_METH_DELETE , .len=6, .text="DELETE" }, |
| 369 | }, |
| 370 | ['G' - 'A'] = { |
| 371 | [0] = { .meth = HTTP_METH_GET , .len=3, .text="GET" }, |
| 372 | }, |
| 373 | ['H' - 'A'] = { |
| 374 | [0] = { .meth = HTTP_METH_HEAD , .len=4, .text="HEAD" }, |
| 375 | }, |
| 376 | ['P' - 'A'] = { |
| 377 | [0] = { .meth = HTTP_METH_POST , .len=4, .text="POST" }, |
| 378 | [1] = { .meth = HTTP_METH_PUT , .len=3, .text="PUT" }, |
| 379 | }, |
| 380 | ['T' - 'A'] = { |
| 381 | [0] = { .meth = HTTP_METH_TRACE , .len=5, .text="TRACE" }, |
| 382 | }, |
| 383 | /* rest is empty like this : |
| 384 | * [1] = { .meth = HTTP_METH_NONE , .len=0, .text="" }, |
| 385 | */ |
| 386 | }; |
| 387 | |
Thierry FOURNIER | d437314 | 2013-12-17 01:10:10 +0100 | [diff] [blame] | 388 | const struct http_method_name http_known_methods[HTTP_METH_OTHER] = { |
| 389 | [HTTP_METH_NONE] = { "", 0 }, |
| 390 | [HTTP_METH_OPTIONS] = { "OPTIONS", 7 }, |
| 391 | [HTTP_METH_GET] = { "GET", 3 }, |
| 392 | [HTTP_METH_HEAD] = { "HEAD", 4 }, |
| 393 | [HTTP_METH_POST] = { "POST", 4 }, |
| 394 | [HTTP_METH_PUT] = { "PUT", 3 }, |
| 395 | [HTTP_METH_DELETE] = { "DELETE", 6 }, |
| 396 | [HTTP_METH_TRACE] = { "TRACE", 5 }, |
| 397 | [HTTP_METH_CONNECT] = { "CONNECT", 7 }, |
| 398 | }; |
| 399 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 400 | /* It is about twice as fast on recent architectures to lookup a byte in a |
matt.farnsworth@nokia.com | 1c2ab96 | 2008-04-14 20:47:37 +0200 | [diff] [blame] | 401 | * table than to perform a boolean AND or OR between two tests. Refer to |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 402 | * RFC2616 for those chars. |
| 403 | */ |
| 404 | |
| 405 | const char http_is_spht[256] = { |
| 406 | [' '] = 1, ['\t'] = 1, |
| 407 | }; |
| 408 | |
| 409 | const char http_is_crlf[256] = { |
| 410 | ['\r'] = 1, ['\n'] = 1, |
| 411 | }; |
| 412 | |
| 413 | const char http_is_lws[256] = { |
| 414 | [' '] = 1, ['\t'] = 1, |
| 415 | ['\r'] = 1, ['\n'] = 1, |
| 416 | }; |
| 417 | |
| 418 | const char http_is_sep[256] = { |
| 419 | ['('] = 1, [')'] = 1, ['<'] = 1, ['>'] = 1, |
| 420 | ['@'] = 1, [','] = 1, [';'] = 1, [':'] = 1, |
| 421 | ['"'] = 1, ['/'] = 1, ['['] = 1, [']'] = 1, |
| 422 | ['{'] = 1, ['}'] = 1, ['?'] = 1, ['='] = 1, |
| 423 | [' '] = 1, ['\t'] = 1, ['\\'] = 1, |
| 424 | }; |
| 425 | |
| 426 | const char http_is_ctl[256] = { |
| 427 | [0 ... 31] = 1, |
| 428 | [127] = 1, |
| 429 | }; |
| 430 | |
| 431 | /* |
| 432 | * A token is any ASCII char that is neither a separator nor a CTL char. |
| 433 | * Do not overwrite values in assignment since gcc-2.95 will not handle |
| 434 | * them correctly. Instead, define every non-CTL char's status. |
| 435 | */ |
| 436 | const char http_is_token[256] = { |
| 437 | [' '] = 0, ['!'] = 1, ['"'] = 0, ['#'] = 1, |
| 438 | ['$'] = 1, ['%'] = 1, ['&'] = 1, ['\''] = 1, |
| 439 | ['('] = 0, [')'] = 0, ['*'] = 1, ['+'] = 1, |
| 440 | [','] = 0, ['-'] = 1, ['.'] = 1, ['/'] = 0, |
| 441 | ['0'] = 1, ['1'] = 1, ['2'] = 1, ['3'] = 1, |
| 442 | ['4'] = 1, ['5'] = 1, ['6'] = 1, ['7'] = 1, |
| 443 | ['8'] = 1, ['9'] = 1, [':'] = 0, [';'] = 0, |
| 444 | ['<'] = 0, ['='] = 0, ['>'] = 0, ['?'] = 0, |
| 445 | ['@'] = 0, ['A'] = 1, ['B'] = 1, ['C'] = 1, |
| 446 | ['D'] = 1, ['E'] = 1, ['F'] = 1, ['G'] = 1, |
| 447 | ['H'] = 1, ['I'] = 1, ['J'] = 1, ['K'] = 1, |
| 448 | ['L'] = 1, ['M'] = 1, ['N'] = 1, ['O'] = 1, |
| 449 | ['P'] = 1, ['Q'] = 1, ['R'] = 1, ['S'] = 1, |
| 450 | ['T'] = 1, ['U'] = 1, ['V'] = 1, ['W'] = 1, |
| 451 | ['X'] = 1, ['Y'] = 1, ['Z'] = 1, ['['] = 0, |
| 452 | ['\\'] = 0, [']'] = 0, ['^'] = 1, ['_'] = 1, |
| 453 | ['`'] = 1, ['a'] = 1, ['b'] = 1, ['c'] = 1, |
| 454 | ['d'] = 1, ['e'] = 1, ['f'] = 1, ['g'] = 1, |
| 455 | ['h'] = 1, ['i'] = 1, ['j'] = 1, ['k'] = 1, |
| 456 | ['l'] = 1, ['m'] = 1, ['n'] = 1, ['o'] = 1, |
| 457 | ['p'] = 1, ['q'] = 1, ['r'] = 1, ['s'] = 1, |
| 458 | ['t'] = 1, ['u'] = 1, ['v'] = 1, ['w'] = 1, |
| 459 | ['x'] = 1, ['y'] = 1, ['z'] = 1, ['{'] = 0, |
| 460 | ['|'] = 1, ['}'] = 0, ['~'] = 1, |
| 461 | }; |
| 462 | |
| 463 | |
Willy Tarreau | 4b89ad4 | 2007-03-04 18:13:58 +0100 | [diff] [blame] | 464 | /* |
| 465 | * An http ver_token is any ASCII which can be found in an HTTP version, |
| 466 | * which includes 'H', 'T', 'P', '/', '.' and any digit. |
| 467 | */ |
| 468 | const char http_is_ver_token[256] = { |
| 469 | ['.'] = 1, ['/'] = 1, |
| 470 | ['0'] = 1, ['1'] = 1, ['2'] = 1, ['3'] = 1, ['4'] = 1, |
| 471 | ['5'] = 1, ['6'] = 1, ['7'] = 1, ['8'] = 1, ['9'] = 1, |
Thierry FOURNIER | 63d692c | 2015-02-28 19:03:56 +0100 | [diff] [blame] | 472 | ['H'] = 1, ['P'] = 1, ['R'] = 1, ['S'] = 1, ['T'] = 1, |
Willy Tarreau | 4b89ad4 | 2007-03-04 18:13:58 +0100 | [diff] [blame] | 473 | }; |
| 474 | |
| 475 | |
Willy Tarreau | 4af6f3a | 2007-03-18 22:36:26 +0100 | [diff] [blame] | 476 | /* |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 477 | * Adds a header and its CRLF at the tail of the message's buffer, just before |
| 478 | * the last CRLF. Text length is measured first, so it cannot be NULL. |
Willy Tarreau | 4af6f3a | 2007-03-18 22:36:26 +0100 | [diff] [blame] | 479 | * The header is also automatically added to the index <hdr_idx>, and the end |
| 480 | * of headers is automatically adjusted. The number of bytes added is returned |
| 481 | * on success, otherwise <0 is returned indicating an error. |
| 482 | */ |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 483 | int http_header_add_tail(struct http_msg *msg, struct hdr_idx *hdr_idx, const char *text) |
Willy Tarreau | 4af6f3a | 2007-03-18 22:36:26 +0100 | [diff] [blame] | 484 | { |
| 485 | int bytes, len; |
| 486 | |
| 487 | len = strlen(text); |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 488 | bytes = buffer_insert_line2(msg->chn->buf, msg->chn->buf->p + msg->eoh, text, len); |
Willy Tarreau | 4af6f3a | 2007-03-18 22:36:26 +0100 | [diff] [blame] | 489 | if (!bytes) |
| 490 | return -1; |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 491 | http_msg_move_end(msg, bytes); |
Willy Tarreau | 4af6f3a | 2007-03-18 22:36:26 +0100 | [diff] [blame] | 492 | return hdr_idx_add(len, 1, hdr_idx, hdr_idx->tail); |
| 493 | } |
| 494 | |
| 495 | /* |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 496 | * Adds a header and its CRLF at the tail of the message's buffer, just before |
| 497 | * the last CRLF. <len> bytes are copied, not counting the CRLF. If <text> is NULL, then |
Willy Tarreau | 4af6f3a | 2007-03-18 22:36:26 +0100 | [diff] [blame] | 498 | * the buffer is only opened and the space reserved, but nothing is copied. |
| 499 | * The header is also automatically added to the index <hdr_idx>, and the end |
| 500 | * of headers is automatically adjusted. The number of bytes added is returned |
| 501 | * on success, otherwise <0 is returned indicating an error. |
| 502 | */ |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 503 | int http_header_add_tail2(struct http_msg *msg, |
| 504 | struct hdr_idx *hdr_idx, const char *text, int len) |
Willy Tarreau | 4af6f3a | 2007-03-18 22:36:26 +0100 | [diff] [blame] | 505 | { |
| 506 | int bytes; |
| 507 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 508 | bytes = buffer_insert_line2(msg->chn->buf, msg->chn->buf->p + msg->eoh, text, len); |
Willy Tarreau | 4af6f3a | 2007-03-18 22:36:26 +0100 | [diff] [blame] | 509 | if (!bytes) |
| 510 | return -1; |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 511 | http_msg_move_end(msg, bytes); |
Willy Tarreau | 4af6f3a | 2007-03-18 22:36:26 +0100 | [diff] [blame] | 512 | return hdr_idx_add(len, 1, hdr_idx, hdr_idx->tail); |
| 513 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 514 | |
| 515 | /* |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 516 | * Checks if <hdr> is exactly <name> for <len> chars, and ends with a colon. |
| 517 | * If so, returns the position of the first non-space character relative to |
| 518 | * <hdr>, or <end>-<hdr> if not found before. If no value is found, it tries |
| 519 | * to return a pointer to the place after the first space. Returns 0 if the |
| 520 | * header name does not match. Checks are case-insensitive. |
| 521 | */ |
| 522 | int http_header_match2(const char *hdr, const char *end, |
| 523 | const char *name, int len) |
| 524 | { |
| 525 | const char *val; |
| 526 | |
| 527 | if (hdr + len >= end) |
| 528 | return 0; |
| 529 | if (hdr[len] != ':') |
| 530 | return 0; |
| 531 | if (strncasecmp(hdr, name, len) != 0) |
| 532 | return 0; |
| 533 | val = hdr + len + 1; |
| 534 | while (val < end && HTTP_IS_SPHT(*val)) |
| 535 | val++; |
| 536 | if ((val >= end) && (len + 2 <= end - hdr)) |
| 537 | return len + 2; /* we may replace starting from second space */ |
| 538 | return val - hdr; |
| 539 | } |
| 540 | |
Willy Tarreau | 04ff9f1 | 2013-06-10 18:39:42 +0200 | [diff] [blame] | 541 | /* Find the first or next occurrence of header <name> in message buffer <sol> |
| 542 | * using headers index <idx>, and return it in the <ctx> structure. This |
| 543 | * structure holds everything necessary to use the header and find next |
| 544 | * occurrence. If its <idx> member is 0, the header is searched from the |
| 545 | * beginning. Otherwise, the next occurrence is returned. The function returns |
| 546 | * 1 when it finds a value, and 0 when there is no more. It is very similar to |
| 547 | * http_find_header2() except that it is designed to work with full-line headers |
| 548 | * whose comma is not a delimiter but is part of the syntax. As a special case, |
| 549 | * if ctx->val is NULL when searching for a new values of a header, the current |
| 550 | * header is rescanned. This allows rescanning after a header deletion. |
| 551 | */ |
| 552 | int http_find_full_header2(const char *name, int len, |
| 553 | char *sol, struct hdr_idx *idx, |
| 554 | struct hdr_ctx *ctx) |
| 555 | { |
| 556 | char *eol, *sov; |
| 557 | int cur_idx, old_idx; |
| 558 | |
| 559 | cur_idx = ctx->idx; |
| 560 | if (cur_idx) { |
| 561 | /* We have previously returned a header, let's search another one */ |
| 562 | sol = ctx->line; |
| 563 | eol = sol + idx->v[cur_idx].len; |
| 564 | goto next_hdr; |
| 565 | } |
| 566 | |
| 567 | /* first request for this header */ |
| 568 | sol += hdr_idx_first_pos(idx); |
| 569 | old_idx = 0; |
| 570 | cur_idx = hdr_idx_first_idx(idx); |
| 571 | while (cur_idx) { |
| 572 | eol = sol + idx->v[cur_idx].len; |
| 573 | |
| 574 | if (len == 0) { |
| 575 | /* No argument was passed, we want any header. |
| 576 | * To achieve this, we simply build a fake request. */ |
| 577 | while (sol + len < eol && sol[len] != ':') |
| 578 | len++; |
| 579 | name = sol; |
| 580 | } |
| 581 | |
| 582 | if ((len < eol - sol) && |
| 583 | (sol[len] == ':') && |
| 584 | (strncasecmp(sol, name, len) == 0)) { |
| 585 | ctx->del = len; |
| 586 | sov = sol + len + 1; |
| 587 | while (sov < eol && http_is_lws[(unsigned char)*sov]) |
| 588 | sov++; |
| 589 | |
| 590 | ctx->line = sol; |
| 591 | ctx->prev = old_idx; |
| 592 | ctx->idx = cur_idx; |
| 593 | ctx->val = sov - sol; |
| 594 | ctx->tws = 0; |
| 595 | while (eol > sov && http_is_lws[(unsigned char)*(eol - 1)]) { |
| 596 | eol--; |
| 597 | ctx->tws++; |
| 598 | } |
| 599 | ctx->vlen = eol - sov; |
| 600 | return 1; |
| 601 | } |
| 602 | next_hdr: |
| 603 | sol = eol + idx->v[cur_idx].cr + 1; |
| 604 | old_idx = cur_idx; |
| 605 | cur_idx = idx->v[cur_idx].next; |
| 606 | } |
| 607 | return 0; |
| 608 | } |
| 609 | |
Willy Tarreau | c90dc23 | 2015-02-20 13:51:36 +0100 | [diff] [blame] | 610 | /* Find the first or next header field in message buffer <sol> using headers |
| 611 | * index <idx>, and return it in the <ctx> structure. This structure holds |
| 612 | * everything necessary to use the header and find next occurrence. If its |
| 613 | * <idx> member is 0, the first header is retrieved. Otherwise, the next |
| 614 | * occurrence is returned. The function returns 1 when it finds a value, and |
| 615 | * 0 when there is no more. It is equivalent to http_find_full_header2() with |
| 616 | * no header name. |
| 617 | */ |
| 618 | int http_find_next_header(char *sol, struct hdr_idx *idx, struct hdr_ctx *ctx) |
| 619 | { |
| 620 | char *eol, *sov; |
| 621 | int cur_idx, old_idx; |
| 622 | int len; |
| 623 | |
| 624 | cur_idx = ctx->idx; |
| 625 | if (cur_idx) { |
| 626 | /* We have previously returned a header, let's search another one */ |
| 627 | sol = ctx->line; |
| 628 | eol = sol + idx->v[cur_idx].len; |
| 629 | goto next_hdr; |
| 630 | } |
| 631 | |
| 632 | /* first request for this header */ |
| 633 | sol += hdr_idx_first_pos(idx); |
| 634 | old_idx = 0; |
| 635 | cur_idx = hdr_idx_first_idx(idx); |
| 636 | while (cur_idx) { |
| 637 | eol = sol + idx->v[cur_idx].len; |
| 638 | |
| 639 | len = 0; |
| 640 | while (1) { |
| 641 | if (len >= eol - sol) |
| 642 | goto next_hdr; |
| 643 | if (sol[len] == ':') |
| 644 | break; |
| 645 | len++; |
| 646 | } |
| 647 | |
| 648 | ctx->del = len; |
| 649 | sov = sol + len + 1; |
| 650 | while (sov < eol && http_is_lws[(unsigned char)*sov]) |
| 651 | sov++; |
| 652 | |
| 653 | ctx->line = sol; |
| 654 | ctx->prev = old_idx; |
| 655 | ctx->idx = cur_idx; |
| 656 | ctx->val = sov - sol; |
| 657 | ctx->tws = 0; |
| 658 | |
| 659 | while (eol > sov && http_is_lws[(unsigned char)*(eol - 1)]) { |
| 660 | eol--; |
| 661 | ctx->tws++; |
| 662 | } |
| 663 | ctx->vlen = eol - sov; |
| 664 | return 1; |
| 665 | |
| 666 | next_hdr: |
| 667 | sol = eol + idx->v[cur_idx].cr + 1; |
| 668 | old_idx = cur_idx; |
| 669 | cur_idx = idx->v[cur_idx].next; |
| 670 | } |
| 671 | return 0; |
| 672 | } |
| 673 | |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 674 | /* Find the end of the header value contained between <s> and <e>. See RFC2616, |
| 675 | * par 2.2 for more information. Note that it requires a valid header to return |
| 676 | * a valid result. This works for headers defined as comma-separated lists. |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 677 | */ |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 678 | char *find_hdr_value_end(char *s, const char *e) |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 679 | { |
| 680 | int quoted, qdpair; |
| 681 | |
| 682 | quoted = qdpair = 0; |
| 683 | for (; s < e; s++) { |
| 684 | if (qdpair) qdpair = 0; |
Willy Tarreau | 0f7f51f | 2010-08-30 11:06:34 +0200 | [diff] [blame] | 685 | else if (quoted) { |
| 686 | if (*s == '\\') qdpair = 1; |
| 687 | else if (*s == '"') quoted = 0; |
| 688 | } |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 689 | else if (*s == '"') quoted = 1; |
| 690 | else if (*s == ',') return s; |
| 691 | } |
| 692 | return s; |
| 693 | } |
| 694 | |
| 695 | /* Find the first or next occurrence of header <name> in message buffer <sol> |
| 696 | * using headers index <idx>, and return it in the <ctx> structure. This |
| 697 | * structure holds everything necessary to use the header and find next |
| 698 | * occurrence. If its <idx> member is 0, the header is searched from the |
| 699 | * beginning. Otherwise, the next occurrence is returned. The function returns |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 700 | * 1 when it finds a value, and 0 when there is no more. It is designed to work |
| 701 | * with headers defined as comma-separated lists. As a special case, if ctx->val |
| 702 | * is NULL when searching for a new values of a header, the current header is |
| 703 | * rescanned. This allows rescanning after a header deletion. |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 704 | */ |
| 705 | int http_find_header2(const char *name, int len, |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 706 | char *sol, struct hdr_idx *idx, |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 707 | struct hdr_ctx *ctx) |
| 708 | { |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 709 | char *eol, *sov; |
| 710 | int cur_idx, old_idx; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 711 | |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 712 | cur_idx = ctx->idx; |
| 713 | if (cur_idx) { |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 714 | /* We have previously returned a value, let's search |
| 715 | * another one on the same line. |
| 716 | */ |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 717 | sol = ctx->line; |
Willy Tarreau | 588bd4f | 2011-09-01 22:22:28 +0200 | [diff] [blame] | 718 | ctx->del = ctx->val + ctx->vlen + ctx->tws; |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 719 | sov = sol + ctx->del; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 720 | eol = sol + idx->v[cur_idx].len; |
| 721 | |
| 722 | if (sov >= eol) |
| 723 | /* no more values in this header */ |
| 724 | goto next_hdr; |
| 725 | |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 726 | /* values remaining for this header, skip the comma but save it |
| 727 | * for later use (eg: for header deletion). |
| 728 | */ |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 729 | sov++; |
| 730 | while (sov < eol && http_is_lws[(unsigned char)*sov]) |
| 731 | sov++; |
| 732 | |
| 733 | goto return_hdr; |
| 734 | } |
| 735 | |
| 736 | /* first request for this header */ |
| 737 | sol += hdr_idx_first_pos(idx); |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 738 | old_idx = 0; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 739 | cur_idx = hdr_idx_first_idx(idx); |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 740 | while (cur_idx) { |
| 741 | eol = sol + idx->v[cur_idx].len; |
| 742 | |
Willy Tarreau | 1ad7c6d | 2007-06-10 21:42:55 +0200 | [diff] [blame] | 743 | if (len == 0) { |
| 744 | /* No argument was passed, we want any header. |
| 745 | * To achieve this, we simply build a fake request. */ |
| 746 | while (sol + len < eol && sol[len] != ':') |
| 747 | len++; |
| 748 | name = sol; |
| 749 | } |
| 750 | |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 751 | if ((len < eol - sol) && |
| 752 | (sol[len] == ':') && |
| 753 | (strncasecmp(sol, name, len) == 0)) { |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 754 | ctx->del = len; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 755 | sov = sol + len + 1; |
| 756 | while (sov < eol && http_is_lws[(unsigned char)*sov]) |
| 757 | sov++; |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 758 | |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 759 | ctx->line = sol; |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 760 | ctx->prev = old_idx; |
| 761 | return_hdr: |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 762 | ctx->idx = cur_idx; |
| 763 | ctx->val = sov - sol; |
| 764 | |
| 765 | eol = find_hdr_value_end(sov, eol); |
Willy Tarreau | 588bd4f | 2011-09-01 22:22:28 +0200 | [diff] [blame] | 766 | ctx->tws = 0; |
Willy Tarreau | 275600b | 2011-09-16 08:11:26 +0200 | [diff] [blame] | 767 | while (eol > sov && http_is_lws[(unsigned char)*(eol - 1)]) { |
Willy Tarreau | 588bd4f | 2011-09-01 22:22:28 +0200 | [diff] [blame] | 768 | eol--; |
| 769 | ctx->tws++; |
| 770 | } |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 771 | ctx->vlen = eol - sov; |
| 772 | return 1; |
| 773 | } |
| 774 | next_hdr: |
| 775 | sol = eol + idx->v[cur_idx].cr + 1; |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 776 | old_idx = cur_idx; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 777 | cur_idx = idx->v[cur_idx].next; |
| 778 | } |
| 779 | return 0; |
| 780 | } |
| 781 | |
| 782 | int http_find_header(const char *name, |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 783 | char *sol, struct hdr_idx *idx, |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 784 | struct hdr_ctx *ctx) |
| 785 | { |
| 786 | return http_find_header2(name, strlen(name), sol, idx, ctx); |
| 787 | } |
| 788 | |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 789 | /* Remove one value of a header. This only works on a <ctx> returned by one of |
| 790 | * the http_find_header functions. The value is removed, as well as surrounding |
| 791 | * commas if any. If the removed value was alone, the whole header is removed. |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 792 | * The ctx is always updated accordingly, as well as the buffer and HTTP |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 793 | * message <msg>. The new index is returned. If it is zero, it means there is |
| 794 | * no more header, so any processing may stop. The ctx is always left in a form |
| 795 | * that can be handled by http_find_header2() to find next occurrence. |
| 796 | */ |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 797 | int http_remove_header2(struct http_msg *msg, struct hdr_idx *idx, struct hdr_ctx *ctx) |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 798 | { |
| 799 | int cur_idx = ctx->idx; |
| 800 | char *sol = ctx->line; |
| 801 | struct hdr_idx_elem *hdr; |
| 802 | int delta, skip_comma; |
| 803 | |
| 804 | if (!cur_idx) |
| 805 | return 0; |
| 806 | |
| 807 | hdr = &idx->v[cur_idx]; |
Willy Tarreau | 588bd4f | 2011-09-01 22:22:28 +0200 | [diff] [blame] | 808 | if (sol[ctx->del] == ':' && ctx->val + ctx->vlen + ctx->tws == hdr->len) { |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 809 | /* This was the only value of the header, we must now remove it entirely. */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 810 | delta = buffer_replace2(msg->chn->buf, sol, sol + hdr->len + hdr->cr + 1, NULL, 0); |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 811 | http_msg_move_end(msg, delta); |
| 812 | idx->used--; |
| 813 | hdr->len = 0; /* unused entry */ |
| 814 | idx->v[ctx->prev].next = idx->v[ctx->idx].next; |
Willy Tarreau | 5c4784f | 2011-02-12 13:07:35 +0100 | [diff] [blame] | 815 | if (idx->tail == ctx->idx) |
| 816 | idx->tail = ctx->prev; |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 817 | ctx->idx = ctx->prev; /* walk back to the end of previous header */ |
Willy Tarreau | 7c1c217 | 2015-01-07 17:23:50 +0100 | [diff] [blame] | 818 | ctx->line -= idx->v[ctx->idx].len + idx->v[ctx->idx].cr + 1; |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 819 | ctx->val = idx->v[ctx->idx].len; /* point to end of previous header */ |
Willy Tarreau | 588bd4f | 2011-09-01 22:22:28 +0200 | [diff] [blame] | 820 | ctx->tws = ctx->vlen = 0; |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 821 | return ctx->idx; |
| 822 | } |
| 823 | |
| 824 | /* This was not the only value of this header. We have to remove between |
Willy Tarreau | 588bd4f | 2011-09-01 22:22:28 +0200 | [diff] [blame] | 825 | * ctx->del+1 and ctx->val+ctx->vlen+ctx->tws+1 included. If it is the |
| 826 | * last entry of the list, we remove the last separator. |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 827 | */ |
| 828 | |
Willy Tarreau | 588bd4f | 2011-09-01 22:22:28 +0200 | [diff] [blame] | 829 | skip_comma = (ctx->val + ctx->vlen + ctx->tws == hdr->len) ? 0 : 1; |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 830 | delta = buffer_replace2(msg->chn->buf, sol + ctx->del + skip_comma, |
Willy Tarreau | 588bd4f | 2011-09-01 22:22:28 +0200 | [diff] [blame] | 831 | sol + ctx->val + ctx->vlen + ctx->tws + skip_comma, |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 832 | NULL, 0); |
| 833 | hdr->len += delta; |
| 834 | http_msg_move_end(msg, delta); |
| 835 | ctx->val = ctx->del; |
Willy Tarreau | 588bd4f | 2011-09-01 22:22:28 +0200 | [diff] [blame] | 836 | ctx->tws = ctx->vlen = 0; |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 837 | return ctx->idx; |
| 838 | } |
| 839 | |
Willy Tarreau | 2d3d94c | 2008-11-30 20:20:08 +0100 | [diff] [blame] | 840 | /* This function handles a server error at the stream interface level. The |
| 841 | * stream interface is assumed to be already in a closed state. An optional |
| 842 | * message is copied into the input buffer, and an HTTP status code stored. |
| 843 | * The error flags are set to the values in arguments. Any pending request |
Willy Tarreau | 6f0aa47 | 2009-03-08 20:33:29 +0100 | [diff] [blame] | 844 | * in this buffer will be lost. |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 845 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 846 | static void http_server_error(struct stream *s, struct stream_interface *si, |
Willy Tarreau | 2d3d94c | 2008-11-30 20:20:08 +0100 | [diff] [blame] | 847 | int err, int finst, int status, const struct chunk *msg) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 848 | { |
Willy Tarreau | 2bb4a96 | 2014-11-28 11:11:05 +0100 | [diff] [blame] | 849 | channel_auto_read(si_oc(si)); |
| 850 | channel_abort(si_oc(si)); |
| 851 | channel_auto_close(si_oc(si)); |
| 852 | channel_erase(si_oc(si)); |
| 853 | channel_auto_close(si_ic(si)); |
| 854 | channel_auto_read(si_ic(si)); |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 855 | if (status > 0 && msg) { |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 856 | s->txn->status = status; |
Willy Tarreau | 2bb4a96 | 2014-11-28 11:11:05 +0100 | [diff] [blame] | 857 | bo_inject(si_ic(si), msg->str, msg->len); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 858 | } |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 859 | if (!(s->flags & SF_ERR_MASK)) |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 860 | s->flags |= err; |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 861 | if (!(s->flags & SF_FINST_MASK)) |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 862 | s->flags |= finst; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 863 | } |
| 864 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 865 | /* This function returns the appropriate error location for the given stream |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 866 | * and message. |
| 867 | */ |
| 868 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 869 | struct chunk *http_error_message(struct stream *s, int msgnum) |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 870 | { |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 871 | if (s->be->errmsg[msgnum].str) |
| 872 | return &s->be->errmsg[msgnum]; |
Willy Tarreau | d0d8da9 | 2015-04-04 02:10:38 +0200 | [diff] [blame] | 873 | else if (strm_fe(s)->errmsg[msgnum].str) |
| 874 | return &strm_fe(s)->errmsg[msgnum]; |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 875 | else |
| 876 | return &http_err_chunks[msgnum]; |
| 877 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 878 | |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 879 | /* |
| 880 | * returns HTTP_METH_NONE if there is nothing valid to read (empty or non-text |
| 881 | * string), HTTP_METH_OTHER for unknown methods, or the identified method. |
| 882 | */ |
Thierry FOURNIER | d437314 | 2013-12-17 01:10:10 +0100 | [diff] [blame] | 883 | enum http_meth_t find_http_meth(const char *str, const int len) |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 884 | { |
| 885 | unsigned char m; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 886 | const struct http_method_desc *h; |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 887 | |
| 888 | m = ((unsigned)*str - 'A'); |
| 889 | |
| 890 | if (m < 26) { |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 891 | for (h = http_methods[m]; h->len > 0; h++) { |
| 892 | if (unlikely(h->len != len)) |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 893 | continue; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 894 | if (likely(memcmp(str, h->text, h->len) == 0)) |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 895 | return h->meth; |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 896 | }; |
| 897 | return HTTP_METH_OTHER; |
| 898 | } |
| 899 | return HTTP_METH_NONE; |
| 900 | |
| 901 | } |
| 902 | |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 903 | /* Parse the URI from the given transaction (which is assumed to be in request |
| 904 | * phase) and look for the "/" beginning the PATH. If not found, return NULL. |
| 905 | * It is returned otherwise. |
| 906 | */ |
| 907 | static char * |
| 908 | http_get_path(struct http_txn *txn) |
| 909 | { |
| 910 | char *ptr, *end; |
| 911 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 912 | ptr = txn->req.chn->buf->p + txn->req.sl.rq.u; |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 913 | end = ptr + txn->req.sl.rq.u_l; |
| 914 | |
| 915 | if (ptr >= end) |
| 916 | return NULL; |
| 917 | |
| 918 | /* RFC2616, par. 5.1.2 : |
| 919 | * Request-URI = "*" | absuri | abspath | authority |
| 920 | */ |
| 921 | |
| 922 | if (*ptr == '*') |
| 923 | return NULL; |
| 924 | |
| 925 | if (isalpha((unsigned char)*ptr)) { |
| 926 | /* this is a scheme as described by RFC3986, par. 3.1 */ |
| 927 | ptr++; |
| 928 | while (ptr < end && |
| 929 | (isalnum((unsigned char)*ptr) || *ptr == '+' || *ptr == '-' || *ptr == '.')) |
| 930 | ptr++; |
| 931 | /* skip '://' */ |
| 932 | if (ptr == end || *ptr++ != ':') |
| 933 | return NULL; |
| 934 | if (ptr == end || *ptr++ != '/') |
| 935 | return NULL; |
| 936 | if (ptr == end || *ptr++ != '/') |
| 937 | return NULL; |
| 938 | } |
| 939 | /* skip [user[:passwd]@]host[:[port]] */ |
| 940 | |
| 941 | while (ptr < end && *ptr != '/') |
| 942 | ptr++; |
| 943 | |
| 944 | if (ptr == end) |
| 945 | return NULL; |
| 946 | |
| 947 | /* OK, we got the '/' ! */ |
| 948 | return ptr; |
| 949 | } |
| 950 | |
William Lallemand | 65ad6e1 | 2014-01-31 15:08:02 +0100 | [diff] [blame] | 951 | /* Parse the URI from the given string and look for the "/" beginning the PATH. |
| 952 | * If not found, return NULL. It is returned otherwise. |
| 953 | */ |
| 954 | static char * |
| 955 | http_get_path_from_string(char *str) |
| 956 | { |
| 957 | char *ptr = str; |
| 958 | |
| 959 | /* RFC2616, par. 5.1.2 : |
| 960 | * Request-URI = "*" | absuri | abspath | authority |
| 961 | */ |
| 962 | |
| 963 | if (*ptr == '*') |
| 964 | return NULL; |
| 965 | |
| 966 | if (isalpha((unsigned char)*ptr)) { |
| 967 | /* this is a scheme as described by RFC3986, par. 3.1 */ |
| 968 | ptr++; |
| 969 | while (isalnum((unsigned char)*ptr) || *ptr == '+' || *ptr == '-' || *ptr == '.') |
| 970 | ptr++; |
| 971 | /* skip '://' */ |
| 972 | if (*ptr == '\0' || *ptr++ != ':') |
| 973 | return NULL; |
| 974 | if (*ptr == '\0' || *ptr++ != '/') |
| 975 | return NULL; |
| 976 | if (*ptr == '\0' || *ptr++ != '/') |
| 977 | return NULL; |
| 978 | } |
| 979 | /* skip [user[:passwd]@]host[:[port]] */ |
| 980 | |
| 981 | while (*ptr != '\0' && *ptr != ' ' && *ptr != '/') |
| 982 | ptr++; |
| 983 | |
| 984 | if (*ptr == '\0' || *ptr == ' ') |
| 985 | return NULL; |
| 986 | |
| 987 | /* OK, we got the '/' ! */ |
| 988 | return ptr; |
| 989 | } |
| 990 | |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 991 | /* Returns a 302 for a redirectable request that reaches a server working in |
| 992 | * in redirect mode. This may only be called just after the stream interface |
| 993 | * has moved to SI_ST_ASS. Unprocessable requests are left unchanged and will |
| 994 | * follow normal proxy processing. NOTE: this function is designed to support |
| 995 | * being called once data are scheduled for forwarding. |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 996 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 997 | void http_perform_server_redirect(struct stream *s, struct stream_interface *si) |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 998 | { |
| 999 | struct http_txn *txn; |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 1000 | struct server *srv; |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 1001 | char *path; |
Willy Tarreau | cde18fc | 2012-05-30 07:59:54 +0200 | [diff] [blame] | 1002 | int len, rewind; |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 1003 | |
| 1004 | /* 1: create the response header */ |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 1005 | trash.len = strlen(HTTP_302); |
| 1006 | memcpy(trash.str, HTTP_302, trash.len); |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 1007 | |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 1008 | srv = objt_server(s->target); |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 1009 | |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 1010 | /* 2: add the server's prefix */ |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 1011 | if (trash.len + srv->rdr_len > trash.size) |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 1012 | return; |
| 1013 | |
Willy Tarreau | dcb75c4 | 2010-01-10 00:24:22 +0100 | [diff] [blame] | 1014 | /* special prefix "/" means don't change URL */ |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 1015 | if (srv->rdr_len != 1 || *srv->rdr_pfx != '/') { |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 1016 | memcpy(trash.str + trash.len, srv->rdr_pfx, srv->rdr_len); |
| 1017 | trash.len += srv->rdr_len; |
Willy Tarreau | dcb75c4 | 2010-01-10 00:24:22 +0100 | [diff] [blame] | 1018 | } |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 1019 | |
Willy Tarreau | cde18fc | 2012-05-30 07:59:54 +0200 | [diff] [blame] | 1020 | /* 3: add the request URI. Since it was already forwarded, we need |
| 1021 | * to temporarily rewind the buffer. |
| 1022 | */ |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 1023 | txn = s->txn; |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 1024 | b_rew(s->req.buf, rewind = http_hdr_rewind(&txn->req)); |
Willy Tarreau | cde18fc | 2012-05-30 07:59:54 +0200 | [diff] [blame] | 1025 | |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 1026 | path = http_get_path(txn); |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 1027 | len = buffer_count(s->req.buf, path, b_ptr(s->req.buf, txn->req.sl.rq.u + txn->req.sl.rq.u_l)); |
Willy Tarreau | cde18fc | 2012-05-30 07:59:54 +0200 | [diff] [blame] | 1028 | |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 1029 | b_adv(s->req.buf, rewind); |
Willy Tarreau | cde18fc | 2012-05-30 07:59:54 +0200 | [diff] [blame] | 1030 | |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 1031 | if (!path) |
| 1032 | return; |
| 1033 | |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 1034 | if (trash.len + len > trash.size - 4) /* 4 for CRLF-CRLF */ |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 1035 | return; |
| 1036 | |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 1037 | memcpy(trash.str + trash.len, path, len); |
| 1038 | trash.len += len; |
Willy Tarreau | 88d349d | 2010-01-25 12:15:43 +0100 | [diff] [blame] | 1039 | |
| 1040 | if (unlikely(txn->flags & TX_USE_PX_CONN)) { |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 1041 | memcpy(trash.str + trash.len, "\r\nProxy-Connection: close\r\n\r\n", 29); |
| 1042 | trash.len += 29; |
Willy Tarreau | 88d349d | 2010-01-25 12:15:43 +0100 | [diff] [blame] | 1043 | } else { |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 1044 | memcpy(trash.str + trash.len, "\r\nConnection: close\r\n\r\n", 23); |
| 1045 | trash.len += 23; |
Willy Tarreau | 88d349d | 2010-01-25 12:15:43 +0100 | [diff] [blame] | 1046 | } |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 1047 | |
| 1048 | /* prepare to return without error. */ |
Willy Tarreau | 73b013b | 2012-05-21 16:31:45 +0200 | [diff] [blame] | 1049 | si_shutr(si); |
| 1050 | si_shutw(si); |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 1051 | si->err_type = SI_ET_NONE; |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 1052 | si->state = SI_ST_CLO; |
| 1053 | |
| 1054 | /* send the message */ |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 1055 | http_server_error(s, si, SF_ERR_LOCAL, SF_FINST_C, 302, &trash); |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 1056 | |
| 1057 | /* FIXME: we should increase a counter of redirects per server and per backend. */ |
Willy Tarreau | 4521ba6 | 2013-01-24 01:25:25 +0100 | [diff] [blame] | 1058 | srv_inc_sess_ctr(srv); |
Bhaskar Maddala | a20cb85 | 2014-02-03 16:26:46 -0500 | [diff] [blame] | 1059 | srv_set_sess_last(srv); |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 1060 | } |
| 1061 | |
Willy Tarreau | 0cac36f | 2008-11-30 20:44:17 +0100 | [diff] [blame] | 1062 | /* Return the error message corresponding to si->err_type. It is assumed |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 1063 | * that the server side is closed. Note that err_type is actually a |
| 1064 | * bitmask, where almost only aborts may be cumulated with other |
| 1065 | * values. We consider that aborted operations are more important |
| 1066 | * than timeouts or errors due to the fact that nobody else in the |
| 1067 | * logs might explain incomplete retries. All others should avoid |
| 1068 | * being cumulated. It should normally not be possible to have multiple |
| 1069 | * aborts at once, but just in case, the first one in sequence is reported. |
Willy Tarreau | 6b726ad | 2013-12-15 19:31:37 +0100 | [diff] [blame] | 1070 | * Note that connection errors appearing on the second request of a keep-alive |
| 1071 | * connection are not reported since this allows the client to retry. |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 1072 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 1073 | void http_return_srv_error(struct stream *s, struct stream_interface *si) |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 1074 | { |
Willy Tarreau | 0cac36f | 2008-11-30 20:44:17 +0100 | [diff] [blame] | 1075 | int err_type = si->err_type; |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 1076 | |
| 1077 | if (err_type & SI_ET_QUEUE_ABRT) |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 1078 | http_server_error(s, si, SF_ERR_CLICL, SF_FINST_Q, |
Willy Tarreau | 783f258 | 2012-09-04 12:19:04 +0200 | [diff] [blame] | 1079 | 503, http_error_message(s, HTTP_ERR_503)); |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 1080 | else if (err_type & SI_ET_CONN_ABRT) |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 1081 | http_server_error(s, si, SF_ERR_CLICL, SF_FINST_C, |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 1082 | 503, (s->txn->flags & TX_NOT_FIRST) ? NULL : |
Willy Tarreau | 6b726ad | 2013-12-15 19:31:37 +0100 | [diff] [blame] | 1083 | http_error_message(s, HTTP_ERR_503)); |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 1084 | else if (err_type & SI_ET_QUEUE_TO) |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 1085 | http_server_error(s, si, SF_ERR_SRVTO, SF_FINST_Q, |
Willy Tarreau | 783f258 | 2012-09-04 12:19:04 +0200 | [diff] [blame] | 1086 | 503, http_error_message(s, HTTP_ERR_503)); |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 1087 | else if (err_type & SI_ET_QUEUE_ERR) |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 1088 | http_server_error(s, si, SF_ERR_SRVCL, SF_FINST_Q, |
Willy Tarreau | 783f258 | 2012-09-04 12:19:04 +0200 | [diff] [blame] | 1089 | 503, http_error_message(s, HTTP_ERR_503)); |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 1090 | else if (err_type & SI_ET_CONN_TO) |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 1091 | http_server_error(s, si, SF_ERR_SRVTO, SF_FINST_C, |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 1092 | 503, (s->txn->flags & TX_NOT_FIRST) ? NULL : |
Willy Tarreau | 6b726ad | 2013-12-15 19:31:37 +0100 | [diff] [blame] | 1093 | http_error_message(s, HTTP_ERR_503)); |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 1094 | else if (err_type & SI_ET_CONN_ERR) |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 1095 | http_server_error(s, si, SF_ERR_SRVCL, SF_FINST_C, |
| 1096 | 503, (s->flags & SF_SRV_REUSED) ? NULL : |
Willy Tarreau | 6b726ad | 2013-12-15 19:31:37 +0100 | [diff] [blame] | 1097 | http_error_message(s, HTTP_ERR_503)); |
Willy Tarreau | 2d400bb | 2012-05-14 12:11:47 +0200 | [diff] [blame] | 1098 | else if (err_type & SI_ET_CONN_RES) |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 1099 | http_server_error(s, si, SF_ERR_RESOURCE, SF_FINST_C, |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 1100 | 503, (s->txn->flags & TX_NOT_FIRST) ? NULL : |
Willy Tarreau | 6b726ad | 2013-12-15 19:31:37 +0100 | [diff] [blame] | 1101 | http_error_message(s, HTTP_ERR_503)); |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 1102 | else /* SI_ET_CONN_OTHER and others */ |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 1103 | http_server_error(s, si, SF_ERR_INTERNAL, SF_FINST_C, |
Willy Tarreau | 783f258 | 2012-09-04 12:19:04 +0200 | [diff] [blame] | 1104 | 500, http_error_message(s, HTTP_ERR_500)); |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 1105 | } |
| 1106 | |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 1107 | extern const char sess_term_cond[8]; |
| 1108 | extern const char sess_fin_state[8]; |
| 1109 | extern const char *monthname[12]; |
Willy Tarreau | 63986c7 | 2015-04-03 22:55:33 +0200 | [diff] [blame] | 1110 | struct pool_head *pool2_http_txn; |
Willy Tarreau | 332f8bf | 2007-05-13 21:36:56 +0200 | [diff] [blame] | 1111 | struct pool_head *pool2_requri; |
Willy Tarreau | 193b8c6 | 2012-11-22 00:17:38 +0100 | [diff] [blame] | 1112 | struct pool_head *pool2_capture = NULL; |
William Lallemand | a73203e | 2012-03-12 12:48:57 +0100 | [diff] [blame] | 1113 | struct pool_head *pool2_uniqueid; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1114 | |
Willy Tarreau | 117f59e | 2007-03-04 18:17:17 +0100 | [diff] [blame] | 1115 | /* |
| 1116 | * Capture headers from message starting at <som> according to header list |
Willy Tarreau | 54da8db | 2014-06-13 16:11:48 +0200 | [diff] [blame] | 1117 | * <cap_hdr>, and fill the <cap> pointers appropriately. |
Willy Tarreau | 117f59e | 2007-03-04 18:17:17 +0100 | [diff] [blame] | 1118 | */ |
| 1119 | void capture_headers(char *som, struct hdr_idx *idx, |
| 1120 | char **cap, struct cap_hdr *cap_hdr) |
| 1121 | { |
| 1122 | char *eol, *sol, *col, *sov; |
| 1123 | int cur_idx; |
| 1124 | struct cap_hdr *h; |
| 1125 | int len; |
| 1126 | |
| 1127 | sol = som + hdr_idx_first_pos(idx); |
| 1128 | cur_idx = hdr_idx_first_idx(idx); |
| 1129 | |
| 1130 | while (cur_idx) { |
| 1131 | eol = sol + idx->v[cur_idx].len; |
| 1132 | |
| 1133 | col = sol; |
| 1134 | while (col < eol && *col != ':') |
| 1135 | col++; |
| 1136 | |
| 1137 | sov = col + 1; |
| 1138 | while (sov < eol && http_is_lws[(unsigned char)*sov]) |
| 1139 | sov++; |
| 1140 | |
| 1141 | for (h = cap_hdr; h; h = h->next) { |
Willy Tarreau | 54da8db | 2014-06-13 16:11:48 +0200 | [diff] [blame] | 1142 | if (h->namelen && (h->namelen == col - sol) && |
Willy Tarreau | 117f59e | 2007-03-04 18:17:17 +0100 | [diff] [blame] | 1143 | (strncasecmp(sol, h->name, h->namelen) == 0)) { |
| 1144 | if (cap[h->index] == NULL) |
| 1145 | cap[h->index] = |
Willy Tarreau | cf7f320 | 2007-05-13 22:46:04 +0200 | [diff] [blame] | 1146 | pool_alloc2(h->pool); |
Willy Tarreau | 117f59e | 2007-03-04 18:17:17 +0100 | [diff] [blame] | 1147 | |
| 1148 | if (cap[h->index] == NULL) { |
| 1149 | Alert("HTTP capture : out of memory.\n"); |
| 1150 | continue; |
| 1151 | } |
| 1152 | |
| 1153 | len = eol - sov; |
| 1154 | if (len > h->len) |
| 1155 | len = h->len; |
| 1156 | |
| 1157 | memcpy(cap[h->index], sov, len); |
| 1158 | cap[h->index][len]=0; |
| 1159 | } |
| 1160 | } |
| 1161 | sol = eol + idx->v[cur_idx].cr + 1; |
| 1162 | cur_idx = idx->v[cur_idx].next; |
| 1163 | } |
| 1164 | } |
| 1165 | |
| 1166 | |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 1167 | /* either we find an LF at <ptr> or we jump to <bad>. |
| 1168 | */ |
| 1169 | #define EXPECT_LF_HERE(ptr, bad) do { if (unlikely(*(ptr) != '\n')) goto bad; } while (0) |
| 1170 | |
| 1171 | /* plays with variables <ptr>, <end> and <state>. Jumps to <good> if OK, |
| 1172 | * otherwise to <http_msg_ood> with <state> set to <st>. |
| 1173 | */ |
| 1174 | #define EAT_AND_JUMP_OR_RETURN(good, st) do { \ |
| 1175 | ptr++; \ |
| 1176 | if (likely(ptr < end)) \ |
| 1177 | goto good; \ |
| 1178 | else { \ |
| 1179 | state = (st); \ |
| 1180 | goto http_msg_ood; \ |
| 1181 | } \ |
| 1182 | } while (0) |
| 1183 | |
| 1184 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1185 | /* |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 1186 | * This function parses a status line between <ptr> and <end>, starting with |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1187 | * parser state <state>. Only states HTTP_MSG_RPVER, HTTP_MSG_RPVER_SP, |
| 1188 | * HTTP_MSG_RPCODE, HTTP_MSG_RPCODE_SP and HTTP_MSG_RPREASON are handled. Others |
| 1189 | * will give undefined results. |
| 1190 | * Note that it is upon the caller's responsibility to ensure that ptr < end, |
| 1191 | * and that msg->sol points to the beginning of the response. |
| 1192 | * If a complete line is found (which implies that at least one CR or LF is |
| 1193 | * found before <end>, the updated <ptr> is returned, otherwise NULL is |
| 1194 | * returned indicating an incomplete line (which does not mean that parts have |
| 1195 | * not been updated). In the incomplete case, if <ret_ptr> or <ret_state> are |
| 1196 | * non-NULL, they are fed with the new <ptr> and <state> values to be passed |
| 1197 | * upon next call. |
| 1198 | * |
Willy Tarreau | 9cdde23 | 2007-05-02 20:58:19 +0200 | [diff] [blame] | 1199 | * This function was intentionally designed to be called from |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1200 | * http_msg_analyzer() with the lowest overhead. It should integrate perfectly |
| 1201 | * within its state machine and use the same macros, hence the need for same |
Willy Tarreau | 9cdde23 | 2007-05-02 20:58:19 +0200 | [diff] [blame] | 1202 | * labels and variable names. Note that msg->sol is left unchanged. |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1203 | */ |
Willy Tarreau | 69d8c5d | 2012-05-08 09:44:41 +0200 | [diff] [blame] | 1204 | const char *http_parse_stsline(struct http_msg *msg, |
Willy Tarreau | 3770f23 | 2013-12-07 00:01:53 +0100 | [diff] [blame] | 1205 | enum ht_state state, const char *ptr, const char *end, |
| 1206 | unsigned int *ret_ptr, enum ht_state *ret_state) |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1207 | { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 1208 | const char *msg_start = msg->chn->buf->p; |
Willy Tarreau | 62f791e | 2012-03-09 11:32:30 +0100 | [diff] [blame] | 1209 | |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1210 | switch (state) { |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1211 | case HTTP_MSG_RPVER: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1212 | http_msg_rpver: |
Willy Tarreau | 4b89ad4 | 2007-03-04 18:13:58 +0100 | [diff] [blame] | 1213 | if (likely(HTTP_IS_VER_TOKEN(*ptr))) |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1214 | EAT_AND_JUMP_OR_RETURN(http_msg_rpver, HTTP_MSG_RPVER); |
| 1215 | |
| 1216 | if (likely(HTTP_IS_SPHT(*ptr))) { |
Willy Tarreau | ea1175a | 2012-03-05 15:52:30 +0100 | [diff] [blame] | 1217 | msg->sl.st.v_l = ptr - msg_start; |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1218 | EAT_AND_JUMP_OR_RETURN(http_msg_rpver_sp, HTTP_MSG_RPVER_SP); |
| 1219 | } |
Willy Tarreau | 7552c03 | 2009-03-01 11:10:40 +0100 | [diff] [blame] | 1220 | state = HTTP_MSG_ERROR; |
| 1221 | break; |
| 1222 | |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1223 | case HTTP_MSG_RPVER_SP: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1224 | http_msg_rpver_sp: |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1225 | if (likely(!HTTP_IS_LWS(*ptr))) { |
Willy Tarreau | ea1175a | 2012-03-05 15:52:30 +0100 | [diff] [blame] | 1226 | msg->sl.st.c = ptr - msg_start; |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1227 | goto http_msg_rpcode; |
| 1228 | } |
| 1229 | if (likely(HTTP_IS_SPHT(*ptr))) |
| 1230 | EAT_AND_JUMP_OR_RETURN(http_msg_rpver_sp, HTTP_MSG_RPVER_SP); |
| 1231 | /* so it's a CR/LF, this is invalid */ |
Willy Tarreau | 7552c03 | 2009-03-01 11:10:40 +0100 | [diff] [blame] | 1232 | state = HTTP_MSG_ERROR; |
| 1233 | break; |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1234 | |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1235 | case HTTP_MSG_RPCODE: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1236 | http_msg_rpcode: |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1237 | if (likely(!HTTP_IS_LWS(*ptr))) |
| 1238 | EAT_AND_JUMP_OR_RETURN(http_msg_rpcode, HTTP_MSG_RPCODE); |
| 1239 | |
| 1240 | if (likely(HTTP_IS_SPHT(*ptr))) { |
Willy Tarreau | ea1175a | 2012-03-05 15:52:30 +0100 | [diff] [blame] | 1241 | msg->sl.st.c_l = ptr - msg_start - msg->sl.st.c; |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1242 | EAT_AND_JUMP_OR_RETURN(http_msg_rpcode_sp, HTTP_MSG_RPCODE_SP); |
| 1243 | } |
| 1244 | |
| 1245 | /* so it's a CR/LF, so there is no reason phrase */ |
Willy Tarreau | ea1175a | 2012-03-05 15:52:30 +0100 | [diff] [blame] | 1246 | msg->sl.st.c_l = ptr - msg_start - msg->sl.st.c; |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1247 | http_msg_rsp_reason: |
| 1248 | /* FIXME: should we support HTTP responses without any reason phrase ? */ |
Willy Tarreau | ea1175a | 2012-03-05 15:52:30 +0100 | [diff] [blame] | 1249 | msg->sl.st.r = ptr - msg_start; |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1250 | msg->sl.st.r_l = 0; |
| 1251 | goto http_msg_rpline_eol; |
| 1252 | |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1253 | case HTTP_MSG_RPCODE_SP: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1254 | http_msg_rpcode_sp: |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1255 | if (likely(!HTTP_IS_LWS(*ptr))) { |
Willy Tarreau | ea1175a | 2012-03-05 15:52:30 +0100 | [diff] [blame] | 1256 | msg->sl.st.r = ptr - msg_start; |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1257 | goto http_msg_rpreason; |
| 1258 | } |
| 1259 | if (likely(HTTP_IS_SPHT(*ptr))) |
| 1260 | EAT_AND_JUMP_OR_RETURN(http_msg_rpcode_sp, HTTP_MSG_RPCODE_SP); |
| 1261 | /* so it's a CR/LF, so there is no reason phrase */ |
| 1262 | goto http_msg_rsp_reason; |
| 1263 | |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1264 | case HTTP_MSG_RPREASON: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1265 | http_msg_rpreason: |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1266 | if (likely(!HTTP_IS_CRLF(*ptr))) |
| 1267 | EAT_AND_JUMP_OR_RETURN(http_msg_rpreason, HTTP_MSG_RPREASON); |
Willy Tarreau | ea1175a | 2012-03-05 15:52:30 +0100 | [diff] [blame] | 1268 | msg->sl.st.r_l = ptr - msg_start - msg->sl.st.r; |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1269 | http_msg_rpline_eol: |
| 1270 | /* We have seen the end of line. Note that we do not |
| 1271 | * necessarily have the \n yet, but at least we know that we |
| 1272 | * have EITHER \r OR \n, otherwise the response would not be |
| 1273 | * complete. We can then record the response length and return |
| 1274 | * to the caller which will be able to register it. |
| 1275 | */ |
Willy Tarreau | 3a215be | 2012-03-09 21:39:51 +0100 | [diff] [blame] | 1276 | msg->sl.st.l = ptr - msg_start - msg->sol; |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1277 | return ptr; |
| 1278 | |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1279 | default: |
Willy Tarreau | 3770f23 | 2013-12-07 00:01:53 +0100 | [diff] [blame] | 1280 | #ifdef DEBUG_FULL |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1281 | fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state); |
| 1282 | exit(1); |
| 1283 | #endif |
Willy Tarreau | 3770f23 | 2013-12-07 00:01:53 +0100 | [diff] [blame] | 1284 | ; |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1285 | } |
| 1286 | |
| 1287 | http_msg_ood: |
Willy Tarreau | 7552c03 | 2009-03-01 11:10:40 +0100 | [diff] [blame] | 1288 | /* out of valid data */ |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1289 | if (ret_state) |
| 1290 | *ret_state = state; |
| 1291 | if (ret_ptr) |
Willy Tarreau | a458b67 | 2012-03-05 11:17:50 +0100 | [diff] [blame] | 1292 | *ret_ptr = ptr - msg_start; |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1293 | return NULL; |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1294 | } |
| 1295 | |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1296 | /* |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1297 | * This function parses a request line between <ptr> and <end>, starting with |
| 1298 | * parser state <state>. Only states HTTP_MSG_RQMETH, HTTP_MSG_RQMETH_SP, |
| 1299 | * HTTP_MSG_RQURI, HTTP_MSG_RQURI_SP and HTTP_MSG_RQVER are handled. Others |
| 1300 | * will give undefined results. |
| 1301 | * Note that it is upon the caller's responsibility to ensure that ptr < end, |
| 1302 | * and that msg->sol points to the beginning of the request. |
| 1303 | * If a complete line is found (which implies that at least one CR or LF is |
| 1304 | * found before <end>, the updated <ptr> is returned, otherwise NULL is |
| 1305 | * returned indicating an incomplete line (which does not mean that parts have |
| 1306 | * not been updated). In the incomplete case, if <ret_ptr> or <ret_state> are |
| 1307 | * non-NULL, they are fed with the new <ptr> and <state> values to be passed |
| 1308 | * upon next call. |
| 1309 | * |
Willy Tarreau | 9cdde23 | 2007-05-02 20:58:19 +0200 | [diff] [blame] | 1310 | * This function was intentionally designed to be called from |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1311 | * http_msg_analyzer() with the lowest overhead. It should integrate perfectly |
| 1312 | * within its state machine and use the same macros, hence the need for same |
Willy Tarreau | 9cdde23 | 2007-05-02 20:58:19 +0200 | [diff] [blame] | 1313 | * labels and variable names. Note that msg->sol is left unchanged. |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1314 | */ |
Willy Tarreau | 69d8c5d | 2012-05-08 09:44:41 +0200 | [diff] [blame] | 1315 | const char *http_parse_reqline(struct http_msg *msg, |
Willy Tarreau | 3770f23 | 2013-12-07 00:01:53 +0100 | [diff] [blame] | 1316 | enum ht_state state, const char *ptr, const char *end, |
| 1317 | unsigned int *ret_ptr, enum ht_state *ret_state) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1318 | { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 1319 | const char *msg_start = msg->chn->buf->p; |
Willy Tarreau | 62f791e | 2012-03-09 11:32:30 +0100 | [diff] [blame] | 1320 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1321 | switch (state) { |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1322 | case HTTP_MSG_RQMETH: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1323 | http_msg_rqmeth: |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1324 | if (likely(HTTP_IS_TOKEN(*ptr))) |
| 1325 | EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth, HTTP_MSG_RQMETH); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1326 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1327 | if (likely(HTTP_IS_SPHT(*ptr))) { |
Willy Tarreau | ea1175a | 2012-03-05 15:52:30 +0100 | [diff] [blame] | 1328 | msg->sl.rq.m_l = ptr - msg_start; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1329 | EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth_sp, HTTP_MSG_RQMETH_SP); |
| 1330 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1331 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1332 | if (likely(HTTP_IS_CRLF(*ptr))) { |
| 1333 | /* HTTP 0.9 request */ |
Willy Tarreau | ea1175a | 2012-03-05 15:52:30 +0100 | [diff] [blame] | 1334 | msg->sl.rq.m_l = ptr - msg_start; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1335 | http_msg_req09_uri: |
Willy Tarreau | ea1175a | 2012-03-05 15:52:30 +0100 | [diff] [blame] | 1336 | msg->sl.rq.u = ptr - msg_start; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1337 | http_msg_req09_uri_e: |
Willy Tarreau | ea1175a | 2012-03-05 15:52:30 +0100 | [diff] [blame] | 1338 | msg->sl.rq.u_l = ptr - msg_start - msg->sl.rq.u; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1339 | http_msg_req09_ver: |
Willy Tarreau | ea1175a | 2012-03-05 15:52:30 +0100 | [diff] [blame] | 1340 | msg->sl.rq.v = ptr - msg_start; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1341 | msg->sl.rq.v_l = 0; |
| 1342 | goto http_msg_rqline_eol; |
| 1343 | } |
Willy Tarreau | 7552c03 | 2009-03-01 11:10:40 +0100 | [diff] [blame] | 1344 | state = HTTP_MSG_ERROR; |
| 1345 | break; |
| 1346 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1347 | case HTTP_MSG_RQMETH_SP: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1348 | http_msg_rqmeth_sp: |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1349 | if (likely(!HTTP_IS_LWS(*ptr))) { |
Willy Tarreau | ea1175a | 2012-03-05 15:52:30 +0100 | [diff] [blame] | 1350 | msg->sl.rq.u = ptr - msg_start; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1351 | goto http_msg_rquri; |
| 1352 | } |
| 1353 | if (likely(HTTP_IS_SPHT(*ptr))) |
| 1354 | EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth_sp, HTTP_MSG_RQMETH_SP); |
| 1355 | /* so it's a CR/LF, meaning an HTTP 0.9 request */ |
| 1356 | goto http_msg_req09_uri; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1357 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1358 | case HTTP_MSG_RQURI: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1359 | http_msg_rquri: |
Willy Tarreau | 2e9506d | 2012-01-07 23:22:31 +0100 | [diff] [blame] | 1360 | if (likely((unsigned char)(*ptr - 33) <= 93)) /* 33 to 126 included */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1361 | EAT_AND_JUMP_OR_RETURN(http_msg_rquri, HTTP_MSG_RQURI); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1362 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1363 | if (likely(HTTP_IS_SPHT(*ptr))) { |
Willy Tarreau | ea1175a | 2012-03-05 15:52:30 +0100 | [diff] [blame] | 1364 | msg->sl.rq.u_l = ptr - msg_start - msg->sl.rq.u; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1365 | EAT_AND_JUMP_OR_RETURN(http_msg_rquri_sp, HTTP_MSG_RQURI_SP); |
| 1366 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1367 | |
Willy Tarreau | 2e9506d | 2012-01-07 23:22:31 +0100 | [diff] [blame] | 1368 | if (likely((unsigned char)*ptr >= 128)) { |
Willy Tarreau | 422246e | 2012-01-07 23:54:13 +0100 | [diff] [blame] | 1369 | /* non-ASCII chars are forbidden unless option |
| 1370 | * accept-invalid-http-request is enabled in the frontend. |
| 1371 | * In any case, we capture the faulty char. |
Willy Tarreau | 2e9506d | 2012-01-07 23:22:31 +0100 | [diff] [blame] | 1372 | */ |
Willy Tarreau | 422246e | 2012-01-07 23:54:13 +0100 | [diff] [blame] | 1373 | if (msg->err_pos < -1) |
| 1374 | goto invalid_char; |
| 1375 | if (msg->err_pos == -1) |
Willy Tarreau | 69d8c5d | 2012-05-08 09:44:41 +0200 | [diff] [blame] | 1376 | msg->err_pos = ptr - msg_start; |
Willy Tarreau | 2e9506d | 2012-01-07 23:22:31 +0100 | [diff] [blame] | 1377 | EAT_AND_JUMP_OR_RETURN(http_msg_rquri, HTTP_MSG_RQURI); |
| 1378 | } |
| 1379 | |
| 1380 | if (likely(HTTP_IS_CRLF(*ptr))) { |
| 1381 | /* so it's a CR/LF, meaning an HTTP 0.9 request */ |
| 1382 | goto http_msg_req09_uri_e; |
| 1383 | } |
| 1384 | |
| 1385 | /* OK forbidden chars, 0..31 or 127 */ |
Willy Tarreau | 422246e | 2012-01-07 23:54:13 +0100 | [diff] [blame] | 1386 | invalid_char: |
Willy Tarreau | 69d8c5d | 2012-05-08 09:44:41 +0200 | [diff] [blame] | 1387 | msg->err_pos = ptr - msg_start; |
Willy Tarreau | 2e9506d | 2012-01-07 23:22:31 +0100 | [diff] [blame] | 1388 | state = HTTP_MSG_ERROR; |
| 1389 | break; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1390 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1391 | case HTTP_MSG_RQURI_SP: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1392 | http_msg_rquri_sp: |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1393 | if (likely(!HTTP_IS_LWS(*ptr))) { |
Willy Tarreau | ea1175a | 2012-03-05 15:52:30 +0100 | [diff] [blame] | 1394 | msg->sl.rq.v = ptr - msg_start; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1395 | goto http_msg_rqver; |
| 1396 | } |
| 1397 | if (likely(HTTP_IS_SPHT(*ptr))) |
| 1398 | EAT_AND_JUMP_OR_RETURN(http_msg_rquri_sp, HTTP_MSG_RQURI_SP); |
| 1399 | /* so it's a CR/LF, meaning an HTTP 0.9 request */ |
| 1400 | goto http_msg_req09_ver; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1401 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1402 | case HTTP_MSG_RQVER: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1403 | http_msg_rqver: |
Willy Tarreau | 4b89ad4 | 2007-03-04 18:13:58 +0100 | [diff] [blame] | 1404 | if (likely(HTTP_IS_VER_TOKEN(*ptr))) |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1405 | EAT_AND_JUMP_OR_RETURN(http_msg_rqver, HTTP_MSG_RQVER); |
Willy Tarreau | 4b89ad4 | 2007-03-04 18:13:58 +0100 | [diff] [blame] | 1406 | |
| 1407 | if (likely(HTTP_IS_CRLF(*ptr))) { |
Willy Tarreau | ea1175a | 2012-03-05 15:52:30 +0100 | [diff] [blame] | 1408 | msg->sl.rq.v_l = ptr - msg_start - msg->sl.rq.v; |
Willy Tarreau | 4b89ad4 | 2007-03-04 18:13:58 +0100 | [diff] [blame] | 1409 | http_msg_rqline_eol: |
| 1410 | /* We have seen the end of line. Note that we do not |
| 1411 | * necessarily have the \n yet, but at least we know that we |
| 1412 | * have EITHER \r OR \n, otherwise the request would not be |
| 1413 | * complete. We can then record the request length and return |
| 1414 | * to the caller which will be able to register it. |
| 1415 | */ |
Willy Tarreau | 3a215be | 2012-03-09 21:39:51 +0100 | [diff] [blame] | 1416 | msg->sl.rq.l = ptr - msg_start - msg->sol; |
Willy Tarreau | 4b89ad4 | 2007-03-04 18:13:58 +0100 | [diff] [blame] | 1417 | return ptr; |
| 1418 | } |
| 1419 | |
| 1420 | /* neither an HTTP_VER token nor a CRLF */ |
Willy Tarreau | 7552c03 | 2009-03-01 11:10:40 +0100 | [diff] [blame] | 1421 | state = HTTP_MSG_ERROR; |
| 1422 | break; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1423 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1424 | default: |
Willy Tarreau | 3770f23 | 2013-12-07 00:01:53 +0100 | [diff] [blame] | 1425 | #ifdef DEBUG_FULL |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1426 | fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state); |
| 1427 | exit(1); |
| 1428 | #endif |
Willy Tarreau | 3770f23 | 2013-12-07 00:01:53 +0100 | [diff] [blame] | 1429 | ; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1430 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1431 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1432 | http_msg_ood: |
Willy Tarreau | 7552c03 | 2009-03-01 11:10:40 +0100 | [diff] [blame] | 1433 | /* out of valid data */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1434 | if (ret_state) |
| 1435 | *ret_state = state; |
| 1436 | if (ret_ptr) |
Willy Tarreau | a458b67 | 2012-03-05 11:17:50 +0100 | [diff] [blame] | 1437 | *ret_ptr = ptr - msg_start; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1438 | return NULL; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1439 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1440 | |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 1441 | /* |
| 1442 | * Returns the data from Authorization header. Function may be called more |
| 1443 | * than once so data is stored in txn->auth_data. When no header is found |
| 1444 | * or auth method is unknown auth_method is set to HTTP_AUTH_WRONG to avoid |
Thierry FOURNIER | 98d9695 | 2014-01-23 12:13:02 +0100 | [diff] [blame] | 1445 | * searching again for something we are unable to find anyway. However, if |
| 1446 | * the result if valid, the cache is not reused because we would risk to |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 1447 | * have the credentials overwritten by another stream in parallel. |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 1448 | */ |
| 1449 | |
Thierry FOURNIER | 9eec0a6 | 2014-01-22 18:38:02 +0100 | [diff] [blame] | 1450 | /* This bufffer is initialized in the file 'src/haproxy.c'. This length is |
| 1451 | * set according to global.tune.bufsize. |
| 1452 | */ |
Willy Tarreau | 7e2c647 | 2012-10-29 20:44:36 +0100 | [diff] [blame] | 1453 | char *get_http_auth_buff; |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 1454 | |
| 1455 | int |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 1456 | get_http_auth(struct stream *s) |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 1457 | { |
| 1458 | |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 1459 | struct http_txn *txn = s->txn; |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 1460 | struct chunk auth_method; |
| 1461 | struct hdr_ctx ctx; |
| 1462 | char *h, *p; |
| 1463 | int len; |
| 1464 | |
| 1465 | #ifdef DEBUG_AUTH |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 1466 | printf("Auth for stream %p: %d\n", s, txn->auth.method); |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 1467 | #endif |
| 1468 | |
| 1469 | if (txn->auth.method == HTTP_AUTH_WRONG) |
| 1470 | return 0; |
| 1471 | |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 1472 | txn->auth.method = HTTP_AUTH_WRONG; |
| 1473 | |
| 1474 | ctx.idx = 0; |
Willy Tarreau | 844a7e7 | 2010-01-31 21:46:18 +0100 | [diff] [blame] | 1475 | |
| 1476 | if (txn->flags & TX_USE_PX_CONN) { |
| 1477 | h = "Proxy-Authorization"; |
| 1478 | len = strlen(h); |
| 1479 | } else { |
| 1480 | h = "Authorization"; |
| 1481 | len = strlen(h); |
| 1482 | } |
| 1483 | |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 1484 | if (!http_find_header2(h, len, s->req.buf->p, &txn->hdr_idx, &ctx)) |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 1485 | return 0; |
| 1486 | |
| 1487 | h = ctx.line + ctx.val; |
| 1488 | |
| 1489 | p = memchr(h, ' ', ctx.vlen); |
| 1490 | if (!p || p == h) |
| 1491 | return 0; |
| 1492 | |
| 1493 | chunk_initlen(&auth_method, h, 0, p-h); |
| 1494 | chunk_initlen(&txn->auth.method_data, p+1, 0, ctx.vlen-(p-h)-1); |
| 1495 | |
| 1496 | if (!strncasecmp("Basic", auth_method.str, auth_method.len)) { |
| 1497 | |
| 1498 | len = base64dec(txn->auth.method_data.str, txn->auth.method_data.len, |
Willy Tarreau | 7e2c647 | 2012-10-29 20:44:36 +0100 | [diff] [blame] | 1499 | get_http_auth_buff, global.tune.bufsize - 1); |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 1500 | |
| 1501 | if (len < 0) |
| 1502 | return 0; |
| 1503 | |
| 1504 | |
| 1505 | get_http_auth_buff[len] = '\0'; |
| 1506 | |
| 1507 | p = strchr(get_http_auth_buff, ':'); |
| 1508 | |
| 1509 | if (!p) |
| 1510 | return 0; |
| 1511 | |
| 1512 | txn->auth.user = get_http_auth_buff; |
| 1513 | *p = '\0'; |
| 1514 | txn->auth.pass = p+1; |
| 1515 | |
| 1516 | txn->auth.method = HTTP_AUTH_BASIC; |
| 1517 | return 1; |
| 1518 | } |
| 1519 | |
| 1520 | return 0; |
| 1521 | } |
| 1522 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1523 | |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1524 | /* |
| 1525 | * This function parses an HTTP message, either a request or a response, |
Willy Tarreau | 8b1323e | 2012-03-09 14:46:19 +0100 | [diff] [blame] | 1526 | * depending on the initial msg->msg_state. The caller is responsible for |
| 1527 | * ensuring that the message does not wrap. The function can be preempted |
| 1528 | * everywhere when data are missing and recalled at the exact same location |
| 1529 | * with no information loss. The message may even be realigned between two |
| 1530 | * calls. The header index is re-initialized when switching from |
Willy Tarreau | 9cdde23 | 2007-05-02 20:58:19 +0200 | [diff] [blame] | 1531 | * MSG_R[PQ]BEFORE to MSG_RPVER|MSG_RQMETH. It modifies msg->sol among other |
Willy Tarreau | 2692736 | 2012-05-18 23:22:52 +0200 | [diff] [blame] | 1532 | * fields. Note that msg->sol will be initialized after completing the first |
| 1533 | * state, so that none of the msg pointers has to be initialized prior to the |
| 1534 | * first call. |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1535 | */ |
Willy Tarreau | a560c21 | 2012-03-09 13:50:57 +0100 | [diff] [blame] | 1536 | void http_msg_analyzer(struct http_msg *msg, struct hdr_idx *idx) |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1537 | { |
Willy Tarreau | 3770f23 | 2013-12-07 00:01:53 +0100 | [diff] [blame] | 1538 | enum ht_state state; /* updated only when leaving the FSM */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1539 | register char *ptr, *end; /* request pointers, to avoid dereferences */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1540 | struct buffer *buf; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1541 | |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1542 | state = msg->msg_state; |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 1543 | buf = msg->chn->buf; |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1544 | ptr = buf->p + msg->next; |
| 1545 | end = buf->p + buf->i; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1546 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1547 | if (unlikely(ptr >= end)) |
| 1548 | goto http_msg_ood; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1549 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1550 | switch (state) { |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1551 | /* |
| 1552 | * First, states that are specific to the response only. |
| 1553 | * We check them first so that request and headers are |
| 1554 | * closer to each other (accessed more often). |
| 1555 | */ |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1556 | case HTTP_MSG_RPBEFORE: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1557 | http_msg_rpbefore: |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1558 | if (likely(HTTP_IS_TOKEN(*ptr))) { |
Willy Tarreau | 15de77e | 2010-01-02 21:59:16 +0100 | [diff] [blame] | 1559 | /* we have a start of message, but we have to check |
| 1560 | * first if we need to remove some CRLF. We can only |
Willy Tarreau | 2e046c6 | 2012-03-01 16:08:30 +0100 | [diff] [blame] | 1561 | * do this when o=0. |
Willy Tarreau | 15de77e | 2010-01-02 21:59:16 +0100 | [diff] [blame] | 1562 | */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1563 | if (unlikely(ptr != buf->p)) { |
| 1564 | if (buf->o) |
Willy Tarreau | 15de77e | 2010-01-02 21:59:16 +0100 | [diff] [blame] | 1565 | goto http_msg_ood; |
Willy Tarreau | 1d3bcce | 2009-12-27 15:50:06 +0100 | [diff] [blame] | 1566 | /* Remove empty leading lines, as recommended by RFC2616. */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1567 | bi_fast_delete(buf, ptr - buf->p); |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1568 | } |
Willy Tarreau | 2692736 | 2012-05-18 23:22:52 +0200 | [diff] [blame] | 1569 | msg->sol = 0; |
Willy Tarreau | e92693a | 2012-09-24 21:13:39 +0200 | [diff] [blame] | 1570 | msg->sl.st.l = 0; /* used in debug mode */ |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1571 | hdr_idx_init(idx); |
| 1572 | state = HTTP_MSG_RPVER; |
| 1573 | goto http_msg_rpver; |
| 1574 | } |
| 1575 | |
| 1576 | if (unlikely(!HTTP_IS_CRLF(*ptr))) |
| 1577 | goto http_msg_invalid; |
| 1578 | |
| 1579 | if (unlikely(*ptr == '\n')) |
| 1580 | EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore, HTTP_MSG_RPBEFORE); |
| 1581 | EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore_cr, HTTP_MSG_RPBEFORE_CR); |
| 1582 | /* stop here */ |
| 1583 | |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1584 | case HTTP_MSG_RPBEFORE_CR: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1585 | http_msg_rpbefore_cr: |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1586 | EXPECT_LF_HERE(ptr, http_msg_invalid); |
| 1587 | EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore, HTTP_MSG_RPBEFORE); |
| 1588 | /* stop here */ |
| 1589 | |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1590 | case HTTP_MSG_RPVER: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1591 | http_msg_rpver: |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1592 | case HTTP_MSG_RPVER_SP: |
| 1593 | case HTTP_MSG_RPCODE: |
| 1594 | case HTTP_MSG_RPCODE_SP: |
| 1595 | case HTTP_MSG_RPREASON: |
Willy Tarreau | 69d8c5d | 2012-05-08 09:44:41 +0200 | [diff] [blame] | 1596 | ptr = (char *)http_parse_stsline(msg, |
Willy Tarreau | a458b67 | 2012-03-05 11:17:50 +0100 | [diff] [blame] | 1597 | state, ptr, end, |
| 1598 | &msg->next, &msg->msg_state); |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1599 | if (unlikely(!ptr)) |
| 1600 | return; |
| 1601 | |
| 1602 | /* we have a full response and we know that we have either a CR |
| 1603 | * or an LF at <ptr>. |
| 1604 | */ |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1605 | hdr_idx_set_start(idx, msg->sl.st.l, *ptr == '\r'); |
| 1606 | |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1607 | msg->sol = ptr - buf->p; |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1608 | if (likely(*ptr == '\r')) |
| 1609 | EAT_AND_JUMP_OR_RETURN(http_msg_rpline_end, HTTP_MSG_RPLINE_END); |
| 1610 | goto http_msg_rpline_end; |
| 1611 | |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1612 | case HTTP_MSG_RPLINE_END: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1613 | http_msg_rpline_end: |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1614 | /* msg->sol must point to the first of CR or LF. */ |
| 1615 | EXPECT_LF_HERE(ptr, http_msg_invalid); |
| 1616 | EAT_AND_JUMP_OR_RETURN(http_msg_hdr_first, HTTP_MSG_HDR_FIRST); |
| 1617 | /* stop here */ |
| 1618 | |
| 1619 | /* |
| 1620 | * Second, states that are specific to the request only |
| 1621 | */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1622 | case HTTP_MSG_RQBEFORE: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1623 | http_msg_rqbefore: |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1624 | if (likely(HTTP_IS_TOKEN(*ptr))) { |
Willy Tarreau | 15de77e | 2010-01-02 21:59:16 +0100 | [diff] [blame] | 1625 | /* we have a start of message, but we have to check |
| 1626 | * first if we need to remove some CRLF. We can only |
Willy Tarreau | 2e046c6 | 2012-03-01 16:08:30 +0100 | [diff] [blame] | 1627 | * do this when o=0. |
Willy Tarreau | 15de77e | 2010-01-02 21:59:16 +0100 | [diff] [blame] | 1628 | */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1629 | if (likely(ptr != buf->p)) { |
| 1630 | if (buf->o) |
Willy Tarreau | 15de77e | 2010-01-02 21:59:16 +0100 | [diff] [blame] | 1631 | goto http_msg_ood; |
Willy Tarreau | 1d3bcce | 2009-12-27 15:50:06 +0100 | [diff] [blame] | 1632 | /* Remove empty leading lines, as recommended by RFC2616. */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1633 | bi_fast_delete(buf, ptr - buf->p); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1634 | } |
Willy Tarreau | 2692736 | 2012-05-18 23:22:52 +0200 | [diff] [blame] | 1635 | msg->sol = 0; |
Willy Tarreau | e92693a | 2012-09-24 21:13:39 +0200 | [diff] [blame] | 1636 | msg->sl.rq.l = 0; /* used in debug mode */ |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1637 | state = HTTP_MSG_RQMETH; |
| 1638 | goto http_msg_rqmeth; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1639 | } |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1640 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1641 | if (unlikely(!HTTP_IS_CRLF(*ptr))) |
| 1642 | goto http_msg_invalid; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1643 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1644 | if (unlikely(*ptr == '\n')) |
| 1645 | EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore, HTTP_MSG_RQBEFORE); |
| 1646 | EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore_cr, HTTP_MSG_RQBEFORE_CR); |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1647 | /* stop here */ |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1648 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1649 | case HTTP_MSG_RQBEFORE_CR: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1650 | http_msg_rqbefore_cr: |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1651 | EXPECT_LF_HERE(ptr, http_msg_invalid); |
| 1652 | EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore, HTTP_MSG_RQBEFORE); |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1653 | /* stop here */ |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1654 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1655 | case HTTP_MSG_RQMETH: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1656 | http_msg_rqmeth: |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1657 | case HTTP_MSG_RQMETH_SP: |
| 1658 | case HTTP_MSG_RQURI: |
| 1659 | case HTTP_MSG_RQURI_SP: |
| 1660 | case HTTP_MSG_RQVER: |
Willy Tarreau | 69d8c5d | 2012-05-08 09:44:41 +0200 | [diff] [blame] | 1661 | ptr = (char *)http_parse_reqline(msg, |
Willy Tarreau | a458b67 | 2012-03-05 11:17:50 +0100 | [diff] [blame] | 1662 | state, ptr, end, |
| 1663 | &msg->next, &msg->msg_state); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1664 | if (unlikely(!ptr)) |
| 1665 | return; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1666 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1667 | /* we have a full request and we know that we have either a CR |
| 1668 | * or an LF at <ptr>. |
| 1669 | */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1670 | hdr_idx_set_start(idx, msg->sl.rq.l, *ptr == '\r'); |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1671 | |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1672 | msg->sol = ptr - buf->p; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1673 | if (likely(*ptr == '\r')) |
| 1674 | EAT_AND_JUMP_OR_RETURN(http_msg_rqline_end, HTTP_MSG_RQLINE_END); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1675 | goto http_msg_rqline_end; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1676 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1677 | case HTTP_MSG_RQLINE_END: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1678 | http_msg_rqline_end: |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1679 | /* check for HTTP/0.9 request : no version information available. |
| 1680 | * msg->sol must point to the first of CR or LF. |
| 1681 | */ |
| 1682 | if (unlikely(msg->sl.rq.v_l == 0)) |
| 1683 | goto http_msg_last_lf; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1684 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1685 | EXPECT_LF_HERE(ptr, http_msg_invalid); |
| 1686 | EAT_AND_JUMP_OR_RETURN(http_msg_hdr_first, HTTP_MSG_HDR_FIRST); |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1687 | /* stop here */ |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1688 | |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1689 | /* |
| 1690 | * Common states below |
| 1691 | */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1692 | case HTTP_MSG_HDR_FIRST: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1693 | http_msg_hdr_first: |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1694 | msg->sol = ptr - buf->p; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1695 | if (likely(!HTTP_IS_CRLF(*ptr))) { |
| 1696 | goto http_msg_hdr_name; |
| 1697 | } |
| 1698 | |
| 1699 | if (likely(*ptr == '\r')) |
| 1700 | EAT_AND_JUMP_OR_RETURN(http_msg_last_lf, HTTP_MSG_LAST_LF); |
| 1701 | goto http_msg_last_lf; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1702 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1703 | case HTTP_MSG_HDR_NAME: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1704 | http_msg_hdr_name: |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1705 | /* assumes msg->sol points to the first char */ |
| 1706 | if (likely(HTTP_IS_TOKEN(*ptr))) |
| 1707 | EAT_AND_JUMP_OR_RETURN(http_msg_hdr_name, HTTP_MSG_HDR_NAME); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1708 | |
Willy Tarreau | fa4a03c | 2012-03-09 21:28:54 +0100 | [diff] [blame] | 1709 | if (likely(*ptr == ':')) |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1710 | EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_sp, HTTP_MSG_HDR_L1_SP); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1711 | |
Willy Tarreau | 32a4ec0 | 2009-04-02 11:35:18 +0200 | [diff] [blame] | 1712 | if (likely(msg->err_pos < -1) || *ptr == '\n') |
| 1713 | goto http_msg_invalid; |
| 1714 | |
| 1715 | if (msg->err_pos == -1) /* capture error pointer */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1716 | msg->err_pos = ptr - buf->p; /* >= 0 now */ |
Willy Tarreau | 32a4ec0 | 2009-04-02 11:35:18 +0200 | [diff] [blame] | 1717 | |
| 1718 | /* and we still accept this non-token character */ |
| 1719 | EAT_AND_JUMP_OR_RETURN(http_msg_hdr_name, HTTP_MSG_HDR_NAME); |
Willy Tarreau | 230fd0b | 2006-12-17 12:05:00 +0100 | [diff] [blame] | 1720 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1721 | case HTTP_MSG_HDR_L1_SP: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1722 | http_msg_hdr_l1_sp: |
Willy Tarreau | fa4a03c | 2012-03-09 21:28:54 +0100 | [diff] [blame] | 1723 | /* assumes msg->sol points to the first char */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1724 | if (likely(HTTP_IS_SPHT(*ptr))) |
| 1725 | EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_sp, HTTP_MSG_HDR_L1_SP); |
Willy Tarreau | 230fd0b | 2006-12-17 12:05:00 +0100 | [diff] [blame] | 1726 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1727 | /* header value can be basically anything except CR/LF */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1728 | msg->sov = ptr - buf->p; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1729 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1730 | if (likely(!HTTP_IS_CRLF(*ptr))) { |
| 1731 | goto http_msg_hdr_val; |
| 1732 | } |
| 1733 | |
| 1734 | if (likely(*ptr == '\r')) |
| 1735 | EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_lf, HTTP_MSG_HDR_L1_LF); |
| 1736 | goto http_msg_hdr_l1_lf; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1737 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1738 | case HTTP_MSG_HDR_L1_LF: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1739 | http_msg_hdr_l1_lf: |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1740 | EXPECT_LF_HERE(ptr, http_msg_invalid); |
| 1741 | EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_lws, HTTP_MSG_HDR_L1_LWS); |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1742 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1743 | case HTTP_MSG_HDR_L1_LWS: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1744 | http_msg_hdr_l1_lws: |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1745 | if (likely(HTTP_IS_SPHT(*ptr))) { |
| 1746 | /* replace HT,CR,LF with spaces */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1747 | for (; buf->p + msg->sov < ptr; msg->sov++) |
| 1748 | buf->p[msg->sov] = ' '; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1749 | goto http_msg_hdr_l1_sp; |
| 1750 | } |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 1751 | /* we had a header consisting only in spaces ! */ |
Willy Tarreau | 12e48b3 | 2012-03-05 16:57:34 +0100 | [diff] [blame] | 1752 | msg->eol = msg->sov; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1753 | goto http_msg_complete_header; |
| 1754 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1755 | case HTTP_MSG_HDR_VAL: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1756 | http_msg_hdr_val: |
Willy Tarreau | fa4a03c | 2012-03-09 21:28:54 +0100 | [diff] [blame] | 1757 | /* assumes msg->sol points to the first char, and msg->sov |
| 1758 | * points to the first character of the value. |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1759 | */ |
| 1760 | if (likely(!HTTP_IS_CRLF(*ptr))) |
| 1761 | EAT_AND_JUMP_OR_RETURN(http_msg_hdr_val, HTTP_MSG_HDR_VAL); |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1762 | |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1763 | msg->eol = ptr - buf->p; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1764 | /* Note: we could also copy eol into ->eoh so that we have the |
| 1765 | * real header end in case it ends with lots of LWS, but is this |
| 1766 | * really needed ? |
| 1767 | */ |
| 1768 | if (likely(*ptr == '\r')) |
| 1769 | EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l2_lf, HTTP_MSG_HDR_L2_LF); |
| 1770 | goto http_msg_hdr_l2_lf; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1771 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1772 | case HTTP_MSG_HDR_L2_LF: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1773 | http_msg_hdr_l2_lf: |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1774 | EXPECT_LF_HERE(ptr, http_msg_invalid); |
| 1775 | EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l2_lws, HTTP_MSG_HDR_L2_LWS); |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1776 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1777 | case HTTP_MSG_HDR_L2_LWS: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1778 | http_msg_hdr_l2_lws: |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1779 | if (unlikely(HTTP_IS_SPHT(*ptr))) { |
| 1780 | /* LWS: replace HT,CR,LF with spaces */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1781 | for (; buf->p + msg->eol < ptr; msg->eol++) |
| 1782 | buf->p[msg->eol] = ' '; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1783 | goto http_msg_hdr_val; |
| 1784 | } |
| 1785 | http_msg_complete_header: |
| 1786 | /* |
| 1787 | * It was a new header, so the last one is finished. |
Willy Tarreau | fa4a03c | 2012-03-09 21:28:54 +0100 | [diff] [blame] | 1788 | * Assumes msg->sol points to the first char, msg->sov points |
| 1789 | * to the first character of the value and msg->eol to the |
| 1790 | * first CR or LF so we know how the line ends. We insert last |
| 1791 | * header into the index. |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1792 | */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1793 | if (unlikely(hdr_idx_add(msg->eol - msg->sol, buf->p[msg->eol] == '\r', |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1794 | idx, idx->tail) < 0)) |
| 1795 | goto http_msg_invalid; |
Willy Tarreau | 230fd0b | 2006-12-17 12:05:00 +0100 | [diff] [blame] | 1796 | |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1797 | msg->sol = ptr - buf->p; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1798 | if (likely(!HTTP_IS_CRLF(*ptr))) { |
| 1799 | goto http_msg_hdr_name; |
| 1800 | } |
| 1801 | |
| 1802 | if (likely(*ptr == '\r')) |
| 1803 | EAT_AND_JUMP_OR_RETURN(http_msg_last_lf, HTTP_MSG_LAST_LF); |
| 1804 | goto http_msg_last_lf; |
Willy Tarreau | 230fd0b | 2006-12-17 12:05:00 +0100 | [diff] [blame] | 1805 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1806 | case HTTP_MSG_LAST_LF: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1807 | http_msg_last_lf: |
Willy Tarreau | 0558a02 | 2014-03-13 15:48:45 +0100 | [diff] [blame] | 1808 | /* Assumes msg->sol points to the first of either CR or LF. |
| 1809 | * Sets ->sov and ->next to the total header length, ->eoh to |
| 1810 | * the last CRLF, and ->eol to the last CRLF length (1 or 2). |
| 1811 | */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1812 | EXPECT_LF_HERE(ptr, http_msg_invalid); |
| 1813 | ptr++; |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1814 | msg->sov = msg->next = ptr - buf->p; |
Willy Tarreau | 3a215be | 2012-03-09 21:39:51 +0100 | [diff] [blame] | 1815 | msg->eoh = msg->sol; |
| 1816 | msg->sol = 0; |
Willy Tarreau | 0558a02 | 2014-03-13 15:48:45 +0100 | [diff] [blame] | 1817 | msg->eol = msg->sov - msg->eoh; |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1818 | msg->msg_state = HTTP_MSG_BODY; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1819 | return; |
Willy Tarreau | b56928a | 2012-04-16 14:51:55 +0200 | [diff] [blame] | 1820 | |
| 1821 | case HTTP_MSG_ERROR: |
| 1822 | /* this may only happen if we call http_msg_analyser() twice with an error */ |
| 1823 | break; |
| 1824 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1825 | default: |
Willy Tarreau | 3770f23 | 2013-12-07 00:01:53 +0100 | [diff] [blame] | 1826 | #ifdef DEBUG_FULL |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1827 | fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state); |
| 1828 | exit(1); |
Willy Tarreau | 230fd0b | 2006-12-17 12:05:00 +0100 | [diff] [blame] | 1829 | #endif |
Willy Tarreau | 3770f23 | 2013-12-07 00:01:53 +0100 | [diff] [blame] | 1830 | ; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1831 | } |
| 1832 | http_msg_ood: |
| 1833 | /* out of data */ |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1834 | msg->msg_state = state; |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1835 | msg->next = ptr - buf->p; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1836 | return; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1837 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1838 | http_msg_invalid: |
| 1839 | /* invalid message */ |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1840 | msg->msg_state = HTTP_MSG_ERROR; |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1841 | msg->next = ptr - buf->p; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1842 | return; |
| 1843 | } |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 1844 | |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 1845 | /* convert an HTTP/0.9 request into an HTTP/1.0 request. Returns 1 if the |
| 1846 | * conversion succeeded, 0 in case of error. If the request was already 1.X, |
| 1847 | * nothing is done and 1 is returned. |
| 1848 | */ |
Willy Tarreau | 418bfcc | 2012-03-09 13:56:20 +0100 | [diff] [blame] | 1849 | static int http_upgrade_v09_to_v10(struct http_txn *txn) |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 1850 | { |
| 1851 | int delta; |
| 1852 | char *cur_end; |
Willy Tarreau | 418bfcc | 2012-03-09 13:56:20 +0100 | [diff] [blame] | 1853 | struct http_msg *msg = &txn->req; |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 1854 | |
| 1855 | if (msg->sl.rq.v_l != 0) |
| 1856 | return 1; |
| 1857 | |
Apollon Oikonomopoulos | 25a1522 | 2014-04-06 02:46:00 +0300 | [diff] [blame] | 1858 | /* RFC 1945 allows only GET for HTTP/0.9 requests */ |
| 1859 | if (txn->meth != HTTP_METH_GET) |
| 1860 | return 0; |
| 1861 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 1862 | cur_end = msg->chn->buf->p + msg->sl.rq.l; |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 1863 | delta = 0; |
| 1864 | |
| 1865 | if (msg->sl.rq.u_l == 0) { |
Apollon Oikonomopoulos | 25a1522 | 2014-04-06 02:46:00 +0300 | [diff] [blame] | 1866 | /* HTTP/0.9 requests *must* have a request URI, per RFC 1945 */ |
| 1867 | return 0; |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 1868 | } |
| 1869 | /* add HTTP version */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 1870 | delta = buffer_replace2(msg->chn->buf, cur_end, cur_end, " HTTP/1.0\r\n", 11); |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 1871 | http_msg_move_end(msg, delta); |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 1872 | cur_end += delta; |
Willy Tarreau | 69d8c5d | 2012-05-08 09:44:41 +0200 | [diff] [blame] | 1873 | cur_end = (char *)http_parse_reqline(msg, |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 1874 | HTTP_MSG_RQMETH, |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 1875 | msg->chn->buf->p, cur_end + 1, |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 1876 | NULL, NULL); |
| 1877 | if (unlikely(!cur_end)) |
| 1878 | return 0; |
| 1879 | |
| 1880 | /* we have a full HTTP/1.0 request now and we know that |
| 1881 | * we have either a CR or an LF at <ptr>. |
| 1882 | */ |
| 1883 | hdr_idx_set_start(&txn->hdr_idx, msg->sl.rq.l, *cur_end == '\r'); |
| 1884 | return 1; |
| 1885 | } |
| 1886 | |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 1887 | /* Parse the Connection: header of an HTTP request, looking for both "close" |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 1888 | * and "keep-alive" values. If we already know that some headers may safely |
| 1889 | * be removed, we remove them now. The <to_del> flags are used for that : |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 1890 | * - bit 0 means remove "close" headers (in HTTP/1.0 requests/responses) |
| 1891 | * - bit 1 means remove "keep-alive" headers (in HTTP/1.1 reqs/resp to 1.1). |
Willy Tarreau | 50fc777 | 2012-11-11 22:19:57 +0100 | [diff] [blame] | 1892 | * Presence of the "Upgrade" token is also checked and reported. |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 1893 | * The TX_HDR_CONN_* flags are adjusted in txn->flags depending on what was |
| 1894 | * found, and TX_CON_*_SET is adjusted depending on what is left so only |
| 1895 | * harmless combinations may be removed. Do not call that after changes have |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 1896 | * been processed. |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 1897 | */ |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 1898 | void http_parse_connection_header(struct http_txn *txn, struct http_msg *msg, int to_del) |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 1899 | { |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 1900 | struct hdr_ctx ctx; |
Willy Tarreau | 88d349d | 2010-01-25 12:15:43 +0100 | [diff] [blame] | 1901 | const char *hdr_val = "Connection"; |
| 1902 | int hdr_len = 10; |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 1903 | |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 1904 | if (txn->flags & TX_HDR_CONN_PRS) |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 1905 | return; |
| 1906 | |
Willy Tarreau | 88d349d | 2010-01-25 12:15:43 +0100 | [diff] [blame] | 1907 | if (unlikely(txn->flags & TX_USE_PX_CONN)) { |
| 1908 | hdr_val = "Proxy-Connection"; |
| 1909 | hdr_len = 16; |
| 1910 | } |
| 1911 | |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 1912 | ctx.idx = 0; |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 1913 | txn->flags &= ~(TX_CON_KAL_SET|TX_CON_CLO_SET); |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 1914 | while (http_find_header2(hdr_val, hdr_len, msg->chn->buf->p, &txn->hdr_idx, &ctx)) { |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 1915 | if (ctx.vlen >= 10 && word_match(ctx.line + ctx.val, ctx.vlen, "keep-alive", 10)) { |
| 1916 | txn->flags |= TX_HDR_CONN_KAL; |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 1917 | if (to_del & 2) |
| 1918 | http_remove_header2(msg, &txn->hdr_idx, &ctx); |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 1919 | else |
| 1920 | txn->flags |= TX_CON_KAL_SET; |
| 1921 | } |
| 1922 | else if (ctx.vlen >= 5 && word_match(ctx.line + ctx.val, ctx.vlen, "close", 5)) { |
| 1923 | txn->flags |= TX_HDR_CONN_CLO; |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 1924 | if (to_del & 1) |
| 1925 | http_remove_header2(msg, &txn->hdr_idx, &ctx); |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 1926 | else |
| 1927 | txn->flags |= TX_CON_CLO_SET; |
| 1928 | } |
Willy Tarreau | 50fc777 | 2012-11-11 22:19:57 +0100 | [diff] [blame] | 1929 | else if (ctx.vlen >= 7 && word_match(ctx.line + ctx.val, ctx.vlen, "upgrade", 7)) { |
| 1930 | txn->flags |= TX_HDR_CONN_UPG; |
| 1931 | } |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 1932 | } |
| 1933 | |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 1934 | txn->flags |= TX_HDR_CONN_PRS; |
| 1935 | return; |
| 1936 | } |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 1937 | |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 1938 | /* Apply desired changes on the Connection: header. Values may be removed and/or |
| 1939 | * added depending on the <wanted> flags, which are exclusively composed of |
| 1940 | * TX_CON_CLO_SET and TX_CON_KAL_SET, depending on what flags are desired. The |
| 1941 | * TX_CON_*_SET flags are adjusted in txn->flags depending on what is left. |
| 1942 | */ |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 1943 | void http_change_connection_header(struct http_txn *txn, struct http_msg *msg, int wanted) |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 1944 | { |
| 1945 | struct hdr_ctx ctx; |
Willy Tarreau | 88d349d | 2010-01-25 12:15:43 +0100 | [diff] [blame] | 1946 | const char *hdr_val = "Connection"; |
| 1947 | int hdr_len = 10; |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 1948 | |
| 1949 | ctx.idx = 0; |
| 1950 | |
Willy Tarreau | 88d349d | 2010-01-25 12:15:43 +0100 | [diff] [blame] | 1951 | |
| 1952 | if (unlikely(txn->flags & TX_USE_PX_CONN)) { |
| 1953 | hdr_val = "Proxy-Connection"; |
| 1954 | hdr_len = 16; |
| 1955 | } |
| 1956 | |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 1957 | txn->flags &= ~(TX_CON_CLO_SET | TX_CON_KAL_SET); |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 1958 | while (http_find_header2(hdr_val, hdr_len, msg->chn->buf->p, &txn->hdr_idx, &ctx)) { |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 1959 | if (ctx.vlen >= 10 && word_match(ctx.line + ctx.val, ctx.vlen, "keep-alive", 10)) { |
| 1960 | if (wanted & TX_CON_KAL_SET) |
| 1961 | txn->flags |= TX_CON_KAL_SET; |
| 1962 | else |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 1963 | http_remove_header2(msg, &txn->hdr_idx, &ctx); |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 1964 | } |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 1965 | else if (ctx.vlen >= 5 && word_match(ctx.line + ctx.val, ctx.vlen, "close", 5)) { |
| 1966 | if (wanted & TX_CON_CLO_SET) |
| 1967 | txn->flags |= TX_CON_CLO_SET; |
| 1968 | else |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 1969 | http_remove_header2(msg, &txn->hdr_idx, &ctx); |
Willy Tarreau | 0dfdf19 | 2010-01-05 11:33:11 +0100 | [diff] [blame] | 1970 | } |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 1971 | } |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 1972 | |
| 1973 | if (wanted == (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET))) |
| 1974 | return; |
| 1975 | |
| 1976 | if ((wanted & TX_CON_CLO_SET) && !(txn->flags & TX_CON_CLO_SET)) { |
| 1977 | txn->flags |= TX_CON_CLO_SET; |
Willy Tarreau | 88d349d | 2010-01-25 12:15:43 +0100 | [diff] [blame] | 1978 | hdr_val = "Connection: close"; |
| 1979 | hdr_len = 17; |
| 1980 | if (unlikely(txn->flags & TX_USE_PX_CONN)) { |
| 1981 | hdr_val = "Proxy-Connection: close"; |
| 1982 | hdr_len = 23; |
| 1983 | } |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 1984 | http_header_add_tail2(msg, &txn->hdr_idx, hdr_val, hdr_len); |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 1985 | } |
| 1986 | |
| 1987 | if ((wanted & TX_CON_KAL_SET) && !(txn->flags & TX_CON_KAL_SET)) { |
| 1988 | txn->flags |= TX_CON_KAL_SET; |
Willy Tarreau | 88d349d | 2010-01-25 12:15:43 +0100 | [diff] [blame] | 1989 | hdr_val = "Connection: keep-alive"; |
| 1990 | hdr_len = 22; |
| 1991 | if (unlikely(txn->flags & TX_USE_PX_CONN)) { |
| 1992 | hdr_val = "Proxy-Connection: keep-alive"; |
| 1993 | hdr_len = 28; |
| 1994 | } |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 1995 | http_header_add_tail2(msg, &txn->hdr_idx, hdr_val, hdr_len); |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 1996 | } |
| 1997 | return; |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 1998 | } |
| 1999 | |
Willy Tarreau | c24715e | 2014-04-17 15:21:20 +0200 | [diff] [blame] | 2000 | /* Parse the chunk size at msg->next. Once done, it adjusts ->next to point to |
| 2001 | * the first byte of data after the chunk size, so that we know we can forward |
| 2002 | * exactly msg->next bytes. msg->sol contains the exact number of bytes forming |
| 2003 | * the chunk size. That way it is always possible to differentiate between the |
| 2004 | * start of the body and the start of the data. |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 2005 | * Return >0 on success, 0 when some data is missing, <0 on error. |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2006 | * Note: this function is designed to parse wrapped CRLF at the end of the buffer. |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 2007 | */ |
Willy Tarreau | 24e6d97 | 2012-10-26 00:49:52 +0200 | [diff] [blame] | 2008 | static inline int http_parse_chunk_size(struct http_msg *msg) |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 2009 | { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2010 | const struct buffer *buf = msg->chn->buf; |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 2011 | const char *ptr = b_ptr(buf, msg->next); |
Willy Tarreau | 4baf44b | 2012-03-09 14:10:20 +0100 | [diff] [blame] | 2012 | const char *ptr_old = ptr; |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 2013 | const char *end = buf->data + buf->size; |
| 2014 | const char *stop = bi_end(buf); |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 2015 | unsigned int chunk = 0; |
| 2016 | |
| 2017 | /* The chunk size is in the following form, though we are only |
| 2018 | * interested in the size and CRLF : |
| 2019 | * 1*HEXDIGIT *WSP *[ ';' extensions ] CRLF |
| 2020 | */ |
| 2021 | while (1) { |
| 2022 | int c; |
Willy Tarreau | 363a5bb | 2012-03-02 20:14:45 +0100 | [diff] [blame] | 2023 | if (ptr == stop) |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 2024 | return 0; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2025 | c = hex2i(*ptr); |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 2026 | if (c < 0) /* not a hex digit anymore */ |
| 2027 | break; |
Willy Tarreau | 0161d62 | 2013-04-02 01:26:55 +0200 | [diff] [blame] | 2028 | if (unlikely(++ptr >= end)) |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 2029 | ptr = buf->data; |
Willy Tarreau | 431946e | 2012-02-24 19:20:12 +0100 | [diff] [blame] | 2030 | if (chunk & 0xF8000000) /* integer overflow will occur if result >= 2GB */ |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 2031 | goto error; |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 2032 | chunk = (chunk << 4) + c; |
| 2033 | } |
| 2034 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2035 | /* empty size not allowed */ |
Willy Tarreau | 0161d62 | 2013-04-02 01:26:55 +0200 | [diff] [blame] | 2036 | if (unlikely(ptr == ptr_old)) |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 2037 | goto error; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2038 | |
| 2039 | while (http_is_spht[(unsigned char)*ptr]) { |
| 2040 | if (++ptr >= end) |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 2041 | ptr = buf->data; |
Willy Tarreau | 0161d62 | 2013-04-02 01:26:55 +0200 | [diff] [blame] | 2042 | if (unlikely(ptr == stop)) |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 2043 | return 0; |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 2044 | } |
| 2045 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2046 | /* Up to there, we know that at least one byte is present at *ptr. Check |
| 2047 | * for the end of chunk size. |
| 2048 | */ |
| 2049 | while (1) { |
| 2050 | if (likely(HTTP_IS_CRLF(*ptr))) { |
| 2051 | /* we now have a CR or an LF at ptr */ |
| 2052 | if (likely(*ptr == '\r')) { |
| 2053 | if (++ptr >= end) |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 2054 | ptr = buf->data; |
Willy Tarreau | 363a5bb | 2012-03-02 20:14:45 +0100 | [diff] [blame] | 2055 | if (ptr == stop) |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2056 | return 0; |
| 2057 | } |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 2058 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2059 | if (*ptr != '\n') |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 2060 | goto error; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2061 | if (++ptr >= end) |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 2062 | ptr = buf->data; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2063 | /* done */ |
| 2064 | break; |
| 2065 | } |
| 2066 | else if (*ptr == ';') { |
| 2067 | /* chunk extension, ends at next CRLF */ |
| 2068 | if (++ptr >= end) |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 2069 | ptr = buf->data; |
Willy Tarreau | 363a5bb | 2012-03-02 20:14:45 +0100 | [diff] [blame] | 2070 | if (ptr == stop) |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 2071 | return 0; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2072 | |
| 2073 | while (!HTTP_IS_CRLF(*ptr)) { |
| 2074 | if (++ptr >= end) |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 2075 | ptr = buf->data; |
Willy Tarreau | 363a5bb | 2012-03-02 20:14:45 +0100 | [diff] [blame] | 2076 | if (ptr == stop) |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2077 | return 0; |
| 2078 | } |
| 2079 | /* we have a CRLF now, loop above */ |
| 2080 | continue; |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 2081 | } |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2082 | else |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 2083 | goto error; |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 2084 | } |
| 2085 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2086 | /* OK we found our CRLF and now <ptr> points to the next byte, |
Willy Tarreau | c24715e | 2014-04-17 15:21:20 +0200 | [diff] [blame] | 2087 | * which may or may not be present. We save that into ->next, |
| 2088 | * and the number of bytes parsed into msg->sol. |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 2089 | */ |
Willy Tarreau | c24715e | 2014-04-17 15:21:20 +0200 | [diff] [blame] | 2090 | msg->sol = ptr - ptr_old; |
Willy Tarreau | 0161d62 | 2013-04-02 01:26:55 +0200 | [diff] [blame] | 2091 | if (unlikely(ptr < ptr_old)) |
Willy Tarreau | c24715e | 2014-04-17 15:21:20 +0200 | [diff] [blame] | 2092 | msg->sol += buf->size; |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 2093 | msg->next = buffer_count(buf, buf->p, ptr); |
Willy Tarreau | 124d991 | 2011-03-01 20:30:48 +0100 | [diff] [blame] | 2094 | msg->chunk_len = chunk; |
| 2095 | msg->body_len += chunk; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2096 | msg->msg_state = chunk ? HTTP_MSG_DATA : HTTP_MSG_TRAILERS; |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 2097 | return 1; |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 2098 | error: |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 2099 | msg->err_pos = buffer_count(buf, buf->p, ptr); |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 2100 | return -1; |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 2101 | } |
| 2102 | |
Willy Tarreau | 4baf44b | 2012-03-09 14:10:20 +0100 | [diff] [blame] | 2103 | /* This function skips trailers in the buffer associated with HTTP |
Willy Tarreau | a458b67 | 2012-03-05 11:17:50 +0100 | [diff] [blame] | 2104 | * message <msg>. The first visited position is msg->next. If the end of |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2105 | * the trailers is found, it is automatically scheduled to be forwarded, |
| 2106 | * msg->msg_state switches to HTTP_MSG_DONE, and the function returns >0. |
| 2107 | * If not enough data are available, the function does not change anything |
Willy Tarreau | c24715e | 2014-04-17 15:21:20 +0200 | [diff] [blame] | 2108 | * except maybe msg->next if it could parse some lines, and returns zero. |
| 2109 | * If a parse error is encountered, the function returns < 0 and does not |
| 2110 | * change anything except maybe msg->next. Note that the message must |
| 2111 | * already be in HTTP_MSG_TRAILERS state before calling this function, |
Willy Tarreau | 638cd02 | 2010-01-03 07:42:04 +0100 | [diff] [blame] | 2112 | * which implies that all non-trailers data have already been scheduled for |
Willy Tarreau | c24715e | 2014-04-17 15:21:20 +0200 | [diff] [blame] | 2113 | * forwarding, and that msg->next exactly matches the length of trailers |
| 2114 | * already parsed and not forwarded. It is also important to note that this |
| 2115 | * function is designed to be able to parse wrapped headers at end of buffer. |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2116 | */ |
Willy Tarreau | 24e6d97 | 2012-10-26 00:49:52 +0200 | [diff] [blame] | 2117 | static int http_forward_trailers(struct http_msg *msg) |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2118 | { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2119 | const struct buffer *buf = msg->chn->buf; |
Willy Tarreau | 4baf44b | 2012-03-09 14:10:20 +0100 | [diff] [blame] | 2120 | |
Willy Tarreau | a458b67 | 2012-03-05 11:17:50 +0100 | [diff] [blame] | 2121 | /* we have msg->next which points to next line. Look for CRLF. */ |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2122 | while (1) { |
Willy Tarreau | 4baf44b | 2012-03-09 14:10:20 +0100 | [diff] [blame] | 2123 | const char *p1 = NULL, *p2 = NULL; |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 2124 | const char *ptr = b_ptr(buf, msg->next); |
| 2125 | const char *stop = bi_end(buf); |
Willy Tarreau | 638cd02 | 2010-01-03 07:42:04 +0100 | [diff] [blame] | 2126 | int bytes; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2127 | |
| 2128 | /* scan current line and stop at LF or CRLF */ |
| 2129 | while (1) { |
Willy Tarreau | 363a5bb | 2012-03-02 20:14:45 +0100 | [diff] [blame] | 2130 | if (ptr == stop) |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2131 | return 0; |
| 2132 | |
| 2133 | if (*ptr == '\n') { |
| 2134 | if (!p1) |
| 2135 | p1 = ptr; |
| 2136 | p2 = ptr; |
| 2137 | break; |
| 2138 | } |
| 2139 | |
| 2140 | if (*ptr == '\r') { |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 2141 | if (p1) { |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 2142 | msg->err_pos = buffer_count(buf, buf->p, ptr); |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2143 | return -1; |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 2144 | } |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2145 | p1 = ptr; |
| 2146 | } |
| 2147 | |
| 2148 | ptr++; |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 2149 | if (ptr >= buf->data + buf->size) |
| 2150 | ptr = buf->data; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2151 | } |
| 2152 | |
| 2153 | /* after LF; point to beginning of next line */ |
| 2154 | p2++; |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 2155 | if (p2 >= buf->data + buf->size) |
| 2156 | p2 = buf->data; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2157 | |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 2158 | bytes = p2 - b_ptr(buf, msg->next); |
Willy Tarreau | 638cd02 | 2010-01-03 07:42:04 +0100 | [diff] [blame] | 2159 | if (bytes < 0) |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 2160 | bytes += buf->size; |
Willy Tarreau | 638cd02 | 2010-01-03 07:42:04 +0100 | [diff] [blame] | 2161 | |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 2162 | if (p1 == b_ptr(buf, msg->next)) { |
Willy Tarreau | 638cd02 | 2010-01-03 07:42:04 +0100 | [diff] [blame] | 2163 | /* LF/CRLF at beginning of line => end of trailers at p2. |
| 2164 | * Everything was scheduled for forwarding, there's nothing |
| 2165 | * left from this message. |
Willy Tarreau | 5523b32 | 2009-12-29 12:05:52 +0100 | [diff] [blame] | 2166 | */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 2167 | msg->next = buffer_count(buf, buf->p, p2); |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2168 | msg->msg_state = HTTP_MSG_DONE; |
| 2169 | return 1; |
| 2170 | } |
| 2171 | /* OK, next line then */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 2172 | msg->next = buffer_count(buf, buf->p, p2); |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2173 | } |
| 2174 | } |
| 2175 | |
Willy Tarreau | c24715e | 2014-04-17 15:21:20 +0200 | [diff] [blame] | 2176 | /* This function may be called only in HTTP_MSG_CHUNK_CRLF. It reads the CRLF |
| 2177 | * or a possible LF alone at the end of a chunk. It automatically adjusts |
| 2178 | * msg->next in order to include this part into the next forwarding phase. |
Willy Tarreau | a458b67 | 2012-03-05 11:17:50 +0100 | [diff] [blame] | 2179 | * Note that the caller must ensure that ->p points to the first byte to parse. |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2180 | * It also sets msg_state to HTTP_MSG_CHUNK_SIZE and returns >0 on success. If |
| 2181 | * not enough data are available, the function does not change anything and |
| 2182 | * returns zero. If a parse error is encountered, the function returns < 0 and |
| 2183 | * does not change anything. Note: this function is designed to parse wrapped |
| 2184 | * CRLF at the end of the buffer. |
| 2185 | */ |
Willy Tarreau | 24e6d97 | 2012-10-26 00:49:52 +0200 | [diff] [blame] | 2186 | static inline int http_skip_chunk_crlf(struct http_msg *msg) |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2187 | { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2188 | const struct buffer *buf = msg->chn->buf; |
Willy Tarreau | 4baf44b | 2012-03-09 14:10:20 +0100 | [diff] [blame] | 2189 | const char *ptr; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2190 | int bytes; |
| 2191 | |
| 2192 | /* NB: we'll check data availabilty at the end. It's not a |
| 2193 | * problem because whatever we match first will be checked |
| 2194 | * against the correct length. |
| 2195 | */ |
| 2196 | bytes = 1; |
Willy Tarreau | 0669d7d | 2014-04-17 11:40:10 +0200 | [diff] [blame] | 2197 | ptr = b_ptr(buf, msg->next); |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2198 | if (*ptr == '\r') { |
| 2199 | bytes++; |
| 2200 | ptr++; |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 2201 | if (ptr >= buf->data + buf->size) |
| 2202 | ptr = buf->data; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2203 | } |
| 2204 | |
Willy Tarreau | 0669d7d | 2014-04-17 11:40:10 +0200 | [diff] [blame] | 2205 | if (msg->next + bytes > buf->i) |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2206 | return 0; |
| 2207 | |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 2208 | if (*ptr != '\n') { |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 2209 | msg->err_pos = buffer_count(buf, buf->p, ptr); |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2210 | return -1; |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 2211 | } |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2212 | |
| 2213 | ptr++; |
Willy Tarreau | 0161d62 | 2013-04-02 01:26:55 +0200 | [diff] [blame] | 2214 | if (unlikely(ptr >= buf->data + buf->size)) |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 2215 | ptr = buf->data; |
Willy Tarreau | c24715e | 2014-04-17 15:21:20 +0200 | [diff] [blame] | 2216 | /* Advance ->next to allow the CRLF to be forwarded */ |
Willy Tarreau | 0669d7d | 2014-04-17 11:40:10 +0200 | [diff] [blame] | 2217 | msg->next += bytes; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2218 | msg->msg_state = HTTP_MSG_CHUNK_SIZE; |
| 2219 | return 1; |
| 2220 | } |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 2221 | |
Willy Tarreau | 0e9b1b4 | 2014-03-19 12:07:52 +0100 | [diff] [blame] | 2222 | /* Parses a qvalue and returns it multipled by 1000, from 0 to 1000. If the |
| 2223 | * value is larger than 1000, it is bound to 1000. The parser consumes up to |
| 2224 | * 1 digit, one dot and 3 digits and stops on the first invalid character. |
| 2225 | * Unparsable qvalues return 1000 as "q=1.000". |
| 2226 | */ |
Thierry FOURNIER | ad90351 | 2014-04-11 17:51:01 +0200 | [diff] [blame] | 2227 | int parse_qvalue(const char *qvalue, const char **end) |
Willy Tarreau | 0e9b1b4 | 2014-03-19 12:07:52 +0100 | [diff] [blame] | 2228 | { |
| 2229 | int q = 1000; |
| 2230 | |
Willy Tarreau | 506c69a | 2014-07-08 00:59:48 +0200 | [diff] [blame] | 2231 | if (!isdigit((unsigned char)*qvalue)) |
Willy Tarreau | 0e9b1b4 | 2014-03-19 12:07:52 +0100 | [diff] [blame] | 2232 | goto out; |
| 2233 | q = (*qvalue++ - '0') * 1000; |
| 2234 | |
| 2235 | if (*qvalue++ != '.') |
| 2236 | goto out; |
| 2237 | |
Willy Tarreau | 506c69a | 2014-07-08 00:59:48 +0200 | [diff] [blame] | 2238 | if (!isdigit((unsigned char)*qvalue)) |
Willy Tarreau | 0e9b1b4 | 2014-03-19 12:07:52 +0100 | [diff] [blame] | 2239 | goto out; |
| 2240 | q += (*qvalue++ - '0') * 100; |
| 2241 | |
Willy Tarreau | 506c69a | 2014-07-08 00:59:48 +0200 | [diff] [blame] | 2242 | if (!isdigit((unsigned char)*qvalue)) |
Willy Tarreau | 0e9b1b4 | 2014-03-19 12:07:52 +0100 | [diff] [blame] | 2243 | goto out; |
| 2244 | q += (*qvalue++ - '0') * 10; |
| 2245 | |
Willy Tarreau | 506c69a | 2014-07-08 00:59:48 +0200 | [diff] [blame] | 2246 | if (!isdigit((unsigned char)*qvalue)) |
Willy Tarreau | 0e9b1b4 | 2014-03-19 12:07:52 +0100 | [diff] [blame] | 2247 | goto out; |
| 2248 | q += (*qvalue++ - '0') * 1; |
| 2249 | out: |
| 2250 | if (q > 1000) |
| 2251 | q = 1000; |
Willy Tarreau | 38b3aa5 | 2014-04-22 23:32:05 +0200 | [diff] [blame] | 2252 | if (end) |
Thierry FOURNIER | ad90351 | 2014-04-11 17:51:01 +0200 | [diff] [blame] | 2253 | *end = qvalue; |
Willy Tarreau | 0e9b1b4 | 2014-03-19 12:07:52 +0100 | [diff] [blame] | 2254 | return q; |
| 2255 | } |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 2256 | |
| 2257 | /* |
| 2258 | * Selects a compression algorithm depending on the client request. |
Willy Tarreau | 05d8460 | 2012-10-26 02:11:25 +0200 | [diff] [blame] | 2259 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 2260 | int select_compression_request_header(struct stream *s, struct buffer *req) |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 2261 | { |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 2262 | struct http_txn *txn = s->txn; |
Willy Tarreau | 70737d1 | 2012-10-27 00:34:28 +0200 | [diff] [blame] | 2263 | struct http_msg *msg = &txn->req; |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 2264 | struct hdr_ctx ctx; |
| 2265 | struct comp_algo *comp_algo = NULL; |
Willy Tarreau | 3c7b97b | 2012-10-26 14:50:26 +0200 | [diff] [blame] | 2266 | struct comp_algo *comp_algo_back = NULL; |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 2267 | |
Finn Arne Gangstad | cbb9a4b | 2012-10-29 21:43:01 +0100 | [diff] [blame] | 2268 | /* Disable compression for older user agents announcing themselves as "Mozilla/4" |
| 2269 | * unless they are known good (MSIE 6 with XP SP2, or MSIE 7 and later). |
Willy Tarreau | 05d8460 | 2012-10-26 02:11:25 +0200 | [diff] [blame] | 2270 | * See http://zoompf.com/2012/02/lose-the-wait-http-compression for more details. |
| 2271 | */ |
| 2272 | ctx.idx = 0; |
| 2273 | if (http_find_header2("User-Agent", 10, req->p, &txn->hdr_idx, &ctx) && |
| 2274 | ctx.vlen >= 9 && |
Finn Arne Gangstad | cbb9a4b | 2012-10-29 21:43:01 +0100 | [diff] [blame] | 2275 | memcmp(ctx.line + ctx.val, "Mozilla/4", 9) == 0 && |
| 2276 | (ctx.vlen < 31 || |
| 2277 | memcmp(ctx.line + ctx.val + 25, "MSIE ", 5) != 0 || |
| 2278 | ctx.line[ctx.val + 30] < '6' || |
| 2279 | (ctx.line[ctx.val + 30] == '6' && |
| 2280 | (ctx.vlen < 54 || memcmp(ctx.line + 51, "SV1", 3) != 0)))) { |
| 2281 | s->comp_algo = NULL; |
| 2282 | return 0; |
Willy Tarreau | 05d8460 | 2012-10-26 02:11:25 +0200 | [diff] [blame] | 2283 | } |
| 2284 | |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 2285 | /* search for the algo in the backend in priority or the frontend */ |
Willy Tarreau | d0d8da9 | 2015-04-04 02:10:38 +0200 | [diff] [blame] | 2286 | if ((s->be->comp && (comp_algo_back = s->be->comp->algos)) || (strm_fe(s)->comp && (comp_algo_back = strm_fe(s)->comp->algos))) { |
Willy Tarreau | 0e9b1b4 | 2014-03-19 12:07:52 +0100 | [diff] [blame] | 2287 | int best_q = 0; |
| 2288 | |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 2289 | ctx.idx = 0; |
| 2290 | while (http_find_header2("Accept-Encoding", 15, req->p, &txn->hdr_idx, &ctx)) { |
Willy Tarreau | 0e9b1b4 | 2014-03-19 12:07:52 +0100 | [diff] [blame] | 2291 | const char *qval; |
| 2292 | int q; |
| 2293 | int toklen; |
| 2294 | |
| 2295 | /* try to isolate the token from the optional q-value */ |
| 2296 | toklen = 0; |
| 2297 | while (toklen < ctx.vlen && http_is_token[(unsigned char)*(ctx.line + ctx.val + toklen)]) |
| 2298 | toklen++; |
| 2299 | |
| 2300 | qval = ctx.line + ctx.val + toklen; |
| 2301 | while (1) { |
| 2302 | while (qval < ctx.line + ctx.val + ctx.vlen && http_is_lws[(unsigned char)*qval]) |
| 2303 | qval++; |
| 2304 | |
| 2305 | if (qval >= ctx.line + ctx.val + ctx.vlen || *qval != ';') { |
| 2306 | qval = NULL; |
| 2307 | break; |
| 2308 | } |
| 2309 | qval++; |
Willy Tarreau | 70737d1 | 2012-10-27 00:34:28 +0200 | [diff] [blame] | 2310 | |
Willy Tarreau | 0e9b1b4 | 2014-03-19 12:07:52 +0100 | [diff] [blame] | 2311 | while (qval < ctx.line + ctx.val + ctx.vlen && http_is_lws[(unsigned char)*qval]) |
| 2312 | qval++; |
Willy Tarreau | 70737d1 | 2012-10-27 00:34:28 +0200 | [diff] [blame] | 2313 | |
Willy Tarreau | 0e9b1b4 | 2014-03-19 12:07:52 +0100 | [diff] [blame] | 2314 | if (qval >= ctx.line + ctx.val + ctx.vlen) { |
| 2315 | qval = NULL; |
| 2316 | break; |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 2317 | } |
Willy Tarreau | 0e9b1b4 | 2014-03-19 12:07:52 +0100 | [diff] [blame] | 2318 | if (strncmp(qval, "q=", MIN(ctx.line + ctx.val + ctx.vlen - qval, 2)) == 0) |
| 2319 | break; |
| 2320 | |
| 2321 | while (qval < ctx.line + ctx.val + ctx.vlen && *qval != ';') |
| 2322 | qval++; |
| 2323 | } |
| 2324 | |
| 2325 | /* here we have qval pointing to the first "q=" attribute or NULL if not found */ |
Thierry FOURNIER | ad90351 | 2014-04-11 17:51:01 +0200 | [diff] [blame] | 2326 | q = qval ? parse_qvalue(qval + 2, NULL) : 1000; |
Willy Tarreau | 0e9b1b4 | 2014-03-19 12:07:52 +0100 | [diff] [blame] | 2327 | |
| 2328 | if (q <= best_q) |
| 2329 | continue; |
| 2330 | |
| 2331 | for (comp_algo = comp_algo_back; comp_algo; comp_algo = comp_algo->next) { |
| 2332 | if (*(ctx.line + ctx.val) == '*' || |
Willy Tarreau | 615105e | 2015-03-28 16:40:46 +0100 | [diff] [blame] | 2333 | word_match(ctx.line + ctx.val, toklen, comp_algo->ua_name, comp_algo->ua_name_len)) { |
Willy Tarreau | 0e9b1b4 | 2014-03-19 12:07:52 +0100 | [diff] [blame] | 2334 | s->comp_algo = comp_algo; |
| 2335 | best_q = q; |
| 2336 | break; |
| 2337 | } |
| 2338 | } |
| 2339 | } |
| 2340 | } |
| 2341 | |
| 2342 | /* remove all occurrences of the header when "compression offload" is set */ |
| 2343 | if (s->comp_algo) { |
Willy Tarreau | d0d8da9 | 2015-04-04 02:10:38 +0200 | [diff] [blame] | 2344 | if ((s->be->comp && s->be->comp->offload) || (strm_fe(s)->comp && strm_fe(s)->comp->offload)) { |
Willy Tarreau | 0e9b1b4 | 2014-03-19 12:07:52 +0100 | [diff] [blame] | 2345 | http_remove_header2(msg, &txn->hdr_idx, &ctx); |
| 2346 | ctx.idx = 0; |
| 2347 | while (http_find_header2("Accept-Encoding", 15, req->p, &txn->hdr_idx, &ctx)) { |
| 2348 | http_remove_header2(msg, &txn->hdr_idx, &ctx); |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 2349 | } |
| 2350 | } |
Willy Tarreau | 0e9b1b4 | 2014-03-19 12:07:52 +0100 | [diff] [blame] | 2351 | return 1; |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 2352 | } |
| 2353 | |
| 2354 | /* identity is implicit does not require headers */ |
Willy Tarreau | d0d8da9 | 2015-04-04 02:10:38 +0200 | [diff] [blame] | 2355 | if ((s->be->comp && (comp_algo_back = s->be->comp->algos)) || (strm_fe(s)->comp && (comp_algo_back = strm_fe(s)->comp->algos))) { |
Willy Tarreau | 3c7b97b | 2012-10-26 14:50:26 +0200 | [diff] [blame] | 2356 | for (comp_algo = comp_algo_back; comp_algo; comp_algo = comp_algo->next) { |
Willy Tarreau | 615105e | 2015-03-28 16:40:46 +0100 | [diff] [blame] | 2357 | if (comp_algo->cfg_name_len == 8 && memcmp(comp_algo->cfg_name, "identity", 8) == 0) { |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 2358 | s->comp_algo = comp_algo; |
| 2359 | return 1; |
| 2360 | } |
| 2361 | } |
| 2362 | } |
| 2363 | |
| 2364 | s->comp_algo = NULL; |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 2365 | return 0; |
| 2366 | } |
| 2367 | |
| 2368 | /* |
| 2369 | * Selects a comression algorithm depending of the server response. |
| 2370 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 2371 | int select_compression_response_header(struct stream *s, struct buffer *res) |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 2372 | { |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 2373 | struct http_txn *txn = s->txn; |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 2374 | struct http_msg *msg = &txn->rsp; |
| 2375 | struct hdr_ctx ctx; |
| 2376 | struct comp_type *comp_type; |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 2377 | |
| 2378 | /* no common compression algorithm was found in request header */ |
| 2379 | if (s->comp_algo == NULL) |
| 2380 | goto fail; |
| 2381 | |
| 2382 | /* HTTP < 1.1 should not be compressed */ |
Willy Tarreau | 7257550 | 2013-12-24 14:41:35 +0100 | [diff] [blame] | 2383 | if (!(msg->flags & HTTP_MSGF_VER_11) || !(txn->req.flags & HTTP_MSGF_VER_11)) |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 2384 | goto fail; |
| 2385 | |
Jesse Hathaway | 2468d4e | 2015-03-06 20:16:15 +0000 | [diff] [blame] | 2386 | /* compress 200,201,202,203 responses only */ |
| 2387 | if ((txn->status != 200) && |
| 2388 | (txn->status != 201) && |
| 2389 | (txn->status != 202) && |
| 2390 | (txn->status != 203)) |
William Lallemand | d300261 | 2012-11-26 14:34:47 +0100 | [diff] [blame] | 2391 | goto fail; |
| 2392 | |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 2393 | /* Content-Length is null */ |
| 2394 | if (!(msg->flags & HTTP_MSGF_TE_CHNK) && msg->body_len == 0) |
| 2395 | goto fail; |
| 2396 | |
| 2397 | /* content is already compressed */ |
Willy Tarreau | 0a80a8d | 2012-11-26 16:33:37 +0100 | [diff] [blame] | 2398 | ctx.idx = 0; |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 2399 | if (http_find_header2("Content-Encoding", 16, res->p, &txn->hdr_idx, &ctx)) |
| 2400 | goto fail; |
| 2401 | |
Willy Tarreau | 56e9ffa | 2013-01-05 16:20:35 +0100 | [diff] [blame] | 2402 | /* no compression when Cache-Control: no-transform is present in the message */ |
| 2403 | ctx.idx = 0; |
| 2404 | while (http_find_header2("Cache-Control", 13, res->p, &txn->hdr_idx, &ctx)) { |
| 2405 | if (word_match(ctx.line + ctx.val, ctx.vlen, "no-transform", 12)) |
| 2406 | goto fail; |
| 2407 | } |
| 2408 | |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 2409 | comp_type = NULL; |
| 2410 | |
Willy Tarreau | 0a80a8d | 2012-11-26 16:33:37 +0100 | [diff] [blame] | 2411 | /* we don't want to compress multipart content-types, nor content-types that are |
| 2412 | * not listed in the "compression type" directive if any. If no content-type was |
| 2413 | * found but configuration requires one, we don't compress either. Backend has |
| 2414 | * the priority. |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 2415 | */ |
Willy Tarreau | 0a80a8d | 2012-11-26 16:33:37 +0100 | [diff] [blame] | 2416 | ctx.idx = 0; |
| 2417 | if (http_find_header2("Content-Type", 12, res->p, &txn->hdr_idx, &ctx)) { |
| 2418 | if (ctx.vlen >= 9 && strncasecmp("multipart", ctx.line+ctx.val, 9) == 0) |
| 2419 | goto fail; |
| 2420 | |
| 2421 | if ((s->be->comp && (comp_type = s->be->comp->types)) || |
Willy Tarreau | d0d8da9 | 2015-04-04 02:10:38 +0200 | [diff] [blame] | 2422 | (strm_fe(s)->comp && (comp_type = strm_fe(s)->comp->types))) { |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 2423 | for (; comp_type; comp_type = comp_type->next) { |
Willy Tarreau | 0a80a8d | 2012-11-26 16:33:37 +0100 | [diff] [blame] | 2424 | if (ctx.vlen >= comp_type->name_len && |
| 2425 | strncasecmp(ctx.line+ctx.val, comp_type->name, comp_type->name_len) == 0) |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 2426 | /* this Content-Type should be compressed */ |
| 2427 | break; |
| 2428 | } |
Willy Tarreau | 0a80a8d | 2012-11-26 16:33:37 +0100 | [diff] [blame] | 2429 | /* this Content-Type should not be compressed */ |
| 2430 | if (comp_type == NULL) |
| 2431 | goto fail; |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 2432 | } |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 2433 | } |
Willy Tarreau | 0a80a8d | 2012-11-26 16:33:37 +0100 | [diff] [blame] | 2434 | else { /* no content-type header */ |
Willy Tarreau | d0d8da9 | 2015-04-04 02:10:38 +0200 | [diff] [blame] | 2435 | if ((s->be->comp && s->be->comp->types) || (strm_fe(s)->comp && strm_fe(s)->comp->types)) |
Willy Tarreau | 0a80a8d | 2012-11-26 16:33:37 +0100 | [diff] [blame] | 2436 | goto fail; /* a content-type was required */ |
William Lallemand | d300261 | 2012-11-26 14:34:47 +0100 | [diff] [blame] | 2437 | } |
| 2438 | |
William Lallemand | d85f917 | 2012-11-09 17:05:39 +0100 | [diff] [blame] | 2439 | /* limit compression rate */ |
| 2440 | if (global.comp_rate_lim > 0) |
| 2441 | if (read_freq_ctr(&global.comp_bps_in) > global.comp_rate_lim) |
| 2442 | goto fail; |
| 2443 | |
William Lallemand | 072a2bf | 2012-11-20 17:01:01 +0100 | [diff] [blame] | 2444 | /* limit cpu usage */ |
| 2445 | if (idle_pct < compress_min_idle) |
| 2446 | goto fail; |
| 2447 | |
William Lallemand | 4c49fae | 2012-11-07 15:00:23 +0100 | [diff] [blame] | 2448 | /* initialize compression */ |
William Lallemand | f374783 | 2012-11-09 12:33:10 +0100 | [diff] [blame] | 2449 | if (s->comp_algo->init(&s->comp_ctx, global.tune.comp_maxlevel) < 0) |
William Lallemand | 4c49fae | 2012-11-07 15:00:23 +0100 | [diff] [blame] | 2450 | goto fail; |
| 2451 | |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 2452 | s->flags |= SF_COMP_READY; |
William Lallemand | ec3e389 | 2012-11-12 17:02:18 +0100 | [diff] [blame] | 2453 | |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 2454 | /* remove Content-Length header */ |
Willy Tarreau | 0a80a8d | 2012-11-26 16:33:37 +0100 | [diff] [blame] | 2455 | ctx.idx = 0; |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 2456 | if ((msg->flags & HTTP_MSGF_CNT_LEN) && http_find_header2("Content-Length", 14, res->p, &txn->hdr_idx, &ctx)) |
| 2457 | http_remove_header2(msg, &txn->hdr_idx, &ctx); |
| 2458 | |
| 2459 | /* add Transfer-Encoding header */ |
| 2460 | if (!(msg->flags & HTTP_MSGF_TE_CHNK)) |
| 2461 | http_header_add_tail2(&txn->rsp, &txn->hdr_idx, "Transfer-Encoding: chunked", 26); |
| 2462 | |
| 2463 | /* |
| 2464 | * Add Content-Encoding header when it's not identity encoding. |
| 2465 | * RFC 2616 : Identity encoding: This content-coding is used only in the |
| 2466 | * Accept-Encoding header, and SHOULD NOT be used in the Content-Encoding |
| 2467 | * header. |
| 2468 | */ |
Willy Tarreau | 615105e | 2015-03-28 16:40:46 +0100 | [diff] [blame] | 2469 | if (s->comp_algo->cfg_name_len != 8 || memcmp(s->comp_algo->cfg_name, "identity", 8) != 0) { |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 2470 | trash.len = 18; |
| 2471 | memcpy(trash.str, "Content-Encoding: ", trash.len); |
Willy Tarreau | 615105e | 2015-03-28 16:40:46 +0100 | [diff] [blame] | 2472 | memcpy(trash.str + trash.len, s->comp_algo->ua_name, s->comp_algo->ua_name_len); |
| 2473 | trash.len += s->comp_algo->ua_name_len; |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 2474 | trash.str[trash.len] = '\0'; |
| 2475 | http_header_add_tail2(&txn->rsp, &txn->hdr_idx, trash.str, trash.len); |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 2476 | } |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 2477 | return 1; |
| 2478 | |
| 2479 | fail: |
Willy Tarreau | b97b619 | 2012-11-19 14:55:02 +0100 | [diff] [blame] | 2480 | s->comp_algo = NULL; |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 2481 | return 0; |
| 2482 | } |
| 2483 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 2484 | void http_adjust_conn_mode(struct stream *s, struct http_txn *txn, struct http_msg *msg) |
Willy Tarreau | 4e21ff9 | 2014-09-30 18:44:22 +0200 | [diff] [blame] | 2485 | { |
Willy Tarreau | d0d8da9 | 2015-04-04 02:10:38 +0200 | [diff] [blame] | 2486 | struct proxy *fe = strm_fe(s); |
Willy Tarreau | 4e21ff9 | 2014-09-30 18:44:22 +0200 | [diff] [blame] | 2487 | int tmp = TX_CON_WANT_KAL; |
| 2488 | |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 2489 | if (!((fe->options2|s->be->options2) & PR_O2_FAKE_KA)) { |
| 2490 | if ((fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_TUN || |
Willy Tarreau | 4e21ff9 | 2014-09-30 18:44:22 +0200 | [diff] [blame] | 2491 | (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_TUN) |
| 2492 | tmp = TX_CON_WANT_TUN; |
| 2493 | |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 2494 | if ((fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL || |
Willy Tarreau | 4e21ff9 | 2014-09-30 18:44:22 +0200 | [diff] [blame] | 2495 | (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL) |
| 2496 | tmp = TX_CON_WANT_TUN; |
| 2497 | } |
| 2498 | |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 2499 | if ((fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_SCL || |
Willy Tarreau | 4e21ff9 | 2014-09-30 18:44:22 +0200 | [diff] [blame] | 2500 | (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_SCL) { |
| 2501 | /* option httpclose + server_close => forceclose */ |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 2502 | if ((fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL || |
Willy Tarreau | 4e21ff9 | 2014-09-30 18:44:22 +0200 | [diff] [blame] | 2503 | (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL) |
| 2504 | tmp = TX_CON_WANT_CLO; |
| 2505 | else |
| 2506 | tmp = TX_CON_WANT_SCL; |
| 2507 | } |
| 2508 | |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 2509 | if ((fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_FCL || |
Willy Tarreau | 4e21ff9 | 2014-09-30 18:44:22 +0200 | [diff] [blame] | 2510 | (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_FCL) |
| 2511 | tmp = TX_CON_WANT_CLO; |
| 2512 | |
| 2513 | if ((txn->flags & TX_CON_WANT_MSK) < tmp) |
| 2514 | txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | tmp; |
| 2515 | |
| 2516 | if (!(txn->flags & TX_HDR_CONN_PRS) && |
| 2517 | (txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) { |
| 2518 | /* parse the Connection header and possibly clean it */ |
| 2519 | int to_del = 0; |
| 2520 | if ((msg->flags & HTTP_MSGF_VER_11) || |
| 2521 | ((txn->flags & TX_CON_WANT_MSK) >= TX_CON_WANT_SCL && |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 2522 | !((fe->options2|s->be->options2) & PR_O2_FAKE_KA))) |
Willy Tarreau | 4e21ff9 | 2014-09-30 18:44:22 +0200 | [diff] [blame] | 2523 | to_del |= 2; /* remove "keep-alive" */ |
| 2524 | if (!(msg->flags & HTTP_MSGF_VER_11)) |
| 2525 | to_del |= 1; /* remove "close" */ |
| 2526 | http_parse_connection_header(txn, msg, to_del); |
| 2527 | } |
| 2528 | |
| 2529 | /* check if client or config asks for explicit close in KAL/SCL */ |
| 2530 | if (((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL || |
| 2531 | (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) && |
| 2532 | ((txn->flags & TX_HDR_CONN_CLO) || /* "connection: close" */ |
| 2533 | (!(msg->flags & HTTP_MSGF_VER_11) && !(txn->flags & TX_HDR_CONN_KAL)) || /* no "connection: k-a" in 1.0 */ |
| 2534 | !(msg->flags & HTTP_MSGF_XFER_LEN) || /* no length known => close */ |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 2535 | fe->state == PR_STSTOPPED)) /* frontend is stopping */ |
Willy Tarreau | 4e21ff9 | 2014-09-30 18:44:22 +0200 | [diff] [blame] | 2536 | txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO; |
| 2537 | } |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 2538 | |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 2539 | /* This stream analyser waits for a complete HTTP request. It returns 1 if the |
| 2540 | * processing can continue on next analysers, or zero if it either needs more |
| 2541 | * data or wants to immediately abort the request (eg: timeout, error, ...). It |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 2542 | * is tied to AN_REQ_WAIT_HTTP and may may remove itself from s->req.analysers |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 2543 | * when it has nothing left to do, and may remove any analyser when it wants to |
| 2544 | * abort. |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2545 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 2546 | int http_wait_for_request(struct stream *s, struct channel *req, int an_bit) |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2547 | { |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2548 | /* |
| 2549 | * We will parse the partial (or complete) lines. |
| 2550 | * We will check the request syntax, and also join multi-line |
| 2551 | * headers. An index of all the lines will be elaborated while |
| 2552 | * parsing. |
| 2553 | * |
| 2554 | * For the parsing, we use a 28 states FSM. |
| 2555 | * |
| 2556 | * Here is the information we currently have : |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2557 | * req->buf->p = beginning of request |
| 2558 | * req->buf->p + msg->eoh = end of processed headers / start of current one |
| 2559 | * req->buf->p + req->buf->i = end of input data |
Willy Tarreau | 2692736 | 2012-05-18 23:22:52 +0200 | [diff] [blame] | 2560 | * msg->eol = end of current header or line (LF or CRLF) |
| 2561 | * msg->next = first non-visited byte |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 2562 | * |
| 2563 | * At end of parsing, we may perform a capture of the error (if any), and |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 2564 | * we will set a few fields (txn->meth, sn->flags/SF_REDIRECTABLE). |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 2565 | * We also check for monitor-uri, logging, HTTP/0.9 to 1.0 conversion, and |
| 2566 | * finally headers capture. |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2567 | */ |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 2568 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2569 | int cur_idx; |
Willy Tarreau | fb0afa7 | 2015-04-03 14:46:27 +0200 | [diff] [blame] | 2570 | struct session *sess = s->sess; |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 2571 | struct http_txn *txn = s->txn; |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2572 | struct http_msg *msg = &txn->req; |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 2573 | struct hdr_ctx ctx; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 2574 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 2575 | DPRINTF(stderr,"[%u] %s: stream=%p b=%p, exp(r,w)=%u,%u bf=%08x bh=%d analysers=%02x\n", |
Willy Tarreau | 6bf1736 | 2009-02-24 10:48:35 +0100 | [diff] [blame] | 2576 | now_ms, __FUNCTION__, |
| 2577 | s, |
| 2578 | req, |
| 2579 | req->rex, req->wex, |
| 2580 | req->flags, |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2581 | req->buf->i, |
Willy Tarreau | 6bf1736 | 2009-02-24 10:48:35 +0100 | [diff] [blame] | 2582 | req->analysers); |
| 2583 | |
Willy Tarreau | 52a0c60 | 2009-08-16 22:45:38 +0200 | [diff] [blame] | 2584 | /* we're speaking HTTP here, so let's speak HTTP to the client */ |
| 2585 | s->srv_error = http_return_srv_error; |
| 2586 | |
Willy Tarreau | 83e3af0 | 2009-12-28 17:39:57 +0100 | [diff] [blame] | 2587 | /* There's a protected area at the end of the buffer for rewriting |
| 2588 | * purposes. We don't want to start to parse the request if the |
| 2589 | * protected area is affected, because we may have to move processed |
| 2590 | * data later, which is much more complicated. |
| 2591 | */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2592 | if (buffer_not_empty(req->buf) && msg->msg_state < HTTP_MSG_ERROR) { |
Willy Tarreau | 379357a | 2013-06-08 12:55:46 +0200 | [diff] [blame] | 2593 | if (txn->flags & TX_NOT_FIRST) { |
Willy Tarreau | ba0902e | 2015-01-13 14:39:16 +0100 | [diff] [blame] | 2594 | if (unlikely(!channel_is_rewritable(req))) { |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 2595 | if (req->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_WRITE_ERROR|CF_WRITE_TIMEOUT)) |
Willy Tarreau | 6464841 | 2010-03-05 10:41:54 +0100 | [diff] [blame] | 2596 | goto failed_keep_alive; |
Willy Tarreau | 2ab6eb1 | 2010-01-02 22:04:45 +0100 | [diff] [blame] | 2597 | /* some data has still not left the buffer, wake us once that's done */ |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 2598 | channel_dont_connect(req); |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 2599 | req->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */ |
Willy Tarreau | d7ad9f5 | 2013-12-31 17:26:25 +0100 | [diff] [blame] | 2600 | req->flags |= CF_WAKE_WRITE; |
Willy Tarreau | 2ab6eb1 | 2010-01-02 22:04:45 +0100 | [diff] [blame] | 2601 | return 0; |
| 2602 | } |
Willy Tarreau | 379357a | 2013-06-08 12:55:46 +0200 | [diff] [blame] | 2603 | if (unlikely(bi_end(req->buf) < b_ptr(req->buf, msg->next) || |
| 2604 | bi_end(req->buf) > req->buf->data + req->buf->size - global.tune.maxrewrite)) |
| 2605 | buffer_slow_realign(req->buf); |
Willy Tarreau | 83e3af0 | 2009-12-28 17:39:57 +0100 | [diff] [blame] | 2606 | } |
| 2607 | |
Willy Tarreau | 065e833 | 2010-01-08 00:30:20 +0100 | [diff] [blame] | 2608 | /* Note that we have the same problem with the response ; we |
| 2609 | * may want to send a redirect, error or anything which requires |
| 2610 | * some spare space. So we'll ensure that we have at least |
| 2611 | * maxrewrite bytes available in the response buffer before |
| 2612 | * processing that one. This will only affect pipelined |
| 2613 | * keep-alive requests. |
| 2614 | */ |
| 2615 | if ((txn->flags & TX_NOT_FIRST) && |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 2616 | unlikely(!channel_is_rewritable(&s->res) || |
| 2617 | bi_end(s->res.buf) < b_ptr(s->res.buf, txn->rsp.next) || |
| 2618 | bi_end(s->res.buf) > s->res.buf->data + s->res.buf->size - global.tune.maxrewrite)) { |
| 2619 | if (s->res.buf->o) { |
| 2620 | if (s->res.flags & (CF_SHUTW|CF_SHUTW_NOW|CF_WRITE_ERROR|CF_WRITE_TIMEOUT)) |
Willy Tarreau | 6464841 | 2010-03-05 10:41:54 +0100 | [diff] [blame] | 2621 | goto failed_keep_alive; |
Willy Tarreau | 065e833 | 2010-01-08 00:30:20 +0100 | [diff] [blame] | 2622 | /* don't let a connection request be initiated */ |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 2623 | channel_dont_connect(req); |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 2624 | s->res.flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */ |
| 2625 | s->res.flags |= CF_WAKE_WRITE; |
| 2626 | s->res.analysers |= an_bit; /* wake us up once it changes */ |
Willy Tarreau | 065e833 | 2010-01-08 00:30:20 +0100 | [diff] [blame] | 2627 | return 0; |
| 2628 | } |
| 2629 | } |
| 2630 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2631 | if (likely(msg->next < req->buf->i)) /* some unparsed data are available */ |
Willy Tarreau | a560c21 | 2012-03-09 13:50:57 +0100 | [diff] [blame] | 2632 | http_msg_analyzer(msg, &txn->hdr_idx); |
Willy Tarreau | 83e3af0 | 2009-12-28 17:39:57 +0100 | [diff] [blame] | 2633 | } |
| 2634 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2635 | /* 1: we might have to print this header in debug mode */ |
| 2636 | if (unlikely((global.mode & MODE_DEBUG) && |
| 2637 | (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) && |
Willy Tarreau | 7d59e90 | 2014-10-21 19:36:09 +0200 | [diff] [blame] | 2638 | msg->msg_state >= HTTP_MSG_BODY)) { |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2639 | char *eol, *sol; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2640 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2641 | sol = req->buf->p; |
Willy Tarreau | e92693a | 2012-09-24 21:13:39 +0200 | [diff] [blame] | 2642 | /* this is a bit complex : in case of error on the request line, |
| 2643 | * we know that rq.l is still zero, so we display only the part |
| 2644 | * up to the end of the line (truncated by debug_hdr). |
| 2645 | */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2646 | eol = sol + (msg->sl.rq.l ? msg->sl.rq.l : req->buf->i); |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2647 | debug_hdr("clireq", s, sol, eol); |
Willy Tarreau | 45e73e3 | 2006-12-17 00:05:15 +0100 | [diff] [blame] | 2648 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2649 | sol += hdr_idx_first_pos(&txn->hdr_idx); |
| 2650 | cur_idx = hdr_idx_first_idx(&txn->hdr_idx); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2651 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2652 | while (cur_idx) { |
| 2653 | eol = sol + txn->hdr_idx.v[cur_idx].len; |
| 2654 | debug_hdr("clihdr", s, sol, eol); |
| 2655 | sol = eol + txn->hdr_idx.v[cur_idx].cr + 1; |
| 2656 | cur_idx = txn->hdr_idx.v[cur_idx].next; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2657 | } |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2658 | } |
| 2659 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2660 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2661 | /* |
| 2662 | * Now we quickly check if we have found a full valid request. |
| 2663 | * If not so, we check the FD and buffer states before leaving. |
| 2664 | * A full request is indicated by the fact that we have seen |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 2665 | * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid |
Willy Tarreau | d3c343f | 2010-01-16 10:26:19 +0100 | [diff] [blame] | 2666 | * requests are checked first. When waiting for a second request |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 2667 | * on a keep-alive stream, if we encounter and error, close, t/o, |
| 2668 | * we note the error in the stream flags but don't set any state. |
Willy Tarreau | d3c343f | 2010-01-16 10:26:19 +0100 | [diff] [blame] | 2669 | * Since the error will be noted there, it will not be counted by |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 2670 | * process_stream() as a frontend error. |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 2671 | * Last, we may increase some tracked counters' http request errors on |
| 2672 | * the cases that are deliberately the client's fault. For instance, |
| 2673 | * a timeout or connection reset is not counted as an error. However |
| 2674 | * a bad request is. |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2675 | */ |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2676 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 2677 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2678 | /* |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2679 | * First, let's catch bad requests. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2680 | */ |
Willy Tarreau | 3e1b6d1 | 2010-03-04 23:02:38 +0100 | [diff] [blame] | 2681 | if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) { |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 2682 | stream_inc_http_req_ctr(s); |
| 2683 | stream_inc_http_err_ctr(s); |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 2684 | proxy_inc_fe_req_ctr(sess->fe); |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2685 | goto return_bad_req; |
Willy Tarreau | 3e1b6d1 | 2010-03-04 23:02:38 +0100 | [diff] [blame] | 2686 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2687 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2688 | /* 1: Since we are in header mode, if there's no space |
| 2689 | * left for headers, we won't be able to free more |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 2690 | * later, so the stream will never terminate. We |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2691 | * must terminate it now. |
| 2692 | */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2693 | if (unlikely(buffer_full(req->buf, global.tune.maxrewrite))) { |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2694 | /* FIXME: check if URI is set and return Status |
| 2695 | * 414 Request URI too long instead. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2696 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 2697 | stream_inc_http_req_ctr(s); |
| 2698 | stream_inc_http_err_ctr(s); |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 2699 | proxy_inc_fe_req_ctr(sess->fe); |
Willy Tarreau | fec4d89 | 2011-09-02 20:04:57 +0200 | [diff] [blame] | 2700 | if (msg->err_pos < 0) |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2701 | msg->err_pos = req->buf->i; |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2702 | goto return_bad_req; |
| 2703 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2704 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2705 | /* 2: have we encountered a read error ? */ |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 2706 | else if (req->flags & CF_READ_ERROR) { |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 2707 | if (!(s->flags & SF_ERR_MASK)) |
| 2708 | s->flags |= SF_ERR_CLICL; |
Willy Tarreau | d3c343f | 2010-01-16 10:26:19 +0100 | [diff] [blame] | 2709 | |
Willy Tarreau | fcffa69 | 2010-01-10 14:21:19 +0100 | [diff] [blame] | 2710 | if (txn->flags & TX_WAIT_NEXT_RQ) |
Willy Tarreau | b608feb | 2010-01-02 22:47:18 +0100 | [diff] [blame] | 2711 | goto failed_keep_alive; |
| 2712 | |
Willy Tarreau | 0f228a0 | 2015-05-01 15:37:53 +0200 | [diff] [blame] | 2713 | if (sess->fe->options & PR_O_IGNORE_PRB) |
| 2714 | goto failed_keep_alive; |
| 2715 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2716 | /* we cannot return any message on error */ |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 2717 | if (msg->err_pos >= 0) { |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 2718 | http_capture_bad_message(&sess->fe->invalid_req, s, msg, msg->msg_state, sess->fe); |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 2719 | stream_inc_http_err_ctr(s); |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 2720 | } |
| 2721 | |
Willy Tarreau | dc979f2 | 2012-12-04 10:39:01 +0100 | [diff] [blame] | 2722 | txn->status = 400; |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 2723 | stream_int_retnclose(&s->si[0], NULL); |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2724 | msg->msg_state = HTTP_MSG_ERROR; |
| 2725 | req->analysers = 0; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 2726 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 2727 | stream_inc_http_req_ctr(s); |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 2728 | proxy_inc_fe_req_ctr(sess->fe); |
| 2729 | sess->fe->fe_counters.failed_req++; |
Willy Tarreau | fb0afa7 | 2015-04-03 14:46:27 +0200 | [diff] [blame] | 2730 | if (sess->listener->counters) |
| 2731 | sess->listener->counters->failed_req++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 2732 | |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 2733 | if (!(s->flags & SF_FINST_MASK)) |
| 2734 | s->flags |= SF_FINST_R; |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2735 | return 0; |
| 2736 | } |
Willy Tarreau | f9839bd | 2008-08-27 23:57:16 +0200 | [diff] [blame] | 2737 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2738 | /* 3: has the read timeout expired ? */ |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 2739 | else if (req->flags & CF_READ_TIMEOUT || tick_is_expired(req->analyse_exp, now_ms)) { |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 2740 | if (!(s->flags & SF_ERR_MASK)) |
| 2741 | s->flags |= SF_ERR_CLITO; |
Willy Tarreau | d3c343f | 2010-01-16 10:26:19 +0100 | [diff] [blame] | 2742 | |
Willy Tarreau | fcffa69 | 2010-01-10 14:21:19 +0100 | [diff] [blame] | 2743 | if (txn->flags & TX_WAIT_NEXT_RQ) |
Willy Tarreau | b608feb | 2010-01-02 22:47:18 +0100 | [diff] [blame] | 2744 | goto failed_keep_alive; |
| 2745 | |
Willy Tarreau | 0f228a0 | 2015-05-01 15:37:53 +0200 | [diff] [blame] | 2746 | if (sess->fe->options & PR_O_IGNORE_PRB) |
| 2747 | goto failed_keep_alive; |
| 2748 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2749 | /* read timeout : give up with an error message. */ |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 2750 | if (msg->err_pos >= 0) { |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 2751 | http_capture_bad_message(&sess->fe->invalid_req, s, msg, msg->msg_state, sess->fe); |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 2752 | stream_inc_http_err_ctr(s); |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 2753 | } |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2754 | txn->status = 408; |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 2755 | stream_int_retnclose(&s->si[0], http_error_message(s, HTTP_ERR_408)); |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2756 | msg->msg_state = HTTP_MSG_ERROR; |
| 2757 | req->analysers = 0; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 2758 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 2759 | stream_inc_http_req_ctr(s); |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 2760 | proxy_inc_fe_req_ctr(sess->fe); |
| 2761 | sess->fe->fe_counters.failed_req++; |
Willy Tarreau | fb0afa7 | 2015-04-03 14:46:27 +0200 | [diff] [blame] | 2762 | if (sess->listener->counters) |
| 2763 | sess->listener->counters->failed_req++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 2764 | |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 2765 | if (!(s->flags & SF_FINST_MASK)) |
| 2766 | s->flags |= SF_FINST_R; |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2767 | return 0; |
| 2768 | } |
Willy Tarreau | c65a3ba | 2008-08-11 23:42:50 +0200 | [diff] [blame] | 2769 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2770 | /* 4: have we encountered a close ? */ |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 2771 | else if (req->flags & CF_SHUTR) { |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 2772 | if (!(s->flags & SF_ERR_MASK)) |
| 2773 | s->flags |= SF_ERR_CLICL; |
Willy Tarreau | d3c343f | 2010-01-16 10:26:19 +0100 | [diff] [blame] | 2774 | |
Willy Tarreau | fcffa69 | 2010-01-10 14:21:19 +0100 | [diff] [blame] | 2775 | if (txn->flags & TX_WAIT_NEXT_RQ) |
Willy Tarreau | b608feb | 2010-01-02 22:47:18 +0100 | [diff] [blame] | 2776 | goto failed_keep_alive; |
| 2777 | |
Willy Tarreau | 0f228a0 | 2015-05-01 15:37:53 +0200 | [diff] [blame] | 2778 | if (sess->fe->options & PR_O_IGNORE_PRB) |
| 2779 | goto failed_keep_alive; |
| 2780 | |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 2781 | if (msg->err_pos >= 0) |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 2782 | http_capture_bad_message(&sess->fe->invalid_req, s, msg, msg->msg_state, sess->fe); |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2783 | txn->status = 400; |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 2784 | stream_int_retnclose(&s->si[0], http_error_message(s, HTTP_ERR_400)); |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2785 | msg->msg_state = HTTP_MSG_ERROR; |
| 2786 | req->analysers = 0; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 2787 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 2788 | stream_inc_http_err_ctr(s); |
| 2789 | stream_inc_http_req_ctr(s); |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 2790 | proxy_inc_fe_req_ctr(sess->fe); |
| 2791 | sess->fe->fe_counters.failed_req++; |
Willy Tarreau | fb0afa7 | 2015-04-03 14:46:27 +0200 | [diff] [blame] | 2792 | if (sess->listener->counters) |
| 2793 | sess->listener->counters->failed_req++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 2794 | |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 2795 | if (!(s->flags & SF_FINST_MASK)) |
| 2796 | s->flags |= SF_FINST_R; |
Willy Tarreau | dafde43 | 2008-08-17 01:00:46 +0200 | [diff] [blame] | 2797 | return 0; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2798 | } |
| 2799 | |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 2800 | channel_dont_connect(req); |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 2801 | req->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */ |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 2802 | s->res.flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */ |
Willy Tarreau | 5e20552 | 2011-12-17 16:34:27 +0100 | [diff] [blame] | 2803 | #ifdef TCP_QUICKACK |
Willy Tarreau | 9ad7bd4 | 2015-04-03 19:19:59 +0200 | [diff] [blame] | 2804 | if (sess->listener->options & LI_O_NOQUICKACK && req->buf->i && |
| 2805 | objt_conn(sess->origin) && conn_ctrl_ready(__objt_conn(sess->origin))) { |
Willy Tarreau | 5e20552 | 2011-12-17 16:34:27 +0100 | [diff] [blame] | 2806 | /* We need more data, we have to re-enable quick-ack in case we |
| 2807 | * previously disabled it, otherwise we might cause the client |
| 2808 | * to delay next data. |
| 2809 | */ |
Willy Tarreau | 9ad7bd4 | 2015-04-03 19:19:59 +0200 | [diff] [blame] | 2810 | setsockopt(__objt_conn(sess->origin)->t.sock.fd, IPPROTO_TCP, TCP_QUICKACK, &one, sizeof(one)); |
Willy Tarreau | 5e20552 | 2011-12-17 16:34:27 +0100 | [diff] [blame] | 2811 | } |
| 2812 | #endif |
Willy Tarreau | 1b194fe | 2009-03-21 21:10:04 +0100 | [diff] [blame] | 2813 | |
Willy Tarreau | fcffa69 | 2010-01-10 14:21:19 +0100 | [diff] [blame] | 2814 | if ((msg->msg_state != HTTP_MSG_RQBEFORE) && (txn->flags & TX_WAIT_NEXT_RQ)) { |
| 2815 | /* If the client starts to talk, let's fall back to |
| 2816 | * request timeout processing. |
| 2817 | */ |
| 2818 | txn->flags &= ~TX_WAIT_NEXT_RQ; |
Willy Tarreau | b16a574 | 2010-01-10 14:46:16 +0100 | [diff] [blame] | 2819 | req->analyse_exp = TICK_ETERNITY; |
Willy Tarreau | fcffa69 | 2010-01-10 14:21:19 +0100 | [diff] [blame] | 2820 | } |
| 2821 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2822 | /* just set the request timeout once at the beginning of the request */ |
Willy Tarreau | b16a574 | 2010-01-10 14:46:16 +0100 | [diff] [blame] | 2823 | if (!tick_isset(req->analyse_exp)) { |
| 2824 | if ((msg->msg_state == HTTP_MSG_RQBEFORE) && |
| 2825 | (txn->flags & TX_WAIT_NEXT_RQ) && |
| 2826 | tick_isset(s->be->timeout.httpka)) |
| 2827 | req->analyse_exp = tick_add(now_ms, s->be->timeout.httpka); |
| 2828 | else |
| 2829 | req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.httpreq); |
| 2830 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2831 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2832 | /* we're not ready yet */ |
| 2833 | return 0; |
Willy Tarreau | b608feb | 2010-01-02 22:47:18 +0100 | [diff] [blame] | 2834 | |
| 2835 | failed_keep_alive: |
| 2836 | /* Here we process low-level errors for keep-alive requests. In |
| 2837 | * short, if the request is not the first one and it experiences |
| 2838 | * a timeout, read error or shutdown, we just silently close so |
| 2839 | * that the client can try again. |
| 2840 | */ |
| 2841 | txn->status = 0; |
| 2842 | msg->msg_state = HTTP_MSG_RQBEFORE; |
| 2843 | req->analysers = 0; |
| 2844 | s->logs.logwait = 0; |
Willy Tarreau | abcd514 | 2013-06-11 17:18:02 +0200 | [diff] [blame] | 2845 | s->logs.level = 0; |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 2846 | s->res.flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */ |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 2847 | stream_int_retnclose(&s->si[0], NULL); |
Willy Tarreau | b608feb | 2010-01-02 22:47:18 +0100 | [diff] [blame] | 2848 | return 0; |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2849 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2850 | |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 2851 | /* OK now we have a complete HTTP request with indexed headers. Let's |
| 2852 | * complete the request parsing by setting a few fields we will need |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2853 | * later. At this point, we have the last CRLF at req->buf->data + msg->eoh. |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 2854 | * If the request is in HTTP/0.9 form, the rule is still true, and eoh |
Willy Tarreau | a458b67 | 2012-03-05 11:17:50 +0100 | [diff] [blame] | 2855 | * points to the CRLF of the request line. msg->next points to the first |
Willy Tarreau | fa4a03c | 2012-03-09 21:28:54 +0100 | [diff] [blame] | 2856 | * byte after the last LF. msg->sov points to the first byte of data. |
| 2857 | * msg->eol cannot be trusted because it may have been left uninitialized |
| 2858 | * (for instance in the absence of headers). |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 2859 | */ |
Willy Tarreau | 9cdde23 | 2007-05-02 20:58:19 +0200 | [diff] [blame] | 2860 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 2861 | stream_inc_http_req_ctr(s); |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 2862 | proxy_inc_fe_req_ctr(sess->fe); /* one more valid request for this FE */ |
Willy Tarreau | d9b587f | 2010-02-26 10:05:55 +0100 | [diff] [blame] | 2863 | |
Willy Tarreau | b16a574 | 2010-01-10 14:46:16 +0100 | [diff] [blame] | 2864 | if (txn->flags & TX_WAIT_NEXT_RQ) { |
| 2865 | /* kill the pending keep-alive timeout */ |
| 2866 | txn->flags &= ~TX_WAIT_NEXT_RQ; |
| 2867 | req->analyse_exp = TICK_ETERNITY; |
| 2868 | } |
| 2869 | |
| 2870 | |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 2871 | /* Maybe we found in invalid header name while we were configured not |
| 2872 | * to block on that, so we have to capture it now. |
| 2873 | */ |
| 2874 | if (unlikely(msg->err_pos >= 0)) |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 2875 | http_capture_bad_message(&sess->fe->invalid_req, s, msg, msg->msg_state, sess->fe); |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 2876 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2877 | /* |
| 2878 | * 1: identify the method |
| 2879 | */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2880 | txn->meth = find_http_meth(req->buf->p, msg->sl.rq.m_l); |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2881 | |
| 2882 | /* we can make use of server redirect on GET and HEAD */ |
| 2883 | if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD) |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 2884 | s->flags |= SF_REDIRECTABLE; |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 2885 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2886 | /* |
| 2887 | * 2: check if the URI matches the monitor_uri. |
| 2888 | * We have to do this for every request which gets in, because |
| 2889 | * the monitor-uri is defined by the frontend. |
| 2890 | */ |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 2891 | if (unlikely((sess->fe->monitor_uri_len != 0) && |
| 2892 | (sess->fe->monitor_uri_len == msg->sl.rq.u_l) && |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2893 | !memcmp(req->buf->p + msg->sl.rq.u, |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 2894 | sess->fe->monitor_uri, |
| 2895 | sess->fe->monitor_uri_len))) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2896 | /* |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2897 | * We have found the monitor URI |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2898 | */ |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2899 | struct acl_cond *cond; |
Willy Tarreau | b80c230 | 2007-11-30 20:51:32 +0100 | [diff] [blame] | 2900 | |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 2901 | s->flags |= SF_MONITOR; |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 2902 | sess->fe->fe_counters.intercepted_req++; |
Willy Tarreau | b80c230 | 2007-11-30 20:51:32 +0100 | [diff] [blame] | 2903 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2904 | /* Check if we want to fail this monitor request or not */ |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 2905 | list_for_each_entry(cond, &sess->fe->mon_fail_cond, list) { |
Willy Tarreau | 192252e | 2015-04-04 01:47:55 +0200 | [diff] [blame] | 2906 | int ret = acl_exec_cond(cond, sess->fe, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL); |
Willy Tarreau | 1138281 | 2008-07-09 16:18:21 +0200 | [diff] [blame] | 2907 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2908 | ret = acl_pass(ret); |
| 2909 | if (cond->pol == ACL_COND_UNLESS) |
| 2910 | ret = !ret; |
Willy Tarreau | b80c230 | 2007-11-30 20:51:32 +0100 | [diff] [blame] | 2911 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2912 | if (ret) { |
| 2913 | /* we fail this request, let's return 503 service unavail */ |
| 2914 | txn->status = 503; |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 2915 | stream_int_retnclose(&s->si[0], http_error_message(s, HTTP_ERR_503)); |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 2916 | if (!(s->flags & SF_ERR_MASK)) |
| 2917 | s->flags |= SF_ERR_LOCAL; /* we don't want a real error here */ |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2918 | goto return_prx_cond; |
Willy Tarreau | b80c230 | 2007-11-30 20:51:32 +0100 | [diff] [blame] | 2919 | } |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2920 | } |
Willy Tarreau | a5555ec | 2008-11-30 19:02:32 +0100 | [diff] [blame] | 2921 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2922 | /* nothing to fail, let's reply normaly */ |
| 2923 | txn->status = 200; |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 2924 | stream_int_retnclose(&s->si[0], http_error_message(s, HTTP_ERR_200)); |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 2925 | if (!(s->flags & SF_ERR_MASK)) |
| 2926 | s->flags |= SF_ERR_LOCAL; /* we don't want a real error here */ |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2927 | goto return_prx_cond; |
| 2928 | } |
| 2929 | |
| 2930 | /* |
| 2931 | * 3: Maybe we have to copy the original REQURI for the logs ? |
| 2932 | * Note: we cannot log anymore if the request has been |
| 2933 | * classified as invalid. |
| 2934 | */ |
| 2935 | if (unlikely(s->logs.logwait & LW_REQ)) { |
| 2936 | /* we have a complete HTTP request that we must log */ |
| 2937 | if ((txn->uri = pool_alloc2(pool2_requri)) != NULL) { |
| 2938 | int urilen = msg->sl.rq.l; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2939 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2940 | if (urilen >= REQURI_LEN) |
| 2941 | urilen = REQURI_LEN - 1; |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2942 | memcpy(txn->uri, req->buf->p, urilen); |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2943 | txn->uri[urilen] = 0; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2944 | |
Willy Tarreau | d79a3b2 | 2012-12-28 09:40:16 +0100 | [diff] [blame] | 2945 | if (!(s->logs.logwait &= ~(LW_REQ|LW_INIT))) |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2946 | s->do_log(s); |
| 2947 | } else { |
| 2948 | Alert("HTTP logging : out of memory.\n"); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2949 | } |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2950 | } |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 2951 | |
Willy Tarreau | 91852eb | 2015-05-01 13:26:00 +0200 | [diff] [blame] | 2952 | /* RFC7230#2.6 has enforced the format of the HTTP version string to be |
| 2953 | * exactly one digit "." one digit. This check may be disabled using |
| 2954 | * option accept-invalid-http-request. |
| 2955 | */ |
| 2956 | if (!(sess->fe->options2 & PR_O2_REQBUG_OK)) { |
| 2957 | if (msg->sl.rq.v_l != 8) { |
| 2958 | msg->err_pos = msg->sl.rq.v; |
| 2959 | goto return_bad_req; |
| 2960 | } |
| 2961 | |
| 2962 | if (req->buf->p[msg->sl.rq.v + 4] != '/' || |
| 2963 | !isdigit((unsigned char)req->buf->p[msg->sl.rq.v + 5]) || |
| 2964 | req->buf->p[msg->sl.rq.v + 6] != '.' || |
| 2965 | !isdigit((unsigned char)req->buf->p[msg->sl.rq.v + 7])) { |
| 2966 | msg->err_pos = msg->sl.rq.v + 4; |
| 2967 | goto return_bad_req; |
| 2968 | } |
| 2969 | } |
Willy Tarreau | 1331766 | 2015-05-01 13:47:08 +0200 | [diff] [blame] | 2970 | else { |
| 2971 | /* 4. We may have to convert HTTP/0.9 requests to HTTP/1.0 */ |
| 2972 | if (unlikely(msg->sl.rq.v_l == 0) && !http_upgrade_v09_to_v10(txn)) |
| 2973 | goto return_bad_req; |
| 2974 | } |
Willy Tarreau | 91852eb | 2015-05-01 13:26:00 +0200 | [diff] [blame] | 2975 | |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 2976 | /* ... and check if the request is HTTP/1.1 or above */ |
| 2977 | if ((msg->sl.rq.v_l == 8) && |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2978 | ((req->buf->p[msg->sl.rq.v + 5] > '1') || |
| 2979 | ((req->buf->p[msg->sl.rq.v + 5] == '1') && |
| 2980 | (req->buf->p[msg->sl.rq.v + 7] >= '1')))) |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 2981 | msg->flags |= HTTP_MSGF_VER_11; |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 2982 | |
| 2983 | /* "connection" has not been parsed yet */ |
Willy Tarreau | 50fc777 | 2012-11-11 22:19:57 +0100 | [diff] [blame] | 2984 | txn->flags &= ~(TX_HDR_CONN_PRS | TX_HDR_CONN_CLO | TX_HDR_CONN_KAL | TX_HDR_CONN_UPG); |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 2985 | |
Willy Tarreau | 88d349d | 2010-01-25 12:15:43 +0100 | [diff] [blame] | 2986 | /* if the frontend has "option http-use-proxy-header", we'll check if |
| 2987 | * we have what looks like a proxied connection instead of a connection, |
| 2988 | * and in this case set the TX_USE_PX_CONN flag to use Proxy-connection. |
| 2989 | * Note that this is *not* RFC-compliant, however browsers and proxies |
| 2990 | * happen to do that despite being non-standard :-( |
| 2991 | * We consider that a request not beginning with either '/' or '*' is |
| 2992 | * a proxied connection, which covers both "scheme://location" and |
| 2993 | * CONNECT ip:port. |
| 2994 | */ |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 2995 | if ((sess->fe->options2 & PR_O2_USE_PXHDR) && |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2996 | req->buf->p[msg->sl.rq.u] != '/' && req->buf->p[msg->sl.rq.u] != '*') |
Willy Tarreau | 88d349d | 2010-01-25 12:15:43 +0100 | [diff] [blame] | 2997 | txn->flags |= TX_USE_PX_CONN; |
| 2998 | |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 2999 | /* transfer length unknown*/ |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 3000 | msg->flags &= ~HTTP_MSGF_XFER_LEN; |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 3001 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3002 | /* 5: we may need to capture headers */ |
Willy Tarreau | cb7dd01 | 2015-04-03 22:16:32 +0200 | [diff] [blame] | 3003 | if (unlikely((s->logs.logwait & LW_REQHDR) && s->req_cap)) |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 3004 | capture_headers(req->buf->p, &txn->hdr_idx, |
Willy Tarreau | cb7dd01 | 2015-04-03 22:16:32 +0200 | [diff] [blame] | 3005 | s->req_cap, sess->fe->req_cap); |
Willy Tarreau | 1138281 | 2008-07-09 16:18:21 +0200 | [diff] [blame] | 3006 | |
Willy Tarreau | 557f199 | 2015-05-01 10:05:17 +0200 | [diff] [blame] | 3007 | /* 6: determine the transfer-length according to RFC2616 #4.4, updated |
| 3008 | * by RFC7230#3.3.3 : |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 3009 | * |
Willy Tarreau | 557f199 | 2015-05-01 10:05:17 +0200 | [diff] [blame] | 3010 | * The length of a message body is determined by one of the following |
| 3011 | * (in order of precedence): |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 3012 | * |
Willy Tarreau | 557f199 | 2015-05-01 10:05:17 +0200 | [diff] [blame] | 3013 | * 1. Any response to a HEAD request and any response with a 1xx |
| 3014 | * (Informational), 204 (No Content), or 304 (Not Modified) status |
| 3015 | * code is always terminated by the first empty line after the |
| 3016 | * header fields, regardless of the header fields present in the |
| 3017 | * message, and thus cannot contain a message body. |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 3018 | * |
Willy Tarreau | 557f199 | 2015-05-01 10:05:17 +0200 | [diff] [blame] | 3019 | * 2. Any 2xx (Successful) response to a CONNECT request implies that |
| 3020 | * the connection will become a tunnel immediately after the empty |
| 3021 | * line that concludes the header fields. A client MUST ignore any |
| 3022 | * Content-Length or Transfer-Encoding header fields received in |
| 3023 | * such a message. |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 3024 | * |
Willy Tarreau | 557f199 | 2015-05-01 10:05:17 +0200 | [diff] [blame] | 3025 | * 3. If a Transfer-Encoding header field is present and the chunked |
| 3026 | * transfer coding (Section 4.1) is the final encoding, the message |
| 3027 | * body length is determined by reading and decoding the chunked |
| 3028 | * data until the transfer coding indicates the data is complete. |
| 3029 | * |
| 3030 | * If a Transfer-Encoding header field is present in a response and |
| 3031 | * the chunked transfer coding is not the final encoding, the |
| 3032 | * message body length is determined by reading the connection until |
| 3033 | * it is closed by the server. If a Transfer-Encoding header field |
| 3034 | * is present in a request and the chunked transfer coding is not |
| 3035 | * the final encoding, the message body length cannot be determined |
| 3036 | * reliably; the server MUST respond with the 400 (Bad Request) |
| 3037 | * status code and then close the connection. |
| 3038 | * |
| 3039 | * If a message is received with both a Transfer-Encoding and a |
| 3040 | * Content-Length header field, the Transfer-Encoding overrides the |
| 3041 | * Content-Length. Such a message might indicate an attempt to |
| 3042 | * perform request smuggling (Section 9.5) or response splitting |
| 3043 | * (Section 9.4) and ought to be handled as an error. A sender MUST |
| 3044 | * remove the received Content-Length field prior to forwarding such |
| 3045 | * a message downstream. |
| 3046 | * |
| 3047 | * 4. If a message is received without Transfer-Encoding and with |
| 3048 | * either multiple Content-Length header fields having differing |
| 3049 | * field-values or a single Content-Length header field having an |
| 3050 | * invalid value, then the message framing is invalid and the |
| 3051 | * recipient MUST treat it as an unrecoverable error. If this is a |
| 3052 | * request message, the server MUST respond with a 400 (Bad Request) |
| 3053 | * status code and then close the connection. If this is a response |
| 3054 | * message received by a proxy, the proxy MUST close the connection |
| 3055 | * to the server, discard the received response, and send a 502 (Bad |
| 3056 | * Gateway) response to the client. If this is a response message |
| 3057 | * received by a user agent, the user agent MUST close the |
| 3058 | * connection to the server and discard the received response. |
| 3059 | * |
| 3060 | * 5. If a valid Content-Length header field is present without |
| 3061 | * Transfer-Encoding, its decimal value defines the expected message |
| 3062 | * body length in octets. If the sender closes the connection or |
| 3063 | * the recipient times out before the indicated number of octets are |
| 3064 | * received, the recipient MUST consider the message to be |
| 3065 | * incomplete and close the connection. |
| 3066 | * |
| 3067 | * 6. If this is a request message and none of the above are true, then |
| 3068 | * the message body length is zero (no message body is present). |
| 3069 | * |
| 3070 | * 7. Otherwise, this is a response message without a declared message |
| 3071 | * body length, so the message body length is determined by the |
| 3072 | * number of octets received prior to the server closing the |
| 3073 | * connection. |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 3074 | */ |
| 3075 | |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 3076 | ctx.idx = 0; |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 3077 | /* set TE_CHNK and XFER_LEN only if "chunked" is seen last */ |
Willy Tarreau | 4979d5c | 2015-05-01 10:06:30 +0200 | [diff] [blame] | 3078 | while (http_find_header2("Transfer-Encoding", 17, req->buf->p, &txn->hdr_idx, &ctx)) { |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 3079 | if (ctx.vlen == 7 && strncasecmp(ctx.line + ctx.val, "chunked", 7) == 0) |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 3080 | msg->flags |= (HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN); |
| 3081 | else if (msg->flags & HTTP_MSGF_TE_CHNK) { |
Willy Tarreau | 34dfc60 | 2015-05-01 10:09:49 +0200 | [diff] [blame] | 3082 | /* chunked not last, return badreq */ |
| 3083 | goto return_bad_req; |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 3084 | } |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 3085 | } |
| 3086 | |
Willy Tarreau | 1c91391 | 2015-04-30 10:57:51 +0200 | [diff] [blame] | 3087 | /* Chunked requests must have their content-length removed */ |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 3088 | ctx.idx = 0; |
Willy Tarreau | 1c91391 | 2015-04-30 10:57:51 +0200 | [diff] [blame] | 3089 | if (msg->flags & HTTP_MSGF_TE_CHNK) { |
| 3090 | while (http_find_header2("Content-Length", 14, req->buf->p, &txn->hdr_idx, &ctx)) |
| 3091 | http_remove_header2(msg, &txn->hdr_idx, &ctx); |
| 3092 | } |
Willy Tarreau | 34dfc60 | 2015-05-01 10:09:49 +0200 | [diff] [blame] | 3093 | else while (http_find_header2("Content-Length", 14, req->buf->p, &txn->hdr_idx, &ctx)) { |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 3094 | signed long long cl; |
| 3095 | |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 3096 | if (!ctx.vlen) { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 3097 | msg->err_pos = ctx.line + ctx.val - req->buf->p; |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 3098 | goto return_bad_req; |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 3099 | } |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 3100 | |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 3101 | if (strl2llrc(ctx.line + ctx.val, ctx.vlen, &cl)) { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 3102 | msg->err_pos = ctx.line + ctx.val - req->buf->p; |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 3103 | goto return_bad_req; /* parse failure */ |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 3104 | } |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 3105 | |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 3106 | if (cl < 0) { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 3107 | msg->err_pos = ctx.line + ctx.val - req->buf->p; |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 3108 | goto return_bad_req; |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 3109 | } |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 3110 | |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 3111 | if ((msg->flags & HTTP_MSGF_CNT_LEN) && (msg->chunk_len != cl)) { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 3112 | msg->err_pos = ctx.line + ctx.val - req->buf->p; |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 3113 | goto return_bad_req; /* already specified, was different */ |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 3114 | } |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 3115 | |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 3116 | msg->flags |= HTTP_MSGF_CNT_LEN | HTTP_MSGF_XFER_LEN; |
Willy Tarreau | 124d991 | 2011-03-01 20:30:48 +0100 | [diff] [blame] | 3117 | msg->body_len = msg->chunk_len = cl; |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 3118 | } |
| 3119 | |
Willy Tarreau | 34dfc60 | 2015-05-01 10:09:49 +0200 | [diff] [blame] | 3120 | /* even bodyless requests have a known length */ |
| 3121 | msg->flags |= HTTP_MSGF_XFER_LEN; |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 3122 | |
Willy Tarreau | 179085c | 2014-04-28 16:48:56 +0200 | [diff] [blame] | 3123 | /* Until set to anything else, the connection mode is set as Keep-Alive. It will |
| 3124 | * only change if both the request and the config reference something else. |
| 3125 | * Option httpclose by itself sets tunnel mode where headers are mangled. |
| 3126 | * However, if another mode is set, it will affect it (eg: server-close/ |
| 3127 | * keep-alive + httpclose = close). Note that we avoid to redo the same work |
| 3128 | * if FE and BE have the same settings (common). The method consists in |
| 3129 | * checking if options changed between the two calls (implying that either |
| 3130 | * one is non-null, or one of them is non-null and we are there for the first |
| 3131 | * time. |
| 3132 | */ |
| 3133 | if (!(txn->flags & TX_HDR_CONN_PRS) || |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 3134 | ((sess->fe->options & PR_O_HTTP_MODE) != (s->be->options & PR_O_HTTP_MODE))) |
Willy Tarreau | 4e21ff9 | 2014-09-30 18:44:22 +0200 | [diff] [blame] | 3135 | http_adjust_conn_mode(s, txn, msg); |
Willy Tarreau | 179085c | 2014-04-28 16:48:56 +0200 | [diff] [blame] | 3136 | |
Willy Tarreau | 9fbe18e | 2015-05-01 22:42:08 +0200 | [diff] [blame] | 3137 | /* we may have to wait for the request's body */ |
| 3138 | if ((s->be->options & PR_O_WREQ_BODY) && |
| 3139 | (msg->body_len || (msg->flags & HTTP_MSGF_TE_CHNK))) |
| 3140 | req->analysers |= AN_REQ_HTTP_BODY; |
| 3141 | |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 3142 | /* end of job, return OK */ |
Willy Tarreau | 3a81629 | 2009-07-07 10:55:49 +0200 | [diff] [blame] | 3143 | req->analysers &= ~an_bit; |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 3144 | req->analyse_exp = TICK_ETERNITY; |
| 3145 | return 1; |
| 3146 | |
| 3147 | return_bad_req: |
| 3148 | /* We centralize bad requests processing here */ |
| 3149 | if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) { |
| 3150 | /* we detected a parsing error. We want to archive this request |
| 3151 | * in the dedicated proxy area for later troubleshooting. |
| 3152 | */ |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 3153 | http_capture_bad_message(&sess->fe->invalid_req, s, msg, msg->msg_state, sess->fe); |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 3154 | } |
| 3155 | |
| 3156 | txn->req.msg_state = HTTP_MSG_ERROR; |
| 3157 | txn->status = 400; |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 3158 | stream_int_retnclose(&s->si[0], http_error_message(s, HTTP_ERR_400)); |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 3159 | |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 3160 | sess->fe->fe_counters.failed_req++; |
Willy Tarreau | fb0afa7 | 2015-04-03 14:46:27 +0200 | [diff] [blame] | 3161 | if (sess->listener->counters) |
| 3162 | sess->listener->counters->failed_req++; |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 3163 | |
| 3164 | return_prx_cond: |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 3165 | if (!(s->flags & SF_ERR_MASK)) |
| 3166 | s->flags |= SF_ERR_PRXCOND; |
| 3167 | if (!(s->flags & SF_FINST_MASK)) |
| 3168 | s->flags |= SF_FINST_R; |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 3169 | |
| 3170 | req->analysers = 0; |
| 3171 | req->analyse_exp = TICK_ETERNITY; |
| 3172 | return 0; |
| 3173 | } |
| 3174 | |
Willy Tarreau | 4f8a83c | 2012-06-04 00:26:23 +0200 | [diff] [blame] | 3175 | |
Willy Tarreau | 347a35d | 2013-11-22 17:51:09 +0100 | [diff] [blame] | 3176 | /* This function prepares an applet to handle the stats. It can deal with the |
| 3177 | * "100-continue" expectation, check that admin rules are met for POST requests, |
| 3178 | * and program a response message if something was unexpected. It cannot fail |
| 3179 | * and always relies on the stats applet to complete the job. It does not touch |
Willy Tarreau | 414e9bb | 2013-11-23 00:30:38 +0100 | [diff] [blame] | 3180 | * analysers nor counters, which are left to the caller. It does not touch |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 3181 | * s->target which is supposed to already point to the stats applet. The caller |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 3182 | * is expected to have already assigned an appctx to the stream. |
Willy Tarreau | 1facd6d | 2012-12-22 22:03:39 +0100 | [diff] [blame] | 3183 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 3184 | int http_handle_stats(struct stream *s, struct channel *req) |
Willy Tarreau | 1facd6d | 2012-12-22 22:03:39 +0100 | [diff] [blame] | 3185 | { |
| 3186 | struct stats_admin_rule *stats_admin_rule; |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 3187 | struct stream_interface *si = &s->si[1]; |
Willy Tarreau | 192252e | 2015-04-04 01:47:55 +0200 | [diff] [blame] | 3188 | struct session *sess = s->sess; |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 3189 | struct http_txn *txn = s->txn; |
Willy Tarreau | 1facd6d | 2012-12-22 22:03:39 +0100 | [diff] [blame] | 3190 | struct http_msg *msg = &txn->req; |
Willy Tarreau | 414e9bb | 2013-11-23 00:30:38 +0100 | [diff] [blame] | 3191 | struct uri_auth *uri_auth = s->be->uri_auth; |
| 3192 | const char *uri, *h, *lookup; |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 3193 | struct appctx *appctx; |
Willy Tarreau | 414e9bb | 2013-11-23 00:30:38 +0100 | [diff] [blame] | 3194 | |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 3195 | appctx = si_appctx(si); |
| 3196 | memset(&appctx->ctx.stats, 0, sizeof(appctx->ctx.stats)); |
| 3197 | appctx->st1 = appctx->st2 = 0; |
| 3198 | appctx->ctx.stats.st_code = STAT_STATUS_INIT; |
| 3199 | appctx->ctx.stats.flags |= STAT_FMT_HTML; /* assume HTML mode by default */ |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 3200 | if ((msg->flags & HTTP_MSGF_VER_11) && (s->txn->meth != HTTP_METH_HEAD)) |
Willy Tarreau | af3cf70 | 2014-04-22 22:19:53 +0200 | [diff] [blame] | 3201 | appctx->ctx.stats.flags |= STAT_CHUNKED; |
Willy Tarreau | 414e9bb | 2013-11-23 00:30:38 +0100 | [diff] [blame] | 3202 | |
| 3203 | uri = msg->chn->buf->p + msg->sl.rq.u; |
| 3204 | lookup = uri + uri_auth->uri_len; |
| 3205 | |
| 3206 | for (h = lookup; h <= uri + msg->sl.rq.u_l - 3; h++) { |
| 3207 | if (memcmp(h, ";up", 3) == 0) { |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 3208 | appctx->ctx.stats.flags |= STAT_HIDE_DOWN; |
Willy Tarreau | 414e9bb | 2013-11-23 00:30:38 +0100 | [diff] [blame] | 3209 | break; |
| 3210 | } |
| 3211 | } |
| 3212 | |
| 3213 | if (uri_auth->refresh) { |
| 3214 | for (h = lookup; h <= uri + msg->sl.rq.u_l - 10; h++) { |
| 3215 | if (memcmp(h, ";norefresh", 10) == 0) { |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 3216 | appctx->ctx.stats.flags |= STAT_NO_REFRESH; |
Willy Tarreau | 414e9bb | 2013-11-23 00:30:38 +0100 | [diff] [blame] | 3217 | break; |
| 3218 | } |
| 3219 | } |
| 3220 | } |
| 3221 | |
| 3222 | for (h = lookup; h <= uri + msg->sl.rq.u_l - 4; h++) { |
| 3223 | if (memcmp(h, ";csv", 4) == 0) { |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 3224 | appctx->ctx.stats.flags &= ~STAT_FMT_HTML; |
Willy Tarreau | 414e9bb | 2013-11-23 00:30:38 +0100 | [diff] [blame] | 3225 | break; |
| 3226 | } |
| 3227 | } |
| 3228 | |
| 3229 | for (h = lookup; h <= uri + msg->sl.rq.u_l - 8; h++) { |
| 3230 | if (memcmp(h, ";st=", 4) == 0) { |
| 3231 | int i; |
| 3232 | h += 4; |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 3233 | appctx->ctx.stats.st_code = STAT_STATUS_UNKN; |
Willy Tarreau | 414e9bb | 2013-11-23 00:30:38 +0100 | [diff] [blame] | 3234 | for (i = STAT_STATUS_INIT + 1; i < STAT_STATUS_SIZE; i++) { |
| 3235 | if (strncmp(stat_status_codes[i], h, 4) == 0) { |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 3236 | appctx->ctx.stats.st_code = i; |
Willy Tarreau | 414e9bb | 2013-11-23 00:30:38 +0100 | [diff] [blame] | 3237 | break; |
| 3238 | } |
| 3239 | } |
| 3240 | break; |
| 3241 | } |
| 3242 | } |
| 3243 | |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 3244 | appctx->ctx.stats.scope_str = 0; |
| 3245 | appctx->ctx.stats.scope_len = 0; |
Willy Tarreau | 414e9bb | 2013-11-23 00:30:38 +0100 | [diff] [blame] | 3246 | for (h = lookup; h <= uri + msg->sl.rq.u_l - 8; h++) { |
| 3247 | if (memcmp(h, STAT_SCOPE_INPUT_NAME "=", strlen(STAT_SCOPE_INPUT_NAME) + 1) == 0) { |
| 3248 | int itx = 0; |
| 3249 | const char *h2; |
| 3250 | char scope_txt[STAT_SCOPE_TXT_MAXLEN + 1]; |
| 3251 | const char *err; |
| 3252 | |
| 3253 | h += strlen(STAT_SCOPE_INPUT_NAME) + 1; |
| 3254 | h2 = h; |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 3255 | appctx->ctx.stats.scope_str = h2 - msg->chn->buf->p; |
Willy Tarreau | 414e9bb | 2013-11-23 00:30:38 +0100 | [diff] [blame] | 3256 | while (*h != ';' && *h != '\0' && *h != '&' && *h != ' ' && *h != '\n') { |
| 3257 | itx++; |
| 3258 | h++; |
| 3259 | } |
| 3260 | |
| 3261 | if (itx > STAT_SCOPE_TXT_MAXLEN) |
| 3262 | itx = STAT_SCOPE_TXT_MAXLEN; |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 3263 | appctx->ctx.stats.scope_len = itx; |
Willy Tarreau | 414e9bb | 2013-11-23 00:30:38 +0100 | [diff] [blame] | 3264 | |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 3265 | /* scope_txt = search query, appctx->ctx.stats.scope_len is always <= STAT_SCOPE_TXT_MAXLEN */ |
Willy Tarreau | 414e9bb | 2013-11-23 00:30:38 +0100 | [diff] [blame] | 3266 | memcpy(scope_txt, h2, itx); |
| 3267 | scope_txt[itx] = '\0'; |
| 3268 | err = invalid_char(scope_txt); |
| 3269 | if (err) { |
| 3270 | /* bad char in search text => clear scope */ |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 3271 | appctx->ctx.stats.scope_str = 0; |
| 3272 | appctx->ctx.stats.scope_len = 0; |
Willy Tarreau | 414e9bb | 2013-11-23 00:30:38 +0100 | [diff] [blame] | 3273 | } |
| 3274 | break; |
| 3275 | } |
| 3276 | } |
Willy Tarreau | 1facd6d | 2012-12-22 22:03:39 +0100 | [diff] [blame] | 3277 | |
| 3278 | /* now check whether we have some admin rules for this request */ |
Willy Tarreau | 414e9bb | 2013-11-23 00:30:38 +0100 | [diff] [blame] | 3279 | list_for_each_entry(stats_admin_rule, &uri_auth->admin_rules, list) { |
Willy Tarreau | 1facd6d | 2012-12-22 22:03:39 +0100 | [diff] [blame] | 3280 | int ret = 1; |
| 3281 | |
| 3282 | if (stats_admin_rule->cond) { |
Willy Tarreau | 192252e | 2015-04-04 01:47:55 +0200 | [diff] [blame] | 3283 | ret = acl_exec_cond(stats_admin_rule->cond, s->be, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL); |
Willy Tarreau | 1facd6d | 2012-12-22 22:03:39 +0100 | [diff] [blame] | 3284 | ret = acl_pass(ret); |
| 3285 | if (stats_admin_rule->cond->pol == ACL_COND_UNLESS) |
| 3286 | ret = !ret; |
| 3287 | } |
| 3288 | |
| 3289 | if (ret) { |
| 3290 | /* no rule, or the rule matches */ |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 3291 | appctx->ctx.stats.flags |= STAT_ADMIN; |
Willy Tarreau | 1facd6d | 2012-12-22 22:03:39 +0100 | [diff] [blame] | 3292 | break; |
| 3293 | } |
| 3294 | } |
| 3295 | |
| 3296 | /* Was the status page requested with a POST ? */ |
Willy Tarreau | b8cdf52 | 2015-05-29 01:09:15 +0200 | [diff] [blame] | 3297 | if (unlikely(txn->meth == HTTP_METH_POST && txn->req.body_len > 0)) { |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 3298 | if (appctx->ctx.stats.flags & STAT_ADMIN) { |
Willy Tarreau | cfe7fdd | 2014-04-26 22:08:32 +0200 | [diff] [blame] | 3299 | /* we'll need the request body, possibly after sending 100-continue */ |
Willy Tarreau | b8cdf52 | 2015-05-29 01:09:15 +0200 | [diff] [blame] | 3300 | if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) |
| 3301 | req->analysers |= AN_REQ_HTTP_BODY; |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 3302 | appctx->st0 = STAT_HTTP_POST; |
Willy Tarreau | 1facd6d | 2012-12-22 22:03:39 +0100 | [diff] [blame] | 3303 | } |
Willy Tarreau | 347a35d | 2013-11-22 17:51:09 +0100 | [diff] [blame] | 3304 | else { |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 3305 | appctx->ctx.stats.st_code = STAT_STATUS_DENY; |
| 3306 | appctx->st0 = STAT_HTTP_LAST; |
de Lafond Guillaume | 88c278f | 2013-04-15 19:27:10 +0200 | [diff] [blame] | 3307 | } |
Willy Tarreau | 1facd6d | 2012-12-22 22:03:39 +0100 | [diff] [blame] | 3308 | } |
Willy Tarreau | 347a35d | 2013-11-22 17:51:09 +0100 | [diff] [blame] | 3309 | else { |
| 3310 | /* So it was another method (GET/HEAD) */ |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 3311 | appctx->st0 = STAT_HTTP_HEAD; |
Willy Tarreau | 1facd6d | 2012-12-22 22:03:39 +0100 | [diff] [blame] | 3312 | } |
| 3313 | |
Willy Tarreau | 1facd6d | 2012-12-22 22:03:39 +0100 | [diff] [blame] | 3314 | s->task->nice = -32; /* small boost for HTTP statistics */ |
Willy Tarreau | 1facd6d | 2012-12-22 22:03:39 +0100 | [diff] [blame] | 3315 | return 1; |
| 3316 | } |
| 3317 | |
Lukas Tribus | 67db8df | 2013-06-23 17:37:13 +0200 | [diff] [blame] | 3318 | /* Sets the TOS header in IPv4 and the traffic class header in IPv6 packets |
| 3319 | * (as per RFC3260 #4 and BCP37 #4.2 and #5.2). |
| 3320 | */ |
Thierry FOURNIER | 7fe75e0 | 2015-03-16 12:03:44 +0100 | [diff] [blame] | 3321 | void inet_set_tos(int fd, struct sockaddr_storage from, int tos) |
Lukas Tribus | 67db8df | 2013-06-23 17:37:13 +0200 | [diff] [blame] | 3322 | { |
| 3323 | #ifdef IP_TOS |
| 3324 | if (from.ss_family == AF_INET) |
| 3325 | setsockopt(fd, IPPROTO_IP, IP_TOS, &tos, sizeof(tos)); |
| 3326 | #endif |
| 3327 | #ifdef IPV6_TCLASS |
| 3328 | if (from.ss_family == AF_INET6) { |
| 3329 | if (IN6_IS_ADDR_V4MAPPED(&((struct sockaddr_in6 *)&from)->sin6_addr)) |
| 3330 | /* v4-mapped addresses need IP_TOS */ |
| 3331 | setsockopt(fd, IPPROTO_IP, IP_TOS, &tos, sizeof(tos)); |
| 3332 | else |
| 3333 | setsockopt(fd, IPPROTO_IPV6, IPV6_TCLASS, &tos, sizeof(tos)); |
| 3334 | } |
| 3335 | #endif |
| 3336 | } |
| 3337 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 3338 | int http_transform_header_str(struct stream* s, struct http_msg *msg, |
Thierry FOURNIER | 5531f87 | 2015-03-16 11:15:50 +0100 | [diff] [blame] | 3339 | const char* name, unsigned int name_len, |
| 3340 | const char *str, struct my_regex *re, |
| 3341 | int action) |
Sasha Pachev | 218f064 | 2014-06-16 12:05:59 -0600 | [diff] [blame] | 3342 | { |
Thierry FOURNIER | 5a33ac7 | 2015-03-16 23:54:35 +0100 | [diff] [blame] | 3343 | struct hdr_ctx ctx; |
| 3344 | char *buf = msg->chn->buf->p; |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 3345 | struct hdr_idx *idx = &s->txn->hdr_idx; |
Thierry FOURNIER | 5531f87 | 2015-03-16 11:15:50 +0100 | [diff] [blame] | 3346 | int (*http_find_hdr_func)(const char *name, int len, char *sol, |
| 3347 | struct hdr_idx *idx, struct hdr_ctx *ctx); |
Thierry FOURNIER | 191f9ef | 2015-03-16 23:23:53 +0100 | [diff] [blame] | 3348 | struct chunk *output = get_trash_chunk(); |
| 3349 | |
Thierry FOURNIER | 5a33ac7 | 2015-03-16 23:54:35 +0100 | [diff] [blame] | 3350 | ctx.idx = 0; |
Sasha Pachev | 218f064 | 2014-06-16 12:05:59 -0600 | [diff] [blame] | 3351 | |
Thierry FOURNIER | 191f9ef | 2015-03-16 23:23:53 +0100 | [diff] [blame] | 3352 | /* Choose the header browsing function. */ |
| 3353 | switch (action) { |
| 3354 | case HTTP_REQ_ACT_REPLACE_VAL: |
| 3355 | case HTTP_RES_ACT_REPLACE_VAL: |
| 3356 | http_find_hdr_func = http_find_header2; |
| 3357 | break; |
| 3358 | case HTTP_REQ_ACT_REPLACE_HDR: |
| 3359 | case HTTP_RES_ACT_REPLACE_HDR: |
| 3360 | http_find_hdr_func = http_find_full_header2; |
| 3361 | break; |
| 3362 | default: /* impossible */ |
| 3363 | return -1; |
| 3364 | } |
| 3365 | |
Thierry FOURNIER | 5a33ac7 | 2015-03-16 23:54:35 +0100 | [diff] [blame] | 3366 | while (http_find_hdr_func(name, name_len, buf, idx, &ctx)) { |
| 3367 | struct hdr_idx_elem *hdr = idx->v + ctx.idx; |
Sasha Pachev | 218f064 | 2014-06-16 12:05:59 -0600 | [diff] [blame] | 3368 | int delta; |
Thierry FOURNIER | 5a33ac7 | 2015-03-16 23:54:35 +0100 | [diff] [blame] | 3369 | char *val = ctx.line + ctx.val; |
| 3370 | char* val_end = val + ctx.vlen; |
Sasha Pachev | 218f064 | 2014-06-16 12:05:59 -0600 | [diff] [blame] | 3371 | |
Thierry FOURNIER | 191f9ef | 2015-03-16 23:23:53 +0100 | [diff] [blame] | 3372 | if (!regex_exec_match2(re, val, val_end-val, MAX_MATCH, pmatch, 0)) |
| 3373 | continue; |
Sasha Pachev | 218f064 | 2014-06-16 12:05:59 -0600 | [diff] [blame] | 3374 | |
Thierry FOURNIER | 5531f87 | 2015-03-16 11:15:50 +0100 | [diff] [blame] | 3375 | output->len = exp_replace(output->str, output->size, val, str, pmatch); |
Thierry FOURNIER | 191f9ef | 2015-03-16 23:23:53 +0100 | [diff] [blame] | 3376 | if (output->len == -1) |
Sasha Pachev | 218f064 | 2014-06-16 12:05:59 -0600 | [diff] [blame] | 3377 | return -1; |
Sasha Pachev | 218f064 | 2014-06-16 12:05:59 -0600 | [diff] [blame] | 3378 | |
Thierry FOURNIER | 191f9ef | 2015-03-16 23:23:53 +0100 | [diff] [blame] | 3379 | delta = buffer_replace2(msg->chn->buf, val, val_end, output->str, output->len); |
Sasha Pachev | 218f064 | 2014-06-16 12:05:59 -0600 | [diff] [blame] | 3380 | |
| 3381 | hdr->len += delta; |
| 3382 | http_msg_move_end(msg, delta); |
Thierry FOURNIER | 191f9ef | 2015-03-16 23:23:53 +0100 | [diff] [blame] | 3383 | |
| 3384 | /* Adjust the length of the current value of the index. */ |
Thierry FOURNIER | 5a33ac7 | 2015-03-16 23:54:35 +0100 | [diff] [blame] | 3385 | ctx.vlen += delta; |
Sasha Pachev | 218f064 | 2014-06-16 12:05:59 -0600 | [diff] [blame] | 3386 | } |
| 3387 | |
| 3388 | return 0; |
| 3389 | } |
| 3390 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 3391 | static int http_transform_header(struct stream* s, struct http_msg *msg, |
Thierry FOURNIER | 5531f87 | 2015-03-16 11:15:50 +0100 | [diff] [blame] | 3392 | const char* name, unsigned int name_len, |
| 3393 | struct list *fmt, struct my_regex *re, |
| 3394 | int action) |
| 3395 | { |
| 3396 | struct chunk *replace = get_trash_chunk(); |
| 3397 | |
| 3398 | replace->len = build_logline(s, replace->str, replace->size, fmt); |
| 3399 | if (replace->len >= replace->size - 1) |
| 3400 | return -1; |
| 3401 | |
| 3402 | return http_transform_header_str(s, msg, name, name_len, replace->str, re, action); |
| 3403 | } |
| 3404 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 3405 | /* Executes the http-request rules <rules> for stream <s>, proxy <px> and |
Willy Tarreau | 0b74833 | 2014-04-29 00:13:29 +0200 | [diff] [blame] | 3406 | * transaction <txn>. Returns the verdict of the first rule that prevents |
| 3407 | * further processing of the request (auth, deny, ...), and defaults to |
| 3408 | * HTTP_RULE_RES_STOP if it executed all rules or stopped on an allow, or |
| 3409 | * HTTP_RULE_RES_CONT if the last rule was reached. It may set the TX_CLTARPIT |
| 3410 | * on txn->flags if it encounters a tarpit rule. |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 3411 | */ |
Willy Tarreau | 0b74833 | 2014-04-29 00:13:29 +0200 | [diff] [blame] | 3412 | enum rule_result |
Willy Tarreau | 987e3fb | 2015-04-04 01:09:08 +0200 | [diff] [blame] | 3413 | http_req_get_intercept_rule(struct proxy *px, struct list *rules, struct stream *s) |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 3414 | { |
Willy Tarreau | 9ad7bd4 | 2015-04-03 19:19:59 +0200 | [diff] [blame] | 3415 | struct session *sess = strm_sess(s); |
Willy Tarreau | 987e3fb | 2015-04-04 01:09:08 +0200 | [diff] [blame] | 3416 | struct http_txn *txn = s->txn; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 3417 | struct connection *cli_conn; |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 3418 | struct http_req_rule *rule; |
Willy Tarreau | 20b0de5 | 2012-12-24 15:45:22 +0100 | [diff] [blame] | 3419 | struct hdr_ctx ctx; |
Willy Tarreau | ae3c010 | 2014-04-28 23:22:08 +0200 | [diff] [blame] | 3420 | const char *auth_realm; |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 3421 | |
Thierry FOURNIER | bc4c1ac | 2015-02-25 13:36:14 +0100 | [diff] [blame] | 3422 | /* If "the current_rule_list" match the executed rule list, we are in |
| 3423 | * resume condition. If a resume is needed it is always in the action |
| 3424 | * and never in the ACL or converters. In this case, we initialise the |
| 3425 | * current rule, and go to the action execution point. |
| 3426 | */ |
Willy Tarreau | 152b81e | 2015-04-20 13:26:17 +0200 | [diff] [blame] | 3427 | if (s->current_rule) { |
| 3428 | rule = s->current_rule; |
| 3429 | s->current_rule = NULL; |
| 3430 | if (s->current_rule_list == rules) |
| 3431 | goto resume_execution; |
Thierry FOURNIER | bc4c1ac | 2015-02-25 13:36:14 +0100 | [diff] [blame] | 3432 | } |
| 3433 | s->current_rule_list = rules; |
Willy Tarreau | 152b81e | 2015-04-20 13:26:17 +0200 | [diff] [blame] | 3434 | |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 3435 | list_for_each_entry(rule, rules, list) { |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 3436 | if (rule->action >= HTTP_REQ_ACT_MAX) |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 3437 | continue; |
| 3438 | |
Willy Tarreau | 96257ec | 2012-12-27 10:46:37 +0100 | [diff] [blame] | 3439 | /* check optional condition */ |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 3440 | if (rule->cond) { |
Willy Tarreau | 96257ec | 2012-12-27 10:46:37 +0100 | [diff] [blame] | 3441 | int ret; |
| 3442 | |
Willy Tarreau | 192252e | 2015-04-04 01:47:55 +0200 | [diff] [blame] | 3443 | ret = acl_exec_cond(rule->cond, px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL); |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 3444 | ret = acl_pass(ret); |
| 3445 | |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 3446 | if (rule->cond->pol == ACL_COND_UNLESS) |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 3447 | ret = !ret; |
Willy Tarreau | 96257ec | 2012-12-27 10:46:37 +0100 | [diff] [blame] | 3448 | |
| 3449 | if (!ret) /* condition not matched */ |
| 3450 | continue; |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 3451 | } |
| 3452 | |
Thierry FOURNIER | bc4c1ac | 2015-02-25 13:36:14 +0100 | [diff] [blame] | 3453 | resume_execution: |
Willy Tarreau | 96257ec | 2012-12-27 10:46:37 +0100 | [diff] [blame] | 3454 | switch (rule->action) { |
| 3455 | case HTTP_REQ_ACT_ALLOW: |
Willy Tarreau | 0b74833 | 2014-04-29 00:13:29 +0200 | [diff] [blame] | 3456 | return HTTP_RULE_RES_STOP; |
Willy Tarreau | 96257ec | 2012-12-27 10:46:37 +0100 | [diff] [blame] | 3457 | |
| 3458 | case HTTP_REQ_ACT_DENY: |
CJ Ess | 108b1dd | 2015-04-07 12:03:37 -0400 | [diff] [blame] | 3459 | txn->rule_deny_status = rule->deny_status; |
Willy Tarreau | 0b74833 | 2014-04-29 00:13:29 +0200 | [diff] [blame] | 3460 | return HTTP_RULE_RES_DENY; |
Willy Tarreau | 96257ec | 2012-12-27 10:46:37 +0100 | [diff] [blame] | 3461 | |
Willy Tarreau | ccbcc37 | 2012-12-27 12:37:57 +0100 | [diff] [blame] | 3462 | case HTTP_REQ_ACT_TARPIT: |
| 3463 | txn->flags |= TX_CLTARPIT; |
CJ Ess | 108b1dd | 2015-04-07 12:03:37 -0400 | [diff] [blame] | 3464 | txn->rule_deny_status = rule->deny_status; |
Willy Tarreau | 0b74833 | 2014-04-29 00:13:29 +0200 | [diff] [blame] | 3465 | return HTTP_RULE_RES_DENY; |
Willy Tarreau | ccbcc37 | 2012-12-27 12:37:57 +0100 | [diff] [blame] | 3466 | |
Willy Tarreau | 96257ec | 2012-12-27 10:46:37 +0100 | [diff] [blame] | 3467 | case HTTP_REQ_ACT_AUTH: |
Willy Tarreau | ae3c010 | 2014-04-28 23:22:08 +0200 | [diff] [blame] | 3468 | /* Auth might be performed on regular http-req rules as well as on stats */ |
| 3469 | auth_realm = rule->arg.auth.realm; |
| 3470 | if (!auth_realm) { |
| 3471 | if (px->uri_auth && rules == &px->uri_auth->http_req_rules) |
| 3472 | auth_realm = STATS_DEFAULT_REALM; |
| 3473 | else |
| 3474 | auth_realm = px->id; |
| 3475 | } |
| 3476 | /* send 401/407 depending on whether we use a proxy or not. We still |
| 3477 | * count one error, because normal browsing won't significantly |
| 3478 | * increase the counter but brute force attempts will. |
| 3479 | */ |
| 3480 | chunk_printf(&trash, (txn->flags & TX_USE_PX_CONN) ? HTTP_407_fmt : HTTP_401_fmt, auth_realm); |
| 3481 | txn->status = (txn->flags & TX_USE_PX_CONN) ? 407 : 401; |
| 3482 | stream_int_retnclose(&s->si[0], &trash); |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 3483 | stream_inc_http_err_ctr(s); |
Willy Tarreau | 0b74833 | 2014-04-29 00:13:29 +0200 | [diff] [blame] | 3484 | return HTTP_RULE_RES_ABRT; |
Willy Tarreau | 96257ec | 2012-12-27 10:46:37 +0100 | [diff] [blame] | 3485 | |
Willy Tarreau | 81499eb | 2012-12-27 12:19:02 +0100 | [diff] [blame] | 3486 | case HTTP_REQ_ACT_REDIR: |
Willy Tarreau | 0b74833 | 2014-04-29 00:13:29 +0200 | [diff] [blame] | 3487 | if (!http_apply_redirect_rule(rule->arg.redir, s, txn)) |
| 3488 | return HTTP_RULE_RES_BADREQ; |
| 3489 | return HTTP_RULE_RES_DONE; |
Willy Tarreau | 81499eb | 2012-12-27 12:19:02 +0100 | [diff] [blame] | 3490 | |
Willy Tarreau | f4c43c1 | 2013-06-11 17:01:13 +0200 | [diff] [blame] | 3491 | case HTTP_REQ_ACT_SET_NICE: |
| 3492 | s->task->nice = rule->arg.nice; |
| 3493 | break; |
| 3494 | |
Willy Tarreau | 42cf39e | 2013-06-11 18:51:32 +0200 | [diff] [blame] | 3495 | case HTTP_REQ_ACT_SET_TOS: |
Willy Tarreau | 9ad7bd4 | 2015-04-03 19:19:59 +0200 | [diff] [blame] | 3496 | if ((cli_conn = objt_conn(sess->origin)) && conn_ctrl_ready(cli_conn)) |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 3497 | inet_set_tos(cli_conn->t.sock.fd, cli_conn->addr.from, rule->arg.tos); |
Willy Tarreau | 42cf39e | 2013-06-11 18:51:32 +0200 | [diff] [blame] | 3498 | break; |
| 3499 | |
Willy Tarreau | 51347ed | 2013-06-11 19:34:13 +0200 | [diff] [blame] | 3500 | case HTTP_REQ_ACT_SET_MARK: |
| 3501 | #ifdef SO_MARK |
Willy Tarreau | 9ad7bd4 | 2015-04-03 19:19:59 +0200 | [diff] [blame] | 3502 | if ((cli_conn = objt_conn(sess->origin)) && conn_ctrl_ready(cli_conn)) |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 3503 | setsockopt(cli_conn->t.sock.fd, SOL_SOCKET, SO_MARK, &rule->arg.mark, sizeof(rule->arg.mark)); |
Willy Tarreau | 51347ed | 2013-06-11 19:34:13 +0200 | [diff] [blame] | 3504 | #endif |
| 3505 | break; |
| 3506 | |
Willy Tarreau | 9a355ec | 2013-06-11 17:45:46 +0200 | [diff] [blame] | 3507 | case HTTP_REQ_ACT_SET_LOGL: |
| 3508 | s->logs.level = rule->arg.loglevel; |
| 3509 | break; |
| 3510 | |
Sasha Pachev | 218f064 | 2014-06-16 12:05:59 -0600 | [diff] [blame] | 3511 | case HTTP_REQ_ACT_REPLACE_HDR: |
| 3512 | case HTTP_REQ_ACT_REPLACE_VAL: |
Thierry FOURNIER | 5a33ac7 | 2015-03-16 23:54:35 +0100 | [diff] [blame] | 3513 | if (http_transform_header(s, &txn->req, rule->arg.hdr_add.name, |
| 3514 | rule->arg.hdr_add.name_len, |
| 3515 | &rule->arg.hdr_add.fmt, |
| 3516 | &rule->arg.hdr_add.re, rule->action)) |
Sasha Pachev | 218f064 | 2014-06-16 12:05:59 -0600 | [diff] [blame] | 3517 | return HTTP_RULE_RES_BADREQ; |
| 3518 | break; |
| 3519 | |
Thierry FOURNIER | dad3d1d | 2014-04-22 18:07:25 +0200 | [diff] [blame] | 3520 | case HTTP_REQ_ACT_DEL_HDR: |
Willy Tarreau | 96257ec | 2012-12-27 10:46:37 +0100 | [diff] [blame] | 3521 | ctx.idx = 0; |
| 3522 | /* remove all occurrences of the header */ |
| 3523 | while (http_find_header2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len, |
| 3524 | txn->req.chn->buf->p, &txn->hdr_idx, &ctx)) { |
| 3525 | http_remove_header2(&txn->req, &txn->hdr_idx, &ctx); |
Willy Tarreau | 20b0de5 | 2012-12-24 15:45:22 +0100 | [diff] [blame] | 3526 | } |
Willy Tarreau | 8560328 | 2015-01-21 20:39:27 +0100 | [diff] [blame] | 3527 | break; |
Willy Tarreau | 96257ec | 2012-12-27 10:46:37 +0100 | [diff] [blame] | 3528 | |
Willy Tarreau | 8560328 | 2015-01-21 20:39:27 +0100 | [diff] [blame] | 3529 | case HTTP_REQ_ACT_SET_HDR: |
Willy Tarreau | 96257ec | 2012-12-27 10:46:37 +0100 | [diff] [blame] | 3530 | case HTTP_REQ_ACT_ADD_HDR: |
| 3531 | chunk_printf(&trash, "%s: ", rule->arg.hdr_add.name); |
| 3532 | memcpy(trash.str, rule->arg.hdr_add.name, rule->arg.hdr_add.name_len); |
| 3533 | trash.len = rule->arg.hdr_add.name_len; |
| 3534 | trash.str[trash.len++] = ':'; |
| 3535 | trash.str[trash.len++] = ' '; |
| 3536 | trash.len += build_logline(s, trash.str + trash.len, trash.size - trash.len, &rule->arg.hdr_add.fmt); |
Willy Tarreau | 8560328 | 2015-01-21 20:39:27 +0100 | [diff] [blame] | 3537 | |
| 3538 | if (rule->action == HTTP_REQ_ACT_SET_HDR) { |
| 3539 | /* remove all occurrences of the header */ |
| 3540 | ctx.idx = 0; |
| 3541 | while (http_find_header2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len, |
| 3542 | txn->req.chn->buf->p, &txn->hdr_idx, &ctx)) { |
| 3543 | http_remove_header2(&txn->req, &txn->hdr_idx, &ctx); |
| 3544 | } |
| 3545 | } |
| 3546 | |
Willy Tarreau | 96257ec | 2012-12-27 10:46:37 +0100 | [diff] [blame] | 3547 | http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, trash.len); |
| 3548 | break; |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 3549 | |
| 3550 | case HTTP_REQ_ACT_DEL_ACL: |
| 3551 | case HTTP_REQ_ACT_DEL_MAP: { |
| 3552 | struct pat_ref *ref; |
| 3553 | char *key; |
| 3554 | int len; |
| 3555 | |
| 3556 | /* collect reference */ |
| 3557 | ref = pat_ref_lookup(rule->arg.map.ref); |
| 3558 | if (!ref) |
| 3559 | continue; |
| 3560 | |
| 3561 | /* collect key */ |
| 3562 | len = build_logline(s, trash.str, trash.size, &rule->arg.map.key); |
| 3563 | key = trash.str; |
| 3564 | key[len] = '\0'; |
| 3565 | |
| 3566 | /* perform update */ |
| 3567 | /* returned code: 1=ok, 0=ko */ |
| 3568 | pat_ref_delete(ref, key); |
| 3569 | |
| 3570 | break; |
| 3571 | } |
| 3572 | |
| 3573 | case HTTP_REQ_ACT_ADD_ACL: { |
| 3574 | struct pat_ref *ref; |
| 3575 | char *key; |
| 3576 | struct chunk *trash_key; |
| 3577 | int len; |
| 3578 | |
| 3579 | trash_key = get_trash_chunk(); |
| 3580 | |
| 3581 | /* collect reference */ |
| 3582 | ref = pat_ref_lookup(rule->arg.map.ref); |
| 3583 | if (!ref) |
| 3584 | continue; |
| 3585 | |
| 3586 | /* collect key */ |
| 3587 | len = build_logline(s, trash_key->str, trash_key->size, &rule->arg.map.key); |
| 3588 | key = trash_key->str; |
| 3589 | key[len] = '\0'; |
| 3590 | |
| 3591 | /* perform update */ |
| 3592 | /* add entry only if it does not already exist */ |
| 3593 | if (pat_ref_find_elt(ref, key) == NULL) |
Thierry FOURNIER | e47e4e2 | 2014-04-28 11:18:57 +0200 | [diff] [blame] | 3594 | pat_ref_add(ref, key, NULL, NULL); |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 3595 | |
| 3596 | break; |
| 3597 | } |
| 3598 | |
| 3599 | case HTTP_REQ_ACT_SET_MAP: { |
| 3600 | struct pat_ref *ref; |
| 3601 | char *key, *value; |
| 3602 | struct chunk *trash_key, *trash_value; |
| 3603 | int len; |
| 3604 | |
| 3605 | trash_key = get_trash_chunk(); |
| 3606 | trash_value = get_trash_chunk(); |
| 3607 | |
| 3608 | /* collect reference */ |
| 3609 | ref = pat_ref_lookup(rule->arg.map.ref); |
| 3610 | if (!ref) |
| 3611 | continue; |
| 3612 | |
| 3613 | /* collect key */ |
| 3614 | len = build_logline(s, trash_key->str, trash_key->size, &rule->arg.map.key); |
| 3615 | key = trash_key->str; |
| 3616 | key[len] = '\0'; |
| 3617 | |
| 3618 | /* collect value */ |
| 3619 | len = build_logline(s, trash_value->str, trash_value->size, &rule->arg.map.value); |
| 3620 | value = trash_value->str; |
| 3621 | value[len] = '\0'; |
| 3622 | |
| 3623 | /* perform update */ |
| 3624 | if (pat_ref_find_elt(ref, key) != NULL) |
| 3625 | /* update entry if it exists */ |
| 3626 | pat_ref_set(ref, key, value, NULL); |
| 3627 | else |
| 3628 | /* insert a new entry */ |
Thierry FOURNIER | e47e4e2 | 2014-04-28 11:18:57 +0200 | [diff] [blame] | 3629 | pat_ref_add(ref, key, value, NULL); |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 3630 | |
| 3631 | break; |
| 3632 | } |
William Lallemand | 73025dd | 2014-04-24 14:38:37 +0200 | [diff] [blame] | 3633 | |
| 3634 | case HTTP_REQ_ACT_CUSTOM_CONT: |
Willy Tarreau | 987e3fb | 2015-04-04 01:09:08 +0200 | [diff] [blame] | 3635 | if (!rule->action_ptr(rule, px, s)) { |
Willy Tarreau | 152b81e | 2015-04-20 13:26:17 +0200 | [diff] [blame] | 3636 | s->current_rule = rule; |
Thierry FOURNIER | bc4c1ac | 2015-02-25 13:36:14 +0100 | [diff] [blame] | 3637 | return HTTP_RULE_RES_YIELD; |
| 3638 | } |
William Lallemand | 73025dd | 2014-04-24 14:38:37 +0200 | [diff] [blame] | 3639 | break; |
| 3640 | |
| 3641 | case HTTP_REQ_ACT_CUSTOM_STOP: |
Willy Tarreau | 987e3fb | 2015-04-04 01:09:08 +0200 | [diff] [blame] | 3642 | rule->action_ptr(rule, px, s); |
Willy Tarreau | 0b74833 | 2014-04-29 00:13:29 +0200 | [diff] [blame] | 3643 | return HTTP_RULE_RES_DONE; |
Willy Tarreau | 09448f7 | 2014-06-25 18:12:15 +0200 | [diff] [blame] | 3644 | |
| 3645 | case HTTP_REQ_ACT_TRK_SC0 ... HTTP_REQ_ACT_TRK_SCMAX: |
| 3646 | /* Note: only the first valid tracking parameter of each |
| 3647 | * applies. |
| 3648 | */ |
| 3649 | |
| 3650 | if (stkctr_entry(&s->stkctr[http_req_trk_idx(rule->action)]) == NULL) { |
| 3651 | struct stktable *t; |
| 3652 | struct stksess *ts; |
| 3653 | struct stktable_key *key; |
| 3654 | void *ptr; |
| 3655 | |
| 3656 | t = rule->act_prm.trk_ctr.table.t; |
Willy Tarreau | 192252e | 2015-04-04 01:47:55 +0200 | [diff] [blame] | 3657 | key = stktable_fetch_key(t, s->be, sess, s, SMP_OPT_DIR_REQ | SMP_OPT_FINAL, rule->act_prm.trk_ctr.expr, NULL); |
Willy Tarreau | 09448f7 | 2014-06-25 18:12:15 +0200 | [diff] [blame] | 3658 | |
| 3659 | if (key && (ts = stktable_get_entry(t, key))) { |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 3660 | stream_track_stkctr(&s->stkctr[http_req_trk_idx(rule->action)], t, ts); |
Willy Tarreau | 09448f7 | 2014-06-25 18:12:15 +0200 | [diff] [blame] | 3661 | |
| 3662 | /* let's count a new HTTP request as it's the first time we do it */ |
| 3663 | ptr = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_REQ_CNT); |
| 3664 | if (ptr) |
| 3665 | stktable_data_cast(ptr, http_req_cnt)++; |
| 3666 | |
| 3667 | ptr = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_REQ_RATE); |
| 3668 | if (ptr) |
| 3669 | update_freq_ctr_period(&stktable_data_cast(ptr, http_req_rate), |
| 3670 | t->data_arg[STKTABLE_DT_HTTP_REQ_RATE].u, 1); |
| 3671 | |
| 3672 | stkctr_set_flags(&s->stkctr[http_req_trk_idx(rule->action)], STKCTR_TRACK_CONTENT); |
Willy Tarreau | 9ad7bd4 | 2015-04-03 19:19:59 +0200 | [diff] [blame] | 3673 | if (sess->fe != s->be) |
Willy Tarreau | 09448f7 | 2014-06-25 18:12:15 +0200 | [diff] [blame] | 3674 | stkctr_set_flags(&s->stkctr[http_req_trk_idx(rule->action)], STKCTR_TRACK_BACKEND); |
| 3675 | } |
| 3676 | } |
Adis Nezirovic | 2fbcafc | 2015-07-06 15:44:30 +0200 | [diff] [blame] | 3677 | break; |
| 3678 | |
| 3679 | case HTTP_REQ_ACT_SET_SRC: |
| 3680 | if ((cli_conn = objt_conn(sess->origin)) && conn_ctrl_ready(cli_conn)) { |
| 3681 | struct sample *smp; |
| 3682 | |
| 3683 | smp = sample_fetch_as_type(px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL, rule->arg.act.p[0], SMP_T_ADDR); |
| 3684 | |
| 3685 | if (smp) { |
| 3686 | if (smp->type == SMP_T_IPV4) { |
| 3687 | ((struct sockaddr_in *)&cli_conn->addr.from)->sin_family = AF_INET; |
| 3688 | ((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr.s_addr = smp->data.ipv4.s_addr; |
| 3689 | ((struct sockaddr_in *)&cli_conn->addr.from)->sin_port = 0; |
| 3690 | } else if (smp->type == SMP_T_IPV6) { |
| 3691 | ((struct sockaddr_in6 *)&cli_conn->addr.from)->sin6_family = AF_INET6; |
| 3692 | memcpy(&((struct sockaddr_in6 *)&cli_conn->addr.from)->sin6_addr, &smp->data.ipv6, sizeof(struct in6_addr)); |
| 3693 | ((struct sockaddr_in6 *)&cli_conn->addr.from)->sin6_port = 0; |
| 3694 | } |
| 3695 | } |
| 3696 | } |
| 3697 | break; |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 3698 | } |
| 3699 | } |
Willy Tarreau | 96257ec | 2012-12-27 10:46:37 +0100 | [diff] [blame] | 3700 | |
| 3701 | /* we reached the end of the rules, nothing to report */ |
Willy Tarreau | 0b74833 | 2014-04-29 00:13:29 +0200 | [diff] [blame] | 3702 | return HTTP_RULE_RES_CONT; |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 3703 | } |
| 3704 | |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 3705 | |
Willy Tarreau | 51d861a | 2015-05-22 17:30:48 +0200 | [diff] [blame] | 3706 | /* Executes the http-response rules <rules> for stream <s> and proxy <px>. It |
| 3707 | * returns one of 5 possible statuses: HTTP_RULE_RES_CONT, HTTP_RULE_RES_STOP, |
| 3708 | * HTTP_RULE_RES_DONE, HTTP_RULE_RES_YIELD, or HTTP_RULE_RES_BADREQ. If *CONT |
| 3709 | * is returned, the process can continue the evaluation of next rule list. If |
| 3710 | * *STOP or *DONE is returned, the process must stop the evaluation. If *BADREQ |
| 3711 | * is returned, it means the operation could not be processed and a server error |
| 3712 | * must be returned. It may set the TX_SVDENY on txn->flags if it encounters a |
| 3713 | * deny rule. If *YIELD is returned, the caller must call again the function |
| 3714 | * with the same context. |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 3715 | */ |
Thierry FOURNIER | 9e2ef99 | 2015-02-25 13:51:19 +0100 | [diff] [blame] | 3716 | static enum rule_result |
Willy Tarreau | 987e3fb | 2015-04-04 01:09:08 +0200 | [diff] [blame] | 3717 | http_res_get_intercept_rule(struct proxy *px, struct list *rules, struct stream *s) |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 3718 | { |
Willy Tarreau | 9ad7bd4 | 2015-04-03 19:19:59 +0200 | [diff] [blame] | 3719 | struct session *sess = strm_sess(s); |
Willy Tarreau | 987e3fb | 2015-04-04 01:09:08 +0200 | [diff] [blame] | 3720 | struct http_txn *txn = s->txn; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 3721 | struct connection *cli_conn; |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 3722 | struct http_res_rule *rule; |
| 3723 | struct hdr_ctx ctx; |
| 3724 | |
Thierry FOURNIER | bc4c1ac | 2015-02-25 13:36:14 +0100 | [diff] [blame] | 3725 | /* If "the current_rule_list" match the executed rule list, we are in |
| 3726 | * resume condition. If a resume is needed it is always in the action |
| 3727 | * and never in the ACL or converters. In this case, we initialise the |
| 3728 | * current rule, and go to the action execution point. |
| 3729 | */ |
Willy Tarreau | 152b81e | 2015-04-20 13:26:17 +0200 | [diff] [blame] | 3730 | if (s->current_rule) { |
| 3731 | rule = s->current_rule; |
| 3732 | s->current_rule = NULL; |
| 3733 | if (s->current_rule_list == rules) |
| 3734 | goto resume_execution; |
Thierry FOURNIER | bc4c1ac | 2015-02-25 13:36:14 +0100 | [diff] [blame] | 3735 | } |
| 3736 | s->current_rule_list = rules; |
Willy Tarreau | 152b81e | 2015-04-20 13:26:17 +0200 | [diff] [blame] | 3737 | |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 3738 | list_for_each_entry(rule, rules, list) { |
| 3739 | if (rule->action >= HTTP_RES_ACT_MAX) |
| 3740 | continue; |
| 3741 | |
| 3742 | /* check optional condition */ |
| 3743 | if (rule->cond) { |
| 3744 | int ret; |
| 3745 | |
Willy Tarreau | 192252e | 2015-04-04 01:47:55 +0200 | [diff] [blame] | 3746 | ret = acl_exec_cond(rule->cond, px, sess, s, SMP_OPT_DIR_RES|SMP_OPT_FINAL); |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 3747 | ret = acl_pass(ret); |
| 3748 | |
| 3749 | if (rule->cond->pol == ACL_COND_UNLESS) |
| 3750 | ret = !ret; |
| 3751 | |
| 3752 | if (!ret) /* condition not matched */ |
| 3753 | continue; |
| 3754 | } |
| 3755 | |
Thierry FOURNIER | bc4c1ac | 2015-02-25 13:36:14 +0100 | [diff] [blame] | 3756 | resume_execution: |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 3757 | switch (rule->action) { |
| 3758 | case HTTP_RES_ACT_ALLOW: |
Thierry FOURNIER | 9e2ef99 | 2015-02-25 13:51:19 +0100 | [diff] [blame] | 3759 | return HTTP_RULE_RES_STOP; /* "allow" rules are OK */ |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 3760 | |
| 3761 | case HTTP_RES_ACT_DENY: |
| 3762 | txn->flags |= TX_SVDENY; |
Thierry FOURNIER | 9e2ef99 | 2015-02-25 13:51:19 +0100 | [diff] [blame] | 3763 | return HTTP_RULE_RES_STOP; |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 3764 | |
Willy Tarreau | f4c43c1 | 2013-06-11 17:01:13 +0200 | [diff] [blame] | 3765 | case HTTP_RES_ACT_SET_NICE: |
| 3766 | s->task->nice = rule->arg.nice; |
| 3767 | break; |
| 3768 | |
Willy Tarreau | 42cf39e | 2013-06-11 18:51:32 +0200 | [diff] [blame] | 3769 | case HTTP_RES_ACT_SET_TOS: |
Willy Tarreau | 9ad7bd4 | 2015-04-03 19:19:59 +0200 | [diff] [blame] | 3770 | if ((cli_conn = objt_conn(sess->origin)) && conn_ctrl_ready(cli_conn)) |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 3771 | inet_set_tos(cli_conn->t.sock.fd, cli_conn->addr.from, rule->arg.tos); |
Willy Tarreau | 42cf39e | 2013-06-11 18:51:32 +0200 | [diff] [blame] | 3772 | break; |
| 3773 | |
Willy Tarreau | 51347ed | 2013-06-11 19:34:13 +0200 | [diff] [blame] | 3774 | case HTTP_RES_ACT_SET_MARK: |
| 3775 | #ifdef SO_MARK |
Willy Tarreau | 9ad7bd4 | 2015-04-03 19:19:59 +0200 | [diff] [blame] | 3776 | if ((cli_conn = objt_conn(sess->origin)) && conn_ctrl_ready(cli_conn)) |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 3777 | setsockopt(cli_conn->t.sock.fd, SOL_SOCKET, SO_MARK, &rule->arg.mark, sizeof(rule->arg.mark)); |
Willy Tarreau | 51347ed | 2013-06-11 19:34:13 +0200 | [diff] [blame] | 3778 | #endif |
| 3779 | break; |
| 3780 | |
Willy Tarreau | 9a355ec | 2013-06-11 17:45:46 +0200 | [diff] [blame] | 3781 | case HTTP_RES_ACT_SET_LOGL: |
| 3782 | s->logs.level = rule->arg.loglevel; |
| 3783 | break; |
| 3784 | |
Sasha Pachev | 218f064 | 2014-06-16 12:05:59 -0600 | [diff] [blame] | 3785 | case HTTP_RES_ACT_REPLACE_HDR: |
| 3786 | case HTTP_RES_ACT_REPLACE_VAL: |
Thierry FOURNIER | 5a33ac7 | 2015-03-16 23:54:35 +0100 | [diff] [blame] | 3787 | if (http_transform_header(s, &txn->rsp, rule->arg.hdr_add.name, |
| 3788 | rule->arg.hdr_add.name_len, |
| 3789 | &rule->arg.hdr_add.fmt, |
| 3790 | &rule->arg.hdr_add.re, rule->action)) |
Thierry FOURNIER | 9e2ef99 | 2015-02-25 13:51:19 +0100 | [diff] [blame] | 3791 | return HTTP_RULE_RES_STOP; /* note: we should report an error here */ |
Sasha Pachev | 218f064 | 2014-06-16 12:05:59 -0600 | [diff] [blame] | 3792 | break; |
| 3793 | |
Thierry FOURNIER | dad3d1d | 2014-04-22 18:07:25 +0200 | [diff] [blame] | 3794 | case HTTP_RES_ACT_DEL_HDR: |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 3795 | ctx.idx = 0; |
| 3796 | /* remove all occurrences of the header */ |
| 3797 | while (http_find_header2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len, |
| 3798 | txn->rsp.chn->buf->p, &txn->hdr_idx, &ctx)) { |
| 3799 | http_remove_header2(&txn->rsp, &txn->hdr_idx, &ctx); |
| 3800 | } |
Willy Tarreau | 8560328 | 2015-01-21 20:39:27 +0100 | [diff] [blame] | 3801 | break; |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 3802 | |
Willy Tarreau | 8560328 | 2015-01-21 20:39:27 +0100 | [diff] [blame] | 3803 | case HTTP_RES_ACT_SET_HDR: |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 3804 | case HTTP_RES_ACT_ADD_HDR: |
| 3805 | chunk_printf(&trash, "%s: ", rule->arg.hdr_add.name); |
| 3806 | memcpy(trash.str, rule->arg.hdr_add.name, rule->arg.hdr_add.name_len); |
| 3807 | trash.len = rule->arg.hdr_add.name_len; |
| 3808 | trash.str[trash.len++] = ':'; |
| 3809 | trash.str[trash.len++] = ' '; |
| 3810 | trash.len += build_logline(s, trash.str + trash.len, trash.size - trash.len, &rule->arg.hdr_add.fmt); |
Willy Tarreau | 8560328 | 2015-01-21 20:39:27 +0100 | [diff] [blame] | 3811 | |
| 3812 | if (rule->action == HTTP_RES_ACT_SET_HDR) { |
| 3813 | /* remove all occurrences of the header */ |
| 3814 | ctx.idx = 0; |
| 3815 | while (http_find_header2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len, |
| 3816 | txn->rsp.chn->buf->p, &txn->hdr_idx, &ctx)) { |
| 3817 | http_remove_header2(&txn->rsp, &txn->hdr_idx, &ctx); |
| 3818 | } |
| 3819 | } |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 3820 | http_header_add_tail2(&txn->rsp, &txn->hdr_idx, trash.str, trash.len); |
| 3821 | break; |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 3822 | |
| 3823 | case HTTP_RES_ACT_DEL_ACL: |
| 3824 | case HTTP_RES_ACT_DEL_MAP: { |
| 3825 | struct pat_ref *ref; |
| 3826 | char *key; |
| 3827 | int len; |
| 3828 | |
| 3829 | /* collect reference */ |
| 3830 | ref = pat_ref_lookup(rule->arg.map.ref); |
| 3831 | if (!ref) |
| 3832 | continue; |
| 3833 | |
| 3834 | /* collect key */ |
| 3835 | len = build_logline(s, trash.str, trash.size, &rule->arg.map.key); |
| 3836 | key = trash.str; |
| 3837 | key[len] = '\0'; |
| 3838 | |
| 3839 | /* perform update */ |
| 3840 | /* returned code: 1=ok, 0=ko */ |
| 3841 | pat_ref_delete(ref, key); |
| 3842 | |
| 3843 | break; |
| 3844 | } |
| 3845 | |
| 3846 | case HTTP_RES_ACT_ADD_ACL: { |
| 3847 | struct pat_ref *ref; |
| 3848 | char *key; |
| 3849 | struct chunk *trash_key; |
| 3850 | int len; |
| 3851 | |
| 3852 | trash_key = get_trash_chunk(); |
| 3853 | |
| 3854 | /* collect reference */ |
| 3855 | ref = pat_ref_lookup(rule->arg.map.ref); |
| 3856 | if (!ref) |
| 3857 | continue; |
| 3858 | |
| 3859 | /* collect key */ |
| 3860 | len = build_logline(s, trash_key->str, trash_key->size, &rule->arg.map.key); |
| 3861 | key = trash_key->str; |
| 3862 | key[len] = '\0'; |
| 3863 | |
| 3864 | /* perform update */ |
| 3865 | /* check if the entry already exists */ |
| 3866 | if (pat_ref_find_elt(ref, key) == NULL) |
Thierry FOURNIER | e47e4e2 | 2014-04-28 11:18:57 +0200 | [diff] [blame] | 3867 | pat_ref_add(ref, key, NULL, NULL); |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 3868 | |
| 3869 | break; |
| 3870 | } |
| 3871 | |
| 3872 | case HTTP_RES_ACT_SET_MAP: { |
| 3873 | struct pat_ref *ref; |
| 3874 | char *key, *value; |
| 3875 | struct chunk *trash_key, *trash_value; |
| 3876 | int len; |
| 3877 | |
| 3878 | trash_key = get_trash_chunk(); |
| 3879 | trash_value = get_trash_chunk(); |
| 3880 | |
| 3881 | /* collect reference */ |
| 3882 | ref = pat_ref_lookup(rule->arg.map.ref); |
| 3883 | if (!ref) |
| 3884 | continue; |
| 3885 | |
| 3886 | /* collect key */ |
| 3887 | len = build_logline(s, trash_key->str, trash_key->size, &rule->arg.map.key); |
| 3888 | key = trash_key->str; |
| 3889 | key[len] = '\0'; |
| 3890 | |
| 3891 | /* collect value */ |
| 3892 | len = build_logline(s, trash_value->str, trash_value->size, &rule->arg.map.value); |
| 3893 | value = trash_value->str; |
| 3894 | value[len] = '\0'; |
| 3895 | |
| 3896 | /* perform update */ |
| 3897 | if (pat_ref_find_elt(ref, key) != NULL) |
| 3898 | /* update entry if it exists */ |
| 3899 | pat_ref_set(ref, key, value, NULL); |
| 3900 | else |
| 3901 | /* insert a new entry */ |
Thierry FOURNIER | e47e4e2 | 2014-04-28 11:18:57 +0200 | [diff] [blame] | 3902 | pat_ref_add(ref, key, value, NULL); |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 3903 | |
| 3904 | break; |
| 3905 | } |
William Lallemand | 73025dd | 2014-04-24 14:38:37 +0200 | [diff] [blame] | 3906 | |
Willy Tarreau | 51d861a | 2015-05-22 17:30:48 +0200 | [diff] [blame] | 3907 | case HTTP_RES_ACT_REDIR: |
| 3908 | if (!http_apply_redirect_rule(rule->arg.redir, s, txn)) |
| 3909 | return HTTP_RULE_RES_BADREQ; |
| 3910 | return HTTP_RULE_RES_DONE; |
| 3911 | |
William Lallemand | 73025dd | 2014-04-24 14:38:37 +0200 | [diff] [blame] | 3912 | case HTTP_RES_ACT_CUSTOM_CONT: |
Willy Tarreau | 987e3fb | 2015-04-04 01:09:08 +0200 | [diff] [blame] | 3913 | if (!rule->action_ptr(rule, px, s)) { |
Willy Tarreau | 152b81e | 2015-04-20 13:26:17 +0200 | [diff] [blame] | 3914 | s->current_rule = rule; |
Thierry FOURNIER | bc4c1ac | 2015-02-25 13:36:14 +0100 | [diff] [blame] | 3915 | return HTTP_RULE_RES_YIELD; |
| 3916 | } |
William Lallemand | 73025dd | 2014-04-24 14:38:37 +0200 | [diff] [blame] | 3917 | break; |
| 3918 | |
| 3919 | case HTTP_RES_ACT_CUSTOM_STOP: |
Willy Tarreau | 987e3fb | 2015-04-04 01:09:08 +0200 | [diff] [blame] | 3920 | rule->action_ptr(rule, px, s); |
Thierry FOURNIER | 9e2ef99 | 2015-02-25 13:51:19 +0100 | [diff] [blame] | 3921 | return HTTP_RULE_RES_STOP; |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 3922 | } |
| 3923 | } |
| 3924 | |
| 3925 | /* we reached the end of the rules, nothing to report */ |
Thierry FOURNIER | 9e2ef99 | 2015-02-25 13:51:19 +0100 | [diff] [blame] | 3926 | return HTTP_RULE_RES_CONT; |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 3927 | } |
| 3928 | |
| 3929 | |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 3930 | /* Perform an HTTP redirect based on the information in <rule>. The function |
| 3931 | * returns non-zero on success, or zero in case of a, irrecoverable error such |
| 3932 | * as too large a request to build a valid response. |
| 3933 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 3934 | static int http_apply_redirect_rule(struct redirect_rule *rule, struct stream *s, struct http_txn *txn) |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 3935 | { |
Willy Tarreau | b329a31 | 2015-05-22 16:27:37 +0200 | [diff] [blame] | 3936 | struct http_msg *req = &txn->req; |
| 3937 | struct http_msg *res = &txn->rsp; |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 3938 | const char *msg_fmt; |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 3939 | const char *location; |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 3940 | |
| 3941 | /* build redirect message */ |
| 3942 | switch(rule->code) { |
Yves Lafon | 3e8d1ae | 2013-03-11 11:06:05 -0400 | [diff] [blame] | 3943 | case 308: |
| 3944 | msg_fmt = HTTP_308; |
| 3945 | break; |
| 3946 | case 307: |
| 3947 | msg_fmt = HTTP_307; |
| 3948 | break; |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 3949 | case 303: |
| 3950 | msg_fmt = HTTP_303; |
| 3951 | break; |
| 3952 | case 301: |
| 3953 | msg_fmt = HTTP_301; |
| 3954 | break; |
| 3955 | case 302: |
| 3956 | default: |
| 3957 | msg_fmt = HTTP_302; |
| 3958 | break; |
| 3959 | } |
| 3960 | |
| 3961 | if (unlikely(!chunk_strcpy(&trash, msg_fmt))) |
| 3962 | return 0; |
| 3963 | |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 3964 | location = trash.str + trash.len; |
| 3965 | |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 3966 | switch(rule->type) { |
| 3967 | case REDIRECT_TYPE_SCHEME: { |
| 3968 | const char *path; |
| 3969 | const char *host; |
| 3970 | struct hdr_ctx ctx; |
| 3971 | int pathlen; |
| 3972 | int hostlen; |
| 3973 | |
| 3974 | host = ""; |
| 3975 | hostlen = 0; |
| 3976 | ctx.idx = 0; |
Willy Tarreau | b329a31 | 2015-05-22 16:27:37 +0200 | [diff] [blame] | 3977 | if (http_find_header2("Host", 4, req->chn->buf->p, &txn->hdr_idx, &ctx)) { |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 3978 | host = ctx.line + ctx.val; |
| 3979 | hostlen = ctx.vlen; |
| 3980 | } |
| 3981 | |
| 3982 | path = http_get_path(txn); |
| 3983 | /* build message using path */ |
| 3984 | if (path) { |
Willy Tarreau | b329a31 | 2015-05-22 16:27:37 +0200 | [diff] [blame] | 3985 | pathlen = req->sl.rq.u_l + (req->chn->buf->p + req->sl.rq.u) - path; |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 3986 | if (rule->flags & REDIRECT_FLAG_DROP_QS) { |
| 3987 | int qs = 0; |
| 3988 | while (qs < pathlen) { |
| 3989 | if (path[qs] == '?') { |
| 3990 | pathlen = qs; |
| 3991 | break; |
| 3992 | } |
| 3993 | qs++; |
| 3994 | } |
| 3995 | } |
| 3996 | } else { |
| 3997 | path = "/"; |
| 3998 | pathlen = 1; |
| 3999 | } |
| 4000 | |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 4001 | if (rule->rdr_str) { /* this is an old "redirect" rule */ |
| 4002 | /* check if we can add scheme + "://" + host + path */ |
| 4003 | if (trash.len + rule->rdr_len + 3 + hostlen + pathlen > trash.size - 4) |
| 4004 | return 0; |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 4005 | |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 4006 | /* add scheme */ |
| 4007 | memcpy(trash.str + trash.len, rule->rdr_str, rule->rdr_len); |
| 4008 | trash.len += rule->rdr_len; |
| 4009 | } |
| 4010 | else { |
| 4011 | /* add scheme with executing log format */ |
| 4012 | trash.len += build_logline(s, trash.str + trash.len, trash.size - trash.len, &rule->rdr_fmt); |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 4013 | |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 4014 | /* check if we can add scheme + "://" + host + path */ |
| 4015 | if (trash.len + 3 + hostlen + pathlen > trash.size - 4) |
| 4016 | return 0; |
| 4017 | } |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 4018 | /* add "://" */ |
| 4019 | memcpy(trash.str + trash.len, "://", 3); |
| 4020 | trash.len += 3; |
| 4021 | |
| 4022 | /* add host */ |
| 4023 | memcpy(trash.str + trash.len, host, hostlen); |
| 4024 | trash.len += hostlen; |
| 4025 | |
| 4026 | /* add path */ |
| 4027 | memcpy(trash.str + trash.len, path, pathlen); |
| 4028 | trash.len += pathlen; |
| 4029 | |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 4030 | /* append a slash at the end of the location if needed and missing */ |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 4031 | if (trash.len && trash.str[trash.len - 1] != '/' && |
| 4032 | (rule->flags & REDIRECT_FLAG_APPEND_SLASH)) { |
| 4033 | if (trash.len > trash.size - 5) |
| 4034 | return 0; |
| 4035 | trash.str[trash.len] = '/'; |
| 4036 | trash.len++; |
| 4037 | } |
| 4038 | |
| 4039 | break; |
| 4040 | } |
| 4041 | case REDIRECT_TYPE_PREFIX: { |
| 4042 | const char *path; |
| 4043 | int pathlen; |
| 4044 | |
| 4045 | path = http_get_path(txn); |
| 4046 | /* build message using path */ |
| 4047 | if (path) { |
Willy Tarreau | b329a31 | 2015-05-22 16:27:37 +0200 | [diff] [blame] | 4048 | pathlen = req->sl.rq.u_l + (req->chn->buf->p + req->sl.rq.u) - path; |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 4049 | if (rule->flags & REDIRECT_FLAG_DROP_QS) { |
| 4050 | int qs = 0; |
| 4051 | while (qs < pathlen) { |
| 4052 | if (path[qs] == '?') { |
| 4053 | pathlen = qs; |
| 4054 | break; |
| 4055 | } |
| 4056 | qs++; |
| 4057 | } |
| 4058 | } |
| 4059 | } else { |
| 4060 | path = "/"; |
| 4061 | pathlen = 1; |
| 4062 | } |
| 4063 | |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 4064 | if (rule->rdr_str) { /* this is an old "redirect" rule */ |
| 4065 | if (trash.len + rule->rdr_len + pathlen > trash.size - 4) |
| 4066 | return 0; |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 4067 | |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 4068 | /* add prefix. Note that if prefix == "/", we don't want to |
| 4069 | * add anything, otherwise it makes it hard for the user to |
| 4070 | * configure a self-redirection. |
| 4071 | */ |
| 4072 | if (rule->rdr_len != 1 || *rule->rdr_str != '/') { |
| 4073 | memcpy(trash.str + trash.len, rule->rdr_str, rule->rdr_len); |
| 4074 | trash.len += rule->rdr_len; |
| 4075 | } |
| 4076 | } |
| 4077 | else { |
| 4078 | /* add prefix with executing log format */ |
| 4079 | trash.len += build_logline(s, trash.str + trash.len, trash.size - trash.len, &rule->rdr_fmt); |
| 4080 | |
| 4081 | /* Check length */ |
| 4082 | if (trash.len + pathlen > trash.size - 4) |
| 4083 | return 0; |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 4084 | } |
| 4085 | |
| 4086 | /* add path */ |
| 4087 | memcpy(trash.str + trash.len, path, pathlen); |
| 4088 | trash.len += pathlen; |
| 4089 | |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 4090 | /* append a slash at the end of the location if needed and missing */ |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 4091 | if (trash.len && trash.str[trash.len - 1] != '/' && |
| 4092 | (rule->flags & REDIRECT_FLAG_APPEND_SLASH)) { |
| 4093 | if (trash.len > trash.size - 5) |
| 4094 | return 0; |
| 4095 | trash.str[trash.len] = '/'; |
| 4096 | trash.len++; |
| 4097 | } |
| 4098 | |
| 4099 | break; |
| 4100 | } |
| 4101 | case REDIRECT_TYPE_LOCATION: |
| 4102 | default: |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 4103 | if (rule->rdr_str) { /* this is an old "redirect" rule */ |
| 4104 | if (trash.len + rule->rdr_len > trash.size - 4) |
| 4105 | return 0; |
| 4106 | |
| 4107 | /* add location */ |
| 4108 | memcpy(trash.str + trash.len, rule->rdr_str, rule->rdr_len); |
| 4109 | trash.len += rule->rdr_len; |
| 4110 | } |
| 4111 | else { |
| 4112 | /* add location with executing log format */ |
| 4113 | trash.len += build_logline(s, trash.str + trash.len, trash.size - trash.len, &rule->rdr_fmt); |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 4114 | |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 4115 | /* Check left length */ |
| 4116 | if (trash.len > trash.size - 4) |
| 4117 | return 0; |
| 4118 | } |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 4119 | break; |
| 4120 | } |
| 4121 | |
| 4122 | if (rule->cookie_len) { |
| 4123 | memcpy(trash.str + trash.len, "\r\nSet-Cookie: ", 14); |
| 4124 | trash.len += 14; |
| 4125 | memcpy(trash.str + trash.len, rule->cookie_str, rule->cookie_len); |
| 4126 | trash.len += rule->cookie_len; |
| 4127 | memcpy(trash.str + trash.len, "\r\n", 2); |
| 4128 | trash.len += 2; |
| 4129 | } |
| 4130 | |
| 4131 | /* add end of headers and the keep-alive/close status. |
| 4132 | * We may choose to set keep-alive if the Location begins |
| 4133 | * with a slash, because the client will come back to the |
| 4134 | * same server. |
| 4135 | */ |
| 4136 | txn->status = rule->code; |
| 4137 | /* let's log the request time */ |
| 4138 | s->logs.tv_request = now; |
| 4139 | |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 4140 | if (*location == '/' && |
Willy Tarreau | b329a31 | 2015-05-22 16:27:37 +0200 | [diff] [blame] | 4141 | (req->flags & HTTP_MSGF_XFER_LEN) && |
Willy Tarreau | 2de8a50 | 2015-05-28 17:23:54 +0200 | [diff] [blame] | 4142 | ((!(req->flags & HTTP_MSGF_TE_CHNK) && !req->body_len) || (req->msg_state == HTTP_MSG_DONE)) && |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 4143 | ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL || |
| 4144 | (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL)) { |
| 4145 | /* keep-alive possible */ |
Willy Tarreau | b329a31 | 2015-05-22 16:27:37 +0200 | [diff] [blame] | 4146 | if (!(req->flags & HTTP_MSGF_VER_11)) { |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 4147 | if (unlikely(txn->flags & TX_USE_PX_CONN)) { |
| 4148 | memcpy(trash.str + trash.len, "\r\nProxy-Connection: keep-alive", 30); |
| 4149 | trash.len += 30; |
| 4150 | } else { |
| 4151 | memcpy(trash.str + trash.len, "\r\nConnection: keep-alive", 24); |
| 4152 | trash.len += 24; |
| 4153 | } |
| 4154 | } |
| 4155 | memcpy(trash.str + trash.len, "\r\n\r\n", 4); |
| 4156 | trash.len += 4; |
Willy Tarreau | b329a31 | 2015-05-22 16:27:37 +0200 | [diff] [blame] | 4157 | bo_inject(res->chn, trash.str, trash.len); |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 4158 | /* "eat" the request */ |
Willy Tarreau | b329a31 | 2015-05-22 16:27:37 +0200 | [diff] [blame] | 4159 | bi_fast_delete(req->chn->buf, req->sov); |
| 4160 | req->next -= req->sov; |
| 4161 | req->sov = 0; |
| 4162 | s->req.analysers = AN_REQ_HTTP_XFER_BODY; |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 4163 | s->res.analysers = AN_RES_HTTP_XFER_BODY; |
Willy Tarreau | b329a31 | 2015-05-22 16:27:37 +0200 | [diff] [blame] | 4164 | req->msg_state = HTTP_MSG_CLOSED; |
| 4165 | res->msg_state = HTTP_MSG_DONE; |
Willy Tarreau | 51d861a | 2015-05-22 17:30:48 +0200 | [diff] [blame] | 4166 | /* Trim any possible response */ |
| 4167 | res->chn->buf->i = 0; |
| 4168 | res->next = res->sov = 0; |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 4169 | } else { |
| 4170 | /* keep-alive not possible */ |
| 4171 | if (unlikely(txn->flags & TX_USE_PX_CONN)) { |
| 4172 | memcpy(trash.str + trash.len, "\r\nProxy-Connection: close\r\n\r\n", 29); |
| 4173 | trash.len += 29; |
| 4174 | } else { |
| 4175 | memcpy(trash.str + trash.len, "\r\nConnection: close\r\n\r\n", 23); |
| 4176 | trash.len += 23; |
| 4177 | } |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 4178 | stream_int_retnclose(&s->si[0], &trash); |
Willy Tarreau | b329a31 | 2015-05-22 16:27:37 +0200 | [diff] [blame] | 4179 | req->chn->analysers = 0; |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 4180 | } |
| 4181 | |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 4182 | if (!(s->flags & SF_ERR_MASK)) |
| 4183 | s->flags |= SF_ERR_LOCAL; |
| 4184 | if (!(s->flags & SF_FINST_MASK)) |
| 4185 | s->flags |= SF_FINST_R; |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 4186 | |
| 4187 | return 1; |
| 4188 | } |
| 4189 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 4190 | /* This stream analyser runs all HTTP request processing which is common to |
| 4191 | * frontends and backends, which means blocking ACLs, filters, connection-close, |
| 4192 | * reqadd, stats and redirects. This is performed for the designated proxy. |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 4193 | * It returns 1 if the processing can continue on next analysers, or zero if it |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 4194 | * either needs more data or wants to immediately abort the request (eg: deny, |
| 4195 | * error, ...). |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 4196 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 4197 | int http_process_req_common(struct stream *s, struct channel *req, int an_bit, struct proxy *px) |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 4198 | { |
Willy Tarreau | fb0afa7 | 2015-04-03 14:46:27 +0200 | [diff] [blame] | 4199 | struct session *sess = s->sess; |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 4200 | struct http_txn *txn = s->txn; |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 4201 | struct http_msg *msg = &txn->req; |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 4202 | struct redirect_rule *rule; |
Willy Tarreau | f4f0412 | 2010-01-28 18:10:50 +0100 | [diff] [blame] | 4203 | struct cond_wordlist *wl; |
Willy Tarreau | 0b74833 | 2014-04-29 00:13:29 +0200 | [diff] [blame] | 4204 | enum rule_result verdict; |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 4205 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 4206 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) { |
Willy Tarreau | 51aecc7 | 2009-07-12 09:47:04 +0200 | [diff] [blame] | 4207 | /* we need more data */ |
Thierry FOURNIER | bc4c1ac | 2015-02-25 13:36:14 +0100 | [diff] [blame] | 4208 | goto return_prx_yield; |
Willy Tarreau | 51aecc7 | 2009-07-12 09:47:04 +0200 | [diff] [blame] | 4209 | } |
| 4210 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 4211 | DPRINTF(stderr,"[%u] %s: stream=%p b=%p, exp(r,w)=%u,%u bf=%08x bh=%d analysers=%02x\n", |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 4212 | now_ms, __FUNCTION__, |
| 4213 | s, |
| 4214 | req, |
| 4215 | req->rex, req->wex, |
| 4216 | req->flags, |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 4217 | req->buf->i, |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 4218 | req->analysers); |
| 4219 | |
Willy Tarreau | 6541083 | 2014-04-28 21:25:43 +0200 | [diff] [blame] | 4220 | /* just in case we have some per-backend tracking */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 4221 | stream_inc_be_http_req_ctr(s); |
Willy Tarreau | 6541083 | 2014-04-28 21:25:43 +0200 | [diff] [blame] | 4222 | |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 4223 | /* evaluate http-request rules */ |
Willy Tarreau | 0b74833 | 2014-04-29 00:13:29 +0200 | [diff] [blame] | 4224 | if (!LIST_ISEMPTY(&px->http_req_rules)) { |
Willy Tarreau | 987e3fb | 2015-04-04 01:09:08 +0200 | [diff] [blame] | 4225 | verdict = http_req_get_intercept_rule(px, &px->http_req_rules, s); |
Willy Tarreau | 5142594 | 2010-02-01 10:40:19 +0100 | [diff] [blame] | 4226 | |
Willy Tarreau | 0b74833 | 2014-04-29 00:13:29 +0200 | [diff] [blame] | 4227 | switch (verdict) { |
Thierry FOURNIER | bc4c1ac | 2015-02-25 13:36:14 +0100 | [diff] [blame] | 4228 | case HTTP_RULE_RES_YIELD: /* some data miss, call the function later. */ |
| 4229 | goto return_prx_yield; |
| 4230 | |
Willy Tarreau | 0b74833 | 2014-04-29 00:13:29 +0200 | [diff] [blame] | 4231 | case HTTP_RULE_RES_CONT: |
| 4232 | case HTTP_RULE_RES_STOP: /* nothing to do */ |
| 4233 | break; |
Willy Tarreau | 5254259 | 2014-04-28 18:33:26 +0200 | [diff] [blame] | 4234 | |
Willy Tarreau | 0b74833 | 2014-04-29 00:13:29 +0200 | [diff] [blame] | 4235 | case HTTP_RULE_RES_DENY: /* deny or tarpit */ |
| 4236 | if (txn->flags & TX_CLTARPIT) |
| 4237 | goto tarpit; |
| 4238 | goto deny; |
Willy Tarreau | 5254259 | 2014-04-28 18:33:26 +0200 | [diff] [blame] | 4239 | |
Willy Tarreau | 0b74833 | 2014-04-29 00:13:29 +0200 | [diff] [blame] | 4240 | case HTTP_RULE_RES_ABRT: /* abort request, response already sent. Eg: auth */ |
| 4241 | goto return_prx_cond; |
Willy Tarreau | 5254259 | 2014-04-28 18:33:26 +0200 | [diff] [blame] | 4242 | |
Willy Tarreau | 0b74833 | 2014-04-29 00:13:29 +0200 | [diff] [blame] | 4243 | case HTTP_RULE_RES_DONE: /* OK, but terminate request processing (eg: redirect) */ |
Willy Tarreau | 5254259 | 2014-04-28 18:33:26 +0200 | [diff] [blame] | 4244 | goto done; |
| 4245 | |
Willy Tarreau | 0b74833 | 2014-04-29 00:13:29 +0200 | [diff] [blame] | 4246 | case HTTP_RULE_RES_BADREQ: /* failed with a bad request */ |
| 4247 | goto return_bad_req; |
| 4248 | } |
Willy Tarreau | bbba2a8 | 2014-04-28 13:57:26 +0200 | [diff] [blame] | 4249 | } |
| 4250 | |
Willy Tarreau | 5254259 | 2014-04-28 18:33:26 +0200 | [diff] [blame] | 4251 | /* OK at this stage, we know that the request was accepted according to |
| 4252 | * the http-request rules, we can check for the stats. Note that the |
| 4253 | * URI is detected *before* the req* rules in order not to be affected |
| 4254 | * by a possible reqrep, while they are processed *after* so that a |
| 4255 | * reqdeny can still block them. This clearly needs to change in 1.6! |
| 4256 | */ |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 4257 | if (stats_check_uri(&s->si[1], txn, px)) { |
Willy Tarreau | 5254259 | 2014-04-28 18:33:26 +0200 | [diff] [blame] | 4258 | s->target = &http_stats_applet.obj_type; |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 4259 | if (unlikely(!stream_int_register_handler(&s->si[1], objt_applet(s->target)))) { |
Willy Tarreau | 5254259 | 2014-04-28 18:33:26 +0200 | [diff] [blame] | 4260 | txn->status = 500; |
| 4261 | s->logs.tv_request = now; |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 4262 | stream_int_retnclose(&s->si[0], http_error_message(s, HTTP_ERR_500)); |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 4263 | |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 4264 | if (!(s->flags & SF_ERR_MASK)) |
| 4265 | s->flags |= SF_ERR_RESOURCE; |
Willy Tarreau | 5254259 | 2014-04-28 18:33:26 +0200 | [diff] [blame] | 4266 | goto return_prx_cond; |
| 4267 | } |
| 4268 | |
| 4269 | /* parse the whole stats request and extract the relevant information */ |
| 4270 | http_handle_stats(s, req); |
Willy Tarreau | 987e3fb | 2015-04-04 01:09:08 +0200 | [diff] [blame] | 4271 | verdict = http_req_get_intercept_rule(px, &px->uri_auth->http_req_rules, s); |
Willy Tarreau | 0b74833 | 2014-04-29 00:13:29 +0200 | [diff] [blame] | 4272 | /* not all actions implemented: deny, allow, auth */ |
Willy Tarreau | bbba2a8 | 2014-04-28 13:57:26 +0200 | [diff] [blame] | 4273 | |
Willy Tarreau | 0b74833 | 2014-04-29 00:13:29 +0200 | [diff] [blame] | 4274 | if (verdict == HTTP_RULE_RES_DENY) /* stats http-request deny */ |
| 4275 | goto deny; |
Willy Tarreau | 5254259 | 2014-04-28 18:33:26 +0200 | [diff] [blame] | 4276 | |
Willy Tarreau | 0b74833 | 2014-04-29 00:13:29 +0200 | [diff] [blame] | 4277 | if (verdict == HTTP_RULE_RES_ABRT) /* stats auth / stats http-request auth */ |
| 4278 | goto return_prx_cond; |
Krzysztof Piotr Oledzki | 59bb218 | 2010-01-29 17:58:21 +0100 | [diff] [blame] | 4279 | } |
| 4280 | |
Willy Tarreau | bbba2a8 | 2014-04-28 13:57:26 +0200 | [diff] [blame] | 4281 | /* evaluate the req* rules except reqadd */ |
| 4282 | if (px->req_exp != NULL) { |
Willy Tarreau | 6c123b1 | 2010-01-28 20:22:06 +0100 | [diff] [blame] | 4283 | if (apply_filters_to_request(s, req, px) < 0) |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 4284 | goto return_bad_req; |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 4285 | |
Willy Tarreau | bbba2a8 | 2014-04-28 13:57:26 +0200 | [diff] [blame] | 4286 | if (txn->flags & TX_CLDENY) |
| 4287 | goto deny; |
Willy Tarreau | c465fd7 | 2009-08-31 00:17:18 +0200 | [diff] [blame] | 4288 | |
Willy Tarreau | bbba2a8 | 2014-04-28 13:57:26 +0200 | [diff] [blame] | 4289 | if (txn->flags & TX_CLTARPIT) |
| 4290 | goto tarpit; |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 4291 | } |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 4292 | |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 4293 | /* add request headers from the rule sets in the same order */ |
| 4294 | list_for_each_entry(wl, &px->req_add, list) { |
| 4295 | if (wl->cond) { |
Willy Tarreau | 192252e | 2015-04-04 01:47:55 +0200 | [diff] [blame] | 4296 | int ret = acl_exec_cond(wl->cond, px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL); |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 4297 | ret = acl_pass(ret); |
| 4298 | if (((struct acl_cond *)wl->cond)->pol == ACL_COND_UNLESS) |
| 4299 | ret = !ret; |
| 4300 | if (!ret) |
| 4301 | continue; |
| 4302 | } |
| 4303 | |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 4304 | if (unlikely(http_header_add_tail(&txn->req, &txn->hdr_idx, wl->s) < 0)) |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 4305 | goto return_bad_req; |
Willy Tarreau | 81499eb | 2012-12-27 12:19:02 +0100 | [diff] [blame] | 4306 | } |
| 4307 | |
Willy Tarreau | 5254259 | 2014-04-28 18:33:26 +0200 | [diff] [blame] | 4308 | |
| 4309 | /* Proceed with the stats now. */ |
Willy Tarreau | 414e9bb | 2013-11-23 00:30:38 +0100 | [diff] [blame] | 4310 | if (unlikely(objt_applet(s->target) == &http_stats_applet)) { |
Willy Tarreau | 1facd6d | 2012-12-22 22:03:39 +0100 | [diff] [blame] | 4311 | /* process the stats request now */ |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 4312 | if (sess->fe == s->be) /* report it if the request was intercepted by the frontend */ |
| 4313 | sess->fe->fe_counters.intercepted_req++; |
Willy Tarreau | 347a35d | 2013-11-22 17:51:09 +0100 | [diff] [blame] | 4314 | |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 4315 | if (!(s->flags & SF_ERR_MASK)) // this is not really an error but it is |
| 4316 | s->flags |= SF_ERR_LOCAL; // to mark that it comes from the proxy |
| 4317 | if (!(s->flags & SF_FINST_MASK)) |
| 4318 | s->flags |= SF_FINST_R; |
Willy Tarreau | 347a35d | 2013-11-22 17:51:09 +0100 | [diff] [blame] | 4319 | |
Willy Tarreau | 70730dd | 2014-04-24 18:06:27 +0200 | [diff] [blame] | 4320 | /* we may want to compress the stats page */ |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 4321 | if (sess->fe->comp || s->be->comp) |
Willy Tarreau | 70730dd | 2014-04-24 18:06:27 +0200 | [diff] [blame] | 4322 | select_compression_request_header(s, req->buf); |
| 4323 | |
| 4324 | /* enable the minimally required analyzers to handle keep-alive and compression on the HTTP response */ |
Willy Tarreau | 5506e3f | 2014-11-20 22:23:10 +0100 | [diff] [blame] | 4325 | req->analysers = (req->analysers & AN_REQ_HTTP_BODY) | AN_REQ_HTTP_XFER_BODY; |
Willy Tarreau | bbba2a8 | 2014-04-28 13:57:26 +0200 | [diff] [blame] | 4326 | goto done; |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 4327 | } |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 4328 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 4329 | /* check whether we have some ACLs set to redirect this request */ |
| 4330 | list_for_each_entry(rule, &px->redirect_rules, list) { |
Willy Tarreau | f285f54 | 2010-01-03 20:03:03 +0100 | [diff] [blame] | 4331 | if (rule->cond) { |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 4332 | int ret; |
| 4333 | |
Willy Tarreau | 192252e | 2015-04-04 01:47:55 +0200 | [diff] [blame] | 4334 | ret = acl_exec_cond(rule->cond, px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL); |
Willy Tarreau | f285f54 | 2010-01-03 20:03:03 +0100 | [diff] [blame] | 4335 | ret = acl_pass(ret); |
| 4336 | if (rule->cond->pol == ACL_COND_UNLESS) |
| 4337 | ret = !ret; |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 4338 | if (!ret) |
| 4339 | continue; |
Willy Tarreau | f285f54 | 2010-01-03 20:03:03 +0100 | [diff] [blame] | 4340 | } |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 4341 | if (!http_apply_redirect_rule(rule, s, txn)) |
| 4342 | goto return_bad_req; |
Willy Tarreau | bbba2a8 | 2014-04-28 13:57:26 +0200 | [diff] [blame] | 4343 | goto done; |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 4344 | } |
Willy Tarreau | 55ea757 | 2007-06-17 19:56:27 +0200 | [diff] [blame] | 4345 | |
Willy Tarreau | 2be3939 | 2010-01-03 17:24:51 +0100 | [diff] [blame] | 4346 | /* POST requests may be accompanied with an "Expect: 100-Continue" header. |
| 4347 | * If this happens, then the data will not come immediately, so we must |
| 4348 | * send all what we have without waiting. Note that due to the small gain |
| 4349 | * in waiting for the body of the request, it's easier to simply put the |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 4350 | * CF_SEND_DONTWAIT flag any time. It's a one-shot flag so it will remove |
Willy Tarreau | 2be3939 | 2010-01-03 17:24:51 +0100 | [diff] [blame] | 4351 | * itself once used. |
| 4352 | */ |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 4353 | req->flags |= CF_SEND_DONTWAIT; |
Willy Tarreau | 2be3939 | 2010-01-03 17:24:51 +0100 | [diff] [blame] | 4354 | |
Willy Tarreau | bbba2a8 | 2014-04-28 13:57:26 +0200 | [diff] [blame] | 4355 | done: /* done with this analyser, continue with next ones that the calling |
| 4356 | * points will have set, if any. |
| 4357 | */ |
Willy Tarreau | bbba2a8 | 2014-04-28 13:57:26 +0200 | [diff] [blame] | 4358 | req->analyse_exp = TICK_ETERNITY; |
Thierry FOURNIER | 7566e30 | 2014-08-22 06:55:26 +0200 | [diff] [blame] | 4359 | done_without_exp: /* done with this analyser, but dont reset the analyse_exp. */ |
| 4360 | req->analysers &= ~an_bit; |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 4361 | return 1; |
Willy Tarreau | 1138281 | 2008-07-09 16:18:21 +0200 | [diff] [blame] | 4362 | |
Willy Tarreau | bbba2a8 | 2014-04-28 13:57:26 +0200 | [diff] [blame] | 4363 | tarpit: |
| 4364 | /* When a connection is tarpitted, we use the tarpit timeout, |
| 4365 | * which may be the same as the connect timeout if unspecified. |
| 4366 | * If unset, then set it to zero because we really want it to |
| 4367 | * eventually expire. We build the tarpit as an analyser. |
| 4368 | */ |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 4369 | channel_erase(&s->req); |
Willy Tarreau | bbba2a8 | 2014-04-28 13:57:26 +0200 | [diff] [blame] | 4370 | |
| 4371 | /* wipe the request out so that we can drop the connection early |
| 4372 | * if the client closes first. |
| 4373 | */ |
| 4374 | channel_dont_connect(req); |
| 4375 | req->analysers = 0; /* remove switching rules etc... */ |
| 4376 | req->analysers |= AN_REQ_HTTP_TARPIT; |
| 4377 | req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.tarpit); |
| 4378 | if (!req->analyse_exp) |
| 4379 | req->analyse_exp = tick_add(now_ms, 0); |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 4380 | stream_inc_http_err_ctr(s); |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 4381 | sess->fe->fe_counters.denied_req++; |
| 4382 | if (sess->fe != s->be) |
Willy Tarreau | bbba2a8 | 2014-04-28 13:57:26 +0200 | [diff] [blame] | 4383 | s->be->be_counters.denied_req++; |
Willy Tarreau | fb0afa7 | 2015-04-03 14:46:27 +0200 | [diff] [blame] | 4384 | if (sess->listener->counters) |
| 4385 | sess->listener->counters->denied_req++; |
Thierry FOURNIER | 7566e30 | 2014-08-22 06:55:26 +0200 | [diff] [blame] | 4386 | goto done_without_exp; |
Willy Tarreau | bbba2a8 | 2014-04-28 13:57:26 +0200 | [diff] [blame] | 4387 | |
| 4388 | deny: /* this request was blocked (denied) */ |
Willy Tarreau | 0b74833 | 2014-04-29 00:13:29 +0200 | [diff] [blame] | 4389 | txn->flags |= TX_CLDENY; |
CJ Ess | 108b1dd | 2015-04-07 12:03:37 -0400 | [diff] [blame] | 4390 | txn->status = http_err_codes[txn->rule_deny_status]; |
Willy Tarreau | bbba2a8 | 2014-04-28 13:57:26 +0200 | [diff] [blame] | 4391 | s->logs.tv_request = now; |
CJ Ess | 108b1dd | 2015-04-07 12:03:37 -0400 | [diff] [blame] | 4392 | stream_int_retnclose(&s->si[0], http_error_message(s, txn->rule_deny_status)); |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 4393 | stream_inc_http_err_ctr(s); |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 4394 | sess->fe->fe_counters.denied_req++; |
| 4395 | if (sess->fe != s->be) |
Willy Tarreau | bbba2a8 | 2014-04-28 13:57:26 +0200 | [diff] [blame] | 4396 | s->be->be_counters.denied_req++; |
Willy Tarreau | fb0afa7 | 2015-04-03 14:46:27 +0200 | [diff] [blame] | 4397 | if (sess->listener->counters) |
| 4398 | sess->listener->counters->denied_req++; |
Willy Tarreau | bbba2a8 | 2014-04-28 13:57:26 +0200 | [diff] [blame] | 4399 | goto return_prx_cond; |
| 4400 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 4401 | return_bad_req: |
| 4402 | /* We centralize bad requests processing here */ |
| 4403 | if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) { |
| 4404 | /* we detected a parsing error. We want to archive this request |
| 4405 | * in the dedicated proxy area for later troubleshooting. |
| 4406 | */ |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 4407 | http_capture_bad_message(&sess->fe->invalid_req, s, msg, msg->msg_state, sess->fe); |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 4408 | } |
Willy Tarreau | 55ea757 | 2007-06-17 19:56:27 +0200 | [diff] [blame] | 4409 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 4410 | txn->req.msg_state = HTTP_MSG_ERROR; |
| 4411 | txn->status = 400; |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 4412 | stream_int_retnclose(&s->si[0], http_error_message(s, HTTP_ERR_400)); |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 4413 | |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 4414 | sess->fe->fe_counters.failed_req++; |
Willy Tarreau | fb0afa7 | 2015-04-03 14:46:27 +0200 | [diff] [blame] | 4415 | if (sess->listener->counters) |
| 4416 | sess->listener->counters->failed_req++; |
Willy Tarreau | 6e4261e | 2007-09-18 18:36:05 +0200 | [diff] [blame] | 4417 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 4418 | return_prx_cond: |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 4419 | if (!(s->flags & SF_ERR_MASK)) |
| 4420 | s->flags |= SF_ERR_PRXCOND; |
| 4421 | if (!(s->flags & SF_FINST_MASK)) |
| 4422 | s->flags |= SF_FINST_R; |
Willy Tarreau | f1221aa | 2006-12-17 22:14:12 +0100 | [diff] [blame] | 4423 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 4424 | req->analysers = 0; |
| 4425 | req->analyse_exp = TICK_ETERNITY; |
| 4426 | return 0; |
Thierry FOURNIER | bc4c1ac | 2015-02-25 13:36:14 +0100 | [diff] [blame] | 4427 | |
| 4428 | return_prx_yield: |
| 4429 | channel_dont_connect(req); |
| 4430 | return 0; |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 4431 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4432 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 4433 | /* This function performs all the processing enabled for the current request. |
| 4434 | * It returns 1 if the processing can continue on next analysers, or zero if it |
| 4435 | * needs more data, encounters an error, or wants to immediately abort the |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 4436 | * request. It relies on buffers flags, and updates s->req.analysers. |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 4437 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 4438 | int http_process_request(struct stream *s, struct channel *req, int an_bit) |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 4439 | { |
Willy Tarreau | fb0afa7 | 2015-04-03 14:46:27 +0200 | [diff] [blame] | 4440 | struct session *sess = s->sess; |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 4441 | struct http_txn *txn = s->txn; |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 4442 | struct http_msg *msg = &txn->req; |
Willy Tarreau | ee335e6 | 2015-04-21 18:15:13 +0200 | [diff] [blame] | 4443 | struct connection *cli_conn = objt_conn(strm_sess(s)->origin); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4444 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 4445 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) { |
Willy Tarreau | 51aecc7 | 2009-07-12 09:47:04 +0200 | [diff] [blame] | 4446 | /* we need more data */ |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 4447 | channel_dont_connect(req); |
Willy Tarreau | 51aecc7 | 2009-07-12 09:47:04 +0200 | [diff] [blame] | 4448 | return 0; |
| 4449 | } |
| 4450 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 4451 | DPRINTF(stderr,"[%u] %s: stream=%p b=%p, exp(r,w)=%u,%u bf=%08x bh=%d analysers=%02x\n", |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 4452 | now_ms, __FUNCTION__, |
| 4453 | s, |
| 4454 | req, |
| 4455 | req->rex, req->wex, |
| 4456 | req->flags, |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 4457 | req->buf->i, |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 4458 | req->analysers); |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 4459 | |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 4460 | if (sess->fe->comp || s->be->comp) |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 4461 | select_compression_request_header(s, req->buf); |
| 4462 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4463 | /* |
| 4464 | * Right now, we know that we have processed the entire headers |
| 4465 | * and that unwanted requests have been filtered out. We can do |
| 4466 | * whatever we want with the remaining request. Also, now we |
| 4467 | * may have separate values for ->fe, ->be. |
| 4468 | */ |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 4469 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4470 | /* |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 4471 | * If HTTP PROXY is set we simply get remote server address parsing |
| 4472 | * incoming request. Note that this requires that a connection is |
| 4473 | * allocated on the server side. |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4474 | */ |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 4475 | if ((s->be->options & PR_O_HTTP_PROXY) && !(s->flags & SF_ADDR_SET)) { |
Willy Tarreau | 32e3c6a | 2013-10-11 19:34:20 +0200 | [diff] [blame] | 4476 | struct connection *conn; |
Willy Tarreau | e8df1e1 | 2013-12-16 14:30:55 +0100 | [diff] [blame] | 4477 | char *path; |
Willy Tarreau | 32e3c6a | 2013-10-11 19:34:20 +0200 | [diff] [blame] | 4478 | |
Willy Tarreau | 9471b8c | 2013-12-15 13:31:35 +0100 | [diff] [blame] | 4479 | /* Note that for now we don't reuse existing proxy connections */ |
Willy Tarreau | 973a542 | 2015-08-05 21:47:23 +0200 | [diff] [blame] | 4480 | if (unlikely((conn = si_alloc_conn(&s->si[1])) == NULL)) { |
Willy Tarreau | 32e3c6a | 2013-10-11 19:34:20 +0200 | [diff] [blame] | 4481 | txn->req.msg_state = HTTP_MSG_ERROR; |
| 4482 | txn->status = 500; |
| 4483 | req->analysers = 0; |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 4484 | stream_int_retnclose(&s->si[0], http_error_message(s, HTTP_ERR_500)); |
Willy Tarreau | 32e3c6a | 2013-10-11 19:34:20 +0200 | [diff] [blame] | 4485 | |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 4486 | if (!(s->flags & SF_ERR_MASK)) |
| 4487 | s->flags |= SF_ERR_RESOURCE; |
| 4488 | if (!(s->flags & SF_FINST_MASK)) |
| 4489 | s->flags |= SF_FINST_R; |
Willy Tarreau | 32e3c6a | 2013-10-11 19:34:20 +0200 | [diff] [blame] | 4490 | |
| 4491 | return 0; |
| 4492 | } |
Willy Tarreau | e8df1e1 | 2013-12-16 14:30:55 +0100 | [diff] [blame] | 4493 | |
| 4494 | path = http_get_path(txn); |
| 4495 | url2sa(req->buf->p + msg->sl.rq.u, |
| 4496 | path ? path - (req->buf->p + msg->sl.rq.u) : msg->sl.rq.u_l, |
Thierry FOURNIER | 9f95e40 | 2014-03-21 14:51:46 +0100 | [diff] [blame] | 4497 | &conn->addr.to, NULL); |
Willy Tarreau | e8df1e1 | 2013-12-16 14:30:55 +0100 | [diff] [blame] | 4498 | /* if the path was found, we have to remove everything between |
| 4499 | * req->buf->p + msg->sl.rq.u and path (excluded). If it was not |
| 4500 | * found, we need to replace from req->buf->p + msg->sl.rq.u for |
| 4501 | * u_l characters by a single "/". |
| 4502 | */ |
| 4503 | if (path) { |
| 4504 | char *cur_ptr = req->buf->p; |
| 4505 | char *cur_end = cur_ptr + txn->req.sl.rq.l; |
| 4506 | int delta; |
| 4507 | |
| 4508 | delta = buffer_replace2(req->buf, req->buf->p + msg->sl.rq.u, path, NULL, 0); |
| 4509 | http_msg_move_end(&txn->req, delta); |
| 4510 | cur_end += delta; |
| 4511 | if (http_parse_reqline(&txn->req, HTTP_MSG_RQMETH, cur_ptr, cur_end + 1, NULL, NULL) == NULL) |
| 4512 | goto return_bad_req; |
| 4513 | } |
| 4514 | else { |
| 4515 | char *cur_ptr = req->buf->p; |
| 4516 | char *cur_end = cur_ptr + txn->req.sl.rq.l; |
| 4517 | int delta; |
| 4518 | |
| 4519 | delta = buffer_replace2(req->buf, req->buf->p + msg->sl.rq.u, |
| 4520 | req->buf->p + msg->sl.rq.u + msg->sl.rq.u_l, "/", 1); |
| 4521 | http_msg_move_end(&txn->req, delta); |
| 4522 | cur_end += delta; |
| 4523 | if (http_parse_reqline(&txn->req, HTTP_MSG_RQMETH, cur_ptr, cur_end + 1, NULL, NULL) == NULL) |
| 4524 | goto return_bad_req; |
| 4525 | } |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4526 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4527 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4528 | /* |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 4529 | * 7: Now we can work with the cookies. |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4530 | * Note that doing so might move headers in the request, but |
| 4531 | * the fields will stay coherent and the URI will not move. |
| 4532 | * This should only be performed in the backend. |
| 4533 | */ |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 4534 | if ((s->be->cookie_name || s->be->appsession_name || sess->fe->capture_name) |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4535 | && !(txn->flags & (TX_CLDENY|TX_CLTARPIT))) |
| 4536 | manage_client_side_cookies(s, req); |
Willy Tarreau | 7ac51f6 | 2007-03-25 16:00:04 +0200 | [diff] [blame] | 4537 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4538 | /* |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 4539 | * 8: the appsession cookie was looked up very early in 1.2, |
| 4540 | * so let's do the same now. |
| 4541 | */ |
| 4542 | |
Cyril Bonté | 47fdd8e | 2010-04-25 00:00:51 +0200 | [diff] [blame] | 4543 | /* It needs to look into the URI unless persistence must be ignored */ |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 4544 | if ((txn->sessid == NULL) && s->be->appsession_name && !(s->flags & SF_IGNORE_PRST)) { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 4545 | get_srv_from_appsession(s, req->buf->p + msg->sl.rq.u, msg->sl.rq.u_l); |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 4546 | } |
| 4547 | |
William Lallemand | a73203e | 2012-03-12 12:48:57 +0100 | [diff] [blame] | 4548 | /* add unique-id if "header-unique-id" is specified */ |
| 4549 | |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 4550 | if (!LIST_ISEMPTY(&sess->fe->format_unique_id)) { |
William Lallemand | 5b7ea3a | 2013-08-28 15:44:19 +0200 | [diff] [blame] | 4551 | if ((s->unique_id = pool_alloc2(pool2_uniqueid)) == NULL) |
| 4552 | goto return_bad_req; |
| 4553 | s->unique_id[0] = '\0'; |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 4554 | build_logline(s, s->unique_id, UNIQUEID_LEN, &sess->fe->format_unique_id); |
William Lallemand | 5b7ea3a | 2013-08-28 15:44:19 +0200 | [diff] [blame] | 4555 | } |
William Lallemand | a73203e | 2012-03-12 12:48:57 +0100 | [diff] [blame] | 4556 | |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 4557 | if (sess->fe->header_unique_id && s->unique_id) { |
| 4558 | chunk_printf(&trash, "%s: %s", sess->fe->header_unique_id, s->unique_id); |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 4559 | if (trash.len < 0) |
William Lallemand | a73203e | 2012-03-12 12:48:57 +0100 | [diff] [blame] | 4560 | goto return_bad_req; |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 4561 | if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, trash.len) < 0)) |
William Lallemand | a73203e | 2012-03-12 12:48:57 +0100 | [diff] [blame] | 4562 | goto return_bad_req; |
| 4563 | } |
| 4564 | |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 4565 | /* |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4566 | * 9: add X-Forwarded-For if either the frontend or the backend |
| 4567 | * asks for it. |
| 4568 | */ |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 4569 | if ((sess->fe->options | s->be->options) & PR_O_FWDFOR) { |
Willy Tarreau | 87cf514 | 2011-08-19 22:57:24 +0200 | [diff] [blame] | 4570 | struct hdr_ctx ctx = { .idx = 0 }; |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 4571 | if (!((sess->fe->options | s->be->options) & PR_O_FF_ALWAYS) && |
| 4572 | http_find_header2(s->be->fwdfor_hdr_len ? s->be->fwdfor_hdr_name : sess->fe->fwdfor_hdr_name, |
| 4573 | s->be->fwdfor_hdr_len ? s->be->fwdfor_hdr_len : sess->fe->fwdfor_hdr_len, |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 4574 | req->buf->p, &txn->hdr_idx, &ctx)) { |
Willy Tarreau | 87cf514 | 2011-08-19 22:57:24 +0200 | [diff] [blame] | 4575 | /* The header is set to be added only if none is present |
| 4576 | * and we found it, so don't do anything. |
| 4577 | */ |
| 4578 | } |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 4579 | else if (cli_conn && cli_conn->addr.from.ss_family == AF_INET) { |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4580 | /* Add an X-Forwarded-For header unless the source IP is |
| 4581 | * in the 'except' network range. |
| 4582 | */ |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 4583 | if ((!sess->fe->except_mask.s_addr || |
| 4584 | (((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr.s_addr & sess->fe->except_mask.s_addr) |
| 4585 | != sess->fe->except_net.s_addr) && |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4586 | (!s->be->except_mask.s_addr || |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 4587 | (((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr.s_addr & s->be->except_mask.s_addr) |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4588 | != s->be->except_net.s_addr)) { |
Willy Tarreau | 2a32428 | 2006-12-05 00:05:46 +0100 | [diff] [blame] | 4589 | int len; |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4590 | unsigned char *pn; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 4591 | pn = (unsigned char *)&((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr; |
Ross West | af72a1d | 2008-08-03 10:51:45 +0200 | [diff] [blame] | 4592 | |
| 4593 | /* Note: we rely on the backend to get the header name to be used for |
| 4594 | * x-forwarded-for, because the header is really meant for the backends. |
| 4595 | * However, if the backend did not specify any option, we have to rely |
| 4596 | * on the frontend's header name. |
| 4597 | */ |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4598 | if (s->be->fwdfor_hdr_len) { |
| 4599 | len = s->be->fwdfor_hdr_len; |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 4600 | memcpy(trash.str, s->be->fwdfor_hdr_name, len); |
Ross West | af72a1d | 2008-08-03 10:51:45 +0200 | [diff] [blame] | 4601 | } else { |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 4602 | len = sess->fe->fwdfor_hdr_len; |
| 4603 | memcpy(trash.str, sess->fe->fwdfor_hdr_name, len); |
Willy Tarreau | b86db34 | 2009-11-30 11:50:16 +0100 | [diff] [blame] | 4604 | } |
Willy Tarreau | e9187f8 | 2014-04-14 15:27:14 +0200 | [diff] [blame] | 4605 | len += snprintf(trash.str + len, trash.size - len, ": %d.%d.%d.%d", pn[0], pn[1], pn[2], pn[3]); |
Willy Tarreau | edcf668 | 2008-11-30 23:15:34 +0100 | [diff] [blame] | 4606 | |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 4607 | if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, len) < 0)) |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 4608 | goto return_bad_req; |
Willy Tarreau | 2a32428 | 2006-12-05 00:05:46 +0100 | [diff] [blame] | 4609 | } |
| 4610 | } |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 4611 | else if (cli_conn && cli_conn->addr.from.ss_family == AF_INET6) { |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4612 | /* FIXME: for the sake of completeness, we should also support |
| 4613 | * 'except' here, although it is mostly useless in this case. |
matt.farnsworth@nokia.com | 1c2ab96 | 2008-04-14 20:47:37 +0200 | [diff] [blame] | 4614 | */ |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4615 | int len; |
| 4616 | char pn[INET6_ADDRSTRLEN]; |
| 4617 | inet_ntop(AF_INET6, |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 4618 | (const void *)&((struct sockaddr_in6 *)(&cli_conn->addr.from))->sin6_addr, |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4619 | pn, sizeof(pn)); |
matt.farnsworth@nokia.com | 1c2ab96 | 2008-04-14 20:47:37 +0200 | [diff] [blame] | 4620 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4621 | /* Note: we rely on the backend to get the header name to be used for |
| 4622 | * x-forwarded-for, because the header is really meant for the backends. |
| 4623 | * However, if the backend did not specify any option, we have to rely |
| 4624 | * on the frontend's header name. |
matt.farnsworth@nokia.com | 1c2ab96 | 2008-04-14 20:47:37 +0200 | [diff] [blame] | 4625 | */ |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4626 | if (s->be->fwdfor_hdr_len) { |
| 4627 | len = s->be->fwdfor_hdr_len; |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 4628 | memcpy(trash.str, s->be->fwdfor_hdr_name, len); |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4629 | } else { |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 4630 | len = sess->fe->fwdfor_hdr_len; |
| 4631 | memcpy(trash.str, sess->fe->fwdfor_hdr_name, len); |
matt.farnsworth@nokia.com | 1c2ab96 | 2008-04-14 20:47:37 +0200 | [diff] [blame] | 4632 | } |
Willy Tarreau | e9187f8 | 2014-04-14 15:27:14 +0200 | [diff] [blame] | 4633 | len += snprintf(trash.str + len, trash.size - len, ": %s", pn); |
Willy Tarreau | adfb856 | 2008-08-11 15:24:42 +0200 | [diff] [blame] | 4634 | |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 4635 | if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, len) < 0)) |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4636 | goto return_bad_req; |
| 4637 | } |
| 4638 | } |
| 4639 | |
| 4640 | /* |
Maik Broemme | 2850cb4 | 2009-04-17 18:53:21 +0200 | [diff] [blame] | 4641 | * 10: add X-Original-To if either the frontend or the backend |
| 4642 | * asks for it. |
| 4643 | */ |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 4644 | if ((sess->fe->options | s->be->options) & PR_O_ORGTO) { |
Maik Broemme | 2850cb4 | 2009-04-17 18:53:21 +0200 | [diff] [blame] | 4645 | |
| 4646 | /* FIXME: don't know if IPv6 can handle that case too. */ |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 4647 | if (cli_conn && cli_conn->addr.from.ss_family == AF_INET) { |
Maik Broemme | 2850cb4 | 2009-04-17 18:53:21 +0200 | [diff] [blame] | 4648 | /* Add an X-Original-To header unless the destination IP is |
| 4649 | * in the 'except' network range. |
| 4650 | */ |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 4651 | conn_get_to_addr(cli_conn); |
Maik Broemme | 2850cb4 | 2009-04-17 18:53:21 +0200 | [diff] [blame] | 4652 | |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 4653 | if (cli_conn->addr.to.ss_family == AF_INET && |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 4654 | ((!sess->fe->except_mask_to.s_addr || |
| 4655 | (((struct sockaddr_in *)&cli_conn->addr.to)->sin_addr.s_addr & sess->fe->except_mask_to.s_addr) |
| 4656 | != sess->fe->except_to.s_addr) && |
Emeric Brun | 5bd86a8 | 2010-10-22 17:23:04 +0200 | [diff] [blame] | 4657 | (!s->be->except_mask_to.s_addr || |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 4658 | (((struct sockaddr_in *)&cli_conn->addr.to)->sin_addr.s_addr & s->be->except_mask_to.s_addr) |
Emeric Brun | 5bd86a8 | 2010-10-22 17:23:04 +0200 | [diff] [blame] | 4659 | != s->be->except_to.s_addr))) { |
Maik Broemme | 2850cb4 | 2009-04-17 18:53:21 +0200 | [diff] [blame] | 4660 | int len; |
| 4661 | unsigned char *pn; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 4662 | pn = (unsigned char *)&((struct sockaddr_in *)&cli_conn->addr.to)->sin_addr; |
Maik Broemme | 2850cb4 | 2009-04-17 18:53:21 +0200 | [diff] [blame] | 4663 | |
| 4664 | /* Note: we rely on the backend to get the header name to be used for |
| 4665 | * x-original-to, because the header is really meant for the backends. |
| 4666 | * However, if the backend did not specify any option, we have to rely |
| 4667 | * on the frontend's header name. |
| 4668 | */ |
| 4669 | if (s->be->orgto_hdr_len) { |
| 4670 | len = s->be->orgto_hdr_len; |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 4671 | memcpy(trash.str, s->be->orgto_hdr_name, len); |
Maik Broemme | 2850cb4 | 2009-04-17 18:53:21 +0200 | [diff] [blame] | 4672 | } else { |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 4673 | len = sess->fe->orgto_hdr_len; |
| 4674 | memcpy(trash.str, sess->fe->orgto_hdr_name, len); |
Willy Tarreau | b86db34 | 2009-11-30 11:50:16 +0100 | [diff] [blame] | 4675 | } |
Willy Tarreau | e9187f8 | 2014-04-14 15:27:14 +0200 | [diff] [blame] | 4676 | len += snprintf(trash.str + len, trash.size - len, ": %d.%d.%d.%d", pn[0], pn[1], pn[2], pn[3]); |
Maik Broemme | 2850cb4 | 2009-04-17 18:53:21 +0200 | [diff] [blame] | 4677 | |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 4678 | if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, len) < 0)) |
Maik Broemme | 2850cb4 | 2009-04-17 18:53:21 +0200 | [diff] [blame] | 4679 | goto return_bad_req; |
| 4680 | } |
| 4681 | } |
| 4682 | } |
| 4683 | |
Willy Tarreau | 50fc777 | 2012-11-11 22:19:57 +0100 | [diff] [blame] | 4684 | /* 11: add "Connection: close" or "Connection: keep-alive" if needed and not yet set. |
| 4685 | * If an "Upgrade" token is found, the header is left untouched in order not to have |
| 4686 | * to deal with some servers bugs : some of them fail an Upgrade if anything but |
| 4687 | * "Upgrade" is present in the Connection header. |
| 4688 | */ |
| 4689 | if (!(txn->flags & TX_HDR_CONN_UPG) && |
| 4690 | (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) || |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 4691 | ((sess->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL || |
Willy Tarreau | 02bce8b | 2014-01-30 00:15:28 +0100 | [diff] [blame] | 4692 | (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL))) { |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 4693 | unsigned int want_flags = 0; |
| 4694 | |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 4695 | if (msg->flags & HTTP_MSGF_VER_11) { |
Willy Tarreau | 22a9534 | 2010-09-29 14:31:41 +0200 | [diff] [blame] | 4696 | if (((txn->flags & TX_CON_WANT_MSK) >= TX_CON_WANT_SCL || |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 4697 | ((sess->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL || |
Willy Tarreau | 02bce8b | 2014-01-30 00:15:28 +0100 | [diff] [blame] | 4698 | (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL)) && |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 4699 | !((sess->fe->options2|s->be->options2) & PR_O2_FAKE_KA)) |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 4700 | want_flags |= TX_CON_CLO_SET; |
| 4701 | } else { |
Willy Tarreau | 22a9534 | 2010-09-29 14:31:41 +0200 | [diff] [blame] | 4702 | if (((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL && |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 4703 | ((sess->fe->options & PR_O_HTTP_MODE) != PR_O_HTTP_PCL && |
Willy Tarreau | 02bce8b | 2014-01-30 00:15:28 +0100 | [diff] [blame] | 4704 | (s->be->options & PR_O_HTTP_MODE) != PR_O_HTTP_PCL)) || |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 4705 | ((sess->fe->options2|s->be->options2) & PR_O2_FAKE_KA)) |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 4706 | want_flags |= TX_CON_KAL_SET; |
| 4707 | } |
| 4708 | |
| 4709 | if (want_flags != (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET))) |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 4710 | http_change_connection_header(txn, msg, want_flags); |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4711 | } |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 4712 | |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 4713 | |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 4714 | /* If we have no server assigned yet and we're balancing on url_param |
| 4715 | * with a POST request, we may be interested in checking the body for |
| 4716 | * that parameter. This will be done in another analyser. |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4717 | */ |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 4718 | if (!(s->flags & (SF_ASSIGNED|SF_DIRECT)) && |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 4719 | s->txn->meth == HTTP_METH_POST && s->be->url_param_name != NULL && |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 4720 | (msg->flags & (HTTP_MSGF_CNT_LEN|HTTP_MSGF_TE_CHNK))) { |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 4721 | channel_dont_connect(req); |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 4722 | req->analysers |= AN_REQ_HTTP_BODY; |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4723 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 4724 | |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 4725 | if (msg->flags & HTTP_MSGF_XFER_LEN) { |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4726 | req->analysers |= AN_REQ_HTTP_XFER_BODY; |
Willy Tarreau | 5e20552 | 2011-12-17 16:34:27 +0100 | [diff] [blame] | 4727 | #ifdef TCP_QUICKACK |
| 4728 | /* We expect some data from the client. Unless we know for sure |
| 4729 | * we already have a full request, we have to re-enable quick-ack |
| 4730 | * in case we previously disabled it, otherwise we might cause |
| 4731 | * the client to delay further data. |
| 4732 | */ |
Willy Tarreau | fb0afa7 | 2015-04-03 14:46:27 +0200 | [diff] [blame] | 4733 | if ((sess->listener->options & LI_O_NOQUICKACK) && |
Willy Tarreau | 3c72872 | 2014-01-23 13:50:42 +0100 | [diff] [blame] | 4734 | cli_conn && conn_ctrl_ready(cli_conn) && |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 4735 | ((msg->flags & HTTP_MSGF_TE_CHNK) || |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 4736 | (msg->body_len > req->buf->i - txn->req.eoh - 2))) |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 4737 | setsockopt(cli_conn->t.sock.fd, IPPROTO_TCP, TCP_QUICKACK, &one, sizeof(one)); |
Willy Tarreau | 5e20552 | 2011-12-17 16:34:27 +0100 | [diff] [blame] | 4738 | #endif |
| 4739 | } |
Willy Tarreau | 0394594 | 2009-12-22 16:50:27 +0100 | [diff] [blame] | 4740 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4741 | /************************************************************* |
| 4742 | * OK, that's finished for the headers. We have done what we * |
| 4743 | * could. Let's switch to the DATA state. * |
| 4744 | ************************************************************/ |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 4745 | req->analyse_exp = TICK_ETERNITY; |
| 4746 | req->analysers &= ~an_bit; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 4747 | |
Willy Tarreau | 7bb68ab | 2012-05-13 14:48:59 +0200 | [diff] [blame] | 4748 | /* if the server closes the connection, we want to immediately react |
| 4749 | * and close the socket to save packets and syscalls. |
| 4750 | */ |
Willy Tarreau | 40f151a | 2012-12-20 12:10:09 +0100 | [diff] [blame] | 4751 | if (!(req->analysers & AN_REQ_HTTP_XFER_BODY)) |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 4752 | s->si[1].flags |= SI_FL_NOHALF; |
Willy Tarreau | 7bb68ab | 2012-05-13 14:48:59 +0200 | [diff] [blame] | 4753 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4754 | s->logs.tv_request = now; |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4755 | /* OK let's go on with the BODY now */ |
| 4756 | return 1; |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 4757 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4758 | return_bad_req: /* let's centralize all bad requests */ |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 4759 | if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) { |
Willy Tarreau | f073a83 | 2009-03-01 23:21:47 +0100 | [diff] [blame] | 4760 | /* we detected a parsing error. We want to archive this request |
| 4761 | * in the dedicated proxy area for later troubleshooting. |
| 4762 | */ |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 4763 | http_capture_bad_message(&sess->fe->invalid_req, s, msg, msg->msg_state, sess->fe); |
Willy Tarreau | f073a83 | 2009-03-01 23:21:47 +0100 | [diff] [blame] | 4764 | } |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 4765 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4766 | txn->req.msg_state = HTTP_MSG_ERROR; |
| 4767 | txn->status = 400; |
| 4768 | req->analysers = 0; |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 4769 | stream_int_retnclose(&s->si[0], http_error_message(s, HTTP_ERR_400)); |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 4770 | |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 4771 | sess->fe->fe_counters.failed_req++; |
Willy Tarreau | fb0afa7 | 2015-04-03 14:46:27 +0200 | [diff] [blame] | 4772 | if (sess->listener->counters) |
| 4773 | sess->listener->counters->failed_req++; |
Willy Tarreau | adfb856 | 2008-08-11 15:24:42 +0200 | [diff] [blame] | 4774 | |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 4775 | if (!(s->flags & SF_ERR_MASK)) |
| 4776 | s->flags |= SF_ERR_PRXCOND; |
| 4777 | if (!(s->flags & SF_FINST_MASK)) |
| 4778 | s->flags |= SF_FINST_R; |
Willy Tarreau | dafde43 | 2008-08-17 01:00:46 +0200 | [diff] [blame] | 4779 | return 0; |
Willy Tarreau | c65a3ba | 2008-08-11 23:42:50 +0200 | [diff] [blame] | 4780 | } |
Willy Tarreau | adfb856 | 2008-08-11 15:24:42 +0200 | [diff] [blame] | 4781 | |
Willy Tarreau | 60b85b0 | 2008-11-30 23:28:40 +0100 | [diff] [blame] | 4782 | /* This function is an analyser which processes the HTTP tarpit. It always |
| 4783 | * returns zero, at the beginning because it prevents any other processing |
| 4784 | * from occurring, and at the end because it terminates the request. |
| 4785 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 4786 | int http_process_tarpit(struct stream *s, struct channel *req, int an_bit) |
Willy Tarreau | 60b85b0 | 2008-11-30 23:28:40 +0100 | [diff] [blame] | 4787 | { |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 4788 | struct http_txn *txn = s->txn; |
Willy Tarreau | 60b85b0 | 2008-11-30 23:28:40 +0100 | [diff] [blame] | 4789 | |
| 4790 | /* This connection is being tarpitted. The CLIENT side has |
| 4791 | * already set the connect expiration date to the right |
| 4792 | * timeout. We just have to check that the client is still |
| 4793 | * there and that the timeout has not expired. |
| 4794 | */ |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 4795 | channel_dont_connect(req); |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 4796 | if ((req->flags & (CF_SHUTR|CF_READ_ERROR)) == 0 && |
Willy Tarreau | 60b85b0 | 2008-11-30 23:28:40 +0100 | [diff] [blame] | 4797 | !tick_is_expired(req->analyse_exp, now_ms)) |
| 4798 | return 0; |
| 4799 | |
| 4800 | /* We will set the queue timer to the time spent, just for |
| 4801 | * logging purposes. We fake a 500 server error, so that the |
| 4802 | * attacker will not suspect his connection has been tarpitted. |
| 4803 | * It will not cause trouble to the logs because we can exclude |
| 4804 | * the tarpitted connections by filtering on the 'PT' status flags. |
| 4805 | */ |
Willy Tarreau | 60b85b0 | 2008-11-30 23:28:40 +0100 | [diff] [blame] | 4806 | s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now); |
| 4807 | |
| 4808 | txn->status = 500; |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 4809 | if (!(req->flags & CF_READ_ERROR)) |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 4810 | stream_int_retnclose(&s->si[0], http_error_message(s, HTTP_ERR_500)); |
Willy Tarreau | 60b85b0 | 2008-11-30 23:28:40 +0100 | [diff] [blame] | 4811 | |
| 4812 | req->analysers = 0; |
| 4813 | req->analyse_exp = TICK_ETERNITY; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 4814 | |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 4815 | if (!(s->flags & SF_ERR_MASK)) |
| 4816 | s->flags |= SF_ERR_PRXCOND; |
| 4817 | if (!(s->flags & SF_FINST_MASK)) |
| 4818 | s->flags |= SF_FINST_T; |
Willy Tarreau | 60b85b0 | 2008-11-30 23:28:40 +0100 | [diff] [blame] | 4819 | return 0; |
| 4820 | } |
| 4821 | |
Willy Tarreau | 5a8f947 | 2014-04-10 11:16:06 +0200 | [diff] [blame] | 4822 | /* This function is an analyser which waits for the HTTP request body. It waits |
| 4823 | * for either the buffer to be full, or the full advertised contents to have |
| 4824 | * reached the buffer. It must only be called after the standard HTTP request |
| 4825 | * processing has occurred, because it expects the request to be parsed and will |
| 4826 | * look for the Expect header. It may send a 100-Continue interim response. It |
| 4827 | * takes in input any state starting from HTTP_MSG_BODY and leaves with one of |
| 4828 | * HTTP_MSG_CHK_SIZE, HTTP_MSG_DATA or HTTP_MSG_TRAILERS. It returns zero if it |
| 4829 | * needs to read more data, or 1 once it has completed its analysis. |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 4830 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 4831 | int http_wait_for_request_body(struct stream *s, struct channel *req, int an_bit) |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 4832 | { |
Willy Tarreau | fb0afa7 | 2015-04-03 14:46:27 +0200 | [diff] [blame] | 4833 | struct session *sess = s->sess; |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 4834 | struct http_txn *txn = s->txn; |
| 4835 | struct http_msg *msg = &s->txn->req; |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 4836 | |
| 4837 | /* We have to parse the HTTP request body to find any required data. |
| 4838 | * "balance url_param check_post" should have been the only way to get |
| 4839 | * into this. We were brought here after HTTP header analysis, so all |
| 4840 | * related structures are ready. |
| 4841 | */ |
| 4842 | |
Willy Tarreau | 890988f | 2014-04-10 11:59:33 +0200 | [diff] [blame] | 4843 | if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) { |
| 4844 | /* This is the first call */ |
| 4845 | if (msg->msg_state < HTTP_MSG_BODY) |
| 4846 | goto missing_data; |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 4847 | |
Willy Tarreau | 890988f | 2014-04-10 11:59:33 +0200 | [diff] [blame] | 4848 | if (msg->msg_state < HTTP_MSG_100_SENT) { |
| 4849 | /* If we have HTTP/1.1 and Expect: 100-continue, then we must |
| 4850 | * send an HTTP/1.1 100 Continue intermediate response. |
| 4851 | */ |
| 4852 | if (msg->flags & HTTP_MSGF_VER_11) { |
| 4853 | struct hdr_ctx ctx; |
| 4854 | ctx.idx = 0; |
| 4855 | /* Expect is allowed in 1.1, look for it */ |
| 4856 | if (http_find_header2("Expect", 6, req->buf->p, &txn->hdr_idx, &ctx) && |
| 4857 | unlikely(ctx.vlen == 12 && strncasecmp(ctx.line+ctx.val, "100-continue", 12) == 0)) { |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 4858 | bo_inject(&s->res, http_100_chunk.str, http_100_chunk.len); |
Willy Tarreau | 890988f | 2014-04-10 11:59:33 +0200 | [diff] [blame] | 4859 | } |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 4860 | } |
Willy Tarreau | 890988f | 2014-04-10 11:59:33 +0200 | [diff] [blame] | 4861 | msg->msg_state = HTTP_MSG_100_SENT; |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 4862 | } |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 4863 | |
Willy Tarreau | fa4a03c | 2012-03-09 21:28:54 +0100 | [diff] [blame] | 4864 | /* we have msg->sov which points to the first byte of message body. |
Willy Tarreau | 877e78d | 2013-04-07 18:48:08 +0200 | [diff] [blame] | 4865 | * req->buf->p still points to the beginning of the message. We |
| 4866 | * must save the body in msg->next because it survives buffer |
| 4867 | * re-alignments. |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4868 | */ |
Willy Tarreau | ea1175a | 2012-03-05 15:52:30 +0100 | [diff] [blame] | 4869 | msg->next = msg->sov; |
Willy Tarreau | a458b67 | 2012-03-05 11:17:50 +0100 | [diff] [blame] | 4870 | |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 4871 | if (msg->flags & HTTP_MSGF_TE_CHNK) |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 4872 | msg->msg_state = HTTP_MSG_CHUNK_SIZE; |
| 4873 | else |
| 4874 | msg->msg_state = HTTP_MSG_DATA; |
| 4875 | } |
| 4876 | |
Willy Tarreau | 890988f | 2014-04-10 11:59:33 +0200 | [diff] [blame] | 4877 | if (!(msg->flags & HTTP_MSGF_TE_CHNK)) { |
| 4878 | /* We're in content-length mode, we just have to wait for enough data. */ |
Willy Tarreau | e115b49 | 2015-05-01 23:05:14 +0200 | [diff] [blame] | 4879 | if (http_body_bytes(msg) < msg->body_len) |
Willy Tarreau | 890988f | 2014-04-10 11:59:33 +0200 | [diff] [blame] | 4880 | goto missing_data; |
| 4881 | |
| 4882 | /* OK we have everything we need now */ |
| 4883 | goto http_end; |
| 4884 | } |
| 4885 | |
| 4886 | /* OK here we're parsing a chunked-encoded message */ |
| 4887 | |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 4888 | if (msg->msg_state == HTTP_MSG_CHUNK_SIZE) { |
Willy Tarreau | 124d991 | 2011-03-01 20:30:48 +0100 | [diff] [blame] | 4889 | /* read the chunk size and assign it to ->chunk_len, then |
Willy Tarreau | a458b67 | 2012-03-05 11:17:50 +0100 | [diff] [blame] | 4890 | * set ->sov and ->next to point to the body and switch to DATA or |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4891 | * TRAILERS state. |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 4892 | */ |
Willy Tarreau | 4baf44b | 2012-03-09 14:10:20 +0100 | [diff] [blame] | 4893 | int ret = http_parse_chunk_size(msg); |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 4894 | |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 4895 | if (!ret) |
| 4896 | goto missing_data; |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 4897 | else if (ret < 0) { |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 4898 | stream_inc_http_err_ctr(s); |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 4899 | goto return_bad_req; |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 4900 | } |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 4901 | } |
| 4902 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4903 | /* Now we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state. |
Willy Tarreau | e115b49 | 2015-05-01 23:05:14 +0200 | [diff] [blame] | 4904 | * We have the first data byte is in msg->sov + msg->sol. We're waiting |
| 4905 | * for at least a whole chunk or the whole content length bytes after |
| 4906 | * msg->sov + msg->sol. |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 4907 | */ |
Willy Tarreau | 890988f | 2014-04-10 11:59:33 +0200 | [diff] [blame] | 4908 | if (msg->msg_state == HTTP_MSG_TRAILERS) |
| 4909 | goto http_end; |
| 4910 | |
Willy Tarreau | e115b49 | 2015-05-01 23:05:14 +0200 | [diff] [blame] | 4911 | if (http_body_bytes(msg) >= msg->body_len) /* we have enough bytes now */ |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 4912 | goto http_end; |
| 4913 | |
| 4914 | missing_data: |
Willy Tarreau | 31a1995 | 2014-04-10 11:50:37 +0200 | [diff] [blame] | 4915 | /* we get here if we need to wait for more data. If the buffer is full, |
| 4916 | * we have the maximum we can expect. |
| 4917 | */ |
| 4918 | if (buffer_full(req->buf, global.tune.maxrewrite)) |
| 4919 | goto http_end; |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 4920 | |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 4921 | if ((req->flags & CF_READ_TIMEOUT) || tick_is_expired(req->analyse_exp, now_ms)) { |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 4922 | txn->status = 408; |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 4923 | stream_int_retnclose(&s->si[0], http_error_message(s, HTTP_ERR_408)); |
Willy Tarreau | 79ebac6 | 2010-06-07 13:47:49 +0200 | [diff] [blame] | 4924 | |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 4925 | if (!(s->flags & SF_ERR_MASK)) |
| 4926 | s->flags |= SF_ERR_CLITO; |
| 4927 | if (!(s->flags & SF_FINST_MASK)) |
| 4928 | s->flags |= SF_FINST_D; |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 4929 | goto return_err_msg; |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 4930 | } |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 4931 | |
| 4932 | /* we get here if we need to wait for more data */ |
Willy Tarreau | 31a1995 | 2014-04-10 11:50:37 +0200 | [diff] [blame] | 4933 | if (!(req->flags & (CF_SHUTR | CF_READ_ERROR))) { |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 4934 | /* Not enough data. We'll re-use the http-request |
| 4935 | * timeout here. Ideally, we should set the timeout |
| 4936 | * relative to the accept() date. We just set the |
| 4937 | * request timeout once at the beginning of the |
| 4938 | * request. |
| 4939 | */ |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 4940 | channel_dont_connect(req); |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 4941 | if (!tick_isset(req->analyse_exp)) |
Willy Tarreau | cd7afc0 | 2009-07-12 10:03:17 +0200 | [diff] [blame] | 4942 | req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.httpreq); |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 4943 | return 0; |
| 4944 | } |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 4945 | |
| 4946 | http_end: |
| 4947 | /* The situation will not evolve, so let's give up on the analysis. */ |
| 4948 | s->logs.tv_request = now; /* update the request timer to reflect full request */ |
| 4949 | req->analysers &= ~an_bit; |
| 4950 | req->analyse_exp = TICK_ETERNITY; |
| 4951 | return 1; |
| 4952 | |
| 4953 | return_bad_req: /* let's centralize all bad requests */ |
| 4954 | txn->req.msg_state = HTTP_MSG_ERROR; |
| 4955 | txn->status = 400; |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 4956 | stream_int_retnclose(&s->si[0], http_error_message(s, HTTP_ERR_400)); |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 4957 | |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 4958 | if (!(s->flags & SF_ERR_MASK)) |
| 4959 | s->flags |= SF_ERR_PRXCOND; |
| 4960 | if (!(s->flags & SF_FINST_MASK)) |
| 4961 | s->flags |= SF_FINST_R; |
Willy Tarreau | 79ebac6 | 2010-06-07 13:47:49 +0200 | [diff] [blame] | 4962 | |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 4963 | return_err_msg: |
| 4964 | req->analysers = 0; |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 4965 | sess->fe->fe_counters.failed_req++; |
Willy Tarreau | fb0afa7 | 2015-04-03 14:46:27 +0200 | [diff] [blame] | 4966 | if (sess->listener->counters) |
| 4967 | sess->listener->counters->failed_req++; |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 4968 | return 0; |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 4969 | } |
| 4970 | |
Willy Tarreau | d1de8af | 2012-05-18 22:12:14 +0200 | [diff] [blame] | 4971 | /* send a server's name with an outgoing request over an established connection. |
| 4972 | * Note: this function is designed to be called once the request has been scheduled |
| 4973 | * for being forwarded. This is the reason why it rewinds the buffer before |
| 4974 | * proceeding. |
| 4975 | */ |
Willy Tarreau | 45c0d98 | 2012-03-09 12:11:57 +0100 | [diff] [blame] | 4976 | int http_send_name_header(struct http_txn *txn, struct proxy* be, const char* srv_name) { |
Mark Lamourine | c2247f0 | 2012-01-04 13:02:01 -0500 | [diff] [blame] | 4977 | |
| 4978 | struct hdr_ctx ctx; |
| 4979 | |
Mark Lamourine | c2247f0 | 2012-01-04 13:02:01 -0500 | [diff] [blame] | 4980 | char *hdr_name = be->server_id_hdr_name; |
| 4981 | int hdr_name_len = be->server_id_hdr_len; |
Willy Tarreau | 394db37 | 2012-10-12 22:40:39 +0200 | [diff] [blame] | 4982 | struct channel *chn = txn->req.chn; |
Mark Lamourine | c2247f0 | 2012-01-04 13:02:01 -0500 | [diff] [blame] | 4983 | char *hdr_val; |
Willy Tarreau | d1de8af | 2012-05-18 22:12:14 +0200 | [diff] [blame] | 4984 | unsigned int old_o, old_i; |
Mark Lamourine | c2247f0 | 2012-01-04 13:02:01 -0500 | [diff] [blame] | 4985 | |
William Lallemand | d9e9066 | 2012-01-30 17:27:17 +0100 | [diff] [blame] | 4986 | ctx.idx = 0; |
| 4987 | |
Willy Tarreau | 211cdec | 2014-04-17 20:18:08 +0200 | [diff] [blame] | 4988 | old_o = http_hdr_rewind(&txn->req); |
Willy Tarreau | d1de8af | 2012-05-18 22:12:14 +0200 | [diff] [blame] | 4989 | if (old_o) { |
| 4990 | /* The request was already skipped, let's restore it */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 4991 | b_rew(chn->buf, old_o); |
Willy Tarreau | 877e78d | 2013-04-07 18:48:08 +0200 | [diff] [blame] | 4992 | txn->req.next += old_o; |
| 4993 | txn->req.sov += old_o; |
Willy Tarreau | d1de8af | 2012-05-18 22:12:14 +0200 | [diff] [blame] | 4994 | } |
| 4995 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 4996 | old_i = chn->buf->i; |
| 4997 | while (http_find_header2(hdr_name, hdr_name_len, txn->req.chn->buf->p, &txn->hdr_idx, &ctx)) { |
Mark Lamourine | c2247f0 | 2012-01-04 13:02:01 -0500 | [diff] [blame] | 4998 | /* remove any existing values from the header */ |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 4999 | http_remove_header2(&txn->req, &txn->hdr_idx, &ctx); |
Mark Lamourine | c2247f0 | 2012-01-04 13:02:01 -0500 | [diff] [blame] | 5000 | } |
| 5001 | |
| 5002 | /* Add the new header requested with the server value */ |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 5003 | hdr_val = trash.str; |
Mark Lamourine | c2247f0 | 2012-01-04 13:02:01 -0500 | [diff] [blame] | 5004 | memcpy(hdr_val, hdr_name, hdr_name_len); |
| 5005 | hdr_val += hdr_name_len; |
| 5006 | *hdr_val++ = ':'; |
| 5007 | *hdr_val++ = ' '; |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 5008 | hdr_val += strlcpy2(hdr_val, srv_name, trash.str + trash.size - hdr_val); |
| 5009 | http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, hdr_val - trash.str); |
Mark Lamourine | c2247f0 | 2012-01-04 13:02:01 -0500 | [diff] [blame] | 5010 | |
Willy Tarreau | d1de8af | 2012-05-18 22:12:14 +0200 | [diff] [blame] | 5011 | if (old_o) { |
| 5012 | /* If this was a forwarded request, we must readjust the amount of |
| 5013 | * data to be forwarded in order to take into account the size |
Willy Tarreau | 877e78d | 2013-04-07 18:48:08 +0200 | [diff] [blame] | 5014 | * variations. Note that the current state is >= HTTP_MSG_BODY, |
| 5015 | * so we don't have to adjust ->sol. |
Willy Tarreau | d1de8af | 2012-05-18 22:12:14 +0200 | [diff] [blame] | 5016 | */ |
Willy Tarreau | 877e78d | 2013-04-07 18:48:08 +0200 | [diff] [blame] | 5017 | old_o += chn->buf->i - old_i; |
| 5018 | b_adv(chn->buf, old_o); |
| 5019 | txn->req.next -= old_o; |
| 5020 | txn->req.sov -= old_o; |
Willy Tarreau | d1de8af | 2012-05-18 22:12:14 +0200 | [diff] [blame] | 5021 | } |
| 5022 | |
Mark Lamourine | c2247f0 | 2012-01-04 13:02:01 -0500 | [diff] [blame] | 5023 | return 0; |
| 5024 | } |
| 5025 | |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5026 | /* Terminate current transaction and prepare a new one. This is very tricky |
| 5027 | * right now but it works. |
| 5028 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 5029 | void http_end_txn_clean_session(struct stream *s) |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5030 | { |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 5031 | int prev_status = s->txn->status; |
Willy Tarreau | d0d8da9 | 2015-04-04 02:10:38 +0200 | [diff] [blame] | 5032 | struct proxy *fe = strm_fe(s); |
Willy Tarreau | 323a2d9 | 2015-08-04 19:00:17 +0200 | [diff] [blame] | 5033 | struct connection *srv_conn; |
| 5034 | struct server *srv; |
Willy Tarreau | 068621e | 2013-12-23 15:11:25 +0100 | [diff] [blame] | 5035 | |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5036 | /* FIXME: We need a more portable way of releasing a backend's and a |
| 5037 | * server's connections. We need a safer way to reinitialize buffer |
| 5038 | * flags. We also need a more accurate method for computing per-request |
| 5039 | * data. |
| 5040 | */ |
Willy Tarreau | 323a2d9 | 2015-08-04 19:00:17 +0200 | [diff] [blame] | 5041 | srv_conn = objt_conn(s->si[1].end); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5042 | |
Willy Tarreau | 4213a11 | 2013-12-15 10:25:42 +0100 | [diff] [blame] | 5043 | /* unless we're doing keep-alive, we want to quickly close the connection |
| 5044 | * to the server. |
| 5045 | */ |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 5046 | if (((s->txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) || |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 5047 | !si_conn_ready(&s->si[1])) { |
| 5048 | s->si[1].flags |= SI_FL_NOLINGER | SI_FL_NOHALF; |
| 5049 | si_shutr(&s->si[1]); |
| 5050 | si_shutw(&s->si[1]); |
Willy Tarreau | 4213a11 | 2013-12-15 10:25:42 +0100 | [diff] [blame] | 5051 | } |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5052 | |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 5053 | if (s->flags & SF_BE_ASSIGNED) { |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5054 | s->be->beconn--; |
Willy Tarreau | 2d5cd47 | 2012-03-01 23:34:37 +0100 | [diff] [blame] | 5055 | if (unlikely(s->srv_conn)) |
| 5056 | sess_change_server(s, NULL); |
| 5057 | } |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5058 | |
| 5059 | s->logs.t_close = tv_ms_elapsed(&s->logs.tv_accept, &now); |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 5060 | stream_process_counters(s); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5061 | |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 5062 | if (s->txn->status) { |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5063 | int n; |
| 5064 | |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 5065 | n = s->txn->status / 100; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5066 | if (n < 1 || n > 5) |
| 5067 | n = 0; |
| 5068 | |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 5069 | if (fe->mode == PR_MODE_HTTP) { |
| 5070 | fe->fe_counters.p.http.rsp[n]++; |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 5071 | if (s->comp_algo && (s->flags & SF_COMP_READY)) |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 5072 | fe->fe_counters.p.http.comp_rsp++; |
Willy Tarreau | 5e16cbc | 2012-11-24 14:54:13 +0100 | [diff] [blame] | 5073 | } |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 5074 | if ((s->flags & SF_BE_ASSIGNED) && |
Willy Tarreau | 5e16cbc | 2012-11-24 14:54:13 +0100 | [diff] [blame] | 5075 | (s->be->mode == PR_MODE_HTTP)) { |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 5076 | s->be->be_counters.p.http.rsp[n]++; |
Willy Tarreau | 5e16cbc | 2012-11-24 14:54:13 +0100 | [diff] [blame] | 5077 | s->be->be_counters.p.http.cum_req++; |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 5078 | if (s->comp_algo && (s->flags & SF_COMP_READY)) |
Willy Tarreau | 5e16cbc | 2012-11-24 14:54:13 +0100 | [diff] [blame] | 5079 | s->be->be_counters.p.http.comp_rsp++; |
| 5080 | } |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5081 | } |
| 5082 | |
| 5083 | /* don't count other requests' data */ |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 5084 | s->logs.bytes_in -= s->req.buf->i; |
| 5085 | s->logs.bytes_out -= s->res.buf->i; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5086 | |
| 5087 | /* let's do a final log if we need it */ |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 5088 | if (!LIST_ISEMPTY(&fe->logformat) && s->logs.logwait && |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 5089 | !(s->flags & SF_MONITOR) && |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 5090 | (!(fe->options & PR_O_NULLNOLOG) || s->req.total)) { |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5091 | s->do_log(s); |
| 5092 | } |
| 5093 | |
Willy Tarreau | d713bcc | 2014-06-25 15:36:04 +0200 | [diff] [blame] | 5094 | /* stop tracking content-based counters */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 5095 | stream_stop_content_counters(s); |
| 5096 | stream_update_time_stats(s); |
Willy Tarreau | 4bfc580 | 2014-06-17 12:19:18 +0200 | [diff] [blame] | 5097 | |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5098 | s->logs.accept_date = date; /* user-visible date for logging */ |
| 5099 | s->logs.tv_accept = now; /* corrected date for internal use */ |
| 5100 | tv_zero(&s->logs.tv_request); |
| 5101 | s->logs.t_queue = -1; |
| 5102 | s->logs.t_connect = -1; |
| 5103 | s->logs.t_data = -1; |
| 5104 | s->logs.t_close = 0; |
| 5105 | s->logs.prx_queue_size = 0; /* we get the number of pending conns before us */ |
| 5106 | s->logs.srv_queue_size = 0; /* we will get this number soon */ |
| 5107 | |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 5108 | s->logs.bytes_in = s->req.total = s->req.buf->i; |
| 5109 | s->logs.bytes_out = s->res.total = s->res.buf->i; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5110 | |
| 5111 | if (s->pend_pos) |
| 5112 | pendconn_free(s->pend_pos); |
| 5113 | |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 5114 | if (objt_server(s->target)) { |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 5115 | if (s->flags & SF_CURR_SESS) { |
| 5116 | s->flags &= ~SF_CURR_SESS; |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 5117 | objt_server(s->target)->cur_sess--; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5118 | } |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 5119 | if (may_dequeue_tasks(objt_server(s->target), s->be)) |
| 5120 | process_srv_queue(objt_server(s->target)); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5121 | } |
| 5122 | |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 5123 | s->target = NULL; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5124 | |
Willy Tarreau | 4213a11 | 2013-12-15 10:25:42 +0100 | [diff] [blame] | 5125 | /* only release our endpoint if we don't intend to reuse the |
| 5126 | * connection. |
| 5127 | */ |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 5128 | if (((s->txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) || |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 5129 | !si_conn_ready(&s->si[1])) { |
| 5130 | si_release_endpoint(&s->si[1]); |
Willy Tarreau | 323a2d9 | 2015-08-04 19:00:17 +0200 | [diff] [blame] | 5131 | srv_conn = NULL; |
Willy Tarreau | 4213a11 | 2013-12-15 10:25:42 +0100 | [diff] [blame] | 5132 | } |
| 5133 | |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 5134 | s->si[1].state = s->si[1].prev_state = SI_ST_INI; |
| 5135 | s->si[1].err_type = SI_ET_NONE; |
| 5136 | s->si[1].conn_retries = 0; /* used for logging too */ |
| 5137 | s->si[1].exp = TICK_ETERNITY; |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 5138 | s->si[1].flags &= SI_FL_ISBACK | SI_FL_DONT_WAKE; /* we're in the context of process_stream */ |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 5139 | 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); |
| 5140 | 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 Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 5141 | s->flags &= ~(SF_DIRECT|SF_ASSIGNED|SF_ADDR_SET|SF_BE_ASSIGNED|SF_FORCE_PRST|SF_IGNORE_PRST); |
| 5142 | s->flags &= ~(SF_CURR_SESS|SF_REDIRECTABLE|SF_SRV_REUSED); |
| 5143 | s->flags &= ~(SF_ERR_MASK|SF_FINST_MASK|SF_REDISP); |
Willy Tarreau | 543db62 | 2012-11-15 16:41:22 +0100 | [diff] [blame] | 5144 | |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 5145 | s->txn->meth = 0; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5146 | http_reset_txn(s); |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 5147 | s->txn->flags |= TX_NOT_FIRST | TX_WAIT_NEXT_RQ; |
Willy Tarreau | 068621e | 2013-12-23 15:11:25 +0100 | [diff] [blame] | 5148 | |
| 5149 | if (prev_status == 401 || prev_status == 407) { |
| 5150 | /* In HTTP keep-alive mode, if we receive a 401, we still have |
| 5151 | * a chance of being able to send the visitor again to the same |
| 5152 | * server over the same connection. This is required by some |
| 5153 | * broken protocols such as NTLM, and anyway whenever there is |
| 5154 | * an opportunity for sending the challenge to the proper place, |
| 5155 | * it's better to do it (at least it helps with debugging). |
| 5156 | */ |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 5157 | s->txn->flags |= TX_PREFER_LAST; |
Willy Tarreau | 387ebf8 | 2015-08-04 19:24:13 +0200 | [diff] [blame] | 5158 | srv_conn->flags |= CO_FL_PRIVATE; |
Willy Tarreau | 068621e | 2013-12-23 15:11:25 +0100 | [diff] [blame] | 5159 | } |
| 5160 | |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 5161 | if (fe->options2 & PR_O2_INDEPSTR) |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 5162 | s->si[1].flags |= SI_FL_INDEP_STR; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5163 | |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 5164 | if (fe->options2 & PR_O2_NODELAY) { |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 5165 | s->req.flags |= CF_NEVER_WAIT; |
| 5166 | s->res.flags |= CF_NEVER_WAIT; |
Willy Tarreau | 96e3121 | 2011-05-30 18:10:30 +0200 | [diff] [blame] | 5167 | } |
| 5168 | |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5169 | /* if the request buffer is not empty, it means we're |
| 5170 | * about to process another request, so send pending |
| 5171 | * data with MSG_MORE to merge TCP packets when possible. |
Willy Tarreau | 065e833 | 2010-01-08 00:30:20 +0100 | [diff] [blame] | 5172 | * Just don't do this if the buffer is close to be full, |
| 5173 | * because the request will wait for it to flush a little |
| 5174 | * bit before proceeding. |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5175 | */ |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 5176 | if (s->req.buf->i) { |
| 5177 | if (s->res.buf->o && |
| 5178 | !buffer_full(s->res.buf, global.tune.maxrewrite) && |
| 5179 | bi_end(s->res.buf) <= s->res.buf->data + s->res.buf->size - global.tune.maxrewrite) |
| 5180 | s->res.flags |= CF_EXPECT_MORE; |
Willy Tarreau | 065e833 | 2010-01-08 00:30:20 +0100 | [diff] [blame] | 5181 | } |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 5182 | |
| 5183 | /* we're removing the analysers, we MUST re-enable events detection */ |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 5184 | channel_auto_read(&s->req); |
| 5185 | channel_auto_close(&s->req); |
| 5186 | channel_auto_read(&s->res); |
| 5187 | channel_auto_close(&s->res); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5188 | |
Willy Tarreau | 2737562 | 2013-12-17 00:00:28 +0100 | [diff] [blame] | 5189 | /* we're in keep-alive with an idle connection, monitor it */ |
Willy Tarreau | 4320eaa | 2015-08-05 11:08:30 +0200 | [diff] [blame] | 5190 | if (srv_conn) { |
Willy Tarreau | 323a2d9 | 2015-08-04 19:00:17 +0200 | [diff] [blame] | 5191 | srv = objt_server(srv_conn->target); |
Willy Tarreau | 8dff998 | 2015-08-04 20:45:52 +0200 | [diff] [blame] | 5192 | if (!srv) |
| 5193 | si_idle_conn(&s->si[1], NULL); |
| 5194 | else if ((srv_conn->flags & CO_FL_PRIVATE) || |
| 5195 | ((s->be->options & PR_O_REUSE_MASK) == PR_O_REUSE_NEVR)) |
| 5196 | si_idle_conn(&s->si[1], &srv->priv_conns); |
| 5197 | else |
| 5198 | si_idle_conn(&s->si[1], &srv->idle_conns); |
Willy Tarreau | 4320eaa | 2015-08-05 11:08:30 +0200 | [diff] [blame] | 5199 | } |
Willy Tarreau | 2737562 | 2013-12-17 00:00:28 +0100 | [diff] [blame] | 5200 | |
Willy Tarreau | d0d8da9 | 2015-04-04 02:10:38 +0200 | [diff] [blame] | 5201 | s->req.analysers = strm_li(s)->analysers; |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 5202 | s->res.analysers = 0; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5203 | } |
| 5204 | |
| 5205 | |
| 5206 | /* This function updates the request state machine according to the response |
| 5207 | * state machine and buffer flags. It returns 1 if it changes anything (flag |
| 5208 | * or state), otherwise zero. It ignores any state before HTTP_MSG_DONE, as |
| 5209 | * it is only used to find when a request/response couple is complete. Both |
| 5210 | * this function and its equivalent should loop until both return zero. It |
| 5211 | * can set its own state to DONE, CLOSING, CLOSED, TUNNEL, ERROR. |
| 5212 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 5213 | int http_sync_req_state(struct stream *s) |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5214 | { |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 5215 | struct channel *chn = &s->req; |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 5216 | struct http_txn *txn = s->txn; |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 5217 | unsigned int old_flags = chn->flags; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5218 | unsigned int old_state = txn->req.msg_state; |
| 5219 | |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5220 | if (unlikely(txn->req.msg_state < HTTP_MSG_BODY)) |
| 5221 | return 0; |
| 5222 | |
| 5223 | if (txn->req.msg_state == HTTP_MSG_DONE) { |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 5224 | /* No need to read anymore, the request was completely parsed. |
Willy Tarreau | 58bd8fd | 2010-09-28 14:16:41 +0200 | [diff] [blame] | 5225 | * We can shut the read side unless we want to abort_on_close, |
| 5226 | * or we have a POST request. The issue with POST requests is |
| 5227 | * that some browsers still send a CRLF after the request, and |
| 5228 | * this CRLF must be read so that it does not remain in the kernel |
| 5229 | * buffers, otherwise a close could cause an RST on some systems |
| 5230 | * (eg: Linux). |
Willy Tarreau | 3988d93 | 2013-12-27 23:03:08 +0100 | [diff] [blame] | 5231 | * Note that if we're using keep-alive on the client side, we'd |
| 5232 | * rather poll now and keep the polling enabled for the whole |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 5233 | * stream's life than enabling/disabling it between each |
Willy Tarreau | 3988d93 | 2013-12-27 23:03:08 +0100 | [diff] [blame] | 5234 | * response and next request. |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 5235 | */ |
Willy Tarreau | 3988d93 | 2013-12-27 23:03:08 +0100 | [diff] [blame] | 5236 | if (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_SCL) && |
| 5237 | ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) && |
| 5238 | !(s->be->options & PR_O_ABRT_CLOSE) && |
| 5239 | txn->meth != HTTP_METH_POST) |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 5240 | channel_dont_read(chn); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5241 | |
Willy Tarreau | 40f151a | 2012-12-20 12:10:09 +0100 | [diff] [blame] | 5242 | /* if the server closes the connection, we want to immediately react |
| 5243 | * and close the socket to save packets and syscalls. |
| 5244 | */ |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 5245 | s->si[1].flags |= SI_FL_NOHALF; |
Willy Tarreau | 40f151a | 2012-12-20 12:10:09 +0100 | [diff] [blame] | 5246 | |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5247 | if (txn->rsp.msg_state == HTTP_MSG_ERROR) |
| 5248 | goto wait_other_side; |
| 5249 | |
| 5250 | if (txn->rsp.msg_state < HTTP_MSG_DONE) { |
| 5251 | /* The server has not finished to respond, so we |
| 5252 | * don't want to move in order not to upset it. |
| 5253 | */ |
| 5254 | goto wait_other_side; |
| 5255 | } |
| 5256 | |
| 5257 | if (txn->rsp.msg_state == HTTP_MSG_TUNNEL) { |
| 5258 | /* if any side switches to tunnel mode, the other one does too */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 5259 | channel_auto_read(chn); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5260 | txn->req.msg_state = HTTP_MSG_TUNNEL; |
Willy Tarreau | fc47f91 | 2012-10-20 10:38:09 +0200 | [diff] [blame] | 5261 | chn->flags |= CF_NEVER_WAIT; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5262 | goto wait_other_side; |
| 5263 | } |
| 5264 | |
| 5265 | /* When we get here, it means that both the request and the |
| 5266 | * response have finished receiving. Depending on the connection |
| 5267 | * mode, we'll have to wait for the last bytes to leave in either |
| 5268 | * direction, and sometimes for a close to be effective. |
| 5269 | */ |
| 5270 | |
Willy Tarreau | cce7fa4 | 2010-01-16 23:19:39 +0100 | [diff] [blame] | 5271 | if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) { |
| 5272 | /* Server-close mode : queue a connection close to the server */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 5273 | if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW))) |
| 5274 | channel_shutw_now(chn); |
Willy Tarreau | cce7fa4 | 2010-01-16 23:19:39 +0100 | [diff] [blame] | 5275 | } |
| 5276 | else if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) { |
| 5277 | /* Option forceclose is set, or either side wants to close, |
| 5278 | * let's enforce it now that we're not expecting any new |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 5279 | * data to come. The caller knows the stream is complete |
Willy Tarreau | cce7fa4 | 2010-01-16 23:19:39 +0100 | [diff] [blame] | 5280 | * once both states are CLOSED. |
| 5281 | */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 5282 | if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW))) { |
| 5283 | channel_shutr_now(chn); |
| 5284 | channel_shutw_now(chn); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5285 | } |
Willy Tarreau | cce7fa4 | 2010-01-16 23:19:39 +0100 | [diff] [blame] | 5286 | } |
| 5287 | else { |
Willy Tarreau | 4213a11 | 2013-12-15 10:25:42 +0100 | [diff] [blame] | 5288 | /* The last possible modes are keep-alive and tunnel. Tunnel mode |
| 5289 | * will not have any analyser so it needs to poll for reads. |
Willy Tarreau | cce7fa4 | 2010-01-16 23:19:39 +0100 | [diff] [blame] | 5290 | */ |
Willy Tarreau | 4213a11 | 2013-12-15 10:25:42 +0100 | [diff] [blame] | 5291 | if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN) { |
| 5292 | channel_auto_read(chn); |
| 5293 | txn->req.msg_state = HTTP_MSG_TUNNEL; |
| 5294 | chn->flags |= CF_NEVER_WAIT; |
| 5295 | } |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5296 | } |
| 5297 | |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 5298 | if (chn->flags & (CF_SHUTW|CF_SHUTW_NOW)) { |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5299 | /* if we've just closed an output, let's switch */ |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 5300 | s->si[1].flags |= SI_FL_NOLINGER; /* we want to close ASAP */ |
Willy Tarreau | cce7fa4 | 2010-01-16 23:19:39 +0100 | [diff] [blame] | 5301 | |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 5302 | if (!channel_is_empty(chn)) { |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5303 | txn->req.msg_state = HTTP_MSG_CLOSING; |
| 5304 | goto http_msg_closing; |
| 5305 | } |
| 5306 | else { |
| 5307 | txn->req.msg_state = HTTP_MSG_CLOSED; |
| 5308 | goto http_msg_closed; |
| 5309 | } |
| 5310 | } |
Willy Tarreau | cce7fa4 | 2010-01-16 23:19:39 +0100 | [diff] [blame] | 5311 | goto wait_other_side; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5312 | } |
| 5313 | |
| 5314 | if (txn->req.msg_state == HTTP_MSG_CLOSING) { |
| 5315 | http_msg_closing: |
| 5316 | /* nothing else to forward, just waiting for the output buffer |
| 5317 | * to be empty and for the shutw_now to take effect. |
| 5318 | */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 5319 | if (channel_is_empty(chn)) { |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5320 | txn->req.msg_state = HTTP_MSG_CLOSED; |
| 5321 | goto http_msg_closed; |
| 5322 | } |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 5323 | else if (chn->flags & CF_SHUTW) { |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5324 | txn->req.msg_state = HTTP_MSG_ERROR; |
| 5325 | goto wait_other_side; |
| 5326 | } |
| 5327 | } |
| 5328 | |
| 5329 | if (txn->req.msg_state == HTTP_MSG_CLOSED) { |
| 5330 | http_msg_closed: |
Willy Tarreau | 3988d93 | 2013-12-27 23:03:08 +0100 | [diff] [blame] | 5331 | /* see above in MSG_DONE why we only do this in these states */ |
| 5332 | if (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_SCL) && |
| 5333 | ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) && |
| 5334 | !(s->be->options & PR_O_ABRT_CLOSE)) |
Willy Tarreau | 2e7a165 | 2013-12-15 15:32:10 +0100 | [diff] [blame] | 5335 | channel_dont_read(chn); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5336 | goto wait_other_side; |
| 5337 | } |
| 5338 | |
| 5339 | wait_other_side: |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 5340 | return txn->req.msg_state != old_state || chn->flags != old_flags; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5341 | } |
| 5342 | |
| 5343 | |
| 5344 | /* This function updates the response state machine according to the request |
| 5345 | * state machine and buffer flags. It returns 1 if it changes anything (flag |
| 5346 | * or state), otherwise zero. It ignores any state before HTTP_MSG_DONE, as |
| 5347 | * it is only used to find when a request/response couple is complete. Both |
| 5348 | * this function and its equivalent should loop until both return zero. It |
| 5349 | * can set its own state to DONE, CLOSING, CLOSED, TUNNEL, ERROR. |
| 5350 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 5351 | int http_sync_res_state(struct stream *s) |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5352 | { |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 5353 | struct channel *chn = &s->res; |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 5354 | struct http_txn *txn = s->txn; |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 5355 | unsigned int old_flags = chn->flags; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5356 | unsigned int old_state = txn->rsp.msg_state; |
| 5357 | |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5358 | if (unlikely(txn->rsp.msg_state < HTTP_MSG_BODY)) |
| 5359 | return 0; |
| 5360 | |
| 5361 | if (txn->rsp.msg_state == HTTP_MSG_DONE) { |
| 5362 | /* In theory, we don't need to read anymore, but we must |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 5363 | * still monitor the server connection for a possible close |
| 5364 | * while the request is being uploaded, so we don't disable |
| 5365 | * reading. |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5366 | */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 5367 | /* channel_dont_read(chn); */ |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5368 | |
| 5369 | if (txn->req.msg_state == HTTP_MSG_ERROR) |
| 5370 | goto wait_other_side; |
| 5371 | |
| 5372 | if (txn->req.msg_state < HTTP_MSG_DONE) { |
| 5373 | /* The client seems to still be sending data, probably |
| 5374 | * because we got an error response during an upload. |
| 5375 | * We have the choice of either breaking the connection |
| 5376 | * or letting it pass through. Let's do the later. |
| 5377 | */ |
| 5378 | goto wait_other_side; |
| 5379 | } |
| 5380 | |
| 5381 | if (txn->req.msg_state == HTTP_MSG_TUNNEL) { |
| 5382 | /* if any side switches to tunnel mode, the other one does too */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 5383 | channel_auto_read(chn); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5384 | txn->rsp.msg_state = HTTP_MSG_TUNNEL; |
Willy Tarreau | fc47f91 | 2012-10-20 10:38:09 +0200 | [diff] [blame] | 5385 | chn->flags |= CF_NEVER_WAIT; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5386 | goto wait_other_side; |
| 5387 | } |
| 5388 | |
| 5389 | /* When we get here, it means that both the request and the |
| 5390 | * response have finished receiving. Depending on the connection |
| 5391 | * mode, we'll have to wait for the last bytes to leave in either |
| 5392 | * direction, and sometimes for a close to be effective. |
| 5393 | */ |
| 5394 | |
| 5395 | if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) { |
| 5396 | /* Server-close mode : shut read and wait for the request |
| 5397 | * side to close its output buffer. The caller will detect |
| 5398 | * when we're in DONE and the other is in CLOSED and will |
| 5399 | * catch that for the final cleanup. |
| 5400 | */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 5401 | if (!(chn->flags & (CF_SHUTR|CF_SHUTR_NOW))) |
| 5402 | channel_shutr_now(chn); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5403 | } |
Willy Tarreau | cce7fa4 | 2010-01-16 23:19:39 +0100 | [diff] [blame] | 5404 | else if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) { |
| 5405 | /* Option forceclose is set, or either side wants to close, |
| 5406 | * let's enforce it now that we're not expecting any new |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 5407 | * data to come. The caller knows the stream is complete |
Willy Tarreau | cce7fa4 | 2010-01-16 23:19:39 +0100 | [diff] [blame] | 5408 | * once both states are CLOSED. |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5409 | */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 5410 | if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW))) { |
| 5411 | channel_shutr_now(chn); |
| 5412 | channel_shutw_now(chn); |
Willy Tarreau | cce7fa4 | 2010-01-16 23:19:39 +0100 | [diff] [blame] | 5413 | } |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5414 | } |
| 5415 | else { |
Willy Tarreau | 4213a11 | 2013-12-15 10:25:42 +0100 | [diff] [blame] | 5416 | /* The last possible modes are keep-alive and tunnel. Tunnel will |
| 5417 | * need to forward remaining data. Keep-alive will need to monitor |
| 5418 | * for connection closing. |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5419 | */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 5420 | channel_auto_read(chn); |
Willy Tarreau | fc47f91 | 2012-10-20 10:38:09 +0200 | [diff] [blame] | 5421 | chn->flags |= CF_NEVER_WAIT; |
Willy Tarreau | 4213a11 | 2013-12-15 10:25:42 +0100 | [diff] [blame] | 5422 | if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN) |
| 5423 | txn->rsp.msg_state = HTTP_MSG_TUNNEL; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5424 | } |
| 5425 | |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 5426 | if (chn->flags & (CF_SHUTW|CF_SHUTW_NOW)) { |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5427 | /* if we've just closed an output, let's switch */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 5428 | if (!channel_is_empty(chn)) { |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5429 | txn->rsp.msg_state = HTTP_MSG_CLOSING; |
| 5430 | goto http_msg_closing; |
| 5431 | } |
| 5432 | else { |
| 5433 | txn->rsp.msg_state = HTTP_MSG_CLOSED; |
| 5434 | goto http_msg_closed; |
| 5435 | } |
| 5436 | } |
| 5437 | goto wait_other_side; |
| 5438 | } |
| 5439 | |
| 5440 | if (txn->rsp.msg_state == HTTP_MSG_CLOSING) { |
| 5441 | http_msg_closing: |
| 5442 | /* nothing else to forward, just waiting for the output buffer |
| 5443 | * to be empty and for the shutw_now to take effect. |
| 5444 | */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 5445 | if (channel_is_empty(chn)) { |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5446 | txn->rsp.msg_state = HTTP_MSG_CLOSED; |
| 5447 | goto http_msg_closed; |
| 5448 | } |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 5449 | else if (chn->flags & CF_SHUTW) { |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5450 | txn->rsp.msg_state = HTTP_MSG_ERROR; |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 5451 | s->be->be_counters.cli_aborts++; |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 5452 | if (objt_server(s->target)) |
| 5453 | objt_server(s->target)->counters.cli_aborts++; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5454 | goto wait_other_side; |
| 5455 | } |
| 5456 | } |
| 5457 | |
| 5458 | if (txn->rsp.msg_state == HTTP_MSG_CLOSED) { |
| 5459 | http_msg_closed: |
| 5460 | /* drop any pending data */ |
Willy Tarreau | 319f745 | 2015-01-14 20:32:59 +0100 | [diff] [blame] | 5461 | channel_truncate(chn); |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 5462 | channel_auto_close(chn); |
| 5463 | channel_auto_read(chn); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5464 | goto wait_other_side; |
| 5465 | } |
| 5466 | |
| 5467 | wait_other_side: |
Willy Tarreau | fc47f91 | 2012-10-20 10:38:09 +0200 | [diff] [blame] | 5468 | /* We force the response to leave immediately if we're waiting for the |
| 5469 | * other side, since there is no pending shutdown to push it out. |
| 5470 | */ |
| 5471 | if (!channel_is_empty(chn)) |
| 5472 | chn->flags |= CF_SEND_DONTWAIT; |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 5473 | return txn->rsp.msg_state != old_state || chn->flags != old_flags; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5474 | } |
| 5475 | |
| 5476 | |
| 5477 | /* Resync the request and response state machines. Return 1 if either state |
| 5478 | * changes. |
| 5479 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 5480 | int http_resync_states(struct stream *s) |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5481 | { |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 5482 | struct http_txn *txn = s->txn; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5483 | int old_req_state = txn->req.msg_state; |
| 5484 | int old_res_state = txn->rsp.msg_state; |
| 5485 | |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5486 | http_sync_req_state(s); |
| 5487 | while (1) { |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5488 | if (!http_sync_res_state(s)) |
| 5489 | break; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5490 | if (!http_sync_req_state(s)) |
| 5491 | break; |
| 5492 | } |
Willy Tarreau | 3ce10ff | 2014-04-22 08:24:38 +0200 | [diff] [blame] | 5493 | |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5494 | /* OK, both state machines agree on a compatible state. |
| 5495 | * There are a few cases we're interested in : |
| 5496 | * - HTTP_MSG_TUNNEL on either means we have to disable both analysers |
| 5497 | * - HTTP_MSG_CLOSED on both sides means we've reached the end in both |
| 5498 | * directions, so let's simply disable both analysers. |
| 5499 | * - HTTP_MSG_CLOSED on the response only means we must abort the |
| 5500 | * request. |
| 5501 | * - HTTP_MSG_CLOSED on the request and HTTP_MSG_DONE on the response |
| 5502 | * with server-close mode means we've completed one request and we |
| 5503 | * must re-initialize the server connection. |
| 5504 | */ |
| 5505 | |
| 5506 | if (txn->req.msg_state == HTTP_MSG_TUNNEL || |
| 5507 | txn->rsp.msg_state == HTTP_MSG_TUNNEL || |
| 5508 | (txn->req.msg_state == HTTP_MSG_CLOSED && |
| 5509 | txn->rsp.msg_state == HTTP_MSG_CLOSED)) { |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 5510 | s->req.analysers = 0; |
| 5511 | channel_auto_close(&s->req); |
| 5512 | channel_auto_read(&s->req); |
| 5513 | s->res.analysers = 0; |
| 5514 | channel_auto_close(&s->res); |
| 5515 | channel_auto_read(&s->res); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5516 | } |
Willy Tarreau | 40f151a | 2012-12-20 12:10:09 +0100 | [diff] [blame] | 5517 | else if ((txn->req.msg_state >= HTTP_MSG_DONE && |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 5518 | (txn->rsp.msg_state == HTTP_MSG_CLOSED || (s->res.flags & CF_SHUTW))) || |
Willy Tarreau | 2fa144c | 2010-01-04 23:13:26 +0100 | [diff] [blame] | 5519 | txn->rsp.msg_state == HTTP_MSG_ERROR || |
Willy Tarreau | 40f151a | 2012-12-20 12:10:09 +0100 | [diff] [blame] | 5520 | txn->req.msg_state == HTTP_MSG_ERROR) { |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 5521 | s->res.analysers = 0; |
| 5522 | channel_auto_close(&s->res); |
| 5523 | channel_auto_read(&s->res); |
| 5524 | s->req.analysers = 0; |
| 5525 | channel_abort(&s->req); |
| 5526 | channel_auto_close(&s->req); |
| 5527 | channel_auto_read(&s->req); |
| 5528 | channel_truncate(&s->req); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5529 | } |
Willy Tarreau | 4213a11 | 2013-12-15 10:25:42 +0100 | [diff] [blame] | 5530 | else if ((txn->req.msg_state == HTTP_MSG_DONE || |
| 5531 | txn->req.msg_state == HTTP_MSG_CLOSED) && |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5532 | txn->rsp.msg_state == HTTP_MSG_DONE && |
Willy Tarreau | 4213a11 | 2013-12-15 10:25:42 +0100 | [diff] [blame] | 5533 | ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL || |
| 5534 | (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL)) { |
| 5535 | /* server-close/keep-alive: terminate this transaction, |
| 5536 | * possibly killing the server connection and reinitialize |
| 5537 | * a fresh-new transaction. |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5538 | */ |
| 5539 | http_end_txn_clean_session(s); |
| 5540 | } |
| 5541 | |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5542 | return txn->req.msg_state != old_req_state || |
| 5543 | txn->rsp.msg_state != old_res_state; |
| 5544 | } |
| 5545 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5546 | /* This function is an analyser which forwards request body (including chunk |
| 5547 | * sizes if any). It is called as soon as we must forward, even if we forward |
| 5548 | * zero byte. The only situation where it must not be called is when we're in |
| 5549 | * tunnel mode and we want to forward till the close. It's used both to forward |
| 5550 | * remaining data and to resync after end of body. It expects the msg_state to |
| 5551 | * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 5552 | * read more data, or 1 once we can go on with next request or end the stream. |
Willy Tarreau | 124d991 | 2011-03-01 20:30:48 +0100 | [diff] [blame] | 5553 | * When in MSG_DATA or MSG_TRAILERS, it will automatically forward chunk_len |
Willy Tarreau | c24715e | 2014-04-17 15:21:20 +0200 | [diff] [blame] | 5554 | * bytes of pending data + the headers if not already done. |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5555 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 5556 | int http_request_forward_body(struct stream *s, struct channel *req, int an_bit) |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5557 | { |
Willy Tarreau | fb0afa7 | 2015-04-03 14:46:27 +0200 | [diff] [blame] | 5558 | struct session *sess = s->sess; |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 5559 | struct http_txn *txn = s->txn; |
| 5560 | struct http_msg *msg = &s->txn->req; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5561 | |
Willy Tarreau | f5c8bd6 | 2010-01-04 07:10:34 +0100 | [diff] [blame] | 5562 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) |
| 5563 | return 0; |
| 5564 | |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 5565 | if ((req->flags & (CF_READ_ERROR|CF_READ_TIMEOUT|CF_WRITE_ERROR|CF_WRITE_TIMEOUT)) || |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 5566 | ((req->flags & CF_SHUTW) && (req->to_forward || req->buf->o))) { |
Willy Tarreau | 4fe4190 | 2010-06-07 22:27:41 +0200 | [diff] [blame] | 5567 | /* Output closed while we were sending data. We must abort and |
| 5568 | * wake the other side up. |
| 5569 | */ |
| 5570 | msg->msg_state = HTTP_MSG_ERROR; |
| 5571 | http_resync_states(s); |
Willy Tarreau | 082b01c | 2010-01-02 23:58:04 +0100 | [diff] [blame] | 5572 | return 1; |
| 5573 | } |
| 5574 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5575 | /* Note that we don't have to send 100-continue back because we don't |
| 5576 | * need the data to complete our job, and it's up to the server to |
| 5577 | * decide whether to return 100, 417 or anything else in return of |
| 5578 | * an "Expect: 100-continue" header. |
| 5579 | */ |
| 5580 | |
Willy Tarreau | bb2e669 | 2014-07-10 19:06:10 +0200 | [diff] [blame] | 5581 | if (msg->sov > 0) { |
Willy Tarreau | b59c7bf | 2014-04-22 14:29:58 +0200 | [diff] [blame] | 5582 | /* we have msg->sov which points to the first byte of message |
| 5583 | * body, and req->buf.p still points to the beginning of the |
| 5584 | * message. We forward the headers now, as we don't need them |
| 5585 | * anymore, and we want to flush them. |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5586 | */ |
Willy Tarreau | b59c7bf | 2014-04-22 14:29:58 +0200 | [diff] [blame] | 5587 | b_adv(req->buf, msg->sov); |
| 5588 | msg->next -= msg->sov; |
| 5589 | msg->sov = 0; |
Willy Tarreau | a458b67 | 2012-03-05 11:17:50 +0100 | [diff] [blame] | 5590 | |
Willy Tarreau | b59c7bf | 2014-04-22 14:29:58 +0200 | [diff] [blame] | 5591 | /* The previous analysers guarantee that the state is somewhere |
| 5592 | * between MSG_BODY and the first MSG_DATA. So msg->sol and |
| 5593 | * msg->next are always correct. |
| 5594 | */ |
| 5595 | if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) { |
| 5596 | if (msg->flags & HTTP_MSGF_TE_CHNK) |
| 5597 | msg->msg_state = HTTP_MSG_CHUNK_SIZE; |
| 5598 | else |
| 5599 | msg->msg_state = HTTP_MSG_DATA; |
| 5600 | } |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5601 | } |
| 5602 | |
Willy Tarreau | 7ba2354 | 2014-04-17 21:50:00 +0200 | [diff] [blame] | 5603 | /* Some post-connect processing might want us to refrain from starting to |
| 5604 | * forward data. Currently, the only reason for this is "balance url_param" |
| 5605 | * whichs need to parse/process the request after we've enabled forwarding. |
| 5606 | */ |
| 5607 | if (unlikely(msg->flags & HTTP_MSGF_WAIT_CONN)) { |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 5608 | if (!(s->res.flags & CF_READ_ATTACHED)) { |
Willy Tarreau | 7ba2354 | 2014-04-17 21:50:00 +0200 | [diff] [blame] | 5609 | channel_auto_connect(req); |
Willy Tarreau | 644c101 | 2014-04-30 18:11:11 +0200 | [diff] [blame] | 5610 | req->flags |= CF_WAKE_CONNECT; |
Willy Tarreau | 7ba2354 | 2014-04-17 21:50:00 +0200 | [diff] [blame] | 5611 | goto missing_data; |
| 5612 | } |
| 5613 | msg->flags &= ~HTTP_MSGF_WAIT_CONN; |
| 5614 | } |
| 5615 | |
Willy Tarreau | 80a92c0 | 2014-03-12 10:41:13 +0100 | [diff] [blame] | 5616 | /* in most states, we should abort in case of early close */ |
| 5617 | channel_auto_close(req); |
| 5618 | |
Willy Tarreau | efdf094 | 2014-04-24 20:08:57 +0200 | [diff] [blame] | 5619 | if (req->to_forward) { |
| 5620 | /* We can't process the buffer's contents yet */ |
| 5621 | req->flags |= CF_WAKE_WRITE; |
| 5622 | goto missing_data; |
| 5623 | } |
| 5624 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5625 | while (1) { |
Willy Tarreau | caabe41 | 2010-01-03 23:08:28 +0100 | [diff] [blame] | 5626 | if (msg->msg_state == HTTP_MSG_DATA) { |
| 5627 | /* must still forward */ |
Willy Tarreau | bed410e | 2014-04-22 08:19:34 +0200 | [diff] [blame] | 5628 | /* we may have some pending data starting at req->buf->p */ |
| 5629 | if (msg->chunk_len > req->buf->i - msg->next) { |
Willy Tarreau | 4afd70a | 2014-01-25 02:26:39 +0100 | [diff] [blame] | 5630 | req->flags |= CF_WAKE_WRITE; |
Willy Tarreau | f5c8bd6 | 2010-01-04 07:10:34 +0100 | [diff] [blame] | 5631 | goto missing_data; |
Willy Tarreau | 4afd70a | 2014-01-25 02:26:39 +0100 | [diff] [blame] | 5632 | } |
Willy Tarreau | bed410e | 2014-04-22 08:19:34 +0200 | [diff] [blame] | 5633 | msg->next += msg->chunk_len; |
| 5634 | msg->chunk_len = 0; |
Willy Tarreau | caabe41 | 2010-01-03 23:08:28 +0100 | [diff] [blame] | 5635 | |
| 5636 | /* nothing left to forward */ |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 5637 | if (msg->flags & HTTP_MSGF_TE_CHNK) |
Willy Tarreau | 54d23df | 2012-10-25 19:04:45 +0200 | [diff] [blame] | 5638 | msg->msg_state = HTTP_MSG_CHUNK_CRLF; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5639 | else |
Willy Tarreau | caabe41 | 2010-01-03 23:08:28 +0100 | [diff] [blame] | 5640 | msg->msg_state = HTTP_MSG_DONE; |
Willy Tarreau | caabe41 | 2010-01-03 23:08:28 +0100 | [diff] [blame] | 5641 | } |
| 5642 | else if (msg->msg_state == HTTP_MSG_CHUNK_SIZE) { |
Willy Tarreau | 124d991 | 2011-03-01 20:30:48 +0100 | [diff] [blame] | 5643 | /* read the chunk size and assign it to ->chunk_len, then |
Willy Tarreau | c24715e | 2014-04-17 15:21:20 +0200 | [diff] [blame] | 5644 | * set ->next to point to the body and switch to DATA or |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5645 | * TRAILERS state. |
| 5646 | */ |
Willy Tarreau | 4baf44b | 2012-03-09 14:10:20 +0100 | [diff] [blame] | 5647 | int ret = http_parse_chunk_size(msg); |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5648 | |
Willy Tarreau | 54d23df | 2012-10-25 19:04:45 +0200 | [diff] [blame] | 5649 | if (ret == 0) |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5650 | goto missing_data; |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 5651 | else if (ret < 0) { |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 5652 | stream_inc_http_err_ctr(s); |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 5653 | if (msg->err_pos >= 0) |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 5654 | http_capture_bad_message(&sess->fe->invalid_req, s, msg, HTTP_MSG_CHUNK_SIZE, s->be); |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5655 | goto return_bad_req; |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 5656 | } |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5657 | /* otherwise we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state */ |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5658 | } |
Willy Tarreau | 54d23df | 2012-10-25 19:04:45 +0200 | [diff] [blame] | 5659 | else if (msg->msg_state == HTTP_MSG_CHUNK_CRLF) { |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5660 | /* we want the CRLF after the data */ |
Willy Tarreau | 54d23df | 2012-10-25 19:04:45 +0200 | [diff] [blame] | 5661 | int ret = http_skip_chunk_crlf(msg); |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5662 | |
| 5663 | if (ret == 0) |
| 5664 | goto missing_data; |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 5665 | else if (ret < 0) { |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 5666 | stream_inc_http_err_ctr(s); |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 5667 | if (msg->err_pos >= 0) |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 5668 | http_capture_bad_message(&sess->fe->invalid_req, s, msg, HTTP_MSG_CHUNK_CRLF, s->be); |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5669 | goto return_bad_req; |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 5670 | } |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5671 | /* we're in MSG_CHUNK_SIZE now */ |
| 5672 | } |
| 5673 | else if (msg->msg_state == HTTP_MSG_TRAILERS) { |
Willy Tarreau | 4baf44b | 2012-03-09 14:10:20 +0100 | [diff] [blame] | 5674 | int ret = http_forward_trailers(msg); |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5675 | |
| 5676 | if (ret == 0) |
| 5677 | goto missing_data; |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 5678 | else if (ret < 0) { |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 5679 | stream_inc_http_err_ctr(s); |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 5680 | if (msg->err_pos >= 0) |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 5681 | http_capture_bad_message(&sess->fe->invalid_req, s, msg, HTTP_MSG_TRAILERS, s->be); |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5682 | goto return_bad_req; |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 5683 | } |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5684 | /* we're in HTTP_MSG_DONE now */ |
| 5685 | } |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5686 | else { |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 5687 | int old_state = msg->msg_state; |
| 5688 | |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5689 | /* other states, DONE...TUNNEL */ |
Willy Tarreau | bed410e | 2014-04-22 08:19:34 +0200 | [diff] [blame] | 5690 | |
| 5691 | /* we may have some pending data starting at req->buf->p |
| 5692 | * such as last chunk of data or trailers. |
| 5693 | */ |
| 5694 | b_adv(req->buf, msg->next); |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 5695 | if (unlikely(!(s->req.flags & CF_WROTE_DATA))) |
Willy Tarreau | bb2e669 | 2014-07-10 19:06:10 +0200 | [diff] [blame] | 5696 | msg->sov -= msg->next; |
Willy Tarreau | bed410e | 2014-04-22 08:19:34 +0200 | [diff] [blame] | 5697 | msg->next = 0; |
| 5698 | |
Willy Tarreau | bbfb6c4 | 2015-05-11 18:30:33 +0200 | [diff] [blame] | 5699 | /* we don't want to forward closes on DONE except in |
| 5700 | * tunnel mode. |
| 5701 | */ |
| 5702 | if ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 5703 | channel_dont_close(req); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5704 | if (http_resync_states(s)) { |
| 5705 | /* some state changes occurred, maybe the analyser |
| 5706 | * was disabled too. |
Willy Tarreau | face839 | 2010-01-03 11:37:54 +0100 | [diff] [blame] | 5707 | */ |
Willy Tarreau | 3fe693b | 2010-12-12 12:50:05 +0100 | [diff] [blame] | 5708 | if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) { |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 5709 | if (req->flags & CF_SHUTW) { |
Willy Tarreau | 3fe693b | 2010-12-12 12:50:05 +0100 | [diff] [blame] | 5710 | /* request errors are most likely due to |
| 5711 | * the server aborting the transfer. |
| 5712 | */ |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 5713 | goto aborted_xfer; |
Willy Tarreau | 3fe693b | 2010-12-12 12:50:05 +0100 | [diff] [blame] | 5714 | } |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 5715 | if (msg->err_pos >= 0) |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 5716 | http_capture_bad_message(&sess->fe->invalid_req, s, msg, old_state, s->be); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5717 | goto return_bad_req; |
Willy Tarreau | 3fe693b | 2010-12-12 12:50:05 +0100 | [diff] [blame] | 5718 | } |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5719 | return 1; |
Willy Tarreau | b608feb | 2010-01-02 22:47:18 +0100 | [diff] [blame] | 5720 | } |
Willy Tarreau | 5c54c71 | 2010-07-17 08:02:58 +0200 | [diff] [blame] | 5721 | |
| 5722 | /* If "option abortonclose" is set on the backend, we |
| 5723 | * want to monitor the client's connection and forward |
| 5724 | * any shutdown notification to the server, which will |
| 5725 | * decide whether to close or to go on processing the |
Willy Tarreau | bbfb6c4 | 2015-05-11 18:30:33 +0200 | [diff] [blame] | 5726 | * request. We only do that in tunnel mode, and not in |
| 5727 | * other modes since it can be abused to exhaust source |
| 5728 | * ports. |
Willy Tarreau | 5c54c71 | 2010-07-17 08:02:58 +0200 | [diff] [blame] | 5729 | */ |
| 5730 | if (s->be->options & PR_O_ABRT_CLOSE) { |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 5731 | channel_auto_read(req); |
Willy Tarreau | bbfb6c4 | 2015-05-11 18:30:33 +0200 | [diff] [blame] | 5732 | if ((req->flags & (CF_SHUTR|CF_READ_NULL)) && |
| 5733 | ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN)) |
| 5734 | s->si[1].flags |= SI_FL_NOLINGER; |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 5735 | channel_auto_close(req); |
Willy Tarreau | 5c54c71 | 2010-07-17 08:02:58 +0200 | [diff] [blame] | 5736 | } |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 5737 | else if (s->txn->meth == HTTP_METH_POST) { |
Willy Tarreau | 58bd8fd | 2010-09-28 14:16:41 +0200 | [diff] [blame] | 5738 | /* POST requests may require to read extra CRLF |
| 5739 | * sent by broken browsers and which could cause |
| 5740 | * an RST to be sent upon close on some systems |
| 5741 | * (eg: Linux). |
| 5742 | */ |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 5743 | channel_auto_read(req); |
Willy Tarreau | 58bd8fd | 2010-09-28 14:16:41 +0200 | [diff] [blame] | 5744 | } |
Willy Tarreau | 5c54c71 | 2010-07-17 08:02:58 +0200 | [diff] [blame] | 5745 | |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5746 | return 0; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5747 | } |
| 5748 | } |
| 5749 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5750 | missing_data: |
Willy Tarreau | bed410e | 2014-04-22 08:19:34 +0200 | [diff] [blame] | 5751 | /* we may have some pending data starting at req->buf->p */ |
| 5752 | b_adv(req->buf, msg->next); |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 5753 | if (unlikely(!(s->req.flags & CF_WROTE_DATA))) |
Willy Tarreau | bb2e669 | 2014-07-10 19:06:10 +0200 | [diff] [blame] | 5754 | msg->sov -= msg->next + MIN(msg->chunk_len, req->buf->i); |
| 5755 | |
Willy Tarreau | bed410e | 2014-04-22 08:19:34 +0200 | [diff] [blame] | 5756 | msg->next = 0; |
| 5757 | msg->chunk_len -= channel_forward(req, msg->chunk_len); |
| 5758 | |
Willy Tarreau | f5c8bd6 | 2010-01-04 07:10:34 +0100 | [diff] [blame] | 5759 | /* stop waiting for data if the input is closed before the end */ |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 5760 | if (req->flags & CF_SHUTR) { |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 5761 | if (!(s->flags & SF_ERR_MASK)) |
| 5762 | s->flags |= SF_ERR_CLICL; |
| 5763 | if (!(s->flags & SF_FINST_MASK)) { |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 5764 | if (txn->rsp.msg_state < HTTP_MSG_ERROR) |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 5765 | s->flags |= SF_FINST_H; |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 5766 | else |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 5767 | s->flags |= SF_FINST_D; |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 5768 | } |
| 5769 | |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 5770 | sess->fe->fe_counters.cli_aborts++; |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 5771 | s->be->be_counters.cli_aborts++; |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 5772 | if (objt_server(s->target)) |
| 5773 | objt_server(s->target)->counters.cli_aborts++; |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 5774 | |
| 5775 | goto return_bad_req_stats_ok; |
Willy Tarreau | 79ebac6 | 2010-06-07 13:47:49 +0200 | [diff] [blame] | 5776 | } |
Willy Tarreau | f5c8bd6 | 2010-01-04 07:10:34 +0100 | [diff] [blame] | 5777 | |
Willy Tarreau | f5c8bd6 | 2010-01-04 07:10:34 +0100 | [diff] [blame] | 5778 | /* waiting for the last bits to leave the buffer */ |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 5779 | if (req->flags & CF_SHUTW) |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 5780 | goto aborted_xfer; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5781 | |
Willy Tarreau | 92aa1fa | 2010-08-28 18:57:20 +0200 | [diff] [blame] | 5782 | /* When TE: chunked is used, we need to get there again to parse remaining |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 5783 | * chunks even if the client has closed, so we don't want to set CF_DONTCLOSE. |
Willy Tarreau | 92aa1fa | 2010-08-28 18:57:20 +0200 | [diff] [blame] | 5784 | */ |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 5785 | if (msg->flags & HTTP_MSGF_TE_CHNK) |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 5786 | channel_dont_close(req); |
Willy Tarreau | 92aa1fa | 2010-08-28 18:57:20 +0200 | [diff] [blame] | 5787 | |
Willy Tarreau | 5c62092 | 2011-05-11 19:56:11 +0200 | [diff] [blame] | 5788 | /* We know that more data are expected, but we couldn't send more that |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 5789 | * what we did. So we always set the CF_EXPECT_MORE flag so that the |
Willy Tarreau | 0729303 | 2011-05-30 18:29:28 +0200 | [diff] [blame] | 5790 | * system knows it must not set a PUSH on this first part. Interactive |
Willy Tarreau | 869fc1e | 2012-03-05 08:29:20 +0100 | [diff] [blame] | 5791 | * modes are already handled by the stream sock layer. We must not do |
| 5792 | * this in content-length mode because it could present the MSG_MORE |
| 5793 | * flag with the last block of forwarded data, which would cause an |
| 5794 | * additional delay to be observed by the receiver. |
Willy Tarreau | 5c62092 | 2011-05-11 19:56:11 +0200 | [diff] [blame] | 5795 | */ |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 5796 | if (msg->flags & HTTP_MSGF_TE_CHNK) |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 5797 | req->flags |= CF_EXPECT_MORE; |
Willy Tarreau | 5c62092 | 2011-05-11 19:56:11 +0200 | [diff] [blame] | 5798 | |
Willy Tarreau | f5c8bd6 | 2010-01-04 07:10:34 +0100 | [diff] [blame] | 5799 | return 0; |
| 5800 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5801 | return_bad_req: /* let's centralize all bad requests */ |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 5802 | sess->fe->fe_counters.failed_req++; |
Willy Tarreau | fb0afa7 | 2015-04-03 14:46:27 +0200 | [diff] [blame] | 5803 | if (sess->listener->counters) |
| 5804 | sess->listener->counters->failed_req++; |
Willy Tarreau | bed410e | 2014-04-22 08:19:34 +0200 | [diff] [blame] | 5805 | |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 5806 | return_bad_req_stats_ok: |
Willy Tarreau | bed410e | 2014-04-22 08:19:34 +0200 | [diff] [blame] | 5807 | /* we may have some pending data starting at req->buf->p */ |
| 5808 | b_adv(req->buf, msg->next); |
| 5809 | msg->next = 0; |
| 5810 | |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 5811 | txn->req.msg_state = HTTP_MSG_ERROR; |
| 5812 | if (txn->status) { |
| 5813 | /* Note: we don't send any error if some data were already sent */ |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 5814 | stream_int_retnclose(&s->si[0], NULL); |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 5815 | } else { |
| 5816 | txn->status = 400; |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 5817 | stream_int_retnclose(&s->si[0], http_error_message(s, HTTP_ERR_400)); |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 5818 | } |
| 5819 | req->analysers = 0; |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 5820 | s->res.analysers = 0; /* we're in data phase, we want to abort both directions */ |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5821 | |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 5822 | if (!(s->flags & SF_ERR_MASK)) |
| 5823 | s->flags |= SF_ERR_PRXCOND; |
| 5824 | if (!(s->flags & SF_FINST_MASK)) { |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 5825 | if (txn->rsp.msg_state < HTTP_MSG_ERROR) |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 5826 | s->flags |= SF_FINST_H; |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 5827 | else |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 5828 | s->flags |= SF_FINST_D; |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 5829 | } |
| 5830 | return 0; |
| 5831 | |
| 5832 | aborted_xfer: |
| 5833 | txn->req.msg_state = HTTP_MSG_ERROR; |
| 5834 | if (txn->status) { |
| 5835 | /* Note: we don't send any error if some data were already sent */ |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 5836 | stream_int_retnclose(&s->si[0], NULL); |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 5837 | } else { |
| 5838 | txn->status = 502; |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 5839 | stream_int_retnclose(&s->si[0], http_error_message(s, HTTP_ERR_502)); |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 5840 | } |
| 5841 | req->analysers = 0; |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 5842 | s->res.analysers = 0; /* we're in data phase, we want to abort both directions */ |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 5843 | |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 5844 | sess->fe->fe_counters.srv_aborts++; |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 5845 | s->be->be_counters.srv_aborts++; |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 5846 | if (objt_server(s->target)) |
| 5847 | objt_server(s->target)->counters.srv_aborts++; |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 5848 | |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 5849 | if (!(s->flags & SF_ERR_MASK)) |
| 5850 | s->flags |= SF_ERR_SRVCL; |
| 5851 | if (!(s->flags & SF_FINST_MASK)) { |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 5852 | if (txn->rsp.msg_state < HTTP_MSG_ERROR) |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 5853 | s->flags |= SF_FINST_H; |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 5854 | else |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 5855 | s->flags |= SF_FINST_D; |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 5856 | } |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5857 | return 0; |
| 5858 | } |
| 5859 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5860 | /* This stream analyser waits for a complete HTTP response. It returns 1 if the |
| 5861 | * processing can continue on next analysers, or zero if it either needs more |
| 5862 | * data or wants to immediately abort the response (eg: timeout, error, ...). It |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 5863 | * is tied to AN_RES_WAIT_HTTP and may may remove itself from s->res.analysers |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5864 | * when it has nothing left to do, and may remove any analyser when it wants to |
| 5865 | * abort. |
Willy Tarreau | c65a3ba | 2008-08-11 23:42:50 +0200 | [diff] [blame] | 5866 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 5867 | int http_wait_for_response(struct stream *s, struct channel *rep, int an_bit) |
Willy Tarreau | c65a3ba | 2008-08-11 23:42:50 +0200 | [diff] [blame] | 5868 | { |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 5869 | struct session *sess = s->sess; |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 5870 | struct http_txn *txn = s->txn; |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5871 | struct http_msg *msg = &txn->rsp; |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 5872 | struct hdr_ctx ctx; |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 5873 | int use_close_only; |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5874 | int cur_idx; |
Krzysztof Piotr Oledzki | 5fb1882 | 2009-10-13 21:14:09 +0200 | [diff] [blame] | 5875 | int n; |
Willy Tarreau | adfb856 | 2008-08-11 15:24:42 +0200 | [diff] [blame] | 5876 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 5877 | DPRINTF(stderr,"[%u] %s: stream=%p b=%p, exp(r,w)=%u,%u bf=%08x bh=%d analysers=%02x\n", |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 5878 | now_ms, __FUNCTION__, |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5879 | s, |
Willy Tarreau | 3a16b2c | 2008-08-28 08:54:27 +0200 | [diff] [blame] | 5880 | rep, |
| 5881 | rep->rex, rep->wex, |
| 5882 | rep->flags, |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 5883 | rep->buf->i, |
Willy Tarreau | 3a16b2c | 2008-08-28 08:54:27 +0200 | [diff] [blame] | 5884 | rep->analysers); |
Willy Tarreau | 67f0eea | 2008-08-10 22:55:22 +0200 | [diff] [blame] | 5885 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5886 | /* |
| 5887 | * Now parse the partial (or complete) lines. |
| 5888 | * We will check the response syntax, and also join multi-line |
| 5889 | * headers. An index of all the lines will be elaborated while |
| 5890 | * parsing. |
| 5891 | * |
| 5892 | * For the parsing, we use a 28 states FSM. |
| 5893 | * |
| 5894 | * Here is the information we currently have : |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 5895 | * rep->buf->p = beginning of response |
| 5896 | * rep->buf->p + msg->eoh = end of processed headers / start of current one |
| 5897 | * rep->buf->p + rep->buf->i = end of input data |
Willy Tarreau | 2692736 | 2012-05-18 23:22:52 +0200 | [diff] [blame] | 5898 | * msg->eol = end of current header or line (LF or CRLF) |
| 5899 | * msg->next = first non-visited byte |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5900 | */ |
| 5901 | |
Willy Tarreau | 628c40c | 2014-04-24 19:11:26 +0200 | [diff] [blame] | 5902 | next_one: |
Willy Tarreau | 83e3af0 | 2009-12-28 17:39:57 +0100 | [diff] [blame] | 5903 | /* There's a protected area at the end of the buffer for rewriting |
| 5904 | * purposes. We don't want to start to parse the request if the |
| 5905 | * protected area is affected, because we may have to move processed |
| 5906 | * data later, which is much more complicated. |
| 5907 | */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 5908 | if (buffer_not_empty(rep->buf) && msg->msg_state < HTTP_MSG_ERROR) { |
Willy Tarreau | ba0902e | 2015-01-13 14:39:16 +0100 | [diff] [blame] | 5909 | if (unlikely(!channel_is_rewritable(rep))) { |
Willy Tarreau | 379357a | 2013-06-08 12:55:46 +0200 | [diff] [blame] | 5910 | /* some data has still not left the buffer, wake us once that's done */ |
| 5911 | if (rep->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_WRITE_ERROR|CF_WRITE_TIMEOUT)) |
| 5912 | goto abort_response; |
| 5913 | channel_dont_close(rep); |
| 5914 | rep->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */ |
Willy Tarreau | d7ad9f5 | 2013-12-31 17:26:25 +0100 | [diff] [blame] | 5915 | rep->flags |= CF_WAKE_WRITE; |
Willy Tarreau | 379357a | 2013-06-08 12:55:46 +0200 | [diff] [blame] | 5916 | return 0; |
Willy Tarreau | 83e3af0 | 2009-12-28 17:39:57 +0100 | [diff] [blame] | 5917 | } |
| 5918 | |
Willy Tarreau | 379357a | 2013-06-08 12:55:46 +0200 | [diff] [blame] | 5919 | if (unlikely(bi_end(rep->buf) < b_ptr(rep->buf, msg->next) || |
| 5920 | bi_end(rep->buf) > rep->buf->data + rep->buf->size - global.tune.maxrewrite)) |
| 5921 | buffer_slow_realign(rep->buf); |
| 5922 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 5923 | if (likely(msg->next < rep->buf->i)) |
Willy Tarreau | a560c21 | 2012-03-09 13:50:57 +0100 | [diff] [blame] | 5924 | http_msg_analyzer(msg, &txn->hdr_idx); |
Willy Tarreau | 83e3af0 | 2009-12-28 17:39:57 +0100 | [diff] [blame] | 5925 | } |
| 5926 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5927 | /* 1: we might have to print this header in debug mode */ |
| 5928 | if (unlikely((global.mode & MODE_DEBUG) && |
| 5929 | (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) && |
Willy Tarreau | 7d59e90 | 2014-10-21 19:36:09 +0200 | [diff] [blame] | 5930 | msg->msg_state >= HTTP_MSG_BODY)) { |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5931 | char *eol, *sol; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5932 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 5933 | sol = rep->buf->p; |
| 5934 | eol = sol + (msg->sl.st.l ? msg->sl.st.l : rep->buf->i); |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5935 | debug_hdr("srvrep", s, sol, eol); |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5936 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5937 | sol += hdr_idx_first_pos(&txn->hdr_idx); |
| 5938 | cur_idx = hdr_idx_first_idx(&txn->hdr_idx); |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5939 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5940 | while (cur_idx) { |
| 5941 | eol = sol + txn->hdr_idx.v[cur_idx].len; |
| 5942 | debug_hdr("srvhdr", s, sol, eol); |
| 5943 | sol = eol + txn->hdr_idx.v[cur_idx].cr + 1; |
| 5944 | cur_idx = txn->hdr_idx.v[cur_idx].next; |
| 5945 | } |
| 5946 | } |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5947 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5948 | /* |
| 5949 | * Now we quickly check if we have found a full valid response. |
| 5950 | * If not so, we check the FD and buffer states before leaving. |
| 5951 | * A full response is indicated by the fact that we have seen |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 5952 | * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5953 | * responses are checked first. |
| 5954 | * |
| 5955 | * Depending on whether the client is still there or not, we |
| 5956 | * may send an error response back or not. Note that normally |
| 5957 | * we should only check for HTTP status there, and check I/O |
| 5958 | * errors somewhere else. |
| 5959 | */ |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5960 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 5961 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) { |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5962 | /* Invalid response */ |
| 5963 | if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) { |
| 5964 | /* we detected a parsing error. We want to archive this response |
| 5965 | * in the dedicated proxy area for later troubleshooting. |
| 5966 | */ |
| 5967 | hdr_response_bad: |
| 5968 | if (msg->msg_state == HTTP_MSG_ERROR || msg->err_pos >= 0) |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 5969 | http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, sess->fe); |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5970 | |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 5971 | s->be->be_counters.failed_resp++; |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 5972 | if (objt_server(s->target)) { |
| 5973 | objt_server(s->target)->counters.failed_resp++; |
| 5974 | health_adjust(objt_server(s->target), HANA_STATUS_HTTP_HDRRSP); |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 5975 | } |
Willy Tarreau | 6464841 | 2010-03-05 10:41:54 +0100 | [diff] [blame] | 5976 | abort_response: |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 5977 | channel_auto_close(rep); |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5978 | rep->analysers = 0; |
| 5979 | txn->status = 502; |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 5980 | s->si[1].flags |= SI_FL_NOLINGER; |
Willy Tarreau | 319f745 | 2015-01-14 20:32:59 +0100 | [diff] [blame] | 5981 | channel_truncate(rep); |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 5982 | stream_int_retnclose(&s->si[0], http_error_message(s, HTTP_ERR_502)); |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5983 | |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 5984 | if (!(s->flags & SF_ERR_MASK)) |
| 5985 | s->flags |= SF_ERR_PRXCOND; |
| 5986 | if (!(s->flags & SF_FINST_MASK)) |
| 5987 | s->flags |= SF_FINST_H; |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5988 | |
| 5989 | return 0; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 5990 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 5991 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5992 | /* too large response does not fit in buffer. */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 5993 | else if (buffer_full(rep->buf, global.tune.maxrewrite)) { |
Willy Tarreau | fec4d89 | 2011-09-02 20:04:57 +0200 | [diff] [blame] | 5994 | if (msg->err_pos < 0) |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 5995 | msg->err_pos = rep->buf->i; |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5996 | goto hdr_response_bad; |
| 5997 | } |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5998 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5999 | /* read error */ |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 6000 | else if (rep->flags & CF_READ_ERROR) { |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 6001 | if (msg->err_pos >= 0) |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 6002 | http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, sess->fe); |
Willy Tarreau | 6b726ad | 2013-12-15 19:31:37 +0100 | [diff] [blame] | 6003 | else if (txn->flags & TX_NOT_FIRST) |
| 6004 | goto abort_keep_alive; |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 6005 | |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 6006 | s->be->be_counters.failed_resp++; |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 6007 | if (objt_server(s->target)) { |
| 6008 | objt_server(s->target)->counters.failed_resp++; |
| 6009 | health_adjust(objt_server(s->target), HANA_STATUS_HTTP_READ_ERROR); |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 6010 | } |
Willy Tarreau | 461f662 | 2008-08-15 23:43:19 +0200 | [diff] [blame] | 6011 | |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 6012 | channel_auto_close(rep); |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 6013 | rep->analysers = 0; |
| 6014 | txn->status = 502; |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 6015 | s->si[1].flags |= SI_FL_NOLINGER; |
Willy Tarreau | 319f745 | 2015-01-14 20:32:59 +0100 | [diff] [blame] | 6016 | channel_truncate(rep); |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 6017 | stream_int_retnclose(&s->si[0], http_error_message(s, HTTP_ERR_502)); |
Willy Tarreau | 816b979 | 2009-09-15 21:25:21 +0200 | [diff] [blame] | 6018 | |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 6019 | if (!(s->flags & SF_ERR_MASK)) |
| 6020 | s->flags |= SF_ERR_SRVCL; |
| 6021 | if (!(s->flags & SF_FINST_MASK)) |
| 6022 | s->flags |= SF_FINST_H; |
Willy Tarreau | cebf57e | 2008-08-15 18:16:37 +0200 | [diff] [blame] | 6023 | return 0; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 6024 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 6025 | |
Willy Tarreau | 6f0a7ba | 2014-06-23 15:22:31 +0200 | [diff] [blame] | 6026 | /* read timeout : return a 504 to the client. */ |
| 6027 | else if (rep->flags & CF_READ_TIMEOUT) { |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 6028 | if (msg->err_pos >= 0) |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 6029 | http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, sess->fe); |
Willy Tarreau | 6b726ad | 2013-12-15 19:31:37 +0100 | [diff] [blame] | 6030 | else if (txn->flags & TX_NOT_FIRST) |
| 6031 | goto abort_keep_alive; |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 6032 | |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 6033 | s->be->be_counters.failed_resp++; |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 6034 | if (objt_server(s->target)) { |
| 6035 | objt_server(s->target)->counters.failed_resp++; |
| 6036 | health_adjust(objt_server(s->target), HANA_STATUS_HTTP_READ_TIMEOUT); |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 6037 | } |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 6038 | |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 6039 | channel_auto_close(rep); |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 6040 | rep->analysers = 0; |
| 6041 | txn->status = 504; |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 6042 | s->si[1].flags |= SI_FL_NOLINGER; |
Willy Tarreau | 319f745 | 2015-01-14 20:32:59 +0100 | [diff] [blame] | 6043 | channel_truncate(rep); |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 6044 | stream_int_retnclose(&s->si[0], http_error_message(s, HTTP_ERR_504)); |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 6045 | |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 6046 | if (!(s->flags & SF_ERR_MASK)) |
| 6047 | s->flags |= SF_ERR_SRVTO; |
| 6048 | if (!(s->flags & SF_FINST_MASK)) |
| 6049 | s->flags |= SF_FINST_H; |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 6050 | return 0; |
| 6051 | } |
Willy Tarreau | a7c5276 | 2008-08-16 18:40:18 +0200 | [diff] [blame] | 6052 | |
Willy Tarreau | f003d37 | 2012-11-26 13:35:37 +0100 | [diff] [blame] | 6053 | /* client abort with an abortonclose */ |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 6054 | else if ((rep->flags & CF_SHUTR) && ((s->req.flags & (CF_SHUTR|CF_SHUTW)) == (CF_SHUTR|CF_SHUTW))) { |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 6055 | sess->fe->fe_counters.cli_aborts++; |
Willy Tarreau | f003d37 | 2012-11-26 13:35:37 +0100 | [diff] [blame] | 6056 | s->be->be_counters.cli_aborts++; |
| 6057 | if (objt_server(s->target)) |
| 6058 | objt_server(s->target)->counters.cli_aborts++; |
| 6059 | |
| 6060 | rep->analysers = 0; |
| 6061 | channel_auto_close(rep); |
| 6062 | |
| 6063 | txn->status = 400; |
Willy Tarreau | 319f745 | 2015-01-14 20:32:59 +0100 | [diff] [blame] | 6064 | channel_truncate(rep); |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 6065 | stream_int_retnclose(&s->si[0], http_error_message(s, HTTP_ERR_400)); |
Willy Tarreau | f003d37 | 2012-11-26 13:35:37 +0100 | [diff] [blame] | 6066 | |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 6067 | if (!(s->flags & SF_ERR_MASK)) |
| 6068 | s->flags |= SF_ERR_CLICL; |
| 6069 | if (!(s->flags & SF_FINST_MASK)) |
| 6070 | s->flags |= SF_FINST_H; |
Willy Tarreau | f003d37 | 2012-11-26 13:35:37 +0100 | [diff] [blame] | 6071 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 6072 | /* process_stream() will take care of the error */ |
Willy Tarreau | f003d37 | 2012-11-26 13:35:37 +0100 | [diff] [blame] | 6073 | return 0; |
| 6074 | } |
| 6075 | |
Willy Tarreau | 3b8c08a | 2011-09-02 20:16:24 +0200 | [diff] [blame] | 6076 | /* close from server, capture the response if the server has started to respond */ |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 6077 | else if (rep->flags & CF_SHUTR) { |
Willy Tarreau | 3b8c08a | 2011-09-02 20:16:24 +0200 | [diff] [blame] | 6078 | if (msg->msg_state >= HTTP_MSG_RPVER || msg->err_pos >= 0) |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 6079 | http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, sess->fe); |
Willy Tarreau | 6b726ad | 2013-12-15 19:31:37 +0100 | [diff] [blame] | 6080 | else if (txn->flags & TX_NOT_FIRST) |
| 6081 | goto abort_keep_alive; |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 6082 | |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 6083 | s->be->be_counters.failed_resp++; |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 6084 | if (objt_server(s->target)) { |
| 6085 | objt_server(s->target)->counters.failed_resp++; |
| 6086 | health_adjust(objt_server(s->target), HANA_STATUS_HTTP_BROKEN_PIPE); |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 6087 | } |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 6088 | |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 6089 | channel_auto_close(rep); |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 6090 | rep->analysers = 0; |
| 6091 | txn->status = 502; |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 6092 | s->si[1].flags |= SI_FL_NOLINGER; |
Willy Tarreau | 319f745 | 2015-01-14 20:32:59 +0100 | [diff] [blame] | 6093 | channel_truncate(rep); |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 6094 | stream_int_retnclose(&s->si[0], http_error_message(s, HTTP_ERR_502)); |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 6095 | |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 6096 | if (!(s->flags & SF_ERR_MASK)) |
| 6097 | s->flags |= SF_ERR_SRVCL; |
| 6098 | if (!(s->flags & SF_FINST_MASK)) |
| 6099 | s->flags |= SF_FINST_H; |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 6100 | return 0; |
| 6101 | } |
Krzysztof Piotr Oledzki | 5fb1882 | 2009-10-13 21:14:09 +0200 | [diff] [blame] | 6102 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 6103 | /* write error to client (we don't send any message then) */ |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 6104 | else if (rep->flags & CF_WRITE_ERROR) { |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 6105 | if (msg->err_pos >= 0) |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 6106 | http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, sess->fe); |
Willy Tarreau | 6b726ad | 2013-12-15 19:31:37 +0100 | [diff] [blame] | 6107 | else if (txn->flags & TX_NOT_FIRST) |
| 6108 | goto abort_keep_alive; |
Krzysztof Piotr Oledzki | 5fb1882 | 2009-10-13 21:14:09 +0200 | [diff] [blame] | 6109 | |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 6110 | s->be->be_counters.failed_resp++; |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 6111 | rep->analysers = 0; |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 6112 | channel_auto_close(rep); |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 6113 | |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 6114 | if (!(s->flags & SF_ERR_MASK)) |
| 6115 | s->flags |= SF_ERR_CLICL; |
| 6116 | if (!(s->flags & SF_FINST_MASK)) |
| 6117 | s->flags |= SF_FINST_H; |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 6118 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 6119 | /* process_stream() will take care of the error */ |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 6120 | return 0; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 6121 | } |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 6122 | |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 6123 | channel_dont_close(rep); |
Willy Tarreau | 3f3997e | 2013-12-15 15:21:32 +0100 | [diff] [blame] | 6124 | rep->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */ |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 6125 | return 0; |
| 6126 | } |
| 6127 | |
| 6128 | /* More interesting part now : we know that we have a complete |
| 6129 | * response which at least looks like HTTP. We have an indicator |
| 6130 | * of each header's length, so we can parse them quickly. |
| 6131 | */ |
| 6132 | |
| 6133 | if (unlikely(msg->err_pos >= 0)) |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 6134 | http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, sess->fe); |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 6135 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 6136 | /* |
| 6137 | * 1: get the status code |
| 6138 | */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 6139 | n = rep->buf->p[msg->sl.st.c] - '0'; |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 6140 | if (n < 1 || n > 5) |
| 6141 | n = 0; |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 6142 | /* when the client triggers a 4xx from the server, it's most often due |
| 6143 | * to a missing object or permission. These events should be tracked |
| 6144 | * because if they happen often, it may indicate a brute force or a |
| 6145 | * vulnerability scan. |
| 6146 | */ |
| 6147 | if (n == 4) |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 6148 | stream_inc_http_err_ctr(s); |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 6149 | |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 6150 | if (objt_server(s->target)) |
| 6151 | objt_server(s->target)->counters.p.http.rsp[n]++; |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 6152 | |
Willy Tarreau | 91852eb | 2015-05-01 13:26:00 +0200 | [diff] [blame] | 6153 | /* RFC7230#2.6 has enforced the format of the HTTP version string to be |
| 6154 | * exactly one digit "." one digit. This check may be disabled using |
| 6155 | * option accept-invalid-http-response. |
| 6156 | */ |
| 6157 | if (!(s->be->options2 & PR_O2_RSPBUG_OK)) { |
| 6158 | if (msg->sl.st.v_l != 8) { |
| 6159 | msg->err_pos = 0; |
| 6160 | goto hdr_response_bad; |
| 6161 | } |
| 6162 | |
| 6163 | if (rep->buf->p[4] != '/' || |
| 6164 | !isdigit((unsigned char)rep->buf->p[5]) || |
| 6165 | rep->buf->p[6] != '.' || |
| 6166 | !isdigit((unsigned char)rep->buf->p[7])) { |
| 6167 | msg->err_pos = 4; |
| 6168 | goto hdr_response_bad; |
| 6169 | } |
| 6170 | } |
| 6171 | |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 6172 | /* check if the response is HTTP/1.1 or above */ |
| 6173 | if ((msg->sl.st.v_l == 8) && |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 6174 | ((rep->buf->p[5] > '1') || |
| 6175 | ((rep->buf->p[5] == '1') && (rep->buf->p[7] >= '1')))) |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 6176 | msg->flags |= HTTP_MSGF_VER_11; |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 6177 | |
| 6178 | /* "connection" has not been parsed yet */ |
Willy Tarreau | 50fc777 | 2012-11-11 22:19:57 +0100 | [diff] [blame] | 6179 | 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 Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 6180 | |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 6181 | /* transfer length unknown*/ |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 6182 | msg->flags &= ~HTTP_MSGF_XFER_LEN; |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 6183 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 6184 | txn->status = strl2ui(rep->buf->p + msg->sl.st.c, msg->sl.st.c_l); |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 6185 | |
Willy Tarreau | 3965040 | 2010-03-15 19:44:39 +0100 | [diff] [blame] | 6186 | /* Adjust server's health based on status code. Note: status codes 501 |
| 6187 | * and 505 are triggered on demand by client request, so we must not |
| 6188 | * count them as server failures. |
| 6189 | */ |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 6190 | if (objt_server(s->target)) { |
Willy Tarreau | d45b3d5 | 2010-05-20 11:49:03 +0200 | [diff] [blame] | 6191 | if (txn->status >= 100 && (txn->status < 500 || txn->status == 501 || txn->status == 505)) |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 6192 | health_adjust(objt_server(s->target), HANA_STATUS_HTTP_OK); |
Willy Tarreau | d45b3d5 | 2010-05-20 11:49:03 +0200 | [diff] [blame] | 6193 | else |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 6194 | health_adjust(objt_server(s->target), HANA_STATUS_HTTP_STS); |
Willy Tarreau | d45b3d5 | 2010-05-20 11:49:03 +0200 | [diff] [blame] | 6195 | } |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 6196 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 6197 | /* |
| 6198 | * 2: check for cacheability. |
| 6199 | */ |
| 6200 | |
| 6201 | switch (txn->status) { |
Willy Tarreau | 628c40c | 2014-04-24 19:11:26 +0200 | [diff] [blame] | 6202 | case 100: |
| 6203 | /* |
| 6204 | * We may be facing a 100-continue response, in which case this |
| 6205 | * is not the right response, and we're waiting for the next one. |
| 6206 | * Let's allow this response to go to the client and wait for the |
| 6207 | * next one. |
| 6208 | */ |
| 6209 | hdr_idx_init(&txn->hdr_idx); |
| 6210 | msg->next -= channel_forward(rep, msg->next); |
| 6211 | msg->msg_state = HTTP_MSG_RPBEFORE; |
| 6212 | txn->status = 0; |
| 6213 | s->logs.t_data = -1; /* was not a response yet */ |
| 6214 | goto next_one; |
| 6215 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 6216 | case 200: |
| 6217 | case 203: |
| 6218 | case 206: |
| 6219 | case 300: |
| 6220 | case 301: |
| 6221 | case 410: |
| 6222 | /* RFC2616 @13.4: |
| 6223 | * "A response received with a status code of |
| 6224 | * 200, 203, 206, 300, 301 or 410 MAY be stored |
| 6225 | * by a cache (...) unless a cache-control |
| 6226 | * directive prohibits caching." |
| 6227 | * |
| 6228 | * RFC2616 @9.5: POST method : |
| 6229 | * "Responses to this method are not cacheable, |
| 6230 | * unless the response includes appropriate |
| 6231 | * Cache-Control or Expires header fields." |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 6232 | */ |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 6233 | if (likely(txn->meth != HTTP_METH_POST) && |
Willy Tarreau | 6740213 | 2012-05-31 20:40:20 +0200 | [diff] [blame] | 6234 | ((s->be->options & PR_O_CHK_CACHE) || (s->be->ck_opts & PR_CK_NOC))) |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 6235 | txn->flags |= TX_CACHEABLE | TX_CACHE_COOK; |
| 6236 | break; |
| 6237 | default: |
| 6238 | break; |
| 6239 | } |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 6240 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 6241 | /* |
| 6242 | * 3: we may need to capture headers |
| 6243 | */ |
| 6244 | s->logs.logwait &= ~LW_RESP; |
Willy Tarreau | cb7dd01 | 2015-04-03 22:16:32 +0200 | [diff] [blame] | 6245 | if (unlikely((s->logs.logwait & LW_RSPHDR) && s->res_cap)) |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 6246 | capture_headers(rep->buf->p, &txn->hdr_idx, |
Willy Tarreau | cb7dd01 | 2015-04-03 22:16:32 +0200 | [diff] [blame] | 6247 | s->res_cap, sess->fe->rsp_cap); |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 6248 | |
Willy Tarreau | 557f199 | 2015-05-01 10:05:17 +0200 | [diff] [blame] | 6249 | /* 4: determine the transfer-length according to RFC2616 #4.4, updated |
| 6250 | * by RFC7230#3.3.3 : |
| 6251 | * |
| 6252 | * The length of a message body is determined by one of the following |
| 6253 | * (in order of precedence): |
| 6254 | * |
| 6255 | * 1. Any response to a HEAD request and any response with a 1xx |
| 6256 | * (Informational), 204 (No Content), or 304 (Not Modified) status |
| 6257 | * code is always terminated by the first empty line after the |
| 6258 | * header fields, regardless of the header fields present in the |
| 6259 | * message, and thus cannot contain a message body. |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 6260 | * |
Willy Tarreau | 557f199 | 2015-05-01 10:05:17 +0200 | [diff] [blame] | 6261 | * 2. Any 2xx (Successful) response to a CONNECT request implies that |
| 6262 | * the connection will become a tunnel immediately after the empty |
| 6263 | * line that concludes the header fields. A client MUST ignore any |
| 6264 | * Content-Length or Transfer-Encoding header fields received in |
| 6265 | * such a message. |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 6266 | * |
Willy Tarreau | 557f199 | 2015-05-01 10:05:17 +0200 | [diff] [blame] | 6267 | * 3. If a Transfer-Encoding header field is present and the chunked |
| 6268 | * transfer coding (Section 4.1) is the final encoding, the message |
| 6269 | * body length is determined by reading and decoding the chunked |
| 6270 | * data until the transfer coding indicates the data is complete. |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 6271 | * |
Willy Tarreau | 557f199 | 2015-05-01 10:05:17 +0200 | [diff] [blame] | 6272 | * If a Transfer-Encoding header field is present in a response and |
| 6273 | * the chunked transfer coding is not the final encoding, the |
| 6274 | * message body length is determined by reading the connection until |
| 6275 | * it is closed by the server. If a Transfer-Encoding header field |
| 6276 | * is present in a request and the chunked transfer coding is not |
| 6277 | * the final encoding, the message body length cannot be determined |
| 6278 | * reliably; the server MUST respond with the 400 (Bad Request) |
| 6279 | * status code and then close the connection. |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 6280 | * |
Willy Tarreau | 557f199 | 2015-05-01 10:05:17 +0200 | [diff] [blame] | 6281 | * If a message is received with both a Transfer-Encoding and a |
| 6282 | * Content-Length header field, the Transfer-Encoding overrides the |
| 6283 | * Content-Length. Such a message might indicate an attempt to |
| 6284 | * perform request smuggling (Section 9.5) or response splitting |
| 6285 | * (Section 9.4) and ought to be handled as an error. A sender MUST |
| 6286 | * remove the received Content-Length field prior to forwarding such |
| 6287 | * a message downstream. |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 6288 | * |
Willy Tarreau | 557f199 | 2015-05-01 10:05:17 +0200 | [diff] [blame] | 6289 | * 4. If a message is received without Transfer-Encoding and with |
| 6290 | * either multiple Content-Length header fields having differing |
| 6291 | * field-values or a single Content-Length header field having an |
| 6292 | * invalid value, then the message framing is invalid and the |
| 6293 | * recipient MUST treat it as an unrecoverable error. If this is a |
| 6294 | * request message, the server MUST respond with a 400 (Bad Request) |
| 6295 | * status code and then close the connection. If this is a response |
| 6296 | * message received by a proxy, the proxy MUST close the connection |
| 6297 | * to the server, discard the received response, and send a 502 (Bad |
| 6298 | * Gateway) response to the client. If this is a response message |
| 6299 | * received by a user agent, the user agent MUST close the |
| 6300 | * connection to the server and discard the received response. |
| 6301 | * |
| 6302 | * 5. If a valid Content-Length header field is present without |
| 6303 | * Transfer-Encoding, its decimal value defines the expected message |
| 6304 | * body length in octets. If the sender closes the connection or |
| 6305 | * the recipient times out before the indicated number of octets are |
| 6306 | * received, the recipient MUST consider the message to be |
| 6307 | * incomplete and close the connection. |
| 6308 | * |
| 6309 | * 6. If this is a request message and none of the above are true, then |
| 6310 | * the message body length is zero (no message body is present). |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 6311 | * |
Willy Tarreau | 557f199 | 2015-05-01 10:05:17 +0200 | [diff] [blame] | 6312 | * 7. Otherwise, this is a response message without a declared message |
| 6313 | * body length, so the message body length is determined by the |
| 6314 | * number of octets received prior to the server closing the |
| 6315 | * connection. |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 6316 | */ |
| 6317 | |
| 6318 | /* Skip parsing if no content length is possible. The response flags |
Willy Tarreau | 124d991 | 2011-03-01 20:30:48 +0100 | [diff] [blame] | 6319 | * remain 0 as well as the chunk_len, which may or may not mirror |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 6320 | * the real header value, and we note that we know the response's length. |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 6321 | * FIXME: should we parse anyway and return an error on chunked encoding ? |
| 6322 | */ |
| 6323 | if (txn->meth == HTTP_METH_HEAD || |
| 6324 | (txn->status >= 100 && txn->status < 200) || |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 6325 | txn->status == 204 || txn->status == 304) { |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 6326 | msg->flags |= HTTP_MSGF_XFER_LEN; |
Willy Tarreau | 9101535 | 2012-11-27 07:31:33 +0100 | [diff] [blame] | 6327 | s->comp_algo = NULL; |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 6328 | goto skip_content_length; |
| 6329 | } |
| 6330 | |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 6331 | use_close_only = 0; |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 6332 | ctx.idx = 0; |
Willy Tarreau | 4979d5c | 2015-05-01 10:06:30 +0200 | [diff] [blame] | 6333 | while (http_find_header2("Transfer-Encoding", 17, rep->buf->p, &txn->hdr_idx, &ctx)) { |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 6334 | if (ctx.vlen == 7 && strncasecmp(ctx.line + ctx.val, "chunked", 7) == 0) |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 6335 | msg->flags |= (HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN); |
| 6336 | else if (msg->flags & HTTP_MSGF_TE_CHNK) { |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 6337 | /* bad transfer-encoding (chunked followed by something else) */ |
| 6338 | use_close_only = 1; |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 6339 | msg->flags &= ~(HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN); |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 6340 | break; |
| 6341 | } |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 6342 | } |
| 6343 | |
Willy Tarreau | 1c91391 | 2015-04-30 10:57:51 +0200 | [diff] [blame] | 6344 | /* Chunked responses must have their content-length removed */ |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 6345 | ctx.idx = 0; |
Willy Tarreau | b4d0c03 | 2015-05-01 10:25:45 +0200 | [diff] [blame] | 6346 | if (use_close_only || (msg->flags & HTTP_MSGF_TE_CHNK)) { |
Willy Tarreau | 1c91391 | 2015-04-30 10:57:51 +0200 | [diff] [blame] | 6347 | while (http_find_header2("Content-Length", 14, rep->buf->p, &txn->hdr_idx, &ctx)) |
| 6348 | http_remove_header2(msg, &txn->hdr_idx, &ctx); |
| 6349 | } |
Willy Tarreau | b4d0c03 | 2015-05-01 10:25:45 +0200 | [diff] [blame] | 6350 | else while (http_find_header2("Content-Length", 14, rep->buf->p, &txn->hdr_idx, &ctx)) { |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 6351 | signed long long cl; |
| 6352 | |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 6353 | if (!ctx.vlen) { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 6354 | msg->err_pos = ctx.line + ctx.val - rep->buf->p; |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 6355 | goto hdr_response_bad; |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 6356 | } |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 6357 | |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 6358 | if (strl2llrc(ctx.line + ctx.val, ctx.vlen, &cl)) { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 6359 | msg->err_pos = ctx.line + ctx.val - rep->buf->p; |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 6360 | goto hdr_response_bad; /* parse failure */ |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 6361 | } |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 6362 | |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 6363 | if (cl < 0) { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 6364 | msg->err_pos = ctx.line + ctx.val - rep->buf->p; |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 6365 | goto hdr_response_bad; |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 6366 | } |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 6367 | |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 6368 | if ((msg->flags & HTTP_MSGF_CNT_LEN) && (msg->chunk_len != cl)) { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 6369 | msg->err_pos = ctx.line + ctx.val - rep->buf->p; |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 6370 | goto hdr_response_bad; /* already specified, was different */ |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 6371 | } |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 6372 | |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 6373 | msg->flags |= HTTP_MSGF_CNT_LEN | HTTP_MSGF_XFER_LEN; |
Willy Tarreau | 124d991 | 2011-03-01 20:30:48 +0100 | [diff] [blame] | 6374 | msg->body_len = msg->chunk_len = cl; |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 6375 | } |
| 6376 | |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 6377 | if (sess->fe->comp || s->be->comp) |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 6378 | select_compression_response_header(s, rep->buf); |
| 6379 | |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 6380 | skip_content_length: |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 6381 | /* Now we have to check if we need to modify the Connection header. |
| 6382 | * This is more difficult on the response than it is on the request, |
| 6383 | * because we can have two different HTTP versions and we don't know |
| 6384 | * how the client will interprete a response. For instance, let's say |
| 6385 | * that the client sends a keep-alive request in HTTP/1.0 and gets an |
| 6386 | * HTTP/1.1 response without any header. Maybe it will bound itself to |
| 6387 | * HTTP/1.0 because it only knows about it, and will consider the lack |
| 6388 | * of header as a close, or maybe it knows HTTP/1.1 and can consider |
| 6389 | * the lack of header as a keep-alive. Thus we will use two flags |
| 6390 | * indicating how a request MAY be understood by the client. In case |
| 6391 | * of multiple possibilities, we'll fix the header to be explicit. If |
| 6392 | * ambiguous cases such as both close and keepalive are seen, then we |
| 6393 | * will fall back to explicit close. Note that we won't take risks with |
| 6394 | * HTTP/1.0 clients which may not necessarily understand keep-alive. |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 6395 | * See doc/internals/connection-header.txt for the complete matrix. |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 6396 | */ |
| 6397 | |
Willy Tarreau | dc008c5 | 2010-02-01 16:20:08 +0100 | [diff] [blame] | 6398 | if (unlikely((txn->meth == HTTP_METH_CONNECT && txn->status == 200) || |
| 6399 | txn->status == 101)) { |
| 6400 | /* Either we've established an explicit tunnel, or we're |
| 6401 | * switching the protocol. In both cases, we're very unlikely |
Willy Tarreau | 5843d1a | 2010-02-01 15:13:32 +0100 | [diff] [blame] | 6402 | * to understand the next protocols. We have to switch to tunnel |
| 6403 | * mode, so that we transfer the request and responses then let |
| 6404 | * this protocol pass unmodified. When we later implement specific |
| 6405 | * parsers for such protocols, we'll want to check the Upgrade |
Willy Tarreau | dc008c5 | 2010-02-01 16:20:08 +0100 | [diff] [blame] | 6406 | * header which contains information about that protocol for |
| 6407 | * responses with status 101 (eg: see RFC2817 about TLS). |
Willy Tarreau | 5843d1a | 2010-02-01 15:13:32 +0100 | [diff] [blame] | 6408 | */ |
| 6409 | txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_TUN; |
| 6410 | } |
Willy Tarreau | dc008c5 | 2010-02-01 16:20:08 +0100 | [diff] [blame] | 6411 | else if ((txn->status >= 200) && !(txn->flags & TX_HDR_CONN_PRS) && |
| 6412 | ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN || |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 6413 | ((sess->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL || |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 6414 | (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL))) { |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 6415 | int to_del = 0; |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 6416 | |
Willy Tarreau | 70dffda | 2014-01-30 03:07:23 +0100 | [diff] [blame] | 6417 | /* this situation happens when combining pretend-keepalive with httpclose. */ |
| 6418 | if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL && |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 6419 | ((sess->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL || |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 6420 | (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL)) |
Willy Tarreau | 70dffda | 2014-01-30 03:07:23 +0100 | [diff] [blame] | 6421 | txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO; |
| 6422 | |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 6423 | /* on unknown transfer length, we must close */ |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 6424 | if (!(msg->flags & HTTP_MSGF_XFER_LEN) && |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 6425 | (txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) |
| 6426 | txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO; |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 6427 | |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 6428 | /* now adjust header transformations depending on current state */ |
| 6429 | if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN || |
| 6430 | (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) { |
| 6431 | to_del |= 2; /* remove "keep-alive" on any response */ |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 6432 | if (!(msg->flags & HTTP_MSGF_VER_11)) |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 6433 | to_del |= 1; /* remove "close" for HTTP/1.0 responses */ |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 6434 | } |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 6435 | else { /* SCL / KAL */ |
| 6436 | to_del |= 1; /* remove "close" on any response */ |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 6437 | if (txn->req.flags & msg->flags & HTTP_MSGF_VER_11) |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 6438 | to_del |= 2; /* remove "keep-alive" on pure 1.1 responses */ |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 6439 | } |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 6440 | |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 6441 | /* Parse and remove some headers from the connection header */ |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 6442 | http_parse_connection_header(txn, msg, to_del); |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 6443 | |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 6444 | /* Some keep-alive responses are converted to Server-close if |
| 6445 | * the server wants to close. |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 6446 | */ |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 6447 | if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL) { |
| 6448 | if ((txn->flags & TX_HDR_CONN_CLO) || |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 6449 | (!(txn->flags & TX_HDR_CONN_KAL) && !(msg->flags & HTTP_MSGF_VER_11))) |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 6450 | txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_SCL; |
Willy Tarreau | b608feb | 2010-01-02 22:47:18 +0100 | [diff] [blame] | 6451 | } |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 6452 | } |
| 6453 | |
Willy Tarreau | 7959a55 | 2013-09-23 16:44:27 +0200 | [diff] [blame] | 6454 | /* we want to have the response time before we start processing it */ |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 6455 | s->logs.t_data = tv_ms_elapsed(&s->logs.tv_accept, &now); |
Willy Tarreau | 7959a55 | 2013-09-23 16:44:27 +0200 | [diff] [blame] | 6456 | |
Willy Tarreau | f118d9f | 2014-04-24 18:26:08 +0200 | [diff] [blame] | 6457 | /* end of job, return OK */ |
| 6458 | rep->analysers &= ~an_bit; |
| 6459 | rep->analyse_exp = TICK_ETERNITY; |
| 6460 | channel_auto_close(rep); |
| 6461 | return 1; |
| 6462 | |
| 6463 | abort_keep_alive: |
| 6464 | /* A keep-alive request to the server failed on a network error. |
| 6465 | * The client is required to retry. We need to close without returning |
| 6466 | * any other information so that the client retries. |
| 6467 | */ |
| 6468 | txn->status = 0; |
| 6469 | rep->analysers = 0; |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 6470 | s->req.analysers = 0; |
Willy Tarreau | f118d9f | 2014-04-24 18:26:08 +0200 | [diff] [blame] | 6471 | channel_auto_close(rep); |
| 6472 | s->logs.logwait = 0; |
| 6473 | s->logs.level = 0; |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 6474 | s->res.flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */ |
Willy Tarreau | 319f745 | 2015-01-14 20:32:59 +0100 | [diff] [blame] | 6475 | channel_truncate(rep); |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 6476 | stream_int_retnclose(&s->si[0], NULL); |
Willy Tarreau | f118d9f | 2014-04-24 18:26:08 +0200 | [diff] [blame] | 6477 | return 0; |
| 6478 | } |
| 6479 | |
| 6480 | /* This function performs all the processing enabled for the current response. |
| 6481 | * It normally returns 1 unless it wants to break. It relies on buffers flags, |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 6482 | * and updates s->res.analysers. It might make sense to explode it into several |
Willy Tarreau | f118d9f | 2014-04-24 18:26:08 +0200 | [diff] [blame] | 6483 | * other functions. It works like process_request (see indications above). |
| 6484 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 6485 | int http_process_res_common(struct stream *s, struct channel *rep, int an_bit, struct proxy *px) |
Willy Tarreau | f118d9f | 2014-04-24 18:26:08 +0200 | [diff] [blame] | 6486 | { |
Willy Tarreau | fb0afa7 | 2015-04-03 14:46:27 +0200 | [diff] [blame] | 6487 | struct session *sess = s->sess; |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 6488 | struct http_txn *txn = s->txn; |
Willy Tarreau | f118d9f | 2014-04-24 18:26:08 +0200 | [diff] [blame] | 6489 | struct http_msg *msg = &txn->rsp; |
| 6490 | struct proxy *cur_proxy; |
| 6491 | struct cond_wordlist *wl; |
Thierry FOURNIER | 9e2ef99 | 2015-02-25 13:51:19 +0100 | [diff] [blame] | 6492 | enum rule_result ret = HTTP_RULE_RES_CONT; |
Willy Tarreau | f118d9f | 2014-04-24 18:26:08 +0200 | [diff] [blame] | 6493 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 6494 | DPRINTF(stderr,"[%u] %s: stream=%p b=%p, exp(r,w)=%u,%u bf=%08x bh=%d analysers=%02x\n", |
Willy Tarreau | f118d9f | 2014-04-24 18:26:08 +0200 | [diff] [blame] | 6495 | now_ms, __FUNCTION__, |
| 6496 | s, |
| 6497 | rep, |
| 6498 | rep->rex, rep->wex, |
| 6499 | rep->flags, |
| 6500 | rep->buf->i, |
| 6501 | rep->analysers); |
| 6502 | |
| 6503 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) /* we need more data */ |
| 6504 | return 0; |
| 6505 | |
Willy Tarreau | 70730dd | 2014-04-24 18:06:27 +0200 | [diff] [blame] | 6506 | /* The stats applet needs to adjust the Connection header but we don't |
| 6507 | * apply any filter there. |
| 6508 | */ |
Willy Tarreau | 612adb8 | 2015-03-10 15:25:54 +0100 | [diff] [blame] | 6509 | if (unlikely(objt_applet(s->target) == &http_stats_applet)) { |
| 6510 | rep->analysers &= ~an_bit; |
| 6511 | rep->analyse_exp = TICK_ETERNITY; |
Willy Tarreau | 70730dd | 2014-04-24 18:06:27 +0200 | [diff] [blame] | 6512 | goto skip_filters; |
Willy Tarreau | 612adb8 | 2015-03-10 15:25:54 +0100 | [diff] [blame] | 6513 | } |
Willy Tarreau | 70730dd | 2014-04-24 18:06:27 +0200 | [diff] [blame] | 6514 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6515 | /* |
| 6516 | * We will have to evaluate the filters. |
| 6517 | * As opposed to version 1.2, now they will be evaluated in the |
| 6518 | * filters order and not in the header order. This means that |
| 6519 | * each filter has to be validated among all headers. |
| 6520 | * |
| 6521 | * Filters are tried with ->be first, then with ->fe if it is |
| 6522 | * different from ->be. |
Thierry FOURNIER | bc4c1ac | 2015-02-25 13:36:14 +0100 | [diff] [blame] | 6523 | * |
| 6524 | * Maybe we are in resume condiion. In this case I choose the |
| 6525 | * "struct proxy" which contains the rule list matching the resume |
| 6526 | * pointer. If none of theses "struct proxy" match, I initialise |
| 6527 | * the process with the first one. |
| 6528 | * |
| 6529 | * In fact, I check only correspondance betwwen the current list |
| 6530 | * pointer and the ->fe rule list. If it doesn't match, I initialize |
| 6531 | * the loop with the ->be. |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6532 | */ |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 6533 | if (s->current_rule_list == &sess->fe->http_res_rules) |
| 6534 | cur_proxy = sess->fe; |
Thierry FOURNIER | bc4c1ac | 2015-02-25 13:36:14 +0100 | [diff] [blame] | 6535 | else |
| 6536 | cur_proxy = s->be; |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6537 | while (1) { |
| 6538 | struct proxy *rule_set = cur_proxy; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 6539 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6540 | /* evaluate http-response rules */ |
Willy Tarreau | 51d861a | 2015-05-22 17:30:48 +0200 | [diff] [blame] | 6541 | if (ret == HTTP_RULE_RES_CONT) { |
Willy Tarreau | 987e3fb | 2015-04-04 01:09:08 +0200 | [diff] [blame] | 6542 | ret = http_res_get_intercept_rule(cur_proxy, &cur_proxy->http_res_rules, s); |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 6543 | |
Willy Tarreau | 51d861a | 2015-05-22 17:30:48 +0200 | [diff] [blame] | 6544 | if (ret == HTTP_RULE_RES_BADREQ) |
| 6545 | goto return_srv_prx_502; |
| 6546 | |
| 6547 | if (ret == HTTP_RULE_RES_DONE) { |
| 6548 | rep->analysers &= ~an_bit; |
| 6549 | rep->analyse_exp = TICK_ETERNITY; |
| 6550 | return 1; |
| 6551 | } |
| 6552 | } |
| 6553 | |
Thierry FOURNIER | bc4c1ac | 2015-02-25 13:36:14 +0100 | [diff] [blame] | 6554 | /* we need to be called again. */ |
| 6555 | if (ret == HTTP_RULE_RES_YIELD) { |
| 6556 | channel_dont_close(rep); |
| 6557 | return 0; |
| 6558 | } |
| 6559 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6560 | /* try headers filters */ |
| 6561 | if (rule_set->rsp_exp != NULL) { |
| 6562 | if (apply_filters_to_response(s, rep, rule_set) < 0) { |
| 6563 | return_bad_resp: |
| 6564 | if (objt_server(s->target)) { |
| 6565 | objt_server(s->target)->counters.failed_resp++; |
| 6566 | health_adjust(objt_server(s->target), HANA_STATUS_HTTP_RSP); |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 6567 | } |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6568 | s->be->be_counters.failed_resp++; |
| 6569 | return_srv_prx_502: |
| 6570 | rep->analysers = 0; |
| 6571 | txn->status = 502; |
| 6572 | s->logs.t_data = -1; /* was not a valid response */ |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 6573 | s->si[1].flags |= SI_FL_NOLINGER; |
Willy Tarreau | 319f745 | 2015-01-14 20:32:59 +0100 | [diff] [blame] | 6574 | channel_truncate(rep); |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 6575 | stream_int_retnclose(&s->si[0], http_error_message(s, HTTP_ERR_502)); |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 6576 | if (!(s->flags & SF_ERR_MASK)) |
| 6577 | s->flags |= SF_ERR_PRXCOND; |
| 6578 | if (!(s->flags & SF_FINST_MASK)) |
| 6579 | s->flags |= SF_FINST_H; |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6580 | return 0; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 6581 | } |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6582 | } |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 6583 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6584 | /* has the response been denied ? */ |
| 6585 | if (txn->flags & TX_SVDENY) { |
| 6586 | if (objt_server(s->target)) |
| 6587 | objt_server(s->target)->counters.failed_secu++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 6588 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6589 | s->be->be_counters.denied_resp++; |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 6590 | sess->fe->fe_counters.denied_resp++; |
Willy Tarreau | fb0afa7 | 2015-04-03 14:46:27 +0200 | [diff] [blame] | 6591 | if (sess->listener->counters) |
| 6592 | sess->listener->counters->denied_resp++; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 6593 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6594 | goto return_srv_prx_502; |
| 6595 | } |
Willy Tarreau | 0bbc3cf | 2006-10-15 14:26:02 +0200 | [diff] [blame] | 6596 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6597 | /* add response headers from the rule sets in the same order */ |
| 6598 | list_for_each_entry(wl, &rule_set->rsp_add, list) { |
Willy Tarreau | ce730de | 2014-09-16 10:40:38 +0200 | [diff] [blame] | 6599 | if (txn->status < 200 && txn->status != 101) |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 6600 | break; |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6601 | if (wl->cond) { |
Willy Tarreau | 192252e | 2015-04-04 01:47:55 +0200 | [diff] [blame] | 6602 | int ret = acl_exec_cond(wl->cond, px, sess, s, SMP_OPT_DIR_RES|SMP_OPT_FINAL); |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6603 | ret = acl_pass(ret); |
| 6604 | if (((struct acl_cond *)wl->cond)->pol == ACL_COND_UNLESS) |
| 6605 | ret = !ret; |
| 6606 | if (!ret) |
| 6607 | continue; |
| 6608 | } |
| 6609 | if (unlikely(http_header_add_tail(&txn->rsp, &txn->hdr_idx, wl->s) < 0)) |
| 6610 | goto return_bad_resp; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 6611 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 6612 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6613 | /* check whether we're already working on the frontend */ |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 6614 | if (cur_proxy == sess->fe) |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6615 | break; |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 6616 | cur_proxy = sess->fe; |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6617 | } |
Willy Tarreau | 63c9e5f | 2009-12-22 16:01:27 +0100 | [diff] [blame] | 6618 | |
Thierry FOURNIER | bc4c1ac | 2015-02-25 13:36:14 +0100 | [diff] [blame] | 6619 | /* After this point, this anayzer can't return yield, so we can |
| 6620 | * remove the bit corresponding to this analyzer from the list. |
| 6621 | * |
| 6622 | * Note that the intermediate returns and goto found previously |
| 6623 | * reset the analyzers. |
| 6624 | */ |
| 6625 | rep->analysers &= ~an_bit; |
| 6626 | rep->analyse_exp = TICK_ETERNITY; |
| 6627 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6628 | /* OK that's all we can do for 1xx responses */ |
Willy Tarreau | ce730de | 2014-09-16 10:40:38 +0200 | [diff] [blame] | 6629 | if (unlikely(txn->status < 200 && txn->status != 101)) |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6630 | goto skip_header_mangling; |
Willy Tarreau | 63c9e5f | 2009-12-22 16:01:27 +0100 | [diff] [blame] | 6631 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6632 | /* |
| 6633 | * Now check for a server cookie. |
| 6634 | */ |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 6635 | if (s->be->cookie_name || s->be->appsession_name || sess->fe->capture_name || |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6636 | (s->be->options & PR_O_CHK_CACHE)) |
| 6637 | manage_server_side_cookies(s, rep); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 6638 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6639 | /* |
| 6640 | * Check for cache-control or pragma headers if required. |
| 6641 | */ |
Willy Tarreau | ce730de | 2014-09-16 10:40:38 +0200 | [diff] [blame] | 6642 | if (((s->be->options & PR_O_CHK_CACHE) || (s->be->ck_opts & PR_CK_NOC)) && txn->status != 101) |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6643 | check_response_for_cacheability(s, rep); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 6644 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6645 | /* |
| 6646 | * Add server cookie in the response if needed |
| 6647 | */ |
| 6648 | if (objt_server(s->target) && (s->be->ck_opts & PR_CK_INS) && |
| 6649 | !((txn->flags & TX_SCK_FOUND) && (s->be->ck_opts & PR_CK_PSV)) && |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 6650 | (!(s->flags & SF_DIRECT) || |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6651 | ((s->be->cookie_maxidle || txn->cookie_last_date) && |
| 6652 | (!txn->cookie_last_date || (txn->cookie_last_date - date.tv_sec) < 0)) || |
| 6653 | (s->be->cookie_maxlife && !txn->cookie_first_date) || // set the first_date |
| 6654 | (!s->be->cookie_maxlife && txn->cookie_first_date)) && // remove the first_date |
| 6655 | (!(s->be->ck_opts & PR_CK_POST) || (txn->meth == HTTP_METH_POST)) && |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 6656 | !(s->flags & SF_IGNORE_PRST)) { |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6657 | /* the server is known, it's not the one the client requested, or the |
| 6658 | * cookie's last seen date needs to be refreshed. We have to |
| 6659 | * insert a set-cookie here, except if we want to insert only on POST |
| 6660 | * requests and this one isn't. Note that servers which don't have cookies |
| 6661 | * (eg: some backup servers) will return a full cookie removal request. |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6662 | */ |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6663 | if (!objt_server(s->target)->cookie) { |
| 6664 | chunk_printf(&trash, |
| 6665 | "Set-Cookie: %s=; Expires=Thu, 01-Jan-1970 00:00:01 GMT; path=/", |
| 6666 | s->be->cookie_name); |
| 6667 | } |
| 6668 | else { |
| 6669 | chunk_printf(&trash, "Set-Cookie: %s=%s", s->be->cookie_name, objt_server(s->target)->cookie); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6670 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6671 | if (s->be->cookie_maxidle || s->be->cookie_maxlife) { |
| 6672 | /* emit last_date, which is mandatory */ |
| 6673 | trash.str[trash.len++] = COOKIE_DELIM_DATE; |
| 6674 | s30tob64((date.tv_sec+3) >> 2, trash.str + trash.len); |
| 6675 | trash.len += 5; |
Willy Tarreau | ef4f391 | 2010-10-07 21:00:29 +0200 | [diff] [blame] | 6676 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6677 | if (s->be->cookie_maxlife) { |
| 6678 | /* emit first_date, which is either the original one or |
| 6679 | * the current date. |
| 6680 | */ |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 6681 | trash.str[trash.len++] = COOKIE_DELIM_DATE; |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6682 | s30tob64(txn->cookie_first_date ? |
| 6683 | txn->cookie_first_date >> 2 : |
| 6684 | (date.tv_sec+3) >> 2, trash.str + trash.len); |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 6685 | trash.len += 5; |
Willy Tarreau | ef4f391 | 2010-10-07 21:00:29 +0200 | [diff] [blame] | 6686 | } |
Willy Tarreau | ef4f391 | 2010-10-07 21:00:29 +0200 | [diff] [blame] | 6687 | } |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6688 | chunk_appendf(&trash, "; path=/"); |
| 6689 | } |
Willy Tarreau | 4992dd2 | 2012-05-31 21:02:17 +0200 | [diff] [blame] | 6690 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6691 | if (s->be->cookie_domain) |
| 6692 | chunk_appendf(&trash, "; domain=%s", s->be->cookie_domain); |
Willy Tarreau | ef4f391 | 2010-10-07 21:00:29 +0200 | [diff] [blame] | 6693 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6694 | if (s->be->ck_opts & PR_CK_HTTPONLY) |
| 6695 | chunk_appendf(&trash, "; HttpOnly"); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 6696 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6697 | if (s->be->ck_opts & PR_CK_SECURE) |
| 6698 | chunk_appendf(&trash, "; Secure"); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 6699 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6700 | if (unlikely(http_header_add_tail2(&txn->rsp, &txn->hdr_idx, trash.str, trash.len) < 0)) |
| 6701 | goto return_bad_resp; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 6702 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6703 | txn->flags &= ~TX_SCK_MASK; |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 6704 | if (objt_server(s->target)->cookie && (s->flags & SF_DIRECT)) |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6705 | /* the server did not change, only the date was updated */ |
| 6706 | txn->flags |= TX_SCK_UPDATED; |
| 6707 | else |
| 6708 | txn->flags |= TX_SCK_INSERTED; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 6709 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6710 | /* Here, we will tell an eventual cache on the client side that we don't |
| 6711 | * want it to cache this reply because HTTP/1.0 caches also cache cookies ! |
| 6712 | * Some caches understand the correct form: 'no-cache="set-cookie"', but |
| 6713 | * others don't (eg: apache <= 1.3.26). So we use 'private' instead. |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 6714 | */ |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6715 | if ((s->be->ck_opts & PR_CK_NOC) && (txn->flags & TX_CACHEABLE)) { |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 6716 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6717 | txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 6718 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6719 | if (unlikely(http_header_add_tail2(&txn->rsp, &txn->hdr_idx, |
| 6720 | "Cache-control: private", 22) < 0)) |
| 6721 | goto return_bad_resp; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 6722 | } |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6723 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6724 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6725 | /* |
| 6726 | * Check if result will be cacheable with a cookie. |
| 6727 | * We'll block the response if security checks have caught |
| 6728 | * nasty things such as a cacheable cookie. |
| 6729 | */ |
| 6730 | if (((txn->flags & (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) == |
| 6731 | (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) && |
| 6732 | (s->be->options & PR_O_CHK_CACHE)) { |
| 6733 | /* we're in presence of a cacheable response containing |
| 6734 | * a set-cookie header. We'll block it as requested by |
| 6735 | * the 'checkcache' option, and send an alert. |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6736 | */ |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6737 | if (objt_server(s->target)) |
| 6738 | objt_server(s->target)->counters.failed_secu++; |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 6739 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6740 | s->be->be_counters.denied_resp++; |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 6741 | sess->fe->fe_counters.denied_resp++; |
Willy Tarreau | fb0afa7 | 2015-04-03 14:46:27 +0200 | [diff] [blame] | 6742 | if (sess->listener->counters) |
| 6743 | sess->listener->counters->denied_resp++; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6744 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6745 | Alert("Blocking cacheable cookie in response from instance %s, server %s.\n", |
| 6746 | s->be->id, objt_server(s->target) ? objt_server(s->target)->id : "<dispatch>"); |
| 6747 | send_log(s->be, LOG_ALERT, |
| 6748 | "Blocking cacheable cookie in response from instance %s, server %s.\n", |
| 6749 | s->be->id, objt_server(s->target) ? objt_server(s->target)->id : "<dispatch>"); |
| 6750 | goto return_srv_prx_502; |
| 6751 | } |
Willy Tarreau | 0394594 | 2009-12-22 16:50:27 +0100 | [diff] [blame] | 6752 | |
Willy Tarreau | 70730dd | 2014-04-24 18:06:27 +0200 | [diff] [blame] | 6753 | skip_filters: |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6754 | /* |
| 6755 | * Adjust "Connection: close" or "Connection: keep-alive" if needed. |
| 6756 | * If an "Upgrade" token is found, the header is left untouched in order |
| 6757 | * not to have to deal with some client bugs : some of them fail an upgrade |
Willy Tarreau | ce730de | 2014-09-16 10:40:38 +0200 | [diff] [blame] | 6758 | * if anything but "Upgrade" is present in the Connection header. We don't |
| 6759 | * want to touch any 101 response either since it's switching to another |
| 6760 | * protocol. |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6761 | */ |
Willy Tarreau | ce730de | 2014-09-16 10:40:38 +0200 | [diff] [blame] | 6762 | if ((txn->status != 101) && !(txn->flags & TX_HDR_CONN_UPG) && |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6763 | (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) || |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 6764 | ((sess->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL || |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6765 | (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL))) { |
| 6766 | unsigned int want_flags = 0; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 6767 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6768 | if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL || |
| 6769 | (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) { |
| 6770 | /* we want a keep-alive response here. Keep-alive header |
| 6771 | * required if either side is not 1.1. |
| 6772 | */ |
| 6773 | if (!(txn->req.flags & msg->flags & HTTP_MSGF_VER_11)) |
| 6774 | want_flags |= TX_CON_KAL_SET; |
| 6775 | } |
| 6776 | else { |
| 6777 | /* we want a close response here. Close header required if |
| 6778 | * the server is 1.1, regardless of the client. |
| 6779 | */ |
| 6780 | if (msg->flags & HTTP_MSGF_VER_11) |
| 6781 | want_flags |= TX_CON_CLO_SET; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 6782 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6783 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6784 | if (want_flags != (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET))) |
| 6785 | http_change_connection_header(txn, msg, want_flags); |
| 6786 | } |
| 6787 | |
| 6788 | skip_header_mangling: |
| 6789 | if ((msg->flags & HTTP_MSGF_XFER_LEN) || |
| 6790 | (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN) |
| 6791 | rep->analysers |= AN_RES_HTTP_XFER_BODY; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6792 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6793 | /* if the user wants to log as soon as possible, without counting |
| 6794 | * bytes from the server, then this is the right moment. We have |
| 6795 | * to temporarily assign bytes_out to log what we currently have. |
| 6796 | */ |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 6797 | if (!LIST_ISEMPTY(&sess->fe->logformat) && !(s->logs.logwait & LW_BYTES)) { |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6798 | s->logs.t_close = s->logs.t_data; /* to get a valid end date */ |
| 6799 | s->logs.bytes_out = txn->rsp.eoh; |
| 6800 | s->do_log(s); |
| 6801 | s->logs.bytes_out = 0; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 6802 | } |
Willy Tarreau | e3fa6e5 | 2010-01-04 22:57:43 +0100 | [diff] [blame] | 6803 | return 1; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 6804 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6805 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 6806 | /* This function is an analyser which forwards response body (including chunk |
| 6807 | * sizes if any). It is called as soon as we must forward, even if we forward |
| 6808 | * zero byte. The only situation where it must not be called is when we're in |
| 6809 | * tunnel mode and we want to forward till the close. It's used both to forward |
| 6810 | * remaining data and to resync after end of body. It expects the msg_state to |
| 6811 | * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 6812 | * read more data, or 1 once we can go on with next request or end the stream. |
Willy Tarreau | d351021 | 2014-04-21 11:24:13 +0200 | [diff] [blame] | 6813 | * |
| 6814 | * It is capable of compressing response data both in content-length mode and |
| 6815 | * in chunked mode. The state machines follows different flows depending on |
| 6816 | * whether content-length and chunked modes are used, since there are no |
| 6817 | * trailers in content-length : |
| 6818 | * |
| 6819 | * chk-mode cl-mode |
| 6820 | * ,----- BODY -----. |
| 6821 | * / \ |
| 6822 | * V size > 0 V chk-mode |
| 6823 | * .--> SIZE -------------> DATA -------------> CRLF |
| 6824 | * | | size == 0 | last byte | |
| 6825 | * | v final crlf v inspected | |
| 6826 | * | TRAILERS -----------> DONE | |
| 6827 | * | | |
| 6828 | * `----------------------------------------------' |
| 6829 | * |
| 6830 | * Compression only happens in the DATA state, and must be flushed in final |
| 6831 | * states (TRAILERS/DONE) or when leaving on missing data. Normal forwarding |
| 6832 | * is performed at once on final states for all bytes parsed, or when leaving |
| 6833 | * on missing data. |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 6834 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 6835 | int http_response_forward_body(struct stream *s, struct channel *res, int an_bit) |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 6836 | { |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 6837 | struct session *sess = s->sess; |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 6838 | struct http_txn *txn = s->txn; |
| 6839 | struct http_msg *msg = &s->txn->rsp; |
Willy Tarreau | f2f7d6b | 2014-11-24 11:55:08 +0100 | [diff] [blame] | 6840 | static struct buffer *tmpbuf = &buf_empty; |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 6841 | int compressing = 0; |
Willy Tarreau | d655ffe | 2013-04-02 01:48:58 +0200 | [diff] [blame] | 6842 | int ret; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 6843 | |
Willy Tarreau | f5c8bd6 | 2010-01-04 07:10:34 +0100 | [diff] [blame] | 6844 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) |
| 6845 | return 0; |
| 6846 | |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 6847 | if ((res->flags & (CF_READ_ERROR|CF_READ_TIMEOUT|CF_WRITE_ERROR|CF_WRITE_TIMEOUT)) || |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 6848 | ((res->flags & CF_SHUTW) && (res->to_forward || res->buf->o)) || |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 6849 | !s->req.analysers) { |
Willy Tarreau | 4fe4190 | 2010-06-07 22:27:41 +0200 | [diff] [blame] | 6850 | /* Output closed while we were sending data. We must abort and |
| 6851 | * wake the other side up. |
| 6852 | */ |
| 6853 | msg->msg_state = HTTP_MSG_ERROR; |
| 6854 | http_resync_states(s); |
Willy Tarreau | 082b01c | 2010-01-02 23:58:04 +0100 | [diff] [blame] | 6855 | return 1; |
| 6856 | } |
| 6857 | |
Willy Tarreau | 4fe4190 | 2010-06-07 22:27:41 +0200 | [diff] [blame] | 6858 | /* in most states, we should abort in case of early close */ |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 6859 | channel_auto_close(res); |
Willy Tarreau | b608feb | 2010-01-02 22:47:18 +0100 | [diff] [blame] | 6860 | |
Willy Tarreau | bb2e669 | 2014-07-10 19:06:10 +0200 | [diff] [blame] | 6861 | if (msg->sov > 0) { |
Willy Tarreau | b59c7bf | 2014-04-22 14:29:58 +0200 | [diff] [blame] | 6862 | /* we have msg->sov which points to the first byte of message |
| 6863 | * body, and res->buf.p still points to the beginning of the |
| 6864 | * message. We forward the headers now, as we don't need them |
| 6865 | * anymore, and we want to flush them. |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 6866 | */ |
Willy Tarreau | b59c7bf | 2014-04-22 14:29:58 +0200 | [diff] [blame] | 6867 | b_adv(res->buf, msg->sov); |
| 6868 | msg->next -= msg->sov; |
| 6869 | msg->sov = 0; |
Willy Tarreau | a458b67 | 2012-03-05 11:17:50 +0100 | [diff] [blame] | 6870 | |
Willy Tarreau | b59c7bf | 2014-04-22 14:29:58 +0200 | [diff] [blame] | 6871 | /* The previous analysers guarantee that the state is somewhere |
| 6872 | * between MSG_BODY and the first MSG_DATA. So msg->sol and |
| 6873 | * msg->next are always correct. |
| 6874 | */ |
| 6875 | if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) { |
| 6876 | if (msg->flags & HTTP_MSGF_TE_CHNK) |
| 6877 | msg->msg_state = HTTP_MSG_CHUNK_SIZE; |
| 6878 | else |
| 6879 | msg->msg_state = HTTP_MSG_DATA; |
| 6880 | } |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 6881 | } |
| 6882 | |
Willy Tarreau | efdf094 | 2014-04-24 20:08:57 +0200 | [diff] [blame] | 6883 | if (res->to_forward) { |
| 6884 | /* We can't process the buffer's contents yet */ |
| 6885 | res->flags |= CF_WAKE_WRITE; |
| 6886 | goto missing_data; |
| 6887 | } |
| 6888 | |
Willy Tarreau | 32b5ab2 | 2014-04-21 11:27:29 +0200 | [diff] [blame] | 6889 | if (unlikely(s->comp_algo != NULL) && msg->msg_state < HTTP_MSG_TRAILERS) { |
| 6890 | /* We need a compression buffer in the DATA state to put the |
| 6891 | * output of compressed data, and in CRLF state to let the |
| 6892 | * TRAILERS state finish the job of removing the trailing CRLF. |
| 6893 | */ |
Willy Tarreau | f2f7d6b | 2014-11-24 11:55:08 +0100 | [diff] [blame] | 6894 | if (unlikely(!tmpbuf->size)) { |
Willy Tarreau | 32b5ab2 | 2014-04-21 11:27:29 +0200 | [diff] [blame] | 6895 | /* this is the first time we need the compression buffer */ |
Willy Tarreau | e583ea5 | 2014-11-24 11:30:16 +0100 | [diff] [blame] | 6896 | if (b_alloc(&tmpbuf) == NULL) |
Willy Tarreau | 32b5ab2 | 2014-04-21 11:27:29 +0200 | [diff] [blame] | 6897 | goto aborted_xfer; /* no memory */ |
| 6898 | } |
| 6899 | |
| 6900 | ret = http_compression_buffer_init(s, res->buf, tmpbuf); |
Willy Tarreau | 4afd70a | 2014-01-25 02:26:39 +0100 | [diff] [blame] | 6901 | if (ret < 0) { |
| 6902 | res->flags |= CF_WAKE_WRITE; |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 6903 | goto missing_data; /* not enough spaces in buffers */ |
Willy Tarreau | 4afd70a | 2014-01-25 02:26:39 +0100 | [diff] [blame] | 6904 | } |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 6905 | compressing = 1; |
| 6906 | } |
| 6907 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 6908 | while (1) { |
Willy Tarreau | d655ffe | 2013-04-02 01:48:58 +0200 | [diff] [blame] | 6909 | switch (msg->msg_state - HTTP_MSG_DATA) { |
| 6910 | case HTTP_MSG_DATA - HTTP_MSG_DATA: /* must still forward */ |
Willy Tarreau | c623c17 | 2014-04-18 09:53:50 +0200 | [diff] [blame] | 6911 | /* we may have some pending data starting at res->buf->p */ |
| 6912 | if (unlikely(s->comp_algo)) { |
Willy Tarreau | 7f2f8d5 | 2014-04-18 00:20:14 +0200 | [diff] [blame] | 6913 | ret = http_compression_buffer_add_data(s, res->buf, tmpbuf); |
William Lallemand | bf3ae61 | 2012-11-19 12:35:37 +0100 | [diff] [blame] | 6914 | if (ret < 0) |
| 6915 | goto aborted_xfer; |
Willy Tarreau | c623c17 | 2014-04-18 09:53:50 +0200 | [diff] [blame] | 6916 | |
Willy Tarreau | d5a6783 | 2014-04-21 10:54:27 +0200 | [diff] [blame] | 6917 | if (msg->chunk_len) { |
| 6918 | /* input empty or output full */ |
| 6919 | if (res->buf->i > msg->next) |
| 6920 | res->flags |= CF_WAKE_WRITE; |
Willy Tarreau | c623c17 | 2014-04-18 09:53:50 +0200 | [diff] [blame] | 6921 | goto missing_data; |
| 6922 | } |
William Lallemand | bf3ae61 | 2012-11-19 12:35:37 +0100 | [diff] [blame] | 6923 | } |
Willy Tarreau | c623c17 | 2014-04-18 09:53:50 +0200 | [diff] [blame] | 6924 | else { |
Willy Tarreau | d5a6783 | 2014-04-21 10:54:27 +0200 | [diff] [blame] | 6925 | if (msg->chunk_len > res->buf->i - msg->next) { |
| 6926 | /* output full */ |
| 6927 | res->flags |= CF_WAKE_WRITE; |
| 6928 | goto missing_data; |
| 6929 | } |
Willy Tarreau | c623c17 | 2014-04-18 09:53:50 +0200 | [diff] [blame] | 6930 | msg->next += msg->chunk_len; |
| 6931 | msg->chunk_len = 0; |
| 6932 | } |
Willy Tarreau | caabe41 | 2010-01-03 23:08:28 +0100 | [diff] [blame] | 6933 | |
| 6934 | /* nothing left to forward */ |
William Lallemand | bf3ae61 | 2012-11-19 12:35:37 +0100 | [diff] [blame] | 6935 | if (msg->flags & HTTP_MSGF_TE_CHNK) { |
Willy Tarreau | 54d23df | 2012-10-25 19:04:45 +0200 | [diff] [blame] | 6936 | msg->msg_state = HTTP_MSG_CHUNK_CRLF; |
William Lallemand | bf3ae61 | 2012-11-19 12:35:37 +0100 | [diff] [blame] | 6937 | } else { |
Willy Tarreau | caabe41 | 2010-01-03 23:08:28 +0100 | [diff] [blame] | 6938 | msg->msg_state = HTTP_MSG_DONE; |
Willy Tarreau | d655ffe | 2013-04-02 01:48:58 +0200 | [diff] [blame] | 6939 | break; |
William Lallemand | bf3ae61 | 2012-11-19 12:35:37 +0100 | [diff] [blame] | 6940 | } |
Willy Tarreau | d655ffe | 2013-04-02 01:48:58 +0200 | [diff] [blame] | 6941 | /* fall through for HTTP_MSG_CHUNK_CRLF */ |
| 6942 | |
| 6943 | case HTTP_MSG_CHUNK_CRLF - HTTP_MSG_DATA: |
| 6944 | /* we want the CRLF after the data */ |
| 6945 | |
| 6946 | ret = http_skip_chunk_crlf(msg); |
| 6947 | if (ret == 0) |
| 6948 | goto missing_data; |
| 6949 | else if (ret < 0) { |
| 6950 | if (msg->err_pos >= 0) |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 6951 | http_capture_bad_message(&s->be->invalid_rep, s, msg, HTTP_MSG_CHUNK_CRLF, sess->fe); |
Willy Tarreau | d655ffe | 2013-04-02 01:48:58 +0200 | [diff] [blame] | 6952 | goto return_bad_res; |
| 6953 | } |
Willy Tarreau | d655ffe | 2013-04-02 01:48:58 +0200 | [diff] [blame] | 6954 | /* we're in MSG_CHUNK_SIZE now, fall through */ |
| 6955 | |
| 6956 | case HTTP_MSG_CHUNK_SIZE - HTTP_MSG_DATA: |
Willy Tarreau | 124d991 | 2011-03-01 20:30:48 +0100 | [diff] [blame] | 6957 | /* read the chunk size and assign it to ->chunk_len, then |
Willy Tarreau | c24715e | 2014-04-17 15:21:20 +0200 | [diff] [blame] | 6958 | * set ->next to point to the body and switch to DATA or |
Willy Tarreau | a458b67 | 2012-03-05 11:17:50 +0100 | [diff] [blame] | 6959 | * TRAILERS state. |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 6960 | */ |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 6961 | |
Willy Tarreau | d655ffe | 2013-04-02 01:48:58 +0200 | [diff] [blame] | 6962 | ret = http_parse_chunk_size(msg); |
Willy Tarreau | 54d23df | 2012-10-25 19:04:45 +0200 | [diff] [blame] | 6963 | if (ret == 0) |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 6964 | goto missing_data; |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 6965 | else if (ret < 0) { |
| 6966 | if (msg->err_pos >= 0) |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 6967 | http_capture_bad_message(&s->be->invalid_rep, s, msg, HTTP_MSG_CHUNK_SIZE, sess->fe); |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 6968 | goto return_bad_res; |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 6969 | } |
Willy Tarreau | 0161d62 | 2013-04-02 01:26:55 +0200 | [diff] [blame] | 6970 | /* otherwise we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state */ |
Willy Tarreau | d655ffe | 2013-04-02 01:48:58 +0200 | [diff] [blame] | 6971 | break; |
Willy Tarreau | 5523b32 | 2009-12-29 12:05:52 +0100 | [diff] [blame] | 6972 | |
Willy Tarreau | d655ffe | 2013-04-02 01:48:58 +0200 | [diff] [blame] | 6973 | case HTTP_MSG_TRAILERS - HTTP_MSG_DATA: |
Willy Tarreau | 168ebc5 | 2014-04-18 00:53:43 +0200 | [diff] [blame] | 6974 | if (unlikely(compressing)) { |
| 6975 | /* we need to flush output contents before syncing FSMs */ |
| 6976 | http_compression_buffer_end(s, &res->buf, &tmpbuf, 1); |
| 6977 | compressing = 0; |
| 6978 | } |
| 6979 | |
Willy Tarreau | d655ffe | 2013-04-02 01:48:58 +0200 | [diff] [blame] | 6980 | ret = http_forward_trailers(msg); |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 6981 | if (ret == 0) |
| 6982 | goto missing_data; |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 6983 | else if (ret < 0) { |
| 6984 | if (msg->err_pos >= 0) |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 6985 | http_capture_bad_message(&s->be->invalid_rep, s, msg, HTTP_MSG_TRAILERS, sess->fe); |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 6986 | goto return_bad_res; |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 6987 | } |
Willy Tarreau | 168ebc5 | 2014-04-18 00:53:43 +0200 | [diff] [blame] | 6988 | /* we're in HTTP_MSG_DONE now, fall through */ |
Willy Tarreau | d655ffe | 2013-04-02 01:48:58 +0200 | [diff] [blame] | 6989 | |
| 6990 | default: |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 6991 | /* other states, DONE...TUNNEL */ |
Willy Tarreau | 168ebc5 | 2014-04-18 00:53:43 +0200 | [diff] [blame] | 6992 | if (unlikely(compressing)) { |
| 6993 | /* we need to flush output contents before syncing FSMs */ |
| 6994 | http_compression_buffer_end(s, &res->buf, &tmpbuf, 1); |
| 6995 | compressing = 0; |
| 6996 | } |
Willy Tarreau | d655ffe | 2013-04-02 01:48:58 +0200 | [diff] [blame] | 6997 | |
Willy Tarreau | c623c17 | 2014-04-18 09:53:50 +0200 | [diff] [blame] | 6998 | /* we may have some pending data starting at res->buf->p |
| 6999 | * such as a last chunk of data or trailers. |
| 7000 | */ |
| 7001 | b_adv(res->buf, msg->next); |
| 7002 | msg->next = 0; |
| 7003 | |
Willy Tarreau | d655ffe | 2013-04-02 01:48:58 +0200 | [diff] [blame] | 7004 | ret = msg->msg_state; |
Willy Tarreau | 4fe4190 | 2010-06-07 22:27:41 +0200 | [diff] [blame] | 7005 | /* for keep-alive we don't want to forward closes on DONE */ |
Willy Tarreau | 92aa1fa | 2010-08-28 18:57:20 +0200 | [diff] [blame] | 7006 | if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL || |
| 7007 | (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 7008 | channel_dont_close(res); |
Willy Tarreau | 3ce10ff | 2014-04-22 08:24:38 +0200 | [diff] [blame] | 7009 | |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 7010 | if (http_resync_states(s)) { |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 7011 | /* some state changes occurred, maybe the analyser |
| 7012 | * was disabled too. |
Willy Tarreau | 5523b32 | 2009-12-29 12:05:52 +0100 | [diff] [blame] | 7013 | */ |
Willy Tarreau | 3fe693b | 2010-12-12 12:50:05 +0100 | [diff] [blame] | 7014 | if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) { |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 7015 | if (res->flags & CF_SHUTW) { |
Willy Tarreau | 3fe693b | 2010-12-12 12:50:05 +0100 | [diff] [blame] | 7016 | /* response errors are most likely due to |
| 7017 | * the client aborting the transfer. |
| 7018 | */ |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 7019 | goto aborted_xfer; |
Willy Tarreau | 3fe693b | 2010-12-12 12:50:05 +0100 | [diff] [blame] | 7020 | } |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 7021 | if (msg->err_pos >= 0) |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 7022 | http_capture_bad_message(&s->be->invalid_rep, s, msg, ret, sess->fe); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 7023 | goto return_bad_res; |
Willy Tarreau | 3fe693b | 2010-12-12 12:50:05 +0100 | [diff] [blame] | 7024 | } |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 7025 | return 1; |
Willy Tarreau | 5523b32 | 2009-12-29 12:05:52 +0100 | [diff] [blame] | 7026 | } |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 7027 | return 0; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 7028 | } |
| 7029 | } |
| 7030 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 7031 | missing_data: |
Willy Tarreau | c623c17 | 2014-04-18 09:53:50 +0200 | [diff] [blame] | 7032 | /* we may have some pending data starting at res->buf->p */ |
Willy Tarreau | 168ebc5 | 2014-04-18 00:53:43 +0200 | [diff] [blame] | 7033 | if (unlikely(compressing)) { |
| 7034 | http_compression_buffer_end(s, &res->buf, &tmpbuf, msg->msg_state >= HTTP_MSG_TRAILERS); |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 7035 | compressing = 0; |
| 7036 | } |
Willy Tarreau | f003d37 | 2012-11-26 13:35:37 +0100 | [diff] [blame] | 7037 | |
Willy Tarreau | c623c17 | 2014-04-18 09:53:50 +0200 | [diff] [blame] | 7038 | if ((s->comp_algo == NULL || msg->msg_state >= HTTP_MSG_TRAILERS)) { |
| 7039 | b_adv(res->buf, msg->next); |
| 7040 | msg->next = 0; |
| 7041 | msg->chunk_len -= channel_forward(res, msg->chunk_len); |
| 7042 | } |
| 7043 | |
Willy Tarreau | f003d37 | 2012-11-26 13:35:37 +0100 | [diff] [blame] | 7044 | if (res->flags & CF_SHUTW) |
| 7045 | goto aborted_xfer; |
| 7046 | |
| 7047 | /* stop waiting for data if the input is closed before the end. If the |
| 7048 | * client side was already closed, it means that the client has aborted, |
| 7049 | * so we don't want to count this as a server abort. Otherwise it's a |
| 7050 | * server abort. |
| 7051 | */ |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 7052 | if (res->flags & CF_SHUTR) { |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 7053 | if ((s->req.flags & (CF_SHUTR|CF_SHUTW)) == (CF_SHUTR|CF_SHUTW)) |
Willy Tarreau | f003d37 | 2012-11-26 13:35:37 +0100 | [diff] [blame] | 7054 | goto aborted_xfer; |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 7055 | if (!(s->flags & SF_ERR_MASK)) |
| 7056 | s->flags |= SF_ERR_SRVCL; |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 7057 | s->be->be_counters.srv_aborts++; |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 7058 | if (objt_server(s->target)) |
| 7059 | objt_server(s->target)->counters.srv_aborts++; |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 7060 | goto return_bad_res_stats_ok; |
Willy Tarreau | 40dba09 | 2010-03-04 18:14:51 +0100 | [diff] [blame] | 7061 | } |
Willy Tarreau | f5c8bd6 | 2010-01-04 07:10:34 +0100 | [diff] [blame] | 7062 | |
Willy Tarreau | 40dba09 | 2010-03-04 18:14:51 +0100 | [diff] [blame] | 7063 | /* we need to obey the req analyser, so if it leaves, we must too */ |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 7064 | if (!s->req.analysers) |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 7065 | goto return_bad_res; |
| 7066 | |
Willy Tarreau | 92aa1fa | 2010-08-28 18:57:20 +0200 | [diff] [blame] | 7067 | /* When TE: chunked is used, we need to get there again to parse remaining |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 7068 | * chunks even if the server has closed, so we don't want to set CF_DONTCLOSE. |
Willy Tarreau | 92aa1fa | 2010-08-28 18:57:20 +0200 | [diff] [blame] | 7069 | * Similarly, with keep-alive on the client side, we don't want to forward a |
| 7070 | * close. |
| 7071 | */ |
Willy Tarreau | 08b4d79 | 2012-10-27 01:36:34 +0200 | [diff] [blame] | 7072 | if ((msg->flags & HTTP_MSGF_TE_CHNK) || s->comp_algo || |
Willy Tarreau | 92aa1fa | 2010-08-28 18:57:20 +0200 | [diff] [blame] | 7073 | (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL || |
| 7074 | (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 7075 | channel_dont_close(res); |
Willy Tarreau | 92aa1fa | 2010-08-28 18:57:20 +0200 | [diff] [blame] | 7076 | |
Willy Tarreau | 5c62092 | 2011-05-11 19:56:11 +0200 | [diff] [blame] | 7077 | /* We know that more data are expected, but we couldn't send more that |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 7078 | * what we did. So we always set the CF_EXPECT_MORE flag so that the |
Willy Tarreau | 0729303 | 2011-05-30 18:29:28 +0200 | [diff] [blame] | 7079 | * system knows it must not set a PUSH on this first part. Interactive |
Willy Tarreau | 869fc1e | 2012-03-05 08:29:20 +0100 | [diff] [blame] | 7080 | * modes are already handled by the stream sock layer. We must not do |
| 7081 | * this in content-length mode because it could present the MSG_MORE |
| 7082 | * flag with the last block of forwarded data, which would cause an |
| 7083 | * additional delay to be observed by the receiver. |
Willy Tarreau | 5c62092 | 2011-05-11 19:56:11 +0200 | [diff] [blame] | 7084 | */ |
Willy Tarreau | 08b4d79 | 2012-10-27 01:36:34 +0200 | [diff] [blame] | 7085 | if ((msg->flags & HTTP_MSGF_TE_CHNK) || s->comp_algo) |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 7086 | res->flags |= CF_EXPECT_MORE; |
Willy Tarreau | 5c62092 | 2011-05-11 19:56:11 +0200 | [diff] [blame] | 7087 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 7088 | /* the stream handler will take care of timeouts and errors */ |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 7089 | return 0; |
| 7090 | |
Willy Tarreau | 40dba09 | 2010-03-04 18:14:51 +0100 | [diff] [blame] | 7091 | return_bad_res: /* let's centralize all bad responses */ |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 7092 | s->be->be_counters.failed_resp++; |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 7093 | if (objt_server(s->target)) |
| 7094 | objt_server(s->target)->counters.failed_resp++; |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 7095 | |
| 7096 | return_bad_res_stats_ok: |
Willy Tarreau | d01f426 | 2014-04-21 11:00:13 +0200 | [diff] [blame] | 7097 | if (unlikely(compressing)) { |
Willy Tarreau | 168ebc5 | 2014-04-18 00:53:43 +0200 | [diff] [blame] | 7098 | http_compression_buffer_end(s, &res->buf, &tmpbuf, msg->msg_state >= HTTP_MSG_TRAILERS); |
Willy Tarreau | d01f426 | 2014-04-21 11:00:13 +0200 | [diff] [blame] | 7099 | compressing = 0; |
| 7100 | } |
| 7101 | |
Willy Tarreau | c623c17 | 2014-04-18 09:53:50 +0200 | [diff] [blame] | 7102 | /* we may have some pending data starting at res->buf->p */ |
| 7103 | if (s->comp_algo == NULL) { |
| 7104 | b_adv(res->buf, msg->next); |
| 7105 | msg->next = 0; |
| 7106 | } |
| 7107 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 7108 | txn->rsp.msg_state = HTTP_MSG_ERROR; |
Willy Tarreau | 148d099 | 2010-01-10 10:21:21 +0100 | [diff] [blame] | 7109 | /* don't send any error message as we're in the body */ |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 7110 | stream_int_retnclose(&s->si[0], NULL); |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 7111 | res->analysers = 0; |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 7112 | s->req.analysers = 0; /* we're in data phase, we want to abort both directions */ |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 7113 | if (objt_server(s->target)) |
| 7114 | health_adjust(objt_server(s->target), HANA_STATUS_HTTP_HDRRSP); |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 7115 | |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 7116 | if (!(s->flags & SF_ERR_MASK)) |
| 7117 | s->flags |= SF_ERR_PRXCOND; |
| 7118 | if (!(s->flags & SF_FINST_MASK)) |
| 7119 | s->flags |= SF_FINST_D; |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 7120 | return 0; |
| 7121 | |
| 7122 | aborted_xfer: |
Willy Tarreau | 6fef8ae | 2014-04-22 21:22:06 +0200 | [diff] [blame] | 7123 | if (unlikely(compressing)) { |
| 7124 | http_compression_buffer_end(s, &res->buf, &tmpbuf, msg->msg_state >= HTTP_MSG_TRAILERS); |
| 7125 | compressing = 0; |
| 7126 | } |
| 7127 | |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 7128 | txn->rsp.msg_state = HTTP_MSG_ERROR; |
| 7129 | /* don't send any error message as we're in the body */ |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 7130 | stream_int_retnclose(&s->si[0], NULL); |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 7131 | res->analysers = 0; |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 7132 | s->req.analysers = 0; /* we're in data phase, we want to abort both directions */ |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 7133 | |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 7134 | sess->fe->fe_counters.cli_aborts++; |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 7135 | s->be->be_counters.cli_aborts++; |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 7136 | if (objt_server(s->target)) |
| 7137 | objt_server(s->target)->counters.cli_aborts++; |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 7138 | |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 7139 | if (!(s->flags & SF_ERR_MASK)) |
| 7140 | s->flags |= SF_ERR_CLICL; |
| 7141 | if (!(s->flags & SF_FINST_MASK)) |
| 7142 | s->flags |= SF_FINST_D; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 7143 | return 0; |
| 7144 | } |
| 7145 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7146 | /* Iterate the same filter through all request headers. |
| 7147 | * Returns 1 if this filter can be stopped upon return, otherwise 0. |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7148 | * Since it can manage the switch to another backend, it updates the per-proxy |
| 7149 | * DENY stats. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7150 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 7151 | int apply_filter_to_req_headers(struct stream *s, struct channel *req, struct hdr_exp *exp) |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7152 | { |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7153 | char *cur_ptr, *cur_end, *cur_next; |
| 7154 | int cur_idx, old_idx, last_hdr; |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 7155 | struct http_txn *txn = s->txn; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7156 | struct hdr_idx_elem *cur_hdr; |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 7157 | int delta; |
Willy Tarreau | 0f7562b | 2007-01-07 15:46:13 +0100 | [diff] [blame] | 7158 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7159 | last_hdr = 0; |
| 7160 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 7161 | cur_next = req->buf->p + hdr_idx_first_pos(&txn->hdr_idx); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7162 | old_idx = 0; |
| 7163 | |
| 7164 | while (!last_hdr) { |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 7165 | if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT))) |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7166 | return 1; |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 7167 | else if (unlikely(txn->flags & TX_CLALLOW) && |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7168 | (exp->action == ACT_ALLOW || |
| 7169 | exp->action == ACT_DENY || |
| 7170 | exp->action == ACT_TARPIT)) |
| 7171 | return 0; |
| 7172 | |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 7173 | cur_idx = txn->hdr_idx.v[old_idx].next; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7174 | if (!cur_idx) |
| 7175 | break; |
| 7176 | |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 7177 | cur_hdr = &txn->hdr_idx.v[cur_idx]; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7178 | cur_ptr = cur_next; |
| 7179 | cur_end = cur_ptr + cur_hdr->len; |
| 7180 | cur_next = cur_end + cur_hdr->cr + 1; |
| 7181 | |
| 7182 | /* Now we have one header between cur_ptr and cur_end, |
| 7183 | * and the next header starts at cur_next. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7184 | */ |
| 7185 | |
Willy Tarreau | 15a53a4 | 2015-01-21 13:39:42 +0100 | [diff] [blame] | 7186 | if (regex_exec_match2(exp->preg, cur_ptr, cur_end-cur_ptr, MAX_MATCH, pmatch, 0)) { |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7187 | switch (exp->action) { |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7188 | case ACT_ALLOW: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 7189 | txn->flags |= TX_CLALLOW; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7190 | last_hdr = 1; |
| 7191 | break; |
| 7192 | |
| 7193 | case ACT_DENY: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 7194 | txn->flags |= TX_CLDENY; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7195 | last_hdr = 1; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7196 | break; |
| 7197 | |
| 7198 | case ACT_TARPIT: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 7199 | txn->flags |= TX_CLTARPIT; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7200 | last_hdr = 1; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7201 | break; |
| 7202 | |
| 7203 | case ACT_REPLACE: |
Sasha Pachev | c600204 | 2014-05-26 12:33:48 -0600 | [diff] [blame] | 7204 | trash.len = exp_replace(trash.str, trash.size, cur_ptr, exp->replace, pmatch); |
| 7205 | if (trash.len < 0) |
| 7206 | return -1; |
| 7207 | |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 7208 | delta = buffer_replace2(req->buf, cur_ptr, cur_end, trash.str, trash.len); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7209 | /* FIXME: if the user adds a newline in the replacement, the |
| 7210 | * index will not be recalculated for now, and the new line |
| 7211 | * will not be counted as a new header. |
| 7212 | */ |
| 7213 | |
| 7214 | cur_end += delta; |
| 7215 | cur_next += delta; |
| 7216 | cur_hdr->len += delta; |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 7217 | http_msg_move_end(&txn->req, delta); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7218 | break; |
| 7219 | |
| 7220 | case ACT_REMOVE: |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 7221 | delta = buffer_replace2(req->buf, cur_ptr, cur_next, NULL, 0); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7222 | cur_next += delta; |
| 7223 | |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 7224 | http_msg_move_end(&txn->req, delta); |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 7225 | txn->hdr_idx.v[old_idx].next = cur_hdr->next; |
| 7226 | txn->hdr_idx.used--; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7227 | cur_hdr->len = 0; |
| 7228 | cur_end = NULL; /* null-term has been rewritten */ |
Willy Tarreau | 26db59e | 2010-11-28 06:57:24 +0100 | [diff] [blame] | 7229 | cur_idx = old_idx; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7230 | break; |
| 7231 | |
| 7232 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7233 | } |
| 7234 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7235 | /* keep the link from this header to next one in case of later |
| 7236 | * removal of next header. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7237 | */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7238 | old_idx = cur_idx; |
| 7239 | } |
| 7240 | return 0; |
| 7241 | } |
| 7242 | |
| 7243 | |
| 7244 | /* Apply the filter to the request line. |
| 7245 | * Returns 0 if nothing has been done, 1 if the filter has been applied, |
| 7246 | * or -1 if a replacement resulted in an invalid request line. |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7247 | * Since it can manage the switch to another backend, it updates the per-proxy |
| 7248 | * DENY stats. |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7249 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 7250 | int apply_filter_to_req_line(struct stream *s, struct channel *req, struct hdr_exp *exp) |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7251 | { |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7252 | char *cur_ptr, *cur_end; |
| 7253 | int done; |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 7254 | struct http_txn *txn = s->txn; |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 7255 | int delta; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7256 | |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 7257 | if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT))) |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7258 | return 1; |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 7259 | else if (unlikely(txn->flags & TX_CLALLOW) && |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7260 | (exp->action == ACT_ALLOW || |
| 7261 | exp->action == ACT_DENY || |
| 7262 | exp->action == ACT_TARPIT)) |
| 7263 | return 0; |
| 7264 | else if (exp->action == ACT_REMOVE) |
| 7265 | return 0; |
| 7266 | |
| 7267 | done = 0; |
| 7268 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 7269 | cur_ptr = req->buf->p; |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 7270 | cur_end = cur_ptr + txn->req.sl.rq.l; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7271 | |
| 7272 | /* Now we have the request line between cur_ptr and cur_end */ |
| 7273 | |
Willy Tarreau | 15a53a4 | 2015-01-21 13:39:42 +0100 | [diff] [blame] | 7274 | if (regex_exec_match2(exp->preg, cur_ptr, cur_end-cur_ptr, MAX_MATCH, pmatch, 0)) { |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7275 | switch (exp->action) { |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7276 | case ACT_ALLOW: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 7277 | txn->flags |= TX_CLALLOW; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7278 | done = 1; |
| 7279 | break; |
Willy Tarreau | a496b60 | 2006-12-17 23:15:24 +0100 | [diff] [blame] | 7280 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7281 | case ACT_DENY: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 7282 | txn->flags |= TX_CLDENY; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7283 | done = 1; |
| 7284 | break; |
Willy Tarreau | a496b60 | 2006-12-17 23:15:24 +0100 | [diff] [blame] | 7285 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7286 | case ACT_TARPIT: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 7287 | txn->flags |= TX_CLTARPIT; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7288 | done = 1; |
| 7289 | break; |
Willy Tarreau | a496b60 | 2006-12-17 23:15:24 +0100 | [diff] [blame] | 7290 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7291 | case ACT_REPLACE: |
Sasha Pachev | c600204 | 2014-05-26 12:33:48 -0600 | [diff] [blame] | 7292 | trash.len = exp_replace(trash.str, trash.size, cur_ptr, exp->replace, pmatch); |
| 7293 | if (trash.len < 0) |
| 7294 | return -1; |
| 7295 | |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 7296 | delta = buffer_replace2(req->buf, cur_ptr, cur_end, trash.str, trash.len); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7297 | /* FIXME: if the user adds a newline in the replacement, the |
| 7298 | * index will not be recalculated for now, and the new line |
| 7299 | * will not be counted as a new header. |
| 7300 | */ |
Willy Tarreau | a496b60 | 2006-12-17 23:15:24 +0100 | [diff] [blame] | 7301 | |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 7302 | http_msg_move_end(&txn->req, delta); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7303 | cur_end += delta; |
Willy Tarreau | 69d8c5d | 2012-05-08 09:44:41 +0200 | [diff] [blame] | 7304 | cur_end = (char *)http_parse_reqline(&txn->req, |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7305 | HTTP_MSG_RQMETH, |
| 7306 | cur_ptr, cur_end + 1, |
| 7307 | NULL, NULL); |
| 7308 | if (unlikely(!cur_end)) |
| 7309 | return -1; |
Willy Tarreau | a496b60 | 2006-12-17 23:15:24 +0100 | [diff] [blame] | 7310 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7311 | /* we have a full request and we know that we have either a CR |
| 7312 | * or an LF at <ptr>. |
| 7313 | */ |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 7314 | txn->meth = find_http_meth(cur_ptr, txn->req.sl.rq.m_l); |
| 7315 | hdr_idx_set_start(&txn->hdr_idx, txn->req.sl.rq.l, *cur_end == '\r'); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7316 | /* there is no point trying this regex on headers */ |
| 7317 | return 1; |
| 7318 | } |
| 7319 | } |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7320 | return done; |
| 7321 | } |
Willy Tarreau | 97de624 | 2006-12-27 17:18:38 +0100 | [diff] [blame] | 7322 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7323 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7324 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7325 | /* |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 7326 | * Apply all the req filters of proxy <px> to all headers in buffer <req> of stream <s>. |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7327 | * Returns 0 if everything is alright, or -1 in case a replacement lead to an |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7328 | * unparsable request. Since it can manage the switch to another backend, it |
| 7329 | * updates the per-proxy DENY stats. |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7330 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 7331 | int apply_filters_to_request(struct stream *s, struct channel *req, struct proxy *px) |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7332 | { |
Willy Tarreau | 192252e | 2015-04-04 01:47:55 +0200 | [diff] [blame] | 7333 | struct session *sess = s->sess; |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 7334 | struct http_txn *txn = s->txn; |
Willy Tarreau | 6c123b1 | 2010-01-28 20:22:06 +0100 | [diff] [blame] | 7335 | struct hdr_exp *exp; |
| 7336 | |
| 7337 | for (exp = px->req_exp; exp; exp = exp->next) { |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7338 | int ret; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7339 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7340 | /* |
| 7341 | * The interleaving of transformations and verdicts |
| 7342 | * makes it difficult to decide to continue or stop |
| 7343 | * the evaluation. |
| 7344 | */ |
| 7345 | |
Willy Tarreau | 6c123b1 | 2010-01-28 20:22:06 +0100 | [diff] [blame] | 7346 | if (txn->flags & (TX_CLDENY|TX_CLTARPIT)) |
| 7347 | break; |
| 7348 | |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 7349 | if ((txn->flags & TX_CLALLOW) && |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7350 | (exp->action == ACT_ALLOW || exp->action == ACT_DENY || |
Willy Tarreau | 6c123b1 | 2010-01-28 20:22:06 +0100 | [diff] [blame] | 7351 | exp->action == ACT_TARPIT || exp->action == ACT_PASS)) |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7352 | continue; |
Willy Tarreau | 6c123b1 | 2010-01-28 20:22:06 +0100 | [diff] [blame] | 7353 | |
| 7354 | /* if this filter had a condition, evaluate it now and skip to |
| 7355 | * next filter if the condition does not match. |
| 7356 | */ |
| 7357 | if (exp->cond) { |
Willy Tarreau | 192252e | 2015-04-04 01:47:55 +0200 | [diff] [blame] | 7358 | ret = acl_exec_cond(exp->cond, px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL); |
Willy Tarreau | 6c123b1 | 2010-01-28 20:22:06 +0100 | [diff] [blame] | 7359 | ret = acl_pass(ret); |
| 7360 | if (((struct acl_cond *)exp->cond)->pol == ACL_COND_UNLESS) |
| 7361 | ret = !ret; |
| 7362 | |
| 7363 | if (!ret) |
| 7364 | continue; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7365 | } |
| 7366 | |
| 7367 | /* Apply the filter to the request line. */ |
Willy Tarreau | 6c123b1 | 2010-01-28 20:22:06 +0100 | [diff] [blame] | 7368 | ret = apply_filter_to_req_line(s, req, exp); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7369 | if (unlikely(ret < 0)) |
| 7370 | return -1; |
| 7371 | |
| 7372 | if (likely(ret == 0)) { |
| 7373 | /* The filter did not match the request, it can be |
| 7374 | * iterated through all headers. |
| 7375 | */ |
Willy Tarreau | 34d4c3c | 2015-01-30 20:58:58 +0100 | [diff] [blame] | 7376 | if (unlikely(apply_filter_to_req_headers(s, req, exp) < 0)) |
| 7377 | return -1; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7378 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7379 | } |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7380 | return 0; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7381 | } |
| 7382 | |
| 7383 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7384 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7385 | /* |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 7386 | * Try to retrieve the server associated to the appsession. |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 7387 | * If the server is found, it's assigned to the stream. |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 7388 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 7389 | void manage_client_side_appsession(struct stream *s, const char *buf, int len) { |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 7390 | struct http_txn *txn = s->txn; |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 7391 | appsess *asession = NULL; |
| 7392 | char *sessid_temp = NULL; |
| 7393 | |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 7394 | if (len > s->be->appsession_len) { |
| 7395 | len = s->be->appsession_len; |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 7396 | } |
| 7397 | |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 7398 | if (s->be->options2 & PR_O2_AS_REQL) { |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 7399 | /* request-learn option is enabled : store the sessid in the stream for future use */ |
Willy Tarreau | a3377ee | 2010-01-10 10:49:11 +0100 | [diff] [blame] | 7400 | if (txn->sessid != NULL) { |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 7401 | /* free previously allocated memory as we don't need the stream id found in the URL anymore */ |
Willy Tarreau | a3377ee | 2010-01-10 10:49:11 +0100 | [diff] [blame] | 7402 | pool_free2(apools.sessid, txn->sessid); |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 7403 | } |
| 7404 | |
Willy Tarreau | a3377ee | 2010-01-10 10:49:11 +0100 | [diff] [blame] | 7405 | if ((txn->sessid = pool_alloc2(apools.sessid)) == NULL) { |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 7406 | Alert("Not enough memory process_cli():asession->sessid:malloc().\n"); |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 7407 | send_log(s->be, LOG_ALERT, "Not enough memory process_cli():asession->sessid:malloc().\n"); |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 7408 | return; |
| 7409 | } |
| 7410 | |
Willy Tarreau | a3377ee | 2010-01-10 10:49:11 +0100 | [diff] [blame] | 7411 | memcpy(txn->sessid, buf, len); |
| 7412 | txn->sessid[len] = 0; |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 7413 | } |
| 7414 | |
| 7415 | if ((sessid_temp = pool_alloc2(apools.sessid)) == NULL) { |
| 7416 | Alert("Not enough memory process_cli():asession->sessid:malloc().\n"); |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 7417 | send_log(s->be, LOG_ALERT, "Not enough memory process_cli():asession->sessid:malloc().\n"); |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 7418 | return; |
| 7419 | } |
| 7420 | |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 7421 | memcpy(sessid_temp, buf, len); |
| 7422 | sessid_temp[len] = 0; |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 7423 | |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 7424 | asession = appsession_hash_lookup(&(s->be->htbl_proxy), sessid_temp); |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 7425 | /* free previously allocated memory */ |
| 7426 | pool_free2(apools.sessid, sessid_temp); |
| 7427 | |
| 7428 | if (asession != NULL) { |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 7429 | asession->expire = tick_add_ifset(now_ms, s->be->timeout.appsession); |
| 7430 | if (!(s->be->options2 & PR_O2_AS_REQL)) |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 7431 | asession->request_count++; |
| 7432 | |
| 7433 | if (asession->serverid != NULL) { |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 7434 | struct server *srv = s->be->srv; |
Cyril Bonté | 47fdd8e | 2010-04-25 00:00:51 +0200 | [diff] [blame] | 7435 | |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 7436 | while (srv) { |
| 7437 | if (strcmp(srv->id, asession->serverid) == 0) { |
Willy Tarreau | 892337c | 2014-05-13 23:41:20 +0200 | [diff] [blame] | 7438 | if ((srv->state != SRV_ST_STOPPED) || |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 7439 | (s->be->options & PR_O_PERSIST) || |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 7440 | (s->flags & SF_FORCE_PRST)) { |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 7441 | /* we found the server and it's usable */ |
| 7442 | txn->flags &= ~TX_CK_MASK; |
Willy Tarreau | 892337c | 2014-05-13 23:41:20 +0200 | [diff] [blame] | 7443 | txn->flags |= (srv->state != SRV_ST_STOPPED) ? TX_CK_VALID : TX_CK_DOWN; |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 7444 | s->flags |= SF_DIRECT | SF_ASSIGNED; |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 7445 | s->target = &srv->obj_type; |
Willy Tarreau | 664beb8 | 2011-03-10 11:38:29 +0100 | [diff] [blame] | 7446 | |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 7447 | break; |
| 7448 | } else { |
| 7449 | txn->flags &= ~TX_CK_MASK; |
| 7450 | txn->flags |= TX_CK_DOWN; |
| 7451 | } |
| 7452 | } |
| 7453 | srv = srv->next; |
| 7454 | } |
| 7455 | } |
| 7456 | } |
| 7457 | } |
| 7458 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7459 | /* Find the end of a cookie value contained between <s> and <e>. It works the |
| 7460 | * same way as with headers above except that the semi-colon also ends a token. |
| 7461 | * See RFC2965 for more information. Note that it requires a valid header to |
| 7462 | * return a valid result. |
| 7463 | */ |
| 7464 | char *find_cookie_value_end(char *s, const char *e) |
| 7465 | { |
| 7466 | int quoted, qdpair; |
| 7467 | |
| 7468 | quoted = qdpair = 0; |
| 7469 | for (; s < e; s++) { |
| 7470 | if (qdpair) qdpair = 0; |
| 7471 | else if (quoted) { |
| 7472 | if (*s == '\\') qdpair = 1; |
| 7473 | else if (*s == '"') quoted = 0; |
| 7474 | } |
| 7475 | else if (*s == '"') quoted = 1; |
| 7476 | else if (*s == ',' || *s == ';') return s; |
| 7477 | } |
| 7478 | return s; |
| 7479 | } |
| 7480 | |
| 7481 | /* Delete a value in a header between delimiters <from> and <next> in buffer |
| 7482 | * <buf>. The number of characters displaced is returned, and the pointer to |
| 7483 | * the first delimiter is updated if required. The function tries as much as |
| 7484 | * possible to respect the following principles : |
| 7485 | * - replace <from> delimiter by the <next> one unless <from> points to a |
| 7486 | * colon, in which case <next> is simply removed |
| 7487 | * - set exactly one space character after the new first delimiter, unless |
| 7488 | * there are not enough characters in the block being moved to do so. |
| 7489 | * - remove unneeded spaces before the previous delimiter and after the new |
| 7490 | * one. |
| 7491 | * |
| 7492 | * It is the caller's responsibility to ensure that : |
| 7493 | * - <from> points to a valid delimiter or the colon ; |
| 7494 | * - <next> points to a valid delimiter or the final CR/LF ; |
| 7495 | * - there are non-space chars before <from> ; |
| 7496 | * - there is a CR/LF at or after <next>. |
| 7497 | */ |
Willy Tarreau | af81935 | 2012-08-27 22:08:00 +0200 | [diff] [blame] | 7498 | int del_hdr_value(struct buffer *buf, char **from, char *next) |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7499 | { |
| 7500 | char *prev = *from; |
| 7501 | |
| 7502 | if (*prev == ':') { |
| 7503 | /* We're removing the first value, preserve the colon and add a |
| 7504 | * space if possible. |
| 7505 | */ |
| 7506 | if (!http_is_crlf[(unsigned char)*next]) |
| 7507 | next++; |
| 7508 | prev++; |
| 7509 | if (prev < next) |
| 7510 | *prev++ = ' '; |
| 7511 | |
| 7512 | while (http_is_spht[(unsigned char)*next]) |
| 7513 | next++; |
| 7514 | } else { |
| 7515 | /* Remove useless spaces before the old delimiter. */ |
| 7516 | while (http_is_spht[(unsigned char)*(prev-1)]) |
| 7517 | prev--; |
| 7518 | *from = prev; |
| 7519 | |
| 7520 | /* copy the delimiter and if possible a space if we're |
| 7521 | * not at the end of the line. |
| 7522 | */ |
| 7523 | if (!http_is_crlf[(unsigned char)*next]) { |
| 7524 | *prev++ = *next++; |
| 7525 | if (prev + 1 < next) |
| 7526 | *prev++ = ' '; |
| 7527 | while (http_is_spht[(unsigned char)*next]) |
| 7528 | next++; |
| 7529 | } |
| 7530 | } |
| 7531 | return buffer_replace2(buf, prev, next, NULL, 0); |
| 7532 | } |
| 7533 | |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 7534 | /* |
Willy Tarreau | 396d2c6 | 2007-11-04 19:30:00 +0100 | [diff] [blame] | 7535 | * Manage client-side cookie. It can impact performance by about 2% so it is |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7536 | * desirable to call it only when needed. This code is quite complex because |
| 7537 | * of the multiple very crappy and ambiguous syntaxes we have to support. it |
| 7538 | * highly recommended not to touch this part without a good reason ! |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7539 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 7540 | void manage_client_side_cookies(struct stream *s, struct channel *req) |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7541 | { |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 7542 | struct http_txn *txn = s->txn; |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 7543 | struct session *sess = s->sess; |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7544 | int preserve_hdr; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7545 | int cur_idx, old_idx; |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7546 | char *hdr_beg, *hdr_end, *hdr_next, *del_from; |
| 7547 | char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7548 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7549 | /* Iterate through the headers, we start with the start line. */ |
Willy Tarreau | 83969f4 | 2007-01-22 08:55:47 +0100 | [diff] [blame] | 7550 | old_idx = 0; |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 7551 | hdr_next = req->buf->p + hdr_idx_first_pos(&txn->hdr_idx); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7552 | |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 7553 | while ((cur_idx = txn->hdr_idx.v[old_idx].next)) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7554 | struct hdr_idx_elem *cur_hdr; |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 7555 | int val; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7556 | |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 7557 | cur_hdr = &txn->hdr_idx.v[cur_idx]; |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7558 | hdr_beg = hdr_next; |
| 7559 | hdr_end = hdr_beg + cur_hdr->len; |
| 7560 | hdr_next = hdr_end + cur_hdr->cr + 1; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7561 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7562 | /* We have one full header between hdr_beg and hdr_end, and the |
| 7563 | * next header starts at hdr_next. We're only interested in |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7564 | * "Cookie:" headers. |
| 7565 | */ |
| 7566 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7567 | val = http_header_match2(hdr_beg, hdr_end, "Cookie", 6); |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 7568 | if (!val) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7569 | old_idx = cur_idx; |
| 7570 | continue; |
| 7571 | } |
| 7572 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7573 | del_from = NULL; /* nothing to be deleted */ |
| 7574 | preserve_hdr = 0; /* assume we may kill the whole header */ |
| 7575 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7576 | /* Now look for cookies. Conforming to RFC2109, we have to support |
| 7577 | * attributes whose name begin with a '$', and associate them with |
| 7578 | * the right cookie, if we want to delete this cookie. |
| 7579 | * So there are 3 cases for each cookie read : |
| 7580 | * 1) it's a special attribute, beginning with a '$' : ignore it. |
| 7581 | * 2) it's a server id cookie that we *MAY* want to delete : save |
| 7582 | * some pointers on it (last semi-colon, beginning of cookie...) |
| 7583 | * 3) it's an application cookie : we *MAY* have to delete a previous |
| 7584 | * "special" cookie. |
| 7585 | * At the end of loop, if a "special" cookie remains, we may have to |
| 7586 | * remove it. If no application cookie persists in the header, we |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7587 | * *MUST* delete it. |
| 7588 | * |
| 7589 | * Note: RFC2965 is unclear about the processing of spaces around |
| 7590 | * the equal sign in the ATTR=VALUE form. A careful inspection of |
| 7591 | * the RFC explicitly allows spaces before it, and not within the |
| 7592 | * tokens (attrs or values). An inspection of RFC2109 allows that |
| 7593 | * too but section 10.1.3 lets one think that spaces may be allowed |
| 7594 | * after the equal sign too, resulting in some (rare) buggy |
| 7595 | * implementations trying to do that. So let's do what servers do. |
| 7596 | * Latest ietf draft forbids spaces all around. Also, earlier RFCs |
| 7597 | * allowed quoted strings in values, with any possible character |
| 7598 | * after a backslash, including control chars and delimitors, which |
| 7599 | * causes parsing to become ambiguous. Browsers also allow spaces |
| 7600 | * within values even without quotes. |
| 7601 | * |
| 7602 | * We have to keep multiple pointers in order to support cookie |
| 7603 | * removal at the beginning, middle or end of header without |
| 7604 | * corrupting the header. All of these headers are valid : |
| 7605 | * |
| 7606 | * Cookie:NAME1=VALUE1;NAME2=VALUE2;NAME3=VALUE3\r\n |
| 7607 | * Cookie:NAME1=VALUE1;NAME2_ONLY ;NAME3=VALUE3\r\n |
| 7608 | * Cookie: NAME1 = VALUE 1 ; NAME2 = VALUE2 ; NAME3 = VALUE3\r\n |
| 7609 | * | | | | | | | | | |
| 7610 | * | | | | | | | | hdr_end <--+ |
| 7611 | * | | | | | | | +--> next |
| 7612 | * | | | | | | +----> val_end |
| 7613 | * | | | | | +-----------> val_beg |
| 7614 | * | | | | +--------------> equal |
| 7615 | * | | | +----------------> att_end |
| 7616 | * | | +---------------------> att_beg |
| 7617 | * | +--------------------------> prev |
| 7618 | * +--------------------------------> hdr_beg |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7619 | */ |
| 7620 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7621 | for (prev = hdr_beg + 6; prev < hdr_end; prev = next) { |
| 7622 | /* Iterate through all cookies on this line */ |
| 7623 | |
| 7624 | /* find att_beg */ |
| 7625 | att_beg = prev + 1; |
| 7626 | while (att_beg < hdr_end && http_is_spht[(unsigned char)*att_beg]) |
| 7627 | att_beg++; |
| 7628 | |
| 7629 | /* find att_end : this is the first character after the last non |
| 7630 | * space before the equal. It may be equal to hdr_end. |
| 7631 | */ |
| 7632 | equal = att_end = att_beg; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7633 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7634 | while (equal < hdr_end) { |
| 7635 | if (*equal == '=' || *equal == ',' || *equal == ';') |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7636 | break; |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7637 | if (http_is_spht[(unsigned char)*equal++]) |
| 7638 | continue; |
| 7639 | att_end = equal; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7640 | } |
| 7641 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7642 | /* here, <equal> points to '=', a delimitor or the end. <att_end> |
| 7643 | * is between <att_beg> and <equal>, both may be identical. |
| 7644 | */ |
| 7645 | |
| 7646 | /* look for end of cookie if there is an equal sign */ |
| 7647 | if (equal < hdr_end && *equal == '=') { |
| 7648 | /* look for the beginning of the value */ |
| 7649 | val_beg = equal + 1; |
| 7650 | while (val_beg < hdr_end && http_is_spht[(unsigned char)*val_beg]) |
| 7651 | val_beg++; |
| 7652 | |
| 7653 | /* find the end of the value, respecting quotes */ |
| 7654 | next = find_cookie_value_end(val_beg, hdr_end); |
| 7655 | |
| 7656 | /* make val_end point to the first white space or delimitor after the value */ |
| 7657 | val_end = next; |
| 7658 | while (val_end > val_beg && http_is_spht[(unsigned char)*(val_end - 1)]) |
| 7659 | val_end--; |
| 7660 | } else { |
| 7661 | val_beg = val_end = next = equal; |
Willy Tarreau | 305ae85 | 2010-01-03 19:45:54 +0100 | [diff] [blame] | 7662 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7663 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7664 | /* We have nothing to do with attributes beginning with '$'. However, |
| 7665 | * they will automatically be removed if a header before them is removed, |
| 7666 | * since they're supposed to be linked together. |
| 7667 | */ |
| 7668 | if (*att_beg == '$') |
| 7669 | continue; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7670 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7671 | /* Ignore cookies with no equal sign */ |
| 7672 | if (equal == next) { |
| 7673 | /* This is not our cookie, so we must preserve it. But if we already |
| 7674 | * scheduled another cookie for removal, we cannot remove the |
| 7675 | * complete header, but we can remove the previous block itself. |
| 7676 | */ |
| 7677 | preserve_hdr = 1; |
| 7678 | if (del_from != NULL) { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 7679 | int delta = del_hdr_value(req->buf, &del_from, prev); |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7680 | val_end += delta; |
| 7681 | next += delta; |
| 7682 | hdr_end += delta; |
| 7683 | hdr_next += delta; |
| 7684 | cur_hdr->len += delta; |
| 7685 | http_msg_move_end(&txn->req, delta); |
| 7686 | prev = del_from; |
| 7687 | del_from = NULL; |
| 7688 | } |
| 7689 | continue; |
Willy Tarreau | 305ae85 | 2010-01-03 19:45:54 +0100 | [diff] [blame] | 7690 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7691 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7692 | /* if there are spaces around the equal sign, we need to |
| 7693 | * strip them otherwise we'll get trouble for cookie captures, |
| 7694 | * or even for rewrites. Since this happens extremely rarely, |
| 7695 | * it does not hurt performance. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7696 | */ |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7697 | if (unlikely(att_end != equal || val_beg > equal + 1)) { |
| 7698 | int stripped_before = 0; |
| 7699 | int stripped_after = 0; |
| 7700 | |
| 7701 | if (att_end != equal) { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 7702 | stripped_before = buffer_replace2(req->buf, att_end, equal, NULL, 0); |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7703 | equal += stripped_before; |
| 7704 | val_beg += stripped_before; |
| 7705 | } |
| 7706 | |
| 7707 | if (val_beg > equal + 1) { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 7708 | stripped_after = buffer_replace2(req->buf, equal + 1, val_beg, NULL, 0); |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7709 | val_beg += stripped_after; |
| 7710 | stripped_before += stripped_after; |
| 7711 | } |
| 7712 | |
| 7713 | val_end += stripped_before; |
| 7714 | next += stripped_before; |
| 7715 | hdr_end += stripped_before; |
| 7716 | hdr_next += stripped_before; |
| 7717 | cur_hdr->len += stripped_before; |
| 7718 | http_msg_move_end(&txn->req, stripped_before); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7719 | } |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7720 | /* now everything is as on the diagram above */ |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7721 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7722 | /* First, let's see if we want to capture this cookie. We check |
| 7723 | * that we don't already have a client side cookie, because we |
| 7724 | * can only capture one. Also as an optimisation, we ignore |
| 7725 | * cookies shorter than the declared name. |
| 7726 | */ |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 7727 | if (sess->fe->capture_name != NULL && txn->cli_cookie == NULL && |
| 7728 | (val_end - att_beg >= sess->fe->capture_namelen) && |
| 7729 | memcmp(att_beg, sess->fe->capture_name, sess->fe->capture_namelen) == 0) { |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7730 | int log_len = val_end - att_beg; |
| 7731 | |
| 7732 | if ((txn->cli_cookie = pool_alloc2(pool2_capture)) == NULL) { |
| 7733 | Alert("HTTP logging : out of memory.\n"); |
| 7734 | } else { |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 7735 | if (log_len > sess->fe->capture_len) |
| 7736 | log_len = sess->fe->capture_len; |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7737 | memcpy(txn->cli_cookie, att_beg, log_len); |
| 7738 | txn->cli_cookie[log_len] = 0; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7739 | } |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7740 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7741 | |
Willy Tarreau | bca9969 | 2010-10-06 19:25:55 +0200 | [diff] [blame] | 7742 | /* Persistence cookies in passive, rewrite or insert mode have the |
| 7743 | * following form : |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7744 | * |
Willy Tarreau | bca9969 | 2010-10-06 19:25:55 +0200 | [diff] [blame] | 7745 | * Cookie: NAME=SRV[|<lastseen>[|<firstseen>]] |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7746 | * |
Willy Tarreau | bca9969 | 2010-10-06 19:25:55 +0200 | [diff] [blame] | 7747 | * For cookies in prefix mode, the form is : |
| 7748 | * |
| 7749 | * Cookie: NAME=SRV~VALUE |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7750 | */ |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 7751 | if ((att_end - att_beg == s->be->cookie_len) && (s->be->cookie_name != NULL) && |
| 7752 | (memcmp(att_beg, s->be->cookie_name, att_end - att_beg) == 0)) { |
| 7753 | struct server *srv = s->be->srv; |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7754 | char *delim; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7755 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7756 | /* if we're in cookie prefix mode, we'll search the delimitor so that we |
| 7757 | * have the server ID between val_beg and delim, and the original cookie between |
| 7758 | * delim+1 and val_end. Otherwise, delim==val_end : |
| 7759 | * |
| 7760 | * Cookie: NAME=SRV; # in all but prefix modes |
| 7761 | * Cookie: NAME=SRV~OPAQUE ; # in prefix mode |
| 7762 | * | || || | |+-> next |
| 7763 | * | || || | +--> val_end |
| 7764 | * | || || +---------> delim |
| 7765 | * | || |+------------> val_beg |
| 7766 | * | || +-------------> att_end = equal |
| 7767 | * | |+-----------------> att_beg |
| 7768 | * | +------------------> prev |
| 7769 | * +-------------------------> hdr_beg |
| 7770 | */ |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7771 | |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 7772 | if (s->be->ck_opts & PR_CK_PFX) { |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7773 | for (delim = val_beg; delim < val_end; delim++) |
| 7774 | if (*delim == COOKIE_DELIM) |
| 7775 | break; |
Willy Tarreau | bca9969 | 2010-10-06 19:25:55 +0200 | [diff] [blame] | 7776 | } else { |
| 7777 | char *vbar1; |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7778 | delim = val_end; |
Willy Tarreau | bca9969 | 2010-10-06 19:25:55 +0200 | [diff] [blame] | 7779 | /* Now check if the cookie contains a date field, which would |
| 7780 | * appear after a vertical bar ('|') just after the server name |
| 7781 | * and before the delimiter. |
| 7782 | */ |
| 7783 | vbar1 = memchr(val_beg, COOKIE_DELIM_DATE, val_end - val_beg); |
| 7784 | if (vbar1) { |
| 7785 | /* OK, so left of the bar is the server's cookie and |
Willy Tarreau | f64d141 | 2010-10-07 20:06:11 +0200 | [diff] [blame] | 7786 | * right is the last seen date. It is a base64 encoded |
| 7787 | * 30-bit value representing the UNIX date since the |
| 7788 | * epoch in 4-second quantities. |
Willy Tarreau | bca9969 | 2010-10-06 19:25:55 +0200 | [diff] [blame] | 7789 | */ |
Willy Tarreau | f64d141 | 2010-10-07 20:06:11 +0200 | [diff] [blame] | 7790 | int val; |
Willy Tarreau | bca9969 | 2010-10-06 19:25:55 +0200 | [diff] [blame] | 7791 | delim = vbar1++; |
Willy Tarreau | f64d141 | 2010-10-07 20:06:11 +0200 | [diff] [blame] | 7792 | if (val_end - vbar1 >= 5) { |
| 7793 | val = b64tos30(vbar1); |
| 7794 | if (val > 0) |
| 7795 | txn->cookie_last_date = val << 2; |
| 7796 | } |
| 7797 | /* look for a second vertical bar */ |
| 7798 | vbar1 = memchr(vbar1, COOKIE_DELIM_DATE, val_end - vbar1); |
| 7799 | if (vbar1 && (val_end - vbar1 > 5)) { |
| 7800 | val = b64tos30(vbar1 + 1); |
| 7801 | if (val > 0) |
| 7802 | txn->cookie_first_date = val << 2; |
| 7803 | } |
Willy Tarreau | bca9969 | 2010-10-06 19:25:55 +0200 | [diff] [blame] | 7804 | } |
| 7805 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7806 | |
Willy Tarreau | f64d141 | 2010-10-07 20:06:11 +0200 | [diff] [blame] | 7807 | /* if the cookie has an expiration date and the proxy wants to check |
| 7808 | * it, then we do that now. We first check if the cookie is too old, |
| 7809 | * then only if it has expired. We detect strict overflow because the |
| 7810 | * time resolution here is not great (4 seconds). Cookies with dates |
| 7811 | * in the future are ignored if their offset is beyond one day. This |
| 7812 | * allows an admin to fix timezone issues without expiring everyone |
| 7813 | * and at the same time avoids keeping unwanted side effects for too |
| 7814 | * long. |
| 7815 | */ |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 7816 | if (txn->cookie_first_date && s->be->cookie_maxlife && |
| 7817 | (((signed)(date.tv_sec - txn->cookie_first_date) > (signed)s->be->cookie_maxlife) || |
Willy Tarreau | ef4f391 | 2010-10-07 21:00:29 +0200 | [diff] [blame] | 7818 | ((signed)(txn->cookie_first_date - date.tv_sec) > 86400))) { |
Willy Tarreau | f64d141 | 2010-10-07 20:06:11 +0200 | [diff] [blame] | 7819 | txn->flags &= ~TX_CK_MASK; |
| 7820 | txn->flags |= TX_CK_OLD; |
| 7821 | delim = val_beg; // let's pretend we have not found the cookie |
| 7822 | txn->cookie_first_date = 0; |
| 7823 | txn->cookie_last_date = 0; |
| 7824 | } |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 7825 | else if (txn->cookie_last_date && s->be->cookie_maxidle && |
| 7826 | (((signed)(date.tv_sec - txn->cookie_last_date) > (signed)s->be->cookie_maxidle) || |
Willy Tarreau | ef4f391 | 2010-10-07 21:00:29 +0200 | [diff] [blame] | 7827 | ((signed)(txn->cookie_last_date - date.tv_sec) > 86400))) { |
Willy Tarreau | f64d141 | 2010-10-07 20:06:11 +0200 | [diff] [blame] | 7828 | txn->flags &= ~TX_CK_MASK; |
| 7829 | txn->flags |= TX_CK_EXPIRED; |
| 7830 | delim = val_beg; // let's pretend we have not found the cookie |
| 7831 | txn->cookie_first_date = 0; |
| 7832 | txn->cookie_last_date = 0; |
| 7833 | } |
| 7834 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7835 | /* Here, we'll look for the first running server which supports the cookie. |
| 7836 | * This allows to share a same cookie between several servers, for example |
| 7837 | * to dedicate backup servers to specific servers only. |
| 7838 | * However, to prevent clients from sticking to cookie-less backup server |
| 7839 | * when they have incidentely learned an empty cookie, we simply ignore |
| 7840 | * empty cookies and mark them as invalid. |
| 7841 | * The same behaviour is applied when persistence must be ignored. |
| 7842 | */ |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 7843 | if ((delim == val_beg) || (s->flags & (SF_IGNORE_PRST | SF_ASSIGNED))) |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7844 | srv = NULL; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7845 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7846 | while (srv) { |
| 7847 | if (srv->cookie && (srv->cklen == delim - val_beg) && |
| 7848 | !memcmp(val_beg, srv->cookie, delim - val_beg)) { |
Willy Tarreau | 892337c | 2014-05-13 23:41:20 +0200 | [diff] [blame] | 7849 | if ((srv->state != SRV_ST_STOPPED) || |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 7850 | (s->be->options & PR_O_PERSIST) || |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 7851 | (s->flags & SF_FORCE_PRST)) { |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7852 | /* we found the server and we can use it */ |
| 7853 | txn->flags &= ~TX_CK_MASK; |
Willy Tarreau | 892337c | 2014-05-13 23:41:20 +0200 | [diff] [blame] | 7854 | txn->flags |= (srv->state != SRV_ST_STOPPED) ? TX_CK_VALID : TX_CK_DOWN; |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 7855 | s->flags |= SF_DIRECT | SF_ASSIGNED; |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 7856 | s->target = &srv->obj_type; |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7857 | break; |
| 7858 | } else { |
| 7859 | /* we found a server, but it's down, |
| 7860 | * mark it as such and go on in case |
| 7861 | * another one is available. |
| 7862 | */ |
| 7863 | txn->flags &= ~TX_CK_MASK; |
| 7864 | txn->flags |= TX_CK_DOWN; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7865 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7866 | } |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7867 | srv = srv->next; |
| 7868 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7869 | |
Willy Tarreau | f64d141 | 2010-10-07 20:06:11 +0200 | [diff] [blame] | 7870 | if (!srv && !(txn->flags & (TX_CK_DOWN|TX_CK_EXPIRED|TX_CK_OLD))) { |
Willy Tarreau | c89ccb6 | 2012-04-05 21:18:22 +0200 | [diff] [blame] | 7871 | /* no server matched this cookie or we deliberately skipped it */ |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7872 | txn->flags &= ~TX_CK_MASK; |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 7873 | if ((s->flags & (SF_IGNORE_PRST | SF_ASSIGNED))) |
Willy Tarreau | c89ccb6 | 2012-04-05 21:18:22 +0200 | [diff] [blame] | 7874 | txn->flags |= TX_CK_UNUSED; |
| 7875 | else |
| 7876 | txn->flags |= TX_CK_INVALID; |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7877 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7878 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7879 | /* depending on the cookie mode, we may have to either : |
| 7880 | * - delete the complete cookie if we're in insert+indirect mode, so that |
| 7881 | * the server never sees it ; |
| 7882 | * - remove the server id from the cookie value, and tag the cookie as an |
| 7883 | * application cookie so that it does not get accidentely removed later, |
| 7884 | * if we're in cookie prefix mode |
| 7885 | */ |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 7886 | if ((s->be->ck_opts & PR_CK_PFX) && (delim != val_end)) { |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7887 | int delta; /* negative */ |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7888 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 7889 | delta = buffer_replace2(req->buf, val_beg, delim + 1, NULL, 0); |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7890 | val_end += delta; |
| 7891 | next += delta; |
| 7892 | hdr_end += delta; |
| 7893 | hdr_next += delta; |
| 7894 | cur_hdr->len += delta; |
| 7895 | http_msg_move_end(&txn->req, delta); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7896 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7897 | del_from = NULL; |
| 7898 | preserve_hdr = 1; /* we want to keep this cookie */ |
| 7899 | } |
| 7900 | else if (del_from == NULL && |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 7901 | (s->be->ck_opts & (PR_CK_INS | PR_CK_IND)) == (PR_CK_INS | PR_CK_IND)) { |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7902 | del_from = prev; |
| 7903 | } |
| 7904 | } else { |
| 7905 | /* This is not our cookie, so we must preserve it. But if we already |
| 7906 | * scheduled another cookie for removal, we cannot remove the |
| 7907 | * complete header, but we can remove the previous block itself. |
| 7908 | */ |
| 7909 | preserve_hdr = 1; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7910 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7911 | if (del_from != NULL) { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 7912 | int delta = del_hdr_value(req->buf, &del_from, prev); |
Willy Tarreau | b810554 | 2010-11-24 18:31:28 +0100 | [diff] [blame] | 7913 | if (att_beg >= del_from) |
| 7914 | att_beg += delta; |
| 7915 | if (att_end >= del_from) |
| 7916 | att_end += delta; |
| 7917 | val_beg += delta; |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7918 | val_end += delta; |
| 7919 | next += delta; |
| 7920 | hdr_end += delta; |
| 7921 | hdr_next += delta; |
| 7922 | cur_hdr->len += delta; |
| 7923 | http_msg_move_end(&txn->req, delta); |
| 7924 | prev = del_from; |
| 7925 | del_from = NULL; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7926 | } |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7927 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7928 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7929 | /* Look for the appsession cookie unless persistence must be ignored */ |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 7930 | if (!(s->flags & SF_IGNORE_PRST) && (s->be->appsession_name != NULL)) { |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7931 | int cmp_len, value_len; |
| 7932 | char *value_begin; |
Aleksandar Lazic | 697bbb0 | 2008-08-13 19:57:02 +0200 | [diff] [blame] | 7933 | |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 7934 | if (s->be->options2 & PR_O2_AS_PFX) { |
| 7935 | cmp_len = MIN(val_end - att_beg, s->be->appsession_name_len); |
| 7936 | value_begin = att_beg + s->be->appsession_name_len; |
| 7937 | value_len = val_end - att_beg - s->be->appsession_name_len; |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7938 | } else { |
| 7939 | cmp_len = att_end - att_beg; |
| 7940 | value_begin = val_beg; |
| 7941 | value_len = val_end - val_beg; |
| 7942 | } |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 7943 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7944 | /* let's see if the cookie is our appcookie */ |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 7945 | if (cmp_len == s->be->appsession_name_len && |
| 7946 | memcmp(att_beg, s->be->appsession_name, cmp_len) == 0) { |
| 7947 | manage_client_side_appsession(s, value_begin, value_len); |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7948 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7949 | } |
| 7950 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7951 | /* continue with next cookie on this header line */ |
| 7952 | att_beg = next; |
| 7953 | } /* for each cookie */ |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7954 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7955 | /* There are no more cookies on this line. |
| 7956 | * We may still have one (or several) marked for deletion at the |
| 7957 | * end of the line. We must do this now in two ways : |
| 7958 | * - if some cookies must be preserved, we only delete from the |
| 7959 | * mark to the end of line ; |
| 7960 | * - if nothing needs to be preserved, simply delete the whole header |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7961 | */ |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7962 | if (del_from) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7963 | int delta; |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7964 | if (preserve_hdr) { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 7965 | delta = del_hdr_value(req->buf, &del_from, hdr_end); |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7966 | hdr_end = del_from; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7967 | cur_hdr->len += delta; |
| 7968 | } else { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 7969 | delta = buffer_replace2(req->buf, hdr_beg, hdr_next, NULL, 0); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7970 | |
| 7971 | /* FIXME: this should be a separate function */ |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 7972 | txn->hdr_idx.v[old_idx].next = cur_hdr->next; |
| 7973 | txn->hdr_idx.used--; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7974 | cur_hdr->len = 0; |
Willy Tarreau | 26db59e | 2010-11-28 06:57:24 +0100 | [diff] [blame] | 7975 | cur_idx = old_idx; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7976 | } |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7977 | hdr_next += delta; |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 7978 | http_msg_move_end(&txn->req, delta); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7979 | } |
| 7980 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7981 | /* check next header */ |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7982 | old_idx = cur_idx; |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7983 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7984 | } |
| 7985 | |
| 7986 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7987 | /* Iterate the same filter through all response headers contained in <rtr>. |
| 7988 | * Returns 1 if this filter can be stopped upon return, otherwise 0. |
| 7989 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 7990 | int apply_filter_to_resp_headers(struct stream *s, struct channel *rtr, struct hdr_exp *exp) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7991 | { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7992 | char *cur_ptr, *cur_end, *cur_next; |
| 7993 | int cur_idx, old_idx, last_hdr; |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 7994 | struct http_txn *txn = s->txn; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7995 | struct hdr_idx_elem *cur_hdr; |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 7996 | int delta; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7997 | |
| 7998 | last_hdr = 0; |
| 7999 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 8000 | cur_next = rtr->buf->p + hdr_idx_first_pos(&txn->hdr_idx); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8001 | old_idx = 0; |
| 8002 | |
| 8003 | while (!last_hdr) { |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 8004 | if (unlikely(txn->flags & TX_SVDENY)) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8005 | return 1; |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 8006 | else if (unlikely(txn->flags & TX_SVALLOW) && |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8007 | (exp->action == ACT_ALLOW || |
| 8008 | exp->action == ACT_DENY)) |
| 8009 | return 0; |
| 8010 | |
| 8011 | cur_idx = txn->hdr_idx.v[old_idx].next; |
| 8012 | if (!cur_idx) |
| 8013 | break; |
| 8014 | |
| 8015 | cur_hdr = &txn->hdr_idx.v[cur_idx]; |
| 8016 | cur_ptr = cur_next; |
| 8017 | cur_end = cur_ptr + cur_hdr->len; |
| 8018 | cur_next = cur_end + cur_hdr->cr + 1; |
| 8019 | |
| 8020 | /* Now we have one header between cur_ptr and cur_end, |
| 8021 | * and the next header starts at cur_next. |
| 8022 | */ |
| 8023 | |
Willy Tarreau | 15a53a4 | 2015-01-21 13:39:42 +0100 | [diff] [blame] | 8024 | if (regex_exec_match2(exp->preg, cur_ptr, cur_end-cur_ptr, MAX_MATCH, pmatch, 0)) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8025 | switch (exp->action) { |
| 8026 | case ACT_ALLOW: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 8027 | txn->flags |= TX_SVALLOW; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8028 | last_hdr = 1; |
| 8029 | break; |
| 8030 | |
| 8031 | case ACT_DENY: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 8032 | txn->flags |= TX_SVDENY; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8033 | last_hdr = 1; |
| 8034 | break; |
| 8035 | |
| 8036 | case ACT_REPLACE: |
Sasha Pachev | c600204 | 2014-05-26 12:33:48 -0600 | [diff] [blame] | 8037 | trash.len = exp_replace(trash.str, trash.size, cur_ptr, exp->replace, pmatch); |
| 8038 | if (trash.len < 0) |
| 8039 | return -1; |
| 8040 | |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 8041 | delta = buffer_replace2(rtr->buf, cur_ptr, cur_end, trash.str, trash.len); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8042 | /* FIXME: if the user adds a newline in the replacement, the |
| 8043 | * index will not be recalculated for now, and the new line |
| 8044 | * will not be counted as a new header. |
| 8045 | */ |
| 8046 | |
| 8047 | cur_end += delta; |
| 8048 | cur_next += delta; |
| 8049 | cur_hdr->len += delta; |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 8050 | http_msg_move_end(&txn->rsp, delta); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8051 | break; |
| 8052 | |
| 8053 | case ACT_REMOVE: |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 8054 | delta = buffer_replace2(rtr->buf, cur_ptr, cur_next, NULL, 0); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8055 | cur_next += delta; |
| 8056 | |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 8057 | http_msg_move_end(&txn->rsp, delta); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8058 | txn->hdr_idx.v[old_idx].next = cur_hdr->next; |
| 8059 | txn->hdr_idx.used--; |
| 8060 | cur_hdr->len = 0; |
| 8061 | cur_end = NULL; /* null-term has been rewritten */ |
Willy Tarreau | 26db59e | 2010-11-28 06:57:24 +0100 | [diff] [blame] | 8062 | cur_idx = old_idx; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8063 | break; |
| 8064 | |
| 8065 | } |
| 8066 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8067 | |
| 8068 | /* keep the link from this header to next one in case of later |
| 8069 | * removal of next header. |
| 8070 | */ |
| 8071 | old_idx = cur_idx; |
| 8072 | } |
| 8073 | return 0; |
| 8074 | } |
| 8075 | |
| 8076 | |
| 8077 | /* Apply the filter to the status line in the response buffer <rtr>. |
| 8078 | * Returns 0 if nothing has been done, 1 if the filter has been applied, |
| 8079 | * or -1 if a replacement resulted in an invalid status line. |
| 8080 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 8081 | int apply_filter_to_sts_line(struct stream *s, struct channel *rtr, struct hdr_exp *exp) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8082 | { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8083 | char *cur_ptr, *cur_end; |
| 8084 | int done; |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 8085 | struct http_txn *txn = s->txn; |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 8086 | int delta; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8087 | |
| 8088 | |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 8089 | if (unlikely(txn->flags & TX_SVDENY)) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8090 | return 1; |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 8091 | else if (unlikely(txn->flags & TX_SVALLOW) && |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8092 | (exp->action == ACT_ALLOW || |
| 8093 | exp->action == ACT_DENY)) |
| 8094 | return 0; |
| 8095 | else if (exp->action == ACT_REMOVE) |
| 8096 | return 0; |
| 8097 | |
| 8098 | done = 0; |
| 8099 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 8100 | cur_ptr = rtr->buf->p; |
Willy Tarreau | 1ba0e5f | 2010-06-07 13:57:32 +0200 | [diff] [blame] | 8101 | cur_end = cur_ptr + txn->rsp.sl.st.l; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8102 | |
| 8103 | /* Now we have the status line between cur_ptr and cur_end */ |
| 8104 | |
Willy Tarreau | 15a53a4 | 2015-01-21 13:39:42 +0100 | [diff] [blame] | 8105 | if (regex_exec_match2(exp->preg, cur_ptr, cur_end-cur_ptr, MAX_MATCH, pmatch, 0)) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8106 | switch (exp->action) { |
| 8107 | case ACT_ALLOW: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 8108 | txn->flags |= TX_SVALLOW; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8109 | done = 1; |
| 8110 | break; |
| 8111 | |
| 8112 | case ACT_DENY: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 8113 | txn->flags |= TX_SVDENY; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8114 | done = 1; |
| 8115 | break; |
| 8116 | |
| 8117 | case ACT_REPLACE: |
Sasha Pachev | c600204 | 2014-05-26 12:33:48 -0600 | [diff] [blame] | 8118 | trash.len = exp_replace(trash.str, trash.size, cur_ptr, exp->replace, pmatch); |
| 8119 | if (trash.len < 0) |
| 8120 | return -1; |
| 8121 | |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 8122 | delta = buffer_replace2(rtr->buf, cur_ptr, cur_end, trash.str, trash.len); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8123 | /* FIXME: if the user adds a newline in the replacement, the |
| 8124 | * index will not be recalculated for now, and the new line |
| 8125 | * will not be counted as a new header. |
| 8126 | */ |
| 8127 | |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 8128 | http_msg_move_end(&txn->rsp, delta); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8129 | cur_end += delta; |
Willy Tarreau | 69d8c5d | 2012-05-08 09:44:41 +0200 | [diff] [blame] | 8130 | cur_end = (char *)http_parse_stsline(&txn->rsp, |
Willy Tarreau | 0278576 | 2007-04-03 14:45:44 +0200 | [diff] [blame] | 8131 | HTTP_MSG_RPVER, |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8132 | cur_ptr, cur_end + 1, |
| 8133 | NULL, NULL); |
| 8134 | if (unlikely(!cur_end)) |
| 8135 | return -1; |
| 8136 | |
| 8137 | /* we have a full respnse and we know that we have either a CR |
| 8138 | * or an LF at <ptr>. |
| 8139 | */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 8140 | txn->status = strl2ui(rtr->buf->p + txn->rsp.sl.st.c, txn->rsp.sl.st.c_l); |
Willy Tarreau | 1ba0e5f | 2010-06-07 13:57:32 +0200 | [diff] [blame] | 8141 | hdr_idx_set_start(&txn->hdr_idx, txn->rsp.sl.st.l, *cur_end == '\r'); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8142 | /* there is no point trying this regex on headers */ |
| 8143 | return 1; |
| 8144 | } |
| 8145 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8146 | return done; |
| 8147 | } |
| 8148 | |
| 8149 | |
| 8150 | |
| 8151 | /* |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 8152 | * Apply all the resp filters of proxy <px> to all headers in buffer <rtr> of stream <s>. |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8153 | * Returns 0 if everything is alright, or -1 in case a replacement lead to an |
| 8154 | * unparsable response. |
| 8155 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 8156 | int apply_filters_to_response(struct stream *s, struct channel *rtr, struct proxy *px) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8157 | { |
Willy Tarreau | 192252e | 2015-04-04 01:47:55 +0200 | [diff] [blame] | 8158 | struct session *sess = s->sess; |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 8159 | struct http_txn *txn = s->txn; |
Willy Tarreau | fdb563c | 2010-01-31 15:43:27 +0100 | [diff] [blame] | 8160 | struct hdr_exp *exp; |
| 8161 | |
| 8162 | for (exp = px->rsp_exp; exp; exp = exp->next) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8163 | int ret; |
| 8164 | |
| 8165 | /* |
| 8166 | * The interleaving of transformations and verdicts |
| 8167 | * makes it difficult to decide to continue or stop |
| 8168 | * the evaluation. |
| 8169 | */ |
| 8170 | |
Willy Tarreau | fdb563c | 2010-01-31 15:43:27 +0100 | [diff] [blame] | 8171 | if (txn->flags & TX_SVDENY) |
| 8172 | break; |
| 8173 | |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 8174 | if ((txn->flags & TX_SVALLOW) && |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8175 | (exp->action == ACT_ALLOW || exp->action == ACT_DENY || |
| 8176 | exp->action == ACT_PASS)) { |
| 8177 | exp = exp->next; |
| 8178 | continue; |
| 8179 | } |
| 8180 | |
Willy Tarreau | fdb563c | 2010-01-31 15:43:27 +0100 | [diff] [blame] | 8181 | /* if this filter had a condition, evaluate it now and skip to |
| 8182 | * next filter if the condition does not match. |
| 8183 | */ |
| 8184 | if (exp->cond) { |
Willy Tarreau | 192252e | 2015-04-04 01:47:55 +0200 | [diff] [blame] | 8185 | ret = acl_exec_cond(exp->cond, px, sess, s, SMP_OPT_DIR_RES|SMP_OPT_FINAL); |
Willy Tarreau | fdb563c | 2010-01-31 15:43:27 +0100 | [diff] [blame] | 8186 | ret = acl_pass(ret); |
| 8187 | if (((struct acl_cond *)exp->cond)->pol == ACL_COND_UNLESS) |
| 8188 | ret = !ret; |
| 8189 | if (!ret) |
| 8190 | continue; |
| 8191 | } |
| 8192 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8193 | /* Apply the filter to the status line. */ |
Willy Tarreau | fdb563c | 2010-01-31 15:43:27 +0100 | [diff] [blame] | 8194 | ret = apply_filter_to_sts_line(s, rtr, exp); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8195 | if (unlikely(ret < 0)) |
| 8196 | return -1; |
| 8197 | |
| 8198 | if (likely(ret == 0)) { |
| 8199 | /* The filter did not match the response, it can be |
| 8200 | * iterated through all headers. |
| 8201 | */ |
Sasha Pachev | c600204 | 2014-05-26 12:33:48 -0600 | [diff] [blame] | 8202 | if (unlikely(apply_filter_to_resp_headers(s, rtr, exp) < 0)) |
| 8203 | return -1; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8204 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8205 | } |
| 8206 | return 0; |
| 8207 | } |
| 8208 | |
| 8209 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8210 | /* |
Willy Tarreau | 396d2c6 | 2007-11-04 19:30:00 +0100 | [diff] [blame] | 8211 | * Manage server-side cookies. It can impact performance by about 2% so it is |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 8212 | * desirable to call it only when needed. This function is also used when we |
| 8213 | * just need to know if there is a cookie (eg: for check-cache). |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8214 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 8215 | void manage_server_side_cookies(struct stream *s, struct channel *res) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8216 | { |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 8217 | struct http_txn *txn = s->txn; |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 8218 | struct session *sess = s->sess; |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 8219 | struct server *srv; |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 8220 | int is_cookie2; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8221 | int cur_idx, old_idx, delta; |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 8222 | char *hdr_beg, *hdr_end, *hdr_next; |
| 8223 | char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8224 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8225 | /* Iterate through the headers. |
| 8226 | * we start with the start line. |
| 8227 | */ |
| 8228 | old_idx = 0; |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 8229 | hdr_next = res->buf->p + hdr_idx_first_pos(&txn->hdr_idx); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8230 | |
| 8231 | while ((cur_idx = txn->hdr_idx.v[old_idx].next)) { |
| 8232 | struct hdr_idx_elem *cur_hdr; |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 8233 | int val; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8234 | |
| 8235 | cur_hdr = &txn->hdr_idx.v[cur_idx]; |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 8236 | hdr_beg = hdr_next; |
| 8237 | hdr_end = hdr_beg + cur_hdr->len; |
| 8238 | hdr_next = hdr_end + cur_hdr->cr + 1; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8239 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 8240 | /* We have one full header between hdr_beg and hdr_end, and the |
| 8241 | * next header starts at hdr_next. We're only interested in |
| 8242 | * "Set-Cookie" and "Set-Cookie2" headers. |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8243 | */ |
| 8244 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 8245 | is_cookie2 = 0; |
| 8246 | prev = hdr_beg + 10; |
| 8247 | val = http_header_match2(hdr_beg, hdr_end, "Set-Cookie", 10); |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 8248 | if (!val) { |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 8249 | val = http_header_match2(hdr_beg, hdr_end, "Set-Cookie2", 11); |
| 8250 | if (!val) { |
| 8251 | old_idx = cur_idx; |
| 8252 | continue; |
| 8253 | } |
| 8254 | is_cookie2 = 1; |
| 8255 | prev = hdr_beg + 11; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8256 | } |
| 8257 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 8258 | /* OK, right now we know we have a Set-Cookie* at hdr_beg, and |
| 8259 | * <prev> points to the colon. |
| 8260 | */ |
Willy Tarreau | f134831 | 2010-10-07 15:54:11 +0200 | [diff] [blame] | 8261 | txn->flags |= TX_SCK_PRESENT; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8262 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 8263 | /* Maybe we only wanted to see if there was a Set-Cookie (eg: |
| 8264 | * check-cache is enabled) and we are not interested in checking |
| 8265 | * them. Warning, the cookie capture is declared in the frontend. |
Willy Tarreau | fd39dda | 2008-10-17 12:01:58 +0200 | [diff] [blame] | 8266 | */ |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 8267 | if (s->be->cookie_name == NULL && |
| 8268 | s->be->appsession_name == NULL && |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 8269 | sess->fe->capture_name == NULL) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8270 | return; |
| 8271 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 8272 | /* OK so now we know we have to process this response cookie. |
| 8273 | * The format of the Set-Cookie header is slightly different |
| 8274 | * from the format of the Cookie header in that it does not |
| 8275 | * support the comma as a cookie delimiter (thus the header |
| 8276 | * cannot be folded) because the Expires attribute described in |
| 8277 | * the original Netscape's spec may contain an unquoted date |
| 8278 | * with a comma inside. We have to live with this because |
| 8279 | * many browsers don't support Max-Age and some browsers don't |
| 8280 | * support quoted strings. However the Set-Cookie2 header is |
| 8281 | * clean. |
| 8282 | * |
| 8283 | * We have to keep multiple pointers in order to support cookie |
| 8284 | * removal at the beginning, middle or end of header without |
| 8285 | * corrupting the header (in case of set-cookie2). A special |
| 8286 | * pointer, <scav> points to the beginning of the set-cookie-av |
| 8287 | * fields after the first semi-colon. The <next> pointer points |
| 8288 | * either to the end of line (set-cookie) or next unquoted comma |
| 8289 | * (set-cookie2). All of these headers are valid : |
| 8290 | * |
| 8291 | * Set-Cookie: NAME1 = VALUE 1 ; Secure; Path="/"\r\n |
| 8292 | * Set-Cookie:NAME=VALUE; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT\r\n |
| 8293 | * Set-Cookie: NAME = VALUE ; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT\r\n |
| 8294 | * Set-Cookie2: NAME1 = VALUE 1 ; Max-Age=0, NAME2=VALUE2; Discard\r\n |
| 8295 | * | | | | | | | | | | |
| 8296 | * | | | | | | | | +-> next hdr_end <--+ |
| 8297 | * | | | | | | | +------------> scav |
| 8298 | * | | | | | | +--------------> val_end |
| 8299 | * | | | | | +--------------------> val_beg |
| 8300 | * | | | | +----------------------> equal |
| 8301 | * | | | +------------------------> att_end |
| 8302 | * | | +----------------------------> att_beg |
| 8303 | * | +------------------------------> prev |
| 8304 | * +-----------------------------------------> hdr_beg |
| 8305 | */ |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8306 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 8307 | for (; prev < hdr_end; prev = next) { |
| 8308 | /* Iterate through all cookies on this line */ |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8309 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 8310 | /* find att_beg */ |
| 8311 | att_beg = prev + 1; |
| 8312 | while (att_beg < hdr_end && http_is_spht[(unsigned char)*att_beg]) |
| 8313 | att_beg++; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8314 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 8315 | /* find att_end : this is the first character after the last non |
| 8316 | * space before the equal. It may be equal to hdr_end. |
| 8317 | */ |
| 8318 | equal = att_end = att_beg; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8319 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 8320 | while (equal < hdr_end) { |
| 8321 | if (*equal == '=' || *equal == ';' || (is_cookie2 && *equal == ',')) |
| 8322 | break; |
| 8323 | if (http_is_spht[(unsigned char)*equal++]) |
| 8324 | continue; |
| 8325 | att_end = equal; |
| 8326 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8327 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 8328 | /* here, <equal> points to '=', a delimitor or the end. <att_end> |
| 8329 | * is between <att_beg> and <equal>, both may be identical. |
| 8330 | */ |
| 8331 | |
| 8332 | /* look for end of cookie if there is an equal sign */ |
| 8333 | if (equal < hdr_end && *equal == '=') { |
| 8334 | /* look for the beginning of the value */ |
| 8335 | val_beg = equal + 1; |
| 8336 | while (val_beg < hdr_end && http_is_spht[(unsigned char)*val_beg]) |
| 8337 | val_beg++; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8338 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 8339 | /* find the end of the value, respecting quotes */ |
| 8340 | next = find_cookie_value_end(val_beg, hdr_end); |
| 8341 | |
| 8342 | /* make val_end point to the first white space or delimitor after the value */ |
| 8343 | val_end = next; |
| 8344 | while (val_end > val_beg && http_is_spht[(unsigned char)*(val_end - 1)]) |
| 8345 | val_end--; |
| 8346 | } else { |
| 8347 | /* <equal> points to next comma, semi-colon or EOL */ |
| 8348 | val_beg = val_end = next = equal; |
| 8349 | } |
| 8350 | |
| 8351 | if (next < hdr_end) { |
| 8352 | /* Set-Cookie2 supports multiple cookies, and <next> points to |
| 8353 | * a colon or semi-colon before the end. So skip all attr-value |
| 8354 | * pairs and look for the next comma. For Set-Cookie, since |
| 8355 | * commas are permitted in values, skip to the end. |
| 8356 | */ |
| 8357 | if (is_cookie2) |
| 8358 | next = find_hdr_value_end(next, hdr_end); |
| 8359 | else |
| 8360 | next = hdr_end; |
| 8361 | } |
| 8362 | |
| 8363 | /* Now everything is as on the diagram above */ |
| 8364 | |
| 8365 | /* Ignore cookies with no equal sign */ |
| 8366 | if (equal == val_end) |
| 8367 | continue; |
| 8368 | |
| 8369 | /* If there are spaces around the equal sign, we need to |
| 8370 | * strip them otherwise we'll get trouble for cookie captures, |
| 8371 | * or even for rewrites. Since this happens extremely rarely, |
| 8372 | * it does not hurt performance. |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8373 | */ |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 8374 | if (unlikely(att_end != equal || val_beg > equal + 1)) { |
| 8375 | int stripped_before = 0; |
| 8376 | int stripped_after = 0; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8377 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 8378 | if (att_end != equal) { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 8379 | stripped_before = buffer_replace2(res->buf, att_end, equal, NULL, 0); |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 8380 | equal += stripped_before; |
| 8381 | val_beg += stripped_before; |
| 8382 | } |
| 8383 | |
| 8384 | if (val_beg > equal + 1) { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 8385 | stripped_after = buffer_replace2(res->buf, equal + 1, val_beg, NULL, 0); |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 8386 | val_beg += stripped_after; |
| 8387 | stripped_before += stripped_after; |
| 8388 | } |
| 8389 | |
| 8390 | val_end += stripped_before; |
| 8391 | next += stripped_before; |
| 8392 | hdr_end += stripped_before; |
| 8393 | hdr_next += stripped_before; |
| 8394 | cur_hdr->len += stripped_before; |
Willy Tarreau | 1fc1f45 | 2011-04-07 22:35:37 +0200 | [diff] [blame] | 8395 | http_msg_move_end(&txn->rsp, stripped_before); |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 8396 | } |
| 8397 | |
| 8398 | /* First, let's see if we want to capture this cookie. We check |
| 8399 | * that we don't already have a server side cookie, because we |
| 8400 | * can only capture one. Also as an optimisation, we ignore |
| 8401 | * cookies shorter than the declared name. |
| 8402 | */ |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 8403 | if (sess->fe->capture_name != NULL && |
Willy Tarreau | 3bac9ff | 2007-03-18 17:31:28 +0100 | [diff] [blame] | 8404 | txn->srv_cookie == NULL && |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 8405 | (val_end - att_beg >= sess->fe->capture_namelen) && |
| 8406 | memcmp(att_beg, sess->fe->capture_name, sess->fe->capture_namelen) == 0) { |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 8407 | int log_len = val_end - att_beg; |
Willy Tarreau | 086b3b4 | 2007-05-13 21:45:51 +0200 | [diff] [blame] | 8408 | if ((txn->srv_cookie = pool_alloc2(pool2_capture)) == NULL) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8409 | Alert("HTTP logging : out of memory.\n"); |
| 8410 | } |
Willy Tarreau | f70fc75 | 2010-11-19 11:27:18 +0100 | [diff] [blame] | 8411 | else { |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 8412 | if (log_len > sess->fe->capture_len) |
| 8413 | log_len = sess->fe->capture_len; |
Willy Tarreau | f70fc75 | 2010-11-19 11:27:18 +0100 | [diff] [blame] | 8414 | memcpy(txn->srv_cookie, att_beg, log_len); |
| 8415 | txn->srv_cookie[log_len] = 0; |
| 8416 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8417 | } |
| 8418 | |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 8419 | srv = objt_server(s->target); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8420 | /* now check if we need to process it for persistence */ |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 8421 | if (!(s->flags & SF_IGNORE_PRST) && |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 8422 | (att_end - att_beg == s->be->cookie_len) && (s->be->cookie_name != NULL) && |
| 8423 | (memcmp(att_beg, s->be->cookie_name, att_end - att_beg) == 0)) { |
Willy Tarreau | f134831 | 2010-10-07 15:54:11 +0200 | [diff] [blame] | 8424 | /* assume passive cookie by default */ |
| 8425 | txn->flags &= ~TX_SCK_MASK; |
| 8426 | txn->flags |= TX_SCK_FOUND; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8427 | |
| 8428 | /* If the cookie is in insert mode on a known server, we'll delete |
| 8429 | * this occurrence because we'll insert another one later. |
| 8430 | * We'll delete it too if the "indirect" option is set and we're in |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 8431 | * a direct access. |
| 8432 | */ |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 8433 | if (s->be->ck_opts & PR_CK_PSV) { |
Willy Tarreau | ba4c5be | 2010-10-23 12:46:42 +0200 | [diff] [blame] | 8434 | /* The "preserve" flag was set, we don't want to touch the |
| 8435 | * server's cookie. |
| 8436 | */ |
| 8437 | } |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 8438 | else if ((srv && (s->be->ck_opts & PR_CK_INS)) || |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 8439 | ((s->flags & SF_DIRECT) && (s->be->ck_opts & PR_CK_IND))) { |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 8440 | /* this cookie must be deleted */ |
| 8441 | if (*prev == ':' && next == hdr_end) { |
| 8442 | /* whole header */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 8443 | delta = buffer_replace2(res->buf, hdr_beg, hdr_next, NULL, 0); |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 8444 | txn->hdr_idx.v[old_idx].next = cur_hdr->next; |
| 8445 | txn->hdr_idx.used--; |
| 8446 | cur_hdr->len = 0; |
Willy Tarreau | 26db59e | 2010-11-28 06:57:24 +0100 | [diff] [blame] | 8447 | cur_idx = old_idx; |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 8448 | hdr_next += delta; |
| 8449 | http_msg_move_end(&txn->rsp, delta); |
| 8450 | /* note: while both invalid now, <next> and <hdr_end> |
| 8451 | * are still equal, so the for() will stop as expected. |
| 8452 | */ |
| 8453 | } else { |
| 8454 | /* just remove the value */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 8455 | int delta = del_hdr_value(res->buf, &prev, next); |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 8456 | next = prev; |
| 8457 | hdr_end += delta; |
| 8458 | hdr_next += delta; |
| 8459 | cur_hdr->len += delta; |
| 8460 | http_msg_move_end(&txn->rsp, delta); |
| 8461 | } |
Willy Tarreau | f134831 | 2010-10-07 15:54:11 +0200 | [diff] [blame] | 8462 | txn->flags &= ~TX_SCK_MASK; |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 8463 | txn->flags |= TX_SCK_DELETED; |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 8464 | /* and go on with next cookie */ |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8465 | } |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 8466 | else if (srv && srv->cookie && (s->be->ck_opts & PR_CK_RW)) { |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 8467 | /* replace bytes val_beg->val_end with the cookie name associated |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8468 | * with this server since we know it. |
| 8469 | */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 8470 | delta = buffer_replace2(res->buf, val_beg, val_end, srv->cookie, srv->cklen); |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 8471 | next += delta; |
| 8472 | hdr_end += delta; |
| 8473 | hdr_next += delta; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8474 | cur_hdr->len += delta; |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 8475 | http_msg_move_end(&txn->rsp, delta); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8476 | |
Willy Tarreau | f134831 | 2010-10-07 15:54:11 +0200 | [diff] [blame] | 8477 | txn->flags &= ~TX_SCK_MASK; |
| 8478 | txn->flags |= TX_SCK_REPLACED; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8479 | } |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 8480 | else if (srv && srv->cookie && (s->be->ck_opts & PR_CK_PFX)) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8481 | /* insert the cookie name associated with this server |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 8482 | * before existing cookie, and insert a delimiter between them.. |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8483 | */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 8484 | delta = buffer_replace2(res->buf, val_beg, val_beg, srv->cookie, srv->cklen + 1); |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 8485 | next += delta; |
| 8486 | hdr_end += delta; |
| 8487 | hdr_next += delta; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8488 | cur_hdr->len += delta; |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 8489 | http_msg_move_end(&txn->rsp, delta); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8490 | |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 8491 | val_beg[srv->cklen] = COOKIE_DELIM; |
Willy Tarreau | f134831 | 2010-10-07 15:54:11 +0200 | [diff] [blame] | 8492 | txn->flags &= ~TX_SCK_MASK; |
| 8493 | txn->flags |= TX_SCK_REPLACED; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8494 | } |
| 8495 | } |
Cyril Bonté | 47fdd8e | 2010-04-25 00:00:51 +0200 | [diff] [blame] | 8496 | /* next, let's see if the cookie is our appcookie, unless persistence must be ignored */ |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 8497 | else if (!(s->flags & SF_IGNORE_PRST) && (s->be->appsession_name != NULL)) { |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 8498 | int cmp_len, value_len; |
| 8499 | char *value_begin; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8500 | |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 8501 | if (s->be->options2 & PR_O2_AS_PFX) { |
| 8502 | cmp_len = MIN(val_end - att_beg, s->be->appsession_name_len); |
| 8503 | value_begin = att_beg + s->be->appsession_name_len; |
| 8504 | value_len = MIN(s->be->appsession_len, val_end - att_beg - s->be->appsession_name_len); |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 8505 | } else { |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 8506 | cmp_len = att_end - att_beg; |
| 8507 | value_begin = val_beg; |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 8508 | value_len = MIN(s->be->appsession_len, val_end - val_beg); |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 8509 | } |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 8510 | |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 8511 | if ((cmp_len == s->be->appsession_name_len) && |
| 8512 | (memcmp(att_beg, s->be->appsession_name, s->be->appsession_name_len) == 0)) { |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 8513 | /* free a possibly previously allocated memory */ |
| 8514 | pool_free2(apools.sessid, txn->sessid); |
| 8515 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 8516 | /* Store the sessid in the stream for future use */ |
Willy Tarreau | a3377ee | 2010-01-10 10:49:11 +0100 | [diff] [blame] | 8517 | if ((txn->sessid = pool_alloc2(apools.sessid)) == NULL) { |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 8518 | Alert("Not enough Memory process_srv():asession->sessid:malloc().\n"); |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 8519 | send_log(s->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n"); |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 8520 | return; |
| 8521 | } |
Willy Tarreau | a3377ee | 2010-01-10 10:49:11 +0100 | [diff] [blame] | 8522 | memcpy(txn->sessid, value_begin, value_len); |
| 8523 | txn->sessid[value_len] = 0; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8524 | } |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 8525 | } |
| 8526 | /* that's done for this cookie, check the next one on the same |
| 8527 | * line when next != hdr_end (only if is_cookie2). |
| 8528 | */ |
| 8529 | } |
| 8530 | /* check next header */ |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8531 | old_idx = cur_idx; |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 8532 | } |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 8533 | |
Willy Tarreau | a3377ee | 2010-01-10 10:49:11 +0100 | [diff] [blame] | 8534 | if (txn->sessid != NULL) { |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 8535 | appsess *asession = NULL; |
| 8536 | /* only do insert, if lookup fails */ |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 8537 | asession = appsession_hash_lookup(&(s->be->htbl_proxy), txn->sessid); |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 8538 | if (asession == NULL) { |
Willy Tarreau | 1fac753 | 2010-01-09 19:23:06 +0100 | [diff] [blame] | 8539 | size_t server_id_len; |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 8540 | if ((asession = pool_alloc2(pool2_appsess)) == NULL) { |
| 8541 | Alert("Not enough Memory process_srv():asession:calloc().\n"); |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 8542 | send_log(s->be, LOG_ALERT, "Not enough Memory process_srv():asession:calloc().\n"); |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 8543 | return; |
| 8544 | } |
Willy Tarreau | 77eb9b8 | 2010-11-19 11:29:06 +0100 | [diff] [blame] | 8545 | asession->serverid = NULL; /* to avoid a double free in case of allocation error */ |
| 8546 | |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 8547 | if ((asession->sessid = pool_alloc2(apools.sessid)) == NULL) { |
| 8548 | Alert("Not enough Memory process_srv():asession->sessid:malloc().\n"); |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 8549 | send_log(s->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n"); |
| 8550 | s->be->htbl_proxy.destroy(asession); |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 8551 | return; |
| 8552 | } |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 8553 | memcpy(asession->sessid, txn->sessid, s->be->appsession_len); |
| 8554 | asession->sessid[s->be->appsession_len] = 0; |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 8555 | |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 8556 | server_id_len = strlen(objt_server(s->target)->id) + 1; |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 8557 | if ((asession->serverid = pool_alloc2(apools.serverid)) == NULL) { |
Willy Tarreau | 77eb9b8 | 2010-11-19 11:29:06 +0100 | [diff] [blame] | 8558 | Alert("Not enough Memory process_srv():asession->serverid:malloc().\n"); |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 8559 | send_log(s->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n"); |
| 8560 | s->be->htbl_proxy.destroy(asession); |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 8561 | return; |
| 8562 | } |
| 8563 | asession->serverid[0] = '\0'; |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 8564 | memcpy(asession->serverid, objt_server(s->target)->id, server_id_len); |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 8565 | |
| 8566 | asession->request_count = 0; |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 8567 | appsession_hash_insert(&(s->be->htbl_proxy), asession); |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 8568 | } |
| 8569 | |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 8570 | asession->expire = tick_add_ifset(now_ms, s->be->timeout.appsession); |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 8571 | asession->request_count++; |
| 8572 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8573 | } |
| 8574 | |
| 8575 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8576 | /* |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 8577 | * Check if response is cacheable or not. Updates s->flags. |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8578 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 8579 | void check_response_for_cacheability(struct stream *s, struct channel *rtr) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8580 | { |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 8581 | struct http_txn *txn = s->txn; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8582 | char *p1, *p2; |
| 8583 | |
| 8584 | char *cur_ptr, *cur_end, *cur_next; |
| 8585 | int cur_idx; |
| 8586 | |
Willy Tarreau | 5df5187 | 2007-11-25 16:20:08 +0100 | [diff] [blame] | 8587 | if (!(txn->flags & TX_CACHEABLE)) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8588 | return; |
| 8589 | |
| 8590 | /* Iterate through the headers. |
| 8591 | * we start with the start line. |
| 8592 | */ |
| 8593 | cur_idx = 0; |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 8594 | cur_next = rtr->buf->p + hdr_idx_first_pos(&txn->hdr_idx); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8595 | |
| 8596 | while ((cur_idx = txn->hdr_idx.v[cur_idx].next)) { |
| 8597 | struct hdr_idx_elem *cur_hdr; |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 8598 | int val; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8599 | |
| 8600 | cur_hdr = &txn->hdr_idx.v[cur_idx]; |
| 8601 | cur_ptr = cur_next; |
| 8602 | cur_end = cur_ptr + cur_hdr->len; |
| 8603 | cur_next = cur_end + cur_hdr->cr + 1; |
| 8604 | |
| 8605 | /* We have one full header between cur_ptr and cur_end, and the |
| 8606 | * next header starts at cur_next. We're only interested in |
| 8607 | * "Cookie:" headers. |
| 8608 | */ |
| 8609 | |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 8610 | val = http_header_match2(cur_ptr, cur_end, "Pragma", 6); |
| 8611 | if (val) { |
| 8612 | if ((cur_end - (cur_ptr + val) >= 8) && |
| 8613 | strncasecmp(cur_ptr + val, "no-cache", 8) == 0) { |
| 8614 | txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK; |
| 8615 | return; |
| 8616 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8617 | } |
| 8618 | |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 8619 | val = http_header_match2(cur_ptr, cur_end, "Cache-control", 13); |
| 8620 | if (!val) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8621 | continue; |
| 8622 | |
| 8623 | /* OK, right now we know we have a cache-control header at cur_ptr */ |
| 8624 | |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 8625 | p1 = cur_ptr + val; /* first non-space char after 'cache-control:' */ |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8626 | |
| 8627 | if (p1 >= cur_end) /* no more info */ |
| 8628 | continue; |
| 8629 | |
| 8630 | /* p1 is at the beginning of the value */ |
| 8631 | p2 = p1; |
| 8632 | |
Willy Tarreau | 8f8e645 | 2007-06-17 21:51:38 +0200 | [diff] [blame] | 8633 | while (p2 < cur_end && *p2 != '=' && *p2 != ',' && !isspace((unsigned char)*p2)) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8634 | p2++; |
| 8635 | |
| 8636 | /* we have a complete value between p1 and p2 */ |
| 8637 | if (p2 < cur_end && *p2 == '=') { |
| 8638 | /* we have something of the form no-cache="set-cookie" */ |
| 8639 | if ((cur_end - p1 >= 21) && |
| 8640 | strncasecmp(p1, "no-cache=\"set-cookie", 20) == 0 |
| 8641 | && (p1[20] == '"' || p1[20] == ',')) |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 8642 | txn->flags &= ~TX_CACHE_COOK; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8643 | continue; |
| 8644 | } |
| 8645 | |
| 8646 | /* OK, so we know that either p2 points to the end of string or to a comma */ |
| 8647 | if (((p2 - p1 == 7) && strncasecmp(p1, "private", 7) == 0) || |
Willy Tarreau | 5b15f90 | 2013-07-04 12:46:56 +0200 | [diff] [blame] | 8648 | ((p2 - p1 == 8) && strncasecmp(p1, "no-cache", 8) == 0) || |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8649 | ((p2 - p1 == 8) && strncasecmp(p1, "no-store", 8) == 0) || |
| 8650 | ((p2 - p1 == 9) && strncasecmp(p1, "max-age=0", 9) == 0) || |
| 8651 | ((p2 - p1 == 10) && strncasecmp(p1, "s-maxage=0", 10) == 0)) { |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 8652 | txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8653 | return; |
| 8654 | } |
| 8655 | |
| 8656 | if ((p2 - p1 == 6) && strncasecmp(p1, "public", 6) == 0) { |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 8657 | txn->flags |= TX_CACHEABLE | TX_CACHE_COOK; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8658 | continue; |
| 8659 | } |
| 8660 | } |
| 8661 | } |
| 8662 | |
| 8663 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 8664 | /* |
| 8665 | * Try to retrieve a known appsession in the URI, then the associated server. |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 8666 | * If the server is found, it's assigned to the stream. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 8667 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 8668 | void get_srv_from_appsession(struct stream *s, const char *begin, int len) |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 8669 | { |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 8670 | char *end_params, *first_param, *cur_param, *next_param; |
| 8671 | char separator; |
| 8672 | int value_len; |
| 8673 | |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 8674 | int mode = s->be->options2 & PR_O2_AS_M_ANY; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 8675 | |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 8676 | if (s->be->appsession_name == NULL || |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 8677 | (s->txn->meth != HTTP_METH_GET && s->txn->meth != HTTP_METH_POST && s->txn->meth != HTTP_METH_HEAD)) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 8678 | return; |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 8679 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 8680 | |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 8681 | first_param = NULL; |
| 8682 | switch (mode) { |
| 8683 | case PR_O2_AS_M_PP: |
| 8684 | first_param = memchr(begin, ';', len); |
| 8685 | break; |
| 8686 | case PR_O2_AS_M_QS: |
| 8687 | first_param = memchr(begin, '?', len); |
| 8688 | break; |
| 8689 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 8690 | |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 8691 | if (first_param == NULL) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 8692 | return; |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 8693 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 8694 | |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 8695 | switch (mode) { |
| 8696 | case PR_O2_AS_M_PP: |
| 8697 | if ((end_params = memchr(first_param, '?', len - (begin - first_param))) == NULL) { |
| 8698 | end_params = (char *) begin + len; |
| 8699 | } |
| 8700 | separator = ';'; |
| 8701 | break; |
| 8702 | case PR_O2_AS_M_QS: |
| 8703 | end_params = (char *) begin + len; |
| 8704 | separator = '&'; |
| 8705 | break; |
| 8706 | default: |
| 8707 | /* unknown mode, shouldn't happen */ |
| 8708 | return; |
| 8709 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 8710 | |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 8711 | cur_param = next_param = end_params; |
| 8712 | while (cur_param > first_param) { |
| 8713 | cur_param--; |
| 8714 | if ((cur_param[0] == separator) || (cur_param == first_param)) { |
| 8715 | /* let's see if this is the appsession parameter */ |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 8716 | if ((cur_param + s->be->appsession_name_len + 1 < next_param) && |
| 8717 | ((s->be->options2 & PR_O2_AS_PFX) || cur_param[s->be->appsession_name_len + 1] == '=') && |
| 8718 | (strncasecmp(cur_param + 1, s->be->appsession_name, s->be->appsession_name_len) == 0)) { |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 8719 | /* Cool... it's the right one */ |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 8720 | cur_param += s->be->appsession_name_len + (s->be->options2 & PR_O2_AS_PFX ? 1 : 2); |
| 8721 | value_len = MIN(s->be->appsession_len, next_param - cur_param); |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 8722 | if (value_len > 0) { |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 8723 | manage_client_side_appsession(s, cur_param, value_len); |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 8724 | } |
| 8725 | break; |
| 8726 | } |
| 8727 | next_param = cur_param; |
| 8728 | } |
| 8729 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 8730 | #if defined(DEBUG_HASH) |
Aleksandar Lazic | 697bbb0 | 2008-08-13 19:57:02 +0200 | [diff] [blame] | 8731 | Alert("get_srv_from_appsession\n"); |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 8732 | appsession_hash_dump(&(s->be->htbl_proxy)); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 8733 | #endif |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 8734 | } |
| 8735 | |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 8736 | /* |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 8737 | * In a GET, HEAD or POST request, check if the requested URI matches the stats uri |
Krzysztof Piotr Oledzki | 8c8bd45 | 2010-01-29 19:29:32 +0100 | [diff] [blame] | 8738 | * for the current backend. |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 8739 | * |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 8740 | * It is assumed that the request is either a HEAD, GET, or POST and that the |
Willy Tarreau | 295a837 | 2011-03-10 11:25:07 +0100 | [diff] [blame] | 8741 | * uri_auth field is valid. |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 8742 | * |
Krzysztof Piotr Oledzki | 8c8bd45 | 2010-01-29 19:29:32 +0100 | [diff] [blame] | 8743 | * Returns 1 if stats should be provided, otherwise 0. |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 8744 | */ |
Willy Tarreau | 295a837 | 2011-03-10 11:25:07 +0100 | [diff] [blame] | 8745 | int stats_check_uri(struct stream_interface *si, struct http_txn *txn, struct proxy *backend) |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 8746 | { |
| 8747 | struct uri_auth *uri_auth = backend->uri_auth; |
Willy Tarreau | 3a215be | 2012-03-09 21:39:51 +0100 | [diff] [blame] | 8748 | struct http_msg *msg = &txn->req; |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 8749 | const char *uri = msg->chn->buf->p+ msg->sl.rq.u; |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 8750 | |
Krzysztof Piotr Oledzki | 8c8bd45 | 2010-01-29 19:29:32 +0100 | [diff] [blame] | 8751 | if (!uri_auth) |
| 8752 | return 0; |
| 8753 | |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 8754 | if (txn->meth != HTTP_METH_GET && txn->meth != HTTP_METH_HEAD && txn->meth != HTTP_METH_POST) |
Krzysztof Piotr Oledzki | 8c8bd45 | 2010-01-29 19:29:32 +0100 | [diff] [blame] | 8755 | return 0; |
| 8756 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 8757 | /* check URI size */ |
Willy Tarreau | 3a215be | 2012-03-09 21:39:51 +0100 | [diff] [blame] | 8758 | if (uri_auth->uri_len > msg->sl.rq.u_l) |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 8759 | return 0; |
| 8760 | |
Willy Tarreau | 414e9bb | 2013-11-23 00:30:38 +0100 | [diff] [blame] | 8761 | if (memcmp(uri, uri_auth->uri_prefix, uri_auth->uri_len) != 0) |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 8762 | return 0; |
| 8763 | |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 8764 | return 1; |
| 8765 | } |
| 8766 | |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 8767 | /* |
| 8768 | * Capture a bad request or response and archive it in the proxy's structure. |
Willy Tarreau | 69d8c5d | 2012-05-08 09:44:41 +0200 | [diff] [blame] | 8769 | * By default it tries to report the error position as msg->err_pos. However if |
| 8770 | * this one is not set, it will then report msg->next, which is the last known |
| 8771 | * parsing point. The function is able to deal with wrapping buffers. It always |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 8772 | * displays buffers as a contiguous area starting at buf->p. |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 8773 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 8774 | void http_capture_bad_message(struct error_snapshot *es, struct stream *s, |
Willy Tarreau | 8a0cef2 | 2012-03-09 13:39:23 +0100 | [diff] [blame] | 8775 | struct http_msg *msg, |
Willy Tarreau | 3770f23 | 2013-12-07 00:01:53 +0100 | [diff] [blame] | 8776 | enum ht_state state, struct proxy *other_end) |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 8777 | { |
Willy Tarreau | 9ad7bd4 | 2015-04-03 19:19:59 +0200 | [diff] [blame] | 8778 | struct session *sess = strm_sess(s); |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 8779 | struct channel *chn = msg->chn; |
Willy Tarreau | 69d8c5d | 2012-05-08 09:44:41 +0200 | [diff] [blame] | 8780 | int len1, len2; |
Willy Tarreau | 8a0cef2 | 2012-03-09 13:39:23 +0100 | [diff] [blame] | 8781 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 8782 | es->len = MIN(chn->buf->i, sizeof(es->buf)); |
| 8783 | len1 = chn->buf->data + chn->buf->size - chn->buf->p; |
Willy Tarreau | 69d8c5d | 2012-05-08 09:44:41 +0200 | [diff] [blame] | 8784 | len1 = MIN(len1, es->len); |
| 8785 | len2 = es->len - len1; /* remaining data if buffer wraps */ |
| 8786 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 8787 | memcpy(es->buf, chn->buf->p, len1); |
Willy Tarreau | 69d8c5d | 2012-05-08 09:44:41 +0200 | [diff] [blame] | 8788 | if (len2) |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 8789 | memcpy(es->buf + len1, chn->buf->data, len2); |
Willy Tarreau | 81f2fb9 | 2010-12-12 13:09:08 +0100 | [diff] [blame] | 8790 | |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 8791 | if (msg->err_pos >= 0) |
Willy Tarreau | 69d8c5d | 2012-05-08 09:44:41 +0200 | [diff] [blame] | 8792 | es->pos = msg->err_pos; |
Willy Tarreau | 81f2fb9 | 2010-12-12 13:09:08 +0100 | [diff] [blame] | 8793 | else |
Willy Tarreau | 69d8c5d | 2012-05-08 09:44:41 +0200 | [diff] [blame] | 8794 | es->pos = msg->next; |
Willy Tarreau | 81f2fb9 | 2010-12-12 13:09:08 +0100 | [diff] [blame] | 8795 | |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 8796 | es->when = date; // user-visible date |
| 8797 | es->sid = s->uniq_id; |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 8798 | es->srv = objt_server(s->target); |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 8799 | es->oe = other_end; |
Willy Tarreau | 9ad7bd4 | 2015-04-03 19:19:59 +0200 | [diff] [blame] | 8800 | if (objt_conn(sess->origin)) |
| 8801 | es->src = __objt_conn(sess->origin)->addr.from; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 8802 | else |
| 8803 | memset(&es->src, 0, sizeof(es->src)); |
| 8804 | |
Willy Tarreau | 078272e | 2010-12-12 12:46:33 +0100 | [diff] [blame] | 8805 | es->state = state; |
Willy Tarreau | 10479e4 | 2010-12-12 14:00:34 +0100 | [diff] [blame] | 8806 | es->ev_id = error_snapshot_id++; |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 8807 | es->b_flags = chn->flags; |
Willy Tarreau | d04b1bc | 2012-05-08 11:03:10 +0200 | [diff] [blame] | 8808 | es->s_flags = s->flags; |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 8809 | es->t_flags = s->txn->flags; |
Willy Tarreau | d04b1bc | 2012-05-08 11:03:10 +0200 | [diff] [blame] | 8810 | es->m_flags = msg->flags; |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 8811 | es->b_out = chn->buf->o; |
| 8812 | es->b_wrap = chn->buf->data + chn->buf->size - chn->buf->p; |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 8813 | es->b_tot = chn->total; |
Willy Tarreau | d04b1bc | 2012-05-08 11:03:10 +0200 | [diff] [blame] | 8814 | es->m_clen = msg->chunk_len; |
| 8815 | es->m_blen = msg->body_len; |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 8816 | } |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 8817 | |
Willy Tarreau | 294c473 | 2011-12-16 21:35:50 +0100 | [diff] [blame] | 8818 | /* Return in <vptr> and <vlen> the pointer and length of occurrence <occ> of |
| 8819 | * header whose name is <hname> of length <hlen>. If <ctx> is null, lookup is |
| 8820 | * performed over the whole headers. Otherwise it must contain a valid header |
| 8821 | * context, initialised with ctx->idx=0 for the first lookup in a series. If |
| 8822 | * <occ> is positive or null, occurrence #occ from the beginning (or last ctx) |
| 8823 | * is returned. Occ #0 and #1 are equivalent. If <occ> is negative (and no less |
| 8824 | * than -MAX_HDR_HISTORY), the occurrence is counted from the last one which is |
Willy Tarreau | 04ff9f1 | 2013-06-10 18:39:42 +0200 | [diff] [blame] | 8825 | * -1. The value fetch stops at commas, so this function is suited for use with |
| 8826 | * list headers. |
Willy Tarreau | 294c473 | 2011-12-16 21:35:50 +0100 | [diff] [blame] | 8827 | * The return value is 0 if nothing was found, or non-zero otherwise. |
Willy Tarreau | bce7088 | 2009-09-07 11:51:47 +0200 | [diff] [blame] | 8828 | */ |
Willy Tarreau | 185b5c4 | 2012-04-26 15:11:51 +0200 | [diff] [blame] | 8829 | unsigned int http_get_hdr(const struct http_msg *msg, const char *hname, int hlen, |
Willy Tarreau | 294c473 | 2011-12-16 21:35:50 +0100 | [diff] [blame] | 8830 | struct hdr_idx *idx, int occ, |
| 8831 | struct hdr_ctx *ctx, char **vptr, int *vlen) |
Willy Tarreau | bce7088 | 2009-09-07 11:51:47 +0200 | [diff] [blame] | 8832 | { |
Willy Tarreau | 294c473 | 2011-12-16 21:35:50 +0100 | [diff] [blame] | 8833 | struct hdr_ctx local_ctx; |
| 8834 | char *ptr_hist[MAX_HDR_HISTORY]; |
| 8835 | int len_hist[MAX_HDR_HISTORY]; |
Willy Tarreau | bce7088 | 2009-09-07 11:51:47 +0200 | [diff] [blame] | 8836 | unsigned int hist_ptr; |
Willy Tarreau | 294c473 | 2011-12-16 21:35:50 +0100 | [diff] [blame] | 8837 | int found; |
Willy Tarreau | bce7088 | 2009-09-07 11:51:47 +0200 | [diff] [blame] | 8838 | |
Willy Tarreau | 294c473 | 2011-12-16 21:35:50 +0100 | [diff] [blame] | 8839 | if (!ctx) { |
| 8840 | local_ctx.idx = 0; |
| 8841 | ctx = &local_ctx; |
| 8842 | } |
| 8843 | |
Willy Tarreau | bce7088 | 2009-09-07 11:51:47 +0200 | [diff] [blame] | 8844 | if (occ >= 0) { |
Willy Tarreau | 294c473 | 2011-12-16 21:35:50 +0100 | [diff] [blame] | 8845 | /* search from the beginning */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 8846 | while (http_find_header2(hname, hlen, msg->chn->buf->p, idx, ctx)) { |
Willy Tarreau | bce7088 | 2009-09-07 11:51:47 +0200 | [diff] [blame] | 8847 | occ--; |
| 8848 | if (occ <= 0) { |
Willy Tarreau | 294c473 | 2011-12-16 21:35:50 +0100 | [diff] [blame] | 8849 | *vptr = ctx->line + ctx->val; |
| 8850 | *vlen = ctx->vlen; |
| 8851 | return 1; |
Willy Tarreau | bce7088 | 2009-09-07 11:51:47 +0200 | [diff] [blame] | 8852 | } |
| 8853 | } |
Willy Tarreau | 294c473 | 2011-12-16 21:35:50 +0100 | [diff] [blame] | 8854 | return 0; |
Willy Tarreau | bce7088 | 2009-09-07 11:51:47 +0200 | [diff] [blame] | 8855 | } |
| 8856 | |
| 8857 | /* negative occurrence, we scan all the list then walk back */ |
| 8858 | if (-occ > MAX_HDR_HISTORY) |
| 8859 | return 0; |
| 8860 | |
Willy Tarreau | 294c473 | 2011-12-16 21:35:50 +0100 | [diff] [blame] | 8861 | found = hist_ptr = 0; |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 8862 | while (http_find_header2(hname, hlen, msg->chn->buf->p, idx, ctx)) { |
Willy Tarreau | 294c473 | 2011-12-16 21:35:50 +0100 | [diff] [blame] | 8863 | ptr_hist[hist_ptr] = ctx->line + ctx->val; |
| 8864 | len_hist[hist_ptr] = ctx->vlen; |
| 8865 | if (++hist_ptr >= MAX_HDR_HISTORY) |
Willy Tarreau | bce7088 | 2009-09-07 11:51:47 +0200 | [diff] [blame] | 8866 | hist_ptr = 0; |
| 8867 | found++; |
| 8868 | } |
| 8869 | if (-occ > found) |
| 8870 | return 0; |
| 8871 | /* OK now we have the last occurrence in [hist_ptr-1], and we need to |
Willy Tarreau | 67dad27 | 2013-06-12 22:27:44 +0200 | [diff] [blame] | 8872 | * find occurrence -occ. 0 <= hist_ptr < MAX_HDR_HISTORY, and we have |
| 8873 | * -10 <= occ <= -1. So we have to check [hist_ptr%MAX_HDR_HISTORY+occ] |
| 8874 | * to remain in the 0..9 range. |
Willy Tarreau | bce7088 | 2009-09-07 11:51:47 +0200 | [diff] [blame] | 8875 | */ |
Willy Tarreau | 67dad27 | 2013-06-12 22:27:44 +0200 | [diff] [blame] | 8876 | hist_ptr += occ + MAX_HDR_HISTORY; |
Willy Tarreau | bce7088 | 2009-09-07 11:51:47 +0200 | [diff] [blame] | 8877 | if (hist_ptr >= MAX_HDR_HISTORY) |
| 8878 | hist_ptr -= MAX_HDR_HISTORY; |
Willy Tarreau | 294c473 | 2011-12-16 21:35:50 +0100 | [diff] [blame] | 8879 | *vptr = ptr_hist[hist_ptr]; |
| 8880 | *vlen = len_hist[hist_ptr]; |
| 8881 | return 1; |
Willy Tarreau | bce7088 | 2009-09-07 11:51:47 +0200 | [diff] [blame] | 8882 | } |
| 8883 | |
Willy Tarreau | 04ff9f1 | 2013-06-10 18:39:42 +0200 | [diff] [blame] | 8884 | /* Return in <vptr> and <vlen> the pointer and length of occurrence <occ> of |
| 8885 | * header whose name is <hname> of length <hlen>. If <ctx> is null, lookup is |
| 8886 | * performed over the whole headers. Otherwise it must contain a valid header |
| 8887 | * context, initialised with ctx->idx=0 for the first lookup in a series. If |
| 8888 | * <occ> is positive or null, occurrence #occ from the beginning (or last ctx) |
| 8889 | * is returned. Occ #0 and #1 are equivalent. If <occ> is negative (and no less |
| 8890 | * than -MAX_HDR_HISTORY), the occurrence is counted from the last one which is |
| 8891 | * -1. This function differs from http_get_hdr() in that it only returns full |
| 8892 | * line header values and does not stop at commas. |
| 8893 | * The return value is 0 if nothing was found, or non-zero otherwise. |
| 8894 | */ |
| 8895 | unsigned int http_get_fhdr(const struct http_msg *msg, const char *hname, int hlen, |
| 8896 | struct hdr_idx *idx, int occ, |
| 8897 | struct hdr_ctx *ctx, char **vptr, int *vlen) |
| 8898 | { |
| 8899 | struct hdr_ctx local_ctx; |
| 8900 | char *ptr_hist[MAX_HDR_HISTORY]; |
| 8901 | int len_hist[MAX_HDR_HISTORY]; |
| 8902 | unsigned int hist_ptr; |
| 8903 | int found; |
| 8904 | |
| 8905 | if (!ctx) { |
| 8906 | local_ctx.idx = 0; |
| 8907 | ctx = &local_ctx; |
| 8908 | } |
| 8909 | |
| 8910 | if (occ >= 0) { |
| 8911 | /* search from the beginning */ |
| 8912 | while (http_find_full_header2(hname, hlen, msg->chn->buf->p, idx, ctx)) { |
| 8913 | occ--; |
| 8914 | if (occ <= 0) { |
| 8915 | *vptr = ctx->line + ctx->val; |
| 8916 | *vlen = ctx->vlen; |
| 8917 | return 1; |
| 8918 | } |
| 8919 | } |
| 8920 | return 0; |
| 8921 | } |
| 8922 | |
| 8923 | /* negative occurrence, we scan all the list then walk back */ |
| 8924 | if (-occ > MAX_HDR_HISTORY) |
| 8925 | return 0; |
| 8926 | |
| 8927 | found = hist_ptr = 0; |
| 8928 | while (http_find_full_header2(hname, hlen, msg->chn->buf->p, idx, ctx)) { |
| 8929 | ptr_hist[hist_ptr] = ctx->line + ctx->val; |
| 8930 | len_hist[hist_ptr] = ctx->vlen; |
| 8931 | if (++hist_ptr >= MAX_HDR_HISTORY) |
| 8932 | hist_ptr = 0; |
| 8933 | found++; |
| 8934 | } |
| 8935 | if (-occ > found) |
| 8936 | return 0; |
| 8937 | /* OK now we have the last occurrence in [hist_ptr-1], and we need to |
| 8938 | * find occurrence -occ, so we have to check [hist_ptr+occ]. |
| 8939 | */ |
| 8940 | hist_ptr += occ; |
| 8941 | if (hist_ptr >= MAX_HDR_HISTORY) |
| 8942 | hist_ptr -= MAX_HDR_HISTORY; |
| 8943 | *vptr = ptr_hist[hist_ptr]; |
| 8944 | *vlen = len_hist[hist_ptr]; |
| 8945 | return 1; |
| 8946 | } |
| 8947 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 8948 | /* |
Willy Tarreau | e92693a | 2012-09-24 21:13:39 +0200 | [diff] [blame] | 8949 | * Print a debug line with a header. Always stop at the first CR or LF char, |
| 8950 | * so it is safe to pass it a full buffer if needed. If <err> is not NULL, an |
| 8951 | * arrow is printed after the line which contains the pointer. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 8952 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 8953 | void debug_hdr(const char *dir, struct stream *s, const char *start, const char *end) |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 8954 | { |
Willy Tarreau | 9ad7bd4 | 2015-04-03 19:19:59 +0200 | [diff] [blame] | 8955 | struct session *sess = strm_sess(s); |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 8956 | int max; |
Willy Tarreau | 9ad7bd4 | 2015-04-03 19:19:59 +0200 | [diff] [blame] | 8957 | |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 8958 | chunk_printf(&trash, "%08x:%s.%s[%04x:%04x]: ", s->uniq_id, s->be->id, |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 8959 | dir, |
Willy Tarreau | 9ad7bd4 | 2015-04-03 19:19:59 +0200 | [diff] [blame] | 8960 | objt_conn(sess->origin) ? (unsigned short)objt_conn(sess->origin)->t.sock.fd : -1, |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 8961 | objt_conn(s->si[1].end) ? (unsigned short)objt_conn(s->si[1].end)->t.sock.fd : -1); |
Willy Tarreau | e92693a | 2012-09-24 21:13:39 +0200 | [diff] [blame] | 8962 | |
| 8963 | for (max = 0; start + max < end; max++) |
| 8964 | if (start[max] == '\r' || start[max] == '\n') |
| 8965 | break; |
| 8966 | |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 8967 | UBOUND(max, trash.size - trash.len - 3); |
| 8968 | trash.len += strlcpy2(trash.str + trash.len, start, max + 1); |
| 8969 | trash.str[trash.len++] = '\n'; |
Willy Tarreau | 89efaed | 2013-12-13 15:14:55 +0100 | [diff] [blame] | 8970 | shut_your_big_mouth_gcc(write(1, trash.str, trash.len)); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 8971 | } |
| 8972 | |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 8973 | |
| 8974 | /* Allocate a new HTTP transaction for stream <s> unless there is one already. |
| 8975 | * The hdr_idx is allocated as well. In case of allocation failure, everything |
| 8976 | * allocated is freed and NULL is returned. Otherwise the new transaction is |
| 8977 | * assigned to the stream and returned. |
| 8978 | */ |
| 8979 | struct http_txn *http_alloc_txn(struct stream *s) |
| 8980 | { |
| 8981 | struct http_txn *txn = s->txn; |
| 8982 | |
| 8983 | if (txn) |
| 8984 | return txn; |
| 8985 | |
| 8986 | txn = pool_alloc2(pool2_http_txn); |
| 8987 | if (!txn) |
| 8988 | return txn; |
| 8989 | |
| 8990 | txn->hdr_idx.size = global.tune.max_http_hdr; |
| 8991 | txn->hdr_idx.v = pool_alloc2(pool2_hdr_idx); |
| 8992 | if (!txn->hdr_idx.v) { |
| 8993 | pool_free2(pool2_http_txn, txn); |
| 8994 | return NULL; |
| 8995 | } |
| 8996 | |
| 8997 | s->txn = txn; |
| 8998 | return txn; |
| 8999 | } |
| 9000 | |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 9001 | /* |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 9002 | * Initialize a new HTTP transaction for stream <s>. It is assumed that all |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 9003 | * the required fields are properly allocated and that we only need to (re)init |
| 9004 | * them. This should be used before processing any new request. |
| 9005 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 9006 | void http_init_txn(struct stream *s) |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 9007 | { |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 9008 | struct http_txn *txn = s->txn; |
Willy Tarreau | d0d8da9 | 2015-04-04 02:10:38 +0200 | [diff] [blame] | 9009 | struct proxy *fe = strm_fe(s); |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 9010 | |
| 9011 | txn->flags = 0; |
| 9012 | txn->status = -1; |
| 9013 | |
Willy Tarreau | f64d141 | 2010-10-07 20:06:11 +0200 | [diff] [blame] | 9014 | txn->cookie_first_date = 0; |
| 9015 | txn->cookie_last_date = 0; |
| 9016 | |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 9017 | txn->sessid = NULL; |
| 9018 | txn->srv_cookie = NULL; |
| 9019 | txn->cli_cookie = NULL; |
| 9020 | txn->uri = NULL; |
| 9021 | |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 9022 | txn->req.flags = 0; |
Willy Tarreau | 2692736 | 2012-05-18 23:22:52 +0200 | [diff] [blame] | 9023 | txn->req.sol = txn->req.eol = txn->req.eoh = 0; /* relative to the buffer */ |
Willy Tarreau | a458b67 | 2012-03-05 11:17:50 +0100 | [diff] [blame] | 9024 | txn->req.next = 0; |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 9025 | txn->rsp.flags = 0; |
Willy Tarreau | 2692736 | 2012-05-18 23:22:52 +0200 | [diff] [blame] | 9026 | txn->rsp.sol = txn->rsp.eol = txn->rsp.eoh = 0; /* relative to the buffer */ |
Willy Tarreau | a458b67 | 2012-03-05 11:17:50 +0100 | [diff] [blame] | 9027 | txn->rsp.next = 0; |
Willy Tarreau | 124d991 | 2011-03-01 20:30:48 +0100 | [diff] [blame] | 9028 | txn->req.chunk_len = 0LL; |
| 9029 | txn->req.body_len = 0LL; |
| 9030 | txn->rsp.chunk_len = 0LL; |
| 9031 | txn->rsp.body_len = 0LL; |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 9032 | txn->req.msg_state = HTTP_MSG_RQBEFORE; /* at the very beginning of the request */ |
| 9033 | txn->rsp.msg_state = HTTP_MSG_RPBEFORE; /* at the very beginning of the response */ |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 9034 | txn->req.chn = &s->req; |
| 9035 | txn->rsp.chn = &s->res; |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 9036 | |
| 9037 | txn->auth.method = HTTP_AUTH_UNKNOWN; |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 9038 | |
| 9039 | txn->req.err_pos = txn->rsp.err_pos = -2; /* block buggy requests/responses */ |
| 9040 | if (fe->options2 & PR_O2_REQBUG_OK) |
| 9041 | txn->req.err_pos = -1; /* let buggy requests pass */ |
| 9042 | |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 9043 | if (txn->hdr_idx.v) |
| 9044 | hdr_idx_init(&txn->hdr_idx); |
Thierry FOURNIER | 4834bc7 | 2015-06-06 19:29:07 +0200 | [diff] [blame] | 9045 | |
| 9046 | vars_init(&s->vars_txn, SCOPE_TXN); |
| 9047 | vars_init(&s->vars_reqres, SCOPE_REQ); |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 9048 | } |
| 9049 | |
| 9050 | /* to be used at the end of a transaction */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 9051 | void http_end_txn(struct stream *s) |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 9052 | { |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 9053 | struct http_txn *txn = s->txn; |
Willy Tarreau | d0d8da9 | 2015-04-04 02:10:38 +0200 | [diff] [blame] | 9054 | struct proxy *fe = strm_fe(s); |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 9055 | |
Willy Tarreau | 7519560 | 2014-03-11 15:48:55 +0100 | [diff] [blame] | 9056 | /* release any possible compression context */ |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 9057 | if (s->flags & SF_COMP_READY) |
Willy Tarreau | 7519560 | 2014-03-11 15:48:55 +0100 | [diff] [blame] | 9058 | s->comp_algo->end(&s->comp_ctx); |
| 9059 | s->comp_algo = NULL; |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 9060 | s->flags &= ~SF_COMP_READY; |
Willy Tarreau | 7519560 | 2014-03-11 15:48:55 +0100 | [diff] [blame] | 9061 | |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 9062 | /* these ones will have been dynamically allocated */ |
| 9063 | pool_free2(pool2_requri, txn->uri); |
| 9064 | pool_free2(pool2_capture, txn->cli_cookie); |
| 9065 | pool_free2(pool2_capture, txn->srv_cookie); |
Willy Tarreau | a3377ee | 2010-01-10 10:49:11 +0100 | [diff] [blame] | 9066 | pool_free2(apools.sessid, txn->sessid); |
William Lallemand | a73203e | 2012-03-12 12:48:57 +0100 | [diff] [blame] | 9067 | pool_free2(pool2_uniqueid, s->unique_id); |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 9068 | |
William Lallemand | a73203e | 2012-03-12 12:48:57 +0100 | [diff] [blame] | 9069 | s->unique_id = NULL; |
Willy Tarreau | a3377ee | 2010-01-10 10:49:11 +0100 | [diff] [blame] | 9070 | txn->sessid = NULL; |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 9071 | txn->uri = NULL; |
| 9072 | txn->srv_cookie = NULL; |
| 9073 | txn->cli_cookie = NULL; |
Willy Tarreau | 4602363 | 2010-01-07 22:51:47 +0100 | [diff] [blame] | 9074 | |
Willy Tarreau | cb7dd01 | 2015-04-03 22:16:32 +0200 | [diff] [blame] | 9075 | if (s->req_cap) { |
Willy Tarreau | 4602363 | 2010-01-07 22:51:47 +0100 | [diff] [blame] | 9076 | struct cap_hdr *h; |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 9077 | for (h = fe->req_cap; h; h = h->next) |
Willy Tarreau | cb7dd01 | 2015-04-03 22:16:32 +0200 | [diff] [blame] | 9078 | pool_free2(h->pool, s->req_cap[h->index]); |
| 9079 | memset(s->req_cap, 0, fe->nb_req_cap * sizeof(void *)); |
Willy Tarreau | 4602363 | 2010-01-07 22:51:47 +0100 | [diff] [blame] | 9080 | } |
| 9081 | |
Willy Tarreau | cb7dd01 | 2015-04-03 22:16:32 +0200 | [diff] [blame] | 9082 | if (s->res_cap) { |
Willy Tarreau | 4602363 | 2010-01-07 22:51:47 +0100 | [diff] [blame] | 9083 | struct cap_hdr *h; |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 9084 | for (h = fe->rsp_cap; h; h = h->next) |
Willy Tarreau | cb7dd01 | 2015-04-03 22:16:32 +0200 | [diff] [blame] | 9085 | pool_free2(h->pool, s->res_cap[h->index]); |
| 9086 | memset(s->res_cap, 0, fe->nb_rsp_cap * sizeof(void *)); |
Willy Tarreau | 4602363 | 2010-01-07 22:51:47 +0100 | [diff] [blame] | 9087 | } |
| 9088 | |
Thierry FOURNIER | 4834bc7 | 2015-06-06 19:29:07 +0200 | [diff] [blame] | 9089 | vars_prune(&s->vars_txn, s); |
| 9090 | vars_prune(&s->vars_reqres, s); |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 9091 | } |
| 9092 | |
| 9093 | /* to be used at the end of a transaction to prepare a new one */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 9094 | void http_reset_txn(struct stream *s) |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 9095 | { |
| 9096 | http_end_txn(s); |
| 9097 | http_init_txn(s); |
| 9098 | |
Thierry FOURNIER | bc4c1ac | 2015-02-25 13:36:14 +0100 | [diff] [blame] | 9099 | /* reinitialise the current rule list pointer to NULL. We are sure that |
| 9100 | * any rulelist match the NULL pointer. |
| 9101 | */ |
| 9102 | s->current_rule_list = NULL; |
| 9103 | |
Willy Tarreau | d0d8da9 | 2015-04-04 02:10:38 +0200 | [diff] [blame] | 9104 | s->be = strm_fe(s); |
| 9105 | s->logs.logwait = strm_fe(s)->to_log; |
Willy Tarreau | abcd514 | 2013-06-11 17:18:02 +0200 | [diff] [blame] | 9106 | s->logs.level = 0; |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 9107 | stream_del_srv_conn(s); |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 9108 | s->target = NULL; |
Emeric Brun | b982a3d | 2010-01-04 15:45:53 +0100 | [diff] [blame] | 9109 | /* re-init store persistence */ |
| 9110 | s->store_count = 0; |
Willy Tarreau | 1f0da24 | 2014-01-25 11:01:50 +0100 | [diff] [blame] | 9111 | s->uniq_id = global.req_count++; |
Emeric Brun | b982a3d | 2010-01-04 15:45:53 +0100 | [diff] [blame] | 9112 | |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 9113 | s->pend_pos = NULL; |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 9114 | |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 9115 | s->req.flags |= CF_READ_DONTWAIT; /* one read is usually enough */ |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 9116 | |
Willy Tarreau | 739cfba | 2010-01-25 23:11:14 +0100 | [diff] [blame] | 9117 | /* We must trim any excess data from the response buffer, because we |
| 9118 | * may have blocked an invalid response from a server that we don't |
| 9119 | * want to accidentely forward once we disable the analysers, nor do |
| 9120 | * we want those data to come along with next response. A typical |
| 9121 | * example of such data would be from a buggy server responding to |
| 9122 | * a HEAD with some data, or sending more than the advertised |
| 9123 | * content-length. |
| 9124 | */ |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 9125 | if (unlikely(s->res.buf->i)) |
| 9126 | s->res.buf->i = 0; |
Willy Tarreau | 739cfba | 2010-01-25 23:11:14 +0100 | [diff] [blame] | 9127 | |
Willy Tarreau | d0d8da9 | 2015-04-04 02:10:38 +0200 | [diff] [blame] | 9128 | s->req.rto = strm_fe(s)->timeout.client; |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 9129 | s->req.wto = TICK_ETERNITY; |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 9130 | |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 9131 | s->res.rto = TICK_ETERNITY; |
Willy Tarreau | d0d8da9 | 2015-04-04 02:10:38 +0200 | [diff] [blame] | 9132 | s->res.wto = strm_fe(s)->timeout.client; |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 9133 | |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 9134 | s->req.rex = TICK_ETERNITY; |
| 9135 | s->req.wex = TICK_ETERNITY; |
| 9136 | s->req.analyse_exp = TICK_ETERNITY; |
| 9137 | s->res.rex = TICK_ETERNITY; |
| 9138 | s->res.wex = TICK_ETERNITY; |
| 9139 | s->res.analyse_exp = TICK_ETERNITY; |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 9140 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 9141 | |
Sasha Pachev | 218f064 | 2014-06-16 12:05:59 -0600 | [diff] [blame] | 9142 | void free_http_res_rules(struct list *r) |
| 9143 | { |
| 9144 | struct http_res_rule *tr, *pr; |
| 9145 | |
| 9146 | list_for_each_entry_safe(pr, tr, r, list) { |
| 9147 | LIST_DEL(&pr->list); |
Thierry FOURNIER | 09af0d6 | 2014-06-18 11:35:54 +0200 | [diff] [blame] | 9148 | regex_free(&pr->arg.hdr_add.re); |
Sasha Pachev | 218f064 | 2014-06-16 12:05:59 -0600 | [diff] [blame] | 9149 | free(pr); |
| 9150 | } |
| 9151 | } |
| 9152 | |
| 9153 | void free_http_req_rules(struct list *r) |
| 9154 | { |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 9155 | struct http_req_rule *tr, *pr; |
| 9156 | |
| 9157 | list_for_each_entry_safe(pr, tr, r, list) { |
| 9158 | LIST_DEL(&pr->list); |
Willy Tarreau | 20b0de5 | 2012-12-24 15:45:22 +0100 | [diff] [blame] | 9159 | if (pr->action == HTTP_REQ_ACT_AUTH) |
Willy Tarreau | 5c2e198 | 2012-12-24 12:00:25 +0100 | [diff] [blame] | 9160 | free(pr->arg.auth.realm); |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 9161 | |
Thierry FOURNIER | 09af0d6 | 2014-06-18 11:35:54 +0200 | [diff] [blame] | 9162 | regex_free(&pr->arg.hdr_add.re); |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 9163 | free(pr); |
| 9164 | } |
| 9165 | } |
| 9166 | |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 9167 | /* parse an "http-request" rule */ |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 9168 | struct http_req_rule *parse_http_req_cond(const char **args, const char *file, int linenum, struct proxy *proxy) |
| 9169 | { |
| 9170 | struct http_req_rule *rule; |
William Lallemand | 73025dd | 2014-04-24 14:38:37 +0200 | [diff] [blame] | 9171 | struct http_req_action_kw *custom = NULL; |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 9172 | int cur_arg; |
Thierry FOURNIER | 09af0d6 | 2014-06-18 11:35:54 +0200 | [diff] [blame] | 9173 | char *error; |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 9174 | |
| 9175 | rule = (struct http_req_rule*)calloc(1, sizeof(struct http_req_rule)); |
| 9176 | if (!rule) { |
| 9177 | Alert("parsing [%s:%d]: out of memory.\n", file, linenum); |
Willy Tarreau | 81499eb | 2012-12-27 12:19:02 +0100 | [diff] [blame] | 9178 | goto out_err; |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 9179 | } |
| 9180 | |
CJ Ess | 108b1dd | 2015-04-07 12:03:37 -0400 | [diff] [blame] | 9181 | rule->deny_status = HTTP_ERR_403; |
Willy Tarreau | 5c2e198 | 2012-12-24 12:00:25 +0100 | [diff] [blame] | 9182 | if (!strcmp(args[0], "allow")) { |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 9183 | rule->action = HTTP_REQ_ACT_ALLOW; |
| 9184 | cur_arg = 1; |
Willy Tarreau | 5bd6759 | 2014-04-28 22:00:46 +0200 | [diff] [blame] | 9185 | } else if (!strcmp(args[0], "deny") || !strcmp(args[0], "block")) { |
CJ Ess | 108b1dd | 2015-04-07 12:03:37 -0400 | [diff] [blame] | 9186 | int code; |
| 9187 | int hc; |
| 9188 | |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 9189 | rule->action = HTTP_REQ_ACT_DENY; |
| 9190 | cur_arg = 1; |
CJ Ess | 108b1dd | 2015-04-07 12:03:37 -0400 | [diff] [blame] | 9191 | if (strcmp(args[cur_arg], "deny_status") == 0) { |
| 9192 | cur_arg++; |
| 9193 | if (!args[cur_arg]) { |
| 9194 | Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : missing status code.\n", |
| 9195 | file, linenum, proxy_type_str(proxy), proxy->id, args[0]); |
| 9196 | goto out_err; |
| 9197 | } |
| 9198 | |
| 9199 | code = atol(args[cur_arg]); |
| 9200 | cur_arg++; |
| 9201 | for (hc = 0; hc < HTTP_ERR_SIZE; hc++) { |
| 9202 | if (http_err_codes[hc] == code) { |
| 9203 | rule->deny_status = hc; |
| 9204 | break; |
| 9205 | } |
| 9206 | } |
| 9207 | |
| 9208 | if (hc >= HTTP_ERR_SIZE) { |
| 9209 | Warning("parsing [%s:%d] : status code %d not handled, using default code 403.\n", |
| 9210 | file, linenum, code); |
| 9211 | } |
| 9212 | } |
Willy Tarreau | ccbcc37 | 2012-12-27 12:37:57 +0100 | [diff] [blame] | 9213 | } else if (!strcmp(args[0], "tarpit")) { |
| 9214 | rule->action = HTTP_REQ_ACT_TARPIT; |
| 9215 | cur_arg = 1; |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 9216 | } else if (!strcmp(args[0], "auth")) { |
Willy Tarreau | 20b0de5 | 2012-12-24 15:45:22 +0100 | [diff] [blame] | 9217 | rule->action = HTTP_REQ_ACT_AUTH; |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 9218 | cur_arg = 1; |
| 9219 | |
| 9220 | while(*args[cur_arg]) { |
| 9221 | if (!strcmp(args[cur_arg], "realm")) { |
Willy Tarreau | 5c2e198 | 2012-12-24 12:00:25 +0100 | [diff] [blame] | 9222 | rule->arg.auth.realm = strdup(args[cur_arg + 1]); |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 9223 | cur_arg+=2; |
| 9224 | continue; |
| 9225 | } else |
| 9226 | break; |
| 9227 | } |
Willy Tarreau | f4c43c1 | 2013-06-11 17:01:13 +0200 | [diff] [blame] | 9228 | } else if (!strcmp(args[0], "set-nice")) { |
| 9229 | rule->action = HTTP_REQ_ACT_SET_NICE; |
| 9230 | cur_arg = 1; |
| 9231 | |
| 9232 | if (!*args[cur_arg] || |
| 9233 | (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) { |
| 9234 | Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (integer value).\n", |
| 9235 | file, linenum, args[0]); |
| 9236 | goto out_err; |
| 9237 | } |
| 9238 | rule->arg.nice = atoi(args[cur_arg]); |
| 9239 | if (rule->arg.nice < -1024) |
| 9240 | rule->arg.nice = -1024; |
| 9241 | else if (rule->arg.nice > 1024) |
| 9242 | rule->arg.nice = 1024; |
| 9243 | cur_arg++; |
Willy Tarreau | 42cf39e | 2013-06-11 18:51:32 +0200 | [diff] [blame] | 9244 | } else if (!strcmp(args[0], "set-tos")) { |
| 9245 | #ifdef IP_TOS |
| 9246 | char *err; |
| 9247 | rule->action = HTTP_REQ_ACT_SET_TOS; |
| 9248 | cur_arg = 1; |
| 9249 | |
| 9250 | if (!*args[cur_arg] || |
| 9251 | (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) { |
| 9252 | Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (integer/hex value).\n", |
| 9253 | file, linenum, args[0]); |
| 9254 | goto out_err; |
| 9255 | } |
| 9256 | |
| 9257 | rule->arg.tos = strtol(args[cur_arg], &err, 0); |
| 9258 | if (err && *err != '\0') { |
| 9259 | Alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-request %s' (integer/hex value expected).\n", |
| 9260 | file, linenum, err, args[0]); |
| 9261 | goto out_err; |
| 9262 | } |
| 9263 | cur_arg++; |
| 9264 | #else |
| 9265 | Alert("parsing [%s:%d]: 'http-request %s' is not supported on this platform (IP_TOS undefined).\n", file, linenum, args[0]); |
| 9266 | goto out_err; |
| 9267 | #endif |
Willy Tarreau | 51347ed | 2013-06-11 19:34:13 +0200 | [diff] [blame] | 9268 | } else if (!strcmp(args[0], "set-mark")) { |
| 9269 | #ifdef SO_MARK |
| 9270 | char *err; |
| 9271 | rule->action = HTTP_REQ_ACT_SET_MARK; |
| 9272 | cur_arg = 1; |
| 9273 | |
| 9274 | if (!*args[cur_arg] || |
| 9275 | (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) { |
| 9276 | Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (integer/hex value).\n", |
| 9277 | file, linenum, args[0]); |
| 9278 | goto out_err; |
| 9279 | } |
| 9280 | |
| 9281 | rule->arg.mark = strtoul(args[cur_arg], &err, 0); |
| 9282 | if (err && *err != '\0') { |
| 9283 | Alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-request %s' (integer/hex value expected).\n", |
| 9284 | file, linenum, err, args[0]); |
| 9285 | goto out_err; |
| 9286 | } |
| 9287 | cur_arg++; |
| 9288 | global.last_checks |= LSTCHK_NETADM; |
| 9289 | #else |
| 9290 | Alert("parsing [%s:%d]: 'http-request %s' is not supported on this platform (SO_MARK undefined).\n", file, linenum, args[0]); |
| 9291 | goto out_err; |
| 9292 | #endif |
Willy Tarreau | 9a355ec | 2013-06-11 17:45:46 +0200 | [diff] [blame] | 9293 | } else if (!strcmp(args[0], "set-log-level")) { |
| 9294 | rule->action = HTTP_REQ_ACT_SET_LOGL; |
| 9295 | cur_arg = 1; |
| 9296 | |
| 9297 | if (!*args[cur_arg] || |
| 9298 | (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) { |
| 9299 | bad_log_level: |
| 9300 | Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (log level name or 'silent').\n", |
| 9301 | file, linenum, args[0]); |
| 9302 | goto out_err; |
| 9303 | } |
| 9304 | if (strcmp(args[cur_arg], "silent") == 0) |
| 9305 | rule->arg.loglevel = -1; |
| 9306 | else if ((rule->arg.loglevel = get_log_level(args[cur_arg]) + 1) == 0) |
| 9307 | goto bad_log_level; |
| 9308 | cur_arg++; |
Willy Tarreau | 20b0de5 | 2012-12-24 15:45:22 +0100 | [diff] [blame] | 9309 | } else if (strcmp(args[0], "add-header") == 0 || strcmp(args[0], "set-header") == 0) { |
| 9310 | rule->action = *args[0] == 'a' ? HTTP_REQ_ACT_ADD_HDR : HTTP_REQ_ACT_SET_HDR; |
| 9311 | cur_arg = 1; |
| 9312 | |
Willy Tarreau | 8d1c516 | 2013-04-03 14:13:58 +0200 | [diff] [blame] | 9313 | if (!*args[cur_arg] || !*args[cur_arg+1] || |
| 9314 | (*args[cur_arg+2] && strcmp(args[cur_arg+2], "if") != 0 && strcmp(args[cur_arg+2], "unless") != 0)) { |
Willy Tarreau | 20b0de5 | 2012-12-24 15:45:22 +0100 | [diff] [blame] | 9315 | Alert("parsing [%s:%d]: 'http-request %s' expects exactly 2 arguments.\n", |
| 9316 | file, linenum, args[0]); |
Willy Tarreau | 81499eb | 2012-12-27 12:19:02 +0100 | [diff] [blame] | 9317 | goto out_err; |
Willy Tarreau | 20b0de5 | 2012-12-24 15:45:22 +0100 | [diff] [blame] | 9318 | } |
| 9319 | |
| 9320 | rule->arg.hdr_add.name = strdup(args[cur_arg]); |
| 9321 | rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name); |
| 9322 | LIST_INIT(&rule->arg.hdr_add.fmt); |
Willy Tarreau | a4312fa | 2013-04-02 16:34:32 +0200 | [diff] [blame] | 9323 | |
Thierry FOURNIER | 1c0054f | 2013-11-20 15:09:52 +0100 | [diff] [blame] | 9324 | proxy->conf.args.ctx = ARGC_HRQ; |
Thierry FOURNIER | d048d8b | 2014-03-13 16:46:18 +0100 | [diff] [blame] | 9325 | parse_logformat_string(args[cur_arg + 1], proxy, &rule->arg.hdr_add.fmt, LOG_OPT_HTTP, |
Thierry FOURNIER | eeaa951 | 2014-02-11 14:00:19 +0100 | [diff] [blame] | 9326 | (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR, |
| 9327 | file, linenum); |
Willy Tarreau | 59ad1a2 | 2014-01-29 14:39:58 +0100 | [diff] [blame] | 9328 | free(proxy->conf.lfs_file); |
| 9329 | proxy->conf.lfs_file = strdup(proxy->conf.args.file); |
| 9330 | proxy->conf.lfs_line = proxy->conf.args.line; |
Willy Tarreau | 20b0de5 | 2012-12-24 15:45:22 +0100 | [diff] [blame] | 9331 | cur_arg += 2; |
Willy Tarreau | b854392 | 2014-06-17 18:58:26 +0200 | [diff] [blame] | 9332 | } else if (strcmp(args[0], "replace-header") == 0 || strcmp(args[0], "replace-value") == 0) { |
| 9333 | rule->action = args[0][8] == 'h' ? HTTP_REQ_ACT_REPLACE_HDR : HTTP_REQ_ACT_REPLACE_VAL; |
Sasha Pachev | 218f064 | 2014-06-16 12:05:59 -0600 | [diff] [blame] | 9334 | cur_arg = 1; |
| 9335 | |
| 9336 | if (!*args[cur_arg] || !*args[cur_arg+1] || !*args[cur_arg+2] || |
Baptiste Assmann | 92df370 | 2014-06-24 11:10:00 +0200 | [diff] [blame] | 9337 | (*args[cur_arg+3] && strcmp(args[cur_arg+3], "if") != 0 && strcmp(args[cur_arg+3], "unless") != 0)) { |
Sasha Pachev | 218f064 | 2014-06-16 12:05:59 -0600 | [diff] [blame] | 9338 | Alert("parsing [%s:%d]: 'http-request %s' expects exactly 3 arguments.\n", |
| 9339 | file, linenum, args[0]); |
| 9340 | goto out_err; |
| 9341 | } |
| 9342 | |
| 9343 | rule->arg.hdr_add.name = strdup(args[cur_arg]); |
| 9344 | rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name); |
| 9345 | LIST_INIT(&rule->arg.hdr_add.fmt); |
| 9346 | |
Thierry FOURNIER | 09af0d6 | 2014-06-18 11:35:54 +0200 | [diff] [blame] | 9347 | error = NULL; |
| 9348 | if (!regex_comp(args[cur_arg + 1], &rule->arg.hdr_add.re, 1, 1, &error)) { |
| 9349 | Alert("parsing [%s:%d] : '%s' : %s.\n", file, linenum, |
| 9350 | args[cur_arg + 1], error); |
| 9351 | free(error); |
Sasha Pachev | 218f064 | 2014-06-16 12:05:59 -0600 | [diff] [blame] | 9352 | goto out_err; |
| 9353 | } |
| 9354 | |
| 9355 | proxy->conf.args.ctx = ARGC_HRQ; |
| 9356 | parse_logformat_string(args[cur_arg + 2], proxy, &rule->arg.hdr_add.fmt, LOG_OPT_HTTP, |
| 9357 | (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR, |
| 9358 | file, linenum); |
| 9359 | |
| 9360 | free(proxy->conf.lfs_file); |
| 9361 | proxy->conf.lfs_file = strdup(proxy->conf.args.file); |
| 9362 | proxy->conf.lfs_line = proxy->conf.args.line; |
| 9363 | cur_arg += 3; |
Thierry FOURNIER | dad3d1d | 2014-04-22 18:07:25 +0200 | [diff] [blame] | 9364 | } else if (strcmp(args[0], "del-header") == 0) { |
| 9365 | rule->action = HTTP_REQ_ACT_DEL_HDR; |
| 9366 | cur_arg = 1; |
| 9367 | |
| 9368 | if (!*args[cur_arg] || |
| 9369 | (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) { |
| 9370 | Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument.\n", |
| 9371 | file, linenum, args[0]); |
| 9372 | goto out_err; |
| 9373 | } |
| 9374 | |
| 9375 | rule->arg.hdr_add.name = strdup(args[cur_arg]); |
| 9376 | rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name); |
| 9377 | |
| 9378 | proxy->conf.args.ctx = ARGC_HRQ; |
| 9379 | free(proxy->conf.lfs_file); |
| 9380 | proxy->conf.lfs_file = strdup(proxy->conf.args.file); |
| 9381 | proxy->conf.lfs_line = proxy->conf.args.line; |
| 9382 | cur_arg += 1; |
Willy Tarreau | 09448f7 | 2014-06-25 18:12:15 +0200 | [diff] [blame] | 9383 | } else if (strncmp(args[0], "track-sc", 8) == 0 && |
| 9384 | args[0][9] == '\0' && args[0][8] >= '0' && |
Willy Tarreau | e1cfc1f | 2014-10-17 11:53:05 +0200 | [diff] [blame] | 9385 | args[0][8] < '0' + MAX_SESS_STKCTR) { /* track-sc 0..9 */ |
Willy Tarreau | 09448f7 | 2014-06-25 18:12:15 +0200 | [diff] [blame] | 9386 | struct sample_expr *expr; |
| 9387 | unsigned int where; |
| 9388 | char *err = NULL; |
| 9389 | |
| 9390 | cur_arg = 1; |
| 9391 | proxy->conf.args.ctx = ARGC_TRK; |
| 9392 | |
| 9393 | expr = sample_parse_expr((char **)args, &cur_arg, file, linenum, &err, &proxy->conf.args); |
| 9394 | if (!expr) { |
| 9395 | Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : %s.\n", |
| 9396 | file, linenum, proxy_type_str(proxy), proxy->id, args[0], err); |
| 9397 | free(err); |
| 9398 | goto out_err; |
| 9399 | } |
| 9400 | |
| 9401 | where = 0; |
| 9402 | if (proxy->cap & PR_CAP_FE) |
| 9403 | where |= SMP_VAL_FE_HRQ_HDR; |
| 9404 | if (proxy->cap & PR_CAP_BE) |
| 9405 | where |= SMP_VAL_BE_HRQ_HDR; |
| 9406 | |
| 9407 | if (!(expr->fetch->val & where)) { |
| 9408 | Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule :" |
| 9409 | " fetch method '%s' extracts information from '%s', none of which is available here.\n", |
| 9410 | file, linenum, proxy_type_str(proxy), proxy->id, args[0], |
| 9411 | args[cur_arg-1], sample_src_names(expr->fetch->use)); |
| 9412 | free(expr); |
| 9413 | goto out_err; |
| 9414 | } |
| 9415 | |
| 9416 | if (strcmp(args[cur_arg], "table") == 0) { |
| 9417 | cur_arg++; |
| 9418 | if (!args[cur_arg]) { |
| 9419 | Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : missing table name.\n", |
| 9420 | file, linenum, proxy_type_str(proxy), proxy->id, args[0]); |
| 9421 | free(expr); |
| 9422 | goto out_err; |
| 9423 | } |
| 9424 | /* we copy the table name for now, it will be resolved later */ |
| 9425 | rule->act_prm.trk_ctr.table.n = strdup(args[cur_arg]); |
| 9426 | cur_arg++; |
| 9427 | } |
| 9428 | rule->act_prm.trk_ctr.expr = expr; |
| 9429 | rule->action = HTTP_REQ_ACT_TRK_SC0 + args[0][8] - '0'; |
Willy Tarreau | 81499eb | 2012-12-27 12:19:02 +0100 | [diff] [blame] | 9430 | } else if (strcmp(args[0], "redirect") == 0) { |
| 9431 | struct redirect_rule *redir; |
Willy Tarreau | 6d4890c | 2013-11-18 18:04:25 +0100 | [diff] [blame] | 9432 | char *errmsg = NULL; |
Willy Tarreau | 81499eb | 2012-12-27 12:19:02 +0100 | [diff] [blame] | 9433 | |
Willy Tarreau | be4653b | 2015-05-28 15:26:58 +0200 | [diff] [blame] | 9434 | if ((redir = http_parse_redirect_rule(file, linenum, proxy, (const char **)args + 1, &errmsg, 1, 0)) == NULL) { |
Willy Tarreau | 81499eb | 2012-12-27 12:19:02 +0100 | [diff] [blame] | 9435 | Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : %s.\n", |
| 9436 | file, linenum, proxy_type_str(proxy), proxy->id, args[0], errmsg); |
| 9437 | goto out_err; |
| 9438 | } |
| 9439 | |
| 9440 | /* this redirect rule might already contain a parsed condition which |
| 9441 | * we'll pass to the http-request rule. |
| 9442 | */ |
| 9443 | rule->action = HTTP_REQ_ACT_REDIR; |
| 9444 | rule->arg.redir = redir; |
| 9445 | rule->cond = redir->cond; |
| 9446 | redir->cond = NULL; |
| 9447 | cur_arg = 2; |
| 9448 | return rule; |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 9449 | } else if (strncmp(args[0], "add-acl", 7) == 0) { |
| 9450 | /* http-request add-acl(<reference (acl name)>) <key pattern> */ |
| 9451 | rule->action = HTTP_REQ_ACT_ADD_ACL; |
| 9452 | /* |
| 9453 | * '+ 8' for 'add-acl(' |
| 9454 | * '- 9' for 'add-acl(' + trailing ')' |
| 9455 | */ |
Willy Tarreau | 6c09c2c | 2014-04-25 21:38:08 +0200 | [diff] [blame] | 9456 | rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9); |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 9457 | |
| 9458 | cur_arg = 1; |
| 9459 | |
| 9460 | if (!*args[cur_arg] || |
| 9461 | (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) { |
| 9462 | Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument.\n", |
| 9463 | file, linenum, args[0]); |
| 9464 | goto out_err; |
| 9465 | } |
| 9466 | |
| 9467 | LIST_INIT(&rule->arg.map.key); |
| 9468 | proxy->conf.args.ctx = ARGC_HRQ; |
| 9469 | parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP, |
| 9470 | (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR, |
| 9471 | file, linenum); |
| 9472 | free(proxy->conf.lfs_file); |
| 9473 | proxy->conf.lfs_file = strdup(proxy->conf.args.file); |
| 9474 | proxy->conf.lfs_line = proxy->conf.args.line; |
| 9475 | cur_arg += 1; |
| 9476 | } else if (strncmp(args[0], "del-acl", 7) == 0) { |
| 9477 | /* http-request del-acl(<reference (acl name)>) <key pattern> */ |
| 9478 | rule->action = HTTP_REQ_ACT_DEL_ACL; |
| 9479 | /* |
| 9480 | * '+ 8' for 'del-acl(' |
| 9481 | * '- 9' for 'del-acl(' + trailing ')' |
| 9482 | */ |
Willy Tarreau | 6c09c2c | 2014-04-25 21:38:08 +0200 | [diff] [blame] | 9483 | rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9); |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 9484 | |
| 9485 | cur_arg = 1; |
| 9486 | |
| 9487 | if (!*args[cur_arg] || |
| 9488 | (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) { |
| 9489 | Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument.\n", |
| 9490 | file, linenum, args[0]); |
| 9491 | goto out_err; |
| 9492 | } |
| 9493 | |
| 9494 | LIST_INIT(&rule->arg.map.key); |
| 9495 | proxy->conf.args.ctx = ARGC_HRQ; |
| 9496 | parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP, |
| 9497 | (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR, |
| 9498 | file, linenum); |
| 9499 | free(proxy->conf.lfs_file); |
| 9500 | proxy->conf.lfs_file = strdup(proxy->conf.args.file); |
| 9501 | proxy->conf.lfs_line = proxy->conf.args.line; |
| 9502 | cur_arg += 1; |
| 9503 | } else if (strncmp(args[0], "del-map", 7) == 0) { |
| 9504 | /* http-request del-map(<reference (map name)>) <key pattern> */ |
| 9505 | rule->action = HTTP_REQ_ACT_DEL_MAP; |
| 9506 | /* |
| 9507 | * '+ 8' for 'del-map(' |
| 9508 | * '- 9' for 'del-map(' + trailing ')' |
| 9509 | */ |
Willy Tarreau | 6c09c2c | 2014-04-25 21:38:08 +0200 | [diff] [blame] | 9510 | rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9); |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 9511 | |
| 9512 | cur_arg = 1; |
| 9513 | |
| 9514 | if (!*args[cur_arg] || |
| 9515 | (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) { |
| 9516 | Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument.\n", |
| 9517 | file, linenum, args[0]); |
| 9518 | goto out_err; |
| 9519 | } |
| 9520 | |
| 9521 | LIST_INIT(&rule->arg.map.key); |
| 9522 | proxy->conf.args.ctx = ARGC_HRQ; |
| 9523 | parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP, |
| 9524 | (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR, |
| 9525 | file, linenum); |
| 9526 | free(proxy->conf.lfs_file); |
| 9527 | proxy->conf.lfs_file = strdup(proxy->conf.args.file); |
| 9528 | proxy->conf.lfs_line = proxy->conf.args.line; |
| 9529 | cur_arg += 1; |
| 9530 | } else if (strncmp(args[0], "set-map", 7) == 0) { |
| 9531 | /* http-request set-map(<reference (map name)>) <key pattern> <value pattern> */ |
| 9532 | rule->action = HTTP_REQ_ACT_SET_MAP; |
| 9533 | /* |
| 9534 | * '+ 8' for 'set-map(' |
| 9535 | * '- 9' for 'set-map(' + trailing ')' |
| 9536 | */ |
Willy Tarreau | 6c09c2c | 2014-04-25 21:38:08 +0200 | [diff] [blame] | 9537 | rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9); |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 9538 | |
| 9539 | cur_arg = 1; |
| 9540 | |
| 9541 | if (!*args[cur_arg] || !*args[cur_arg+1] || |
| 9542 | (*args[cur_arg+2] && strcmp(args[cur_arg+2], "if") != 0 && strcmp(args[cur_arg+2], "unless") != 0)) { |
| 9543 | Alert("parsing [%s:%d]: 'http-request %s' expects exactly 2 arguments.\n", |
| 9544 | file, linenum, args[0]); |
| 9545 | goto out_err; |
| 9546 | } |
| 9547 | |
| 9548 | LIST_INIT(&rule->arg.map.key); |
| 9549 | LIST_INIT(&rule->arg.map.value); |
| 9550 | proxy->conf.args.ctx = ARGC_HRQ; |
| 9551 | |
| 9552 | /* key pattern */ |
| 9553 | parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP, |
| 9554 | (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR, |
| 9555 | file, linenum); |
| 9556 | |
| 9557 | /* value pattern */ |
| 9558 | parse_logformat_string(args[cur_arg + 1], proxy, &rule->arg.map.value, LOG_OPT_HTTP, |
| 9559 | (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR, |
| 9560 | file, linenum); |
| 9561 | free(proxy->conf.lfs_file); |
| 9562 | proxy->conf.lfs_file = strdup(proxy->conf.args.file); |
| 9563 | proxy->conf.lfs_line = proxy->conf.args.line; |
| 9564 | |
| 9565 | cur_arg += 2; |
Adis Nezirovic | 2fbcafc | 2015-07-06 15:44:30 +0200 | [diff] [blame] | 9566 | } else if (strncmp(args[0], "set-src", 7) == 0) { |
| 9567 | struct sample_expr *expr; |
| 9568 | unsigned int where; |
| 9569 | char *err = NULL; |
| 9570 | |
| 9571 | cur_arg = 1; |
| 9572 | proxy->conf.args.ctx = ARGC_HRQ; |
| 9573 | |
| 9574 | expr = sample_parse_expr((char **)args, &cur_arg, file, linenum, &err, &proxy->conf.args); |
| 9575 | if (!expr) { |
| 9576 | Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : %s.\n", |
| 9577 | file, linenum, proxy_type_str(proxy), proxy->id, args[0], err); |
| 9578 | free(err); |
| 9579 | goto out_err; |
| 9580 | } |
| 9581 | |
| 9582 | where = 0; |
| 9583 | if (proxy->cap & PR_CAP_FE) |
| 9584 | where |= SMP_VAL_FE_HRQ_HDR; |
| 9585 | if (proxy->cap & PR_CAP_BE) |
| 9586 | where |= SMP_VAL_BE_HRQ_HDR; |
| 9587 | |
| 9588 | if (!(expr->fetch->val & where)) { |
| 9589 | Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule :" |
| 9590 | " fetch method '%s' extracts information from '%s', none of which is available here.\n", |
| 9591 | file, linenum, proxy_type_str(proxy), proxy->id, args[0], |
| 9592 | args[cur_arg-1], sample_src_names(expr->fetch->use)); |
| 9593 | free(expr); |
| 9594 | goto out_err; |
| 9595 | } |
| 9596 | |
| 9597 | rule->arg.act.p[0] = expr; |
| 9598 | rule->action = HTTP_REQ_ACT_SET_SRC; |
William Lallemand | 73025dd | 2014-04-24 14:38:37 +0200 | [diff] [blame] | 9599 | } else if (((custom = action_http_req_custom(args[0])) != NULL)) { |
| 9600 | char *errmsg = NULL; |
| 9601 | cur_arg = 1; |
| 9602 | /* try in the module list */ |
| 9603 | if (custom->parse(args, &cur_arg, proxy, rule, &errmsg) < 0) { |
| 9604 | Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : %s.\n", |
| 9605 | file, linenum, proxy_type_str(proxy), proxy->id, args[0], errmsg); |
| 9606 | free(errmsg); |
| 9607 | goto out_err; |
| 9608 | } |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 9609 | } else { |
Adis Nezirovic | 2fbcafc | 2015-07-06 15:44:30 +0200 | [diff] [blame] | 9610 | Alert("parsing [%s:%d]: 'http-request' expects 'allow', 'deny', 'auth', 'redirect', 'tarpit', 'add-header', 'set-header', 'replace-header', 'replace-value', 'set-nice', 'set-tos', 'set-mark', 'set-log-level', 'add-acl', 'del-acl', 'del-map', 'set-map', 'set-var', 'set-src', but got '%s'%s.\n", |
Willy Tarreau | 5c2e198 | 2012-12-24 12:00:25 +0100 | [diff] [blame] | 9611 | file, linenum, args[0], *args[0] ? "" : " (missing argument)"); |
Willy Tarreau | 81499eb | 2012-12-27 12:19:02 +0100 | [diff] [blame] | 9612 | goto out_err; |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 9613 | } |
| 9614 | |
| 9615 | if (strcmp(args[cur_arg], "if") == 0 || strcmp(args[cur_arg], "unless") == 0) { |
| 9616 | struct acl_cond *cond; |
Willy Tarreau | b7451bb | 2012-04-27 12:38:15 +0200 | [diff] [blame] | 9617 | char *errmsg = NULL; |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 9618 | |
Willy Tarreau | b7451bb | 2012-04-27 12:38:15 +0200 | [diff] [blame] | 9619 | if ((cond = build_acl_cond(file, linenum, proxy, args+cur_arg, &errmsg)) == NULL) { |
| 9620 | Alert("parsing [%s:%d] : error detected while parsing an 'http-request %s' condition : %s.\n", |
| 9621 | file, linenum, args[0], errmsg); |
| 9622 | free(errmsg); |
Willy Tarreau | 81499eb | 2012-12-27 12:19:02 +0100 | [diff] [blame] | 9623 | goto out_err; |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 9624 | } |
| 9625 | rule->cond = cond; |
| 9626 | } |
| 9627 | else if (*args[cur_arg]) { |
| 9628 | Alert("parsing [%s:%d]: 'http-request %s' expects 'realm' for 'auth' or" |
| 9629 | " either 'if' or 'unless' followed by a condition but found '%s'.\n", |
| 9630 | file, linenum, args[0], args[cur_arg]); |
Willy Tarreau | 81499eb | 2012-12-27 12:19:02 +0100 | [diff] [blame] | 9631 | goto out_err; |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 9632 | } |
| 9633 | |
| 9634 | return rule; |
Willy Tarreau | 81499eb | 2012-12-27 12:19:02 +0100 | [diff] [blame] | 9635 | out_err: |
| 9636 | free(rule); |
| 9637 | return NULL; |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 9638 | } |
| 9639 | |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 9640 | /* parse an "http-respose" rule */ |
| 9641 | struct http_res_rule *parse_http_res_cond(const char **args, const char *file, int linenum, struct proxy *proxy) |
| 9642 | { |
| 9643 | struct http_res_rule *rule; |
William Lallemand | 73025dd | 2014-04-24 14:38:37 +0200 | [diff] [blame] | 9644 | struct http_res_action_kw *custom = NULL; |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 9645 | int cur_arg; |
Thierry FOURNIER | 09af0d6 | 2014-06-18 11:35:54 +0200 | [diff] [blame] | 9646 | char *error; |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 9647 | |
| 9648 | rule = calloc(1, sizeof(*rule)); |
| 9649 | if (!rule) { |
| 9650 | Alert("parsing [%s:%d]: out of memory.\n", file, linenum); |
| 9651 | goto out_err; |
| 9652 | } |
| 9653 | |
| 9654 | if (!strcmp(args[0], "allow")) { |
| 9655 | rule->action = HTTP_RES_ACT_ALLOW; |
| 9656 | cur_arg = 1; |
| 9657 | } else if (!strcmp(args[0], "deny")) { |
| 9658 | rule->action = HTTP_RES_ACT_DENY; |
| 9659 | cur_arg = 1; |
Willy Tarreau | f4c43c1 | 2013-06-11 17:01:13 +0200 | [diff] [blame] | 9660 | } else if (!strcmp(args[0], "set-nice")) { |
| 9661 | rule->action = HTTP_RES_ACT_SET_NICE; |
| 9662 | cur_arg = 1; |
| 9663 | |
| 9664 | if (!*args[cur_arg] || |
| 9665 | (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) { |
| 9666 | Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (integer value).\n", |
| 9667 | file, linenum, args[0]); |
| 9668 | goto out_err; |
| 9669 | } |
| 9670 | rule->arg.nice = atoi(args[cur_arg]); |
| 9671 | if (rule->arg.nice < -1024) |
| 9672 | rule->arg.nice = -1024; |
| 9673 | else if (rule->arg.nice > 1024) |
| 9674 | rule->arg.nice = 1024; |
| 9675 | cur_arg++; |
Willy Tarreau | 42cf39e | 2013-06-11 18:51:32 +0200 | [diff] [blame] | 9676 | } else if (!strcmp(args[0], "set-tos")) { |
| 9677 | #ifdef IP_TOS |
| 9678 | char *err; |
| 9679 | rule->action = HTTP_RES_ACT_SET_TOS; |
| 9680 | cur_arg = 1; |
| 9681 | |
| 9682 | if (!*args[cur_arg] || |
| 9683 | (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) { |
| 9684 | Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (integer/hex value).\n", |
| 9685 | file, linenum, args[0]); |
| 9686 | goto out_err; |
| 9687 | } |
| 9688 | |
| 9689 | rule->arg.tos = strtol(args[cur_arg], &err, 0); |
| 9690 | if (err && *err != '\0') { |
| 9691 | Alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-response %s' (integer/hex value expected).\n", |
| 9692 | file, linenum, err, args[0]); |
| 9693 | goto out_err; |
| 9694 | } |
| 9695 | cur_arg++; |
| 9696 | #else |
| 9697 | Alert("parsing [%s:%d]: 'http-response %s' is not supported on this platform (IP_TOS undefined).\n", file, linenum, args[0]); |
| 9698 | goto out_err; |
| 9699 | #endif |
Willy Tarreau | 51347ed | 2013-06-11 19:34:13 +0200 | [diff] [blame] | 9700 | } else if (!strcmp(args[0], "set-mark")) { |
| 9701 | #ifdef SO_MARK |
| 9702 | char *err; |
| 9703 | rule->action = HTTP_RES_ACT_SET_MARK; |
| 9704 | cur_arg = 1; |
| 9705 | |
| 9706 | if (!*args[cur_arg] || |
| 9707 | (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) { |
| 9708 | Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (integer/hex value).\n", |
| 9709 | file, linenum, args[0]); |
| 9710 | goto out_err; |
| 9711 | } |
| 9712 | |
| 9713 | rule->arg.mark = strtoul(args[cur_arg], &err, 0); |
| 9714 | if (err && *err != '\0') { |
| 9715 | Alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-response %s' (integer/hex value expected).\n", |
| 9716 | file, linenum, err, args[0]); |
| 9717 | goto out_err; |
| 9718 | } |
| 9719 | cur_arg++; |
| 9720 | global.last_checks |= LSTCHK_NETADM; |
| 9721 | #else |
| 9722 | Alert("parsing [%s:%d]: 'http-response %s' is not supported on this platform (SO_MARK undefined).\n", file, linenum, args[0]); |
| 9723 | goto out_err; |
| 9724 | #endif |
Willy Tarreau | 9a355ec | 2013-06-11 17:45:46 +0200 | [diff] [blame] | 9725 | } else if (!strcmp(args[0], "set-log-level")) { |
| 9726 | rule->action = HTTP_RES_ACT_SET_LOGL; |
| 9727 | cur_arg = 1; |
| 9728 | |
| 9729 | if (!*args[cur_arg] || |
| 9730 | (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) { |
| 9731 | bad_log_level: |
| 9732 | Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (log level name or 'silent').\n", |
| 9733 | file, linenum, args[0]); |
| 9734 | goto out_err; |
| 9735 | } |
| 9736 | if (strcmp(args[cur_arg], "silent") == 0) |
| 9737 | rule->arg.loglevel = -1; |
| 9738 | else if ((rule->arg.loglevel = get_log_level(args[cur_arg] + 1)) == 0) |
| 9739 | goto bad_log_level; |
| 9740 | cur_arg++; |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 9741 | } else if (strcmp(args[0], "add-header") == 0 || strcmp(args[0], "set-header") == 0) { |
| 9742 | rule->action = *args[0] == 'a' ? HTTP_RES_ACT_ADD_HDR : HTTP_RES_ACT_SET_HDR; |
| 9743 | cur_arg = 1; |
| 9744 | |
| 9745 | if (!*args[cur_arg] || !*args[cur_arg+1] || |
| 9746 | (*args[cur_arg+2] && strcmp(args[cur_arg+2], "if") != 0 && strcmp(args[cur_arg+2], "unless") != 0)) { |
| 9747 | Alert("parsing [%s:%d]: 'http-response %s' expects exactly 2 arguments.\n", |
| 9748 | file, linenum, args[0]); |
| 9749 | goto out_err; |
| 9750 | } |
| 9751 | |
| 9752 | rule->arg.hdr_add.name = strdup(args[cur_arg]); |
| 9753 | rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name); |
| 9754 | LIST_INIT(&rule->arg.hdr_add.fmt); |
| 9755 | |
Thierry FOURNIER | 1c0054f | 2013-11-20 15:09:52 +0100 | [diff] [blame] | 9756 | proxy->conf.args.ctx = ARGC_HRS; |
Thierry FOURNIER | d048d8b | 2014-03-13 16:46:18 +0100 | [diff] [blame] | 9757 | parse_logformat_string(args[cur_arg + 1], proxy, &rule->arg.hdr_add.fmt, LOG_OPT_HTTP, |
Thierry FOURNIER | eeaa951 | 2014-02-11 14:00:19 +0100 | [diff] [blame] | 9758 | (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR, |
| 9759 | file, linenum); |
Willy Tarreau | 59ad1a2 | 2014-01-29 14:39:58 +0100 | [diff] [blame] | 9760 | free(proxy->conf.lfs_file); |
| 9761 | proxy->conf.lfs_file = strdup(proxy->conf.args.file); |
| 9762 | proxy->conf.lfs_line = proxy->conf.args.line; |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 9763 | cur_arg += 2; |
Sasha Pachev | 218f064 | 2014-06-16 12:05:59 -0600 | [diff] [blame] | 9764 | } else if (strcmp(args[0], "replace-header") == 0 || strcmp(args[0], "replace-value") == 0) { |
Willy Tarreau | b854392 | 2014-06-17 18:58:26 +0200 | [diff] [blame] | 9765 | rule->action = args[0][8] == 'h' ? HTTP_RES_ACT_REPLACE_HDR : HTTP_RES_ACT_REPLACE_VAL; |
Sasha Pachev | 218f064 | 2014-06-16 12:05:59 -0600 | [diff] [blame] | 9766 | cur_arg = 1; |
| 9767 | |
| 9768 | if (!*args[cur_arg] || !*args[cur_arg+1] || !*args[cur_arg+2] || |
Baptiste Assmann | 12cb00b | 2014-08-08 17:29:06 +0200 | [diff] [blame] | 9769 | (*args[cur_arg+3] && strcmp(args[cur_arg+3], "if") != 0 && strcmp(args[cur_arg+3], "unless") != 0)) { |
| 9770 | Alert("parsing [%s:%d]: 'http-response %s' expects exactly 3 arguments.\n", |
Sasha Pachev | 218f064 | 2014-06-16 12:05:59 -0600 | [diff] [blame] | 9771 | file, linenum, args[0]); |
| 9772 | goto out_err; |
| 9773 | } |
| 9774 | |
| 9775 | rule->arg.hdr_add.name = strdup(args[cur_arg]); |
| 9776 | rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name); |
| 9777 | LIST_INIT(&rule->arg.hdr_add.fmt); |
| 9778 | |
Thierry FOURNIER | 09af0d6 | 2014-06-18 11:35:54 +0200 | [diff] [blame] | 9779 | error = NULL; |
| 9780 | if (!regex_comp(args[cur_arg + 1], &rule->arg.hdr_add.re, 1, 1, &error)) { |
| 9781 | Alert("parsing [%s:%d] : '%s' : %s.\n", file, linenum, |
| 9782 | args[cur_arg + 1], error); |
| 9783 | free(error); |
Sasha Pachev | 218f064 | 2014-06-16 12:05:59 -0600 | [diff] [blame] | 9784 | goto out_err; |
| 9785 | } |
| 9786 | |
| 9787 | proxy->conf.args.ctx = ARGC_HRQ; |
| 9788 | parse_logformat_string(args[cur_arg + 2], proxy, &rule->arg.hdr_add.fmt, LOG_OPT_HTTP, |
| 9789 | (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR, |
| 9790 | file, linenum); |
| 9791 | |
| 9792 | free(proxy->conf.lfs_file); |
| 9793 | proxy->conf.lfs_file = strdup(proxy->conf.args.file); |
| 9794 | proxy->conf.lfs_line = proxy->conf.args.line; |
| 9795 | cur_arg += 3; |
Thierry FOURNIER | dad3d1d | 2014-04-22 18:07:25 +0200 | [diff] [blame] | 9796 | } else if (strcmp(args[0], "del-header") == 0) { |
| 9797 | rule->action = HTTP_RES_ACT_DEL_HDR; |
| 9798 | cur_arg = 1; |
| 9799 | |
| 9800 | if (!*args[cur_arg] || |
| 9801 | (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) { |
| 9802 | Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument.\n", |
| 9803 | file, linenum, args[0]); |
| 9804 | goto out_err; |
| 9805 | } |
| 9806 | |
| 9807 | rule->arg.hdr_add.name = strdup(args[cur_arg]); |
| 9808 | rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name); |
| 9809 | |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 9810 | proxy->conf.args.ctx = ARGC_HRS; |
| 9811 | free(proxy->conf.lfs_file); |
| 9812 | proxy->conf.lfs_file = strdup(proxy->conf.args.file); |
| 9813 | proxy->conf.lfs_line = proxy->conf.args.line; |
| 9814 | cur_arg += 1; |
| 9815 | } else if (strncmp(args[0], "add-acl", 7) == 0) { |
| 9816 | /* http-request add-acl(<reference (acl name)>) <key pattern> */ |
| 9817 | rule->action = HTTP_RES_ACT_ADD_ACL; |
| 9818 | /* |
| 9819 | * '+ 8' for 'add-acl(' |
| 9820 | * '- 9' for 'add-acl(' + trailing ')' |
| 9821 | */ |
Willy Tarreau | 6c09c2c | 2014-04-25 21:38:08 +0200 | [diff] [blame] | 9822 | rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9); |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 9823 | |
| 9824 | cur_arg = 1; |
| 9825 | |
| 9826 | if (!*args[cur_arg] || |
| 9827 | (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) { |
| 9828 | Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument.\n", |
| 9829 | file, linenum, args[0]); |
| 9830 | goto out_err; |
| 9831 | } |
| 9832 | |
| 9833 | LIST_INIT(&rule->arg.map.key); |
Thierry FOURNIER | dad3d1d | 2014-04-22 18:07:25 +0200 | [diff] [blame] | 9834 | proxy->conf.args.ctx = ARGC_HRS; |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 9835 | parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP, |
| 9836 | (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR, |
| 9837 | file, linenum); |
Thierry FOURNIER | dad3d1d | 2014-04-22 18:07:25 +0200 | [diff] [blame] | 9838 | free(proxy->conf.lfs_file); |
| 9839 | proxy->conf.lfs_file = strdup(proxy->conf.args.file); |
| 9840 | proxy->conf.lfs_line = proxy->conf.args.line; |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 9841 | |
Thierry FOURNIER | dad3d1d | 2014-04-22 18:07:25 +0200 | [diff] [blame] | 9842 | cur_arg += 1; |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 9843 | } else if (strncmp(args[0], "del-acl", 7) == 0) { |
| 9844 | /* http-response del-acl(<reference (acl name)>) <key pattern> */ |
| 9845 | rule->action = HTTP_RES_ACT_DEL_ACL; |
| 9846 | /* |
| 9847 | * '+ 8' for 'del-acl(' |
| 9848 | * '- 9' for 'del-acl(' + trailing ')' |
| 9849 | */ |
Willy Tarreau | 6c09c2c | 2014-04-25 21:38:08 +0200 | [diff] [blame] | 9850 | rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9); |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 9851 | |
| 9852 | cur_arg = 1; |
| 9853 | |
| 9854 | if (!*args[cur_arg] || |
| 9855 | (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) { |
| 9856 | Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument.\n", |
| 9857 | file, linenum, args[0]); |
| 9858 | goto out_err; |
| 9859 | } |
| 9860 | |
| 9861 | LIST_INIT(&rule->arg.map.key); |
| 9862 | proxy->conf.args.ctx = ARGC_HRS; |
| 9863 | parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP, |
| 9864 | (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR, |
| 9865 | file, linenum); |
| 9866 | free(proxy->conf.lfs_file); |
| 9867 | proxy->conf.lfs_file = strdup(proxy->conf.args.file); |
| 9868 | proxy->conf.lfs_line = proxy->conf.args.line; |
| 9869 | cur_arg += 1; |
| 9870 | } else if (strncmp(args[0], "del-map", 7) == 0) { |
| 9871 | /* http-response del-map(<reference (map name)>) <key pattern> */ |
| 9872 | rule->action = HTTP_RES_ACT_DEL_MAP; |
| 9873 | /* |
| 9874 | * '+ 8' for 'del-map(' |
| 9875 | * '- 9' for 'del-map(' + trailing ')' |
| 9876 | */ |
Willy Tarreau | 6c09c2c | 2014-04-25 21:38:08 +0200 | [diff] [blame] | 9877 | rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9); |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 9878 | |
| 9879 | cur_arg = 1; |
| 9880 | |
| 9881 | if (!*args[cur_arg] || |
| 9882 | (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) { |
| 9883 | Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument.\n", |
| 9884 | file, linenum, args[0]); |
| 9885 | goto out_err; |
| 9886 | } |
| 9887 | |
| 9888 | LIST_INIT(&rule->arg.map.key); |
| 9889 | proxy->conf.args.ctx = ARGC_HRS; |
| 9890 | parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP, |
| 9891 | (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR, |
| 9892 | file, linenum); |
| 9893 | free(proxy->conf.lfs_file); |
| 9894 | proxy->conf.lfs_file = strdup(proxy->conf.args.file); |
| 9895 | proxy->conf.lfs_line = proxy->conf.args.line; |
| 9896 | cur_arg += 1; |
| 9897 | } else if (strncmp(args[0], "set-map", 7) == 0) { |
| 9898 | /* http-response set-map(<reference (map name)>) <key pattern> <value pattern> */ |
| 9899 | rule->action = HTTP_RES_ACT_SET_MAP; |
| 9900 | /* |
| 9901 | * '+ 8' for 'set-map(' |
| 9902 | * '- 9' for 'set-map(' + trailing ')' |
| 9903 | */ |
Willy Tarreau | 6c09c2c | 2014-04-25 21:38:08 +0200 | [diff] [blame] | 9904 | rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9); |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 9905 | |
| 9906 | cur_arg = 1; |
| 9907 | |
| 9908 | if (!*args[cur_arg] || !*args[cur_arg+1] || |
| 9909 | (*args[cur_arg+2] && strcmp(args[cur_arg+2], "if") != 0 && strcmp(args[cur_arg+2], "unless") != 0)) { |
| 9910 | Alert("parsing [%s:%d]: 'http-response %s' expects exactly 2 arguments.\n", |
| 9911 | file, linenum, args[0]); |
| 9912 | goto out_err; |
| 9913 | } |
| 9914 | |
| 9915 | LIST_INIT(&rule->arg.map.key); |
| 9916 | LIST_INIT(&rule->arg.map.value); |
| 9917 | |
| 9918 | proxy->conf.args.ctx = ARGC_HRS; |
| 9919 | |
| 9920 | /* key pattern */ |
| 9921 | parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP, |
| 9922 | (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR, |
| 9923 | file, linenum); |
| 9924 | |
| 9925 | /* value pattern */ |
| 9926 | parse_logformat_string(args[cur_arg + 1], proxy, &rule->arg.map.value, LOG_OPT_HTTP, |
| 9927 | (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR, |
| 9928 | file, linenum); |
| 9929 | |
| 9930 | free(proxy->conf.lfs_file); |
| 9931 | proxy->conf.lfs_file = strdup(proxy->conf.args.file); |
| 9932 | proxy->conf.lfs_line = proxy->conf.args.line; |
| 9933 | |
| 9934 | cur_arg += 2; |
Willy Tarreau | 51d861a | 2015-05-22 17:30:48 +0200 | [diff] [blame] | 9935 | } else if (strcmp(args[0], "redirect") == 0) { |
| 9936 | struct redirect_rule *redir; |
| 9937 | char *errmsg = NULL; |
| 9938 | |
| 9939 | if ((redir = http_parse_redirect_rule(file, linenum, proxy, (const char **)args + 1, &errmsg, 1, 1)) == NULL) { |
| 9940 | Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-response %s' rule : %s.\n", |
| 9941 | file, linenum, proxy_type_str(proxy), proxy->id, args[0], errmsg); |
| 9942 | goto out_err; |
| 9943 | } |
| 9944 | |
| 9945 | /* this redirect rule might already contain a parsed condition which |
| 9946 | * we'll pass to the http-request rule. |
| 9947 | */ |
| 9948 | rule->action = HTTP_RES_ACT_REDIR; |
| 9949 | rule->arg.redir = redir; |
| 9950 | rule->cond = redir->cond; |
| 9951 | redir->cond = NULL; |
| 9952 | cur_arg = 2; |
| 9953 | return rule; |
William Lallemand | 73025dd | 2014-04-24 14:38:37 +0200 | [diff] [blame] | 9954 | } else if (((custom = action_http_res_custom(args[0])) != NULL)) { |
| 9955 | char *errmsg = NULL; |
| 9956 | cur_arg = 1; |
| 9957 | /* try in the module list */ |
| 9958 | if (custom->parse(args, &cur_arg, proxy, rule, &errmsg) < 0) { |
| 9959 | Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-response %s' rule : %s.\n", |
| 9960 | file, linenum, proxy_type_str(proxy), proxy->id, args[0], errmsg); |
| 9961 | free(errmsg); |
| 9962 | goto out_err; |
| 9963 | } |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 9964 | } else { |
Thierry FOURNIER | 4834bc7 | 2015-06-06 19:29:07 +0200 | [diff] [blame] | 9965 | Alert("parsing [%s:%d]: 'http-response' expects 'allow', 'deny', 'redirect', 'add-header', 'del-header', 'set-header', 'replace-header', 'replace-value', 'set-nice', 'set-tos', 'set-mark', 'set-log-level', 'del-acl', 'add-acl', 'del-map', 'set-map', 'set-var' but got '%s'%s.\n", |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 9966 | file, linenum, args[0], *args[0] ? "" : " (missing argument)"); |
| 9967 | goto out_err; |
| 9968 | } |
| 9969 | |
| 9970 | if (strcmp(args[cur_arg], "if") == 0 || strcmp(args[cur_arg], "unless") == 0) { |
| 9971 | struct acl_cond *cond; |
| 9972 | char *errmsg = NULL; |
| 9973 | |
| 9974 | if ((cond = build_acl_cond(file, linenum, proxy, args+cur_arg, &errmsg)) == NULL) { |
| 9975 | Alert("parsing [%s:%d] : error detected while parsing an 'http-response %s' condition : %s.\n", |
| 9976 | file, linenum, args[0], errmsg); |
| 9977 | free(errmsg); |
| 9978 | goto out_err; |
| 9979 | } |
| 9980 | rule->cond = cond; |
| 9981 | } |
| 9982 | else if (*args[cur_arg]) { |
| 9983 | Alert("parsing [%s:%d]: 'http-response %s' expects" |
| 9984 | " either 'if' or 'unless' followed by a condition but found '%s'.\n", |
| 9985 | file, linenum, args[0], args[cur_arg]); |
| 9986 | goto out_err; |
| 9987 | } |
| 9988 | |
| 9989 | return rule; |
| 9990 | out_err: |
| 9991 | free(rule); |
| 9992 | return NULL; |
| 9993 | } |
| 9994 | |
Willy Tarreau | 4baae24 | 2012-12-27 12:00:31 +0100 | [diff] [blame] | 9995 | /* Parses a redirect rule. Returns the redirect rule on success or NULL on error, |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 9996 | * with <err> filled with the error message. If <use_fmt> is not null, builds a |
Willy Tarreau | be4653b | 2015-05-28 15:26:58 +0200 | [diff] [blame] | 9997 | * dynamic log-format rule instead of a static string. Parameter <dir> indicates |
| 9998 | * the direction of the rule, and equals 0 for request, non-zero for responses. |
Willy Tarreau | 4baae24 | 2012-12-27 12:00:31 +0100 | [diff] [blame] | 9999 | */ |
| 10000 | struct redirect_rule *http_parse_redirect_rule(const char *file, int linenum, struct proxy *curproxy, |
Willy Tarreau | be4653b | 2015-05-28 15:26:58 +0200 | [diff] [blame] | 10001 | const char **args, char **errmsg, int use_fmt, int dir) |
Willy Tarreau | 4baae24 | 2012-12-27 12:00:31 +0100 | [diff] [blame] | 10002 | { |
| 10003 | struct redirect_rule *rule; |
| 10004 | int cur_arg; |
| 10005 | int type = REDIRECT_TYPE_NONE; |
| 10006 | int code = 302; |
| 10007 | const char *destination = NULL; |
| 10008 | const char *cookie = NULL; |
| 10009 | int cookie_set = 0; |
| 10010 | unsigned int flags = REDIRECT_FLAG_NONE; |
| 10011 | struct acl_cond *cond = NULL; |
| 10012 | |
| 10013 | cur_arg = 0; |
| 10014 | while (*(args[cur_arg])) { |
| 10015 | if (strcmp(args[cur_arg], "location") == 0) { |
| 10016 | if (!*args[cur_arg + 1]) |
| 10017 | goto missing_arg; |
| 10018 | |
| 10019 | type = REDIRECT_TYPE_LOCATION; |
| 10020 | cur_arg++; |
| 10021 | destination = args[cur_arg]; |
| 10022 | } |
| 10023 | else if (strcmp(args[cur_arg], "prefix") == 0) { |
| 10024 | if (!*args[cur_arg + 1]) |
| 10025 | goto missing_arg; |
Willy Tarreau | 4baae24 | 2012-12-27 12:00:31 +0100 | [diff] [blame] | 10026 | type = REDIRECT_TYPE_PREFIX; |
| 10027 | cur_arg++; |
| 10028 | destination = args[cur_arg]; |
| 10029 | } |
| 10030 | else if (strcmp(args[cur_arg], "scheme") == 0) { |
| 10031 | if (!*args[cur_arg + 1]) |
| 10032 | goto missing_arg; |
| 10033 | |
| 10034 | type = REDIRECT_TYPE_SCHEME; |
| 10035 | cur_arg++; |
| 10036 | destination = args[cur_arg]; |
| 10037 | } |
| 10038 | else if (strcmp(args[cur_arg], "set-cookie") == 0) { |
| 10039 | if (!*args[cur_arg + 1]) |
| 10040 | goto missing_arg; |
| 10041 | |
| 10042 | cur_arg++; |
| 10043 | cookie = args[cur_arg]; |
| 10044 | cookie_set = 1; |
| 10045 | } |
| 10046 | else if (strcmp(args[cur_arg], "clear-cookie") == 0) { |
| 10047 | if (!*args[cur_arg + 1]) |
| 10048 | goto missing_arg; |
| 10049 | |
| 10050 | cur_arg++; |
| 10051 | cookie = args[cur_arg]; |
| 10052 | cookie_set = 0; |
| 10053 | } |
| 10054 | else if (strcmp(args[cur_arg], "code") == 0) { |
| 10055 | if (!*args[cur_arg + 1]) |
| 10056 | goto missing_arg; |
| 10057 | |
| 10058 | cur_arg++; |
| 10059 | code = atol(args[cur_arg]); |
Yves Lafon | 3e8d1ae | 2013-03-11 11:06:05 -0400 | [diff] [blame] | 10060 | if (code < 301 || code > 308 || (code > 303 && code < 307)) { |
Willy Tarreau | 4baae24 | 2012-12-27 12:00:31 +0100 | [diff] [blame] | 10061 | memprintf(errmsg, |
Yves Lafon | 3e8d1ae | 2013-03-11 11:06:05 -0400 | [diff] [blame] | 10062 | "'%s': unsupported HTTP code '%s' (must be one of 301, 302, 303, 307 or 308)", |
Willy Tarreau | 4baae24 | 2012-12-27 12:00:31 +0100 | [diff] [blame] | 10063 | args[cur_arg - 1], args[cur_arg]); |
| 10064 | return NULL; |
| 10065 | } |
| 10066 | } |
| 10067 | else if (!strcmp(args[cur_arg],"drop-query")) { |
| 10068 | flags |= REDIRECT_FLAG_DROP_QS; |
| 10069 | } |
| 10070 | else if (!strcmp(args[cur_arg],"append-slash")) { |
| 10071 | flags |= REDIRECT_FLAG_APPEND_SLASH; |
| 10072 | } |
| 10073 | else if (strcmp(args[cur_arg], "if") == 0 || |
| 10074 | strcmp(args[cur_arg], "unless") == 0) { |
| 10075 | cond = build_acl_cond(file, linenum, curproxy, (const char **)args + cur_arg, errmsg); |
| 10076 | if (!cond) { |
| 10077 | memprintf(errmsg, "error in condition: %s", *errmsg); |
| 10078 | return NULL; |
| 10079 | } |
| 10080 | break; |
| 10081 | } |
| 10082 | else { |
| 10083 | memprintf(errmsg, |
| 10084 | "expects 'code', 'prefix', 'location', 'scheme', 'set-cookie', 'clear-cookie', 'drop-query' or 'append-slash' (was '%s')", |
| 10085 | args[cur_arg]); |
| 10086 | return NULL; |
| 10087 | } |
| 10088 | cur_arg++; |
| 10089 | } |
| 10090 | |
| 10091 | if (type == REDIRECT_TYPE_NONE) { |
| 10092 | memprintf(errmsg, "redirection type expected ('prefix', 'location', or 'scheme')"); |
| 10093 | return NULL; |
| 10094 | } |
| 10095 | |
Willy Tarreau | be4653b | 2015-05-28 15:26:58 +0200 | [diff] [blame] | 10096 | if (dir && type != REDIRECT_TYPE_LOCATION) { |
| 10097 | memprintf(errmsg, "response only supports redirect type 'location'"); |
| 10098 | return NULL; |
| 10099 | } |
| 10100 | |
Willy Tarreau | 4baae24 | 2012-12-27 12:00:31 +0100 | [diff] [blame] | 10101 | rule = (struct redirect_rule *)calloc(1, sizeof(*rule)); |
| 10102 | rule->cond = cond; |
Willy Tarreau | 60e0838 | 2013-12-03 00:48:45 +0100 | [diff] [blame] | 10103 | LIST_INIT(&rule->rdr_fmt); |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 10104 | |
| 10105 | if (!use_fmt) { |
| 10106 | /* old-style static redirect rule */ |
| 10107 | rule->rdr_str = strdup(destination); |
| 10108 | rule->rdr_len = strlen(destination); |
| 10109 | } |
| 10110 | else { |
| 10111 | /* log-format based redirect rule */ |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 10112 | |
| 10113 | /* Parse destination. Note that in the REDIRECT_TYPE_PREFIX case, |
| 10114 | * if prefix == "/", we don't want to add anything, otherwise it |
| 10115 | * makes it hard for the user to configure a self-redirection. |
| 10116 | */ |
Godbach | d972203 | 2014-12-18 15:44:58 +0800 | [diff] [blame] | 10117 | curproxy->conf.args.ctx = ARGC_RDR; |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 10118 | if (!(type == REDIRECT_TYPE_PREFIX && destination[0] == '/' && destination[1] == '\0')) { |
Thierry FOURNIER | d048d8b | 2014-03-13 16:46:18 +0100 | [diff] [blame] | 10119 | parse_logformat_string(destination, curproxy, &rule->rdr_fmt, LOG_OPT_HTTP, |
Willy Tarreau | be4653b | 2015-05-28 15:26:58 +0200 | [diff] [blame] | 10120 | dir ? (curproxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRS_HDR : SMP_VAL_BE_HRS_HDR |
| 10121 | : (curproxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR, |
Thierry FOURNIER | eeaa951 | 2014-02-11 14:00:19 +0100 | [diff] [blame] | 10122 | file, linenum); |
Willy Tarreau | 59ad1a2 | 2014-01-29 14:39:58 +0100 | [diff] [blame] | 10123 | free(curproxy->conf.lfs_file); |
| 10124 | curproxy->conf.lfs_file = strdup(curproxy->conf.args.file); |
| 10125 | curproxy->conf.lfs_line = curproxy->conf.args.line; |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 10126 | } |
| 10127 | } |
| 10128 | |
Willy Tarreau | 4baae24 | 2012-12-27 12:00:31 +0100 | [diff] [blame] | 10129 | if (cookie) { |
| 10130 | /* depending on cookie_set, either we want to set the cookie, or to clear it. |
| 10131 | * a clear consists in appending "; path=/; Max-Age=0;" at the end. |
| 10132 | */ |
| 10133 | rule->cookie_len = strlen(cookie); |
| 10134 | if (cookie_set) { |
| 10135 | rule->cookie_str = malloc(rule->cookie_len + 10); |
| 10136 | memcpy(rule->cookie_str, cookie, rule->cookie_len); |
| 10137 | memcpy(rule->cookie_str + rule->cookie_len, "; path=/;", 10); |
| 10138 | rule->cookie_len += 9; |
| 10139 | } else { |
| 10140 | rule->cookie_str = malloc(rule->cookie_len + 21); |
| 10141 | memcpy(rule->cookie_str, cookie, rule->cookie_len); |
| 10142 | memcpy(rule->cookie_str + rule->cookie_len, "; path=/; Max-Age=0;", 21); |
| 10143 | rule->cookie_len += 20; |
| 10144 | } |
| 10145 | } |
| 10146 | rule->type = type; |
| 10147 | rule->code = code; |
| 10148 | rule->flags = flags; |
| 10149 | LIST_INIT(&rule->list); |
| 10150 | return rule; |
| 10151 | |
| 10152 | missing_arg: |
| 10153 | memprintf(errmsg, "missing argument for '%s'", args[cur_arg]); |
| 10154 | return NULL; |
| 10155 | } |
| 10156 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 10157 | /************************************************************************/ |
| 10158 | /* The code below is dedicated to ACL parsing and matching */ |
| 10159 | /************************************************************************/ |
| 10160 | |
| 10161 | |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 10162 | /* This function ensures that the prerequisites for an L7 fetch are ready, |
| 10163 | * which means that a request or response is ready. If some data is missing, |
| 10164 | * a parsing attempt is made. This is useful in TCP-based ACLs which are able |
Willy Tarreau | 24e32d8 | 2012-04-23 23:55:44 +0200 | [diff] [blame] | 10165 | * to extract data from L7. If <req_vol> is non-null during a request prefetch, |
| 10166 | * another test is made to ensure the required information is not gone. |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 10167 | * |
| 10168 | * The function returns : |
Willy Tarreau | 506d050 | 2013-07-06 13:29:24 +0200 | [diff] [blame] | 10169 | * 0 with SMP_F_MAY_CHANGE in the sample flags if some data is missing to |
| 10170 | * decide whether or not an HTTP message is present ; |
| 10171 | * 0 if the requested data cannot be fetched or if it is certain that |
| 10172 | * we'll never have any HTTP message there ; |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 10173 | * 1 if an HTTP message is ready |
| 10174 | */ |
| 10175 | static int |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 10176 | smp_prefetch_http(struct proxy *px, struct stream *s, unsigned int opt, |
Willy Tarreau | 24e32d8 | 2012-04-23 23:55:44 +0200 | [diff] [blame] | 10177 | const struct arg *args, struct sample *smp, int req_vol) |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 10178 | { |
Willy Tarreau | 192252e | 2015-04-04 01:47:55 +0200 | [diff] [blame] | 10179 | struct http_txn *txn; |
| 10180 | struct http_msg *msg; |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 10181 | |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 10182 | /* Note: this function may only be used from places where |
| 10183 | * http_init_txn() has already been done, and implies that <s>, |
| 10184 | * <txn>, and <hdr_idx.v> are properly set. An extra check protects |
| 10185 | * against an eventual mistake in the fetch capability matrix. |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 10186 | */ |
| 10187 | |
Willy Tarreau | 192252e | 2015-04-04 01:47:55 +0200 | [diff] [blame] | 10188 | if (!s) |
| 10189 | return 0; |
| 10190 | txn = s->txn; |
| 10191 | |
| 10192 | if (!txn) |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 10193 | return 0; |
Willy Tarreau | 192252e | 2015-04-04 01:47:55 +0200 | [diff] [blame] | 10194 | msg = &txn->req; |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 10195 | |
| 10196 | /* Check for a dependency on a request */ |
Willy Tarreau | f853c46 | 2012-04-23 18:53:56 +0200 | [diff] [blame] | 10197 | smp->type = SMP_T_BOOL; |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 10198 | |
Willy Tarreau | 32a6f2e | 2012-04-25 10:13:36 +0200 | [diff] [blame] | 10199 | if ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) { |
Willy Tarreau | aae75e3 | 2013-03-29 12:31:49 +0100 | [diff] [blame] | 10200 | /* If the buffer does not leave enough free space at the end, |
| 10201 | * we must first realign it. |
| 10202 | */ |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 10203 | if (s->req.buf->p > s->req.buf->data && |
| 10204 | s->req.buf->i + s->req.buf->p > s->req.buf->data + s->req.buf->size - global.tune.maxrewrite) |
| 10205 | buffer_slow_realign(s->req.buf); |
Willy Tarreau | aae75e3 | 2013-03-29 12:31:49 +0100 | [diff] [blame] | 10206 | |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 10207 | if (unlikely(txn->req.msg_state < HTTP_MSG_BODY)) { |
Willy Tarreau | 472b1ee | 2013-10-14 22:41:30 +0200 | [diff] [blame] | 10208 | if (msg->msg_state == HTTP_MSG_ERROR) |
Willy Tarreau | 506d050 | 2013-07-06 13:29:24 +0200 | [diff] [blame] | 10209 | return 0; |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 10210 | |
| 10211 | /* Try to decode HTTP request */ |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 10212 | if (likely(msg->next < s->req.buf->i)) |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 10213 | http_msg_analyzer(msg, &txn->hdr_idx); |
| 10214 | |
| 10215 | /* Still no valid request ? */ |
| 10216 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) { |
Willy Tarreau | 3bf1b2b | 2012-08-27 20:46:07 +0200 | [diff] [blame] | 10217 | if ((msg->msg_state == HTTP_MSG_ERROR) || |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 10218 | buffer_full(s->req.buf, global.tune.maxrewrite)) { |
Willy Tarreau | 506d050 | 2013-07-06 13:29:24 +0200 | [diff] [blame] | 10219 | return 0; |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 10220 | } |
| 10221 | /* wait for final state */ |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 10222 | smp->flags |= SMP_F_MAY_CHANGE; |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 10223 | return 0; |
| 10224 | } |
| 10225 | |
| 10226 | /* OK we just got a valid HTTP request. We have some minor |
| 10227 | * preparation to perform so that further checks can rely |
| 10228 | * on HTTP tests. |
| 10229 | */ |
Willy Tarreau | aae75e3 | 2013-03-29 12:31:49 +0100 | [diff] [blame] | 10230 | |
| 10231 | /* If the request was parsed but was too large, we must absolutely |
| 10232 | * return an error so that it is not processed. At the moment this |
| 10233 | * cannot happen, but if the parsers are to change in the future, |
| 10234 | * we want this check to be maintained. |
| 10235 | */ |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 10236 | if (unlikely(s->req.buf->i + s->req.buf->p > |
| 10237 | s->req.buf->data + s->req.buf->size - global.tune.maxrewrite)) { |
Willy Tarreau | aae75e3 | 2013-03-29 12:31:49 +0100 | [diff] [blame] | 10238 | msg->msg_state = HTTP_MSG_ERROR; |
Thierry FOURNIER | 07ee64e | 2015-07-06 23:43:03 +0200 | [diff] [blame] | 10239 | smp->data.sint = 1; |
Willy Tarreau | aae75e3 | 2013-03-29 12:31:49 +0100 | [diff] [blame] | 10240 | return 1; |
| 10241 | } |
| 10242 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 10243 | txn->meth = find_http_meth(msg->chn->buf->p, msg->sl.rq.m_l); |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 10244 | if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD) |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 10245 | s->flags |= SF_REDIRECTABLE; |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 10246 | |
Willy Tarreau | 506d050 | 2013-07-06 13:29:24 +0200 | [diff] [blame] | 10247 | if (unlikely(msg->sl.rq.v_l == 0) && !http_upgrade_v09_to_v10(txn)) |
| 10248 | return 0; |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 10249 | } |
| 10250 | |
Willy Tarreau | 506d050 | 2013-07-06 13:29:24 +0200 | [diff] [blame] | 10251 | if (req_vol && txn->rsp.msg_state != HTTP_MSG_RPBEFORE) { |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 10252 | return 0; /* data might have moved and indexes changed */ |
Willy Tarreau | 506d050 | 2013-07-06 13:29:24 +0200 | [diff] [blame] | 10253 | } |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 10254 | |
| 10255 | /* otherwise everything's ready for the request */ |
| 10256 | } |
Willy Tarreau | 24e32d8 | 2012-04-23 23:55:44 +0200 | [diff] [blame] | 10257 | else { |
| 10258 | /* Check for a dependency on a response */ |
Willy Tarreau | 506d050 | 2013-07-06 13:29:24 +0200 | [diff] [blame] | 10259 | if (txn->rsp.msg_state < HTTP_MSG_BODY) { |
| 10260 | smp->flags |= SMP_F_MAY_CHANGE; |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 10261 | return 0; |
Willy Tarreau | 506d050 | 2013-07-06 13:29:24 +0200 | [diff] [blame] | 10262 | } |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 10263 | } |
| 10264 | |
| 10265 | /* everything's OK */ |
Thierry FOURNIER | 07ee64e | 2015-07-06 23:43:03 +0200 | [diff] [blame] | 10266 | smp->data.sint = 1; |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 10267 | return 1; |
| 10268 | } |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 10269 | |
Willy Tarreau | 6c616e0 | 2014-06-25 16:56:41 +0200 | [diff] [blame] | 10270 | /* Note: these functinos *do* modify the sample. Even in case of success, at |
| 10271 | * least the type and uint value are modified. |
| 10272 | */ |
Willy Tarreau | c0239e0 | 2012-04-16 14:42:55 +0200 | [diff] [blame] | 10273 | #define CHECK_HTTP_MESSAGE_FIRST() \ |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10274 | do { int r = smp_prefetch_http(smp->px, smp->strm, smp->opt, args, smp, 1); if (r <= 0) return r; } while (0) |
Willy Tarreau | c0239e0 | 2012-04-16 14:42:55 +0200 | [diff] [blame] | 10275 | |
Willy Tarreau | 24e32d8 | 2012-04-23 23:55:44 +0200 | [diff] [blame] | 10276 | #define CHECK_HTTP_MESSAGE_FIRST_PERM() \ |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10277 | do { int r = smp_prefetch_http(smp->px, smp->strm, smp->opt, args, smp, 0); if (r <= 0) return r; } while (0) |
Willy Tarreau | 24e32d8 | 2012-04-23 23:55:44 +0200 | [diff] [blame] | 10278 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 10279 | |
| 10280 | /* 1. Check on METHOD |
| 10281 | * We use the pre-parsed method if it is known, and store its number as an |
| 10282 | * integer. If it is unknown, we use the pointer and the length. |
| 10283 | */ |
Thierry FOURNIER | e47e4e2 | 2014-04-28 11:18:57 +0200 | [diff] [blame] | 10284 | static int pat_parse_meth(const char *text, struct pattern *pattern, int mflags, char **err) |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 10285 | { |
| 10286 | int len, meth; |
| 10287 | |
Thierry FOURNIER | 580c32c | 2014-01-24 10:58:12 +0100 | [diff] [blame] | 10288 | len = strlen(text); |
| 10289 | meth = find_http_meth(text, len); |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 10290 | |
| 10291 | pattern->val.i = meth; |
| 10292 | if (meth == HTTP_METH_OTHER) { |
Willy Tarreau | 912c119 | 2014-08-29 15:15:50 +0200 | [diff] [blame] | 10293 | pattern->ptr.str = (char *)text; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 10294 | pattern->len = len; |
| 10295 | } |
Thierry FOURNIER | d437314 | 2013-12-17 01:10:10 +0100 | [diff] [blame] | 10296 | else { |
| 10297 | pattern->ptr.str = NULL; |
| 10298 | pattern->len = 0; |
Thierry FOURNIER | d437314 | 2013-12-17 01:10:10 +0100 | [diff] [blame] | 10299 | } |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 10300 | return 1; |
| 10301 | } |
| 10302 | |
Willy Tarreau | 8e5e955 | 2011-12-16 15:38:49 +0100 | [diff] [blame] | 10303 | /* This function fetches the method of current HTTP request and stores |
| 10304 | * it in the global pattern struct as a chunk. There are two possibilities : |
| 10305 | * - if the method is known (not HTTP_METH_OTHER), its identifier is stored |
| 10306 | * in <len> and <ptr> is NULL ; |
| 10307 | * - if the method is unknown (HTTP_METH_OTHER), <ptr> points to the text and |
| 10308 | * <len> to its length. |
Thierry FOURNIER | a65b343 | 2013-11-28 18:22:00 +0100 | [diff] [blame] | 10309 | * This is intended to be used with pat_match_meth() only. |
Willy Tarreau | 8e5e955 | 2011-12-16 15:38:49 +0100 | [diff] [blame] | 10310 | */ |
Willy Tarreau | d41f8d8 | 2007-06-10 10:06:18 +0200 | [diff] [blame] | 10311 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10312 | smp_fetch_meth(const struct arg *args, struct sample *smp, const char *kw, void *private) |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 10313 | { |
| 10314 | int meth; |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 10315 | struct http_txn *txn = smp->strm->txn; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 10316 | |
Willy Tarreau | 24e32d8 | 2012-04-23 23:55:44 +0200 | [diff] [blame] | 10317 | CHECK_HTTP_MESSAGE_FIRST_PERM(); |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 10318 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 10319 | meth = txn->meth; |
Thierry FOURNIER | d437314 | 2013-12-17 01:10:10 +0100 | [diff] [blame] | 10320 | smp->type = SMP_T_METH; |
| 10321 | smp->data.meth.meth = meth; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 10322 | if (meth == HTTP_METH_OTHER) { |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 10323 | if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE) |
| 10324 | /* ensure the indexes are not affected */ |
| 10325 | return 0; |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 10326 | smp->flags |= SMP_F_CONST; |
Thierry FOURNIER | d437314 | 2013-12-17 01:10:10 +0100 | [diff] [blame] | 10327 | smp->data.meth.str.len = txn->req.sl.rq.m_l; |
| 10328 | smp->data.meth.str.str = txn->req.chn->buf->p; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 10329 | } |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 10330 | smp->flags |= SMP_F_VOL_1ST; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 10331 | return 1; |
| 10332 | } |
| 10333 | |
Willy Tarreau | 8e5e955 | 2011-12-16 15:38:49 +0100 | [diff] [blame] | 10334 | /* See above how the method is stored in the global pattern */ |
Thierry FOURNIER | 5338eea | 2013-12-16 14:22:13 +0100 | [diff] [blame] | 10335 | static struct pattern *pat_match_meth(struct sample *smp, struct pattern_expr *expr, int fill) |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 10336 | { |
Willy Tarreau | c8d7c96 | 2007-06-17 08:20:33 +0200 | [diff] [blame] | 10337 | int icase; |
Thierry FOURNIER | 5338eea | 2013-12-16 14:22:13 +0100 | [diff] [blame] | 10338 | struct pattern_list *lst; |
| 10339 | struct pattern *pattern; |
Willy Tarreau | c8d7c96 | 2007-06-17 08:20:33 +0200 | [diff] [blame] | 10340 | |
Thierry FOURNIER | 5338eea | 2013-12-16 14:22:13 +0100 | [diff] [blame] | 10341 | list_for_each_entry(lst, &expr->patterns, list) { |
| 10342 | pattern = &lst->pat; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 10343 | |
Thierry FOURNIER | 5338eea | 2013-12-16 14:22:13 +0100 | [diff] [blame] | 10344 | /* well-known method */ |
| 10345 | if (pattern->val.i != HTTP_METH_OTHER) { |
| 10346 | if (smp->data.meth.meth == pattern->val.i) |
| 10347 | return pattern; |
| 10348 | else |
| 10349 | continue; |
| 10350 | } |
Willy Tarreau | c8d7c96 | 2007-06-17 08:20:33 +0200 | [diff] [blame] | 10351 | |
Thierry FOURNIER | 5338eea | 2013-12-16 14:22:13 +0100 | [diff] [blame] | 10352 | /* Other method, we must compare the strings */ |
| 10353 | if (pattern->len != smp->data.meth.str.len) |
| 10354 | continue; |
| 10355 | |
Thierry FOURNIER | e47e4e2 | 2014-04-28 11:18:57 +0200 | [diff] [blame] | 10356 | icase = expr->mflags & PAT_MF_IGNORE_CASE; |
Willy Tarreau | 4de2a94 | 2014-08-28 20:42:57 +0200 | [diff] [blame] | 10357 | if ((icase && strncasecmp(pattern->ptr.str, smp->data.meth.str.str, smp->data.meth.str.len) == 0) || |
| 10358 | (!icase && strncmp(pattern->ptr.str, smp->data.meth.str.str, smp->data.meth.str.len) == 0)) |
Thierry FOURNIER | 5338eea | 2013-12-16 14:22:13 +0100 | [diff] [blame] | 10359 | return pattern; |
| 10360 | } |
| 10361 | return NULL; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 10362 | } |
| 10363 | |
Willy Tarreau | d41f8d8 | 2007-06-10 10:06:18 +0200 | [diff] [blame] | 10364 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10365 | smp_fetch_rqver(const struct arg *args, struct sample *smp, const char *kw, void *private) |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 10366 | { |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 10367 | struct http_txn *txn = smp->strm->txn; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 10368 | char *ptr; |
| 10369 | int len; |
| 10370 | |
Willy Tarreau | c0239e0 | 2012-04-16 14:42:55 +0200 | [diff] [blame] | 10371 | CHECK_HTTP_MESSAGE_FIRST(); |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 10372 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 10373 | len = txn->req.sl.rq.v_l; |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 10374 | ptr = txn->req.chn->buf->p + txn->req.sl.rq.v; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 10375 | |
| 10376 | while ((len-- > 0) && (*ptr++ != '/')); |
| 10377 | if (len <= 0) |
| 10378 | return 0; |
| 10379 | |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 10380 | smp->type = SMP_T_STR; |
Willy Tarreau | f853c46 | 2012-04-23 18:53:56 +0200 | [diff] [blame] | 10381 | smp->data.str.str = ptr; |
| 10382 | smp->data.str.len = len; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 10383 | |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 10384 | smp->flags = SMP_F_VOL_1ST | SMP_F_CONST; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 10385 | return 1; |
| 10386 | } |
| 10387 | |
Willy Tarreau | d41f8d8 | 2007-06-10 10:06:18 +0200 | [diff] [blame] | 10388 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10389 | smp_fetch_stver(const struct arg *args, struct sample *smp, const char *kw, void *private) |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 10390 | { |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 10391 | struct http_txn *txn; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 10392 | char *ptr; |
| 10393 | int len; |
| 10394 | |
Willy Tarreau | c0239e0 | 2012-04-16 14:42:55 +0200 | [diff] [blame] | 10395 | CHECK_HTTP_MESSAGE_FIRST(); |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 10396 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 10397 | txn = smp->strm->txn; |
Willy Tarreau | f26b252 | 2012-12-14 08:33:14 +0100 | [diff] [blame] | 10398 | if (txn->rsp.msg_state < HTTP_MSG_BODY) |
| 10399 | return 0; |
| 10400 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 10401 | len = txn->rsp.sl.st.v_l; |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 10402 | ptr = txn->rsp.chn->buf->p; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 10403 | |
| 10404 | while ((len-- > 0) && (*ptr++ != '/')); |
| 10405 | if (len <= 0) |
| 10406 | return 0; |
| 10407 | |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 10408 | smp->type = SMP_T_STR; |
Willy Tarreau | f853c46 | 2012-04-23 18:53:56 +0200 | [diff] [blame] | 10409 | smp->data.str.str = ptr; |
| 10410 | smp->data.str.len = len; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 10411 | |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 10412 | smp->flags = SMP_F_VOL_1ST | SMP_F_CONST; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 10413 | return 1; |
| 10414 | } |
| 10415 | |
| 10416 | /* 3. Check on Status Code. We manipulate integers here. */ |
Willy Tarreau | d41f8d8 | 2007-06-10 10:06:18 +0200 | [diff] [blame] | 10417 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10418 | smp_fetch_stcode(const struct arg *args, struct sample *smp, const char *kw, void *private) |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 10419 | { |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 10420 | struct http_txn *txn; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 10421 | char *ptr; |
| 10422 | int len; |
| 10423 | |
Willy Tarreau | c0239e0 | 2012-04-16 14:42:55 +0200 | [diff] [blame] | 10424 | CHECK_HTTP_MESSAGE_FIRST(); |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 10425 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 10426 | txn = smp->strm->txn; |
Willy Tarreau | f26b252 | 2012-12-14 08:33:14 +0100 | [diff] [blame] | 10427 | if (txn->rsp.msg_state < HTTP_MSG_BODY) |
| 10428 | return 0; |
| 10429 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 10430 | len = txn->rsp.sl.st.c_l; |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 10431 | ptr = txn->rsp.chn->buf->p + txn->rsp.sl.st.c; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 10432 | |
Thierry FOURNIER | 07ee64e | 2015-07-06 23:43:03 +0200 | [diff] [blame] | 10433 | smp->type = SMP_T_SINT; |
| 10434 | smp->data.sint = __strl2ui(ptr, len); |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 10435 | smp->flags = SMP_F_VOL_1ST; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 10436 | return 1; |
| 10437 | } |
| 10438 | |
Willy Tarreau | a5910cc | 2015-05-02 00:46:08 +0200 | [diff] [blame] | 10439 | /* returns the longest available part of the body. This requires that the body |
| 10440 | * has been waited for using http-buffer-request. |
| 10441 | */ |
| 10442 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10443 | smp_fetch_body(const struct arg *args, struct sample *smp, const char *kw, void *private) |
Willy Tarreau | a5910cc | 2015-05-02 00:46:08 +0200 | [diff] [blame] | 10444 | { |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 10445 | struct http_txn *txn = smp->strm->txn; |
Willy Tarreau | a5910cc | 2015-05-02 00:46:08 +0200 | [diff] [blame] | 10446 | struct http_msg *msg; |
| 10447 | unsigned long len; |
| 10448 | unsigned long block1; |
| 10449 | char *body; |
| 10450 | struct chunk *temp; |
| 10451 | |
| 10452 | CHECK_HTTP_MESSAGE_FIRST(); |
| 10453 | |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10454 | if ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) |
Willy Tarreau | a5910cc | 2015-05-02 00:46:08 +0200 | [diff] [blame] | 10455 | msg = &txn->req; |
| 10456 | else |
| 10457 | msg = &txn->rsp; |
| 10458 | |
| 10459 | len = http_body_bytes(msg); |
| 10460 | body = b_ptr(msg->chn->buf, -http_data_rewind(msg)); |
| 10461 | |
| 10462 | block1 = len; |
| 10463 | if (block1 > msg->chn->buf->data + msg->chn->buf->size - body) |
| 10464 | block1 = msg->chn->buf->data + msg->chn->buf->size - body; |
| 10465 | |
| 10466 | if (block1 == len) { |
| 10467 | /* buffer is not wrapped (or empty) */ |
| 10468 | smp->type = SMP_T_BIN; |
| 10469 | smp->data.str.str = body; |
| 10470 | smp->data.str.len = len; |
| 10471 | smp->flags = SMP_F_VOL_TEST | SMP_F_CONST; |
| 10472 | } |
| 10473 | else { |
| 10474 | /* buffer is wrapped, we need to defragment it */ |
| 10475 | temp = get_trash_chunk(); |
| 10476 | memcpy(temp->str, body, block1); |
| 10477 | memcpy(temp->str + block1, msg->chn->buf->data, len - block1); |
| 10478 | smp->type = SMP_T_BIN; |
| 10479 | smp->data.str.str = temp->str; |
| 10480 | smp->data.str.len = len; |
| 10481 | smp->flags = SMP_F_VOL_TEST; |
| 10482 | } |
| 10483 | return 1; |
| 10484 | } |
| 10485 | |
| 10486 | |
| 10487 | /* returns the available length of the body. This requires that the body |
| 10488 | * has been waited for using http-buffer-request. |
| 10489 | */ |
| 10490 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10491 | smp_fetch_body_len(const struct arg *args, struct sample *smp, const char *kw, void *private) |
Willy Tarreau | a5910cc | 2015-05-02 00:46:08 +0200 | [diff] [blame] | 10492 | { |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 10493 | struct http_txn *txn = smp->strm->txn; |
Willy Tarreau | a5910cc | 2015-05-02 00:46:08 +0200 | [diff] [blame] | 10494 | struct http_msg *msg; |
| 10495 | |
| 10496 | CHECK_HTTP_MESSAGE_FIRST(); |
| 10497 | |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10498 | if ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) |
Willy Tarreau | a5910cc | 2015-05-02 00:46:08 +0200 | [diff] [blame] | 10499 | msg = &txn->req; |
| 10500 | else |
| 10501 | msg = &txn->rsp; |
| 10502 | |
Thierry FOURNIER | 07ee64e | 2015-07-06 23:43:03 +0200 | [diff] [blame] | 10503 | smp->type = SMP_T_SINT; |
| 10504 | smp->data.sint = http_body_bytes(msg); |
Willy Tarreau | a5910cc | 2015-05-02 00:46:08 +0200 | [diff] [blame] | 10505 | |
| 10506 | smp->flags = SMP_F_VOL_TEST; |
| 10507 | return 1; |
| 10508 | } |
| 10509 | |
| 10510 | |
| 10511 | /* returns the advertised length of the body, or the advertised size of the |
| 10512 | * chunks available in the buffer. This requires that the body has been waited |
| 10513 | * for using http-buffer-request. |
| 10514 | */ |
| 10515 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10516 | smp_fetch_body_size(const struct arg *args, struct sample *smp, const char *kw, void *private) |
Willy Tarreau | a5910cc | 2015-05-02 00:46:08 +0200 | [diff] [blame] | 10517 | { |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 10518 | struct http_txn *txn = smp->strm->txn; |
Willy Tarreau | a5910cc | 2015-05-02 00:46:08 +0200 | [diff] [blame] | 10519 | struct http_msg *msg; |
| 10520 | |
| 10521 | CHECK_HTTP_MESSAGE_FIRST(); |
| 10522 | |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10523 | if ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) |
Willy Tarreau | a5910cc | 2015-05-02 00:46:08 +0200 | [diff] [blame] | 10524 | msg = &txn->req; |
| 10525 | else |
| 10526 | msg = &txn->rsp; |
| 10527 | |
Thierry FOURNIER | 07ee64e | 2015-07-06 23:43:03 +0200 | [diff] [blame] | 10528 | smp->type = SMP_T_SINT; |
| 10529 | smp->data.sint = msg->body_len; |
Willy Tarreau | a5910cc | 2015-05-02 00:46:08 +0200 | [diff] [blame] | 10530 | |
| 10531 | smp->flags = SMP_F_VOL_TEST; |
| 10532 | return 1; |
| 10533 | } |
| 10534 | |
| 10535 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 10536 | /* 4. Check on URL/URI. A pointer to the URI is stored. */ |
Willy Tarreau | d41f8d8 | 2007-06-10 10:06:18 +0200 | [diff] [blame] | 10537 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10538 | smp_fetch_url(const struct arg *args, struct sample *smp, const char *kw, void *private) |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 10539 | { |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 10540 | struct http_txn *txn; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 10541 | |
Willy Tarreau | c0239e0 | 2012-04-16 14:42:55 +0200 | [diff] [blame] | 10542 | CHECK_HTTP_MESSAGE_FIRST(); |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 10543 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 10544 | txn = smp->strm->txn; |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 10545 | smp->type = SMP_T_STR; |
Willy Tarreau | f853c46 | 2012-04-23 18:53:56 +0200 | [diff] [blame] | 10546 | smp->data.str.len = txn->req.sl.rq.u_l; |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 10547 | smp->data.str.str = txn->req.chn->buf->p + txn->req.sl.rq.u; |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 10548 | smp->flags = SMP_F_VOL_1ST | SMP_F_CONST; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 10549 | return 1; |
| 10550 | } |
| 10551 | |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 10552 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10553 | smp_fetch_url_ip(const struct arg *args, struct sample *smp, const char *kw, void *private) |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 10554 | { |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 10555 | struct http_txn *txn; |
Willy Tarreau | 4c804ec | 2013-09-30 14:37:14 +0200 | [diff] [blame] | 10556 | struct sockaddr_storage addr; |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 10557 | |
Willy Tarreau | c0239e0 | 2012-04-16 14:42:55 +0200 | [diff] [blame] | 10558 | CHECK_HTTP_MESSAGE_FIRST(); |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 10559 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 10560 | txn = smp->strm->txn; |
Thierry FOURNIER | 9f95e40 | 2014-03-21 14:51:46 +0100 | [diff] [blame] | 10561 | url2sa(txn->req.chn->buf->p + txn->req.sl.rq.u, txn->req.sl.rq.u_l, &addr, NULL); |
Willy Tarreau | 4c804ec | 2013-09-30 14:37:14 +0200 | [diff] [blame] | 10562 | if (((struct sockaddr_in *)&addr)->sin_family != AF_INET) |
Willy Tarreau | f4362b3 | 2011-12-16 17:49:52 +0100 | [diff] [blame] | 10563 | return 0; |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 10564 | |
Willy Tarreau | 4c804ec | 2013-09-30 14:37:14 +0200 | [diff] [blame] | 10565 | smp->type = SMP_T_IPV4; |
| 10566 | smp->data.ipv4 = ((struct sockaddr_in *)&addr)->sin_addr; |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 10567 | smp->flags = 0; |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 10568 | return 1; |
| 10569 | } |
| 10570 | |
| 10571 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10572 | smp_fetch_url_port(const struct arg *args, struct sample *smp, const char *kw, void *private) |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 10573 | { |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 10574 | struct http_txn *txn; |
Willy Tarreau | 4c804ec | 2013-09-30 14:37:14 +0200 | [diff] [blame] | 10575 | struct sockaddr_storage addr; |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 10576 | |
Willy Tarreau | c0239e0 | 2012-04-16 14:42:55 +0200 | [diff] [blame] | 10577 | CHECK_HTTP_MESSAGE_FIRST(); |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 10578 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 10579 | txn = smp->strm->txn; |
Thierry FOURNIER | 9f95e40 | 2014-03-21 14:51:46 +0100 | [diff] [blame] | 10580 | url2sa(txn->req.chn->buf->p + txn->req.sl.rq.u, txn->req.sl.rq.u_l, &addr, NULL); |
Willy Tarreau | 4c804ec | 2013-09-30 14:37:14 +0200 | [diff] [blame] | 10581 | if (((struct sockaddr_in *)&addr)->sin_family != AF_INET) |
| 10582 | return 0; |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 10583 | |
Thierry FOURNIER | 07ee64e | 2015-07-06 23:43:03 +0200 | [diff] [blame] | 10584 | smp->type = SMP_T_SINT; |
| 10585 | smp->data.sint = ntohs(((struct sockaddr_in *)&addr)->sin_port); |
Willy Tarreau | 21e5b0e | 2012-04-23 19:25:44 +0200 | [diff] [blame] | 10586 | smp->flags = 0; |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 10587 | return 1; |
| 10588 | } |
| 10589 | |
Willy Tarreau | 185b5c4 | 2012-04-26 15:11:51 +0200 | [diff] [blame] | 10590 | /* Fetch an HTTP header. A pointer to the beginning of the value is returned. |
| 10591 | * Accepts an optional argument of type string containing the header field name, |
| 10592 | * and an optional argument of type signed or unsigned integer to request an |
| 10593 | * explicit occurrence of the header. Note that in the event of a missing name, |
Willy Tarreau | 04ff9f1 | 2013-06-10 18:39:42 +0200 | [diff] [blame] | 10594 | * headers are considered from the first one. It does not stop on commas and |
| 10595 | * returns full lines instead (useful for User-Agent or Date for example). |
| 10596 | */ |
| 10597 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10598 | smp_fetch_fhdr(const struct arg *args, struct sample *smp, const char *kw, void *private) |
Willy Tarreau | 04ff9f1 | 2013-06-10 18:39:42 +0200 | [diff] [blame] | 10599 | { |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 10600 | struct hdr_idx *idx; |
Willy Tarreau | 04ff9f1 | 2013-06-10 18:39:42 +0200 | [diff] [blame] | 10601 | struct hdr_ctx *ctx = smp->ctx.a[0]; |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 10602 | const struct http_msg *msg; |
Willy Tarreau | 04ff9f1 | 2013-06-10 18:39:42 +0200 | [diff] [blame] | 10603 | int occ = 0; |
| 10604 | const char *name_str = NULL; |
| 10605 | int name_len = 0; |
| 10606 | |
| 10607 | if (!ctx) { |
| 10608 | /* first call */ |
| 10609 | ctx = &static_hdr_ctx; |
| 10610 | ctx->idx = 0; |
| 10611 | smp->ctx.a[0] = ctx; |
| 10612 | } |
| 10613 | |
| 10614 | if (args) { |
| 10615 | if (args[0].type != ARGT_STR) |
| 10616 | return 0; |
| 10617 | name_str = args[0].data.str.str; |
| 10618 | name_len = args[0].data.str.len; |
| 10619 | |
Thierry FOURNIER | bf65cd4 | 2015-07-20 17:45:02 +0200 | [diff] [blame] | 10620 | if (args[1].type == ARGT_SINT) |
| 10621 | occ = args[1].data.sint; |
Willy Tarreau | 04ff9f1 | 2013-06-10 18:39:42 +0200 | [diff] [blame] | 10622 | } |
| 10623 | |
| 10624 | CHECK_HTTP_MESSAGE_FIRST(); |
| 10625 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 10626 | idx = &smp->strm->txn->hdr_idx; |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10627 | msg = ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) ? &smp->strm->txn->req : &smp->strm->txn->rsp; |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 10628 | |
Willy Tarreau | 04ff9f1 | 2013-06-10 18:39:42 +0200 | [diff] [blame] | 10629 | if (ctx && !(smp->flags & SMP_F_NOT_LAST)) |
| 10630 | /* search for header from the beginning */ |
| 10631 | ctx->idx = 0; |
| 10632 | |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10633 | if (!occ && !(smp->opt & SMP_OPT_ITERATE)) |
Willy Tarreau | 04ff9f1 | 2013-06-10 18:39:42 +0200 | [diff] [blame] | 10634 | /* no explicit occurrence and single fetch => last header by default */ |
| 10635 | occ = -1; |
| 10636 | |
| 10637 | if (!occ) |
| 10638 | /* prepare to report multiple occurrences for ACL fetches */ |
| 10639 | smp->flags |= SMP_F_NOT_LAST; |
| 10640 | |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 10641 | smp->type = SMP_T_STR; |
| 10642 | smp->flags |= SMP_F_VOL_HDR | SMP_F_CONST; |
Willy Tarreau | 04ff9f1 | 2013-06-10 18:39:42 +0200 | [diff] [blame] | 10643 | if (http_get_fhdr(msg, name_str, name_len, idx, occ, ctx, &smp->data.str.str, &smp->data.str.len)) |
| 10644 | return 1; |
| 10645 | |
| 10646 | smp->flags &= ~SMP_F_NOT_LAST; |
| 10647 | return 0; |
| 10648 | } |
| 10649 | |
| 10650 | /* 6. Check on HTTP header count. The number of occurrences is returned. |
| 10651 | * Accepts exactly 1 argument of type string. It does not stop on commas and |
| 10652 | * returns full lines instead (useful for User-Agent or Date for example). |
| 10653 | */ |
| 10654 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10655 | smp_fetch_fhdr_cnt(const struct arg *args, struct sample *smp, const char *kw, void *private) |
Willy Tarreau | 04ff9f1 | 2013-06-10 18:39:42 +0200 | [diff] [blame] | 10656 | { |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 10657 | struct hdr_idx *idx; |
Willy Tarreau | 04ff9f1 | 2013-06-10 18:39:42 +0200 | [diff] [blame] | 10658 | struct hdr_ctx ctx; |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 10659 | const struct http_msg *msg; |
Willy Tarreau | 04ff9f1 | 2013-06-10 18:39:42 +0200 | [diff] [blame] | 10660 | int cnt; |
Willy Tarreau | 601a4d1 | 2015-04-01 19:16:09 +0200 | [diff] [blame] | 10661 | const char *name = NULL; |
| 10662 | int len = 0; |
Willy Tarreau | 04ff9f1 | 2013-06-10 18:39:42 +0200 | [diff] [blame] | 10663 | |
Willy Tarreau | 601a4d1 | 2015-04-01 19:16:09 +0200 | [diff] [blame] | 10664 | if (args && args->type == ARGT_STR) { |
| 10665 | name = args->data.str.str; |
| 10666 | len = args->data.str.len; |
| 10667 | } |
Willy Tarreau | 04ff9f1 | 2013-06-10 18:39:42 +0200 | [diff] [blame] | 10668 | |
| 10669 | CHECK_HTTP_MESSAGE_FIRST(); |
| 10670 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 10671 | idx = &smp->strm->txn->hdr_idx; |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10672 | msg = ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) ? &smp->strm->txn->req : &smp->strm->txn->rsp; |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 10673 | |
Willy Tarreau | 04ff9f1 | 2013-06-10 18:39:42 +0200 | [diff] [blame] | 10674 | ctx.idx = 0; |
| 10675 | cnt = 0; |
Willy Tarreau | 601a4d1 | 2015-04-01 19:16:09 +0200 | [diff] [blame] | 10676 | while (http_find_full_header2(name, len, msg->chn->buf->p, idx, &ctx)) |
Willy Tarreau | 04ff9f1 | 2013-06-10 18:39:42 +0200 | [diff] [blame] | 10677 | cnt++; |
| 10678 | |
Thierry FOURNIER | 07ee64e | 2015-07-06 23:43:03 +0200 | [diff] [blame] | 10679 | smp->type = SMP_T_SINT; |
| 10680 | smp->data.sint = cnt; |
Willy Tarreau | 04ff9f1 | 2013-06-10 18:39:42 +0200 | [diff] [blame] | 10681 | smp->flags = SMP_F_VOL_HDR; |
| 10682 | return 1; |
| 10683 | } |
| 10684 | |
Willy Tarreau | eb27ec7 | 2015-02-20 13:55:29 +0100 | [diff] [blame] | 10685 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10686 | smp_fetch_hdr_names(const struct arg *args, struct sample *smp, const char *kw, void *private) |
Willy Tarreau | eb27ec7 | 2015-02-20 13:55:29 +0100 | [diff] [blame] | 10687 | { |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 10688 | struct hdr_idx *idx; |
Willy Tarreau | eb27ec7 | 2015-02-20 13:55:29 +0100 | [diff] [blame] | 10689 | struct hdr_ctx ctx; |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 10690 | const struct http_msg *msg; |
Willy Tarreau | eb27ec7 | 2015-02-20 13:55:29 +0100 | [diff] [blame] | 10691 | struct chunk *temp; |
| 10692 | char del = ','; |
| 10693 | |
| 10694 | if (args && args->type == ARGT_STR) |
| 10695 | del = *args[0].data.str.str; |
| 10696 | |
| 10697 | CHECK_HTTP_MESSAGE_FIRST(); |
| 10698 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 10699 | idx = &smp->strm->txn->hdr_idx; |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10700 | msg = ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) ? &smp->strm->txn->req : &smp->strm->txn->rsp; |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 10701 | |
Willy Tarreau | eb27ec7 | 2015-02-20 13:55:29 +0100 | [diff] [blame] | 10702 | temp = get_trash_chunk(); |
| 10703 | |
| 10704 | ctx.idx = 0; |
| 10705 | while (http_find_next_header(msg->chn->buf->p, idx, &ctx)) { |
| 10706 | if (temp->len) |
| 10707 | temp->str[temp->len++] = del; |
| 10708 | memcpy(temp->str + temp->len, ctx.line, ctx.del); |
| 10709 | temp->len += ctx.del; |
| 10710 | } |
| 10711 | |
| 10712 | smp->type = SMP_T_STR; |
| 10713 | smp->data.str.str = temp->str; |
| 10714 | smp->data.str.len = temp->len; |
| 10715 | smp->flags = SMP_F_VOL_HDR; |
| 10716 | return 1; |
| 10717 | } |
| 10718 | |
Willy Tarreau | 04ff9f1 | 2013-06-10 18:39:42 +0200 | [diff] [blame] | 10719 | /* Fetch an HTTP header. A pointer to the beginning of the value is returned. |
| 10720 | * Accepts an optional argument of type string containing the header field name, |
| 10721 | * and an optional argument of type signed or unsigned integer to request an |
| 10722 | * explicit occurrence of the header. Note that in the event of a missing name, |
Willy Tarreau | 185b5c4 | 2012-04-26 15:11:51 +0200 | [diff] [blame] | 10723 | * headers are considered from the first one. |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 10724 | */ |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 10725 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10726 | smp_fetch_hdr(const struct arg *args, struct sample *smp, const char *kw, void *private) |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 10727 | { |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 10728 | struct hdr_idx *idx; |
Willy Tarreau | a890d07 | 2013-04-02 12:01:06 +0200 | [diff] [blame] | 10729 | struct hdr_ctx *ctx = smp->ctx.a[0]; |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 10730 | const struct http_msg *msg; |
Willy Tarreau | 185b5c4 | 2012-04-26 15:11:51 +0200 | [diff] [blame] | 10731 | int occ = 0; |
| 10732 | const char *name_str = NULL; |
| 10733 | int name_len = 0; |
Willy Tarreau | e333ec9 | 2012-04-16 16:26:40 +0200 | [diff] [blame] | 10734 | |
Willy Tarreau | a890d07 | 2013-04-02 12:01:06 +0200 | [diff] [blame] | 10735 | if (!ctx) { |
| 10736 | /* first call */ |
| 10737 | ctx = &static_hdr_ctx; |
| 10738 | ctx->idx = 0; |
Willy Tarreau | ffb6f08 | 2013-04-02 23:16:53 +0200 | [diff] [blame] | 10739 | smp->ctx.a[0] = ctx; |
Willy Tarreau | a890d07 | 2013-04-02 12:01:06 +0200 | [diff] [blame] | 10740 | } |
| 10741 | |
Willy Tarreau | 185b5c4 | 2012-04-26 15:11:51 +0200 | [diff] [blame] | 10742 | if (args) { |
| 10743 | if (args[0].type != ARGT_STR) |
| 10744 | return 0; |
| 10745 | name_str = args[0].data.str.str; |
| 10746 | name_len = args[0].data.str.len; |
| 10747 | |
Thierry FOURNIER | bf65cd4 | 2015-07-20 17:45:02 +0200 | [diff] [blame] | 10748 | if (args[1].type == ARGT_SINT) |
| 10749 | occ = args[1].data.sint; |
Willy Tarreau | 185b5c4 | 2012-04-26 15:11:51 +0200 | [diff] [blame] | 10750 | } |
Willy Tarreau | 34db108 | 2012-04-19 17:16:54 +0200 | [diff] [blame] | 10751 | |
Willy Tarreau | e333ec9 | 2012-04-16 16:26:40 +0200 | [diff] [blame] | 10752 | CHECK_HTTP_MESSAGE_FIRST(); |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 10753 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 10754 | idx = &smp->strm->txn->hdr_idx; |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10755 | msg = ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) ? &smp->strm->txn->req : &smp->strm->txn->rsp; |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 10756 | |
Willy Tarreau | 185b5c4 | 2012-04-26 15:11:51 +0200 | [diff] [blame] | 10757 | if (ctx && !(smp->flags & SMP_F_NOT_LAST)) |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 10758 | /* search for header from the beginning */ |
| 10759 | ctx->idx = 0; |
| 10760 | |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10761 | if (!occ && !(smp->opt & SMP_OPT_ITERATE)) |
Willy Tarreau | 185b5c4 | 2012-04-26 15:11:51 +0200 | [diff] [blame] | 10762 | /* no explicit occurrence and single fetch => last header by default */ |
| 10763 | occ = -1; |
| 10764 | |
| 10765 | if (!occ) |
| 10766 | /* prepare to report multiple occurrences for ACL fetches */ |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 10767 | smp->flags |= SMP_F_NOT_LAST; |
Willy Tarreau | 664092c | 2011-12-16 19:11:42 +0100 | [diff] [blame] | 10768 | |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 10769 | smp->type = SMP_T_STR; |
| 10770 | smp->flags |= SMP_F_VOL_HDR | SMP_F_CONST; |
Willy Tarreau | 185b5c4 | 2012-04-26 15:11:51 +0200 | [diff] [blame] | 10771 | if (http_get_hdr(msg, name_str, name_len, idx, occ, ctx, &smp->data.str.str, &smp->data.str.len)) |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 10772 | return 1; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 10773 | |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 10774 | smp->flags &= ~SMP_F_NOT_LAST; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 10775 | return 0; |
| 10776 | } |
| 10777 | |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 10778 | /* 6. Check on HTTP header count. The number of occurrences is returned. |
Willy Tarreau | 34db108 | 2012-04-19 17:16:54 +0200 | [diff] [blame] | 10779 | * Accepts exactly 1 argument of type string. |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 10780 | */ |
| 10781 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10782 | smp_fetch_hdr_cnt(const struct arg *args, struct sample *smp, const char *kw, void *private) |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 10783 | { |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 10784 | struct hdr_idx *idx; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 10785 | struct hdr_ctx ctx; |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 10786 | const struct http_msg *msg; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 10787 | int cnt; |
Willy Tarreau | 601a4d1 | 2015-04-01 19:16:09 +0200 | [diff] [blame] | 10788 | const char *name = NULL; |
| 10789 | int len = 0; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 10790 | |
Willy Tarreau | 601a4d1 | 2015-04-01 19:16:09 +0200 | [diff] [blame] | 10791 | if (args && args->type == ARGT_STR) { |
| 10792 | name = args->data.str.str; |
| 10793 | len = args->data.str.len; |
| 10794 | } |
Willy Tarreau | 34db108 | 2012-04-19 17:16:54 +0200 | [diff] [blame] | 10795 | |
Willy Tarreau | e333ec9 | 2012-04-16 16:26:40 +0200 | [diff] [blame] | 10796 | CHECK_HTTP_MESSAGE_FIRST(); |
| 10797 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 10798 | idx = &smp->strm->txn->hdr_idx; |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10799 | msg = ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) ? &smp->strm->txn->req : &smp->strm->txn->rsp; |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 10800 | |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 10801 | ctx.idx = 0; |
| 10802 | cnt = 0; |
Willy Tarreau | 601a4d1 | 2015-04-01 19:16:09 +0200 | [diff] [blame] | 10803 | while (http_find_header2(name, len, msg->chn->buf->p, idx, &ctx)) |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 10804 | cnt++; |
| 10805 | |
Thierry FOURNIER | 07ee64e | 2015-07-06 23:43:03 +0200 | [diff] [blame] | 10806 | smp->type = SMP_T_SINT; |
| 10807 | smp->data.sint = cnt; |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 10808 | smp->flags = SMP_F_VOL_HDR; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 10809 | return 1; |
| 10810 | } |
| 10811 | |
Willy Tarreau | 185b5c4 | 2012-04-26 15:11:51 +0200 | [diff] [blame] | 10812 | /* Fetch an HTTP header's integer value. The integer value is returned. It |
| 10813 | * takes a mandatory argument of type string and an optional one of type int |
| 10814 | * to designate a specific occurrence. It returns an unsigned integer, which |
| 10815 | * may or may not be appropriate for everything. |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 10816 | */ |
| 10817 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10818 | smp_fetch_hdr_val(const struct arg *args, struct sample *smp, const char *kw, void *private) |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 10819 | { |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10820 | int ret = smp_fetch_hdr(args, smp, kw, private); |
Willy Tarreau | e333ec9 | 2012-04-16 16:26:40 +0200 | [diff] [blame] | 10821 | |
Willy Tarreau | f853c46 | 2012-04-23 18:53:56 +0200 | [diff] [blame] | 10822 | if (ret > 0) { |
Thierry FOURNIER | 07ee64e | 2015-07-06 23:43:03 +0200 | [diff] [blame] | 10823 | smp->type = SMP_T_SINT; |
| 10824 | smp->data.sint = strl2ic(smp->data.str.str, smp->data.str.len); |
Willy Tarreau | f853c46 | 2012-04-23 18:53:56 +0200 | [diff] [blame] | 10825 | } |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 10826 | |
Willy Tarreau | d53e242 | 2012-04-16 17:21:11 +0200 | [diff] [blame] | 10827 | return ret; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 10828 | } |
| 10829 | |
Cyril Bonté | 69fa992 | 2012-10-25 00:01:06 +0200 | [diff] [blame] | 10830 | /* Fetch an HTTP header's IP value. takes a mandatory argument of type string |
| 10831 | * and an optional one of type int to designate a specific occurrence. |
| 10832 | * It returns an IPv4 or IPv6 address. |
Willy Tarreau | 106f979 | 2009-09-19 07:54:16 +0200 | [diff] [blame] | 10833 | */ |
| 10834 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10835 | smp_fetch_hdr_ip(const struct arg *args, struct sample *smp, const char *kw, void *private) |
Willy Tarreau | 106f979 | 2009-09-19 07:54:16 +0200 | [diff] [blame] | 10836 | { |
Willy Tarreau | d53e242 | 2012-04-16 17:21:11 +0200 | [diff] [blame] | 10837 | int ret; |
Willy Tarreau | e333ec9 | 2012-04-16 16:26:40 +0200 | [diff] [blame] | 10838 | |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10839 | while ((ret = smp_fetch_hdr(args, smp, kw, private)) > 0) { |
Cyril Bonté | 69fa992 | 2012-10-25 00:01:06 +0200 | [diff] [blame] | 10840 | if (url2ipv4((char *)smp->data.str.str, &smp->data.ipv4)) { |
| 10841 | smp->type = SMP_T_IPV4; |
Willy Tarreau | d53e242 | 2012-04-16 17:21:11 +0200 | [diff] [blame] | 10842 | break; |
Cyril Bonté | 69fa992 | 2012-10-25 00:01:06 +0200 | [diff] [blame] | 10843 | } else { |
Willy Tarreau | 47ca545 | 2012-12-23 20:22:19 +0100 | [diff] [blame] | 10844 | struct chunk *temp = get_trash_chunk(); |
Cyril Bonté | 69fa992 | 2012-10-25 00:01:06 +0200 | [diff] [blame] | 10845 | if (smp->data.str.len < temp->size - 1) { |
| 10846 | memcpy(temp->str, smp->data.str.str, smp->data.str.len); |
| 10847 | temp->str[smp->data.str.len] = '\0'; |
| 10848 | if (inet_pton(AF_INET6, temp->str, &smp->data.ipv6)) { |
| 10849 | smp->type = SMP_T_IPV6; |
| 10850 | break; |
| 10851 | } |
| 10852 | } |
| 10853 | } |
| 10854 | |
Willy Tarreau | d53e242 | 2012-04-16 17:21:11 +0200 | [diff] [blame] | 10855 | /* if the header doesn't match an IP address, fetch next one */ |
Willy Tarreau | 185b5c4 | 2012-04-26 15:11:51 +0200 | [diff] [blame] | 10856 | if (!(smp->flags & SMP_F_NOT_LAST)) |
| 10857 | return 0; |
Willy Tarreau | 106f979 | 2009-09-19 07:54:16 +0200 | [diff] [blame] | 10858 | } |
Willy Tarreau | d53e242 | 2012-04-16 17:21:11 +0200 | [diff] [blame] | 10859 | return ret; |
Willy Tarreau | 106f979 | 2009-09-19 07:54:16 +0200 | [diff] [blame] | 10860 | } |
| 10861 | |
Willy Tarreau | 737b0c1 | 2007-06-10 21:28:46 +0200 | [diff] [blame] | 10862 | /* 8. Check on URI PATH. A pointer to the PATH is stored. The path starts at |
| 10863 | * the first '/' after the possible hostname, and ends before the possible '?'. |
| 10864 | */ |
| 10865 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10866 | smp_fetch_path(const struct arg *args, struct sample *smp, const char *kw, void *private) |
Willy Tarreau | 737b0c1 | 2007-06-10 21:28:46 +0200 | [diff] [blame] | 10867 | { |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 10868 | struct http_txn *txn; |
Willy Tarreau | 737b0c1 | 2007-06-10 21:28:46 +0200 | [diff] [blame] | 10869 | char *ptr, *end; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 10870 | |
Willy Tarreau | c0239e0 | 2012-04-16 14:42:55 +0200 | [diff] [blame] | 10871 | CHECK_HTTP_MESSAGE_FIRST(); |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 10872 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 10873 | txn = smp->strm->txn; |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 10874 | end = txn->req.chn->buf->p + txn->req.sl.rq.u + txn->req.sl.rq.u_l; |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 10875 | ptr = http_get_path(txn); |
| 10876 | if (!ptr) |
Willy Tarreau | 737b0c1 | 2007-06-10 21:28:46 +0200 | [diff] [blame] | 10877 | return 0; |
| 10878 | |
| 10879 | /* OK, we got the '/' ! */ |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 10880 | smp->type = SMP_T_STR; |
Willy Tarreau | f853c46 | 2012-04-23 18:53:56 +0200 | [diff] [blame] | 10881 | smp->data.str.str = ptr; |
Willy Tarreau | 737b0c1 | 2007-06-10 21:28:46 +0200 | [diff] [blame] | 10882 | |
| 10883 | while (ptr < end && *ptr != '?') |
| 10884 | ptr++; |
| 10885 | |
Willy Tarreau | f853c46 | 2012-04-23 18:53:56 +0200 | [diff] [blame] | 10886 | smp->data.str.len = ptr - smp->data.str.str; |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 10887 | smp->flags = SMP_F_VOL_1ST | SMP_F_CONST; |
Willy Tarreau | 737b0c1 | 2007-06-10 21:28:46 +0200 | [diff] [blame] | 10888 | return 1; |
| 10889 | } |
| 10890 | |
Willy Tarreau | a7ad50c | 2012-04-29 15:39:40 +0200 | [diff] [blame] | 10891 | /* This produces a concatenation of the first occurrence of the Host header |
| 10892 | * followed by the path component if it begins with a slash ('/'). This means |
| 10893 | * that '*' will not be added, resulting in exactly the first Host entry. |
| 10894 | * If no Host header is found, then the path is returned as-is. The returned |
| 10895 | * value is stored in the trash so it does not need to be marked constant. |
Willy Tarreau | b169eba | 2013-12-16 15:14:43 +0100 | [diff] [blame] | 10896 | * The returned sample is of type string. |
Willy Tarreau | a7ad50c | 2012-04-29 15:39:40 +0200 | [diff] [blame] | 10897 | */ |
| 10898 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10899 | smp_fetch_base(const struct arg *args, struct sample *smp, const char *kw, void *private) |
Willy Tarreau | a7ad50c | 2012-04-29 15:39:40 +0200 | [diff] [blame] | 10900 | { |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 10901 | struct http_txn *txn; |
Willy Tarreau | a7ad50c | 2012-04-29 15:39:40 +0200 | [diff] [blame] | 10902 | char *ptr, *end, *beg; |
| 10903 | struct hdr_ctx ctx; |
Willy Tarreau | 3caf2af | 2014-06-24 17:27:02 +0200 | [diff] [blame] | 10904 | struct chunk *temp; |
Willy Tarreau | a7ad50c | 2012-04-29 15:39:40 +0200 | [diff] [blame] | 10905 | |
| 10906 | CHECK_HTTP_MESSAGE_FIRST(); |
| 10907 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 10908 | txn = smp->strm->txn; |
Willy Tarreau | a7ad50c | 2012-04-29 15:39:40 +0200 | [diff] [blame] | 10909 | ctx.idx = 0; |
Willy Tarreau | 877e78d | 2013-04-07 18:48:08 +0200 | [diff] [blame] | 10910 | if (!http_find_header2("Host", 4, txn->req.chn->buf->p, &txn->hdr_idx, &ctx) || !ctx.vlen) |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10911 | return smp_fetch_path(args, smp, kw, private); |
Willy Tarreau | a7ad50c | 2012-04-29 15:39:40 +0200 | [diff] [blame] | 10912 | |
| 10913 | /* OK we have the header value in ctx.line+ctx.val for ctx.vlen bytes */ |
Willy Tarreau | 3caf2af | 2014-06-24 17:27:02 +0200 | [diff] [blame] | 10914 | temp = get_trash_chunk(); |
| 10915 | memcpy(temp->str, ctx.line + ctx.val, ctx.vlen); |
Willy Tarreau | a7ad50c | 2012-04-29 15:39:40 +0200 | [diff] [blame] | 10916 | smp->type = SMP_T_STR; |
Willy Tarreau | 3caf2af | 2014-06-24 17:27:02 +0200 | [diff] [blame] | 10917 | smp->data.str.str = temp->str; |
Willy Tarreau | a7ad50c | 2012-04-29 15:39:40 +0200 | [diff] [blame] | 10918 | smp->data.str.len = ctx.vlen; |
| 10919 | |
| 10920 | /* now retrieve the path */ |
Willy Tarreau | 877e78d | 2013-04-07 18:48:08 +0200 | [diff] [blame] | 10921 | end = txn->req.chn->buf->p + txn->req.sl.rq.u + txn->req.sl.rq.u_l; |
Willy Tarreau | a7ad50c | 2012-04-29 15:39:40 +0200 | [diff] [blame] | 10922 | beg = http_get_path(txn); |
| 10923 | if (!beg) |
| 10924 | beg = end; |
| 10925 | |
| 10926 | for (ptr = beg; ptr < end && *ptr != '?'; ptr++); |
| 10927 | |
| 10928 | if (beg < ptr && *beg == '/') { |
| 10929 | memcpy(smp->data.str.str + smp->data.str.len, beg, ptr - beg); |
| 10930 | smp->data.str.len += ptr - beg; |
| 10931 | } |
| 10932 | |
| 10933 | smp->flags = SMP_F_VOL_1ST; |
| 10934 | return 1; |
| 10935 | } |
| 10936 | |
Willy Tarreau | ab1f7b7 | 2012-12-09 13:38:54 +0100 | [diff] [blame] | 10937 | /* This produces a 32-bit hash of the concatenation of the first occurrence of |
| 10938 | * the Host header followed by the path component if it begins with a slash ('/'). |
| 10939 | * This means that '*' will not be added, resulting in exactly the first Host |
| 10940 | * entry. If no Host header is found, then the path is used. The resulting value |
Neil - HAProxy List | 39c63c5 | 2013-11-04 13:48:42 +0000 | [diff] [blame] | 10941 | * is hashed using the path hash followed by a full avalanche hash and provides a |
| 10942 | * 32-bit integer value. This fetch is useful for tracking per-path activity on |
Willy Tarreau | ab1f7b7 | 2012-12-09 13:38:54 +0100 | [diff] [blame] | 10943 | * high-traffic sites without having to store whole paths. |
| 10944 | */ |
Thierry FOURNIER | 055b9d5 | 2014-07-15 16:11:07 +0200 | [diff] [blame] | 10945 | int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10946 | smp_fetch_base32(const struct arg *args, struct sample *smp, const char *kw, void *private) |
Willy Tarreau | ab1f7b7 | 2012-12-09 13:38:54 +0100 | [diff] [blame] | 10947 | { |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 10948 | struct http_txn *txn; |
Willy Tarreau | ab1f7b7 | 2012-12-09 13:38:54 +0100 | [diff] [blame] | 10949 | struct hdr_ctx ctx; |
| 10950 | unsigned int hash = 0; |
| 10951 | char *ptr, *beg, *end; |
| 10952 | int len; |
| 10953 | |
| 10954 | CHECK_HTTP_MESSAGE_FIRST(); |
| 10955 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 10956 | txn = smp->strm->txn; |
Willy Tarreau | ab1f7b7 | 2012-12-09 13:38:54 +0100 | [diff] [blame] | 10957 | ctx.idx = 0; |
Willy Tarreau | 877e78d | 2013-04-07 18:48:08 +0200 | [diff] [blame] | 10958 | if (http_find_header2("Host", 4, txn->req.chn->buf->p, &txn->hdr_idx, &ctx)) { |
Willy Tarreau | ab1f7b7 | 2012-12-09 13:38:54 +0100 | [diff] [blame] | 10959 | /* OK we have the header value in ctx.line+ctx.val for ctx.vlen bytes */ |
| 10960 | ptr = ctx.line + ctx.val; |
| 10961 | len = ctx.vlen; |
| 10962 | while (len--) |
| 10963 | hash = *(ptr++) + (hash << 6) + (hash << 16) - hash; |
| 10964 | } |
| 10965 | |
| 10966 | /* now retrieve the path */ |
Willy Tarreau | 877e78d | 2013-04-07 18:48:08 +0200 | [diff] [blame] | 10967 | end = txn->req.chn->buf->p + txn->req.sl.rq.u + txn->req.sl.rq.u_l; |
Willy Tarreau | ab1f7b7 | 2012-12-09 13:38:54 +0100 | [diff] [blame] | 10968 | beg = http_get_path(txn); |
| 10969 | if (!beg) |
| 10970 | beg = end; |
| 10971 | |
| 10972 | for (ptr = beg; ptr < end && *ptr != '?'; ptr++); |
| 10973 | |
| 10974 | if (beg < ptr && *beg == '/') { |
| 10975 | while (beg < ptr) |
| 10976 | hash = *(beg++) + (hash << 6) + (hash << 16) - hash; |
| 10977 | } |
| 10978 | hash = full_hash(hash); |
| 10979 | |
Thierry FOURNIER | 07ee64e | 2015-07-06 23:43:03 +0200 | [diff] [blame] | 10980 | smp->type = SMP_T_SINT; |
| 10981 | smp->data.sint = hash; |
Willy Tarreau | ab1f7b7 | 2012-12-09 13:38:54 +0100 | [diff] [blame] | 10982 | smp->flags = SMP_F_VOL_1ST; |
| 10983 | return 1; |
| 10984 | } |
| 10985 | |
Willy Tarreau | 4a55060 | 2012-12-09 14:53:32 +0100 | [diff] [blame] | 10986 | /* This concatenates the source address with the 32-bit hash of the Host and |
Neil - HAProxy List | 39c63c5 | 2013-11-04 13:48:42 +0000 | [diff] [blame] | 10987 | * path as returned by smp_fetch_base32(). The idea is to have per-source and |
| 10988 | * per-path counters. The result is a binary block from 8 to 20 bytes depending |
| 10989 | * on the source address length. The path hash is stored before the address so |
Willy Tarreau | 4a55060 | 2012-12-09 14:53:32 +0100 | [diff] [blame] | 10990 | * that in environments where IPv6 is insignificant, truncating the output to |
| 10991 | * 8 bytes would still work. |
| 10992 | */ |
| 10993 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10994 | smp_fetch_base32_src(const struct arg *args, struct sample *smp, const char *kw, void *private) |
Willy Tarreau | 4a55060 | 2012-12-09 14:53:32 +0100 | [diff] [blame] | 10995 | { |
Willy Tarreau | 47ca545 | 2012-12-23 20:22:19 +0100 | [diff] [blame] | 10996 | struct chunk *temp; |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 10997 | struct connection *cli_conn = objt_conn(smp->sess->origin); |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 10998 | |
| 10999 | if (!cli_conn) |
| 11000 | return 0; |
Willy Tarreau | 4a55060 | 2012-12-09 14:53:32 +0100 | [diff] [blame] | 11001 | |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 11002 | if (!smp_fetch_base32(args, smp, kw, private)) |
Willy Tarreau | 4a55060 | 2012-12-09 14:53:32 +0100 | [diff] [blame] | 11003 | return 0; |
| 11004 | |
Willy Tarreau | 47ca545 | 2012-12-23 20:22:19 +0100 | [diff] [blame] | 11005 | temp = get_trash_chunk(); |
Thierry FOURNIER | 07ee64e | 2015-07-06 23:43:03 +0200 | [diff] [blame] | 11006 | *(unsigned int *)temp->str = htonl(smp->data.sint); |
Willy Tarreau | 5ad6e1d | 2014-07-15 21:34:06 +0200 | [diff] [blame] | 11007 | temp->len += sizeof(unsigned int); |
Willy Tarreau | 4a55060 | 2012-12-09 14:53:32 +0100 | [diff] [blame] | 11008 | |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 11009 | switch (cli_conn->addr.from.ss_family) { |
Willy Tarreau | 4a55060 | 2012-12-09 14:53:32 +0100 | [diff] [blame] | 11010 | case AF_INET: |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 11011 | memcpy(temp->str + temp->len, &((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr, 4); |
Willy Tarreau | 4a55060 | 2012-12-09 14:53:32 +0100 | [diff] [blame] | 11012 | temp->len += 4; |
| 11013 | break; |
| 11014 | case AF_INET6: |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 11015 | memcpy(temp->str + temp->len, &((struct sockaddr_in6 *)&cli_conn->addr.from)->sin6_addr, 16); |
Willy Tarreau | 4a55060 | 2012-12-09 14:53:32 +0100 | [diff] [blame] | 11016 | temp->len += 16; |
| 11017 | break; |
| 11018 | default: |
| 11019 | return 0; |
| 11020 | } |
| 11021 | |
| 11022 | smp->data.str = *temp; |
| 11023 | smp->type = SMP_T_BIN; |
| 11024 | return 1; |
| 11025 | } |
| 11026 | |
Willy Tarreau | 49ad95c | 2015-01-19 15:06:26 +0100 | [diff] [blame] | 11027 | /* Extracts the query string, which comes after the question mark '?'. If no |
| 11028 | * question mark is found, nothing is returned. Otherwise it returns a sample |
| 11029 | * of type string carrying the whole query string. |
| 11030 | */ |
| 11031 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 11032 | smp_fetch_query(const struct arg *args, struct sample *smp, const char *kw, void *private) |
Willy Tarreau | 49ad95c | 2015-01-19 15:06:26 +0100 | [diff] [blame] | 11033 | { |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 11034 | struct http_txn *txn; |
Willy Tarreau | 49ad95c | 2015-01-19 15:06:26 +0100 | [diff] [blame] | 11035 | char *ptr, *end; |
| 11036 | |
| 11037 | CHECK_HTTP_MESSAGE_FIRST(); |
| 11038 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 11039 | txn = smp->strm->txn; |
Willy Tarreau | 49ad95c | 2015-01-19 15:06:26 +0100 | [diff] [blame] | 11040 | ptr = txn->req.chn->buf->p + txn->req.sl.rq.u; |
| 11041 | end = ptr + txn->req.sl.rq.u_l; |
| 11042 | |
| 11043 | /* look up the '?' */ |
| 11044 | do { |
| 11045 | if (ptr == end) |
| 11046 | return 0; |
| 11047 | } while (*ptr++ != '?'); |
| 11048 | |
| 11049 | smp->type = SMP_T_STR; |
| 11050 | smp->data.str.str = ptr; |
| 11051 | smp->data.str.len = end - ptr; |
| 11052 | smp->flags = SMP_F_VOL_1ST | SMP_F_CONST; |
| 11053 | return 1; |
| 11054 | } |
| 11055 | |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 11056 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 11057 | smp_fetch_proto_http(const struct arg *args, struct sample *smp, const char *kw, void *private) |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 11058 | { |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 11059 | /* Note: hdr_idx.v cannot be NULL in this ACL because the ACL is tagged |
| 11060 | * as a layer7 ACL, which involves automatic allocation of hdr_idx. |
| 11061 | */ |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 11062 | |
Willy Tarreau | 24e32d8 | 2012-04-23 23:55:44 +0200 | [diff] [blame] | 11063 | CHECK_HTTP_MESSAGE_FIRST_PERM(); |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 11064 | |
Willy Tarreau | f853c46 | 2012-04-23 18:53:56 +0200 | [diff] [blame] | 11065 | smp->type = SMP_T_BOOL; |
Thierry FOURNIER | 07ee64e | 2015-07-06 23:43:03 +0200 | [diff] [blame] | 11066 | smp->data.sint = 1; |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 11067 | return 1; |
| 11068 | } |
| 11069 | |
Willy Tarreau | 7f18e52 | 2010-10-22 20:04:13 +0200 | [diff] [blame] | 11070 | /* return a valid test if the current request is the first one on the connection */ |
| 11071 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 11072 | smp_fetch_http_first_req(const struct arg *args, struct sample *smp, const char *kw, void *private) |
Willy Tarreau | 7f18e52 | 2010-10-22 20:04:13 +0200 | [diff] [blame] | 11073 | { |
Willy Tarreau | f853c46 | 2012-04-23 18:53:56 +0200 | [diff] [blame] | 11074 | smp->type = SMP_T_BOOL; |
Thierry FOURNIER | 07ee64e | 2015-07-06 23:43:03 +0200 | [diff] [blame] | 11075 | smp->data.sint = !(smp->strm->txn->flags & TX_NOT_FIRST); |
Willy Tarreau | 7f18e52 | 2010-10-22 20:04:13 +0200 | [diff] [blame] | 11076 | return 1; |
| 11077 | } |
| 11078 | |
Willy Tarreau | 34db108 | 2012-04-19 17:16:54 +0200 | [diff] [blame] | 11079 | /* Accepts exactly 1 argument of type userlist */ |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 11080 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 11081 | smp_fetch_http_auth(const struct arg *args, struct sample *smp, const char *kw, void *private) |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 11082 | { |
| 11083 | |
Willy Tarreau | 24e32d8 | 2012-04-23 23:55:44 +0200 | [diff] [blame] | 11084 | if (!args || args->type != ARGT_USR) |
Willy Tarreau | 34db108 | 2012-04-19 17:16:54 +0200 | [diff] [blame] | 11085 | return 0; |
| 11086 | |
Willy Tarreau | c0239e0 | 2012-04-16 14:42:55 +0200 | [diff] [blame] | 11087 | CHECK_HTTP_MESSAGE_FIRST(); |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 11088 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 11089 | if (!get_http_auth(smp->strm)) |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 11090 | return 0; |
| 11091 | |
Willy Tarreau | f853c46 | 2012-04-23 18:53:56 +0200 | [diff] [blame] | 11092 | smp->type = SMP_T_BOOL; |
Thierry FOURNIER | 07ee64e | 2015-07-06 23:43:03 +0200 | [diff] [blame] | 11093 | smp->data.sint = check_user(args->data.usr, smp->strm->txn->auth.user, |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 11094 | smp->strm->txn->auth.pass); |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 11095 | return 1; |
| 11096 | } |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 11097 | |
Willy Tarreau | 4a3fd4c | 2012-05-10 23:18:26 +0200 | [diff] [blame] | 11098 | /* Accepts exactly 1 argument of type userlist */ |
| 11099 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 11100 | smp_fetch_http_auth_grp(const struct arg *args, struct sample *smp, const char *kw, void *private) |
Willy Tarreau | 4a3fd4c | 2012-05-10 23:18:26 +0200 | [diff] [blame] | 11101 | { |
Willy Tarreau | 4a3fd4c | 2012-05-10 23:18:26 +0200 | [diff] [blame] | 11102 | if (!args || args->type != ARGT_USR) |
| 11103 | return 0; |
| 11104 | |
| 11105 | CHECK_HTTP_MESSAGE_FIRST(); |
| 11106 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 11107 | if (!get_http_auth(smp->strm)) |
Willy Tarreau | 4a3fd4c | 2012-05-10 23:18:26 +0200 | [diff] [blame] | 11108 | return 0; |
| 11109 | |
Willy Tarreau | 4a3fd4c | 2012-05-10 23:18:26 +0200 | [diff] [blame] | 11110 | /* if the user does not belong to the userlist or has a wrong password, |
| 11111 | * report that it unconditionally does not match. Otherwise we return |
Thierry FOURNIER | 9eec0a6 | 2014-01-22 18:38:02 +0100 | [diff] [blame] | 11112 | * a string containing the username. |
Willy Tarreau | 4a3fd4c | 2012-05-10 23:18:26 +0200 | [diff] [blame] | 11113 | */ |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 11114 | if (!check_user(args->data.usr, smp->strm->txn->auth.user, |
| 11115 | smp->strm->txn->auth.pass)) |
Thierry FOURNIER | 9eec0a6 | 2014-01-22 18:38:02 +0100 | [diff] [blame] | 11116 | return 0; |
| 11117 | |
| 11118 | /* pat_match_auth() will need the user list */ |
| 11119 | smp->ctx.a[0] = args->data.usr; |
| 11120 | |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 11121 | smp->type = SMP_T_STR; |
| 11122 | smp->flags = SMP_F_CONST; |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 11123 | smp->data.str.str = smp->strm->txn->auth.user; |
| 11124 | smp->data.str.len = strlen(smp->strm->txn->auth.user); |
Willy Tarreau | 4a3fd4c | 2012-05-10 23:18:26 +0200 | [diff] [blame] | 11125 | |
| 11126 | return 1; |
| 11127 | } |
| 11128 | |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 11129 | /* Try to find the next occurrence of a cookie name in a cookie header value. |
| 11130 | * The lookup begins at <hdr>. The pointer and size of the next occurrence of |
| 11131 | * the cookie value is returned into *value and *value_l, and the function |
| 11132 | * returns a pointer to the next pointer to search from if the value was found. |
| 11133 | * Otherwise if the cookie was not found, NULL is returned and neither value |
| 11134 | * nor value_l are touched. The input <hdr> string should first point to the |
| 11135 | * header's value, and the <hdr_end> pointer must point to the first character |
| 11136 | * not part of the value. <list> must be non-zero if value may represent a list |
| 11137 | * of values (cookie headers). This makes it faster to abort parsing when no |
| 11138 | * list is expected. |
| 11139 | */ |
| 11140 | static char * |
| 11141 | extract_cookie_value(char *hdr, const char *hdr_end, |
| 11142 | char *cookie_name, size_t cookie_name_l, int list, |
Willy Tarreau | 3fb818c | 2012-04-11 17:21:08 +0200 | [diff] [blame] | 11143 | char **value, int *value_l) |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 11144 | { |
| 11145 | char *equal, *att_end, *att_beg, *val_beg, *val_end; |
| 11146 | char *next; |
| 11147 | |
| 11148 | /* we search at least a cookie name followed by an equal, and more |
| 11149 | * generally something like this : |
| 11150 | * Cookie: NAME1 = VALUE 1 ; NAME2 = VALUE2 ; NAME3 = VALUE3\r\n |
| 11151 | */ |
| 11152 | for (att_beg = hdr; att_beg + cookie_name_l + 1 < hdr_end; att_beg = next + 1) { |
| 11153 | /* Iterate through all cookies on this line */ |
| 11154 | |
| 11155 | while (att_beg < hdr_end && http_is_spht[(unsigned char)*att_beg]) |
| 11156 | att_beg++; |
| 11157 | |
| 11158 | /* find att_end : this is the first character after the last non |
| 11159 | * space before the equal. It may be equal to hdr_end. |
| 11160 | */ |
| 11161 | equal = att_end = att_beg; |
| 11162 | |
| 11163 | while (equal < hdr_end) { |
| 11164 | if (*equal == '=' || *equal == ';' || (list && *equal == ',')) |
| 11165 | break; |
| 11166 | if (http_is_spht[(unsigned char)*equal++]) |
| 11167 | continue; |
| 11168 | att_end = equal; |
| 11169 | } |
| 11170 | |
| 11171 | /* here, <equal> points to '=', a delimitor or the end. <att_end> |
| 11172 | * is between <att_beg> and <equal>, both may be identical. |
| 11173 | */ |
| 11174 | |
| 11175 | /* look for end of cookie if there is an equal sign */ |
| 11176 | if (equal < hdr_end && *equal == '=') { |
| 11177 | /* look for the beginning of the value */ |
| 11178 | val_beg = equal + 1; |
| 11179 | while (val_beg < hdr_end && http_is_spht[(unsigned char)*val_beg]) |
| 11180 | val_beg++; |
| 11181 | |
| 11182 | /* find the end of the value, respecting quotes */ |
| 11183 | next = find_cookie_value_end(val_beg, hdr_end); |
| 11184 | |
| 11185 | /* make val_end point to the first white space or delimitor after the value */ |
| 11186 | val_end = next; |
| 11187 | while (val_end > val_beg && http_is_spht[(unsigned char)*(val_end - 1)]) |
| 11188 | val_end--; |
| 11189 | } else { |
| 11190 | val_beg = val_end = next = equal; |
| 11191 | } |
| 11192 | |
| 11193 | /* We have nothing to do with attributes beginning with '$'. However, |
| 11194 | * they will automatically be removed if a header before them is removed, |
| 11195 | * since they're supposed to be linked together. |
| 11196 | */ |
| 11197 | if (*att_beg == '$') |
| 11198 | continue; |
| 11199 | |
| 11200 | /* Ignore cookies with no equal sign */ |
| 11201 | if (equal == next) |
| 11202 | continue; |
| 11203 | |
| 11204 | /* Now we have the cookie name between att_beg and att_end, and |
| 11205 | * its value between val_beg and val_end. |
| 11206 | */ |
| 11207 | |
| 11208 | if (att_end - att_beg == cookie_name_l && |
| 11209 | memcmp(att_beg, cookie_name, cookie_name_l) == 0) { |
| 11210 | /* let's return this value and indicate where to go on from */ |
| 11211 | *value = val_beg; |
| 11212 | *value_l = val_end - val_beg; |
| 11213 | return next + 1; |
| 11214 | } |
| 11215 | |
| 11216 | /* Set-Cookie headers only have the name in the first attr=value part */ |
| 11217 | if (!list) |
| 11218 | break; |
| 11219 | } |
| 11220 | |
| 11221 | return NULL; |
| 11222 | } |
| 11223 | |
William Lallemand | a43ba4e | 2014-01-28 18:14:25 +0100 | [diff] [blame] | 11224 | /* Fetch a captured HTTP request header. The index is the position of |
| 11225 | * the "capture" option in the configuration file |
| 11226 | */ |
| 11227 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 11228 | smp_fetch_capture_header_req(const struct arg *args, struct sample *smp, const char *kw, void *private) |
William Lallemand | a43ba4e | 2014-01-28 18:14:25 +0100 | [diff] [blame] | 11229 | { |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 11230 | struct proxy *fe = strm_fe(smp->strm); |
William Lallemand | a43ba4e | 2014-01-28 18:14:25 +0100 | [diff] [blame] | 11231 | int idx; |
| 11232 | |
Thierry FOURNIER | bf65cd4 | 2015-07-20 17:45:02 +0200 | [diff] [blame] | 11233 | if (!args || args->type != ARGT_SINT) |
William Lallemand | a43ba4e | 2014-01-28 18:14:25 +0100 | [diff] [blame] | 11234 | return 0; |
| 11235 | |
Thierry FOURNIER | bf65cd4 | 2015-07-20 17:45:02 +0200 | [diff] [blame] | 11236 | idx = args->data.sint; |
William Lallemand | a43ba4e | 2014-01-28 18:14:25 +0100 | [diff] [blame] | 11237 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 11238 | if (idx > (fe->nb_req_cap - 1) || smp->strm->req_cap == NULL || smp->strm->req_cap[idx] == NULL) |
William Lallemand | a43ba4e | 2014-01-28 18:14:25 +0100 | [diff] [blame] | 11239 | return 0; |
| 11240 | |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 11241 | smp->type = SMP_T_STR; |
| 11242 | smp->flags |= SMP_F_CONST; |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 11243 | smp->data.str.str = smp->strm->req_cap[idx]; |
| 11244 | smp->data.str.len = strlen(smp->strm->req_cap[idx]); |
William Lallemand | a43ba4e | 2014-01-28 18:14:25 +0100 | [diff] [blame] | 11245 | |
| 11246 | return 1; |
| 11247 | } |
| 11248 | |
| 11249 | /* Fetch a captured HTTP response header. The index is the position of |
| 11250 | * the "capture" option in the configuration file |
| 11251 | */ |
| 11252 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 11253 | smp_fetch_capture_header_res(const struct arg *args, struct sample *smp, const char *kw, void *private) |
William Lallemand | a43ba4e | 2014-01-28 18:14:25 +0100 | [diff] [blame] | 11254 | { |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 11255 | struct proxy *fe = strm_fe(smp->strm); |
William Lallemand | a43ba4e | 2014-01-28 18:14:25 +0100 | [diff] [blame] | 11256 | int idx; |
| 11257 | |
Thierry FOURNIER | bf65cd4 | 2015-07-20 17:45:02 +0200 | [diff] [blame] | 11258 | if (!args || args->type != ARGT_SINT) |
William Lallemand | a43ba4e | 2014-01-28 18:14:25 +0100 | [diff] [blame] | 11259 | return 0; |
| 11260 | |
Thierry FOURNIER | bf65cd4 | 2015-07-20 17:45:02 +0200 | [diff] [blame] | 11261 | idx = args->data.sint; |
William Lallemand | a43ba4e | 2014-01-28 18:14:25 +0100 | [diff] [blame] | 11262 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 11263 | if (idx > (fe->nb_rsp_cap - 1) || smp->strm->res_cap == NULL || smp->strm->res_cap[idx] == NULL) |
William Lallemand | a43ba4e | 2014-01-28 18:14:25 +0100 | [diff] [blame] | 11264 | return 0; |
| 11265 | |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 11266 | smp->type = SMP_T_STR; |
| 11267 | smp->flags |= SMP_F_CONST; |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 11268 | smp->data.str.str = smp->strm->res_cap[idx]; |
| 11269 | smp->data.str.len = strlen(smp->strm->res_cap[idx]); |
William Lallemand | a43ba4e | 2014-01-28 18:14:25 +0100 | [diff] [blame] | 11270 | |
| 11271 | return 1; |
| 11272 | } |
| 11273 | |
William Lallemand | 65ad6e1 | 2014-01-31 15:08:02 +0100 | [diff] [blame] | 11274 | /* Extracts the METHOD in the HTTP request, the txn->uri should be filled before the call */ |
| 11275 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 11276 | smp_fetch_capture_req_method(const struct arg *args, struct sample *smp, const char *kw, void *private) |
William Lallemand | 65ad6e1 | 2014-01-31 15:08:02 +0100 | [diff] [blame] | 11277 | { |
| 11278 | struct chunk *temp; |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 11279 | struct http_txn *txn = smp->strm->txn; |
William Lallemand | 96a7785 | 2014-02-05 00:30:02 +0100 | [diff] [blame] | 11280 | char *ptr; |
William Lallemand | 65ad6e1 | 2014-01-31 15:08:02 +0100 | [diff] [blame] | 11281 | |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 11282 | if (!txn || !txn->uri) |
William Lallemand | 65ad6e1 | 2014-01-31 15:08:02 +0100 | [diff] [blame] | 11283 | return 0; |
| 11284 | |
William Lallemand | 96a7785 | 2014-02-05 00:30:02 +0100 | [diff] [blame] | 11285 | ptr = txn->uri; |
William Lallemand | 65ad6e1 | 2014-01-31 15:08:02 +0100 | [diff] [blame] | 11286 | |
William Lallemand | 96a7785 | 2014-02-05 00:30:02 +0100 | [diff] [blame] | 11287 | while (*ptr != ' ' && *ptr != '\0') /* find first space */ |
| 11288 | ptr++; |
William Lallemand | 65ad6e1 | 2014-01-31 15:08:02 +0100 | [diff] [blame] | 11289 | |
William Lallemand | 96a7785 | 2014-02-05 00:30:02 +0100 | [diff] [blame] | 11290 | temp = get_trash_chunk(); |
| 11291 | temp->str = txn->uri; |
| 11292 | temp->len = ptr - txn->uri; |
William Lallemand | 65ad6e1 | 2014-01-31 15:08:02 +0100 | [diff] [blame] | 11293 | smp->data.str = *temp; |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 11294 | smp->type = SMP_T_STR; |
| 11295 | smp->flags = SMP_F_CONST; |
William Lallemand | 65ad6e1 | 2014-01-31 15:08:02 +0100 | [diff] [blame] | 11296 | |
| 11297 | return 1; |
| 11298 | |
| 11299 | } |
| 11300 | |
| 11301 | /* Extracts the path in the HTTP request, the txn->uri should be filled before the call */ |
| 11302 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 11303 | smp_fetch_capture_req_uri(const struct arg *args, struct sample *smp, const char *kw, void *private) |
William Lallemand | 65ad6e1 | 2014-01-31 15:08:02 +0100 | [diff] [blame] | 11304 | { |
| 11305 | struct chunk *temp; |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 11306 | struct http_txn *txn = smp->strm->txn; |
William Lallemand | 65ad6e1 | 2014-01-31 15:08:02 +0100 | [diff] [blame] | 11307 | char *ptr; |
William Lallemand | 65ad6e1 | 2014-01-31 15:08:02 +0100 | [diff] [blame] | 11308 | |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 11309 | if (!txn || !txn->uri) |
William Lallemand | 65ad6e1 | 2014-01-31 15:08:02 +0100 | [diff] [blame] | 11310 | return 0; |
William Lallemand | 96a7785 | 2014-02-05 00:30:02 +0100 | [diff] [blame] | 11311 | |
William Lallemand | 65ad6e1 | 2014-01-31 15:08:02 +0100 | [diff] [blame] | 11312 | ptr = txn->uri; |
| 11313 | |
| 11314 | while (*ptr != ' ' && *ptr != '\0') /* find first space */ |
| 11315 | ptr++; |
William Lallemand | 96a7785 | 2014-02-05 00:30:02 +0100 | [diff] [blame] | 11316 | |
William Lallemand | 65ad6e1 | 2014-01-31 15:08:02 +0100 | [diff] [blame] | 11317 | if (!*ptr) |
| 11318 | return 0; |
| 11319 | |
| 11320 | ptr++; /* skip the space */ |
| 11321 | |
| 11322 | temp = get_trash_chunk(); |
William Lallemand | 96a7785 | 2014-02-05 00:30:02 +0100 | [diff] [blame] | 11323 | ptr = temp->str = http_get_path_from_string(ptr); |
William Lallemand | 65ad6e1 | 2014-01-31 15:08:02 +0100 | [diff] [blame] | 11324 | if (!ptr) |
| 11325 | return 0; |
| 11326 | while (*ptr != ' ' && *ptr != '\0') /* find space after URI */ |
| 11327 | ptr++; |
William Lallemand | 65ad6e1 | 2014-01-31 15:08:02 +0100 | [diff] [blame] | 11328 | |
| 11329 | smp->data.str = *temp; |
William Lallemand | 96a7785 | 2014-02-05 00:30:02 +0100 | [diff] [blame] | 11330 | smp->data.str.len = ptr - temp->str; |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 11331 | smp->type = SMP_T_STR; |
| 11332 | smp->flags = SMP_F_CONST; |
William Lallemand | 65ad6e1 | 2014-01-31 15:08:02 +0100 | [diff] [blame] | 11333 | |
| 11334 | return 1; |
| 11335 | } |
| 11336 | |
Willy Tarreau | 3c1b5ec | 2014-04-24 23:41:57 +0200 | [diff] [blame] | 11337 | /* Retrieves the HTTP version from the request (either 1.0 or 1.1) and emits it |
| 11338 | * as a string (either "HTTP/1.0" or "HTTP/1.1"). |
| 11339 | */ |
| 11340 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 11341 | smp_fetch_capture_req_ver(const struct arg *args, struct sample *smp, const char *kw, void *private) |
Willy Tarreau | 3c1b5ec | 2014-04-24 23:41:57 +0200 | [diff] [blame] | 11342 | { |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 11343 | struct http_txn *txn = smp->strm->txn; |
Willy Tarreau | 3c1b5ec | 2014-04-24 23:41:57 +0200 | [diff] [blame] | 11344 | |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 11345 | if (!txn || txn->req.msg_state < HTTP_MSG_HDR_FIRST) |
Willy Tarreau | 3c1b5ec | 2014-04-24 23:41:57 +0200 | [diff] [blame] | 11346 | return 0; |
| 11347 | |
| 11348 | if (txn->req.flags & HTTP_MSGF_VER_11) |
| 11349 | smp->data.str.str = "HTTP/1.1"; |
| 11350 | else |
| 11351 | smp->data.str.str = "HTTP/1.0"; |
| 11352 | |
| 11353 | smp->data.str.len = 8; |
| 11354 | smp->type = SMP_T_STR; |
| 11355 | smp->flags = SMP_F_CONST; |
| 11356 | return 1; |
| 11357 | |
| 11358 | } |
| 11359 | |
| 11360 | /* Retrieves the HTTP version from the response (either 1.0 or 1.1) and emits it |
| 11361 | * as a string (either "HTTP/1.0" or "HTTP/1.1"). |
| 11362 | */ |
| 11363 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 11364 | smp_fetch_capture_res_ver(const struct arg *args, struct sample *smp, const char *kw, void *private) |
Willy Tarreau | 3c1b5ec | 2014-04-24 23:41:57 +0200 | [diff] [blame] | 11365 | { |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 11366 | struct http_txn *txn = smp->strm->txn; |
Willy Tarreau | 3c1b5ec | 2014-04-24 23:41:57 +0200 | [diff] [blame] | 11367 | |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 11368 | if (!txn || txn->rsp.msg_state < HTTP_MSG_HDR_FIRST) |
Willy Tarreau | 3c1b5ec | 2014-04-24 23:41:57 +0200 | [diff] [blame] | 11369 | return 0; |
| 11370 | |
| 11371 | if (txn->rsp.flags & HTTP_MSGF_VER_11) |
| 11372 | smp->data.str.str = "HTTP/1.1"; |
| 11373 | else |
| 11374 | smp->data.str.str = "HTTP/1.0"; |
| 11375 | |
| 11376 | smp->data.str.len = 8; |
| 11377 | smp->type = SMP_T_STR; |
| 11378 | smp->flags = SMP_F_CONST; |
| 11379 | return 1; |
| 11380 | |
| 11381 | } |
| 11382 | |
William Lallemand | 65ad6e1 | 2014-01-31 15:08:02 +0100 | [diff] [blame] | 11383 | |
Willy Tarreau | e333ec9 | 2012-04-16 16:26:40 +0200 | [diff] [blame] | 11384 | /* Iterate over all cookies present in a message. The context is stored in |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 11385 | * smp->ctx.a[0] for the in-header position, smp->ctx.a[1] for the |
Willy Tarreau | a890d07 | 2013-04-02 12:01:06 +0200 | [diff] [blame] | 11386 | * end-of-header-value, and smp->ctx.a[2] for the hdr_ctx. Depending on |
Willy Tarreau | e333ec9 | 2012-04-16 16:26:40 +0200 | [diff] [blame] | 11387 | * the direction, multiple cookies may be parsed on the same line or not. |
Willy Tarreau | 24e32d8 | 2012-04-23 23:55:44 +0200 | [diff] [blame] | 11388 | * The cookie name is in args and the name length in args->data.str.len. |
Willy Tarreau | 28376d6 | 2012-04-26 21:26:10 +0200 | [diff] [blame] | 11389 | * Accepts exactly 1 argument of type string. If the input options indicate |
| 11390 | * that no iterating is desired, then only last value is fetched if any. |
William Lallemand | 07c8b24 | 2014-05-02 17:11:07 +0200 | [diff] [blame] | 11391 | * The returned sample is of type CSTR. Can be used to parse cookies in other |
| 11392 | * files. |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 11393 | */ |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 11394 | int smp_fetch_cookie(const struct arg *args, struct sample *smp, const char *kw, void *private) |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 11395 | { |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 11396 | struct http_txn *txn; |
| 11397 | struct hdr_idx *idx; |
Willy Tarreau | a890d07 | 2013-04-02 12:01:06 +0200 | [diff] [blame] | 11398 | struct hdr_ctx *ctx = smp->ctx.a[2]; |
Willy Tarreau | e333ec9 | 2012-04-16 16:26:40 +0200 | [diff] [blame] | 11399 | const struct http_msg *msg; |
| 11400 | const char *hdr_name; |
| 11401 | int hdr_name_len; |
| 11402 | char *sol; |
Willy Tarreau | 28376d6 | 2012-04-26 21:26:10 +0200 | [diff] [blame] | 11403 | int occ = 0; |
| 11404 | int found = 0; |
Willy Tarreau | e333ec9 | 2012-04-16 16:26:40 +0200 | [diff] [blame] | 11405 | |
Willy Tarreau | 24e32d8 | 2012-04-23 23:55:44 +0200 | [diff] [blame] | 11406 | if (!args || args->type != ARGT_STR) |
Willy Tarreau | 34db108 | 2012-04-19 17:16:54 +0200 | [diff] [blame] | 11407 | return 0; |
| 11408 | |
Willy Tarreau | a890d07 | 2013-04-02 12:01:06 +0200 | [diff] [blame] | 11409 | if (!ctx) { |
| 11410 | /* first call */ |
| 11411 | ctx = &static_hdr_ctx; |
| 11412 | ctx->idx = 0; |
| 11413 | smp->ctx.a[2] = ctx; |
| 11414 | } |
| 11415 | |
Willy Tarreau | e333ec9 | 2012-04-16 16:26:40 +0200 | [diff] [blame] | 11416 | CHECK_HTTP_MESSAGE_FIRST(); |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 11417 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 11418 | txn = smp->strm->txn; |
| 11419 | idx = &smp->strm->txn->hdr_idx; |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 11420 | |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 11421 | if ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) { |
Willy Tarreau | e333ec9 | 2012-04-16 16:26:40 +0200 | [diff] [blame] | 11422 | msg = &txn->req; |
| 11423 | hdr_name = "Cookie"; |
| 11424 | hdr_name_len = 6; |
| 11425 | } else { |
| 11426 | msg = &txn->rsp; |
| 11427 | hdr_name = "Set-Cookie"; |
| 11428 | hdr_name_len = 10; |
| 11429 | } |
| 11430 | |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 11431 | if (!occ && !(smp->opt & SMP_OPT_ITERATE)) |
Willy Tarreau | 28376d6 | 2012-04-26 21:26:10 +0200 | [diff] [blame] | 11432 | /* no explicit occurrence and single fetch => last cookie by default */ |
| 11433 | occ = -1; |
| 11434 | |
| 11435 | /* OK so basically here, either we want only one value and it's the |
| 11436 | * last one, or we want to iterate over all of them and we fetch the |
| 11437 | * next one. |
| 11438 | */ |
| 11439 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 11440 | sol = msg->chn->buf->p; |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 11441 | if (!(smp->flags & SMP_F_NOT_LAST)) { |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 11442 | /* search for the header from the beginning, we must first initialize |
| 11443 | * the search parameters. |
| 11444 | */ |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 11445 | smp->ctx.a[0] = NULL; |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 11446 | ctx->idx = 0; |
| 11447 | } |
| 11448 | |
Willy Tarreau | 28376d6 | 2012-04-26 21:26:10 +0200 | [diff] [blame] | 11449 | smp->flags |= SMP_F_VOL_HDR; |
| 11450 | |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 11451 | while (1) { |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 11452 | /* Note: smp->ctx.a[0] == NULL every time we need to fetch a new header */ |
| 11453 | if (!smp->ctx.a[0]) { |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 11454 | if (!http_find_header2(hdr_name, hdr_name_len, sol, idx, ctx)) |
| 11455 | goto out; |
| 11456 | |
Willy Tarreau | 24e32d8 | 2012-04-23 23:55:44 +0200 | [diff] [blame] | 11457 | if (ctx->vlen < args->data.str.len + 1) |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 11458 | continue; |
| 11459 | |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 11460 | smp->ctx.a[0] = ctx->line + ctx->val; |
| 11461 | smp->ctx.a[1] = smp->ctx.a[0] + ctx->vlen; |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 11462 | } |
| 11463 | |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 11464 | smp->type = SMP_T_STR; |
| 11465 | smp->flags |= SMP_F_CONST; |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 11466 | smp->ctx.a[0] = extract_cookie_value(smp->ctx.a[0], smp->ctx.a[1], |
Willy Tarreau | 24e32d8 | 2012-04-23 23:55:44 +0200 | [diff] [blame] | 11467 | args->data.str.str, args->data.str.len, |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 11468 | (smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ, |
Willy Tarreau | f853c46 | 2012-04-23 18:53:56 +0200 | [diff] [blame] | 11469 | &smp->data.str.str, |
| 11470 | &smp->data.str.len); |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 11471 | if (smp->ctx.a[0]) { |
Willy Tarreau | 28376d6 | 2012-04-26 21:26:10 +0200 | [diff] [blame] | 11472 | found = 1; |
| 11473 | if (occ >= 0) { |
| 11474 | /* one value was returned into smp->data.str.{str,len} */ |
| 11475 | smp->flags |= SMP_F_NOT_LAST; |
| 11476 | return 1; |
| 11477 | } |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 11478 | } |
Willy Tarreau | 28376d6 | 2012-04-26 21:26:10 +0200 | [diff] [blame] | 11479 | /* if we're looking for last occurrence, let's loop */ |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 11480 | } |
Willy Tarreau | 28376d6 | 2012-04-26 21:26:10 +0200 | [diff] [blame] | 11481 | /* all cookie headers and values were scanned. If we're looking for the |
| 11482 | * last occurrence, we may return it now. |
| 11483 | */ |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 11484 | out: |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 11485 | smp->flags &= ~SMP_F_NOT_LAST; |
Willy Tarreau | 28376d6 | 2012-04-26 21:26:10 +0200 | [diff] [blame] | 11486 | return found; |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 11487 | } |
| 11488 | |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 11489 | /* Iterate over all cookies present in a request to count how many occurrences |
Willy Tarreau | 24e32d8 | 2012-04-23 23:55:44 +0200 | [diff] [blame] | 11490 | * match the name in args and args->data.str.len. If <multi> is non-null, then |
Willy Tarreau | b169eba | 2013-12-16 15:14:43 +0100 | [diff] [blame] | 11491 | * multiple cookies may be parsed on the same line. The returned sample is of |
| 11492 | * type UINT. Accepts exactly 1 argument of type string. |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 11493 | */ |
| 11494 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 11495 | smp_fetch_cookie_cnt(const struct arg *args, struct sample *smp, const char *kw, void *private) |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 11496 | { |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 11497 | struct http_txn *txn; |
| 11498 | struct hdr_idx *idx; |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 11499 | struct hdr_ctx ctx; |
Willy Tarreau | e333ec9 | 2012-04-16 16:26:40 +0200 | [diff] [blame] | 11500 | const struct http_msg *msg; |
| 11501 | const char *hdr_name; |
| 11502 | int hdr_name_len; |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 11503 | int cnt; |
| 11504 | char *val_beg, *val_end; |
Willy Tarreau | e333ec9 | 2012-04-16 16:26:40 +0200 | [diff] [blame] | 11505 | char *sol; |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 11506 | |
Willy Tarreau | 24e32d8 | 2012-04-23 23:55:44 +0200 | [diff] [blame] | 11507 | if (!args || args->type != ARGT_STR) |
Willy Tarreau | 34db108 | 2012-04-19 17:16:54 +0200 | [diff] [blame] | 11508 | return 0; |
| 11509 | |
Willy Tarreau | e333ec9 | 2012-04-16 16:26:40 +0200 | [diff] [blame] | 11510 | CHECK_HTTP_MESSAGE_FIRST(); |
| 11511 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 11512 | txn = smp->strm->txn; |
| 11513 | idx = &smp->strm->txn->hdr_idx; |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 11514 | |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 11515 | if ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) { |
Willy Tarreau | e333ec9 | 2012-04-16 16:26:40 +0200 | [diff] [blame] | 11516 | msg = &txn->req; |
| 11517 | hdr_name = "Cookie"; |
| 11518 | hdr_name_len = 6; |
| 11519 | } else { |
| 11520 | msg = &txn->rsp; |
| 11521 | hdr_name = "Set-Cookie"; |
| 11522 | hdr_name_len = 10; |
| 11523 | } |
| 11524 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 11525 | sol = msg->chn->buf->p; |
Willy Tarreau | 46787ed | 2012-04-11 17:28:40 +0200 | [diff] [blame] | 11526 | val_end = val_beg = NULL; |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 11527 | ctx.idx = 0; |
| 11528 | cnt = 0; |
| 11529 | |
| 11530 | while (1) { |
| 11531 | /* Note: val_beg == NULL every time we need to fetch a new header */ |
| 11532 | if (!val_beg) { |
| 11533 | if (!http_find_header2(hdr_name, hdr_name_len, sol, idx, &ctx)) |
| 11534 | break; |
| 11535 | |
Willy Tarreau | 24e32d8 | 2012-04-23 23:55:44 +0200 | [diff] [blame] | 11536 | if (ctx.vlen < args->data.str.len + 1) |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 11537 | continue; |
| 11538 | |
| 11539 | val_beg = ctx.line + ctx.val; |
| 11540 | val_end = val_beg + ctx.vlen; |
| 11541 | } |
| 11542 | |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 11543 | smp->type = SMP_T_STR; |
| 11544 | smp->flags |= SMP_F_CONST; |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 11545 | while ((val_beg = extract_cookie_value(val_beg, val_end, |
Willy Tarreau | 24e32d8 | 2012-04-23 23:55:44 +0200 | [diff] [blame] | 11546 | args->data.str.str, args->data.str.len, |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 11547 | (smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ, |
Willy Tarreau | f853c46 | 2012-04-23 18:53:56 +0200 | [diff] [blame] | 11548 | &smp->data.str.str, |
| 11549 | &smp->data.str.len))) { |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 11550 | cnt++; |
| 11551 | } |
| 11552 | } |
| 11553 | |
Thierry FOURNIER | 07ee64e | 2015-07-06 23:43:03 +0200 | [diff] [blame] | 11554 | smp->type = SMP_T_SINT; |
| 11555 | smp->data.sint = cnt; |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 11556 | smp->flags |= SMP_F_VOL_HDR; |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 11557 | return 1; |
| 11558 | } |
| 11559 | |
Willy Tarreau | 5153936 | 2012-05-08 12:46:28 +0200 | [diff] [blame] | 11560 | /* Fetch an cookie's integer value. The integer value is returned. It |
| 11561 | * takes a mandatory argument of type string. It relies on smp_fetch_cookie(). |
| 11562 | */ |
| 11563 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 11564 | smp_fetch_cookie_val(const struct arg *args, struct sample *smp, const char *kw, void *private) |
Willy Tarreau | 5153936 | 2012-05-08 12:46:28 +0200 | [diff] [blame] | 11565 | { |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 11566 | int ret = smp_fetch_cookie(args, smp, kw, private); |
Willy Tarreau | 5153936 | 2012-05-08 12:46:28 +0200 | [diff] [blame] | 11567 | |
| 11568 | if (ret > 0) { |
Thierry FOURNIER | 07ee64e | 2015-07-06 23:43:03 +0200 | [diff] [blame] | 11569 | smp->type = SMP_T_SINT; |
| 11570 | smp->data.sint = strl2ic(smp->data.str.str, smp->data.str.len); |
Willy Tarreau | 5153936 | 2012-05-08 12:46:28 +0200 | [diff] [blame] | 11571 | } |
| 11572 | |
| 11573 | return ret; |
| 11574 | } |
| 11575 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 11576 | /************************************************************************/ |
Willy Tarreau | 1278578 | 2012-04-27 21:37:17 +0200 | [diff] [blame] | 11577 | /* The code below is dedicated to sample fetches */ |
Willy Tarreau | 4a56897 | 2010-05-12 08:08:50 +0200 | [diff] [blame] | 11578 | /************************************************************************/ |
| 11579 | |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 11580 | /* |
| 11581 | * Given a path string and its length, find the position of beginning of the |
| 11582 | * query string. Returns NULL if no query string is found in the path. |
| 11583 | * |
| 11584 | * Example: if path = "/foo/bar/fubar?yo=mama;ye=daddy", and n = 22: |
| 11585 | * |
Willy Tarreau | 1ede1da | 2015-05-07 16:06:18 +0200 | [diff] [blame] | 11586 | * find_query_string(path, n, '?') points to "yo=mama;ye=daddy" string. |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 11587 | */ |
bedis | 4c75cca | 2012-10-05 08:38:24 +0200 | [diff] [blame] | 11588 | static inline char *find_param_list(char *path, size_t path_l, char delim) |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 11589 | { |
| 11590 | char *p; |
Emeric Brun | 485479d | 2010-09-23 18:02:19 +0200 | [diff] [blame] | 11591 | |
bedis | 4c75cca | 2012-10-05 08:38:24 +0200 | [diff] [blame] | 11592 | p = memchr(path, delim, path_l); |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 11593 | return p ? p + 1 : NULL; |
| 11594 | } |
| 11595 | |
bedis | 4c75cca | 2012-10-05 08:38:24 +0200 | [diff] [blame] | 11596 | static inline int is_param_delimiter(char c, char delim) |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 11597 | { |
bedis | 4c75cca | 2012-10-05 08:38:24 +0200 | [diff] [blame] | 11598 | return c == '&' || c == ';' || c == delim; |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 11599 | } |
| 11600 | |
Thierry FOURNIER | 8be451c | 2015-05-20 15:28:12 +0200 | [diff] [blame] | 11601 | /* after increasing a pointer value, it can exceed the first buffer |
| 11602 | * size. This function transform the value of <ptr> according with |
| 11603 | * the expected position. <chunks> is an array of the one or two |
| 11604 | * avalaible chunks. The first value is the start of the first chunk, |
| 11605 | * the second value if the end+1 of the first chunks. The third value |
| 11606 | * is NULL or the start of the second chunk and the fourth value is |
| 11607 | * the end+1 of the second chunk. The function returns 1 if does a |
| 11608 | * wrap, else returns 0. |
| 11609 | */ |
| 11610 | static inline int fix_pointer_if_wrap(const char **chunks, const char **ptr) |
| 11611 | { |
| 11612 | if (*ptr < chunks[1]) |
| 11613 | return 0; |
| 11614 | if (!chunks[2]) |
| 11615 | return 0; |
| 11616 | *ptr = chunks[2] + ( *ptr - chunks[1] ); |
| 11617 | return 1; |
| 11618 | } |
| 11619 | |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 11620 | /* |
| 11621 | * Given a url parameter, find the starting position of the first occurence, |
| 11622 | * or NULL if the parameter is not found. |
| 11623 | * |
| 11624 | * Example: if query_string is "yo=mama;ye=daddy" and url_param_name is "ye", |
| 11625 | * the function will return query_string+8. |
Thierry FOURNIER | 8be451c | 2015-05-20 15:28:12 +0200 | [diff] [blame] | 11626 | * |
| 11627 | * Warning:this function returns a pointer that can be point to the first chunk |
| 11628 | * or the second chunk. The caller must be check the position before using the |
| 11629 | * result. |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 11630 | */ |
Thierry FOURNIER | 8be451c | 2015-05-20 15:28:12 +0200 | [diff] [blame] | 11631 | static const char * |
| 11632 | find_url_param_pos(const char **chunks, |
Willy Tarreau | 1ede1da | 2015-05-07 16:06:18 +0200 | [diff] [blame] | 11633 | const char* url_param_name, size_t url_param_name_l, |
bedis | 4c75cca | 2012-10-05 08:38:24 +0200 | [diff] [blame] | 11634 | char delim) |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 11635 | { |
Thierry FOURNIER | 8be451c | 2015-05-20 15:28:12 +0200 | [diff] [blame] | 11636 | const char *pos, *last, *equal; |
| 11637 | const char **bufs = chunks; |
| 11638 | int l1, l2; |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 11639 | |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 11640 | |
Thierry FOURNIER | 8be451c | 2015-05-20 15:28:12 +0200 | [diff] [blame] | 11641 | pos = bufs[0]; |
| 11642 | last = bufs[1]; |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 11643 | while (pos <= last) { |
Thierry FOURNIER | 8be451c | 2015-05-20 15:28:12 +0200 | [diff] [blame] | 11644 | /* Check the equal. */ |
| 11645 | equal = pos + url_param_name_l; |
| 11646 | if (fix_pointer_if_wrap(chunks, &equal)) { |
| 11647 | if (equal >= chunks[3]) |
| 11648 | return NULL; |
| 11649 | } else { |
| 11650 | if (equal >= chunks[1]) |
| 11651 | return NULL; |
| 11652 | } |
| 11653 | if (*equal == '=') { |
| 11654 | if (pos + url_param_name_l > last) { |
| 11655 | /* process wrap case, we detect a wrap. In this case, the |
| 11656 | * comparison is performed in two parts. |
| 11657 | */ |
| 11658 | |
| 11659 | /* This is the end, we dont have any other chunk. */ |
| 11660 | if (bufs != chunks || !bufs[2]) |
| 11661 | return NULL; |
| 11662 | |
| 11663 | /* Compute the length of each part of the comparison. */ |
| 11664 | l1 = last - pos; |
| 11665 | l2 = url_param_name_l - l1; |
| 11666 | |
| 11667 | /* The second buffer is too short to contain the compared string. */ |
| 11668 | if (bufs[2] + l2 > bufs[3]) |
| 11669 | return NULL; |
| 11670 | |
| 11671 | if (memcmp(pos, url_param_name, l1) == 0 && |
| 11672 | memcmp(bufs[2], url_param_name+l1, l2) == 0) |
| 11673 | return pos; |
| 11674 | |
| 11675 | /* Perform wrapping and jump the string who fail the comparison. */ |
| 11676 | bufs += 2; |
| 11677 | pos = bufs[0] + l2; |
| 11678 | last = bufs[1]; |
| 11679 | |
| 11680 | } else { |
| 11681 | /* process a simple comparison. */ |
| 11682 | if (memcmp(pos, url_param_name, url_param_name_l) == 0) { |
| 11683 | return pos; } |
| 11684 | pos += url_param_name_l + 1; |
| 11685 | if (fix_pointer_if_wrap(chunks, &pos)) |
| 11686 | last = bufs[2]; |
| 11687 | } |
| 11688 | } |
| 11689 | |
| 11690 | while (1) { |
| 11691 | /* Look for the next delimiter. */ |
| 11692 | while (pos <= last && !is_param_delimiter(*pos, delim)) |
| 11693 | pos++; |
| 11694 | if (pos < last) |
| 11695 | break; |
| 11696 | /* process buffer wrapping. */ |
| 11697 | if (bufs != chunks || !bufs[2]) |
| 11698 | return NULL; |
| 11699 | bufs += 2; |
| 11700 | pos = bufs[0]; |
| 11701 | last = bufs[1]; |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 11702 | } |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 11703 | pos++; |
| 11704 | } |
| 11705 | return NULL; |
| 11706 | } |
| 11707 | |
| 11708 | /* |
Willy Tarreau | 1ede1da | 2015-05-07 16:06:18 +0200 | [diff] [blame] | 11709 | * Given a url parameter name and a query string, returns its value and size |
| 11710 | * into *value and *value_l respectively, and returns non-zero. An empty |
| 11711 | * url_param_name matches the first available parameter. If the parameter is |
| 11712 | * not found, zero is returned and value/value_l are not touched. |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 11713 | */ |
| 11714 | static int |
Thierry FOURNIER | 8be451c | 2015-05-20 15:28:12 +0200 | [diff] [blame] | 11715 | find_next_url_param(const char **chunks, |
Willy Tarreau | 1ede1da | 2015-05-07 16:06:18 +0200 | [diff] [blame] | 11716 | const char* url_param_name, size_t url_param_name_l, |
Thierry FOURNIER | 8be451c | 2015-05-20 15:28:12 +0200 | [diff] [blame] | 11717 | const char **vstart, const char **vend, char delim) |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 11718 | { |
Thierry FOURNIER | 8be451c | 2015-05-20 15:28:12 +0200 | [diff] [blame] | 11719 | const char *arg_start, *qs_end; |
| 11720 | const char *value_start, *value_end; |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 11721 | |
Thierry FOURNIER | 8be451c | 2015-05-20 15:28:12 +0200 | [diff] [blame] | 11722 | arg_start = chunks[0]; |
| 11723 | qs_end = chunks[1]; |
Willy Tarreau | 1ede1da | 2015-05-07 16:06:18 +0200 | [diff] [blame] | 11724 | if (url_param_name_l) { |
Thierry FOURNIER | 8be451c | 2015-05-20 15:28:12 +0200 | [diff] [blame] | 11725 | /* Looks for an argument name. */ |
| 11726 | arg_start = find_url_param_pos(chunks, |
Willy Tarreau | 1ede1da | 2015-05-07 16:06:18 +0200 | [diff] [blame] | 11727 | url_param_name, url_param_name_l, |
| 11728 | delim); |
Thierry FOURNIER | 8be451c | 2015-05-20 15:28:12 +0200 | [diff] [blame] | 11729 | /* Check for wrapping. */ |
| 11730 | if (arg_start > qs_end) |
| 11731 | qs_end = chunks[3]; |
Willy Tarreau | 1ede1da | 2015-05-07 16:06:18 +0200 | [diff] [blame] | 11732 | } |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 11733 | if (!arg_start) |
| 11734 | return 0; |
| 11735 | |
Willy Tarreau | 1ede1da | 2015-05-07 16:06:18 +0200 | [diff] [blame] | 11736 | if (!url_param_name_l) { |
Thierry FOURNIER | 8be451c | 2015-05-20 15:28:12 +0200 | [diff] [blame] | 11737 | while (1) { |
| 11738 | /* looks for the first argument. */ |
| 11739 | value_start = memchr(arg_start, '=', qs_end - arg_start); |
| 11740 | if (!value_start) { |
| 11741 | |
| 11742 | /* Check for wrapping. */ |
| 11743 | if (arg_start >= chunks[0] && |
| 11744 | arg_start <= chunks[1] && |
| 11745 | chunks[2]) { |
| 11746 | arg_start = chunks[2]; |
| 11747 | qs_end = chunks[3]; |
| 11748 | continue; |
| 11749 | } |
| 11750 | return 0; |
| 11751 | } |
| 11752 | break; |
| 11753 | } |
Willy Tarreau | 1ede1da | 2015-05-07 16:06:18 +0200 | [diff] [blame] | 11754 | value_start++; |
| 11755 | } |
Thierry FOURNIER | 8be451c | 2015-05-20 15:28:12 +0200 | [diff] [blame] | 11756 | else { |
| 11757 | /* Jump the argument length. */ |
Willy Tarreau | 1ede1da | 2015-05-07 16:06:18 +0200 | [diff] [blame] | 11758 | value_start = arg_start + url_param_name_l + 1; |
| 11759 | |
Thierry FOURNIER | 8be451c | 2015-05-20 15:28:12 +0200 | [diff] [blame] | 11760 | /* Check for pointer wrapping. */ |
| 11761 | if (fix_pointer_if_wrap(chunks, &value_start)) { |
| 11762 | /* Update the end pointer. */ |
| 11763 | qs_end = chunks[3]; |
| 11764 | |
| 11765 | /* Check for overflow. */ |
| 11766 | if (value_start > qs_end) |
| 11767 | return 0; |
| 11768 | } |
| 11769 | } |
| 11770 | |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 11771 | value_end = value_start; |
| 11772 | |
Thierry FOURNIER | 8be451c | 2015-05-20 15:28:12 +0200 | [diff] [blame] | 11773 | while (1) { |
| 11774 | while ((value_end < qs_end) && !is_param_delimiter(*value_end, delim)) |
| 11775 | value_end++; |
| 11776 | if (value_end < qs_end) |
| 11777 | break; |
| 11778 | /* process buffer wrapping. */ |
| 11779 | if (value_end >= chunks[0] && |
| 11780 | value_end <= chunks[1] && |
| 11781 | chunks[2]) { |
| 11782 | value_end = chunks[2]; |
| 11783 | qs_end = chunks[3]; |
| 11784 | continue; |
| 11785 | } |
| 11786 | break; |
| 11787 | } |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 11788 | |
Thierry FOURNIER | 8be451c | 2015-05-20 15:28:12 +0200 | [diff] [blame] | 11789 | *vstart = value_start; |
| 11790 | *vend = value_end; |
Willy Tarreau | 0013433 | 2011-01-04 14:57:34 +0100 | [diff] [blame] | 11791 | return value_end != value_start; |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 11792 | } |
| 11793 | |
Thierry FOURNIER | 8be451c | 2015-05-20 15:28:12 +0200 | [diff] [blame] | 11794 | /* This scans a URL-encoded query string. It takes an optionally wrapping |
| 11795 | * string whose first contigous chunk has its beginning in ctx->a[0] and end |
| 11796 | * in ctx->a[1], and the optional second part in (ctx->a[2]..ctx->a[3]). The |
| 11797 | * pointers are updated for next iteration before leaving. |
Thierry FOURNIER | 4fdc74c | 2015-05-19 14:46:23 +0200 | [diff] [blame] | 11798 | */ |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 11799 | static int |
Thierry FOURNIER | 4fdc74c | 2015-05-19 14:46:23 +0200 | [diff] [blame] | 11800 | smp_fetch_param(char delim, const char *name, int name_len, const struct arg *args, struct sample *smp, const char *kw, void *private) |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 11801 | { |
Thierry FOURNIER | 8be451c | 2015-05-20 15:28:12 +0200 | [diff] [blame] | 11802 | const char *vstart, *vend; |
| 11803 | struct chunk *temp; |
| 11804 | const char **chunks = (const char **)smp->ctx.a; |
bedis | 4c75cca | 2012-10-05 08:38:24 +0200 | [diff] [blame] | 11805 | |
Thierry FOURNIER | 8be451c | 2015-05-20 15:28:12 +0200 | [diff] [blame] | 11806 | if (!find_next_url_param(chunks, |
Thierry FOURNIER | 0948d41 | 2015-05-20 15:22:37 +0200 | [diff] [blame] | 11807 | name, name_len, |
Thierry FOURNIER | 8be451c | 2015-05-20 15:28:12 +0200 | [diff] [blame] | 11808 | &vstart, &vend, |
Thierry FOURNIER | 0948d41 | 2015-05-20 15:22:37 +0200 | [diff] [blame] | 11809 | delim)) |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 11810 | return 0; |
| 11811 | |
Thierry FOURNIER | 8be451c | 2015-05-20 15:28:12 +0200 | [diff] [blame] | 11812 | /* Create sample. If the value is contiguous, return the pointer as CONST, |
| 11813 | * if the value is wrapped, copy-it in a buffer. |
| 11814 | */ |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 11815 | smp->type = SMP_T_STR; |
Thierry FOURNIER | 8be451c | 2015-05-20 15:28:12 +0200 | [diff] [blame] | 11816 | if (chunks[2] && |
| 11817 | vstart >= chunks[0] && vstart <= chunks[1] && |
| 11818 | vend >= chunks[2] && vend <= chunks[3]) { |
| 11819 | /* Wrapped case. */ |
| 11820 | temp = get_trash_chunk(); |
| 11821 | memcpy(temp->str, vstart, chunks[1] - vstart); |
| 11822 | memcpy(temp->str + ( chunks[1] - vstart ), chunks[2], vend - chunks[2]); |
| 11823 | smp->data.str.str = temp->str; |
| 11824 | smp->data.str.len = ( chunks[1] - vstart ) + ( vend - chunks[2] ); |
| 11825 | } else { |
| 11826 | /* Contiguous case. */ |
| 11827 | smp->data.str.str = (char *)vstart; |
| 11828 | smp->data.str.len = vend - vstart; |
| 11829 | smp->flags = SMP_F_VOL_1ST | SMP_F_CONST; |
| 11830 | } |
| 11831 | |
| 11832 | /* Update context, check wrapping. */ |
| 11833 | chunks[0] = vend; |
| 11834 | if (chunks[2] && vend >= chunks[2] && vend <= chunks[3]) { |
| 11835 | chunks[1] = chunks[3]; |
| 11836 | chunks[2] = NULL; |
| 11837 | } |
Willy Tarreau | 1ede1da | 2015-05-07 16:06:18 +0200 | [diff] [blame] | 11838 | |
Thierry FOURNIER | 8be451c | 2015-05-20 15:28:12 +0200 | [diff] [blame] | 11839 | if (chunks[0] < chunks[1]) |
Willy Tarreau | 1ede1da | 2015-05-07 16:06:18 +0200 | [diff] [blame] | 11840 | smp->flags |= SMP_F_NOT_LAST; |
| 11841 | |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 11842 | return 1; |
| 11843 | } |
| 11844 | |
Thierry FOURNIER | 4fdc74c | 2015-05-19 14:46:23 +0200 | [diff] [blame] | 11845 | /* This function iterates over each parameter of the query string. It uses |
| 11846 | * ctx->a[0] and ctx->a[1] to store the beginning and end of the current |
Thierry FOURNIER | 8be451c | 2015-05-20 15:28:12 +0200 | [diff] [blame] | 11847 | * parameter. Since it uses smp_fetch_param(), ctx->a[2..3] are both NULL. |
| 11848 | * An optional parameter name is passed in args[0], otherwise any parameter is |
| 11849 | * considered. It supports an optional delimiter argument for the beginning of |
| 11850 | * the string in args[1], which defaults to "?". |
Thierry FOURNIER | 4fdc74c | 2015-05-19 14:46:23 +0200 | [diff] [blame] | 11851 | */ |
| 11852 | static int |
| 11853 | smp_fetch_url_param(const struct arg *args, struct sample *smp, const char *kw, void *private) |
| 11854 | { |
| 11855 | struct http_msg *msg; |
| 11856 | char delim = '?'; |
| 11857 | const char *name; |
| 11858 | int name_len; |
| 11859 | |
Dragan Dosen | 26f77e5 | 2015-05-25 10:02:11 +0200 | [diff] [blame] | 11860 | if (!args || |
| 11861 | (args[0].type && args[0].type != ARGT_STR) || |
| 11862 | (args[1].type && args[1].type != ARGT_STR)) |
| 11863 | return 0; |
Thierry FOURNIER | 4fdc74c | 2015-05-19 14:46:23 +0200 | [diff] [blame] | 11864 | |
Dragan Dosen | 26f77e5 | 2015-05-25 10:02:11 +0200 | [diff] [blame] | 11865 | name = ""; |
| 11866 | name_len = 0; |
| 11867 | if (args->type == ARGT_STR) { |
| 11868 | name = args->data.str.str; |
| 11869 | name_len = args->data.str.len; |
| 11870 | } |
Thierry FOURNIER | 4fdc74c | 2015-05-19 14:46:23 +0200 | [diff] [blame] | 11871 | |
Dragan Dosen | 26f77e5 | 2015-05-25 10:02:11 +0200 | [diff] [blame] | 11872 | if (args[1].type) |
| 11873 | delim = *args[1].data.str.str; |
Thierry FOURNIER | 4fdc74c | 2015-05-19 14:46:23 +0200 | [diff] [blame] | 11874 | |
Dragan Dosen | 26f77e5 | 2015-05-25 10:02:11 +0200 | [diff] [blame] | 11875 | if (!smp->ctx.a[0]) { // first call, find the query string |
Thierry FOURNIER | 4fdc74c | 2015-05-19 14:46:23 +0200 | [diff] [blame] | 11876 | CHECK_HTTP_MESSAGE_FIRST(); |
| 11877 | |
| 11878 | msg = &smp->strm->txn->req; |
| 11879 | |
| 11880 | smp->ctx.a[0] = find_param_list(msg->chn->buf->p + msg->sl.rq.u, |
| 11881 | msg->sl.rq.u_l, delim); |
| 11882 | if (!smp->ctx.a[0]) |
| 11883 | return 0; |
| 11884 | |
| 11885 | smp->ctx.a[1] = msg->chn->buf->p + msg->sl.rq.u + msg->sl.rq.u_l; |
Thierry FOURNIER | 8be451c | 2015-05-20 15:28:12 +0200 | [diff] [blame] | 11886 | |
| 11887 | /* Assume that the context is filled with NULL pointer |
| 11888 | * before the first call. |
| 11889 | * smp->ctx.a[2] = NULL; |
| 11890 | * smp->ctx.a[3] = NULL; |
| 11891 | */ |
Thierry FOURNIER | 4fdc74c | 2015-05-19 14:46:23 +0200 | [diff] [blame] | 11892 | } |
| 11893 | |
| 11894 | return smp_fetch_param(delim, name, name_len, args, smp, kw, private); |
| 11895 | } |
| 11896 | |
Thierry FOURNIER | e28c499 | 2015-05-19 14:45:09 +0200 | [diff] [blame] | 11897 | /* This function iterates over each parameter of the body. This requires |
| 11898 | * that the body has been waited for using http-buffer-request. It uses |
Thierry FOURNIER | 8be451c | 2015-05-20 15:28:12 +0200 | [diff] [blame] | 11899 | * ctx->a[0] and ctx->a[1] to store the beginning and end of the first |
| 11900 | * contigous part of the body, and optionally ctx->a[2..3] to reference the |
| 11901 | * optional second part if the body wraps at the end of the buffer. An optional |
| 11902 | * parameter name is passed in args[0], otherwise any parameter is considered. |
Thierry FOURNIER | e28c499 | 2015-05-19 14:45:09 +0200 | [diff] [blame] | 11903 | */ |
| 11904 | static int |
| 11905 | smp_fetch_body_param(const struct arg *args, struct sample *smp, const char *kw, void *private) |
| 11906 | { |
| 11907 | struct http_txn *txn = smp->strm->txn; |
| 11908 | struct http_msg *msg; |
| 11909 | unsigned long len; |
| 11910 | unsigned long block1; |
| 11911 | char *body; |
| 11912 | const char *name; |
| 11913 | int name_len; |
| 11914 | |
| 11915 | if (!args || (args[0].type && args[0].type != ARGT_STR)) |
| 11916 | return 0; |
| 11917 | |
| 11918 | name = ""; |
| 11919 | name_len = 0; |
| 11920 | if (args[0].type == ARGT_STR) { |
| 11921 | name = args[0].data.str.str; |
| 11922 | name_len = args[0].data.str.len; |
| 11923 | } |
| 11924 | |
| 11925 | if (!smp->ctx.a[0]) { // first call, find the query string |
| 11926 | CHECK_HTTP_MESSAGE_FIRST(); |
| 11927 | |
| 11928 | if ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) |
| 11929 | msg = &txn->req; |
| 11930 | else |
| 11931 | msg = &txn->rsp; |
| 11932 | |
| 11933 | len = http_body_bytes(msg); |
| 11934 | body = b_ptr(msg->chn->buf, -http_data_rewind(msg)); |
| 11935 | |
| 11936 | block1 = len; |
| 11937 | if (block1 > msg->chn->buf->data + msg->chn->buf->size - body) |
| 11938 | block1 = msg->chn->buf->data + msg->chn->buf->size - body; |
| 11939 | |
| 11940 | if (block1 == len) { |
| 11941 | /* buffer is not wrapped (or empty) */ |
| 11942 | smp->ctx.a[0] = body; |
| 11943 | smp->ctx.a[1] = body + len; |
Thierry FOURNIER | 8be451c | 2015-05-20 15:28:12 +0200 | [diff] [blame] | 11944 | |
| 11945 | /* Assume that the context is filled with NULL pointer |
| 11946 | * before the first call. |
| 11947 | * smp->ctx.a[2] = NULL; |
| 11948 | * smp->ctx.a[3] = NULL; |
| 11949 | */ |
Thierry FOURNIER | e28c499 | 2015-05-19 14:45:09 +0200 | [diff] [blame] | 11950 | } |
| 11951 | else { |
| 11952 | /* buffer is wrapped, we need to defragment it */ |
| 11953 | smp->ctx.a[0] = body; |
| 11954 | smp->ctx.a[1] = body + block1; |
Thierry FOURNIER | 8be451c | 2015-05-20 15:28:12 +0200 | [diff] [blame] | 11955 | smp->ctx.a[2] = msg->chn->buf->data; |
| 11956 | smp->ctx.a[3] = msg->chn->buf->data + ( len - block1 ); |
Thierry FOURNIER | e28c499 | 2015-05-19 14:45:09 +0200 | [diff] [blame] | 11957 | } |
| 11958 | } |
| 11959 | return smp_fetch_param('&', name, name_len, args, smp, kw, private); |
| 11960 | } |
| 11961 | |
Willy Tarreau | a9fddca | 2012-07-31 07:51:48 +0200 | [diff] [blame] | 11962 | /* Return the signed integer value for the specified url parameter (see url_param |
| 11963 | * above). |
| 11964 | */ |
| 11965 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 11966 | smp_fetch_url_param_val(const struct arg *args, struct sample *smp, const char *kw, void *private) |
Willy Tarreau | a9fddca | 2012-07-31 07:51:48 +0200 | [diff] [blame] | 11967 | { |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 11968 | int ret = smp_fetch_url_param(args, smp, kw, private); |
Willy Tarreau | a9fddca | 2012-07-31 07:51:48 +0200 | [diff] [blame] | 11969 | |
| 11970 | if (ret > 0) { |
Thierry FOURNIER | 07ee64e | 2015-07-06 23:43:03 +0200 | [diff] [blame] | 11971 | smp->type = SMP_T_SINT; |
| 11972 | smp->data.sint = strl2ic(smp->data.str.str, smp->data.str.len); |
Willy Tarreau | a9fddca | 2012-07-31 07:51:48 +0200 | [diff] [blame] | 11973 | } |
| 11974 | |
| 11975 | return ret; |
| 11976 | } |
| 11977 | |
Neil - HAProxy List | 39c63c5 | 2013-11-04 13:48:42 +0000 | [diff] [blame] | 11978 | /* This produces a 32-bit hash of the concatenation of the first occurrence of |
| 11979 | * the Host header followed by the path component if it begins with a slash ('/'). |
| 11980 | * This means that '*' will not be added, resulting in exactly the first Host |
| 11981 | * entry. If no Host header is found, then the path is used. The resulting value |
| 11982 | * is hashed using the url hash followed by a full avalanche hash and provides a |
| 11983 | * 32-bit integer value. This fetch is useful for tracking per-URL activity on |
| 11984 | * high-traffic sites without having to store whole paths. |
| 11985 | * this differs from the base32 functions in that it includes the url parameters |
| 11986 | * as well as the path |
| 11987 | */ |
| 11988 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 11989 | smp_fetch_url32(const struct arg *args, struct sample *smp, const char *kw, void *private) |
Neil - HAProxy List | 39c63c5 | 2013-11-04 13:48:42 +0000 | [diff] [blame] | 11990 | { |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 11991 | struct http_txn *txn; |
Neil - HAProxy List | 39c63c5 | 2013-11-04 13:48:42 +0000 | [diff] [blame] | 11992 | struct hdr_ctx ctx; |
| 11993 | unsigned int hash = 0; |
| 11994 | char *ptr, *beg, *end; |
| 11995 | int len; |
| 11996 | |
| 11997 | CHECK_HTTP_MESSAGE_FIRST(); |
| 11998 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 11999 | txn = smp->strm->txn; |
Neil - HAProxy List | 39c63c5 | 2013-11-04 13:48:42 +0000 | [diff] [blame] | 12000 | ctx.idx = 0; |
Willy Tarreau | 877e78d | 2013-04-07 18:48:08 +0200 | [diff] [blame] | 12001 | if (http_find_header2("Host", 4, txn->req.chn->buf->p, &txn->hdr_idx, &ctx)) { |
Neil - HAProxy List | 39c63c5 | 2013-11-04 13:48:42 +0000 | [diff] [blame] | 12002 | /* OK we have the header value in ctx.line+ctx.val for ctx.vlen bytes */ |
| 12003 | ptr = ctx.line + ctx.val; |
| 12004 | len = ctx.vlen; |
| 12005 | while (len--) |
| 12006 | hash = *(ptr++) + (hash << 6) + (hash << 16) - hash; |
| 12007 | } |
| 12008 | |
| 12009 | /* now retrieve the path */ |
Willy Tarreau | 877e78d | 2013-04-07 18:48:08 +0200 | [diff] [blame] | 12010 | end = txn->req.chn->buf->p + txn->req.sl.rq.u + txn->req.sl.rq.u_l; |
Neil - HAProxy List | 39c63c5 | 2013-11-04 13:48:42 +0000 | [diff] [blame] | 12011 | beg = http_get_path(txn); |
| 12012 | if (!beg) |
| 12013 | beg = end; |
| 12014 | |
| 12015 | for (ptr = beg; ptr < end ; ptr++); |
| 12016 | |
| 12017 | if (beg < ptr && *beg == '/') { |
| 12018 | while (beg < ptr) |
| 12019 | hash = *(beg++) + (hash << 6) + (hash << 16) - hash; |
| 12020 | } |
| 12021 | hash = full_hash(hash); |
| 12022 | |
Thierry FOURNIER | 07ee64e | 2015-07-06 23:43:03 +0200 | [diff] [blame] | 12023 | smp->type = SMP_T_SINT; |
| 12024 | smp->data.sint = hash; |
Neil - HAProxy List | 39c63c5 | 2013-11-04 13:48:42 +0000 | [diff] [blame] | 12025 | smp->flags = SMP_F_VOL_1ST; |
| 12026 | return 1; |
| 12027 | } |
| 12028 | |
| 12029 | /* This concatenates the source address with the 32-bit hash of the Host and |
| 12030 | * URL as returned by smp_fetch_base32(). The idea is to have per-source and |
| 12031 | * per-url counters. The result is a binary block from 8 to 20 bytes depending |
| 12032 | * on the source address length. The URL hash is stored before the address so |
| 12033 | * that in environments where IPv6 is insignificant, truncating the output to |
| 12034 | * 8 bytes would still work. |
| 12035 | */ |
| 12036 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 12037 | smp_fetch_url32_src(const struct arg *args, struct sample *smp, const char *kw, void *private) |
Neil - HAProxy List | 39c63c5 | 2013-11-04 13:48:42 +0000 | [diff] [blame] | 12038 | { |
| 12039 | struct chunk *temp; |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 12040 | struct connection *cli_conn = objt_conn(smp->sess->origin); |
Thierry FOURNIER | 07ee64e | 2015-07-06 23:43:03 +0200 | [diff] [blame] | 12041 | unsigned int hash; |
Neil - HAProxy List | 39c63c5 | 2013-11-04 13:48:42 +0000 | [diff] [blame] | 12042 | |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 12043 | if (!smp_fetch_url32(args, smp, kw, private)) |
Neil - HAProxy List | 39c63c5 | 2013-11-04 13:48:42 +0000 | [diff] [blame] | 12044 | return 0; |
| 12045 | |
Thierry FOURNIER | 07ee64e | 2015-07-06 23:43:03 +0200 | [diff] [blame] | 12046 | /* The returned hash is a 32 bytes integer. */ |
| 12047 | hash = smp->data.sint; |
| 12048 | |
Neil - HAProxy List | 39c63c5 | 2013-11-04 13:48:42 +0000 | [diff] [blame] | 12049 | temp = get_trash_chunk(); |
Thierry FOURNIER | 07ee64e | 2015-07-06 23:43:03 +0200 | [diff] [blame] | 12050 | memcpy(temp->str + temp->len, &hash, sizeof(hash)); |
| 12051 | temp->len += sizeof(hash); |
Neil - HAProxy List | 39c63c5 | 2013-11-04 13:48:42 +0000 | [diff] [blame] | 12052 | |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 12053 | switch (cli_conn->addr.from.ss_family) { |
Neil - HAProxy List | 39c63c5 | 2013-11-04 13:48:42 +0000 | [diff] [blame] | 12054 | case AF_INET: |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 12055 | memcpy(temp->str + temp->len, &((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr, 4); |
Neil - HAProxy List | 39c63c5 | 2013-11-04 13:48:42 +0000 | [diff] [blame] | 12056 | temp->len += 4; |
| 12057 | break; |
| 12058 | case AF_INET6: |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 12059 | memcpy(temp->str + temp->len, &((struct sockaddr_in6 *)&cli_conn->addr.from)->sin6_addr, 16); |
Neil - HAProxy List | 39c63c5 | 2013-11-04 13:48:42 +0000 | [diff] [blame] | 12060 | temp->len += 16; |
| 12061 | break; |
| 12062 | default: |
| 12063 | return 0; |
| 12064 | } |
| 12065 | |
| 12066 | smp->data.str = *temp; |
| 12067 | smp->type = SMP_T_BIN; |
| 12068 | return 1; |
| 12069 | } |
| 12070 | |
Willy Tarreau | 185b5c4 | 2012-04-26 15:11:51 +0200 | [diff] [blame] | 12071 | /* This function is used to validate the arguments passed to any "hdr" fetch |
| 12072 | * keyword. These keywords support an optional positive or negative occurrence |
| 12073 | * number. We must ensure that the number is greater than -MAX_HDR_HISTORY. It |
| 12074 | * is assumed that the types are already the correct ones. Returns 0 on error, |
| 12075 | * non-zero if OK. If <err> is not NULL, it will be filled with a pointer to an |
| 12076 | * error message in case of error, that the caller is responsible for freeing. |
| 12077 | * The initial location must either be freeable or NULL. |
| 12078 | */ |
Thierry FOURNIER | 49f45af | 2014-12-08 19:50:43 +0100 | [diff] [blame] | 12079 | int val_hdr(struct arg *arg, char **err_msg) |
Willy Tarreau | 185b5c4 | 2012-04-26 15:11:51 +0200 | [diff] [blame] | 12080 | { |
| 12081 | if (arg && arg[1].type == ARGT_SINT && arg[1].data.sint < -MAX_HDR_HISTORY) { |
Willy Tarreau | eb6cead | 2012-09-20 19:43:14 +0200 | [diff] [blame] | 12082 | memprintf(err_msg, "header occurrence must be >= %d", -MAX_HDR_HISTORY); |
Willy Tarreau | 185b5c4 | 2012-04-26 15:11:51 +0200 | [diff] [blame] | 12083 | return 0; |
| 12084 | } |
| 12085 | return 1; |
| 12086 | } |
| 12087 | |
Willy Tarreau | 276fae9 | 2013-07-25 14:36:01 +0200 | [diff] [blame] | 12088 | /* takes an UINT value on input supposed to represent the time since EPOCH, |
| 12089 | * adds an optional offset found in args[0] and emits a string representing |
| 12090 | * the date in RFC-1123/5322 format. |
| 12091 | */ |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 12092 | static int sample_conv_http_date(const struct arg *args, struct sample *smp, void *private) |
Willy Tarreau | 276fae9 | 2013-07-25 14:36:01 +0200 | [diff] [blame] | 12093 | { |
| 12094 | const char day[7][4] = { "Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun" }; |
| 12095 | const char mon[12][4] = { "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" }; |
| 12096 | struct chunk *temp; |
| 12097 | struct tm *tm; |
Thierry FOURNIER | bf65cd4 | 2015-07-20 17:45:02 +0200 | [diff] [blame] | 12098 | /* With high numbers, the date returned can be negative, the 55 bits mask prevent this. */ |
| 12099 | time_t curr_date = smp->data.sint & 0x007fffffffffffffLL; |
Willy Tarreau | 276fae9 | 2013-07-25 14:36:01 +0200 | [diff] [blame] | 12100 | |
| 12101 | /* add offset */ |
Thierry FOURNIER | bf65cd4 | 2015-07-20 17:45:02 +0200 | [diff] [blame] | 12102 | if (args && (args[0].type == ARGT_SINT)) |
Willy Tarreau | 276fae9 | 2013-07-25 14:36:01 +0200 | [diff] [blame] | 12103 | curr_date += args[0].data.sint; |
| 12104 | |
| 12105 | tm = gmtime(&curr_date); |
Thierry FOURNIER | fac9ccf | 2015-07-08 00:15:20 +0200 | [diff] [blame] | 12106 | if (!tm) |
| 12107 | return 0; |
Willy Tarreau | 276fae9 | 2013-07-25 14:36:01 +0200 | [diff] [blame] | 12108 | |
| 12109 | temp = get_trash_chunk(); |
| 12110 | temp->len = snprintf(temp->str, temp->size - temp->len, |
| 12111 | "%s, %02d %s %04d %02d:%02d:%02d GMT", |
| 12112 | day[tm->tm_wday], tm->tm_mday, mon[tm->tm_mon], 1900+tm->tm_year, |
| 12113 | tm->tm_hour, tm->tm_min, tm->tm_sec); |
| 12114 | |
| 12115 | smp->data.str = *temp; |
| 12116 | smp->type = SMP_T_STR; |
| 12117 | return 1; |
| 12118 | } |
| 12119 | |
Thierry FOURNIER | ad90351 | 2014-04-11 17:51:01 +0200 | [diff] [blame] | 12120 | /* Match language range with language tag. RFC2616 14.4: |
| 12121 | * |
| 12122 | * A language-range matches a language-tag if it exactly equals |
| 12123 | * the tag, or if it exactly equals a prefix of the tag such |
| 12124 | * that the first tag character following the prefix is "-". |
| 12125 | * |
| 12126 | * Return 1 if the strings match, else return 0. |
| 12127 | */ |
| 12128 | static inline int language_range_match(const char *range, int range_len, |
| 12129 | const char *tag, int tag_len) |
| 12130 | { |
| 12131 | const char *end = range + range_len; |
| 12132 | const char *tend = tag + tag_len; |
| 12133 | while (range < end) { |
| 12134 | if (*range == '-' && tag == tend) |
| 12135 | return 1; |
| 12136 | if (*range != *tag || tag == tend) |
| 12137 | return 0; |
| 12138 | range++; |
| 12139 | tag++; |
| 12140 | } |
| 12141 | /* Return true only if the last char of the tag is matched. */ |
| 12142 | return tag == tend; |
| 12143 | } |
| 12144 | |
| 12145 | /* Arguments: The list of expected value, the number of parts returned and the separator */ |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 12146 | static int sample_conv_q_prefered(const struct arg *args, struct sample *smp, void *private) |
Thierry FOURNIER | ad90351 | 2014-04-11 17:51:01 +0200 | [diff] [blame] | 12147 | { |
| 12148 | const char *al = smp->data.str.str; |
| 12149 | const char *end = al + smp->data.str.len; |
| 12150 | const char *token; |
| 12151 | int toklen; |
| 12152 | int qvalue; |
| 12153 | const char *str; |
| 12154 | const char *w; |
| 12155 | int best_q = 0; |
| 12156 | |
| 12157 | /* Set the constant to the sample, because the output of the |
| 12158 | * function will be peek in the constant configuration string. |
| 12159 | */ |
| 12160 | smp->flags |= SMP_F_CONST; |
| 12161 | smp->data.str.size = 0; |
| 12162 | smp->data.str.str = ""; |
| 12163 | smp->data.str.len = 0; |
| 12164 | |
| 12165 | /* Parse the accept language */ |
| 12166 | while (1) { |
| 12167 | |
| 12168 | /* Jump spaces, quit if the end is detected. */ |
Willy Tarreau | 506c69a | 2014-07-08 00:59:48 +0200 | [diff] [blame] | 12169 | while (al < end && isspace((unsigned char)*al)) |
Thierry FOURNIER | ad90351 | 2014-04-11 17:51:01 +0200 | [diff] [blame] | 12170 | al++; |
| 12171 | if (al >= end) |
| 12172 | break; |
| 12173 | |
| 12174 | /* Start of the fisrt word. */ |
| 12175 | token = al; |
| 12176 | |
| 12177 | /* Look for separator: isspace(), ',' or ';'. Next value if 0 length word. */ |
Willy Tarreau | 506c69a | 2014-07-08 00:59:48 +0200 | [diff] [blame] | 12178 | while (al < end && *al != ';' && *al != ',' && !isspace((unsigned char)*al)) |
Thierry FOURNIER | ad90351 | 2014-04-11 17:51:01 +0200 | [diff] [blame] | 12179 | al++; |
| 12180 | if (al == token) |
| 12181 | goto expect_comma; |
| 12182 | |
| 12183 | /* Length of the token. */ |
| 12184 | toklen = al - token; |
| 12185 | qvalue = 1000; |
| 12186 | |
| 12187 | /* Check if the token exists in the list. If the token not exists, |
| 12188 | * jump to the next token. |
| 12189 | */ |
| 12190 | str = args[0].data.str.str; |
| 12191 | w = str; |
| 12192 | while (1) { |
| 12193 | if (*str == ';' || *str == '\0') { |
| 12194 | if (language_range_match(token, toklen, w, str-w)) |
| 12195 | goto look_for_q; |
| 12196 | if (*str == '\0') |
| 12197 | goto expect_comma; |
| 12198 | w = str + 1; |
| 12199 | } |
| 12200 | str++; |
| 12201 | } |
| 12202 | goto expect_comma; |
| 12203 | |
| 12204 | look_for_q: |
| 12205 | |
| 12206 | /* Jump spaces, quit if the end is detected. */ |
Willy Tarreau | 506c69a | 2014-07-08 00:59:48 +0200 | [diff] [blame] | 12207 | while (al < end && isspace((unsigned char)*al)) |
Thierry FOURNIER | ad90351 | 2014-04-11 17:51:01 +0200 | [diff] [blame] | 12208 | al++; |
| 12209 | if (al >= end) |
| 12210 | goto process_value; |
| 12211 | |
| 12212 | /* If ',' is found, process the result */ |
| 12213 | if (*al == ',') |
| 12214 | goto process_value; |
| 12215 | |
| 12216 | /* If the character is different from ';', look |
| 12217 | * for the end of the header part in best effort. |
| 12218 | */ |
| 12219 | if (*al != ';') |
| 12220 | goto expect_comma; |
| 12221 | |
| 12222 | /* Assumes that the char is ';', now expect "q=". */ |
| 12223 | al++; |
| 12224 | |
| 12225 | /* Jump spaces, process value if the end is detected. */ |
Willy Tarreau | 506c69a | 2014-07-08 00:59:48 +0200 | [diff] [blame] | 12226 | while (al < end && isspace((unsigned char)*al)) |
Thierry FOURNIER | ad90351 | 2014-04-11 17:51:01 +0200 | [diff] [blame] | 12227 | al++; |
| 12228 | if (al >= end) |
| 12229 | goto process_value; |
| 12230 | |
| 12231 | /* Expect 'q'. If no 'q', continue in best effort */ |
| 12232 | if (*al != 'q') |
| 12233 | goto process_value; |
| 12234 | al++; |
| 12235 | |
| 12236 | /* Jump spaces, process value if the end is detected. */ |
Willy Tarreau | 506c69a | 2014-07-08 00:59:48 +0200 | [diff] [blame] | 12237 | while (al < end && isspace((unsigned char)*al)) |
Thierry FOURNIER | ad90351 | 2014-04-11 17:51:01 +0200 | [diff] [blame] | 12238 | al++; |
| 12239 | if (al >= end) |
| 12240 | goto process_value; |
| 12241 | |
| 12242 | /* Expect '='. If no '=', continue in best effort */ |
| 12243 | if (*al != '=') |
| 12244 | goto process_value; |
| 12245 | al++; |
| 12246 | |
| 12247 | /* Jump spaces, process value if the end is detected. */ |
Willy Tarreau | 506c69a | 2014-07-08 00:59:48 +0200 | [diff] [blame] | 12248 | while (al < end && isspace((unsigned char)*al)) |
Thierry FOURNIER | ad90351 | 2014-04-11 17:51:01 +0200 | [diff] [blame] | 12249 | al++; |
| 12250 | if (al >= end) |
| 12251 | goto process_value; |
| 12252 | |
| 12253 | /* Parse the q value. */ |
| 12254 | qvalue = parse_qvalue(al, &al); |
| 12255 | |
| 12256 | process_value: |
| 12257 | |
| 12258 | /* If the new q value is the best q value, then store the associated |
| 12259 | * language in the response. If qvalue is the biggest value (1000), |
| 12260 | * break the process. |
| 12261 | */ |
| 12262 | if (qvalue > best_q) { |
| 12263 | smp->data.str.str = (char *)w; |
| 12264 | smp->data.str.len = str - w; |
| 12265 | if (qvalue >= 1000) |
| 12266 | break; |
| 12267 | best_q = qvalue; |
| 12268 | } |
| 12269 | |
| 12270 | expect_comma: |
| 12271 | |
| 12272 | /* Expect comma or end. If the end is detected, quit the loop. */ |
| 12273 | while (al < end && *al != ',') |
| 12274 | al++; |
| 12275 | if (al >= end) |
| 12276 | break; |
| 12277 | |
| 12278 | /* Comma is found, jump it and restart the analyzer. */ |
| 12279 | al++; |
| 12280 | } |
| 12281 | |
| 12282 | /* Set default value if required. */ |
| 12283 | if (smp->data.str.len == 0 && args[1].type == ARGT_STR) { |
| 12284 | smp->data.str.str = args[1].data.str.str; |
| 12285 | smp->data.str.len = args[1].data.str.len; |
| 12286 | } |
| 12287 | |
| 12288 | /* Return true only if a matching language was found. */ |
| 12289 | return smp->data.str.len != 0; |
| 12290 | } |
| 12291 | |
Thierry FOURNIER | 82ff3c9 | 2015-05-07 15:46:20 +0200 | [diff] [blame] | 12292 | /* This fetch url-decode any input string. */ |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 12293 | static int sample_conv_url_dec(const struct arg *args, struct sample *smp, void *private) |
Thierry FOURNIER | 82ff3c9 | 2015-05-07 15:46:20 +0200 | [diff] [blame] | 12294 | { |
| 12295 | /* If the constant flag is set or if not size is avalaible at |
| 12296 | * the end of the buffer, copy the string in other buffer |
| 12297 | * before decoding. |
| 12298 | */ |
| 12299 | if (smp->flags & SMP_F_CONST || smp->data.str.size <= smp->data.str.len) { |
| 12300 | struct chunk *str = get_trash_chunk(); |
| 12301 | memcpy(str->str, smp->data.str.str, smp->data.str.len); |
| 12302 | smp->data.str.str = str->str; |
| 12303 | smp->data.str.size = str->size; |
| 12304 | smp->flags &= ~SMP_F_CONST; |
| 12305 | } |
| 12306 | |
| 12307 | /* Add final \0 required by url_decode(), and convert the input string. */ |
| 12308 | smp->data.str.str[smp->data.str.len] = '\0'; |
| 12309 | smp->data.str.len = url_decode(smp->data.str.str); |
| 12310 | return 1; |
| 12311 | } |
| 12312 | |
Thierry FOURNIER | 35ab275 | 2015-05-28 13:22:03 +0200 | [diff] [blame] | 12313 | static int smp_conv_req_capture(const struct arg *args, struct sample *smp, void *private) |
| 12314 | { |
| 12315 | struct proxy *fe = strm_fe(smp->strm); |
| 12316 | int idx, i; |
| 12317 | struct cap_hdr *hdr; |
| 12318 | int len; |
| 12319 | |
Thierry FOURNIER | bf65cd4 | 2015-07-20 17:45:02 +0200 | [diff] [blame] | 12320 | if (!args || args->type != ARGT_SINT) |
Thierry FOURNIER | 35ab275 | 2015-05-28 13:22:03 +0200 | [diff] [blame] | 12321 | return 0; |
| 12322 | |
Thierry FOURNIER | bf65cd4 | 2015-07-20 17:45:02 +0200 | [diff] [blame] | 12323 | idx = args->data.sint; |
Thierry FOURNIER | 35ab275 | 2015-05-28 13:22:03 +0200 | [diff] [blame] | 12324 | |
| 12325 | /* Check the availibity of the capture id. */ |
| 12326 | if (idx > fe->nb_req_cap - 1) |
| 12327 | return 0; |
| 12328 | |
| 12329 | /* Look for the original configuration. */ |
| 12330 | for (hdr = fe->req_cap, i = fe->nb_req_cap - 1; |
| 12331 | hdr != NULL && i != idx ; |
| 12332 | i--, hdr = hdr->next); |
| 12333 | if (!hdr) |
| 12334 | return 0; |
| 12335 | |
| 12336 | /* check for the memory allocation */ |
| 12337 | if (smp->strm->req_cap[hdr->index] == NULL) |
| 12338 | smp->strm->req_cap[hdr->index] = pool_alloc2(hdr->pool); |
| 12339 | if (smp->strm->req_cap[hdr->index] == NULL) |
| 12340 | return 0; |
| 12341 | |
| 12342 | /* Check length. */ |
| 12343 | len = smp->data.str.len; |
| 12344 | if (len > hdr->len) |
| 12345 | len = hdr->len; |
| 12346 | |
| 12347 | /* Capture input data. */ |
| 12348 | memcpy(smp->strm->req_cap[idx], smp->data.str.str, len); |
| 12349 | smp->strm->req_cap[idx][len] = '\0'; |
| 12350 | |
| 12351 | return 1; |
| 12352 | } |
| 12353 | |
| 12354 | static int smp_conv_res_capture(const struct arg *args, struct sample *smp, void *private) |
| 12355 | { |
| 12356 | struct proxy *fe = strm_fe(smp->strm); |
| 12357 | int idx, i; |
| 12358 | struct cap_hdr *hdr; |
| 12359 | int len; |
| 12360 | |
Thierry FOURNIER | bf65cd4 | 2015-07-20 17:45:02 +0200 | [diff] [blame] | 12361 | if (!args || args->type != ARGT_SINT) |
Thierry FOURNIER | 35ab275 | 2015-05-28 13:22:03 +0200 | [diff] [blame] | 12362 | return 0; |
| 12363 | |
Thierry FOURNIER | bf65cd4 | 2015-07-20 17:45:02 +0200 | [diff] [blame] | 12364 | idx = args->data.sint; |
Thierry FOURNIER | 35ab275 | 2015-05-28 13:22:03 +0200 | [diff] [blame] | 12365 | |
| 12366 | /* Check the availibity of the capture id. */ |
| 12367 | if (idx > fe->nb_rsp_cap - 1) |
| 12368 | return 0; |
| 12369 | |
| 12370 | /* Look for the original configuration. */ |
| 12371 | for (hdr = fe->rsp_cap, i = fe->nb_rsp_cap - 1; |
| 12372 | hdr != NULL && i != idx ; |
| 12373 | i--, hdr = hdr->next); |
| 12374 | if (!hdr) |
| 12375 | return 0; |
| 12376 | |
| 12377 | /* check for the memory allocation */ |
| 12378 | if (smp->strm->res_cap[hdr->index] == NULL) |
| 12379 | smp->strm->res_cap[hdr->index] = pool_alloc2(hdr->pool); |
| 12380 | if (smp->strm->res_cap[hdr->index] == NULL) |
| 12381 | return 0; |
| 12382 | |
| 12383 | /* Check length. */ |
| 12384 | len = smp->data.str.len; |
| 12385 | if (len > hdr->len) |
| 12386 | len = hdr->len; |
| 12387 | |
| 12388 | /* Capture input data. */ |
| 12389 | memcpy(smp->strm->res_cap[idx], smp->data.str.str, len); |
| 12390 | smp->strm->res_cap[idx][len] = '\0'; |
| 12391 | |
| 12392 | return 1; |
| 12393 | } |
| 12394 | |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 12395 | /* This function executes one of the set-{method,path,query,uri} actions. It |
Thierry FOURNIER | b77aece | 2015-03-14 13:55:46 +0100 | [diff] [blame] | 12396 | * takes the string from the variable 'replace' with length 'len', then modifies |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 12397 | * the relevant part of the request line accordingly. Then it updates various |
| 12398 | * pointers to the next elements which were moved, and the total buffer length. |
| 12399 | * It finds the action to be performed in p[2], previously filled by function |
Thierry FOURNIER | b77aece | 2015-03-14 13:55:46 +0100 | [diff] [blame] | 12400 | * parse_set_req_line(). It returns 0 in case of success, -1 in case of internal |
| 12401 | * error, though this can be revisited when this code is finally exploited. |
| 12402 | * |
| 12403 | * 'action' can be '0' to replace method, '1' to replace path, '2' to replace |
| 12404 | * query string and 3 to replace uri. |
| 12405 | * |
| 12406 | * In query string case, the mark question '?' must be set at the start of the |
| 12407 | * string by the caller, event if the replacement query string is empty. |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 12408 | */ |
Thierry FOURNIER | b77aece | 2015-03-14 13:55:46 +0100 | [diff] [blame] | 12409 | int http_replace_req_line(int action, const char *replace, int len, |
Willy Tarreau | 987e3fb | 2015-04-04 01:09:08 +0200 | [diff] [blame] | 12410 | struct proxy *px, struct stream *s) |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 12411 | { |
Willy Tarreau | 987e3fb | 2015-04-04 01:09:08 +0200 | [diff] [blame] | 12412 | struct http_txn *txn = s->txn; |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 12413 | char *cur_ptr, *cur_end; |
Thierry FOURNIER | b77aece | 2015-03-14 13:55:46 +0100 | [diff] [blame] | 12414 | int offset = 0; |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 12415 | int delta; |
| 12416 | |
Thierry FOURNIER | b77aece | 2015-03-14 13:55:46 +0100 | [diff] [blame] | 12417 | switch (action) { |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 12418 | case 0: // method |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 12419 | cur_ptr = s->req.buf->p; |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 12420 | cur_end = cur_ptr + txn->req.sl.rq.m_l; |
| 12421 | |
| 12422 | /* adjust req line offsets and lengths */ |
Thierry FOURNIER | b77aece | 2015-03-14 13:55:46 +0100 | [diff] [blame] | 12423 | delta = len - offset - (cur_end - cur_ptr); |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 12424 | txn->req.sl.rq.m_l += delta; |
| 12425 | txn->req.sl.rq.u += delta; |
| 12426 | txn->req.sl.rq.v += delta; |
| 12427 | break; |
| 12428 | |
| 12429 | case 1: // path |
| 12430 | cur_ptr = http_get_path(txn); |
| 12431 | if (!cur_ptr) |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 12432 | cur_ptr = s->req.buf->p + txn->req.sl.rq.u; |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 12433 | |
| 12434 | cur_end = cur_ptr; |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 12435 | while (cur_end < s->req.buf->p + txn->req.sl.rq.u + txn->req.sl.rq.u_l && *cur_end != '?') |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 12436 | cur_end++; |
| 12437 | |
| 12438 | /* adjust req line offsets and lengths */ |
Thierry FOURNIER | b77aece | 2015-03-14 13:55:46 +0100 | [diff] [blame] | 12439 | delta = len - offset - (cur_end - cur_ptr); |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 12440 | txn->req.sl.rq.u_l += delta; |
| 12441 | txn->req.sl.rq.v += delta; |
| 12442 | break; |
| 12443 | |
| 12444 | case 2: // query |
Thierry FOURNIER | b77aece | 2015-03-14 13:55:46 +0100 | [diff] [blame] | 12445 | offset = 1; |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 12446 | cur_ptr = s->req.buf->p + txn->req.sl.rq.u; |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 12447 | cur_end = cur_ptr + txn->req.sl.rq.u_l; |
| 12448 | while (cur_ptr < cur_end && *cur_ptr != '?') |
| 12449 | cur_ptr++; |
| 12450 | |
| 12451 | /* skip the question mark or indicate that we must insert it |
| 12452 | * (but only if the format string is not empty then). |
| 12453 | */ |
| 12454 | if (cur_ptr < cur_end) |
| 12455 | cur_ptr++; |
Thierry FOURNIER | b77aece | 2015-03-14 13:55:46 +0100 | [diff] [blame] | 12456 | else if (len > 1) |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 12457 | offset = 0; |
| 12458 | |
| 12459 | /* adjust req line offsets and lengths */ |
Thierry FOURNIER | b77aece | 2015-03-14 13:55:46 +0100 | [diff] [blame] | 12460 | delta = len - offset - (cur_end - cur_ptr); |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 12461 | txn->req.sl.rq.u_l += delta; |
| 12462 | txn->req.sl.rq.v += delta; |
| 12463 | break; |
| 12464 | |
| 12465 | case 3: // uri |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 12466 | cur_ptr = s->req.buf->p + txn->req.sl.rq.u; |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 12467 | cur_end = cur_ptr + txn->req.sl.rq.u_l; |
| 12468 | |
| 12469 | /* adjust req line offsets and lengths */ |
Thierry FOURNIER | b77aece | 2015-03-14 13:55:46 +0100 | [diff] [blame] | 12470 | delta = len - offset - (cur_end - cur_ptr); |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 12471 | txn->req.sl.rq.u_l += delta; |
| 12472 | txn->req.sl.rq.v += delta; |
| 12473 | break; |
| 12474 | |
| 12475 | default: |
Thierry FOURNIER | b77aece | 2015-03-14 13:55:46 +0100 | [diff] [blame] | 12476 | return -1; |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 12477 | } |
| 12478 | |
| 12479 | /* commit changes and adjust end of message */ |
Thierry FOURNIER | b77aece | 2015-03-14 13:55:46 +0100 | [diff] [blame] | 12480 | delta = buffer_replace2(s->req.buf, cur_ptr, cur_end, replace + offset, len - offset); |
Thierry FOURNIER | 7f6192c | 2015-04-26 18:01:40 +0200 | [diff] [blame] | 12481 | txn->req.sl.rq.l += delta; |
| 12482 | txn->hdr_idx.v[0].len += delta; |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 12483 | http_msg_move_end(&txn->req, delta); |
Thierry FOURNIER | b77aece | 2015-03-14 13:55:46 +0100 | [diff] [blame] | 12484 | return 0; |
| 12485 | } |
| 12486 | |
| 12487 | /* This function executes one of the set-{method,path,query,uri} actions. It |
| 12488 | * builds a string in the trash from the specified format string. It finds |
| 12489 | * the action to be performed in p[2], previously filled by function |
| 12490 | * parse_set_req_line(). The replacement action is excuted by the function |
| 12491 | * http_action_set_req_line_exec(). It always returns 1. If an error occurs |
| 12492 | * the action is canceled, but the rule processing continue. |
| 12493 | */ |
Willy Tarreau | 987e3fb | 2015-04-04 01:09:08 +0200 | [diff] [blame] | 12494 | int http_action_set_req_line(struct http_req_rule *rule, struct proxy *px, struct stream *s) |
Thierry FOURNIER | b77aece | 2015-03-14 13:55:46 +0100 | [diff] [blame] | 12495 | { |
| 12496 | chunk_reset(&trash); |
| 12497 | |
| 12498 | /* If we have to create a query string, prepare a '?'. */ |
| 12499 | if (*(int *)&rule->arg.act.p[2] == 2) |
| 12500 | trash.str[trash.len++] = '?'; |
| 12501 | trash.len += build_logline(s, trash.str + trash.len, trash.size - trash.len, (struct list *)&rule->arg.act.p[0]); |
| 12502 | |
Willy Tarreau | 987e3fb | 2015-04-04 01:09:08 +0200 | [diff] [blame] | 12503 | http_replace_req_line(*(int *)&rule->arg.act.p[2], trash.str, trash.len, px, s); |
Thierry FOURNIER | 01c3012 | 2015-03-14 14:14:47 +0100 | [diff] [blame] | 12504 | return 1; |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 12505 | } |
| 12506 | |
| 12507 | /* parse an http-request action among : |
| 12508 | * set-method |
| 12509 | * set-path |
| 12510 | * set-query |
| 12511 | * set-uri |
| 12512 | * |
| 12513 | * All of them accept a single argument of type string representing a log-format. |
| 12514 | * The resulting rule makes use of arg->act.p[0..1] to store the log-format list |
| 12515 | * head, and p[2] to store the action as an int (0=method, 1=path, 2=query, 3=uri). |
| 12516 | * It returns 0 on success, < 0 on error. |
| 12517 | */ |
| 12518 | int parse_set_req_line(const char **args, int *orig_arg, struct proxy *px, struct http_req_rule *rule, char **err) |
| 12519 | { |
| 12520 | int cur_arg = *orig_arg; |
| 12521 | |
| 12522 | rule->action = HTTP_REQ_ACT_CUSTOM_CONT; |
| 12523 | |
| 12524 | switch (args[0][4]) { |
| 12525 | case 'm' : |
| 12526 | *(int *)&rule->arg.act.p[2] = 0; |
| 12527 | rule->action_ptr = http_action_set_req_line; |
| 12528 | break; |
| 12529 | case 'p' : |
| 12530 | *(int *)&rule->arg.act.p[2] = 1; |
| 12531 | rule->action_ptr = http_action_set_req_line; |
| 12532 | break; |
| 12533 | case 'q' : |
| 12534 | *(int *)&rule->arg.act.p[2] = 2; |
| 12535 | rule->action_ptr = http_action_set_req_line; |
| 12536 | break; |
| 12537 | case 'u' : |
| 12538 | *(int *)&rule->arg.act.p[2] = 3; |
| 12539 | rule->action_ptr = http_action_set_req_line; |
| 12540 | break; |
| 12541 | default: |
| 12542 | memprintf(err, "internal error: unhandled action '%s'", args[0]); |
| 12543 | return -1; |
| 12544 | } |
| 12545 | |
| 12546 | if (!*args[cur_arg] || |
| 12547 | (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) { |
| 12548 | memprintf(err, "expects exactly 1 argument <format>"); |
| 12549 | return -1; |
| 12550 | } |
| 12551 | |
| 12552 | LIST_INIT((struct list *)&rule->arg.act.p[0]); |
| 12553 | proxy->conf.args.ctx = ARGC_HRQ; |
| 12554 | parse_logformat_string(args[cur_arg], proxy, (struct list *)&rule->arg.act.p[0], LOG_OPT_HTTP, |
| 12555 | (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR, |
| 12556 | proxy->conf.args.file, proxy->conf.args.line); |
| 12557 | |
| 12558 | (*orig_arg)++; |
| 12559 | return 0; |
| 12560 | } |
| 12561 | |
Willy Tarreau | a9083d0 | 2015-05-08 15:27:59 +0200 | [diff] [blame] | 12562 | /* This function executes the "capture" action. It executes a fetch expression, |
| 12563 | * turns the result into a string and puts it in a capture slot. It always |
| 12564 | * returns 1. If an error occurs the action is cancelled, but the rule |
| 12565 | * processing continues. |
| 12566 | */ |
| 12567 | int http_action_req_capture(struct http_req_rule *rule, struct proxy *px, struct stream *s) |
| 12568 | { |
| 12569 | struct session *sess = s->sess; |
| 12570 | struct sample *key; |
| 12571 | struct sample_expr *expr = rule->arg.act.p[0]; |
| 12572 | struct cap_hdr *h = rule->arg.act.p[1]; |
| 12573 | char **cap = s->req_cap; |
| 12574 | int len; |
| 12575 | |
Adis Nezirovic | 79beb24 | 2015-07-06 15:41:02 +0200 | [diff] [blame] | 12576 | key = sample_fetch_as_type(s->be, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL, expr, SMP_T_STR); |
Willy Tarreau | a9083d0 | 2015-05-08 15:27:59 +0200 | [diff] [blame] | 12577 | if (!key) |
| 12578 | return 1; |
| 12579 | |
| 12580 | if (cap[h->index] == NULL) |
| 12581 | cap[h->index] = pool_alloc2(h->pool); |
| 12582 | |
| 12583 | if (cap[h->index] == NULL) /* no more capture memory */ |
| 12584 | return 1; |
| 12585 | |
| 12586 | len = key->data.str.len; |
| 12587 | if (len > h->len) |
| 12588 | len = h->len; |
| 12589 | |
| 12590 | memcpy(cap[h->index], key->data.str.str, len); |
| 12591 | cap[h->index][len] = 0; |
| 12592 | return 1; |
| 12593 | } |
| 12594 | |
Thierry FOURNIER | 82bf70d | 2015-05-26 17:58:29 +0200 | [diff] [blame] | 12595 | /* This function executes the "capture" action and store the result in a |
| 12596 | * capture slot if exists. It executes a fetch expression, turns the result |
| 12597 | * into a string and puts it in a capture slot. It always returns 1. If an |
| 12598 | * error occurs the action is cancelled, but the rule processing continues. |
| 12599 | */ |
| 12600 | int http_action_req_capture_by_id(struct http_req_rule *rule, struct proxy *px, struct stream *s) |
| 12601 | { |
| 12602 | struct session *sess = s->sess; |
| 12603 | struct sample *key; |
| 12604 | struct sample_expr *expr = rule->arg.act.p[0]; |
| 12605 | struct cap_hdr *h; |
| 12606 | int idx = (long)rule->arg.act.p[1]; |
| 12607 | char **cap = s->req_cap; |
| 12608 | struct proxy *fe = strm_fe(s); |
| 12609 | int len; |
| 12610 | int i; |
| 12611 | |
| 12612 | /* Look for the original configuration. */ |
| 12613 | for (h = fe->req_cap, i = fe->nb_req_cap - 1; |
| 12614 | h != NULL && i != idx ; |
| 12615 | i--, h = h->next); |
| 12616 | if (!h) |
| 12617 | return 1; |
| 12618 | |
Adis Nezirovic | 79beb24 | 2015-07-06 15:41:02 +0200 | [diff] [blame] | 12619 | key = sample_fetch_as_type(s->be, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL, expr, SMP_T_STR); |
Thierry FOURNIER | 82bf70d | 2015-05-26 17:58:29 +0200 | [diff] [blame] | 12620 | if (!key) |
| 12621 | return 1; |
| 12622 | |
| 12623 | if (cap[h->index] == NULL) |
| 12624 | cap[h->index] = pool_alloc2(h->pool); |
| 12625 | |
| 12626 | if (cap[h->index] == NULL) /* no more capture memory */ |
| 12627 | return 1; |
| 12628 | |
| 12629 | len = key->data.str.len; |
| 12630 | if (len > h->len) |
| 12631 | len = h->len; |
| 12632 | |
| 12633 | memcpy(cap[h->index], key->data.str.str, len); |
| 12634 | cap[h->index][len] = 0; |
| 12635 | return 1; |
| 12636 | } |
| 12637 | |
Willy Tarreau | a9083d0 | 2015-05-08 15:27:59 +0200 | [diff] [blame] | 12638 | /* parse an "http-request capture" action. It takes a single argument which is |
| 12639 | * a sample fetch expression. It stores the expression into arg->act.p[0] and |
Thierry FOURNIER | 82bf70d | 2015-05-26 17:58:29 +0200 | [diff] [blame] | 12640 | * the allocated hdr_cap struct or the preallocated "id" into arg->act.p[1]. |
Willy Tarreau | a9083d0 | 2015-05-08 15:27:59 +0200 | [diff] [blame] | 12641 | * It returns 0 on success, < 0 on error. |
| 12642 | */ |
| 12643 | int parse_http_req_capture(const char **args, int *orig_arg, struct proxy *px, struct http_req_rule *rule, char **err) |
| 12644 | { |
| 12645 | struct sample_expr *expr; |
| 12646 | struct cap_hdr *hdr; |
| 12647 | int cur_arg; |
Willy Tarreau | 3986ac1 | 2015-05-08 16:13:42 +0200 | [diff] [blame] | 12648 | int len = 0; |
Willy Tarreau | a9083d0 | 2015-05-08 15:27:59 +0200 | [diff] [blame] | 12649 | |
| 12650 | for (cur_arg = *orig_arg; cur_arg < *orig_arg + 3 && *args[cur_arg]; cur_arg++) |
| 12651 | if (strcmp(args[cur_arg], "if") == 0 || |
| 12652 | strcmp(args[cur_arg], "unless") == 0) |
| 12653 | break; |
| 12654 | |
| 12655 | if (cur_arg < *orig_arg + 3) { |
Thierry FOURNIER | 82bf70d | 2015-05-26 17:58:29 +0200 | [diff] [blame] | 12656 | memprintf(err, "expects <expression> [ 'len' <length> | id <idx> ]"); |
Willy Tarreau | a9083d0 | 2015-05-08 15:27:59 +0200 | [diff] [blame] | 12657 | return -1; |
| 12658 | } |
| 12659 | |
Willy Tarreau | a9083d0 | 2015-05-08 15:27:59 +0200 | [diff] [blame] | 12660 | cur_arg = *orig_arg; |
| 12661 | expr = sample_parse_expr((char **)args, &cur_arg, px->conf.args.file, px->conf.args.line, err, &px->conf.args); |
| 12662 | if (!expr) |
| 12663 | return -1; |
| 12664 | |
| 12665 | if (!(expr->fetch->val & SMP_VAL_FE_HRQ_HDR)) { |
| 12666 | memprintf(err, |
| 12667 | "fetch method '%s' extracts information from '%s', none of which is available here", |
| 12668 | args[cur_arg-1], sample_src_names(expr->fetch->use)); |
| 12669 | free(expr); |
| 12670 | return -1; |
| 12671 | } |
| 12672 | |
Thierry FOURNIER | 82bf70d | 2015-05-26 17:58:29 +0200 | [diff] [blame] | 12673 | if (!args[cur_arg] || !*args[cur_arg]) { |
| 12674 | memprintf(err, "expects 'len or 'id'"); |
| 12675 | free(expr); |
| 12676 | return -1; |
| 12677 | } |
| 12678 | |
Willy Tarreau | a9083d0 | 2015-05-08 15:27:59 +0200 | [diff] [blame] | 12679 | if (strcmp(args[cur_arg], "len") == 0) { |
| 12680 | cur_arg++; |
Thierry FOURNIER | 82bf70d | 2015-05-26 17:58:29 +0200 | [diff] [blame] | 12681 | |
| 12682 | if (!(px->cap & PR_CAP_FE)) { |
| 12683 | memprintf(err, "proxy '%s' has no frontend capability", px->id); |
| 12684 | return -1; |
| 12685 | } |
| 12686 | |
| 12687 | proxy->conf.args.ctx = ARGC_CAP; |
| 12688 | |
Willy Tarreau | a9083d0 | 2015-05-08 15:27:59 +0200 | [diff] [blame] | 12689 | if (!args[cur_arg]) { |
| 12690 | memprintf(err, "missing length value"); |
| 12691 | free(expr); |
| 12692 | return -1; |
| 12693 | } |
| 12694 | /* we copy the table name for now, it will be resolved later */ |
| 12695 | len = atoi(args[cur_arg]); |
| 12696 | if (len <= 0) { |
| 12697 | memprintf(err, "length must be > 0"); |
| 12698 | free(expr); |
| 12699 | return -1; |
| 12700 | } |
| 12701 | cur_arg++; |
Willy Tarreau | a9083d0 | 2015-05-08 15:27:59 +0200 | [diff] [blame] | 12702 | |
Thierry FOURNIER | 82bf70d | 2015-05-26 17:58:29 +0200 | [diff] [blame] | 12703 | if (!len) { |
| 12704 | memprintf(err, "a positive 'len' argument is mandatory"); |
| 12705 | free(expr); |
| 12706 | return -1; |
| 12707 | } |
| 12708 | |
| 12709 | hdr = calloc(sizeof(struct cap_hdr), 1); |
| 12710 | hdr->next = px->req_cap; |
| 12711 | hdr->name = NULL; /* not a header capture */ |
| 12712 | hdr->namelen = 0; |
| 12713 | hdr->len = len; |
| 12714 | hdr->pool = create_pool("caphdr", hdr->len + 1, MEM_F_SHARED); |
| 12715 | hdr->index = px->nb_req_cap++; |
| 12716 | |
| 12717 | px->req_cap = hdr; |
| 12718 | px->to_log |= LW_REQHDR; |
| 12719 | |
| 12720 | rule->action = HTTP_REQ_ACT_CUSTOM_CONT; |
| 12721 | rule->action_ptr = http_action_req_capture; |
| 12722 | rule->arg.act.p[0] = expr; |
| 12723 | rule->arg.act.p[1] = hdr; |
Willy Tarreau | a9083d0 | 2015-05-08 15:27:59 +0200 | [diff] [blame] | 12724 | } |
| 12725 | |
Thierry FOURNIER | 82bf70d | 2015-05-26 17:58:29 +0200 | [diff] [blame] | 12726 | else if (strcmp(args[cur_arg], "id") == 0) { |
| 12727 | int id; |
| 12728 | char *error; |
Willy Tarreau | a9083d0 | 2015-05-08 15:27:59 +0200 | [diff] [blame] | 12729 | |
Thierry FOURNIER | 82bf70d | 2015-05-26 17:58:29 +0200 | [diff] [blame] | 12730 | cur_arg++; |
Willy Tarreau | a9083d0 | 2015-05-08 15:27:59 +0200 | [diff] [blame] | 12731 | |
Thierry FOURNIER | 82bf70d | 2015-05-26 17:58:29 +0200 | [diff] [blame] | 12732 | if (!args[cur_arg]) { |
| 12733 | memprintf(err, "missing id value"); |
| 12734 | free(expr); |
| 12735 | return -1; |
| 12736 | } |
Willy Tarreau | a9083d0 | 2015-05-08 15:27:59 +0200 | [diff] [blame] | 12737 | |
Thierry FOURNIER | 82bf70d | 2015-05-26 17:58:29 +0200 | [diff] [blame] | 12738 | id = strtol(args[cur_arg], &error, 10); |
| 12739 | if (*error != '\0') { |
| 12740 | memprintf(err, "cannot parse id '%s'", args[cur_arg]); |
| 12741 | free(expr); |
| 12742 | return -1; |
| 12743 | } |
| 12744 | cur_arg++; |
| 12745 | |
| 12746 | proxy->conf.args.ctx = ARGC_CAP; |
| 12747 | |
| 12748 | rule->action = HTTP_REQ_ACT_CUSTOM_CONT; |
| 12749 | rule->action_ptr = http_action_req_capture_by_id; |
| 12750 | rule->arg.act.p[0] = expr; |
| 12751 | rule->arg.act.p[1] = (void *)(long)id; |
| 12752 | } |
| 12753 | |
| 12754 | else { |
| 12755 | memprintf(err, "expects 'len' or 'id', found '%s'", args[cur_arg]); |
| 12756 | free(expr); |
| 12757 | return -1; |
| 12758 | } |
Willy Tarreau | a9083d0 | 2015-05-08 15:27:59 +0200 | [diff] [blame] | 12759 | |
| 12760 | *orig_arg = cur_arg; |
| 12761 | return 0; |
| 12762 | } |
| 12763 | |
Thierry FOURNIER | e80fada | 2015-05-26 18:06:31 +0200 | [diff] [blame] | 12764 | /* This function executes the "capture" action and store the result in a |
| 12765 | * capture slot if exists. It executes a fetch expression, turns the result |
| 12766 | * into a string and puts it in a capture slot. It always returns 1. If an |
| 12767 | * error occurs the action is cancelled, but the rule processing continues. |
| 12768 | */ |
| 12769 | int http_action_res_capture_by_id(struct http_res_rule *rule, struct proxy *px, struct stream *s) |
| 12770 | { |
| 12771 | struct session *sess = s->sess; |
| 12772 | struct sample *key; |
| 12773 | struct sample_expr *expr = rule->arg.act.p[0]; |
| 12774 | struct cap_hdr *h; |
| 12775 | int idx = (long)rule->arg.act.p[1]; |
| 12776 | char **cap = s->res_cap; |
| 12777 | struct proxy *fe = strm_fe(s); |
| 12778 | int len; |
| 12779 | int i; |
| 12780 | |
| 12781 | /* Look for the original configuration. */ |
| 12782 | for (h = fe->rsp_cap, i = fe->nb_rsp_cap - 1; |
| 12783 | h != NULL && i != idx ; |
| 12784 | i--, h = h->next); |
| 12785 | if (!h) |
| 12786 | return 1; |
| 12787 | |
Adis Nezirovic | 79beb24 | 2015-07-06 15:41:02 +0200 | [diff] [blame] | 12788 | key = sample_fetch_as_type(s->be, sess, s, SMP_OPT_DIR_RES|SMP_OPT_FINAL, expr, SMP_T_STR); |
Thierry FOURNIER | e80fada | 2015-05-26 18:06:31 +0200 | [diff] [blame] | 12789 | if (!key) |
| 12790 | return 1; |
| 12791 | |
| 12792 | if (cap[h->index] == NULL) |
| 12793 | cap[h->index] = pool_alloc2(h->pool); |
| 12794 | |
| 12795 | if (cap[h->index] == NULL) /* no more capture memory */ |
| 12796 | return 1; |
| 12797 | |
| 12798 | len = key->data.str.len; |
| 12799 | if (len > h->len) |
| 12800 | len = h->len; |
| 12801 | |
| 12802 | memcpy(cap[h->index], key->data.str.str, len); |
| 12803 | cap[h->index][len] = 0; |
| 12804 | return 1; |
| 12805 | } |
| 12806 | |
| 12807 | /* parse an "http-response capture" action. It takes a single argument which is |
| 12808 | * a sample fetch expression. It stores the expression into arg->act.p[0] and |
| 12809 | * the allocated hdr_cap struct od the preallocated id into arg->act.p[1]. |
| 12810 | * It returns 0 on success, < 0 on error. |
| 12811 | */ |
| 12812 | int parse_http_res_capture(const char **args, int *orig_arg, struct proxy *px, struct http_res_rule *rule, char **err) |
| 12813 | { |
| 12814 | struct sample_expr *expr; |
| 12815 | int cur_arg; |
| 12816 | int id; |
| 12817 | char *error; |
| 12818 | |
| 12819 | for (cur_arg = *orig_arg; cur_arg < *orig_arg + 3 && *args[cur_arg]; cur_arg++) |
| 12820 | if (strcmp(args[cur_arg], "if") == 0 || |
| 12821 | strcmp(args[cur_arg], "unless") == 0) |
| 12822 | break; |
| 12823 | |
| 12824 | if (cur_arg < *orig_arg + 3) { |
| 12825 | memprintf(err, "expects <expression> [ 'len' <length> | id <idx> ]"); |
| 12826 | return -1; |
| 12827 | } |
| 12828 | |
| 12829 | cur_arg = *orig_arg; |
| 12830 | expr = sample_parse_expr((char **)args, &cur_arg, px->conf.args.file, px->conf.args.line, err, &px->conf.args); |
| 12831 | if (!expr) |
| 12832 | return -1; |
| 12833 | |
| 12834 | if (!(expr->fetch->val & SMP_VAL_FE_HRS_HDR)) { |
| 12835 | memprintf(err, |
| 12836 | "fetch method '%s' extracts information from '%s', none of which is available here", |
| 12837 | args[cur_arg-1], sample_src_names(expr->fetch->use)); |
| 12838 | free(expr); |
| 12839 | return -1; |
| 12840 | } |
| 12841 | |
| 12842 | if (!args[cur_arg] || !*args[cur_arg]) { |
| 12843 | memprintf(err, "expects 'len or 'id'"); |
| 12844 | free(expr); |
| 12845 | return -1; |
| 12846 | } |
| 12847 | |
| 12848 | if (strcmp(args[cur_arg], "id") != 0) { |
| 12849 | memprintf(err, "expects 'id', found '%s'", args[cur_arg]); |
| 12850 | free(expr); |
| 12851 | return -1; |
| 12852 | } |
| 12853 | |
| 12854 | cur_arg++; |
| 12855 | |
| 12856 | if (!args[cur_arg]) { |
| 12857 | memprintf(err, "missing id value"); |
| 12858 | free(expr); |
| 12859 | return -1; |
| 12860 | } |
| 12861 | |
| 12862 | id = strtol(args[cur_arg], &error, 10); |
| 12863 | if (*error != '\0') { |
| 12864 | memprintf(err, "cannot parse id '%s'", args[cur_arg]); |
| 12865 | free(expr); |
| 12866 | return -1; |
| 12867 | } |
| 12868 | cur_arg++; |
| 12869 | |
| 12870 | LIST_INIT((struct list *)&rule->arg.act.p[0]); |
| 12871 | proxy->conf.args.ctx = ARGC_CAP; |
| 12872 | |
| 12873 | rule->action = HTTP_RES_ACT_CUSTOM_CONT; |
| 12874 | rule->action_ptr = http_action_res_capture_by_id; |
| 12875 | rule->arg.act.p[0] = expr; |
| 12876 | rule->arg.act.p[1] = (void *)(long)id; |
| 12877 | |
| 12878 | *orig_arg = cur_arg; |
| 12879 | return 0; |
| 12880 | } |
| 12881 | |
William Lallemand | 73025dd | 2014-04-24 14:38:37 +0200 | [diff] [blame] | 12882 | /* |
| 12883 | * Return the struct http_req_action_kw associated to a keyword. |
| 12884 | */ |
| 12885 | struct http_req_action_kw *action_http_req_custom(const char *kw) |
| 12886 | { |
| 12887 | if (!LIST_ISEMPTY(&http_req_keywords.list)) { |
| 12888 | struct http_req_action_kw_list *kw_list; |
| 12889 | int i; |
| 12890 | |
| 12891 | list_for_each_entry(kw_list, &http_req_keywords.list, list) { |
| 12892 | for (i = 0; kw_list->kw[i].kw != NULL; i++) { |
Thierry FOURNIER | 0e11863 | 2015-06-04 11:44:06 +0200 | [diff] [blame] | 12893 | if (kw_list->kw[i].match_pfx && |
| 12894 | strncmp(kw, kw_list->kw[i].kw, strlen(kw_list->kw[i].kw)) == 0) |
| 12895 | return &kw_list->kw[i]; |
William Lallemand | 73025dd | 2014-04-24 14:38:37 +0200 | [diff] [blame] | 12896 | if (!strcmp(kw, kw_list->kw[i].kw)) |
| 12897 | return &kw_list->kw[i]; |
| 12898 | } |
| 12899 | } |
| 12900 | } |
| 12901 | return NULL; |
| 12902 | } |
| 12903 | |
| 12904 | /* |
| 12905 | * Return the struct http_res_action_kw associated to a keyword. |
| 12906 | */ |
| 12907 | struct http_res_action_kw *action_http_res_custom(const char *kw) |
| 12908 | { |
| 12909 | if (!LIST_ISEMPTY(&http_res_keywords.list)) { |
| 12910 | struct http_res_action_kw_list *kw_list; |
| 12911 | int i; |
| 12912 | |
| 12913 | list_for_each_entry(kw_list, &http_res_keywords.list, list) { |
| 12914 | for (i = 0; kw_list->kw[i].kw != NULL; i++) { |
Thierry FOURNIER | 0e11863 | 2015-06-04 11:44:06 +0200 | [diff] [blame] | 12915 | if (kw_list->kw[i].match_pfx && |
| 12916 | strncmp(kw, kw_list->kw[i].kw, strlen(kw_list->kw[i].kw)) == 0) |
| 12917 | return &kw_list->kw[i]; |
William Lallemand | 73025dd | 2014-04-24 14:38:37 +0200 | [diff] [blame] | 12918 | if (!strcmp(kw, kw_list->kw[i].kw)) |
| 12919 | return &kw_list->kw[i]; |
| 12920 | } |
| 12921 | } |
| 12922 | } |
| 12923 | return NULL; |
| 12924 | } |
| 12925 | |
Willy Tarreau | 4a56897 | 2010-05-12 08:08:50 +0200 | [diff] [blame] | 12926 | /************************************************************************/ |
Willy Tarreau | 25c1ebc | 2012-04-25 16:21:44 +0200 | [diff] [blame] | 12927 | /* All supported ACL keywords must be declared here. */ |
| 12928 | /************************************************************************/ |
| 12929 | |
| 12930 | /* Note: must not be declared <const> as its list will be overwritten. |
| 12931 | * Please take care of keeping this list alphabetically sorted. |
| 12932 | */ |
Willy Tarreau | dc13c11 | 2013-06-21 23:16:39 +0200 | [diff] [blame] | 12933 | static struct acl_kw_list acl_kws = {ILH, { |
Thierry FOURNIER | c5a4e98 | 2014-03-05 16:07:08 +0100 | [diff] [blame] | 12934 | { "base", "base", PAT_MATCH_STR }, |
| 12935 | { "base_beg", "base", PAT_MATCH_BEG }, |
| 12936 | { "base_dir", "base", PAT_MATCH_DIR }, |
| 12937 | { "base_dom", "base", PAT_MATCH_DOM }, |
| 12938 | { "base_end", "base", PAT_MATCH_END }, |
| 12939 | { "base_len", "base", PAT_MATCH_LEN }, |
| 12940 | { "base_reg", "base", PAT_MATCH_REG }, |
| 12941 | { "base_sub", "base", PAT_MATCH_SUB }, |
Willy Tarreau | a7ad50c | 2012-04-29 15:39:40 +0200 | [diff] [blame] | 12942 | |
Thierry FOURNIER | c5a4e98 | 2014-03-05 16:07:08 +0100 | [diff] [blame] | 12943 | { "cook", "req.cook", PAT_MATCH_STR }, |
| 12944 | { "cook_beg", "req.cook", PAT_MATCH_BEG }, |
| 12945 | { "cook_dir", "req.cook", PAT_MATCH_DIR }, |
| 12946 | { "cook_dom", "req.cook", PAT_MATCH_DOM }, |
| 12947 | { "cook_end", "req.cook", PAT_MATCH_END }, |
| 12948 | { "cook_len", "req.cook", PAT_MATCH_LEN }, |
| 12949 | { "cook_reg", "req.cook", PAT_MATCH_REG }, |
| 12950 | { "cook_sub", "req.cook", PAT_MATCH_SUB }, |
Willy Tarreau | 25c1ebc | 2012-04-25 16:21:44 +0200 | [diff] [blame] | 12951 | |
Thierry FOURNIER | c5a4e98 | 2014-03-05 16:07:08 +0100 | [diff] [blame] | 12952 | { "hdr", "req.hdr", PAT_MATCH_STR }, |
| 12953 | { "hdr_beg", "req.hdr", PAT_MATCH_BEG }, |
| 12954 | { "hdr_dir", "req.hdr", PAT_MATCH_DIR }, |
| 12955 | { "hdr_dom", "req.hdr", PAT_MATCH_DOM }, |
| 12956 | { "hdr_end", "req.hdr", PAT_MATCH_END }, |
| 12957 | { "hdr_len", "req.hdr", PAT_MATCH_LEN }, |
| 12958 | { "hdr_reg", "req.hdr", PAT_MATCH_REG }, |
| 12959 | { "hdr_sub", "req.hdr", PAT_MATCH_SUB }, |
Willy Tarreau | 25c1ebc | 2012-04-25 16:21:44 +0200 | [diff] [blame] | 12960 | |
Thierry FOURNIER | c5a4e98 | 2014-03-05 16:07:08 +0100 | [diff] [blame] | 12961 | /* these two declarations uses strings with list storage (in place |
| 12962 | * of tree storage). The basic match is PAT_MATCH_STR, but the indexation |
| 12963 | * and delete functions are relative to the list management. The parse |
| 12964 | * and match method are related to the corresponding fetch methods. This |
| 12965 | * is very particular ACL declaration mode. |
| 12966 | */ |
| 12967 | { "http_auth_group", NULL, PAT_MATCH_STR, NULL, pat_idx_list_str, pat_del_list_ptr, NULL, pat_match_auth }, |
| 12968 | { "method", NULL, PAT_MATCH_STR, pat_parse_meth, pat_idx_list_str, pat_del_list_ptr, NULL, pat_match_meth }, |
Willy Tarreau | 25c1ebc | 2012-04-25 16:21:44 +0200 | [diff] [blame] | 12969 | |
Thierry FOURNIER | c5a4e98 | 2014-03-05 16:07:08 +0100 | [diff] [blame] | 12970 | { "path", "path", PAT_MATCH_STR }, |
| 12971 | { "path_beg", "path", PAT_MATCH_BEG }, |
| 12972 | { "path_dir", "path", PAT_MATCH_DIR }, |
| 12973 | { "path_dom", "path", PAT_MATCH_DOM }, |
| 12974 | { "path_end", "path", PAT_MATCH_END }, |
| 12975 | { "path_len", "path", PAT_MATCH_LEN }, |
| 12976 | { "path_reg", "path", PAT_MATCH_REG }, |
| 12977 | { "path_sub", "path", PAT_MATCH_SUB }, |
Willy Tarreau | 25c1ebc | 2012-04-25 16:21:44 +0200 | [diff] [blame] | 12978 | |
Thierry FOURNIER | c5a4e98 | 2014-03-05 16:07:08 +0100 | [diff] [blame] | 12979 | { "req_ver", "req.ver", PAT_MATCH_STR }, |
| 12980 | { "resp_ver", "res.ver", PAT_MATCH_STR }, |
Willy Tarreau | 25c1ebc | 2012-04-25 16:21:44 +0200 | [diff] [blame] | 12981 | |
Thierry FOURNIER | c5a4e98 | 2014-03-05 16:07:08 +0100 | [diff] [blame] | 12982 | { "scook", "res.cook", PAT_MATCH_STR }, |
| 12983 | { "scook_beg", "res.cook", PAT_MATCH_BEG }, |
| 12984 | { "scook_dir", "res.cook", PAT_MATCH_DIR }, |
| 12985 | { "scook_dom", "res.cook", PAT_MATCH_DOM }, |
| 12986 | { "scook_end", "res.cook", PAT_MATCH_END }, |
| 12987 | { "scook_len", "res.cook", PAT_MATCH_LEN }, |
| 12988 | { "scook_reg", "res.cook", PAT_MATCH_REG }, |
| 12989 | { "scook_sub", "res.cook", PAT_MATCH_SUB }, |
Willy Tarreau | 25c1ebc | 2012-04-25 16:21:44 +0200 | [diff] [blame] | 12990 | |
Thierry FOURNIER | c5a4e98 | 2014-03-05 16:07:08 +0100 | [diff] [blame] | 12991 | { "shdr", "res.hdr", PAT_MATCH_STR }, |
| 12992 | { "shdr_beg", "res.hdr", PAT_MATCH_BEG }, |
| 12993 | { "shdr_dir", "res.hdr", PAT_MATCH_DIR }, |
| 12994 | { "shdr_dom", "res.hdr", PAT_MATCH_DOM }, |
| 12995 | { "shdr_end", "res.hdr", PAT_MATCH_END }, |
| 12996 | { "shdr_len", "res.hdr", PAT_MATCH_LEN }, |
| 12997 | { "shdr_reg", "res.hdr", PAT_MATCH_REG }, |
| 12998 | { "shdr_sub", "res.hdr", PAT_MATCH_SUB }, |
Willy Tarreau | 25c1ebc | 2012-04-25 16:21:44 +0200 | [diff] [blame] | 12999 | |
Thierry FOURNIER | c5a4e98 | 2014-03-05 16:07:08 +0100 | [diff] [blame] | 13000 | { "url", "url", PAT_MATCH_STR }, |
| 13001 | { "url_beg", "url", PAT_MATCH_BEG }, |
| 13002 | { "url_dir", "url", PAT_MATCH_DIR }, |
| 13003 | { "url_dom", "url", PAT_MATCH_DOM }, |
| 13004 | { "url_end", "url", PAT_MATCH_END }, |
| 13005 | { "url_len", "url", PAT_MATCH_LEN }, |
| 13006 | { "url_reg", "url", PAT_MATCH_REG }, |
| 13007 | { "url_sub", "url", PAT_MATCH_SUB }, |
Willy Tarreau | 25c1ebc | 2012-04-25 16:21:44 +0200 | [diff] [blame] | 13008 | |
Thierry FOURNIER | c5a4e98 | 2014-03-05 16:07:08 +0100 | [diff] [blame] | 13009 | { "urlp", "urlp", PAT_MATCH_STR }, |
| 13010 | { "urlp_beg", "urlp", PAT_MATCH_BEG }, |
| 13011 | { "urlp_dir", "urlp", PAT_MATCH_DIR }, |
| 13012 | { "urlp_dom", "urlp", PAT_MATCH_DOM }, |
| 13013 | { "urlp_end", "urlp", PAT_MATCH_END }, |
| 13014 | { "urlp_len", "urlp", PAT_MATCH_LEN }, |
| 13015 | { "urlp_reg", "urlp", PAT_MATCH_REG }, |
| 13016 | { "urlp_sub", "urlp", PAT_MATCH_SUB }, |
Willy Tarreau | 25c1ebc | 2012-04-25 16:21:44 +0200 | [diff] [blame] | 13017 | |
Willy Tarreau | 8ed669b | 2013-01-11 15:49:37 +0100 | [diff] [blame] | 13018 | { /* END */ }, |
Willy Tarreau | 25c1ebc | 2012-04-25 16:21:44 +0200 | [diff] [blame] | 13019 | }}; |
| 13020 | |
| 13021 | /************************************************************************/ |
| 13022 | /* All supported pattern keywords must be declared here. */ |
Willy Tarreau | 4a56897 | 2010-05-12 08:08:50 +0200 | [diff] [blame] | 13023 | /************************************************************************/ |
| 13024 | /* Note: must not be declared <const> as its list will be overwritten */ |
Willy Tarreau | dc13c11 | 2013-06-21 23:16:39 +0200 | [diff] [blame] | 13025 | static struct sample_fetch_kw_list sample_fetch_keywords = {ILH, { |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 13026 | { "base", smp_fetch_base, 0, NULL, SMP_T_STR, SMP_USE_HRQHV }, |
Thierry FOURNIER | 07ee64e | 2015-07-06 23:43:03 +0200 | [diff] [blame] | 13027 | { "base32", smp_fetch_base32, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV }, |
Willy Tarreau | 409bcde | 2013-01-08 00:31:00 +0100 | [diff] [blame] | 13028 | { "base32+src", smp_fetch_base32_src, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV }, |
| 13029 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 13030 | /* capture are allocated and are permanent in the stream */ |
Thierry FOURNIER | bf65cd4 | 2015-07-20 17:45:02 +0200 | [diff] [blame] | 13031 | { "capture.req.hdr", smp_fetch_capture_header_req, ARG1(1,SINT), NULL, SMP_T_STR, SMP_USE_HRQHP }, |
Willy Tarreau | 3c1b5ec | 2014-04-24 23:41:57 +0200 | [diff] [blame] | 13032 | |
| 13033 | /* retrieve these captures from the HTTP logs */ |
Thierry FOURNIER | bf65cd4 | 2015-07-20 17:45:02 +0200 | [diff] [blame] | 13034 | { "capture.req.method", smp_fetch_capture_req_method, 0, NULL, SMP_T_STR, SMP_USE_HRQHP }, |
| 13035 | { "capture.req.uri", smp_fetch_capture_req_uri, 0, NULL, SMP_T_STR, SMP_USE_HRQHP }, |
| 13036 | { "capture.req.ver", smp_fetch_capture_req_ver, 0, NULL, SMP_T_STR, SMP_USE_HRQHP }, |
Willy Tarreau | 3c1b5ec | 2014-04-24 23:41:57 +0200 | [diff] [blame] | 13037 | |
Thierry FOURNIER | bf65cd4 | 2015-07-20 17:45:02 +0200 | [diff] [blame] | 13038 | { "capture.res.hdr", smp_fetch_capture_header_res, ARG1(1,SINT), NULL, SMP_T_STR, SMP_USE_HRSHP }, |
| 13039 | { "capture.res.ver", smp_fetch_capture_res_ver, 0, NULL, SMP_T_STR, SMP_USE_HRQHP }, |
William Lallemand | a43ba4e | 2014-01-28 18:14:25 +0100 | [diff] [blame] | 13040 | |
Willy Tarreau | 409bcde | 2013-01-08 00:31:00 +0100 | [diff] [blame] | 13041 | /* cookie is valid in both directions (eg: for "stick ...") but cook* |
| 13042 | * are only here to match the ACL's name, are request-only and are used |
| 13043 | * for ACL compatibility only. |
| 13044 | */ |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 13045 | { "cook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRQHV }, |
| 13046 | { "cookie", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRQHV|SMP_USE_HRSHV }, |
Thierry FOURNIER | 07ee64e | 2015-07-06 23:43:03 +0200 | [diff] [blame] | 13047 | { "cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV }, |
| 13048 | { "cook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV }, |
Willy Tarreau | 409bcde | 2013-01-08 00:31:00 +0100 | [diff] [blame] | 13049 | |
| 13050 | /* hdr is valid in both directions (eg: for "stick ...") but hdr_* are |
| 13051 | * only here to match the ACL's name, are request-only and are used for |
| 13052 | * ACL compatibility only. |
| 13053 | */ |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 13054 | { "hdr", smp_fetch_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRQHV|SMP_USE_HRSHV }, |
Thierry FOURNIER | 07ee64e | 2015-07-06 23:43:03 +0200 | [diff] [blame] | 13055 | { "hdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV }, |
Willy Tarreau | 409bcde | 2013-01-08 00:31:00 +0100 | [diff] [blame] | 13056 | { "hdr_ip", smp_fetch_hdr_ip, ARG2(0,STR,SINT), val_hdr, SMP_T_IPV4, SMP_USE_HRQHV }, |
Thierry FOURNIER | 07ee64e | 2015-07-06 23:43:03 +0200 | [diff] [blame] | 13057 | { "hdr_val", smp_fetch_hdr_val, ARG2(0,STR,SINT), val_hdr, SMP_T_SINT, SMP_USE_HRQHV }, |
Willy Tarreau | 409bcde | 2013-01-08 00:31:00 +0100 | [diff] [blame] | 13058 | |
Willy Tarreau | 0a0daec | 2013-04-02 22:44:58 +0200 | [diff] [blame] | 13059 | { "http_auth", smp_fetch_http_auth, ARG1(1,USR), NULL, SMP_T_BOOL, SMP_USE_HRQHV }, |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 13060 | { "http_auth_group", smp_fetch_http_auth_grp, ARG1(1,USR), NULL, SMP_T_STR, SMP_USE_HRQHV }, |
Willy Tarreau | 409bcde | 2013-01-08 00:31:00 +0100 | [diff] [blame] | 13061 | { "http_first_req", smp_fetch_http_first_req, 0, NULL, SMP_T_BOOL, SMP_USE_HRQHP }, |
Thierry FOURNIER | d437314 | 2013-12-17 01:10:10 +0100 | [diff] [blame] | 13062 | { "method", smp_fetch_meth, 0, NULL, SMP_T_METH, SMP_USE_HRQHP }, |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 13063 | { "path", smp_fetch_path, 0, NULL, SMP_T_STR, SMP_USE_HRQHV }, |
Willy Tarreau | 49ad95c | 2015-01-19 15:06:26 +0100 | [diff] [blame] | 13064 | { "query", smp_fetch_query, 0, NULL, SMP_T_STR, SMP_USE_HRQHV }, |
Willy Tarreau | 18ed256 | 2013-01-14 15:56:36 +0100 | [diff] [blame] | 13065 | |
| 13066 | /* HTTP protocol on the request path */ |
| 13067 | { "req.proto_http", smp_fetch_proto_http, 0, NULL, SMP_T_BOOL, SMP_USE_HRQHP }, |
Willy Tarreau | 409bcde | 2013-01-08 00:31:00 +0100 | [diff] [blame] | 13068 | { "req_proto_http", smp_fetch_proto_http, 0, NULL, SMP_T_BOOL, SMP_USE_HRQHP }, |
Willy Tarreau | 18ed256 | 2013-01-14 15:56:36 +0100 | [diff] [blame] | 13069 | |
| 13070 | /* HTTP version on the request path */ |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 13071 | { "req.ver", smp_fetch_rqver, 0, NULL, SMP_T_STR, SMP_USE_HRQHV }, |
| 13072 | { "req_ver", smp_fetch_rqver, 0, NULL, SMP_T_STR, SMP_USE_HRQHV }, |
Willy Tarreau | 18ed256 | 2013-01-14 15:56:36 +0100 | [diff] [blame] | 13073 | |
Willy Tarreau | a5910cc | 2015-05-02 00:46:08 +0200 | [diff] [blame] | 13074 | { "req.body", smp_fetch_body, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV }, |
Thierry FOURNIER | 07ee64e | 2015-07-06 23:43:03 +0200 | [diff] [blame] | 13075 | { "req.body_len", smp_fetch_body_len, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV }, |
| 13076 | { "req.body_size", smp_fetch_body_size, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV }, |
Thierry FOURNIER | e28c499 | 2015-05-19 14:45:09 +0200 | [diff] [blame] | 13077 | { "req.body_param", smp_fetch_body_param, ARG1(0,STR), NULL, SMP_T_BIN, SMP_USE_HRQHV }, |
Willy Tarreau | a5910cc | 2015-05-02 00:46:08 +0200 | [diff] [blame] | 13078 | |
Willy Tarreau | 18ed256 | 2013-01-14 15:56:36 +0100 | [diff] [blame] | 13079 | /* HTTP version on the response path */ |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 13080 | { "res.ver", smp_fetch_stver, 0, NULL, SMP_T_STR, SMP_USE_HRSHV }, |
| 13081 | { "resp_ver", smp_fetch_stver, 0, NULL, SMP_T_STR, SMP_USE_HRSHV }, |
Willy Tarreau | 409bcde | 2013-01-08 00:31:00 +0100 | [diff] [blame] | 13082 | |
Willy Tarreau | 18ed256 | 2013-01-14 15:56:36 +0100 | [diff] [blame] | 13083 | /* explicit req.{cook,hdr} are used to force the fetch direction to be request-only */ |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 13084 | { "req.cook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRQHV }, |
Thierry FOURNIER | 07ee64e | 2015-07-06 23:43:03 +0200 | [diff] [blame] | 13085 | { "req.cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV }, |
| 13086 | { "req.cook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV }, |
Willy Tarreau | 18ed256 | 2013-01-14 15:56:36 +0100 | [diff] [blame] | 13087 | |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 13088 | { "req.fhdr", smp_fetch_fhdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRQHV }, |
Thierry FOURNIER | 07ee64e | 2015-07-06 23:43:03 +0200 | [diff] [blame] | 13089 | { "req.fhdr_cnt", smp_fetch_fhdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV }, |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 13090 | { "req.hdr", smp_fetch_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRQHV }, |
Thierry FOURNIER | 07ee64e | 2015-07-06 23:43:03 +0200 | [diff] [blame] | 13091 | { "req.hdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV }, |
Willy Tarreau | 18ed256 | 2013-01-14 15:56:36 +0100 | [diff] [blame] | 13092 | { "req.hdr_ip", smp_fetch_hdr_ip, ARG2(0,STR,SINT), val_hdr, SMP_T_IPV4, SMP_USE_HRQHV }, |
Willy Tarreau | eb27ec7 | 2015-02-20 13:55:29 +0100 | [diff] [blame] | 13093 | { "req.hdr_names", smp_fetch_hdr_names, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRQHV }, |
Thierry FOURNIER | 07ee64e | 2015-07-06 23:43:03 +0200 | [diff] [blame] | 13094 | { "req.hdr_val", smp_fetch_hdr_val, ARG2(0,STR,SINT), val_hdr, SMP_T_SINT, SMP_USE_HRQHV }, |
Willy Tarreau | 18ed256 | 2013-01-14 15:56:36 +0100 | [diff] [blame] | 13095 | |
| 13096 | /* explicit req.{cook,hdr} are used to force the fetch direction to be response-only */ |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 13097 | { "res.cook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRSHV }, |
Thierry FOURNIER | 07ee64e | 2015-07-06 23:43:03 +0200 | [diff] [blame] | 13098 | { "res.cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV }, |
| 13099 | { "res.cook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV }, |
Willy Tarreau | 18ed256 | 2013-01-14 15:56:36 +0100 | [diff] [blame] | 13100 | |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 13101 | { "res.fhdr", smp_fetch_fhdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRSHV }, |
Thierry FOURNIER | 07ee64e | 2015-07-06 23:43:03 +0200 | [diff] [blame] | 13102 | { "res.fhdr_cnt", smp_fetch_fhdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV }, |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 13103 | { "res.hdr", smp_fetch_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRSHV }, |
Thierry FOURNIER | 07ee64e | 2015-07-06 23:43:03 +0200 | [diff] [blame] | 13104 | { "res.hdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV }, |
Willy Tarreau | 18ed256 | 2013-01-14 15:56:36 +0100 | [diff] [blame] | 13105 | { "res.hdr_ip", smp_fetch_hdr_ip, ARG2(0,STR,SINT), val_hdr, SMP_T_IPV4, SMP_USE_HRSHV }, |
Willy Tarreau | eb27ec7 | 2015-02-20 13:55:29 +0100 | [diff] [blame] | 13106 | { "res.hdr_names", smp_fetch_hdr_names, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRSHV }, |
Thierry FOURNIER | 07ee64e | 2015-07-06 23:43:03 +0200 | [diff] [blame] | 13107 | { "res.hdr_val", smp_fetch_hdr_val, ARG2(0,STR,SINT), val_hdr, SMP_T_SINT, SMP_USE_HRSHV }, |
Willy Tarreau | 18ed256 | 2013-01-14 15:56:36 +0100 | [diff] [blame] | 13108 | |
Willy Tarreau | 409bcde | 2013-01-08 00:31:00 +0100 | [diff] [blame] | 13109 | /* scook is valid only on the response and is used for ACL compatibility */ |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 13110 | { "scook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRSHV }, |
Thierry FOURNIER | 07ee64e | 2015-07-06 23:43:03 +0200 | [diff] [blame] | 13111 | { "scook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV }, |
| 13112 | { "scook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV }, |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 13113 | { "set-cookie", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRSHV }, /* deprecated */ |
Willy Tarreau | 409bcde | 2013-01-08 00:31:00 +0100 | [diff] [blame] | 13114 | |
| 13115 | /* shdr is valid only on the response and is used for ACL compatibility */ |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 13116 | { "shdr", smp_fetch_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRSHV }, |
Thierry FOURNIER | 07ee64e | 2015-07-06 23:43:03 +0200 | [diff] [blame] | 13117 | { "shdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV }, |
Willy Tarreau | 409bcde | 2013-01-08 00:31:00 +0100 | [diff] [blame] | 13118 | { "shdr_ip", smp_fetch_hdr_ip, ARG2(0,STR,SINT), val_hdr, SMP_T_IPV4, SMP_USE_HRSHV }, |
Thierry FOURNIER | 07ee64e | 2015-07-06 23:43:03 +0200 | [diff] [blame] | 13119 | { "shdr_val", smp_fetch_hdr_val, ARG2(0,STR,SINT), val_hdr, SMP_T_SINT, SMP_USE_HRSHV }, |
Willy Tarreau | 409bcde | 2013-01-08 00:31:00 +0100 | [diff] [blame] | 13120 | |
Thierry FOURNIER | 07ee64e | 2015-07-06 23:43:03 +0200 | [diff] [blame] | 13121 | { "status", smp_fetch_stcode, 0, NULL, SMP_T_SINT, SMP_USE_HRSHP }, |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 13122 | { "url", smp_fetch_url, 0, NULL, SMP_T_STR, SMP_USE_HRQHV }, |
Thierry FOURNIER | 07ee64e | 2015-07-06 23:43:03 +0200 | [diff] [blame] | 13123 | { "url32", smp_fetch_url32, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV }, |
Neil - HAProxy List | 39c63c5 | 2013-11-04 13:48:42 +0000 | [diff] [blame] | 13124 | { "url32+src", smp_fetch_url32_src, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV }, |
Willy Tarreau | 409bcde | 2013-01-08 00:31:00 +0100 | [diff] [blame] | 13125 | { "url_ip", smp_fetch_url_ip, 0, NULL, SMP_T_IPV4, SMP_USE_HRQHV }, |
Thierry FOURNIER | 07ee64e | 2015-07-06 23:43:03 +0200 | [diff] [blame] | 13126 | { "url_port", smp_fetch_url_port, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV }, |
Willy Tarreau | 1ede1da | 2015-05-07 16:06:18 +0200 | [diff] [blame] | 13127 | { "url_param", smp_fetch_url_param, ARG2(0,STR,STR), NULL, SMP_T_STR, SMP_USE_HRQHV }, |
| 13128 | { "urlp" , smp_fetch_url_param, ARG2(0,STR,STR), NULL, SMP_T_STR, SMP_USE_HRQHV }, |
Thierry FOURNIER | 07ee64e | 2015-07-06 23:43:03 +0200 | [diff] [blame] | 13129 | { "urlp_val", smp_fetch_url_param_val, ARG2(0,STR,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV }, |
Willy Tarreau | 409bcde | 2013-01-08 00:31:00 +0100 | [diff] [blame] | 13130 | { /* END */ }, |
Willy Tarreau | 4a56897 | 2010-05-12 08:08:50 +0200 | [diff] [blame] | 13131 | }}; |
| 13132 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 13133 | |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 13134 | /************************************************************************/ |
| 13135 | /* All supported converter keywords must be declared here. */ |
| 13136 | /************************************************************************/ |
Willy Tarreau | 276fae9 | 2013-07-25 14:36:01 +0200 | [diff] [blame] | 13137 | /* Note: must not be declared <const> as its list will be overwritten */ |
| 13138 | static struct sample_conv_kw_list sample_conv_kws = {ILH, { |
Thierry FOURNIER | 07ee64e | 2015-07-06 23:43:03 +0200 | [diff] [blame] | 13139 | { "http_date", sample_conv_http_date, ARG1(0,SINT), NULL, SMP_T_SINT, SMP_T_STR}, |
Thierry FOURNIER | ad90351 | 2014-04-11 17:51:01 +0200 | [diff] [blame] | 13140 | { "language", sample_conv_q_prefered, ARG2(1,STR,STR), NULL, SMP_T_STR, SMP_T_STR}, |
Thierry FOURNIER | bf65cd4 | 2015-07-20 17:45:02 +0200 | [diff] [blame] | 13141 | { "capture-req", smp_conv_req_capture, ARG1(1,SINT), NULL, SMP_T_STR, SMP_T_STR}, |
| 13142 | { "capture-res", smp_conv_res_capture, ARG1(1,SINT), NULL, SMP_T_STR, SMP_T_STR}, |
Thierry FOURNIER | 82ff3c9 | 2015-05-07 15:46:20 +0200 | [diff] [blame] | 13143 | { "url_dec", sample_conv_url_dec, 0, NULL, SMP_T_STR, SMP_T_STR}, |
Willy Tarreau | 276fae9 | 2013-07-25 14:36:01 +0200 | [diff] [blame] | 13144 | { NULL, NULL, 0, 0, 0 }, |
| 13145 | }}; |
| 13146 | |
Thierry FOURNIER | 35ab275 | 2015-05-28 13:22:03 +0200 | [diff] [blame] | 13147 | |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 13148 | /************************************************************************/ |
| 13149 | /* All supported http-request action keywords must be declared here. */ |
| 13150 | /************************************************************************/ |
| 13151 | struct http_req_action_kw_list http_req_actions = { |
| 13152 | .scope = "http", |
| 13153 | .kw = { |
Willy Tarreau | a9083d0 | 2015-05-08 15:27:59 +0200 | [diff] [blame] | 13154 | { "capture", parse_http_req_capture }, |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 13155 | { "set-method", parse_set_req_line }, |
| 13156 | { "set-path", parse_set_req_line }, |
| 13157 | { "set-query", parse_set_req_line }, |
| 13158 | { "set-uri", parse_set_req_line }, |
Willy Tarreau | cb703b0 | 2015-04-03 09:52:01 +0200 | [diff] [blame] | 13159 | { NULL, NULL } |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 13160 | } |
| 13161 | }; |
| 13162 | |
Thierry FOURNIER | e80fada | 2015-05-26 18:06:31 +0200 | [diff] [blame] | 13163 | struct http_res_action_kw_list http_res_actions = { |
| 13164 | .scope = "http", |
| 13165 | .kw = { |
| 13166 | { "capture", parse_http_res_capture }, |
| 13167 | { NULL, NULL } |
| 13168 | } |
| 13169 | }; |
| 13170 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 13171 | __attribute__((constructor)) |
| 13172 | static void __http_protocol_init(void) |
| 13173 | { |
| 13174 | acl_register_keywords(&acl_kws); |
Willy Tarreau | 1278578 | 2012-04-27 21:37:17 +0200 | [diff] [blame] | 13175 | sample_register_fetches(&sample_fetch_keywords); |
Willy Tarreau | 276fae9 | 2013-07-25 14:36:01 +0200 | [diff] [blame] | 13176 | sample_register_convs(&sample_conv_kws); |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 13177 | http_req_keywords_register(&http_req_actions); |
Thierry FOURNIER | e80fada | 2015-05-26 18:06:31 +0200 | [diff] [blame] | 13178 | http_res_keywords_register(&http_res_actions); |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 13179 | } |
| 13180 | |
| 13181 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 13182 | /* |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 13183 | * Local variables: |
| 13184 | * c-indent-level: 8 |
| 13185 | * c-basic-offset: 8 |
| 13186 | * End: |
| 13187 | */ |