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 | |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 28 | #include <common/base64.h> |
Willy Tarreau | c7e4238 | 2012-08-24 19:22:53 +0200 | [diff] [blame] | 29 | #include <common/chunk.h> |
Willy Tarreau | 2dd0d47 | 2006-06-29 17:53:05 +0200 | [diff] [blame] | 30 | #include <common/compat.h> |
| 31 | #include <common/config.h> |
Willy Tarreau | a4cd1f5 | 2006-12-16 19:57:26 +0100 | [diff] [blame] | 32 | #include <common/debug.h> |
Willy Tarreau | 2dd0d47 | 2006-06-29 17:53:05 +0200 | [diff] [blame] | 33 | #include <common/memory.h> |
| 34 | #include <common/mini-clist.h> |
| 35 | #include <common/standard.h> |
Willy Tarreau | 0c303ee | 2008-07-07 00:09:58 +0200 | [diff] [blame] | 36 | #include <common/ticks.h> |
Willy Tarreau | 2dd0d47 | 2006-06-29 17:53:05 +0200 | [diff] [blame] | 37 | #include <common/time.h> |
| 38 | #include <common/uri_auth.h> |
| 39 | #include <common/version.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 40 | |
| 41 | #include <types/capture.h> |
Willy Tarreau | 12207b3 | 2016-11-22 19:48:51 +0100 | [diff] [blame] | 42 | #include <types/cli.h> |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 43 | #include <types/filters.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 44 | #include <types/global.h> |
William Lallemand | 9ed6203 | 2016-11-21 17:49:11 +0100 | [diff] [blame] | 45 | #include <types/stats.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 46 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 47 | #include <proto/acl.h> |
Thierry FOURNIER | 322a124 | 2015-08-19 09:07:47 +0200 | [diff] [blame] | 48 | #include <proto/action.h> |
Willy Tarreau | 61612d4 | 2012-04-19 18:42:05 +0200 | [diff] [blame] | 49 | #include <proto/arg.h> |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 50 | #include <proto/auth.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 51 | #include <proto/backend.h> |
Willy Tarreau | c7e4238 | 2012-08-24 19:22:53 +0200 | [diff] [blame] | 52 | #include <proto/channel.h> |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 53 | #include <proto/checks.h> |
Willy Tarreau | 12207b3 | 2016-11-22 19:48:51 +0100 | [diff] [blame] | 54 | #include <proto/cli.h> |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 55 | #include <proto/compression.h> |
William Lallemand | 9ed6203 | 2016-11-21 17:49:11 +0100 | [diff] [blame] | 56 | #include <proto/stats.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 57 | #include <proto/fd.h> |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 58 | #include <proto/filters.h> |
Willy Tarreau | 03fa5df | 2010-05-24 21:02:37 +0200 | [diff] [blame] | 59 | #include <proto/frontend.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 60 | #include <proto/log.h> |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 61 | #include <proto/hdr_idx.h> |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 62 | #include <proto/pattern.h> |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 63 | #include <proto/proto_tcp.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 64 | #include <proto/proto_http.h> |
Willy Tarreau | 7f062c4 | 2009-03-05 18:43:00 +0100 | [diff] [blame] | 65 | #include <proto/proxy.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 66 | #include <proto/queue.h> |
Willy Tarreau | cd3b094 | 2012-04-27 21:52:18 +0200 | [diff] [blame] | 67 | #include <proto/sample.h> |
Willy Tarreau | 7f062c4 | 2009-03-05 18:43:00 +0100 | [diff] [blame] | 68 | #include <proto/server.h> |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 69 | #include <proto/stream.h> |
Willy Tarreau | cff6411 | 2008-11-03 06:26:53 +0100 | [diff] [blame] | 70 | #include <proto/stream_interface.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 71 | #include <proto/task.h> |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 72 | #include <proto/pattern.h> |
Thierry FOURNIER | 4834bc7 | 2015-06-06 19:29:07 +0200 | [diff] [blame] | 73 | #include <proto/vars.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 74 | |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 75 | const char HTTP_100[] = |
| 76 | "HTTP/1.1 100 Continue\r\n\r\n"; |
| 77 | |
| 78 | const struct chunk http_100_chunk = { |
| 79 | .str = (char *)&HTTP_100, |
| 80 | .len = sizeof(HTTP_100)-1 |
| 81 | }; |
| 82 | |
Willy Tarreau | a9679ac | 2010-01-03 17:32:57 +0100 | [diff] [blame] | 83 | /* Warning: no "connection" header is provided with the 3xx messages below */ |
Willy Tarreau | b463dfb | 2008-06-07 23:08:56 +0200 | [diff] [blame] | 84 | const char *HTTP_301 = |
Willy Tarreau | bc5aa19 | 2010-01-03 15:09:36 +0100 | [diff] [blame] | 85 | "HTTP/1.1 301 Moved Permanently\r\n" |
Willy Tarreau | bc5aa19 | 2010-01-03 15:09:36 +0100 | [diff] [blame] | 86 | "Content-length: 0\r\n" |
Willy Tarreau | b463dfb | 2008-06-07 23:08:56 +0200 | [diff] [blame] | 87 | "Location: "; /* not terminated since it will be concatenated with the URL */ |
| 88 | |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 89 | const char *HTTP_302 = |
Willy Tarreau | bc5aa19 | 2010-01-03 15:09:36 +0100 | [diff] [blame] | 90 | "HTTP/1.1 302 Found\r\n" |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 91 | "Cache-Control: no-cache\r\n" |
Willy Tarreau | bc5aa19 | 2010-01-03 15:09:36 +0100 | [diff] [blame] | 92 | "Content-length: 0\r\n" |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 93 | "Location: "; /* not terminated since it will be concatenated with the URL */ |
| 94 | |
| 95 | /* same as 302 except that the browser MUST retry with the GET method */ |
| 96 | const char *HTTP_303 = |
Willy Tarreau | bc5aa19 | 2010-01-03 15:09:36 +0100 | [diff] [blame] | 97 | "HTTP/1.1 303 See Other\r\n" |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 98 | "Cache-Control: no-cache\r\n" |
Willy Tarreau | bc5aa19 | 2010-01-03 15:09:36 +0100 | [diff] [blame] | 99 | "Content-length: 0\r\n" |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 100 | "Location: "; /* not terminated since it will be concatenated with the URL */ |
| 101 | |
Yves Lafon | 3e8d1ae | 2013-03-11 11:06:05 -0400 | [diff] [blame] | 102 | |
| 103 | /* same as 302 except that the browser MUST retry with the same method */ |
| 104 | const char *HTTP_307 = |
| 105 | "HTTP/1.1 307 Temporary Redirect\r\n" |
| 106 | "Cache-Control: no-cache\r\n" |
| 107 | "Content-length: 0\r\n" |
| 108 | "Location: "; /* not terminated since it will be concatenated with the URL */ |
| 109 | |
| 110 | /* same as 301 except that the browser MUST retry with the same method */ |
| 111 | const char *HTTP_308 = |
| 112 | "HTTP/1.1 308 Permanent Redirect\r\n" |
| 113 | "Content-length: 0\r\n" |
| 114 | "Location: "; /* not terminated since it will be concatenated with the URL */ |
| 115 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 116 | /* Warning: this one is an sprintf() fmt string, with <realm> as its only argument */ |
| 117 | const char *HTTP_401_fmt = |
| 118 | "HTTP/1.0 401 Unauthorized\r\n" |
| 119 | "Cache-Control: no-cache\r\n" |
| 120 | "Connection: close\r\n" |
Willy Tarreau | 791d66d | 2006-07-08 16:53:38 +0200 | [diff] [blame] | 121 | "Content-Type: text/html\r\n" |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 122 | "WWW-Authenticate: Basic realm=\"%s\"\r\n" |
| 123 | "\r\n" |
| 124 | "<html><body><h1>401 Unauthorized</h1>\nYou need a valid user and password to access this content.\n</body></html>\n"; |
| 125 | |
Willy Tarreau | 844a7e7 | 2010-01-31 21:46:18 +0100 | [diff] [blame] | 126 | const char *HTTP_407_fmt = |
| 127 | "HTTP/1.0 407 Unauthorized\r\n" |
| 128 | "Cache-Control: no-cache\r\n" |
| 129 | "Connection: close\r\n" |
| 130 | "Content-Type: text/html\r\n" |
| 131 | "Proxy-Authenticate: Basic realm=\"%s\"\r\n" |
| 132 | "\r\n" |
Godbach | 1f1fae6 | 2014-12-17 16:32:05 +0800 | [diff] [blame] | 133 | "<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] | 134 | |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 135 | |
| 136 | const int http_err_codes[HTTP_ERR_SIZE] = { |
Willy Tarreau | ae94d4d | 2011-05-11 16:28:49 +0200 | [diff] [blame] | 137 | [HTTP_ERR_200] = 200, /* used by "monitor-uri" */ |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 138 | [HTTP_ERR_400] = 400, |
| 139 | [HTTP_ERR_403] = 403, |
CJ Ess | 108b1dd | 2015-04-07 12:03:37 -0400 | [diff] [blame] | 140 | [HTTP_ERR_405] = 405, |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 141 | [HTTP_ERR_408] = 408, |
CJ Ess | 108b1dd | 2015-04-07 12:03:37 -0400 | [diff] [blame] | 142 | [HTTP_ERR_429] = 429, |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 143 | [HTTP_ERR_500] = 500, |
| 144 | [HTTP_ERR_502] = 502, |
| 145 | [HTTP_ERR_503] = 503, |
| 146 | [HTTP_ERR_504] = 504, |
| 147 | }; |
| 148 | |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 149 | static const char *http_err_msgs[HTTP_ERR_SIZE] = { |
Willy Tarreau | ae94d4d | 2011-05-11 16:28:49 +0200 | [diff] [blame] | 150 | [HTTP_ERR_200] = |
| 151 | "HTTP/1.0 200 OK\r\n" |
| 152 | "Cache-Control: no-cache\r\n" |
| 153 | "Connection: close\r\n" |
| 154 | "Content-Type: text/html\r\n" |
| 155 | "\r\n" |
| 156 | "<html><body><h1>200 OK</h1>\nService ready.\n</body></html>\n", |
| 157 | |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 158 | [HTTP_ERR_400] = |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 159 | "HTTP/1.0 400 Bad request\r\n" |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 160 | "Cache-Control: no-cache\r\n" |
| 161 | "Connection: close\r\n" |
| 162 | "Content-Type: text/html\r\n" |
| 163 | "\r\n" |
| 164 | "<html><body><h1>400 Bad request</h1>\nYour browser sent an invalid request.\n</body></html>\n", |
| 165 | |
| 166 | [HTTP_ERR_403] = |
| 167 | "HTTP/1.0 403 Forbidden\r\n" |
| 168 | "Cache-Control: no-cache\r\n" |
| 169 | "Connection: close\r\n" |
| 170 | "Content-Type: text/html\r\n" |
| 171 | "\r\n" |
| 172 | "<html><body><h1>403 Forbidden</h1>\nRequest forbidden by administrative rules.\n</body></html>\n", |
| 173 | |
CJ Ess | 108b1dd | 2015-04-07 12:03:37 -0400 | [diff] [blame] | 174 | [HTTP_ERR_405] = |
| 175 | "HTTP/1.0 405 Method Not Allowed\r\n" |
| 176 | "Cache-Control: no-cache\r\n" |
| 177 | "Connection: close\r\n" |
| 178 | "Content-Type: text/html\r\n" |
| 179 | "\r\n" |
| 180 | "<html><body><h1>405 Method Not Allowed</h1>\nA request was made of a resource using a request method not supported by that resource\n</body></html>\n", |
| 181 | |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 182 | [HTTP_ERR_408] = |
| 183 | "HTTP/1.0 408 Request Time-out\r\n" |
| 184 | "Cache-Control: no-cache\r\n" |
| 185 | "Connection: close\r\n" |
| 186 | "Content-Type: text/html\r\n" |
| 187 | "\r\n" |
| 188 | "<html><body><h1>408 Request Time-out</h1>\nYour browser didn't send a complete request in time.\n</body></html>\n", |
| 189 | |
CJ Ess | 108b1dd | 2015-04-07 12:03:37 -0400 | [diff] [blame] | 190 | [HTTP_ERR_429] = |
| 191 | "HTTP/1.0 429 Too Many Requests\r\n" |
| 192 | "Cache-Control: no-cache\r\n" |
| 193 | "Connection: close\r\n" |
| 194 | "Content-Type: text/html\r\n" |
| 195 | "\r\n" |
| 196 | "<html><body><h1>429 Too Many Requests</h1>\nYou have sent too many requests in a given amount of time.\n</body></html>\n", |
| 197 | |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 198 | [HTTP_ERR_500] = |
| 199 | "HTTP/1.0 500 Server Error\r\n" |
| 200 | "Cache-Control: no-cache\r\n" |
| 201 | "Connection: close\r\n" |
| 202 | "Content-Type: text/html\r\n" |
| 203 | "\r\n" |
| 204 | "<html><body><h1>500 Server Error</h1>\nAn internal server error occured.\n</body></html>\n", |
| 205 | |
| 206 | [HTTP_ERR_502] = |
| 207 | "HTTP/1.0 502 Bad Gateway\r\n" |
| 208 | "Cache-Control: no-cache\r\n" |
| 209 | "Connection: close\r\n" |
| 210 | "Content-Type: text/html\r\n" |
| 211 | "\r\n" |
| 212 | "<html><body><h1>502 Bad Gateway</h1>\nThe server returned an invalid or incomplete response.\n</body></html>\n", |
| 213 | |
| 214 | [HTTP_ERR_503] = |
| 215 | "HTTP/1.0 503 Service Unavailable\r\n" |
| 216 | "Cache-Control: no-cache\r\n" |
| 217 | "Connection: close\r\n" |
| 218 | "Content-Type: text/html\r\n" |
| 219 | "\r\n" |
| 220 | "<html><body><h1>503 Service Unavailable</h1>\nNo server is available to handle this request.\n</body></html>\n", |
| 221 | |
| 222 | [HTTP_ERR_504] = |
| 223 | "HTTP/1.0 504 Gateway Time-out\r\n" |
| 224 | "Cache-Control: no-cache\r\n" |
| 225 | "Connection: close\r\n" |
| 226 | "Content-Type: text/html\r\n" |
| 227 | "\r\n" |
| 228 | "<html><body><h1>504 Gateway Time-out</h1>\nThe server didn't respond in time.\n</body></html>\n", |
| 229 | |
| 230 | }; |
| 231 | |
Cyril Bonté | 19979e1 | 2012-04-04 12:57:21 +0200 | [diff] [blame] | 232 | /* status codes available for the stats admin page (strictly 4 chars length) */ |
| 233 | const char *stat_status_codes[STAT_STATUS_SIZE] = { |
| 234 | [STAT_STATUS_DENY] = "DENY", |
| 235 | [STAT_STATUS_DONE] = "DONE", |
| 236 | [STAT_STATUS_ERRP] = "ERRP", |
| 237 | [STAT_STATUS_EXCD] = "EXCD", |
| 238 | [STAT_STATUS_NONE] = "NONE", |
| 239 | [STAT_STATUS_PART] = "PART", |
| 240 | [STAT_STATUS_UNKN] = "UNKN", |
| 241 | }; |
| 242 | |
| 243 | |
William Lallemand | 73025dd | 2014-04-24 14:38:37 +0200 | [diff] [blame] | 244 | /* List head of all known action keywords for "http-request" */ |
Thierry FOURNIER | 36481b8 | 2015-08-19 09:01:53 +0200 | [diff] [blame] | 245 | struct action_kw_list http_req_keywords = { |
William Lallemand | 73025dd | 2014-04-24 14:38:37 +0200 | [diff] [blame] | 246 | .list = LIST_HEAD_INIT(http_req_keywords.list) |
| 247 | }; |
| 248 | |
| 249 | /* List head of all known action keywords for "http-response" */ |
Thierry FOURNIER | 36481b8 | 2015-08-19 09:01:53 +0200 | [diff] [blame] | 250 | struct action_kw_list http_res_keywords = { |
William Lallemand | 73025dd | 2014-04-24 14:38:37 +0200 | [diff] [blame] | 251 | .list = LIST_HEAD_INIT(http_res_keywords.list) |
| 252 | }; |
| 253 | |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 254 | /* We must put the messages here since GCC cannot initialize consts depending |
| 255 | * on strlen(). |
| 256 | */ |
| 257 | struct chunk http_err_chunks[HTTP_ERR_SIZE]; |
| 258 | |
Willy Tarreau | a890d07 | 2013-04-02 12:01:06 +0200 | [diff] [blame] | 259 | /* this struct is used between calls to smp_fetch_hdr() or smp_fetch_cookie() */ |
| 260 | static struct hdr_ctx static_hdr_ctx; |
| 261 | |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 262 | #define FD_SETS_ARE_BITFIELDS |
| 263 | #ifdef FD_SETS_ARE_BITFIELDS |
| 264 | /* |
| 265 | * This map is used with all the FD_* macros to check whether a particular bit |
| 266 | * is set or not. Each bit represents an ACSII code. FD_SET() sets those bytes |
| 267 | * which should be encoded. When FD_ISSET() returns non-zero, it means that the |
| 268 | * byte should be encoded. Be careful to always pass bytes from 0 to 255 |
| 269 | * exclusively to the macros. |
| 270 | */ |
| 271 | fd_set hdr_encode_map[(sizeof(fd_set) > (256/8)) ? 1 : ((256/8) / sizeof(fd_set))]; |
| 272 | 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] | 273 | 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] | 274 | |
| 275 | #else |
| 276 | #error "Check if your OS uses bitfields for fd_sets" |
| 277 | #endif |
| 278 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 279 | 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] | 280 | |
David Carlier | 7365f7d | 2016-04-04 11:54:42 +0100 | [diff] [blame] | 281 | static inline int http_msg_forward_body(struct stream *s, struct http_msg *msg); |
| 282 | static inline int http_msg_forward_chunked_body(struct stream *s, struct http_msg *msg); |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 283 | |
Thierry FOURNIER | 35d70ef | 2015-08-26 16:21:56 +0200 | [diff] [blame] | 284 | /* This function returns a reason associated with the HTTP status. |
| 285 | * This function never fails, a message is always returned. |
| 286 | */ |
| 287 | const char *get_reason(unsigned int status) |
| 288 | { |
| 289 | switch (status) { |
| 290 | case 100: return "Continue"; |
| 291 | case 101: return "Switching Protocols"; |
| 292 | case 102: return "Processing"; |
| 293 | case 200: return "OK"; |
| 294 | case 201: return "Created"; |
| 295 | case 202: return "Accepted"; |
| 296 | case 203: return "Non-Authoritative Information"; |
| 297 | case 204: return "No Content"; |
| 298 | case 205: return "Reset Content"; |
| 299 | case 206: return "Partial Content"; |
| 300 | case 207: return "Multi-Status"; |
| 301 | case 210: return "Content Different"; |
| 302 | case 226: return "IM Used"; |
| 303 | case 300: return "Multiple Choices"; |
| 304 | case 301: return "Moved Permanently"; |
| 305 | case 302: return "Moved Temporarily"; |
| 306 | case 303: return "See Other"; |
| 307 | case 304: return "Not Modified"; |
| 308 | case 305: return "Use Proxy"; |
| 309 | case 307: return "Temporary Redirect"; |
| 310 | case 308: return "Permanent Redirect"; |
| 311 | case 310: return "Too many Redirects"; |
| 312 | case 400: return "Bad Request"; |
| 313 | case 401: return "Unauthorized"; |
| 314 | case 402: return "Payment Required"; |
| 315 | case 403: return "Forbidden"; |
| 316 | case 404: return "Not Found"; |
| 317 | case 405: return "Method Not Allowed"; |
| 318 | case 406: return "Not Acceptable"; |
| 319 | case 407: return "Proxy Authentication Required"; |
| 320 | case 408: return "Request Time-out"; |
| 321 | case 409: return "Conflict"; |
| 322 | case 410: return "Gone"; |
| 323 | case 411: return "Length Required"; |
| 324 | case 412: return "Precondition Failed"; |
| 325 | case 413: return "Request Entity Too Large"; |
| 326 | case 414: return "Request-URI Too Long"; |
| 327 | case 415: return "Unsupported Media Type"; |
| 328 | case 416: return "Requested range unsatisfiable"; |
| 329 | case 417: return "Expectation failed"; |
| 330 | case 418: return "I'm a teapot"; |
| 331 | case 422: return "Unprocessable entity"; |
| 332 | case 423: return "Locked"; |
| 333 | case 424: return "Method failure"; |
| 334 | case 425: return "Unordered Collection"; |
| 335 | case 426: return "Upgrade Required"; |
| 336 | case 428: return "Precondition Required"; |
| 337 | case 429: return "Too Many Requests"; |
| 338 | case 431: return "Request Header Fields Too Large"; |
| 339 | case 449: return "Retry With"; |
| 340 | case 450: return "Blocked by Windows Parental Controls"; |
| 341 | case 451: return "Unavailable For Legal Reasons"; |
| 342 | case 456: return "Unrecoverable Error"; |
| 343 | case 499: return "client has closed connection"; |
| 344 | case 500: return "Internal Server Error"; |
| 345 | case 501: return "Not Implemented"; |
| 346 | case 502: return "Bad Gateway ou Proxy Error"; |
| 347 | case 503: return "Service Unavailable"; |
| 348 | case 504: return "Gateway Time-out"; |
| 349 | case 505: return "HTTP Version not supported"; |
| 350 | case 506: return "Variant also negociate"; |
| 351 | case 507: return "Insufficient storage"; |
| 352 | case 508: return "Loop detected"; |
| 353 | case 509: return "Bandwidth Limit Exceeded"; |
| 354 | case 510: return "Not extended"; |
| 355 | case 511: return "Network authentication required"; |
| 356 | case 520: return "Web server is returning an unknown error"; |
| 357 | default: |
| 358 | switch (status) { |
| 359 | case 100 ... 199: return "Informational"; |
| 360 | case 200 ... 299: return "Success"; |
| 361 | case 300 ... 399: return "Redirection"; |
| 362 | case 400 ... 499: return "Client Error"; |
| 363 | case 500 ... 599: return "Server Error"; |
| 364 | default: return "Other"; |
| 365 | } |
| 366 | } |
| 367 | } |
| 368 | |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 369 | void init_proto_http() |
| 370 | { |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 371 | int i; |
| 372 | char *tmp; |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 373 | int msg; |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 374 | |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 375 | for (msg = 0; msg < HTTP_ERR_SIZE; msg++) { |
| 376 | if (!http_err_msgs[msg]) { |
| 377 | Alert("Internal error: no message defined for HTTP return code %d. Aborting.\n", msg); |
| 378 | abort(); |
| 379 | } |
| 380 | |
| 381 | http_err_chunks[msg].str = (char *)http_err_msgs[msg]; |
| 382 | http_err_chunks[msg].len = strlen(http_err_msgs[msg]); |
| 383 | } |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 384 | |
| 385 | /* initialize the log header encoding map : '{|}"#' should be encoded with |
| 386 | * '#' as prefix, as well as non-printable characters ( <32 or >= 127 ). |
| 387 | * URL encoding only requires '"', '#' to be encoded as well as non- |
| 388 | * printable characters above. |
| 389 | */ |
| 390 | memset(hdr_encode_map, 0, sizeof(hdr_encode_map)); |
| 391 | memset(url_encode_map, 0, sizeof(url_encode_map)); |
Thierry FOURNIER | d048d8b | 2014-03-13 16:46:18 +0100 | [diff] [blame] | 392 | memset(http_encode_map, 0, sizeof(url_encode_map)); |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 393 | for (i = 0; i < 32; i++) { |
| 394 | FD_SET(i, hdr_encode_map); |
| 395 | FD_SET(i, url_encode_map); |
| 396 | } |
| 397 | for (i = 127; i < 256; i++) { |
| 398 | FD_SET(i, hdr_encode_map); |
| 399 | FD_SET(i, url_encode_map); |
| 400 | } |
| 401 | |
| 402 | tmp = "\"#{|}"; |
| 403 | while (*tmp) { |
| 404 | FD_SET(*tmp, hdr_encode_map); |
| 405 | tmp++; |
| 406 | } |
| 407 | |
| 408 | tmp = "\"#"; |
| 409 | while (*tmp) { |
| 410 | FD_SET(*tmp, url_encode_map); |
| 411 | tmp++; |
| 412 | } |
Willy Tarreau | 332f8bf | 2007-05-13 21:36:56 +0200 | [diff] [blame] | 413 | |
Thierry FOURNIER | d048d8b | 2014-03-13 16:46:18 +0100 | [diff] [blame] | 414 | /* initialize the http header encoding map. The draft httpbis define the |
| 415 | * header content as: |
| 416 | * |
| 417 | * HTTP-message = start-line |
| 418 | * *( header-field CRLF ) |
| 419 | * CRLF |
| 420 | * [ message-body ] |
| 421 | * header-field = field-name ":" OWS field-value OWS |
| 422 | * field-value = *( field-content / obs-fold ) |
| 423 | * field-content = field-vchar [ 1*( SP / HTAB ) field-vchar ] |
| 424 | * obs-fold = CRLF 1*( SP / HTAB ) |
| 425 | * field-vchar = VCHAR / obs-text |
| 426 | * VCHAR = %x21-7E |
| 427 | * obs-text = %x80-FF |
| 428 | * |
| 429 | * All the chars are encoded except "VCHAR", "obs-text", SP and HTAB. |
| 430 | * The encoded chars are form 0x00 to 0x08, 0x0a to 0x1f and 0x7f. The |
| 431 | * "obs-fold" is volontary forgotten because haproxy remove this. |
| 432 | */ |
| 433 | memset(http_encode_map, 0, sizeof(http_encode_map)); |
| 434 | for (i = 0x00; i <= 0x08; i++) |
| 435 | FD_SET(i, http_encode_map); |
| 436 | for (i = 0x0a; i <= 0x1f; i++) |
| 437 | FD_SET(i, http_encode_map); |
| 438 | FD_SET(0x7f, http_encode_map); |
| 439 | |
Willy Tarreau | 332f8bf | 2007-05-13 21:36:56 +0200 | [diff] [blame] | 440 | /* memory allocations */ |
Willy Tarreau | 63986c7 | 2015-04-03 22:55:33 +0200 | [diff] [blame] | 441 | 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] | 442 | pool2_requri = create_pool("requri", REQURI_LEN, MEM_F_SHARED); |
William Lallemand | a73203e | 2012-03-12 12:48:57 +0100 | [diff] [blame] | 443 | pool2_uniqueid = create_pool("uniqueid", UNIQUEID_LEN, MEM_F_SHARED); |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 444 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 445 | |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 446 | /* |
| 447 | * We have 26 list of methods (1 per first letter), each of which can have |
| 448 | * up to 3 entries (2 valid, 1 null). |
| 449 | */ |
| 450 | struct http_method_desc { |
Willy Tarreau | c8987b3 | 2013-12-06 23:43:17 +0100 | [diff] [blame] | 451 | enum http_meth_t meth; |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 452 | int len; |
| 453 | const char text[8]; |
| 454 | }; |
| 455 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 456 | const struct http_method_desc http_methods[26][3] = { |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 457 | ['C' - 'A'] = { |
| 458 | [0] = { .meth = HTTP_METH_CONNECT , .len=7, .text="CONNECT" }, |
| 459 | }, |
| 460 | ['D' - 'A'] = { |
| 461 | [0] = { .meth = HTTP_METH_DELETE , .len=6, .text="DELETE" }, |
| 462 | }, |
| 463 | ['G' - 'A'] = { |
| 464 | [0] = { .meth = HTTP_METH_GET , .len=3, .text="GET" }, |
| 465 | }, |
| 466 | ['H' - 'A'] = { |
| 467 | [0] = { .meth = HTTP_METH_HEAD , .len=4, .text="HEAD" }, |
| 468 | }, |
Christopher Faulet | d57ad64 | 2015-07-31 14:26:57 +0200 | [diff] [blame] | 469 | ['O' - 'A'] = { |
| 470 | [0] = { .meth = HTTP_METH_OPTIONS , .len=7, .text="OPTIONS" }, |
| 471 | }, |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 472 | ['P' - 'A'] = { |
| 473 | [0] = { .meth = HTTP_METH_POST , .len=4, .text="POST" }, |
| 474 | [1] = { .meth = HTTP_METH_PUT , .len=3, .text="PUT" }, |
| 475 | }, |
| 476 | ['T' - 'A'] = { |
| 477 | [0] = { .meth = HTTP_METH_TRACE , .len=5, .text="TRACE" }, |
| 478 | }, |
| 479 | /* rest is empty like this : |
Willy Tarreau | b7ce424 | 2015-09-03 17:15:21 +0200 | [diff] [blame] | 480 | * [0] = { .meth = HTTP_METH_OTHER , .len=0, .text="" }, |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 481 | */ |
| 482 | }; |
| 483 | |
Thierry FOURNIER | d437314 | 2013-12-17 01:10:10 +0100 | [diff] [blame] | 484 | const struct http_method_name http_known_methods[HTTP_METH_OTHER] = { |
Thierry FOURNIER | d437314 | 2013-12-17 01:10:10 +0100 | [diff] [blame] | 485 | [HTTP_METH_OPTIONS] = { "OPTIONS", 7 }, |
| 486 | [HTTP_METH_GET] = { "GET", 3 }, |
| 487 | [HTTP_METH_HEAD] = { "HEAD", 4 }, |
| 488 | [HTTP_METH_POST] = { "POST", 4 }, |
| 489 | [HTTP_METH_PUT] = { "PUT", 3 }, |
| 490 | [HTTP_METH_DELETE] = { "DELETE", 6 }, |
| 491 | [HTTP_METH_TRACE] = { "TRACE", 5 }, |
| 492 | [HTTP_METH_CONNECT] = { "CONNECT", 7 }, |
| 493 | }; |
| 494 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 495 | /* 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] | 496 | * table than to perform a boolean AND or OR between two tests. Refer to |
Willy Tarreau | 2235b26 | 2016-11-05 15:50:20 +0100 | [diff] [blame] | 497 | * RFC2616/RFC5234/RFC7230 for those chars. A token is any ASCII char that is |
| 498 | * neither a separator nor a CTL char. An http ver_token is any ASCII which can |
| 499 | * be found in an HTTP version, which includes 'H', 'T', 'P', '/', '.' and any |
| 500 | * digit. Note: please do not overwrite values in assignment since gcc-2.95 |
| 501 | * will not handle them correctly. It's worth noting that chars 128..255 are |
| 502 | * nothing, not even control chars. |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 503 | */ |
Willy Tarreau | 2235b26 | 2016-11-05 15:50:20 +0100 | [diff] [blame] | 504 | const unsigned char http_char_classes[256] = { |
| 505 | [ 0] = HTTP_FLG_CTL, |
| 506 | [ 1] = HTTP_FLG_CTL, |
| 507 | [ 2] = HTTP_FLG_CTL, |
| 508 | [ 3] = HTTP_FLG_CTL, |
| 509 | [ 4] = HTTP_FLG_CTL, |
| 510 | [ 5] = HTTP_FLG_CTL, |
| 511 | [ 6] = HTTP_FLG_CTL, |
| 512 | [ 7] = HTTP_FLG_CTL, |
| 513 | [ 8] = HTTP_FLG_CTL, |
| 514 | [ 9] = HTTP_FLG_SPHT | HTTP_FLG_LWS | HTTP_FLG_SEP | HTTP_FLG_CTL, |
| 515 | [ 10] = HTTP_FLG_CRLF | HTTP_FLG_LWS | HTTP_FLG_CTL, |
| 516 | [ 11] = HTTP_FLG_CTL, |
| 517 | [ 12] = HTTP_FLG_CTL, |
| 518 | [ 13] = HTTP_FLG_CRLF | HTTP_FLG_LWS | HTTP_FLG_CTL, |
| 519 | [ 14] = HTTP_FLG_CTL, |
| 520 | [ 15] = HTTP_FLG_CTL, |
| 521 | [ 16] = HTTP_FLG_CTL, |
| 522 | [ 17] = HTTP_FLG_CTL, |
| 523 | [ 18] = HTTP_FLG_CTL, |
| 524 | [ 19] = HTTP_FLG_CTL, |
| 525 | [ 20] = HTTP_FLG_CTL, |
| 526 | [ 21] = HTTP_FLG_CTL, |
| 527 | [ 22] = HTTP_FLG_CTL, |
| 528 | [ 23] = HTTP_FLG_CTL, |
| 529 | [ 24] = HTTP_FLG_CTL, |
| 530 | [ 25] = HTTP_FLG_CTL, |
| 531 | [ 26] = HTTP_FLG_CTL, |
| 532 | [ 27] = HTTP_FLG_CTL, |
| 533 | [ 28] = HTTP_FLG_CTL, |
| 534 | [ 29] = HTTP_FLG_CTL, |
| 535 | [ 30] = HTTP_FLG_CTL, |
| 536 | [ 31] = HTTP_FLG_CTL, |
| 537 | [' '] = HTTP_FLG_SPHT | HTTP_FLG_LWS | HTTP_FLG_SEP, |
| 538 | ['!'] = HTTP_FLG_TOK, |
| 539 | ['"'] = HTTP_FLG_SEP, |
| 540 | ['#'] = HTTP_FLG_TOK, |
| 541 | ['$'] = HTTP_FLG_TOK, |
| 542 | ['%'] = HTTP_FLG_TOK, |
| 543 | ['&'] = HTTP_FLG_TOK, |
| 544 | [ 39] = HTTP_FLG_TOK, |
| 545 | ['('] = HTTP_FLG_SEP, |
| 546 | [')'] = HTTP_FLG_SEP, |
| 547 | ['*'] = HTTP_FLG_TOK, |
| 548 | ['+'] = HTTP_FLG_TOK, |
| 549 | [','] = HTTP_FLG_SEP, |
| 550 | ['-'] = HTTP_FLG_TOK, |
| 551 | ['.'] = HTTP_FLG_TOK | HTTP_FLG_VER, |
| 552 | ['/'] = HTTP_FLG_SEP | HTTP_FLG_VER, |
| 553 | ['0'] = HTTP_FLG_TOK | HTTP_FLG_VER, |
| 554 | ['1'] = HTTP_FLG_TOK | HTTP_FLG_VER, |
| 555 | ['2'] = HTTP_FLG_TOK | HTTP_FLG_VER, |
| 556 | ['3'] = HTTP_FLG_TOK | HTTP_FLG_VER, |
| 557 | ['4'] = HTTP_FLG_TOK | HTTP_FLG_VER, |
| 558 | ['5'] = HTTP_FLG_TOK | HTTP_FLG_VER, |
| 559 | ['6'] = HTTP_FLG_TOK | HTTP_FLG_VER, |
| 560 | ['7'] = HTTP_FLG_TOK | HTTP_FLG_VER, |
| 561 | ['8'] = HTTP_FLG_TOK | HTTP_FLG_VER, |
| 562 | ['9'] = HTTP_FLG_TOK | HTTP_FLG_VER, |
| 563 | [':'] = HTTP_FLG_SEP, |
| 564 | [';'] = HTTP_FLG_SEP, |
| 565 | ['<'] = HTTP_FLG_SEP, |
| 566 | ['='] = HTTP_FLG_SEP, |
| 567 | ['>'] = HTTP_FLG_SEP, |
| 568 | ['?'] = HTTP_FLG_SEP, |
| 569 | ['@'] = HTTP_FLG_SEP, |
| 570 | ['A'] = HTTP_FLG_TOK, |
| 571 | ['B'] = HTTP_FLG_TOK, |
| 572 | ['C'] = HTTP_FLG_TOK, |
| 573 | ['D'] = HTTP_FLG_TOK, |
| 574 | ['E'] = HTTP_FLG_TOK, |
| 575 | ['F'] = HTTP_FLG_TOK, |
| 576 | ['G'] = HTTP_FLG_TOK, |
| 577 | ['H'] = HTTP_FLG_TOK | HTTP_FLG_VER, |
| 578 | ['I'] = HTTP_FLG_TOK, |
| 579 | ['J'] = HTTP_FLG_TOK, |
| 580 | ['K'] = HTTP_FLG_TOK, |
| 581 | ['L'] = HTTP_FLG_TOK, |
| 582 | ['M'] = HTTP_FLG_TOK, |
| 583 | ['N'] = HTTP_FLG_TOK, |
| 584 | ['O'] = HTTP_FLG_TOK, |
| 585 | ['P'] = HTTP_FLG_TOK | HTTP_FLG_VER, |
| 586 | ['Q'] = HTTP_FLG_TOK, |
| 587 | ['R'] = HTTP_FLG_TOK | HTTP_FLG_VER, |
| 588 | ['S'] = HTTP_FLG_TOK | HTTP_FLG_VER, |
| 589 | ['T'] = HTTP_FLG_TOK | HTTP_FLG_VER, |
| 590 | ['U'] = HTTP_FLG_TOK, |
| 591 | ['V'] = HTTP_FLG_TOK, |
| 592 | ['W'] = HTTP_FLG_TOK, |
| 593 | ['X'] = HTTP_FLG_TOK, |
| 594 | ['Y'] = HTTP_FLG_TOK, |
| 595 | ['Z'] = HTTP_FLG_TOK, |
| 596 | ['['] = HTTP_FLG_SEP, |
| 597 | [ 92] = HTTP_FLG_SEP, |
| 598 | [']'] = HTTP_FLG_SEP, |
| 599 | ['^'] = HTTP_FLG_TOK, |
| 600 | ['_'] = HTTP_FLG_TOK, |
| 601 | ['`'] = HTTP_FLG_TOK, |
| 602 | ['a'] = HTTP_FLG_TOK, |
| 603 | ['b'] = HTTP_FLG_TOK, |
| 604 | ['c'] = HTTP_FLG_TOK, |
| 605 | ['d'] = HTTP_FLG_TOK, |
| 606 | ['e'] = HTTP_FLG_TOK, |
| 607 | ['f'] = HTTP_FLG_TOK, |
| 608 | ['g'] = HTTP_FLG_TOK, |
| 609 | ['h'] = HTTP_FLG_TOK, |
| 610 | ['i'] = HTTP_FLG_TOK, |
| 611 | ['j'] = HTTP_FLG_TOK, |
| 612 | ['k'] = HTTP_FLG_TOK, |
| 613 | ['l'] = HTTP_FLG_TOK, |
| 614 | ['m'] = HTTP_FLG_TOK, |
| 615 | ['n'] = HTTP_FLG_TOK, |
| 616 | ['o'] = HTTP_FLG_TOK, |
| 617 | ['p'] = HTTP_FLG_TOK, |
| 618 | ['q'] = HTTP_FLG_TOK, |
| 619 | ['r'] = HTTP_FLG_TOK, |
| 620 | ['s'] = HTTP_FLG_TOK, |
| 621 | ['t'] = HTTP_FLG_TOK, |
| 622 | ['u'] = HTTP_FLG_TOK, |
| 623 | ['v'] = HTTP_FLG_TOK, |
| 624 | ['w'] = HTTP_FLG_TOK, |
| 625 | ['x'] = HTTP_FLG_TOK, |
| 626 | ['y'] = HTTP_FLG_TOK, |
| 627 | ['z'] = HTTP_FLG_TOK, |
| 628 | ['{'] = HTTP_FLG_SEP, |
| 629 | ['|'] = HTTP_FLG_TOK, |
| 630 | ['}'] = HTTP_FLG_SEP, |
| 631 | ['~'] = HTTP_FLG_TOK, |
| 632 | [127] = HTTP_FLG_CTL, |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 633 | }; |
| 634 | |
Willy Tarreau | 4af6f3a | 2007-03-18 22:36:26 +0100 | [diff] [blame] | 635 | /* |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 636 | * Adds a header and its CRLF at the tail of the message's buffer, just before |
| 637 | * 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] | 638 | * The header is also automatically added to the index <hdr_idx>, and the end |
| 639 | * of headers is automatically adjusted. The number of bytes added is returned |
| 640 | * on success, otherwise <0 is returned indicating an error. |
| 641 | */ |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 642 | 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] | 643 | { |
| 644 | int bytes, len; |
| 645 | |
| 646 | len = strlen(text); |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 647 | 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] | 648 | if (!bytes) |
| 649 | return -1; |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 650 | http_msg_move_end(msg, bytes); |
Willy Tarreau | 4af6f3a | 2007-03-18 22:36:26 +0100 | [diff] [blame] | 651 | return hdr_idx_add(len, 1, hdr_idx, hdr_idx->tail); |
| 652 | } |
| 653 | |
| 654 | /* |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 655 | * Adds a header and its CRLF at the tail of the message's buffer, just before |
| 656 | * 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] | 657 | * the buffer is only opened and the space reserved, but nothing is copied. |
| 658 | * The header is also automatically added to the index <hdr_idx>, and the end |
| 659 | * of headers is automatically adjusted. The number of bytes added is returned |
| 660 | * on success, otherwise <0 is returned indicating an error. |
| 661 | */ |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 662 | int http_header_add_tail2(struct http_msg *msg, |
| 663 | struct hdr_idx *hdr_idx, const char *text, int len) |
Willy Tarreau | 4af6f3a | 2007-03-18 22:36:26 +0100 | [diff] [blame] | 664 | { |
| 665 | int bytes; |
| 666 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 667 | 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] | 668 | if (!bytes) |
| 669 | return -1; |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 670 | http_msg_move_end(msg, bytes); |
Willy Tarreau | 4af6f3a | 2007-03-18 22:36:26 +0100 | [diff] [blame] | 671 | return hdr_idx_add(len, 1, hdr_idx, hdr_idx->tail); |
| 672 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 673 | |
| 674 | /* |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 675 | * Checks if <hdr> is exactly <name> for <len> chars, and ends with a colon. |
| 676 | * If so, returns the position of the first non-space character relative to |
| 677 | * <hdr>, or <end>-<hdr> if not found before. If no value is found, it tries |
| 678 | * to return a pointer to the place after the first space. Returns 0 if the |
| 679 | * header name does not match. Checks are case-insensitive. |
| 680 | */ |
| 681 | int http_header_match2(const char *hdr, const char *end, |
| 682 | const char *name, int len) |
| 683 | { |
| 684 | const char *val; |
| 685 | |
| 686 | if (hdr + len >= end) |
| 687 | return 0; |
| 688 | if (hdr[len] != ':') |
| 689 | return 0; |
| 690 | if (strncasecmp(hdr, name, len) != 0) |
| 691 | return 0; |
| 692 | val = hdr + len + 1; |
| 693 | while (val < end && HTTP_IS_SPHT(*val)) |
| 694 | val++; |
| 695 | if ((val >= end) && (len + 2 <= end - hdr)) |
| 696 | return len + 2; /* we may replace starting from second space */ |
| 697 | return val - hdr; |
| 698 | } |
| 699 | |
Willy Tarreau | 04ff9f1 | 2013-06-10 18:39:42 +0200 | [diff] [blame] | 700 | /* Find the first or next occurrence of header <name> in message buffer <sol> |
| 701 | * using headers index <idx>, and return it in the <ctx> structure. This |
| 702 | * structure holds everything necessary to use the header and find next |
| 703 | * occurrence. If its <idx> member is 0, the header is searched from the |
| 704 | * beginning. Otherwise, the next occurrence is returned. The function returns |
| 705 | * 1 when it finds a value, and 0 when there is no more. It is very similar to |
| 706 | * http_find_header2() except that it is designed to work with full-line headers |
| 707 | * whose comma is not a delimiter but is part of the syntax. As a special case, |
| 708 | * if ctx->val is NULL when searching for a new values of a header, the current |
| 709 | * header is rescanned. This allows rescanning after a header deletion. |
| 710 | */ |
| 711 | int http_find_full_header2(const char *name, int len, |
| 712 | char *sol, struct hdr_idx *idx, |
| 713 | struct hdr_ctx *ctx) |
| 714 | { |
| 715 | char *eol, *sov; |
| 716 | int cur_idx, old_idx; |
| 717 | |
| 718 | cur_idx = ctx->idx; |
| 719 | if (cur_idx) { |
| 720 | /* We have previously returned a header, let's search another one */ |
| 721 | sol = ctx->line; |
| 722 | eol = sol + idx->v[cur_idx].len; |
| 723 | goto next_hdr; |
| 724 | } |
| 725 | |
| 726 | /* first request for this header */ |
| 727 | sol += hdr_idx_first_pos(idx); |
| 728 | old_idx = 0; |
| 729 | cur_idx = hdr_idx_first_idx(idx); |
| 730 | while (cur_idx) { |
| 731 | eol = sol + idx->v[cur_idx].len; |
| 732 | |
| 733 | if (len == 0) { |
| 734 | /* No argument was passed, we want any header. |
| 735 | * To achieve this, we simply build a fake request. */ |
| 736 | while (sol + len < eol && sol[len] != ':') |
| 737 | len++; |
| 738 | name = sol; |
| 739 | } |
| 740 | |
| 741 | if ((len < eol - sol) && |
| 742 | (sol[len] == ':') && |
| 743 | (strncasecmp(sol, name, len) == 0)) { |
| 744 | ctx->del = len; |
| 745 | sov = sol + len + 1; |
Willy Tarreau | 2235b26 | 2016-11-05 15:50:20 +0100 | [diff] [blame] | 746 | while (sov < eol && HTTP_IS_LWS(*sov)) |
Willy Tarreau | 04ff9f1 | 2013-06-10 18:39:42 +0200 | [diff] [blame] | 747 | sov++; |
| 748 | |
| 749 | ctx->line = sol; |
| 750 | ctx->prev = old_idx; |
| 751 | ctx->idx = cur_idx; |
| 752 | ctx->val = sov - sol; |
| 753 | ctx->tws = 0; |
Willy Tarreau | 2235b26 | 2016-11-05 15:50:20 +0100 | [diff] [blame] | 754 | while (eol > sov && HTTP_IS_LWS(*(eol - 1))) { |
Willy Tarreau | 04ff9f1 | 2013-06-10 18:39:42 +0200 | [diff] [blame] | 755 | eol--; |
| 756 | ctx->tws++; |
| 757 | } |
| 758 | ctx->vlen = eol - sov; |
| 759 | return 1; |
| 760 | } |
| 761 | next_hdr: |
| 762 | sol = eol + idx->v[cur_idx].cr + 1; |
| 763 | old_idx = cur_idx; |
| 764 | cur_idx = idx->v[cur_idx].next; |
| 765 | } |
| 766 | return 0; |
| 767 | } |
| 768 | |
Willy Tarreau | c90dc23 | 2015-02-20 13:51:36 +0100 | [diff] [blame] | 769 | /* Find the first or next header field in message buffer <sol> using headers |
| 770 | * index <idx>, and return it in the <ctx> structure. This structure holds |
| 771 | * everything necessary to use the header and find next occurrence. If its |
| 772 | * <idx> member is 0, the first header is retrieved. Otherwise, the next |
| 773 | * occurrence is returned. The function returns 1 when it finds a value, and |
| 774 | * 0 when there is no more. It is equivalent to http_find_full_header2() with |
| 775 | * no header name. |
| 776 | */ |
| 777 | int http_find_next_header(char *sol, struct hdr_idx *idx, struct hdr_ctx *ctx) |
| 778 | { |
| 779 | char *eol, *sov; |
| 780 | int cur_idx, old_idx; |
| 781 | int len; |
| 782 | |
| 783 | cur_idx = ctx->idx; |
| 784 | if (cur_idx) { |
| 785 | /* We have previously returned a header, let's search another one */ |
| 786 | sol = ctx->line; |
| 787 | eol = sol + idx->v[cur_idx].len; |
| 788 | goto next_hdr; |
| 789 | } |
| 790 | |
| 791 | /* first request for this header */ |
| 792 | sol += hdr_idx_first_pos(idx); |
| 793 | old_idx = 0; |
| 794 | cur_idx = hdr_idx_first_idx(idx); |
| 795 | while (cur_idx) { |
| 796 | eol = sol + idx->v[cur_idx].len; |
| 797 | |
| 798 | len = 0; |
| 799 | while (1) { |
| 800 | if (len >= eol - sol) |
| 801 | goto next_hdr; |
| 802 | if (sol[len] == ':') |
| 803 | break; |
| 804 | len++; |
| 805 | } |
| 806 | |
| 807 | ctx->del = len; |
| 808 | sov = sol + len + 1; |
Willy Tarreau | 2235b26 | 2016-11-05 15:50:20 +0100 | [diff] [blame] | 809 | while (sov < eol && HTTP_IS_LWS(*sov)) |
Willy Tarreau | c90dc23 | 2015-02-20 13:51:36 +0100 | [diff] [blame] | 810 | sov++; |
| 811 | |
| 812 | ctx->line = sol; |
| 813 | ctx->prev = old_idx; |
| 814 | ctx->idx = cur_idx; |
| 815 | ctx->val = sov - sol; |
| 816 | ctx->tws = 0; |
| 817 | |
Willy Tarreau | 2235b26 | 2016-11-05 15:50:20 +0100 | [diff] [blame] | 818 | while (eol > sov && HTTP_IS_LWS(*(eol - 1))) { |
Willy Tarreau | c90dc23 | 2015-02-20 13:51:36 +0100 | [diff] [blame] | 819 | eol--; |
| 820 | ctx->tws++; |
| 821 | } |
| 822 | ctx->vlen = eol - sov; |
| 823 | return 1; |
| 824 | |
| 825 | next_hdr: |
| 826 | sol = eol + idx->v[cur_idx].cr + 1; |
| 827 | old_idx = cur_idx; |
| 828 | cur_idx = idx->v[cur_idx].next; |
| 829 | } |
| 830 | return 0; |
| 831 | } |
| 832 | |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 833 | /* Find the end of the header value contained between <s> and <e>. See RFC2616, |
| 834 | * par 2.2 for more information. Note that it requires a valid header to return |
| 835 | * a valid result. This works for headers defined as comma-separated lists. |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 836 | */ |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 837 | char *find_hdr_value_end(char *s, const char *e) |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 838 | { |
| 839 | int quoted, qdpair; |
| 840 | |
| 841 | quoted = qdpair = 0; |
Willy Tarreau | e6d9c21 | 2016-11-05 18:23:38 +0100 | [diff] [blame] | 842 | |
| 843 | #if defined(__x86_64__) || \ |
| 844 | defined(__i386__) || defined(__i486__) || defined(__i586__) || defined(__i686__) || \ |
| 845 | defined(__ARM_ARCH_7A__) |
| 846 | /* speedup: skip everything not a comma nor a double quote */ |
| 847 | for (; s <= e - sizeof(int); s += sizeof(int)) { |
| 848 | unsigned int c = *(int *)s; // comma |
| 849 | unsigned int q = c; // quote |
| 850 | |
| 851 | c ^= 0x2c2c2c2c; // contains one zero on a comma |
| 852 | q ^= 0x22222222; // contains one zero on a quote |
| 853 | |
| 854 | c = (c - 0x01010101) & ~c; // contains 0x80 below a comma |
| 855 | q = (q - 0x01010101) & ~q; // contains 0x80 below a quote |
| 856 | |
| 857 | if ((c | q) & 0x80808080) |
| 858 | break; // found a comma or a quote |
| 859 | } |
| 860 | #endif |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 861 | for (; s < e; s++) { |
| 862 | if (qdpair) qdpair = 0; |
Willy Tarreau | 0f7f51f | 2010-08-30 11:06:34 +0200 | [diff] [blame] | 863 | else if (quoted) { |
| 864 | if (*s == '\\') qdpair = 1; |
| 865 | else if (*s == '"') quoted = 0; |
| 866 | } |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 867 | else if (*s == '"') quoted = 1; |
| 868 | else if (*s == ',') return s; |
| 869 | } |
| 870 | return s; |
| 871 | } |
| 872 | |
| 873 | /* Find the first or next occurrence of header <name> in message buffer <sol> |
| 874 | * using headers index <idx>, and return it in the <ctx> structure. This |
| 875 | * structure holds everything necessary to use the header and find next |
| 876 | * occurrence. If its <idx> member is 0, the header is searched from the |
| 877 | * beginning. Otherwise, the next occurrence is returned. The function returns |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 878 | * 1 when it finds a value, and 0 when there is no more. It is designed to work |
| 879 | * with headers defined as comma-separated lists. As a special case, if ctx->val |
| 880 | * is NULL when searching for a new values of a header, the current header is |
| 881 | * rescanned. This allows rescanning after a header deletion. |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 882 | */ |
| 883 | int http_find_header2(const char *name, int len, |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 884 | char *sol, struct hdr_idx *idx, |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 885 | struct hdr_ctx *ctx) |
| 886 | { |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 887 | char *eol, *sov; |
| 888 | int cur_idx, old_idx; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 889 | |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 890 | cur_idx = ctx->idx; |
| 891 | if (cur_idx) { |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 892 | /* We have previously returned a value, let's search |
| 893 | * another one on the same line. |
| 894 | */ |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 895 | sol = ctx->line; |
Willy Tarreau | 588bd4f | 2011-09-01 22:22:28 +0200 | [diff] [blame] | 896 | ctx->del = ctx->val + ctx->vlen + ctx->tws; |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 897 | sov = sol + ctx->del; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 898 | eol = sol + idx->v[cur_idx].len; |
| 899 | |
| 900 | if (sov >= eol) |
| 901 | /* no more values in this header */ |
| 902 | goto next_hdr; |
| 903 | |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 904 | /* values remaining for this header, skip the comma but save it |
| 905 | * for later use (eg: for header deletion). |
| 906 | */ |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 907 | sov++; |
Willy Tarreau | 2235b26 | 2016-11-05 15:50:20 +0100 | [diff] [blame] | 908 | while (sov < eol && HTTP_IS_LWS((*sov))) |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 909 | sov++; |
| 910 | |
| 911 | goto return_hdr; |
| 912 | } |
| 913 | |
| 914 | /* first request for this header */ |
| 915 | sol += hdr_idx_first_pos(idx); |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 916 | old_idx = 0; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 917 | cur_idx = hdr_idx_first_idx(idx); |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 918 | while (cur_idx) { |
| 919 | eol = sol + idx->v[cur_idx].len; |
| 920 | |
Willy Tarreau | 1ad7c6d | 2007-06-10 21:42:55 +0200 | [diff] [blame] | 921 | if (len == 0) { |
| 922 | /* No argument was passed, we want any header. |
| 923 | * To achieve this, we simply build a fake request. */ |
| 924 | while (sol + len < eol && sol[len] != ':') |
| 925 | len++; |
| 926 | name = sol; |
| 927 | } |
| 928 | |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 929 | if ((len < eol - sol) && |
| 930 | (sol[len] == ':') && |
| 931 | (strncasecmp(sol, name, len) == 0)) { |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 932 | ctx->del = len; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 933 | sov = sol + len + 1; |
Willy Tarreau | 2235b26 | 2016-11-05 15:50:20 +0100 | [diff] [blame] | 934 | while (sov < eol && HTTP_IS_LWS(*sov)) |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 935 | sov++; |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 936 | |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 937 | ctx->line = sol; |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 938 | ctx->prev = old_idx; |
| 939 | return_hdr: |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 940 | ctx->idx = cur_idx; |
| 941 | ctx->val = sov - sol; |
| 942 | |
| 943 | eol = find_hdr_value_end(sov, eol); |
Willy Tarreau | 588bd4f | 2011-09-01 22:22:28 +0200 | [diff] [blame] | 944 | ctx->tws = 0; |
Willy Tarreau | 2235b26 | 2016-11-05 15:50:20 +0100 | [diff] [blame] | 945 | while (eol > sov && HTTP_IS_LWS(*(eol - 1))) { |
Willy Tarreau | 588bd4f | 2011-09-01 22:22:28 +0200 | [diff] [blame] | 946 | eol--; |
| 947 | ctx->tws++; |
| 948 | } |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 949 | ctx->vlen = eol - sov; |
| 950 | return 1; |
| 951 | } |
| 952 | next_hdr: |
| 953 | sol = eol + idx->v[cur_idx].cr + 1; |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 954 | old_idx = cur_idx; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 955 | cur_idx = idx->v[cur_idx].next; |
| 956 | } |
| 957 | return 0; |
| 958 | } |
| 959 | |
| 960 | int http_find_header(const char *name, |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 961 | char *sol, struct hdr_idx *idx, |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 962 | struct hdr_ctx *ctx) |
| 963 | { |
| 964 | return http_find_header2(name, strlen(name), sol, idx, ctx); |
| 965 | } |
| 966 | |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 967 | /* Remove one value of a header. This only works on a <ctx> returned by one of |
| 968 | * the http_find_header functions. The value is removed, as well as surrounding |
| 969 | * 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] | 970 | * 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] | 971 | * message <msg>. The new index is returned. If it is zero, it means there is |
| 972 | * no more header, so any processing may stop. The ctx is always left in a form |
| 973 | * that can be handled by http_find_header2() to find next occurrence. |
| 974 | */ |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 975 | 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] | 976 | { |
| 977 | int cur_idx = ctx->idx; |
| 978 | char *sol = ctx->line; |
| 979 | struct hdr_idx_elem *hdr; |
| 980 | int delta, skip_comma; |
| 981 | |
| 982 | if (!cur_idx) |
| 983 | return 0; |
| 984 | |
| 985 | hdr = &idx->v[cur_idx]; |
Willy Tarreau | 588bd4f | 2011-09-01 22:22:28 +0200 | [diff] [blame] | 986 | if (sol[ctx->del] == ':' && ctx->val + ctx->vlen + ctx->tws == hdr->len) { |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 987 | /* 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] | 988 | 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] | 989 | http_msg_move_end(msg, delta); |
| 990 | idx->used--; |
| 991 | hdr->len = 0; /* unused entry */ |
| 992 | idx->v[ctx->prev].next = idx->v[ctx->idx].next; |
Willy Tarreau | 5c4784f | 2011-02-12 13:07:35 +0100 | [diff] [blame] | 993 | if (idx->tail == ctx->idx) |
| 994 | idx->tail = ctx->prev; |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 995 | ctx->idx = ctx->prev; /* walk back to the end of previous header */ |
Willy Tarreau | 7c1c217 | 2015-01-07 17:23:50 +0100 | [diff] [blame] | 996 | 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] | 997 | 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] | 998 | ctx->tws = ctx->vlen = 0; |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 999 | return ctx->idx; |
| 1000 | } |
| 1001 | |
| 1002 | /* 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] | 1003 | * ctx->del+1 and ctx->val+ctx->vlen+ctx->tws+1 included. If it is the |
| 1004 | * last entry of the list, we remove the last separator. |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 1005 | */ |
| 1006 | |
Willy Tarreau | 588bd4f | 2011-09-01 22:22:28 +0200 | [diff] [blame] | 1007 | skip_comma = (ctx->val + ctx->vlen + ctx->tws == hdr->len) ? 0 : 1; |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 1008 | delta = buffer_replace2(msg->chn->buf, sol + ctx->del + skip_comma, |
Willy Tarreau | 588bd4f | 2011-09-01 22:22:28 +0200 | [diff] [blame] | 1009 | sol + ctx->val + ctx->vlen + ctx->tws + skip_comma, |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 1010 | NULL, 0); |
| 1011 | hdr->len += delta; |
| 1012 | http_msg_move_end(msg, delta); |
| 1013 | ctx->val = ctx->del; |
Willy Tarreau | 588bd4f | 2011-09-01 22:22:28 +0200 | [diff] [blame] | 1014 | ctx->tws = ctx->vlen = 0; |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 1015 | return ctx->idx; |
| 1016 | } |
| 1017 | |
Willy Tarreau | 2d3d94c | 2008-11-30 20:20:08 +0100 | [diff] [blame] | 1018 | /* This function handles a server error at the stream interface level. The |
| 1019 | * stream interface is assumed to be already in a closed state. An optional |
| 1020 | * message is copied into the input buffer, and an HTTP status code stored. |
| 1021 | * 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] | 1022 | * in this buffer will be lost. |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1023 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 1024 | static void http_server_error(struct stream *s, struct stream_interface *si, |
Willy Tarreau | 2d3d94c | 2008-11-30 20:20:08 +0100 | [diff] [blame] | 1025 | int err, int finst, int status, const struct chunk *msg) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1026 | { |
Christopher Faulet | 3e34429 | 2015-11-24 16:24:13 +0100 | [diff] [blame] | 1027 | FLT_STRM_CB(s, flt_http_reply(s, status, msg)); |
Willy Tarreau | 2bb4a96 | 2014-11-28 11:11:05 +0100 | [diff] [blame] | 1028 | channel_auto_read(si_oc(si)); |
| 1029 | channel_abort(si_oc(si)); |
| 1030 | channel_auto_close(si_oc(si)); |
| 1031 | channel_erase(si_oc(si)); |
| 1032 | channel_auto_close(si_ic(si)); |
| 1033 | channel_auto_read(si_ic(si)); |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 1034 | if (status > 0 && msg) { |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 1035 | s->txn->status = status; |
Willy Tarreau | 2bb4a96 | 2014-11-28 11:11:05 +0100 | [diff] [blame] | 1036 | bo_inject(si_ic(si), msg->str, msg->len); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1037 | } |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 1038 | if (!(s->flags & SF_ERR_MASK)) |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 1039 | s->flags |= err; |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 1040 | if (!(s->flags & SF_FINST_MASK)) |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 1041 | s->flags |= finst; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1042 | } |
| 1043 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 1044 | /* This function returns the appropriate error location for the given stream |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 1045 | * and message. |
| 1046 | */ |
| 1047 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 1048 | struct chunk *http_error_message(struct stream *s, int msgnum) |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 1049 | { |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 1050 | if (s->be->errmsg[msgnum].str) |
| 1051 | return &s->be->errmsg[msgnum]; |
Willy Tarreau | d0d8da9 | 2015-04-04 02:10:38 +0200 | [diff] [blame] | 1052 | else if (strm_fe(s)->errmsg[msgnum].str) |
| 1053 | return &strm_fe(s)->errmsg[msgnum]; |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 1054 | else |
| 1055 | return &http_err_chunks[msgnum]; |
| 1056 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1057 | |
Christopher Faulet | a94e5a5 | 2015-12-09 15:55:06 +0100 | [diff] [blame] | 1058 | void |
| 1059 | http_reply_and_close(struct stream *s, short status, struct chunk *msg) |
| 1060 | { |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 1061 | s->txn->flags &= ~TX_WAIT_NEXT_RQ; |
Christopher Faulet | 3e34429 | 2015-11-24 16:24:13 +0100 | [diff] [blame] | 1062 | FLT_STRM_CB(s, flt_http_reply(s, status, msg)); |
Christopher Faulet | a94e5a5 | 2015-12-09 15:55:06 +0100 | [diff] [blame] | 1063 | stream_int_retnclose(&s->si[0], msg); |
| 1064 | } |
| 1065 | |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 1066 | /* |
Willy Tarreau | b7ce424 | 2015-09-03 17:15:21 +0200 | [diff] [blame] | 1067 | * returns a known method among HTTP_METH_* or HTTP_METH_OTHER for all unknown |
| 1068 | * ones. |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 1069 | */ |
Thierry FOURNIER | d437314 | 2013-12-17 01:10:10 +0100 | [diff] [blame] | 1070 | 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] | 1071 | { |
| 1072 | unsigned char m; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1073 | const struct http_method_desc *h; |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 1074 | |
| 1075 | m = ((unsigned)*str - 'A'); |
| 1076 | |
| 1077 | if (m < 26) { |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1078 | for (h = http_methods[m]; h->len > 0; h++) { |
| 1079 | if (unlikely(h->len != len)) |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 1080 | continue; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1081 | if (likely(memcmp(str, h->text, h->len) == 0)) |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 1082 | return h->meth; |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 1083 | }; |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 1084 | } |
Willy Tarreau | b7ce424 | 2015-09-03 17:15:21 +0200 | [diff] [blame] | 1085 | return HTTP_METH_OTHER; |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 1086 | } |
| 1087 | |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 1088 | /* Parse the URI from the given transaction (which is assumed to be in request |
| 1089 | * phase) and look for the "/" beginning the PATH. If not found, return NULL. |
| 1090 | * It is returned otherwise. |
| 1091 | */ |
Thierry FOURNIER | 3c33178 | 2015-09-17 19:33:35 +0200 | [diff] [blame] | 1092 | char *http_get_path(struct http_txn *txn) |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 1093 | { |
| 1094 | char *ptr, *end; |
| 1095 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 1096 | ptr = txn->req.chn->buf->p + txn->req.sl.rq.u; |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 1097 | end = ptr + txn->req.sl.rq.u_l; |
| 1098 | |
| 1099 | if (ptr >= end) |
| 1100 | return NULL; |
| 1101 | |
| 1102 | /* RFC2616, par. 5.1.2 : |
| 1103 | * Request-URI = "*" | absuri | abspath | authority |
| 1104 | */ |
| 1105 | |
| 1106 | if (*ptr == '*') |
| 1107 | return NULL; |
| 1108 | |
| 1109 | if (isalpha((unsigned char)*ptr)) { |
| 1110 | /* this is a scheme as described by RFC3986, par. 3.1 */ |
| 1111 | ptr++; |
| 1112 | while (ptr < end && |
| 1113 | (isalnum((unsigned char)*ptr) || *ptr == '+' || *ptr == '-' || *ptr == '.')) |
| 1114 | ptr++; |
| 1115 | /* skip '://' */ |
| 1116 | if (ptr == end || *ptr++ != ':') |
| 1117 | return NULL; |
| 1118 | if (ptr == end || *ptr++ != '/') |
| 1119 | return NULL; |
| 1120 | if (ptr == end || *ptr++ != '/') |
| 1121 | return NULL; |
| 1122 | } |
| 1123 | /* skip [user[:passwd]@]host[:[port]] */ |
| 1124 | |
| 1125 | while (ptr < end && *ptr != '/') |
| 1126 | ptr++; |
| 1127 | |
| 1128 | if (ptr == end) |
| 1129 | return NULL; |
| 1130 | |
| 1131 | /* OK, we got the '/' ! */ |
| 1132 | return ptr; |
| 1133 | } |
| 1134 | |
William Lallemand | 65ad6e1 | 2014-01-31 15:08:02 +0100 | [diff] [blame] | 1135 | /* Parse the URI from the given string and look for the "/" beginning the PATH. |
| 1136 | * If not found, return NULL. It is returned otherwise. |
| 1137 | */ |
| 1138 | static char * |
| 1139 | http_get_path_from_string(char *str) |
| 1140 | { |
| 1141 | char *ptr = str; |
| 1142 | |
| 1143 | /* RFC2616, par. 5.1.2 : |
| 1144 | * Request-URI = "*" | absuri | abspath | authority |
| 1145 | */ |
| 1146 | |
| 1147 | if (*ptr == '*') |
| 1148 | return NULL; |
| 1149 | |
| 1150 | if (isalpha((unsigned char)*ptr)) { |
| 1151 | /* this is a scheme as described by RFC3986, par. 3.1 */ |
| 1152 | ptr++; |
| 1153 | while (isalnum((unsigned char)*ptr) || *ptr == '+' || *ptr == '-' || *ptr == '.') |
| 1154 | ptr++; |
| 1155 | /* skip '://' */ |
| 1156 | if (*ptr == '\0' || *ptr++ != ':') |
| 1157 | return NULL; |
| 1158 | if (*ptr == '\0' || *ptr++ != '/') |
| 1159 | return NULL; |
| 1160 | if (*ptr == '\0' || *ptr++ != '/') |
| 1161 | return NULL; |
| 1162 | } |
| 1163 | /* skip [user[:passwd]@]host[:[port]] */ |
| 1164 | |
| 1165 | while (*ptr != '\0' && *ptr != ' ' && *ptr != '/') |
| 1166 | ptr++; |
| 1167 | |
| 1168 | if (*ptr == '\0' || *ptr == ' ') |
| 1169 | return NULL; |
| 1170 | |
| 1171 | /* OK, we got the '/' ! */ |
| 1172 | return ptr; |
| 1173 | } |
| 1174 | |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 1175 | /* Returns a 302 for a redirectable request that reaches a server working in |
| 1176 | * in redirect mode. This may only be called just after the stream interface |
| 1177 | * has moved to SI_ST_ASS. Unprocessable requests are left unchanged and will |
| 1178 | * follow normal proxy processing. NOTE: this function is designed to support |
| 1179 | * being called once data are scheduled for forwarding. |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 1180 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 1181 | void http_perform_server_redirect(struct stream *s, struct stream_interface *si) |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 1182 | { |
| 1183 | struct http_txn *txn; |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 1184 | struct server *srv; |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 1185 | char *path; |
Willy Tarreau | cde18fc | 2012-05-30 07:59:54 +0200 | [diff] [blame] | 1186 | int len, rewind; |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 1187 | |
| 1188 | /* 1: create the response header */ |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 1189 | trash.len = strlen(HTTP_302); |
| 1190 | memcpy(trash.str, HTTP_302, trash.len); |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 1191 | |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 1192 | srv = objt_server(s->target); |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 1193 | |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 1194 | /* 2: add the server's prefix */ |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 1195 | if (trash.len + srv->rdr_len > trash.size) |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 1196 | return; |
| 1197 | |
Willy Tarreau | dcb75c4 | 2010-01-10 00:24:22 +0100 | [diff] [blame] | 1198 | /* special prefix "/" means don't change URL */ |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 1199 | if (srv->rdr_len != 1 || *srv->rdr_pfx != '/') { |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 1200 | memcpy(trash.str + trash.len, srv->rdr_pfx, srv->rdr_len); |
| 1201 | trash.len += srv->rdr_len; |
Willy Tarreau | dcb75c4 | 2010-01-10 00:24:22 +0100 | [diff] [blame] | 1202 | } |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 1203 | |
Willy Tarreau | cde18fc | 2012-05-30 07:59:54 +0200 | [diff] [blame] | 1204 | /* 3: add the request URI. Since it was already forwarded, we need |
| 1205 | * to temporarily rewind the buffer. |
| 1206 | */ |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 1207 | txn = s->txn; |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 1208 | b_rew(s->req.buf, rewind = http_hdr_rewind(&txn->req)); |
Willy Tarreau | cde18fc | 2012-05-30 07:59:54 +0200 | [diff] [blame] | 1209 | |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 1210 | path = http_get_path(txn); |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 1211 | 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] | 1212 | |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 1213 | b_adv(s->req.buf, rewind); |
Willy Tarreau | cde18fc | 2012-05-30 07:59:54 +0200 | [diff] [blame] | 1214 | |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 1215 | if (!path) |
| 1216 | return; |
| 1217 | |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 1218 | if (trash.len + len > trash.size - 4) /* 4 for CRLF-CRLF */ |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 1219 | return; |
| 1220 | |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 1221 | memcpy(trash.str + trash.len, path, len); |
| 1222 | trash.len += len; |
Willy Tarreau | 88d349d | 2010-01-25 12:15:43 +0100 | [diff] [blame] | 1223 | |
| 1224 | if (unlikely(txn->flags & TX_USE_PX_CONN)) { |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 1225 | memcpy(trash.str + trash.len, "\r\nProxy-Connection: close\r\n\r\n", 29); |
| 1226 | trash.len += 29; |
Willy Tarreau | 88d349d | 2010-01-25 12:15:43 +0100 | [diff] [blame] | 1227 | } else { |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 1228 | memcpy(trash.str + trash.len, "\r\nConnection: close\r\n\r\n", 23); |
| 1229 | trash.len += 23; |
Willy Tarreau | 88d349d | 2010-01-25 12:15:43 +0100 | [diff] [blame] | 1230 | } |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 1231 | |
| 1232 | /* prepare to return without error. */ |
Willy Tarreau | 73b013b | 2012-05-21 16:31:45 +0200 | [diff] [blame] | 1233 | si_shutr(si); |
| 1234 | si_shutw(si); |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 1235 | si->err_type = SI_ET_NONE; |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 1236 | si->state = SI_ST_CLO; |
| 1237 | |
| 1238 | /* send the message */ |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 1239 | 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] | 1240 | |
| 1241 | /* 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] | 1242 | srv_inc_sess_ctr(srv); |
Bhaskar Maddala | a20cb85 | 2014-02-03 16:26:46 -0500 | [diff] [blame] | 1243 | srv_set_sess_last(srv); |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 1244 | } |
| 1245 | |
Willy Tarreau | 0cac36f | 2008-11-30 20:44:17 +0100 | [diff] [blame] | 1246 | /* Return the error message corresponding to si->err_type. It is assumed |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 1247 | * that the server side is closed. Note that err_type is actually a |
| 1248 | * bitmask, where almost only aborts may be cumulated with other |
| 1249 | * values. We consider that aborted operations are more important |
| 1250 | * than timeouts or errors due to the fact that nobody else in the |
| 1251 | * logs might explain incomplete retries. All others should avoid |
| 1252 | * being cumulated. It should normally not be possible to have multiple |
| 1253 | * 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] | 1254 | * Note that connection errors appearing on the second request of a keep-alive |
| 1255 | * connection are not reported since this allows the client to retry. |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 1256 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 1257 | void http_return_srv_error(struct stream *s, struct stream_interface *si) |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 1258 | { |
Willy Tarreau | 0cac36f | 2008-11-30 20:44:17 +0100 | [diff] [blame] | 1259 | int err_type = si->err_type; |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 1260 | |
| 1261 | if (err_type & SI_ET_QUEUE_ABRT) |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 1262 | http_server_error(s, si, SF_ERR_CLICL, SF_FINST_Q, |
Willy Tarreau | 783f258 | 2012-09-04 12:19:04 +0200 | [diff] [blame] | 1263 | 503, http_error_message(s, HTTP_ERR_503)); |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 1264 | else if (err_type & SI_ET_CONN_ABRT) |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 1265 | http_server_error(s, si, SF_ERR_CLICL, SF_FINST_C, |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 1266 | 503, (s->txn->flags & TX_NOT_FIRST) ? NULL : |
Willy Tarreau | 6b726ad | 2013-12-15 19:31:37 +0100 | [diff] [blame] | 1267 | http_error_message(s, HTTP_ERR_503)); |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 1268 | else if (err_type & SI_ET_QUEUE_TO) |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 1269 | http_server_error(s, si, SF_ERR_SRVTO, SF_FINST_Q, |
Willy Tarreau | 783f258 | 2012-09-04 12:19:04 +0200 | [diff] [blame] | 1270 | 503, http_error_message(s, HTTP_ERR_503)); |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 1271 | else if (err_type & SI_ET_QUEUE_ERR) |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 1272 | http_server_error(s, si, SF_ERR_SRVCL, SF_FINST_Q, |
Willy Tarreau | 783f258 | 2012-09-04 12:19:04 +0200 | [diff] [blame] | 1273 | 503, http_error_message(s, HTTP_ERR_503)); |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 1274 | else if (err_type & SI_ET_CONN_TO) |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 1275 | http_server_error(s, si, SF_ERR_SRVTO, SF_FINST_C, |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 1276 | 503, (s->txn->flags & TX_NOT_FIRST) ? NULL : |
Willy Tarreau | 6b726ad | 2013-12-15 19:31:37 +0100 | [diff] [blame] | 1277 | http_error_message(s, HTTP_ERR_503)); |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 1278 | else if (err_type & SI_ET_CONN_ERR) |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 1279 | http_server_error(s, si, SF_ERR_SRVCL, SF_FINST_C, |
| 1280 | 503, (s->flags & SF_SRV_REUSED) ? NULL : |
Willy Tarreau | 6b726ad | 2013-12-15 19:31:37 +0100 | [diff] [blame] | 1281 | http_error_message(s, HTTP_ERR_503)); |
Willy Tarreau | 2d400bb | 2012-05-14 12:11:47 +0200 | [diff] [blame] | 1282 | else if (err_type & SI_ET_CONN_RES) |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 1283 | http_server_error(s, si, SF_ERR_RESOURCE, SF_FINST_C, |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 1284 | 503, (s->txn->flags & TX_NOT_FIRST) ? NULL : |
Willy Tarreau | 6b726ad | 2013-12-15 19:31:37 +0100 | [diff] [blame] | 1285 | http_error_message(s, HTTP_ERR_503)); |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 1286 | else /* SI_ET_CONN_OTHER and others */ |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 1287 | http_server_error(s, si, SF_ERR_INTERNAL, SF_FINST_C, |
Willy Tarreau | 783f258 | 2012-09-04 12:19:04 +0200 | [diff] [blame] | 1288 | 500, http_error_message(s, HTTP_ERR_500)); |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 1289 | } |
| 1290 | |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 1291 | extern const char sess_term_cond[8]; |
| 1292 | extern const char sess_fin_state[8]; |
| 1293 | extern const char *monthname[12]; |
Willy Tarreau | 63986c7 | 2015-04-03 22:55:33 +0200 | [diff] [blame] | 1294 | struct pool_head *pool2_http_txn; |
Willy Tarreau | 332f8bf | 2007-05-13 21:36:56 +0200 | [diff] [blame] | 1295 | struct pool_head *pool2_requri; |
Willy Tarreau | 193b8c6 | 2012-11-22 00:17:38 +0100 | [diff] [blame] | 1296 | struct pool_head *pool2_capture = NULL; |
William Lallemand | a73203e | 2012-03-12 12:48:57 +0100 | [diff] [blame] | 1297 | struct pool_head *pool2_uniqueid; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1298 | |
Willy Tarreau | 117f59e | 2007-03-04 18:17:17 +0100 | [diff] [blame] | 1299 | /* |
| 1300 | * Capture headers from message starting at <som> according to header list |
Willy Tarreau | 54da8db | 2014-06-13 16:11:48 +0200 | [diff] [blame] | 1301 | * <cap_hdr>, and fill the <cap> pointers appropriately. |
Willy Tarreau | 117f59e | 2007-03-04 18:17:17 +0100 | [diff] [blame] | 1302 | */ |
| 1303 | void capture_headers(char *som, struct hdr_idx *idx, |
| 1304 | char **cap, struct cap_hdr *cap_hdr) |
| 1305 | { |
| 1306 | char *eol, *sol, *col, *sov; |
| 1307 | int cur_idx; |
| 1308 | struct cap_hdr *h; |
| 1309 | int len; |
| 1310 | |
| 1311 | sol = som + hdr_idx_first_pos(idx); |
| 1312 | cur_idx = hdr_idx_first_idx(idx); |
| 1313 | |
| 1314 | while (cur_idx) { |
| 1315 | eol = sol + idx->v[cur_idx].len; |
| 1316 | |
| 1317 | col = sol; |
| 1318 | while (col < eol && *col != ':') |
| 1319 | col++; |
| 1320 | |
| 1321 | sov = col + 1; |
Willy Tarreau | 2235b26 | 2016-11-05 15:50:20 +0100 | [diff] [blame] | 1322 | while (sov < eol && HTTP_IS_LWS(*sov)) |
Willy Tarreau | 117f59e | 2007-03-04 18:17:17 +0100 | [diff] [blame] | 1323 | sov++; |
| 1324 | |
| 1325 | for (h = cap_hdr; h; h = h->next) { |
Willy Tarreau | 54da8db | 2014-06-13 16:11:48 +0200 | [diff] [blame] | 1326 | if (h->namelen && (h->namelen == col - sol) && |
Willy Tarreau | 117f59e | 2007-03-04 18:17:17 +0100 | [diff] [blame] | 1327 | (strncasecmp(sol, h->name, h->namelen) == 0)) { |
| 1328 | if (cap[h->index] == NULL) |
| 1329 | cap[h->index] = |
Willy Tarreau | cf7f320 | 2007-05-13 22:46:04 +0200 | [diff] [blame] | 1330 | pool_alloc2(h->pool); |
Willy Tarreau | 117f59e | 2007-03-04 18:17:17 +0100 | [diff] [blame] | 1331 | |
| 1332 | if (cap[h->index] == NULL) { |
| 1333 | Alert("HTTP capture : out of memory.\n"); |
| 1334 | continue; |
| 1335 | } |
| 1336 | |
| 1337 | len = eol - sov; |
| 1338 | if (len > h->len) |
| 1339 | len = h->len; |
| 1340 | |
| 1341 | memcpy(cap[h->index], sov, len); |
| 1342 | cap[h->index][len]=0; |
| 1343 | } |
| 1344 | } |
| 1345 | sol = eol + idx->v[cur_idx].cr + 1; |
| 1346 | cur_idx = idx->v[cur_idx].next; |
| 1347 | } |
| 1348 | } |
| 1349 | |
| 1350 | |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 1351 | /* either we find an LF at <ptr> or we jump to <bad>. |
| 1352 | */ |
| 1353 | #define EXPECT_LF_HERE(ptr, bad) do { if (unlikely(*(ptr) != '\n')) goto bad; } while (0) |
| 1354 | |
| 1355 | /* plays with variables <ptr>, <end> and <state>. Jumps to <good> if OK, |
| 1356 | * otherwise to <http_msg_ood> with <state> set to <st>. |
| 1357 | */ |
| 1358 | #define EAT_AND_JUMP_OR_RETURN(good, st) do { \ |
| 1359 | ptr++; \ |
| 1360 | if (likely(ptr < end)) \ |
| 1361 | goto good; \ |
| 1362 | else { \ |
| 1363 | state = (st); \ |
| 1364 | goto http_msg_ood; \ |
| 1365 | } \ |
| 1366 | } while (0) |
| 1367 | |
| 1368 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1369 | /* |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 1370 | * This function parses a status line between <ptr> and <end>, starting with |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1371 | * parser state <state>. Only states HTTP_MSG_RPVER, HTTP_MSG_RPVER_SP, |
| 1372 | * HTTP_MSG_RPCODE, HTTP_MSG_RPCODE_SP and HTTP_MSG_RPREASON are handled. Others |
| 1373 | * will give undefined results. |
| 1374 | * Note that it is upon the caller's responsibility to ensure that ptr < end, |
| 1375 | * and that msg->sol points to the beginning of the response. |
| 1376 | * If a complete line is found (which implies that at least one CR or LF is |
| 1377 | * found before <end>, the updated <ptr> is returned, otherwise NULL is |
| 1378 | * returned indicating an incomplete line (which does not mean that parts have |
| 1379 | * not been updated). In the incomplete case, if <ret_ptr> or <ret_state> are |
| 1380 | * non-NULL, they are fed with the new <ptr> and <state> values to be passed |
| 1381 | * upon next call. |
| 1382 | * |
Willy Tarreau | 9cdde23 | 2007-05-02 20:58:19 +0200 | [diff] [blame] | 1383 | * This function was intentionally designed to be called from |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1384 | * http_msg_analyzer() with the lowest overhead. It should integrate perfectly |
| 1385 | * 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] | 1386 | * labels and variable names. Note that msg->sol is left unchanged. |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1387 | */ |
Willy Tarreau | 69d8c5d | 2012-05-08 09:44:41 +0200 | [diff] [blame] | 1388 | const char *http_parse_stsline(struct http_msg *msg, |
Willy Tarreau | 3770f23 | 2013-12-07 00:01:53 +0100 | [diff] [blame] | 1389 | enum ht_state state, const char *ptr, const char *end, |
| 1390 | unsigned int *ret_ptr, enum ht_state *ret_state) |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1391 | { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 1392 | const char *msg_start = msg->chn->buf->p; |
Willy Tarreau | 62f791e | 2012-03-09 11:32:30 +0100 | [diff] [blame] | 1393 | |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1394 | switch (state) { |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1395 | case HTTP_MSG_RPVER: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1396 | http_msg_rpver: |
Willy Tarreau | 4b89ad4 | 2007-03-04 18:13:58 +0100 | [diff] [blame] | 1397 | if (likely(HTTP_IS_VER_TOKEN(*ptr))) |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1398 | EAT_AND_JUMP_OR_RETURN(http_msg_rpver, HTTP_MSG_RPVER); |
| 1399 | |
| 1400 | if (likely(HTTP_IS_SPHT(*ptr))) { |
Willy Tarreau | ea1175a | 2012-03-05 15:52:30 +0100 | [diff] [blame] | 1401 | msg->sl.st.v_l = ptr - msg_start; |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1402 | EAT_AND_JUMP_OR_RETURN(http_msg_rpver_sp, HTTP_MSG_RPVER_SP); |
| 1403 | } |
Willy Tarreau | 7552c03 | 2009-03-01 11:10:40 +0100 | [diff] [blame] | 1404 | state = HTTP_MSG_ERROR; |
| 1405 | break; |
| 1406 | |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1407 | case HTTP_MSG_RPVER_SP: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1408 | http_msg_rpver_sp: |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1409 | if (likely(!HTTP_IS_LWS(*ptr))) { |
Willy Tarreau | ea1175a | 2012-03-05 15:52:30 +0100 | [diff] [blame] | 1410 | msg->sl.st.c = ptr - msg_start; |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1411 | goto http_msg_rpcode; |
| 1412 | } |
| 1413 | if (likely(HTTP_IS_SPHT(*ptr))) |
| 1414 | EAT_AND_JUMP_OR_RETURN(http_msg_rpver_sp, HTTP_MSG_RPVER_SP); |
| 1415 | /* so it's a CR/LF, this is invalid */ |
Willy Tarreau | 7552c03 | 2009-03-01 11:10:40 +0100 | [diff] [blame] | 1416 | state = HTTP_MSG_ERROR; |
| 1417 | break; |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1418 | |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1419 | case HTTP_MSG_RPCODE: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1420 | http_msg_rpcode: |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1421 | if (likely(!HTTP_IS_LWS(*ptr))) |
| 1422 | EAT_AND_JUMP_OR_RETURN(http_msg_rpcode, HTTP_MSG_RPCODE); |
| 1423 | |
| 1424 | if (likely(HTTP_IS_SPHT(*ptr))) { |
Willy Tarreau | ea1175a | 2012-03-05 15:52:30 +0100 | [diff] [blame] | 1425 | msg->sl.st.c_l = ptr - msg_start - msg->sl.st.c; |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1426 | EAT_AND_JUMP_OR_RETURN(http_msg_rpcode_sp, HTTP_MSG_RPCODE_SP); |
| 1427 | } |
| 1428 | |
| 1429 | /* so it's a CR/LF, so there is no reason phrase */ |
Willy Tarreau | ea1175a | 2012-03-05 15:52:30 +0100 | [diff] [blame] | 1430 | msg->sl.st.c_l = ptr - msg_start - msg->sl.st.c; |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1431 | http_msg_rsp_reason: |
| 1432 | /* FIXME: should we support HTTP responses without any reason phrase ? */ |
Willy Tarreau | ea1175a | 2012-03-05 15:52:30 +0100 | [diff] [blame] | 1433 | msg->sl.st.r = ptr - msg_start; |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1434 | msg->sl.st.r_l = 0; |
| 1435 | goto http_msg_rpline_eol; |
| 1436 | |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1437 | case HTTP_MSG_RPCODE_SP: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1438 | http_msg_rpcode_sp: |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1439 | if (likely(!HTTP_IS_LWS(*ptr))) { |
Willy Tarreau | ea1175a | 2012-03-05 15:52:30 +0100 | [diff] [blame] | 1440 | msg->sl.st.r = ptr - msg_start; |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1441 | goto http_msg_rpreason; |
| 1442 | } |
| 1443 | if (likely(HTTP_IS_SPHT(*ptr))) |
| 1444 | EAT_AND_JUMP_OR_RETURN(http_msg_rpcode_sp, HTTP_MSG_RPCODE_SP); |
| 1445 | /* so it's a CR/LF, so there is no reason phrase */ |
| 1446 | goto http_msg_rsp_reason; |
| 1447 | |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1448 | case HTTP_MSG_RPREASON: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1449 | http_msg_rpreason: |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1450 | if (likely(!HTTP_IS_CRLF(*ptr))) |
| 1451 | EAT_AND_JUMP_OR_RETURN(http_msg_rpreason, HTTP_MSG_RPREASON); |
Willy Tarreau | ea1175a | 2012-03-05 15:52:30 +0100 | [diff] [blame] | 1452 | msg->sl.st.r_l = ptr - msg_start - msg->sl.st.r; |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1453 | http_msg_rpline_eol: |
| 1454 | /* We have seen the end of line. Note that we do not |
| 1455 | * necessarily have the \n yet, but at least we know that we |
| 1456 | * have EITHER \r OR \n, otherwise the response would not be |
| 1457 | * complete. We can then record the response length and return |
| 1458 | * to the caller which will be able to register it. |
| 1459 | */ |
Willy Tarreau | 3a215be | 2012-03-09 21:39:51 +0100 | [diff] [blame] | 1460 | msg->sl.st.l = ptr - msg_start - msg->sol; |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1461 | return ptr; |
| 1462 | |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1463 | default: |
Willy Tarreau | 3770f23 | 2013-12-07 00:01:53 +0100 | [diff] [blame] | 1464 | #ifdef DEBUG_FULL |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1465 | fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state); |
| 1466 | exit(1); |
| 1467 | #endif |
Willy Tarreau | 3770f23 | 2013-12-07 00:01:53 +0100 | [diff] [blame] | 1468 | ; |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1469 | } |
| 1470 | |
| 1471 | http_msg_ood: |
Willy Tarreau | 7552c03 | 2009-03-01 11:10:40 +0100 | [diff] [blame] | 1472 | /* out of valid data */ |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1473 | if (ret_state) |
| 1474 | *ret_state = state; |
| 1475 | if (ret_ptr) |
Willy Tarreau | a458b67 | 2012-03-05 11:17:50 +0100 | [diff] [blame] | 1476 | *ret_ptr = ptr - msg_start; |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1477 | return NULL; |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1478 | } |
| 1479 | |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1480 | /* |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1481 | * This function parses a request line between <ptr> and <end>, starting with |
| 1482 | * parser state <state>. Only states HTTP_MSG_RQMETH, HTTP_MSG_RQMETH_SP, |
| 1483 | * HTTP_MSG_RQURI, HTTP_MSG_RQURI_SP and HTTP_MSG_RQVER are handled. Others |
| 1484 | * will give undefined results. |
| 1485 | * Note that it is upon the caller's responsibility to ensure that ptr < end, |
| 1486 | * and that msg->sol points to the beginning of the request. |
| 1487 | * If a complete line is found (which implies that at least one CR or LF is |
| 1488 | * found before <end>, the updated <ptr> is returned, otherwise NULL is |
| 1489 | * returned indicating an incomplete line (which does not mean that parts have |
| 1490 | * not been updated). In the incomplete case, if <ret_ptr> or <ret_state> are |
| 1491 | * non-NULL, they are fed with the new <ptr> and <state> values to be passed |
| 1492 | * upon next call. |
| 1493 | * |
Willy Tarreau | 9cdde23 | 2007-05-02 20:58:19 +0200 | [diff] [blame] | 1494 | * This function was intentionally designed to be called from |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1495 | * http_msg_analyzer() with the lowest overhead. It should integrate perfectly |
| 1496 | * 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] | 1497 | * labels and variable names. Note that msg->sol is left unchanged. |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1498 | */ |
Willy Tarreau | 69d8c5d | 2012-05-08 09:44:41 +0200 | [diff] [blame] | 1499 | const char *http_parse_reqline(struct http_msg *msg, |
Willy Tarreau | 3770f23 | 2013-12-07 00:01:53 +0100 | [diff] [blame] | 1500 | enum ht_state state, const char *ptr, const char *end, |
| 1501 | unsigned int *ret_ptr, enum ht_state *ret_state) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1502 | { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 1503 | const char *msg_start = msg->chn->buf->p; |
Willy Tarreau | 62f791e | 2012-03-09 11:32:30 +0100 | [diff] [blame] | 1504 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1505 | switch (state) { |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1506 | case HTTP_MSG_RQMETH: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1507 | http_msg_rqmeth: |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1508 | if (likely(HTTP_IS_TOKEN(*ptr))) |
| 1509 | EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth, HTTP_MSG_RQMETH); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1510 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1511 | if (likely(HTTP_IS_SPHT(*ptr))) { |
Willy Tarreau | ea1175a | 2012-03-05 15:52:30 +0100 | [diff] [blame] | 1512 | msg->sl.rq.m_l = ptr - msg_start; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1513 | EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth_sp, HTTP_MSG_RQMETH_SP); |
| 1514 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1515 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1516 | if (likely(HTTP_IS_CRLF(*ptr))) { |
| 1517 | /* HTTP 0.9 request */ |
Willy Tarreau | ea1175a | 2012-03-05 15:52:30 +0100 | [diff] [blame] | 1518 | msg->sl.rq.m_l = ptr - msg_start; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1519 | http_msg_req09_uri: |
Willy Tarreau | ea1175a | 2012-03-05 15:52:30 +0100 | [diff] [blame] | 1520 | msg->sl.rq.u = ptr - msg_start; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1521 | http_msg_req09_uri_e: |
Willy Tarreau | ea1175a | 2012-03-05 15:52:30 +0100 | [diff] [blame] | 1522 | msg->sl.rq.u_l = ptr - msg_start - msg->sl.rq.u; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1523 | http_msg_req09_ver: |
Willy Tarreau | ea1175a | 2012-03-05 15:52:30 +0100 | [diff] [blame] | 1524 | msg->sl.rq.v = ptr - msg_start; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1525 | msg->sl.rq.v_l = 0; |
| 1526 | goto http_msg_rqline_eol; |
| 1527 | } |
Willy Tarreau | 7552c03 | 2009-03-01 11:10:40 +0100 | [diff] [blame] | 1528 | state = HTTP_MSG_ERROR; |
| 1529 | break; |
| 1530 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1531 | case HTTP_MSG_RQMETH_SP: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1532 | http_msg_rqmeth_sp: |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1533 | if (likely(!HTTP_IS_LWS(*ptr))) { |
Willy Tarreau | ea1175a | 2012-03-05 15:52:30 +0100 | [diff] [blame] | 1534 | msg->sl.rq.u = ptr - msg_start; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1535 | goto http_msg_rquri; |
| 1536 | } |
| 1537 | if (likely(HTTP_IS_SPHT(*ptr))) |
| 1538 | EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth_sp, HTTP_MSG_RQMETH_SP); |
| 1539 | /* so it's a CR/LF, meaning an HTTP 0.9 request */ |
| 1540 | goto http_msg_req09_uri; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1541 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1542 | case HTTP_MSG_RQURI: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1543 | http_msg_rquri: |
Willy Tarreau | 5f10ea3 | 2016-11-05 17:52:06 +0100 | [diff] [blame] | 1544 | #if defined(__x86_64__) || \ |
| 1545 | defined(__i386__) || defined(__i486__) || defined(__i586__) || defined(__i686__) || \ |
| 1546 | defined(__ARM_ARCH_7A__) |
| 1547 | /* speedup: skip bytes not between 0x21 and 0x7e inclusive */ |
| 1548 | while (ptr <= end - sizeof(int)) { |
| 1549 | int x = *(int *)ptr - 0x21212121; |
| 1550 | if (x & 0x80808080) |
| 1551 | break; |
| 1552 | |
| 1553 | x -= 0x5e5e5e5e; |
| 1554 | if (!(x & 0x80808080)) |
| 1555 | break; |
| 1556 | |
| 1557 | ptr += sizeof(int); |
| 1558 | } |
| 1559 | #endif |
| 1560 | http_msg_rquri2: |
Willy Tarreau | 2e9506d | 2012-01-07 23:22:31 +0100 | [diff] [blame] | 1561 | if (likely((unsigned char)(*ptr - 33) <= 93)) /* 33 to 126 included */ |
Willy Tarreau | 5f10ea3 | 2016-11-05 17:52:06 +0100 | [diff] [blame] | 1562 | EAT_AND_JUMP_OR_RETURN(http_msg_rquri2, HTTP_MSG_RQURI); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1563 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1564 | if (likely(HTTP_IS_SPHT(*ptr))) { |
Willy Tarreau | ea1175a | 2012-03-05 15:52:30 +0100 | [diff] [blame] | 1565 | msg->sl.rq.u_l = ptr - msg_start - msg->sl.rq.u; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1566 | EAT_AND_JUMP_OR_RETURN(http_msg_rquri_sp, HTTP_MSG_RQURI_SP); |
| 1567 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1568 | |
Willy Tarreau | 2e9506d | 2012-01-07 23:22:31 +0100 | [diff] [blame] | 1569 | if (likely((unsigned char)*ptr >= 128)) { |
Willy Tarreau | 422246e | 2012-01-07 23:54:13 +0100 | [diff] [blame] | 1570 | /* non-ASCII chars are forbidden unless option |
| 1571 | * accept-invalid-http-request is enabled in the frontend. |
| 1572 | * In any case, we capture the faulty char. |
Willy Tarreau | 2e9506d | 2012-01-07 23:22:31 +0100 | [diff] [blame] | 1573 | */ |
Willy Tarreau | 422246e | 2012-01-07 23:54:13 +0100 | [diff] [blame] | 1574 | if (msg->err_pos < -1) |
| 1575 | goto invalid_char; |
| 1576 | if (msg->err_pos == -1) |
Willy Tarreau | 69d8c5d | 2012-05-08 09:44:41 +0200 | [diff] [blame] | 1577 | msg->err_pos = ptr - msg_start; |
Willy Tarreau | 2e9506d | 2012-01-07 23:22:31 +0100 | [diff] [blame] | 1578 | EAT_AND_JUMP_OR_RETURN(http_msg_rquri, HTTP_MSG_RQURI); |
| 1579 | } |
| 1580 | |
| 1581 | if (likely(HTTP_IS_CRLF(*ptr))) { |
| 1582 | /* so it's a CR/LF, meaning an HTTP 0.9 request */ |
| 1583 | goto http_msg_req09_uri_e; |
| 1584 | } |
| 1585 | |
| 1586 | /* OK forbidden chars, 0..31 or 127 */ |
Willy Tarreau | 422246e | 2012-01-07 23:54:13 +0100 | [diff] [blame] | 1587 | invalid_char: |
Willy Tarreau | 69d8c5d | 2012-05-08 09:44:41 +0200 | [diff] [blame] | 1588 | msg->err_pos = ptr - msg_start; |
Willy Tarreau | 2e9506d | 2012-01-07 23:22:31 +0100 | [diff] [blame] | 1589 | state = HTTP_MSG_ERROR; |
| 1590 | break; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1591 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1592 | case HTTP_MSG_RQURI_SP: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1593 | http_msg_rquri_sp: |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1594 | if (likely(!HTTP_IS_LWS(*ptr))) { |
Willy Tarreau | ea1175a | 2012-03-05 15:52:30 +0100 | [diff] [blame] | 1595 | msg->sl.rq.v = ptr - msg_start; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1596 | goto http_msg_rqver; |
| 1597 | } |
| 1598 | if (likely(HTTP_IS_SPHT(*ptr))) |
| 1599 | EAT_AND_JUMP_OR_RETURN(http_msg_rquri_sp, HTTP_MSG_RQURI_SP); |
| 1600 | /* so it's a CR/LF, meaning an HTTP 0.9 request */ |
| 1601 | goto http_msg_req09_ver; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1602 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1603 | case HTTP_MSG_RQVER: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1604 | http_msg_rqver: |
Willy Tarreau | 4b89ad4 | 2007-03-04 18:13:58 +0100 | [diff] [blame] | 1605 | if (likely(HTTP_IS_VER_TOKEN(*ptr))) |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1606 | EAT_AND_JUMP_OR_RETURN(http_msg_rqver, HTTP_MSG_RQVER); |
Willy Tarreau | 4b89ad4 | 2007-03-04 18:13:58 +0100 | [diff] [blame] | 1607 | |
| 1608 | if (likely(HTTP_IS_CRLF(*ptr))) { |
Willy Tarreau | ea1175a | 2012-03-05 15:52:30 +0100 | [diff] [blame] | 1609 | msg->sl.rq.v_l = ptr - msg_start - msg->sl.rq.v; |
Willy Tarreau | 4b89ad4 | 2007-03-04 18:13:58 +0100 | [diff] [blame] | 1610 | http_msg_rqline_eol: |
| 1611 | /* We have seen the end of line. Note that we do not |
| 1612 | * necessarily have the \n yet, but at least we know that we |
| 1613 | * have EITHER \r OR \n, otherwise the request would not be |
| 1614 | * complete. We can then record the request length and return |
| 1615 | * to the caller which will be able to register it. |
| 1616 | */ |
Willy Tarreau | 3a215be | 2012-03-09 21:39:51 +0100 | [diff] [blame] | 1617 | msg->sl.rq.l = ptr - msg_start - msg->sol; |
Willy Tarreau | 4b89ad4 | 2007-03-04 18:13:58 +0100 | [diff] [blame] | 1618 | return ptr; |
| 1619 | } |
| 1620 | |
| 1621 | /* neither an HTTP_VER token nor a CRLF */ |
Willy Tarreau | 7552c03 | 2009-03-01 11:10:40 +0100 | [diff] [blame] | 1622 | state = HTTP_MSG_ERROR; |
| 1623 | break; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1624 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1625 | default: |
Willy Tarreau | 3770f23 | 2013-12-07 00:01:53 +0100 | [diff] [blame] | 1626 | #ifdef DEBUG_FULL |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1627 | fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state); |
| 1628 | exit(1); |
| 1629 | #endif |
Willy Tarreau | 3770f23 | 2013-12-07 00:01:53 +0100 | [diff] [blame] | 1630 | ; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1631 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1632 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1633 | http_msg_ood: |
Willy Tarreau | 7552c03 | 2009-03-01 11:10:40 +0100 | [diff] [blame] | 1634 | /* out of valid data */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1635 | if (ret_state) |
| 1636 | *ret_state = state; |
| 1637 | if (ret_ptr) |
Willy Tarreau | a458b67 | 2012-03-05 11:17:50 +0100 | [diff] [blame] | 1638 | *ret_ptr = ptr - msg_start; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1639 | return NULL; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1640 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1641 | |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 1642 | /* |
| 1643 | * Returns the data from Authorization header. Function may be called more |
| 1644 | * than once so data is stored in txn->auth_data. When no header is found |
| 1645 | * 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] | 1646 | * searching again for something we are unable to find anyway. However, if |
| 1647 | * 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] | 1648 | * have the credentials overwritten by another stream in parallel. |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 1649 | */ |
| 1650 | |
Thierry FOURNIER | 9eec0a6 | 2014-01-22 18:38:02 +0100 | [diff] [blame] | 1651 | /* This bufffer is initialized in the file 'src/haproxy.c'. This length is |
| 1652 | * set according to global.tune.bufsize. |
| 1653 | */ |
Willy Tarreau | 7e2c647 | 2012-10-29 20:44:36 +0100 | [diff] [blame] | 1654 | char *get_http_auth_buff; |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 1655 | |
| 1656 | int |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 1657 | get_http_auth(struct stream *s) |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 1658 | { |
| 1659 | |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 1660 | struct http_txn *txn = s->txn; |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 1661 | struct chunk auth_method; |
| 1662 | struct hdr_ctx ctx; |
| 1663 | char *h, *p; |
| 1664 | int len; |
| 1665 | |
| 1666 | #ifdef DEBUG_AUTH |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 1667 | printf("Auth for stream %p: %d\n", s, txn->auth.method); |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 1668 | #endif |
| 1669 | |
| 1670 | if (txn->auth.method == HTTP_AUTH_WRONG) |
| 1671 | return 0; |
| 1672 | |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 1673 | txn->auth.method = HTTP_AUTH_WRONG; |
| 1674 | |
| 1675 | ctx.idx = 0; |
Willy Tarreau | 844a7e7 | 2010-01-31 21:46:18 +0100 | [diff] [blame] | 1676 | |
| 1677 | if (txn->flags & TX_USE_PX_CONN) { |
| 1678 | h = "Proxy-Authorization"; |
| 1679 | len = strlen(h); |
| 1680 | } else { |
| 1681 | h = "Authorization"; |
| 1682 | len = strlen(h); |
| 1683 | } |
| 1684 | |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 1685 | 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] | 1686 | return 0; |
| 1687 | |
| 1688 | h = ctx.line + ctx.val; |
| 1689 | |
| 1690 | p = memchr(h, ' ', ctx.vlen); |
Willy Tarreau | 5c557d1 | 2016-03-13 08:17:02 +0100 | [diff] [blame] | 1691 | len = p - h; |
| 1692 | if (!p || len <= 0) |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 1693 | return 0; |
| 1694 | |
David Carlier | 7365f7d | 2016-04-04 11:54:42 +0100 | [diff] [blame] | 1695 | if (chunk_initlen(&auth_method, h, 0, len) != 1) |
| 1696 | return 0; |
| 1697 | |
Willy Tarreau | 5c557d1 | 2016-03-13 08:17:02 +0100 | [diff] [blame] | 1698 | chunk_initlen(&txn->auth.method_data, p + 1, 0, ctx.vlen - len - 1); |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 1699 | |
| 1700 | if (!strncasecmp("Basic", auth_method.str, auth_method.len)) { |
| 1701 | |
| 1702 | len = base64dec(txn->auth.method_data.str, txn->auth.method_data.len, |
Willy Tarreau | 7e2c647 | 2012-10-29 20:44:36 +0100 | [diff] [blame] | 1703 | get_http_auth_buff, global.tune.bufsize - 1); |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 1704 | |
| 1705 | if (len < 0) |
| 1706 | return 0; |
| 1707 | |
| 1708 | |
| 1709 | get_http_auth_buff[len] = '\0'; |
| 1710 | |
| 1711 | p = strchr(get_http_auth_buff, ':'); |
| 1712 | |
| 1713 | if (!p) |
| 1714 | return 0; |
| 1715 | |
| 1716 | txn->auth.user = get_http_auth_buff; |
| 1717 | *p = '\0'; |
| 1718 | txn->auth.pass = p+1; |
| 1719 | |
| 1720 | txn->auth.method = HTTP_AUTH_BASIC; |
| 1721 | return 1; |
| 1722 | } |
| 1723 | |
| 1724 | return 0; |
| 1725 | } |
| 1726 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1727 | |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1728 | /* |
| 1729 | * This function parses an HTTP message, either a request or a response, |
Willy Tarreau | 8b1323e | 2012-03-09 14:46:19 +0100 | [diff] [blame] | 1730 | * depending on the initial msg->msg_state. The caller is responsible for |
| 1731 | * ensuring that the message does not wrap. The function can be preempted |
| 1732 | * everywhere when data are missing and recalled at the exact same location |
| 1733 | * with no information loss. The message may even be realigned between two |
| 1734 | * calls. The header index is re-initialized when switching from |
Willy Tarreau | 9cdde23 | 2007-05-02 20:58:19 +0200 | [diff] [blame] | 1735 | * 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] | 1736 | * fields. Note that msg->sol will be initialized after completing the first |
| 1737 | * state, so that none of the msg pointers has to be initialized prior to the |
| 1738 | * first call. |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1739 | */ |
Willy Tarreau | a560c21 | 2012-03-09 13:50:57 +0100 | [diff] [blame] | 1740 | void http_msg_analyzer(struct http_msg *msg, struct hdr_idx *idx) |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1741 | { |
Willy Tarreau | 3770f23 | 2013-12-07 00:01:53 +0100 | [diff] [blame] | 1742 | enum ht_state state; /* updated only when leaving the FSM */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1743 | register char *ptr, *end; /* request pointers, to avoid dereferences */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1744 | struct buffer *buf; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1745 | |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1746 | state = msg->msg_state; |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 1747 | buf = msg->chn->buf; |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1748 | ptr = buf->p + msg->next; |
| 1749 | end = buf->p + buf->i; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1750 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1751 | if (unlikely(ptr >= end)) |
| 1752 | goto http_msg_ood; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1753 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1754 | switch (state) { |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1755 | /* |
| 1756 | * First, states that are specific to the response only. |
| 1757 | * We check them first so that request and headers are |
| 1758 | * closer to each other (accessed more often). |
| 1759 | */ |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1760 | case HTTP_MSG_RPBEFORE: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1761 | http_msg_rpbefore: |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1762 | if (likely(HTTP_IS_TOKEN(*ptr))) { |
Willy Tarreau | 15de77e | 2010-01-02 21:59:16 +0100 | [diff] [blame] | 1763 | /* we have a start of message, but we have to check |
| 1764 | * first if we need to remove some CRLF. We can only |
Willy Tarreau | 2e046c6 | 2012-03-01 16:08:30 +0100 | [diff] [blame] | 1765 | * do this when o=0. |
Willy Tarreau | 15de77e | 2010-01-02 21:59:16 +0100 | [diff] [blame] | 1766 | */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1767 | if (unlikely(ptr != buf->p)) { |
| 1768 | if (buf->o) |
Willy Tarreau | 15de77e | 2010-01-02 21:59:16 +0100 | [diff] [blame] | 1769 | goto http_msg_ood; |
Willy Tarreau | 1d3bcce | 2009-12-27 15:50:06 +0100 | [diff] [blame] | 1770 | /* Remove empty leading lines, as recommended by RFC2616. */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1771 | bi_fast_delete(buf, ptr - buf->p); |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1772 | } |
Willy Tarreau | 2692736 | 2012-05-18 23:22:52 +0200 | [diff] [blame] | 1773 | msg->sol = 0; |
Willy Tarreau | e92693a | 2012-09-24 21:13:39 +0200 | [diff] [blame] | 1774 | msg->sl.st.l = 0; /* used in debug mode */ |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1775 | hdr_idx_init(idx); |
| 1776 | state = HTTP_MSG_RPVER; |
| 1777 | goto http_msg_rpver; |
| 1778 | } |
| 1779 | |
| 1780 | if (unlikely(!HTTP_IS_CRLF(*ptr))) |
| 1781 | goto http_msg_invalid; |
| 1782 | |
| 1783 | if (unlikely(*ptr == '\n')) |
| 1784 | EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore, HTTP_MSG_RPBEFORE); |
| 1785 | EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore_cr, HTTP_MSG_RPBEFORE_CR); |
| 1786 | /* stop here */ |
| 1787 | |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1788 | case HTTP_MSG_RPBEFORE_CR: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1789 | http_msg_rpbefore_cr: |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1790 | EXPECT_LF_HERE(ptr, http_msg_invalid); |
| 1791 | EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore, HTTP_MSG_RPBEFORE); |
| 1792 | /* stop here */ |
| 1793 | |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1794 | case HTTP_MSG_RPVER: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1795 | http_msg_rpver: |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1796 | case HTTP_MSG_RPVER_SP: |
| 1797 | case HTTP_MSG_RPCODE: |
| 1798 | case HTTP_MSG_RPCODE_SP: |
| 1799 | case HTTP_MSG_RPREASON: |
Willy Tarreau | 69d8c5d | 2012-05-08 09:44:41 +0200 | [diff] [blame] | 1800 | ptr = (char *)http_parse_stsline(msg, |
Willy Tarreau | a458b67 | 2012-03-05 11:17:50 +0100 | [diff] [blame] | 1801 | state, ptr, end, |
| 1802 | &msg->next, &msg->msg_state); |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1803 | if (unlikely(!ptr)) |
| 1804 | return; |
| 1805 | |
| 1806 | /* we have a full response and we know that we have either a CR |
| 1807 | * or an LF at <ptr>. |
| 1808 | */ |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1809 | hdr_idx_set_start(idx, msg->sl.st.l, *ptr == '\r'); |
| 1810 | |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1811 | msg->sol = ptr - buf->p; |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1812 | if (likely(*ptr == '\r')) |
| 1813 | EAT_AND_JUMP_OR_RETURN(http_msg_rpline_end, HTTP_MSG_RPLINE_END); |
| 1814 | goto http_msg_rpline_end; |
| 1815 | |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1816 | case HTTP_MSG_RPLINE_END: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1817 | http_msg_rpline_end: |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1818 | /* msg->sol must point to the first of CR or LF. */ |
| 1819 | EXPECT_LF_HERE(ptr, http_msg_invalid); |
| 1820 | EAT_AND_JUMP_OR_RETURN(http_msg_hdr_first, HTTP_MSG_HDR_FIRST); |
| 1821 | /* stop here */ |
| 1822 | |
| 1823 | /* |
| 1824 | * Second, states that are specific to the request only |
| 1825 | */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1826 | case HTTP_MSG_RQBEFORE: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1827 | http_msg_rqbefore: |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1828 | if (likely(HTTP_IS_TOKEN(*ptr))) { |
Willy Tarreau | 15de77e | 2010-01-02 21:59:16 +0100 | [diff] [blame] | 1829 | /* we have a start of message, but we have to check |
| 1830 | * first if we need to remove some CRLF. We can only |
Willy Tarreau | 2e046c6 | 2012-03-01 16:08:30 +0100 | [diff] [blame] | 1831 | * do this when o=0. |
Willy Tarreau | 15de77e | 2010-01-02 21:59:16 +0100 | [diff] [blame] | 1832 | */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1833 | if (likely(ptr != buf->p)) { |
| 1834 | if (buf->o) |
Willy Tarreau | 15de77e | 2010-01-02 21:59:16 +0100 | [diff] [blame] | 1835 | goto http_msg_ood; |
Willy Tarreau | 1d3bcce | 2009-12-27 15:50:06 +0100 | [diff] [blame] | 1836 | /* Remove empty leading lines, as recommended by RFC2616. */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1837 | bi_fast_delete(buf, ptr - buf->p); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1838 | } |
Willy Tarreau | 2692736 | 2012-05-18 23:22:52 +0200 | [diff] [blame] | 1839 | msg->sol = 0; |
Willy Tarreau | e92693a | 2012-09-24 21:13:39 +0200 | [diff] [blame] | 1840 | msg->sl.rq.l = 0; /* used in debug mode */ |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1841 | state = HTTP_MSG_RQMETH; |
| 1842 | goto http_msg_rqmeth; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1843 | } |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1844 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1845 | if (unlikely(!HTTP_IS_CRLF(*ptr))) |
| 1846 | goto http_msg_invalid; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1847 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1848 | if (unlikely(*ptr == '\n')) |
| 1849 | EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore, HTTP_MSG_RQBEFORE); |
| 1850 | 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] | 1851 | /* stop here */ |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1852 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1853 | case HTTP_MSG_RQBEFORE_CR: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1854 | http_msg_rqbefore_cr: |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1855 | EXPECT_LF_HERE(ptr, http_msg_invalid); |
| 1856 | EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore, HTTP_MSG_RQBEFORE); |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1857 | /* stop here */ |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1858 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1859 | case HTTP_MSG_RQMETH: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1860 | http_msg_rqmeth: |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1861 | case HTTP_MSG_RQMETH_SP: |
| 1862 | case HTTP_MSG_RQURI: |
| 1863 | case HTTP_MSG_RQURI_SP: |
| 1864 | case HTTP_MSG_RQVER: |
Willy Tarreau | 69d8c5d | 2012-05-08 09:44:41 +0200 | [diff] [blame] | 1865 | ptr = (char *)http_parse_reqline(msg, |
Willy Tarreau | a458b67 | 2012-03-05 11:17:50 +0100 | [diff] [blame] | 1866 | state, ptr, end, |
| 1867 | &msg->next, &msg->msg_state); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1868 | if (unlikely(!ptr)) |
| 1869 | return; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1870 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1871 | /* we have a full request and we know that we have either a CR |
| 1872 | * or an LF at <ptr>. |
| 1873 | */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1874 | hdr_idx_set_start(idx, msg->sl.rq.l, *ptr == '\r'); |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1875 | |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1876 | msg->sol = ptr - buf->p; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1877 | if (likely(*ptr == '\r')) |
| 1878 | 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] | 1879 | goto http_msg_rqline_end; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1880 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1881 | case HTTP_MSG_RQLINE_END: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1882 | http_msg_rqline_end: |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1883 | /* check for HTTP/0.9 request : no version information available. |
| 1884 | * msg->sol must point to the first of CR or LF. |
| 1885 | */ |
| 1886 | if (unlikely(msg->sl.rq.v_l == 0)) |
| 1887 | goto http_msg_last_lf; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1888 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1889 | EXPECT_LF_HERE(ptr, http_msg_invalid); |
| 1890 | 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] | 1891 | /* stop here */ |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1892 | |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1893 | /* |
| 1894 | * Common states below |
| 1895 | */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1896 | case HTTP_MSG_HDR_FIRST: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1897 | http_msg_hdr_first: |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1898 | msg->sol = ptr - buf->p; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1899 | if (likely(!HTTP_IS_CRLF(*ptr))) { |
| 1900 | goto http_msg_hdr_name; |
| 1901 | } |
| 1902 | |
| 1903 | if (likely(*ptr == '\r')) |
| 1904 | EAT_AND_JUMP_OR_RETURN(http_msg_last_lf, HTTP_MSG_LAST_LF); |
| 1905 | goto http_msg_last_lf; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1906 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1907 | case HTTP_MSG_HDR_NAME: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1908 | http_msg_hdr_name: |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1909 | /* assumes msg->sol points to the first char */ |
| 1910 | if (likely(HTTP_IS_TOKEN(*ptr))) |
| 1911 | 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] | 1912 | |
Willy Tarreau | fa4a03c | 2012-03-09 21:28:54 +0100 | [diff] [blame] | 1913 | if (likely(*ptr == ':')) |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1914 | 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] | 1915 | |
Willy Tarreau | 32a4ec0 | 2009-04-02 11:35:18 +0200 | [diff] [blame] | 1916 | if (likely(msg->err_pos < -1) || *ptr == '\n') |
| 1917 | goto http_msg_invalid; |
| 1918 | |
| 1919 | if (msg->err_pos == -1) /* capture error pointer */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1920 | msg->err_pos = ptr - buf->p; /* >= 0 now */ |
Willy Tarreau | 32a4ec0 | 2009-04-02 11:35:18 +0200 | [diff] [blame] | 1921 | |
| 1922 | /* and we still accept this non-token character */ |
| 1923 | 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] | 1924 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1925 | case HTTP_MSG_HDR_L1_SP: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1926 | http_msg_hdr_l1_sp: |
Willy Tarreau | fa4a03c | 2012-03-09 21:28:54 +0100 | [diff] [blame] | 1927 | /* assumes msg->sol points to the first char */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1928 | if (likely(HTTP_IS_SPHT(*ptr))) |
| 1929 | 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] | 1930 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1931 | /* header value can be basically anything except CR/LF */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1932 | msg->sov = ptr - buf->p; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1933 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1934 | if (likely(!HTTP_IS_CRLF(*ptr))) { |
| 1935 | goto http_msg_hdr_val; |
| 1936 | } |
| 1937 | |
| 1938 | if (likely(*ptr == '\r')) |
| 1939 | EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_lf, HTTP_MSG_HDR_L1_LF); |
| 1940 | goto http_msg_hdr_l1_lf; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1941 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1942 | case HTTP_MSG_HDR_L1_LF: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1943 | http_msg_hdr_l1_lf: |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1944 | EXPECT_LF_HERE(ptr, http_msg_invalid); |
| 1945 | 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] | 1946 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1947 | case HTTP_MSG_HDR_L1_LWS: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1948 | http_msg_hdr_l1_lws: |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1949 | if (likely(HTTP_IS_SPHT(*ptr))) { |
| 1950 | /* replace HT,CR,LF with spaces */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1951 | for (; buf->p + msg->sov < ptr; msg->sov++) |
| 1952 | buf->p[msg->sov] = ' '; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1953 | goto http_msg_hdr_l1_sp; |
| 1954 | } |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 1955 | /* we had a header consisting only in spaces ! */ |
Willy Tarreau | 12e48b3 | 2012-03-05 16:57:34 +0100 | [diff] [blame] | 1956 | msg->eol = msg->sov; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1957 | goto http_msg_complete_header; |
| 1958 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1959 | case HTTP_MSG_HDR_VAL: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1960 | http_msg_hdr_val: |
Willy Tarreau | fa4a03c | 2012-03-09 21:28:54 +0100 | [diff] [blame] | 1961 | /* assumes msg->sol points to the first char, and msg->sov |
| 1962 | * points to the first character of the value. |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1963 | */ |
Willy Tarreau | 0431f9d | 2016-11-05 17:35:40 +0100 | [diff] [blame] | 1964 | |
| 1965 | /* speedup: we'll skip packs of 4 or 8 bytes not containing bytes 0x0D |
| 1966 | * and lower. In fact since most of the time is spent in the loop, we |
| 1967 | * also remove the sign bit test so that bytes 0x8e..0x0d break the |
| 1968 | * loop, but we don't care since they're very rare in header values. |
| 1969 | */ |
| 1970 | #if defined(__x86_64__) |
| 1971 | while (ptr <= end - sizeof(long)) { |
| 1972 | if ((*(long *)ptr - 0x0e0e0e0e0e0e0e0eULL) & 0x8080808080808080ULL) |
| 1973 | goto http_msg_hdr_val2; |
| 1974 | ptr += sizeof(long); |
| 1975 | } |
| 1976 | #endif |
| 1977 | #if defined(__x86_64__) || \ |
| 1978 | defined(__i386__) || defined(__i486__) || defined(__i586__) || defined(__i686__) || \ |
| 1979 | defined(__ARM_ARCH_7A__) |
| 1980 | while (ptr <= end - sizeof(int)) { |
| 1981 | if ((*(int*)ptr - 0x0e0e0e0e) & 0x80808080) |
| 1982 | goto http_msg_hdr_val2; |
| 1983 | ptr += sizeof(int); |
| 1984 | } |
| 1985 | #endif |
| 1986 | http_msg_hdr_val2: |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1987 | if (likely(!HTTP_IS_CRLF(*ptr))) |
Willy Tarreau | 0431f9d | 2016-11-05 17:35:40 +0100 | [diff] [blame] | 1988 | EAT_AND_JUMP_OR_RETURN(http_msg_hdr_val2, HTTP_MSG_HDR_VAL); |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1989 | |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1990 | msg->eol = ptr - buf->p; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1991 | /* Note: we could also copy eol into ->eoh so that we have the |
| 1992 | * real header end in case it ends with lots of LWS, but is this |
| 1993 | * really needed ? |
| 1994 | */ |
| 1995 | if (likely(*ptr == '\r')) |
| 1996 | EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l2_lf, HTTP_MSG_HDR_L2_LF); |
| 1997 | goto http_msg_hdr_l2_lf; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1998 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1999 | case HTTP_MSG_HDR_L2_LF: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 2000 | http_msg_hdr_l2_lf: |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2001 | EXPECT_LF_HERE(ptr, http_msg_invalid); |
| 2002 | 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] | 2003 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2004 | case HTTP_MSG_HDR_L2_LWS: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 2005 | http_msg_hdr_l2_lws: |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2006 | if (unlikely(HTTP_IS_SPHT(*ptr))) { |
| 2007 | /* LWS: replace HT,CR,LF with spaces */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 2008 | for (; buf->p + msg->eol < ptr; msg->eol++) |
| 2009 | buf->p[msg->eol] = ' '; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2010 | goto http_msg_hdr_val; |
| 2011 | } |
| 2012 | http_msg_complete_header: |
| 2013 | /* |
| 2014 | * It was a new header, so the last one is finished. |
Willy Tarreau | fa4a03c | 2012-03-09 21:28:54 +0100 | [diff] [blame] | 2015 | * Assumes msg->sol points to the first char, msg->sov points |
| 2016 | * to the first character of the value and msg->eol to the |
| 2017 | * first CR or LF so we know how the line ends. We insert last |
| 2018 | * header into the index. |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2019 | */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 2020 | 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] | 2021 | idx, idx->tail) < 0)) |
| 2022 | goto http_msg_invalid; |
Willy Tarreau | 230fd0b | 2006-12-17 12:05:00 +0100 | [diff] [blame] | 2023 | |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 2024 | msg->sol = ptr - buf->p; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2025 | if (likely(!HTTP_IS_CRLF(*ptr))) { |
| 2026 | goto http_msg_hdr_name; |
| 2027 | } |
| 2028 | |
| 2029 | if (likely(*ptr == '\r')) |
| 2030 | EAT_AND_JUMP_OR_RETURN(http_msg_last_lf, HTTP_MSG_LAST_LF); |
| 2031 | goto http_msg_last_lf; |
Willy Tarreau | 230fd0b | 2006-12-17 12:05:00 +0100 | [diff] [blame] | 2032 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2033 | case HTTP_MSG_LAST_LF: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 2034 | http_msg_last_lf: |
Willy Tarreau | 0558a02 | 2014-03-13 15:48:45 +0100 | [diff] [blame] | 2035 | /* Assumes msg->sol points to the first of either CR or LF. |
| 2036 | * Sets ->sov and ->next to the total header length, ->eoh to |
| 2037 | * the last CRLF, and ->eol to the last CRLF length (1 or 2). |
| 2038 | */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2039 | EXPECT_LF_HERE(ptr, http_msg_invalid); |
| 2040 | ptr++; |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 2041 | msg->sov = msg->next = ptr - buf->p; |
Willy Tarreau | 3a215be | 2012-03-09 21:39:51 +0100 | [diff] [blame] | 2042 | msg->eoh = msg->sol; |
| 2043 | msg->sol = 0; |
Willy Tarreau | 0558a02 | 2014-03-13 15:48:45 +0100 | [diff] [blame] | 2044 | msg->eol = msg->sov - msg->eoh; |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 2045 | msg->msg_state = HTTP_MSG_BODY; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2046 | return; |
Willy Tarreau | b56928a | 2012-04-16 14:51:55 +0200 | [diff] [blame] | 2047 | |
| 2048 | case HTTP_MSG_ERROR: |
| 2049 | /* this may only happen if we call http_msg_analyser() twice with an error */ |
| 2050 | break; |
| 2051 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2052 | default: |
Willy Tarreau | 3770f23 | 2013-12-07 00:01:53 +0100 | [diff] [blame] | 2053 | #ifdef DEBUG_FULL |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2054 | fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state); |
| 2055 | exit(1); |
Willy Tarreau | 230fd0b | 2006-12-17 12:05:00 +0100 | [diff] [blame] | 2056 | #endif |
Willy Tarreau | 3770f23 | 2013-12-07 00:01:53 +0100 | [diff] [blame] | 2057 | ; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2058 | } |
| 2059 | http_msg_ood: |
| 2060 | /* out of data */ |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 2061 | msg->msg_state = state; |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 2062 | msg->next = ptr - buf->p; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2063 | return; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2064 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2065 | http_msg_invalid: |
| 2066 | /* invalid message */ |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 2067 | msg->msg_state = HTTP_MSG_ERROR; |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 2068 | msg->next = ptr - buf->p; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2069 | return; |
| 2070 | } |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 2071 | |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 2072 | /* convert an HTTP/0.9 request into an HTTP/1.0 request. Returns 1 if the |
| 2073 | * conversion succeeded, 0 in case of error. If the request was already 1.X, |
| 2074 | * nothing is done and 1 is returned. |
| 2075 | */ |
Willy Tarreau | 418bfcc | 2012-03-09 13:56:20 +0100 | [diff] [blame] | 2076 | static int http_upgrade_v09_to_v10(struct http_txn *txn) |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 2077 | { |
| 2078 | int delta; |
| 2079 | char *cur_end; |
Willy Tarreau | 418bfcc | 2012-03-09 13:56:20 +0100 | [diff] [blame] | 2080 | struct http_msg *msg = &txn->req; |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 2081 | |
| 2082 | if (msg->sl.rq.v_l != 0) |
| 2083 | return 1; |
| 2084 | |
Apollon Oikonomopoulos | 25a1522 | 2014-04-06 02:46:00 +0300 | [diff] [blame] | 2085 | /* RFC 1945 allows only GET for HTTP/0.9 requests */ |
| 2086 | if (txn->meth != HTTP_METH_GET) |
| 2087 | return 0; |
| 2088 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2089 | cur_end = msg->chn->buf->p + msg->sl.rq.l; |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 2090 | |
| 2091 | if (msg->sl.rq.u_l == 0) { |
Apollon Oikonomopoulos | 25a1522 | 2014-04-06 02:46:00 +0300 | [diff] [blame] | 2092 | /* HTTP/0.9 requests *must* have a request URI, per RFC 1945 */ |
| 2093 | return 0; |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 2094 | } |
| 2095 | /* add HTTP version */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2096 | 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] | 2097 | http_msg_move_end(msg, delta); |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 2098 | cur_end += delta; |
Willy Tarreau | 69d8c5d | 2012-05-08 09:44:41 +0200 | [diff] [blame] | 2099 | cur_end = (char *)http_parse_reqline(msg, |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 2100 | HTTP_MSG_RQMETH, |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2101 | msg->chn->buf->p, cur_end + 1, |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 2102 | NULL, NULL); |
| 2103 | if (unlikely(!cur_end)) |
| 2104 | return 0; |
| 2105 | |
| 2106 | /* we have a full HTTP/1.0 request now and we know that |
| 2107 | * we have either a CR or an LF at <ptr>. |
| 2108 | */ |
| 2109 | hdr_idx_set_start(&txn->hdr_idx, msg->sl.rq.l, *cur_end == '\r'); |
| 2110 | return 1; |
| 2111 | } |
| 2112 | |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 2113 | /* Parse the Connection: header of an HTTP request, looking for both "close" |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 2114 | * and "keep-alive" values. If we already know that some headers may safely |
| 2115 | * 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] | 2116 | * - bit 0 means remove "close" headers (in HTTP/1.0 requests/responses) |
| 2117 | * - 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] | 2118 | * Presence of the "Upgrade" token is also checked and reported. |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 2119 | * The TX_HDR_CONN_* flags are adjusted in txn->flags depending on what was |
| 2120 | * found, and TX_CON_*_SET is adjusted depending on what is left so only |
| 2121 | * harmless combinations may be removed. Do not call that after changes have |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 2122 | * been processed. |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 2123 | */ |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 2124 | 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] | 2125 | { |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 2126 | struct hdr_ctx ctx; |
Willy Tarreau | 88d349d | 2010-01-25 12:15:43 +0100 | [diff] [blame] | 2127 | const char *hdr_val = "Connection"; |
| 2128 | int hdr_len = 10; |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 2129 | |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 2130 | if (txn->flags & TX_HDR_CONN_PRS) |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 2131 | return; |
| 2132 | |
Willy Tarreau | 88d349d | 2010-01-25 12:15:43 +0100 | [diff] [blame] | 2133 | if (unlikely(txn->flags & TX_USE_PX_CONN)) { |
| 2134 | hdr_val = "Proxy-Connection"; |
| 2135 | hdr_len = 16; |
| 2136 | } |
| 2137 | |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 2138 | ctx.idx = 0; |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 2139 | txn->flags &= ~(TX_CON_KAL_SET|TX_CON_CLO_SET); |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2140 | 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] | 2141 | if (ctx.vlen >= 10 && word_match(ctx.line + ctx.val, ctx.vlen, "keep-alive", 10)) { |
| 2142 | txn->flags |= TX_HDR_CONN_KAL; |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 2143 | if (to_del & 2) |
| 2144 | http_remove_header2(msg, &txn->hdr_idx, &ctx); |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 2145 | else |
| 2146 | txn->flags |= TX_CON_KAL_SET; |
| 2147 | } |
| 2148 | else if (ctx.vlen >= 5 && word_match(ctx.line + ctx.val, ctx.vlen, "close", 5)) { |
| 2149 | txn->flags |= TX_HDR_CONN_CLO; |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 2150 | if (to_del & 1) |
| 2151 | http_remove_header2(msg, &txn->hdr_idx, &ctx); |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 2152 | else |
| 2153 | txn->flags |= TX_CON_CLO_SET; |
| 2154 | } |
Willy Tarreau | 50fc777 | 2012-11-11 22:19:57 +0100 | [diff] [blame] | 2155 | else if (ctx.vlen >= 7 && word_match(ctx.line + ctx.val, ctx.vlen, "upgrade", 7)) { |
| 2156 | txn->flags |= TX_HDR_CONN_UPG; |
| 2157 | } |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 2158 | } |
| 2159 | |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 2160 | txn->flags |= TX_HDR_CONN_PRS; |
| 2161 | return; |
| 2162 | } |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 2163 | |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 2164 | /* Apply desired changes on the Connection: header. Values may be removed and/or |
| 2165 | * added depending on the <wanted> flags, which are exclusively composed of |
| 2166 | * TX_CON_CLO_SET and TX_CON_KAL_SET, depending on what flags are desired. The |
| 2167 | * TX_CON_*_SET flags are adjusted in txn->flags depending on what is left. |
| 2168 | */ |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 2169 | 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] | 2170 | { |
| 2171 | struct hdr_ctx ctx; |
Willy Tarreau | 88d349d | 2010-01-25 12:15:43 +0100 | [diff] [blame] | 2172 | const char *hdr_val = "Connection"; |
| 2173 | int hdr_len = 10; |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 2174 | |
| 2175 | ctx.idx = 0; |
| 2176 | |
Willy Tarreau | 88d349d | 2010-01-25 12:15:43 +0100 | [diff] [blame] | 2177 | |
| 2178 | if (unlikely(txn->flags & TX_USE_PX_CONN)) { |
| 2179 | hdr_val = "Proxy-Connection"; |
| 2180 | hdr_len = 16; |
| 2181 | } |
| 2182 | |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 2183 | txn->flags &= ~(TX_CON_CLO_SET | TX_CON_KAL_SET); |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2184 | 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] | 2185 | if (ctx.vlen >= 10 && word_match(ctx.line + ctx.val, ctx.vlen, "keep-alive", 10)) { |
| 2186 | if (wanted & TX_CON_KAL_SET) |
| 2187 | txn->flags |= TX_CON_KAL_SET; |
| 2188 | else |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 2189 | http_remove_header2(msg, &txn->hdr_idx, &ctx); |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 2190 | } |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 2191 | else if (ctx.vlen >= 5 && word_match(ctx.line + ctx.val, ctx.vlen, "close", 5)) { |
| 2192 | if (wanted & TX_CON_CLO_SET) |
| 2193 | txn->flags |= TX_CON_CLO_SET; |
| 2194 | else |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 2195 | http_remove_header2(msg, &txn->hdr_idx, &ctx); |
Willy Tarreau | 0dfdf19 | 2010-01-05 11:33:11 +0100 | [diff] [blame] | 2196 | } |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 2197 | } |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 2198 | |
| 2199 | if (wanted == (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET))) |
| 2200 | return; |
| 2201 | |
| 2202 | if ((wanted & TX_CON_CLO_SET) && !(txn->flags & TX_CON_CLO_SET)) { |
| 2203 | txn->flags |= TX_CON_CLO_SET; |
Willy Tarreau | 88d349d | 2010-01-25 12:15:43 +0100 | [diff] [blame] | 2204 | hdr_val = "Connection: close"; |
| 2205 | hdr_len = 17; |
| 2206 | if (unlikely(txn->flags & TX_USE_PX_CONN)) { |
| 2207 | hdr_val = "Proxy-Connection: close"; |
| 2208 | hdr_len = 23; |
| 2209 | } |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 2210 | http_header_add_tail2(msg, &txn->hdr_idx, hdr_val, hdr_len); |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 2211 | } |
| 2212 | |
| 2213 | if ((wanted & TX_CON_KAL_SET) && !(txn->flags & TX_CON_KAL_SET)) { |
| 2214 | txn->flags |= TX_CON_KAL_SET; |
Willy Tarreau | 88d349d | 2010-01-25 12:15:43 +0100 | [diff] [blame] | 2215 | hdr_val = "Connection: keep-alive"; |
| 2216 | hdr_len = 22; |
| 2217 | if (unlikely(txn->flags & TX_USE_PX_CONN)) { |
| 2218 | hdr_val = "Proxy-Connection: keep-alive"; |
| 2219 | hdr_len = 28; |
| 2220 | } |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 2221 | http_header_add_tail2(msg, &txn->hdr_idx, hdr_val, hdr_len); |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 2222 | } |
| 2223 | return; |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 2224 | } |
| 2225 | |
Christopher Faulet | 113f7de | 2015-12-14 14:52:13 +0100 | [diff] [blame] | 2226 | /* Parse the chunk size at msg->next. Once done, caller should adjust ->next to |
| 2227 | * point to the first byte of data after the chunk size, so that we know we can |
| 2228 | * forward exactly msg->next bytes. msg->sol contains the exact number of bytes |
| 2229 | * forming the chunk size. That way it is always possible to differentiate |
| 2230 | * between the start of the body and the start of the data. Return the number |
| 2231 | * of byte parsed on success, 0 when some data is missing, <0 on error. Note: |
| 2232 | * 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] | 2233 | */ |
Willy Tarreau | 24e6d97 | 2012-10-26 00:49:52 +0200 | [diff] [blame] | 2234 | static inline int http_parse_chunk_size(struct http_msg *msg) |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 2235 | { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2236 | const struct buffer *buf = msg->chn->buf; |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 2237 | const char *ptr = b_ptr(buf, msg->next); |
Willy Tarreau | 4baf44b | 2012-03-09 14:10:20 +0100 | [diff] [blame] | 2238 | const char *ptr_old = ptr; |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 2239 | const char *end = buf->data + buf->size; |
| 2240 | const char *stop = bi_end(buf); |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 2241 | unsigned int chunk = 0; |
| 2242 | |
| 2243 | /* The chunk size is in the following form, though we are only |
| 2244 | * interested in the size and CRLF : |
| 2245 | * 1*HEXDIGIT *WSP *[ ';' extensions ] CRLF |
| 2246 | */ |
| 2247 | while (1) { |
| 2248 | int c; |
Willy Tarreau | 363a5bb | 2012-03-02 20:14:45 +0100 | [diff] [blame] | 2249 | if (ptr == stop) |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 2250 | return 0; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2251 | c = hex2i(*ptr); |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 2252 | if (c < 0) /* not a hex digit anymore */ |
| 2253 | break; |
Willy Tarreau | 0161d62 | 2013-04-02 01:26:55 +0200 | [diff] [blame] | 2254 | if (unlikely(++ptr >= end)) |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 2255 | ptr = buf->data; |
Willy Tarreau | 431946e | 2012-02-24 19:20:12 +0100 | [diff] [blame] | 2256 | if (chunk & 0xF8000000) /* integer overflow will occur if result >= 2GB */ |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 2257 | goto error; |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 2258 | chunk = (chunk << 4) + c; |
| 2259 | } |
| 2260 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2261 | /* empty size not allowed */ |
Willy Tarreau | 0161d62 | 2013-04-02 01:26:55 +0200 | [diff] [blame] | 2262 | if (unlikely(ptr == ptr_old)) |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 2263 | goto error; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2264 | |
Willy Tarreau | 2235b26 | 2016-11-05 15:50:20 +0100 | [diff] [blame] | 2265 | while (HTTP_IS_SPHT(*ptr)) { |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2266 | if (++ptr >= end) |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 2267 | ptr = buf->data; |
Willy Tarreau | 0161d62 | 2013-04-02 01:26:55 +0200 | [diff] [blame] | 2268 | if (unlikely(ptr == stop)) |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 2269 | return 0; |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 2270 | } |
| 2271 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2272 | /* Up to there, we know that at least one byte is present at *ptr. Check |
| 2273 | * for the end of chunk size. |
| 2274 | */ |
| 2275 | while (1) { |
| 2276 | if (likely(HTTP_IS_CRLF(*ptr))) { |
| 2277 | /* we now have a CR or an LF at ptr */ |
| 2278 | if (likely(*ptr == '\r')) { |
| 2279 | if (++ptr >= end) |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 2280 | ptr = buf->data; |
Willy Tarreau | 363a5bb | 2012-03-02 20:14:45 +0100 | [diff] [blame] | 2281 | if (ptr == stop) |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2282 | return 0; |
| 2283 | } |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 2284 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2285 | if (*ptr != '\n') |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 2286 | goto error; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2287 | if (++ptr >= end) |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 2288 | ptr = buf->data; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2289 | /* done */ |
| 2290 | break; |
| 2291 | } |
| 2292 | else if (*ptr == ';') { |
| 2293 | /* chunk extension, ends at next CRLF */ |
| 2294 | if (++ptr >= end) |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 2295 | ptr = buf->data; |
Willy Tarreau | 363a5bb | 2012-03-02 20:14:45 +0100 | [diff] [blame] | 2296 | if (ptr == stop) |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 2297 | return 0; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2298 | |
| 2299 | while (!HTTP_IS_CRLF(*ptr)) { |
| 2300 | if (++ptr >= end) |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 2301 | ptr = buf->data; |
Willy Tarreau | 363a5bb | 2012-03-02 20:14:45 +0100 | [diff] [blame] | 2302 | if (ptr == stop) |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2303 | return 0; |
| 2304 | } |
| 2305 | /* we have a CRLF now, loop above */ |
| 2306 | continue; |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 2307 | } |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2308 | else |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 2309 | goto error; |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 2310 | } |
| 2311 | |
Christopher Faulet | 113f7de | 2015-12-14 14:52:13 +0100 | [diff] [blame] | 2312 | /* OK we found our CRLF and now <ptr> points to the next byte, which may |
| 2313 | * or may not be present. We save the number of bytes parsed into |
| 2314 | * msg->sol. |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 2315 | */ |
Willy Tarreau | c24715e | 2014-04-17 15:21:20 +0200 | [diff] [blame] | 2316 | msg->sol = ptr - ptr_old; |
Willy Tarreau | 0161d62 | 2013-04-02 01:26:55 +0200 | [diff] [blame] | 2317 | if (unlikely(ptr < ptr_old)) |
Willy Tarreau | c24715e | 2014-04-17 15:21:20 +0200 | [diff] [blame] | 2318 | msg->sol += buf->size; |
Willy Tarreau | 124d991 | 2011-03-01 20:30:48 +0100 | [diff] [blame] | 2319 | msg->chunk_len = chunk; |
| 2320 | msg->body_len += chunk; |
Christopher Faulet | 113f7de | 2015-12-14 14:52:13 +0100 | [diff] [blame] | 2321 | return msg->sol; |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 2322 | error: |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 2323 | msg->err_pos = buffer_count(buf, buf->p, ptr); |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 2324 | return -1; |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 2325 | } |
| 2326 | |
Christopher Faulet | 113f7de | 2015-12-14 14:52:13 +0100 | [diff] [blame] | 2327 | /* This function skips trailers in the buffer associated with HTTP message |
| 2328 | * <msg>. The first visited position is msg->next. If the end of the trailers is |
| 2329 | * found, the function returns >0. So, the caller can automatically schedul it |
| 2330 | * to be forwarded, and switch msg->msg_state to HTTP_MSG_DONE. If not enough |
| 2331 | * data are available, the function does not change anything except maybe |
| 2332 | * msg->sol if it could parse some lines, and returns zero. If a parse error |
| 2333 | * is encountered, the function returns < 0 and does not change anything except |
| 2334 | * maybe msg->sol. Note that the message must already be in HTTP_MSG_TRAILERS |
| 2335 | * state before calling this function, which implies that all non-trailers data |
| 2336 | * have already been scheduled for forwarding, and that msg->next exactly |
| 2337 | * matches the length of trailers already parsed and not forwarded. It is also |
| 2338 | * important to note that this function is designed to be able to parse wrapped |
| 2339 | * headers at end of buffer. |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2340 | */ |
Willy Tarreau | 24e6d97 | 2012-10-26 00:49:52 +0200 | [diff] [blame] | 2341 | static int http_forward_trailers(struct http_msg *msg) |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2342 | { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2343 | const struct buffer *buf = msg->chn->buf; |
Willy Tarreau | 4baf44b | 2012-03-09 14:10:20 +0100 | [diff] [blame] | 2344 | |
Christopher Faulet | 113f7de | 2015-12-14 14:52:13 +0100 | [diff] [blame] | 2345 | /* we have msg->next which points to next line. Look for CRLF. But |
| 2346 | * first, we reset msg->sol */ |
| 2347 | msg->sol = 0; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2348 | while (1) { |
Willy Tarreau | 4baf44b | 2012-03-09 14:10:20 +0100 | [diff] [blame] | 2349 | const char *p1 = NULL, *p2 = NULL; |
Christopher Faulet | 2fb2880 | 2015-12-01 10:40:57 +0100 | [diff] [blame] | 2350 | const char *start = b_ptr(buf, msg->next + msg->sol); |
| 2351 | const char *stop = bi_end(buf); |
| 2352 | const char *ptr = start; |
| 2353 | int bytes = 0; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2354 | |
| 2355 | /* scan current line and stop at LF or CRLF */ |
| 2356 | while (1) { |
Willy Tarreau | 363a5bb | 2012-03-02 20:14:45 +0100 | [diff] [blame] | 2357 | if (ptr == stop) |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2358 | return 0; |
| 2359 | |
| 2360 | if (*ptr == '\n') { |
| 2361 | if (!p1) |
| 2362 | p1 = ptr; |
| 2363 | p2 = ptr; |
| 2364 | break; |
| 2365 | } |
| 2366 | |
| 2367 | if (*ptr == '\r') { |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 2368 | if (p1) { |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 2369 | msg->err_pos = buffer_count(buf, buf->p, ptr); |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2370 | return -1; |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 2371 | } |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2372 | p1 = ptr; |
| 2373 | } |
| 2374 | |
| 2375 | ptr++; |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 2376 | if (ptr >= buf->data + buf->size) |
| 2377 | ptr = buf->data; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2378 | } |
| 2379 | |
| 2380 | /* after LF; point to beginning of next line */ |
| 2381 | p2++; |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 2382 | if (p2 >= buf->data + buf->size) |
| 2383 | p2 = buf->data; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2384 | |
Christopher Faulet | 2fb2880 | 2015-12-01 10:40:57 +0100 | [diff] [blame] | 2385 | bytes = p2 - start; |
Willy Tarreau | 638cd02 | 2010-01-03 07:42:04 +0100 | [diff] [blame] | 2386 | if (bytes < 0) |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 2387 | bytes += buf->size; |
Christopher Faulet | 2fb2880 | 2015-12-01 10:40:57 +0100 | [diff] [blame] | 2388 | msg->sol += bytes; |
Willy Tarreau | 638cd02 | 2010-01-03 07:42:04 +0100 | [diff] [blame] | 2389 | |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 2390 | /* LF/CRLF at beginning of line => end of trailers at p2. |
| 2391 | * Everything was scheduled for forwarding, there's nothing left |
Christopher Faulet | 2fb2880 | 2015-12-01 10:40:57 +0100 | [diff] [blame] | 2392 | * from this message. */ |
| 2393 | if (p1 == start) |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2394 | return 1; |
Christopher Faulet | 2fb2880 | 2015-12-01 10:40:57 +0100 | [diff] [blame] | 2395 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2396 | /* OK, next line then */ |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2397 | } |
| 2398 | } |
| 2399 | |
Christopher Faulet | 113f7de | 2015-12-14 14:52:13 +0100 | [diff] [blame] | 2400 | /* This function may be called only in HTTP_MSG_CHUNK_CRLF. It reads the CRLF or |
| 2401 | * a possible LF alone at the end of a chunk. The caller should adjust msg->next |
| 2402 | * in order to include this part into the next forwarding phase. Note that the |
| 2403 | * caller must ensure that ->p points to the first byte to parse. It returns |
| 2404 | * the number of bytes parsed on success, so the caller can set msg_state to |
| 2405 | * HTTP_MSG_CHUNK_SIZE. If not enough data are available, the function does not |
| 2406 | * change anything and returns zero. If a parse error is encountered, the |
| 2407 | * function returns < 0. Note: this function is designed to parse wrapped CRLF |
| 2408 | * at the end of the buffer. |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2409 | */ |
Willy Tarreau | 24e6d97 | 2012-10-26 00:49:52 +0200 | [diff] [blame] | 2410 | static inline int http_skip_chunk_crlf(struct http_msg *msg) |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2411 | { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2412 | const struct buffer *buf = msg->chn->buf; |
Willy Tarreau | 4baf44b | 2012-03-09 14:10:20 +0100 | [diff] [blame] | 2413 | const char *ptr; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2414 | int bytes; |
| 2415 | |
| 2416 | /* NB: we'll check data availabilty at the end. It's not a |
| 2417 | * problem because whatever we match first will be checked |
| 2418 | * against the correct length. |
| 2419 | */ |
| 2420 | bytes = 1; |
Willy Tarreau | 0669d7d | 2014-04-17 11:40:10 +0200 | [diff] [blame] | 2421 | ptr = b_ptr(buf, msg->next); |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2422 | if (*ptr == '\r') { |
| 2423 | bytes++; |
| 2424 | ptr++; |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 2425 | if (ptr >= buf->data + buf->size) |
| 2426 | ptr = buf->data; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2427 | } |
| 2428 | |
Willy Tarreau | 0669d7d | 2014-04-17 11:40:10 +0200 | [diff] [blame] | 2429 | if (msg->next + bytes > buf->i) |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2430 | return 0; |
| 2431 | |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 2432 | if (*ptr != '\n') { |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 2433 | msg->err_pos = buffer_count(buf, buf->p, ptr); |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2434 | return -1; |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 2435 | } |
Christopher Faulet | 113f7de | 2015-12-14 14:52:13 +0100 | [diff] [blame] | 2436 | return bytes; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2437 | } |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 2438 | |
Willy Tarreau | 0e9b1b4 | 2014-03-19 12:07:52 +0100 | [diff] [blame] | 2439 | /* Parses a qvalue and returns it multipled by 1000, from 0 to 1000. If the |
| 2440 | * value is larger than 1000, it is bound to 1000. The parser consumes up to |
| 2441 | * 1 digit, one dot and 3 digits and stops on the first invalid character. |
| 2442 | * Unparsable qvalues return 1000 as "q=1.000". |
| 2443 | */ |
Thierry FOURNIER | ad90351 | 2014-04-11 17:51:01 +0200 | [diff] [blame] | 2444 | int parse_qvalue(const char *qvalue, const char **end) |
Willy Tarreau | 0e9b1b4 | 2014-03-19 12:07:52 +0100 | [diff] [blame] | 2445 | { |
| 2446 | int q = 1000; |
| 2447 | |
Willy Tarreau | 506c69a | 2014-07-08 00:59:48 +0200 | [diff] [blame] | 2448 | if (!isdigit((unsigned char)*qvalue)) |
Willy Tarreau | 0e9b1b4 | 2014-03-19 12:07:52 +0100 | [diff] [blame] | 2449 | goto out; |
| 2450 | q = (*qvalue++ - '0') * 1000; |
| 2451 | |
| 2452 | if (*qvalue++ != '.') |
| 2453 | goto out; |
| 2454 | |
Willy Tarreau | 506c69a | 2014-07-08 00:59:48 +0200 | [diff] [blame] | 2455 | if (!isdigit((unsigned char)*qvalue)) |
Willy Tarreau | 0e9b1b4 | 2014-03-19 12:07:52 +0100 | [diff] [blame] | 2456 | goto out; |
| 2457 | q += (*qvalue++ - '0') * 100; |
| 2458 | |
Willy Tarreau | 506c69a | 2014-07-08 00:59:48 +0200 | [diff] [blame] | 2459 | if (!isdigit((unsigned char)*qvalue)) |
Willy Tarreau | 0e9b1b4 | 2014-03-19 12:07:52 +0100 | [diff] [blame] | 2460 | goto out; |
| 2461 | q += (*qvalue++ - '0') * 10; |
| 2462 | |
Willy Tarreau | 506c69a | 2014-07-08 00:59:48 +0200 | [diff] [blame] | 2463 | if (!isdigit((unsigned char)*qvalue)) |
Willy Tarreau | 0e9b1b4 | 2014-03-19 12:07:52 +0100 | [diff] [blame] | 2464 | goto out; |
| 2465 | q += (*qvalue++ - '0') * 1; |
| 2466 | out: |
| 2467 | if (q > 1000) |
| 2468 | q = 1000; |
Willy Tarreau | 38b3aa5 | 2014-04-22 23:32:05 +0200 | [diff] [blame] | 2469 | if (end) |
Thierry FOURNIER | ad90351 | 2014-04-11 17:51:01 +0200 | [diff] [blame] | 2470 | *end = qvalue; |
Willy Tarreau | 0e9b1b4 | 2014-03-19 12:07:52 +0100 | [diff] [blame] | 2471 | return q; |
| 2472 | } |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 2473 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 2474 | 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] | 2475 | { |
Willy Tarreau | d0d8da9 | 2015-04-04 02:10:38 +0200 | [diff] [blame] | 2476 | struct proxy *fe = strm_fe(s); |
Willy Tarreau | 4e21ff9 | 2014-09-30 18:44:22 +0200 | [diff] [blame] | 2477 | int tmp = TX_CON_WANT_KAL; |
| 2478 | |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 2479 | if (!((fe->options2|s->be->options2) & PR_O2_FAKE_KA)) { |
| 2480 | if ((fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_TUN || |
Willy Tarreau | 4e21ff9 | 2014-09-30 18:44:22 +0200 | [diff] [blame] | 2481 | (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_TUN) |
| 2482 | tmp = TX_CON_WANT_TUN; |
| 2483 | |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 2484 | if ((fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL || |
Willy Tarreau | 4e21ff9 | 2014-09-30 18:44:22 +0200 | [diff] [blame] | 2485 | (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL) |
| 2486 | tmp = TX_CON_WANT_TUN; |
| 2487 | } |
| 2488 | |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 2489 | if ((fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_SCL || |
Willy Tarreau | 4e21ff9 | 2014-09-30 18:44:22 +0200 | [diff] [blame] | 2490 | (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_SCL) { |
| 2491 | /* option httpclose + server_close => forceclose */ |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 2492 | if ((fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL || |
Willy Tarreau | 4e21ff9 | 2014-09-30 18:44:22 +0200 | [diff] [blame] | 2493 | (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL) |
| 2494 | tmp = TX_CON_WANT_CLO; |
| 2495 | else |
| 2496 | tmp = TX_CON_WANT_SCL; |
| 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_FCL || |
Willy Tarreau | 4e21ff9 | 2014-09-30 18:44:22 +0200 | [diff] [blame] | 2500 | (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_FCL) |
| 2501 | tmp = TX_CON_WANT_CLO; |
| 2502 | |
| 2503 | if ((txn->flags & TX_CON_WANT_MSK) < tmp) |
| 2504 | txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | tmp; |
| 2505 | |
| 2506 | if (!(txn->flags & TX_HDR_CONN_PRS) && |
| 2507 | (txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) { |
| 2508 | /* parse the Connection header and possibly clean it */ |
| 2509 | int to_del = 0; |
| 2510 | if ((msg->flags & HTTP_MSGF_VER_11) || |
| 2511 | ((txn->flags & TX_CON_WANT_MSK) >= TX_CON_WANT_SCL && |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 2512 | !((fe->options2|s->be->options2) & PR_O2_FAKE_KA))) |
Willy Tarreau | 4e21ff9 | 2014-09-30 18:44:22 +0200 | [diff] [blame] | 2513 | to_del |= 2; /* remove "keep-alive" */ |
| 2514 | if (!(msg->flags & HTTP_MSGF_VER_11)) |
| 2515 | to_del |= 1; /* remove "close" */ |
| 2516 | http_parse_connection_header(txn, msg, to_del); |
| 2517 | } |
| 2518 | |
| 2519 | /* check if client or config asks for explicit close in KAL/SCL */ |
| 2520 | if (((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL || |
| 2521 | (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) && |
| 2522 | ((txn->flags & TX_HDR_CONN_CLO) || /* "connection: close" */ |
| 2523 | (!(msg->flags & HTTP_MSGF_VER_11) && !(txn->flags & TX_HDR_CONN_KAL)) || /* no "connection: k-a" in 1.0 */ |
| 2524 | !(msg->flags & HTTP_MSGF_XFER_LEN) || /* no length known => close */ |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 2525 | fe->state == PR_STSTOPPED)) /* frontend is stopping */ |
Willy Tarreau | 4e21ff9 | 2014-09-30 18:44:22 +0200 | [diff] [blame] | 2526 | txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO; |
| 2527 | } |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 2528 | |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 2529 | /* This stream analyser waits for a complete HTTP request. It returns 1 if the |
| 2530 | * processing can continue on next analysers, or zero if it either needs more |
| 2531 | * data or wants to immediately abort the request (eg: timeout, error, ...). It |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 2532 | * 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] | 2533 | * when it has nothing left to do, and may remove any analyser when it wants to |
| 2534 | * abort. |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2535 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 2536 | 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] | 2537 | { |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2538 | /* |
| 2539 | * We will parse the partial (or complete) lines. |
| 2540 | * We will check the request syntax, and also join multi-line |
| 2541 | * headers. An index of all the lines will be elaborated while |
| 2542 | * parsing. |
| 2543 | * |
| 2544 | * For the parsing, we use a 28 states FSM. |
| 2545 | * |
| 2546 | * Here is the information we currently have : |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2547 | * req->buf->p = beginning of request |
| 2548 | * req->buf->p + msg->eoh = end of processed headers / start of current one |
| 2549 | * req->buf->p + req->buf->i = end of input data |
Willy Tarreau | 2692736 | 2012-05-18 23:22:52 +0200 | [diff] [blame] | 2550 | * msg->eol = end of current header or line (LF or CRLF) |
| 2551 | * msg->next = first non-visited byte |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 2552 | * |
| 2553 | * 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] | 2554 | * we will set a few fields (txn->meth, sn->flags/SF_REDIRECTABLE). |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 2555 | * We also check for monitor-uri, logging, HTTP/0.9 to 1.0 conversion, and |
| 2556 | * finally headers capture. |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2557 | */ |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 2558 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2559 | int cur_idx; |
Willy Tarreau | fb0afa7 | 2015-04-03 14:46:27 +0200 | [diff] [blame] | 2560 | struct session *sess = s->sess; |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 2561 | struct http_txn *txn = s->txn; |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2562 | struct http_msg *msg = &txn->req; |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 2563 | struct hdr_ctx ctx; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 2564 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 2565 | 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] | 2566 | now_ms, __FUNCTION__, |
| 2567 | s, |
| 2568 | req, |
| 2569 | req->rex, req->wex, |
| 2570 | req->flags, |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2571 | req->buf->i, |
Willy Tarreau | 6bf1736 | 2009-02-24 10:48:35 +0100 | [diff] [blame] | 2572 | req->analysers); |
| 2573 | |
Willy Tarreau | 52a0c60 | 2009-08-16 22:45:38 +0200 | [diff] [blame] | 2574 | /* we're speaking HTTP here, so let's speak HTTP to the client */ |
| 2575 | s->srv_error = http_return_srv_error; |
| 2576 | |
Willy Tarreau | 83e3af0 | 2009-12-28 17:39:57 +0100 | [diff] [blame] | 2577 | /* There's a protected area at the end of the buffer for rewriting |
| 2578 | * purposes. We don't want to start to parse the request if the |
| 2579 | * protected area is affected, because we may have to move processed |
| 2580 | * data later, which is much more complicated. |
| 2581 | */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2582 | if (buffer_not_empty(req->buf) && msg->msg_state < HTTP_MSG_ERROR) { |
Thierry FOURNIER / OZON.IO | 4cac359 | 2016-07-28 17:19:45 +0200 | [diff] [blame] | 2583 | |
| 2584 | /* This point is executed when some data is avalaible for analysis, |
| 2585 | * so we log the end of the idle time. */ |
| 2586 | if (s->logs.t_idle == -1) |
| 2587 | s->logs.t_idle = tv_ms_elapsed(&s->logs.tv_accept, &now) - s->logs.t_handshake; |
| 2588 | |
Willy Tarreau | 379357a | 2013-06-08 12:55:46 +0200 | [diff] [blame] | 2589 | if (txn->flags & TX_NOT_FIRST) { |
Willy Tarreau | ba0902e | 2015-01-13 14:39:16 +0100 | [diff] [blame] | 2590 | if (unlikely(!channel_is_rewritable(req))) { |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 2591 | 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] | 2592 | goto failed_keep_alive; |
Willy Tarreau | 2ab6eb1 | 2010-01-02 22:04:45 +0100 | [diff] [blame] | 2593 | /* 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] | 2594 | channel_dont_connect(req); |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 2595 | req->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */ |
Willy Tarreau | d7ad9f5 | 2013-12-31 17:26:25 +0100 | [diff] [blame] | 2596 | req->flags |= CF_WAKE_WRITE; |
Willy Tarreau | 2ab6eb1 | 2010-01-02 22:04:45 +0100 | [diff] [blame] | 2597 | return 0; |
| 2598 | } |
Willy Tarreau | 379357a | 2013-06-08 12:55:46 +0200 | [diff] [blame] | 2599 | if (unlikely(bi_end(req->buf) < b_ptr(req->buf, msg->next) || |
| 2600 | bi_end(req->buf) > req->buf->data + req->buf->size - global.tune.maxrewrite)) |
| 2601 | buffer_slow_realign(req->buf); |
Willy Tarreau | 83e3af0 | 2009-12-28 17:39:57 +0100 | [diff] [blame] | 2602 | } |
| 2603 | |
Willy Tarreau | 065e833 | 2010-01-08 00:30:20 +0100 | [diff] [blame] | 2604 | /* Note that we have the same problem with the response ; we |
| 2605 | * may want to send a redirect, error or anything which requires |
| 2606 | * some spare space. So we'll ensure that we have at least |
| 2607 | * maxrewrite bytes available in the response buffer before |
| 2608 | * processing that one. This will only affect pipelined |
| 2609 | * keep-alive requests. |
| 2610 | */ |
| 2611 | if ((txn->flags & TX_NOT_FIRST) && |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 2612 | unlikely(!channel_is_rewritable(&s->res) || |
| 2613 | bi_end(s->res.buf) < b_ptr(s->res.buf, txn->rsp.next) || |
| 2614 | bi_end(s->res.buf) > s->res.buf->data + s->res.buf->size - global.tune.maxrewrite)) { |
| 2615 | if (s->res.buf->o) { |
| 2616 | 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] | 2617 | goto failed_keep_alive; |
Willy Tarreau | 065e833 | 2010-01-08 00:30:20 +0100 | [diff] [blame] | 2618 | /* don't let a connection request be initiated */ |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 2619 | channel_dont_connect(req); |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 2620 | s->res.flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */ |
| 2621 | s->res.flags |= CF_WAKE_WRITE; |
| 2622 | s->res.analysers |= an_bit; /* wake us up once it changes */ |
Willy Tarreau | 065e833 | 2010-01-08 00:30:20 +0100 | [diff] [blame] | 2623 | return 0; |
| 2624 | } |
| 2625 | } |
| 2626 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2627 | if (likely(msg->next < req->buf->i)) /* some unparsed data are available */ |
Willy Tarreau | a560c21 | 2012-03-09 13:50:57 +0100 | [diff] [blame] | 2628 | http_msg_analyzer(msg, &txn->hdr_idx); |
Willy Tarreau | 83e3af0 | 2009-12-28 17:39:57 +0100 | [diff] [blame] | 2629 | } |
| 2630 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2631 | /* 1: we might have to print this header in debug mode */ |
| 2632 | if (unlikely((global.mode & MODE_DEBUG) && |
| 2633 | (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) && |
Willy Tarreau | 7d59e90 | 2014-10-21 19:36:09 +0200 | [diff] [blame] | 2634 | msg->msg_state >= HTTP_MSG_BODY)) { |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2635 | char *eol, *sol; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2636 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2637 | sol = req->buf->p; |
Willy Tarreau | e92693a | 2012-09-24 21:13:39 +0200 | [diff] [blame] | 2638 | /* this is a bit complex : in case of error on the request line, |
| 2639 | * we know that rq.l is still zero, so we display only the part |
| 2640 | * up to the end of the line (truncated by debug_hdr). |
| 2641 | */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2642 | 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] | 2643 | debug_hdr("clireq", s, sol, eol); |
Willy Tarreau | 45e73e3 | 2006-12-17 00:05:15 +0100 | [diff] [blame] | 2644 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2645 | sol += hdr_idx_first_pos(&txn->hdr_idx); |
| 2646 | cur_idx = hdr_idx_first_idx(&txn->hdr_idx); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2647 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2648 | while (cur_idx) { |
| 2649 | eol = sol + txn->hdr_idx.v[cur_idx].len; |
| 2650 | debug_hdr("clihdr", s, sol, eol); |
| 2651 | sol = eol + txn->hdr_idx.v[cur_idx].cr + 1; |
| 2652 | cur_idx = txn->hdr_idx.v[cur_idx].next; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2653 | } |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2654 | } |
| 2655 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2656 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2657 | /* |
| 2658 | * Now we quickly check if we have found a full valid request. |
| 2659 | * If not so, we check the FD and buffer states before leaving. |
| 2660 | * A full request is indicated by the fact that we have seen |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 2661 | * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid |
Willy Tarreau | d3c343f | 2010-01-16 10:26:19 +0100 | [diff] [blame] | 2662 | * requests are checked first. When waiting for a second request |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 2663 | * on a keep-alive stream, if we encounter and error, close, t/o, |
| 2664 | * 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] | 2665 | * 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] | 2666 | * process_stream() as a frontend error. |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 2667 | * Last, we may increase some tracked counters' http request errors on |
| 2668 | * the cases that are deliberately the client's fault. For instance, |
| 2669 | * a timeout or connection reset is not counted as an error. However |
| 2670 | * a bad request is. |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2671 | */ |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2672 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 2673 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2674 | /* |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2675 | * First, let's catch bad requests. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2676 | */ |
Willy Tarreau | 3e1b6d1 | 2010-03-04 23:02:38 +0100 | [diff] [blame] | 2677 | if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) { |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 2678 | stream_inc_http_req_ctr(s); |
| 2679 | stream_inc_http_err_ctr(s); |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 2680 | proxy_inc_fe_req_ctr(sess->fe); |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2681 | goto return_bad_req; |
Willy Tarreau | 3e1b6d1 | 2010-03-04 23:02:38 +0100 | [diff] [blame] | 2682 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2683 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2684 | /* 1: Since we are in header mode, if there's no space |
| 2685 | * left for headers, we won't be able to free more |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 2686 | * later, so the stream will never terminate. We |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2687 | * must terminate it now. |
| 2688 | */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2689 | if (unlikely(buffer_full(req->buf, global.tune.maxrewrite))) { |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2690 | /* FIXME: check if URI is set and return Status |
| 2691 | * 414 Request URI too long instead. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2692 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 2693 | stream_inc_http_req_ctr(s); |
| 2694 | stream_inc_http_err_ctr(s); |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 2695 | proxy_inc_fe_req_ctr(sess->fe); |
Willy Tarreau | fec4d89 | 2011-09-02 20:04:57 +0200 | [diff] [blame] | 2696 | if (msg->err_pos < 0) |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2697 | msg->err_pos = req->buf->i; |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2698 | goto return_bad_req; |
| 2699 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2700 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2701 | /* 2: have we encountered a read error ? */ |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 2702 | else if (req->flags & CF_READ_ERROR) { |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 2703 | if (!(s->flags & SF_ERR_MASK)) |
| 2704 | s->flags |= SF_ERR_CLICL; |
Willy Tarreau | d3c343f | 2010-01-16 10:26:19 +0100 | [diff] [blame] | 2705 | |
Willy Tarreau | fcffa69 | 2010-01-10 14:21:19 +0100 | [diff] [blame] | 2706 | if (txn->flags & TX_WAIT_NEXT_RQ) |
Willy Tarreau | b608feb | 2010-01-02 22:47:18 +0100 | [diff] [blame] | 2707 | goto failed_keep_alive; |
| 2708 | |
Willy Tarreau | 0f228a0 | 2015-05-01 15:37:53 +0200 | [diff] [blame] | 2709 | if (sess->fe->options & PR_O_IGNORE_PRB) |
| 2710 | goto failed_keep_alive; |
| 2711 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2712 | /* we cannot return any message on error */ |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 2713 | if (msg->err_pos >= 0) { |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 2714 | 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] | 2715 | stream_inc_http_err_ctr(s); |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 2716 | } |
| 2717 | |
Willy Tarreau | dc979f2 | 2012-12-04 10:39:01 +0100 | [diff] [blame] | 2718 | txn->status = 400; |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2719 | msg->msg_state = HTTP_MSG_ERROR; |
Christopher Faulet | a94e5a5 | 2015-12-09 15:55:06 +0100 | [diff] [blame] | 2720 | http_reply_and_close(s, txn->status, NULL); |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 2721 | req->analysers &= AN_FLT_END; |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 2722 | stream_inc_http_req_ctr(s); |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 2723 | proxy_inc_fe_req_ctr(sess->fe); |
| 2724 | sess->fe->fe_counters.failed_req++; |
Willy Tarreau | fb0afa7 | 2015-04-03 14:46:27 +0200 | [diff] [blame] | 2725 | if (sess->listener->counters) |
| 2726 | sess->listener->counters->failed_req++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 2727 | |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 2728 | if (!(s->flags & SF_FINST_MASK)) |
| 2729 | s->flags |= SF_FINST_R; |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2730 | return 0; |
| 2731 | } |
Willy Tarreau | f9839bd | 2008-08-27 23:57:16 +0200 | [diff] [blame] | 2732 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2733 | /* 3: has the read timeout expired ? */ |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 2734 | 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] | 2735 | if (!(s->flags & SF_ERR_MASK)) |
| 2736 | s->flags |= SF_ERR_CLITO; |
Willy Tarreau | d3c343f | 2010-01-16 10:26:19 +0100 | [diff] [blame] | 2737 | |
Willy Tarreau | fcffa69 | 2010-01-10 14:21:19 +0100 | [diff] [blame] | 2738 | if (txn->flags & TX_WAIT_NEXT_RQ) |
Willy Tarreau | b608feb | 2010-01-02 22:47:18 +0100 | [diff] [blame] | 2739 | goto failed_keep_alive; |
| 2740 | |
Willy Tarreau | 0f228a0 | 2015-05-01 15:37:53 +0200 | [diff] [blame] | 2741 | if (sess->fe->options & PR_O_IGNORE_PRB) |
| 2742 | goto failed_keep_alive; |
| 2743 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2744 | /* read timeout : give up with an error message. */ |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 2745 | if (msg->err_pos >= 0) { |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 2746 | 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] | 2747 | stream_inc_http_err_ctr(s); |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 2748 | } |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2749 | txn->status = 408; |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2750 | msg->msg_state = HTTP_MSG_ERROR; |
Christopher Faulet | a94e5a5 | 2015-12-09 15:55:06 +0100 | [diff] [blame] | 2751 | http_reply_and_close(s, txn->status, http_error_message(s, HTTP_ERR_408)); |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 2752 | req->analysers &= AN_FLT_END; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 2753 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 2754 | stream_inc_http_req_ctr(s); |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 2755 | proxy_inc_fe_req_ctr(sess->fe); |
| 2756 | sess->fe->fe_counters.failed_req++; |
Willy Tarreau | fb0afa7 | 2015-04-03 14:46:27 +0200 | [diff] [blame] | 2757 | if (sess->listener->counters) |
| 2758 | sess->listener->counters->failed_req++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 2759 | |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 2760 | if (!(s->flags & SF_FINST_MASK)) |
| 2761 | s->flags |= SF_FINST_R; |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2762 | return 0; |
| 2763 | } |
Willy Tarreau | c65a3ba | 2008-08-11 23:42:50 +0200 | [diff] [blame] | 2764 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2765 | /* 4: have we encountered a close ? */ |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 2766 | else if (req->flags & CF_SHUTR) { |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 2767 | if (!(s->flags & SF_ERR_MASK)) |
| 2768 | s->flags |= SF_ERR_CLICL; |
Willy Tarreau | d3c343f | 2010-01-16 10:26:19 +0100 | [diff] [blame] | 2769 | |
Willy Tarreau | fcffa69 | 2010-01-10 14:21:19 +0100 | [diff] [blame] | 2770 | if (txn->flags & TX_WAIT_NEXT_RQ) |
Willy Tarreau | b608feb | 2010-01-02 22:47:18 +0100 | [diff] [blame] | 2771 | goto failed_keep_alive; |
| 2772 | |
Willy Tarreau | 0f228a0 | 2015-05-01 15:37:53 +0200 | [diff] [blame] | 2773 | if (sess->fe->options & PR_O_IGNORE_PRB) |
| 2774 | goto failed_keep_alive; |
| 2775 | |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 2776 | if (msg->err_pos >= 0) |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 2777 | 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] | 2778 | txn->status = 400; |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2779 | msg->msg_state = HTTP_MSG_ERROR; |
Christopher Faulet | a94e5a5 | 2015-12-09 15:55:06 +0100 | [diff] [blame] | 2780 | http_reply_and_close(s, txn->status, http_error_message(s, HTTP_ERR_400)); |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 2781 | req->analysers &= AN_FLT_END; |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 2782 | stream_inc_http_err_ctr(s); |
| 2783 | stream_inc_http_req_ctr(s); |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 2784 | proxy_inc_fe_req_ctr(sess->fe); |
| 2785 | sess->fe->fe_counters.failed_req++; |
Willy Tarreau | fb0afa7 | 2015-04-03 14:46:27 +0200 | [diff] [blame] | 2786 | if (sess->listener->counters) |
| 2787 | sess->listener->counters->failed_req++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 2788 | |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 2789 | if (!(s->flags & SF_FINST_MASK)) |
| 2790 | s->flags |= SF_FINST_R; |
Willy Tarreau | dafde43 | 2008-08-17 01:00:46 +0200 | [diff] [blame] | 2791 | return 0; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2792 | } |
| 2793 | |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 2794 | channel_dont_connect(req); |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 2795 | req->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */ |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 2796 | s->res.flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */ |
Willy Tarreau | 5e20552 | 2011-12-17 16:34:27 +0100 | [diff] [blame] | 2797 | #ifdef TCP_QUICKACK |
Willy Tarreau | 9ad7bd4 | 2015-04-03 19:19:59 +0200 | [diff] [blame] | 2798 | if (sess->listener->options & LI_O_NOQUICKACK && req->buf->i && |
| 2799 | objt_conn(sess->origin) && conn_ctrl_ready(__objt_conn(sess->origin))) { |
Willy Tarreau | 5e20552 | 2011-12-17 16:34:27 +0100 | [diff] [blame] | 2800 | /* We need more data, we have to re-enable quick-ack in case we |
| 2801 | * previously disabled it, otherwise we might cause the client |
| 2802 | * to delay next data. |
| 2803 | */ |
Willy Tarreau | 9ad7bd4 | 2015-04-03 19:19:59 +0200 | [diff] [blame] | 2804 | 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] | 2805 | } |
| 2806 | #endif |
Willy Tarreau | 1b194fe | 2009-03-21 21:10:04 +0100 | [diff] [blame] | 2807 | |
Willy Tarreau | fcffa69 | 2010-01-10 14:21:19 +0100 | [diff] [blame] | 2808 | if ((msg->msg_state != HTTP_MSG_RQBEFORE) && (txn->flags & TX_WAIT_NEXT_RQ)) { |
| 2809 | /* If the client starts to talk, let's fall back to |
| 2810 | * request timeout processing. |
| 2811 | */ |
| 2812 | txn->flags &= ~TX_WAIT_NEXT_RQ; |
Willy Tarreau | b16a574 | 2010-01-10 14:46:16 +0100 | [diff] [blame] | 2813 | req->analyse_exp = TICK_ETERNITY; |
Willy Tarreau | fcffa69 | 2010-01-10 14:21:19 +0100 | [diff] [blame] | 2814 | } |
| 2815 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2816 | /* just set the request timeout once at the beginning of the request */ |
Willy Tarreau | b16a574 | 2010-01-10 14:46:16 +0100 | [diff] [blame] | 2817 | if (!tick_isset(req->analyse_exp)) { |
| 2818 | if ((msg->msg_state == HTTP_MSG_RQBEFORE) && |
| 2819 | (txn->flags & TX_WAIT_NEXT_RQ) && |
| 2820 | tick_isset(s->be->timeout.httpka)) |
| 2821 | req->analyse_exp = tick_add(now_ms, s->be->timeout.httpka); |
| 2822 | else |
| 2823 | req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.httpreq); |
| 2824 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2825 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2826 | /* we're not ready yet */ |
| 2827 | return 0; |
Willy Tarreau | b608feb | 2010-01-02 22:47:18 +0100 | [diff] [blame] | 2828 | |
| 2829 | failed_keep_alive: |
| 2830 | /* Here we process low-level errors for keep-alive requests. In |
| 2831 | * short, if the request is not the first one and it experiences |
| 2832 | * a timeout, read error or shutdown, we just silently close so |
| 2833 | * that the client can try again. |
| 2834 | */ |
| 2835 | txn->status = 0; |
| 2836 | msg->msg_state = HTTP_MSG_RQBEFORE; |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 2837 | req->analysers &= AN_FLT_END; |
Willy Tarreau | b608feb | 2010-01-02 22:47:18 +0100 | [diff] [blame] | 2838 | s->logs.logwait = 0; |
Willy Tarreau | abcd514 | 2013-06-11 17:18:02 +0200 | [diff] [blame] | 2839 | s->logs.level = 0; |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 2840 | s->res.flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */ |
Christopher Faulet | a94e5a5 | 2015-12-09 15:55:06 +0100 | [diff] [blame] | 2841 | http_reply_and_close(s, txn->status, NULL); |
Willy Tarreau | b608feb | 2010-01-02 22:47:18 +0100 | [diff] [blame] | 2842 | return 0; |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2843 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2844 | |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 2845 | /* OK now we have a complete HTTP request with indexed headers. Let's |
| 2846 | * complete the request parsing by setting a few fields we will need |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2847 | * 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] | 2848 | * 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] | 2849 | * 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] | 2850 | * byte after the last LF. msg->sov points to the first byte of data. |
| 2851 | * msg->eol cannot be trusted because it may have been left uninitialized |
| 2852 | * (for instance in the absence of headers). |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 2853 | */ |
Willy Tarreau | 9cdde23 | 2007-05-02 20:58:19 +0200 | [diff] [blame] | 2854 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 2855 | stream_inc_http_req_ctr(s); |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 2856 | 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] | 2857 | |
Willy Tarreau | b16a574 | 2010-01-10 14:46:16 +0100 | [diff] [blame] | 2858 | if (txn->flags & TX_WAIT_NEXT_RQ) { |
| 2859 | /* kill the pending keep-alive timeout */ |
| 2860 | txn->flags &= ~TX_WAIT_NEXT_RQ; |
| 2861 | req->analyse_exp = TICK_ETERNITY; |
| 2862 | } |
| 2863 | |
| 2864 | |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 2865 | /* Maybe we found in invalid header name while we were configured not |
| 2866 | * to block on that, so we have to capture it now. |
| 2867 | */ |
| 2868 | if (unlikely(msg->err_pos >= 0)) |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 2869 | 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] | 2870 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2871 | /* |
| 2872 | * 1: identify the method |
| 2873 | */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2874 | 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] | 2875 | |
| 2876 | /* we can make use of server redirect on GET and HEAD */ |
| 2877 | if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD) |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 2878 | s->flags |= SF_REDIRECTABLE; |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 2879 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2880 | /* |
| 2881 | * 2: check if the URI matches the monitor_uri. |
| 2882 | * We have to do this for every request which gets in, because |
| 2883 | * the monitor-uri is defined by the frontend. |
| 2884 | */ |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 2885 | if (unlikely((sess->fe->monitor_uri_len != 0) && |
| 2886 | (sess->fe->monitor_uri_len == msg->sl.rq.u_l) && |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2887 | !memcmp(req->buf->p + msg->sl.rq.u, |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 2888 | sess->fe->monitor_uri, |
| 2889 | sess->fe->monitor_uri_len))) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2890 | /* |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2891 | * We have found the monitor URI |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2892 | */ |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2893 | struct acl_cond *cond; |
Willy Tarreau | b80c230 | 2007-11-30 20:51:32 +0100 | [diff] [blame] | 2894 | |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 2895 | s->flags |= SF_MONITOR; |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 2896 | sess->fe->fe_counters.intercepted_req++; |
Willy Tarreau | b80c230 | 2007-11-30 20:51:32 +0100 | [diff] [blame] | 2897 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2898 | /* Check if we want to fail this monitor request or not */ |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 2899 | list_for_each_entry(cond, &sess->fe->mon_fail_cond, list) { |
Willy Tarreau | 192252e | 2015-04-04 01:47:55 +0200 | [diff] [blame] | 2900 | 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] | 2901 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2902 | ret = acl_pass(ret); |
| 2903 | if (cond->pol == ACL_COND_UNLESS) |
| 2904 | ret = !ret; |
Willy Tarreau | b80c230 | 2007-11-30 20:51:32 +0100 | [diff] [blame] | 2905 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2906 | if (ret) { |
| 2907 | /* we fail this request, let's return 503 service unavail */ |
| 2908 | txn->status = 503; |
Christopher Faulet | a94e5a5 | 2015-12-09 15:55:06 +0100 | [diff] [blame] | 2909 | http_reply_and_close(s, txn->status, http_error_message(s, HTTP_ERR_503)); |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 2910 | if (!(s->flags & SF_ERR_MASK)) |
| 2911 | 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] | 2912 | goto return_prx_cond; |
Willy Tarreau | b80c230 | 2007-11-30 20:51:32 +0100 | [diff] [blame] | 2913 | } |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2914 | } |
Willy Tarreau | a5555ec | 2008-11-30 19:02:32 +0100 | [diff] [blame] | 2915 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2916 | /* nothing to fail, let's reply normaly */ |
| 2917 | txn->status = 200; |
Christopher Faulet | a94e5a5 | 2015-12-09 15:55:06 +0100 | [diff] [blame] | 2918 | http_reply_and_close(s, txn->status, http_error_message(s, HTTP_ERR_200)); |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 2919 | if (!(s->flags & SF_ERR_MASK)) |
| 2920 | 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] | 2921 | goto return_prx_cond; |
| 2922 | } |
| 2923 | |
| 2924 | /* |
| 2925 | * 3: Maybe we have to copy the original REQURI for the logs ? |
| 2926 | * Note: we cannot log anymore if the request has been |
| 2927 | * classified as invalid. |
| 2928 | */ |
| 2929 | if (unlikely(s->logs.logwait & LW_REQ)) { |
| 2930 | /* we have a complete HTTP request that we must log */ |
| 2931 | if ((txn->uri = pool_alloc2(pool2_requri)) != NULL) { |
| 2932 | int urilen = msg->sl.rq.l; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2933 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2934 | if (urilen >= REQURI_LEN) |
| 2935 | urilen = REQURI_LEN - 1; |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2936 | memcpy(txn->uri, req->buf->p, urilen); |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2937 | txn->uri[urilen] = 0; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2938 | |
Willy Tarreau | d79a3b2 | 2012-12-28 09:40:16 +0100 | [diff] [blame] | 2939 | if (!(s->logs.logwait &= ~(LW_REQ|LW_INIT))) |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2940 | s->do_log(s); |
| 2941 | } else { |
| 2942 | Alert("HTTP logging : out of memory.\n"); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2943 | } |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2944 | } |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 2945 | |
Willy Tarreau | 91852eb | 2015-05-01 13:26:00 +0200 | [diff] [blame] | 2946 | /* RFC7230#2.6 has enforced the format of the HTTP version string to be |
| 2947 | * exactly one digit "." one digit. This check may be disabled using |
| 2948 | * option accept-invalid-http-request. |
| 2949 | */ |
| 2950 | if (!(sess->fe->options2 & PR_O2_REQBUG_OK)) { |
| 2951 | if (msg->sl.rq.v_l != 8) { |
| 2952 | msg->err_pos = msg->sl.rq.v; |
| 2953 | goto return_bad_req; |
| 2954 | } |
| 2955 | |
| 2956 | if (req->buf->p[msg->sl.rq.v + 4] != '/' || |
| 2957 | !isdigit((unsigned char)req->buf->p[msg->sl.rq.v + 5]) || |
| 2958 | req->buf->p[msg->sl.rq.v + 6] != '.' || |
| 2959 | !isdigit((unsigned char)req->buf->p[msg->sl.rq.v + 7])) { |
| 2960 | msg->err_pos = msg->sl.rq.v + 4; |
| 2961 | goto return_bad_req; |
| 2962 | } |
| 2963 | } |
Willy Tarreau | 1331766 | 2015-05-01 13:47:08 +0200 | [diff] [blame] | 2964 | else { |
| 2965 | /* 4. We may have to convert HTTP/0.9 requests to HTTP/1.0 */ |
| 2966 | if (unlikely(msg->sl.rq.v_l == 0) && !http_upgrade_v09_to_v10(txn)) |
| 2967 | goto return_bad_req; |
| 2968 | } |
Willy Tarreau | 91852eb | 2015-05-01 13:26:00 +0200 | [diff] [blame] | 2969 | |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 2970 | /* ... and check if the request is HTTP/1.1 or above */ |
| 2971 | if ((msg->sl.rq.v_l == 8) && |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2972 | ((req->buf->p[msg->sl.rq.v + 5] > '1') || |
| 2973 | ((req->buf->p[msg->sl.rq.v + 5] == '1') && |
| 2974 | (req->buf->p[msg->sl.rq.v + 7] >= '1')))) |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 2975 | msg->flags |= HTTP_MSGF_VER_11; |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 2976 | |
| 2977 | /* "connection" has not been parsed yet */ |
Willy Tarreau | 50fc777 | 2012-11-11 22:19:57 +0100 | [diff] [blame] | 2978 | 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] | 2979 | |
Willy Tarreau | 88d349d | 2010-01-25 12:15:43 +0100 | [diff] [blame] | 2980 | /* if the frontend has "option http-use-proxy-header", we'll check if |
| 2981 | * we have what looks like a proxied connection instead of a connection, |
| 2982 | * and in this case set the TX_USE_PX_CONN flag to use Proxy-connection. |
| 2983 | * Note that this is *not* RFC-compliant, however browsers and proxies |
| 2984 | * happen to do that despite being non-standard :-( |
| 2985 | * We consider that a request not beginning with either '/' or '*' is |
| 2986 | * a proxied connection, which covers both "scheme://location" and |
| 2987 | * CONNECT ip:port. |
| 2988 | */ |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 2989 | if ((sess->fe->options2 & PR_O2_USE_PXHDR) && |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2990 | 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] | 2991 | txn->flags |= TX_USE_PX_CONN; |
| 2992 | |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 2993 | /* transfer length unknown*/ |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 2994 | msg->flags &= ~HTTP_MSGF_XFER_LEN; |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 2995 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2996 | /* 5: we may need to capture headers */ |
Willy Tarreau | cb7dd01 | 2015-04-03 22:16:32 +0200 | [diff] [blame] | 2997 | if (unlikely((s->logs.logwait & LW_REQHDR) && s->req_cap)) |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2998 | capture_headers(req->buf->p, &txn->hdr_idx, |
Willy Tarreau | cb7dd01 | 2015-04-03 22:16:32 +0200 | [diff] [blame] | 2999 | s->req_cap, sess->fe->req_cap); |
Willy Tarreau | 1138281 | 2008-07-09 16:18:21 +0200 | [diff] [blame] | 3000 | |
Willy Tarreau | 557f199 | 2015-05-01 10:05:17 +0200 | [diff] [blame] | 3001 | /* 6: determine the transfer-length according to RFC2616 #4.4, updated |
| 3002 | * by RFC7230#3.3.3 : |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 3003 | * |
Willy Tarreau | 557f199 | 2015-05-01 10:05:17 +0200 | [diff] [blame] | 3004 | * The length of a message body is determined by one of the following |
| 3005 | * (in order of precedence): |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 3006 | * |
Willy Tarreau | 557f199 | 2015-05-01 10:05:17 +0200 | [diff] [blame] | 3007 | * 1. Any response to a HEAD request and any response with a 1xx |
| 3008 | * (Informational), 204 (No Content), or 304 (Not Modified) status |
| 3009 | * code is always terminated by the first empty line after the |
| 3010 | * header fields, regardless of the header fields present in the |
| 3011 | * message, and thus cannot contain a message body. |
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 | * 2. Any 2xx (Successful) response to a CONNECT request implies that |
| 3014 | * the connection will become a tunnel immediately after the empty |
| 3015 | * line that concludes the header fields. A client MUST ignore any |
| 3016 | * Content-Length or Transfer-Encoding header fields received in |
| 3017 | * such a message. |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 3018 | * |
Willy Tarreau | 557f199 | 2015-05-01 10:05:17 +0200 | [diff] [blame] | 3019 | * 3. If a Transfer-Encoding header field is present and the chunked |
| 3020 | * transfer coding (Section 4.1) is the final encoding, the message |
| 3021 | * body length is determined by reading and decoding the chunked |
| 3022 | * data until the transfer coding indicates the data is complete. |
| 3023 | * |
| 3024 | * If a Transfer-Encoding header field is present in a response and |
| 3025 | * the chunked transfer coding is not the final encoding, the |
| 3026 | * message body length is determined by reading the connection until |
| 3027 | * it is closed by the server. If a Transfer-Encoding header field |
| 3028 | * is present in a request and the chunked transfer coding is not |
| 3029 | * the final encoding, the message body length cannot be determined |
| 3030 | * reliably; the server MUST respond with the 400 (Bad Request) |
| 3031 | * status code and then close the connection. |
| 3032 | * |
| 3033 | * If a message is received with both a Transfer-Encoding and a |
| 3034 | * Content-Length header field, the Transfer-Encoding overrides the |
| 3035 | * Content-Length. Such a message might indicate an attempt to |
| 3036 | * perform request smuggling (Section 9.5) or response splitting |
| 3037 | * (Section 9.4) and ought to be handled as an error. A sender MUST |
| 3038 | * remove the received Content-Length field prior to forwarding such |
| 3039 | * a message downstream. |
| 3040 | * |
| 3041 | * 4. If a message is received without Transfer-Encoding and with |
| 3042 | * either multiple Content-Length header fields having differing |
| 3043 | * field-values or a single Content-Length header field having an |
| 3044 | * invalid value, then the message framing is invalid and the |
| 3045 | * recipient MUST treat it as an unrecoverable error. If this is a |
| 3046 | * request message, the server MUST respond with a 400 (Bad Request) |
| 3047 | * status code and then close the connection. If this is a response |
| 3048 | * message received by a proxy, the proxy MUST close the connection |
| 3049 | * to the server, discard the received response, and send a 502 (Bad |
| 3050 | * Gateway) response to the client. If this is a response message |
| 3051 | * received by a user agent, the user agent MUST close the |
| 3052 | * connection to the server and discard the received response. |
| 3053 | * |
| 3054 | * 5. If a valid Content-Length header field is present without |
| 3055 | * Transfer-Encoding, its decimal value defines the expected message |
| 3056 | * body length in octets. If the sender closes the connection or |
| 3057 | * the recipient times out before the indicated number of octets are |
| 3058 | * received, the recipient MUST consider the message to be |
| 3059 | * incomplete and close the connection. |
| 3060 | * |
| 3061 | * 6. If this is a request message and none of the above are true, then |
| 3062 | * the message body length is zero (no message body is present). |
| 3063 | * |
| 3064 | * 7. Otherwise, this is a response message without a declared message |
| 3065 | * body length, so the message body length is determined by the |
| 3066 | * number of octets received prior to the server closing the |
| 3067 | * connection. |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 3068 | */ |
| 3069 | |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 3070 | ctx.idx = 0; |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 3071 | /* set TE_CHNK and XFER_LEN only if "chunked" is seen last */ |
Willy Tarreau | 4979d5c | 2015-05-01 10:06:30 +0200 | [diff] [blame] | 3072 | 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] | 3073 | if (ctx.vlen == 7 && strncasecmp(ctx.line + ctx.val, "chunked", 7) == 0) |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 3074 | msg->flags |= (HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN); |
| 3075 | else if (msg->flags & HTTP_MSGF_TE_CHNK) { |
Willy Tarreau | 34dfc60 | 2015-05-01 10:09:49 +0200 | [diff] [blame] | 3076 | /* chunked not last, return badreq */ |
| 3077 | goto return_bad_req; |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 3078 | } |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 3079 | } |
| 3080 | |
Willy Tarreau | 1c91391 | 2015-04-30 10:57:51 +0200 | [diff] [blame] | 3081 | /* Chunked requests must have their content-length removed */ |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 3082 | ctx.idx = 0; |
Willy Tarreau | 1c91391 | 2015-04-30 10:57:51 +0200 | [diff] [blame] | 3083 | if (msg->flags & HTTP_MSGF_TE_CHNK) { |
| 3084 | while (http_find_header2("Content-Length", 14, req->buf->p, &txn->hdr_idx, &ctx)) |
| 3085 | http_remove_header2(msg, &txn->hdr_idx, &ctx); |
| 3086 | } |
Willy Tarreau | 34dfc60 | 2015-05-01 10:09:49 +0200 | [diff] [blame] | 3087 | 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] | 3088 | signed long long cl; |
| 3089 | |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 3090 | if (!ctx.vlen) { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 3091 | msg->err_pos = ctx.line + ctx.val - req->buf->p; |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 3092 | goto return_bad_req; |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 3093 | } |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 3094 | |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 3095 | if (strl2llrc(ctx.line + ctx.val, ctx.vlen, &cl)) { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 3096 | msg->err_pos = ctx.line + ctx.val - req->buf->p; |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 3097 | goto return_bad_req; /* parse failure */ |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 3098 | } |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 3099 | |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 3100 | if (cl < 0) { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 3101 | msg->err_pos = ctx.line + ctx.val - req->buf->p; |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 3102 | goto return_bad_req; |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 3103 | } |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 3104 | |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 3105 | if ((msg->flags & HTTP_MSGF_CNT_LEN) && (msg->chunk_len != cl)) { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 3106 | msg->err_pos = ctx.line + ctx.val - req->buf->p; |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 3107 | goto return_bad_req; /* already specified, was different */ |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 3108 | } |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 3109 | |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 3110 | msg->flags |= HTTP_MSGF_CNT_LEN | HTTP_MSGF_XFER_LEN; |
Willy Tarreau | 124d991 | 2011-03-01 20:30:48 +0100 | [diff] [blame] | 3111 | msg->body_len = msg->chunk_len = cl; |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 3112 | } |
| 3113 | |
Willy Tarreau | 34dfc60 | 2015-05-01 10:09:49 +0200 | [diff] [blame] | 3114 | /* even bodyless requests have a known length */ |
| 3115 | msg->flags |= HTTP_MSGF_XFER_LEN; |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 3116 | |
Willy Tarreau | 179085c | 2014-04-28 16:48:56 +0200 | [diff] [blame] | 3117 | /* Until set to anything else, the connection mode is set as Keep-Alive. It will |
| 3118 | * only change if both the request and the config reference something else. |
| 3119 | * Option httpclose by itself sets tunnel mode where headers are mangled. |
| 3120 | * However, if another mode is set, it will affect it (eg: server-close/ |
| 3121 | * keep-alive + httpclose = close). Note that we avoid to redo the same work |
| 3122 | * if FE and BE have the same settings (common). The method consists in |
| 3123 | * checking if options changed between the two calls (implying that either |
| 3124 | * one is non-null, or one of them is non-null and we are there for the first |
| 3125 | * time. |
| 3126 | */ |
| 3127 | if (!(txn->flags & TX_HDR_CONN_PRS) || |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 3128 | ((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] | 3129 | http_adjust_conn_mode(s, txn, msg); |
Willy Tarreau | 179085c | 2014-04-28 16:48:56 +0200 | [diff] [blame] | 3130 | |
Willy Tarreau | 9fbe18e | 2015-05-01 22:42:08 +0200 | [diff] [blame] | 3131 | /* we may have to wait for the request's body */ |
| 3132 | if ((s->be->options & PR_O_WREQ_BODY) && |
| 3133 | (msg->body_len || (msg->flags & HTTP_MSGF_TE_CHNK))) |
| 3134 | req->analysers |= AN_REQ_HTTP_BODY; |
| 3135 | |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 3136 | /* end of job, return OK */ |
Willy Tarreau | 3a81629 | 2009-07-07 10:55:49 +0200 | [diff] [blame] | 3137 | req->analysers &= ~an_bit; |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 3138 | req->analyse_exp = TICK_ETERNITY; |
| 3139 | return 1; |
| 3140 | |
| 3141 | return_bad_req: |
| 3142 | /* We centralize bad requests processing here */ |
| 3143 | if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) { |
| 3144 | /* we detected a parsing error. We want to archive this request |
| 3145 | * in the dedicated proxy area for later troubleshooting. |
| 3146 | */ |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 3147 | 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] | 3148 | } |
| 3149 | |
| 3150 | txn->req.msg_state = HTTP_MSG_ERROR; |
| 3151 | txn->status = 400; |
Christopher Faulet | a94e5a5 | 2015-12-09 15:55:06 +0100 | [diff] [blame] | 3152 | http_reply_and_close(s, txn->status, http_error_message(s, HTTP_ERR_400)); |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 3153 | |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 3154 | sess->fe->fe_counters.failed_req++; |
Willy Tarreau | fb0afa7 | 2015-04-03 14:46:27 +0200 | [diff] [blame] | 3155 | if (sess->listener->counters) |
| 3156 | sess->listener->counters->failed_req++; |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 3157 | |
| 3158 | return_prx_cond: |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 3159 | if (!(s->flags & SF_ERR_MASK)) |
| 3160 | s->flags |= SF_ERR_PRXCOND; |
| 3161 | if (!(s->flags & SF_FINST_MASK)) |
| 3162 | s->flags |= SF_FINST_R; |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 3163 | |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 3164 | req->analysers &= AN_FLT_END; |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 3165 | req->analyse_exp = TICK_ETERNITY; |
| 3166 | return 0; |
| 3167 | } |
| 3168 | |
Willy Tarreau | 4f8a83c | 2012-06-04 00:26:23 +0200 | [diff] [blame] | 3169 | |
Willy Tarreau | 347a35d | 2013-11-22 17:51:09 +0100 | [diff] [blame] | 3170 | /* This function prepares an applet to handle the stats. It can deal with the |
| 3171 | * "100-continue" expectation, check that admin rules are met for POST requests, |
| 3172 | * and program a response message if something was unexpected. It cannot fail |
| 3173 | * 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] | 3174 | * 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] | 3175 | * 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] | 3176 | * is expected to have already assigned an appctx to the stream. |
Willy Tarreau | 1facd6d | 2012-12-22 22:03:39 +0100 | [diff] [blame] | 3177 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 3178 | int http_handle_stats(struct stream *s, struct channel *req) |
Willy Tarreau | 1facd6d | 2012-12-22 22:03:39 +0100 | [diff] [blame] | 3179 | { |
| 3180 | struct stats_admin_rule *stats_admin_rule; |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 3181 | struct stream_interface *si = &s->si[1]; |
Willy Tarreau | 192252e | 2015-04-04 01:47:55 +0200 | [diff] [blame] | 3182 | struct session *sess = s->sess; |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 3183 | struct http_txn *txn = s->txn; |
Willy Tarreau | 1facd6d | 2012-12-22 22:03:39 +0100 | [diff] [blame] | 3184 | struct http_msg *msg = &txn->req; |
Willy Tarreau | 414e9bb | 2013-11-23 00:30:38 +0100 | [diff] [blame] | 3185 | struct uri_auth *uri_auth = s->be->uri_auth; |
| 3186 | const char *uri, *h, *lookup; |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 3187 | struct appctx *appctx; |
Willy Tarreau | 414e9bb | 2013-11-23 00:30:38 +0100 | [diff] [blame] | 3188 | |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 3189 | appctx = si_appctx(si); |
| 3190 | memset(&appctx->ctx.stats, 0, sizeof(appctx->ctx.stats)); |
| 3191 | appctx->st1 = appctx->st2 = 0; |
| 3192 | appctx->ctx.stats.st_code = STAT_STATUS_INIT; |
| 3193 | appctx->ctx.stats.flags |= STAT_FMT_HTML; /* assume HTML mode by default */ |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 3194 | 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] | 3195 | appctx->ctx.stats.flags |= STAT_CHUNKED; |
Willy Tarreau | 414e9bb | 2013-11-23 00:30:38 +0100 | [diff] [blame] | 3196 | |
| 3197 | uri = msg->chn->buf->p + msg->sl.rq.u; |
| 3198 | lookup = uri + uri_auth->uri_len; |
| 3199 | |
| 3200 | for (h = lookup; h <= uri + msg->sl.rq.u_l - 3; h++) { |
| 3201 | if (memcmp(h, ";up", 3) == 0) { |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 3202 | appctx->ctx.stats.flags |= STAT_HIDE_DOWN; |
Willy Tarreau | 414e9bb | 2013-11-23 00:30:38 +0100 | [diff] [blame] | 3203 | break; |
| 3204 | } |
| 3205 | } |
| 3206 | |
| 3207 | if (uri_auth->refresh) { |
| 3208 | for (h = lookup; h <= uri + msg->sl.rq.u_l - 10; h++) { |
| 3209 | if (memcmp(h, ";norefresh", 10) == 0) { |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 3210 | appctx->ctx.stats.flags |= STAT_NO_REFRESH; |
Willy Tarreau | 414e9bb | 2013-11-23 00:30:38 +0100 | [diff] [blame] | 3211 | break; |
| 3212 | } |
| 3213 | } |
| 3214 | } |
| 3215 | |
| 3216 | for (h = lookup; h <= uri + msg->sl.rq.u_l - 4; h++) { |
| 3217 | if (memcmp(h, ";csv", 4) == 0) { |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 3218 | appctx->ctx.stats.flags &= ~STAT_FMT_HTML; |
Willy Tarreau | 414e9bb | 2013-11-23 00:30:38 +0100 | [diff] [blame] | 3219 | break; |
| 3220 | } |
| 3221 | } |
| 3222 | |
Willy Tarreau | 1e62df9 | 2016-01-11 18:57:53 +0100 | [diff] [blame] | 3223 | for (h = lookup; h <= uri + msg->sl.rq.u_l - 6; h++) { |
| 3224 | if (memcmp(h, ";typed", 6) == 0) { |
| 3225 | appctx->ctx.stats.flags &= ~STAT_FMT_HTML; |
| 3226 | appctx->ctx.stats.flags |= STAT_FMT_TYPED; |
| 3227 | break; |
| 3228 | } |
| 3229 | } |
| 3230 | |
Willy Tarreau | 414e9bb | 2013-11-23 00:30:38 +0100 | [diff] [blame] | 3231 | for (h = lookup; h <= uri + msg->sl.rq.u_l - 8; h++) { |
| 3232 | if (memcmp(h, ";st=", 4) == 0) { |
| 3233 | int i; |
| 3234 | h += 4; |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 3235 | appctx->ctx.stats.st_code = STAT_STATUS_UNKN; |
Willy Tarreau | 414e9bb | 2013-11-23 00:30:38 +0100 | [diff] [blame] | 3236 | for (i = STAT_STATUS_INIT + 1; i < STAT_STATUS_SIZE; i++) { |
| 3237 | if (strncmp(stat_status_codes[i], h, 4) == 0) { |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 3238 | appctx->ctx.stats.st_code = i; |
Willy Tarreau | 414e9bb | 2013-11-23 00:30:38 +0100 | [diff] [blame] | 3239 | break; |
| 3240 | } |
| 3241 | } |
| 3242 | break; |
| 3243 | } |
| 3244 | } |
| 3245 | |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 3246 | appctx->ctx.stats.scope_str = 0; |
| 3247 | appctx->ctx.stats.scope_len = 0; |
Willy Tarreau | 414e9bb | 2013-11-23 00:30:38 +0100 | [diff] [blame] | 3248 | for (h = lookup; h <= uri + msg->sl.rq.u_l - 8; h++) { |
| 3249 | if (memcmp(h, STAT_SCOPE_INPUT_NAME "=", strlen(STAT_SCOPE_INPUT_NAME) + 1) == 0) { |
| 3250 | int itx = 0; |
| 3251 | const char *h2; |
| 3252 | char scope_txt[STAT_SCOPE_TXT_MAXLEN + 1]; |
| 3253 | const char *err; |
| 3254 | |
| 3255 | h += strlen(STAT_SCOPE_INPUT_NAME) + 1; |
| 3256 | h2 = h; |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 3257 | appctx->ctx.stats.scope_str = h2 - msg->chn->buf->p; |
Willy Tarreau | 414e9bb | 2013-11-23 00:30:38 +0100 | [diff] [blame] | 3258 | while (*h != ';' && *h != '\0' && *h != '&' && *h != ' ' && *h != '\n') { |
| 3259 | itx++; |
| 3260 | h++; |
| 3261 | } |
| 3262 | |
| 3263 | if (itx > STAT_SCOPE_TXT_MAXLEN) |
| 3264 | itx = STAT_SCOPE_TXT_MAXLEN; |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 3265 | appctx->ctx.stats.scope_len = itx; |
Willy Tarreau | 414e9bb | 2013-11-23 00:30:38 +0100 | [diff] [blame] | 3266 | |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 3267 | /* 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] | 3268 | memcpy(scope_txt, h2, itx); |
| 3269 | scope_txt[itx] = '\0'; |
| 3270 | err = invalid_char(scope_txt); |
| 3271 | if (err) { |
| 3272 | /* bad char in search text => clear scope */ |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 3273 | appctx->ctx.stats.scope_str = 0; |
| 3274 | appctx->ctx.stats.scope_len = 0; |
Willy Tarreau | 414e9bb | 2013-11-23 00:30:38 +0100 | [diff] [blame] | 3275 | } |
| 3276 | break; |
| 3277 | } |
| 3278 | } |
Willy Tarreau | 1facd6d | 2012-12-22 22:03:39 +0100 | [diff] [blame] | 3279 | |
| 3280 | /* now check whether we have some admin rules for this request */ |
Willy Tarreau | 414e9bb | 2013-11-23 00:30:38 +0100 | [diff] [blame] | 3281 | list_for_each_entry(stats_admin_rule, &uri_auth->admin_rules, list) { |
Willy Tarreau | 1facd6d | 2012-12-22 22:03:39 +0100 | [diff] [blame] | 3282 | int ret = 1; |
| 3283 | |
| 3284 | if (stats_admin_rule->cond) { |
Willy Tarreau | 192252e | 2015-04-04 01:47:55 +0200 | [diff] [blame] | 3285 | 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] | 3286 | ret = acl_pass(ret); |
| 3287 | if (stats_admin_rule->cond->pol == ACL_COND_UNLESS) |
| 3288 | ret = !ret; |
| 3289 | } |
| 3290 | |
| 3291 | if (ret) { |
| 3292 | /* no rule, or the rule matches */ |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 3293 | appctx->ctx.stats.flags |= STAT_ADMIN; |
Willy Tarreau | 1facd6d | 2012-12-22 22:03:39 +0100 | [diff] [blame] | 3294 | break; |
| 3295 | } |
| 3296 | } |
| 3297 | |
| 3298 | /* Was the status page requested with a POST ? */ |
Willy Tarreau | b8cdf52 | 2015-05-29 01:09:15 +0200 | [diff] [blame] | 3299 | if (unlikely(txn->meth == HTTP_METH_POST && txn->req.body_len > 0)) { |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 3300 | if (appctx->ctx.stats.flags & STAT_ADMIN) { |
Willy Tarreau | cfe7fdd | 2014-04-26 22:08:32 +0200 | [diff] [blame] | 3301 | /* we'll need the request body, possibly after sending 100-continue */ |
Willy Tarreau | b8cdf52 | 2015-05-29 01:09:15 +0200 | [diff] [blame] | 3302 | if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) |
| 3303 | req->analysers |= AN_REQ_HTTP_BODY; |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 3304 | appctx->st0 = STAT_HTTP_POST; |
Willy Tarreau | 1facd6d | 2012-12-22 22:03:39 +0100 | [diff] [blame] | 3305 | } |
Willy Tarreau | 347a35d | 2013-11-22 17:51:09 +0100 | [diff] [blame] | 3306 | else { |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 3307 | appctx->ctx.stats.st_code = STAT_STATUS_DENY; |
| 3308 | appctx->st0 = STAT_HTTP_LAST; |
de Lafond Guillaume | 88c278f | 2013-04-15 19:27:10 +0200 | [diff] [blame] | 3309 | } |
Willy Tarreau | 1facd6d | 2012-12-22 22:03:39 +0100 | [diff] [blame] | 3310 | } |
Willy Tarreau | 347a35d | 2013-11-22 17:51:09 +0100 | [diff] [blame] | 3311 | else { |
| 3312 | /* So it was another method (GET/HEAD) */ |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 3313 | appctx->st0 = STAT_HTTP_HEAD; |
Willy Tarreau | 1facd6d | 2012-12-22 22:03:39 +0100 | [diff] [blame] | 3314 | } |
| 3315 | |
Willy Tarreau | 1facd6d | 2012-12-22 22:03:39 +0100 | [diff] [blame] | 3316 | s->task->nice = -32; /* small boost for HTTP statistics */ |
Willy Tarreau | 1facd6d | 2012-12-22 22:03:39 +0100 | [diff] [blame] | 3317 | return 1; |
| 3318 | } |
| 3319 | |
Lukas Tribus | 67db8df | 2013-06-23 17:37:13 +0200 | [diff] [blame] | 3320 | /* Sets the TOS header in IPv4 and the traffic class header in IPv6 packets |
| 3321 | * (as per RFC3260 #4 and BCP37 #4.2 and #5.2). |
| 3322 | */ |
Vincent Bernat | 6e61589 | 2016-05-18 16:17:44 +0200 | [diff] [blame] | 3323 | void inet_set_tos(int fd, const struct sockaddr_storage *from, int tos) |
Lukas Tribus | 67db8df | 2013-06-23 17:37:13 +0200 | [diff] [blame] | 3324 | { |
| 3325 | #ifdef IP_TOS |
Vincent Bernat | 6e61589 | 2016-05-18 16:17:44 +0200 | [diff] [blame] | 3326 | if (from->ss_family == AF_INET) |
Lukas Tribus | 67db8df | 2013-06-23 17:37:13 +0200 | [diff] [blame] | 3327 | setsockopt(fd, IPPROTO_IP, IP_TOS, &tos, sizeof(tos)); |
| 3328 | #endif |
| 3329 | #ifdef IPV6_TCLASS |
Vincent Bernat | 6e61589 | 2016-05-18 16:17:44 +0200 | [diff] [blame] | 3330 | if (from->ss_family == AF_INET6) { |
| 3331 | if (IN6_IS_ADDR_V4MAPPED(&((struct sockaddr_in6 *)from)->sin6_addr)) |
Lukas Tribus | 67db8df | 2013-06-23 17:37:13 +0200 | [diff] [blame] | 3332 | /* v4-mapped addresses need IP_TOS */ |
| 3333 | setsockopt(fd, IPPROTO_IP, IP_TOS, &tos, sizeof(tos)); |
| 3334 | else |
| 3335 | setsockopt(fd, IPPROTO_IPV6, IPV6_TCLASS, &tos, sizeof(tos)); |
| 3336 | } |
| 3337 | #endif |
| 3338 | } |
| 3339 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 3340 | int http_transform_header_str(struct stream* s, struct http_msg *msg, |
Thierry FOURNIER | 5531f87 | 2015-03-16 11:15:50 +0100 | [diff] [blame] | 3341 | const char* name, unsigned int name_len, |
| 3342 | const char *str, struct my_regex *re, |
| 3343 | int action) |
Sasha Pachev | 218f064 | 2014-06-16 12:05:59 -0600 | [diff] [blame] | 3344 | { |
Thierry FOURNIER | 5a33ac7 | 2015-03-16 23:54:35 +0100 | [diff] [blame] | 3345 | struct hdr_ctx ctx; |
| 3346 | char *buf = msg->chn->buf->p; |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 3347 | struct hdr_idx *idx = &s->txn->hdr_idx; |
Thierry FOURNIER | 5531f87 | 2015-03-16 11:15:50 +0100 | [diff] [blame] | 3348 | int (*http_find_hdr_func)(const char *name, int len, char *sol, |
| 3349 | struct hdr_idx *idx, struct hdr_ctx *ctx); |
Thierry FOURNIER | 191f9ef | 2015-03-16 23:23:53 +0100 | [diff] [blame] | 3350 | struct chunk *output = get_trash_chunk(); |
| 3351 | |
Thierry FOURNIER | 5a33ac7 | 2015-03-16 23:54:35 +0100 | [diff] [blame] | 3352 | ctx.idx = 0; |
Sasha Pachev | 218f064 | 2014-06-16 12:05:59 -0600 | [diff] [blame] | 3353 | |
Thierry FOURNIER | 191f9ef | 2015-03-16 23:23:53 +0100 | [diff] [blame] | 3354 | /* Choose the header browsing function. */ |
| 3355 | switch (action) { |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 3356 | case ACT_HTTP_REPLACE_VAL: |
Thierry FOURNIER | 191f9ef | 2015-03-16 23:23:53 +0100 | [diff] [blame] | 3357 | http_find_hdr_func = http_find_header2; |
| 3358 | break; |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 3359 | case ACT_HTTP_REPLACE_HDR: |
Thierry FOURNIER | 191f9ef | 2015-03-16 23:23:53 +0100 | [diff] [blame] | 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 |
Willy Tarreau | 58727ec | 2016-05-25 16:23:59 +0200 | [diff] [blame] | 3410 | * on txn->flags if it encounters a tarpit rule. If <deny_status> is not NULL |
| 3411 | * and a deny/tarpit rule is matched, it will be filled with this rule's deny |
| 3412 | * status. |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 3413 | */ |
Willy Tarreau | 0b74833 | 2014-04-29 00:13:29 +0200 | [diff] [blame] | 3414 | enum rule_result |
Willy Tarreau | 58727ec | 2016-05-25 16:23:59 +0200 | [diff] [blame] | 3415 | http_req_get_intercept_rule(struct proxy *px, struct list *rules, struct stream *s, int *deny_status) |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 3416 | { |
Willy Tarreau | 9ad7bd4 | 2015-04-03 19:19:59 +0200 | [diff] [blame] | 3417 | struct session *sess = strm_sess(s); |
Willy Tarreau | 987e3fb | 2015-04-04 01:09:08 +0200 | [diff] [blame] | 3418 | struct http_txn *txn = s->txn; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 3419 | struct connection *cli_conn; |
Thierry FOURNIER | a28a942 | 2015-08-04 19:35:46 +0200 | [diff] [blame] | 3420 | struct act_rule *rule; |
Willy Tarreau | 20b0de5 | 2012-12-24 15:45:22 +0100 | [diff] [blame] | 3421 | struct hdr_ctx ctx; |
Willy Tarreau | ae3c010 | 2014-04-28 23:22:08 +0200 | [diff] [blame] | 3422 | const char *auth_realm; |
Willy Tarreau | acc9800 | 2015-09-27 23:34:39 +0200 | [diff] [blame] | 3423 | int act_flags = 0; |
Thierry Fournier | 4b788f7 | 2016-06-01 13:35:36 +0200 | [diff] [blame] | 3424 | int len; |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 3425 | |
Thierry FOURNIER | bc4c1ac | 2015-02-25 13:36:14 +0100 | [diff] [blame] | 3426 | /* If "the current_rule_list" match the executed rule list, we are in |
| 3427 | * resume condition. If a resume is needed it is always in the action |
| 3428 | * and never in the ACL or converters. In this case, we initialise the |
| 3429 | * current rule, and go to the action execution point. |
| 3430 | */ |
Willy Tarreau | 152b81e | 2015-04-20 13:26:17 +0200 | [diff] [blame] | 3431 | if (s->current_rule) { |
| 3432 | rule = s->current_rule; |
| 3433 | s->current_rule = NULL; |
| 3434 | if (s->current_rule_list == rules) |
| 3435 | goto resume_execution; |
Thierry FOURNIER | bc4c1ac | 2015-02-25 13:36:14 +0100 | [diff] [blame] | 3436 | } |
| 3437 | s->current_rule_list = rules; |
Willy Tarreau | 152b81e | 2015-04-20 13:26:17 +0200 | [diff] [blame] | 3438 | |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 3439 | list_for_each_entry(rule, rules, list) { |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 3440 | |
Willy Tarreau | 96257ec | 2012-12-27 10:46:37 +0100 | [diff] [blame] | 3441 | /* check optional condition */ |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 3442 | if (rule->cond) { |
Willy Tarreau | 96257ec | 2012-12-27 10:46:37 +0100 | [diff] [blame] | 3443 | int ret; |
| 3444 | |
Willy Tarreau | 192252e | 2015-04-04 01:47:55 +0200 | [diff] [blame] | 3445 | 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] | 3446 | ret = acl_pass(ret); |
| 3447 | |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 3448 | if (rule->cond->pol == ACL_COND_UNLESS) |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 3449 | ret = !ret; |
Willy Tarreau | 96257ec | 2012-12-27 10:46:37 +0100 | [diff] [blame] | 3450 | |
| 3451 | if (!ret) /* condition not matched */ |
| 3452 | continue; |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 3453 | } |
| 3454 | |
Willy Tarreau | acc9800 | 2015-09-27 23:34:39 +0200 | [diff] [blame] | 3455 | act_flags |= ACT_FLAG_FIRST; |
Thierry FOURNIER | bc4c1ac | 2015-02-25 13:36:14 +0100 | [diff] [blame] | 3456 | resume_execution: |
Willy Tarreau | 96257ec | 2012-12-27 10:46:37 +0100 | [diff] [blame] | 3457 | switch (rule->action) { |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 3458 | case ACT_ACTION_ALLOW: |
Willy Tarreau | 0b74833 | 2014-04-29 00:13:29 +0200 | [diff] [blame] | 3459 | return HTTP_RULE_RES_STOP; |
Willy Tarreau | 96257ec | 2012-12-27 10:46:37 +0100 | [diff] [blame] | 3460 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 3461 | case ACT_ACTION_DENY: |
Willy Tarreau | 58727ec | 2016-05-25 16:23:59 +0200 | [diff] [blame] | 3462 | if (deny_status) |
| 3463 | *deny_status = rule->deny_status; |
Willy Tarreau | 0b74833 | 2014-04-29 00:13:29 +0200 | [diff] [blame] | 3464 | return HTTP_RULE_RES_DENY; |
Willy Tarreau | 96257ec | 2012-12-27 10:46:37 +0100 | [diff] [blame] | 3465 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 3466 | case ACT_HTTP_REQ_TARPIT: |
Willy Tarreau | ccbcc37 | 2012-12-27 12:37:57 +0100 | [diff] [blame] | 3467 | txn->flags |= TX_CLTARPIT; |
Willy Tarreau | 58727ec | 2016-05-25 16:23:59 +0200 | [diff] [blame] | 3468 | if (deny_status) |
| 3469 | *deny_status = rule->deny_status; |
Willy Tarreau | 0b74833 | 2014-04-29 00:13:29 +0200 | [diff] [blame] | 3470 | return HTTP_RULE_RES_DENY; |
Willy Tarreau | ccbcc37 | 2012-12-27 12:37:57 +0100 | [diff] [blame] | 3471 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 3472 | case ACT_HTTP_REQ_AUTH: |
Willy Tarreau | ae3c010 | 2014-04-28 23:22:08 +0200 | [diff] [blame] | 3473 | /* Auth might be performed on regular http-req rules as well as on stats */ |
| 3474 | auth_realm = rule->arg.auth.realm; |
| 3475 | if (!auth_realm) { |
| 3476 | if (px->uri_auth && rules == &px->uri_auth->http_req_rules) |
| 3477 | auth_realm = STATS_DEFAULT_REALM; |
| 3478 | else |
| 3479 | auth_realm = px->id; |
| 3480 | } |
| 3481 | /* send 401/407 depending on whether we use a proxy or not. We still |
| 3482 | * count one error, because normal browsing won't significantly |
| 3483 | * increase the counter but brute force attempts will. |
| 3484 | */ |
| 3485 | chunk_printf(&trash, (txn->flags & TX_USE_PX_CONN) ? HTTP_407_fmt : HTTP_401_fmt, auth_realm); |
| 3486 | txn->status = (txn->flags & TX_USE_PX_CONN) ? 407 : 401; |
Christopher Faulet | a94e5a5 | 2015-12-09 15:55:06 +0100 | [diff] [blame] | 3487 | http_reply_and_close(s, txn->status, &trash); |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 3488 | stream_inc_http_err_ctr(s); |
Willy Tarreau | 0b74833 | 2014-04-29 00:13:29 +0200 | [diff] [blame] | 3489 | return HTTP_RULE_RES_ABRT; |
Willy Tarreau | 96257ec | 2012-12-27 10:46:37 +0100 | [diff] [blame] | 3490 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 3491 | case ACT_HTTP_REDIR: |
Willy Tarreau | 0b74833 | 2014-04-29 00:13:29 +0200 | [diff] [blame] | 3492 | if (!http_apply_redirect_rule(rule->arg.redir, s, txn)) |
| 3493 | return HTTP_RULE_RES_BADREQ; |
| 3494 | return HTTP_RULE_RES_DONE; |
Willy Tarreau | 81499eb | 2012-12-27 12:19:02 +0100 | [diff] [blame] | 3495 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 3496 | case ACT_HTTP_SET_NICE: |
Willy Tarreau | f4c43c1 | 2013-06-11 17:01:13 +0200 | [diff] [blame] | 3497 | s->task->nice = rule->arg.nice; |
| 3498 | break; |
| 3499 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 3500 | case ACT_HTTP_SET_TOS: |
Willy Tarreau | 9ad7bd4 | 2015-04-03 19:19:59 +0200 | [diff] [blame] | 3501 | if ((cli_conn = objt_conn(sess->origin)) && conn_ctrl_ready(cli_conn)) |
Vincent Bernat | 6e61589 | 2016-05-18 16:17:44 +0200 | [diff] [blame] | 3502 | 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] | 3503 | break; |
| 3504 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 3505 | case ACT_HTTP_SET_MARK: |
Willy Tarreau | 51347ed | 2013-06-11 19:34:13 +0200 | [diff] [blame] | 3506 | #ifdef SO_MARK |
Willy Tarreau | 9ad7bd4 | 2015-04-03 19:19:59 +0200 | [diff] [blame] | 3507 | if ((cli_conn = objt_conn(sess->origin)) && conn_ctrl_ready(cli_conn)) |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 3508 | 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] | 3509 | #endif |
| 3510 | break; |
| 3511 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 3512 | case ACT_HTTP_SET_LOGL: |
Willy Tarreau | 9a355ec | 2013-06-11 17:45:46 +0200 | [diff] [blame] | 3513 | s->logs.level = rule->arg.loglevel; |
| 3514 | break; |
| 3515 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 3516 | case ACT_HTTP_REPLACE_HDR: |
| 3517 | case ACT_HTTP_REPLACE_VAL: |
Thierry FOURNIER | 5a33ac7 | 2015-03-16 23:54:35 +0100 | [diff] [blame] | 3518 | if (http_transform_header(s, &txn->req, rule->arg.hdr_add.name, |
| 3519 | rule->arg.hdr_add.name_len, |
| 3520 | &rule->arg.hdr_add.fmt, |
| 3521 | &rule->arg.hdr_add.re, rule->action)) |
Sasha Pachev | 218f064 | 2014-06-16 12:05:59 -0600 | [diff] [blame] | 3522 | return HTTP_RULE_RES_BADREQ; |
| 3523 | break; |
| 3524 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 3525 | case ACT_HTTP_DEL_HDR: |
Willy Tarreau | 96257ec | 2012-12-27 10:46:37 +0100 | [diff] [blame] | 3526 | ctx.idx = 0; |
| 3527 | /* remove all occurrences of the header */ |
| 3528 | while (http_find_header2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len, |
| 3529 | txn->req.chn->buf->p, &txn->hdr_idx, &ctx)) { |
| 3530 | http_remove_header2(&txn->req, &txn->hdr_idx, &ctx); |
Willy Tarreau | 20b0de5 | 2012-12-24 15:45:22 +0100 | [diff] [blame] | 3531 | } |
Willy Tarreau | 8560328 | 2015-01-21 20:39:27 +0100 | [diff] [blame] | 3532 | break; |
Willy Tarreau | 96257ec | 2012-12-27 10:46:37 +0100 | [diff] [blame] | 3533 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 3534 | case ACT_HTTP_SET_HDR: |
| 3535 | case ACT_HTTP_ADD_HDR: |
Thierry Fournier | 4b788f7 | 2016-06-01 13:35:36 +0200 | [diff] [blame] | 3536 | /* The scope of the trash buffer must be limited to this function. The |
| 3537 | * build_logline() function can execute a lot of other function which |
| 3538 | * can use the trash buffer. So for limiting the scope of this global |
| 3539 | * buffer, we build first the header value using build_logline, and |
| 3540 | * after we store the header name. |
| 3541 | */ |
| 3542 | len = rule->arg.hdr_add.name_len + 2, |
| 3543 | len += build_logline(s, trash.str + len, trash.size - len, &rule->arg.hdr_add.fmt); |
Willy Tarreau | 96257ec | 2012-12-27 10:46:37 +0100 | [diff] [blame] | 3544 | memcpy(trash.str, rule->arg.hdr_add.name, rule->arg.hdr_add.name_len); |
Thierry Fournier | 4b788f7 | 2016-06-01 13:35:36 +0200 | [diff] [blame] | 3545 | trash.str[rule->arg.hdr_add.name_len] = ':'; |
| 3546 | trash.str[rule->arg.hdr_add.name_len + 1] = ' '; |
| 3547 | trash.len = len; |
Willy Tarreau | 8560328 | 2015-01-21 20:39:27 +0100 | [diff] [blame] | 3548 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 3549 | if (rule->action == ACT_HTTP_SET_HDR) { |
Willy Tarreau | 8560328 | 2015-01-21 20:39:27 +0100 | [diff] [blame] | 3550 | /* remove all occurrences of the header */ |
| 3551 | ctx.idx = 0; |
| 3552 | while (http_find_header2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len, |
| 3553 | txn->req.chn->buf->p, &txn->hdr_idx, &ctx)) { |
| 3554 | http_remove_header2(&txn->req, &txn->hdr_idx, &ctx); |
| 3555 | } |
| 3556 | } |
| 3557 | |
Willy Tarreau | 96257ec | 2012-12-27 10:46:37 +0100 | [diff] [blame] | 3558 | http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, trash.len); |
| 3559 | break; |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 3560 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 3561 | case ACT_HTTP_DEL_ACL: |
| 3562 | case ACT_HTTP_DEL_MAP: { |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 3563 | struct pat_ref *ref; |
| 3564 | char *key; |
| 3565 | int len; |
| 3566 | |
| 3567 | /* collect reference */ |
| 3568 | ref = pat_ref_lookup(rule->arg.map.ref); |
| 3569 | if (!ref) |
| 3570 | continue; |
| 3571 | |
| 3572 | /* collect key */ |
| 3573 | len = build_logline(s, trash.str, trash.size, &rule->arg.map.key); |
| 3574 | key = trash.str; |
| 3575 | key[len] = '\0'; |
| 3576 | |
| 3577 | /* perform update */ |
| 3578 | /* returned code: 1=ok, 0=ko */ |
| 3579 | pat_ref_delete(ref, key); |
| 3580 | |
| 3581 | break; |
| 3582 | } |
| 3583 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 3584 | case ACT_HTTP_ADD_ACL: { |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 3585 | struct pat_ref *ref; |
| 3586 | char *key; |
| 3587 | struct chunk *trash_key; |
| 3588 | int len; |
| 3589 | |
| 3590 | trash_key = get_trash_chunk(); |
| 3591 | |
| 3592 | /* collect reference */ |
| 3593 | ref = pat_ref_lookup(rule->arg.map.ref); |
| 3594 | if (!ref) |
| 3595 | continue; |
| 3596 | |
| 3597 | /* collect key */ |
| 3598 | len = build_logline(s, trash_key->str, trash_key->size, &rule->arg.map.key); |
| 3599 | key = trash_key->str; |
| 3600 | key[len] = '\0'; |
| 3601 | |
| 3602 | /* perform update */ |
| 3603 | /* add entry only if it does not already exist */ |
| 3604 | if (pat_ref_find_elt(ref, key) == NULL) |
Thierry FOURNIER | e47e4e2 | 2014-04-28 11:18:57 +0200 | [diff] [blame] | 3605 | pat_ref_add(ref, key, NULL, NULL); |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 3606 | |
| 3607 | break; |
| 3608 | } |
| 3609 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 3610 | case ACT_HTTP_SET_MAP: { |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 3611 | struct pat_ref *ref; |
| 3612 | char *key, *value; |
| 3613 | struct chunk *trash_key, *trash_value; |
| 3614 | int len; |
| 3615 | |
| 3616 | trash_key = get_trash_chunk(); |
| 3617 | trash_value = get_trash_chunk(); |
| 3618 | |
| 3619 | /* collect reference */ |
| 3620 | ref = pat_ref_lookup(rule->arg.map.ref); |
| 3621 | if (!ref) |
| 3622 | continue; |
| 3623 | |
| 3624 | /* collect key */ |
| 3625 | len = build_logline(s, trash_key->str, trash_key->size, &rule->arg.map.key); |
| 3626 | key = trash_key->str; |
| 3627 | key[len] = '\0'; |
| 3628 | |
| 3629 | /* collect value */ |
| 3630 | len = build_logline(s, trash_value->str, trash_value->size, &rule->arg.map.value); |
| 3631 | value = trash_value->str; |
| 3632 | value[len] = '\0'; |
| 3633 | |
| 3634 | /* perform update */ |
| 3635 | if (pat_ref_find_elt(ref, key) != NULL) |
| 3636 | /* update entry if it exists */ |
| 3637 | pat_ref_set(ref, key, value, NULL); |
| 3638 | else |
| 3639 | /* insert a new entry */ |
Thierry FOURNIER | e47e4e2 | 2014-04-28 11:18:57 +0200 | [diff] [blame] | 3640 | pat_ref_add(ref, key, value, NULL); |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 3641 | |
| 3642 | break; |
| 3643 | } |
William Lallemand | 73025dd | 2014-04-24 14:38:37 +0200 | [diff] [blame] | 3644 | |
Thierry FOURNIER | 4214873 | 2015-09-02 17:17:33 +0200 | [diff] [blame] | 3645 | case ACT_CUSTOM: |
Willy Tarreau | acc9800 | 2015-09-27 23:34:39 +0200 | [diff] [blame] | 3646 | if ((px->options & PR_O_ABRT_CLOSE) && (s->req.flags & (CF_SHUTR|CF_READ_NULL|CF_READ_ERROR))) |
| 3647 | act_flags |= ACT_FLAG_FINAL; |
Willy Tarreau | 3945868 | 2015-09-27 10:33:15 +0200 | [diff] [blame] | 3648 | |
Willy Tarreau | acc9800 | 2015-09-27 23:34:39 +0200 | [diff] [blame] | 3649 | switch (rule->action_ptr(rule, px, s->sess, s, act_flags)) { |
Thierry FOURNIER | 24ff6c6 | 2015-08-06 08:52:53 +0200 | [diff] [blame] | 3650 | case ACT_RET_ERR: |
| 3651 | case ACT_RET_CONT: |
| 3652 | break; |
Thierry FOURNIER | 4214873 | 2015-09-02 17:17:33 +0200 | [diff] [blame] | 3653 | case ACT_RET_STOP: |
| 3654 | return HTTP_RULE_RES_DONE; |
Thierry FOURNIER | 24ff6c6 | 2015-08-06 08:52:53 +0200 | [diff] [blame] | 3655 | case ACT_RET_YIELD: |
Willy Tarreau | 152b81e | 2015-04-20 13:26:17 +0200 | [diff] [blame] | 3656 | s->current_rule = rule; |
Thierry FOURNIER | bc4c1ac | 2015-02-25 13:36:14 +0100 | [diff] [blame] | 3657 | return HTTP_RULE_RES_YIELD; |
| 3658 | } |
William Lallemand | 73025dd | 2014-04-24 14:38:37 +0200 | [diff] [blame] | 3659 | break; |
| 3660 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 3661 | case ACT_ACTION_TRK_SC0 ... ACT_ACTION_TRK_SCMAX: |
Willy Tarreau | 09448f7 | 2014-06-25 18:12:15 +0200 | [diff] [blame] | 3662 | /* Note: only the first valid tracking parameter of each |
| 3663 | * applies. |
| 3664 | */ |
| 3665 | |
Ruoshan Huang | e4edc6b | 2016-07-14 15:07:45 +0800 | [diff] [blame] | 3666 | if (stkctr_entry(&s->stkctr[http_trk_idx(rule->action)]) == NULL) { |
Willy Tarreau | 09448f7 | 2014-06-25 18:12:15 +0200 | [diff] [blame] | 3667 | struct stktable *t; |
| 3668 | struct stksess *ts; |
| 3669 | struct stktable_key *key; |
| 3670 | void *ptr; |
| 3671 | |
Thierry FOURNIER | 5ec63e0 | 2015-08-04 09:09:48 +0200 | [diff] [blame] | 3672 | t = rule->arg.trk_ctr.table.t; |
| 3673 | key = stktable_fetch_key(t, s->be, sess, s, SMP_OPT_DIR_REQ | SMP_OPT_FINAL, rule->arg.trk_ctr.expr, NULL); |
Willy Tarreau | 09448f7 | 2014-06-25 18:12:15 +0200 | [diff] [blame] | 3674 | |
| 3675 | if (key && (ts = stktable_get_entry(t, key))) { |
Ruoshan Huang | e4edc6b | 2016-07-14 15:07:45 +0800 | [diff] [blame] | 3676 | stream_track_stkctr(&s->stkctr[http_trk_idx(rule->action)], t, ts); |
Willy Tarreau | 09448f7 | 2014-06-25 18:12:15 +0200 | [diff] [blame] | 3677 | |
| 3678 | /* let's count a new HTTP request as it's the first time we do it */ |
| 3679 | ptr = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_REQ_CNT); |
| 3680 | if (ptr) |
| 3681 | stktable_data_cast(ptr, http_req_cnt)++; |
| 3682 | |
| 3683 | ptr = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_REQ_RATE); |
| 3684 | if (ptr) |
| 3685 | update_freq_ctr_period(&stktable_data_cast(ptr, http_req_rate), |
| 3686 | t->data_arg[STKTABLE_DT_HTTP_REQ_RATE].u, 1); |
| 3687 | |
Ruoshan Huang | e4edc6b | 2016-07-14 15:07:45 +0800 | [diff] [blame] | 3688 | stkctr_set_flags(&s->stkctr[http_trk_idx(rule->action)], STKCTR_TRACK_CONTENT); |
Willy Tarreau | 9ad7bd4 | 2015-04-03 19:19:59 +0200 | [diff] [blame] | 3689 | if (sess->fe != s->be) |
Ruoshan Huang | e4edc6b | 2016-07-14 15:07:45 +0800 | [diff] [blame] | 3690 | stkctr_set_flags(&s->stkctr[http_trk_idx(rule->action)], STKCTR_TRACK_BACKEND); |
Willy Tarreau | 09448f7 | 2014-06-25 18:12:15 +0200 | [diff] [blame] | 3691 | } |
| 3692 | } |
Adis Nezirovic | 2fbcafc | 2015-07-06 15:44:30 +0200 | [diff] [blame] | 3693 | break; |
| 3694 | |
Thierry FOURNIER | 22e4901 | 2015-08-05 19:13:48 +0200 | [diff] [blame] | 3695 | /* other flags exists, but normaly, they never be matched. */ |
| 3696 | default: |
| 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; |
Thierry FOURNIER | a28a942 | 2015-08-04 19:35:46 +0200 | [diff] [blame] | 3722 | struct act_rule *rule; |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 3723 | struct hdr_ctx ctx; |
Willy Tarreau | acc9800 | 2015-09-27 23:34:39 +0200 | [diff] [blame] | 3724 | int act_flags = 0; |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 3725 | |
Thierry FOURNIER | bc4c1ac | 2015-02-25 13:36:14 +0100 | [diff] [blame] | 3726 | /* If "the current_rule_list" match the executed rule list, we are in |
| 3727 | * resume condition. If a resume is needed it is always in the action |
| 3728 | * and never in the ACL or converters. In this case, we initialise the |
| 3729 | * current rule, and go to the action execution point. |
| 3730 | */ |
Willy Tarreau | 152b81e | 2015-04-20 13:26:17 +0200 | [diff] [blame] | 3731 | if (s->current_rule) { |
| 3732 | rule = s->current_rule; |
| 3733 | s->current_rule = NULL; |
| 3734 | if (s->current_rule_list == rules) |
| 3735 | goto resume_execution; |
Thierry FOURNIER | bc4c1ac | 2015-02-25 13:36:14 +0100 | [diff] [blame] | 3736 | } |
| 3737 | s->current_rule_list = rules; |
Willy Tarreau | 152b81e | 2015-04-20 13:26:17 +0200 | [diff] [blame] | 3738 | |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 3739 | list_for_each_entry(rule, rules, list) { |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 3740 | |
| 3741 | /* check optional condition */ |
| 3742 | if (rule->cond) { |
| 3743 | int ret; |
| 3744 | |
Willy Tarreau | 192252e | 2015-04-04 01:47:55 +0200 | [diff] [blame] | 3745 | 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] | 3746 | ret = acl_pass(ret); |
| 3747 | |
| 3748 | if (rule->cond->pol == ACL_COND_UNLESS) |
| 3749 | ret = !ret; |
| 3750 | |
| 3751 | if (!ret) /* condition not matched */ |
| 3752 | continue; |
| 3753 | } |
| 3754 | |
Willy Tarreau | acc9800 | 2015-09-27 23:34:39 +0200 | [diff] [blame] | 3755 | act_flags |= ACT_FLAG_FIRST; |
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) { |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 3758 | case ACT_ACTION_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 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 3761 | case ACT_ACTION_DENY: |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 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 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 3765 | case ACT_HTTP_SET_NICE: |
Willy Tarreau | f4c43c1 | 2013-06-11 17:01:13 +0200 | [diff] [blame] | 3766 | s->task->nice = rule->arg.nice; |
| 3767 | break; |
| 3768 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 3769 | case ACT_HTTP_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)) |
Vincent Bernat | 6e61589 | 2016-05-18 16:17:44 +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 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 3774 | case ACT_HTTP_SET_MARK: |
Willy Tarreau | 51347ed | 2013-06-11 19:34:13 +0200 | [diff] [blame] | 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 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 3781 | case ACT_HTTP_SET_LOGL: |
Willy Tarreau | 9a355ec | 2013-06-11 17:45:46 +0200 | [diff] [blame] | 3782 | s->logs.level = rule->arg.loglevel; |
| 3783 | break; |
| 3784 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 3785 | case ACT_HTTP_REPLACE_HDR: |
| 3786 | case ACT_HTTP_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 | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 3794 | case ACT_HTTP_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 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 3803 | case ACT_HTTP_SET_HDR: |
| 3804 | case ACT_HTTP_ADD_HDR: |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 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 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 3812 | if (rule->action == ACT_HTTP_SET_HDR) { |
Willy Tarreau | 8560328 | 2015-01-21 20:39:27 +0100 | [diff] [blame] | 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 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 3823 | case ACT_HTTP_DEL_ACL: |
| 3824 | case ACT_HTTP_DEL_MAP: { |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 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 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 3846 | case ACT_HTTP_ADD_ACL: { |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 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 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 3872 | case ACT_HTTP_SET_MAP: { |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 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 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 3907 | case ACT_HTTP_REDIR: |
Willy Tarreau | 51d861a | 2015-05-22 17:30:48 +0200 | [diff] [blame] | 3908 | if (!http_apply_redirect_rule(rule->arg.redir, s, txn)) |
| 3909 | return HTTP_RULE_RES_BADREQ; |
| 3910 | return HTTP_RULE_RES_DONE; |
| 3911 | |
Ruoshan Huang | e4edc6b | 2016-07-14 15:07:45 +0800 | [diff] [blame] | 3912 | case ACT_ACTION_TRK_SC0 ... ACT_ACTION_TRK_SCMAX: |
| 3913 | /* Note: only the first valid tracking parameter of each |
| 3914 | * applies. |
| 3915 | */ |
| 3916 | |
| 3917 | if (stkctr_entry(&s->stkctr[http_trk_idx(rule->action)]) == NULL) { |
| 3918 | struct stktable *t; |
| 3919 | struct stksess *ts; |
| 3920 | struct stktable_key *key; |
| 3921 | void *ptr; |
| 3922 | |
| 3923 | t = rule->arg.trk_ctr.table.t; |
| 3924 | key = stktable_fetch_key(t, s->be, sess, s, SMP_OPT_DIR_RES | SMP_OPT_FINAL, rule->arg.trk_ctr.expr, NULL); |
| 3925 | |
| 3926 | if (key && (ts = stktable_get_entry(t, key))) { |
| 3927 | stream_track_stkctr(&s->stkctr[http_trk_idx(rule->action)], t, ts); |
| 3928 | |
| 3929 | /* let's count a new HTTP request as it's the first time we do it */ |
| 3930 | ptr = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_REQ_CNT); |
| 3931 | if (ptr) |
| 3932 | stktable_data_cast(ptr, http_req_cnt)++; |
| 3933 | |
| 3934 | ptr = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_REQ_RATE); |
| 3935 | if (ptr) |
| 3936 | update_freq_ctr_period(&stktable_data_cast(ptr, http_req_rate), |
| 3937 | t->data_arg[STKTABLE_DT_HTTP_REQ_RATE].u, 1); |
| 3938 | |
| 3939 | stkctr_set_flags(&s->stkctr[http_trk_idx(rule->action)], STKCTR_TRACK_CONTENT); |
| 3940 | if (sess->fe != s->be) |
| 3941 | stkctr_set_flags(&s->stkctr[http_trk_idx(rule->action)], STKCTR_TRACK_BACKEND); |
| 3942 | |
| 3943 | /* When the client triggers a 4xx from the server, it's most often due |
| 3944 | * to a missing object or permission. These events should be tracked |
| 3945 | * because if they happen often, it may indicate a brute force or a |
| 3946 | * vulnerability scan. Normally this is done when receiving the response |
| 3947 | * but here we're tracking after this ought to have been done so we have |
| 3948 | * to do it on purpose. |
| 3949 | */ |
Willy Tarreau | 3146a4c | 2016-07-26 15:22:33 +0200 | [diff] [blame] | 3950 | if ((unsigned)(txn->status - 400) < 100) { |
| 3951 | ptr = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_ERR_CNT); |
| 3952 | if (ptr) |
| 3953 | stktable_data_cast(ptr, http_err_cnt)++; |
| 3954 | |
| 3955 | ptr = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_ERR_RATE); |
| 3956 | if (ptr) |
| 3957 | update_freq_ctr_period(&stktable_data_cast(ptr, http_err_rate), |
| 3958 | t->data_arg[STKTABLE_DT_HTTP_ERR_RATE].u, 1); |
| 3959 | } |
Ruoshan Huang | e4edc6b | 2016-07-14 15:07:45 +0800 | [diff] [blame] | 3960 | } |
| 3961 | } |
| 3962 | break; |
| 3963 | |
Thierry FOURNIER | 4214873 | 2015-09-02 17:17:33 +0200 | [diff] [blame] | 3964 | case ACT_CUSTOM: |
Willy Tarreau | acc9800 | 2015-09-27 23:34:39 +0200 | [diff] [blame] | 3965 | if ((px->options & PR_O_ABRT_CLOSE) && (s->req.flags & (CF_SHUTR|CF_READ_NULL|CF_READ_ERROR))) |
| 3966 | act_flags |= ACT_FLAG_FINAL; |
Willy Tarreau | 3945868 | 2015-09-27 10:33:15 +0200 | [diff] [blame] | 3967 | |
Willy Tarreau | acc9800 | 2015-09-27 23:34:39 +0200 | [diff] [blame] | 3968 | switch (rule->action_ptr(rule, px, s->sess, s, act_flags)) { |
Thierry FOURNIER | 24ff6c6 | 2015-08-06 08:52:53 +0200 | [diff] [blame] | 3969 | case ACT_RET_ERR: |
| 3970 | case ACT_RET_CONT: |
| 3971 | break; |
Thierry FOURNIER | 4214873 | 2015-09-02 17:17:33 +0200 | [diff] [blame] | 3972 | case ACT_RET_STOP: |
| 3973 | return HTTP_RULE_RES_STOP; |
Thierry FOURNIER | 24ff6c6 | 2015-08-06 08:52:53 +0200 | [diff] [blame] | 3974 | case ACT_RET_YIELD: |
Willy Tarreau | 152b81e | 2015-04-20 13:26:17 +0200 | [diff] [blame] | 3975 | s->current_rule = rule; |
Thierry FOURNIER | bc4c1ac | 2015-02-25 13:36:14 +0100 | [diff] [blame] | 3976 | return HTTP_RULE_RES_YIELD; |
| 3977 | } |
William Lallemand | 73025dd | 2014-04-24 14:38:37 +0200 | [diff] [blame] | 3978 | break; |
| 3979 | |
Thierry FOURNIER | 22e4901 | 2015-08-05 19:13:48 +0200 | [diff] [blame] | 3980 | /* other flags exists, but normaly, they never be matched. */ |
| 3981 | default: |
| 3982 | break; |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 3983 | } |
| 3984 | } |
| 3985 | |
| 3986 | /* we reached the end of the rules, nothing to report */ |
Thierry FOURNIER | 9e2ef99 | 2015-02-25 13:51:19 +0100 | [diff] [blame] | 3987 | return HTTP_RULE_RES_CONT; |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 3988 | } |
| 3989 | |
| 3990 | |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 3991 | /* Perform an HTTP redirect based on the information in <rule>. The function |
| 3992 | * returns non-zero on success, or zero in case of a, irrecoverable error such |
| 3993 | * as too large a request to build a valid response. |
| 3994 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 3995 | 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] | 3996 | { |
Willy Tarreau | b329a31 | 2015-05-22 16:27:37 +0200 | [diff] [blame] | 3997 | struct http_msg *req = &txn->req; |
| 3998 | struct http_msg *res = &txn->rsp; |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 3999 | const char *msg_fmt; |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 4000 | const char *location; |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 4001 | |
| 4002 | /* build redirect message */ |
| 4003 | switch(rule->code) { |
Yves Lafon | 3e8d1ae | 2013-03-11 11:06:05 -0400 | [diff] [blame] | 4004 | case 308: |
| 4005 | msg_fmt = HTTP_308; |
| 4006 | break; |
| 4007 | case 307: |
| 4008 | msg_fmt = HTTP_307; |
| 4009 | break; |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 4010 | case 303: |
| 4011 | msg_fmt = HTTP_303; |
| 4012 | break; |
| 4013 | case 301: |
| 4014 | msg_fmt = HTTP_301; |
| 4015 | break; |
| 4016 | case 302: |
| 4017 | default: |
| 4018 | msg_fmt = HTTP_302; |
| 4019 | break; |
| 4020 | } |
| 4021 | |
| 4022 | if (unlikely(!chunk_strcpy(&trash, msg_fmt))) |
| 4023 | return 0; |
| 4024 | |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 4025 | location = trash.str + trash.len; |
| 4026 | |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 4027 | switch(rule->type) { |
| 4028 | case REDIRECT_TYPE_SCHEME: { |
| 4029 | const char *path; |
| 4030 | const char *host; |
| 4031 | struct hdr_ctx ctx; |
| 4032 | int pathlen; |
| 4033 | int hostlen; |
| 4034 | |
| 4035 | host = ""; |
| 4036 | hostlen = 0; |
| 4037 | ctx.idx = 0; |
Willy Tarreau | b329a31 | 2015-05-22 16:27:37 +0200 | [diff] [blame] | 4038 | 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] | 4039 | host = ctx.line + ctx.val; |
| 4040 | hostlen = ctx.vlen; |
| 4041 | } |
| 4042 | |
| 4043 | path = http_get_path(txn); |
| 4044 | /* build message using path */ |
| 4045 | if (path) { |
Willy Tarreau | b329a31 | 2015-05-22 16:27:37 +0200 | [diff] [blame] | 4046 | 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] | 4047 | if (rule->flags & REDIRECT_FLAG_DROP_QS) { |
| 4048 | int qs = 0; |
| 4049 | while (qs < pathlen) { |
| 4050 | if (path[qs] == '?') { |
| 4051 | pathlen = qs; |
| 4052 | break; |
| 4053 | } |
| 4054 | qs++; |
| 4055 | } |
| 4056 | } |
| 4057 | } else { |
| 4058 | path = "/"; |
| 4059 | pathlen = 1; |
| 4060 | } |
| 4061 | |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 4062 | if (rule->rdr_str) { /* this is an old "redirect" rule */ |
| 4063 | /* check if we can add scheme + "://" + host + path */ |
| 4064 | if (trash.len + rule->rdr_len + 3 + hostlen + pathlen > trash.size - 4) |
| 4065 | return 0; |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 4066 | |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 4067 | /* add scheme */ |
| 4068 | memcpy(trash.str + trash.len, rule->rdr_str, rule->rdr_len); |
| 4069 | trash.len += rule->rdr_len; |
| 4070 | } |
| 4071 | else { |
| 4072 | /* add scheme with executing log format */ |
| 4073 | 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] | 4074 | |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 4075 | /* check if we can add scheme + "://" + host + path */ |
| 4076 | if (trash.len + 3 + hostlen + pathlen > trash.size - 4) |
| 4077 | return 0; |
| 4078 | } |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 4079 | /* add "://" */ |
| 4080 | memcpy(trash.str + trash.len, "://", 3); |
| 4081 | trash.len += 3; |
| 4082 | |
| 4083 | /* add host */ |
| 4084 | memcpy(trash.str + trash.len, host, hostlen); |
| 4085 | trash.len += hostlen; |
| 4086 | |
| 4087 | /* add path */ |
| 4088 | memcpy(trash.str + trash.len, path, pathlen); |
| 4089 | trash.len += pathlen; |
| 4090 | |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 4091 | /* 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] | 4092 | if (trash.len && trash.str[trash.len - 1] != '/' && |
| 4093 | (rule->flags & REDIRECT_FLAG_APPEND_SLASH)) { |
| 4094 | if (trash.len > trash.size - 5) |
| 4095 | return 0; |
| 4096 | trash.str[trash.len] = '/'; |
| 4097 | trash.len++; |
| 4098 | } |
| 4099 | |
| 4100 | break; |
| 4101 | } |
| 4102 | case REDIRECT_TYPE_PREFIX: { |
| 4103 | const char *path; |
| 4104 | int pathlen; |
| 4105 | |
| 4106 | path = http_get_path(txn); |
| 4107 | /* build message using path */ |
| 4108 | if (path) { |
Willy Tarreau | b329a31 | 2015-05-22 16:27:37 +0200 | [diff] [blame] | 4109 | 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] | 4110 | if (rule->flags & REDIRECT_FLAG_DROP_QS) { |
| 4111 | int qs = 0; |
| 4112 | while (qs < pathlen) { |
| 4113 | if (path[qs] == '?') { |
| 4114 | pathlen = qs; |
| 4115 | break; |
| 4116 | } |
| 4117 | qs++; |
| 4118 | } |
| 4119 | } |
| 4120 | } else { |
| 4121 | path = "/"; |
| 4122 | pathlen = 1; |
| 4123 | } |
| 4124 | |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 4125 | if (rule->rdr_str) { /* this is an old "redirect" rule */ |
| 4126 | if (trash.len + rule->rdr_len + pathlen > trash.size - 4) |
| 4127 | return 0; |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 4128 | |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 4129 | /* add prefix. Note that if prefix == "/", we don't want to |
| 4130 | * add anything, otherwise it makes it hard for the user to |
| 4131 | * configure a self-redirection. |
| 4132 | */ |
| 4133 | if (rule->rdr_len != 1 || *rule->rdr_str != '/') { |
| 4134 | memcpy(trash.str + trash.len, rule->rdr_str, rule->rdr_len); |
| 4135 | trash.len += rule->rdr_len; |
| 4136 | } |
| 4137 | } |
| 4138 | else { |
| 4139 | /* add prefix with executing log format */ |
| 4140 | trash.len += build_logline(s, trash.str + trash.len, trash.size - trash.len, &rule->rdr_fmt); |
| 4141 | |
| 4142 | /* Check length */ |
| 4143 | if (trash.len + pathlen > trash.size - 4) |
| 4144 | return 0; |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 4145 | } |
| 4146 | |
| 4147 | /* add path */ |
| 4148 | memcpy(trash.str + trash.len, path, pathlen); |
| 4149 | trash.len += pathlen; |
| 4150 | |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 4151 | /* 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] | 4152 | if (trash.len && trash.str[trash.len - 1] != '/' && |
| 4153 | (rule->flags & REDIRECT_FLAG_APPEND_SLASH)) { |
| 4154 | if (trash.len > trash.size - 5) |
| 4155 | return 0; |
| 4156 | trash.str[trash.len] = '/'; |
| 4157 | trash.len++; |
| 4158 | } |
| 4159 | |
| 4160 | break; |
| 4161 | } |
| 4162 | case REDIRECT_TYPE_LOCATION: |
| 4163 | default: |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 4164 | if (rule->rdr_str) { /* this is an old "redirect" rule */ |
| 4165 | if (trash.len + rule->rdr_len > trash.size - 4) |
| 4166 | return 0; |
| 4167 | |
| 4168 | /* add location */ |
| 4169 | memcpy(trash.str + trash.len, rule->rdr_str, rule->rdr_len); |
| 4170 | trash.len += rule->rdr_len; |
| 4171 | } |
| 4172 | else { |
| 4173 | /* add location with executing log format */ |
| 4174 | 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] | 4175 | |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 4176 | /* Check left length */ |
| 4177 | if (trash.len > trash.size - 4) |
| 4178 | return 0; |
| 4179 | } |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 4180 | break; |
| 4181 | } |
| 4182 | |
| 4183 | if (rule->cookie_len) { |
| 4184 | memcpy(trash.str + trash.len, "\r\nSet-Cookie: ", 14); |
| 4185 | trash.len += 14; |
| 4186 | memcpy(trash.str + trash.len, rule->cookie_str, rule->cookie_len); |
| 4187 | trash.len += rule->cookie_len; |
| 4188 | memcpy(trash.str + trash.len, "\r\n", 2); |
| 4189 | trash.len += 2; |
| 4190 | } |
| 4191 | |
| 4192 | /* add end of headers and the keep-alive/close status. |
| 4193 | * We may choose to set keep-alive if the Location begins |
| 4194 | * with a slash, because the client will come back to the |
| 4195 | * same server. |
| 4196 | */ |
| 4197 | txn->status = rule->code; |
| 4198 | /* let's log the request time */ |
| 4199 | s->logs.tv_request = now; |
| 4200 | |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 4201 | if (*location == '/' && |
Willy Tarreau | b329a31 | 2015-05-22 16:27:37 +0200 | [diff] [blame] | 4202 | (req->flags & HTTP_MSGF_XFER_LEN) && |
Willy Tarreau | 2de8a50 | 2015-05-28 17:23:54 +0200 | [diff] [blame] | 4203 | ((!(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] | 4204 | ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL || |
| 4205 | (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL)) { |
| 4206 | /* keep-alive possible */ |
Willy Tarreau | b329a31 | 2015-05-22 16:27:37 +0200 | [diff] [blame] | 4207 | if (!(req->flags & HTTP_MSGF_VER_11)) { |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 4208 | if (unlikely(txn->flags & TX_USE_PX_CONN)) { |
| 4209 | memcpy(trash.str + trash.len, "\r\nProxy-Connection: keep-alive", 30); |
| 4210 | trash.len += 30; |
| 4211 | } else { |
| 4212 | memcpy(trash.str + trash.len, "\r\nConnection: keep-alive", 24); |
| 4213 | trash.len += 24; |
| 4214 | } |
| 4215 | } |
| 4216 | memcpy(trash.str + trash.len, "\r\n\r\n", 4); |
| 4217 | trash.len += 4; |
Christopher Faulet | 3e34429 | 2015-11-24 16:24:13 +0100 | [diff] [blame] | 4218 | FLT_STRM_CB(s, flt_http_reply(s, txn->status, &trash)); |
Willy Tarreau | b329a31 | 2015-05-22 16:27:37 +0200 | [diff] [blame] | 4219 | bo_inject(res->chn, trash.str, trash.len); |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 4220 | /* "eat" the request */ |
Willy Tarreau | b329a31 | 2015-05-22 16:27:37 +0200 | [diff] [blame] | 4221 | bi_fast_delete(req->chn->buf, req->sov); |
| 4222 | req->next -= req->sov; |
| 4223 | req->sov = 0; |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 4224 | s->req.analysers = AN_REQ_HTTP_XFER_BODY | (s->req.analysers & AN_FLT_END); |
| 4225 | s->res.analysers = AN_RES_HTTP_XFER_BODY | (s->req.analysers & AN_FLT_END); |
Willy Tarreau | b329a31 | 2015-05-22 16:27:37 +0200 | [diff] [blame] | 4226 | req->msg_state = HTTP_MSG_CLOSED; |
| 4227 | res->msg_state = HTTP_MSG_DONE; |
Willy Tarreau | 51d861a | 2015-05-22 17:30:48 +0200 | [diff] [blame] | 4228 | /* Trim any possible response */ |
| 4229 | res->chn->buf->i = 0; |
| 4230 | res->next = res->sov = 0; |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 4231 | } else { |
| 4232 | /* keep-alive not possible */ |
| 4233 | if (unlikely(txn->flags & TX_USE_PX_CONN)) { |
| 4234 | memcpy(trash.str + trash.len, "\r\nProxy-Connection: close\r\n\r\n", 29); |
| 4235 | trash.len += 29; |
| 4236 | } else { |
| 4237 | memcpy(trash.str + trash.len, "\r\nConnection: close\r\n\r\n", 23); |
| 4238 | trash.len += 23; |
| 4239 | } |
Christopher Faulet | a94e5a5 | 2015-12-09 15:55:06 +0100 | [diff] [blame] | 4240 | http_reply_and_close(s, txn->status, &trash); |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 4241 | req->chn->analysers &= AN_FLT_END; |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 4242 | } |
| 4243 | |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 4244 | if (!(s->flags & SF_ERR_MASK)) |
| 4245 | s->flags |= SF_ERR_LOCAL; |
| 4246 | if (!(s->flags & SF_FINST_MASK)) |
| 4247 | s->flags |= SF_FINST_R; |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 4248 | |
| 4249 | return 1; |
| 4250 | } |
| 4251 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 4252 | /* This stream analyser runs all HTTP request processing which is common to |
| 4253 | * frontends and backends, which means blocking ACLs, filters, connection-close, |
| 4254 | * reqadd, stats and redirects. This is performed for the designated proxy. |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 4255 | * 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] | 4256 | * either needs more data or wants to immediately abort the request (eg: deny, |
| 4257 | * error, ...). |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 4258 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 4259 | 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] | 4260 | { |
Willy Tarreau | fb0afa7 | 2015-04-03 14:46:27 +0200 | [diff] [blame] | 4261 | struct session *sess = s->sess; |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 4262 | struct http_txn *txn = s->txn; |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 4263 | struct http_msg *msg = &txn->req; |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 4264 | struct redirect_rule *rule; |
Willy Tarreau | f4f0412 | 2010-01-28 18:10:50 +0100 | [diff] [blame] | 4265 | struct cond_wordlist *wl; |
Willy Tarreau | 0b74833 | 2014-04-29 00:13:29 +0200 | [diff] [blame] | 4266 | enum rule_result verdict; |
Willy Tarreau | 58727ec | 2016-05-25 16:23:59 +0200 | [diff] [blame] | 4267 | int deny_status = HTTP_ERR_403; |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 4268 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 4269 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) { |
Willy Tarreau | 51aecc7 | 2009-07-12 09:47:04 +0200 | [diff] [blame] | 4270 | /* we need more data */ |
Thierry FOURNIER | bc4c1ac | 2015-02-25 13:36:14 +0100 | [diff] [blame] | 4271 | goto return_prx_yield; |
Willy Tarreau | 51aecc7 | 2009-07-12 09:47:04 +0200 | [diff] [blame] | 4272 | } |
| 4273 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 4274 | 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] | 4275 | now_ms, __FUNCTION__, |
| 4276 | s, |
| 4277 | req, |
| 4278 | req->rex, req->wex, |
| 4279 | req->flags, |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 4280 | req->buf->i, |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 4281 | req->analysers); |
| 4282 | |
Willy Tarreau | 6541083 | 2014-04-28 21:25:43 +0200 | [diff] [blame] | 4283 | /* just in case we have some per-backend tracking */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 4284 | stream_inc_be_http_req_ctr(s); |
Willy Tarreau | 6541083 | 2014-04-28 21:25:43 +0200 | [diff] [blame] | 4285 | |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 4286 | /* evaluate http-request rules */ |
Willy Tarreau | 0b74833 | 2014-04-29 00:13:29 +0200 | [diff] [blame] | 4287 | if (!LIST_ISEMPTY(&px->http_req_rules)) { |
Willy Tarreau | 58727ec | 2016-05-25 16:23:59 +0200 | [diff] [blame] | 4288 | verdict = http_req_get_intercept_rule(px, &px->http_req_rules, s, &deny_status); |
Willy Tarreau | 5142594 | 2010-02-01 10:40:19 +0100 | [diff] [blame] | 4289 | |
Willy Tarreau | 0b74833 | 2014-04-29 00:13:29 +0200 | [diff] [blame] | 4290 | switch (verdict) { |
Thierry FOURNIER | bc4c1ac | 2015-02-25 13:36:14 +0100 | [diff] [blame] | 4291 | case HTTP_RULE_RES_YIELD: /* some data miss, call the function later. */ |
| 4292 | goto return_prx_yield; |
| 4293 | |
Willy Tarreau | 0b74833 | 2014-04-29 00:13:29 +0200 | [diff] [blame] | 4294 | case HTTP_RULE_RES_CONT: |
| 4295 | case HTTP_RULE_RES_STOP: /* nothing to do */ |
| 4296 | break; |
Willy Tarreau | 5254259 | 2014-04-28 18:33:26 +0200 | [diff] [blame] | 4297 | |
Willy Tarreau | 0b74833 | 2014-04-29 00:13:29 +0200 | [diff] [blame] | 4298 | case HTTP_RULE_RES_DENY: /* deny or tarpit */ |
| 4299 | if (txn->flags & TX_CLTARPIT) |
| 4300 | goto tarpit; |
| 4301 | goto deny; |
Willy Tarreau | 5254259 | 2014-04-28 18:33:26 +0200 | [diff] [blame] | 4302 | |
Willy Tarreau | 0b74833 | 2014-04-29 00:13:29 +0200 | [diff] [blame] | 4303 | case HTTP_RULE_RES_ABRT: /* abort request, response already sent. Eg: auth */ |
| 4304 | goto return_prx_cond; |
Willy Tarreau | 5254259 | 2014-04-28 18:33:26 +0200 | [diff] [blame] | 4305 | |
Willy Tarreau | 0b74833 | 2014-04-29 00:13:29 +0200 | [diff] [blame] | 4306 | case HTTP_RULE_RES_DONE: /* OK, but terminate request processing (eg: redirect) */ |
Willy Tarreau | 5254259 | 2014-04-28 18:33:26 +0200 | [diff] [blame] | 4307 | goto done; |
| 4308 | |
Willy Tarreau | 0b74833 | 2014-04-29 00:13:29 +0200 | [diff] [blame] | 4309 | case HTTP_RULE_RES_BADREQ: /* failed with a bad request */ |
| 4310 | goto return_bad_req; |
| 4311 | } |
Willy Tarreau | bbba2a8 | 2014-04-28 13:57:26 +0200 | [diff] [blame] | 4312 | } |
| 4313 | |
Willy Tarreau | 5254259 | 2014-04-28 18:33:26 +0200 | [diff] [blame] | 4314 | /* OK at this stage, we know that the request was accepted according to |
| 4315 | * the http-request rules, we can check for the stats. Note that the |
| 4316 | * URI is detected *before* the req* rules in order not to be affected |
| 4317 | * by a possible reqrep, while they are processed *after* so that a |
| 4318 | * reqdeny can still block them. This clearly needs to change in 1.6! |
| 4319 | */ |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 4320 | if (stats_check_uri(&s->si[1], txn, px)) { |
Willy Tarreau | 5254259 | 2014-04-28 18:33:26 +0200 | [diff] [blame] | 4321 | s->target = &http_stats_applet.obj_type; |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 4322 | 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] | 4323 | txn->status = 500; |
| 4324 | s->logs.tv_request = now; |
Christopher Faulet | a94e5a5 | 2015-12-09 15:55:06 +0100 | [diff] [blame] | 4325 | http_reply_and_close(s, txn->status, http_error_message(s, HTTP_ERR_500)); |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 4326 | |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 4327 | if (!(s->flags & SF_ERR_MASK)) |
| 4328 | s->flags |= SF_ERR_RESOURCE; |
Willy Tarreau | 5254259 | 2014-04-28 18:33:26 +0200 | [diff] [blame] | 4329 | goto return_prx_cond; |
| 4330 | } |
| 4331 | |
| 4332 | /* parse the whole stats request and extract the relevant information */ |
| 4333 | http_handle_stats(s, req); |
Willy Tarreau | 58727ec | 2016-05-25 16:23:59 +0200 | [diff] [blame] | 4334 | verdict = http_req_get_intercept_rule(px, &px->uri_auth->http_req_rules, s, &deny_status); |
Willy Tarreau | 0b74833 | 2014-04-29 00:13:29 +0200 | [diff] [blame] | 4335 | /* not all actions implemented: deny, allow, auth */ |
Willy Tarreau | bbba2a8 | 2014-04-28 13:57:26 +0200 | [diff] [blame] | 4336 | |
Willy Tarreau | 0b74833 | 2014-04-29 00:13:29 +0200 | [diff] [blame] | 4337 | if (verdict == HTTP_RULE_RES_DENY) /* stats http-request deny */ |
| 4338 | goto deny; |
Willy Tarreau | 5254259 | 2014-04-28 18:33:26 +0200 | [diff] [blame] | 4339 | |
Willy Tarreau | 0b74833 | 2014-04-29 00:13:29 +0200 | [diff] [blame] | 4340 | if (verdict == HTTP_RULE_RES_ABRT) /* stats auth / stats http-request auth */ |
| 4341 | goto return_prx_cond; |
Krzysztof Piotr Oledzki | 59bb218 | 2010-01-29 17:58:21 +0100 | [diff] [blame] | 4342 | } |
| 4343 | |
Willy Tarreau | bbba2a8 | 2014-04-28 13:57:26 +0200 | [diff] [blame] | 4344 | /* evaluate the req* rules except reqadd */ |
| 4345 | if (px->req_exp != NULL) { |
Willy Tarreau | 6c123b1 | 2010-01-28 20:22:06 +0100 | [diff] [blame] | 4346 | if (apply_filters_to_request(s, req, px) < 0) |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 4347 | goto return_bad_req; |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 4348 | |
Willy Tarreau | bbba2a8 | 2014-04-28 13:57:26 +0200 | [diff] [blame] | 4349 | if (txn->flags & TX_CLDENY) |
| 4350 | goto deny; |
Willy Tarreau | c465fd7 | 2009-08-31 00:17:18 +0200 | [diff] [blame] | 4351 | |
Willy Tarreau | bbba2a8 | 2014-04-28 13:57:26 +0200 | [diff] [blame] | 4352 | if (txn->flags & TX_CLTARPIT) |
| 4353 | goto tarpit; |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 4354 | } |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 4355 | |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 4356 | /* add request headers from the rule sets in the same order */ |
| 4357 | list_for_each_entry(wl, &px->req_add, list) { |
| 4358 | if (wl->cond) { |
Willy Tarreau | 192252e | 2015-04-04 01:47:55 +0200 | [diff] [blame] | 4359 | 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] | 4360 | ret = acl_pass(ret); |
| 4361 | if (((struct acl_cond *)wl->cond)->pol == ACL_COND_UNLESS) |
| 4362 | ret = !ret; |
| 4363 | if (!ret) |
| 4364 | continue; |
| 4365 | } |
| 4366 | |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 4367 | 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] | 4368 | goto return_bad_req; |
Willy Tarreau | 81499eb | 2012-12-27 12:19:02 +0100 | [diff] [blame] | 4369 | } |
| 4370 | |
Willy Tarreau | 5254259 | 2014-04-28 18:33:26 +0200 | [diff] [blame] | 4371 | |
| 4372 | /* Proceed with the stats now. */ |
Willy Tarreau | 414e9bb | 2013-11-23 00:30:38 +0100 | [diff] [blame] | 4373 | if (unlikely(objt_applet(s->target) == &http_stats_applet)) { |
Willy Tarreau | 1facd6d | 2012-12-22 22:03:39 +0100 | [diff] [blame] | 4374 | /* process the stats request now */ |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 4375 | if (sess->fe == s->be) /* report it if the request was intercepted by the frontend */ |
| 4376 | sess->fe->fe_counters.intercepted_req++; |
Willy Tarreau | 347a35d | 2013-11-22 17:51:09 +0100 | [diff] [blame] | 4377 | |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 4378 | if (!(s->flags & SF_ERR_MASK)) // this is not really an error but it is |
| 4379 | s->flags |= SF_ERR_LOCAL; // to mark that it comes from the proxy |
| 4380 | if (!(s->flags & SF_FINST_MASK)) |
| 4381 | s->flags |= SF_FINST_R; |
Willy Tarreau | 347a35d | 2013-11-22 17:51:09 +0100 | [diff] [blame] | 4382 | |
Willy Tarreau | 70730dd | 2014-04-24 18:06:27 +0200 | [diff] [blame] | 4383 | /* enable the minimally required analyzers to handle keep-alive and compression on the HTTP response */ |
Christopher Faulet | 309c641 | 2015-12-02 09:57:32 +0100 | [diff] [blame] | 4384 | req->analysers &= (AN_REQ_HTTP_BODY | AN_FLT_HTTP_HDRS | AN_FLT_END); |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 4385 | req->analysers &= ~AN_FLT_XFER_DATA; |
| 4386 | req->analysers |= AN_REQ_HTTP_XFER_BODY; |
Willy Tarreau | bbba2a8 | 2014-04-28 13:57:26 +0200 | [diff] [blame] | 4387 | goto done; |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 4388 | } |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 4389 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 4390 | /* check whether we have some ACLs set to redirect this request */ |
| 4391 | list_for_each_entry(rule, &px->redirect_rules, list) { |
Willy Tarreau | f285f54 | 2010-01-03 20:03:03 +0100 | [diff] [blame] | 4392 | if (rule->cond) { |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 4393 | int ret; |
| 4394 | |
Willy Tarreau | 192252e | 2015-04-04 01:47:55 +0200 | [diff] [blame] | 4395 | 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] | 4396 | ret = acl_pass(ret); |
| 4397 | if (rule->cond->pol == ACL_COND_UNLESS) |
| 4398 | ret = !ret; |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 4399 | if (!ret) |
| 4400 | continue; |
Willy Tarreau | f285f54 | 2010-01-03 20:03:03 +0100 | [diff] [blame] | 4401 | } |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 4402 | if (!http_apply_redirect_rule(rule, s, txn)) |
| 4403 | goto return_bad_req; |
Willy Tarreau | bbba2a8 | 2014-04-28 13:57:26 +0200 | [diff] [blame] | 4404 | goto done; |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 4405 | } |
Willy Tarreau | 55ea757 | 2007-06-17 19:56:27 +0200 | [diff] [blame] | 4406 | |
Willy Tarreau | 2be3939 | 2010-01-03 17:24:51 +0100 | [diff] [blame] | 4407 | /* POST requests may be accompanied with an "Expect: 100-Continue" header. |
| 4408 | * If this happens, then the data will not come immediately, so we must |
| 4409 | * send all what we have without waiting. Note that due to the small gain |
| 4410 | * 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] | 4411 | * 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] | 4412 | * itself once used. |
| 4413 | */ |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 4414 | req->flags |= CF_SEND_DONTWAIT; |
Willy Tarreau | 2be3939 | 2010-01-03 17:24:51 +0100 | [diff] [blame] | 4415 | |
Willy Tarreau | bbba2a8 | 2014-04-28 13:57:26 +0200 | [diff] [blame] | 4416 | done: /* done with this analyser, continue with next ones that the calling |
| 4417 | * points will have set, if any. |
| 4418 | */ |
Willy Tarreau | bbba2a8 | 2014-04-28 13:57:26 +0200 | [diff] [blame] | 4419 | req->analyse_exp = TICK_ETERNITY; |
Thierry FOURNIER | 7566e30 | 2014-08-22 06:55:26 +0200 | [diff] [blame] | 4420 | done_without_exp: /* done with this analyser, but dont reset the analyse_exp. */ |
| 4421 | req->analysers &= ~an_bit; |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 4422 | return 1; |
Willy Tarreau | 1138281 | 2008-07-09 16:18:21 +0200 | [diff] [blame] | 4423 | |
Willy Tarreau | bbba2a8 | 2014-04-28 13:57:26 +0200 | [diff] [blame] | 4424 | tarpit: |
| 4425 | /* When a connection is tarpitted, we use the tarpit timeout, |
| 4426 | * which may be the same as the connect timeout if unspecified. |
| 4427 | * If unset, then set it to zero because we really want it to |
| 4428 | * eventually expire. We build the tarpit as an analyser. |
| 4429 | */ |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 4430 | channel_erase(&s->req); |
Willy Tarreau | bbba2a8 | 2014-04-28 13:57:26 +0200 | [diff] [blame] | 4431 | |
| 4432 | /* wipe the request out so that we can drop the connection early |
| 4433 | * if the client closes first. |
| 4434 | */ |
| 4435 | channel_dont_connect(req); |
Bertrand Paquet | 83a2c3d | 2016-04-06 11:58:31 +0200 | [diff] [blame] | 4436 | |
| 4437 | /* Allow cookie logging |
| 4438 | */ |
| 4439 | if (s->be->cookie_name || sess->fe->capture_name) |
| 4440 | manage_client_side_cookies(s, req); |
| 4441 | |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 4442 | req->analysers &= AN_FLT_END; /* remove switching rules etc... */ |
Willy Tarreau | bbba2a8 | 2014-04-28 13:57:26 +0200 | [diff] [blame] | 4443 | req->analysers |= AN_REQ_HTTP_TARPIT; |
| 4444 | req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.tarpit); |
| 4445 | if (!req->analyse_exp) |
| 4446 | req->analyse_exp = tick_add(now_ms, 0); |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 4447 | stream_inc_http_err_ctr(s); |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 4448 | sess->fe->fe_counters.denied_req++; |
| 4449 | if (sess->fe != s->be) |
Willy Tarreau | bbba2a8 | 2014-04-28 13:57:26 +0200 | [diff] [blame] | 4450 | s->be->be_counters.denied_req++; |
Willy Tarreau | fb0afa7 | 2015-04-03 14:46:27 +0200 | [diff] [blame] | 4451 | if (sess->listener->counters) |
| 4452 | sess->listener->counters->denied_req++; |
Thierry FOURNIER | 7566e30 | 2014-08-22 06:55:26 +0200 | [diff] [blame] | 4453 | goto done_without_exp; |
Willy Tarreau | bbba2a8 | 2014-04-28 13:57:26 +0200 | [diff] [blame] | 4454 | |
| 4455 | deny: /* this request was blocked (denied) */ |
Bertrand Paquet | 83a2c3d | 2016-04-06 11:58:31 +0200 | [diff] [blame] | 4456 | |
| 4457 | /* Allow cookie logging |
| 4458 | */ |
| 4459 | if (s->be->cookie_name || sess->fe->capture_name) |
| 4460 | manage_client_side_cookies(s, req); |
| 4461 | |
Willy Tarreau | 0b74833 | 2014-04-29 00:13:29 +0200 | [diff] [blame] | 4462 | txn->flags |= TX_CLDENY; |
Willy Tarreau | 58727ec | 2016-05-25 16:23:59 +0200 | [diff] [blame] | 4463 | txn->status = http_err_codes[deny_status]; |
Willy Tarreau | bbba2a8 | 2014-04-28 13:57:26 +0200 | [diff] [blame] | 4464 | s->logs.tv_request = now; |
Willy Tarreau | 58727ec | 2016-05-25 16:23:59 +0200 | [diff] [blame] | 4465 | http_reply_and_close(s, txn->status, http_error_message(s, deny_status)); |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 4466 | stream_inc_http_err_ctr(s); |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 4467 | sess->fe->fe_counters.denied_req++; |
| 4468 | if (sess->fe != s->be) |
Willy Tarreau | bbba2a8 | 2014-04-28 13:57:26 +0200 | [diff] [blame] | 4469 | s->be->be_counters.denied_req++; |
Willy Tarreau | fb0afa7 | 2015-04-03 14:46:27 +0200 | [diff] [blame] | 4470 | if (sess->listener->counters) |
| 4471 | sess->listener->counters->denied_req++; |
Willy Tarreau | bbba2a8 | 2014-04-28 13:57:26 +0200 | [diff] [blame] | 4472 | goto return_prx_cond; |
| 4473 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 4474 | return_bad_req: |
| 4475 | /* We centralize bad requests processing here */ |
| 4476 | if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) { |
| 4477 | /* we detected a parsing error. We want to archive this request |
| 4478 | * in the dedicated proxy area for later troubleshooting. |
| 4479 | */ |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 4480 | 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] | 4481 | } |
Willy Tarreau | 55ea757 | 2007-06-17 19:56:27 +0200 | [diff] [blame] | 4482 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 4483 | txn->req.msg_state = HTTP_MSG_ERROR; |
| 4484 | txn->status = 400; |
Christopher Faulet | a94e5a5 | 2015-12-09 15:55:06 +0100 | [diff] [blame] | 4485 | http_reply_and_close(s, txn->status, http_error_message(s, HTTP_ERR_400)); |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 4486 | |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 4487 | sess->fe->fe_counters.failed_req++; |
Willy Tarreau | fb0afa7 | 2015-04-03 14:46:27 +0200 | [diff] [blame] | 4488 | if (sess->listener->counters) |
| 4489 | sess->listener->counters->failed_req++; |
Willy Tarreau | 6e4261e | 2007-09-18 18:36:05 +0200 | [diff] [blame] | 4490 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 4491 | return_prx_cond: |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 4492 | if (!(s->flags & SF_ERR_MASK)) |
| 4493 | s->flags |= SF_ERR_PRXCOND; |
| 4494 | if (!(s->flags & SF_FINST_MASK)) |
| 4495 | s->flags |= SF_FINST_R; |
Willy Tarreau | f1221aa | 2006-12-17 22:14:12 +0100 | [diff] [blame] | 4496 | |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 4497 | req->analysers &= AN_FLT_END; |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 4498 | req->analyse_exp = TICK_ETERNITY; |
| 4499 | return 0; |
Thierry FOURNIER | bc4c1ac | 2015-02-25 13:36:14 +0100 | [diff] [blame] | 4500 | |
| 4501 | return_prx_yield: |
| 4502 | channel_dont_connect(req); |
| 4503 | return 0; |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 4504 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4505 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 4506 | /* This function performs all the processing enabled for the current request. |
| 4507 | * It returns 1 if the processing can continue on next analysers, or zero if it |
| 4508 | * needs more data, encounters an error, or wants to immediately abort the |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 4509 | * request. It relies on buffers flags, and updates s->req.analysers. |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 4510 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 4511 | 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] | 4512 | { |
Willy Tarreau | fb0afa7 | 2015-04-03 14:46:27 +0200 | [diff] [blame] | 4513 | struct session *sess = s->sess; |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 4514 | struct http_txn *txn = s->txn; |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 4515 | struct http_msg *msg = &txn->req; |
Willy Tarreau | ee335e6 | 2015-04-21 18:15:13 +0200 | [diff] [blame] | 4516 | struct connection *cli_conn = objt_conn(strm_sess(s)->origin); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4517 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 4518 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) { |
Willy Tarreau | 51aecc7 | 2009-07-12 09:47:04 +0200 | [diff] [blame] | 4519 | /* we need more data */ |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 4520 | channel_dont_connect(req); |
Willy Tarreau | 51aecc7 | 2009-07-12 09:47:04 +0200 | [diff] [blame] | 4521 | return 0; |
| 4522 | } |
| 4523 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 4524 | 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] | 4525 | now_ms, __FUNCTION__, |
| 4526 | s, |
| 4527 | req, |
| 4528 | req->rex, req->wex, |
| 4529 | req->flags, |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 4530 | req->buf->i, |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 4531 | req->analysers); |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 4532 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4533 | /* |
| 4534 | * Right now, we know that we have processed the entire headers |
| 4535 | * and that unwanted requests have been filtered out. We can do |
| 4536 | * whatever we want with the remaining request. Also, now we |
| 4537 | * may have separate values for ->fe, ->be. |
| 4538 | */ |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 4539 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4540 | /* |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 4541 | * If HTTP PROXY is set we simply get remote server address parsing |
| 4542 | * incoming request. Note that this requires that a connection is |
| 4543 | * allocated on the server side. |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4544 | */ |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 4545 | 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] | 4546 | struct connection *conn; |
Willy Tarreau | e8df1e1 | 2013-12-16 14:30:55 +0100 | [diff] [blame] | 4547 | char *path; |
Willy Tarreau | 32e3c6a | 2013-10-11 19:34:20 +0200 | [diff] [blame] | 4548 | |
Willy Tarreau | 9471b8c | 2013-12-15 13:31:35 +0100 | [diff] [blame] | 4549 | /* Note that for now we don't reuse existing proxy connections */ |
Willy Tarreau | 973a542 | 2015-08-05 21:47:23 +0200 | [diff] [blame] | 4550 | if (unlikely((conn = si_alloc_conn(&s->si[1])) == NULL)) { |
Willy Tarreau | 32e3c6a | 2013-10-11 19:34:20 +0200 | [diff] [blame] | 4551 | txn->req.msg_state = HTTP_MSG_ERROR; |
| 4552 | txn->status = 500; |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 4553 | req->analysers &= AN_FLT_END; |
Christopher Faulet | a94e5a5 | 2015-12-09 15:55:06 +0100 | [diff] [blame] | 4554 | http_reply_and_close(s, txn->status, http_error_message(s, HTTP_ERR_500)); |
Willy Tarreau | 32e3c6a | 2013-10-11 19:34:20 +0200 | [diff] [blame] | 4555 | |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 4556 | if (!(s->flags & SF_ERR_MASK)) |
| 4557 | s->flags |= SF_ERR_RESOURCE; |
| 4558 | if (!(s->flags & SF_FINST_MASK)) |
| 4559 | s->flags |= SF_FINST_R; |
Willy Tarreau | 32e3c6a | 2013-10-11 19:34:20 +0200 | [diff] [blame] | 4560 | |
| 4561 | return 0; |
| 4562 | } |
Willy Tarreau | e8df1e1 | 2013-12-16 14:30:55 +0100 | [diff] [blame] | 4563 | |
| 4564 | path = http_get_path(txn); |
| 4565 | url2sa(req->buf->p + msg->sl.rq.u, |
| 4566 | 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] | 4567 | &conn->addr.to, NULL); |
Willy Tarreau | e8df1e1 | 2013-12-16 14:30:55 +0100 | [diff] [blame] | 4568 | /* if the path was found, we have to remove everything between |
| 4569 | * req->buf->p + msg->sl.rq.u and path (excluded). If it was not |
| 4570 | * found, we need to replace from req->buf->p + msg->sl.rq.u for |
| 4571 | * u_l characters by a single "/". |
| 4572 | */ |
| 4573 | if (path) { |
| 4574 | char *cur_ptr = req->buf->p; |
| 4575 | char *cur_end = cur_ptr + txn->req.sl.rq.l; |
| 4576 | int delta; |
| 4577 | |
| 4578 | delta = buffer_replace2(req->buf, req->buf->p + msg->sl.rq.u, path, NULL, 0); |
| 4579 | http_msg_move_end(&txn->req, delta); |
| 4580 | cur_end += delta; |
| 4581 | if (http_parse_reqline(&txn->req, HTTP_MSG_RQMETH, cur_ptr, cur_end + 1, NULL, NULL) == NULL) |
| 4582 | goto return_bad_req; |
| 4583 | } |
| 4584 | else { |
| 4585 | char *cur_ptr = req->buf->p; |
| 4586 | char *cur_end = cur_ptr + txn->req.sl.rq.l; |
| 4587 | int delta; |
| 4588 | |
| 4589 | delta = buffer_replace2(req->buf, req->buf->p + msg->sl.rq.u, |
| 4590 | req->buf->p + msg->sl.rq.u + msg->sl.rq.u_l, "/", 1); |
| 4591 | http_msg_move_end(&txn->req, delta); |
| 4592 | cur_end += delta; |
| 4593 | if (http_parse_reqline(&txn->req, HTTP_MSG_RQMETH, cur_ptr, cur_end + 1, NULL, NULL) == NULL) |
| 4594 | goto return_bad_req; |
| 4595 | } |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4596 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4597 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4598 | /* |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 4599 | * 7: Now we can work with the cookies. |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4600 | * Note that doing so might move headers in the request, but |
| 4601 | * the fields will stay coherent and the URI will not move. |
| 4602 | * This should only be performed in the backend. |
| 4603 | */ |
Bertrand Paquet | 83a2c3d | 2016-04-06 11:58:31 +0200 | [diff] [blame] | 4604 | if (s->be->cookie_name || sess->fe->capture_name) |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4605 | manage_client_side_cookies(s, req); |
Willy Tarreau | 7ac51f6 | 2007-03-25 16:00:04 +0200 | [diff] [blame] | 4606 | |
William Lallemand | a73203e | 2012-03-12 12:48:57 +0100 | [diff] [blame] | 4607 | /* add unique-id if "header-unique-id" is specified */ |
| 4608 | |
Thierry Fournier | f4011dd | 2016-03-29 17:23:51 +0200 | [diff] [blame] | 4609 | if (!LIST_ISEMPTY(&sess->fe->format_unique_id) && !s->unique_id) { |
William Lallemand | 5b7ea3a | 2013-08-28 15:44:19 +0200 | [diff] [blame] | 4610 | if ((s->unique_id = pool_alloc2(pool2_uniqueid)) == NULL) |
| 4611 | goto return_bad_req; |
| 4612 | s->unique_id[0] = '\0'; |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 4613 | 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] | 4614 | } |
William Lallemand | a73203e | 2012-03-12 12:48:57 +0100 | [diff] [blame] | 4615 | |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 4616 | if (sess->fe->header_unique_id && s->unique_id) { |
| 4617 | 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] | 4618 | if (trash.len < 0) |
William Lallemand | a73203e | 2012-03-12 12:48:57 +0100 | [diff] [blame] | 4619 | goto return_bad_req; |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 4620 | 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] | 4621 | goto return_bad_req; |
| 4622 | } |
| 4623 | |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 4624 | /* |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4625 | * 9: add X-Forwarded-For if either the frontend or the backend |
| 4626 | * asks for it. |
| 4627 | */ |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 4628 | if ((sess->fe->options | s->be->options) & PR_O_FWDFOR) { |
Willy Tarreau | 87cf514 | 2011-08-19 22:57:24 +0200 | [diff] [blame] | 4629 | struct hdr_ctx ctx = { .idx = 0 }; |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 4630 | if (!((sess->fe->options | s->be->options) & PR_O_FF_ALWAYS) && |
| 4631 | http_find_header2(s->be->fwdfor_hdr_len ? s->be->fwdfor_hdr_name : sess->fe->fwdfor_hdr_name, |
| 4632 | 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] | 4633 | req->buf->p, &txn->hdr_idx, &ctx)) { |
Willy Tarreau | 87cf514 | 2011-08-19 22:57:24 +0200 | [diff] [blame] | 4634 | /* The header is set to be added only if none is present |
| 4635 | * and we found it, so don't do anything. |
| 4636 | */ |
| 4637 | } |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 4638 | else if (cli_conn && cli_conn->addr.from.ss_family == AF_INET) { |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4639 | /* Add an X-Forwarded-For header unless the source IP is |
| 4640 | * in the 'except' network range. |
| 4641 | */ |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 4642 | if ((!sess->fe->except_mask.s_addr || |
| 4643 | (((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr.s_addr & sess->fe->except_mask.s_addr) |
| 4644 | != sess->fe->except_net.s_addr) && |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4645 | (!s->be->except_mask.s_addr || |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 4646 | (((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] | 4647 | != s->be->except_net.s_addr)) { |
Willy Tarreau | 2a32428 | 2006-12-05 00:05:46 +0100 | [diff] [blame] | 4648 | int len; |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4649 | unsigned char *pn; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 4650 | pn = (unsigned char *)&((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr; |
Ross West | af72a1d | 2008-08-03 10:51:45 +0200 | [diff] [blame] | 4651 | |
| 4652 | /* Note: we rely on the backend to get the header name to be used for |
| 4653 | * x-forwarded-for, because the header is really meant for the backends. |
| 4654 | * However, if the backend did not specify any option, we have to rely |
| 4655 | * on the frontend's header name. |
| 4656 | */ |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4657 | if (s->be->fwdfor_hdr_len) { |
| 4658 | len = s->be->fwdfor_hdr_len; |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 4659 | memcpy(trash.str, s->be->fwdfor_hdr_name, len); |
Ross West | af72a1d | 2008-08-03 10:51:45 +0200 | [diff] [blame] | 4660 | } else { |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 4661 | len = sess->fe->fwdfor_hdr_len; |
| 4662 | memcpy(trash.str, sess->fe->fwdfor_hdr_name, len); |
Willy Tarreau | b86db34 | 2009-11-30 11:50:16 +0100 | [diff] [blame] | 4663 | } |
Willy Tarreau | e9187f8 | 2014-04-14 15:27:14 +0200 | [diff] [blame] | 4664 | 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] | 4665 | |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 4666 | 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] | 4667 | goto return_bad_req; |
Willy Tarreau | 2a32428 | 2006-12-05 00:05:46 +0100 | [diff] [blame] | 4668 | } |
| 4669 | } |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 4670 | else if (cli_conn && cli_conn->addr.from.ss_family == AF_INET6) { |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4671 | /* FIXME: for the sake of completeness, we should also support |
| 4672 | * 'except' here, although it is mostly useless in this case. |
matt.farnsworth@nokia.com | 1c2ab96 | 2008-04-14 20:47:37 +0200 | [diff] [blame] | 4673 | */ |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4674 | int len; |
| 4675 | char pn[INET6_ADDRSTRLEN]; |
| 4676 | inet_ntop(AF_INET6, |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 4677 | (const void *)&((struct sockaddr_in6 *)(&cli_conn->addr.from))->sin6_addr, |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4678 | pn, sizeof(pn)); |
matt.farnsworth@nokia.com | 1c2ab96 | 2008-04-14 20:47:37 +0200 | [diff] [blame] | 4679 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4680 | /* Note: we rely on the backend to get the header name to be used for |
| 4681 | * x-forwarded-for, because the header is really meant for the backends. |
| 4682 | * However, if the backend did not specify any option, we have to rely |
| 4683 | * on the frontend's header name. |
matt.farnsworth@nokia.com | 1c2ab96 | 2008-04-14 20:47:37 +0200 | [diff] [blame] | 4684 | */ |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4685 | if (s->be->fwdfor_hdr_len) { |
| 4686 | len = s->be->fwdfor_hdr_len; |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 4687 | memcpy(trash.str, s->be->fwdfor_hdr_name, len); |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4688 | } else { |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 4689 | len = sess->fe->fwdfor_hdr_len; |
| 4690 | memcpy(trash.str, sess->fe->fwdfor_hdr_name, len); |
matt.farnsworth@nokia.com | 1c2ab96 | 2008-04-14 20:47:37 +0200 | [diff] [blame] | 4691 | } |
Willy Tarreau | e9187f8 | 2014-04-14 15:27:14 +0200 | [diff] [blame] | 4692 | len += snprintf(trash.str + len, trash.size - len, ": %s", pn); |
Willy Tarreau | adfb856 | 2008-08-11 15:24:42 +0200 | [diff] [blame] | 4693 | |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 4694 | 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] | 4695 | goto return_bad_req; |
| 4696 | } |
| 4697 | } |
| 4698 | |
| 4699 | /* |
Maik Broemme | 2850cb4 | 2009-04-17 18:53:21 +0200 | [diff] [blame] | 4700 | * 10: add X-Original-To if either the frontend or the backend |
| 4701 | * asks for it. |
| 4702 | */ |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 4703 | if ((sess->fe->options | s->be->options) & PR_O_ORGTO) { |
Maik Broemme | 2850cb4 | 2009-04-17 18:53:21 +0200 | [diff] [blame] | 4704 | |
| 4705 | /* FIXME: don't know if IPv6 can handle that case too. */ |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 4706 | if (cli_conn && cli_conn->addr.from.ss_family == AF_INET) { |
Maik Broemme | 2850cb4 | 2009-04-17 18:53:21 +0200 | [diff] [blame] | 4707 | /* Add an X-Original-To header unless the destination IP is |
| 4708 | * in the 'except' network range. |
| 4709 | */ |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 4710 | conn_get_to_addr(cli_conn); |
Maik Broemme | 2850cb4 | 2009-04-17 18:53:21 +0200 | [diff] [blame] | 4711 | |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 4712 | if (cli_conn->addr.to.ss_family == AF_INET && |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 4713 | ((!sess->fe->except_mask_to.s_addr || |
| 4714 | (((struct sockaddr_in *)&cli_conn->addr.to)->sin_addr.s_addr & sess->fe->except_mask_to.s_addr) |
| 4715 | != sess->fe->except_to.s_addr) && |
Emeric Brun | 5bd86a8 | 2010-10-22 17:23:04 +0200 | [diff] [blame] | 4716 | (!s->be->except_mask_to.s_addr || |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 4717 | (((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] | 4718 | != s->be->except_to.s_addr))) { |
Maik Broemme | 2850cb4 | 2009-04-17 18:53:21 +0200 | [diff] [blame] | 4719 | int len; |
| 4720 | unsigned char *pn; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 4721 | pn = (unsigned char *)&((struct sockaddr_in *)&cli_conn->addr.to)->sin_addr; |
Maik Broemme | 2850cb4 | 2009-04-17 18:53:21 +0200 | [diff] [blame] | 4722 | |
| 4723 | /* Note: we rely on the backend to get the header name to be used for |
| 4724 | * x-original-to, because the header is really meant for the backends. |
| 4725 | * However, if the backend did not specify any option, we have to rely |
| 4726 | * on the frontend's header name. |
| 4727 | */ |
| 4728 | if (s->be->orgto_hdr_len) { |
| 4729 | len = s->be->orgto_hdr_len; |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 4730 | memcpy(trash.str, s->be->orgto_hdr_name, len); |
Maik Broemme | 2850cb4 | 2009-04-17 18:53:21 +0200 | [diff] [blame] | 4731 | } else { |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 4732 | len = sess->fe->orgto_hdr_len; |
| 4733 | memcpy(trash.str, sess->fe->orgto_hdr_name, len); |
Willy Tarreau | b86db34 | 2009-11-30 11:50:16 +0100 | [diff] [blame] | 4734 | } |
Willy Tarreau | e9187f8 | 2014-04-14 15:27:14 +0200 | [diff] [blame] | 4735 | 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] | 4736 | |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 4737 | 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] | 4738 | goto return_bad_req; |
| 4739 | } |
| 4740 | } |
| 4741 | } |
| 4742 | |
Willy Tarreau | 50fc777 | 2012-11-11 22:19:57 +0100 | [diff] [blame] | 4743 | /* 11: add "Connection: close" or "Connection: keep-alive" if needed and not yet set. |
| 4744 | * If an "Upgrade" token is found, the header is left untouched in order not to have |
| 4745 | * to deal with some servers bugs : some of them fail an Upgrade if anything but |
| 4746 | * "Upgrade" is present in the Connection header. |
| 4747 | */ |
| 4748 | if (!(txn->flags & TX_HDR_CONN_UPG) && |
| 4749 | (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) || |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 4750 | ((sess->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL || |
Willy Tarreau | 02bce8b | 2014-01-30 00:15:28 +0100 | [diff] [blame] | 4751 | (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL))) { |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 4752 | unsigned int want_flags = 0; |
| 4753 | |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 4754 | if (msg->flags & HTTP_MSGF_VER_11) { |
Willy Tarreau | 22a9534 | 2010-09-29 14:31:41 +0200 | [diff] [blame] | 4755 | if (((txn->flags & TX_CON_WANT_MSK) >= TX_CON_WANT_SCL || |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 4756 | ((sess->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL || |
Willy Tarreau | 02bce8b | 2014-01-30 00:15:28 +0100 | [diff] [blame] | 4757 | (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL)) && |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 4758 | !((sess->fe->options2|s->be->options2) & PR_O2_FAKE_KA)) |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 4759 | want_flags |= TX_CON_CLO_SET; |
| 4760 | } else { |
Willy Tarreau | 22a9534 | 2010-09-29 14:31:41 +0200 | [diff] [blame] | 4761 | if (((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL && |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 4762 | ((sess->fe->options & PR_O_HTTP_MODE) != PR_O_HTTP_PCL && |
Willy Tarreau | 02bce8b | 2014-01-30 00:15:28 +0100 | [diff] [blame] | 4763 | (s->be->options & PR_O_HTTP_MODE) != PR_O_HTTP_PCL)) || |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 4764 | ((sess->fe->options2|s->be->options2) & PR_O2_FAKE_KA)) |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 4765 | want_flags |= TX_CON_KAL_SET; |
| 4766 | } |
| 4767 | |
| 4768 | 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] | 4769 | http_change_connection_header(txn, msg, want_flags); |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4770 | } |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 4771 | |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 4772 | |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 4773 | /* If we have no server assigned yet and we're balancing on url_param |
| 4774 | * with a POST request, we may be interested in checking the body for |
| 4775 | * that parameter. This will be done in another analyser. |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4776 | */ |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 4777 | if (!(s->flags & (SF_ASSIGNED|SF_DIRECT)) && |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 4778 | s->txn->meth == HTTP_METH_POST && s->be->url_param_name != NULL && |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 4779 | (msg->flags & (HTTP_MSGF_CNT_LEN|HTTP_MSGF_TE_CHNK))) { |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 4780 | channel_dont_connect(req); |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 4781 | req->analysers |= AN_REQ_HTTP_BODY; |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4782 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 4783 | |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 4784 | if (msg->flags & HTTP_MSGF_XFER_LEN) { |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 4785 | req->analysers &= ~AN_FLT_XFER_DATA; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4786 | req->analysers |= AN_REQ_HTTP_XFER_BODY; |
Willy Tarreau | 5e20552 | 2011-12-17 16:34:27 +0100 | [diff] [blame] | 4787 | #ifdef TCP_QUICKACK |
| 4788 | /* We expect some data from the client. Unless we know for sure |
| 4789 | * we already have a full request, we have to re-enable quick-ack |
| 4790 | * in case we previously disabled it, otherwise we might cause |
| 4791 | * the client to delay further data. |
| 4792 | */ |
Willy Tarreau | fb0afa7 | 2015-04-03 14:46:27 +0200 | [diff] [blame] | 4793 | if ((sess->listener->options & LI_O_NOQUICKACK) && |
Willy Tarreau | 3c72872 | 2014-01-23 13:50:42 +0100 | [diff] [blame] | 4794 | cli_conn && conn_ctrl_ready(cli_conn) && |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 4795 | ((msg->flags & HTTP_MSGF_TE_CHNK) || |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 4796 | (msg->body_len > req->buf->i - txn->req.eoh - 2))) |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 4797 | 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] | 4798 | #endif |
| 4799 | } |
Willy Tarreau | 0394594 | 2009-12-22 16:50:27 +0100 | [diff] [blame] | 4800 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4801 | /************************************************************* |
| 4802 | * OK, that's finished for the headers. We have done what we * |
| 4803 | * could. Let's switch to the DATA state. * |
| 4804 | ************************************************************/ |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 4805 | req->analyse_exp = TICK_ETERNITY; |
| 4806 | req->analysers &= ~an_bit; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 4807 | |
Willy Tarreau | 7bb68ab | 2012-05-13 14:48:59 +0200 | [diff] [blame] | 4808 | /* if the server closes the connection, we want to immediately react |
| 4809 | * and close the socket to save packets and syscalls. |
| 4810 | */ |
Willy Tarreau | 40f151a | 2012-12-20 12:10:09 +0100 | [diff] [blame] | 4811 | if (!(req->analysers & AN_REQ_HTTP_XFER_BODY)) |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 4812 | s->si[1].flags |= SI_FL_NOHALF; |
Willy Tarreau | 7bb68ab | 2012-05-13 14:48:59 +0200 | [diff] [blame] | 4813 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4814 | s->logs.tv_request = now; |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4815 | /* OK let's go on with the BODY now */ |
| 4816 | return 1; |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 4817 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4818 | return_bad_req: /* let's centralize all bad requests */ |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 4819 | if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) { |
Willy Tarreau | f073a83 | 2009-03-01 23:21:47 +0100 | [diff] [blame] | 4820 | /* we detected a parsing error. We want to archive this request |
| 4821 | * in the dedicated proxy area for later troubleshooting. |
| 4822 | */ |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 4823 | 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] | 4824 | } |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 4825 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4826 | txn->req.msg_state = HTTP_MSG_ERROR; |
| 4827 | txn->status = 400; |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 4828 | req->analysers &= AN_FLT_END; |
Christopher Faulet | a94e5a5 | 2015-12-09 15:55:06 +0100 | [diff] [blame] | 4829 | http_reply_and_close(s, txn->status, http_error_message(s, HTTP_ERR_400)); |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 4830 | |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 4831 | sess->fe->fe_counters.failed_req++; |
Willy Tarreau | fb0afa7 | 2015-04-03 14:46:27 +0200 | [diff] [blame] | 4832 | if (sess->listener->counters) |
| 4833 | sess->listener->counters->failed_req++; |
Willy Tarreau | adfb856 | 2008-08-11 15:24:42 +0200 | [diff] [blame] | 4834 | |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 4835 | if (!(s->flags & SF_ERR_MASK)) |
| 4836 | s->flags |= SF_ERR_PRXCOND; |
| 4837 | if (!(s->flags & SF_FINST_MASK)) |
| 4838 | s->flags |= SF_FINST_R; |
Willy Tarreau | dafde43 | 2008-08-17 01:00:46 +0200 | [diff] [blame] | 4839 | return 0; |
Willy Tarreau | c65a3ba | 2008-08-11 23:42:50 +0200 | [diff] [blame] | 4840 | } |
Willy Tarreau | adfb856 | 2008-08-11 15:24:42 +0200 | [diff] [blame] | 4841 | |
Willy Tarreau | 60b85b0 | 2008-11-30 23:28:40 +0100 | [diff] [blame] | 4842 | /* This function is an analyser which processes the HTTP tarpit. It always |
| 4843 | * returns zero, at the beginning because it prevents any other processing |
| 4844 | * from occurring, and at the end because it terminates the request. |
| 4845 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 4846 | 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] | 4847 | { |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 4848 | struct http_txn *txn = s->txn; |
Willy Tarreau | 60b85b0 | 2008-11-30 23:28:40 +0100 | [diff] [blame] | 4849 | |
| 4850 | /* This connection is being tarpitted. The CLIENT side has |
| 4851 | * already set the connect expiration date to the right |
| 4852 | * timeout. We just have to check that the client is still |
| 4853 | * there and that the timeout has not expired. |
| 4854 | */ |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 4855 | channel_dont_connect(req); |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 4856 | if ((req->flags & (CF_SHUTR|CF_READ_ERROR)) == 0 && |
Willy Tarreau | 60b85b0 | 2008-11-30 23:28:40 +0100 | [diff] [blame] | 4857 | !tick_is_expired(req->analyse_exp, now_ms)) |
| 4858 | return 0; |
| 4859 | |
| 4860 | /* We will set the queue timer to the time spent, just for |
| 4861 | * logging purposes. We fake a 500 server error, so that the |
| 4862 | * attacker will not suspect his connection has been tarpitted. |
| 4863 | * It will not cause trouble to the logs because we can exclude |
| 4864 | * the tarpitted connections by filtering on the 'PT' status flags. |
| 4865 | */ |
Willy Tarreau | 60b85b0 | 2008-11-30 23:28:40 +0100 | [diff] [blame] | 4866 | s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now); |
| 4867 | |
| 4868 | txn->status = 500; |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 4869 | if (!(req->flags & CF_READ_ERROR)) |
Christopher Faulet | a94e5a5 | 2015-12-09 15:55:06 +0100 | [diff] [blame] | 4870 | http_reply_and_close(s, txn->status, http_error_message(s, HTTP_ERR_500)); |
Willy Tarreau | 60b85b0 | 2008-11-30 23:28:40 +0100 | [diff] [blame] | 4871 | |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 4872 | req->analysers &= AN_FLT_END; |
Willy Tarreau | 60b85b0 | 2008-11-30 23:28:40 +0100 | [diff] [blame] | 4873 | req->analyse_exp = TICK_ETERNITY; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 4874 | |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 4875 | if (!(s->flags & SF_ERR_MASK)) |
| 4876 | s->flags |= SF_ERR_PRXCOND; |
| 4877 | if (!(s->flags & SF_FINST_MASK)) |
| 4878 | s->flags |= SF_FINST_T; |
Willy Tarreau | 60b85b0 | 2008-11-30 23:28:40 +0100 | [diff] [blame] | 4879 | return 0; |
| 4880 | } |
| 4881 | |
Willy Tarreau | 5a8f947 | 2014-04-10 11:16:06 +0200 | [diff] [blame] | 4882 | /* This function is an analyser which waits for the HTTP request body. It waits |
| 4883 | * for either the buffer to be full, or the full advertised contents to have |
| 4884 | * reached the buffer. It must only be called after the standard HTTP request |
| 4885 | * processing has occurred, because it expects the request to be parsed and will |
| 4886 | * look for the Expect header. It may send a 100-Continue interim response. It |
| 4887 | * takes in input any state starting from HTTP_MSG_BODY and leaves with one of |
| 4888 | * HTTP_MSG_CHK_SIZE, HTTP_MSG_DATA or HTTP_MSG_TRAILERS. It returns zero if it |
| 4889 | * 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] | 4890 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 4891 | 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] | 4892 | { |
Willy Tarreau | fb0afa7 | 2015-04-03 14:46:27 +0200 | [diff] [blame] | 4893 | struct session *sess = s->sess; |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 4894 | struct http_txn *txn = s->txn; |
| 4895 | struct http_msg *msg = &s->txn->req; |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 4896 | |
| 4897 | /* We have to parse the HTTP request body to find any required data. |
| 4898 | * "balance url_param check_post" should have been the only way to get |
| 4899 | * into this. We were brought here after HTTP header analysis, so all |
| 4900 | * related structures are ready. |
| 4901 | */ |
| 4902 | |
Willy Tarreau | 890988f | 2014-04-10 11:59:33 +0200 | [diff] [blame] | 4903 | if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) { |
| 4904 | /* This is the first call */ |
| 4905 | if (msg->msg_state < HTTP_MSG_BODY) |
| 4906 | goto missing_data; |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 4907 | |
Willy Tarreau | 890988f | 2014-04-10 11:59:33 +0200 | [diff] [blame] | 4908 | if (msg->msg_state < HTTP_MSG_100_SENT) { |
| 4909 | /* If we have HTTP/1.1 and Expect: 100-continue, then we must |
| 4910 | * send an HTTP/1.1 100 Continue intermediate response. |
| 4911 | */ |
| 4912 | if (msg->flags & HTTP_MSGF_VER_11) { |
| 4913 | struct hdr_ctx ctx; |
| 4914 | ctx.idx = 0; |
| 4915 | /* Expect is allowed in 1.1, look for it */ |
| 4916 | if (http_find_header2("Expect", 6, req->buf->p, &txn->hdr_idx, &ctx) && |
| 4917 | 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] | 4918 | bo_inject(&s->res, http_100_chunk.str, http_100_chunk.len); |
Willy Tarreau | 890988f | 2014-04-10 11:59:33 +0200 | [diff] [blame] | 4919 | } |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 4920 | } |
Willy Tarreau | 890988f | 2014-04-10 11:59:33 +0200 | [diff] [blame] | 4921 | msg->msg_state = HTTP_MSG_100_SENT; |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 4922 | } |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 4923 | |
Willy Tarreau | fa4a03c | 2012-03-09 21:28:54 +0100 | [diff] [blame] | 4924 | /* 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] | 4925 | * req->buf->p still points to the beginning of the message. We |
| 4926 | * must save the body in msg->next because it survives buffer |
| 4927 | * re-alignments. |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4928 | */ |
Willy Tarreau | ea1175a | 2012-03-05 15:52:30 +0100 | [diff] [blame] | 4929 | msg->next = msg->sov; |
Willy Tarreau | a458b67 | 2012-03-05 11:17:50 +0100 | [diff] [blame] | 4930 | |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 4931 | if (msg->flags & HTTP_MSGF_TE_CHNK) |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 4932 | msg->msg_state = HTTP_MSG_CHUNK_SIZE; |
| 4933 | else |
| 4934 | msg->msg_state = HTTP_MSG_DATA; |
| 4935 | } |
| 4936 | |
Willy Tarreau | 890988f | 2014-04-10 11:59:33 +0200 | [diff] [blame] | 4937 | if (!(msg->flags & HTTP_MSGF_TE_CHNK)) { |
| 4938 | /* 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] | 4939 | if (http_body_bytes(msg) < msg->body_len) |
Willy Tarreau | 890988f | 2014-04-10 11:59:33 +0200 | [diff] [blame] | 4940 | goto missing_data; |
| 4941 | |
| 4942 | /* OK we have everything we need now */ |
| 4943 | goto http_end; |
| 4944 | } |
| 4945 | |
| 4946 | /* OK here we're parsing a chunked-encoded message */ |
| 4947 | |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 4948 | if (msg->msg_state == HTTP_MSG_CHUNK_SIZE) { |
Willy Tarreau | 124d991 | 2011-03-01 20:30:48 +0100 | [diff] [blame] | 4949 | /* read the chunk size and assign it to ->chunk_len, then |
Willy Tarreau | a458b67 | 2012-03-05 11:17:50 +0100 | [diff] [blame] | 4950 | * 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] | 4951 | * TRAILERS state. |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 4952 | */ |
Willy Tarreau | 4baf44b | 2012-03-09 14:10:20 +0100 | [diff] [blame] | 4953 | int ret = http_parse_chunk_size(msg); |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 4954 | |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 4955 | if (!ret) |
| 4956 | goto missing_data; |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 4957 | else if (ret < 0) { |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 4958 | stream_inc_http_err_ctr(s); |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 4959 | goto return_bad_req; |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 4960 | } |
Christopher Faulet | 113f7de | 2015-12-14 14:52:13 +0100 | [diff] [blame] | 4961 | msg->next += ret; |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 4962 | msg->msg_state = msg->chunk_len ? HTTP_MSG_DATA : HTTP_MSG_TRAILERS; |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 4963 | } |
| 4964 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4965 | /* Now we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state. |
Willy Tarreau | e115b49 | 2015-05-01 23:05:14 +0200 | [diff] [blame] | 4966 | * We have the first data byte is in msg->sov + msg->sol. We're waiting |
| 4967 | * for at least a whole chunk or the whole content length bytes after |
| 4968 | * msg->sov + msg->sol. |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 4969 | */ |
Willy Tarreau | 890988f | 2014-04-10 11:59:33 +0200 | [diff] [blame] | 4970 | if (msg->msg_state == HTTP_MSG_TRAILERS) |
| 4971 | goto http_end; |
| 4972 | |
Willy Tarreau | e115b49 | 2015-05-01 23:05:14 +0200 | [diff] [blame] | 4973 | 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] | 4974 | goto http_end; |
| 4975 | |
| 4976 | missing_data: |
Willy Tarreau | 31a1995 | 2014-04-10 11:50:37 +0200 | [diff] [blame] | 4977 | /* we get here if we need to wait for more data. If the buffer is full, |
| 4978 | * we have the maximum we can expect. |
| 4979 | */ |
| 4980 | if (buffer_full(req->buf, global.tune.maxrewrite)) |
| 4981 | goto http_end; |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 4982 | |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 4983 | 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] | 4984 | txn->status = 408; |
Christopher Faulet | a94e5a5 | 2015-12-09 15:55:06 +0100 | [diff] [blame] | 4985 | http_reply_and_close(s, txn->status, http_error_message(s, HTTP_ERR_408)); |
Willy Tarreau | 79ebac6 | 2010-06-07 13:47:49 +0200 | [diff] [blame] | 4986 | |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 4987 | if (!(s->flags & SF_ERR_MASK)) |
| 4988 | s->flags |= SF_ERR_CLITO; |
| 4989 | if (!(s->flags & SF_FINST_MASK)) |
| 4990 | s->flags |= SF_FINST_D; |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 4991 | goto return_err_msg; |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 4992 | } |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 4993 | |
| 4994 | /* we get here if we need to wait for more data */ |
Willy Tarreau | 31a1995 | 2014-04-10 11:50:37 +0200 | [diff] [blame] | 4995 | if (!(req->flags & (CF_SHUTR | CF_READ_ERROR))) { |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 4996 | /* Not enough data. We'll re-use the http-request |
| 4997 | * timeout here. Ideally, we should set the timeout |
| 4998 | * relative to the accept() date. We just set the |
| 4999 | * request timeout once at the beginning of the |
| 5000 | * request. |
| 5001 | */ |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 5002 | channel_dont_connect(req); |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 5003 | if (!tick_isset(req->analyse_exp)) |
Willy Tarreau | cd7afc0 | 2009-07-12 10:03:17 +0200 | [diff] [blame] | 5004 | req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.httpreq); |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 5005 | return 0; |
| 5006 | } |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 5007 | |
| 5008 | http_end: |
| 5009 | /* The situation will not evolve, so let's give up on the analysis. */ |
| 5010 | s->logs.tv_request = now; /* update the request timer to reflect full request */ |
| 5011 | req->analysers &= ~an_bit; |
| 5012 | req->analyse_exp = TICK_ETERNITY; |
| 5013 | return 1; |
| 5014 | |
| 5015 | return_bad_req: /* let's centralize all bad requests */ |
| 5016 | txn->req.msg_state = HTTP_MSG_ERROR; |
| 5017 | txn->status = 400; |
Christopher Faulet | a94e5a5 | 2015-12-09 15:55:06 +0100 | [diff] [blame] | 5018 | http_reply_and_close(s, txn->status, http_error_message(s, HTTP_ERR_400)); |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 5019 | |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 5020 | if (!(s->flags & SF_ERR_MASK)) |
| 5021 | s->flags |= SF_ERR_PRXCOND; |
| 5022 | if (!(s->flags & SF_FINST_MASK)) |
| 5023 | s->flags |= SF_FINST_R; |
Willy Tarreau | 79ebac6 | 2010-06-07 13:47:49 +0200 | [diff] [blame] | 5024 | |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 5025 | return_err_msg: |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 5026 | req->analysers &= AN_FLT_END; |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 5027 | sess->fe->fe_counters.failed_req++; |
Willy Tarreau | fb0afa7 | 2015-04-03 14:46:27 +0200 | [diff] [blame] | 5028 | if (sess->listener->counters) |
| 5029 | sess->listener->counters->failed_req++; |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 5030 | return 0; |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 5031 | } |
| 5032 | |
Willy Tarreau | d1de8af | 2012-05-18 22:12:14 +0200 | [diff] [blame] | 5033 | /* send a server's name with an outgoing request over an established connection. |
| 5034 | * Note: this function is designed to be called once the request has been scheduled |
| 5035 | * for being forwarded. This is the reason why it rewinds the buffer before |
| 5036 | * proceeding. |
| 5037 | */ |
Willy Tarreau | 45c0d98 | 2012-03-09 12:11:57 +0100 | [diff] [blame] | 5038 | 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] | 5039 | |
| 5040 | struct hdr_ctx ctx; |
| 5041 | |
Mark Lamourine | c2247f0 | 2012-01-04 13:02:01 -0500 | [diff] [blame] | 5042 | char *hdr_name = be->server_id_hdr_name; |
| 5043 | int hdr_name_len = be->server_id_hdr_len; |
Willy Tarreau | 394db37 | 2012-10-12 22:40:39 +0200 | [diff] [blame] | 5044 | struct channel *chn = txn->req.chn; |
Mark Lamourine | c2247f0 | 2012-01-04 13:02:01 -0500 | [diff] [blame] | 5045 | char *hdr_val; |
Willy Tarreau | d1de8af | 2012-05-18 22:12:14 +0200 | [diff] [blame] | 5046 | unsigned int old_o, old_i; |
Mark Lamourine | c2247f0 | 2012-01-04 13:02:01 -0500 | [diff] [blame] | 5047 | |
William Lallemand | d9e9066 | 2012-01-30 17:27:17 +0100 | [diff] [blame] | 5048 | ctx.idx = 0; |
| 5049 | |
Willy Tarreau | 211cdec | 2014-04-17 20:18:08 +0200 | [diff] [blame] | 5050 | old_o = http_hdr_rewind(&txn->req); |
Willy Tarreau | d1de8af | 2012-05-18 22:12:14 +0200 | [diff] [blame] | 5051 | if (old_o) { |
| 5052 | /* The request was already skipped, let's restore it */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 5053 | b_rew(chn->buf, old_o); |
Willy Tarreau | 877e78d | 2013-04-07 18:48:08 +0200 | [diff] [blame] | 5054 | txn->req.next += old_o; |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 5055 | txn->req.sov += old_o; |
Willy Tarreau | d1de8af | 2012-05-18 22:12:14 +0200 | [diff] [blame] | 5056 | } |
| 5057 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 5058 | old_i = chn->buf->i; |
| 5059 | 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] | 5060 | /* remove any existing values from the header */ |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 5061 | http_remove_header2(&txn->req, &txn->hdr_idx, &ctx); |
Mark Lamourine | c2247f0 | 2012-01-04 13:02:01 -0500 | [diff] [blame] | 5062 | } |
| 5063 | |
| 5064 | /* Add the new header requested with the server value */ |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 5065 | hdr_val = trash.str; |
Mark Lamourine | c2247f0 | 2012-01-04 13:02:01 -0500 | [diff] [blame] | 5066 | memcpy(hdr_val, hdr_name, hdr_name_len); |
| 5067 | hdr_val += hdr_name_len; |
| 5068 | *hdr_val++ = ':'; |
| 5069 | *hdr_val++ = ' '; |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 5070 | hdr_val += strlcpy2(hdr_val, srv_name, trash.str + trash.size - hdr_val); |
| 5071 | 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] | 5072 | |
Willy Tarreau | d1de8af | 2012-05-18 22:12:14 +0200 | [diff] [blame] | 5073 | if (old_o) { |
| 5074 | /* If this was a forwarded request, we must readjust the amount of |
| 5075 | * data to be forwarded in order to take into account the size |
Willy Tarreau | 877e78d | 2013-04-07 18:48:08 +0200 | [diff] [blame] | 5076 | * variations. Note that the current state is >= HTTP_MSG_BODY, |
| 5077 | * so we don't have to adjust ->sol. |
Willy Tarreau | d1de8af | 2012-05-18 22:12:14 +0200 | [diff] [blame] | 5078 | */ |
Willy Tarreau | 877e78d | 2013-04-07 18:48:08 +0200 | [diff] [blame] | 5079 | old_o += chn->buf->i - old_i; |
| 5080 | b_adv(chn->buf, old_o); |
| 5081 | txn->req.next -= old_o; |
| 5082 | txn->req.sov -= old_o; |
Willy Tarreau | d1de8af | 2012-05-18 22:12:14 +0200 | [diff] [blame] | 5083 | } |
| 5084 | |
Mark Lamourine | c2247f0 | 2012-01-04 13:02:01 -0500 | [diff] [blame] | 5085 | return 0; |
| 5086 | } |
| 5087 | |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5088 | /* Terminate current transaction and prepare a new one. This is very tricky |
| 5089 | * right now but it works. |
| 5090 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 5091 | void http_end_txn_clean_session(struct stream *s) |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5092 | { |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 5093 | int prev_status = s->txn->status; |
Willy Tarreau | d0d8da9 | 2015-04-04 02:10:38 +0200 | [diff] [blame] | 5094 | struct proxy *fe = strm_fe(s); |
Willy Tarreau | 858b103 | 2015-12-07 17:04:59 +0100 | [diff] [blame] | 5095 | struct proxy *be = s->be; |
Willy Tarreau | 323a2d9 | 2015-08-04 19:00:17 +0200 | [diff] [blame] | 5096 | struct connection *srv_conn; |
| 5097 | struct server *srv; |
Willy Tarreau | 449d74a | 2015-08-05 17:16:33 +0200 | [diff] [blame] | 5098 | unsigned int prev_flags = s->txn->flags; |
Willy Tarreau | 068621e | 2013-12-23 15:11:25 +0100 | [diff] [blame] | 5099 | |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5100 | /* FIXME: We need a more portable way of releasing a backend's and a |
| 5101 | * server's connections. We need a safer way to reinitialize buffer |
| 5102 | * flags. We also need a more accurate method for computing per-request |
| 5103 | * data. |
| 5104 | */ |
Willy Tarreau | 323a2d9 | 2015-08-04 19:00:17 +0200 | [diff] [blame] | 5105 | srv_conn = objt_conn(s->si[1].end); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5106 | |
Willy Tarreau | 4213a11 | 2013-12-15 10:25:42 +0100 | [diff] [blame] | 5107 | /* unless we're doing keep-alive, we want to quickly close the connection |
| 5108 | * to the server. |
| 5109 | */ |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 5110 | if (((s->txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) || |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 5111 | !si_conn_ready(&s->si[1])) { |
| 5112 | s->si[1].flags |= SI_FL_NOLINGER | SI_FL_NOHALF; |
| 5113 | si_shutr(&s->si[1]); |
| 5114 | si_shutw(&s->si[1]); |
Willy Tarreau | 4213a11 | 2013-12-15 10:25:42 +0100 | [diff] [blame] | 5115 | } |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5116 | |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 5117 | if (s->flags & SF_BE_ASSIGNED) { |
Willy Tarreau | 858b103 | 2015-12-07 17:04:59 +0100 | [diff] [blame] | 5118 | be->beconn--; |
Willy Tarreau | 2d5cd47 | 2012-03-01 23:34:37 +0100 | [diff] [blame] | 5119 | if (unlikely(s->srv_conn)) |
| 5120 | sess_change_server(s, NULL); |
| 5121 | } |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5122 | |
| 5123 | s->logs.t_close = tv_ms_elapsed(&s->logs.tv_accept, &now); |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 5124 | stream_process_counters(s); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5125 | |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 5126 | if (s->txn->status) { |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5127 | int n; |
| 5128 | |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 5129 | n = s->txn->status / 100; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5130 | if (n < 1 || n > 5) |
| 5131 | n = 0; |
| 5132 | |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 5133 | if (fe->mode == PR_MODE_HTTP) { |
| 5134 | fe->fe_counters.p.http.rsp[n]++; |
Willy Tarreau | 5e16cbc | 2012-11-24 14:54:13 +0100 | [diff] [blame] | 5135 | } |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 5136 | if ((s->flags & SF_BE_ASSIGNED) && |
Willy Tarreau | 858b103 | 2015-12-07 17:04:59 +0100 | [diff] [blame] | 5137 | (be->mode == PR_MODE_HTTP)) { |
| 5138 | be->be_counters.p.http.rsp[n]++; |
| 5139 | be->be_counters.p.http.cum_req++; |
Willy Tarreau | 5e16cbc | 2012-11-24 14:54:13 +0100 | [diff] [blame] | 5140 | } |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5141 | } |
| 5142 | |
| 5143 | /* don't count other requests' data */ |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 5144 | s->logs.bytes_in -= s->req.buf->i; |
| 5145 | s->logs.bytes_out -= s->res.buf->i; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5146 | |
| 5147 | /* let's do a final log if we need it */ |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 5148 | if (!LIST_ISEMPTY(&fe->logformat) && s->logs.logwait && |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 5149 | !(s->flags & SF_MONITOR) && |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 5150 | (!(fe->options & PR_O_NULLNOLOG) || s->req.total)) { |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5151 | s->do_log(s); |
| 5152 | } |
| 5153 | |
Willy Tarreau | d713bcc | 2014-06-25 15:36:04 +0200 | [diff] [blame] | 5154 | /* stop tracking content-based counters */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 5155 | stream_stop_content_counters(s); |
| 5156 | stream_update_time_stats(s); |
Willy Tarreau | 4bfc580 | 2014-06-17 12:19:18 +0200 | [diff] [blame] | 5157 | |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5158 | s->logs.accept_date = date; /* user-visible date for logging */ |
| 5159 | s->logs.tv_accept = now; /* corrected date for internal use */ |
Thierry FOURNIER / OZON.IO | 4cac359 | 2016-07-28 17:19:45 +0200 | [diff] [blame] | 5160 | s->logs.t_handshake = 0; /* There are no handshake in keep alive connection. */ |
| 5161 | s->logs.t_idle = -1; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5162 | tv_zero(&s->logs.tv_request); |
| 5163 | s->logs.t_queue = -1; |
| 5164 | s->logs.t_connect = -1; |
| 5165 | s->logs.t_data = -1; |
| 5166 | s->logs.t_close = 0; |
| 5167 | s->logs.prx_queue_size = 0; /* we get the number of pending conns before us */ |
| 5168 | s->logs.srv_queue_size = 0; /* we will get this number soon */ |
| 5169 | |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 5170 | s->logs.bytes_in = s->req.total = s->req.buf->i; |
| 5171 | s->logs.bytes_out = s->res.total = s->res.buf->i; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5172 | |
| 5173 | if (s->pend_pos) |
| 5174 | pendconn_free(s->pend_pos); |
| 5175 | |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 5176 | if (objt_server(s->target)) { |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 5177 | if (s->flags & SF_CURR_SESS) { |
| 5178 | s->flags &= ~SF_CURR_SESS; |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 5179 | objt_server(s->target)->cur_sess--; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5180 | } |
Willy Tarreau | 858b103 | 2015-12-07 17:04:59 +0100 | [diff] [blame] | 5181 | if (may_dequeue_tasks(objt_server(s->target), be)) |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 5182 | process_srv_queue(objt_server(s->target)); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5183 | } |
| 5184 | |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 5185 | s->target = NULL; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5186 | |
Willy Tarreau | 4213a11 | 2013-12-15 10:25:42 +0100 | [diff] [blame] | 5187 | /* only release our endpoint if we don't intend to reuse the |
| 5188 | * connection. |
| 5189 | */ |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 5190 | if (((s->txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) || |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 5191 | !si_conn_ready(&s->si[1])) { |
| 5192 | si_release_endpoint(&s->si[1]); |
Willy Tarreau | 323a2d9 | 2015-08-04 19:00:17 +0200 | [diff] [blame] | 5193 | srv_conn = NULL; |
Willy Tarreau | 4213a11 | 2013-12-15 10:25:42 +0100 | [diff] [blame] | 5194 | } |
| 5195 | |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 5196 | s->si[1].state = s->si[1].prev_state = SI_ST_INI; |
| 5197 | s->si[1].err_type = SI_ET_NONE; |
| 5198 | s->si[1].conn_retries = 0; /* used for logging too */ |
| 5199 | s->si[1].exp = TICK_ETERNITY; |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 5200 | 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] | 5201 | 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); |
| 5202 | 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] | 5203 | s->flags &= ~(SF_DIRECT|SF_ASSIGNED|SF_ADDR_SET|SF_BE_ASSIGNED|SF_FORCE_PRST|SF_IGNORE_PRST); |
| 5204 | s->flags &= ~(SF_CURR_SESS|SF_REDIRECTABLE|SF_SRV_REUSED); |
| 5205 | s->flags &= ~(SF_ERR_MASK|SF_FINST_MASK|SF_REDISP); |
Willy Tarreau | 543db62 | 2012-11-15 16:41:22 +0100 | [diff] [blame] | 5206 | |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 5207 | s->txn->meth = 0; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5208 | http_reset_txn(s); |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 5209 | s->txn->flags |= TX_NOT_FIRST | TX_WAIT_NEXT_RQ; |
Willy Tarreau | 068621e | 2013-12-23 15:11:25 +0100 | [diff] [blame] | 5210 | |
| 5211 | if (prev_status == 401 || prev_status == 407) { |
| 5212 | /* In HTTP keep-alive mode, if we receive a 401, we still have |
| 5213 | * a chance of being able to send the visitor again to the same |
| 5214 | * server over the same connection. This is required by some |
| 5215 | * broken protocols such as NTLM, and anyway whenever there is |
| 5216 | * an opportunity for sending the challenge to the proper place, |
| 5217 | * it's better to do it (at least it helps with debugging). |
| 5218 | */ |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 5219 | s->txn->flags |= TX_PREFER_LAST; |
Willy Tarreau | bd99d58 | 2015-09-02 10:40:43 +0200 | [diff] [blame] | 5220 | if (srv_conn) |
| 5221 | srv_conn->flags |= CO_FL_PRIVATE; |
Willy Tarreau | 068621e | 2013-12-23 15:11:25 +0100 | [diff] [blame] | 5222 | } |
| 5223 | |
Willy Tarreau | 53f9685 | 2016-02-02 18:50:47 +0100 | [diff] [blame] | 5224 | /* Never ever allow to reuse a connection from a non-reuse backend */ |
| 5225 | if (srv_conn && (be->options & PR_O_REUSE_MASK) == PR_O_REUSE_NEVR) |
| 5226 | srv_conn->flags |= CO_FL_PRIVATE; |
| 5227 | |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 5228 | if (fe->options2 & PR_O2_INDEPSTR) |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 5229 | s->si[1].flags |= SI_FL_INDEP_STR; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5230 | |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 5231 | if (fe->options2 & PR_O2_NODELAY) { |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 5232 | s->req.flags |= CF_NEVER_WAIT; |
| 5233 | s->res.flags |= CF_NEVER_WAIT; |
Willy Tarreau | 96e3121 | 2011-05-30 18:10:30 +0200 | [diff] [blame] | 5234 | } |
| 5235 | |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5236 | /* if the request buffer is not empty, it means we're |
| 5237 | * about to process another request, so send pending |
| 5238 | * data with MSG_MORE to merge TCP packets when possible. |
Willy Tarreau | 065e833 | 2010-01-08 00:30:20 +0100 | [diff] [blame] | 5239 | * Just don't do this if the buffer is close to be full, |
| 5240 | * because the request will wait for it to flush a little |
| 5241 | * bit before proceeding. |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5242 | */ |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 5243 | if (s->req.buf->i) { |
| 5244 | if (s->res.buf->o && |
| 5245 | !buffer_full(s->res.buf, global.tune.maxrewrite) && |
| 5246 | bi_end(s->res.buf) <= s->res.buf->data + s->res.buf->size - global.tune.maxrewrite) |
| 5247 | s->res.flags |= CF_EXPECT_MORE; |
Willy Tarreau | 065e833 | 2010-01-08 00:30:20 +0100 | [diff] [blame] | 5248 | } |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 5249 | |
Willy Tarreau | 714ea78 | 2015-11-25 20:11:11 +0100 | [diff] [blame] | 5250 | /* we're removing the analysers, we MUST re-enable events detection. |
| 5251 | * We don't enable close on the response channel since it's either |
| 5252 | * already closed, or in keep-alive with an idle connection handler. |
| 5253 | */ |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 5254 | channel_auto_read(&s->req); |
| 5255 | channel_auto_close(&s->req); |
| 5256 | channel_auto_read(&s->res); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5257 | |
Willy Tarreau | 1c59bd5 | 2015-11-02 20:20:11 +0100 | [diff] [blame] | 5258 | /* we're in keep-alive with an idle connection, monitor it if not already done */ |
| 5259 | if (srv_conn && LIST_ISEMPTY(&srv_conn->list)) { |
Willy Tarreau | 323a2d9 | 2015-08-04 19:00:17 +0200 | [diff] [blame] | 5260 | srv = objt_server(srv_conn->target); |
Willy Tarreau | 8dff998 | 2015-08-04 20:45:52 +0200 | [diff] [blame] | 5261 | if (!srv) |
| 5262 | si_idle_conn(&s->si[1], NULL); |
Willy Tarreau | 53f9685 | 2016-02-02 18:50:47 +0100 | [diff] [blame] | 5263 | else if (srv_conn->flags & CO_FL_PRIVATE) |
Willy Tarreau | 8dff998 | 2015-08-04 20:45:52 +0200 | [diff] [blame] | 5264 | si_idle_conn(&s->si[1], &srv->priv_conns); |
Willy Tarreau | 449d74a | 2015-08-05 17:16:33 +0200 | [diff] [blame] | 5265 | else if (prev_flags & TX_NOT_FIRST) |
| 5266 | /* note: we check the request, not the connection, but |
| 5267 | * this is valid for strategies SAFE and AGGR, and in |
| 5268 | * case of ALWS, we don't care anyway. |
| 5269 | */ |
| 5270 | si_idle_conn(&s->si[1], &srv->safe_conns); |
Willy Tarreau | 8dff998 | 2015-08-04 20:45:52 +0200 | [diff] [blame] | 5271 | else |
| 5272 | si_idle_conn(&s->si[1], &srv->idle_conns); |
Willy Tarreau | 4320eaa | 2015-08-05 11:08:30 +0200 | [diff] [blame] | 5273 | } |
Willy Tarreau | 2737562 | 2013-12-17 00:00:28 +0100 | [diff] [blame] | 5274 | |
Christopher Faulet | 3e34429 | 2015-11-24 16:24:13 +0100 | [diff] [blame] | 5275 | if (HAS_FILTERS(s)) { |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 5276 | s->req.analysers &= AN_FLT_END; |
| 5277 | s->res.analysers &= AN_FLT_END; |
| 5278 | } |
Christopher Faulet | 3e34429 | 2015-11-24 16:24:13 +0100 | [diff] [blame] | 5279 | else { |
| 5280 | s->req.analysers = strm_li(s) ? strm_li(s)->analysers : 0; |
| 5281 | s->res.analysers = 0; |
| 5282 | } |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5283 | } |
| 5284 | |
| 5285 | |
| 5286 | /* This function updates the request state machine according to the response |
| 5287 | * state machine and buffer flags. It returns 1 if it changes anything (flag |
| 5288 | * or state), otherwise zero. It ignores any state before HTTP_MSG_DONE, as |
| 5289 | * it is only used to find when a request/response couple is complete. Both |
| 5290 | * this function and its equivalent should loop until both return zero. It |
| 5291 | * can set its own state to DONE, CLOSING, CLOSED, TUNNEL, ERROR. |
| 5292 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 5293 | int http_sync_req_state(struct stream *s) |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5294 | { |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 5295 | struct channel *chn = &s->req; |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 5296 | struct http_txn *txn = s->txn; |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 5297 | unsigned int old_flags = chn->flags; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5298 | unsigned int old_state = txn->req.msg_state; |
| 5299 | |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5300 | if (unlikely(txn->req.msg_state < HTTP_MSG_BODY)) |
| 5301 | return 0; |
| 5302 | |
| 5303 | if (txn->req.msg_state == HTTP_MSG_DONE) { |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 5304 | /* No need to read anymore, the request was completely parsed. |
Willy Tarreau | 58bd8fd | 2010-09-28 14:16:41 +0200 | [diff] [blame] | 5305 | * We can shut the read side unless we want to abort_on_close, |
| 5306 | * or we have a POST request. The issue with POST requests is |
| 5307 | * that some browsers still send a CRLF after the request, and |
| 5308 | * this CRLF must be read so that it does not remain in the kernel |
| 5309 | * buffers, otherwise a close could cause an RST on some systems |
| 5310 | * (eg: Linux). |
Willy Tarreau | 3988d93 | 2013-12-27 23:03:08 +0100 | [diff] [blame] | 5311 | * Note that if we're using keep-alive on the client side, we'd |
| 5312 | * rather poll now and keep the polling enabled for the whole |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 5313 | * stream's life than enabling/disabling it between each |
Willy Tarreau | 3988d93 | 2013-12-27 23:03:08 +0100 | [diff] [blame] | 5314 | * response and next request. |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 5315 | */ |
Willy Tarreau | 3988d93 | 2013-12-27 23:03:08 +0100 | [diff] [blame] | 5316 | if (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_SCL) && |
| 5317 | ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) && |
| 5318 | !(s->be->options & PR_O_ABRT_CLOSE) && |
| 5319 | txn->meth != HTTP_METH_POST) |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 5320 | channel_dont_read(chn); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5321 | |
Willy Tarreau | 40f151a | 2012-12-20 12:10:09 +0100 | [diff] [blame] | 5322 | /* if the server closes the connection, we want to immediately react |
| 5323 | * and close the socket to save packets and syscalls. |
| 5324 | */ |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 5325 | s->si[1].flags |= SI_FL_NOHALF; |
Willy Tarreau | 40f151a | 2012-12-20 12:10:09 +0100 | [diff] [blame] | 5326 | |
Willy Tarreau | 7f876a1 | 2015-11-18 11:59:55 +0100 | [diff] [blame] | 5327 | /* In any case we've finished parsing the request so we must |
| 5328 | * disable Nagle when sending data because 1) we're not going |
| 5329 | * to shut this side, and 2) the server is waiting for us to |
| 5330 | * send pending data. |
| 5331 | */ |
| 5332 | chn->flags |= CF_NEVER_WAIT; |
| 5333 | |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5334 | if (txn->rsp.msg_state == HTTP_MSG_ERROR) |
| 5335 | goto wait_other_side; |
| 5336 | |
| 5337 | if (txn->rsp.msg_state < HTTP_MSG_DONE) { |
| 5338 | /* The server has not finished to respond, so we |
| 5339 | * don't want to move in order not to upset it. |
| 5340 | */ |
| 5341 | goto wait_other_side; |
| 5342 | } |
| 5343 | |
| 5344 | if (txn->rsp.msg_state == HTTP_MSG_TUNNEL) { |
| 5345 | /* if any side switches to tunnel mode, the other one does too */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 5346 | channel_auto_read(chn); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5347 | txn->req.msg_state = HTTP_MSG_TUNNEL; |
| 5348 | goto wait_other_side; |
| 5349 | } |
| 5350 | |
| 5351 | /* When we get here, it means that both the request and the |
| 5352 | * response have finished receiving. Depending on the connection |
| 5353 | * mode, we'll have to wait for the last bytes to leave in either |
| 5354 | * direction, and sometimes for a close to be effective. |
| 5355 | */ |
| 5356 | |
Willy Tarreau | cce7fa4 | 2010-01-16 23:19:39 +0100 | [diff] [blame] | 5357 | if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) { |
| 5358 | /* Server-close mode : queue a connection close to the server */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 5359 | if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW))) |
| 5360 | channel_shutw_now(chn); |
Willy Tarreau | cce7fa4 | 2010-01-16 23:19:39 +0100 | [diff] [blame] | 5361 | } |
| 5362 | else if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) { |
| 5363 | /* Option forceclose is set, or either side wants to close, |
| 5364 | * let's enforce it now that we're not expecting any new |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 5365 | * data to come. The caller knows the stream is complete |
Willy Tarreau | cce7fa4 | 2010-01-16 23:19:39 +0100 | [diff] [blame] | 5366 | * once both states are CLOSED. |
| 5367 | */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 5368 | if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW))) { |
| 5369 | channel_shutr_now(chn); |
| 5370 | channel_shutw_now(chn); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5371 | } |
Willy Tarreau | cce7fa4 | 2010-01-16 23:19:39 +0100 | [diff] [blame] | 5372 | } |
| 5373 | else { |
Willy Tarreau | 4213a11 | 2013-12-15 10:25:42 +0100 | [diff] [blame] | 5374 | /* The last possible modes are keep-alive and tunnel. Tunnel mode |
| 5375 | * will not have any analyser so it needs to poll for reads. |
Willy Tarreau | cce7fa4 | 2010-01-16 23:19:39 +0100 | [diff] [blame] | 5376 | */ |
Willy Tarreau | 4213a11 | 2013-12-15 10:25:42 +0100 | [diff] [blame] | 5377 | if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN) { |
| 5378 | channel_auto_read(chn); |
| 5379 | txn->req.msg_state = HTTP_MSG_TUNNEL; |
Willy Tarreau | 4213a11 | 2013-12-15 10:25:42 +0100 | [diff] [blame] | 5380 | } |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5381 | } |
| 5382 | |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 5383 | if (chn->flags & (CF_SHUTW|CF_SHUTW_NOW)) { |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5384 | /* if we've just closed an output, let's switch */ |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 5385 | s->si[1].flags |= SI_FL_NOLINGER; /* we want to close ASAP */ |
Willy Tarreau | cce7fa4 | 2010-01-16 23:19:39 +0100 | [diff] [blame] | 5386 | |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 5387 | if (!channel_is_empty(chn)) { |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5388 | txn->req.msg_state = HTTP_MSG_CLOSING; |
| 5389 | goto http_msg_closing; |
| 5390 | } |
| 5391 | else { |
| 5392 | txn->req.msg_state = HTTP_MSG_CLOSED; |
| 5393 | goto http_msg_closed; |
| 5394 | } |
| 5395 | } |
Willy Tarreau | cce7fa4 | 2010-01-16 23:19:39 +0100 | [diff] [blame] | 5396 | goto wait_other_side; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5397 | } |
| 5398 | |
| 5399 | if (txn->req.msg_state == HTTP_MSG_CLOSING) { |
| 5400 | http_msg_closing: |
| 5401 | /* nothing else to forward, just waiting for the output buffer |
| 5402 | * to be empty and for the shutw_now to take effect. |
| 5403 | */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 5404 | if (channel_is_empty(chn)) { |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5405 | txn->req.msg_state = HTTP_MSG_CLOSED; |
| 5406 | goto http_msg_closed; |
| 5407 | } |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 5408 | else if (chn->flags & CF_SHUTW) { |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5409 | txn->req.msg_state = HTTP_MSG_ERROR; |
| 5410 | goto wait_other_side; |
| 5411 | } |
| 5412 | } |
| 5413 | |
| 5414 | if (txn->req.msg_state == HTTP_MSG_CLOSED) { |
| 5415 | http_msg_closed: |
Willy Tarreau | 3988d93 | 2013-12-27 23:03:08 +0100 | [diff] [blame] | 5416 | /* see above in MSG_DONE why we only do this in these states */ |
| 5417 | if (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_SCL) && |
| 5418 | ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) && |
| 5419 | !(s->be->options & PR_O_ABRT_CLOSE)) |
Willy Tarreau | 2e7a165 | 2013-12-15 15:32:10 +0100 | [diff] [blame] | 5420 | channel_dont_read(chn); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5421 | goto wait_other_side; |
| 5422 | } |
| 5423 | |
| 5424 | wait_other_side: |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 5425 | return txn->req.msg_state != old_state || chn->flags != old_flags; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5426 | } |
| 5427 | |
| 5428 | |
| 5429 | /* This function updates the response state machine according to the request |
| 5430 | * state machine and buffer flags. It returns 1 if it changes anything (flag |
| 5431 | * or state), otherwise zero. It ignores any state before HTTP_MSG_DONE, as |
| 5432 | * it is only used to find when a request/response couple is complete. Both |
| 5433 | * this function and its equivalent should loop until both return zero. It |
| 5434 | * can set its own state to DONE, CLOSING, CLOSED, TUNNEL, ERROR. |
| 5435 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 5436 | int http_sync_res_state(struct stream *s) |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5437 | { |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 5438 | struct channel *chn = &s->res; |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 5439 | struct http_txn *txn = s->txn; |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 5440 | unsigned int old_flags = chn->flags; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5441 | unsigned int old_state = txn->rsp.msg_state; |
| 5442 | |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5443 | if (unlikely(txn->rsp.msg_state < HTTP_MSG_BODY)) |
| 5444 | return 0; |
| 5445 | |
| 5446 | if (txn->rsp.msg_state == HTTP_MSG_DONE) { |
| 5447 | /* In theory, we don't need to read anymore, but we must |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 5448 | * still monitor the server connection for a possible close |
| 5449 | * while the request is being uploaded, so we don't disable |
| 5450 | * reading. |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5451 | */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 5452 | /* channel_dont_read(chn); */ |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5453 | |
| 5454 | if (txn->req.msg_state == HTTP_MSG_ERROR) |
| 5455 | goto wait_other_side; |
| 5456 | |
| 5457 | if (txn->req.msg_state < HTTP_MSG_DONE) { |
| 5458 | /* The client seems to still be sending data, probably |
| 5459 | * because we got an error response during an upload. |
| 5460 | * We have the choice of either breaking the connection |
| 5461 | * or letting it pass through. Let's do the later. |
| 5462 | */ |
| 5463 | goto wait_other_side; |
| 5464 | } |
| 5465 | |
| 5466 | if (txn->req.msg_state == HTTP_MSG_TUNNEL) { |
| 5467 | /* if any side switches to tunnel mode, the other one does too */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 5468 | channel_auto_read(chn); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5469 | txn->rsp.msg_state = HTTP_MSG_TUNNEL; |
Willy Tarreau | fc47f91 | 2012-10-20 10:38:09 +0200 | [diff] [blame] | 5470 | chn->flags |= CF_NEVER_WAIT; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5471 | goto wait_other_side; |
| 5472 | } |
| 5473 | |
| 5474 | /* When we get here, it means that both the request and the |
| 5475 | * response have finished receiving. Depending on the connection |
| 5476 | * mode, we'll have to wait for the last bytes to leave in either |
| 5477 | * direction, and sometimes for a close to be effective. |
| 5478 | */ |
| 5479 | |
| 5480 | if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) { |
| 5481 | /* Server-close mode : shut read and wait for the request |
| 5482 | * side to close its output buffer. The caller will detect |
| 5483 | * when we're in DONE and the other is in CLOSED and will |
| 5484 | * catch that for the final cleanup. |
| 5485 | */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 5486 | if (!(chn->flags & (CF_SHUTR|CF_SHUTR_NOW))) |
| 5487 | channel_shutr_now(chn); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5488 | } |
Willy Tarreau | cce7fa4 | 2010-01-16 23:19:39 +0100 | [diff] [blame] | 5489 | else if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) { |
| 5490 | /* Option forceclose is set, or either side wants to close, |
| 5491 | * let's enforce it now that we're not expecting any new |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 5492 | * data to come. The caller knows the stream is complete |
Willy Tarreau | cce7fa4 | 2010-01-16 23:19:39 +0100 | [diff] [blame] | 5493 | * once both states are CLOSED. |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5494 | */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 5495 | if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW))) { |
| 5496 | channel_shutr_now(chn); |
| 5497 | channel_shutw_now(chn); |
Willy Tarreau | cce7fa4 | 2010-01-16 23:19:39 +0100 | [diff] [blame] | 5498 | } |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5499 | } |
| 5500 | else { |
Willy Tarreau | 4213a11 | 2013-12-15 10:25:42 +0100 | [diff] [blame] | 5501 | /* The last possible modes are keep-alive and tunnel. Tunnel will |
| 5502 | * need to forward remaining data. Keep-alive will need to monitor |
| 5503 | * for connection closing. |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5504 | */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 5505 | channel_auto_read(chn); |
Willy Tarreau | fc47f91 | 2012-10-20 10:38:09 +0200 | [diff] [blame] | 5506 | chn->flags |= CF_NEVER_WAIT; |
Willy Tarreau | 4213a11 | 2013-12-15 10:25:42 +0100 | [diff] [blame] | 5507 | if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN) |
| 5508 | txn->rsp.msg_state = HTTP_MSG_TUNNEL; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5509 | } |
| 5510 | |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 5511 | if (chn->flags & (CF_SHUTW|CF_SHUTW_NOW)) { |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5512 | /* if we've just closed an output, let's switch */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 5513 | if (!channel_is_empty(chn)) { |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5514 | txn->rsp.msg_state = HTTP_MSG_CLOSING; |
| 5515 | goto http_msg_closing; |
| 5516 | } |
| 5517 | else { |
| 5518 | txn->rsp.msg_state = HTTP_MSG_CLOSED; |
| 5519 | goto http_msg_closed; |
| 5520 | } |
| 5521 | } |
| 5522 | goto wait_other_side; |
| 5523 | } |
| 5524 | |
| 5525 | if (txn->rsp.msg_state == HTTP_MSG_CLOSING) { |
| 5526 | http_msg_closing: |
| 5527 | /* nothing else to forward, just waiting for the output buffer |
| 5528 | * to be empty and for the shutw_now to take effect. |
| 5529 | */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 5530 | if (channel_is_empty(chn)) { |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5531 | txn->rsp.msg_state = HTTP_MSG_CLOSED; |
| 5532 | goto http_msg_closed; |
| 5533 | } |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 5534 | else if (chn->flags & CF_SHUTW) { |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5535 | txn->rsp.msg_state = HTTP_MSG_ERROR; |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 5536 | s->be->be_counters.cli_aborts++; |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 5537 | if (objt_server(s->target)) |
| 5538 | objt_server(s->target)->counters.cli_aborts++; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5539 | goto wait_other_side; |
| 5540 | } |
| 5541 | } |
| 5542 | |
| 5543 | if (txn->rsp.msg_state == HTTP_MSG_CLOSED) { |
| 5544 | http_msg_closed: |
| 5545 | /* drop any pending data */ |
Willy Tarreau | 319f745 | 2015-01-14 20:32:59 +0100 | [diff] [blame] | 5546 | channel_truncate(chn); |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 5547 | channel_auto_close(chn); |
| 5548 | channel_auto_read(chn); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5549 | goto wait_other_side; |
| 5550 | } |
| 5551 | |
| 5552 | wait_other_side: |
Willy Tarreau | fc47f91 | 2012-10-20 10:38:09 +0200 | [diff] [blame] | 5553 | /* We force the response to leave immediately if we're waiting for the |
| 5554 | * other side, since there is no pending shutdown to push it out. |
| 5555 | */ |
| 5556 | if (!channel_is_empty(chn)) |
| 5557 | chn->flags |= CF_SEND_DONTWAIT; |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 5558 | return txn->rsp.msg_state != old_state || chn->flags != old_flags; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5559 | } |
| 5560 | |
| 5561 | |
| 5562 | /* Resync the request and response state machines. Return 1 if either state |
| 5563 | * changes. |
| 5564 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 5565 | int http_resync_states(struct stream *s) |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5566 | { |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 5567 | struct http_txn *txn = s->txn; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5568 | int old_req_state = txn->req.msg_state; |
| 5569 | int old_res_state = txn->rsp.msg_state; |
| 5570 | |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5571 | http_sync_req_state(s); |
| 5572 | while (1) { |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5573 | if (!http_sync_res_state(s)) |
| 5574 | break; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5575 | if (!http_sync_req_state(s)) |
| 5576 | break; |
| 5577 | } |
Willy Tarreau | 3ce10ff | 2014-04-22 08:24:38 +0200 | [diff] [blame] | 5578 | |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5579 | /* OK, both state machines agree on a compatible state. |
| 5580 | * There are a few cases we're interested in : |
| 5581 | * - HTTP_MSG_TUNNEL on either means we have to disable both analysers |
| 5582 | * - HTTP_MSG_CLOSED on both sides means we've reached the end in both |
| 5583 | * directions, so let's simply disable both analysers. |
| 5584 | * - HTTP_MSG_CLOSED on the response only means we must abort the |
| 5585 | * request. |
| 5586 | * - HTTP_MSG_CLOSED on the request and HTTP_MSG_DONE on the response |
| 5587 | * with server-close mode means we've completed one request and we |
| 5588 | * must re-initialize the server connection. |
| 5589 | */ |
| 5590 | |
| 5591 | if (txn->req.msg_state == HTTP_MSG_TUNNEL || |
| 5592 | txn->rsp.msg_state == HTTP_MSG_TUNNEL || |
| 5593 | (txn->req.msg_state == HTTP_MSG_CLOSED && |
| 5594 | txn->rsp.msg_state == HTTP_MSG_CLOSED)) { |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 5595 | s->req.analysers &= AN_FLT_END; |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 5596 | channel_auto_close(&s->req); |
| 5597 | channel_auto_read(&s->req); |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 5598 | s->res.analysers &= AN_FLT_END; |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 5599 | channel_auto_close(&s->res); |
| 5600 | channel_auto_read(&s->res); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5601 | } |
Willy Tarreau | 40f151a | 2012-12-20 12:10:09 +0100 | [diff] [blame] | 5602 | else if ((txn->req.msg_state >= HTTP_MSG_DONE && |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 5603 | (txn->rsp.msg_state == HTTP_MSG_CLOSED || (s->res.flags & CF_SHUTW))) || |
Willy Tarreau | 2fa144c | 2010-01-04 23:13:26 +0100 | [diff] [blame] | 5604 | txn->rsp.msg_state == HTTP_MSG_ERROR || |
Willy Tarreau | 40f151a | 2012-12-20 12:10:09 +0100 | [diff] [blame] | 5605 | txn->req.msg_state == HTTP_MSG_ERROR) { |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 5606 | s->res.analysers &= AN_FLT_END; |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 5607 | channel_auto_close(&s->res); |
| 5608 | channel_auto_read(&s->res); |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 5609 | s->req.analysers &= AN_FLT_END; |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 5610 | channel_abort(&s->req); |
| 5611 | channel_auto_close(&s->req); |
| 5612 | channel_auto_read(&s->req); |
| 5613 | channel_truncate(&s->req); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5614 | } |
Willy Tarreau | 4213a11 | 2013-12-15 10:25:42 +0100 | [diff] [blame] | 5615 | else if ((txn->req.msg_state == HTTP_MSG_DONE || |
| 5616 | txn->req.msg_state == HTTP_MSG_CLOSED) && |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5617 | txn->rsp.msg_state == HTTP_MSG_DONE && |
Willy Tarreau | 4213a11 | 2013-12-15 10:25:42 +0100 | [diff] [blame] | 5618 | ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL || |
| 5619 | (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL)) { |
| 5620 | /* server-close/keep-alive: terminate this transaction, |
| 5621 | * possibly killing the server connection and reinitialize |
Willy Tarreau | 3de5bd6 | 2016-05-02 16:07:07 +0200 | [diff] [blame] | 5622 | * a fresh-new transaction, but only once we're sure there's |
| 5623 | * enough room in the request and response buffer to process |
| 5624 | * another request. The request buffer must not hold any |
| 5625 | * pending output data and the request buffer must not have |
| 5626 | * output data occupying the reserve. |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5627 | */ |
Willy Tarreau | 3de5bd6 | 2016-05-02 16:07:07 +0200 | [diff] [blame] | 5628 | if (s->req.buf->o) |
| 5629 | s->req.flags |= CF_WAKE_WRITE; |
| 5630 | else if (channel_congested(&s->res)) |
| 5631 | s->res.flags |= CF_WAKE_WRITE; |
| 5632 | else |
| 5633 | http_end_txn_clean_session(s); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5634 | } |
| 5635 | |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5636 | return txn->req.msg_state != old_req_state || |
| 5637 | txn->rsp.msg_state != old_res_state; |
| 5638 | } |
| 5639 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5640 | /* This function is an analyser which forwards request body (including chunk |
| 5641 | * sizes if any). It is called as soon as we must forward, even if we forward |
| 5642 | * zero byte. The only situation where it must not be called is when we're in |
| 5643 | * tunnel mode and we want to forward till the close. It's used both to forward |
| 5644 | * remaining data and to resync after end of body. It expects the msg_state to |
| 5645 | * 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] | 5646 | * 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] | 5647 | * 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] | 5648 | * bytes of pending data + the headers if not already done. |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5649 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 5650 | 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] | 5651 | { |
Willy Tarreau | fb0afa7 | 2015-04-03 14:46:27 +0200 | [diff] [blame] | 5652 | struct session *sess = s->sess; |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 5653 | struct http_txn *txn = s->txn; |
| 5654 | struct http_msg *msg = &s->txn->req; |
Christopher Faulet | 3e34429 | 2015-11-24 16:24:13 +0100 | [diff] [blame] | 5655 | int ret; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5656 | |
Willy Tarreau | f5c8bd6 | 2010-01-04 07:10:34 +0100 | [diff] [blame] | 5657 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) |
| 5658 | return 0; |
| 5659 | |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 5660 | 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] | 5661 | ((req->flags & CF_SHUTW) && (req->to_forward || req->buf->o))) { |
Willy Tarreau | 4fe4190 | 2010-06-07 22:27:41 +0200 | [diff] [blame] | 5662 | /* Output closed while we were sending data. We must abort and |
| 5663 | * wake the other side up. |
| 5664 | */ |
| 5665 | msg->msg_state = HTTP_MSG_ERROR; |
| 5666 | http_resync_states(s); |
Willy Tarreau | 082b01c | 2010-01-02 23:58:04 +0100 | [diff] [blame] | 5667 | return 1; |
| 5668 | } |
| 5669 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5670 | /* Note that we don't have to send 100-continue back because we don't |
| 5671 | * need the data to complete our job, and it's up to the server to |
| 5672 | * decide whether to return 100, 417 or anything else in return of |
| 5673 | * an "Expect: 100-continue" header. |
| 5674 | */ |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 5675 | if (msg->msg_state == HTTP_MSG_BODY) { |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 5676 | msg->msg_state = ((msg->flags & HTTP_MSGF_TE_CHNK) |
| 5677 | ? HTTP_MSG_CHUNK_SIZE |
| 5678 | : HTTP_MSG_DATA); |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 5679 | |
| 5680 | /* TODO/filters: when http-buffer-request option is set or if a |
| 5681 | * rule on url_param exists, the first chunk size could be |
| 5682 | * already parsed. In that case, msg->next is after the chunk |
| 5683 | * size (including the CRLF after the size). So this case should |
| 5684 | * be handled to */ |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5685 | } |
| 5686 | |
Willy Tarreau | 7ba2354 | 2014-04-17 21:50:00 +0200 | [diff] [blame] | 5687 | /* Some post-connect processing might want us to refrain from starting to |
| 5688 | * forward data. Currently, the only reason for this is "balance url_param" |
| 5689 | * whichs need to parse/process the request after we've enabled forwarding. |
| 5690 | */ |
| 5691 | if (unlikely(msg->flags & HTTP_MSGF_WAIT_CONN)) { |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 5692 | if (!(s->res.flags & CF_READ_ATTACHED)) { |
Willy Tarreau | 7ba2354 | 2014-04-17 21:50:00 +0200 | [diff] [blame] | 5693 | channel_auto_connect(req); |
Willy Tarreau | 644c101 | 2014-04-30 18:11:11 +0200 | [diff] [blame] | 5694 | req->flags |= CF_WAKE_CONNECT; |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 5695 | goto missing_data_or_waiting; |
Willy Tarreau | 7ba2354 | 2014-04-17 21:50:00 +0200 | [diff] [blame] | 5696 | } |
| 5697 | msg->flags &= ~HTTP_MSGF_WAIT_CONN; |
| 5698 | } |
| 5699 | |
Willy Tarreau | 80a92c0 | 2014-03-12 10:41:13 +0100 | [diff] [blame] | 5700 | /* in most states, we should abort in case of early close */ |
| 5701 | channel_auto_close(req); |
| 5702 | |
Willy Tarreau | efdf094 | 2014-04-24 20:08:57 +0200 | [diff] [blame] | 5703 | if (req->to_forward) { |
| 5704 | /* We can't process the buffer's contents yet */ |
| 5705 | req->flags |= CF_WAKE_WRITE; |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 5706 | goto missing_data_or_waiting; |
Willy Tarreau | efdf094 | 2014-04-24 20:08:57 +0200 | [diff] [blame] | 5707 | } |
| 5708 | |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 5709 | if (msg->msg_state < HTTP_MSG_DONE) { |
| 5710 | ret = ((msg->flags & HTTP_MSGF_TE_CHNK) |
| 5711 | ? http_msg_forward_chunked_body(s, msg) |
| 5712 | : http_msg_forward_body(s, msg)); |
| 5713 | if (!ret) |
| 5714 | goto missing_data_or_waiting; |
| 5715 | if (ret < 0) |
| 5716 | goto return_bad_req; |
| 5717 | } |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 5718 | |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 5719 | /* other states, DONE...TUNNEL */ |
| 5720 | /* we don't want to forward closes on DONE except in tunnel mode. */ |
| 5721 | if ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) |
| 5722 | channel_dont_close(req); |
Willy Tarreau | 5c54c71 | 2010-07-17 08:02:58 +0200 | [diff] [blame] | 5723 | |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 5724 | ret = msg->msg_state; |
| 5725 | if (http_resync_states(s)) { |
| 5726 | /* some state changes occurred, maybe the analyser |
| 5727 | * was disabled too. */ |
| 5728 | if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) { |
| 5729 | if (req->flags & CF_SHUTW) { |
| 5730 | /* request errors are most likely due to the |
| 5731 | * server aborting the transfer. */ |
| 5732 | goto aborted_xfer; |
Willy Tarreau | 58bd8fd | 2010-09-28 14:16:41 +0200 | [diff] [blame] | 5733 | } |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 5734 | if (msg->err_pos >= 0) |
| 5735 | http_capture_bad_message(&sess->fe->invalid_req, s, msg, ret, s->be); |
| 5736 | goto return_bad_req; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5737 | } |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 5738 | return 1; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5739 | } |
| 5740 | |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 5741 | /* If "option abortonclose" is set on the backend, we want to monitor |
| 5742 | * the client's connection and forward any shutdown notification to the |
| 5743 | * server, which will decide whether to close or to go on processing the |
| 5744 | * request. We only do that in tunnel mode, and not in other modes since |
| 5745 | * it can be abused to exhaust source ports. */ |
| 5746 | if (s->be->options & PR_O_ABRT_CLOSE) { |
| 5747 | channel_auto_read(req); |
| 5748 | if ((req->flags & (CF_SHUTR|CF_READ_NULL)) && |
| 5749 | ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN)) |
| 5750 | s->si[1].flags |= SI_FL_NOLINGER; |
| 5751 | channel_auto_close(req); |
| 5752 | } |
| 5753 | else if (s->txn->meth == HTTP_METH_POST) { |
| 5754 | /* POST requests may require to read extra CRLF sent by broken |
| 5755 | * browsers and which could cause an RST to be sent upon close |
| 5756 | * on some systems (eg: Linux). */ |
| 5757 | channel_auto_read(req); |
| 5758 | } |
| 5759 | return 0; |
Willy Tarreau | bed410e | 2014-04-22 08:19:34 +0200 | [diff] [blame] | 5760 | |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 5761 | missing_data_or_waiting: |
Willy Tarreau | f5c8bd6 | 2010-01-04 07:10:34 +0100 | [diff] [blame] | 5762 | /* stop waiting for data if the input is closed before the end */ |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 5763 | if (req->flags & CF_SHUTR) { |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 5764 | if (!(s->flags & SF_ERR_MASK)) |
| 5765 | s->flags |= SF_ERR_CLICL; |
| 5766 | if (!(s->flags & SF_FINST_MASK)) { |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 5767 | if (txn->rsp.msg_state < HTTP_MSG_ERROR) |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 5768 | s->flags |= SF_FINST_H; |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 5769 | else |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 5770 | s->flags |= SF_FINST_D; |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 5771 | } |
| 5772 | |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 5773 | sess->fe->fe_counters.cli_aborts++; |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 5774 | s->be->be_counters.cli_aborts++; |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 5775 | if (objt_server(s->target)) |
| 5776 | objt_server(s->target)->counters.cli_aborts++; |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 5777 | |
| 5778 | goto return_bad_req_stats_ok; |
Willy Tarreau | 79ebac6 | 2010-06-07 13:47:49 +0200 | [diff] [blame] | 5779 | } |
Willy Tarreau | f5c8bd6 | 2010-01-04 07:10:34 +0100 | [diff] [blame] | 5780 | |
Willy Tarreau | f5c8bd6 | 2010-01-04 07:10:34 +0100 | [diff] [blame] | 5781 | /* waiting for the last bits to leave the buffer */ |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 5782 | if (req->flags & CF_SHUTW) |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 5783 | goto aborted_xfer; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5784 | |
Willy Tarreau | 92aa1fa | 2010-08-28 18:57:20 +0200 | [diff] [blame] | 5785 | /* 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] | 5786 | * 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] | 5787 | */ |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 5788 | if (msg->flags & HTTP_MSGF_TE_CHNK) |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 5789 | channel_dont_close(req); |
Willy Tarreau | 92aa1fa | 2010-08-28 18:57:20 +0200 | [diff] [blame] | 5790 | |
Willy Tarreau | 5c62092 | 2011-05-11 19:56:11 +0200 | [diff] [blame] | 5791 | /* 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] | 5792 | * 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] | 5793 | * 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] | 5794 | * modes are already handled by the stream sock layer. We must not do |
| 5795 | * this in content-length mode because it could present the MSG_MORE |
| 5796 | * flag with the last block of forwarded data, which would cause an |
| 5797 | * additional delay to be observed by the receiver. |
Willy Tarreau | 5c62092 | 2011-05-11 19:56:11 +0200 | [diff] [blame] | 5798 | */ |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 5799 | if (msg->flags & HTTP_MSGF_TE_CHNK) |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 5800 | req->flags |= CF_EXPECT_MORE; |
Willy Tarreau | 5c62092 | 2011-05-11 19:56:11 +0200 | [diff] [blame] | 5801 | |
Willy Tarreau | f5c8bd6 | 2010-01-04 07:10:34 +0100 | [diff] [blame] | 5802 | return 0; |
| 5803 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5804 | return_bad_req: /* let's centralize all bad requests */ |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 5805 | sess->fe->fe_counters.failed_req++; |
Willy Tarreau | fb0afa7 | 2015-04-03 14:46:27 +0200 | [diff] [blame] | 5806 | if (sess->listener->counters) |
| 5807 | sess->listener->counters->failed_req++; |
Willy Tarreau | bed410e | 2014-04-22 08:19:34 +0200 | [diff] [blame] | 5808 | |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 5809 | return_bad_req_stats_ok: |
| 5810 | txn->req.msg_state = HTTP_MSG_ERROR; |
| 5811 | if (txn->status) { |
| 5812 | /* Note: we don't send any error if some data were already sent */ |
Christopher Faulet | a94e5a5 | 2015-12-09 15:55:06 +0100 | [diff] [blame] | 5813 | http_reply_and_close(s, txn->status, NULL); |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 5814 | } else { |
| 5815 | txn->status = 400; |
Christopher Faulet | a94e5a5 | 2015-12-09 15:55:06 +0100 | [diff] [blame] | 5816 | http_reply_and_close(s, txn->status, http_error_message(s, HTTP_ERR_400)); |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 5817 | } |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 5818 | req->analysers &= AN_FLT_END; |
| 5819 | s->res.analysers &= AN_FLT_END; /* we're in data phase, we want to abort both directions */ |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5820 | |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 5821 | if (!(s->flags & SF_ERR_MASK)) |
| 5822 | s->flags |= SF_ERR_PRXCOND; |
| 5823 | if (!(s->flags & SF_FINST_MASK)) { |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 5824 | if (txn->rsp.msg_state < HTTP_MSG_ERROR) |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 5825 | s->flags |= SF_FINST_H; |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 5826 | else |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 5827 | s->flags |= SF_FINST_D; |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 5828 | } |
| 5829 | return 0; |
| 5830 | |
| 5831 | aborted_xfer: |
| 5832 | txn->req.msg_state = HTTP_MSG_ERROR; |
| 5833 | if (txn->status) { |
| 5834 | /* Note: we don't send any error if some data were already sent */ |
Christopher Faulet | a94e5a5 | 2015-12-09 15:55:06 +0100 | [diff] [blame] | 5835 | http_reply_and_close(s, txn->status, NULL); |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 5836 | } else { |
| 5837 | txn->status = 502; |
Christopher Faulet | a94e5a5 | 2015-12-09 15:55:06 +0100 | [diff] [blame] | 5838 | http_reply_and_close(s, txn->status, http_error_message(s, HTTP_ERR_502)); |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 5839 | } |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 5840 | req->analysers &= AN_FLT_END; |
| 5841 | s->res.analysers &= AN_FLT_END; /* we're in data phase, we want to abort both directions */ |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 5842 | |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 5843 | sess->fe->fe_counters.srv_aborts++; |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 5844 | s->be->be_counters.srv_aborts++; |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 5845 | if (objt_server(s->target)) |
| 5846 | objt_server(s->target)->counters.srv_aborts++; |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 5847 | |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 5848 | if (!(s->flags & SF_ERR_MASK)) |
| 5849 | s->flags |= SF_ERR_SRVCL; |
| 5850 | if (!(s->flags & SF_FINST_MASK)) { |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 5851 | if (txn->rsp.msg_state < HTTP_MSG_ERROR) |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 5852 | s->flags |= SF_FINST_H; |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 5853 | else |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 5854 | s->flags |= SF_FINST_D; |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 5855 | } |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5856 | return 0; |
| 5857 | } |
| 5858 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5859 | /* This stream analyser waits for a complete HTTP response. It returns 1 if the |
| 5860 | * processing can continue on next analysers, or zero if it either needs more |
| 5861 | * data or wants to immediately abort the response (eg: timeout, error, ...). It |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 5862 | * 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] | 5863 | * when it has nothing left to do, and may remove any analyser when it wants to |
| 5864 | * abort. |
Willy Tarreau | c65a3ba | 2008-08-11 23:42:50 +0200 | [diff] [blame] | 5865 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 5866 | 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] | 5867 | { |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 5868 | struct session *sess = s->sess; |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 5869 | struct http_txn *txn = s->txn; |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5870 | struct http_msg *msg = &txn->rsp; |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 5871 | struct hdr_ctx ctx; |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 5872 | int use_close_only; |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5873 | int cur_idx; |
Krzysztof Piotr Oledzki | 5fb1882 | 2009-10-13 21:14:09 +0200 | [diff] [blame] | 5874 | int n; |
Willy Tarreau | adfb856 | 2008-08-11 15:24:42 +0200 | [diff] [blame] | 5875 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 5876 | 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] | 5877 | now_ms, __FUNCTION__, |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5878 | s, |
Willy Tarreau | 3a16b2c | 2008-08-28 08:54:27 +0200 | [diff] [blame] | 5879 | rep, |
| 5880 | rep->rex, rep->wex, |
| 5881 | rep->flags, |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 5882 | rep->buf->i, |
Willy Tarreau | 3a16b2c | 2008-08-28 08:54:27 +0200 | [diff] [blame] | 5883 | rep->analysers); |
Willy Tarreau | 67f0eea | 2008-08-10 22:55:22 +0200 | [diff] [blame] | 5884 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5885 | /* |
| 5886 | * Now parse the partial (or complete) lines. |
| 5887 | * We will check the response syntax, and also join multi-line |
| 5888 | * headers. An index of all the lines will be elaborated while |
| 5889 | * parsing. |
| 5890 | * |
| 5891 | * For the parsing, we use a 28 states FSM. |
| 5892 | * |
| 5893 | * Here is the information we currently have : |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 5894 | * rep->buf->p = beginning of response |
| 5895 | * rep->buf->p + msg->eoh = end of processed headers / start of current one |
| 5896 | * rep->buf->p + rep->buf->i = end of input data |
Willy Tarreau | 2692736 | 2012-05-18 23:22:52 +0200 | [diff] [blame] | 5897 | * msg->eol = end of current header or line (LF or CRLF) |
| 5898 | * msg->next = first non-visited byte |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5899 | */ |
| 5900 | |
Willy Tarreau | 628c40c | 2014-04-24 19:11:26 +0200 | [diff] [blame] | 5901 | next_one: |
Willy Tarreau | 83e3af0 | 2009-12-28 17:39:57 +0100 | [diff] [blame] | 5902 | /* There's a protected area at the end of the buffer for rewriting |
| 5903 | * purposes. We don't want to start to parse the request if the |
| 5904 | * protected area is affected, because we may have to move processed |
| 5905 | * data later, which is much more complicated. |
| 5906 | */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 5907 | if (buffer_not_empty(rep->buf) && msg->msg_state < HTTP_MSG_ERROR) { |
Willy Tarreau | ba0902e | 2015-01-13 14:39:16 +0100 | [diff] [blame] | 5908 | if (unlikely(!channel_is_rewritable(rep))) { |
Willy Tarreau | 379357a | 2013-06-08 12:55:46 +0200 | [diff] [blame] | 5909 | /* some data has still not left the buffer, wake us once that's done */ |
| 5910 | if (rep->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_WRITE_ERROR|CF_WRITE_TIMEOUT)) |
| 5911 | goto abort_response; |
| 5912 | channel_dont_close(rep); |
| 5913 | rep->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */ |
Willy Tarreau | d7ad9f5 | 2013-12-31 17:26:25 +0100 | [diff] [blame] | 5914 | rep->flags |= CF_WAKE_WRITE; |
Willy Tarreau | 379357a | 2013-06-08 12:55:46 +0200 | [diff] [blame] | 5915 | return 0; |
Willy Tarreau | 83e3af0 | 2009-12-28 17:39:57 +0100 | [diff] [blame] | 5916 | } |
| 5917 | |
Willy Tarreau | 379357a | 2013-06-08 12:55:46 +0200 | [diff] [blame] | 5918 | if (unlikely(bi_end(rep->buf) < b_ptr(rep->buf, msg->next) || |
| 5919 | bi_end(rep->buf) > rep->buf->data + rep->buf->size - global.tune.maxrewrite)) |
| 5920 | buffer_slow_realign(rep->buf); |
| 5921 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 5922 | if (likely(msg->next < rep->buf->i)) |
Willy Tarreau | a560c21 | 2012-03-09 13:50:57 +0100 | [diff] [blame] | 5923 | http_msg_analyzer(msg, &txn->hdr_idx); |
Willy Tarreau | 83e3af0 | 2009-12-28 17:39:57 +0100 | [diff] [blame] | 5924 | } |
| 5925 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5926 | /* 1: we might have to print this header in debug mode */ |
| 5927 | if (unlikely((global.mode & MODE_DEBUG) && |
| 5928 | (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) && |
Willy Tarreau | 7d59e90 | 2014-10-21 19:36:09 +0200 | [diff] [blame] | 5929 | msg->msg_state >= HTTP_MSG_BODY)) { |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5930 | char *eol, *sol; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5931 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 5932 | sol = rep->buf->p; |
| 5933 | 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] | 5934 | debug_hdr("srvrep", s, sol, eol); |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5935 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5936 | sol += hdr_idx_first_pos(&txn->hdr_idx); |
| 5937 | cur_idx = hdr_idx_first_idx(&txn->hdr_idx); |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5938 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5939 | while (cur_idx) { |
| 5940 | eol = sol + txn->hdr_idx.v[cur_idx].len; |
| 5941 | debug_hdr("srvhdr", s, sol, eol); |
| 5942 | sol = eol + txn->hdr_idx.v[cur_idx].cr + 1; |
| 5943 | cur_idx = txn->hdr_idx.v[cur_idx].next; |
| 5944 | } |
| 5945 | } |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5946 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5947 | /* |
| 5948 | * Now we quickly check if we have found a full valid response. |
| 5949 | * If not so, we check the FD and buffer states before leaving. |
| 5950 | * A full response is indicated by the fact that we have seen |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 5951 | * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5952 | * responses are checked first. |
| 5953 | * |
| 5954 | * Depending on whether the client is still there or not, we |
| 5955 | * may send an error response back or not. Note that normally |
| 5956 | * we should only check for HTTP status there, and check I/O |
| 5957 | * errors somewhere else. |
| 5958 | */ |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5959 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 5960 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) { |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5961 | /* Invalid response */ |
| 5962 | if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) { |
| 5963 | /* we detected a parsing error. We want to archive this response |
| 5964 | * in the dedicated proxy area for later troubleshooting. |
| 5965 | */ |
| 5966 | hdr_response_bad: |
| 5967 | if (msg->msg_state == HTTP_MSG_ERROR || msg->err_pos >= 0) |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 5968 | 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] | 5969 | |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 5970 | s->be->be_counters.failed_resp++; |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 5971 | if (objt_server(s->target)) { |
| 5972 | objt_server(s->target)->counters.failed_resp++; |
| 5973 | health_adjust(objt_server(s->target), HANA_STATUS_HTTP_HDRRSP); |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 5974 | } |
Willy Tarreau | 6464841 | 2010-03-05 10:41:54 +0100 | [diff] [blame] | 5975 | abort_response: |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 5976 | channel_auto_close(rep); |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 5977 | rep->analysers &= AN_FLT_END; |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5978 | txn->status = 502; |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 5979 | s->si[1].flags |= SI_FL_NOLINGER; |
Willy Tarreau | 319f745 | 2015-01-14 20:32:59 +0100 | [diff] [blame] | 5980 | channel_truncate(rep); |
Christopher Faulet | a94e5a5 | 2015-12-09 15:55:06 +0100 | [diff] [blame] | 5981 | http_reply_and_close(s, txn->status, http_error_message(s, HTTP_ERR_502)); |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5982 | |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 5983 | if (!(s->flags & SF_ERR_MASK)) |
| 5984 | s->flags |= SF_ERR_PRXCOND; |
| 5985 | if (!(s->flags & SF_FINST_MASK)) |
| 5986 | s->flags |= SF_FINST_H; |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5987 | |
| 5988 | return 0; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 5989 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 5990 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5991 | /* too large response does not fit in buffer. */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 5992 | else if (buffer_full(rep->buf, global.tune.maxrewrite)) { |
Willy Tarreau | fec4d89 | 2011-09-02 20:04:57 +0200 | [diff] [blame] | 5993 | if (msg->err_pos < 0) |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 5994 | msg->err_pos = rep->buf->i; |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5995 | goto hdr_response_bad; |
| 5996 | } |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5997 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5998 | /* read error */ |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 5999 | else if (rep->flags & CF_READ_ERROR) { |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 6000 | if (msg->err_pos >= 0) |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 6001 | 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] | 6002 | else if (txn->flags & TX_NOT_FIRST) |
| 6003 | goto abort_keep_alive; |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 6004 | |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 6005 | s->be->be_counters.failed_resp++; |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 6006 | if (objt_server(s->target)) { |
| 6007 | objt_server(s->target)->counters.failed_resp++; |
| 6008 | health_adjust(objt_server(s->target), HANA_STATUS_HTTP_READ_ERROR); |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 6009 | } |
Willy Tarreau | 461f662 | 2008-08-15 23:43:19 +0200 | [diff] [blame] | 6010 | |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 6011 | channel_auto_close(rep); |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 6012 | rep->analysers &= AN_FLT_END; |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 6013 | txn->status = 502; |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 6014 | s->si[1].flags |= SI_FL_NOLINGER; |
Willy Tarreau | 319f745 | 2015-01-14 20:32:59 +0100 | [diff] [blame] | 6015 | channel_truncate(rep); |
Christopher Faulet | a94e5a5 | 2015-12-09 15:55:06 +0100 | [diff] [blame] | 6016 | http_reply_and_close(s, txn->status, http_error_message(s, HTTP_ERR_502)); |
Willy Tarreau | 816b979 | 2009-09-15 21:25:21 +0200 | [diff] [blame] | 6017 | |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 6018 | if (!(s->flags & SF_ERR_MASK)) |
| 6019 | s->flags |= SF_ERR_SRVCL; |
| 6020 | if (!(s->flags & SF_FINST_MASK)) |
| 6021 | s->flags |= SF_FINST_H; |
Willy Tarreau | cebf57e | 2008-08-15 18:16:37 +0200 | [diff] [blame] | 6022 | return 0; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 6023 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 6024 | |
Willy Tarreau | 6f0a7ba | 2014-06-23 15:22:31 +0200 | [diff] [blame] | 6025 | /* read timeout : return a 504 to the client. */ |
| 6026 | else if (rep->flags & CF_READ_TIMEOUT) { |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 6027 | if (msg->err_pos >= 0) |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 6028 | http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, sess->fe); |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 6029 | |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 6030 | s->be->be_counters.failed_resp++; |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 6031 | if (objt_server(s->target)) { |
| 6032 | objt_server(s->target)->counters.failed_resp++; |
| 6033 | health_adjust(objt_server(s->target), HANA_STATUS_HTTP_READ_TIMEOUT); |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 6034 | } |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 6035 | |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 6036 | channel_auto_close(rep); |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 6037 | rep->analysers &= AN_FLT_END; |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 6038 | txn->status = 504; |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 6039 | s->si[1].flags |= SI_FL_NOLINGER; |
Willy Tarreau | 319f745 | 2015-01-14 20:32:59 +0100 | [diff] [blame] | 6040 | channel_truncate(rep); |
Christopher Faulet | a94e5a5 | 2015-12-09 15:55:06 +0100 | [diff] [blame] | 6041 | http_reply_and_close(s, txn->status, http_error_message(s, HTTP_ERR_504)); |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 6042 | |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 6043 | if (!(s->flags & SF_ERR_MASK)) |
| 6044 | s->flags |= SF_ERR_SRVTO; |
| 6045 | if (!(s->flags & SF_FINST_MASK)) |
| 6046 | s->flags |= SF_FINST_H; |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 6047 | return 0; |
| 6048 | } |
Willy Tarreau | a7c5276 | 2008-08-16 18:40:18 +0200 | [diff] [blame] | 6049 | |
Willy Tarreau | f003d37 | 2012-11-26 13:35:37 +0100 | [diff] [blame] | 6050 | /* client abort with an abortonclose */ |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 6051 | 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] | 6052 | sess->fe->fe_counters.cli_aborts++; |
Willy Tarreau | f003d37 | 2012-11-26 13:35:37 +0100 | [diff] [blame] | 6053 | s->be->be_counters.cli_aborts++; |
| 6054 | if (objt_server(s->target)) |
| 6055 | objt_server(s->target)->counters.cli_aborts++; |
| 6056 | |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 6057 | rep->analysers &= AN_FLT_END; |
Willy Tarreau | f003d37 | 2012-11-26 13:35:37 +0100 | [diff] [blame] | 6058 | channel_auto_close(rep); |
| 6059 | |
| 6060 | txn->status = 400; |
Willy Tarreau | 319f745 | 2015-01-14 20:32:59 +0100 | [diff] [blame] | 6061 | channel_truncate(rep); |
Christopher Faulet | a94e5a5 | 2015-12-09 15:55:06 +0100 | [diff] [blame] | 6062 | http_reply_and_close(s, txn->status, http_error_message(s, HTTP_ERR_400)); |
Willy Tarreau | f003d37 | 2012-11-26 13:35:37 +0100 | [diff] [blame] | 6063 | |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 6064 | if (!(s->flags & SF_ERR_MASK)) |
| 6065 | s->flags |= SF_ERR_CLICL; |
| 6066 | if (!(s->flags & SF_FINST_MASK)) |
| 6067 | s->flags |= SF_FINST_H; |
Willy Tarreau | f003d37 | 2012-11-26 13:35:37 +0100 | [diff] [blame] | 6068 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 6069 | /* process_stream() will take care of the error */ |
Willy Tarreau | f003d37 | 2012-11-26 13:35:37 +0100 | [diff] [blame] | 6070 | return 0; |
| 6071 | } |
| 6072 | |
Willy Tarreau | 3b8c08a | 2011-09-02 20:16:24 +0200 | [diff] [blame] | 6073 | /* 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] | 6074 | else if (rep->flags & CF_SHUTR) { |
Willy Tarreau | 3b8c08a | 2011-09-02 20:16:24 +0200 | [diff] [blame] | 6075 | if (msg->msg_state >= HTTP_MSG_RPVER || msg->err_pos >= 0) |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 6076 | 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] | 6077 | else if (txn->flags & TX_NOT_FIRST) |
| 6078 | goto abort_keep_alive; |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 6079 | |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 6080 | s->be->be_counters.failed_resp++; |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 6081 | if (objt_server(s->target)) { |
| 6082 | objt_server(s->target)->counters.failed_resp++; |
| 6083 | health_adjust(objt_server(s->target), HANA_STATUS_HTTP_BROKEN_PIPE); |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 6084 | } |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 6085 | |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 6086 | channel_auto_close(rep); |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 6087 | rep->analysers &= AN_FLT_END; |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 6088 | txn->status = 502; |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 6089 | s->si[1].flags |= SI_FL_NOLINGER; |
Willy Tarreau | 319f745 | 2015-01-14 20:32:59 +0100 | [diff] [blame] | 6090 | channel_truncate(rep); |
Christopher Faulet | a94e5a5 | 2015-12-09 15:55:06 +0100 | [diff] [blame] | 6091 | http_reply_and_close(s, txn->status, http_error_message(s, HTTP_ERR_502)); |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 6092 | |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 6093 | if (!(s->flags & SF_ERR_MASK)) |
| 6094 | s->flags |= SF_ERR_SRVCL; |
| 6095 | if (!(s->flags & SF_FINST_MASK)) |
| 6096 | s->flags |= SF_FINST_H; |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 6097 | return 0; |
| 6098 | } |
Krzysztof Piotr Oledzki | 5fb1882 | 2009-10-13 21:14:09 +0200 | [diff] [blame] | 6099 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 6100 | /* write error to client (we don't send any message then) */ |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 6101 | else if (rep->flags & CF_WRITE_ERROR) { |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 6102 | if (msg->err_pos >= 0) |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 6103 | 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] | 6104 | else if (txn->flags & TX_NOT_FIRST) |
| 6105 | goto abort_keep_alive; |
Krzysztof Piotr Oledzki | 5fb1882 | 2009-10-13 21:14:09 +0200 | [diff] [blame] | 6106 | |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 6107 | s->be->be_counters.failed_resp++; |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 6108 | rep->analysers &= AN_FLT_END; |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 6109 | channel_auto_close(rep); |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 6110 | |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 6111 | if (!(s->flags & SF_ERR_MASK)) |
| 6112 | s->flags |= SF_ERR_CLICL; |
| 6113 | if (!(s->flags & SF_FINST_MASK)) |
| 6114 | s->flags |= SF_FINST_H; |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 6115 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 6116 | /* process_stream() will take care of the error */ |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 6117 | return 0; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 6118 | } |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 6119 | |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 6120 | channel_dont_close(rep); |
Willy Tarreau | 3f3997e | 2013-12-15 15:21:32 +0100 | [diff] [blame] | 6121 | rep->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */ |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 6122 | return 0; |
| 6123 | } |
| 6124 | |
| 6125 | /* More interesting part now : we know that we have a complete |
| 6126 | * response which at least looks like HTTP. We have an indicator |
| 6127 | * of each header's length, so we can parse them quickly. |
| 6128 | */ |
| 6129 | |
| 6130 | if (unlikely(msg->err_pos >= 0)) |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 6131 | 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] | 6132 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 6133 | /* |
| 6134 | * 1: get the status code |
| 6135 | */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 6136 | n = rep->buf->p[msg->sl.st.c] - '0'; |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 6137 | if (n < 1 || n > 5) |
| 6138 | n = 0; |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 6139 | /* when the client triggers a 4xx from the server, it's most often due |
| 6140 | * to a missing object or permission. These events should be tracked |
| 6141 | * because if they happen often, it may indicate a brute force or a |
| 6142 | * vulnerability scan. |
| 6143 | */ |
| 6144 | if (n == 4) |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 6145 | stream_inc_http_err_ctr(s); |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 6146 | |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 6147 | if (objt_server(s->target)) |
| 6148 | objt_server(s->target)->counters.p.http.rsp[n]++; |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 6149 | |
Willy Tarreau | 91852eb | 2015-05-01 13:26:00 +0200 | [diff] [blame] | 6150 | /* RFC7230#2.6 has enforced the format of the HTTP version string to be |
| 6151 | * exactly one digit "." one digit. This check may be disabled using |
| 6152 | * option accept-invalid-http-response. |
| 6153 | */ |
| 6154 | if (!(s->be->options2 & PR_O2_RSPBUG_OK)) { |
| 6155 | if (msg->sl.st.v_l != 8) { |
| 6156 | msg->err_pos = 0; |
| 6157 | goto hdr_response_bad; |
| 6158 | } |
| 6159 | |
| 6160 | if (rep->buf->p[4] != '/' || |
| 6161 | !isdigit((unsigned char)rep->buf->p[5]) || |
| 6162 | rep->buf->p[6] != '.' || |
| 6163 | !isdigit((unsigned char)rep->buf->p[7])) { |
| 6164 | msg->err_pos = 4; |
| 6165 | goto hdr_response_bad; |
| 6166 | } |
| 6167 | } |
| 6168 | |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 6169 | /* check if the response is HTTP/1.1 or above */ |
| 6170 | if ((msg->sl.st.v_l == 8) && |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 6171 | ((rep->buf->p[5] > '1') || |
| 6172 | ((rep->buf->p[5] == '1') && (rep->buf->p[7] >= '1')))) |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 6173 | msg->flags |= HTTP_MSGF_VER_11; |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 6174 | |
| 6175 | /* "connection" has not been parsed yet */ |
Willy Tarreau | 50fc777 | 2012-11-11 22:19:57 +0100 | [diff] [blame] | 6176 | 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] | 6177 | |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 6178 | /* transfer length unknown*/ |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 6179 | msg->flags &= ~HTTP_MSGF_XFER_LEN; |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 6180 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 6181 | 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] | 6182 | |
Willy Tarreau | 3965040 | 2010-03-15 19:44:39 +0100 | [diff] [blame] | 6183 | /* Adjust server's health based on status code. Note: status codes 501 |
| 6184 | * and 505 are triggered on demand by client request, so we must not |
| 6185 | * count them as server failures. |
| 6186 | */ |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 6187 | if (objt_server(s->target)) { |
Willy Tarreau | d45b3d5 | 2010-05-20 11:49:03 +0200 | [diff] [blame] | 6188 | 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] | 6189 | health_adjust(objt_server(s->target), HANA_STATUS_HTTP_OK); |
Willy Tarreau | d45b3d5 | 2010-05-20 11:49:03 +0200 | [diff] [blame] | 6190 | else |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 6191 | health_adjust(objt_server(s->target), HANA_STATUS_HTTP_STS); |
Willy Tarreau | d45b3d5 | 2010-05-20 11:49:03 +0200 | [diff] [blame] | 6192 | } |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 6193 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 6194 | /* |
| 6195 | * 2: check for cacheability. |
| 6196 | */ |
| 6197 | |
| 6198 | switch (txn->status) { |
Willy Tarreau | 628c40c | 2014-04-24 19:11:26 +0200 | [diff] [blame] | 6199 | case 100: |
| 6200 | /* |
| 6201 | * We may be facing a 100-continue response, in which case this |
| 6202 | * is not the right response, and we're waiting for the next one. |
| 6203 | * Let's allow this response to go to the client and wait for the |
| 6204 | * next one. |
| 6205 | */ |
| 6206 | hdr_idx_init(&txn->hdr_idx); |
| 6207 | msg->next -= channel_forward(rep, msg->next); |
| 6208 | msg->msg_state = HTTP_MSG_RPBEFORE; |
| 6209 | txn->status = 0; |
| 6210 | s->logs.t_data = -1; /* was not a response yet */ |
Christopher Faulet | 3e34429 | 2015-11-24 16:24:13 +0100 | [diff] [blame] | 6211 | FLT_STRM_CB(s, flt_http_reset(s, msg)); |
Willy Tarreau | 628c40c | 2014-04-24 19:11:26 +0200 | [diff] [blame] | 6212 | goto next_one; |
| 6213 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 6214 | case 200: |
| 6215 | case 203: |
| 6216 | case 206: |
| 6217 | case 300: |
| 6218 | case 301: |
| 6219 | case 410: |
| 6220 | /* RFC2616 @13.4: |
| 6221 | * "A response received with a status code of |
| 6222 | * 200, 203, 206, 300, 301 or 410 MAY be stored |
| 6223 | * by a cache (...) unless a cache-control |
| 6224 | * directive prohibits caching." |
| 6225 | * |
| 6226 | * RFC2616 @9.5: POST method : |
| 6227 | * "Responses to this method are not cacheable, |
| 6228 | * unless the response includes appropriate |
| 6229 | * Cache-Control or Expires header fields." |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 6230 | */ |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 6231 | if (likely(txn->meth != HTTP_METH_POST) && |
Willy Tarreau | 6740213 | 2012-05-31 20:40:20 +0200 | [diff] [blame] | 6232 | ((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] | 6233 | txn->flags |= TX_CACHEABLE | TX_CACHE_COOK; |
| 6234 | break; |
| 6235 | default: |
| 6236 | break; |
| 6237 | } |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 6238 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 6239 | /* |
| 6240 | * 3: we may need to capture headers |
| 6241 | */ |
| 6242 | s->logs.logwait &= ~LW_RESP; |
Willy Tarreau | cb7dd01 | 2015-04-03 22:16:32 +0200 | [diff] [blame] | 6243 | if (unlikely((s->logs.logwait & LW_RSPHDR) && s->res_cap)) |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 6244 | capture_headers(rep->buf->p, &txn->hdr_idx, |
Willy Tarreau | cb7dd01 | 2015-04-03 22:16:32 +0200 | [diff] [blame] | 6245 | s->res_cap, sess->fe->rsp_cap); |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 6246 | |
Willy Tarreau | 557f199 | 2015-05-01 10:05:17 +0200 | [diff] [blame] | 6247 | /* 4: determine the transfer-length according to RFC2616 #4.4, updated |
| 6248 | * by RFC7230#3.3.3 : |
| 6249 | * |
| 6250 | * The length of a message body is determined by one of the following |
| 6251 | * (in order of precedence): |
| 6252 | * |
| 6253 | * 1. Any response to a HEAD request and any response with a 1xx |
| 6254 | * (Informational), 204 (No Content), or 304 (Not Modified) status |
| 6255 | * code is always terminated by the first empty line after the |
| 6256 | * header fields, regardless of the header fields present in the |
| 6257 | * message, and thus cannot contain a message body. |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 6258 | * |
Willy Tarreau | 557f199 | 2015-05-01 10:05:17 +0200 | [diff] [blame] | 6259 | * 2. Any 2xx (Successful) response to a CONNECT request implies that |
| 6260 | * the connection will become a tunnel immediately after the empty |
| 6261 | * line that concludes the header fields. A client MUST ignore any |
| 6262 | * Content-Length or Transfer-Encoding header fields received in |
| 6263 | * such a message. |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 6264 | * |
Willy Tarreau | 557f199 | 2015-05-01 10:05:17 +0200 | [diff] [blame] | 6265 | * 3. If a Transfer-Encoding header field is present and the chunked |
| 6266 | * transfer coding (Section 4.1) is the final encoding, the message |
| 6267 | * body length is determined by reading and decoding the chunked |
| 6268 | * data until the transfer coding indicates the data is complete. |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 6269 | * |
Willy Tarreau | 557f199 | 2015-05-01 10:05:17 +0200 | [diff] [blame] | 6270 | * If a Transfer-Encoding header field is present in a response and |
| 6271 | * the chunked transfer coding is not the final encoding, the |
| 6272 | * message body length is determined by reading the connection until |
| 6273 | * it is closed by the server. If a Transfer-Encoding header field |
| 6274 | * is present in a request and the chunked transfer coding is not |
| 6275 | * the final encoding, the message body length cannot be determined |
| 6276 | * reliably; the server MUST respond with the 400 (Bad Request) |
| 6277 | * status code and then close the connection. |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 6278 | * |
Willy Tarreau | 557f199 | 2015-05-01 10:05:17 +0200 | [diff] [blame] | 6279 | * If a message is received with both a Transfer-Encoding and a |
| 6280 | * Content-Length header field, the Transfer-Encoding overrides the |
| 6281 | * Content-Length. Such a message might indicate an attempt to |
| 6282 | * perform request smuggling (Section 9.5) or response splitting |
| 6283 | * (Section 9.4) and ought to be handled as an error. A sender MUST |
| 6284 | * remove the received Content-Length field prior to forwarding such |
| 6285 | * a message downstream. |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 6286 | * |
Willy Tarreau | 557f199 | 2015-05-01 10:05:17 +0200 | [diff] [blame] | 6287 | * 4. If a message is received without Transfer-Encoding and with |
| 6288 | * either multiple Content-Length header fields having differing |
| 6289 | * field-values or a single Content-Length header field having an |
| 6290 | * invalid value, then the message framing is invalid and the |
| 6291 | * recipient MUST treat it as an unrecoverable error. If this is a |
| 6292 | * request message, the server MUST respond with a 400 (Bad Request) |
| 6293 | * status code and then close the connection. If this is a response |
| 6294 | * message received by a proxy, the proxy MUST close the connection |
| 6295 | * to the server, discard the received response, and send a 502 (Bad |
| 6296 | * Gateway) response to the client. If this is a response message |
| 6297 | * received by a user agent, the user agent MUST close the |
| 6298 | * connection to the server and discard the received response. |
| 6299 | * |
| 6300 | * 5. If a valid Content-Length header field is present without |
| 6301 | * Transfer-Encoding, its decimal value defines the expected message |
| 6302 | * body length in octets. If the sender closes the connection or |
| 6303 | * the recipient times out before the indicated number of octets are |
| 6304 | * received, the recipient MUST consider the message to be |
| 6305 | * incomplete and close the connection. |
| 6306 | * |
| 6307 | * 6. If this is a request message and none of the above are true, then |
| 6308 | * the message body length is zero (no message body is present). |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 6309 | * |
Willy Tarreau | 557f199 | 2015-05-01 10:05:17 +0200 | [diff] [blame] | 6310 | * 7. Otherwise, this is a response message without a declared message |
| 6311 | * body length, so the message body length is determined by the |
| 6312 | * number of octets received prior to the server closing the |
| 6313 | * connection. |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 6314 | */ |
| 6315 | |
| 6316 | /* Skip parsing if no content length is possible. The response flags |
Willy Tarreau | 124d991 | 2011-03-01 20:30:48 +0100 | [diff] [blame] | 6317 | * 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] | 6318 | * 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] | 6319 | * FIXME: should we parse anyway and return an error on chunked encoding ? |
| 6320 | */ |
| 6321 | if (txn->meth == HTTP_METH_HEAD || |
| 6322 | (txn->status >= 100 && txn->status < 200) || |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 6323 | txn->status == 204 || txn->status == 304) { |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 6324 | msg->flags |= HTTP_MSGF_XFER_LEN; |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 6325 | goto skip_content_length; |
| 6326 | } |
| 6327 | |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 6328 | use_close_only = 0; |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 6329 | ctx.idx = 0; |
Willy Tarreau | 4979d5c | 2015-05-01 10:06:30 +0200 | [diff] [blame] | 6330 | 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] | 6331 | if (ctx.vlen == 7 && strncasecmp(ctx.line + ctx.val, "chunked", 7) == 0) |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 6332 | msg->flags |= (HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN); |
| 6333 | else if (msg->flags & HTTP_MSGF_TE_CHNK) { |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 6334 | /* bad transfer-encoding (chunked followed by something else) */ |
| 6335 | use_close_only = 1; |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 6336 | msg->flags &= ~(HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN); |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 6337 | break; |
| 6338 | } |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 6339 | } |
| 6340 | |
Willy Tarreau | 1c91391 | 2015-04-30 10:57:51 +0200 | [diff] [blame] | 6341 | /* Chunked responses must have their content-length removed */ |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 6342 | ctx.idx = 0; |
Willy Tarreau | b4d0c03 | 2015-05-01 10:25:45 +0200 | [diff] [blame] | 6343 | if (use_close_only || (msg->flags & HTTP_MSGF_TE_CHNK)) { |
Willy Tarreau | 1c91391 | 2015-04-30 10:57:51 +0200 | [diff] [blame] | 6344 | while (http_find_header2("Content-Length", 14, rep->buf->p, &txn->hdr_idx, &ctx)) |
| 6345 | http_remove_header2(msg, &txn->hdr_idx, &ctx); |
| 6346 | } |
Willy Tarreau | b4d0c03 | 2015-05-01 10:25:45 +0200 | [diff] [blame] | 6347 | 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] | 6348 | signed long long cl; |
| 6349 | |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 6350 | if (!ctx.vlen) { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 6351 | msg->err_pos = ctx.line + ctx.val - rep->buf->p; |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 6352 | goto hdr_response_bad; |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 6353 | } |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 6354 | |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 6355 | if (strl2llrc(ctx.line + ctx.val, ctx.vlen, &cl)) { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 6356 | msg->err_pos = ctx.line + ctx.val - rep->buf->p; |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 6357 | goto hdr_response_bad; /* parse failure */ |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 6358 | } |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 6359 | |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 6360 | if (cl < 0) { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 6361 | msg->err_pos = ctx.line + ctx.val - rep->buf->p; |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 6362 | goto hdr_response_bad; |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 6363 | } |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 6364 | |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 6365 | if ((msg->flags & HTTP_MSGF_CNT_LEN) && (msg->chunk_len != cl)) { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 6366 | msg->err_pos = ctx.line + ctx.val - rep->buf->p; |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 6367 | goto hdr_response_bad; /* already specified, was different */ |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 6368 | } |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 6369 | |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 6370 | msg->flags |= HTTP_MSGF_CNT_LEN | HTTP_MSGF_XFER_LEN; |
Willy Tarreau | 124d991 | 2011-03-01 20:30:48 +0100 | [diff] [blame] | 6371 | msg->body_len = msg->chunk_len = cl; |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 6372 | } |
| 6373 | |
| 6374 | skip_content_length: |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 6375 | /* Now we have to check if we need to modify the Connection header. |
| 6376 | * This is more difficult on the response than it is on the request, |
| 6377 | * because we can have two different HTTP versions and we don't know |
| 6378 | * how the client will interprete a response. For instance, let's say |
| 6379 | * that the client sends a keep-alive request in HTTP/1.0 and gets an |
| 6380 | * HTTP/1.1 response without any header. Maybe it will bound itself to |
| 6381 | * HTTP/1.0 because it only knows about it, and will consider the lack |
| 6382 | * of header as a close, or maybe it knows HTTP/1.1 and can consider |
| 6383 | * the lack of header as a keep-alive. Thus we will use two flags |
| 6384 | * indicating how a request MAY be understood by the client. In case |
| 6385 | * of multiple possibilities, we'll fix the header to be explicit. If |
| 6386 | * ambiguous cases such as both close and keepalive are seen, then we |
| 6387 | * will fall back to explicit close. Note that we won't take risks with |
| 6388 | * HTTP/1.0 clients which may not necessarily understand keep-alive. |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 6389 | * See doc/internals/connection-header.txt for the complete matrix. |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 6390 | */ |
| 6391 | |
Willy Tarreau | dc008c5 | 2010-02-01 16:20:08 +0100 | [diff] [blame] | 6392 | if (unlikely((txn->meth == HTTP_METH_CONNECT && txn->status == 200) || |
| 6393 | txn->status == 101)) { |
| 6394 | /* Either we've established an explicit tunnel, or we're |
| 6395 | * switching the protocol. In both cases, we're very unlikely |
Willy Tarreau | 5843d1a | 2010-02-01 15:13:32 +0100 | [diff] [blame] | 6396 | * to understand the next protocols. We have to switch to tunnel |
| 6397 | * mode, so that we transfer the request and responses then let |
| 6398 | * this protocol pass unmodified. When we later implement specific |
| 6399 | * parsers for such protocols, we'll want to check the Upgrade |
Willy Tarreau | dc008c5 | 2010-02-01 16:20:08 +0100 | [diff] [blame] | 6400 | * header which contains information about that protocol for |
| 6401 | * responses with status 101 (eg: see RFC2817 about TLS). |
Willy Tarreau | 5843d1a | 2010-02-01 15:13:32 +0100 | [diff] [blame] | 6402 | */ |
| 6403 | txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_TUN; |
| 6404 | } |
Willy Tarreau | dc008c5 | 2010-02-01 16:20:08 +0100 | [diff] [blame] | 6405 | else if ((txn->status >= 200) && !(txn->flags & TX_HDR_CONN_PRS) && |
| 6406 | ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN || |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 6407 | ((sess->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL || |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 6408 | (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL))) { |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 6409 | int to_del = 0; |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 6410 | |
Willy Tarreau | 70dffda | 2014-01-30 03:07:23 +0100 | [diff] [blame] | 6411 | /* this situation happens when combining pretend-keepalive with httpclose. */ |
| 6412 | if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL && |
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 | 70dffda | 2014-01-30 03:07:23 +0100 | [diff] [blame] | 6415 | txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO; |
| 6416 | |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 6417 | /* on unknown transfer length, we must close */ |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 6418 | if (!(msg->flags & HTTP_MSGF_XFER_LEN) && |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 6419 | (txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) |
| 6420 | txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO; |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 6421 | |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 6422 | /* now adjust header transformations depending on current state */ |
| 6423 | if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN || |
| 6424 | (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) { |
| 6425 | to_del |= 2; /* remove "keep-alive" on any response */ |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 6426 | if (!(msg->flags & HTTP_MSGF_VER_11)) |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 6427 | to_del |= 1; /* remove "close" for HTTP/1.0 responses */ |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 6428 | } |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 6429 | else { /* SCL / KAL */ |
| 6430 | to_del |= 1; /* remove "close" on any response */ |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 6431 | if (txn->req.flags & msg->flags & HTTP_MSGF_VER_11) |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 6432 | to_del |= 2; /* remove "keep-alive" on pure 1.1 responses */ |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 6433 | } |
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 | /* Parse and remove some headers from the connection header */ |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 6436 | http_parse_connection_header(txn, msg, to_del); |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 6437 | |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 6438 | /* Some keep-alive responses are converted to Server-close if |
| 6439 | * the server wants to close. |
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 | if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL) { |
| 6442 | if ((txn->flags & TX_HDR_CONN_CLO) || |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 6443 | (!(txn->flags & TX_HDR_CONN_KAL) && !(msg->flags & HTTP_MSGF_VER_11))) |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 6444 | txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_SCL; |
Willy Tarreau | b608feb | 2010-01-02 22:47:18 +0100 | [diff] [blame] | 6445 | } |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 6446 | } |
| 6447 | |
Willy Tarreau | 7959a55 | 2013-09-23 16:44:27 +0200 | [diff] [blame] | 6448 | /* we want to have the response time before we start processing it */ |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 6449 | s->logs.t_data = tv_ms_elapsed(&s->logs.tv_accept, &now); |
Willy Tarreau | 7959a55 | 2013-09-23 16:44:27 +0200 | [diff] [blame] | 6450 | |
Willy Tarreau | f118d9f | 2014-04-24 18:26:08 +0200 | [diff] [blame] | 6451 | /* end of job, return OK */ |
| 6452 | rep->analysers &= ~an_bit; |
| 6453 | rep->analyse_exp = TICK_ETERNITY; |
| 6454 | channel_auto_close(rep); |
| 6455 | return 1; |
| 6456 | |
| 6457 | abort_keep_alive: |
| 6458 | /* A keep-alive request to the server failed on a network error. |
| 6459 | * The client is required to retry. We need to close without returning |
| 6460 | * any other information so that the client retries. |
| 6461 | */ |
| 6462 | txn->status = 0; |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 6463 | rep->analysers &= AN_FLT_END; |
| 6464 | s->req.analysers &= AN_FLT_END; |
Willy Tarreau | f118d9f | 2014-04-24 18:26:08 +0200 | [diff] [blame] | 6465 | channel_auto_close(rep); |
| 6466 | s->logs.logwait = 0; |
| 6467 | s->logs.level = 0; |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 6468 | s->res.flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */ |
Willy Tarreau | 319f745 | 2015-01-14 20:32:59 +0100 | [diff] [blame] | 6469 | channel_truncate(rep); |
Christopher Faulet | a94e5a5 | 2015-12-09 15:55:06 +0100 | [diff] [blame] | 6470 | http_reply_and_close(s, txn->status, NULL); |
Willy Tarreau | f118d9f | 2014-04-24 18:26:08 +0200 | [diff] [blame] | 6471 | return 0; |
| 6472 | } |
| 6473 | |
| 6474 | /* This function performs all the processing enabled for the current response. |
| 6475 | * 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] | 6476 | * 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] | 6477 | * other functions. It works like process_request (see indications above). |
| 6478 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 6479 | 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] | 6480 | { |
Willy Tarreau | fb0afa7 | 2015-04-03 14:46:27 +0200 | [diff] [blame] | 6481 | struct session *sess = s->sess; |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 6482 | struct http_txn *txn = s->txn; |
Willy Tarreau | f118d9f | 2014-04-24 18:26:08 +0200 | [diff] [blame] | 6483 | struct http_msg *msg = &txn->rsp; |
| 6484 | struct proxy *cur_proxy; |
| 6485 | struct cond_wordlist *wl; |
Thierry FOURNIER | 9e2ef99 | 2015-02-25 13:51:19 +0100 | [diff] [blame] | 6486 | enum rule_result ret = HTTP_RULE_RES_CONT; |
Willy Tarreau | f118d9f | 2014-04-24 18:26:08 +0200 | [diff] [blame] | 6487 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 6488 | 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] | 6489 | now_ms, __FUNCTION__, |
| 6490 | s, |
| 6491 | rep, |
| 6492 | rep->rex, rep->wex, |
| 6493 | rep->flags, |
| 6494 | rep->buf->i, |
| 6495 | rep->analysers); |
| 6496 | |
| 6497 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) /* we need more data */ |
| 6498 | return 0; |
| 6499 | |
Willy Tarreau | 70730dd | 2014-04-24 18:06:27 +0200 | [diff] [blame] | 6500 | /* The stats applet needs to adjust the Connection header but we don't |
| 6501 | * apply any filter there. |
| 6502 | */ |
Willy Tarreau | 612adb8 | 2015-03-10 15:25:54 +0100 | [diff] [blame] | 6503 | if (unlikely(objt_applet(s->target) == &http_stats_applet)) { |
| 6504 | rep->analysers &= ~an_bit; |
| 6505 | rep->analyse_exp = TICK_ETERNITY; |
Willy Tarreau | 70730dd | 2014-04-24 18:06:27 +0200 | [diff] [blame] | 6506 | goto skip_filters; |
Willy Tarreau | 612adb8 | 2015-03-10 15:25:54 +0100 | [diff] [blame] | 6507 | } |
Willy Tarreau | 70730dd | 2014-04-24 18:06:27 +0200 | [diff] [blame] | 6508 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6509 | /* |
| 6510 | * We will have to evaluate the filters. |
| 6511 | * As opposed to version 1.2, now they will be evaluated in the |
| 6512 | * filters order and not in the header order. This means that |
| 6513 | * each filter has to be validated among all headers. |
| 6514 | * |
| 6515 | * Filters are tried with ->be first, then with ->fe if it is |
| 6516 | * different from ->be. |
Thierry FOURNIER | bc4c1ac | 2015-02-25 13:36:14 +0100 | [diff] [blame] | 6517 | * |
| 6518 | * Maybe we are in resume condiion. In this case I choose the |
| 6519 | * "struct proxy" which contains the rule list matching the resume |
| 6520 | * pointer. If none of theses "struct proxy" match, I initialise |
| 6521 | * the process with the first one. |
| 6522 | * |
| 6523 | * In fact, I check only correspondance betwwen the current list |
| 6524 | * pointer and the ->fe rule list. If it doesn't match, I initialize |
| 6525 | * the loop with the ->be. |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6526 | */ |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 6527 | if (s->current_rule_list == &sess->fe->http_res_rules) |
| 6528 | cur_proxy = sess->fe; |
Thierry FOURNIER | bc4c1ac | 2015-02-25 13:36:14 +0100 | [diff] [blame] | 6529 | else |
| 6530 | cur_proxy = s->be; |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6531 | while (1) { |
| 6532 | struct proxy *rule_set = cur_proxy; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 6533 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6534 | /* evaluate http-response rules */ |
Willy Tarreau | 51d861a | 2015-05-22 17:30:48 +0200 | [diff] [blame] | 6535 | if (ret == HTTP_RULE_RES_CONT) { |
Willy Tarreau | 987e3fb | 2015-04-04 01:09:08 +0200 | [diff] [blame] | 6536 | 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] | 6537 | |
Willy Tarreau | 51d861a | 2015-05-22 17:30:48 +0200 | [diff] [blame] | 6538 | if (ret == HTTP_RULE_RES_BADREQ) |
| 6539 | goto return_srv_prx_502; |
| 6540 | |
| 6541 | if (ret == HTTP_RULE_RES_DONE) { |
| 6542 | rep->analysers &= ~an_bit; |
| 6543 | rep->analyse_exp = TICK_ETERNITY; |
| 6544 | return 1; |
| 6545 | } |
| 6546 | } |
| 6547 | |
Thierry FOURNIER | bc4c1ac | 2015-02-25 13:36:14 +0100 | [diff] [blame] | 6548 | /* we need to be called again. */ |
| 6549 | if (ret == HTTP_RULE_RES_YIELD) { |
| 6550 | channel_dont_close(rep); |
| 6551 | return 0; |
| 6552 | } |
| 6553 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6554 | /* try headers filters */ |
| 6555 | if (rule_set->rsp_exp != NULL) { |
| 6556 | if (apply_filters_to_response(s, rep, rule_set) < 0) { |
| 6557 | return_bad_resp: |
| 6558 | if (objt_server(s->target)) { |
| 6559 | objt_server(s->target)->counters.failed_resp++; |
| 6560 | health_adjust(objt_server(s->target), HANA_STATUS_HTTP_RSP); |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 6561 | } |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6562 | s->be->be_counters.failed_resp++; |
| 6563 | return_srv_prx_502: |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 6564 | rep->analysers &= AN_FLT_END; |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6565 | txn->status = 502; |
| 6566 | s->logs.t_data = -1; /* was not a valid response */ |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 6567 | s->si[1].flags |= SI_FL_NOLINGER; |
Willy Tarreau | 319f745 | 2015-01-14 20:32:59 +0100 | [diff] [blame] | 6568 | channel_truncate(rep); |
Christopher Faulet | a94e5a5 | 2015-12-09 15:55:06 +0100 | [diff] [blame] | 6569 | http_reply_and_close(s, txn->status, http_error_message(s, HTTP_ERR_502)); |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 6570 | if (!(s->flags & SF_ERR_MASK)) |
| 6571 | s->flags |= SF_ERR_PRXCOND; |
| 6572 | if (!(s->flags & SF_FINST_MASK)) |
| 6573 | s->flags |= SF_FINST_H; |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6574 | return 0; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 6575 | } |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6576 | } |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 6577 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6578 | /* has the response been denied ? */ |
| 6579 | if (txn->flags & TX_SVDENY) { |
| 6580 | if (objt_server(s->target)) |
| 6581 | objt_server(s->target)->counters.failed_secu++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 6582 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6583 | s->be->be_counters.denied_resp++; |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 6584 | sess->fe->fe_counters.denied_resp++; |
Willy Tarreau | fb0afa7 | 2015-04-03 14:46:27 +0200 | [diff] [blame] | 6585 | if (sess->listener->counters) |
| 6586 | sess->listener->counters->denied_resp++; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 6587 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6588 | goto return_srv_prx_502; |
| 6589 | } |
Willy Tarreau | 0bbc3cf | 2006-10-15 14:26:02 +0200 | [diff] [blame] | 6590 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6591 | /* add response headers from the rule sets in the same order */ |
| 6592 | list_for_each_entry(wl, &rule_set->rsp_add, list) { |
Willy Tarreau | ce730de | 2014-09-16 10:40:38 +0200 | [diff] [blame] | 6593 | if (txn->status < 200 && txn->status != 101) |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 6594 | break; |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6595 | if (wl->cond) { |
Willy Tarreau | 192252e | 2015-04-04 01:47:55 +0200 | [diff] [blame] | 6596 | 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] | 6597 | ret = acl_pass(ret); |
| 6598 | if (((struct acl_cond *)wl->cond)->pol == ACL_COND_UNLESS) |
| 6599 | ret = !ret; |
| 6600 | if (!ret) |
| 6601 | continue; |
| 6602 | } |
| 6603 | if (unlikely(http_header_add_tail(&txn->rsp, &txn->hdr_idx, wl->s) < 0)) |
| 6604 | goto return_bad_resp; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 6605 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 6606 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6607 | /* check whether we're already working on the frontend */ |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 6608 | if (cur_proxy == sess->fe) |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6609 | break; |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 6610 | cur_proxy = sess->fe; |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6611 | } |
Willy Tarreau | 63c9e5f | 2009-12-22 16:01:27 +0100 | [diff] [blame] | 6612 | |
Thierry FOURNIER | bc4c1ac | 2015-02-25 13:36:14 +0100 | [diff] [blame] | 6613 | /* After this point, this anayzer can't return yield, so we can |
| 6614 | * remove the bit corresponding to this analyzer from the list. |
| 6615 | * |
| 6616 | * Note that the intermediate returns and goto found previously |
| 6617 | * reset the analyzers. |
| 6618 | */ |
| 6619 | rep->analysers &= ~an_bit; |
| 6620 | rep->analyse_exp = TICK_ETERNITY; |
| 6621 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6622 | /* OK that's all we can do for 1xx responses */ |
Willy Tarreau | ce730de | 2014-09-16 10:40:38 +0200 | [diff] [blame] | 6623 | if (unlikely(txn->status < 200 && txn->status != 101)) |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6624 | goto skip_header_mangling; |
Willy Tarreau | 63c9e5f | 2009-12-22 16:01:27 +0100 | [diff] [blame] | 6625 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6626 | /* |
| 6627 | * Now check for a server cookie. |
| 6628 | */ |
Willy Tarreau | 53a09d5 | 2015-08-10 18:59:40 +0200 | [diff] [blame] | 6629 | if (s->be->cookie_name || sess->fe->capture_name || (s->be->options & PR_O_CHK_CACHE)) |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6630 | manage_server_side_cookies(s, rep); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 6631 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6632 | /* |
| 6633 | * Check for cache-control or pragma headers if required. |
| 6634 | */ |
Willy Tarreau | ce730de | 2014-09-16 10:40:38 +0200 | [diff] [blame] | 6635 | 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] | 6636 | check_response_for_cacheability(s, rep); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 6637 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6638 | /* |
| 6639 | * Add server cookie in the response if needed |
| 6640 | */ |
| 6641 | if (objt_server(s->target) && (s->be->ck_opts & PR_CK_INS) && |
| 6642 | !((txn->flags & TX_SCK_FOUND) && (s->be->ck_opts & PR_CK_PSV)) && |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 6643 | (!(s->flags & SF_DIRECT) || |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6644 | ((s->be->cookie_maxidle || txn->cookie_last_date) && |
| 6645 | (!txn->cookie_last_date || (txn->cookie_last_date - date.tv_sec) < 0)) || |
| 6646 | (s->be->cookie_maxlife && !txn->cookie_first_date) || // set the first_date |
| 6647 | (!s->be->cookie_maxlife && txn->cookie_first_date)) && // remove the first_date |
| 6648 | (!(s->be->ck_opts & PR_CK_POST) || (txn->meth == HTTP_METH_POST)) && |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 6649 | !(s->flags & SF_IGNORE_PRST)) { |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6650 | /* the server is known, it's not the one the client requested, or the |
| 6651 | * cookie's last seen date needs to be refreshed. We have to |
| 6652 | * insert a set-cookie here, except if we want to insert only on POST |
| 6653 | * requests and this one isn't. Note that servers which don't have cookies |
| 6654 | * (eg: some backup servers) will return a full cookie removal request. |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6655 | */ |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6656 | if (!objt_server(s->target)->cookie) { |
| 6657 | chunk_printf(&trash, |
| 6658 | "Set-Cookie: %s=; Expires=Thu, 01-Jan-1970 00:00:01 GMT; path=/", |
| 6659 | s->be->cookie_name); |
| 6660 | } |
| 6661 | else { |
| 6662 | 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] | 6663 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6664 | if (s->be->cookie_maxidle || s->be->cookie_maxlife) { |
| 6665 | /* emit last_date, which is mandatory */ |
| 6666 | trash.str[trash.len++] = COOKIE_DELIM_DATE; |
| 6667 | s30tob64((date.tv_sec+3) >> 2, trash.str + trash.len); |
| 6668 | trash.len += 5; |
Willy Tarreau | ef4f391 | 2010-10-07 21:00:29 +0200 | [diff] [blame] | 6669 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6670 | if (s->be->cookie_maxlife) { |
| 6671 | /* emit first_date, which is either the original one or |
| 6672 | * the current date. |
| 6673 | */ |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 6674 | trash.str[trash.len++] = COOKIE_DELIM_DATE; |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6675 | s30tob64(txn->cookie_first_date ? |
| 6676 | txn->cookie_first_date >> 2 : |
| 6677 | (date.tv_sec+3) >> 2, trash.str + trash.len); |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 6678 | trash.len += 5; |
Willy Tarreau | ef4f391 | 2010-10-07 21:00:29 +0200 | [diff] [blame] | 6679 | } |
Willy Tarreau | ef4f391 | 2010-10-07 21:00:29 +0200 | [diff] [blame] | 6680 | } |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6681 | chunk_appendf(&trash, "; path=/"); |
| 6682 | } |
Willy Tarreau | 4992dd2 | 2012-05-31 21:02:17 +0200 | [diff] [blame] | 6683 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6684 | if (s->be->cookie_domain) |
| 6685 | chunk_appendf(&trash, "; domain=%s", s->be->cookie_domain); |
Willy Tarreau | ef4f391 | 2010-10-07 21:00:29 +0200 | [diff] [blame] | 6686 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6687 | if (s->be->ck_opts & PR_CK_HTTPONLY) |
| 6688 | chunk_appendf(&trash, "; HttpOnly"); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 6689 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6690 | if (s->be->ck_opts & PR_CK_SECURE) |
| 6691 | chunk_appendf(&trash, "; Secure"); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 6692 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6693 | if (unlikely(http_header_add_tail2(&txn->rsp, &txn->hdr_idx, trash.str, trash.len) < 0)) |
| 6694 | goto return_bad_resp; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 6695 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6696 | txn->flags &= ~TX_SCK_MASK; |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 6697 | if (objt_server(s->target)->cookie && (s->flags & SF_DIRECT)) |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6698 | /* the server did not change, only the date was updated */ |
| 6699 | txn->flags |= TX_SCK_UPDATED; |
| 6700 | else |
| 6701 | txn->flags |= TX_SCK_INSERTED; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 6702 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6703 | /* Here, we will tell an eventual cache on the client side that we don't |
| 6704 | * want it to cache this reply because HTTP/1.0 caches also cache cookies ! |
| 6705 | * Some caches understand the correct form: 'no-cache="set-cookie"', but |
| 6706 | * 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] | 6707 | */ |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6708 | if ((s->be->ck_opts & PR_CK_NOC) && (txn->flags & TX_CACHEABLE)) { |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 6709 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6710 | txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 6711 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6712 | if (unlikely(http_header_add_tail2(&txn->rsp, &txn->hdr_idx, |
| 6713 | "Cache-control: private", 22) < 0)) |
| 6714 | goto return_bad_resp; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 6715 | } |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6716 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6717 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6718 | /* |
| 6719 | * Check if result will be cacheable with a cookie. |
| 6720 | * We'll block the response if security checks have caught |
| 6721 | * nasty things such as a cacheable cookie. |
| 6722 | */ |
| 6723 | if (((txn->flags & (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) == |
| 6724 | (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) && |
| 6725 | (s->be->options & PR_O_CHK_CACHE)) { |
| 6726 | /* we're in presence of a cacheable response containing |
| 6727 | * a set-cookie header. We'll block it as requested by |
| 6728 | * the 'checkcache' option, and send an alert. |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6729 | */ |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6730 | if (objt_server(s->target)) |
| 6731 | objt_server(s->target)->counters.failed_secu++; |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 6732 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6733 | s->be->be_counters.denied_resp++; |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 6734 | sess->fe->fe_counters.denied_resp++; |
Willy Tarreau | fb0afa7 | 2015-04-03 14:46:27 +0200 | [diff] [blame] | 6735 | if (sess->listener->counters) |
| 6736 | sess->listener->counters->denied_resp++; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6737 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6738 | Alert("Blocking cacheable cookie in response from instance %s, server %s.\n", |
| 6739 | s->be->id, objt_server(s->target) ? objt_server(s->target)->id : "<dispatch>"); |
| 6740 | send_log(s->be, LOG_ALERT, |
| 6741 | "Blocking cacheable cookie in response from instance %s, server %s.\n", |
| 6742 | s->be->id, objt_server(s->target) ? objt_server(s->target)->id : "<dispatch>"); |
| 6743 | goto return_srv_prx_502; |
| 6744 | } |
Willy Tarreau | 0394594 | 2009-12-22 16:50:27 +0100 | [diff] [blame] | 6745 | |
Willy Tarreau | 70730dd | 2014-04-24 18:06:27 +0200 | [diff] [blame] | 6746 | skip_filters: |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6747 | /* |
| 6748 | * Adjust "Connection: close" or "Connection: keep-alive" if needed. |
| 6749 | * If an "Upgrade" token is found, the header is left untouched in order |
| 6750 | * 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] | 6751 | * if anything but "Upgrade" is present in the Connection header. We don't |
| 6752 | * want to touch any 101 response either since it's switching to another |
| 6753 | * protocol. |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6754 | */ |
Willy Tarreau | ce730de | 2014-09-16 10:40:38 +0200 | [diff] [blame] | 6755 | if ((txn->status != 101) && !(txn->flags & TX_HDR_CONN_UPG) && |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6756 | (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) || |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 6757 | ((sess->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL || |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6758 | (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL))) { |
| 6759 | unsigned int want_flags = 0; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 6760 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6761 | if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL || |
| 6762 | (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) { |
| 6763 | /* we want a keep-alive response here. Keep-alive header |
| 6764 | * required if either side is not 1.1. |
| 6765 | */ |
| 6766 | if (!(txn->req.flags & msg->flags & HTTP_MSGF_VER_11)) |
| 6767 | want_flags |= TX_CON_KAL_SET; |
| 6768 | } |
| 6769 | else { |
| 6770 | /* we want a close response here. Close header required if |
| 6771 | * the server is 1.1, regardless of the client. |
| 6772 | */ |
| 6773 | if (msg->flags & HTTP_MSGF_VER_11) |
| 6774 | want_flags |= TX_CON_CLO_SET; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 6775 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6776 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6777 | if (want_flags != (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET))) |
| 6778 | http_change_connection_header(txn, msg, want_flags); |
| 6779 | } |
| 6780 | |
| 6781 | skip_header_mangling: |
Christopher Faulet | 3e34429 | 2015-11-24 16:24:13 +0100 | [diff] [blame] | 6782 | if ((msg->flags & HTTP_MSGF_XFER_LEN) || HAS_FILTERS(s) || |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 6783 | (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN) { |
| 6784 | rep->analysers &= ~AN_FLT_XFER_DATA; |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6785 | rep->analysers |= AN_RES_HTTP_XFER_BODY; |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 6786 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6787 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6788 | /* if the user wants to log as soon as possible, without counting |
| 6789 | * bytes from the server, then this is the right moment. We have |
| 6790 | * to temporarily assign bytes_out to log what we currently have. |
| 6791 | */ |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 6792 | if (!LIST_ISEMPTY(&sess->fe->logformat) && !(s->logs.logwait & LW_BYTES)) { |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6793 | s->logs.t_close = s->logs.t_data; /* to get a valid end date */ |
| 6794 | s->logs.bytes_out = txn->rsp.eoh; |
| 6795 | s->do_log(s); |
| 6796 | s->logs.bytes_out = 0; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 6797 | } |
Willy Tarreau | e3fa6e5 | 2010-01-04 22:57:43 +0100 | [diff] [blame] | 6798 | return 1; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 6799 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6800 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 6801 | /* This function is an analyser which forwards response body (including chunk |
| 6802 | * sizes if any). It is called as soon as we must forward, even if we forward |
| 6803 | * zero byte. The only situation where it must not be called is when we're in |
| 6804 | * tunnel mode and we want to forward till the close. It's used both to forward |
| 6805 | * remaining data and to resync after end of body. It expects the msg_state to |
| 6806 | * 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] | 6807 | * 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] | 6808 | * |
| 6809 | * It is capable of compressing response data both in content-length mode and |
| 6810 | * in chunked mode. The state machines follows different flows depending on |
| 6811 | * whether content-length and chunked modes are used, since there are no |
| 6812 | * trailers in content-length : |
| 6813 | * |
| 6814 | * chk-mode cl-mode |
| 6815 | * ,----- BODY -----. |
| 6816 | * / \ |
| 6817 | * V size > 0 V chk-mode |
| 6818 | * .--> SIZE -------------> DATA -------------> CRLF |
| 6819 | * | | size == 0 | last byte | |
| 6820 | * | v final crlf v inspected | |
| 6821 | * | TRAILERS -----------> DONE | |
| 6822 | * | | |
| 6823 | * `----------------------------------------------' |
| 6824 | * |
| 6825 | * Compression only happens in the DATA state, and must be flushed in final |
| 6826 | * states (TRAILERS/DONE) or when leaving on missing data. Normal forwarding |
| 6827 | * is performed at once on final states for all bytes parsed, or when leaving |
| 6828 | * on missing data. |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 6829 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 6830 | 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] | 6831 | { |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 6832 | struct session *sess = s->sess; |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 6833 | struct http_txn *txn = s->txn; |
| 6834 | struct http_msg *msg = &s->txn->rsp; |
Christopher Faulet | 3e34429 | 2015-11-24 16:24:13 +0100 | [diff] [blame] | 6835 | int ret; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 6836 | |
Willy Tarreau | f5c8bd6 | 2010-01-04 07:10:34 +0100 | [diff] [blame] | 6837 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) |
| 6838 | return 0; |
| 6839 | |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 6840 | 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] | 6841 | ((res->flags & CF_SHUTW) && (res->to_forward || res->buf->o)) || |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 6842 | !s->req.analysers) { |
Willy Tarreau | 4fe4190 | 2010-06-07 22:27:41 +0200 | [diff] [blame] | 6843 | /* Output closed while we were sending data. We must abort and |
| 6844 | * wake the other side up. |
| 6845 | */ |
| 6846 | msg->msg_state = HTTP_MSG_ERROR; |
| 6847 | http_resync_states(s); |
Willy Tarreau | 082b01c | 2010-01-02 23:58:04 +0100 | [diff] [blame] | 6848 | return 1; |
| 6849 | } |
| 6850 | |
Willy Tarreau | 4fe4190 | 2010-06-07 22:27:41 +0200 | [diff] [blame] | 6851 | /* in most states, we should abort in case of early close */ |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 6852 | channel_auto_close(res); |
Willy Tarreau | b608feb | 2010-01-02 22:47:18 +0100 | [diff] [blame] | 6853 | |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 6854 | if (msg->msg_state == HTTP_MSG_BODY) { |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 6855 | msg->msg_state = ((msg->flags & HTTP_MSGF_TE_CHNK) |
| 6856 | ? HTTP_MSG_CHUNK_SIZE |
| 6857 | : HTTP_MSG_DATA); |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 6858 | } |
| 6859 | |
Willy Tarreau | efdf094 | 2014-04-24 20:08:57 +0200 | [diff] [blame] | 6860 | if (res->to_forward) { |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 6861 | /* We can't process the buffer's contents yet */ |
Willy Tarreau | efdf094 | 2014-04-24 20:08:57 +0200 | [diff] [blame] | 6862 | res->flags |= CF_WAKE_WRITE; |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 6863 | goto missing_data_or_waiting; |
Willy Tarreau | efdf094 | 2014-04-24 20:08:57 +0200 | [diff] [blame] | 6864 | } |
| 6865 | |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 6866 | if (msg->msg_state < HTTP_MSG_DONE) { |
| 6867 | ret = ((msg->flags & HTTP_MSGF_TE_CHNK) |
| 6868 | ? http_msg_forward_chunked_body(s, msg) |
| 6869 | : http_msg_forward_body(s, msg)); |
| 6870 | if (!ret) |
| 6871 | goto missing_data_or_waiting; |
| 6872 | if (ret < 0) |
| 6873 | goto return_bad_res; |
| 6874 | } |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 6875 | |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 6876 | /* other states, DONE...TUNNEL */ |
| 6877 | /* for keep-alive we don't want to forward closes on DONE */ |
| 6878 | if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL || |
| 6879 | (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) |
| 6880 | channel_dont_close(res); |
Willy Tarreau | 3ce10ff | 2014-04-22 08:24:38 +0200 | [diff] [blame] | 6881 | |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 6882 | ret = msg->msg_state; |
| 6883 | if (http_resync_states(s)) { |
| 6884 | /* some state changes occurred, maybe the analyser was disabled |
| 6885 | * too. */ |
| 6886 | if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) { |
| 6887 | if (res->flags & CF_SHUTW) { |
| 6888 | /* response errors are most likely due to the |
| 6889 | * client aborting the transfer. */ |
| 6890 | goto aborted_xfer; |
Willy Tarreau | 5523b32 | 2009-12-29 12:05:52 +0100 | [diff] [blame] | 6891 | } |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 6892 | if (msg->err_pos >= 0) |
| 6893 | http_capture_bad_message(&s->be->invalid_rep, s, msg, ret, strm_fe(s)); |
| 6894 | goto return_bad_res; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 6895 | } |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 6896 | return 1; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 6897 | } |
Willy Tarreau | f51d03c | 2016-05-02 15:25:15 +0200 | [diff] [blame] | 6898 | return 0; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 6899 | |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 6900 | missing_data_or_waiting: |
Willy Tarreau | f003d37 | 2012-11-26 13:35:37 +0100 | [diff] [blame] | 6901 | if (res->flags & CF_SHUTW) |
| 6902 | goto aborted_xfer; |
| 6903 | |
| 6904 | /* stop waiting for data if the input is closed before the end. If the |
| 6905 | * client side was already closed, it means that the client has aborted, |
| 6906 | * so we don't want to count this as a server abort. Otherwise it's a |
| 6907 | * server abort. |
| 6908 | */ |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 6909 | if (res->flags & CF_SHUTR) { |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 6910 | if ((s->req.flags & (CF_SHUTR|CF_SHUTW)) == (CF_SHUTR|CF_SHUTW)) |
Willy Tarreau | f003d37 | 2012-11-26 13:35:37 +0100 | [diff] [blame] | 6911 | goto aborted_xfer; |
Christopher Faulet | a46bbd8 | 2015-06-19 09:00:58 +0200 | [diff] [blame] | 6912 | /* If we have some pending data, we continue the processing */ |
| 6913 | if (!buffer_pending(res->buf)) { |
| 6914 | if (!(s->flags & SF_ERR_MASK)) |
| 6915 | s->flags |= SF_ERR_SRVCL; |
| 6916 | s->be->be_counters.srv_aborts++; |
| 6917 | if (objt_server(s->target)) |
| 6918 | objt_server(s->target)->counters.srv_aborts++; |
| 6919 | goto return_bad_res_stats_ok; |
| 6920 | } |
Willy Tarreau | 40dba09 | 2010-03-04 18:14:51 +0100 | [diff] [blame] | 6921 | } |
Willy Tarreau | f5c8bd6 | 2010-01-04 07:10:34 +0100 | [diff] [blame] | 6922 | |
Willy Tarreau | 40dba09 | 2010-03-04 18:14:51 +0100 | [diff] [blame] | 6923 | /* 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] | 6924 | if (!s->req.analysers) |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 6925 | goto return_bad_res; |
| 6926 | |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 6927 | /* When TE: chunked is used, we need to get there again to parse |
| 6928 | * remaining chunks even if the server has closed, so we don't want to |
| 6929 | * set CF_DONTCLOSE. Similarly, if the body length is undefined, if |
| 6930 | * keep-alive is set on the client side or if there are filters |
| 6931 | * registered on the stream, we don't want to forward a close |
Willy Tarreau | 92aa1fa | 2010-08-28 18:57:20 +0200 | [diff] [blame] | 6932 | */ |
Christopher Faulet | 92d3638 | 2015-11-05 13:35:03 +0100 | [diff] [blame] | 6933 | if ((msg->flags & HTTP_MSGF_TE_CHNK) || !msg->body_len || |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 6934 | HAS_FILTERS(s) || |
Willy Tarreau | 92aa1fa | 2010-08-28 18:57:20 +0200 | [diff] [blame] | 6935 | (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL || |
| 6936 | (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 6937 | channel_dont_close(res); |
Willy Tarreau | 92aa1fa | 2010-08-28 18:57:20 +0200 | [diff] [blame] | 6938 | |
Willy Tarreau | 5c62092 | 2011-05-11 19:56:11 +0200 | [diff] [blame] | 6939 | /* 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] | 6940 | * 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] | 6941 | * 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] | 6942 | * modes are already handled by the stream sock layer. We must not do |
| 6943 | * this in content-length mode because it could present the MSG_MORE |
| 6944 | * flag with the last block of forwarded data, which would cause an |
| 6945 | * additional delay to be observed by the receiver. |
Willy Tarreau | 5c62092 | 2011-05-11 19:56:11 +0200 | [diff] [blame] | 6946 | */ |
Christopher Faulet | 92d3638 | 2015-11-05 13:35:03 +0100 | [diff] [blame] | 6947 | if ((msg->flags & HTTP_MSGF_TE_CHNK) || (msg->flags & HTTP_MSGF_COMPRESSING)) |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 6948 | res->flags |= CF_EXPECT_MORE; |
Willy Tarreau | 5c62092 | 2011-05-11 19:56:11 +0200 | [diff] [blame] | 6949 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 6950 | /* the stream handler will take care of timeouts and errors */ |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 6951 | return 0; |
| 6952 | |
Willy Tarreau | 40dba09 | 2010-03-04 18:14:51 +0100 | [diff] [blame] | 6953 | return_bad_res: /* let's centralize all bad responses */ |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 6954 | s->be->be_counters.failed_resp++; |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 6955 | if (objt_server(s->target)) |
| 6956 | objt_server(s->target)->counters.failed_resp++; |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 6957 | |
| 6958 | return_bad_res_stats_ok: |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 6959 | txn->rsp.msg_state = HTTP_MSG_ERROR; |
Willy Tarreau | 148d099 | 2010-01-10 10:21:21 +0100 | [diff] [blame] | 6960 | /* don't send any error message as we're in the body */ |
Christopher Faulet | a94e5a5 | 2015-12-09 15:55:06 +0100 | [diff] [blame] | 6961 | http_reply_and_close(s, txn->status, NULL); |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 6962 | res->analysers &= AN_FLT_END; |
| 6963 | s->req.analysers &= AN_FLT_END; /* we're in data phase, we want to abort both directions */ |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 6964 | if (objt_server(s->target)) |
| 6965 | health_adjust(objt_server(s->target), HANA_STATUS_HTTP_HDRRSP); |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 6966 | |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 6967 | if (!(s->flags & SF_ERR_MASK)) |
| 6968 | s->flags |= SF_ERR_PRXCOND; |
| 6969 | if (!(s->flags & SF_FINST_MASK)) |
| 6970 | s->flags |= SF_FINST_D; |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 6971 | return 0; |
| 6972 | |
| 6973 | aborted_xfer: |
| 6974 | txn->rsp.msg_state = HTTP_MSG_ERROR; |
| 6975 | /* don't send any error message as we're in the body */ |
Christopher Faulet | a94e5a5 | 2015-12-09 15:55:06 +0100 | [diff] [blame] | 6976 | http_reply_and_close(s, txn->status, NULL); |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 6977 | res->analysers &= AN_FLT_END; |
| 6978 | s->req.analysers &= AN_FLT_END; /* we're in data phase, we want to abort both directions */ |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 6979 | |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 6980 | sess->fe->fe_counters.cli_aborts++; |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 6981 | s->be->be_counters.cli_aborts++; |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 6982 | if (objt_server(s->target)) |
| 6983 | objt_server(s->target)->counters.cli_aborts++; |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 6984 | |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 6985 | if (!(s->flags & SF_ERR_MASK)) |
| 6986 | s->flags |= SF_ERR_CLICL; |
| 6987 | if (!(s->flags & SF_FINST_MASK)) |
| 6988 | s->flags |= SF_FINST_D; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 6989 | return 0; |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 6990 | } |
| 6991 | |
| 6992 | |
| 6993 | static inline int |
| 6994 | http_msg_forward_body(struct stream *s, struct http_msg *msg) |
| 6995 | { |
| 6996 | struct channel *chn = msg->chn; |
| 6997 | int ret; |
| 6998 | |
| 6999 | /* Here we have the guarantee to be in HTTP_MSG_DATA or HTTP_MSG_ENDING state */ |
| 7000 | |
| 7001 | if (msg->msg_state == HTTP_MSG_ENDING) |
| 7002 | goto ending; |
| 7003 | |
| 7004 | /* Neither content-length, nor transfer-encoding was found, so we must |
| 7005 | * read the body until the server connection is closed. In that case, we |
| 7006 | * eat data as they come. Of course, this happens for response only. */ |
| 7007 | if (!(msg->flags & HTTP_MSGF_XFER_LEN)) { |
| 7008 | unsigned long long len = (chn->buf->i - msg->next); |
| 7009 | msg->chunk_len += len; |
| 7010 | msg->body_len += len; |
| 7011 | } |
Christopher Faulet | da02e17 | 2015-12-04 09:25:05 +0100 | [diff] [blame] | 7012 | ret = FLT_STRM_DATA_CB(s, chn, flt_http_data(s, msg), |
| 7013 | /* default_ret */ MIN(msg->chunk_len, chn->buf->i - msg->next), |
| 7014 | /* on_error */ goto error); |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 7015 | msg->next += ret; |
| 7016 | msg->chunk_len -= ret; |
| 7017 | if (msg->chunk_len) { |
| 7018 | /* input empty or output full */ |
| 7019 | if (chn->buf->i > msg->next) |
| 7020 | chn->flags |= CF_WAKE_WRITE; |
| 7021 | goto missing_data_or_waiting; |
| 7022 | } |
| 7023 | |
| 7024 | if (!(msg->flags & HTTP_MSGF_XFER_LEN) && !(chn->flags & CF_SHUTR)) { |
| 7025 | /* The server still sending data */ |
| 7026 | goto missing_data_or_waiting; |
| 7027 | } |
| 7028 | msg->msg_state = HTTP_MSG_ENDING; |
| 7029 | |
| 7030 | ending: |
| 7031 | /* we may have some pending data starting at res->buf->p such as a last |
| 7032 | * chunk of data or trailers. */ |
Christopher Faulet | da02e17 | 2015-12-04 09:25:05 +0100 | [diff] [blame] | 7033 | ret = FLT_STRM_DATA_CB(s, chn, flt_http_forward_data(s, msg, msg->next), |
| 7034 | /* default_ret */ msg->next, |
| 7035 | /* on_error */ goto error); |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 7036 | b_adv(chn->buf, ret); |
| 7037 | msg->next -= ret; |
Willy Tarreau | 9962f8f | 2016-06-28 11:52:08 +0200 | [diff] [blame] | 7038 | if (unlikely(!(chn->flags & CF_WROTE_DATA) || msg->sov > 0)) |
| 7039 | msg->sov -= ret; |
Christopher Faulet | a9300a3 | 2016-06-28 15:54:44 +0200 | [diff] [blame] | 7040 | if (msg->next) |
| 7041 | goto waiting; |
Willy Tarreau | 9962f8f | 2016-06-28 11:52:08 +0200 | [diff] [blame] | 7042 | |
Christopher Faulet | da02e17 | 2015-12-04 09:25:05 +0100 | [diff] [blame] | 7043 | FLT_STRM_DATA_CB(s, chn, flt_http_end(s, msg), |
| 7044 | /* default_ret */ 1, |
| 7045 | /* on_error */ goto error, |
| 7046 | /* on_wait */ goto waiting); |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 7047 | msg->msg_state = HTTP_MSG_DONE; |
| 7048 | return 1; |
| 7049 | |
| 7050 | missing_data_or_waiting: |
| 7051 | /* we may have some pending data starting at chn->buf->p */ |
Christopher Faulet | da02e17 | 2015-12-04 09:25:05 +0100 | [diff] [blame] | 7052 | ret = FLT_STRM_DATA_CB(s, chn, flt_http_forward_data(s, msg, msg->next), |
| 7053 | /* default_ret */ msg->next, |
| 7054 | /* on_error */ goto error); |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 7055 | b_adv(chn->buf, ret); |
| 7056 | msg->next -= ret; |
| 7057 | if (!(chn->flags & CF_WROTE_DATA) || msg->sov > 0) |
| 7058 | msg->sov -= ret; |
Christopher Faulet | 75e2eb6 | 2015-12-15 10:41:47 +0100 | [diff] [blame] | 7059 | if (!HAS_DATA_FILTERS(s, chn)) |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 7060 | msg->chunk_len -= channel_forward(chn, msg->chunk_len); |
Christopher Faulet | a9300a3 | 2016-06-28 15:54:44 +0200 | [diff] [blame] | 7061 | waiting: |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 7062 | return 0; |
| 7063 | error: |
| 7064 | return -1; |
| 7065 | } |
| 7066 | |
| 7067 | static inline int |
| 7068 | http_msg_forward_chunked_body(struct stream *s, struct http_msg *msg) |
| 7069 | { |
| 7070 | struct channel *chn = msg->chn; |
| 7071 | int ret; |
| 7072 | |
| 7073 | /* Here we have the guarantee to be in one of the following state: |
| 7074 | * HTTP_MSG_DATA, HTTP_MSG_CHUNK_SIZE, HTTP_MSG_CHUNK_CRLF, |
| 7075 | * HTTP_MSG_TRAILERS or HTTP_MSG_ENDING. */ |
| 7076 | |
| 7077 | switch_states: |
| 7078 | switch (msg->msg_state) { |
| 7079 | case HTTP_MSG_DATA: |
Christopher Faulet | da02e17 | 2015-12-04 09:25:05 +0100 | [diff] [blame] | 7080 | ret = FLT_STRM_DATA_CB(s, chn, flt_http_data(s, msg), |
| 7081 | /* default_ret */ MIN(msg->chunk_len, chn->buf->i - msg->next), |
| 7082 | /* on_error */ goto error); |
| 7083 | msg->next += ret; |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 7084 | msg->chunk_len -= ret; |
| 7085 | if (msg->chunk_len) { |
| 7086 | /* input empty or output full */ |
| 7087 | if (chn->buf->i > msg->next) |
| 7088 | chn->flags |= CF_WAKE_WRITE; |
| 7089 | goto missing_data_or_waiting; |
| 7090 | } |
| 7091 | |
| 7092 | /* nothing left to forward for this chunk*/ |
| 7093 | msg->msg_state = HTTP_MSG_CHUNK_CRLF; |
| 7094 | /* fall through for HTTP_MSG_CHUNK_CRLF */ |
| 7095 | |
| 7096 | case HTTP_MSG_CHUNK_CRLF: |
| 7097 | /* we want the CRLF after the data */ |
| 7098 | ret = http_skip_chunk_crlf(msg); |
| 7099 | if (ret == 0) |
| 7100 | goto missing_data_or_waiting; |
| 7101 | if (ret < 0) |
| 7102 | goto chunk_parsing_error; |
Christopher Faulet | 113f7de | 2015-12-14 14:52:13 +0100 | [diff] [blame] | 7103 | msg->next += ret; |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 7104 | msg->msg_state = HTTP_MSG_CHUNK_SIZE; |
| 7105 | /* fall through for HTTP_MSG_CHUNK_SIZE */ |
| 7106 | |
| 7107 | case HTTP_MSG_CHUNK_SIZE: |
| 7108 | /* read the chunk size and assign it to ->chunk_len, |
| 7109 | * then set ->next to point to the body and switch to |
| 7110 | * DATA or TRAILERS state. |
| 7111 | */ |
| 7112 | ret = http_parse_chunk_size(msg); |
| 7113 | if (ret == 0) |
| 7114 | goto missing_data_or_waiting; |
| 7115 | if (ret < 0) |
| 7116 | goto chunk_parsing_error; |
Christopher Faulet | 113f7de | 2015-12-14 14:52:13 +0100 | [diff] [blame] | 7117 | msg->next += ret; |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 7118 | if (msg->chunk_len) { |
| 7119 | msg->msg_state = HTTP_MSG_DATA; |
| 7120 | goto switch_states; |
| 7121 | } |
| 7122 | msg->msg_state = HTTP_MSG_TRAILERS; |
| 7123 | /* fall through for HTTP_MSG_TRAILERS */ |
| 7124 | |
| 7125 | case HTTP_MSG_TRAILERS: |
| 7126 | ret = http_forward_trailers(msg); |
| 7127 | if (ret < 0) |
| 7128 | goto chunk_parsing_error; |
Christopher Faulet | da02e17 | 2015-12-04 09:25:05 +0100 | [diff] [blame] | 7129 | FLT_STRM_DATA_CB(s, chn, flt_http_chunk_trailers(s, msg), |
| 7130 | /* default_ret */ 1, |
| 7131 | /* on_error */ goto error); |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 7132 | msg->next += msg->sol; |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 7133 | if (!ret) |
| 7134 | goto missing_data_or_waiting; |
| 7135 | break; |
| 7136 | |
| 7137 | case HTTP_MSG_ENDING: |
| 7138 | goto ending; |
| 7139 | |
| 7140 | default: |
| 7141 | /* This should no happen in this function */ |
| 7142 | goto error; |
| 7143 | } |
| 7144 | |
| 7145 | msg->msg_state = HTTP_MSG_ENDING; |
| 7146 | ending: |
| 7147 | /* we may have some pending data starting at res->buf->p such as a last |
| 7148 | * chunk of data or trailers. */ |
Christopher Faulet | da02e17 | 2015-12-04 09:25:05 +0100 | [diff] [blame] | 7149 | ret = FLT_STRM_DATA_CB(s, chn, flt_http_forward_data(s, msg, msg->next), |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 7150 | /* default_ret */ msg->next, |
| 7151 | /* on_error */ goto error); |
| 7152 | b_adv(chn->buf, ret); |
| 7153 | msg->next -= ret; |
Willy Tarreau | 9962f8f | 2016-06-28 11:52:08 +0200 | [diff] [blame] | 7154 | if (unlikely(!(chn->flags & CF_WROTE_DATA) || msg->sov > 0)) |
| 7155 | msg->sov -= ret; |
Christopher Faulet | a9300a3 | 2016-06-28 15:54:44 +0200 | [diff] [blame] | 7156 | if (msg->next) |
| 7157 | goto waiting; |
Willy Tarreau | 9962f8f | 2016-06-28 11:52:08 +0200 | [diff] [blame] | 7158 | |
Christopher Faulet | da02e17 | 2015-12-04 09:25:05 +0100 | [diff] [blame] | 7159 | FLT_STRM_DATA_CB(s, chn, flt_http_end(s, msg), |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 7160 | /* default_ret */ 1, |
| 7161 | /* on_error */ goto error, |
| 7162 | /* on_wait */ goto waiting); |
| 7163 | msg->msg_state = HTTP_MSG_DONE; |
| 7164 | return 1; |
| 7165 | |
| 7166 | missing_data_or_waiting: |
| 7167 | /* we may have some pending data starting at chn->buf->p */ |
Christopher Faulet | da02e17 | 2015-12-04 09:25:05 +0100 | [diff] [blame] | 7168 | ret = FLT_STRM_DATA_CB(s, chn, flt_http_forward_data(s, msg, msg->next), |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 7169 | /* default_ret */ msg->next, |
| 7170 | /* on_error */ goto error); |
| 7171 | b_adv(chn->buf, ret); |
| 7172 | msg->next -= ret; |
| 7173 | if (!(chn->flags & CF_WROTE_DATA) || msg->sov > 0) |
| 7174 | msg->sov -= ret; |
Christopher Faulet | 75e2eb6 | 2015-12-15 10:41:47 +0100 | [diff] [blame] | 7175 | if (!HAS_DATA_FILTERS(s, chn)) |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 7176 | msg->chunk_len -= channel_forward(chn, msg->chunk_len); |
Christopher Faulet | a9300a3 | 2016-06-28 15:54:44 +0200 | [diff] [blame] | 7177 | waiting: |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 7178 | return 0; |
| 7179 | |
| 7180 | chunk_parsing_error: |
| 7181 | if (msg->err_pos >= 0) { |
| 7182 | if (chn->flags & CF_ISRESP) |
| 7183 | http_capture_bad_message(&s->be->invalid_rep, s, msg, |
| 7184 | msg->msg_state, strm_fe(s)); |
| 7185 | else |
| 7186 | http_capture_bad_message(&strm_fe(s)->invalid_req, s, |
| 7187 | msg, msg->msg_state, s->be); |
| 7188 | } |
| 7189 | error: |
| 7190 | return -1; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 7191 | } |
| 7192 | |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 7193 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7194 | /* Iterate the same filter through all request headers. |
| 7195 | * Returns 1 if this filter can be stopped upon return, otherwise 0. |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7196 | * Since it can manage the switch to another backend, it updates the per-proxy |
| 7197 | * DENY stats. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7198 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 7199 | 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] | 7200 | { |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7201 | char *cur_ptr, *cur_end, *cur_next; |
| 7202 | int cur_idx, old_idx, last_hdr; |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 7203 | struct http_txn *txn = s->txn; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7204 | struct hdr_idx_elem *cur_hdr; |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 7205 | int delta; |
Willy Tarreau | 0f7562b | 2007-01-07 15:46:13 +0100 | [diff] [blame] | 7206 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7207 | last_hdr = 0; |
| 7208 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 7209 | cur_next = req->buf->p + hdr_idx_first_pos(&txn->hdr_idx); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7210 | old_idx = 0; |
| 7211 | |
| 7212 | while (!last_hdr) { |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 7213 | if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT))) |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7214 | return 1; |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 7215 | else if (unlikely(txn->flags & TX_CLALLOW) && |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7216 | (exp->action == ACT_ALLOW || |
| 7217 | exp->action == ACT_DENY || |
| 7218 | exp->action == ACT_TARPIT)) |
| 7219 | return 0; |
| 7220 | |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 7221 | cur_idx = txn->hdr_idx.v[old_idx].next; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7222 | if (!cur_idx) |
| 7223 | break; |
| 7224 | |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 7225 | cur_hdr = &txn->hdr_idx.v[cur_idx]; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7226 | cur_ptr = cur_next; |
| 7227 | cur_end = cur_ptr + cur_hdr->len; |
| 7228 | cur_next = cur_end + cur_hdr->cr + 1; |
| 7229 | |
| 7230 | /* Now we have one header between cur_ptr and cur_end, |
| 7231 | * and the next header starts at cur_next. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7232 | */ |
| 7233 | |
Willy Tarreau | 15a53a4 | 2015-01-21 13:39:42 +0100 | [diff] [blame] | 7234 | 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] | 7235 | switch (exp->action) { |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7236 | case ACT_ALLOW: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 7237 | txn->flags |= TX_CLALLOW; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7238 | last_hdr = 1; |
| 7239 | break; |
| 7240 | |
| 7241 | case ACT_DENY: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 7242 | txn->flags |= TX_CLDENY; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7243 | last_hdr = 1; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7244 | break; |
| 7245 | |
| 7246 | case ACT_TARPIT: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 7247 | txn->flags |= TX_CLTARPIT; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7248 | last_hdr = 1; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7249 | break; |
| 7250 | |
| 7251 | case ACT_REPLACE: |
Sasha Pachev | c600204 | 2014-05-26 12:33:48 -0600 | [diff] [blame] | 7252 | trash.len = exp_replace(trash.str, trash.size, cur_ptr, exp->replace, pmatch); |
| 7253 | if (trash.len < 0) |
| 7254 | return -1; |
| 7255 | |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 7256 | 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] | 7257 | /* FIXME: if the user adds a newline in the replacement, the |
| 7258 | * index will not be recalculated for now, and the new line |
| 7259 | * will not be counted as a new header. |
| 7260 | */ |
| 7261 | |
| 7262 | cur_end += delta; |
| 7263 | cur_next += delta; |
| 7264 | cur_hdr->len += delta; |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 7265 | http_msg_move_end(&txn->req, delta); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7266 | break; |
| 7267 | |
| 7268 | case ACT_REMOVE: |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 7269 | delta = buffer_replace2(req->buf, cur_ptr, cur_next, NULL, 0); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7270 | cur_next += delta; |
| 7271 | |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 7272 | http_msg_move_end(&txn->req, delta); |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 7273 | txn->hdr_idx.v[old_idx].next = cur_hdr->next; |
| 7274 | txn->hdr_idx.used--; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7275 | cur_hdr->len = 0; |
| 7276 | cur_end = NULL; /* null-term has been rewritten */ |
Willy Tarreau | 26db59e | 2010-11-28 06:57:24 +0100 | [diff] [blame] | 7277 | cur_idx = old_idx; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7278 | break; |
| 7279 | |
| 7280 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7281 | } |
| 7282 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7283 | /* keep the link from this header to next one in case of later |
| 7284 | * removal of next header. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7285 | */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7286 | old_idx = cur_idx; |
| 7287 | } |
| 7288 | return 0; |
| 7289 | } |
| 7290 | |
| 7291 | |
| 7292 | /* Apply the filter to the request line. |
| 7293 | * Returns 0 if nothing has been done, 1 if the filter has been applied, |
| 7294 | * or -1 if a replacement resulted in an invalid request line. |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7295 | * Since it can manage the switch to another backend, it updates the per-proxy |
| 7296 | * DENY stats. |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7297 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 7298 | 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] | 7299 | { |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7300 | char *cur_ptr, *cur_end; |
| 7301 | int done; |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 7302 | struct http_txn *txn = s->txn; |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 7303 | int delta; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7304 | |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 7305 | if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT))) |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7306 | return 1; |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 7307 | else if (unlikely(txn->flags & TX_CLALLOW) && |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7308 | (exp->action == ACT_ALLOW || |
| 7309 | exp->action == ACT_DENY || |
| 7310 | exp->action == ACT_TARPIT)) |
| 7311 | return 0; |
| 7312 | else if (exp->action == ACT_REMOVE) |
| 7313 | return 0; |
| 7314 | |
| 7315 | done = 0; |
| 7316 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 7317 | cur_ptr = req->buf->p; |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 7318 | cur_end = cur_ptr + txn->req.sl.rq.l; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7319 | |
| 7320 | /* Now we have the request line between cur_ptr and cur_end */ |
| 7321 | |
Willy Tarreau | 15a53a4 | 2015-01-21 13:39:42 +0100 | [diff] [blame] | 7322 | 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] | 7323 | switch (exp->action) { |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7324 | case ACT_ALLOW: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 7325 | txn->flags |= TX_CLALLOW; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7326 | done = 1; |
| 7327 | break; |
Willy Tarreau | a496b60 | 2006-12-17 23:15:24 +0100 | [diff] [blame] | 7328 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7329 | case ACT_DENY: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 7330 | txn->flags |= TX_CLDENY; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7331 | done = 1; |
| 7332 | break; |
Willy Tarreau | a496b60 | 2006-12-17 23:15:24 +0100 | [diff] [blame] | 7333 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7334 | case ACT_TARPIT: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 7335 | txn->flags |= TX_CLTARPIT; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7336 | done = 1; |
| 7337 | break; |
Willy Tarreau | a496b60 | 2006-12-17 23:15:24 +0100 | [diff] [blame] | 7338 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7339 | case ACT_REPLACE: |
Sasha Pachev | c600204 | 2014-05-26 12:33:48 -0600 | [diff] [blame] | 7340 | trash.len = exp_replace(trash.str, trash.size, cur_ptr, exp->replace, pmatch); |
| 7341 | if (trash.len < 0) |
| 7342 | return -1; |
| 7343 | |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 7344 | 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] | 7345 | /* FIXME: if the user adds a newline in the replacement, the |
| 7346 | * index will not be recalculated for now, and the new line |
| 7347 | * will not be counted as a new header. |
| 7348 | */ |
Willy Tarreau | a496b60 | 2006-12-17 23:15:24 +0100 | [diff] [blame] | 7349 | |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 7350 | http_msg_move_end(&txn->req, delta); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7351 | cur_end += delta; |
Willy Tarreau | 69d8c5d | 2012-05-08 09:44:41 +0200 | [diff] [blame] | 7352 | cur_end = (char *)http_parse_reqline(&txn->req, |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7353 | HTTP_MSG_RQMETH, |
| 7354 | cur_ptr, cur_end + 1, |
| 7355 | NULL, NULL); |
| 7356 | if (unlikely(!cur_end)) |
| 7357 | return -1; |
Willy Tarreau | a496b60 | 2006-12-17 23:15:24 +0100 | [diff] [blame] | 7358 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7359 | /* we have a full request and we know that we have either a CR |
| 7360 | * or an LF at <ptr>. |
| 7361 | */ |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 7362 | txn->meth = find_http_meth(cur_ptr, txn->req.sl.rq.m_l); |
| 7363 | 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] | 7364 | /* there is no point trying this regex on headers */ |
| 7365 | return 1; |
| 7366 | } |
| 7367 | } |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7368 | return done; |
| 7369 | } |
Willy Tarreau | 97de624 | 2006-12-27 17:18:38 +0100 | [diff] [blame] | 7370 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7371 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7372 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7373 | /* |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 7374 | * 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] | 7375 | * 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] | 7376 | * unparsable request. Since it can manage the switch to another backend, it |
| 7377 | * updates the per-proxy DENY stats. |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7378 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 7379 | 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] | 7380 | { |
Willy Tarreau | 192252e | 2015-04-04 01:47:55 +0200 | [diff] [blame] | 7381 | struct session *sess = s->sess; |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 7382 | struct http_txn *txn = s->txn; |
Willy Tarreau | 6c123b1 | 2010-01-28 20:22:06 +0100 | [diff] [blame] | 7383 | struct hdr_exp *exp; |
| 7384 | |
| 7385 | for (exp = px->req_exp; exp; exp = exp->next) { |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7386 | int ret; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7387 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7388 | /* |
| 7389 | * The interleaving of transformations and verdicts |
| 7390 | * makes it difficult to decide to continue or stop |
| 7391 | * the evaluation. |
| 7392 | */ |
| 7393 | |
Willy Tarreau | 6c123b1 | 2010-01-28 20:22:06 +0100 | [diff] [blame] | 7394 | if (txn->flags & (TX_CLDENY|TX_CLTARPIT)) |
| 7395 | break; |
| 7396 | |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 7397 | if ((txn->flags & TX_CLALLOW) && |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7398 | (exp->action == ACT_ALLOW || exp->action == ACT_DENY || |
Willy Tarreau | 6c123b1 | 2010-01-28 20:22:06 +0100 | [diff] [blame] | 7399 | exp->action == ACT_TARPIT || exp->action == ACT_PASS)) |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7400 | continue; |
Willy Tarreau | 6c123b1 | 2010-01-28 20:22:06 +0100 | [diff] [blame] | 7401 | |
| 7402 | /* if this filter had a condition, evaluate it now and skip to |
| 7403 | * next filter if the condition does not match. |
| 7404 | */ |
| 7405 | if (exp->cond) { |
Willy Tarreau | 192252e | 2015-04-04 01:47:55 +0200 | [diff] [blame] | 7406 | 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] | 7407 | ret = acl_pass(ret); |
| 7408 | if (((struct acl_cond *)exp->cond)->pol == ACL_COND_UNLESS) |
| 7409 | ret = !ret; |
| 7410 | |
| 7411 | if (!ret) |
| 7412 | continue; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7413 | } |
| 7414 | |
| 7415 | /* Apply the filter to the request line. */ |
Willy Tarreau | 6c123b1 | 2010-01-28 20:22:06 +0100 | [diff] [blame] | 7416 | ret = apply_filter_to_req_line(s, req, exp); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7417 | if (unlikely(ret < 0)) |
| 7418 | return -1; |
| 7419 | |
| 7420 | if (likely(ret == 0)) { |
| 7421 | /* The filter did not match the request, it can be |
| 7422 | * iterated through all headers. |
| 7423 | */ |
Willy Tarreau | 34d4c3c | 2015-01-30 20:58:58 +0100 | [diff] [blame] | 7424 | if (unlikely(apply_filter_to_req_headers(s, req, exp) < 0)) |
| 7425 | return -1; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7426 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7427 | } |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7428 | return 0; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7429 | } |
| 7430 | |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 7431 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7432 | /* Find the end of a cookie value contained between <s> and <e>. It works the |
| 7433 | * same way as with headers above except that the semi-colon also ends a token. |
| 7434 | * See RFC2965 for more information. Note that it requires a valid header to |
| 7435 | * return a valid result. |
| 7436 | */ |
| 7437 | char *find_cookie_value_end(char *s, const char *e) |
| 7438 | { |
| 7439 | int quoted, qdpair; |
| 7440 | |
| 7441 | quoted = qdpair = 0; |
| 7442 | for (; s < e; s++) { |
| 7443 | if (qdpair) qdpair = 0; |
| 7444 | else if (quoted) { |
| 7445 | if (*s == '\\') qdpair = 1; |
| 7446 | else if (*s == '"') quoted = 0; |
| 7447 | } |
| 7448 | else if (*s == '"') quoted = 1; |
| 7449 | else if (*s == ',' || *s == ';') return s; |
| 7450 | } |
| 7451 | return s; |
| 7452 | } |
| 7453 | |
| 7454 | /* Delete a value in a header between delimiters <from> and <next> in buffer |
| 7455 | * <buf>. The number of characters displaced is returned, and the pointer to |
| 7456 | * the first delimiter is updated if required. The function tries as much as |
| 7457 | * possible to respect the following principles : |
| 7458 | * - replace <from> delimiter by the <next> one unless <from> points to a |
| 7459 | * colon, in which case <next> is simply removed |
| 7460 | * - set exactly one space character after the new first delimiter, unless |
| 7461 | * there are not enough characters in the block being moved to do so. |
| 7462 | * - remove unneeded spaces before the previous delimiter and after the new |
| 7463 | * one. |
| 7464 | * |
| 7465 | * It is the caller's responsibility to ensure that : |
| 7466 | * - <from> points to a valid delimiter or the colon ; |
| 7467 | * - <next> points to a valid delimiter or the final CR/LF ; |
| 7468 | * - there are non-space chars before <from> ; |
| 7469 | * - there is a CR/LF at or after <next>. |
| 7470 | */ |
Willy Tarreau | af81935 | 2012-08-27 22:08:00 +0200 | [diff] [blame] | 7471 | int del_hdr_value(struct buffer *buf, char **from, char *next) |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7472 | { |
| 7473 | char *prev = *from; |
| 7474 | |
| 7475 | if (*prev == ':') { |
| 7476 | /* We're removing the first value, preserve the colon and add a |
| 7477 | * space if possible. |
| 7478 | */ |
Willy Tarreau | 2235b26 | 2016-11-05 15:50:20 +0100 | [diff] [blame] | 7479 | if (!HTTP_IS_CRLF(*next)) |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7480 | next++; |
| 7481 | prev++; |
| 7482 | if (prev < next) |
| 7483 | *prev++ = ' '; |
| 7484 | |
Willy Tarreau | 2235b26 | 2016-11-05 15:50:20 +0100 | [diff] [blame] | 7485 | while (HTTP_IS_SPHT(*next)) |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7486 | next++; |
| 7487 | } else { |
| 7488 | /* Remove useless spaces before the old delimiter. */ |
Willy Tarreau | 2235b26 | 2016-11-05 15:50:20 +0100 | [diff] [blame] | 7489 | while (HTTP_IS_SPHT(*(prev-1))) |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7490 | prev--; |
| 7491 | *from = prev; |
| 7492 | |
| 7493 | /* copy the delimiter and if possible a space if we're |
| 7494 | * not at the end of the line. |
| 7495 | */ |
Willy Tarreau | 2235b26 | 2016-11-05 15:50:20 +0100 | [diff] [blame] | 7496 | if (!HTTP_IS_CRLF(*next)) { |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7497 | *prev++ = *next++; |
| 7498 | if (prev + 1 < next) |
| 7499 | *prev++ = ' '; |
Willy Tarreau | 2235b26 | 2016-11-05 15:50:20 +0100 | [diff] [blame] | 7500 | while (HTTP_IS_SPHT(*next)) |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7501 | next++; |
| 7502 | } |
| 7503 | } |
| 7504 | return buffer_replace2(buf, prev, next, NULL, 0); |
| 7505 | } |
| 7506 | |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 7507 | /* |
Willy Tarreau | 396d2c6 | 2007-11-04 19:30:00 +0100 | [diff] [blame] | 7508 | * 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] | 7509 | * desirable to call it only when needed. This code is quite complex because |
| 7510 | * of the multiple very crappy and ambiguous syntaxes we have to support. it |
| 7511 | * highly recommended not to touch this part without a good reason ! |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7512 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 7513 | void manage_client_side_cookies(struct stream *s, struct channel *req) |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7514 | { |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 7515 | struct http_txn *txn = s->txn; |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 7516 | struct session *sess = s->sess; |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7517 | int preserve_hdr; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7518 | int cur_idx, old_idx; |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7519 | char *hdr_beg, *hdr_end, *hdr_next, *del_from; |
| 7520 | char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7521 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7522 | /* Iterate through the headers, we start with the start line. */ |
Willy Tarreau | 83969f4 | 2007-01-22 08:55:47 +0100 | [diff] [blame] | 7523 | old_idx = 0; |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 7524 | hdr_next = req->buf->p + hdr_idx_first_pos(&txn->hdr_idx); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7525 | |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 7526 | while ((cur_idx = txn->hdr_idx.v[old_idx].next)) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7527 | struct hdr_idx_elem *cur_hdr; |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 7528 | int val; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7529 | |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 7530 | cur_hdr = &txn->hdr_idx.v[cur_idx]; |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7531 | hdr_beg = hdr_next; |
| 7532 | hdr_end = hdr_beg + cur_hdr->len; |
| 7533 | hdr_next = hdr_end + cur_hdr->cr + 1; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7534 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7535 | /* We have one full header between hdr_beg and hdr_end, and the |
| 7536 | * next header starts at hdr_next. We're only interested in |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7537 | * "Cookie:" headers. |
| 7538 | */ |
| 7539 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7540 | val = http_header_match2(hdr_beg, hdr_end, "Cookie", 6); |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 7541 | if (!val) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7542 | old_idx = cur_idx; |
| 7543 | continue; |
| 7544 | } |
| 7545 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7546 | del_from = NULL; /* nothing to be deleted */ |
| 7547 | preserve_hdr = 0; /* assume we may kill the whole header */ |
| 7548 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7549 | /* Now look for cookies. Conforming to RFC2109, we have to support |
| 7550 | * attributes whose name begin with a '$', and associate them with |
| 7551 | * the right cookie, if we want to delete this cookie. |
| 7552 | * So there are 3 cases for each cookie read : |
| 7553 | * 1) it's a special attribute, beginning with a '$' : ignore it. |
| 7554 | * 2) it's a server id cookie that we *MAY* want to delete : save |
| 7555 | * some pointers on it (last semi-colon, beginning of cookie...) |
| 7556 | * 3) it's an application cookie : we *MAY* have to delete a previous |
| 7557 | * "special" cookie. |
| 7558 | * At the end of loop, if a "special" cookie remains, we may have to |
| 7559 | * remove it. If no application cookie persists in the header, we |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7560 | * *MUST* delete it. |
| 7561 | * |
| 7562 | * Note: RFC2965 is unclear about the processing of spaces around |
| 7563 | * the equal sign in the ATTR=VALUE form. A careful inspection of |
| 7564 | * the RFC explicitly allows spaces before it, and not within the |
| 7565 | * tokens (attrs or values). An inspection of RFC2109 allows that |
| 7566 | * too but section 10.1.3 lets one think that spaces may be allowed |
| 7567 | * after the equal sign too, resulting in some (rare) buggy |
| 7568 | * implementations trying to do that. So let's do what servers do. |
| 7569 | * Latest ietf draft forbids spaces all around. Also, earlier RFCs |
| 7570 | * allowed quoted strings in values, with any possible character |
| 7571 | * after a backslash, including control chars and delimitors, which |
| 7572 | * causes parsing to become ambiguous. Browsers also allow spaces |
| 7573 | * within values even without quotes. |
| 7574 | * |
| 7575 | * We have to keep multiple pointers in order to support cookie |
| 7576 | * removal at the beginning, middle or end of header without |
| 7577 | * corrupting the header. All of these headers are valid : |
| 7578 | * |
| 7579 | * Cookie:NAME1=VALUE1;NAME2=VALUE2;NAME3=VALUE3\r\n |
| 7580 | * Cookie:NAME1=VALUE1;NAME2_ONLY ;NAME3=VALUE3\r\n |
| 7581 | * Cookie: NAME1 = VALUE 1 ; NAME2 = VALUE2 ; NAME3 = VALUE3\r\n |
| 7582 | * | | | | | | | | | |
| 7583 | * | | | | | | | | hdr_end <--+ |
| 7584 | * | | | | | | | +--> next |
| 7585 | * | | | | | | +----> val_end |
| 7586 | * | | | | | +-----------> val_beg |
| 7587 | * | | | | +--------------> equal |
| 7588 | * | | | +----------------> att_end |
| 7589 | * | | +---------------------> att_beg |
| 7590 | * | +--------------------------> prev |
| 7591 | * +--------------------------------> hdr_beg |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7592 | */ |
| 7593 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7594 | for (prev = hdr_beg + 6; prev < hdr_end; prev = next) { |
| 7595 | /* Iterate through all cookies on this line */ |
| 7596 | |
| 7597 | /* find att_beg */ |
| 7598 | att_beg = prev + 1; |
Willy Tarreau | 2235b26 | 2016-11-05 15:50:20 +0100 | [diff] [blame] | 7599 | while (att_beg < hdr_end && HTTP_IS_SPHT(*att_beg)) |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7600 | att_beg++; |
| 7601 | |
| 7602 | /* find att_end : this is the first character after the last non |
| 7603 | * space before the equal. It may be equal to hdr_end. |
| 7604 | */ |
| 7605 | equal = att_end = att_beg; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7606 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7607 | while (equal < hdr_end) { |
| 7608 | if (*equal == '=' || *equal == ',' || *equal == ';') |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7609 | break; |
Willy Tarreau | 2235b26 | 2016-11-05 15:50:20 +0100 | [diff] [blame] | 7610 | if (HTTP_IS_SPHT(*equal++)) |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7611 | continue; |
| 7612 | att_end = equal; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7613 | } |
| 7614 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7615 | /* here, <equal> points to '=', a delimitor or the end. <att_end> |
| 7616 | * is between <att_beg> and <equal>, both may be identical. |
| 7617 | */ |
| 7618 | |
| 7619 | /* look for end of cookie if there is an equal sign */ |
| 7620 | if (equal < hdr_end && *equal == '=') { |
| 7621 | /* look for the beginning of the value */ |
| 7622 | val_beg = equal + 1; |
Willy Tarreau | 2235b26 | 2016-11-05 15:50:20 +0100 | [diff] [blame] | 7623 | while (val_beg < hdr_end && HTTP_IS_SPHT(*val_beg)) |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7624 | val_beg++; |
| 7625 | |
| 7626 | /* find the end of the value, respecting quotes */ |
| 7627 | next = find_cookie_value_end(val_beg, hdr_end); |
| 7628 | |
| 7629 | /* make val_end point to the first white space or delimitor after the value */ |
| 7630 | val_end = next; |
Willy Tarreau | 2235b26 | 2016-11-05 15:50:20 +0100 | [diff] [blame] | 7631 | while (val_end > val_beg && HTTP_IS_SPHT(*(val_end - 1))) |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7632 | val_end--; |
| 7633 | } else { |
| 7634 | val_beg = val_end = next = equal; |
Willy Tarreau | 305ae85 | 2010-01-03 19:45:54 +0100 | [diff] [blame] | 7635 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7636 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7637 | /* We have nothing to do with attributes beginning with '$'. However, |
| 7638 | * they will automatically be removed if a header before them is removed, |
| 7639 | * since they're supposed to be linked together. |
| 7640 | */ |
| 7641 | if (*att_beg == '$') |
| 7642 | continue; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7643 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7644 | /* Ignore cookies with no equal sign */ |
| 7645 | if (equal == next) { |
| 7646 | /* This is not our cookie, so we must preserve it. But if we already |
| 7647 | * scheduled another cookie for removal, we cannot remove the |
| 7648 | * complete header, but we can remove the previous block itself. |
| 7649 | */ |
| 7650 | preserve_hdr = 1; |
| 7651 | if (del_from != NULL) { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 7652 | int delta = del_hdr_value(req->buf, &del_from, prev); |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7653 | val_end += delta; |
| 7654 | next += delta; |
| 7655 | hdr_end += delta; |
| 7656 | hdr_next += delta; |
| 7657 | cur_hdr->len += delta; |
| 7658 | http_msg_move_end(&txn->req, delta); |
| 7659 | prev = del_from; |
| 7660 | del_from = NULL; |
| 7661 | } |
| 7662 | continue; |
Willy Tarreau | 305ae85 | 2010-01-03 19:45:54 +0100 | [diff] [blame] | 7663 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7664 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7665 | /* if there are spaces around the equal sign, we need to |
| 7666 | * strip them otherwise we'll get trouble for cookie captures, |
| 7667 | * or even for rewrites. Since this happens extremely rarely, |
| 7668 | * it does not hurt performance. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7669 | */ |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7670 | if (unlikely(att_end != equal || val_beg > equal + 1)) { |
| 7671 | int stripped_before = 0; |
| 7672 | int stripped_after = 0; |
| 7673 | |
| 7674 | if (att_end != equal) { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 7675 | stripped_before = buffer_replace2(req->buf, att_end, equal, NULL, 0); |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7676 | equal += stripped_before; |
| 7677 | val_beg += stripped_before; |
| 7678 | } |
| 7679 | |
| 7680 | if (val_beg > equal + 1) { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 7681 | stripped_after = buffer_replace2(req->buf, equal + 1, val_beg, NULL, 0); |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7682 | val_beg += stripped_after; |
| 7683 | stripped_before += stripped_after; |
| 7684 | } |
| 7685 | |
| 7686 | val_end += stripped_before; |
| 7687 | next += stripped_before; |
| 7688 | hdr_end += stripped_before; |
| 7689 | hdr_next += stripped_before; |
| 7690 | cur_hdr->len += stripped_before; |
| 7691 | http_msg_move_end(&txn->req, stripped_before); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7692 | } |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7693 | /* now everything is as on the diagram above */ |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7694 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7695 | /* First, let's see if we want to capture this cookie. We check |
| 7696 | * that we don't already have a client side cookie, because we |
| 7697 | * can only capture one. Also as an optimisation, we ignore |
| 7698 | * cookies shorter than the declared name. |
| 7699 | */ |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 7700 | if (sess->fe->capture_name != NULL && txn->cli_cookie == NULL && |
| 7701 | (val_end - att_beg >= sess->fe->capture_namelen) && |
| 7702 | memcmp(att_beg, sess->fe->capture_name, sess->fe->capture_namelen) == 0) { |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7703 | int log_len = val_end - att_beg; |
| 7704 | |
| 7705 | if ((txn->cli_cookie = pool_alloc2(pool2_capture)) == NULL) { |
| 7706 | Alert("HTTP logging : out of memory.\n"); |
| 7707 | } else { |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 7708 | if (log_len > sess->fe->capture_len) |
| 7709 | log_len = sess->fe->capture_len; |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7710 | memcpy(txn->cli_cookie, att_beg, log_len); |
| 7711 | txn->cli_cookie[log_len] = 0; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7712 | } |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7713 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7714 | |
Willy Tarreau | bca9969 | 2010-10-06 19:25:55 +0200 | [diff] [blame] | 7715 | /* Persistence cookies in passive, rewrite or insert mode have the |
| 7716 | * following form : |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7717 | * |
Willy Tarreau | bca9969 | 2010-10-06 19:25:55 +0200 | [diff] [blame] | 7718 | * Cookie: NAME=SRV[|<lastseen>[|<firstseen>]] |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7719 | * |
Willy Tarreau | bca9969 | 2010-10-06 19:25:55 +0200 | [diff] [blame] | 7720 | * For cookies in prefix mode, the form is : |
| 7721 | * |
| 7722 | * Cookie: NAME=SRV~VALUE |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7723 | */ |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 7724 | if ((att_end - att_beg == s->be->cookie_len) && (s->be->cookie_name != NULL) && |
| 7725 | (memcmp(att_beg, s->be->cookie_name, att_end - att_beg) == 0)) { |
| 7726 | struct server *srv = s->be->srv; |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7727 | char *delim; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7728 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7729 | /* if we're in cookie prefix mode, we'll search the delimitor so that we |
| 7730 | * have the server ID between val_beg and delim, and the original cookie between |
| 7731 | * delim+1 and val_end. Otherwise, delim==val_end : |
| 7732 | * |
| 7733 | * Cookie: NAME=SRV; # in all but prefix modes |
| 7734 | * Cookie: NAME=SRV~OPAQUE ; # in prefix mode |
| 7735 | * | || || | |+-> next |
| 7736 | * | || || | +--> val_end |
| 7737 | * | || || +---------> delim |
| 7738 | * | || |+------------> val_beg |
| 7739 | * | || +-------------> att_end = equal |
| 7740 | * | |+-----------------> att_beg |
| 7741 | * | +------------------> prev |
| 7742 | * +-------------------------> hdr_beg |
| 7743 | */ |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7744 | |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 7745 | if (s->be->ck_opts & PR_CK_PFX) { |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7746 | for (delim = val_beg; delim < val_end; delim++) |
| 7747 | if (*delim == COOKIE_DELIM) |
| 7748 | break; |
Willy Tarreau | bca9969 | 2010-10-06 19:25:55 +0200 | [diff] [blame] | 7749 | } else { |
| 7750 | char *vbar1; |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7751 | delim = val_end; |
Willy Tarreau | bca9969 | 2010-10-06 19:25:55 +0200 | [diff] [blame] | 7752 | /* Now check if the cookie contains a date field, which would |
| 7753 | * appear after a vertical bar ('|') just after the server name |
| 7754 | * and before the delimiter. |
| 7755 | */ |
| 7756 | vbar1 = memchr(val_beg, COOKIE_DELIM_DATE, val_end - val_beg); |
| 7757 | if (vbar1) { |
| 7758 | /* OK, so left of the bar is the server's cookie and |
Willy Tarreau | f64d141 | 2010-10-07 20:06:11 +0200 | [diff] [blame] | 7759 | * right is the last seen date. It is a base64 encoded |
| 7760 | * 30-bit value representing the UNIX date since the |
| 7761 | * epoch in 4-second quantities. |
Willy Tarreau | bca9969 | 2010-10-06 19:25:55 +0200 | [diff] [blame] | 7762 | */ |
Willy Tarreau | f64d141 | 2010-10-07 20:06:11 +0200 | [diff] [blame] | 7763 | int val; |
Willy Tarreau | bca9969 | 2010-10-06 19:25:55 +0200 | [diff] [blame] | 7764 | delim = vbar1++; |
Willy Tarreau | f64d141 | 2010-10-07 20:06:11 +0200 | [diff] [blame] | 7765 | if (val_end - vbar1 >= 5) { |
| 7766 | val = b64tos30(vbar1); |
| 7767 | if (val > 0) |
| 7768 | txn->cookie_last_date = val << 2; |
| 7769 | } |
| 7770 | /* look for a second vertical bar */ |
| 7771 | vbar1 = memchr(vbar1, COOKIE_DELIM_DATE, val_end - vbar1); |
| 7772 | if (vbar1 && (val_end - vbar1 > 5)) { |
| 7773 | val = b64tos30(vbar1 + 1); |
| 7774 | if (val > 0) |
| 7775 | txn->cookie_first_date = val << 2; |
| 7776 | } |
Willy Tarreau | bca9969 | 2010-10-06 19:25:55 +0200 | [diff] [blame] | 7777 | } |
| 7778 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7779 | |
Willy Tarreau | f64d141 | 2010-10-07 20:06:11 +0200 | [diff] [blame] | 7780 | /* if the cookie has an expiration date and the proxy wants to check |
| 7781 | * it, then we do that now. We first check if the cookie is too old, |
| 7782 | * then only if it has expired. We detect strict overflow because the |
| 7783 | * time resolution here is not great (4 seconds). Cookies with dates |
| 7784 | * in the future are ignored if their offset is beyond one day. This |
| 7785 | * allows an admin to fix timezone issues without expiring everyone |
| 7786 | * and at the same time avoids keeping unwanted side effects for too |
| 7787 | * long. |
| 7788 | */ |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 7789 | if (txn->cookie_first_date && s->be->cookie_maxlife && |
| 7790 | (((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] | 7791 | ((signed)(txn->cookie_first_date - date.tv_sec) > 86400))) { |
Willy Tarreau | f64d141 | 2010-10-07 20:06:11 +0200 | [diff] [blame] | 7792 | txn->flags &= ~TX_CK_MASK; |
| 7793 | txn->flags |= TX_CK_OLD; |
| 7794 | delim = val_beg; // let's pretend we have not found the cookie |
| 7795 | txn->cookie_first_date = 0; |
| 7796 | txn->cookie_last_date = 0; |
| 7797 | } |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 7798 | else if (txn->cookie_last_date && s->be->cookie_maxidle && |
| 7799 | (((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] | 7800 | ((signed)(txn->cookie_last_date - date.tv_sec) > 86400))) { |
Willy Tarreau | f64d141 | 2010-10-07 20:06:11 +0200 | [diff] [blame] | 7801 | txn->flags &= ~TX_CK_MASK; |
| 7802 | txn->flags |= TX_CK_EXPIRED; |
| 7803 | delim = val_beg; // let's pretend we have not found the cookie |
| 7804 | txn->cookie_first_date = 0; |
| 7805 | txn->cookie_last_date = 0; |
| 7806 | } |
| 7807 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7808 | /* Here, we'll look for the first running server which supports the cookie. |
| 7809 | * This allows to share a same cookie between several servers, for example |
| 7810 | * to dedicate backup servers to specific servers only. |
| 7811 | * However, to prevent clients from sticking to cookie-less backup server |
| 7812 | * when they have incidentely learned an empty cookie, we simply ignore |
| 7813 | * empty cookies and mark them as invalid. |
| 7814 | * The same behaviour is applied when persistence must be ignored. |
| 7815 | */ |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 7816 | if ((delim == val_beg) || (s->flags & (SF_IGNORE_PRST | SF_ASSIGNED))) |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7817 | srv = NULL; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7818 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7819 | while (srv) { |
| 7820 | if (srv->cookie && (srv->cklen == delim - val_beg) && |
| 7821 | !memcmp(val_beg, srv->cookie, delim - val_beg)) { |
Willy Tarreau | 892337c | 2014-05-13 23:41:20 +0200 | [diff] [blame] | 7822 | if ((srv->state != SRV_ST_STOPPED) || |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 7823 | (s->be->options & PR_O_PERSIST) || |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 7824 | (s->flags & SF_FORCE_PRST)) { |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7825 | /* we found the server and we can use it */ |
| 7826 | txn->flags &= ~TX_CK_MASK; |
Willy Tarreau | 892337c | 2014-05-13 23:41:20 +0200 | [diff] [blame] | 7827 | 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] | 7828 | s->flags |= SF_DIRECT | SF_ASSIGNED; |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 7829 | s->target = &srv->obj_type; |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7830 | break; |
| 7831 | } else { |
| 7832 | /* we found a server, but it's down, |
| 7833 | * mark it as such and go on in case |
| 7834 | * another one is available. |
| 7835 | */ |
| 7836 | txn->flags &= ~TX_CK_MASK; |
| 7837 | txn->flags |= TX_CK_DOWN; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7838 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7839 | } |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7840 | srv = srv->next; |
| 7841 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7842 | |
Willy Tarreau | f64d141 | 2010-10-07 20:06:11 +0200 | [diff] [blame] | 7843 | 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] | 7844 | /* no server matched this cookie or we deliberately skipped it */ |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7845 | txn->flags &= ~TX_CK_MASK; |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 7846 | if ((s->flags & (SF_IGNORE_PRST | SF_ASSIGNED))) |
Willy Tarreau | c89ccb6 | 2012-04-05 21:18:22 +0200 | [diff] [blame] | 7847 | txn->flags |= TX_CK_UNUSED; |
| 7848 | else |
| 7849 | txn->flags |= TX_CK_INVALID; |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7850 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7851 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7852 | /* depending on the cookie mode, we may have to either : |
| 7853 | * - delete the complete cookie if we're in insert+indirect mode, so that |
| 7854 | * the server never sees it ; |
| 7855 | * - remove the server id from the cookie value, and tag the cookie as an |
| 7856 | * application cookie so that it does not get accidentely removed later, |
| 7857 | * if we're in cookie prefix mode |
| 7858 | */ |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 7859 | if ((s->be->ck_opts & PR_CK_PFX) && (delim != val_end)) { |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7860 | int delta; /* negative */ |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7861 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 7862 | delta = buffer_replace2(req->buf, val_beg, delim + 1, NULL, 0); |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7863 | val_end += delta; |
| 7864 | next += delta; |
| 7865 | hdr_end += delta; |
| 7866 | hdr_next += delta; |
| 7867 | cur_hdr->len += delta; |
| 7868 | http_msg_move_end(&txn->req, delta); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7869 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7870 | del_from = NULL; |
| 7871 | preserve_hdr = 1; /* we want to keep this cookie */ |
| 7872 | } |
| 7873 | else if (del_from == NULL && |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 7874 | (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] | 7875 | del_from = prev; |
| 7876 | } |
| 7877 | } else { |
| 7878 | /* This is not our cookie, so we must preserve it. But if we already |
| 7879 | * scheduled another cookie for removal, we cannot remove the |
| 7880 | * complete header, but we can remove the previous block itself. |
| 7881 | */ |
| 7882 | preserve_hdr = 1; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7883 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7884 | if (del_from != NULL) { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 7885 | int delta = del_hdr_value(req->buf, &del_from, prev); |
Willy Tarreau | b810554 | 2010-11-24 18:31:28 +0100 | [diff] [blame] | 7886 | if (att_beg >= del_from) |
| 7887 | att_beg += delta; |
| 7888 | if (att_end >= del_from) |
| 7889 | att_end += delta; |
| 7890 | val_beg += delta; |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7891 | val_end += delta; |
| 7892 | next += delta; |
| 7893 | hdr_end += delta; |
| 7894 | hdr_next += delta; |
| 7895 | cur_hdr->len += delta; |
| 7896 | http_msg_move_end(&txn->req, delta); |
| 7897 | prev = del_from; |
| 7898 | del_from = NULL; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7899 | } |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7900 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7901 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7902 | /* continue with next cookie on this header line */ |
| 7903 | att_beg = next; |
| 7904 | } /* for each cookie */ |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7905 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7906 | /* There are no more cookies on this line. |
| 7907 | * We may still have one (or several) marked for deletion at the |
| 7908 | * end of the line. We must do this now in two ways : |
| 7909 | * - if some cookies must be preserved, we only delete from the |
| 7910 | * mark to the end of line ; |
| 7911 | * - if nothing needs to be preserved, simply delete the whole header |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7912 | */ |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7913 | if (del_from) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7914 | int delta; |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7915 | if (preserve_hdr) { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 7916 | delta = del_hdr_value(req->buf, &del_from, hdr_end); |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7917 | hdr_end = del_from; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7918 | cur_hdr->len += delta; |
| 7919 | } else { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 7920 | delta = buffer_replace2(req->buf, hdr_beg, hdr_next, NULL, 0); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7921 | |
| 7922 | /* FIXME: this should be a separate function */ |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 7923 | txn->hdr_idx.v[old_idx].next = cur_hdr->next; |
| 7924 | txn->hdr_idx.used--; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7925 | cur_hdr->len = 0; |
Willy Tarreau | 26db59e | 2010-11-28 06:57:24 +0100 | [diff] [blame] | 7926 | cur_idx = old_idx; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7927 | } |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7928 | hdr_next += delta; |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 7929 | http_msg_move_end(&txn->req, delta); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7930 | } |
| 7931 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7932 | /* check next header */ |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7933 | old_idx = cur_idx; |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7934 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7935 | } |
| 7936 | |
| 7937 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7938 | /* Iterate the same filter through all response headers contained in <rtr>. |
| 7939 | * Returns 1 if this filter can be stopped upon return, otherwise 0. |
| 7940 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 7941 | 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] | 7942 | { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7943 | char *cur_ptr, *cur_end, *cur_next; |
| 7944 | int cur_idx, old_idx, last_hdr; |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 7945 | struct http_txn *txn = s->txn; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7946 | struct hdr_idx_elem *cur_hdr; |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 7947 | int delta; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7948 | |
| 7949 | last_hdr = 0; |
| 7950 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 7951 | cur_next = rtr->buf->p + hdr_idx_first_pos(&txn->hdr_idx); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7952 | old_idx = 0; |
| 7953 | |
| 7954 | while (!last_hdr) { |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 7955 | if (unlikely(txn->flags & TX_SVDENY)) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7956 | return 1; |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 7957 | else if (unlikely(txn->flags & TX_SVALLOW) && |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7958 | (exp->action == ACT_ALLOW || |
| 7959 | exp->action == ACT_DENY)) |
| 7960 | return 0; |
| 7961 | |
| 7962 | cur_idx = txn->hdr_idx.v[old_idx].next; |
| 7963 | if (!cur_idx) |
| 7964 | break; |
| 7965 | |
| 7966 | cur_hdr = &txn->hdr_idx.v[cur_idx]; |
| 7967 | cur_ptr = cur_next; |
| 7968 | cur_end = cur_ptr + cur_hdr->len; |
| 7969 | cur_next = cur_end + cur_hdr->cr + 1; |
| 7970 | |
| 7971 | /* Now we have one header between cur_ptr and cur_end, |
| 7972 | * and the next header starts at cur_next. |
| 7973 | */ |
| 7974 | |
Willy Tarreau | 15a53a4 | 2015-01-21 13:39:42 +0100 | [diff] [blame] | 7975 | 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] | 7976 | switch (exp->action) { |
| 7977 | case ACT_ALLOW: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 7978 | txn->flags |= TX_SVALLOW; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7979 | last_hdr = 1; |
| 7980 | break; |
| 7981 | |
| 7982 | case ACT_DENY: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 7983 | txn->flags |= TX_SVDENY; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7984 | last_hdr = 1; |
| 7985 | break; |
| 7986 | |
| 7987 | case ACT_REPLACE: |
Sasha Pachev | c600204 | 2014-05-26 12:33:48 -0600 | [diff] [blame] | 7988 | trash.len = exp_replace(trash.str, trash.size, cur_ptr, exp->replace, pmatch); |
| 7989 | if (trash.len < 0) |
| 7990 | return -1; |
| 7991 | |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 7992 | 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] | 7993 | /* FIXME: if the user adds a newline in the replacement, the |
| 7994 | * index will not be recalculated for now, and the new line |
| 7995 | * will not be counted as a new header. |
| 7996 | */ |
| 7997 | |
| 7998 | cur_end += delta; |
| 7999 | cur_next += delta; |
| 8000 | cur_hdr->len += delta; |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 8001 | http_msg_move_end(&txn->rsp, delta); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8002 | break; |
| 8003 | |
| 8004 | case ACT_REMOVE: |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 8005 | delta = buffer_replace2(rtr->buf, cur_ptr, cur_next, NULL, 0); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8006 | cur_next += delta; |
| 8007 | |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 8008 | http_msg_move_end(&txn->rsp, delta); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8009 | txn->hdr_idx.v[old_idx].next = cur_hdr->next; |
| 8010 | txn->hdr_idx.used--; |
| 8011 | cur_hdr->len = 0; |
| 8012 | cur_end = NULL; /* null-term has been rewritten */ |
Willy Tarreau | 26db59e | 2010-11-28 06:57:24 +0100 | [diff] [blame] | 8013 | cur_idx = old_idx; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8014 | break; |
| 8015 | |
| 8016 | } |
| 8017 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8018 | |
| 8019 | /* keep the link from this header to next one in case of later |
| 8020 | * removal of next header. |
| 8021 | */ |
| 8022 | old_idx = cur_idx; |
| 8023 | } |
| 8024 | return 0; |
| 8025 | } |
| 8026 | |
| 8027 | |
| 8028 | /* Apply the filter to the status line in the response buffer <rtr>. |
| 8029 | * Returns 0 if nothing has been done, 1 if the filter has been applied, |
| 8030 | * or -1 if a replacement resulted in an invalid status line. |
| 8031 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 8032 | 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] | 8033 | { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8034 | char *cur_ptr, *cur_end; |
| 8035 | int done; |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 8036 | struct http_txn *txn = s->txn; |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 8037 | int delta; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8038 | |
| 8039 | |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 8040 | if (unlikely(txn->flags & TX_SVDENY)) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8041 | return 1; |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 8042 | else if (unlikely(txn->flags & TX_SVALLOW) && |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8043 | (exp->action == ACT_ALLOW || |
| 8044 | exp->action == ACT_DENY)) |
| 8045 | return 0; |
| 8046 | else if (exp->action == ACT_REMOVE) |
| 8047 | return 0; |
| 8048 | |
| 8049 | done = 0; |
| 8050 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 8051 | cur_ptr = rtr->buf->p; |
Willy Tarreau | 1ba0e5f | 2010-06-07 13:57:32 +0200 | [diff] [blame] | 8052 | cur_end = cur_ptr + txn->rsp.sl.st.l; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8053 | |
| 8054 | /* Now we have the status line between cur_ptr and cur_end */ |
| 8055 | |
Willy Tarreau | 15a53a4 | 2015-01-21 13:39:42 +0100 | [diff] [blame] | 8056 | 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] | 8057 | switch (exp->action) { |
| 8058 | case ACT_ALLOW: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 8059 | txn->flags |= TX_SVALLOW; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8060 | done = 1; |
| 8061 | break; |
| 8062 | |
| 8063 | case ACT_DENY: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 8064 | txn->flags |= TX_SVDENY; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8065 | done = 1; |
| 8066 | break; |
| 8067 | |
| 8068 | case ACT_REPLACE: |
Sasha Pachev | c600204 | 2014-05-26 12:33:48 -0600 | [diff] [blame] | 8069 | trash.len = exp_replace(trash.str, trash.size, cur_ptr, exp->replace, pmatch); |
| 8070 | if (trash.len < 0) |
| 8071 | return -1; |
| 8072 | |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 8073 | 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] | 8074 | /* FIXME: if the user adds a newline in the replacement, the |
| 8075 | * index will not be recalculated for now, and the new line |
| 8076 | * will not be counted as a new header. |
| 8077 | */ |
| 8078 | |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 8079 | http_msg_move_end(&txn->rsp, delta); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8080 | cur_end += delta; |
Willy Tarreau | 69d8c5d | 2012-05-08 09:44:41 +0200 | [diff] [blame] | 8081 | cur_end = (char *)http_parse_stsline(&txn->rsp, |
Willy Tarreau | 0278576 | 2007-04-03 14:45:44 +0200 | [diff] [blame] | 8082 | HTTP_MSG_RPVER, |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8083 | cur_ptr, cur_end + 1, |
| 8084 | NULL, NULL); |
| 8085 | if (unlikely(!cur_end)) |
| 8086 | return -1; |
| 8087 | |
| 8088 | /* we have a full respnse and we know that we have either a CR |
| 8089 | * or an LF at <ptr>. |
| 8090 | */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 8091 | 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] | 8092 | 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] | 8093 | /* there is no point trying this regex on headers */ |
| 8094 | return 1; |
| 8095 | } |
| 8096 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8097 | return done; |
| 8098 | } |
| 8099 | |
| 8100 | |
| 8101 | |
| 8102 | /* |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 8103 | * 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] | 8104 | * Returns 0 if everything is alright, or -1 in case a replacement lead to an |
| 8105 | * unparsable response. |
| 8106 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 8107 | 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] | 8108 | { |
Willy Tarreau | 192252e | 2015-04-04 01:47:55 +0200 | [diff] [blame] | 8109 | struct session *sess = s->sess; |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 8110 | struct http_txn *txn = s->txn; |
Willy Tarreau | fdb563c | 2010-01-31 15:43:27 +0100 | [diff] [blame] | 8111 | struct hdr_exp *exp; |
| 8112 | |
| 8113 | for (exp = px->rsp_exp; exp; exp = exp->next) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8114 | int ret; |
| 8115 | |
| 8116 | /* |
| 8117 | * The interleaving of transformations and verdicts |
| 8118 | * makes it difficult to decide to continue or stop |
| 8119 | * the evaluation. |
| 8120 | */ |
| 8121 | |
Willy Tarreau | fdb563c | 2010-01-31 15:43:27 +0100 | [diff] [blame] | 8122 | if (txn->flags & TX_SVDENY) |
| 8123 | break; |
| 8124 | |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 8125 | if ((txn->flags & TX_SVALLOW) && |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8126 | (exp->action == ACT_ALLOW || exp->action == ACT_DENY || |
| 8127 | exp->action == ACT_PASS)) { |
| 8128 | exp = exp->next; |
| 8129 | continue; |
| 8130 | } |
| 8131 | |
Willy Tarreau | fdb563c | 2010-01-31 15:43:27 +0100 | [diff] [blame] | 8132 | /* if this filter had a condition, evaluate it now and skip to |
| 8133 | * next filter if the condition does not match. |
| 8134 | */ |
| 8135 | if (exp->cond) { |
Willy Tarreau | 192252e | 2015-04-04 01:47:55 +0200 | [diff] [blame] | 8136 | 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] | 8137 | ret = acl_pass(ret); |
| 8138 | if (((struct acl_cond *)exp->cond)->pol == ACL_COND_UNLESS) |
| 8139 | ret = !ret; |
| 8140 | if (!ret) |
| 8141 | continue; |
| 8142 | } |
| 8143 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8144 | /* Apply the filter to the status line. */ |
Willy Tarreau | fdb563c | 2010-01-31 15:43:27 +0100 | [diff] [blame] | 8145 | ret = apply_filter_to_sts_line(s, rtr, exp); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8146 | if (unlikely(ret < 0)) |
| 8147 | return -1; |
| 8148 | |
| 8149 | if (likely(ret == 0)) { |
| 8150 | /* The filter did not match the response, it can be |
| 8151 | * iterated through all headers. |
| 8152 | */ |
Sasha Pachev | c600204 | 2014-05-26 12:33:48 -0600 | [diff] [blame] | 8153 | if (unlikely(apply_filter_to_resp_headers(s, rtr, exp) < 0)) |
| 8154 | return -1; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8155 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8156 | } |
| 8157 | return 0; |
| 8158 | } |
| 8159 | |
| 8160 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8161 | /* |
Willy Tarreau | 396d2c6 | 2007-11-04 19:30:00 +0100 | [diff] [blame] | 8162 | * 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] | 8163 | * desirable to call it only when needed. This function is also used when we |
| 8164 | * 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] | 8165 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 8166 | void manage_server_side_cookies(struct stream *s, struct channel *res) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8167 | { |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 8168 | struct http_txn *txn = s->txn; |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 8169 | struct session *sess = s->sess; |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 8170 | struct server *srv; |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 8171 | int is_cookie2; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8172 | int cur_idx, old_idx, delta; |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 8173 | char *hdr_beg, *hdr_end, *hdr_next; |
| 8174 | char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8175 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8176 | /* Iterate through the headers. |
| 8177 | * we start with the start line. |
| 8178 | */ |
| 8179 | old_idx = 0; |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 8180 | hdr_next = res->buf->p + hdr_idx_first_pos(&txn->hdr_idx); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8181 | |
| 8182 | while ((cur_idx = txn->hdr_idx.v[old_idx].next)) { |
| 8183 | struct hdr_idx_elem *cur_hdr; |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 8184 | int val; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8185 | |
| 8186 | cur_hdr = &txn->hdr_idx.v[cur_idx]; |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 8187 | hdr_beg = hdr_next; |
| 8188 | hdr_end = hdr_beg + cur_hdr->len; |
| 8189 | hdr_next = hdr_end + cur_hdr->cr + 1; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8190 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 8191 | /* We have one full header between hdr_beg and hdr_end, and the |
| 8192 | * next header starts at hdr_next. We're only interested in |
| 8193 | * "Set-Cookie" and "Set-Cookie2" headers. |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8194 | */ |
| 8195 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 8196 | is_cookie2 = 0; |
| 8197 | prev = hdr_beg + 10; |
| 8198 | val = http_header_match2(hdr_beg, hdr_end, "Set-Cookie", 10); |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 8199 | if (!val) { |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 8200 | val = http_header_match2(hdr_beg, hdr_end, "Set-Cookie2", 11); |
| 8201 | if (!val) { |
| 8202 | old_idx = cur_idx; |
| 8203 | continue; |
| 8204 | } |
| 8205 | is_cookie2 = 1; |
| 8206 | prev = hdr_beg + 11; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8207 | } |
| 8208 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 8209 | /* OK, right now we know we have a Set-Cookie* at hdr_beg, and |
| 8210 | * <prev> points to the colon. |
| 8211 | */ |
Willy Tarreau | f134831 | 2010-10-07 15:54:11 +0200 | [diff] [blame] | 8212 | txn->flags |= TX_SCK_PRESENT; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8213 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 8214 | /* Maybe we only wanted to see if there was a Set-Cookie (eg: |
| 8215 | * check-cache is enabled) and we are not interested in checking |
| 8216 | * them. Warning, the cookie capture is declared in the frontend. |
Willy Tarreau | fd39dda | 2008-10-17 12:01:58 +0200 | [diff] [blame] | 8217 | */ |
Willy Tarreau | 53a09d5 | 2015-08-10 18:59:40 +0200 | [diff] [blame] | 8218 | if (s->be->cookie_name == NULL && sess->fe->capture_name == NULL) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8219 | return; |
| 8220 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 8221 | /* OK so now we know we have to process this response cookie. |
| 8222 | * The format of the Set-Cookie header is slightly different |
| 8223 | * from the format of the Cookie header in that it does not |
| 8224 | * support the comma as a cookie delimiter (thus the header |
| 8225 | * cannot be folded) because the Expires attribute described in |
| 8226 | * the original Netscape's spec may contain an unquoted date |
| 8227 | * with a comma inside. We have to live with this because |
| 8228 | * many browsers don't support Max-Age and some browsers don't |
| 8229 | * support quoted strings. However the Set-Cookie2 header is |
| 8230 | * clean. |
| 8231 | * |
| 8232 | * We have to keep multiple pointers in order to support cookie |
| 8233 | * removal at the beginning, middle or end of header without |
| 8234 | * corrupting the header (in case of set-cookie2). A special |
| 8235 | * pointer, <scav> points to the beginning of the set-cookie-av |
| 8236 | * fields after the first semi-colon. The <next> pointer points |
| 8237 | * either to the end of line (set-cookie) or next unquoted comma |
| 8238 | * (set-cookie2). All of these headers are valid : |
| 8239 | * |
| 8240 | * Set-Cookie: NAME1 = VALUE 1 ; Secure; Path="/"\r\n |
| 8241 | * Set-Cookie:NAME=VALUE; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT\r\n |
| 8242 | * Set-Cookie: NAME = VALUE ; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT\r\n |
| 8243 | * Set-Cookie2: NAME1 = VALUE 1 ; Max-Age=0, NAME2=VALUE2; Discard\r\n |
| 8244 | * | | | | | | | | | | |
| 8245 | * | | | | | | | | +-> next hdr_end <--+ |
| 8246 | * | | | | | | | +------------> scav |
| 8247 | * | | | | | | +--------------> val_end |
| 8248 | * | | | | | +--------------------> val_beg |
| 8249 | * | | | | +----------------------> equal |
| 8250 | * | | | +------------------------> att_end |
| 8251 | * | | +----------------------------> att_beg |
| 8252 | * | +------------------------------> prev |
| 8253 | * +-----------------------------------------> hdr_beg |
| 8254 | */ |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8255 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 8256 | for (; prev < hdr_end; prev = next) { |
| 8257 | /* Iterate through all cookies on this line */ |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8258 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 8259 | /* find att_beg */ |
| 8260 | att_beg = prev + 1; |
Willy Tarreau | 2235b26 | 2016-11-05 15:50:20 +0100 | [diff] [blame] | 8261 | while (att_beg < hdr_end && HTTP_IS_SPHT(*att_beg)) |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 8262 | att_beg++; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8263 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 8264 | /* find att_end : this is the first character after the last non |
| 8265 | * space before the equal. It may be equal to hdr_end. |
| 8266 | */ |
| 8267 | equal = att_end = att_beg; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8268 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 8269 | while (equal < hdr_end) { |
| 8270 | if (*equal == '=' || *equal == ';' || (is_cookie2 && *equal == ',')) |
| 8271 | break; |
Willy Tarreau | 2235b26 | 2016-11-05 15:50:20 +0100 | [diff] [blame] | 8272 | if (HTTP_IS_SPHT(*equal++)) |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 8273 | continue; |
| 8274 | att_end = equal; |
| 8275 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8276 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 8277 | /* here, <equal> points to '=', a delimitor or the end. <att_end> |
| 8278 | * is between <att_beg> and <equal>, both may be identical. |
| 8279 | */ |
| 8280 | |
| 8281 | /* look for end of cookie if there is an equal sign */ |
| 8282 | if (equal < hdr_end && *equal == '=') { |
| 8283 | /* look for the beginning of the value */ |
| 8284 | val_beg = equal + 1; |
Willy Tarreau | 2235b26 | 2016-11-05 15:50:20 +0100 | [diff] [blame] | 8285 | while (val_beg < hdr_end && HTTP_IS_SPHT(*val_beg)) |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 8286 | val_beg++; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8287 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 8288 | /* find the end of the value, respecting quotes */ |
| 8289 | next = find_cookie_value_end(val_beg, hdr_end); |
| 8290 | |
| 8291 | /* make val_end point to the first white space or delimitor after the value */ |
| 8292 | val_end = next; |
Willy Tarreau | 2235b26 | 2016-11-05 15:50:20 +0100 | [diff] [blame] | 8293 | while (val_end > val_beg && HTTP_IS_SPHT(*(val_end - 1))) |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 8294 | val_end--; |
| 8295 | } else { |
| 8296 | /* <equal> points to next comma, semi-colon or EOL */ |
| 8297 | val_beg = val_end = next = equal; |
| 8298 | } |
| 8299 | |
| 8300 | if (next < hdr_end) { |
| 8301 | /* Set-Cookie2 supports multiple cookies, and <next> points to |
| 8302 | * a colon or semi-colon before the end. So skip all attr-value |
| 8303 | * pairs and look for the next comma. For Set-Cookie, since |
| 8304 | * commas are permitted in values, skip to the end. |
| 8305 | */ |
| 8306 | if (is_cookie2) |
| 8307 | next = find_hdr_value_end(next, hdr_end); |
| 8308 | else |
| 8309 | next = hdr_end; |
| 8310 | } |
| 8311 | |
| 8312 | /* Now everything is as on the diagram above */ |
| 8313 | |
| 8314 | /* Ignore cookies with no equal sign */ |
| 8315 | if (equal == val_end) |
| 8316 | continue; |
| 8317 | |
| 8318 | /* If there are spaces around the equal sign, we need to |
| 8319 | * strip them otherwise we'll get trouble for cookie captures, |
| 8320 | * or even for rewrites. Since this happens extremely rarely, |
| 8321 | * it does not hurt performance. |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8322 | */ |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 8323 | if (unlikely(att_end != equal || val_beg > equal + 1)) { |
| 8324 | int stripped_before = 0; |
| 8325 | int stripped_after = 0; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8326 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 8327 | if (att_end != equal) { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 8328 | stripped_before = buffer_replace2(res->buf, att_end, equal, NULL, 0); |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 8329 | equal += stripped_before; |
| 8330 | val_beg += stripped_before; |
| 8331 | } |
| 8332 | |
| 8333 | if (val_beg > equal + 1) { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 8334 | stripped_after = buffer_replace2(res->buf, equal + 1, val_beg, NULL, 0); |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 8335 | val_beg += stripped_after; |
| 8336 | stripped_before += stripped_after; |
| 8337 | } |
| 8338 | |
| 8339 | val_end += stripped_before; |
| 8340 | next += stripped_before; |
| 8341 | hdr_end += stripped_before; |
| 8342 | hdr_next += stripped_before; |
| 8343 | cur_hdr->len += stripped_before; |
Willy Tarreau | 1fc1f45 | 2011-04-07 22:35:37 +0200 | [diff] [blame] | 8344 | http_msg_move_end(&txn->rsp, stripped_before); |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 8345 | } |
| 8346 | |
| 8347 | /* First, let's see if we want to capture this cookie. We check |
| 8348 | * that we don't already have a server side cookie, because we |
| 8349 | * can only capture one. Also as an optimisation, we ignore |
| 8350 | * cookies shorter than the declared name. |
| 8351 | */ |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 8352 | if (sess->fe->capture_name != NULL && |
Willy Tarreau | 3bac9ff | 2007-03-18 17:31:28 +0100 | [diff] [blame] | 8353 | txn->srv_cookie == NULL && |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 8354 | (val_end - att_beg >= sess->fe->capture_namelen) && |
| 8355 | memcmp(att_beg, sess->fe->capture_name, sess->fe->capture_namelen) == 0) { |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 8356 | int log_len = val_end - att_beg; |
Willy Tarreau | 086b3b4 | 2007-05-13 21:45:51 +0200 | [diff] [blame] | 8357 | if ((txn->srv_cookie = pool_alloc2(pool2_capture)) == NULL) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8358 | Alert("HTTP logging : out of memory.\n"); |
| 8359 | } |
Willy Tarreau | f70fc75 | 2010-11-19 11:27:18 +0100 | [diff] [blame] | 8360 | else { |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 8361 | if (log_len > sess->fe->capture_len) |
| 8362 | log_len = sess->fe->capture_len; |
Willy Tarreau | f70fc75 | 2010-11-19 11:27:18 +0100 | [diff] [blame] | 8363 | memcpy(txn->srv_cookie, att_beg, log_len); |
| 8364 | txn->srv_cookie[log_len] = 0; |
| 8365 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8366 | } |
| 8367 | |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 8368 | srv = objt_server(s->target); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8369 | /* now check if we need to process it for persistence */ |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 8370 | if (!(s->flags & SF_IGNORE_PRST) && |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 8371 | (att_end - att_beg == s->be->cookie_len) && (s->be->cookie_name != NULL) && |
| 8372 | (memcmp(att_beg, s->be->cookie_name, att_end - att_beg) == 0)) { |
Willy Tarreau | f134831 | 2010-10-07 15:54:11 +0200 | [diff] [blame] | 8373 | /* assume passive cookie by default */ |
| 8374 | txn->flags &= ~TX_SCK_MASK; |
| 8375 | txn->flags |= TX_SCK_FOUND; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8376 | |
| 8377 | /* If the cookie is in insert mode on a known server, we'll delete |
| 8378 | * this occurrence because we'll insert another one later. |
| 8379 | * 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] | 8380 | * a direct access. |
| 8381 | */ |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 8382 | if (s->be->ck_opts & PR_CK_PSV) { |
Willy Tarreau | ba4c5be | 2010-10-23 12:46:42 +0200 | [diff] [blame] | 8383 | /* The "preserve" flag was set, we don't want to touch the |
| 8384 | * server's cookie. |
| 8385 | */ |
| 8386 | } |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 8387 | else if ((srv && (s->be->ck_opts & PR_CK_INS)) || |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 8388 | ((s->flags & SF_DIRECT) && (s->be->ck_opts & PR_CK_IND))) { |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 8389 | /* this cookie must be deleted */ |
| 8390 | if (*prev == ':' && next == hdr_end) { |
| 8391 | /* whole header */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 8392 | delta = buffer_replace2(res->buf, hdr_beg, hdr_next, NULL, 0); |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 8393 | txn->hdr_idx.v[old_idx].next = cur_hdr->next; |
| 8394 | txn->hdr_idx.used--; |
| 8395 | cur_hdr->len = 0; |
Willy Tarreau | 26db59e | 2010-11-28 06:57:24 +0100 | [diff] [blame] | 8396 | cur_idx = old_idx; |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 8397 | hdr_next += delta; |
| 8398 | http_msg_move_end(&txn->rsp, delta); |
| 8399 | /* note: while both invalid now, <next> and <hdr_end> |
| 8400 | * are still equal, so the for() will stop as expected. |
| 8401 | */ |
| 8402 | } else { |
| 8403 | /* just remove the value */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 8404 | int delta = del_hdr_value(res->buf, &prev, next); |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 8405 | next = prev; |
| 8406 | hdr_end += delta; |
| 8407 | hdr_next += delta; |
| 8408 | cur_hdr->len += delta; |
| 8409 | http_msg_move_end(&txn->rsp, delta); |
| 8410 | } |
Willy Tarreau | f134831 | 2010-10-07 15:54:11 +0200 | [diff] [blame] | 8411 | txn->flags &= ~TX_SCK_MASK; |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 8412 | txn->flags |= TX_SCK_DELETED; |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 8413 | /* and go on with next cookie */ |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8414 | } |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 8415 | else if (srv && srv->cookie && (s->be->ck_opts & PR_CK_RW)) { |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 8416 | /* replace bytes val_beg->val_end with the cookie name associated |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8417 | * with this server since we know it. |
| 8418 | */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 8419 | 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] | 8420 | next += delta; |
| 8421 | hdr_end += delta; |
| 8422 | hdr_next += delta; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8423 | cur_hdr->len += delta; |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 8424 | http_msg_move_end(&txn->rsp, delta); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8425 | |
Willy Tarreau | f134831 | 2010-10-07 15:54:11 +0200 | [diff] [blame] | 8426 | txn->flags &= ~TX_SCK_MASK; |
| 8427 | txn->flags |= TX_SCK_REPLACED; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8428 | } |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 8429 | else if (srv && srv->cookie && (s->be->ck_opts & PR_CK_PFX)) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8430 | /* insert the cookie name associated with this server |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 8431 | * before existing cookie, and insert a delimiter between them.. |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8432 | */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 8433 | 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] | 8434 | next += delta; |
| 8435 | hdr_end += delta; |
| 8436 | hdr_next += delta; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8437 | cur_hdr->len += delta; |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 8438 | http_msg_move_end(&txn->rsp, delta); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8439 | |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 8440 | val_beg[srv->cklen] = COOKIE_DELIM; |
Willy Tarreau | f134831 | 2010-10-07 15:54:11 +0200 | [diff] [blame] | 8441 | txn->flags &= ~TX_SCK_MASK; |
| 8442 | txn->flags |= TX_SCK_REPLACED; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8443 | } |
| 8444 | } |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 8445 | /* that's done for this cookie, check the next one on the same |
| 8446 | * line when next != hdr_end (only if is_cookie2). |
| 8447 | */ |
| 8448 | } |
| 8449 | /* check next header */ |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8450 | old_idx = cur_idx; |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 8451 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8452 | } |
| 8453 | |
| 8454 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8455 | /* |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 8456 | * Check if response is cacheable or not. Updates s->flags. |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8457 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 8458 | void check_response_for_cacheability(struct stream *s, struct channel *rtr) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8459 | { |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 8460 | struct http_txn *txn = s->txn; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8461 | char *p1, *p2; |
| 8462 | |
| 8463 | char *cur_ptr, *cur_end, *cur_next; |
| 8464 | int cur_idx; |
| 8465 | |
Willy Tarreau | 5df5187 | 2007-11-25 16:20:08 +0100 | [diff] [blame] | 8466 | if (!(txn->flags & TX_CACHEABLE)) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8467 | return; |
| 8468 | |
| 8469 | /* Iterate through the headers. |
| 8470 | * we start with the start line. |
| 8471 | */ |
| 8472 | cur_idx = 0; |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 8473 | cur_next = rtr->buf->p + hdr_idx_first_pos(&txn->hdr_idx); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8474 | |
| 8475 | while ((cur_idx = txn->hdr_idx.v[cur_idx].next)) { |
| 8476 | struct hdr_idx_elem *cur_hdr; |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 8477 | int val; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8478 | |
| 8479 | cur_hdr = &txn->hdr_idx.v[cur_idx]; |
| 8480 | cur_ptr = cur_next; |
| 8481 | cur_end = cur_ptr + cur_hdr->len; |
| 8482 | cur_next = cur_end + cur_hdr->cr + 1; |
| 8483 | |
| 8484 | /* We have one full header between cur_ptr and cur_end, and the |
| 8485 | * next header starts at cur_next. We're only interested in |
| 8486 | * "Cookie:" headers. |
| 8487 | */ |
| 8488 | |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 8489 | val = http_header_match2(cur_ptr, cur_end, "Pragma", 6); |
| 8490 | if (val) { |
| 8491 | if ((cur_end - (cur_ptr + val) >= 8) && |
| 8492 | strncasecmp(cur_ptr + val, "no-cache", 8) == 0) { |
| 8493 | txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK; |
| 8494 | return; |
| 8495 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8496 | } |
| 8497 | |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 8498 | val = http_header_match2(cur_ptr, cur_end, "Cache-control", 13); |
| 8499 | if (!val) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8500 | continue; |
| 8501 | |
| 8502 | /* OK, right now we know we have a cache-control header at cur_ptr */ |
| 8503 | |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 8504 | p1 = cur_ptr + val; /* first non-space char after 'cache-control:' */ |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8505 | |
| 8506 | if (p1 >= cur_end) /* no more info */ |
| 8507 | continue; |
| 8508 | |
| 8509 | /* p1 is at the beginning of the value */ |
| 8510 | p2 = p1; |
| 8511 | |
Willy Tarreau | 8f8e645 | 2007-06-17 21:51:38 +0200 | [diff] [blame] | 8512 | while (p2 < cur_end && *p2 != '=' && *p2 != ',' && !isspace((unsigned char)*p2)) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8513 | p2++; |
| 8514 | |
| 8515 | /* we have a complete value between p1 and p2 */ |
| 8516 | if (p2 < cur_end && *p2 == '=') { |
| 8517 | /* we have something of the form no-cache="set-cookie" */ |
| 8518 | if ((cur_end - p1 >= 21) && |
| 8519 | strncasecmp(p1, "no-cache=\"set-cookie", 20) == 0 |
| 8520 | && (p1[20] == '"' || p1[20] == ',')) |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 8521 | txn->flags &= ~TX_CACHE_COOK; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8522 | continue; |
| 8523 | } |
| 8524 | |
| 8525 | /* OK, so we know that either p2 points to the end of string or to a comma */ |
| 8526 | if (((p2 - p1 == 7) && strncasecmp(p1, "private", 7) == 0) || |
Willy Tarreau | 5b15f90 | 2013-07-04 12:46:56 +0200 | [diff] [blame] | 8527 | ((p2 - p1 == 8) && strncasecmp(p1, "no-cache", 8) == 0) || |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8528 | ((p2 - p1 == 8) && strncasecmp(p1, "no-store", 8) == 0) || |
| 8529 | ((p2 - p1 == 9) && strncasecmp(p1, "max-age=0", 9) == 0) || |
| 8530 | ((p2 - p1 == 10) && strncasecmp(p1, "s-maxage=0", 10) == 0)) { |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 8531 | txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8532 | return; |
| 8533 | } |
| 8534 | |
| 8535 | if ((p2 - p1 == 6) && strncasecmp(p1, "public", 6) == 0) { |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 8536 | txn->flags |= TX_CACHEABLE | TX_CACHE_COOK; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8537 | continue; |
| 8538 | } |
| 8539 | } |
| 8540 | } |
| 8541 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 8542 | |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 8543 | /* |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 8544 | * 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] | 8545 | * for the current backend. |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 8546 | * |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 8547 | * 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] | 8548 | * uri_auth field is valid. |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 8549 | * |
Krzysztof Piotr Oledzki | 8c8bd45 | 2010-01-29 19:29:32 +0100 | [diff] [blame] | 8550 | * Returns 1 if stats should be provided, otherwise 0. |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 8551 | */ |
Willy Tarreau | 295a837 | 2011-03-10 11:25:07 +0100 | [diff] [blame] | 8552 | 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] | 8553 | { |
| 8554 | struct uri_auth *uri_auth = backend->uri_auth; |
Willy Tarreau | 3a215be | 2012-03-09 21:39:51 +0100 | [diff] [blame] | 8555 | struct http_msg *msg = &txn->req; |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 8556 | const char *uri = msg->chn->buf->p+ msg->sl.rq.u; |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 8557 | |
Krzysztof Piotr Oledzki | 8c8bd45 | 2010-01-29 19:29:32 +0100 | [diff] [blame] | 8558 | if (!uri_auth) |
| 8559 | return 0; |
| 8560 | |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 8561 | 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] | 8562 | return 0; |
| 8563 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 8564 | /* check URI size */ |
Willy Tarreau | 3a215be | 2012-03-09 21:39:51 +0100 | [diff] [blame] | 8565 | if (uri_auth->uri_len > msg->sl.rq.u_l) |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 8566 | return 0; |
| 8567 | |
Willy Tarreau | 414e9bb | 2013-11-23 00:30:38 +0100 | [diff] [blame] | 8568 | if (memcmp(uri, uri_auth->uri_prefix, uri_auth->uri_len) != 0) |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 8569 | return 0; |
| 8570 | |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 8571 | return 1; |
| 8572 | } |
| 8573 | |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 8574 | /* |
| 8575 | * 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] | 8576 | * By default it tries to report the error position as msg->err_pos. However if |
| 8577 | * this one is not set, it will then report msg->next, which is the last known |
| 8578 | * 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] | 8579 | * displays buffers as a contiguous area starting at buf->p. |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 8580 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 8581 | void http_capture_bad_message(struct error_snapshot *es, struct stream *s, |
Willy Tarreau | 8a0cef2 | 2012-03-09 13:39:23 +0100 | [diff] [blame] | 8582 | struct http_msg *msg, |
Willy Tarreau | 3770f23 | 2013-12-07 00:01:53 +0100 | [diff] [blame] | 8583 | enum ht_state state, struct proxy *other_end) |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 8584 | { |
Willy Tarreau | 9ad7bd4 | 2015-04-03 19:19:59 +0200 | [diff] [blame] | 8585 | struct session *sess = strm_sess(s); |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 8586 | struct channel *chn = msg->chn; |
Willy Tarreau | 69d8c5d | 2012-05-08 09:44:41 +0200 | [diff] [blame] | 8587 | int len1, len2; |
Willy Tarreau | 8a0cef2 | 2012-03-09 13:39:23 +0100 | [diff] [blame] | 8588 | |
Willy Tarreau | f3764b7 | 2016-03-31 13:45:10 +0200 | [diff] [blame] | 8589 | es->len = MIN(chn->buf->i, global.tune.bufsize); |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 8590 | len1 = chn->buf->data + chn->buf->size - chn->buf->p; |
Willy Tarreau | 69d8c5d | 2012-05-08 09:44:41 +0200 | [diff] [blame] | 8591 | len1 = MIN(len1, es->len); |
| 8592 | len2 = es->len - len1; /* remaining data if buffer wraps */ |
| 8593 | |
Willy Tarreau | f3764b7 | 2016-03-31 13:45:10 +0200 | [diff] [blame] | 8594 | if (!es->buf) |
| 8595 | es->buf = malloc(global.tune.bufsize); |
| 8596 | |
| 8597 | if (es->buf) { |
| 8598 | memcpy(es->buf, chn->buf->p, len1); |
| 8599 | if (len2) |
| 8600 | memcpy(es->buf + len1, chn->buf->data, len2); |
| 8601 | } |
Willy Tarreau | 81f2fb9 | 2010-12-12 13:09:08 +0100 | [diff] [blame] | 8602 | |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 8603 | if (msg->err_pos >= 0) |
Willy Tarreau | 69d8c5d | 2012-05-08 09:44:41 +0200 | [diff] [blame] | 8604 | es->pos = msg->err_pos; |
Willy Tarreau | 81f2fb9 | 2010-12-12 13:09:08 +0100 | [diff] [blame] | 8605 | else |
Willy Tarreau | 69d8c5d | 2012-05-08 09:44:41 +0200 | [diff] [blame] | 8606 | es->pos = msg->next; |
Willy Tarreau | 81f2fb9 | 2010-12-12 13:09:08 +0100 | [diff] [blame] | 8607 | |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 8608 | es->when = date; // user-visible date |
| 8609 | es->sid = s->uniq_id; |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 8610 | es->srv = objt_server(s->target); |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 8611 | es->oe = other_end; |
Willy Tarreau | 9ad7bd4 | 2015-04-03 19:19:59 +0200 | [diff] [blame] | 8612 | if (objt_conn(sess->origin)) |
| 8613 | es->src = __objt_conn(sess->origin)->addr.from; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 8614 | else |
| 8615 | memset(&es->src, 0, sizeof(es->src)); |
| 8616 | |
Willy Tarreau | 078272e | 2010-12-12 12:46:33 +0100 | [diff] [blame] | 8617 | es->state = state; |
Willy Tarreau | 10479e4 | 2010-12-12 14:00:34 +0100 | [diff] [blame] | 8618 | es->ev_id = error_snapshot_id++; |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 8619 | es->b_flags = chn->flags; |
Willy Tarreau | d04b1bc | 2012-05-08 11:03:10 +0200 | [diff] [blame] | 8620 | es->s_flags = s->flags; |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 8621 | es->t_flags = s->txn->flags; |
Willy Tarreau | d04b1bc | 2012-05-08 11:03:10 +0200 | [diff] [blame] | 8622 | es->m_flags = msg->flags; |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 8623 | es->b_out = chn->buf->o; |
| 8624 | es->b_wrap = chn->buf->data + chn->buf->size - chn->buf->p; |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 8625 | es->b_tot = chn->total; |
Willy Tarreau | d04b1bc | 2012-05-08 11:03:10 +0200 | [diff] [blame] | 8626 | es->m_clen = msg->chunk_len; |
| 8627 | es->m_blen = msg->body_len; |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 8628 | } |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 8629 | |
Willy Tarreau | 294c473 | 2011-12-16 21:35:50 +0100 | [diff] [blame] | 8630 | /* Return in <vptr> and <vlen> the pointer and length of occurrence <occ> of |
| 8631 | * header whose name is <hname> of length <hlen>. If <ctx> is null, lookup is |
| 8632 | * performed over the whole headers. Otherwise it must contain a valid header |
| 8633 | * context, initialised with ctx->idx=0 for the first lookup in a series. If |
| 8634 | * <occ> is positive or null, occurrence #occ from the beginning (or last ctx) |
| 8635 | * is returned. Occ #0 and #1 are equivalent. If <occ> is negative (and no less |
| 8636 | * 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] | 8637 | * -1. The value fetch stops at commas, so this function is suited for use with |
| 8638 | * list headers. |
Willy Tarreau | 294c473 | 2011-12-16 21:35:50 +0100 | [diff] [blame] | 8639 | * 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] | 8640 | */ |
Willy Tarreau | 185b5c4 | 2012-04-26 15:11:51 +0200 | [diff] [blame] | 8641 | 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] | 8642 | struct hdr_idx *idx, int occ, |
| 8643 | struct hdr_ctx *ctx, char **vptr, int *vlen) |
Willy Tarreau | bce7088 | 2009-09-07 11:51:47 +0200 | [diff] [blame] | 8644 | { |
Willy Tarreau | 294c473 | 2011-12-16 21:35:50 +0100 | [diff] [blame] | 8645 | struct hdr_ctx local_ctx; |
| 8646 | char *ptr_hist[MAX_HDR_HISTORY]; |
| 8647 | int len_hist[MAX_HDR_HISTORY]; |
Willy Tarreau | bce7088 | 2009-09-07 11:51:47 +0200 | [diff] [blame] | 8648 | unsigned int hist_ptr; |
Willy Tarreau | 294c473 | 2011-12-16 21:35:50 +0100 | [diff] [blame] | 8649 | int found; |
Willy Tarreau | bce7088 | 2009-09-07 11:51:47 +0200 | [diff] [blame] | 8650 | |
Willy Tarreau | 294c473 | 2011-12-16 21:35:50 +0100 | [diff] [blame] | 8651 | if (!ctx) { |
| 8652 | local_ctx.idx = 0; |
| 8653 | ctx = &local_ctx; |
| 8654 | } |
| 8655 | |
Willy Tarreau | bce7088 | 2009-09-07 11:51:47 +0200 | [diff] [blame] | 8656 | if (occ >= 0) { |
Willy Tarreau | 294c473 | 2011-12-16 21:35:50 +0100 | [diff] [blame] | 8657 | /* search from the beginning */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 8658 | while (http_find_header2(hname, hlen, msg->chn->buf->p, idx, ctx)) { |
Willy Tarreau | bce7088 | 2009-09-07 11:51:47 +0200 | [diff] [blame] | 8659 | occ--; |
| 8660 | if (occ <= 0) { |
Willy Tarreau | 294c473 | 2011-12-16 21:35:50 +0100 | [diff] [blame] | 8661 | *vptr = ctx->line + ctx->val; |
| 8662 | *vlen = ctx->vlen; |
| 8663 | return 1; |
Willy Tarreau | bce7088 | 2009-09-07 11:51:47 +0200 | [diff] [blame] | 8664 | } |
| 8665 | } |
Willy Tarreau | 294c473 | 2011-12-16 21:35:50 +0100 | [diff] [blame] | 8666 | return 0; |
Willy Tarreau | bce7088 | 2009-09-07 11:51:47 +0200 | [diff] [blame] | 8667 | } |
| 8668 | |
| 8669 | /* negative occurrence, we scan all the list then walk back */ |
| 8670 | if (-occ > MAX_HDR_HISTORY) |
| 8671 | return 0; |
| 8672 | |
Willy Tarreau | 294c473 | 2011-12-16 21:35:50 +0100 | [diff] [blame] | 8673 | found = hist_ptr = 0; |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 8674 | while (http_find_header2(hname, hlen, msg->chn->buf->p, idx, ctx)) { |
Willy Tarreau | 294c473 | 2011-12-16 21:35:50 +0100 | [diff] [blame] | 8675 | ptr_hist[hist_ptr] = ctx->line + ctx->val; |
| 8676 | len_hist[hist_ptr] = ctx->vlen; |
| 8677 | if (++hist_ptr >= MAX_HDR_HISTORY) |
Willy Tarreau | bce7088 | 2009-09-07 11:51:47 +0200 | [diff] [blame] | 8678 | hist_ptr = 0; |
| 8679 | found++; |
| 8680 | } |
| 8681 | if (-occ > found) |
| 8682 | return 0; |
| 8683 | /* 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] | 8684 | * find occurrence -occ. 0 <= hist_ptr < MAX_HDR_HISTORY, and we have |
| 8685 | * -10 <= occ <= -1. So we have to check [hist_ptr%MAX_HDR_HISTORY+occ] |
| 8686 | * to remain in the 0..9 range. |
Willy Tarreau | bce7088 | 2009-09-07 11:51:47 +0200 | [diff] [blame] | 8687 | */ |
Willy Tarreau | 67dad27 | 2013-06-12 22:27:44 +0200 | [diff] [blame] | 8688 | hist_ptr += occ + MAX_HDR_HISTORY; |
Willy Tarreau | bce7088 | 2009-09-07 11:51:47 +0200 | [diff] [blame] | 8689 | if (hist_ptr >= MAX_HDR_HISTORY) |
| 8690 | hist_ptr -= MAX_HDR_HISTORY; |
Willy Tarreau | 294c473 | 2011-12-16 21:35:50 +0100 | [diff] [blame] | 8691 | *vptr = ptr_hist[hist_ptr]; |
| 8692 | *vlen = len_hist[hist_ptr]; |
| 8693 | return 1; |
Willy Tarreau | bce7088 | 2009-09-07 11:51:47 +0200 | [diff] [blame] | 8694 | } |
| 8695 | |
Willy Tarreau | 04ff9f1 | 2013-06-10 18:39:42 +0200 | [diff] [blame] | 8696 | /* Return in <vptr> and <vlen> the pointer and length of occurrence <occ> of |
| 8697 | * header whose name is <hname> of length <hlen>. If <ctx> is null, lookup is |
| 8698 | * performed over the whole headers. Otherwise it must contain a valid header |
| 8699 | * context, initialised with ctx->idx=0 for the first lookup in a series. If |
| 8700 | * <occ> is positive or null, occurrence #occ from the beginning (or last ctx) |
| 8701 | * is returned. Occ #0 and #1 are equivalent. If <occ> is negative (and no less |
| 8702 | * than -MAX_HDR_HISTORY), the occurrence is counted from the last one which is |
| 8703 | * -1. This function differs from http_get_hdr() in that it only returns full |
| 8704 | * line header values and does not stop at commas. |
| 8705 | * The return value is 0 if nothing was found, or non-zero otherwise. |
| 8706 | */ |
| 8707 | unsigned int http_get_fhdr(const struct http_msg *msg, const char *hname, int hlen, |
| 8708 | struct hdr_idx *idx, int occ, |
| 8709 | struct hdr_ctx *ctx, char **vptr, int *vlen) |
| 8710 | { |
| 8711 | struct hdr_ctx local_ctx; |
| 8712 | char *ptr_hist[MAX_HDR_HISTORY]; |
| 8713 | int len_hist[MAX_HDR_HISTORY]; |
| 8714 | unsigned int hist_ptr; |
| 8715 | int found; |
| 8716 | |
| 8717 | if (!ctx) { |
| 8718 | local_ctx.idx = 0; |
| 8719 | ctx = &local_ctx; |
| 8720 | } |
| 8721 | |
| 8722 | if (occ >= 0) { |
| 8723 | /* search from the beginning */ |
| 8724 | while (http_find_full_header2(hname, hlen, msg->chn->buf->p, idx, ctx)) { |
| 8725 | occ--; |
| 8726 | if (occ <= 0) { |
| 8727 | *vptr = ctx->line + ctx->val; |
| 8728 | *vlen = ctx->vlen; |
| 8729 | return 1; |
| 8730 | } |
| 8731 | } |
| 8732 | return 0; |
| 8733 | } |
| 8734 | |
| 8735 | /* negative occurrence, we scan all the list then walk back */ |
| 8736 | if (-occ > MAX_HDR_HISTORY) |
| 8737 | return 0; |
| 8738 | |
| 8739 | found = hist_ptr = 0; |
| 8740 | while (http_find_full_header2(hname, hlen, msg->chn->buf->p, idx, ctx)) { |
| 8741 | ptr_hist[hist_ptr] = ctx->line + ctx->val; |
| 8742 | len_hist[hist_ptr] = ctx->vlen; |
| 8743 | if (++hist_ptr >= MAX_HDR_HISTORY) |
| 8744 | hist_ptr = 0; |
| 8745 | found++; |
| 8746 | } |
| 8747 | if (-occ > found) |
| 8748 | return 0; |
Nenad Merdanovic | 69ad4b9 | 2016-03-29 13:14:30 +0200 | [diff] [blame] | 8749 | |
Willy Tarreau | 04ff9f1 | 2013-06-10 18:39:42 +0200 | [diff] [blame] | 8750 | /* OK now we have the last occurrence in [hist_ptr-1], and we need to |
Nenad Merdanovic | 69ad4b9 | 2016-03-29 13:14:30 +0200 | [diff] [blame] | 8751 | * find occurrence -occ. 0 <= hist_ptr < MAX_HDR_HISTORY, and we have |
| 8752 | * -10 <= occ <= -1. So we have to check [hist_ptr%MAX_HDR_HISTORY+occ] |
| 8753 | * to remain in the 0..9 range. |
Willy Tarreau | 04ff9f1 | 2013-06-10 18:39:42 +0200 | [diff] [blame] | 8754 | */ |
Nenad Merdanovic | 69ad4b9 | 2016-03-29 13:14:30 +0200 | [diff] [blame] | 8755 | hist_ptr += occ + MAX_HDR_HISTORY; |
Willy Tarreau | 04ff9f1 | 2013-06-10 18:39:42 +0200 | [diff] [blame] | 8756 | if (hist_ptr >= MAX_HDR_HISTORY) |
| 8757 | hist_ptr -= MAX_HDR_HISTORY; |
| 8758 | *vptr = ptr_hist[hist_ptr]; |
| 8759 | *vlen = len_hist[hist_ptr]; |
| 8760 | return 1; |
| 8761 | } |
| 8762 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 8763 | /* |
Willy Tarreau | e92693a | 2012-09-24 21:13:39 +0200 | [diff] [blame] | 8764 | * Print a debug line with a header. Always stop at the first CR or LF char, |
| 8765 | * so it is safe to pass it a full buffer if needed. If <err> is not NULL, an |
| 8766 | * arrow is printed after the line which contains the pointer. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 8767 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 8768 | 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] | 8769 | { |
Willy Tarreau | 9ad7bd4 | 2015-04-03 19:19:59 +0200 | [diff] [blame] | 8770 | struct session *sess = strm_sess(s); |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 8771 | int max; |
Willy Tarreau | 9ad7bd4 | 2015-04-03 19:19:59 +0200 | [diff] [blame] | 8772 | |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 8773 | 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] | 8774 | dir, |
Willy Tarreau | 9ad7bd4 | 2015-04-03 19:19:59 +0200 | [diff] [blame] | 8775 | 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] | 8776 | 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] | 8777 | |
| 8778 | for (max = 0; start + max < end; max++) |
| 8779 | if (start[max] == '\r' || start[max] == '\n') |
| 8780 | break; |
| 8781 | |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 8782 | UBOUND(max, trash.size - trash.len - 3); |
| 8783 | trash.len += strlcpy2(trash.str + trash.len, start, max + 1); |
| 8784 | trash.str[trash.len++] = '\n'; |
Willy Tarreau | 89efaed | 2013-12-13 15:14:55 +0100 | [diff] [blame] | 8785 | shut_your_big_mouth_gcc(write(1, trash.str, trash.len)); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 8786 | } |
| 8787 | |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 8788 | |
| 8789 | /* Allocate a new HTTP transaction for stream <s> unless there is one already. |
| 8790 | * The hdr_idx is allocated as well. In case of allocation failure, everything |
| 8791 | * allocated is freed and NULL is returned. Otherwise the new transaction is |
| 8792 | * assigned to the stream and returned. |
| 8793 | */ |
| 8794 | struct http_txn *http_alloc_txn(struct stream *s) |
| 8795 | { |
| 8796 | struct http_txn *txn = s->txn; |
| 8797 | |
| 8798 | if (txn) |
| 8799 | return txn; |
| 8800 | |
| 8801 | txn = pool_alloc2(pool2_http_txn); |
| 8802 | if (!txn) |
| 8803 | return txn; |
| 8804 | |
| 8805 | txn->hdr_idx.size = global.tune.max_http_hdr; |
| 8806 | txn->hdr_idx.v = pool_alloc2(pool2_hdr_idx); |
| 8807 | if (!txn->hdr_idx.v) { |
| 8808 | pool_free2(pool2_http_txn, txn); |
| 8809 | return NULL; |
| 8810 | } |
| 8811 | |
| 8812 | s->txn = txn; |
| 8813 | return txn; |
| 8814 | } |
| 8815 | |
Thierry FOURNIER | fd50f0b | 2015-09-25 18:53:18 +0200 | [diff] [blame] | 8816 | void http_txn_reset_req(struct http_txn *txn) |
| 8817 | { |
| 8818 | txn->req.flags = 0; |
| 8819 | txn->req.sol = txn->req.eol = txn->req.eoh = 0; /* relative to the buffer */ |
| 8820 | txn->req.next = 0; |
| 8821 | txn->req.chunk_len = 0LL; |
| 8822 | txn->req.body_len = 0LL; |
| 8823 | txn->req.msg_state = HTTP_MSG_RQBEFORE; /* at the very beginning of the request */ |
| 8824 | } |
| 8825 | |
| 8826 | void http_txn_reset_res(struct http_txn *txn) |
| 8827 | { |
| 8828 | txn->rsp.flags = 0; |
| 8829 | txn->rsp.sol = txn->rsp.eol = txn->rsp.eoh = 0; /* relative to the buffer */ |
| 8830 | txn->rsp.next = 0; |
| 8831 | txn->rsp.chunk_len = 0LL; |
| 8832 | txn->rsp.body_len = 0LL; |
| 8833 | txn->rsp.msg_state = HTTP_MSG_RPBEFORE; /* at the very beginning of the response */ |
| 8834 | } |
| 8835 | |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 8836 | /* |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 8837 | * 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] | 8838 | * the required fields are properly allocated and that we only need to (re)init |
| 8839 | * them. This should be used before processing any new request. |
| 8840 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 8841 | void http_init_txn(struct stream *s) |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 8842 | { |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 8843 | struct http_txn *txn = s->txn; |
Willy Tarreau | d0d8da9 | 2015-04-04 02:10:38 +0200 | [diff] [blame] | 8844 | struct proxy *fe = strm_fe(s); |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 8845 | |
| 8846 | txn->flags = 0; |
| 8847 | txn->status = -1; |
| 8848 | |
Willy Tarreau | f64d141 | 2010-10-07 20:06:11 +0200 | [diff] [blame] | 8849 | txn->cookie_first_date = 0; |
| 8850 | txn->cookie_last_date = 0; |
| 8851 | |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 8852 | txn->srv_cookie = NULL; |
| 8853 | txn->cli_cookie = NULL; |
| 8854 | txn->uri = NULL; |
| 8855 | |
Thierry FOURNIER | fd50f0b | 2015-09-25 18:53:18 +0200 | [diff] [blame] | 8856 | http_txn_reset_req(txn); |
| 8857 | http_txn_reset_res(txn); |
| 8858 | |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 8859 | txn->req.chn = &s->req; |
| 8860 | txn->rsp.chn = &s->res; |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 8861 | |
| 8862 | txn->auth.method = HTTP_AUTH_UNKNOWN; |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 8863 | |
| 8864 | txn->req.err_pos = txn->rsp.err_pos = -2; /* block buggy requests/responses */ |
| 8865 | if (fe->options2 & PR_O2_REQBUG_OK) |
| 8866 | txn->req.err_pos = -1; /* let buggy requests pass */ |
| 8867 | |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 8868 | if (txn->hdr_idx.v) |
| 8869 | hdr_idx_init(&txn->hdr_idx); |
Thierry FOURNIER | 4834bc7 | 2015-06-06 19:29:07 +0200 | [diff] [blame] | 8870 | |
| 8871 | vars_init(&s->vars_txn, SCOPE_TXN); |
| 8872 | vars_init(&s->vars_reqres, SCOPE_REQ); |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 8873 | } |
| 8874 | |
| 8875 | /* to be used at the end of a transaction */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 8876 | void http_end_txn(struct stream *s) |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 8877 | { |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 8878 | struct http_txn *txn = s->txn; |
Willy Tarreau | d0d8da9 | 2015-04-04 02:10:38 +0200 | [diff] [blame] | 8879 | struct proxy *fe = strm_fe(s); |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 8880 | |
| 8881 | /* these ones will have been dynamically allocated */ |
| 8882 | pool_free2(pool2_requri, txn->uri); |
| 8883 | pool_free2(pool2_capture, txn->cli_cookie); |
| 8884 | pool_free2(pool2_capture, txn->srv_cookie); |
William Lallemand | a73203e | 2012-03-12 12:48:57 +0100 | [diff] [blame] | 8885 | pool_free2(pool2_uniqueid, s->unique_id); |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 8886 | |
William Lallemand | a73203e | 2012-03-12 12:48:57 +0100 | [diff] [blame] | 8887 | s->unique_id = NULL; |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 8888 | txn->uri = NULL; |
| 8889 | txn->srv_cookie = NULL; |
| 8890 | txn->cli_cookie = NULL; |
Willy Tarreau | 4602363 | 2010-01-07 22:51:47 +0100 | [diff] [blame] | 8891 | |
Willy Tarreau | cb7dd01 | 2015-04-03 22:16:32 +0200 | [diff] [blame] | 8892 | if (s->req_cap) { |
Willy Tarreau | 4602363 | 2010-01-07 22:51:47 +0100 | [diff] [blame] | 8893 | struct cap_hdr *h; |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 8894 | for (h = fe->req_cap; h; h = h->next) |
Willy Tarreau | cb7dd01 | 2015-04-03 22:16:32 +0200 | [diff] [blame] | 8895 | pool_free2(h->pool, s->req_cap[h->index]); |
| 8896 | memset(s->req_cap, 0, fe->nb_req_cap * sizeof(void *)); |
Willy Tarreau | 4602363 | 2010-01-07 22:51:47 +0100 | [diff] [blame] | 8897 | } |
| 8898 | |
Willy Tarreau | cb7dd01 | 2015-04-03 22:16:32 +0200 | [diff] [blame] | 8899 | if (s->res_cap) { |
Willy Tarreau | 4602363 | 2010-01-07 22:51:47 +0100 | [diff] [blame] | 8900 | struct cap_hdr *h; |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 8901 | for (h = fe->rsp_cap; h; h = h->next) |
Willy Tarreau | cb7dd01 | 2015-04-03 22:16:32 +0200 | [diff] [blame] | 8902 | pool_free2(h->pool, s->res_cap[h->index]); |
| 8903 | memset(s->res_cap, 0, fe->nb_rsp_cap * sizeof(void *)); |
Willy Tarreau | 4602363 | 2010-01-07 22:51:47 +0100 | [diff] [blame] | 8904 | } |
| 8905 | |
Willy Tarreau | 6204cd9 | 2016-03-10 16:33:04 +0100 | [diff] [blame] | 8906 | vars_prune(&s->vars_txn, s->sess, s); |
| 8907 | vars_prune(&s->vars_reqres, s->sess, s); |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 8908 | } |
| 8909 | |
| 8910 | /* 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] | 8911 | void http_reset_txn(struct stream *s) |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 8912 | { |
| 8913 | http_end_txn(s); |
| 8914 | http_init_txn(s); |
| 8915 | |
Thierry FOURNIER | bc4c1ac | 2015-02-25 13:36:14 +0100 | [diff] [blame] | 8916 | /* reinitialise the current rule list pointer to NULL. We are sure that |
| 8917 | * any rulelist match the NULL pointer. |
| 8918 | */ |
| 8919 | s->current_rule_list = NULL; |
| 8920 | |
Willy Tarreau | d0d8da9 | 2015-04-04 02:10:38 +0200 | [diff] [blame] | 8921 | s->be = strm_fe(s); |
| 8922 | s->logs.logwait = strm_fe(s)->to_log; |
Willy Tarreau | abcd514 | 2013-06-11 17:18:02 +0200 | [diff] [blame] | 8923 | s->logs.level = 0; |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 8924 | stream_del_srv_conn(s); |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 8925 | s->target = NULL; |
Emeric Brun | b982a3d | 2010-01-04 15:45:53 +0100 | [diff] [blame] | 8926 | /* re-init store persistence */ |
| 8927 | s->store_count = 0; |
Willy Tarreau | 1f0da24 | 2014-01-25 11:01:50 +0100 | [diff] [blame] | 8928 | s->uniq_id = global.req_count++; |
Emeric Brun | b982a3d | 2010-01-04 15:45:53 +0100 | [diff] [blame] | 8929 | |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 8930 | s->pend_pos = NULL; |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 8931 | |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 8932 | s->req.flags |= CF_READ_DONTWAIT; /* one read is usually enough */ |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 8933 | |
Willy Tarreau | 739cfba | 2010-01-25 23:11:14 +0100 | [diff] [blame] | 8934 | /* We must trim any excess data from the response buffer, because we |
| 8935 | * may have blocked an invalid response from a server that we don't |
| 8936 | * want to accidentely forward once we disable the analysers, nor do |
| 8937 | * we want those data to come along with next response. A typical |
| 8938 | * example of such data would be from a buggy server responding to |
| 8939 | * a HEAD with some data, or sending more than the advertised |
| 8940 | * content-length. |
| 8941 | */ |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 8942 | if (unlikely(s->res.buf->i)) |
| 8943 | s->res.buf->i = 0; |
Willy Tarreau | 739cfba | 2010-01-25 23:11:14 +0100 | [diff] [blame] | 8944 | |
Willy Tarreau | d0d8da9 | 2015-04-04 02:10:38 +0200 | [diff] [blame] | 8945 | s->req.rto = strm_fe(s)->timeout.client; |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 8946 | s->req.wto = TICK_ETERNITY; |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 8947 | |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 8948 | s->res.rto = TICK_ETERNITY; |
Willy Tarreau | d0d8da9 | 2015-04-04 02:10:38 +0200 | [diff] [blame] | 8949 | s->res.wto = strm_fe(s)->timeout.client; |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 8950 | |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 8951 | s->req.rex = TICK_ETERNITY; |
| 8952 | s->req.wex = TICK_ETERNITY; |
| 8953 | s->req.analyse_exp = TICK_ETERNITY; |
| 8954 | s->res.rex = TICK_ETERNITY; |
| 8955 | s->res.wex = TICK_ETERNITY; |
| 8956 | s->res.analyse_exp = TICK_ETERNITY; |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 8957 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 8958 | |
Sasha Pachev | 218f064 | 2014-06-16 12:05:59 -0600 | [diff] [blame] | 8959 | void free_http_res_rules(struct list *r) |
| 8960 | { |
Thierry FOURNIER | a28a942 | 2015-08-04 19:35:46 +0200 | [diff] [blame] | 8961 | struct act_rule *tr, *pr; |
Sasha Pachev | 218f064 | 2014-06-16 12:05:59 -0600 | [diff] [blame] | 8962 | |
| 8963 | list_for_each_entry_safe(pr, tr, r, list) { |
| 8964 | LIST_DEL(&pr->list); |
Thierry FOURNIER | 09af0d6 | 2014-06-18 11:35:54 +0200 | [diff] [blame] | 8965 | regex_free(&pr->arg.hdr_add.re); |
Sasha Pachev | 218f064 | 2014-06-16 12:05:59 -0600 | [diff] [blame] | 8966 | free(pr); |
| 8967 | } |
| 8968 | } |
| 8969 | |
| 8970 | void free_http_req_rules(struct list *r) |
| 8971 | { |
Thierry FOURNIER | a28a942 | 2015-08-04 19:35:46 +0200 | [diff] [blame] | 8972 | struct act_rule *tr, *pr; |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 8973 | |
| 8974 | list_for_each_entry_safe(pr, tr, r, list) { |
| 8975 | LIST_DEL(&pr->list); |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 8976 | if (pr->action == ACT_HTTP_REQ_AUTH) |
Willy Tarreau | 5c2e198 | 2012-12-24 12:00:25 +0100 | [diff] [blame] | 8977 | free(pr->arg.auth.realm); |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 8978 | |
Thierry FOURNIER | 09af0d6 | 2014-06-18 11:35:54 +0200 | [diff] [blame] | 8979 | regex_free(&pr->arg.hdr_add.re); |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 8980 | free(pr); |
| 8981 | } |
| 8982 | } |
| 8983 | |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 8984 | /* parse an "http-request" rule */ |
Thierry FOURNIER | a28a942 | 2015-08-04 19:35:46 +0200 | [diff] [blame] | 8985 | struct act_rule *parse_http_req_cond(const char **args, const char *file, int linenum, struct proxy *proxy) |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 8986 | { |
Thierry FOURNIER | a28a942 | 2015-08-04 19:35:46 +0200 | [diff] [blame] | 8987 | struct act_rule *rule; |
Thierry FOURNIER | 36481b8 | 2015-08-19 09:01:53 +0200 | [diff] [blame] | 8988 | struct action_kw *custom = NULL; |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 8989 | int cur_arg; |
Thierry FOURNIER | 09af0d6 | 2014-06-18 11:35:54 +0200 | [diff] [blame] | 8990 | char *error; |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 8991 | |
Vincent Bernat | 02779b6 | 2016-04-03 13:48:43 +0200 | [diff] [blame] | 8992 | rule = calloc(1, sizeof(*rule)); |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 8993 | if (!rule) { |
| 8994 | Alert("parsing [%s:%d]: out of memory.\n", file, linenum); |
Willy Tarreau | 81499eb | 2012-12-27 12:19:02 +0100 | [diff] [blame] | 8995 | goto out_err; |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 8996 | } |
| 8997 | |
CJ Ess | 108b1dd | 2015-04-07 12:03:37 -0400 | [diff] [blame] | 8998 | rule->deny_status = HTTP_ERR_403; |
Willy Tarreau | 5c2e198 | 2012-12-24 12:00:25 +0100 | [diff] [blame] | 8999 | if (!strcmp(args[0], "allow")) { |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 9000 | rule->action = ACT_ACTION_ALLOW; |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 9001 | cur_arg = 1; |
Willy Tarreau | 5bd6759 | 2014-04-28 22:00:46 +0200 | [diff] [blame] | 9002 | } else if (!strcmp(args[0], "deny") || !strcmp(args[0], "block")) { |
CJ Ess | 108b1dd | 2015-04-07 12:03:37 -0400 | [diff] [blame] | 9003 | int code; |
| 9004 | int hc; |
| 9005 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 9006 | rule->action = ACT_ACTION_DENY; |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 9007 | cur_arg = 1; |
CJ Ess | 108b1dd | 2015-04-07 12:03:37 -0400 | [diff] [blame] | 9008 | if (strcmp(args[cur_arg], "deny_status") == 0) { |
| 9009 | cur_arg++; |
| 9010 | if (!args[cur_arg]) { |
| 9011 | Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : missing status code.\n", |
| 9012 | file, linenum, proxy_type_str(proxy), proxy->id, args[0]); |
| 9013 | goto out_err; |
| 9014 | } |
| 9015 | |
| 9016 | code = atol(args[cur_arg]); |
| 9017 | cur_arg++; |
| 9018 | for (hc = 0; hc < HTTP_ERR_SIZE; hc++) { |
| 9019 | if (http_err_codes[hc] == code) { |
| 9020 | rule->deny_status = hc; |
| 9021 | break; |
| 9022 | } |
| 9023 | } |
| 9024 | |
| 9025 | if (hc >= HTTP_ERR_SIZE) { |
| 9026 | Warning("parsing [%s:%d] : status code %d not handled, using default code 403.\n", |
| 9027 | file, linenum, code); |
| 9028 | } |
| 9029 | } |
Willy Tarreau | ccbcc37 | 2012-12-27 12:37:57 +0100 | [diff] [blame] | 9030 | } else if (!strcmp(args[0], "tarpit")) { |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 9031 | rule->action = ACT_HTTP_REQ_TARPIT; |
Willy Tarreau | ccbcc37 | 2012-12-27 12:37:57 +0100 | [diff] [blame] | 9032 | cur_arg = 1; |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 9033 | } else if (!strcmp(args[0], "auth")) { |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 9034 | rule->action = ACT_HTTP_REQ_AUTH; |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 9035 | cur_arg = 1; |
| 9036 | |
| 9037 | while(*args[cur_arg]) { |
| 9038 | if (!strcmp(args[cur_arg], "realm")) { |
Willy Tarreau | 5c2e198 | 2012-12-24 12:00:25 +0100 | [diff] [blame] | 9039 | rule->arg.auth.realm = strdup(args[cur_arg + 1]); |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 9040 | cur_arg+=2; |
| 9041 | continue; |
| 9042 | } else |
| 9043 | break; |
| 9044 | } |
Willy Tarreau | f4c43c1 | 2013-06-11 17:01:13 +0200 | [diff] [blame] | 9045 | } else if (!strcmp(args[0], "set-nice")) { |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 9046 | rule->action = ACT_HTTP_SET_NICE; |
Willy Tarreau | f4c43c1 | 2013-06-11 17:01:13 +0200 | [diff] [blame] | 9047 | cur_arg = 1; |
| 9048 | |
| 9049 | if (!*args[cur_arg] || |
| 9050 | (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) { |
| 9051 | Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (integer value).\n", |
| 9052 | file, linenum, args[0]); |
| 9053 | goto out_err; |
| 9054 | } |
| 9055 | rule->arg.nice = atoi(args[cur_arg]); |
| 9056 | if (rule->arg.nice < -1024) |
| 9057 | rule->arg.nice = -1024; |
| 9058 | else if (rule->arg.nice > 1024) |
| 9059 | rule->arg.nice = 1024; |
| 9060 | cur_arg++; |
Willy Tarreau | 42cf39e | 2013-06-11 18:51:32 +0200 | [diff] [blame] | 9061 | } else if (!strcmp(args[0], "set-tos")) { |
| 9062 | #ifdef IP_TOS |
| 9063 | char *err; |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 9064 | rule->action = ACT_HTTP_SET_TOS; |
Willy Tarreau | 42cf39e | 2013-06-11 18:51:32 +0200 | [diff] [blame] | 9065 | cur_arg = 1; |
| 9066 | |
| 9067 | if (!*args[cur_arg] || |
| 9068 | (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) { |
| 9069 | Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (integer/hex value).\n", |
| 9070 | file, linenum, args[0]); |
| 9071 | goto out_err; |
| 9072 | } |
| 9073 | |
| 9074 | rule->arg.tos = strtol(args[cur_arg], &err, 0); |
| 9075 | if (err && *err != '\0') { |
| 9076 | Alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-request %s' (integer/hex value expected).\n", |
| 9077 | file, linenum, err, args[0]); |
| 9078 | goto out_err; |
| 9079 | } |
| 9080 | cur_arg++; |
| 9081 | #else |
| 9082 | Alert("parsing [%s:%d]: 'http-request %s' is not supported on this platform (IP_TOS undefined).\n", file, linenum, args[0]); |
| 9083 | goto out_err; |
| 9084 | #endif |
Willy Tarreau | 51347ed | 2013-06-11 19:34:13 +0200 | [diff] [blame] | 9085 | } else if (!strcmp(args[0], "set-mark")) { |
| 9086 | #ifdef SO_MARK |
| 9087 | char *err; |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 9088 | rule->action = ACT_HTTP_SET_MARK; |
Willy Tarreau | 51347ed | 2013-06-11 19:34:13 +0200 | [diff] [blame] | 9089 | cur_arg = 1; |
| 9090 | |
| 9091 | if (!*args[cur_arg] || |
| 9092 | (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) { |
| 9093 | Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (integer/hex value).\n", |
| 9094 | file, linenum, args[0]); |
| 9095 | goto out_err; |
| 9096 | } |
| 9097 | |
| 9098 | rule->arg.mark = strtoul(args[cur_arg], &err, 0); |
| 9099 | if (err && *err != '\0') { |
| 9100 | Alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-request %s' (integer/hex value expected).\n", |
| 9101 | file, linenum, err, args[0]); |
| 9102 | goto out_err; |
| 9103 | } |
| 9104 | cur_arg++; |
| 9105 | global.last_checks |= LSTCHK_NETADM; |
| 9106 | #else |
| 9107 | Alert("parsing [%s:%d]: 'http-request %s' is not supported on this platform (SO_MARK undefined).\n", file, linenum, args[0]); |
| 9108 | goto out_err; |
| 9109 | #endif |
Willy Tarreau | 9a355ec | 2013-06-11 17:45:46 +0200 | [diff] [blame] | 9110 | } else if (!strcmp(args[0], "set-log-level")) { |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 9111 | rule->action = ACT_HTTP_SET_LOGL; |
Willy Tarreau | 9a355ec | 2013-06-11 17:45:46 +0200 | [diff] [blame] | 9112 | cur_arg = 1; |
| 9113 | |
| 9114 | if (!*args[cur_arg] || |
| 9115 | (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) { |
| 9116 | bad_log_level: |
| 9117 | Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (log level name or 'silent').\n", |
| 9118 | file, linenum, args[0]); |
| 9119 | goto out_err; |
| 9120 | } |
| 9121 | if (strcmp(args[cur_arg], "silent") == 0) |
| 9122 | rule->arg.loglevel = -1; |
| 9123 | else if ((rule->arg.loglevel = get_log_level(args[cur_arg]) + 1) == 0) |
| 9124 | goto bad_log_level; |
| 9125 | cur_arg++; |
Willy Tarreau | 20b0de5 | 2012-12-24 15:45:22 +0100 | [diff] [blame] | 9126 | } else if (strcmp(args[0], "add-header") == 0 || strcmp(args[0], "set-header") == 0) { |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 9127 | rule->action = *args[0] == 'a' ? ACT_HTTP_ADD_HDR : ACT_HTTP_SET_HDR; |
Willy Tarreau | 20b0de5 | 2012-12-24 15:45:22 +0100 | [diff] [blame] | 9128 | cur_arg = 1; |
| 9129 | |
Willy Tarreau | 8d1c516 | 2013-04-03 14:13:58 +0200 | [diff] [blame] | 9130 | if (!*args[cur_arg] || !*args[cur_arg+1] || |
| 9131 | (*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] | 9132 | Alert("parsing [%s:%d]: 'http-request %s' expects exactly 2 arguments.\n", |
| 9133 | file, linenum, args[0]); |
Willy Tarreau | 81499eb | 2012-12-27 12:19:02 +0100 | [diff] [blame] | 9134 | goto out_err; |
Willy Tarreau | 20b0de5 | 2012-12-24 15:45:22 +0100 | [diff] [blame] | 9135 | } |
| 9136 | |
| 9137 | rule->arg.hdr_add.name = strdup(args[cur_arg]); |
| 9138 | rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name); |
| 9139 | LIST_INIT(&rule->arg.hdr_add.fmt); |
Willy Tarreau | a4312fa | 2013-04-02 16:34:32 +0200 | [diff] [blame] | 9140 | |
Thierry FOURNIER | 1c0054f | 2013-11-20 15:09:52 +0100 | [diff] [blame] | 9141 | proxy->conf.args.ctx = ARGC_HRQ; |
Thierry FOURNIER / OZON.IO | 8a4e442 | 2016-11-23 00:41:28 +0100 | [diff] [blame] | 9142 | error = NULL; |
Thierry FOURNIER / OZON.IO | 59fd511 | 2016-11-22 23:50:02 +0100 | [diff] [blame] | 9143 | if (!parse_logformat_string(args[cur_arg + 1], proxy, &rule->arg.hdr_add.fmt, LOG_OPT_HTTP, |
Thierry FOURNIER / OZON.IO | 8a4e442 | 2016-11-23 00:41:28 +0100 | [diff] [blame] | 9144 | (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR, &error)) { |
| 9145 | Alert("parsing [%s:%d]: 'http-request %s': %s.\n", |
| 9146 | file, linenum, args[0], error); |
| 9147 | free(error); |
Thierry FOURNIER / OZON.IO | 59fd511 | 2016-11-22 23:50:02 +0100 | [diff] [blame] | 9148 | goto out_err; |
| 9149 | } |
Willy Tarreau | 59ad1a2 | 2014-01-29 14:39:58 +0100 | [diff] [blame] | 9150 | free(proxy->conf.lfs_file); |
| 9151 | proxy->conf.lfs_file = strdup(proxy->conf.args.file); |
| 9152 | proxy->conf.lfs_line = proxy->conf.args.line; |
Willy Tarreau | 20b0de5 | 2012-12-24 15:45:22 +0100 | [diff] [blame] | 9153 | cur_arg += 2; |
Willy Tarreau | b854392 | 2014-06-17 18:58:26 +0200 | [diff] [blame] | 9154 | } else if (strcmp(args[0], "replace-header") == 0 || strcmp(args[0], "replace-value") == 0) { |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 9155 | rule->action = args[0][8] == 'h' ? ACT_HTTP_REPLACE_HDR : ACT_HTTP_REPLACE_VAL; |
Sasha Pachev | 218f064 | 2014-06-16 12:05:59 -0600 | [diff] [blame] | 9156 | cur_arg = 1; |
| 9157 | |
| 9158 | if (!*args[cur_arg] || !*args[cur_arg+1] || !*args[cur_arg+2] || |
Baptiste Assmann | 92df370 | 2014-06-24 11:10:00 +0200 | [diff] [blame] | 9159 | (*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] | 9160 | Alert("parsing [%s:%d]: 'http-request %s' expects exactly 3 arguments.\n", |
| 9161 | file, linenum, args[0]); |
| 9162 | goto out_err; |
| 9163 | } |
| 9164 | |
| 9165 | rule->arg.hdr_add.name = strdup(args[cur_arg]); |
| 9166 | rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name); |
| 9167 | LIST_INIT(&rule->arg.hdr_add.fmt); |
| 9168 | |
Thierry FOURNIER | 09af0d6 | 2014-06-18 11:35:54 +0200 | [diff] [blame] | 9169 | error = NULL; |
| 9170 | if (!regex_comp(args[cur_arg + 1], &rule->arg.hdr_add.re, 1, 1, &error)) { |
| 9171 | Alert("parsing [%s:%d] : '%s' : %s.\n", file, linenum, |
| 9172 | args[cur_arg + 1], error); |
| 9173 | free(error); |
Sasha Pachev | 218f064 | 2014-06-16 12:05:59 -0600 | [diff] [blame] | 9174 | goto out_err; |
| 9175 | } |
| 9176 | |
| 9177 | proxy->conf.args.ctx = ARGC_HRQ; |
Thierry FOURNIER / OZON.IO | 8a4e442 | 2016-11-23 00:41:28 +0100 | [diff] [blame] | 9178 | error = NULL; |
Thierry FOURNIER / OZON.IO | 59fd511 | 2016-11-22 23:50:02 +0100 | [diff] [blame] | 9179 | if (!parse_logformat_string(args[cur_arg + 2], proxy, &rule->arg.hdr_add.fmt, LOG_OPT_HTTP, |
Thierry FOURNIER / OZON.IO | 8a4e442 | 2016-11-23 00:41:28 +0100 | [diff] [blame] | 9180 | (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR, &error)) { |
| 9181 | Alert("parsing [%s:%d]: 'http-request %s': %s.\n", |
| 9182 | file, linenum, args[0], error); |
| 9183 | free(error); |
Thierry FOURNIER / OZON.IO | 59fd511 | 2016-11-22 23:50:02 +0100 | [diff] [blame] | 9184 | goto out_err; |
| 9185 | } |
Sasha Pachev | 218f064 | 2014-06-16 12:05:59 -0600 | [diff] [blame] | 9186 | |
| 9187 | free(proxy->conf.lfs_file); |
| 9188 | proxy->conf.lfs_file = strdup(proxy->conf.args.file); |
| 9189 | proxy->conf.lfs_line = proxy->conf.args.line; |
| 9190 | cur_arg += 3; |
Thierry FOURNIER | dad3d1d | 2014-04-22 18:07:25 +0200 | [diff] [blame] | 9191 | } else if (strcmp(args[0], "del-header") == 0) { |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 9192 | rule->action = ACT_HTTP_DEL_HDR; |
Thierry FOURNIER | dad3d1d | 2014-04-22 18:07:25 +0200 | [diff] [blame] | 9193 | cur_arg = 1; |
| 9194 | |
| 9195 | if (!*args[cur_arg] || |
| 9196 | (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) { |
| 9197 | Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument.\n", |
| 9198 | file, linenum, args[0]); |
| 9199 | goto out_err; |
| 9200 | } |
| 9201 | |
| 9202 | rule->arg.hdr_add.name = strdup(args[cur_arg]); |
| 9203 | rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name); |
| 9204 | |
| 9205 | proxy->conf.args.ctx = ARGC_HRQ; |
| 9206 | free(proxy->conf.lfs_file); |
| 9207 | proxy->conf.lfs_file = strdup(proxy->conf.args.file); |
| 9208 | proxy->conf.lfs_line = proxy->conf.args.line; |
| 9209 | cur_arg += 1; |
Willy Tarreau | 09448f7 | 2014-06-25 18:12:15 +0200 | [diff] [blame] | 9210 | } else if (strncmp(args[0], "track-sc", 8) == 0 && |
| 9211 | args[0][9] == '\0' && args[0][8] >= '0' && |
Willy Tarreau | e1cfc1f | 2014-10-17 11:53:05 +0200 | [diff] [blame] | 9212 | args[0][8] < '0' + MAX_SESS_STKCTR) { /* track-sc 0..9 */ |
Willy Tarreau | 09448f7 | 2014-06-25 18:12:15 +0200 | [diff] [blame] | 9213 | struct sample_expr *expr; |
| 9214 | unsigned int where; |
| 9215 | char *err = NULL; |
| 9216 | |
| 9217 | cur_arg = 1; |
| 9218 | proxy->conf.args.ctx = ARGC_TRK; |
| 9219 | |
| 9220 | expr = sample_parse_expr((char **)args, &cur_arg, file, linenum, &err, &proxy->conf.args); |
| 9221 | if (!expr) { |
| 9222 | Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : %s.\n", |
| 9223 | file, linenum, proxy_type_str(proxy), proxy->id, args[0], err); |
| 9224 | free(err); |
| 9225 | goto out_err; |
| 9226 | } |
| 9227 | |
| 9228 | where = 0; |
| 9229 | if (proxy->cap & PR_CAP_FE) |
| 9230 | where |= SMP_VAL_FE_HRQ_HDR; |
| 9231 | if (proxy->cap & PR_CAP_BE) |
| 9232 | where |= SMP_VAL_BE_HRQ_HDR; |
| 9233 | |
| 9234 | if (!(expr->fetch->val & where)) { |
| 9235 | Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule :" |
| 9236 | " fetch method '%s' extracts information from '%s', none of which is available here.\n", |
| 9237 | file, linenum, proxy_type_str(proxy), proxy->id, args[0], |
| 9238 | args[cur_arg-1], sample_src_names(expr->fetch->use)); |
| 9239 | free(expr); |
| 9240 | goto out_err; |
| 9241 | } |
| 9242 | |
| 9243 | if (strcmp(args[cur_arg], "table") == 0) { |
| 9244 | cur_arg++; |
| 9245 | if (!args[cur_arg]) { |
| 9246 | Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : missing table name.\n", |
| 9247 | file, linenum, proxy_type_str(proxy), proxy->id, args[0]); |
| 9248 | free(expr); |
| 9249 | goto out_err; |
| 9250 | } |
| 9251 | /* we copy the table name for now, it will be resolved later */ |
Thierry FOURNIER | 5ec63e0 | 2015-08-04 09:09:48 +0200 | [diff] [blame] | 9252 | rule->arg.trk_ctr.table.n = strdup(args[cur_arg]); |
Willy Tarreau | 09448f7 | 2014-06-25 18:12:15 +0200 | [diff] [blame] | 9253 | cur_arg++; |
| 9254 | } |
Thierry FOURNIER | 5ec63e0 | 2015-08-04 09:09:48 +0200 | [diff] [blame] | 9255 | rule->arg.trk_ctr.expr = expr; |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 9256 | rule->action = ACT_ACTION_TRK_SC0 + args[0][8] - '0'; |
Willy Tarreau | 81499eb | 2012-12-27 12:19:02 +0100 | [diff] [blame] | 9257 | } else if (strcmp(args[0], "redirect") == 0) { |
| 9258 | struct redirect_rule *redir; |
Willy Tarreau | 6d4890c | 2013-11-18 18:04:25 +0100 | [diff] [blame] | 9259 | char *errmsg = NULL; |
Willy Tarreau | 81499eb | 2012-12-27 12:19:02 +0100 | [diff] [blame] | 9260 | |
Willy Tarreau | be4653b | 2015-05-28 15:26:58 +0200 | [diff] [blame] | 9261 | 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] | 9262 | Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : %s.\n", |
| 9263 | file, linenum, proxy_type_str(proxy), proxy->id, args[0], errmsg); |
| 9264 | goto out_err; |
| 9265 | } |
| 9266 | |
| 9267 | /* this redirect rule might already contain a parsed condition which |
| 9268 | * we'll pass to the http-request rule. |
| 9269 | */ |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 9270 | rule->action = ACT_HTTP_REDIR; |
Willy Tarreau | 81499eb | 2012-12-27 12:19:02 +0100 | [diff] [blame] | 9271 | rule->arg.redir = redir; |
| 9272 | rule->cond = redir->cond; |
| 9273 | redir->cond = NULL; |
| 9274 | cur_arg = 2; |
| 9275 | return rule; |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 9276 | } else if (strncmp(args[0], "add-acl", 7) == 0) { |
| 9277 | /* http-request add-acl(<reference (acl name)>) <key pattern> */ |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 9278 | rule->action = ACT_HTTP_ADD_ACL; |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 9279 | /* |
| 9280 | * '+ 8' for 'add-acl(' |
| 9281 | * '- 9' for 'add-acl(' + trailing ')' |
| 9282 | */ |
Willy Tarreau | 6c09c2c | 2014-04-25 21:38:08 +0200 | [diff] [blame] | 9283 | 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] | 9284 | |
| 9285 | cur_arg = 1; |
| 9286 | |
| 9287 | if (!*args[cur_arg] || |
| 9288 | (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) { |
| 9289 | Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument.\n", |
| 9290 | file, linenum, args[0]); |
| 9291 | goto out_err; |
| 9292 | } |
| 9293 | |
| 9294 | LIST_INIT(&rule->arg.map.key); |
| 9295 | proxy->conf.args.ctx = ARGC_HRQ; |
Thierry FOURNIER / OZON.IO | 8a4e442 | 2016-11-23 00:41:28 +0100 | [diff] [blame] | 9296 | error = NULL; |
Thierry FOURNIER / OZON.IO | 59fd511 | 2016-11-22 23:50:02 +0100 | [diff] [blame] | 9297 | if (!parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP, |
Thierry FOURNIER / OZON.IO | 8a4e442 | 2016-11-23 00:41:28 +0100 | [diff] [blame] | 9298 | (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR, &error)) { |
| 9299 | Alert("parsing [%s:%d]: 'http-request %s': %s.\n", |
| 9300 | file, linenum, args[0], error); |
| 9301 | free(error); |
Thierry FOURNIER / OZON.IO | 59fd511 | 2016-11-22 23:50:02 +0100 | [diff] [blame] | 9302 | goto out_err; |
| 9303 | } |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 9304 | free(proxy->conf.lfs_file); |
| 9305 | proxy->conf.lfs_file = strdup(proxy->conf.args.file); |
| 9306 | proxy->conf.lfs_line = proxy->conf.args.line; |
| 9307 | cur_arg += 1; |
| 9308 | } else if (strncmp(args[0], "del-acl", 7) == 0) { |
| 9309 | /* http-request del-acl(<reference (acl name)>) <key pattern> */ |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 9310 | rule->action = ACT_HTTP_DEL_ACL; |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 9311 | /* |
| 9312 | * '+ 8' for 'del-acl(' |
| 9313 | * '- 9' for 'del-acl(' + trailing ')' |
| 9314 | */ |
Willy Tarreau | 6c09c2c | 2014-04-25 21:38:08 +0200 | [diff] [blame] | 9315 | 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] | 9316 | |
| 9317 | cur_arg = 1; |
| 9318 | |
| 9319 | if (!*args[cur_arg] || |
| 9320 | (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) { |
| 9321 | Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument.\n", |
| 9322 | file, linenum, args[0]); |
| 9323 | goto out_err; |
| 9324 | } |
| 9325 | |
| 9326 | LIST_INIT(&rule->arg.map.key); |
| 9327 | proxy->conf.args.ctx = ARGC_HRQ; |
Thierry FOURNIER / OZON.IO | 8a4e442 | 2016-11-23 00:41:28 +0100 | [diff] [blame] | 9328 | error = NULL; |
Thierry FOURNIER / OZON.IO | 59fd511 | 2016-11-22 23:50:02 +0100 | [diff] [blame] | 9329 | if (!parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP, |
Thierry FOURNIER / OZON.IO | 8a4e442 | 2016-11-23 00:41:28 +0100 | [diff] [blame] | 9330 | (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR, &error)) { |
| 9331 | Alert("parsing [%s:%d]: 'http-request %s': %s.\n", |
| 9332 | file, linenum, args[0], error); |
| 9333 | free(error); |
Thierry FOURNIER / OZON.IO | 59fd511 | 2016-11-22 23:50:02 +0100 | [diff] [blame] | 9334 | goto out_err; |
| 9335 | } |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 9336 | free(proxy->conf.lfs_file); |
| 9337 | proxy->conf.lfs_file = strdup(proxy->conf.args.file); |
| 9338 | proxy->conf.lfs_line = proxy->conf.args.line; |
| 9339 | cur_arg += 1; |
| 9340 | } else if (strncmp(args[0], "del-map", 7) == 0) { |
| 9341 | /* http-request del-map(<reference (map name)>) <key pattern> */ |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 9342 | rule->action = ACT_HTTP_DEL_MAP; |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 9343 | /* |
| 9344 | * '+ 8' for 'del-map(' |
| 9345 | * '- 9' for 'del-map(' + trailing ')' |
| 9346 | */ |
Willy Tarreau | 6c09c2c | 2014-04-25 21:38:08 +0200 | [diff] [blame] | 9347 | 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] | 9348 | |
| 9349 | cur_arg = 1; |
| 9350 | |
| 9351 | if (!*args[cur_arg] || |
| 9352 | (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) { |
| 9353 | Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument.\n", |
| 9354 | file, linenum, args[0]); |
| 9355 | goto out_err; |
| 9356 | } |
| 9357 | |
| 9358 | LIST_INIT(&rule->arg.map.key); |
| 9359 | proxy->conf.args.ctx = ARGC_HRQ; |
Thierry FOURNIER / OZON.IO | 8a4e442 | 2016-11-23 00:41:28 +0100 | [diff] [blame] | 9360 | error = NULL; |
Thierry FOURNIER / OZON.IO | 59fd511 | 2016-11-22 23:50:02 +0100 | [diff] [blame] | 9361 | if (!parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP, |
Thierry FOURNIER / OZON.IO | 8a4e442 | 2016-11-23 00:41:28 +0100 | [diff] [blame] | 9362 | (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR, &error)) { |
| 9363 | Alert("parsing [%s:%d]: 'http-request %s': %s.\n", |
| 9364 | file, linenum, args[0], error); |
| 9365 | free(error); |
Thierry FOURNIER / OZON.IO | 59fd511 | 2016-11-22 23:50:02 +0100 | [diff] [blame] | 9366 | goto out_err; |
| 9367 | } |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 9368 | free(proxy->conf.lfs_file); |
| 9369 | proxy->conf.lfs_file = strdup(proxy->conf.args.file); |
| 9370 | proxy->conf.lfs_line = proxy->conf.args.line; |
| 9371 | cur_arg += 1; |
| 9372 | } else if (strncmp(args[0], "set-map", 7) == 0) { |
| 9373 | /* http-request set-map(<reference (map name)>) <key pattern> <value pattern> */ |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 9374 | rule->action = ACT_HTTP_SET_MAP; |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 9375 | /* |
| 9376 | * '+ 8' for 'set-map(' |
| 9377 | * '- 9' for 'set-map(' + trailing ')' |
| 9378 | */ |
Willy Tarreau | 6c09c2c | 2014-04-25 21:38:08 +0200 | [diff] [blame] | 9379 | 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] | 9380 | |
| 9381 | cur_arg = 1; |
| 9382 | |
| 9383 | if (!*args[cur_arg] || !*args[cur_arg+1] || |
| 9384 | (*args[cur_arg+2] && strcmp(args[cur_arg+2], "if") != 0 && strcmp(args[cur_arg+2], "unless") != 0)) { |
| 9385 | Alert("parsing [%s:%d]: 'http-request %s' expects exactly 2 arguments.\n", |
| 9386 | file, linenum, args[0]); |
| 9387 | goto out_err; |
| 9388 | } |
| 9389 | |
| 9390 | LIST_INIT(&rule->arg.map.key); |
| 9391 | LIST_INIT(&rule->arg.map.value); |
| 9392 | proxy->conf.args.ctx = ARGC_HRQ; |
| 9393 | |
| 9394 | /* key pattern */ |
Thierry FOURNIER / OZON.IO | 8a4e442 | 2016-11-23 00:41:28 +0100 | [diff] [blame] | 9395 | error = NULL; |
Thierry FOURNIER / OZON.IO | 59fd511 | 2016-11-22 23:50:02 +0100 | [diff] [blame] | 9396 | if (!parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP, |
Thierry FOURNIER / OZON.IO | 8a4e442 | 2016-11-23 00:41:28 +0100 | [diff] [blame] | 9397 | (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR, &error)) { |
| 9398 | Alert("parsing [%s:%d]: 'http-request %s' key: %s.\n", |
| 9399 | file, linenum, args[0], error); |
| 9400 | free(error); |
Thierry FOURNIER / OZON.IO | 59fd511 | 2016-11-22 23:50:02 +0100 | [diff] [blame] | 9401 | goto out_err; |
| 9402 | } |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 9403 | |
| 9404 | /* value pattern */ |
Thierry FOURNIER / OZON.IO | 8a4e442 | 2016-11-23 00:41:28 +0100 | [diff] [blame] | 9405 | error = NULL; |
Thierry FOURNIER / OZON.IO | 59fd511 | 2016-11-22 23:50:02 +0100 | [diff] [blame] | 9406 | if (!parse_logformat_string(args[cur_arg + 1], proxy, &rule->arg.map.value, LOG_OPT_HTTP, |
Thierry FOURNIER / OZON.IO | 8a4e442 | 2016-11-23 00:41:28 +0100 | [diff] [blame] | 9407 | (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR, &error)) { |
| 9408 | Alert("parsing [%s:%d]: 'http-request %s' pattern: %s.\n", |
| 9409 | file, linenum, args[0], error); |
| 9410 | free(error); |
Thierry FOURNIER / OZON.IO | 59fd511 | 2016-11-22 23:50:02 +0100 | [diff] [blame] | 9411 | goto out_err; |
| 9412 | } |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 9413 | free(proxy->conf.lfs_file); |
| 9414 | proxy->conf.lfs_file = strdup(proxy->conf.args.file); |
| 9415 | proxy->conf.lfs_line = proxy->conf.args.line; |
| 9416 | |
| 9417 | cur_arg += 2; |
William Lallemand | 73025dd | 2014-04-24 14:38:37 +0200 | [diff] [blame] | 9418 | } else if (((custom = action_http_req_custom(args[0])) != NULL)) { |
| 9419 | char *errmsg = NULL; |
| 9420 | cur_arg = 1; |
| 9421 | /* try in the module list */ |
Thierry FOURNIER | 5563e4b | 2015-08-14 19:20:07 +0200 | [diff] [blame] | 9422 | rule->from = ACT_F_HTTP_REQ; |
Thierry FOURNIER | 85c6c97 | 2015-09-22 19:14:35 +0200 | [diff] [blame] | 9423 | rule->kw = custom; |
Thierry FOURNIER | afa8049 | 2015-08-19 09:04:15 +0200 | [diff] [blame] | 9424 | if (custom->parse(args, &cur_arg, proxy, rule, &errmsg) == ACT_RET_PRS_ERR) { |
William Lallemand | 73025dd | 2014-04-24 14:38:37 +0200 | [diff] [blame] | 9425 | Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : %s.\n", |
| 9426 | file, linenum, proxy_type_str(proxy), proxy->id, args[0], errmsg); |
| 9427 | free(errmsg); |
| 9428 | goto out_err; |
| 9429 | } |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 9430 | } else { |
Thierry FOURNIER | ab95e65 | 2015-10-02 08:24:51 +0200 | [diff] [blame] | 9431 | action_build_list(&http_req_keywords.list, &trash); |
| 9432 | Alert("parsing [%s:%d]: 'http-request' expects 'allow', 'deny', 'auth', 'redirect', " |
| 9433 | "'tarpit', 'add-header', 'set-header', 'replace-header', 'replace-value', 'set-nice', " |
Ruoshan Huang | e4edc6b | 2016-07-14 15:07:45 +0800 | [diff] [blame] | 9434 | "'set-tos', 'set-mark', 'set-log-level', 'add-acl', 'del-acl', 'del-map', 'set-map', 'track-sc*'" |
William Lallemand | 2e785f2 | 2016-05-25 01:48:42 +0200 | [diff] [blame] | 9435 | "%s%s, but got '%s'%s.\n", |
Thierry FOURNIER | ab95e65 | 2015-10-02 08:24:51 +0200 | [diff] [blame] | 9436 | file, linenum, *trash.str ? ", " : "", trash.str, args[0], *args[0] ? "" : " (missing argument)"); |
Willy Tarreau | 81499eb | 2012-12-27 12:19:02 +0100 | [diff] [blame] | 9437 | goto out_err; |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 9438 | } |
| 9439 | |
| 9440 | if (strcmp(args[cur_arg], "if") == 0 || strcmp(args[cur_arg], "unless") == 0) { |
| 9441 | struct acl_cond *cond; |
Willy Tarreau | b7451bb | 2012-04-27 12:38:15 +0200 | [diff] [blame] | 9442 | char *errmsg = NULL; |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 9443 | |
Willy Tarreau | b7451bb | 2012-04-27 12:38:15 +0200 | [diff] [blame] | 9444 | if ((cond = build_acl_cond(file, linenum, proxy, args+cur_arg, &errmsg)) == NULL) { |
| 9445 | Alert("parsing [%s:%d] : error detected while parsing an 'http-request %s' condition : %s.\n", |
| 9446 | file, linenum, args[0], errmsg); |
| 9447 | free(errmsg); |
Willy Tarreau | 81499eb | 2012-12-27 12:19:02 +0100 | [diff] [blame] | 9448 | goto out_err; |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 9449 | } |
| 9450 | rule->cond = cond; |
| 9451 | } |
| 9452 | else if (*args[cur_arg]) { |
| 9453 | Alert("parsing [%s:%d]: 'http-request %s' expects 'realm' for 'auth' or" |
| 9454 | " either 'if' or 'unless' followed by a condition but found '%s'.\n", |
| 9455 | file, linenum, args[0], args[cur_arg]); |
Willy Tarreau | 81499eb | 2012-12-27 12:19:02 +0100 | [diff] [blame] | 9456 | goto out_err; |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 9457 | } |
| 9458 | |
| 9459 | return rule; |
Willy Tarreau | 81499eb | 2012-12-27 12:19:02 +0100 | [diff] [blame] | 9460 | out_err: |
| 9461 | free(rule); |
| 9462 | return NULL; |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 9463 | } |
| 9464 | |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 9465 | /* parse an "http-respose" rule */ |
Thierry FOURNIER | a28a942 | 2015-08-04 19:35:46 +0200 | [diff] [blame] | 9466 | struct act_rule *parse_http_res_cond(const char **args, const char *file, int linenum, struct proxy *proxy) |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 9467 | { |
Thierry FOURNIER | a28a942 | 2015-08-04 19:35:46 +0200 | [diff] [blame] | 9468 | struct act_rule *rule; |
Thierry FOURNIER | 36481b8 | 2015-08-19 09:01:53 +0200 | [diff] [blame] | 9469 | struct action_kw *custom = NULL; |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 9470 | int cur_arg; |
Thierry FOURNIER | 09af0d6 | 2014-06-18 11:35:54 +0200 | [diff] [blame] | 9471 | char *error; |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 9472 | |
| 9473 | rule = calloc(1, sizeof(*rule)); |
| 9474 | if (!rule) { |
| 9475 | Alert("parsing [%s:%d]: out of memory.\n", file, linenum); |
| 9476 | goto out_err; |
| 9477 | } |
| 9478 | |
| 9479 | if (!strcmp(args[0], "allow")) { |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 9480 | rule->action = ACT_ACTION_ALLOW; |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 9481 | cur_arg = 1; |
| 9482 | } else if (!strcmp(args[0], "deny")) { |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 9483 | rule->action = ACT_ACTION_DENY; |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 9484 | cur_arg = 1; |
Willy Tarreau | f4c43c1 | 2013-06-11 17:01:13 +0200 | [diff] [blame] | 9485 | } else if (!strcmp(args[0], "set-nice")) { |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 9486 | rule->action = ACT_HTTP_SET_NICE; |
Willy Tarreau | f4c43c1 | 2013-06-11 17:01:13 +0200 | [diff] [blame] | 9487 | cur_arg = 1; |
| 9488 | |
| 9489 | if (!*args[cur_arg] || |
| 9490 | (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) { |
| 9491 | Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (integer value).\n", |
| 9492 | file, linenum, args[0]); |
| 9493 | goto out_err; |
| 9494 | } |
| 9495 | rule->arg.nice = atoi(args[cur_arg]); |
| 9496 | if (rule->arg.nice < -1024) |
| 9497 | rule->arg.nice = -1024; |
| 9498 | else if (rule->arg.nice > 1024) |
| 9499 | rule->arg.nice = 1024; |
| 9500 | cur_arg++; |
Willy Tarreau | 42cf39e | 2013-06-11 18:51:32 +0200 | [diff] [blame] | 9501 | } else if (!strcmp(args[0], "set-tos")) { |
| 9502 | #ifdef IP_TOS |
| 9503 | char *err; |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 9504 | rule->action = ACT_HTTP_SET_TOS; |
Willy Tarreau | 42cf39e | 2013-06-11 18:51:32 +0200 | [diff] [blame] | 9505 | cur_arg = 1; |
| 9506 | |
| 9507 | if (!*args[cur_arg] || |
| 9508 | (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) { |
| 9509 | Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (integer/hex value).\n", |
| 9510 | file, linenum, args[0]); |
| 9511 | goto out_err; |
| 9512 | } |
| 9513 | |
| 9514 | rule->arg.tos = strtol(args[cur_arg], &err, 0); |
| 9515 | if (err && *err != '\0') { |
| 9516 | Alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-response %s' (integer/hex value expected).\n", |
| 9517 | file, linenum, err, args[0]); |
| 9518 | goto out_err; |
| 9519 | } |
| 9520 | cur_arg++; |
| 9521 | #else |
| 9522 | Alert("parsing [%s:%d]: 'http-response %s' is not supported on this platform (IP_TOS undefined).\n", file, linenum, args[0]); |
| 9523 | goto out_err; |
| 9524 | #endif |
Willy Tarreau | 51347ed | 2013-06-11 19:34:13 +0200 | [diff] [blame] | 9525 | } else if (!strcmp(args[0], "set-mark")) { |
| 9526 | #ifdef SO_MARK |
| 9527 | char *err; |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 9528 | rule->action = ACT_HTTP_SET_MARK; |
Willy Tarreau | 51347ed | 2013-06-11 19:34:13 +0200 | [diff] [blame] | 9529 | cur_arg = 1; |
| 9530 | |
| 9531 | if (!*args[cur_arg] || |
| 9532 | (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) { |
| 9533 | Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (integer/hex value).\n", |
| 9534 | file, linenum, args[0]); |
| 9535 | goto out_err; |
| 9536 | } |
| 9537 | |
| 9538 | rule->arg.mark = strtoul(args[cur_arg], &err, 0); |
| 9539 | if (err && *err != '\0') { |
| 9540 | Alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-response %s' (integer/hex value expected).\n", |
| 9541 | file, linenum, err, args[0]); |
| 9542 | goto out_err; |
| 9543 | } |
| 9544 | cur_arg++; |
| 9545 | global.last_checks |= LSTCHK_NETADM; |
| 9546 | #else |
| 9547 | Alert("parsing [%s:%d]: 'http-response %s' is not supported on this platform (SO_MARK undefined).\n", file, linenum, args[0]); |
| 9548 | goto out_err; |
| 9549 | #endif |
Willy Tarreau | 9a355ec | 2013-06-11 17:45:46 +0200 | [diff] [blame] | 9550 | } else if (!strcmp(args[0], "set-log-level")) { |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 9551 | rule->action = ACT_HTTP_SET_LOGL; |
Willy Tarreau | 9a355ec | 2013-06-11 17:45:46 +0200 | [diff] [blame] | 9552 | cur_arg = 1; |
| 9553 | |
| 9554 | if (!*args[cur_arg] || |
| 9555 | (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) { |
| 9556 | bad_log_level: |
| 9557 | Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (log level name or 'silent').\n", |
| 9558 | file, linenum, args[0]); |
| 9559 | goto out_err; |
| 9560 | } |
| 9561 | if (strcmp(args[cur_arg], "silent") == 0) |
| 9562 | rule->arg.loglevel = -1; |
Ruoshan Huang | dd01678 | 2016-06-15 22:16:03 +0800 | [diff] [blame] | 9563 | else if ((rule->arg.loglevel = get_log_level(args[cur_arg]) + 1) == 0) |
Willy Tarreau | 9a355ec | 2013-06-11 17:45:46 +0200 | [diff] [blame] | 9564 | goto bad_log_level; |
| 9565 | cur_arg++; |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 9566 | } else if (strcmp(args[0], "add-header") == 0 || strcmp(args[0], "set-header") == 0) { |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 9567 | rule->action = *args[0] == 'a' ? ACT_HTTP_ADD_HDR : ACT_HTTP_SET_HDR; |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 9568 | cur_arg = 1; |
| 9569 | |
| 9570 | if (!*args[cur_arg] || !*args[cur_arg+1] || |
| 9571 | (*args[cur_arg+2] && strcmp(args[cur_arg+2], "if") != 0 && strcmp(args[cur_arg+2], "unless") != 0)) { |
| 9572 | Alert("parsing [%s:%d]: 'http-response %s' expects exactly 2 arguments.\n", |
| 9573 | file, linenum, args[0]); |
| 9574 | goto out_err; |
| 9575 | } |
| 9576 | |
| 9577 | rule->arg.hdr_add.name = strdup(args[cur_arg]); |
| 9578 | rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name); |
| 9579 | LIST_INIT(&rule->arg.hdr_add.fmt); |
| 9580 | |
Thierry FOURNIER | 1c0054f | 2013-11-20 15:09:52 +0100 | [diff] [blame] | 9581 | proxy->conf.args.ctx = ARGC_HRS; |
Thierry FOURNIER / OZON.IO | 8a4e442 | 2016-11-23 00:41:28 +0100 | [diff] [blame] | 9582 | error = NULL; |
Thierry FOURNIER / OZON.IO | 59fd511 | 2016-11-22 23:50:02 +0100 | [diff] [blame] | 9583 | if (!parse_logformat_string(args[cur_arg + 1], proxy, &rule->arg.hdr_add.fmt, LOG_OPT_HTTP, |
Thierry FOURNIER / OZON.IO | 8a4e442 | 2016-11-23 00:41:28 +0100 | [diff] [blame] | 9584 | (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR, &error)) { |
| 9585 | Alert("parsing [%s:%d]: 'http-response %s': %s.\n", |
| 9586 | file, linenum, args[0], error); |
| 9587 | free(error); |
Thierry FOURNIER / OZON.IO | 59fd511 | 2016-11-22 23:50:02 +0100 | [diff] [blame] | 9588 | goto out_err; |
| 9589 | } |
Willy Tarreau | 59ad1a2 | 2014-01-29 14:39:58 +0100 | [diff] [blame] | 9590 | free(proxy->conf.lfs_file); |
| 9591 | proxy->conf.lfs_file = strdup(proxy->conf.args.file); |
| 9592 | proxy->conf.lfs_line = proxy->conf.args.line; |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 9593 | cur_arg += 2; |
Sasha Pachev | 218f064 | 2014-06-16 12:05:59 -0600 | [diff] [blame] | 9594 | } else if (strcmp(args[0], "replace-header") == 0 || strcmp(args[0], "replace-value") == 0) { |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 9595 | rule->action = args[0][8] == 'h' ? ACT_HTTP_REPLACE_HDR : ACT_HTTP_REPLACE_VAL; |
Sasha Pachev | 218f064 | 2014-06-16 12:05:59 -0600 | [diff] [blame] | 9596 | cur_arg = 1; |
| 9597 | |
| 9598 | if (!*args[cur_arg] || !*args[cur_arg+1] || !*args[cur_arg+2] || |
Baptiste Assmann | 12cb00b | 2014-08-08 17:29:06 +0200 | [diff] [blame] | 9599 | (*args[cur_arg+3] && strcmp(args[cur_arg+3], "if") != 0 && strcmp(args[cur_arg+3], "unless") != 0)) { |
| 9600 | Alert("parsing [%s:%d]: 'http-response %s' expects exactly 3 arguments.\n", |
Sasha Pachev | 218f064 | 2014-06-16 12:05:59 -0600 | [diff] [blame] | 9601 | file, linenum, args[0]); |
| 9602 | goto out_err; |
| 9603 | } |
| 9604 | |
| 9605 | rule->arg.hdr_add.name = strdup(args[cur_arg]); |
| 9606 | rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name); |
| 9607 | LIST_INIT(&rule->arg.hdr_add.fmt); |
| 9608 | |
Thierry FOURNIER | 09af0d6 | 2014-06-18 11:35:54 +0200 | [diff] [blame] | 9609 | error = NULL; |
| 9610 | if (!regex_comp(args[cur_arg + 1], &rule->arg.hdr_add.re, 1, 1, &error)) { |
| 9611 | Alert("parsing [%s:%d] : '%s' : %s.\n", file, linenum, |
| 9612 | args[cur_arg + 1], error); |
| 9613 | free(error); |
Sasha Pachev | 218f064 | 2014-06-16 12:05:59 -0600 | [diff] [blame] | 9614 | goto out_err; |
| 9615 | } |
| 9616 | |
| 9617 | proxy->conf.args.ctx = ARGC_HRQ; |
Thierry FOURNIER / OZON.IO | 8a4e442 | 2016-11-23 00:41:28 +0100 | [diff] [blame] | 9618 | error = NULL; |
Thierry FOURNIER / OZON.IO | 59fd511 | 2016-11-22 23:50:02 +0100 | [diff] [blame] | 9619 | if (!parse_logformat_string(args[cur_arg + 2], proxy, &rule->arg.hdr_add.fmt, LOG_OPT_HTTP, |
Thierry FOURNIER / OZON.IO | 8a4e442 | 2016-11-23 00:41:28 +0100 | [diff] [blame] | 9620 | (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR, &error)) { |
| 9621 | Alert("parsing [%s:%d]: 'http-response %s': %s.\n", |
| 9622 | file, linenum, args[0], error); |
| 9623 | free(error); |
Thierry FOURNIER / OZON.IO | 59fd511 | 2016-11-22 23:50:02 +0100 | [diff] [blame] | 9624 | goto out_err; |
| 9625 | } |
Sasha Pachev | 218f064 | 2014-06-16 12:05:59 -0600 | [diff] [blame] | 9626 | |
| 9627 | free(proxy->conf.lfs_file); |
| 9628 | proxy->conf.lfs_file = strdup(proxy->conf.args.file); |
| 9629 | proxy->conf.lfs_line = proxy->conf.args.line; |
| 9630 | cur_arg += 3; |
Thierry FOURNIER | dad3d1d | 2014-04-22 18:07:25 +0200 | [diff] [blame] | 9631 | } else if (strcmp(args[0], "del-header") == 0) { |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 9632 | rule->action = ACT_HTTP_DEL_HDR; |
Thierry FOURNIER | dad3d1d | 2014-04-22 18:07:25 +0200 | [diff] [blame] | 9633 | cur_arg = 1; |
| 9634 | |
| 9635 | if (!*args[cur_arg] || |
| 9636 | (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) { |
| 9637 | Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument.\n", |
| 9638 | file, linenum, args[0]); |
| 9639 | goto out_err; |
| 9640 | } |
| 9641 | |
| 9642 | rule->arg.hdr_add.name = strdup(args[cur_arg]); |
| 9643 | rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name); |
| 9644 | |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 9645 | proxy->conf.args.ctx = ARGC_HRS; |
| 9646 | free(proxy->conf.lfs_file); |
| 9647 | proxy->conf.lfs_file = strdup(proxy->conf.args.file); |
| 9648 | proxy->conf.lfs_line = proxy->conf.args.line; |
| 9649 | cur_arg += 1; |
| 9650 | } else if (strncmp(args[0], "add-acl", 7) == 0) { |
| 9651 | /* http-request add-acl(<reference (acl name)>) <key pattern> */ |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 9652 | rule->action = ACT_HTTP_ADD_ACL; |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 9653 | /* |
| 9654 | * '+ 8' for 'add-acl(' |
| 9655 | * '- 9' for 'add-acl(' + trailing ')' |
| 9656 | */ |
Willy Tarreau | 6c09c2c | 2014-04-25 21:38:08 +0200 | [diff] [blame] | 9657 | 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] | 9658 | |
| 9659 | cur_arg = 1; |
| 9660 | |
| 9661 | if (!*args[cur_arg] || |
| 9662 | (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) { |
| 9663 | Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument.\n", |
| 9664 | file, linenum, args[0]); |
| 9665 | goto out_err; |
| 9666 | } |
| 9667 | |
| 9668 | LIST_INIT(&rule->arg.map.key); |
Thierry FOURNIER | dad3d1d | 2014-04-22 18:07:25 +0200 | [diff] [blame] | 9669 | proxy->conf.args.ctx = ARGC_HRS; |
Thierry FOURNIER / OZON.IO | 8a4e442 | 2016-11-23 00:41:28 +0100 | [diff] [blame] | 9670 | error = NULL; |
Thierry FOURNIER / OZON.IO | 59fd511 | 2016-11-22 23:50:02 +0100 | [diff] [blame] | 9671 | if (!parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP, |
Thierry FOURNIER / OZON.IO | 8a4e442 | 2016-11-23 00:41:28 +0100 | [diff] [blame] | 9672 | (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR, &error)) { |
| 9673 | Alert("parsing [%s:%d]: 'http-response %s': %s.\n", |
| 9674 | file, linenum, args[0], error); |
| 9675 | free(error); |
Thierry FOURNIER / OZON.IO | 59fd511 | 2016-11-22 23:50:02 +0100 | [diff] [blame] | 9676 | goto out_err; |
| 9677 | } |
Thierry FOURNIER | dad3d1d | 2014-04-22 18:07:25 +0200 | [diff] [blame] | 9678 | free(proxy->conf.lfs_file); |
| 9679 | proxy->conf.lfs_file = strdup(proxy->conf.args.file); |
| 9680 | proxy->conf.lfs_line = proxy->conf.args.line; |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 9681 | |
Thierry FOURNIER | dad3d1d | 2014-04-22 18:07:25 +0200 | [diff] [blame] | 9682 | cur_arg += 1; |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 9683 | } else if (strncmp(args[0], "del-acl", 7) == 0) { |
| 9684 | /* http-response del-acl(<reference (acl name)>) <key pattern> */ |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 9685 | rule->action = ACT_HTTP_DEL_ACL; |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 9686 | /* |
| 9687 | * '+ 8' for 'del-acl(' |
| 9688 | * '- 9' for 'del-acl(' + trailing ')' |
| 9689 | */ |
Willy Tarreau | 6c09c2c | 2014-04-25 21:38:08 +0200 | [diff] [blame] | 9690 | 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] | 9691 | |
| 9692 | cur_arg = 1; |
| 9693 | |
| 9694 | if (!*args[cur_arg] || |
| 9695 | (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) { |
| 9696 | Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument.\n", |
| 9697 | file, linenum, args[0]); |
| 9698 | goto out_err; |
| 9699 | } |
| 9700 | |
| 9701 | LIST_INIT(&rule->arg.map.key); |
| 9702 | proxy->conf.args.ctx = ARGC_HRS; |
Thierry FOURNIER / OZON.IO | 8a4e442 | 2016-11-23 00:41:28 +0100 | [diff] [blame] | 9703 | error = NULL; |
Thierry FOURNIER / OZON.IO | 59fd511 | 2016-11-22 23:50:02 +0100 | [diff] [blame] | 9704 | if (!parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP, |
Thierry FOURNIER / OZON.IO | 8a4e442 | 2016-11-23 00:41:28 +0100 | [diff] [blame] | 9705 | (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR, &error)) { |
| 9706 | Alert("parsing [%s:%d]: 'http-response %s': %s.\n", |
| 9707 | file, linenum, args[0], error); |
| 9708 | free(error); |
Thierry FOURNIER / OZON.IO | 59fd511 | 2016-11-22 23:50:02 +0100 | [diff] [blame] | 9709 | goto out_err; |
| 9710 | } |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 9711 | free(proxy->conf.lfs_file); |
| 9712 | proxy->conf.lfs_file = strdup(proxy->conf.args.file); |
| 9713 | proxy->conf.lfs_line = proxy->conf.args.line; |
| 9714 | cur_arg += 1; |
| 9715 | } else if (strncmp(args[0], "del-map", 7) == 0) { |
| 9716 | /* http-response del-map(<reference (map name)>) <key pattern> */ |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 9717 | rule->action = ACT_HTTP_DEL_MAP; |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 9718 | /* |
| 9719 | * '+ 8' for 'del-map(' |
| 9720 | * '- 9' for 'del-map(' + trailing ')' |
| 9721 | */ |
Willy Tarreau | 6c09c2c | 2014-04-25 21:38:08 +0200 | [diff] [blame] | 9722 | 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] | 9723 | |
| 9724 | cur_arg = 1; |
| 9725 | |
| 9726 | if (!*args[cur_arg] || |
| 9727 | (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) { |
| 9728 | Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument.\n", |
| 9729 | file, linenum, args[0]); |
| 9730 | goto out_err; |
| 9731 | } |
| 9732 | |
| 9733 | LIST_INIT(&rule->arg.map.key); |
| 9734 | proxy->conf.args.ctx = ARGC_HRS; |
Thierry FOURNIER / OZON.IO | 8a4e442 | 2016-11-23 00:41:28 +0100 | [diff] [blame] | 9735 | error = NULL; |
Thierry FOURNIER / OZON.IO | 59fd511 | 2016-11-22 23:50:02 +0100 | [diff] [blame] | 9736 | if (!parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP, |
Thierry FOURNIER / OZON.IO | 8a4e442 | 2016-11-23 00:41:28 +0100 | [diff] [blame] | 9737 | (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR, &error)) { |
| 9738 | Alert("parsing [%s:%d]: 'http-response %s' %s.\n", |
| 9739 | file, linenum, args[0], error); |
| 9740 | free(error); |
Thierry FOURNIER / OZON.IO | 59fd511 | 2016-11-22 23:50:02 +0100 | [diff] [blame] | 9741 | goto out_err; |
| 9742 | } |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 9743 | free(proxy->conf.lfs_file); |
| 9744 | proxy->conf.lfs_file = strdup(proxy->conf.args.file); |
| 9745 | proxy->conf.lfs_line = proxy->conf.args.line; |
| 9746 | cur_arg += 1; |
| 9747 | } else if (strncmp(args[0], "set-map", 7) == 0) { |
| 9748 | /* http-response set-map(<reference (map name)>) <key pattern> <value pattern> */ |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 9749 | rule->action = ACT_HTTP_SET_MAP; |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 9750 | /* |
| 9751 | * '+ 8' for 'set-map(' |
| 9752 | * '- 9' for 'set-map(' + trailing ')' |
| 9753 | */ |
Willy Tarreau | 6c09c2c | 2014-04-25 21:38:08 +0200 | [diff] [blame] | 9754 | 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] | 9755 | |
| 9756 | cur_arg = 1; |
| 9757 | |
| 9758 | if (!*args[cur_arg] || !*args[cur_arg+1] || |
| 9759 | (*args[cur_arg+2] && strcmp(args[cur_arg+2], "if") != 0 && strcmp(args[cur_arg+2], "unless") != 0)) { |
| 9760 | Alert("parsing [%s:%d]: 'http-response %s' expects exactly 2 arguments.\n", |
| 9761 | file, linenum, args[0]); |
| 9762 | goto out_err; |
| 9763 | } |
| 9764 | |
| 9765 | LIST_INIT(&rule->arg.map.key); |
| 9766 | LIST_INIT(&rule->arg.map.value); |
| 9767 | |
| 9768 | proxy->conf.args.ctx = ARGC_HRS; |
| 9769 | |
| 9770 | /* key pattern */ |
Thierry FOURNIER / OZON.IO | 8a4e442 | 2016-11-23 00:41:28 +0100 | [diff] [blame] | 9771 | error = NULL; |
Thierry FOURNIER / OZON.IO | 59fd511 | 2016-11-22 23:50:02 +0100 | [diff] [blame] | 9772 | if (!parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP, |
Thierry FOURNIER / OZON.IO | 8a4e442 | 2016-11-23 00:41:28 +0100 | [diff] [blame] | 9773 | (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR, &error)) { |
| 9774 | Alert("parsing [%s:%d]: 'http-response %s' name: %s.\n", |
| 9775 | file, linenum, args[0], error); |
| 9776 | free(error); |
Thierry FOURNIER / OZON.IO | 59fd511 | 2016-11-22 23:50:02 +0100 | [diff] [blame] | 9777 | goto out_err; |
| 9778 | } |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 9779 | |
| 9780 | /* value pattern */ |
Thierry FOURNIER / OZON.IO | 8a4e442 | 2016-11-23 00:41:28 +0100 | [diff] [blame] | 9781 | error = NULL; |
Thierry FOURNIER / OZON.IO | 59fd511 | 2016-11-22 23:50:02 +0100 | [diff] [blame] | 9782 | if (!parse_logformat_string(args[cur_arg + 1], proxy, &rule->arg.map.value, LOG_OPT_HTTP, |
Thierry FOURNIER / OZON.IO | 8a4e442 | 2016-11-23 00:41:28 +0100 | [diff] [blame] | 9783 | (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR, &error)) { |
| 9784 | Alert("parsing [%s:%d]: 'http-response %s' value: %s.\n", |
| 9785 | file, linenum, args[0], error); |
| 9786 | free(error); |
Thierry FOURNIER / OZON.IO | 59fd511 | 2016-11-22 23:50:02 +0100 | [diff] [blame] | 9787 | goto out_err; |
| 9788 | } |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 9789 | |
| 9790 | free(proxy->conf.lfs_file); |
| 9791 | proxy->conf.lfs_file = strdup(proxy->conf.args.file); |
| 9792 | proxy->conf.lfs_line = proxy->conf.args.line; |
| 9793 | |
| 9794 | cur_arg += 2; |
Willy Tarreau | 51d861a | 2015-05-22 17:30:48 +0200 | [diff] [blame] | 9795 | } else if (strcmp(args[0], "redirect") == 0) { |
| 9796 | struct redirect_rule *redir; |
| 9797 | char *errmsg = NULL; |
| 9798 | |
| 9799 | if ((redir = http_parse_redirect_rule(file, linenum, proxy, (const char **)args + 1, &errmsg, 1, 1)) == NULL) { |
| 9800 | Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-response %s' rule : %s.\n", |
| 9801 | file, linenum, proxy_type_str(proxy), proxy->id, args[0], errmsg); |
| 9802 | goto out_err; |
| 9803 | } |
| 9804 | |
| 9805 | /* this redirect rule might already contain a parsed condition which |
| 9806 | * we'll pass to the http-request rule. |
| 9807 | */ |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 9808 | rule->action = ACT_HTTP_REDIR; |
Willy Tarreau | 51d861a | 2015-05-22 17:30:48 +0200 | [diff] [blame] | 9809 | rule->arg.redir = redir; |
| 9810 | rule->cond = redir->cond; |
| 9811 | redir->cond = NULL; |
| 9812 | cur_arg = 2; |
| 9813 | return rule; |
Ruoshan Huang | e4edc6b | 2016-07-14 15:07:45 +0800 | [diff] [blame] | 9814 | } else if (strncmp(args[0], "track-sc", 8) == 0 && |
| 9815 | args[0][9] == '\0' && args[0][8] >= '0' && |
| 9816 | args[0][8] < '0' + MAX_SESS_STKCTR) { /* track-sc 0..9 */ |
| 9817 | struct sample_expr *expr; |
| 9818 | unsigned int where; |
| 9819 | char *err = NULL; |
| 9820 | |
| 9821 | cur_arg = 1; |
| 9822 | proxy->conf.args.ctx = ARGC_TRK; |
| 9823 | |
| 9824 | expr = sample_parse_expr((char **)args, &cur_arg, file, linenum, &err, &proxy->conf.args); |
| 9825 | if (!expr) { |
| 9826 | Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-response %s' rule : %s.\n", |
| 9827 | file, linenum, proxy_type_str(proxy), proxy->id, args[0], err); |
| 9828 | free(err); |
| 9829 | goto out_err; |
| 9830 | } |
| 9831 | |
| 9832 | where = 0; |
| 9833 | if (proxy->cap & PR_CAP_FE) |
| 9834 | where |= SMP_VAL_FE_HRS_HDR; |
| 9835 | if (proxy->cap & PR_CAP_BE) |
| 9836 | where |= SMP_VAL_BE_HRS_HDR; |
| 9837 | |
| 9838 | if (!(expr->fetch->val & where)) { |
| 9839 | Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-response %s' rule :" |
| 9840 | " fetch method '%s' extracts information from '%s', none of which is available here.\n", |
| 9841 | file, linenum, proxy_type_str(proxy), proxy->id, args[0], |
| 9842 | args[cur_arg-1], sample_src_names(expr->fetch->use)); |
| 9843 | free(expr); |
| 9844 | goto out_err; |
| 9845 | } |
| 9846 | |
| 9847 | if (strcmp(args[cur_arg], "table") == 0) { |
| 9848 | cur_arg++; |
| 9849 | if (!args[cur_arg]) { |
| 9850 | Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-response %s' rule : missing table name.\n", |
| 9851 | file, linenum, proxy_type_str(proxy), proxy->id, args[0]); |
| 9852 | free(expr); |
| 9853 | goto out_err; |
| 9854 | } |
| 9855 | /* we copy the table name for now, it will be resolved later */ |
| 9856 | rule->arg.trk_ctr.table.n = strdup(args[cur_arg]); |
| 9857 | cur_arg++; |
| 9858 | } |
| 9859 | rule->arg.trk_ctr.expr = expr; |
| 9860 | rule->action = ACT_ACTION_TRK_SC0 + args[0][8] - '0'; |
William Lallemand | 73025dd | 2014-04-24 14:38:37 +0200 | [diff] [blame] | 9861 | } else if (((custom = action_http_res_custom(args[0])) != NULL)) { |
| 9862 | char *errmsg = NULL; |
| 9863 | cur_arg = 1; |
| 9864 | /* try in the module list */ |
Thierry FOURNIER | 5563e4b | 2015-08-14 19:20:07 +0200 | [diff] [blame] | 9865 | rule->from = ACT_F_HTTP_RES; |
Thierry FOURNIER | 85c6c97 | 2015-09-22 19:14:35 +0200 | [diff] [blame] | 9866 | rule->kw = custom; |
Thierry FOURNIER | afa8049 | 2015-08-19 09:04:15 +0200 | [diff] [blame] | 9867 | if (custom->parse(args, &cur_arg, proxy, rule, &errmsg) == ACT_RET_PRS_ERR) { |
William Lallemand | 73025dd | 2014-04-24 14:38:37 +0200 | [diff] [blame] | 9868 | Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-response %s' rule : %s.\n", |
| 9869 | file, linenum, proxy_type_str(proxy), proxy->id, args[0], errmsg); |
| 9870 | free(errmsg); |
| 9871 | goto out_err; |
| 9872 | } |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 9873 | } else { |
Thierry FOURNIER | ab95e65 | 2015-10-02 08:24:51 +0200 | [diff] [blame] | 9874 | action_build_list(&http_res_keywords.list, &trash); |
| 9875 | Alert("parsing [%s:%d]: 'http-response' expects 'allow', 'deny', 'redirect', " |
| 9876 | "'add-header', 'del-header', 'set-header', 'replace-header', 'replace-value', 'set-nice', " |
Ruoshan Huang | e4edc6b | 2016-07-14 15:07:45 +0800 | [diff] [blame] | 9877 | "'set-tos', 'set-mark', 'set-log-level', 'add-acl', 'del-acl', 'del-map', 'set-map', 'track-sc*'" |
William Lallemand | 2e785f2 | 2016-05-25 01:48:42 +0200 | [diff] [blame] | 9878 | "%s%s, but got '%s'%s.\n", |
Thierry FOURNIER | ab95e65 | 2015-10-02 08:24:51 +0200 | [diff] [blame] | 9879 | file, linenum, *trash.str ? ", " : "", trash.str, args[0], *args[0] ? "" : " (missing argument)"); |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 9880 | goto out_err; |
| 9881 | } |
| 9882 | |
| 9883 | if (strcmp(args[cur_arg], "if") == 0 || strcmp(args[cur_arg], "unless") == 0) { |
| 9884 | struct acl_cond *cond; |
| 9885 | char *errmsg = NULL; |
| 9886 | |
| 9887 | if ((cond = build_acl_cond(file, linenum, proxy, args+cur_arg, &errmsg)) == NULL) { |
| 9888 | Alert("parsing [%s:%d] : error detected while parsing an 'http-response %s' condition : %s.\n", |
| 9889 | file, linenum, args[0], errmsg); |
| 9890 | free(errmsg); |
| 9891 | goto out_err; |
| 9892 | } |
| 9893 | rule->cond = cond; |
| 9894 | } |
| 9895 | else if (*args[cur_arg]) { |
| 9896 | Alert("parsing [%s:%d]: 'http-response %s' expects" |
| 9897 | " either 'if' or 'unless' followed by a condition but found '%s'.\n", |
| 9898 | file, linenum, args[0], args[cur_arg]); |
| 9899 | goto out_err; |
| 9900 | } |
| 9901 | |
| 9902 | return rule; |
| 9903 | out_err: |
| 9904 | free(rule); |
| 9905 | return NULL; |
| 9906 | } |
| 9907 | |
Willy Tarreau | 4baae24 | 2012-12-27 12:00:31 +0100 | [diff] [blame] | 9908 | /* 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] | 9909 | * 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] | 9910 | * dynamic log-format rule instead of a static string. Parameter <dir> indicates |
| 9911 | * 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] | 9912 | */ |
| 9913 | 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] | 9914 | const char **args, char **errmsg, int use_fmt, int dir) |
Willy Tarreau | 4baae24 | 2012-12-27 12:00:31 +0100 | [diff] [blame] | 9915 | { |
| 9916 | struct redirect_rule *rule; |
| 9917 | int cur_arg; |
| 9918 | int type = REDIRECT_TYPE_NONE; |
| 9919 | int code = 302; |
| 9920 | const char *destination = NULL; |
| 9921 | const char *cookie = NULL; |
| 9922 | int cookie_set = 0; |
| 9923 | unsigned int flags = REDIRECT_FLAG_NONE; |
| 9924 | struct acl_cond *cond = NULL; |
| 9925 | |
| 9926 | cur_arg = 0; |
| 9927 | while (*(args[cur_arg])) { |
| 9928 | if (strcmp(args[cur_arg], "location") == 0) { |
| 9929 | if (!*args[cur_arg + 1]) |
| 9930 | goto missing_arg; |
| 9931 | |
| 9932 | type = REDIRECT_TYPE_LOCATION; |
| 9933 | cur_arg++; |
| 9934 | destination = args[cur_arg]; |
| 9935 | } |
| 9936 | else if (strcmp(args[cur_arg], "prefix") == 0) { |
| 9937 | if (!*args[cur_arg + 1]) |
| 9938 | goto missing_arg; |
Willy Tarreau | 4baae24 | 2012-12-27 12:00:31 +0100 | [diff] [blame] | 9939 | type = REDIRECT_TYPE_PREFIX; |
| 9940 | cur_arg++; |
| 9941 | destination = args[cur_arg]; |
| 9942 | } |
| 9943 | else if (strcmp(args[cur_arg], "scheme") == 0) { |
| 9944 | if (!*args[cur_arg + 1]) |
| 9945 | goto missing_arg; |
| 9946 | |
| 9947 | type = REDIRECT_TYPE_SCHEME; |
| 9948 | cur_arg++; |
| 9949 | destination = args[cur_arg]; |
| 9950 | } |
| 9951 | else if (strcmp(args[cur_arg], "set-cookie") == 0) { |
| 9952 | if (!*args[cur_arg + 1]) |
| 9953 | goto missing_arg; |
| 9954 | |
| 9955 | cur_arg++; |
| 9956 | cookie = args[cur_arg]; |
| 9957 | cookie_set = 1; |
| 9958 | } |
| 9959 | else if (strcmp(args[cur_arg], "clear-cookie") == 0) { |
| 9960 | if (!*args[cur_arg + 1]) |
| 9961 | goto missing_arg; |
| 9962 | |
| 9963 | cur_arg++; |
| 9964 | cookie = args[cur_arg]; |
| 9965 | cookie_set = 0; |
| 9966 | } |
| 9967 | else if (strcmp(args[cur_arg], "code") == 0) { |
| 9968 | if (!*args[cur_arg + 1]) |
| 9969 | goto missing_arg; |
| 9970 | |
| 9971 | cur_arg++; |
| 9972 | code = atol(args[cur_arg]); |
Yves Lafon | 3e8d1ae | 2013-03-11 11:06:05 -0400 | [diff] [blame] | 9973 | if (code < 301 || code > 308 || (code > 303 && code < 307)) { |
Willy Tarreau | 4baae24 | 2012-12-27 12:00:31 +0100 | [diff] [blame] | 9974 | memprintf(errmsg, |
Yves Lafon | 3e8d1ae | 2013-03-11 11:06:05 -0400 | [diff] [blame] | 9975 | "'%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] | 9976 | args[cur_arg - 1], args[cur_arg]); |
| 9977 | return NULL; |
| 9978 | } |
| 9979 | } |
| 9980 | else if (!strcmp(args[cur_arg],"drop-query")) { |
| 9981 | flags |= REDIRECT_FLAG_DROP_QS; |
| 9982 | } |
| 9983 | else if (!strcmp(args[cur_arg],"append-slash")) { |
| 9984 | flags |= REDIRECT_FLAG_APPEND_SLASH; |
| 9985 | } |
| 9986 | else if (strcmp(args[cur_arg], "if") == 0 || |
| 9987 | strcmp(args[cur_arg], "unless") == 0) { |
| 9988 | cond = build_acl_cond(file, linenum, curproxy, (const char **)args + cur_arg, errmsg); |
| 9989 | if (!cond) { |
| 9990 | memprintf(errmsg, "error in condition: %s", *errmsg); |
| 9991 | return NULL; |
| 9992 | } |
| 9993 | break; |
| 9994 | } |
| 9995 | else { |
| 9996 | memprintf(errmsg, |
| 9997 | "expects 'code', 'prefix', 'location', 'scheme', 'set-cookie', 'clear-cookie', 'drop-query' or 'append-slash' (was '%s')", |
| 9998 | args[cur_arg]); |
| 9999 | return NULL; |
| 10000 | } |
| 10001 | cur_arg++; |
| 10002 | } |
| 10003 | |
| 10004 | if (type == REDIRECT_TYPE_NONE) { |
| 10005 | memprintf(errmsg, "redirection type expected ('prefix', 'location', or 'scheme')"); |
| 10006 | return NULL; |
| 10007 | } |
| 10008 | |
Willy Tarreau | be4653b | 2015-05-28 15:26:58 +0200 | [diff] [blame] | 10009 | if (dir && type != REDIRECT_TYPE_LOCATION) { |
| 10010 | memprintf(errmsg, "response only supports redirect type 'location'"); |
| 10011 | return NULL; |
| 10012 | } |
| 10013 | |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 10014 | rule = calloc(1, sizeof(*rule)); |
Willy Tarreau | 4baae24 | 2012-12-27 12:00:31 +0100 | [diff] [blame] | 10015 | rule->cond = cond; |
Willy Tarreau | 60e0838 | 2013-12-03 00:48:45 +0100 | [diff] [blame] | 10016 | LIST_INIT(&rule->rdr_fmt); |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 10017 | |
| 10018 | if (!use_fmt) { |
| 10019 | /* old-style static redirect rule */ |
| 10020 | rule->rdr_str = strdup(destination); |
| 10021 | rule->rdr_len = strlen(destination); |
| 10022 | } |
| 10023 | else { |
| 10024 | /* log-format based redirect rule */ |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 10025 | |
| 10026 | /* Parse destination. Note that in the REDIRECT_TYPE_PREFIX case, |
| 10027 | * if prefix == "/", we don't want to add anything, otherwise it |
| 10028 | * makes it hard for the user to configure a self-redirection. |
| 10029 | */ |
Godbach | d972203 | 2014-12-18 15:44:58 +0800 | [diff] [blame] | 10030 | curproxy->conf.args.ctx = ARGC_RDR; |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 10031 | if (!(type == REDIRECT_TYPE_PREFIX && destination[0] == '/' && destination[1] == '\0')) { |
Thierry FOURNIER / OZON.IO | 59fd511 | 2016-11-22 23:50:02 +0100 | [diff] [blame] | 10032 | if (!parse_logformat_string(destination, curproxy, &rule->rdr_fmt, LOG_OPT_HTTP, |
| 10033 | dir ? (curproxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRS_HDR : SMP_VAL_BE_HRS_HDR |
Thierry FOURNIER / OZON.IO | 8a4e442 | 2016-11-23 00:41:28 +0100 | [diff] [blame] | 10034 | : (curproxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR, |
| 10035 | errmsg)) { |
Thierry FOURNIER / OZON.IO | 59fd511 | 2016-11-22 23:50:02 +0100 | [diff] [blame] | 10036 | return NULL; |
| 10037 | } |
Willy Tarreau | 59ad1a2 | 2014-01-29 14:39:58 +0100 | [diff] [blame] | 10038 | free(curproxy->conf.lfs_file); |
| 10039 | curproxy->conf.lfs_file = strdup(curproxy->conf.args.file); |
| 10040 | curproxy->conf.lfs_line = curproxy->conf.args.line; |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 10041 | } |
| 10042 | } |
| 10043 | |
Willy Tarreau | 4baae24 | 2012-12-27 12:00:31 +0100 | [diff] [blame] | 10044 | if (cookie) { |
| 10045 | /* depending on cookie_set, either we want to set the cookie, or to clear it. |
| 10046 | * a clear consists in appending "; path=/; Max-Age=0;" at the end. |
| 10047 | */ |
| 10048 | rule->cookie_len = strlen(cookie); |
| 10049 | if (cookie_set) { |
| 10050 | rule->cookie_str = malloc(rule->cookie_len + 10); |
| 10051 | memcpy(rule->cookie_str, cookie, rule->cookie_len); |
| 10052 | memcpy(rule->cookie_str + rule->cookie_len, "; path=/;", 10); |
| 10053 | rule->cookie_len += 9; |
| 10054 | } else { |
| 10055 | rule->cookie_str = malloc(rule->cookie_len + 21); |
| 10056 | memcpy(rule->cookie_str, cookie, rule->cookie_len); |
| 10057 | memcpy(rule->cookie_str + rule->cookie_len, "; path=/; Max-Age=0;", 21); |
| 10058 | rule->cookie_len += 20; |
| 10059 | } |
| 10060 | } |
| 10061 | rule->type = type; |
| 10062 | rule->code = code; |
| 10063 | rule->flags = flags; |
| 10064 | LIST_INIT(&rule->list); |
| 10065 | return rule; |
| 10066 | |
| 10067 | missing_arg: |
| 10068 | memprintf(errmsg, "missing argument for '%s'", args[cur_arg]); |
| 10069 | return NULL; |
| 10070 | } |
| 10071 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 10072 | /************************************************************************/ |
| 10073 | /* The code below is dedicated to ACL parsing and matching */ |
| 10074 | /************************************************************************/ |
| 10075 | |
| 10076 | |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 10077 | /* This function ensures that the prerequisites for an L7 fetch are ready, |
| 10078 | * which means that a request or response is ready. If some data is missing, |
| 10079 | * 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] | 10080 | * to extract data from L7. If <req_vol> is non-null during a request prefetch, |
| 10081 | * another test is made to ensure the required information is not gone. |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 10082 | * |
| 10083 | * The function returns : |
Willy Tarreau | 506d050 | 2013-07-06 13:29:24 +0200 | [diff] [blame] | 10084 | * 0 with SMP_F_MAY_CHANGE in the sample flags if some data is missing to |
| 10085 | * decide whether or not an HTTP message is present ; |
| 10086 | * 0 if the requested data cannot be fetched or if it is certain that |
| 10087 | * we'll never have any HTTP message there ; |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 10088 | * 1 if an HTTP message is ready |
| 10089 | */ |
James Rosewell | 91a41cb | 2015-09-18 17:11:16 +0100 | [diff] [blame] | 10090 | int smp_prefetch_http(struct proxy *px, struct stream *s, unsigned int opt, |
Willy Tarreau | 24e32d8 | 2012-04-23 23:55:44 +0200 | [diff] [blame] | 10091 | const struct arg *args, struct sample *smp, int req_vol) |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 10092 | { |
Willy Tarreau | 192252e | 2015-04-04 01:47:55 +0200 | [diff] [blame] | 10093 | struct http_txn *txn; |
| 10094 | struct http_msg *msg; |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 10095 | |
Willy Tarreau | be508f1 | 2016-03-10 11:47:01 +0100 | [diff] [blame] | 10096 | /* Note: it is possible that <s> is NULL when called before stream |
| 10097 | * initialization (eg: tcp-request connection), so this function is the |
| 10098 | * one responsible for guarding against this case for all HTTP users. |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 10099 | */ |
Willy Tarreau | 192252e | 2015-04-04 01:47:55 +0200 | [diff] [blame] | 10100 | if (!s) |
| 10101 | return 0; |
Willy Tarreau | be508f1 | 2016-03-10 11:47:01 +0100 | [diff] [blame] | 10102 | |
Thierry FOURNIER | ed08d6a | 2015-09-24 08:40:18 +0200 | [diff] [blame] | 10103 | if (!s->txn) { |
| 10104 | if (unlikely(!http_alloc_txn(s))) |
| 10105 | return 0; /* not enough memory */ |
| 10106 | http_init_txn(s); |
| 10107 | } |
Willy Tarreau | 192252e | 2015-04-04 01:47:55 +0200 | [diff] [blame] | 10108 | txn = s->txn; |
Willy Tarreau | 192252e | 2015-04-04 01:47:55 +0200 | [diff] [blame] | 10109 | msg = &txn->req; |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 10110 | |
| 10111 | /* Check for a dependency on a request */ |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 10112 | smp->data.type = SMP_T_BOOL; |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 10113 | |
Willy Tarreau | 32a6f2e | 2012-04-25 10:13:36 +0200 | [diff] [blame] | 10114 | if ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) { |
Willy Tarreau | aae75e3 | 2013-03-29 12:31:49 +0100 | [diff] [blame] | 10115 | /* If the buffer does not leave enough free space at the end, |
| 10116 | * we must first realign it. |
| 10117 | */ |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 10118 | if (s->req.buf->p > s->req.buf->data && |
| 10119 | s->req.buf->i + s->req.buf->p > s->req.buf->data + s->req.buf->size - global.tune.maxrewrite) |
| 10120 | buffer_slow_realign(s->req.buf); |
Willy Tarreau | aae75e3 | 2013-03-29 12:31:49 +0100 | [diff] [blame] | 10121 | |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 10122 | if (unlikely(txn->req.msg_state < HTTP_MSG_BODY)) { |
Willy Tarreau | 472b1ee | 2013-10-14 22:41:30 +0200 | [diff] [blame] | 10123 | if (msg->msg_state == HTTP_MSG_ERROR) |
Willy Tarreau | 506d050 | 2013-07-06 13:29:24 +0200 | [diff] [blame] | 10124 | return 0; |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 10125 | |
| 10126 | /* Try to decode HTTP request */ |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 10127 | if (likely(msg->next < s->req.buf->i)) |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 10128 | http_msg_analyzer(msg, &txn->hdr_idx); |
| 10129 | |
| 10130 | /* Still no valid request ? */ |
| 10131 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) { |
Willy Tarreau | 3bf1b2b | 2012-08-27 20:46:07 +0200 | [diff] [blame] | 10132 | if ((msg->msg_state == HTTP_MSG_ERROR) || |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 10133 | buffer_full(s->req.buf, global.tune.maxrewrite)) { |
Willy Tarreau | 506d050 | 2013-07-06 13:29:24 +0200 | [diff] [blame] | 10134 | return 0; |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 10135 | } |
| 10136 | /* wait for final state */ |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 10137 | smp->flags |= SMP_F_MAY_CHANGE; |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 10138 | return 0; |
| 10139 | } |
| 10140 | |
| 10141 | /* OK we just got a valid HTTP request. We have some minor |
| 10142 | * preparation to perform so that further checks can rely |
| 10143 | * on HTTP tests. |
| 10144 | */ |
Willy Tarreau | aae75e3 | 2013-03-29 12:31:49 +0100 | [diff] [blame] | 10145 | |
| 10146 | /* If the request was parsed but was too large, we must absolutely |
| 10147 | * return an error so that it is not processed. At the moment this |
| 10148 | * cannot happen, but if the parsers are to change in the future, |
| 10149 | * we want this check to be maintained. |
| 10150 | */ |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 10151 | if (unlikely(s->req.buf->i + s->req.buf->p > |
| 10152 | s->req.buf->data + s->req.buf->size - global.tune.maxrewrite)) { |
Willy Tarreau | aae75e3 | 2013-03-29 12:31:49 +0100 | [diff] [blame] | 10153 | msg->msg_state = HTTP_MSG_ERROR; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 10154 | smp->data.u.sint = 1; |
Willy Tarreau | aae75e3 | 2013-03-29 12:31:49 +0100 | [diff] [blame] | 10155 | return 1; |
| 10156 | } |
| 10157 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 10158 | 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] | 10159 | if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD) |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 10160 | s->flags |= SF_REDIRECTABLE; |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 10161 | |
Willy Tarreau | 506d050 | 2013-07-06 13:29:24 +0200 | [diff] [blame] | 10162 | if (unlikely(msg->sl.rq.v_l == 0) && !http_upgrade_v09_to_v10(txn)) |
| 10163 | return 0; |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 10164 | } |
| 10165 | |
Willy Tarreau | 506d050 | 2013-07-06 13:29:24 +0200 | [diff] [blame] | 10166 | if (req_vol && txn->rsp.msg_state != HTTP_MSG_RPBEFORE) { |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 10167 | return 0; /* data might have moved and indexes changed */ |
Willy Tarreau | 506d050 | 2013-07-06 13:29:24 +0200 | [diff] [blame] | 10168 | } |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 10169 | |
| 10170 | /* otherwise everything's ready for the request */ |
| 10171 | } |
Willy Tarreau | 24e32d8 | 2012-04-23 23:55:44 +0200 | [diff] [blame] | 10172 | else { |
| 10173 | /* Check for a dependency on a response */ |
Willy Tarreau | 506d050 | 2013-07-06 13:29:24 +0200 | [diff] [blame] | 10174 | if (txn->rsp.msg_state < HTTP_MSG_BODY) { |
| 10175 | smp->flags |= SMP_F_MAY_CHANGE; |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 10176 | return 0; |
Willy Tarreau | 506d050 | 2013-07-06 13:29:24 +0200 | [diff] [blame] | 10177 | } |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 10178 | } |
| 10179 | |
| 10180 | /* everything's OK */ |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 10181 | smp->data.u.sint = 1; |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 10182 | return 1; |
| 10183 | } |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 10184 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 10185 | /* 1. Check on METHOD |
| 10186 | * We use the pre-parsed method if it is known, and store its number as an |
| 10187 | * integer. If it is unknown, we use the pointer and the length. |
| 10188 | */ |
Thierry FOURNIER | e47e4e2 | 2014-04-28 11:18:57 +0200 | [diff] [blame] | 10189 | 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] | 10190 | { |
| 10191 | int len, meth; |
| 10192 | |
Thierry FOURNIER | 580c32c | 2014-01-24 10:58:12 +0100 | [diff] [blame] | 10193 | len = strlen(text); |
| 10194 | meth = find_http_meth(text, len); |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 10195 | |
| 10196 | pattern->val.i = meth; |
| 10197 | if (meth == HTTP_METH_OTHER) { |
Willy Tarreau | 912c119 | 2014-08-29 15:15:50 +0200 | [diff] [blame] | 10198 | pattern->ptr.str = (char *)text; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 10199 | pattern->len = len; |
| 10200 | } |
Thierry FOURNIER | d437314 | 2013-12-17 01:10:10 +0100 | [diff] [blame] | 10201 | else { |
| 10202 | pattern->ptr.str = NULL; |
| 10203 | pattern->len = 0; |
Thierry FOURNIER | d437314 | 2013-12-17 01:10:10 +0100 | [diff] [blame] | 10204 | } |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 10205 | return 1; |
| 10206 | } |
| 10207 | |
Willy Tarreau | 8e5e955 | 2011-12-16 15:38:49 +0100 | [diff] [blame] | 10208 | /* This function fetches the method of current HTTP request and stores |
| 10209 | * it in the global pattern struct as a chunk. There are two possibilities : |
| 10210 | * - if the method is known (not HTTP_METH_OTHER), its identifier is stored |
| 10211 | * in <len> and <ptr> is NULL ; |
| 10212 | * - if the method is unknown (HTTP_METH_OTHER), <ptr> points to the text and |
| 10213 | * <len> to its length. |
Thierry FOURNIER | a65b343 | 2013-11-28 18:22:00 +0100 | [diff] [blame] | 10214 | * This is intended to be used with pat_match_meth() only. |
Willy Tarreau | 8e5e955 | 2011-12-16 15:38:49 +0100 | [diff] [blame] | 10215 | */ |
Willy Tarreau | d41f8d8 | 2007-06-10 10:06:18 +0200 | [diff] [blame] | 10216 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10217 | 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] | 10218 | { |
| 10219 | int meth; |
Willy Tarreau | be508f1 | 2016-03-10 11:47:01 +0100 | [diff] [blame] | 10220 | struct http_txn *txn; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 10221 | |
Willy Tarreau | 24e32d8 | 2012-04-23 23:55:44 +0200 | [diff] [blame] | 10222 | CHECK_HTTP_MESSAGE_FIRST_PERM(); |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 10223 | |
Willy Tarreau | be508f1 | 2016-03-10 11:47:01 +0100 | [diff] [blame] | 10224 | txn = smp->strm->txn; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 10225 | meth = txn->meth; |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 10226 | smp->data.type = SMP_T_METH; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 10227 | smp->data.u.meth.meth = meth; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 10228 | if (meth == HTTP_METH_OTHER) { |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 10229 | if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE) |
| 10230 | /* ensure the indexes are not affected */ |
| 10231 | return 0; |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 10232 | smp->flags |= SMP_F_CONST; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 10233 | smp->data.u.meth.str.len = txn->req.sl.rq.m_l; |
| 10234 | smp->data.u.meth.str.str = txn->req.chn->buf->p; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 10235 | } |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 10236 | smp->flags |= SMP_F_VOL_1ST; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 10237 | return 1; |
| 10238 | } |
| 10239 | |
Willy Tarreau | 8e5e955 | 2011-12-16 15:38:49 +0100 | [diff] [blame] | 10240 | /* See above how the method is stored in the global pattern */ |
Thierry FOURNIER | 5338eea | 2013-12-16 14:22:13 +0100 | [diff] [blame] | 10241 | 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] | 10242 | { |
Willy Tarreau | c8d7c96 | 2007-06-17 08:20:33 +0200 | [diff] [blame] | 10243 | int icase; |
Thierry FOURNIER | 5338eea | 2013-12-16 14:22:13 +0100 | [diff] [blame] | 10244 | struct pattern_list *lst; |
| 10245 | struct pattern *pattern; |
Willy Tarreau | c8d7c96 | 2007-06-17 08:20:33 +0200 | [diff] [blame] | 10246 | |
Thierry FOURNIER | 5338eea | 2013-12-16 14:22:13 +0100 | [diff] [blame] | 10247 | list_for_each_entry(lst, &expr->patterns, list) { |
| 10248 | pattern = &lst->pat; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 10249 | |
Thierry FOURNIER | 5338eea | 2013-12-16 14:22:13 +0100 | [diff] [blame] | 10250 | /* well-known method */ |
| 10251 | if (pattern->val.i != HTTP_METH_OTHER) { |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 10252 | if (smp->data.u.meth.meth == pattern->val.i) |
Thierry FOURNIER | 5338eea | 2013-12-16 14:22:13 +0100 | [diff] [blame] | 10253 | return pattern; |
| 10254 | else |
| 10255 | continue; |
| 10256 | } |
Willy Tarreau | c8d7c96 | 2007-06-17 08:20:33 +0200 | [diff] [blame] | 10257 | |
Thierry FOURNIER | 5338eea | 2013-12-16 14:22:13 +0100 | [diff] [blame] | 10258 | /* Other method, we must compare the strings */ |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 10259 | if (pattern->len != smp->data.u.meth.str.len) |
Thierry FOURNIER | 5338eea | 2013-12-16 14:22:13 +0100 | [diff] [blame] | 10260 | continue; |
| 10261 | |
Thierry FOURNIER | e47e4e2 | 2014-04-28 11:18:57 +0200 | [diff] [blame] | 10262 | icase = expr->mflags & PAT_MF_IGNORE_CASE; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 10263 | if ((icase && strncasecmp(pattern->ptr.str, smp->data.u.meth.str.str, smp->data.u.meth.str.len) == 0) || |
| 10264 | (!icase && strncmp(pattern->ptr.str, smp->data.u.meth.str.str, smp->data.u.meth.str.len) == 0)) |
Thierry FOURNIER | 5338eea | 2013-12-16 14:22:13 +0100 | [diff] [blame] | 10265 | return pattern; |
| 10266 | } |
| 10267 | return NULL; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 10268 | } |
| 10269 | |
Willy Tarreau | d41f8d8 | 2007-06-10 10:06:18 +0200 | [diff] [blame] | 10270 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10271 | 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] | 10272 | { |
Willy Tarreau | be508f1 | 2016-03-10 11:47:01 +0100 | [diff] [blame] | 10273 | struct http_txn *txn; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 10274 | char *ptr; |
| 10275 | int len; |
| 10276 | |
Willy Tarreau | c0239e0 | 2012-04-16 14:42:55 +0200 | [diff] [blame] | 10277 | CHECK_HTTP_MESSAGE_FIRST(); |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 10278 | |
Willy Tarreau | be508f1 | 2016-03-10 11:47:01 +0100 | [diff] [blame] | 10279 | txn = smp->strm->txn; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 10280 | len = txn->req.sl.rq.v_l; |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 10281 | ptr = txn->req.chn->buf->p + txn->req.sl.rq.v; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 10282 | |
| 10283 | while ((len-- > 0) && (*ptr++ != '/')); |
| 10284 | if (len <= 0) |
| 10285 | return 0; |
| 10286 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 10287 | smp->data.type = SMP_T_STR; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 10288 | smp->data.u.str.str = ptr; |
| 10289 | smp->data.u.str.len = len; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 10290 | |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 10291 | smp->flags = SMP_F_VOL_1ST | SMP_F_CONST; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 10292 | return 1; |
| 10293 | } |
| 10294 | |
Willy Tarreau | d41f8d8 | 2007-06-10 10:06:18 +0200 | [diff] [blame] | 10295 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10296 | 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] | 10297 | { |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 10298 | struct http_txn *txn; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 10299 | char *ptr; |
| 10300 | int len; |
| 10301 | |
Willy Tarreau | c0239e0 | 2012-04-16 14:42:55 +0200 | [diff] [blame] | 10302 | CHECK_HTTP_MESSAGE_FIRST(); |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 10303 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 10304 | txn = smp->strm->txn; |
Willy Tarreau | f26b252 | 2012-12-14 08:33:14 +0100 | [diff] [blame] | 10305 | if (txn->rsp.msg_state < HTTP_MSG_BODY) |
| 10306 | return 0; |
| 10307 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 10308 | len = txn->rsp.sl.st.v_l; |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 10309 | ptr = txn->rsp.chn->buf->p; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 10310 | |
| 10311 | while ((len-- > 0) && (*ptr++ != '/')); |
| 10312 | if (len <= 0) |
| 10313 | return 0; |
| 10314 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 10315 | smp->data.type = SMP_T_STR; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 10316 | smp->data.u.str.str = ptr; |
| 10317 | smp->data.u.str.len = len; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 10318 | |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 10319 | smp->flags = SMP_F_VOL_1ST | SMP_F_CONST; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 10320 | return 1; |
| 10321 | } |
| 10322 | |
| 10323 | /* 3. Check on Status Code. We manipulate integers here. */ |
Willy Tarreau | d41f8d8 | 2007-06-10 10:06:18 +0200 | [diff] [blame] | 10324 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10325 | 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] | 10326 | { |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 10327 | struct http_txn *txn; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 10328 | char *ptr; |
| 10329 | int len; |
| 10330 | |
Willy Tarreau | c0239e0 | 2012-04-16 14:42:55 +0200 | [diff] [blame] | 10331 | CHECK_HTTP_MESSAGE_FIRST(); |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 10332 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 10333 | txn = smp->strm->txn; |
Willy Tarreau | f26b252 | 2012-12-14 08:33:14 +0100 | [diff] [blame] | 10334 | if (txn->rsp.msg_state < HTTP_MSG_BODY) |
| 10335 | return 0; |
| 10336 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 10337 | len = txn->rsp.sl.st.c_l; |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 10338 | ptr = txn->rsp.chn->buf->p + txn->rsp.sl.st.c; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 10339 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 10340 | smp->data.type = SMP_T_SINT; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 10341 | smp->data.u.sint = __strl2ui(ptr, len); |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 10342 | smp->flags = SMP_F_VOL_1ST; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 10343 | return 1; |
| 10344 | } |
| 10345 | |
Thierry Fournier | f4011dd | 2016-03-29 17:23:51 +0200 | [diff] [blame] | 10346 | static int |
| 10347 | smp_fetch_uniqueid(const struct arg *args, struct sample *smp, const char *kw, void *private) |
| 10348 | { |
| 10349 | if (LIST_ISEMPTY(&smp->sess->fe->format_unique_id)) |
| 10350 | return 0; |
| 10351 | |
| 10352 | if (!smp->strm->unique_id) { |
| 10353 | if ((smp->strm->unique_id = pool_alloc2(pool2_uniqueid)) == NULL) |
| 10354 | return 0; |
| 10355 | smp->strm->unique_id[0] = '\0'; |
| 10356 | } |
| 10357 | smp->data.u.str.len = build_logline(smp->strm, smp->strm->unique_id, |
| 10358 | UNIQUEID_LEN, &smp->sess->fe->format_unique_id); |
| 10359 | |
| 10360 | smp->data.type = SMP_T_STR; |
| 10361 | smp->data.u.str.str = smp->strm->unique_id; |
| 10362 | smp->flags = SMP_F_CONST; |
| 10363 | return 1; |
| 10364 | } |
| 10365 | |
Willy Tarreau | a5910cc | 2015-05-02 00:46:08 +0200 | [diff] [blame] | 10366 | /* returns the longest available part of the body. This requires that the body |
| 10367 | * has been waited for using http-buffer-request. |
| 10368 | */ |
| 10369 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10370 | 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] | 10371 | { |
Willy Tarreau | a5910cc | 2015-05-02 00:46:08 +0200 | [diff] [blame] | 10372 | struct http_msg *msg; |
| 10373 | unsigned long len; |
| 10374 | unsigned long block1; |
| 10375 | char *body; |
| 10376 | struct chunk *temp; |
| 10377 | |
| 10378 | CHECK_HTTP_MESSAGE_FIRST(); |
| 10379 | |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10380 | if ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) |
Willy Tarreau | be508f1 | 2016-03-10 11:47:01 +0100 | [diff] [blame] | 10381 | msg = &smp->strm->txn->req; |
Willy Tarreau | a5910cc | 2015-05-02 00:46:08 +0200 | [diff] [blame] | 10382 | else |
Willy Tarreau | be508f1 | 2016-03-10 11:47:01 +0100 | [diff] [blame] | 10383 | msg = &smp->strm->txn->rsp; |
Willy Tarreau | a5910cc | 2015-05-02 00:46:08 +0200 | [diff] [blame] | 10384 | |
| 10385 | len = http_body_bytes(msg); |
| 10386 | body = b_ptr(msg->chn->buf, -http_data_rewind(msg)); |
| 10387 | |
| 10388 | block1 = len; |
| 10389 | if (block1 > msg->chn->buf->data + msg->chn->buf->size - body) |
| 10390 | block1 = msg->chn->buf->data + msg->chn->buf->size - body; |
| 10391 | |
| 10392 | if (block1 == len) { |
| 10393 | /* buffer is not wrapped (or empty) */ |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 10394 | smp->data.type = SMP_T_BIN; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 10395 | smp->data.u.str.str = body; |
| 10396 | smp->data.u.str.len = len; |
Willy Tarreau | a5910cc | 2015-05-02 00:46:08 +0200 | [diff] [blame] | 10397 | smp->flags = SMP_F_VOL_TEST | SMP_F_CONST; |
| 10398 | } |
| 10399 | else { |
| 10400 | /* buffer is wrapped, we need to defragment it */ |
| 10401 | temp = get_trash_chunk(); |
| 10402 | memcpy(temp->str, body, block1); |
| 10403 | memcpy(temp->str + block1, msg->chn->buf->data, len - block1); |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 10404 | smp->data.type = SMP_T_BIN; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 10405 | smp->data.u.str.str = temp->str; |
| 10406 | smp->data.u.str.len = len; |
Willy Tarreau | a5910cc | 2015-05-02 00:46:08 +0200 | [diff] [blame] | 10407 | smp->flags = SMP_F_VOL_TEST; |
| 10408 | } |
| 10409 | return 1; |
| 10410 | } |
| 10411 | |
| 10412 | |
| 10413 | /* returns the available length of the body. This requires that the body |
| 10414 | * has been waited for using http-buffer-request. |
| 10415 | */ |
| 10416 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10417 | 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] | 10418 | { |
Willy Tarreau | a5910cc | 2015-05-02 00:46:08 +0200 | [diff] [blame] | 10419 | struct http_msg *msg; |
| 10420 | |
| 10421 | CHECK_HTTP_MESSAGE_FIRST(); |
| 10422 | |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10423 | if ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) |
Willy Tarreau | be508f1 | 2016-03-10 11:47:01 +0100 | [diff] [blame] | 10424 | msg = &smp->strm->txn->req; |
Willy Tarreau | a5910cc | 2015-05-02 00:46:08 +0200 | [diff] [blame] | 10425 | else |
Willy Tarreau | be508f1 | 2016-03-10 11:47:01 +0100 | [diff] [blame] | 10426 | msg = &smp->strm->txn->rsp; |
Willy Tarreau | a5910cc | 2015-05-02 00:46:08 +0200 | [diff] [blame] | 10427 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 10428 | smp->data.type = SMP_T_SINT; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 10429 | smp->data.u.sint = http_body_bytes(msg); |
Willy Tarreau | a5910cc | 2015-05-02 00:46:08 +0200 | [diff] [blame] | 10430 | |
| 10431 | smp->flags = SMP_F_VOL_TEST; |
| 10432 | return 1; |
| 10433 | } |
| 10434 | |
| 10435 | |
| 10436 | /* returns the advertised length of the body, or the advertised size of the |
| 10437 | * chunks available in the buffer. This requires that the body has been waited |
| 10438 | * for using http-buffer-request. |
| 10439 | */ |
| 10440 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10441 | 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] | 10442 | { |
Willy Tarreau | a5910cc | 2015-05-02 00:46:08 +0200 | [diff] [blame] | 10443 | struct http_msg *msg; |
| 10444 | |
| 10445 | CHECK_HTTP_MESSAGE_FIRST(); |
| 10446 | |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10447 | if ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) |
Willy Tarreau | be508f1 | 2016-03-10 11:47:01 +0100 | [diff] [blame] | 10448 | msg = &smp->strm->txn->req; |
Willy Tarreau | a5910cc | 2015-05-02 00:46:08 +0200 | [diff] [blame] | 10449 | else |
Willy Tarreau | be508f1 | 2016-03-10 11:47:01 +0100 | [diff] [blame] | 10450 | msg = &smp->strm->txn->rsp; |
Willy Tarreau | a5910cc | 2015-05-02 00:46:08 +0200 | [diff] [blame] | 10451 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 10452 | smp->data.type = SMP_T_SINT; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 10453 | smp->data.u.sint = msg->body_len; |
Willy Tarreau | a5910cc | 2015-05-02 00:46:08 +0200 | [diff] [blame] | 10454 | |
| 10455 | smp->flags = SMP_F_VOL_TEST; |
| 10456 | return 1; |
| 10457 | } |
| 10458 | |
| 10459 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 10460 | /* 4. Check on URL/URI. A pointer to the URI is stored. */ |
Willy Tarreau | d41f8d8 | 2007-06-10 10:06:18 +0200 | [diff] [blame] | 10461 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10462 | 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] | 10463 | { |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 10464 | struct http_txn *txn; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 10465 | |
Willy Tarreau | c0239e0 | 2012-04-16 14:42:55 +0200 | [diff] [blame] | 10466 | CHECK_HTTP_MESSAGE_FIRST(); |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 10467 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 10468 | txn = smp->strm->txn; |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 10469 | smp->data.type = SMP_T_STR; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 10470 | smp->data.u.str.len = txn->req.sl.rq.u_l; |
| 10471 | smp->data.u.str.str = txn->req.chn->buf->p + txn->req.sl.rq.u; |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 10472 | smp->flags = SMP_F_VOL_1ST | SMP_F_CONST; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 10473 | return 1; |
| 10474 | } |
| 10475 | |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 10476 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10477 | 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] | 10478 | { |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 10479 | struct http_txn *txn; |
Willy Tarreau | 4c804ec | 2013-09-30 14:37:14 +0200 | [diff] [blame] | 10480 | struct sockaddr_storage addr; |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 10481 | |
Willy Tarreau | c0239e0 | 2012-04-16 14:42:55 +0200 | [diff] [blame] | 10482 | CHECK_HTTP_MESSAGE_FIRST(); |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 10483 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 10484 | txn = smp->strm->txn; |
Thierry FOURNIER | 9f95e40 | 2014-03-21 14:51:46 +0100 | [diff] [blame] | 10485 | 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] | 10486 | if (((struct sockaddr_in *)&addr)->sin_family != AF_INET) |
Willy Tarreau | f4362b3 | 2011-12-16 17:49:52 +0100 | [diff] [blame] | 10487 | return 0; |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 10488 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 10489 | smp->data.type = SMP_T_IPV4; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 10490 | smp->data.u.ipv4 = ((struct sockaddr_in *)&addr)->sin_addr; |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 10491 | smp->flags = 0; |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 10492 | return 1; |
| 10493 | } |
| 10494 | |
| 10495 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10496 | 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] | 10497 | { |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 10498 | struct http_txn *txn; |
Willy Tarreau | 4c804ec | 2013-09-30 14:37:14 +0200 | [diff] [blame] | 10499 | struct sockaddr_storage addr; |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 10500 | |
Willy Tarreau | c0239e0 | 2012-04-16 14:42:55 +0200 | [diff] [blame] | 10501 | CHECK_HTTP_MESSAGE_FIRST(); |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 10502 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 10503 | txn = smp->strm->txn; |
Thierry FOURNIER | 9f95e40 | 2014-03-21 14:51:46 +0100 | [diff] [blame] | 10504 | 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] | 10505 | if (((struct sockaddr_in *)&addr)->sin_family != AF_INET) |
| 10506 | return 0; |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 10507 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 10508 | smp->data.type = SMP_T_SINT; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 10509 | smp->data.u.sint = ntohs(((struct sockaddr_in *)&addr)->sin_port); |
Willy Tarreau | 21e5b0e | 2012-04-23 19:25:44 +0200 | [diff] [blame] | 10510 | smp->flags = 0; |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 10511 | return 1; |
| 10512 | } |
| 10513 | |
Willy Tarreau | 185b5c4 | 2012-04-26 15:11:51 +0200 | [diff] [blame] | 10514 | /* Fetch an HTTP header. A pointer to the beginning of the value is returned. |
| 10515 | * Accepts an optional argument of type string containing the header field name, |
| 10516 | * and an optional argument of type signed or unsigned integer to request an |
| 10517 | * 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] | 10518 | * headers are considered from the first one. It does not stop on commas and |
| 10519 | * returns full lines instead (useful for User-Agent or Date for example). |
| 10520 | */ |
| 10521 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10522 | 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] | 10523 | { |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 10524 | struct hdr_idx *idx; |
Willy Tarreau | 04ff9f1 | 2013-06-10 18:39:42 +0200 | [diff] [blame] | 10525 | struct hdr_ctx *ctx = smp->ctx.a[0]; |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 10526 | const struct http_msg *msg; |
Willy Tarreau | 04ff9f1 | 2013-06-10 18:39:42 +0200 | [diff] [blame] | 10527 | int occ = 0; |
| 10528 | const char *name_str = NULL; |
| 10529 | int name_len = 0; |
| 10530 | |
| 10531 | if (!ctx) { |
| 10532 | /* first call */ |
| 10533 | ctx = &static_hdr_ctx; |
| 10534 | ctx->idx = 0; |
| 10535 | smp->ctx.a[0] = ctx; |
| 10536 | } |
| 10537 | |
| 10538 | if (args) { |
| 10539 | if (args[0].type != ARGT_STR) |
| 10540 | return 0; |
| 10541 | name_str = args[0].data.str.str; |
| 10542 | name_len = args[0].data.str.len; |
| 10543 | |
Thierry FOURNIER | bf65cd4 | 2015-07-20 17:45:02 +0200 | [diff] [blame] | 10544 | if (args[1].type == ARGT_SINT) |
| 10545 | occ = args[1].data.sint; |
Willy Tarreau | 04ff9f1 | 2013-06-10 18:39:42 +0200 | [diff] [blame] | 10546 | } |
| 10547 | |
| 10548 | CHECK_HTTP_MESSAGE_FIRST(); |
| 10549 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 10550 | idx = &smp->strm->txn->hdr_idx; |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10551 | 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] | 10552 | |
Willy Tarreau | 04ff9f1 | 2013-06-10 18:39:42 +0200 | [diff] [blame] | 10553 | if (ctx && !(smp->flags & SMP_F_NOT_LAST)) |
| 10554 | /* search for header from the beginning */ |
| 10555 | ctx->idx = 0; |
| 10556 | |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10557 | if (!occ && !(smp->opt & SMP_OPT_ITERATE)) |
Willy Tarreau | 04ff9f1 | 2013-06-10 18:39:42 +0200 | [diff] [blame] | 10558 | /* no explicit occurrence and single fetch => last header by default */ |
| 10559 | occ = -1; |
| 10560 | |
| 10561 | if (!occ) |
| 10562 | /* prepare to report multiple occurrences for ACL fetches */ |
| 10563 | smp->flags |= SMP_F_NOT_LAST; |
| 10564 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 10565 | smp->data.type = SMP_T_STR; |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 10566 | smp->flags |= SMP_F_VOL_HDR | SMP_F_CONST; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 10567 | if (http_get_fhdr(msg, name_str, name_len, idx, occ, ctx, &smp->data.u.str.str, &smp->data.u.str.len)) |
Willy Tarreau | 04ff9f1 | 2013-06-10 18:39:42 +0200 | [diff] [blame] | 10568 | return 1; |
| 10569 | |
| 10570 | smp->flags &= ~SMP_F_NOT_LAST; |
| 10571 | return 0; |
| 10572 | } |
| 10573 | |
| 10574 | /* 6. Check on HTTP header count. The number of occurrences is returned. |
| 10575 | * Accepts exactly 1 argument of type string. It does not stop on commas and |
| 10576 | * returns full lines instead (useful for User-Agent or Date for example). |
| 10577 | */ |
| 10578 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10579 | 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] | 10580 | { |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 10581 | struct hdr_idx *idx; |
Willy Tarreau | 04ff9f1 | 2013-06-10 18:39:42 +0200 | [diff] [blame] | 10582 | struct hdr_ctx ctx; |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 10583 | const struct http_msg *msg; |
Willy Tarreau | 04ff9f1 | 2013-06-10 18:39:42 +0200 | [diff] [blame] | 10584 | int cnt; |
Willy Tarreau | 601a4d1 | 2015-04-01 19:16:09 +0200 | [diff] [blame] | 10585 | const char *name = NULL; |
| 10586 | int len = 0; |
Willy Tarreau | 04ff9f1 | 2013-06-10 18:39:42 +0200 | [diff] [blame] | 10587 | |
Willy Tarreau | 601a4d1 | 2015-04-01 19:16:09 +0200 | [diff] [blame] | 10588 | if (args && args->type == ARGT_STR) { |
| 10589 | name = args->data.str.str; |
| 10590 | len = args->data.str.len; |
| 10591 | } |
Willy Tarreau | 04ff9f1 | 2013-06-10 18:39:42 +0200 | [diff] [blame] | 10592 | |
| 10593 | CHECK_HTTP_MESSAGE_FIRST(); |
| 10594 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 10595 | idx = &smp->strm->txn->hdr_idx; |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10596 | 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] | 10597 | |
Willy Tarreau | 04ff9f1 | 2013-06-10 18:39:42 +0200 | [diff] [blame] | 10598 | ctx.idx = 0; |
| 10599 | cnt = 0; |
Willy Tarreau | 601a4d1 | 2015-04-01 19:16:09 +0200 | [diff] [blame] | 10600 | 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] | 10601 | cnt++; |
| 10602 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 10603 | smp->data.type = SMP_T_SINT; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 10604 | smp->data.u.sint = cnt; |
Willy Tarreau | 04ff9f1 | 2013-06-10 18:39:42 +0200 | [diff] [blame] | 10605 | smp->flags = SMP_F_VOL_HDR; |
| 10606 | return 1; |
| 10607 | } |
| 10608 | |
Willy Tarreau | eb27ec7 | 2015-02-20 13:55:29 +0100 | [diff] [blame] | 10609 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10610 | 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] | 10611 | { |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 10612 | struct hdr_idx *idx; |
Willy Tarreau | eb27ec7 | 2015-02-20 13:55:29 +0100 | [diff] [blame] | 10613 | struct hdr_ctx ctx; |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 10614 | const struct http_msg *msg; |
Willy Tarreau | eb27ec7 | 2015-02-20 13:55:29 +0100 | [diff] [blame] | 10615 | struct chunk *temp; |
| 10616 | char del = ','; |
| 10617 | |
| 10618 | if (args && args->type == ARGT_STR) |
| 10619 | del = *args[0].data.str.str; |
| 10620 | |
| 10621 | CHECK_HTTP_MESSAGE_FIRST(); |
| 10622 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 10623 | idx = &smp->strm->txn->hdr_idx; |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10624 | 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] | 10625 | |
Willy Tarreau | eb27ec7 | 2015-02-20 13:55:29 +0100 | [diff] [blame] | 10626 | temp = get_trash_chunk(); |
| 10627 | |
| 10628 | ctx.idx = 0; |
| 10629 | while (http_find_next_header(msg->chn->buf->p, idx, &ctx)) { |
| 10630 | if (temp->len) |
| 10631 | temp->str[temp->len++] = del; |
| 10632 | memcpy(temp->str + temp->len, ctx.line, ctx.del); |
| 10633 | temp->len += ctx.del; |
| 10634 | } |
| 10635 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 10636 | smp->data.type = SMP_T_STR; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 10637 | smp->data.u.str.str = temp->str; |
| 10638 | smp->data.u.str.len = temp->len; |
Willy Tarreau | eb27ec7 | 2015-02-20 13:55:29 +0100 | [diff] [blame] | 10639 | smp->flags = SMP_F_VOL_HDR; |
| 10640 | return 1; |
| 10641 | } |
| 10642 | |
Willy Tarreau | 04ff9f1 | 2013-06-10 18:39:42 +0200 | [diff] [blame] | 10643 | /* Fetch an HTTP header. A pointer to the beginning of the value is returned. |
| 10644 | * Accepts an optional argument of type string containing the header field name, |
| 10645 | * and an optional argument of type signed or unsigned integer to request an |
| 10646 | * 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] | 10647 | * headers are considered from the first one. |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 10648 | */ |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 10649 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10650 | 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] | 10651 | { |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 10652 | struct hdr_idx *idx; |
Willy Tarreau | a890d07 | 2013-04-02 12:01:06 +0200 | [diff] [blame] | 10653 | struct hdr_ctx *ctx = smp->ctx.a[0]; |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 10654 | const struct http_msg *msg; |
Willy Tarreau | 185b5c4 | 2012-04-26 15:11:51 +0200 | [diff] [blame] | 10655 | int occ = 0; |
| 10656 | const char *name_str = NULL; |
| 10657 | int name_len = 0; |
Willy Tarreau | e333ec9 | 2012-04-16 16:26:40 +0200 | [diff] [blame] | 10658 | |
Willy Tarreau | a890d07 | 2013-04-02 12:01:06 +0200 | [diff] [blame] | 10659 | if (!ctx) { |
| 10660 | /* first call */ |
| 10661 | ctx = &static_hdr_ctx; |
| 10662 | ctx->idx = 0; |
Willy Tarreau | ffb6f08 | 2013-04-02 23:16:53 +0200 | [diff] [blame] | 10663 | smp->ctx.a[0] = ctx; |
Willy Tarreau | a890d07 | 2013-04-02 12:01:06 +0200 | [diff] [blame] | 10664 | } |
| 10665 | |
Willy Tarreau | 185b5c4 | 2012-04-26 15:11:51 +0200 | [diff] [blame] | 10666 | if (args) { |
| 10667 | if (args[0].type != ARGT_STR) |
| 10668 | return 0; |
| 10669 | name_str = args[0].data.str.str; |
| 10670 | name_len = args[0].data.str.len; |
| 10671 | |
Thierry FOURNIER | bf65cd4 | 2015-07-20 17:45:02 +0200 | [diff] [blame] | 10672 | if (args[1].type == ARGT_SINT) |
| 10673 | occ = args[1].data.sint; |
Willy Tarreau | 185b5c4 | 2012-04-26 15:11:51 +0200 | [diff] [blame] | 10674 | } |
Willy Tarreau | 34db108 | 2012-04-19 17:16:54 +0200 | [diff] [blame] | 10675 | |
Willy Tarreau | e333ec9 | 2012-04-16 16:26:40 +0200 | [diff] [blame] | 10676 | CHECK_HTTP_MESSAGE_FIRST(); |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 10677 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 10678 | idx = &smp->strm->txn->hdr_idx; |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10679 | 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] | 10680 | |
Willy Tarreau | 185b5c4 | 2012-04-26 15:11:51 +0200 | [diff] [blame] | 10681 | if (ctx && !(smp->flags & SMP_F_NOT_LAST)) |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 10682 | /* search for header from the beginning */ |
| 10683 | ctx->idx = 0; |
| 10684 | |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10685 | if (!occ && !(smp->opt & SMP_OPT_ITERATE)) |
Willy Tarreau | 185b5c4 | 2012-04-26 15:11:51 +0200 | [diff] [blame] | 10686 | /* no explicit occurrence and single fetch => last header by default */ |
| 10687 | occ = -1; |
| 10688 | |
| 10689 | if (!occ) |
| 10690 | /* prepare to report multiple occurrences for ACL fetches */ |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 10691 | smp->flags |= SMP_F_NOT_LAST; |
Willy Tarreau | 664092c | 2011-12-16 19:11:42 +0100 | [diff] [blame] | 10692 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 10693 | smp->data.type = SMP_T_STR; |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 10694 | smp->flags |= SMP_F_VOL_HDR | SMP_F_CONST; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 10695 | if (http_get_hdr(msg, name_str, name_len, idx, occ, ctx, &smp->data.u.str.str, &smp->data.u.str.len)) |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 10696 | return 1; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 10697 | |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 10698 | smp->flags &= ~SMP_F_NOT_LAST; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 10699 | return 0; |
| 10700 | } |
| 10701 | |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 10702 | /* 6. Check on HTTP header count. The number of occurrences is returned. |
Willy Tarreau | 34db108 | 2012-04-19 17:16:54 +0200 | [diff] [blame] | 10703 | * Accepts exactly 1 argument of type string. |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 10704 | */ |
| 10705 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10706 | 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] | 10707 | { |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 10708 | struct hdr_idx *idx; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 10709 | struct hdr_ctx ctx; |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 10710 | const struct http_msg *msg; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 10711 | int cnt; |
Willy Tarreau | 601a4d1 | 2015-04-01 19:16:09 +0200 | [diff] [blame] | 10712 | const char *name = NULL; |
| 10713 | int len = 0; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 10714 | |
Willy Tarreau | 601a4d1 | 2015-04-01 19:16:09 +0200 | [diff] [blame] | 10715 | if (args && args->type == ARGT_STR) { |
| 10716 | name = args->data.str.str; |
| 10717 | len = args->data.str.len; |
| 10718 | } |
Willy Tarreau | 34db108 | 2012-04-19 17:16:54 +0200 | [diff] [blame] | 10719 | |
Willy Tarreau | e333ec9 | 2012-04-16 16:26:40 +0200 | [diff] [blame] | 10720 | CHECK_HTTP_MESSAGE_FIRST(); |
| 10721 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 10722 | idx = &smp->strm->txn->hdr_idx; |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10723 | 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] | 10724 | |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 10725 | ctx.idx = 0; |
| 10726 | cnt = 0; |
Willy Tarreau | 601a4d1 | 2015-04-01 19:16:09 +0200 | [diff] [blame] | 10727 | while (http_find_header2(name, len, msg->chn->buf->p, idx, &ctx)) |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 10728 | cnt++; |
| 10729 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 10730 | smp->data.type = SMP_T_SINT; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 10731 | smp->data.u.sint = cnt; |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 10732 | smp->flags = SMP_F_VOL_HDR; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 10733 | return 1; |
| 10734 | } |
| 10735 | |
Willy Tarreau | 185b5c4 | 2012-04-26 15:11:51 +0200 | [diff] [blame] | 10736 | /* Fetch an HTTP header's integer value. The integer value is returned. It |
| 10737 | * takes a mandatory argument of type string and an optional one of type int |
| 10738 | * to designate a specific occurrence. It returns an unsigned integer, which |
| 10739 | * may or may not be appropriate for everything. |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 10740 | */ |
| 10741 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10742 | 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] | 10743 | { |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10744 | int ret = smp_fetch_hdr(args, smp, kw, private); |
Willy Tarreau | e333ec9 | 2012-04-16 16:26:40 +0200 | [diff] [blame] | 10745 | |
Willy Tarreau | f853c46 | 2012-04-23 18:53:56 +0200 | [diff] [blame] | 10746 | if (ret > 0) { |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 10747 | smp->data.type = SMP_T_SINT; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 10748 | smp->data.u.sint = strl2ic(smp->data.u.str.str, smp->data.u.str.len); |
Willy Tarreau | f853c46 | 2012-04-23 18:53:56 +0200 | [diff] [blame] | 10749 | } |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 10750 | |
Willy Tarreau | d53e242 | 2012-04-16 17:21:11 +0200 | [diff] [blame] | 10751 | return ret; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 10752 | } |
| 10753 | |
Cyril Bonté | 69fa992 | 2012-10-25 00:01:06 +0200 | [diff] [blame] | 10754 | /* Fetch an HTTP header's IP value. takes a mandatory argument of type string |
| 10755 | * and an optional one of type int to designate a specific occurrence. |
| 10756 | * It returns an IPv4 or IPv6 address. |
Willy Tarreau | 106f979 | 2009-09-19 07:54:16 +0200 | [diff] [blame] | 10757 | */ |
| 10758 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10759 | 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] | 10760 | { |
Willy Tarreau | d53e242 | 2012-04-16 17:21:11 +0200 | [diff] [blame] | 10761 | int ret; |
Willy Tarreau | e333ec9 | 2012-04-16 16:26:40 +0200 | [diff] [blame] | 10762 | |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10763 | while ((ret = smp_fetch_hdr(args, smp, kw, private)) > 0) { |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 10764 | if (url2ipv4((char *)smp->data.u.str.str, &smp->data.u.ipv4)) { |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 10765 | smp->data.type = SMP_T_IPV4; |
Willy Tarreau | d53e242 | 2012-04-16 17:21:11 +0200 | [diff] [blame] | 10766 | break; |
Cyril Bonté | 69fa992 | 2012-10-25 00:01:06 +0200 | [diff] [blame] | 10767 | } else { |
Willy Tarreau | 47ca545 | 2012-12-23 20:22:19 +0100 | [diff] [blame] | 10768 | struct chunk *temp = get_trash_chunk(); |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 10769 | if (smp->data.u.str.len < temp->size - 1) { |
| 10770 | memcpy(temp->str, smp->data.u.str.str, smp->data.u.str.len); |
| 10771 | temp->str[smp->data.u.str.len] = '\0'; |
| 10772 | if (inet_pton(AF_INET6, temp->str, &smp->data.u.ipv6)) { |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 10773 | smp->data.type = SMP_T_IPV6; |
Cyril Bonté | 69fa992 | 2012-10-25 00:01:06 +0200 | [diff] [blame] | 10774 | break; |
| 10775 | } |
| 10776 | } |
| 10777 | } |
| 10778 | |
Willy Tarreau | d53e242 | 2012-04-16 17:21:11 +0200 | [diff] [blame] | 10779 | /* if the header doesn't match an IP address, fetch next one */ |
Willy Tarreau | 185b5c4 | 2012-04-26 15:11:51 +0200 | [diff] [blame] | 10780 | if (!(smp->flags & SMP_F_NOT_LAST)) |
| 10781 | return 0; |
Willy Tarreau | 106f979 | 2009-09-19 07:54:16 +0200 | [diff] [blame] | 10782 | } |
Willy Tarreau | d53e242 | 2012-04-16 17:21:11 +0200 | [diff] [blame] | 10783 | return ret; |
Willy Tarreau | 106f979 | 2009-09-19 07:54:16 +0200 | [diff] [blame] | 10784 | } |
| 10785 | |
Willy Tarreau | 737b0c1 | 2007-06-10 21:28:46 +0200 | [diff] [blame] | 10786 | /* 8. Check on URI PATH. A pointer to the PATH is stored. The path starts at |
| 10787 | * the first '/' after the possible hostname, and ends before the possible '?'. |
| 10788 | */ |
| 10789 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10790 | 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] | 10791 | { |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 10792 | struct http_txn *txn; |
Willy Tarreau | 737b0c1 | 2007-06-10 21:28:46 +0200 | [diff] [blame] | 10793 | char *ptr, *end; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 10794 | |
Willy Tarreau | c0239e0 | 2012-04-16 14:42:55 +0200 | [diff] [blame] | 10795 | CHECK_HTTP_MESSAGE_FIRST(); |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 10796 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 10797 | txn = smp->strm->txn; |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 10798 | 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] | 10799 | ptr = http_get_path(txn); |
| 10800 | if (!ptr) |
Willy Tarreau | 737b0c1 | 2007-06-10 21:28:46 +0200 | [diff] [blame] | 10801 | return 0; |
| 10802 | |
| 10803 | /* OK, we got the '/' ! */ |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 10804 | smp->data.type = SMP_T_STR; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 10805 | smp->data.u.str.str = ptr; |
Willy Tarreau | 737b0c1 | 2007-06-10 21:28:46 +0200 | [diff] [blame] | 10806 | |
| 10807 | while (ptr < end && *ptr != '?') |
| 10808 | ptr++; |
| 10809 | |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 10810 | smp->data.u.str.len = ptr - smp->data.u.str.str; |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 10811 | smp->flags = SMP_F_VOL_1ST | SMP_F_CONST; |
Willy Tarreau | 737b0c1 | 2007-06-10 21:28:46 +0200 | [diff] [blame] | 10812 | return 1; |
| 10813 | } |
| 10814 | |
Willy Tarreau | a7ad50c | 2012-04-29 15:39:40 +0200 | [diff] [blame] | 10815 | /* This produces a concatenation of the first occurrence of the Host header |
| 10816 | * followed by the path component if it begins with a slash ('/'). This means |
| 10817 | * that '*' will not be added, resulting in exactly the first Host entry. |
| 10818 | * If no Host header is found, then the path is returned as-is. The returned |
| 10819 | * 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] | 10820 | * The returned sample is of type string. |
Willy Tarreau | a7ad50c | 2012-04-29 15:39:40 +0200 | [diff] [blame] | 10821 | */ |
| 10822 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10823 | 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] | 10824 | { |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 10825 | struct http_txn *txn; |
Willy Tarreau | a7ad50c | 2012-04-29 15:39:40 +0200 | [diff] [blame] | 10826 | char *ptr, *end, *beg; |
| 10827 | struct hdr_ctx ctx; |
Willy Tarreau | 3caf2af | 2014-06-24 17:27:02 +0200 | [diff] [blame] | 10828 | struct chunk *temp; |
Willy Tarreau | a7ad50c | 2012-04-29 15:39:40 +0200 | [diff] [blame] | 10829 | |
| 10830 | CHECK_HTTP_MESSAGE_FIRST(); |
| 10831 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 10832 | txn = smp->strm->txn; |
Willy Tarreau | a7ad50c | 2012-04-29 15:39:40 +0200 | [diff] [blame] | 10833 | ctx.idx = 0; |
Willy Tarreau | 877e78d | 2013-04-07 18:48:08 +0200 | [diff] [blame] | 10834 | 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] | 10835 | return smp_fetch_path(args, smp, kw, private); |
Willy Tarreau | a7ad50c | 2012-04-29 15:39:40 +0200 | [diff] [blame] | 10836 | |
| 10837 | /* 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] | 10838 | temp = get_trash_chunk(); |
| 10839 | memcpy(temp->str, ctx.line + ctx.val, ctx.vlen); |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 10840 | smp->data.type = SMP_T_STR; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 10841 | smp->data.u.str.str = temp->str; |
| 10842 | smp->data.u.str.len = ctx.vlen; |
Willy Tarreau | a7ad50c | 2012-04-29 15:39:40 +0200 | [diff] [blame] | 10843 | |
| 10844 | /* now retrieve the path */ |
Willy Tarreau | 877e78d | 2013-04-07 18:48:08 +0200 | [diff] [blame] | 10845 | 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] | 10846 | beg = http_get_path(txn); |
| 10847 | if (!beg) |
| 10848 | beg = end; |
| 10849 | |
| 10850 | for (ptr = beg; ptr < end && *ptr != '?'; ptr++); |
| 10851 | |
| 10852 | if (beg < ptr && *beg == '/') { |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 10853 | memcpy(smp->data.u.str.str + smp->data.u.str.len, beg, ptr - beg); |
| 10854 | smp->data.u.str.len += ptr - beg; |
Willy Tarreau | a7ad50c | 2012-04-29 15:39:40 +0200 | [diff] [blame] | 10855 | } |
| 10856 | |
| 10857 | smp->flags = SMP_F_VOL_1ST; |
| 10858 | return 1; |
| 10859 | } |
| 10860 | |
Willy Tarreau | ab1f7b7 | 2012-12-09 13:38:54 +0100 | [diff] [blame] | 10861 | /* This produces a 32-bit hash of the concatenation of the first occurrence of |
| 10862 | * the Host header followed by the path component if it begins with a slash ('/'). |
| 10863 | * This means that '*' will not be added, resulting in exactly the first Host |
| 10864 | * 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] | 10865 | * is hashed using the path hash followed by a full avalanche hash and provides a |
| 10866 | * 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] | 10867 | * high-traffic sites without having to store whole paths. |
| 10868 | */ |
Thierry FOURNIER | 055b9d5 | 2014-07-15 16:11:07 +0200 | [diff] [blame] | 10869 | int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10870 | 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] | 10871 | { |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 10872 | struct http_txn *txn; |
Willy Tarreau | ab1f7b7 | 2012-12-09 13:38:54 +0100 | [diff] [blame] | 10873 | struct hdr_ctx ctx; |
| 10874 | unsigned int hash = 0; |
| 10875 | char *ptr, *beg, *end; |
| 10876 | int len; |
| 10877 | |
| 10878 | CHECK_HTTP_MESSAGE_FIRST(); |
| 10879 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 10880 | txn = smp->strm->txn; |
Willy Tarreau | ab1f7b7 | 2012-12-09 13:38:54 +0100 | [diff] [blame] | 10881 | ctx.idx = 0; |
Willy Tarreau | 877e78d | 2013-04-07 18:48:08 +0200 | [diff] [blame] | 10882 | 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] | 10883 | /* OK we have the header value in ctx.line+ctx.val for ctx.vlen bytes */ |
| 10884 | ptr = ctx.line + ctx.val; |
| 10885 | len = ctx.vlen; |
| 10886 | while (len--) |
| 10887 | hash = *(ptr++) + (hash << 6) + (hash << 16) - hash; |
| 10888 | } |
| 10889 | |
| 10890 | /* now retrieve the path */ |
Willy Tarreau | 877e78d | 2013-04-07 18:48:08 +0200 | [diff] [blame] | 10891 | 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] | 10892 | beg = http_get_path(txn); |
| 10893 | if (!beg) |
| 10894 | beg = end; |
| 10895 | |
| 10896 | for (ptr = beg; ptr < end && *ptr != '?'; ptr++); |
| 10897 | |
| 10898 | if (beg < ptr && *beg == '/') { |
| 10899 | while (beg < ptr) |
| 10900 | hash = *(beg++) + (hash << 6) + (hash << 16) - hash; |
| 10901 | } |
| 10902 | hash = full_hash(hash); |
| 10903 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 10904 | smp->data.type = SMP_T_SINT; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 10905 | smp->data.u.sint = hash; |
Willy Tarreau | ab1f7b7 | 2012-12-09 13:38:54 +0100 | [diff] [blame] | 10906 | smp->flags = SMP_F_VOL_1ST; |
| 10907 | return 1; |
| 10908 | } |
| 10909 | |
Willy Tarreau | 4a55060 | 2012-12-09 14:53:32 +0100 | [diff] [blame] | 10910 | /* 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] | 10911 | * path as returned by smp_fetch_base32(). The idea is to have per-source and |
| 10912 | * per-path counters. The result is a binary block from 8 to 20 bytes depending |
| 10913 | * 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] | 10914 | * that in environments where IPv6 is insignificant, truncating the output to |
| 10915 | * 8 bytes would still work. |
| 10916 | */ |
| 10917 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10918 | 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] | 10919 | { |
Willy Tarreau | 47ca545 | 2012-12-23 20:22:19 +0100 | [diff] [blame] | 10920 | struct chunk *temp; |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 10921 | struct connection *cli_conn = objt_conn(smp->sess->origin); |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 10922 | |
| 10923 | if (!cli_conn) |
| 10924 | return 0; |
Willy Tarreau | 4a55060 | 2012-12-09 14:53:32 +0100 | [diff] [blame] | 10925 | |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10926 | if (!smp_fetch_base32(args, smp, kw, private)) |
Willy Tarreau | 4a55060 | 2012-12-09 14:53:32 +0100 | [diff] [blame] | 10927 | return 0; |
| 10928 | |
Willy Tarreau | 47ca545 | 2012-12-23 20:22:19 +0100 | [diff] [blame] | 10929 | temp = get_trash_chunk(); |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 10930 | *(unsigned int *)temp->str = htonl(smp->data.u.sint); |
Willy Tarreau | 5ad6e1d | 2014-07-15 21:34:06 +0200 | [diff] [blame] | 10931 | temp->len += sizeof(unsigned int); |
Willy Tarreau | 4a55060 | 2012-12-09 14:53:32 +0100 | [diff] [blame] | 10932 | |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 10933 | switch (cli_conn->addr.from.ss_family) { |
Willy Tarreau | 4a55060 | 2012-12-09 14:53:32 +0100 | [diff] [blame] | 10934 | case AF_INET: |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 10935 | 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] | 10936 | temp->len += 4; |
| 10937 | break; |
| 10938 | case AF_INET6: |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 10939 | 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] | 10940 | temp->len += 16; |
| 10941 | break; |
| 10942 | default: |
| 10943 | return 0; |
| 10944 | } |
| 10945 | |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 10946 | smp->data.u.str = *temp; |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 10947 | smp->data.type = SMP_T_BIN; |
Willy Tarreau | 4a55060 | 2012-12-09 14:53:32 +0100 | [diff] [blame] | 10948 | return 1; |
| 10949 | } |
| 10950 | |
Willy Tarreau | 49ad95c | 2015-01-19 15:06:26 +0100 | [diff] [blame] | 10951 | /* Extracts the query string, which comes after the question mark '?'. If no |
| 10952 | * question mark is found, nothing is returned. Otherwise it returns a sample |
| 10953 | * of type string carrying the whole query string. |
| 10954 | */ |
| 10955 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10956 | 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] | 10957 | { |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 10958 | struct http_txn *txn; |
Willy Tarreau | 49ad95c | 2015-01-19 15:06:26 +0100 | [diff] [blame] | 10959 | char *ptr, *end; |
| 10960 | |
| 10961 | CHECK_HTTP_MESSAGE_FIRST(); |
| 10962 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 10963 | txn = smp->strm->txn; |
Willy Tarreau | 49ad95c | 2015-01-19 15:06:26 +0100 | [diff] [blame] | 10964 | ptr = txn->req.chn->buf->p + txn->req.sl.rq.u; |
| 10965 | end = ptr + txn->req.sl.rq.u_l; |
| 10966 | |
| 10967 | /* look up the '?' */ |
| 10968 | do { |
| 10969 | if (ptr == end) |
| 10970 | return 0; |
| 10971 | } while (*ptr++ != '?'); |
| 10972 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 10973 | smp->data.type = SMP_T_STR; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 10974 | smp->data.u.str.str = ptr; |
| 10975 | smp->data.u.str.len = end - ptr; |
Willy Tarreau | 49ad95c | 2015-01-19 15:06:26 +0100 | [diff] [blame] | 10976 | smp->flags = SMP_F_VOL_1ST | SMP_F_CONST; |
| 10977 | return 1; |
| 10978 | } |
| 10979 | |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 10980 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10981 | 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] | 10982 | { |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 10983 | /* Note: hdr_idx.v cannot be NULL in this ACL because the ACL is tagged |
| 10984 | * as a layer7 ACL, which involves automatic allocation of hdr_idx. |
| 10985 | */ |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 10986 | |
Willy Tarreau | 24e32d8 | 2012-04-23 23:55:44 +0200 | [diff] [blame] | 10987 | CHECK_HTTP_MESSAGE_FIRST_PERM(); |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 10988 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 10989 | smp->data.type = SMP_T_BOOL; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 10990 | smp->data.u.sint = 1; |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 10991 | return 1; |
| 10992 | } |
| 10993 | |
Willy Tarreau | 7f18e52 | 2010-10-22 20:04:13 +0200 | [diff] [blame] | 10994 | /* return a valid test if the current request is the first one on the connection */ |
| 10995 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10996 | 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] | 10997 | { |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 10998 | smp->data.type = SMP_T_BOOL; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 10999 | smp->data.u.sint = !(smp->strm->txn->flags & TX_NOT_FIRST); |
Willy Tarreau | 7f18e52 | 2010-10-22 20:04:13 +0200 | [diff] [blame] | 11000 | return 1; |
| 11001 | } |
| 11002 | |
Willy Tarreau | 34db108 | 2012-04-19 17:16:54 +0200 | [diff] [blame] | 11003 | /* Accepts exactly 1 argument of type userlist */ |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 11004 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 11005 | 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] | 11006 | { |
| 11007 | |
Willy Tarreau | 24e32d8 | 2012-04-23 23:55:44 +0200 | [diff] [blame] | 11008 | if (!args || args->type != ARGT_USR) |
Willy Tarreau | 34db108 | 2012-04-19 17:16:54 +0200 | [diff] [blame] | 11009 | return 0; |
| 11010 | |
Willy Tarreau | c0239e0 | 2012-04-16 14:42:55 +0200 | [diff] [blame] | 11011 | CHECK_HTTP_MESSAGE_FIRST(); |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 11012 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 11013 | if (!get_http_auth(smp->strm)) |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 11014 | return 0; |
| 11015 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 11016 | smp->data.type = SMP_T_BOOL; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 11017 | smp->data.u.sint = check_user(args->data.usr, smp->strm->txn->auth.user, |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 11018 | smp->strm->txn->auth.pass); |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 11019 | return 1; |
| 11020 | } |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 11021 | |
Willy Tarreau | 4a3fd4c | 2012-05-10 23:18:26 +0200 | [diff] [blame] | 11022 | /* Accepts exactly 1 argument of type userlist */ |
| 11023 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 11024 | 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] | 11025 | { |
Willy Tarreau | 4a3fd4c | 2012-05-10 23:18:26 +0200 | [diff] [blame] | 11026 | if (!args || args->type != ARGT_USR) |
| 11027 | return 0; |
| 11028 | |
| 11029 | CHECK_HTTP_MESSAGE_FIRST(); |
| 11030 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 11031 | if (!get_http_auth(smp->strm)) |
Willy Tarreau | 4a3fd4c | 2012-05-10 23:18:26 +0200 | [diff] [blame] | 11032 | return 0; |
| 11033 | |
Willy Tarreau | 4a3fd4c | 2012-05-10 23:18:26 +0200 | [diff] [blame] | 11034 | /* if the user does not belong to the userlist or has a wrong password, |
| 11035 | * report that it unconditionally does not match. Otherwise we return |
Thierry FOURNIER | 9eec0a6 | 2014-01-22 18:38:02 +0100 | [diff] [blame] | 11036 | * a string containing the username. |
Willy Tarreau | 4a3fd4c | 2012-05-10 23:18:26 +0200 | [diff] [blame] | 11037 | */ |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 11038 | if (!check_user(args->data.usr, smp->strm->txn->auth.user, |
| 11039 | smp->strm->txn->auth.pass)) |
Thierry FOURNIER | 9eec0a6 | 2014-01-22 18:38:02 +0100 | [diff] [blame] | 11040 | return 0; |
| 11041 | |
| 11042 | /* pat_match_auth() will need the user list */ |
| 11043 | smp->ctx.a[0] = args->data.usr; |
| 11044 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 11045 | smp->data.type = SMP_T_STR; |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 11046 | smp->flags = SMP_F_CONST; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 11047 | smp->data.u.str.str = smp->strm->txn->auth.user; |
| 11048 | smp->data.u.str.len = strlen(smp->strm->txn->auth.user); |
Willy Tarreau | 4a3fd4c | 2012-05-10 23:18:26 +0200 | [diff] [blame] | 11049 | |
| 11050 | return 1; |
| 11051 | } |
| 11052 | |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 11053 | /* Try to find the next occurrence of a cookie name in a cookie header value. |
| 11054 | * The lookup begins at <hdr>. The pointer and size of the next occurrence of |
| 11055 | * the cookie value is returned into *value and *value_l, and the function |
| 11056 | * returns a pointer to the next pointer to search from if the value was found. |
| 11057 | * Otherwise if the cookie was not found, NULL is returned and neither value |
| 11058 | * nor value_l are touched. The input <hdr> string should first point to the |
| 11059 | * header's value, and the <hdr_end> pointer must point to the first character |
| 11060 | * not part of the value. <list> must be non-zero if value may represent a list |
| 11061 | * of values (cookie headers). This makes it faster to abort parsing when no |
| 11062 | * list is expected. |
| 11063 | */ |
David Carlier | 4686f79 | 2015-09-25 14:10:50 +0100 | [diff] [blame] | 11064 | char * |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 11065 | extract_cookie_value(char *hdr, const char *hdr_end, |
| 11066 | char *cookie_name, size_t cookie_name_l, int list, |
Willy Tarreau | 3fb818c | 2012-04-11 17:21:08 +0200 | [diff] [blame] | 11067 | char **value, int *value_l) |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 11068 | { |
| 11069 | char *equal, *att_end, *att_beg, *val_beg, *val_end; |
| 11070 | char *next; |
| 11071 | |
| 11072 | /* we search at least a cookie name followed by an equal, and more |
| 11073 | * generally something like this : |
| 11074 | * Cookie: NAME1 = VALUE 1 ; NAME2 = VALUE2 ; NAME3 = VALUE3\r\n |
| 11075 | */ |
| 11076 | for (att_beg = hdr; att_beg + cookie_name_l + 1 < hdr_end; att_beg = next + 1) { |
| 11077 | /* Iterate through all cookies on this line */ |
| 11078 | |
Willy Tarreau | 2235b26 | 2016-11-05 15:50:20 +0100 | [diff] [blame] | 11079 | while (att_beg < hdr_end && HTTP_IS_SPHT(*att_beg)) |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 11080 | att_beg++; |
| 11081 | |
| 11082 | /* find att_end : this is the first character after the last non |
| 11083 | * space before the equal. It may be equal to hdr_end. |
| 11084 | */ |
| 11085 | equal = att_end = att_beg; |
| 11086 | |
| 11087 | while (equal < hdr_end) { |
| 11088 | if (*equal == '=' || *equal == ';' || (list && *equal == ',')) |
| 11089 | break; |
Willy Tarreau | 2235b26 | 2016-11-05 15:50:20 +0100 | [diff] [blame] | 11090 | if (HTTP_IS_SPHT(*equal++)) |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 11091 | continue; |
| 11092 | att_end = equal; |
| 11093 | } |
| 11094 | |
| 11095 | /* here, <equal> points to '=', a delimitor or the end. <att_end> |
| 11096 | * is between <att_beg> and <equal>, both may be identical. |
| 11097 | */ |
| 11098 | |
| 11099 | /* look for end of cookie if there is an equal sign */ |
| 11100 | if (equal < hdr_end && *equal == '=') { |
| 11101 | /* look for the beginning of the value */ |
| 11102 | val_beg = equal + 1; |
Willy Tarreau | 2235b26 | 2016-11-05 15:50:20 +0100 | [diff] [blame] | 11103 | while (val_beg < hdr_end && HTTP_IS_SPHT(*val_beg)) |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 11104 | val_beg++; |
| 11105 | |
| 11106 | /* find the end of the value, respecting quotes */ |
| 11107 | next = find_cookie_value_end(val_beg, hdr_end); |
| 11108 | |
| 11109 | /* make val_end point to the first white space or delimitor after the value */ |
| 11110 | val_end = next; |
Willy Tarreau | 2235b26 | 2016-11-05 15:50:20 +0100 | [diff] [blame] | 11111 | while (val_end > val_beg && HTTP_IS_SPHT(*(val_end - 1))) |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 11112 | val_end--; |
| 11113 | } else { |
| 11114 | val_beg = val_end = next = equal; |
| 11115 | } |
| 11116 | |
| 11117 | /* We have nothing to do with attributes beginning with '$'. However, |
| 11118 | * they will automatically be removed if a header before them is removed, |
| 11119 | * since they're supposed to be linked together. |
| 11120 | */ |
| 11121 | if (*att_beg == '$') |
| 11122 | continue; |
| 11123 | |
| 11124 | /* Ignore cookies with no equal sign */ |
| 11125 | if (equal == next) |
| 11126 | continue; |
| 11127 | |
| 11128 | /* Now we have the cookie name between att_beg and att_end, and |
| 11129 | * its value between val_beg and val_end. |
| 11130 | */ |
| 11131 | |
| 11132 | if (att_end - att_beg == cookie_name_l && |
| 11133 | memcmp(att_beg, cookie_name, cookie_name_l) == 0) { |
| 11134 | /* let's return this value and indicate where to go on from */ |
| 11135 | *value = val_beg; |
| 11136 | *value_l = val_end - val_beg; |
| 11137 | return next + 1; |
| 11138 | } |
| 11139 | |
| 11140 | /* Set-Cookie headers only have the name in the first attr=value part */ |
| 11141 | if (!list) |
| 11142 | break; |
| 11143 | } |
| 11144 | |
| 11145 | return NULL; |
| 11146 | } |
| 11147 | |
William Lallemand | a43ba4e | 2014-01-28 18:14:25 +0100 | [diff] [blame] | 11148 | /* Fetch a captured HTTP request header. The index is the position of |
| 11149 | * the "capture" option in the configuration file |
| 11150 | */ |
| 11151 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 11152 | 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] | 11153 | { |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 11154 | struct proxy *fe = strm_fe(smp->strm); |
William Lallemand | a43ba4e | 2014-01-28 18:14:25 +0100 | [diff] [blame] | 11155 | int idx; |
| 11156 | |
Thierry FOURNIER | bf65cd4 | 2015-07-20 17:45:02 +0200 | [diff] [blame] | 11157 | if (!args || args->type != ARGT_SINT) |
William Lallemand | a43ba4e | 2014-01-28 18:14:25 +0100 | [diff] [blame] | 11158 | return 0; |
| 11159 | |
Thierry FOURNIER | bf65cd4 | 2015-07-20 17:45:02 +0200 | [diff] [blame] | 11160 | idx = args->data.sint; |
William Lallemand | a43ba4e | 2014-01-28 18:14:25 +0100 | [diff] [blame] | 11161 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 11162 | 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] | 11163 | return 0; |
| 11164 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 11165 | smp->data.type = SMP_T_STR; |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 11166 | smp->flags |= SMP_F_CONST; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 11167 | smp->data.u.str.str = smp->strm->req_cap[idx]; |
| 11168 | smp->data.u.str.len = strlen(smp->strm->req_cap[idx]); |
William Lallemand | a43ba4e | 2014-01-28 18:14:25 +0100 | [diff] [blame] | 11169 | |
| 11170 | return 1; |
| 11171 | } |
| 11172 | |
| 11173 | /* Fetch a captured HTTP response header. The index is the position of |
| 11174 | * the "capture" option in the configuration file |
| 11175 | */ |
| 11176 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 11177 | 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] | 11178 | { |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 11179 | struct proxy *fe = strm_fe(smp->strm); |
William Lallemand | a43ba4e | 2014-01-28 18:14:25 +0100 | [diff] [blame] | 11180 | int idx; |
| 11181 | |
Thierry FOURNIER | bf65cd4 | 2015-07-20 17:45:02 +0200 | [diff] [blame] | 11182 | if (!args || args->type != ARGT_SINT) |
William Lallemand | a43ba4e | 2014-01-28 18:14:25 +0100 | [diff] [blame] | 11183 | return 0; |
| 11184 | |
Thierry FOURNIER | bf65cd4 | 2015-07-20 17:45:02 +0200 | [diff] [blame] | 11185 | idx = args->data.sint; |
William Lallemand | a43ba4e | 2014-01-28 18:14:25 +0100 | [diff] [blame] | 11186 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 11187 | 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] | 11188 | return 0; |
| 11189 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 11190 | smp->data.type = SMP_T_STR; |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 11191 | smp->flags |= SMP_F_CONST; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 11192 | smp->data.u.str.str = smp->strm->res_cap[idx]; |
| 11193 | smp->data.u.str.len = strlen(smp->strm->res_cap[idx]); |
William Lallemand | a43ba4e | 2014-01-28 18:14:25 +0100 | [diff] [blame] | 11194 | |
| 11195 | return 1; |
| 11196 | } |
| 11197 | |
William Lallemand | 65ad6e1 | 2014-01-31 15:08:02 +0100 | [diff] [blame] | 11198 | /* Extracts the METHOD in the HTTP request, the txn->uri should be filled before the call */ |
| 11199 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 11200 | 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] | 11201 | { |
| 11202 | struct chunk *temp; |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 11203 | struct http_txn *txn = smp->strm->txn; |
William Lallemand | 96a7785 | 2014-02-05 00:30:02 +0100 | [diff] [blame] | 11204 | char *ptr; |
William Lallemand | 65ad6e1 | 2014-01-31 15:08:02 +0100 | [diff] [blame] | 11205 | |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 11206 | if (!txn || !txn->uri) |
William Lallemand | 65ad6e1 | 2014-01-31 15:08:02 +0100 | [diff] [blame] | 11207 | return 0; |
| 11208 | |
William Lallemand | 96a7785 | 2014-02-05 00:30:02 +0100 | [diff] [blame] | 11209 | ptr = txn->uri; |
William Lallemand | 65ad6e1 | 2014-01-31 15:08:02 +0100 | [diff] [blame] | 11210 | |
William Lallemand | 96a7785 | 2014-02-05 00:30:02 +0100 | [diff] [blame] | 11211 | while (*ptr != ' ' && *ptr != '\0') /* find first space */ |
| 11212 | ptr++; |
William Lallemand | 65ad6e1 | 2014-01-31 15:08:02 +0100 | [diff] [blame] | 11213 | |
William Lallemand | 96a7785 | 2014-02-05 00:30:02 +0100 | [diff] [blame] | 11214 | temp = get_trash_chunk(); |
| 11215 | temp->str = txn->uri; |
| 11216 | temp->len = ptr - txn->uri; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 11217 | smp->data.u.str = *temp; |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 11218 | smp->data.type = SMP_T_STR; |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 11219 | smp->flags = SMP_F_CONST; |
William Lallemand | 65ad6e1 | 2014-01-31 15:08:02 +0100 | [diff] [blame] | 11220 | |
| 11221 | return 1; |
| 11222 | |
| 11223 | } |
| 11224 | |
| 11225 | /* Extracts the path in the HTTP request, the txn->uri should be filled before the call */ |
| 11226 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 11227 | 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] | 11228 | { |
| 11229 | struct chunk *temp; |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 11230 | struct http_txn *txn = smp->strm->txn; |
William Lallemand | 65ad6e1 | 2014-01-31 15:08:02 +0100 | [diff] [blame] | 11231 | char *ptr; |
William Lallemand | 65ad6e1 | 2014-01-31 15:08:02 +0100 | [diff] [blame] | 11232 | |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 11233 | if (!txn || !txn->uri) |
William Lallemand | 65ad6e1 | 2014-01-31 15:08:02 +0100 | [diff] [blame] | 11234 | return 0; |
William Lallemand | 96a7785 | 2014-02-05 00:30:02 +0100 | [diff] [blame] | 11235 | |
William Lallemand | 65ad6e1 | 2014-01-31 15:08:02 +0100 | [diff] [blame] | 11236 | ptr = txn->uri; |
| 11237 | |
| 11238 | while (*ptr != ' ' && *ptr != '\0') /* find first space */ |
| 11239 | ptr++; |
William Lallemand | 96a7785 | 2014-02-05 00:30:02 +0100 | [diff] [blame] | 11240 | |
William Lallemand | 65ad6e1 | 2014-01-31 15:08:02 +0100 | [diff] [blame] | 11241 | if (!*ptr) |
| 11242 | return 0; |
| 11243 | |
| 11244 | ptr++; /* skip the space */ |
| 11245 | |
| 11246 | temp = get_trash_chunk(); |
William Lallemand | 96a7785 | 2014-02-05 00:30:02 +0100 | [diff] [blame] | 11247 | ptr = temp->str = http_get_path_from_string(ptr); |
William Lallemand | 65ad6e1 | 2014-01-31 15:08:02 +0100 | [diff] [blame] | 11248 | if (!ptr) |
| 11249 | return 0; |
| 11250 | while (*ptr != ' ' && *ptr != '\0') /* find space after URI */ |
| 11251 | ptr++; |
William Lallemand | 65ad6e1 | 2014-01-31 15:08:02 +0100 | [diff] [blame] | 11252 | |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 11253 | smp->data.u.str = *temp; |
| 11254 | smp->data.u.str.len = ptr - temp->str; |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 11255 | smp->data.type = SMP_T_STR; |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 11256 | smp->flags = SMP_F_CONST; |
William Lallemand | 65ad6e1 | 2014-01-31 15:08:02 +0100 | [diff] [blame] | 11257 | |
| 11258 | return 1; |
| 11259 | } |
| 11260 | |
Willy Tarreau | 3c1b5ec | 2014-04-24 23:41:57 +0200 | [diff] [blame] | 11261 | /* Retrieves the HTTP version from the request (either 1.0 or 1.1) and emits it |
| 11262 | * as a string (either "HTTP/1.0" or "HTTP/1.1"). |
| 11263 | */ |
| 11264 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 11265 | 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] | 11266 | { |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 11267 | struct http_txn *txn = smp->strm->txn; |
Willy Tarreau | 3c1b5ec | 2014-04-24 23:41:57 +0200 | [diff] [blame] | 11268 | |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 11269 | if (!txn || txn->req.msg_state < HTTP_MSG_HDR_FIRST) |
Willy Tarreau | 3c1b5ec | 2014-04-24 23:41:57 +0200 | [diff] [blame] | 11270 | return 0; |
| 11271 | |
| 11272 | if (txn->req.flags & HTTP_MSGF_VER_11) |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 11273 | smp->data.u.str.str = "HTTP/1.1"; |
Willy Tarreau | 3c1b5ec | 2014-04-24 23:41:57 +0200 | [diff] [blame] | 11274 | else |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 11275 | smp->data.u.str.str = "HTTP/1.0"; |
Willy Tarreau | 3c1b5ec | 2014-04-24 23:41:57 +0200 | [diff] [blame] | 11276 | |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 11277 | smp->data.u.str.len = 8; |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 11278 | smp->data.type = SMP_T_STR; |
Willy Tarreau | 3c1b5ec | 2014-04-24 23:41:57 +0200 | [diff] [blame] | 11279 | smp->flags = SMP_F_CONST; |
| 11280 | return 1; |
| 11281 | |
| 11282 | } |
| 11283 | |
| 11284 | /* Retrieves the HTTP version from the response (either 1.0 or 1.1) and emits it |
| 11285 | * as a string (either "HTTP/1.0" or "HTTP/1.1"). |
| 11286 | */ |
| 11287 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 11288 | 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] | 11289 | { |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 11290 | struct http_txn *txn = smp->strm->txn; |
Willy Tarreau | 3c1b5ec | 2014-04-24 23:41:57 +0200 | [diff] [blame] | 11291 | |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 11292 | if (!txn || txn->rsp.msg_state < HTTP_MSG_HDR_FIRST) |
Willy Tarreau | 3c1b5ec | 2014-04-24 23:41:57 +0200 | [diff] [blame] | 11293 | return 0; |
| 11294 | |
| 11295 | if (txn->rsp.flags & HTTP_MSGF_VER_11) |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 11296 | smp->data.u.str.str = "HTTP/1.1"; |
Willy Tarreau | 3c1b5ec | 2014-04-24 23:41:57 +0200 | [diff] [blame] | 11297 | else |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 11298 | smp->data.u.str.str = "HTTP/1.0"; |
Willy Tarreau | 3c1b5ec | 2014-04-24 23:41:57 +0200 | [diff] [blame] | 11299 | |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 11300 | smp->data.u.str.len = 8; |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 11301 | smp->data.type = SMP_T_STR; |
Willy Tarreau | 3c1b5ec | 2014-04-24 23:41:57 +0200 | [diff] [blame] | 11302 | smp->flags = SMP_F_CONST; |
| 11303 | return 1; |
| 11304 | |
| 11305 | } |
| 11306 | |
William Lallemand | 65ad6e1 | 2014-01-31 15:08:02 +0100 | [diff] [blame] | 11307 | |
Willy Tarreau | e333ec9 | 2012-04-16 16:26:40 +0200 | [diff] [blame] | 11308 | /* 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] | 11309 | * 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] | 11310 | * 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] | 11311 | * 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] | 11312 | * 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] | 11313 | * Accepts exactly 1 argument of type string. If the input options indicate |
| 11314 | * 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] | 11315 | * The returned sample is of type CSTR. Can be used to parse cookies in other |
| 11316 | * files. |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 11317 | */ |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 11318 | 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] | 11319 | { |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 11320 | struct http_txn *txn; |
| 11321 | struct hdr_idx *idx; |
Willy Tarreau | a890d07 | 2013-04-02 12:01:06 +0200 | [diff] [blame] | 11322 | struct hdr_ctx *ctx = smp->ctx.a[2]; |
Willy Tarreau | e333ec9 | 2012-04-16 16:26:40 +0200 | [diff] [blame] | 11323 | const struct http_msg *msg; |
| 11324 | const char *hdr_name; |
| 11325 | int hdr_name_len; |
| 11326 | char *sol; |
Willy Tarreau | 28376d6 | 2012-04-26 21:26:10 +0200 | [diff] [blame] | 11327 | int occ = 0; |
| 11328 | int found = 0; |
Willy Tarreau | e333ec9 | 2012-04-16 16:26:40 +0200 | [diff] [blame] | 11329 | |
Willy Tarreau | 24e32d8 | 2012-04-23 23:55:44 +0200 | [diff] [blame] | 11330 | if (!args || args->type != ARGT_STR) |
Willy Tarreau | 34db108 | 2012-04-19 17:16:54 +0200 | [diff] [blame] | 11331 | return 0; |
| 11332 | |
Willy Tarreau | a890d07 | 2013-04-02 12:01:06 +0200 | [diff] [blame] | 11333 | if (!ctx) { |
| 11334 | /* first call */ |
| 11335 | ctx = &static_hdr_ctx; |
| 11336 | ctx->idx = 0; |
| 11337 | smp->ctx.a[2] = ctx; |
| 11338 | } |
| 11339 | |
Willy Tarreau | e333ec9 | 2012-04-16 16:26:40 +0200 | [diff] [blame] | 11340 | CHECK_HTTP_MESSAGE_FIRST(); |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 11341 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 11342 | txn = smp->strm->txn; |
| 11343 | idx = &smp->strm->txn->hdr_idx; |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 11344 | |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 11345 | if ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) { |
Willy Tarreau | e333ec9 | 2012-04-16 16:26:40 +0200 | [diff] [blame] | 11346 | msg = &txn->req; |
| 11347 | hdr_name = "Cookie"; |
| 11348 | hdr_name_len = 6; |
| 11349 | } else { |
| 11350 | msg = &txn->rsp; |
| 11351 | hdr_name = "Set-Cookie"; |
| 11352 | hdr_name_len = 10; |
| 11353 | } |
| 11354 | |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 11355 | if (!occ && !(smp->opt & SMP_OPT_ITERATE)) |
Willy Tarreau | 28376d6 | 2012-04-26 21:26:10 +0200 | [diff] [blame] | 11356 | /* no explicit occurrence and single fetch => last cookie by default */ |
| 11357 | occ = -1; |
| 11358 | |
| 11359 | /* OK so basically here, either we want only one value and it's the |
| 11360 | * last one, or we want to iterate over all of them and we fetch the |
| 11361 | * next one. |
| 11362 | */ |
| 11363 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 11364 | sol = msg->chn->buf->p; |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 11365 | if (!(smp->flags & SMP_F_NOT_LAST)) { |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 11366 | /* search for the header from the beginning, we must first initialize |
| 11367 | * the search parameters. |
| 11368 | */ |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 11369 | smp->ctx.a[0] = NULL; |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 11370 | ctx->idx = 0; |
| 11371 | } |
| 11372 | |
Willy Tarreau | 28376d6 | 2012-04-26 21:26:10 +0200 | [diff] [blame] | 11373 | smp->flags |= SMP_F_VOL_HDR; |
| 11374 | |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 11375 | while (1) { |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 11376 | /* Note: smp->ctx.a[0] == NULL every time we need to fetch a new header */ |
| 11377 | if (!smp->ctx.a[0]) { |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 11378 | if (!http_find_header2(hdr_name, hdr_name_len, sol, idx, ctx)) |
| 11379 | goto out; |
| 11380 | |
Willy Tarreau | 24e32d8 | 2012-04-23 23:55:44 +0200 | [diff] [blame] | 11381 | if (ctx->vlen < args->data.str.len + 1) |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 11382 | continue; |
| 11383 | |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 11384 | smp->ctx.a[0] = ctx->line + ctx->val; |
| 11385 | smp->ctx.a[1] = smp->ctx.a[0] + ctx->vlen; |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 11386 | } |
| 11387 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 11388 | smp->data.type = SMP_T_STR; |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 11389 | smp->flags |= SMP_F_CONST; |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 11390 | 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] | 11391 | args->data.str.str, args->data.str.len, |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 11392 | (smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ, |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 11393 | &smp->data.u.str.str, |
| 11394 | &smp->data.u.str.len); |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 11395 | if (smp->ctx.a[0]) { |
Willy Tarreau | 28376d6 | 2012-04-26 21:26:10 +0200 | [diff] [blame] | 11396 | found = 1; |
| 11397 | if (occ >= 0) { |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 11398 | /* one value was returned into smp->data.u.str.{str,len} */ |
Willy Tarreau | 28376d6 | 2012-04-26 21:26:10 +0200 | [diff] [blame] | 11399 | smp->flags |= SMP_F_NOT_LAST; |
| 11400 | return 1; |
| 11401 | } |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 11402 | } |
Willy Tarreau | 28376d6 | 2012-04-26 21:26:10 +0200 | [diff] [blame] | 11403 | /* if we're looking for last occurrence, let's loop */ |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 11404 | } |
Willy Tarreau | 28376d6 | 2012-04-26 21:26:10 +0200 | [diff] [blame] | 11405 | /* all cookie headers and values were scanned. If we're looking for the |
| 11406 | * last occurrence, we may return it now. |
| 11407 | */ |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 11408 | out: |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 11409 | smp->flags &= ~SMP_F_NOT_LAST; |
Willy Tarreau | 28376d6 | 2012-04-26 21:26:10 +0200 | [diff] [blame] | 11410 | return found; |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 11411 | } |
| 11412 | |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 11413 | /* 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] | 11414 | * 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] | 11415 | * multiple cookies may be parsed on the same line. The returned sample is of |
| 11416 | * type UINT. Accepts exactly 1 argument of type string. |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 11417 | */ |
| 11418 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 11419 | 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] | 11420 | { |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 11421 | struct http_txn *txn; |
| 11422 | struct hdr_idx *idx; |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 11423 | struct hdr_ctx ctx; |
Willy Tarreau | e333ec9 | 2012-04-16 16:26:40 +0200 | [diff] [blame] | 11424 | const struct http_msg *msg; |
| 11425 | const char *hdr_name; |
| 11426 | int hdr_name_len; |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 11427 | int cnt; |
| 11428 | char *val_beg, *val_end; |
Willy Tarreau | e333ec9 | 2012-04-16 16:26:40 +0200 | [diff] [blame] | 11429 | char *sol; |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 11430 | |
Willy Tarreau | 24e32d8 | 2012-04-23 23:55:44 +0200 | [diff] [blame] | 11431 | if (!args || args->type != ARGT_STR) |
Willy Tarreau | 34db108 | 2012-04-19 17:16:54 +0200 | [diff] [blame] | 11432 | return 0; |
| 11433 | |
Willy Tarreau | e333ec9 | 2012-04-16 16:26:40 +0200 | [diff] [blame] | 11434 | CHECK_HTTP_MESSAGE_FIRST(); |
| 11435 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 11436 | txn = smp->strm->txn; |
| 11437 | idx = &smp->strm->txn->hdr_idx; |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 11438 | |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 11439 | if ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) { |
Willy Tarreau | e333ec9 | 2012-04-16 16:26:40 +0200 | [diff] [blame] | 11440 | msg = &txn->req; |
| 11441 | hdr_name = "Cookie"; |
| 11442 | hdr_name_len = 6; |
| 11443 | } else { |
| 11444 | msg = &txn->rsp; |
| 11445 | hdr_name = "Set-Cookie"; |
| 11446 | hdr_name_len = 10; |
| 11447 | } |
| 11448 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 11449 | sol = msg->chn->buf->p; |
Willy Tarreau | 46787ed | 2012-04-11 17:28:40 +0200 | [diff] [blame] | 11450 | val_end = val_beg = NULL; |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 11451 | ctx.idx = 0; |
| 11452 | cnt = 0; |
| 11453 | |
| 11454 | while (1) { |
| 11455 | /* Note: val_beg == NULL every time we need to fetch a new header */ |
| 11456 | if (!val_beg) { |
| 11457 | if (!http_find_header2(hdr_name, hdr_name_len, sol, idx, &ctx)) |
| 11458 | break; |
| 11459 | |
Willy Tarreau | 24e32d8 | 2012-04-23 23:55:44 +0200 | [diff] [blame] | 11460 | if (ctx.vlen < args->data.str.len + 1) |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 11461 | continue; |
| 11462 | |
| 11463 | val_beg = ctx.line + ctx.val; |
| 11464 | val_end = val_beg + ctx.vlen; |
| 11465 | } |
| 11466 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 11467 | smp->data.type = SMP_T_STR; |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 11468 | smp->flags |= SMP_F_CONST; |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 11469 | while ((val_beg = extract_cookie_value(val_beg, val_end, |
Willy Tarreau | 24e32d8 | 2012-04-23 23:55:44 +0200 | [diff] [blame] | 11470 | args->data.str.str, args->data.str.len, |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 11471 | (smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ, |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 11472 | &smp->data.u.str.str, |
| 11473 | &smp->data.u.str.len))) { |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 11474 | cnt++; |
| 11475 | } |
| 11476 | } |
| 11477 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 11478 | smp->data.type = SMP_T_SINT; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 11479 | smp->data.u.sint = cnt; |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 11480 | smp->flags |= SMP_F_VOL_HDR; |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 11481 | return 1; |
| 11482 | } |
| 11483 | |
Willy Tarreau | 5153936 | 2012-05-08 12:46:28 +0200 | [diff] [blame] | 11484 | /* Fetch an cookie's integer value. The integer value is returned. It |
| 11485 | * takes a mandatory argument of type string. It relies on smp_fetch_cookie(). |
| 11486 | */ |
| 11487 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 11488 | 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] | 11489 | { |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 11490 | int ret = smp_fetch_cookie(args, smp, kw, private); |
Willy Tarreau | 5153936 | 2012-05-08 12:46:28 +0200 | [diff] [blame] | 11491 | |
| 11492 | if (ret > 0) { |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 11493 | smp->data.type = SMP_T_SINT; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 11494 | smp->data.u.sint = strl2ic(smp->data.u.str.str, smp->data.u.str.len); |
Willy Tarreau | 5153936 | 2012-05-08 12:46:28 +0200 | [diff] [blame] | 11495 | } |
| 11496 | |
| 11497 | return ret; |
| 11498 | } |
| 11499 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 11500 | /************************************************************************/ |
Willy Tarreau | 1278578 | 2012-04-27 21:37:17 +0200 | [diff] [blame] | 11501 | /* The code below is dedicated to sample fetches */ |
Willy Tarreau | 4a56897 | 2010-05-12 08:08:50 +0200 | [diff] [blame] | 11502 | /************************************************************************/ |
| 11503 | |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 11504 | /* |
| 11505 | * Given a path string and its length, find the position of beginning of the |
| 11506 | * query string. Returns NULL if no query string is found in the path. |
| 11507 | * |
| 11508 | * Example: if path = "/foo/bar/fubar?yo=mama;ye=daddy", and n = 22: |
| 11509 | * |
Willy Tarreau | 1ede1da | 2015-05-07 16:06:18 +0200 | [diff] [blame] | 11510 | * find_query_string(path, n, '?') points to "yo=mama;ye=daddy" string. |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 11511 | */ |
bedis | 4c75cca | 2012-10-05 08:38:24 +0200 | [diff] [blame] | 11512 | 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] | 11513 | { |
| 11514 | char *p; |
Emeric Brun | 485479d | 2010-09-23 18:02:19 +0200 | [diff] [blame] | 11515 | |
bedis | 4c75cca | 2012-10-05 08:38:24 +0200 | [diff] [blame] | 11516 | p = memchr(path, delim, path_l); |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 11517 | return p ? p + 1 : NULL; |
| 11518 | } |
| 11519 | |
bedis | 4c75cca | 2012-10-05 08:38:24 +0200 | [diff] [blame] | 11520 | static inline int is_param_delimiter(char c, char delim) |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 11521 | { |
bedis | 4c75cca | 2012-10-05 08:38:24 +0200 | [diff] [blame] | 11522 | return c == '&' || c == ';' || c == delim; |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 11523 | } |
| 11524 | |
Thierry FOURNIER | 8be451c | 2015-05-20 15:28:12 +0200 | [diff] [blame] | 11525 | /* after increasing a pointer value, it can exceed the first buffer |
| 11526 | * size. This function transform the value of <ptr> according with |
| 11527 | * the expected position. <chunks> is an array of the one or two |
| 11528 | * avalaible chunks. The first value is the start of the first chunk, |
| 11529 | * the second value if the end+1 of the first chunks. The third value |
| 11530 | * is NULL or the start of the second chunk and the fourth value is |
| 11531 | * the end+1 of the second chunk. The function returns 1 if does a |
| 11532 | * wrap, else returns 0. |
| 11533 | */ |
| 11534 | static inline int fix_pointer_if_wrap(const char **chunks, const char **ptr) |
| 11535 | { |
| 11536 | if (*ptr < chunks[1]) |
| 11537 | return 0; |
| 11538 | if (!chunks[2]) |
| 11539 | return 0; |
| 11540 | *ptr = chunks[2] + ( *ptr - chunks[1] ); |
| 11541 | return 1; |
| 11542 | } |
| 11543 | |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 11544 | /* |
| 11545 | * Given a url parameter, find the starting position of the first occurence, |
| 11546 | * or NULL if the parameter is not found. |
| 11547 | * |
| 11548 | * Example: if query_string is "yo=mama;ye=daddy" and url_param_name is "ye", |
| 11549 | * the function will return query_string+8. |
Thierry FOURNIER | 8be451c | 2015-05-20 15:28:12 +0200 | [diff] [blame] | 11550 | * |
Willy Tarreau | f662582 | 2015-12-27 14:51:01 +0100 | [diff] [blame] | 11551 | * Warning: this function returns a pointer that can point to the first chunk |
Thierry FOURNIER | 8be451c | 2015-05-20 15:28:12 +0200 | [diff] [blame] | 11552 | * or the second chunk. The caller must be check the position before using the |
| 11553 | * result. |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 11554 | */ |
Thierry FOURNIER | 8be451c | 2015-05-20 15:28:12 +0200 | [diff] [blame] | 11555 | static const char * |
| 11556 | find_url_param_pos(const char **chunks, |
Willy Tarreau | 1ede1da | 2015-05-07 16:06:18 +0200 | [diff] [blame] | 11557 | const char* url_param_name, size_t url_param_name_l, |
bedis | 4c75cca | 2012-10-05 08:38:24 +0200 | [diff] [blame] | 11558 | char delim) |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 11559 | { |
Thierry FOURNIER | 8be451c | 2015-05-20 15:28:12 +0200 | [diff] [blame] | 11560 | const char *pos, *last, *equal; |
| 11561 | const char **bufs = chunks; |
| 11562 | int l1, l2; |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 11563 | |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 11564 | |
Thierry FOURNIER | 8be451c | 2015-05-20 15:28:12 +0200 | [diff] [blame] | 11565 | pos = bufs[0]; |
| 11566 | last = bufs[1]; |
Willy Tarreau | f662582 | 2015-12-27 14:51:01 +0100 | [diff] [blame] | 11567 | while (pos < last) { |
Thierry FOURNIER | 8be451c | 2015-05-20 15:28:12 +0200 | [diff] [blame] | 11568 | /* Check the equal. */ |
| 11569 | equal = pos + url_param_name_l; |
| 11570 | if (fix_pointer_if_wrap(chunks, &equal)) { |
| 11571 | if (equal >= chunks[3]) |
| 11572 | return NULL; |
| 11573 | } else { |
| 11574 | if (equal >= chunks[1]) |
| 11575 | return NULL; |
| 11576 | } |
| 11577 | if (*equal == '=') { |
| 11578 | if (pos + url_param_name_l > last) { |
| 11579 | /* process wrap case, we detect a wrap. In this case, the |
| 11580 | * comparison is performed in two parts. |
| 11581 | */ |
| 11582 | |
| 11583 | /* This is the end, we dont have any other chunk. */ |
| 11584 | if (bufs != chunks || !bufs[2]) |
| 11585 | return NULL; |
| 11586 | |
| 11587 | /* Compute the length of each part of the comparison. */ |
| 11588 | l1 = last - pos; |
| 11589 | l2 = url_param_name_l - l1; |
| 11590 | |
| 11591 | /* The second buffer is too short to contain the compared string. */ |
| 11592 | if (bufs[2] + l2 > bufs[3]) |
| 11593 | return NULL; |
| 11594 | |
| 11595 | if (memcmp(pos, url_param_name, l1) == 0 && |
| 11596 | memcmp(bufs[2], url_param_name+l1, l2) == 0) |
| 11597 | return pos; |
| 11598 | |
| 11599 | /* Perform wrapping and jump the string who fail the comparison. */ |
| 11600 | bufs += 2; |
| 11601 | pos = bufs[0] + l2; |
| 11602 | last = bufs[1]; |
| 11603 | |
| 11604 | } else { |
| 11605 | /* process a simple comparison. */ |
Willy Tarreau | f662582 | 2015-12-27 14:51:01 +0100 | [diff] [blame] | 11606 | if (memcmp(pos, url_param_name, url_param_name_l) == 0) |
| 11607 | return pos; |
Thierry FOURNIER | 8be451c | 2015-05-20 15:28:12 +0200 | [diff] [blame] | 11608 | pos += url_param_name_l + 1; |
| 11609 | if (fix_pointer_if_wrap(chunks, &pos)) |
| 11610 | last = bufs[2]; |
| 11611 | } |
| 11612 | } |
| 11613 | |
| 11614 | while (1) { |
| 11615 | /* Look for the next delimiter. */ |
Willy Tarreau | f662582 | 2015-12-27 14:51:01 +0100 | [diff] [blame] | 11616 | while (pos < last && !is_param_delimiter(*pos, delim)) |
Thierry FOURNIER | 8be451c | 2015-05-20 15:28:12 +0200 | [diff] [blame] | 11617 | pos++; |
| 11618 | if (pos < last) |
| 11619 | break; |
| 11620 | /* process buffer wrapping. */ |
| 11621 | if (bufs != chunks || !bufs[2]) |
| 11622 | return NULL; |
| 11623 | bufs += 2; |
| 11624 | pos = bufs[0]; |
| 11625 | last = bufs[1]; |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 11626 | } |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 11627 | pos++; |
| 11628 | } |
| 11629 | return NULL; |
| 11630 | } |
| 11631 | |
| 11632 | /* |
Cyril Bonté | ce1ef4d | 2015-11-26 21:39:56 +0100 | [diff] [blame] | 11633 | * Given a url parameter name and a query string, find the next value. |
| 11634 | * An empty url_param_name matches the first available parameter. |
| 11635 | * If the parameter is found, 1 is returned and *vstart / *vend are updated to |
| 11636 | * respectively provide a pointer to the value and its end. |
| 11637 | * Otherwise, 0 is returned and vstart/vend are not modified. |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 11638 | */ |
| 11639 | static int |
Thierry FOURNIER | 8be451c | 2015-05-20 15:28:12 +0200 | [diff] [blame] | 11640 | find_next_url_param(const char **chunks, |
Willy Tarreau | 1ede1da | 2015-05-07 16:06:18 +0200 | [diff] [blame] | 11641 | const char* url_param_name, size_t url_param_name_l, |
Thierry FOURNIER | 8be451c | 2015-05-20 15:28:12 +0200 | [diff] [blame] | 11642 | const char **vstart, const char **vend, char delim) |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 11643 | { |
Thierry FOURNIER | 8be451c | 2015-05-20 15:28:12 +0200 | [diff] [blame] | 11644 | const char *arg_start, *qs_end; |
| 11645 | const char *value_start, *value_end; |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 11646 | |
Thierry FOURNIER | 8be451c | 2015-05-20 15:28:12 +0200 | [diff] [blame] | 11647 | arg_start = chunks[0]; |
| 11648 | qs_end = chunks[1]; |
Willy Tarreau | 1ede1da | 2015-05-07 16:06:18 +0200 | [diff] [blame] | 11649 | if (url_param_name_l) { |
Thierry FOURNIER | 8be451c | 2015-05-20 15:28:12 +0200 | [diff] [blame] | 11650 | /* Looks for an argument name. */ |
| 11651 | arg_start = find_url_param_pos(chunks, |
Willy Tarreau | 1ede1da | 2015-05-07 16:06:18 +0200 | [diff] [blame] | 11652 | url_param_name, url_param_name_l, |
| 11653 | delim); |
Thierry FOURNIER | 8be451c | 2015-05-20 15:28:12 +0200 | [diff] [blame] | 11654 | /* Check for wrapping. */ |
Willy Tarreau | f662582 | 2015-12-27 14:51:01 +0100 | [diff] [blame] | 11655 | if (arg_start >= qs_end) |
Thierry FOURNIER | 8be451c | 2015-05-20 15:28:12 +0200 | [diff] [blame] | 11656 | qs_end = chunks[3]; |
Willy Tarreau | 1ede1da | 2015-05-07 16:06:18 +0200 | [diff] [blame] | 11657 | } |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 11658 | if (!arg_start) |
| 11659 | return 0; |
| 11660 | |
Willy Tarreau | 1ede1da | 2015-05-07 16:06:18 +0200 | [diff] [blame] | 11661 | if (!url_param_name_l) { |
Thierry FOURNIER | 8be451c | 2015-05-20 15:28:12 +0200 | [diff] [blame] | 11662 | while (1) { |
| 11663 | /* looks for the first argument. */ |
| 11664 | value_start = memchr(arg_start, '=', qs_end - arg_start); |
| 11665 | if (!value_start) { |
Thierry FOURNIER | 8be451c | 2015-05-20 15:28:12 +0200 | [diff] [blame] | 11666 | /* Check for wrapping. */ |
| 11667 | if (arg_start >= chunks[0] && |
Willy Tarreau | f662582 | 2015-12-27 14:51:01 +0100 | [diff] [blame] | 11668 | arg_start < chunks[1] && |
Thierry FOURNIER | 8be451c | 2015-05-20 15:28:12 +0200 | [diff] [blame] | 11669 | chunks[2]) { |
| 11670 | arg_start = chunks[2]; |
| 11671 | qs_end = chunks[3]; |
| 11672 | continue; |
| 11673 | } |
| 11674 | return 0; |
| 11675 | } |
| 11676 | break; |
| 11677 | } |
Willy Tarreau | 1ede1da | 2015-05-07 16:06:18 +0200 | [diff] [blame] | 11678 | value_start++; |
| 11679 | } |
Thierry FOURNIER | 8be451c | 2015-05-20 15:28:12 +0200 | [diff] [blame] | 11680 | else { |
| 11681 | /* Jump the argument length. */ |
Willy Tarreau | 1ede1da | 2015-05-07 16:06:18 +0200 | [diff] [blame] | 11682 | value_start = arg_start + url_param_name_l + 1; |
| 11683 | |
Thierry FOURNIER | 8be451c | 2015-05-20 15:28:12 +0200 | [diff] [blame] | 11684 | /* Check for pointer wrapping. */ |
| 11685 | if (fix_pointer_if_wrap(chunks, &value_start)) { |
| 11686 | /* Update the end pointer. */ |
| 11687 | qs_end = chunks[3]; |
| 11688 | |
| 11689 | /* Check for overflow. */ |
Willy Tarreau | f662582 | 2015-12-27 14:51:01 +0100 | [diff] [blame] | 11690 | if (value_start >= qs_end) |
Thierry FOURNIER | 8be451c | 2015-05-20 15:28:12 +0200 | [diff] [blame] | 11691 | return 0; |
| 11692 | } |
| 11693 | } |
| 11694 | |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 11695 | value_end = value_start; |
| 11696 | |
Thierry FOURNIER | 8be451c | 2015-05-20 15:28:12 +0200 | [diff] [blame] | 11697 | while (1) { |
| 11698 | while ((value_end < qs_end) && !is_param_delimiter(*value_end, delim)) |
| 11699 | value_end++; |
| 11700 | if (value_end < qs_end) |
| 11701 | break; |
| 11702 | /* process buffer wrapping. */ |
| 11703 | if (value_end >= chunks[0] && |
Willy Tarreau | f662582 | 2015-12-27 14:51:01 +0100 | [diff] [blame] | 11704 | value_end < chunks[1] && |
Thierry FOURNIER | 8be451c | 2015-05-20 15:28:12 +0200 | [diff] [blame] | 11705 | chunks[2]) { |
| 11706 | value_end = chunks[2]; |
| 11707 | qs_end = chunks[3]; |
| 11708 | continue; |
| 11709 | } |
| 11710 | break; |
| 11711 | } |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 11712 | |
Thierry FOURNIER | 8be451c | 2015-05-20 15:28:12 +0200 | [diff] [blame] | 11713 | *vstart = value_start; |
| 11714 | *vend = value_end; |
Cyril Bonté | ce1ef4d | 2015-11-26 21:39:56 +0100 | [diff] [blame] | 11715 | return 1; |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 11716 | } |
| 11717 | |
Thierry FOURNIER | 8be451c | 2015-05-20 15:28:12 +0200 | [diff] [blame] | 11718 | /* This scans a URL-encoded query string. It takes an optionally wrapping |
| 11719 | * string whose first contigous chunk has its beginning in ctx->a[0] and end |
| 11720 | * in ctx->a[1], and the optional second part in (ctx->a[2]..ctx->a[3]). The |
| 11721 | * pointers are updated for next iteration before leaving. |
Thierry FOURNIER | 4fdc74c | 2015-05-19 14:46:23 +0200 | [diff] [blame] | 11722 | */ |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 11723 | static int |
Thierry FOURNIER | 4fdc74c | 2015-05-19 14:46:23 +0200 | [diff] [blame] | 11724 | 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] | 11725 | { |
Thierry FOURNIER | 8be451c | 2015-05-20 15:28:12 +0200 | [diff] [blame] | 11726 | const char *vstart, *vend; |
| 11727 | struct chunk *temp; |
| 11728 | const char **chunks = (const char **)smp->ctx.a; |
bedis | 4c75cca | 2012-10-05 08:38:24 +0200 | [diff] [blame] | 11729 | |
Thierry FOURNIER | 8be451c | 2015-05-20 15:28:12 +0200 | [diff] [blame] | 11730 | if (!find_next_url_param(chunks, |
Thierry FOURNIER | 0948d41 | 2015-05-20 15:22:37 +0200 | [diff] [blame] | 11731 | name, name_len, |
Thierry FOURNIER | 8be451c | 2015-05-20 15:28:12 +0200 | [diff] [blame] | 11732 | &vstart, &vend, |
Thierry FOURNIER | 0948d41 | 2015-05-20 15:22:37 +0200 | [diff] [blame] | 11733 | delim)) |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 11734 | return 0; |
| 11735 | |
Thierry FOURNIER | 8be451c | 2015-05-20 15:28:12 +0200 | [diff] [blame] | 11736 | /* Create sample. If the value is contiguous, return the pointer as CONST, |
| 11737 | * if the value is wrapped, copy-it in a buffer. |
| 11738 | */ |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 11739 | smp->data.type = SMP_T_STR; |
Thierry FOURNIER | 8be451c | 2015-05-20 15:28:12 +0200 | [diff] [blame] | 11740 | if (chunks[2] && |
| 11741 | vstart >= chunks[0] && vstart <= chunks[1] && |
| 11742 | vend >= chunks[2] && vend <= chunks[3]) { |
| 11743 | /* Wrapped case. */ |
| 11744 | temp = get_trash_chunk(); |
| 11745 | memcpy(temp->str, vstart, chunks[1] - vstart); |
| 11746 | memcpy(temp->str + ( chunks[1] - vstart ), chunks[2], vend - chunks[2]); |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 11747 | smp->data.u.str.str = temp->str; |
| 11748 | smp->data.u.str.len = ( chunks[1] - vstart ) + ( vend - chunks[2] ); |
Thierry FOURNIER | 8be451c | 2015-05-20 15:28:12 +0200 | [diff] [blame] | 11749 | } else { |
| 11750 | /* Contiguous case. */ |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 11751 | smp->data.u.str.str = (char *)vstart; |
| 11752 | smp->data.u.str.len = vend - vstart; |
Thierry FOURNIER | 8be451c | 2015-05-20 15:28:12 +0200 | [diff] [blame] | 11753 | smp->flags = SMP_F_VOL_1ST | SMP_F_CONST; |
| 11754 | } |
| 11755 | |
| 11756 | /* Update context, check wrapping. */ |
| 11757 | chunks[0] = vend; |
| 11758 | if (chunks[2] && vend >= chunks[2] && vend <= chunks[3]) { |
| 11759 | chunks[1] = chunks[3]; |
| 11760 | chunks[2] = NULL; |
| 11761 | } |
Willy Tarreau | 1ede1da | 2015-05-07 16:06:18 +0200 | [diff] [blame] | 11762 | |
Thierry FOURNIER | 8be451c | 2015-05-20 15:28:12 +0200 | [diff] [blame] | 11763 | if (chunks[0] < chunks[1]) |
Willy Tarreau | 1ede1da | 2015-05-07 16:06:18 +0200 | [diff] [blame] | 11764 | smp->flags |= SMP_F_NOT_LAST; |
| 11765 | |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 11766 | return 1; |
| 11767 | } |
| 11768 | |
Thierry FOURNIER | 4fdc74c | 2015-05-19 14:46:23 +0200 | [diff] [blame] | 11769 | /* This function iterates over each parameter of the query string. It uses |
| 11770 | * 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] | 11771 | * parameter. Since it uses smp_fetch_param(), ctx->a[2..3] are both NULL. |
| 11772 | * An optional parameter name is passed in args[0], otherwise any parameter is |
| 11773 | * considered. It supports an optional delimiter argument for the beginning of |
| 11774 | * the string in args[1], which defaults to "?". |
Thierry FOURNIER | 4fdc74c | 2015-05-19 14:46:23 +0200 | [diff] [blame] | 11775 | */ |
| 11776 | static int |
| 11777 | smp_fetch_url_param(const struct arg *args, struct sample *smp, const char *kw, void *private) |
| 11778 | { |
| 11779 | struct http_msg *msg; |
| 11780 | char delim = '?'; |
| 11781 | const char *name; |
| 11782 | int name_len; |
| 11783 | |
Dragan Dosen | 26f77e5 | 2015-05-25 10:02:11 +0200 | [diff] [blame] | 11784 | if (!args || |
| 11785 | (args[0].type && args[0].type != ARGT_STR) || |
| 11786 | (args[1].type && args[1].type != ARGT_STR)) |
| 11787 | return 0; |
Thierry FOURNIER | 4fdc74c | 2015-05-19 14:46:23 +0200 | [diff] [blame] | 11788 | |
Dragan Dosen | 26f77e5 | 2015-05-25 10:02:11 +0200 | [diff] [blame] | 11789 | name = ""; |
| 11790 | name_len = 0; |
| 11791 | if (args->type == ARGT_STR) { |
| 11792 | name = args->data.str.str; |
| 11793 | name_len = args->data.str.len; |
| 11794 | } |
Thierry FOURNIER | 4fdc74c | 2015-05-19 14:46:23 +0200 | [diff] [blame] | 11795 | |
Dragan Dosen | 26f77e5 | 2015-05-25 10:02:11 +0200 | [diff] [blame] | 11796 | if (args[1].type) |
| 11797 | delim = *args[1].data.str.str; |
Thierry FOURNIER | 4fdc74c | 2015-05-19 14:46:23 +0200 | [diff] [blame] | 11798 | |
Dragan Dosen | 26f77e5 | 2015-05-25 10:02:11 +0200 | [diff] [blame] | 11799 | if (!smp->ctx.a[0]) { // first call, find the query string |
Thierry FOURNIER | 4fdc74c | 2015-05-19 14:46:23 +0200 | [diff] [blame] | 11800 | CHECK_HTTP_MESSAGE_FIRST(); |
| 11801 | |
| 11802 | msg = &smp->strm->txn->req; |
| 11803 | |
| 11804 | smp->ctx.a[0] = find_param_list(msg->chn->buf->p + msg->sl.rq.u, |
| 11805 | msg->sl.rq.u_l, delim); |
| 11806 | if (!smp->ctx.a[0]) |
| 11807 | return 0; |
| 11808 | |
| 11809 | 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] | 11810 | |
| 11811 | /* Assume that the context is filled with NULL pointer |
| 11812 | * before the first call. |
| 11813 | * smp->ctx.a[2] = NULL; |
| 11814 | * smp->ctx.a[3] = NULL; |
| 11815 | */ |
Thierry FOURNIER | 4fdc74c | 2015-05-19 14:46:23 +0200 | [diff] [blame] | 11816 | } |
| 11817 | |
| 11818 | return smp_fetch_param(delim, name, name_len, args, smp, kw, private); |
| 11819 | } |
| 11820 | |
Thierry FOURNIER | e28c499 | 2015-05-19 14:45:09 +0200 | [diff] [blame] | 11821 | /* This function iterates over each parameter of the body. This requires |
| 11822 | * 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] | 11823 | * ctx->a[0] and ctx->a[1] to store the beginning and end of the first |
| 11824 | * contigous part of the body, and optionally ctx->a[2..3] to reference the |
| 11825 | * optional second part if the body wraps at the end of the buffer. An optional |
| 11826 | * parameter name is passed in args[0], otherwise any parameter is considered. |
Thierry FOURNIER | e28c499 | 2015-05-19 14:45:09 +0200 | [diff] [blame] | 11827 | */ |
| 11828 | static int |
| 11829 | smp_fetch_body_param(const struct arg *args, struct sample *smp, const char *kw, void *private) |
| 11830 | { |
Thierry FOURNIER | e28c499 | 2015-05-19 14:45:09 +0200 | [diff] [blame] | 11831 | struct http_msg *msg; |
| 11832 | unsigned long len; |
| 11833 | unsigned long block1; |
| 11834 | char *body; |
| 11835 | const char *name; |
| 11836 | int name_len; |
| 11837 | |
| 11838 | if (!args || (args[0].type && args[0].type != ARGT_STR)) |
| 11839 | return 0; |
| 11840 | |
| 11841 | name = ""; |
| 11842 | name_len = 0; |
| 11843 | if (args[0].type == ARGT_STR) { |
| 11844 | name = args[0].data.str.str; |
| 11845 | name_len = args[0].data.str.len; |
| 11846 | } |
| 11847 | |
| 11848 | if (!smp->ctx.a[0]) { // first call, find the query string |
| 11849 | CHECK_HTTP_MESSAGE_FIRST(); |
| 11850 | |
| 11851 | if ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) |
Willy Tarreau | be508f1 | 2016-03-10 11:47:01 +0100 | [diff] [blame] | 11852 | msg = &smp->strm->txn->req; |
Thierry FOURNIER | e28c499 | 2015-05-19 14:45:09 +0200 | [diff] [blame] | 11853 | else |
Willy Tarreau | be508f1 | 2016-03-10 11:47:01 +0100 | [diff] [blame] | 11854 | msg = &smp->strm->txn->rsp; |
Thierry FOURNIER | e28c499 | 2015-05-19 14:45:09 +0200 | [diff] [blame] | 11855 | |
| 11856 | len = http_body_bytes(msg); |
| 11857 | body = b_ptr(msg->chn->buf, -http_data_rewind(msg)); |
| 11858 | |
| 11859 | block1 = len; |
| 11860 | if (block1 > msg->chn->buf->data + msg->chn->buf->size - body) |
| 11861 | block1 = msg->chn->buf->data + msg->chn->buf->size - body; |
| 11862 | |
| 11863 | if (block1 == len) { |
| 11864 | /* buffer is not wrapped (or empty) */ |
| 11865 | smp->ctx.a[0] = body; |
| 11866 | smp->ctx.a[1] = body + len; |
Thierry FOURNIER | 8be451c | 2015-05-20 15:28:12 +0200 | [diff] [blame] | 11867 | |
| 11868 | /* Assume that the context is filled with NULL pointer |
| 11869 | * before the first call. |
| 11870 | * smp->ctx.a[2] = NULL; |
| 11871 | * smp->ctx.a[3] = NULL; |
| 11872 | */ |
Thierry FOURNIER | e28c499 | 2015-05-19 14:45:09 +0200 | [diff] [blame] | 11873 | } |
| 11874 | else { |
| 11875 | /* buffer is wrapped, we need to defragment it */ |
| 11876 | smp->ctx.a[0] = body; |
| 11877 | smp->ctx.a[1] = body + block1; |
Thierry FOURNIER | 8be451c | 2015-05-20 15:28:12 +0200 | [diff] [blame] | 11878 | smp->ctx.a[2] = msg->chn->buf->data; |
| 11879 | smp->ctx.a[3] = msg->chn->buf->data + ( len - block1 ); |
Thierry FOURNIER | e28c499 | 2015-05-19 14:45:09 +0200 | [diff] [blame] | 11880 | } |
| 11881 | } |
| 11882 | return smp_fetch_param('&', name, name_len, args, smp, kw, private); |
| 11883 | } |
| 11884 | |
Willy Tarreau | a9fddca | 2012-07-31 07:51:48 +0200 | [diff] [blame] | 11885 | /* Return the signed integer value for the specified url parameter (see url_param |
| 11886 | * above). |
| 11887 | */ |
| 11888 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 11889 | 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] | 11890 | { |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 11891 | int ret = smp_fetch_url_param(args, smp, kw, private); |
Willy Tarreau | a9fddca | 2012-07-31 07:51:48 +0200 | [diff] [blame] | 11892 | |
| 11893 | if (ret > 0) { |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 11894 | smp->data.type = SMP_T_SINT; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 11895 | smp->data.u.sint = strl2ic(smp->data.u.str.str, smp->data.u.str.len); |
Willy Tarreau | a9fddca | 2012-07-31 07:51:48 +0200 | [diff] [blame] | 11896 | } |
| 11897 | |
| 11898 | return ret; |
| 11899 | } |
| 11900 | |
Neil - HAProxy List | 39c63c5 | 2013-11-04 13:48:42 +0000 | [diff] [blame] | 11901 | /* This produces a 32-bit hash of the concatenation of the first occurrence of |
| 11902 | * the Host header followed by the path component if it begins with a slash ('/'). |
| 11903 | * This means that '*' will not be added, resulting in exactly the first Host |
| 11904 | * entry. If no Host header is found, then the path is used. The resulting value |
| 11905 | * is hashed using the url hash followed by a full avalanche hash and provides a |
| 11906 | * 32-bit integer value. This fetch is useful for tracking per-URL activity on |
| 11907 | * high-traffic sites without having to store whole paths. |
| 11908 | * this differs from the base32 functions in that it includes the url parameters |
| 11909 | * as well as the path |
| 11910 | */ |
| 11911 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 11912 | 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] | 11913 | { |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 11914 | struct http_txn *txn; |
Neil - HAProxy List | 39c63c5 | 2013-11-04 13:48:42 +0000 | [diff] [blame] | 11915 | struct hdr_ctx ctx; |
| 11916 | unsigned int hash = 0; |
| 11917 | char *ptr, *beg, *end; |
| 11918 | int len; |
| 11919 | |
| 11920 | CHECK_HTTP_MESSAGE_FIRST(); |
| 11921 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 11922 | txn = smp->strm->txn; |
Neil - HAProxy List | 39c63c5 | 2013-11-04 13:48:42 +0000 | [diff] [blame] | 11923 | ctx.idx = 0; |
Willy Tarreau | 877e78d | 2013-04-07 18:48:08 +0200 | [diff] [blame] | 11924 | 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] | 11925 | /* OK we have the header value in ctx.line+ctx.val for ctx.vlen bytes */ |
| 11926 | ptr = ctx.line + ctx.val; |
| 11927 | len = ctx.vlen; |
| 11928 | while (len--) |
| 11929 | hash = *(ptr++) + (hash << 6) + (hash << 16) - hash; |
| 11930 | } |
| 11931 | |
| 11932 | /* now retrieve the path */ |
Willy Tarreau | 877e78d | 2013-04-07 18:48:08 +0200 | [diff] [blame] | 11933 | 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] | 11934 | beg = http_get_path(txn); |
| 11935 | if (!beg) |
| 11936 | beg = end; |
| 11937 | |
| 11938 | for (ptr = beg; ptr < end ; ptr++); |
| 11939 | |
| 11940 | if (beg < ptr && *beg == '/') { |
| 11941 | while (beg < ptr) |
| 11942 | hash = *(beg++) + (hash << 6) + (hash << 16) - hash; |
| 11943 | } |
| 11944 | hash = full_hash(hash); |
| 11945 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 11946 | smp->data.type = SMP_T_SINT; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 11947 | smp->data.u.sint = hash; |
Neil - HAProxy List | 39c63c5 | 2013-11-04 13:48:42 +0000 | [diff] [blame] | 11948 | smp->flags = SMP_F_VOL_1ST; |
| 11949 | return 1; |
| 11950 | } |
| 11951 | |
| 11952 | /* This concatenates the source address with the 32-bit hash of the Host and |
| 11953 | * URL as returned by smp_fetch_base32(). The idea is to have per-source and |
| 11954 | * per-url counters. The result is a binary block from 8 to 20 bytes depending |
| 11955 | * on the source address length. The URL hash is stored before the address so |
| 11956 | * that in environments where IPv6 is insignificant, truncating the output to |
| 11957 | * 8 bytes would still work. |
| 11958 | */ |
| 11959 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 11960 | 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] | 11961 | { |
| 11962 | struct chunk *temp; |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 11963 | struct connection *cli_conn = objt_conn(smp->sess->origin); |
Neil - HAProxy List | 39c63c5 | 2013-11-04 13:48:42 +0000 | [diff] [blame] | 11964 | |
Dragan Dosen | db5af61 | 2016-06-16 11:23:01 +0200 | [diff] [blame] | 11965 | if (!cli_conn) |
| 11966 | return 0; |
| 11967 | |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 11968 | if (!smp_fetch_url32(args, smp, kw, private)) |
Neil - HAProxy List | 39c63c5 | 2013-11-04 13:48:42 +0000 | [diff] [blame] | 11969 | return 0; |
| 11970 | |
| 11971 | temp = get_trash_chunk(); |
Dragan Dosen | e5f4133 | 2016-06-16 11:08:08 +0200 | [diff] [blame] | 11972 | *(unsigned int *)temp->str = htonl(smp->data.u.sint); |
| 11973 | temp->len += sizeof(unsigned int); |
Neil - HAProxy List | 39c63c5 | 2013-11-04 13:48:42 +0000 | [diff] [blame] | 11974 | |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 11975 | switch (cli_conn->addr.from.ss_family) { |
Neil - HAProxy List | 39c63c5 | 2013-11-04 13:48:42 +0000 | [diff] [blame] | 11976 | case AF_INET: |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 11977 | 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] | 11978 | temp->len += 4; |
| 11979 | break; |
| 11980 | case AF_INET6: |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 11981 | 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] | 11982 | temp->len += 16; |
| 11983 | break; |
| 11984 | default: |
| 11985 | return 0; |
| 11986 | } |
| 11987 | |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 11988 | smp->data.u.str = *temp; |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 11989 | smp->data.type = SMP_T_BIN; |
Neil - HAProxy List | 39c63c5 | 2013-11-04 13:48:42 +0000 | [diff] [blame] | 11990 | return 1; |
| 11991 | } |
| 11992 | |
Willy Tarreau | 185b5c4 | 2012-04-26 15:11:51 +0200 | [diff] [blame] | 11993 | /* This function is used to validate the arguments passed to any "hdr" fetch |
| 11994 | * keyword. These keywords support an optional positive or negative occurrence |
| 11995 | * number. We must ensure that the number is greater than -MAX_HDR_HISTORY. It |
| 11996 | * is assumed that the types are already the correct ones. Returns 0 on error, |
| 11997 | * non-zero if OK. If <err> is not NULL, it will be filled with a pointer to an |
| 11998 | * error message in case of error, that the caller is responsible for freeing. |
| 11999 | * The initial location must either be freeable or NULL. |
| 12000 | */ |
Thierry FOURNIER | 49f45af | 2014-12-08 19:50:43 +0100 | [diff] [blame] | 12001 | int val_hdr(struct arg *arg, char **err_msg) |
Willy Tarreau | 185b5c4 | 2012-04-26 15:11:51 +0200 | [diff] [blame] | 12002 | { |
| 12003 | 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] | 12004 | memprintf(err_msg, "header occurrence must be >= %d", -MAX_HDR_HISTORY); |
Willy Tarreau | 185b5c4 | 2012-04-26 15:11:51 +0200 | [diff] [blame] | 12005 | return 0; |
| 12006 | } |
| 12007 | return 1; |
| 12008 | } |
| 12009 | |
Willy Tarreau | 276fae9 | 2013-07-25 14:36:01 +0200 | [diff] [blame] | 12010 | /* takes an UINT value on input supposed to represent the time since EPOCH, |
| 12011 | * adds an optional offset found in args[0] and emits a string representing |
| 12012 | * the date in RFC-1123/5322 format. |
| 12013 | */ |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 12014 | 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] | 12015 | { |
Cyril Bonté | f78d896 | 2016-01-22 19:40:28 +0100 | [diff] [blame] | 12016 | const char day[7][4] = { "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" }; |
Willy Tarreau | 276fae9 | 2013-07-25 14:36:01 +0200 | [diff] [blame] | 12017 | const char mon[12][4] = { "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" }; |
| 12018 | struct chunk *temp; |
| 12019 | struct tm *tm; |
Thierry FOURNIER | bf65cd4 | 2015-07-20 17:45:02 +0200 | [diff] [blame] | 12020 | /* With high numbers, the date returned can be negative, the 55 bits mask prevent this. */ |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 12021 | time_t curr_date = smp->data.u.sint & 0x007fffffffffffffLL; |
Willy Tarreau | 276fae9 | 2013-07-25 14:36:01 +0200 | [diff] [blame] | 12022 | |
| 12023 | /* add offset */ |
Thierry FOURNIER | bf65cd4 | 2015-07-20 17:45:02 +0200 | [diff] [blame] | 12024 | if (args && (args[0].type == ARGT_SINT)) |
Willy Tarreau | 276fae9 | 2013-07-25 14:36:01 +0200 | [diff] [blame] | 12025 | curr_date += args[0].data.sint; |
| 12026 | |
| 12027 | tm = gmtime(&curr_date); |
Thierry FOURNIER | fac9ccf | 2015-07-08 00:15:20 +0200 | [diff] [blame] | 12028 | if (!tm) |
| 12029 | return 0; |
Willy Tarreau | 276fae9 | 2013-07-25 14:36:01 +0200 | [diff] [blame] | 12030 | |
| 12031 | temp = get_trash_chunk(); |
| 12032 | temp->len = snprintf(temp->str, temp->size - temp->len, |
| 12033 | "%s, %02d %s %04d %02d:%02d:%02d GMT", |
| 12034 | day[tm->tm_wday], tm->tm_mday, mon[tm->tm_mon], 1900+tm->tm_year, |
| 12035 | tm->tm_hour, tm->tm_min, tm->tm_sec); |
| 12036 | |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 12037 | smp->data.u.str = *temp; |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 12038 | smp->data.type = SMP_T_STR; |
Willy Tarreau | 276fae9 | 2013-07-25 14:36:01 +0200 | [diff] [blame] | 12039 | return 1; |
| 12040 | } |
| 12041 | |
Thierry FOURNIER | ad90351 | 2014-04-11 17:51:01 +0200 | [diff] [blame] | 12042 | /* Match language range with language tag. RFC2616 14.4: |
| 12043 | * |
| 12044 | * A language-range matches a language-tag if it exactly equals |
| 12045 | * the tag, or if it exactly equals a prefix of the tag such |
| 12046 | * that the first tag character following the prefix is "-". |
| 12047 | * |
| 12048 | * Return 1 if the strings match, else return 0. |
| 12049 | */ |
| 12050 | static inline int language_range_match(const char *range, int range_len, |
| 12051 | const char *tag, int tag_len) |
| 12052 | { |
| 12053 | const char *end = range + range_len; |
| 12054 | const char *tend = tag + tag_len; |
| 12055 | while (range < end) { |
| 12056 | if (*range == '-' && tag == tend) |
| 12057 | return 1; |
| 12058 | if (*range != *tag || tag == tend) |
| 12059 | return 0; |
| 12060 | range++; |
| 12061 | tag++; |
| 12062 | } |
| 12063 | /* Return true only if the last char of the tag is matched. */ |
| 12064 | return tag == tend; |
| 12065 | } |
| 12066 | |
| 12067 | /* 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] | 12068 | 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] | 12069 | { |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 12070 | const char *al = smp->data.u.str.str; |
| 12071 | const char *end = al + smp->data.u.str.len; |
Thierry FOURNIER | ad90351 | 2014-04-11 17:51:01 +0200 | [diff] [blame] | 12072 | const char *token; |
| 12073 | int toklen; |
| 12074 | int qvalue; |
| 12075 | const char *str; |
| 12076 | const char *w; |
| 12077 | int best_q = 0; |
| 12078 | |
| 12079 | /* Set the constant to the sample, because the output of the |
| 12080 | * function will be peek in the constant configuration string. |
| 12081 | */ |
| 12082 | smp->flags |= SMP_F_CONST; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 12083 | smp->data.u.str.size = 0; |
| 12084 | smp->data.u.str.str = ""; |
| 12085 | smp->data.u.str.len = 0; |
Thierry FOURNIER | ad90351 | 2014-04-11 17:51:01 +0200 | [diff] [blame] | 12086 | |
| 12087 | /* Parse the accept language */ |
| 12088 | while (1) { |
| 12089 | |
| 12090 | /* Jump spaces, quit if the end is detected. */ |
Willy Tarreau | 506c69a | 2014-07-08 00:59:48 +0200 | [diff] [blame] | 12091 | while (al < end && isspace((unsigned char)*al)) |
Thierry FOURNIER | ad90351 | 2014-04-11 17:51:01 +0200 | [diff] [blame] | 12092 | al++; |
| 12093 | if (al >= end) |
| 12094 | break; |
| 12095 | |
| 12096 | /* Start of the fisrt word. */ |
| 12097 | token = al; |
| 12098 | |
| 12099 | /* Look for separator: isspace(), ',' or ';'. Next value if 0 length word. */ |
Willy Tarreau | 506c69a | 2014-07-08 00:59:48 +0200 | [diff] [blame] | 12100 | while (al < end && *al != ';' && *al != ',' && !isspace((unsigned char)*al)) |
Thierry FOURNIER | ad90351 | 2014-04-11 17:51:01 +0200 | [diff] [blame] | 12101 | al++; |
| 12102 | if (al == token) |
| 12103 | goto expect_comma; |
| 12104 | |
| 12105 | /* Length of the token. */ |
| 12106 | toklen = al - token; |
| 12107 | qvalue = 1000; |
| 12108 | |
| 12109 | /* Check if the token exists in the list. If the token not exists, |
| 12110 | * jump to the next token. |
| 12111 | */ |
| 12112 | str = args[0].data.str.str; |
| 12113 | w = str; |
| 12114 | while (1) { |
| 12115 | if (*str == ';' || *str == '\0') { |
| 12116 | if (language_range_match(token, toklen, w, str-w)) |
| 12117 | goto look_for_q; |
| 12118 | if (*str == '\0') |
| 12119 | goto expect_comma; |
| 12120 | w = str + 1; |
| 12121 | } |
| 12122 | str++; |
| 12123 | } |
| 12124 | goto expect_comma; |
| 12125 | |
| 12126 | look_for_q: |
| 12127 | |
| 12128 | /* Jump spaces, quit if the end is detected. */ |
Willy Tarreau | 506c69a | 2014-07-08 00:59:48 +0200 | [diff] [blame] | 12129 | while (al < end && isspace((unsigned char)*al)) |
Thierry FOURNIER | ad90351 | 2014-04-11 17:51:01 +0200 | [diff] [blame] | 12130 | al++; |
| 12131 | if (al >= end) |
| 12132 | goto process_value; |
| 12133 | |
| 12134 | /* If ',' is found, process the result */ |
| 12135 | if (*al == ',') |
| 12136 | goto process_value; |
| 12137 | |
| 12138 | /* If the character is different from ';', look |
| 12139 | * for the end of the header part in best effort. |
| 12140 | */ |
| 12141 | if (*al != ';') |
| 12142 | goto expect_comma; |
| 12143 | |
| 12144 | /* Assumes that the char is ';', now expect "q=". */ |
| 12145 | al++; |
| 12146 | |
| 12147 | /* Jump spaces, process value if the end is detected. */ |
Willy Tarreau | 506c69a | 2014-07-08 00:59:48 +0200 | [diff] [blame] | 12148 | while (al < end && isspace((unsigned char)*al)) |
Thierry FOURNIER | ad90351 | 2014-04-11 17:51:01 +0200 | [diff] [blame] | 12149 | al++; |
| 12150 | if (al >= end) |
| 12151 | goto process_value; |
| 12152 | |
| 12153 | /* Expect 'q'. If no 'q', continue in best effort */ |
| 12154 | if (*al != 'q') |
| 12155 | goto process_value; |
| 12156 | al++; |
| 12157 | |
| 12158 | /* Jump spaces, process value if the end is detected. */ |
Willy Tarreau | 506c69a | 2014-07-08 00:59:48 +0200 | [diff] [blame] | 12159 | while (al < end && isspace((unsigned char)*al)) |
Thierry FOURNIER | ad90351 | 2014-04-11 17:51:01 +0200 | [diff] [blame] | 12160 | al++; |
| 12161 | if (al >= end) |
| 12162 | goto process_value; |
| 12163 | |
| 12164 | /* Expect '='. If no '=', continue in best effort */ |
| 12165 | if (*al != '=') |
| 12166 | goto process_value; |
| 12167 | al++; |
| 12168 | |
| 12169 | /* Jump spaces, process value if the end is detected. */ |
Willy Tarreau | 506c69a | 2014-07-08 00:59:48 +0200 | [diff] [blame] | 12170 | while (al < end && isspace((unsigned char)*al)) |
Thierry FOURNIER | ad90351 | 2014-04-11 17:51:01 +0200 | [diff] [blame] | 12171 | al++; |
| 12172 | if (al >= end) |
| 12173 | goto process_value; |
| 12174 | |
| 12175 | /* Parse the q value. */ |
| 12176 | qvalue = parse_qvalue(al, &al); |
| 12177 | |
| 12178 | process_value: |
| 12179 | |
| 12180 | /* If the new q value is the best q value, then store the associated |
| 12181 | * language in the response. If qvalue is the biggest value (1000), |
| 12182 | * break the process. |
| 12183 | */ |
| 12184 | if (qvalue > best_q) { |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 12185 | smp->data.u.str.str = (char *)w; |
| 12186 | smp->data.u.str.len = str - w; |
Thierry FOURNIER | ad90351 | 2014-04-11 17:51:01 +0200 | [diff] [blame] | 12187 | if (qvalue >= 1000) |
| 12188 | break; |
| 12189 | best_q = qvalue; |
| 12190 | } |
| 12191 | |
| 12192 | expect_comma: |
| 12193 | |
| 12194 | /* Expect comma or end. If the end is detected, quit the loop. */ |
| 12195 | while (al < end && *al != ',') |
| 12196 | al++; |
| 12197 | if (al >= end) |
| 12198 | break; |
| 12199 | |
| 12200 | /* Comma is found, jump it and restart the analyzer. */ |
| 12201 | al++; |
| 12202 | } |
| 12203 | |
| 12204 | /* Set default value if required. */ |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 12205 | if (smp->data.u.str.len == 0 && args[1].type == ARGT_STR) { |
| 12206 | smp->data.u.str.str = args[1].data.str.str; |
| 12207 | smp->data.u.str.len = args[1].data.str.len; |
Thierry FOURNIER | ad90351 | 2014-04-11 17:51:01 +0200 | [diff] [blame] | 12208 | } |
| 12209 | |
| 12210 | /* Return true only if a matching language was found. */ |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 12211 | return smp->data.u.str.len != 0; |
Thierry FOURNIER | ad90351 | 2014-04-11 17:51:01 +0200 | [diff] [blame] | 12212 | } |
| 12213 | |
Thierry FOURNIER | 82ff3c9 | 2015-05-07 15:46:20 +0200 | [diff] [blame] | 12214 | /* This fetch url-decode any input string. */ |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 12215 | 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] | 12216 | { |
| 12217 | /* If the constant flag is set or if not size is avalaible at |
| 12218 | * the end of the buffer, copy the string in other buffer |
| 12219 | * before decoding. |
| 12220 | */ |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 12221 | if (smp->flags & SMP_F_CONST || smp->data.u.str.size <= smp->data.u.str.len) { |
Thierry FOURNIER | 82ff3c9 | 2015-05-07 15:46:20 +0200 | [diff] [blame] | 12222 | struct chunk *str = get_trash_chunk(); |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 12223 | memcpy(str->str, smp->data.u.str.str, smp->data.u.str.len); |
| 12224 | smp->data.u.str.str = str->str; |
| 12225 | smp->data.u.str.size = str->size; |
Thierry FOURNIER | 82ff3c9 | 2015-05-07 15:46:20 +0200 | [diff] [blame] | 12226 | smp->flags &= ~SMP_F_CONST; |
| 12227 | } |
| 12228 | |
| 12229 | /* Add final \0 required by url_decode(), and convert the input string. */ |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 12230 | smp->data.u.str.str[smp->data.u.str.len] = '\0'; |
| 12231 | smp->data.u.str.len = url_decode(smp->data.u.str.str); |
Thierry FOURNIER | 82ff3c9 | 2015-05-07 15:46:20 +0200 | [diff] [blame] | 12232 | return 1; |
| 12233 | } |
| 12234 | |
Thierry FOURNIER | 35ab275 | 2015-05-28 13:22:03 +0200 | [diff] [blame] | 12235 | static int smp_conv_req_capture(const struct arg *args, struct sample *smp, void *private) |
| 12236 | { |
| 12237 | struct proxy *fe = strm_fe(smp->strm); |
| 12238 | int idx, i; |
| 12239 | struct cap_hdr *hdr; |
| 12240 | int len; |
| 12241 | |
Thierry FOURNIER | bf65cd4 | 2015-07-20 17:45:02 +0200 | [diff] [blame] | 12242 | if (!args || args->type != ARGT_SINT) |
Thierry FOURNIER | 35ab275 | 2015-05-28 13:22:03 +0200 | [diff] [blame] | 12243 | return 0; |
| 12244 | |
Thierry FOURNIER | bf65cd4 | 2015-07-20 17:45:02 +0200 | [diff] [blame] | 12245 | idx = args->data.sint; |
Thierry FOURNIER | 35ab275 | 2015-05-28 13:22:03 +0200 | [diff] [blame] | 12246 | |
| 12247 | /* Check the availibity of the capture id. */ |
| 12248 | if (idx > fe->nb_req_cap - 1) |
| 12249 | return 0; |
| 12250 | |
| 12251 | /* Look for the original configuration. */ |
| 12252 | for (hdr = fe->req_cap, i = fe->nb_req_cap - 1; |
| 12253 | hdr != NULL && i != idx ; |
| 12254 | i--, hdr = hdr->next); |
| 12255 | if (!hdr) |
| 12256 | return 0; |
| 12257 | |
| 12258 | /* check for the memory allocation */ |
| 12259 | if (smp->strm->req_cap[hdr->index] == NULL) |
| 12260 | smp->strm->req_cap[hdr->index] = pool_alloc2(hdr->pool); |
| 12261 | if (smp->strm->req_cap[hdr->index] == NULL) |
| 12262 | return 0; |
| 12263 | |
| 12264 | /* Check length. */ |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 12265 | len = smp->data.u.str.len; |
Thierry FOURNIER | 35ab275 | 2015-05-28 13:22:03 +0200 | [diff] [blame] | 12266 | if (len > hdr->len) |
| 12267 | len = hdr->len; |
| 12268 | |
| 12269 | /* Capture input data. */ |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 12270 | memcpy(smp->strm->req_cap[idx], smp->data.u.str.str, len); |
Thierry FOURNIER | 35ab275 | 2015-05-28 13:22:03 +0200 | [diff] [blame] | 12271 | smp->strm->req_cap[idx][len] = '\0'; |
| 12272 | |
| 12273 | return 1; |
| 12274 | } |
| 12275 | |
| 12276 | static int smp_conv_res_capture(const struct arg *args, struct sample *smp, void *private) |
| 12277 | { |
| 12278 | struct proxy *fe = strm_fe(smp->strm); |
| 12279 | int idx, i; |
| 12280 | struct cap_hdr *hdr; |
| 12281 | int len; |
| 12282 | |
Thierry FOURNIER | bf65cd4 | 2015-07-20 17:45:02 +0200 | [diff] [blame] | 12283 | if (!args || args->type != ARGT_SINT) |
Thierry FOURNIER | 35ab275 | 2015-05-28 13:22:03 +0200 | [diff] [blame] | 12284 | return 0; |
| 12285 | |
Thierry FOURNIER | bf65cd4 | 2015-07-20 17:45:02 +0200 | [diff] [blame] | 12286 | idx = args->data.sint; |
Thierry FOURNIER | 35ab275 | 2015-05-28 13:22:03 +0200 | [diff] [blame] | 12287 | |
| 12288 | /* Check the availibity of the capture id. */ |
| 12289 | if (idx > fe->nb_rsp_cap - 1) |
| 12290 | return 0; |
| 12291 | |
| 12292 | /* Look for the original configuration. */ |
| 12293 | for (hdr = fe->rsp_cap, i = fe->nb_rsp_cap - 1; |
| 12294 | hdr != NULL && i != idx ; |
| 12295 | i--, hdr = hdr->next); |
| 12296 | if (!hdr) |
| 12297 | return 0; |
| 12298 | |
| 12299 | /* check for the memory allocation */ |
| 12300 | if (smp->strm->res_cap[hdr->index] == NULL) |
| 12301 | smp->strm->res_cap[hdr->index] = pool_alloc2(hdr->pool); |
| 12302 | if (smp->strm->res_cap[hdr->index] == NULL) |
| 12303 | return 0; |
| 12304 | |
| 12305 | /* Check length. */ |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 12306 | len = smp->data.u.str.len; |
Thierry FOURNIER | 35ab275 | 2015-05-28 13:22:03 +0200 | [diff] [blame] | 12307 | if (len > hdr->len) |
| 12308 | len = hdr->len; |
| 12309 | |
| 12310 | /* Capture input data. */ |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 12311 | memcpy(smp->strm->res_cap[idx], smp->data.u.str.str, len); |
Thierry FOURNIER | 35ab275 | 2015-05-28 13:22:03 +0200 | [diff] [blame] | 12312 | smp->strm->res_cap[idx][len] = '\0'; |
| 12313 | |
| 12314 | return 1; |
| 12315 | } |
| 12316 | |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 12317 | /* 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] | 12318 | * takes the string from the variable 'replace' with length 'len', then modifies |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 12319 | * the relevant part of the request line accordingly. Then it updates various |
| 12320 | * pointers to the next elements which were moved, and the total buffer length. |
| 12321 | * 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] | 12322 | * parse_set_req_line(). It returns 0 in case of success, -1 in case of internal |
| 12323 | * error, though this can be revisited when this code is finally exploited. |
| 12324 | * |
| 12325 | * 'action' can be '0' to replace method, '1' to replace path, '2' to replace |
| 12326 | * query string and 3 to replace uri. |
| 12327 | * |
| 12328 | * In query string case, the mark question '?' must be set at the start of the |
| 12329 | * string by the caller, event if the replacement query string is empty. |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 12330 | */ |
Thierry FOURNIER | b77aece | 2015-03-14 13:55:46 +0100 | [diff] [blame] | 12331 | int http_replace_req_line(int action, const char *replace, int len, |
Willy Tarreau | 987e3fb | 2015-04-04 01:09:08 +0200 | [diff] [blame] | 12332 | struct proxy *px, struct stream *s) |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 12333 | { |
Willy Tarreau | 987e3fb | 2015-04-04 01:09:08 +0200 | [diff] [blame] | 12334 | struct http_txn *txn = s->txn; |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 12335 | char *cur_ptr, *cur_end; |
Thierry FOURNIER | b77aece | 2015-03-14 13:55:46 +0100 | [diff] [blame] | 12336 | int offset = 0; |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 12337 | int delta; |
| 12338 | |
Thierry FOURNIER | b77aece | 2015-03-14 13:55:46 +0100 | [diff] [blame] | 12339 | switch (action) { |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 12340 | case 0: // method |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 12341 | cur_ptr = s->req.buf->p; |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 12342 | cur_end = cur_ptr + txn->req.sl.rq.m_l; |
| 12343 | |
| 12344 | /* adjust req line offsets and lengths */ |
Thierry FOURNIER | b77aece | 2015-03-14 13:55:46 +0100 | [diff] [blame] | 12345 | delta = len - offset - (cur_end - cur_ptr); |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 12346 | txn->req.sl.rq.m_l += delta; |
| 12347 | txn->req.sl.rq.u += delta; |
| 12348 | txn->req.sl.rq.v += delta; |
| 12349 | break; |
| 12350 | |
| 12351 | case 1: // path |
| 12352 | cur_ptr = http_get_path(txn); |
| 12353 | if (!cur_ptr) |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 12354 | cur_ptr = s->req.buf->p + txn->req.sl.rq.u; |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 12355 | |
| 12356 | cur_end = cur_ptr; |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 12357 | 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] | 12358 | cur_end++; |
| 12359 | |
| 12360 | /* adjust req line offsets and lengths */ |
Thierry FOURNIER | b77aece | 2015-03-14 13:55:46 +0100 | [diff] [blame] | 12361 | delta = len - offset - (cur_end - cur_ptr); |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 12362 | txn->req.sl.rq.u_l += delta; |
| 12363 | txn->req.sl.rq.v += delta; |
| 12364 | break; |
| 12365 | |
| 12366 | case 2: // query |
Thierry FOURNIER | b77aece | 2015-03-14 13:55:46 +0100 | [diff] [blame] | 12367 | offset = 1; |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 12368 | cur_ptr = s->req.buf->p + txn->req.sl.rq.u; |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 12369 | cur_end = cur_ptr + txn->req.sl.rq.u_l; |
| 12370 | while (cur_ptr < cur_end && *cur_ptr != '?') |
| 12371 | cur_ptr++; |
| 12372 | |
| 12373 | /* skip the question mark or indicate that we must insert it |
| 12374 | * (but only if the format string is not empty then). |
| 12375 | */ |
| 12376 | if (cur_ptr < cur_end) |
| 12377 | cur_ptr++; |
Thierry FOURNIER | b77aece | 2015-03-14 13:55:46 +0100 | [diff] [blame] | 12378 | else if (len > 1) |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 12379 | offset = 0; |
| 12380 | |
| 12381 | /* adjust req line offsets and lengths */ |
Thierry FOURNIER | b77aece | 2015-03-14 13:55:46 +0100 | [diff] [blame] | 12382 | delta = len - offset - (cur_end - cur_ptr); |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 12383 | txn->req.sl.rq.u_l += delta; |
| 12384 | txn->req.sl.rq.v += delta; |
| 12385 | break; |
| 12386 | |
| 12387 | case 3: // uri |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 12388 | cur_ptr = s->req.buf->p + txn->req.sl.rq.u; |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 12389 | cur_end = cur_ptr + txn->req.sl.rq.u_l; |
| 12390 | |
| 12391 | /* adjust req line offsets and lengths */ |
Thierry FOURNIER | b77aece | 2015-03-14 13:55:46 +0100 | [diff] [blame] | 12392 | delta = len - offset - (cur_end - cur_ptr); |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 12393 | txn->req.sl.rq.u_l += delta; |
| 12394 | txn->req.sl.rq.v += delta; |
| 12395 | break; |
| 12396 | |
| 12397 | default: |
Thierry FOURNIER | b77aece | 2015-03-14 13:55:46 +0100 | [diff] [blame] | 12398 | return -1; |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 12399 | } |
| 12400 | |
| 12401 | /* commit changes and adjust end of message */ |
Thierry FOURNIER | b77aece | 2015-03-14 13:55:46 +0100 | [diff] [blame] | 12402 | 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] | 12403 | txn->req.sl.rq.l += delta; |
| 12404 | txn->hdr_idx.v[0].len += delta; |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 12405 | http_msg_move_end(&txn->req, delta); |
Thierry FOURNIER | b77aece | 2015-03-14 13:55:46 +0100 | [diff] [blame] | 12406 | return 0; |
| 12407 | } |
| 12408 | |
Thierry FOURNIER | 35d70ef | 2015-08-26 16:21:56 +0200 | [diff] [blame] | 12409 | /* This function replace the HTTP status code and the associated message. The |
| 12410 | * variable <status> contains the new status code. This function never fails. |
| 12411 | */ |
| 12412 | void http_set_status(unsigned int status, struct stream *s) |
| 12413 | { |
| 12414 | struct http_txn *txn = s->txn; |
| 12415 | char *cur_ptr, *cur_end; |
| 12416 | int delta; |
| 12417 | char *res; |
| 12418 | int c_l; |
| 12419 | const char *msg; |
| 12420 | int msg_len; |
| 12421 | |
| 12422 | chunk_reset(&trash); |
| 12423 | |
| 12424 | res = ultoa_o(status, trash.str, trash.size); |
| 12425 | c_l = res - trash.str; |
| 12426 | |
| 12427 | trash.str[c_l] = ' '; |
| 12428 | trash.len = c_l + 1; |
| 12429 | |
| 12430 | msg = get_reason(status); |
| 12431 | msg_len = strlen(msg); |
| 12432 | |
| 12433 | strncpy(&trash.str[trash.len], msg, trash.size - trash.len); |
| 12434 | trash.len += msg_len; |
| 12435 | |
| 12436 | cur_ptr = s->res.buf->p + txn->rsp.sl.st.c; |
| 12437 | cur_end = s->res.buf->p + txn->rsp.sl.st.r + txn->rsp.sl.st.r_l; |
| 12438 | |
| 12439 | /* commit changes and adjust message */ |
| 12440 | delta = buffer_replace2(s->res.buf, cur_ptr, cur_end, trash.str, trash.len); |
| 12441 | |
| 12442 | /* adjust res line offsets and lengths */ |
| 12443 | txn->rsp.sl.st.r += c_l - txn->rsp.sl.st.c_l; |
| 12444 | txn->rsp.sl.st.c_l = c_l; |
| 12445 | txn->rsp.sl.st.r_l = msg_len; |
| 12446 | |
| 12447 | delta = trash.len - (cur_end - cur_ptr); |
| 12448 | txn->rsp.sl.st.l += delta; |
| 12449 | txn->hdr_idx.v[0].len += delta; |
| 12450 | http_msg_move_end(&txn->rsp, delta); |
| 12451 | } |
| 12452 | |
Thierry FOURNIER | b77aece | 2015-03-14 13:55:46 +0100 | [diff] [blame] | 12453 | /* This function executes one of the set-{method,path,query,uri} actions. It |
| 12454 | * builds a string in the trash from the specified format string. It finds |
Thierry FOURNIER | 3f4bc65 | 2015-08-26 16:23:34 +0200 | [diff] [blame] | 12455 | * the action to be performed in <http.action>, previously filled by function |
Thierry FOURNIER | b77aece | 2015-03-14 13:55:46 +0100 | [diff] [blame] | 12456 | * parse_set_req_line(). The replacement action is excuted by the function |
Thierry FOURNIER | 3f4bc65 | 2015-08-26 16:23:34 +0200 | [diff] [blame] | 12457 | * http_action_set_req_line(). It always returns ACT_RET_CONT. If an error |
| 12458 | * occurs the action is canceled, but the rule processing continue. |
Thierry FOURNIER | b77aece | 2015-03-14 13:55:46 +0100 | [diff] [blame] | 12459 | */ |
Thierry FOURNIER | 24ff6c6 | 2015-08-06 08:52:53 +0200 | [diff] [blame] | 12460 | enum act_return http_action_set_req_line(struct act_rule *rule, struct proxy *px, |
Willy Tarreau | 658b85b | 2015-09-27 10:00:49 +0200 | [diff] [blame] | 12461 | struct session *sess, struct stream *s, int flags) |
Thierry FOURNIER | b77aece | 2015-03-14 13:55:46 +0100 | [diff] [blame] | 12462 | { |
| 12463 | chunk_reset(&trash); |
| 12464 | |
| 12465 | /* If we have to create a query string, prepare a '?'. */ |
Thierry FOURNIER | 8855a92 | 2015-07-31 08:54:25 +0200 | [diff] [blame] | 12466 | if (rule->arg.http.action == 2) |
Thierry FOURNIER | b77aece | 2015-03-14 13:55:46 +0100 | [diff] [blame] | 12467 | trash.str[trash.len++] = '?'; |
Thierry FOURNIER | 8855a92 | 2015-07-31 08:54:25 +0200 | [diff] [blame] | 12468 | trash.len += build_logline(s, trash.str + trash.len, trash.size - trash.len, &rule->arg.http.logfmt); |
Thierry FOURNIER | b77aece | 2015-03-14 13:55:46 +0100 | [diff] [blame] | 12469 | |
Thierry FOURNIER | 8855a92 | 2015-07-31 08:54:25 +0200 | [diff] [blame] | 12470 | http_replace_req_line(rule->arg.http.action, trash.str, trash.len, px, s); |
Thierry FOURNIER | 24ff6c6 | 2015-08-06 08:52:53 +0200 | [diff] [blame] | 12471 | return ACT_RET_CONT; |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 12472 | } |
| 12473 | |
Thierry FOURNIER | 35d70ef | 2015-08-26 16:21:56 +0200 | [diff] [blame] | 12474 | /* This function is just a compliant action wrapper for "set-status". */ |
| 12475 | enum act_return action_http_set_status(struct act_rule *rule, struct proxy *px, |
Willy Tarreau | 658b85b | 2015-09-27 10:00:49 +0200 | [diff] [blame] | 12476 | struct session *sess, struct stream *s, int flags) |
Thierry FOURNIER | 35d70ef | 2015-08-26 16:21:56 +0200 | [diff] [blame] | 12477 | { |
| 12478 | http_set_status(rule->arg.status.code, s); |
| 12479 | return ACT_RET_CONT; |
| 12480 | } |
| 12481 | |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 12482 | /* parse an http-request action among : |
| 12483 | * set-method |
| 12484 | * set-path |
| 12485 | * set-query |
| 12486 | * set-uri |
| 12487 | * |
| 12488 | * All of them accept a single argument of type string representing a log-format. |
| 12489 | * The resulting rule makes use of arg->act.p[0..1] to store the log-format list |
| 12490 | * head, and p[2] to store the action as an int (0=method, 1=path, 2=query, 3=uri). |
Thierry FOURNIER | afa8049 | 2015-08-19 09:04:15 +0200 | [diff] [blame] | 12491 | * It returns ACT_RET_PRS_OK on success, ACT_RET_PRS_ERR on error. |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 12492 | */ |
Thierry FOURNIER | afa8049 | 2015-08-19 09:04:15 +0200 | [diff] [blame] | 12493 | enum act_parse_ret parse_set_req_line(const char **args, int *orig_arg, struct proxy *px, |
| 12494 | struct act_rule *rule, char **err) |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 12495 | { |
| 12496 | int cur_arg = *orig_arg; |
| 12497 | |
Thierry FOURNIER | 4214873 | 2015-09-02 17:17:33 +0200 | [diff] [blame] | 12498 | rule->action = ACT_CUSTOM; |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 12499 | |
| 12500 | switch (args[0][4]) { |
| 12501 | case 'm' : |
Thierry FOURNIER | 8855a92 | 2015-07-31 08:54:25 +0200 | [diff] [blame] | 12502 | rule->arg.http.action = 0; |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 12503 | rule->action_ptr = http_action_set_req_line; |
| 12504 | break; |
| 12505 | case 'p' : |
Thierry FOURNIER | 8855a92 | 2015-07-31 08:54:25 +0200 | [diff] [blame] | 12506 | rule->arg.http.action = 1; |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 12507 | rule->action_ptr = http_action_set_req_line; |
| 12508 | break; |
| 12509 | case 'q' : |
Thierry FOURNIER | 8855a92 | 2015-07-31 08:54:25 +0200 | [diff] [blame] | 12510 | rule->arg.http.action = 2; |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 12511 | rule->action_ptr = http_action_set_req_line; |
| 12512 | break; |
| 12513 | case 'u' : |
Thierry FOURNIER | 8855a92 | 2015-07-31 08:54:25 +0200 | [diff] [blame] | 12514 | rule->arg.http.action = 3; |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 12515 | rule->action_ptr = http_action_set_req_line; |
| 12516 | break; |
| 12517 | default: |
| 12518 | memprintf(err, "internal error: unhandled action '%s'", args[0]); |
Thierry FOURNIER | afa8049 | 2015-08-19 09:04:15 +0200 | [diff] [blame] | 12519 | return ACT_RET_PRS_ERR; |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 12520 | } |
| 12521 | |
| 12522 | if (!*args[cur_arg] || |
| 12523 | (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) { |
| 12524 | memprintf(err, "expects exactly 1 argument <format>"); |
Thierry FOURNIER | afa8049 | 2015-08-19 09:04:15 +0200 | [diff] [blame] | 12525 | return ACT_RET_PRS_ERR; |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 12526 | } |
| 12527 | |
Thierry FOURNIER | 8855a92 | 2015-07-31 08:54:25 +0200 | [diff] [blame] | 12528 | LIST_INIT(&rule->arg.http.logfmt); |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 12529 | proxy->conf.args.ctx = ARGC_HRQ; |
Thierry FOURNIER / OZON.IO | 59fd511 | 2016-11-22 23:50:02 +0100 | [diff] [blame] | 12530 | if (!parse_logformat_string(args[cur_arg], proxy, &rule->arg.http.logfmt, LOG_OPT_HTTP, |
Thierry FOURNIER / OZON.IO | 8a4e442 | 2016-11-23 00:41:28 +0100 | [diff] [blame] | 12531 | (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR, err)) { |
Thierry FOURNIER / OZON.IO | 59fd511 | 2016-11-22 23:50:02 +0100 | [diff] [blame] | 12532 | return ACT_RET_PRS_ERR; |
| 12533 | } |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 12534 | |
| 12535 | (*orig_arg)++; |
Thierry FOURNIER | afa8049 | 2015-08-19 09:04:15 +0200 | [diff] [blame] | 12536 | return ACT_RET_PRS_OK; |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 12537 | } |
| 12538 | |
Thierry FOURNIER | 35d70ef | 2015-08-26 16:21:56 +0200 | [diff] [blame] | 12539 | /* parse set-status action: |
| 12540 | * This action accepts a single argument of type int representing |
| 12541 | * an http status code. It returns ACT_RET_PRS_OK on success, |
| 12542 | * ACT_RET_PRS_ERR on error. |
| 12543 | */ |
| 12544 | enum act_parse_ret parse_http_set_status(const char **args, int *orig_arg, struct proxy *px, |
| 12545 | struct act_rule *rule, char **err) |
| 12546 | { |
| 12547 | char *error; |
| 12548 | |
Thierry FOURNIER | 4214873 | 2015-09-02 17:17:33 +0200 | [diff] [blame] | 12549 | rule->action = ACT_CUSTOM; |
Thierry FOURNIER | 35d70ef | 2015-08-26 16:21:56 +0200 | [diff] [blame] | 12550 | rule->action_ptr = action_http_set_status; |
| 12551 | |
| 12552 | /* Check if an argument is available */ |
| 12553 | if (!*args[*orig_arg]) { |
| 12554 | memprintf(err, "expects exactly 1 argument <status>"); |
| 12555 | return ACT_RET_PRS_ERR; |
| 12556 | } |
| 12557 | |
| 12558 | /* convert status code as integer */ |
| 12559 | rule->arg.status.code = strtol(args[*orig_arg], &error, 10); |
| 12560 | if (*error != '\0' || rule->arg.status.code < 100 || rule->arg.status.code > 999) { |
| 12561 | memprintf(err, "expects an integer status code between 100 and 999"); |
| 12562 | return ACT_RET_PRS_ERR; |
| 12563 | } |
| 12564 | |
| 12565 | (*orig_arg)++; |
| 12566 | return ACT_RET_PRS_OK; |
| 12567 | } |
| 12568 | |
Willy Tarreau | a9083d0 | 2015-05-08 15:27:59 +0200 | [diff] [blame] | 12569 | /* This function executes the "capture" action. It executes a fetch expression, |
| 12570 | * turns the result into a string and puts it in a capture slot. It always |
| 12571 | * returns 1. If an error occurs the action is cancelled, but the rule |
| 12572 | * processing continues. |
| 12573 | */ |
Thierry FOURNIER | 24ff6c6 | 2015-08-06 08:52:53 +0200 | [diff] [blame] | 12574 | enum act_return http_action_req_capture(struct act_rule *rule, struct proxy *px, |
Willy Tarreau | 658b85b | 2015-09-27 10:00:49 +0200 | [diff] [blame] | 12575 | struct session *sess, struct stream *s, int flags) |
Willy Tarreau | a9083d0 | 2015-05-08 15:27:59 +0200 | [diff] [blame] | 12576 | { |
Willy Tarreau | a9083d0 | 2015-05-08 15:27:59 +0200 | [diff] [blame] | 12577 | struct sample *key; |
Thierry FOURNIER | 32b1500 | 2015-07-31 08:56:16 +0200 | [diff] [blame] | 12578 | struct cap_hdr *h = rule->arg.cap.hdr; |
Willy Tarreau | a9083d0 | 2015-05-08 15:27:59 +0200 | [diff] [blame] | 12579 | char **cap = s->req_cap; |
| 12580 | int len; |
| 12581 | |
Thierry FOURNIER | 32b1500 | 2015-07-31 08:56:16 +0200 | [diff] [blame] | 12582 | key = sample_fetch_as_type(s->be, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL, rule->arg.cap.expr, SMP_T_STR); |
Willy Tarreau | a9083d0 | 2015-05-08 15:27:59 +0200 | [diff] [blame] | 12583 | if (!key) |
Thierry FOURNIER | 24ff6c6 | 2015-08-06 08:52:53 +0200 | [diff] [blame] | 12584 | return ACT_RET_CONT; |
Willy Tarreau | a9083d0 | 2015-05-08 15:27:59 +0200 | [diff] [blame] | 12585 | |
| 12586 | if (cap[h->index] == NULL) |
| 12587 | cap[h->index] = pool_alloc2(h->pool); |
| 12588 | |
| 12589 | if (cap[h->index] == NULL) /* no more capture memory */ |
Thierry FOURNIER | 24ff6c6 | 2015-08-06 08:52:53 +0200 | [diff] [blame] | 12590 | return ACT_RET_CONT; |
Willy Tarreau | a9083d0 | 2015-05-08 15:27:59 +0200 | [diff] [blame] | 12591 | |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 12592 | len = key->data.u.str.len; |
Willy Tarreau | a9083d0 | 2015-05-08 15:27:59 +0200 | [diff] [blame] | 12593 | if (len > h->len) |
| 12594 | len = h->len; |
| 12595 | |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 12596 | memcpy(cap[h->index], key->data.u.str.str, len); |
Willy Tarreau | a9083d0 | 2015-05-08 15:27:59 +0200 | [diff] [blame] | 12597 | cap[h->index][len] = 0; |
Thierry FOURNIER | 24ff6c6 | 2015-08-06 08:52:53 +0200 | [diff] [blame] | 12598 | return ACT_RET_CONT; |
Willy Tarreau | a9083d0 | 2015-05-08 15:27:59 +0200 | [diff] [blame] | 12599 | } |
| 12600 | |
Thierry FOURNIER | 82bf70d | 2015-05-26 17:58:29 +0200 | [diff] [blame] | 12601 | /* This function executes the "capture" action and store the result in a |
| 12602 | * capture slot if exists. It executes a fetch expression, turns the result |
| 12603 | * into a string and puts it in a capture slot. It always returns 1. If an |
| 12604 | * error occurs the action is cancelled, but the rule processing continues. |
| 12605 | */ |
Thierry FOURNIER | 24ff6c6 | 2015-08-06 08:52:53 +0200 | [diff] [blame] | 12606 | enum act_return http_action_req_capture_by_id(struct act_rule *rule, struct proxy *px, |
Willy Tarreau | 658b85b | 2015-09-27 10:00:49 +0200 | [diff] [blame] | 12607 | struct session *sess, struct stream *s, int flags) |
Thierry FOURNIER | 82bf70d | 2015-05-26 17:58:29 +0200 | [diff] [blame] | 12608 | { |
Thierry FOURNIER | 82bf70d | 2015-05-26 17:58:29 +0200 | [diff] [blame] | 12609 | struct sample *key; |
Thierry FOURNIER | 82bf70d | 2015-05-26 17:58:29 +0200 | [diff] [blame] | 12610 | struct cap_hdr *h; |
Thierry FOURNIER | 82bf70d | 2015-05-26 17:58:29 +0200 | [diff] [blame] | 12611 | char **cap = s->req_cap; |
| 12612 | struct proxy *fe = strm_fe(s); |
| 12613 | int len; |
| 12614 | int i; |
| 12615 | |
| 12616 | /* Look for the original configuration. */ |
| 12617 | for (h = fe->req_cap, i = fe->nb_req_cap - 1; |
Thierry FOURNIER | e209797 | 2015-07-31 08:56:35 +0200 | [diff] [blame] | 12618 | h != NULL && i != rule->arg.capid.idx ; |
Thierry FOURNIER | 82bf70d | 2015-05-26 17:58:29 +0200 | [diff] [blame] | 12619 | i--, h = h->next); |
| 12620 | if (!h) |
Thierry FOURNIER | 24ff6c6 | 2015-08-06 08:52:53 +0200 | [diff] [blame] | 12621 | return ACT_RET_CONT; |
Thierry FOURNIER | 82bf70d | 2015-05-26 17:58:29 +0200 | [diff] [blame] | 12622 | |
Thierry FOURNIER | e209797 | 2015-07-31 08:56:35 +0200 | [diff] [blame] | 12623 | key = sample_fetch_as_type(s->be, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL, rule->arg.capid.expr, SMP_T_STR); |
Thierry FOURNIER | 82bf70d | 2015-05-26 17:58:29 +0200 | [diff] [blame] | 12624 | if (!key) |
Thierry FOURNIER | 24ff6c6 | 2015-08-06 08:52:53 +0200 | [diff] [blame] | 12625 | return ACT_RET_CONT; |
Thierry FOURNIER | 82bf70d | 2015-05-26 17:58:29 +0200 | [diff] [blame] | 12626 | |
| 12627 | if (cap[h->index] == NULL) |
| 12628 | cap[h->index] = pool_alloc2(h->pool); |
| 12629 | |
| 12630 | if (cap[h->index] == NULL) /* no more capture memory */ |
Thierry FOURNIER | 24ff6c6 | 2015-08-06 08:52:53 +0200 | [diff] [blame] | 12631 | return ACT_RET_CONT; |
Thierry FOURNIER | 82bf70d | 2015-05-26 17:58:29 +0200 | [diff] [blame] | 12632 | |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 12633 | len = key->data.u.str.len; |
Thierry FOURNIER | 82bf70d | 2015-05-26 17:58:29 +0200 | [diff] [blame] | 12634 | if (len > h->len) |
| 12635 | len = h->len; |
| 12636 | |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 12637 | memcpy(cap[h->index], key->data.u.str.str, len); |
Thierry FOURNIER | 82bf70d | 2015-05-26 17:58:29 +0200 | [diff] [blame] | 12638 | cap[h->index][len] = 0; |
Thierry FOURNIER | 24ff6c6 | 2015-08-06 08:52:53 +0200 | [diff] [blame] | 12639 | return ACT_RET_CONT; |
Thierry FOURNIER | 82bf70d | 2015-05-26 17:58:29 +0200 | [diff] [blame] | 12640 | } |
| 12641 | |
Willy Tarreau | a9083d0 | 2015-05-08 15:27:59 +0200 | [diff] [blame] | 12642 | /* parse an "http-request capture" action. It takes a single argument which is |
| 12643 | * 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] | 12644 | * the allocated hdr_cap struct or the preallocated "id" into arg->act.p[1]. |
Thierry FOURNIER | afa8049 | 2015-08-19 09:04:15 +0200 | [diff] [blame] | 12645 | * It returns ACT_RET_PRS_OK on success, ACT_RET_PRS_ERR on error. |
Willy Tarreau | a9083d0 | 2015-05-08 15:27:59 +0200 | [diff] [blame] | 12646 | */ |
Thierry FOURNIER | afa8049 | 2015-08-19 09:04:15 +0200 | [diff] [blame] | 12647 | enum act_parse_ret parse_http_req_capture(const char **args, int *orig_arg, struct proxy *px, |
| 12648 | struct act_rule *rule, char **err) |
Willy Tarreau | a9083d0 | 2015-05-08 15:27:59 +0200 | [diff] [blame] | 12649 | { |
| 12650 | struct sample_expr *expr; |
| 12651 | struct cap_hdr *hdr; |
| 12652 | int cur_arg; |
Willy Tarreau | 3986ac1 | 2015-05-08 16:13:42 +0200 | [diff] [blame] | 12653 | int len = 0; |
Willy Tarreau | a9083d0 | 2015-05-08 15:27:59 +0200 | [diff] [blame] | 12654 | |
| 12655 | for (cur_arg = *orig_arg; cur_arg < *orig_arg + 3 && *args[cur_arg]; cur_arg++) |
| 12656 | if (strcmp(args[cur_arg], "if") == 0 || |
| 12657 | strcmp(args[cur_arg], "unless") == 0) |
| 12658 | break; |
| 12659 | |
| 12660 | if (cur_arg < *orig_arg + 3) { |
Thierry FOURNIER | 82bf70d | 2015-05-26 17:58:29 +0200 | [diff] [blame] | 12661 | memprintf(err, "expects <expression> [ 'len' <length> | id <idx> ]"); |
Thierry FOURNIER | afa8049 | 2015-08-19 09:04:15 +0200 | [diff] [blame] | 12662 | return ACT_RET_PRS_ERR; |
Willy Tarreau | a9083d0 | 2015-05-08 15:27:59 +0200 | [diff] [blame] | 12663 | } |
| 12664 | |
Willy Tarreau | a9083d0 | 2015-05-08 15:27:59 +0200 | [diff] [blame] | 12665 | cur_arg = *orig_arg; |
| 12666 | expr = sample_parse_expr((char **)args, &cur_arg, px->conf.args.file, px->conf.args.line, err, &px->conf.args); |
| 12667 | if (!expr) |
Thierry FOURNIER | afa8049 | 2015-08-19 09:04:15 +0200 | [diff] [blame] | 12668 | return ACT_RET_PRS_ERR; |
Willy Tarreau | a9083d0 | 2015-05-08 15:27:59 +0200 | [diff] [blame] | 12669 | |
| 12670 | if (!(expr->fetch->val & SMP_VAL_FE_HRQ_HDR)) { |
| 12671 | memprintf(err, |
| 12672 | "fetch method '%s' extracts information from '%s', none of which is available here", |
| 12673 | args[cur_arg-1], sample_src_names(expr->fetch->use)); |
| 12674 | free(expr); |
Thierry FOURNIER | afa8049 | 2015-08-19 09:04:15 +0200 | [diff] [blame] | 12675 | return ACT_RET_PRS_ERR; |
Willy Tarreau | a9083d0 | 2015-05-08 15:27:59 +0200 | [diff] [blame] | 12676 | } |
| 12677 | |
Thierry FOURNIER | 82bf70d | 2015-05-26 17:58:29 +0200 | [diff] [blame] | 12678 | if (!args[cur_arg] || !*args[cur_arg]) { |
| 12679 | memprintf(err, "expects 'len or 'id'"); |
| 12680 | free(expr); |
Thierry FOURNIER | afa8049 | 2015-08-19 09:04:15 +0200 | [diff] [blame] | 12681 | return ACT_RET_PRS_ERR; |
Thierry FOURNIER | 82bf70d | 2015-05-26 17:58:29 +0200 | [diff] [blame] | 12682 | } |
| 12683 | |
Willy Tarreau | a9083d0 | 2015-05-08 15:27:59 +0200 | [diff] [blame] | 12684 | if (strcmp(args[cur_arg], "len") == 0) { |
| 12685 | cur_arg++; |
Thierry FOURNIER | 82bf70d | 2015-05-26 17:58:29 +0200 | [diff] [blame] | 12686 | |
| 12687 | if (!(px->cap & PR_CAP_FE)) { |
| 12688 | memprintf(err, "proxy '%s' has no frontend capability", px->id); |
Thierry FOURNIER | afa8049 | 2015-08-19 09:04:15 +0200 | [diff] [blame] | 12689 | return ACT_RET_PRS_ERR; |
Thierry FOURNIER | 82bf70d | 2015-05-26 17:58:29 +0200 | [diff] [blame] | 12690 | } |
| 12691 | |
| 12692 | proxy->conf.args.ctx = ARGC_CAP; |
| 12693 | |
Willy Tarreau | a9083d0 | 2015-05-08 15:27:59 +0200 | [diff] [blame] | 12694 | if (!args[cur_arg]) { |
| 12695 | memprintf(err, "missing length value"); |
| 12696 | free(expr); |
Thierry FOURNIER | afa8049 | 2015-08-19 09:04:15 +0200 | [diff] [blame] | 12697 | return ACT_RET_PRS_ERR; |
Willy Tarreau | a9083d0 | 2015-05-08 15:27:59 +0200 | [diff] [blame] | 12698 | } |
| 12699 | /* we copy the table name for now, it will be resolved later */ |
| 12700 | len = atoi(args[cur_arg]); |
| 12701 | if (len <= 0) { |
| 12702 | memprintf(err, "length must be > 0"); |
| 12703 | free(expr); |
Thierry FOURNIER | afa8049 | 2015-08-19 09:04:15 +0200 | [diff] [blame] | 12704 | return ACT_RET_PRS_ERR; |
Willy Tarreau | a9083d0 | 2015-05-08 15:27:59 +0200 | [diff] [blame] | 12705 | } |
| 12706 | cur_arg++; |
Willy Tarreau | a9083d0 | 2015-05-08 15:27:59 +0200 | [diff] [blame] | 12707 | |
Thierry FOURNIER | 82bf70d | 2015-05-26 17:58:29 +0200 | [diff] [blame] | 12708 | if (!len) { |
| 12709 | memprintf(err, "a positive 'len' argument is mandatory"); |
| 12710 | free(expr); |
Thierry FOURNIER | afa8049 | 2015-08-19 09:04:15 +0200 | [diff] [blame] | 12711 | return ACT_RET_PRS_ERR; |
Thierry FOURNIER | 82bf70d | 2015-05-26 17:58:29 +0200 | [diff] [blame] | 12712 | } |
| 12713 | |
Vincent Bernat | 02779b6 | 2016-04-03 13:48:43 +0200 | [diff] [blame] | 12714 | hdr = calloc(1, sizeof(*hdr)); |
Thierry FOURNIER | 82bf70d | 2015-05-26 17:58:29 +0200 | [diff] [blame] | 12715 | hdr->next = px->req_cap; |
| 12716 | hdr->name = NULL; /* not a header capture */ |
| 12717 | hdr->namelen = 0; |
| 12718 | hdr->len = len; |
| 12719 | hdr->pool = create_pool("caphdr", hdr->len + 1, MEM_F_SHARED); |
| 12720 | hdr->index = px->nb_req_cap++; |
| 12721 | |
| 12722 | px->req_cap = hdr; |
| 12723 | px->to_log |= LW_REQHDR; |
| 12724 | |
Thierry FOURNIER | 4214873 | 2015-09-02 17:17:33 +0200 | [diff] [blame] | 12725 | rule->action = ACT_CUSTOM; |
Thierry FOURNIER | 82bf70d | 2015-05-26 17:58:29 +0200 | [diff] [blame] | 12726 | rule->action_ptr = http_action_req_capture; |
Thierry FOURNIER | 32b1500 | 2015-07-31 08:56:16 +0200 | [diff] [blame] | 12727 | rule->arg.cap.expr = expr; |
| 12728 | rule->arg.cap.hdr = hdr; |
Willy Tarreau | a9083d0 | 2015-05-08 15:27:59 +0200 | [diff] [blame] | 12729 | } |
| 12730 | |
Thierry FOURNIER | 82bf70d | 2015-05-26 17:58:29 +0200 | [diff] [blame] | 12731 | else if (strcmp(args[cur_arg], "id") == 0) { |
| 12732 | int id; |
| 12733 | char *error; |
Willy Tarreau | a9083d0 | 2015-05-08 15:27:59 +0200 | [diff] [blame] | 12734 | |
Thierry FOURNIER | 82bf70d | 2015-05-26 17:58:29 +0200 | [diff] [blame] | 12735 | cur_arg++; |
Willy Tarreau | a9083d0 | 2015-05-08 15:27:59 +0200 | [diff] [blame] | 12736 | |
Thierry FOURNIER | 82bf70d | 2015-05-26 17:58:29 +0200 | [diff] [blame] | 12737 | if (!args[cur_arg]) { |
| 12738 | memprintf(err, "missing id value"); |
| 12739 | free(expr); |
Thierry FOURNIER | afa8049 | 2015-08-19 09:04:15 +0200 | [diff] [blame] | 12740 | return ACT_RET_PRS_ERR; |
Thierry FOURNIER | 82bf70d | 2015-05-26 17:58:29 +0200 | [diff] [blame] | 12741 | } |
Willy Tarreau | a9083d0 | 2015-05-08 15:27:59 +0200 | [diff] [blame] | 12742 | |
Thierry FOURNIER | 82bf70d | 2015-05-26 17:58:29 +0200 | [diff] [blame] | 12743 | id = strtol(args[cur_arg], &error, 10); |
| 12744 | if (*error != '\0') { |
| 12745 | memprintf(err, "cannot parse id '%s'", args[cur_arg]); |
| 12746 | free(expr); |
Thierry FOURNIER | afa8049 | 2015-08-19 09:04:15 +0200 | [diff] [blame] | 12747 | return ACT_RET_PRS_ERR; |
Thierry FOURNIER | 82bf70d | 2015-05-26 17:58:29 +0200 | [diff] [blame] | 12748 | } |
| 12749 | cur_arg++; |
| 12750 | |
| 12751 | proxy->conf.args.ctx = ARGC_CAP; |
| 12752 | |
Thierry FOURNIER | 4214873 | 2015-09-02 17:17:33 +0200 | [diff] [blame] | 12753 | rule->action = ACT_CUSTOM; |
Thierry FOURNIER | 82bf70d | 2015-05-26 17:58:29 +0200 | [diff] [blame] | 12754 | rule->action_ptr = http_action_req_capture_by_id; |
Thierry FOURNIER | e209797 | 2015-07-31 08:56:35 +0200 | [diff] [blame] | 12755 | rule->arg.capid.expr = expr; |
| 12756 | rule->arg.capid.idx = id; |
Thierry FOURNIER | 82bf70d | 2015-05-26 17:58:29 +0200 | [diff] [blame] | 12757 | } |
| 12758 | |
| 12759 | else { |
| 12760 | memprintf(err, "expects 'len' or 'id', found '%s'", args[cur_arg]); |
| 12761 | free(expr); |
Thierry FOURNIER | afa8049 | 2015-08-19 09:04:15 +0200 | [diff] [blame] | 12762 | return ACT_RET_PRS_ERR; |
Thierry FOURNIER | 82bf70d | 2015-05-26 17:58:29 +0200 | [diff] [blame] | 12763 | } |
Willy Tarreau | a9083d0 | 2015-05-08 15:27:59 +0200 | [diff] [blame] | 12764 | |
| 12765 | *orig_arg = cur_arg; |
Thierry FOURNIER | afa8049 | 2015-08-19 09:04:15 +0200 | [diff] [blame] | 12766 | return ACT_RET_PRS_OK; |
Willy Tarreau | a9083d0 | 2015-05-08 15:27:59 +0200 | [diff] [blame] | 12767 | } |
| 12768 | |
Thierry FOURNIER | e80fada | 2015-05-26 18:06:31 +0200 | [diff] [blame] | 12769 | /* This function executes the "capture" action and store the result in a |
| 12770 | * capture slot if exists. It executes a fetch expression, turns the result |
| 12771 | * into a string and puts it in a capture slot. It always returns 1. If an |
| 12772 | * error occurs the action is cancelled, but the rule processing continues. |
| 12773 | */ |
Thierry FOURNIER | 24ff6c6 | 2015-08-06 08:52:53 +0200 | [diff] [blame] | 12774 | enum act_return http_action_res_capture_by_id(struct act_rule *rule, struct proxy *px, |
Willy Tarreau | 658b85b | 2015-09-27 10:00:49 +0200 | [diff] [blame] | 12775 | struct session *sess, struct stream *s, int flags) |
Thierry FOURNIER | e80fada | 2015-05-26 18:06:31 +0200 | [diff] [blame] | 12776 | { |
Thierry FOURNIER | e80fada | 2015-05-26 18:06:31 +0200 | [diff] [blame] | 12777 | struct sample *key; |
Thierry FOURNIER | e80fada | 2015-05-26 18:06:31 +0200 | [diff] [blame] | 12778 | struct cap_hdr *h; |
Thierry FOURNIER | e80fada | 2015-05-26 18:06:31 +0200 | [diff] [blame] | 12779 | char **cap = s->res_cap; |
| 12780 | struct proxy *fe = strm_fe(s); |
| 12781 | int len; |
| 12782 | int i; |
| 12783 | |
| 12784 | /* Look for the original configuration. */ |
| 12785 | for (h = fe->rsp_cap, i = fe->nb_rsp_cap - 1; |
Thierry FOURNIER | e209797 | 2015-07-31 08:56:35 +0200 | [diff] [blame] | 12786 | h != NULL && i != rule->arg.capid.idx ; |
Thierry FOURNIER | e80fada | 2015-05-26 18:06:31 +0200 | [diff] [blame] | 12787 | i--, h = h->next); |
| 12788 | if (!h) |
Thierry FOURNIER | 24ff6c6 | 2015-08-06 08:52:53 +0200 | [diff] [blame] | 12789 | return ACT_RET_CONT; |
Thierry FOURNIER | e80fada | 2015-05-26 18:06:31 +0200 | [diff] [blame] | 12790 | |
Thierry FOURNIER | e209797 | 2015-07-31 08:56:35 +0200 | [diff] [blame] | 12791 | key = sample_fetch_as_type(s->be, sess, s, SMP_OPT_DIR_RES|SMP_OPT_FINAL, rule->arg.capid.expr, SMP_T_STR); |
Thierry FOURNIER | e80fada | 2015-05-26 18:06:31 +0200 | [diff] [blame] | 12792 | if (!key) |
Thierry FOURNIER | 24ff6c6 | 2015-08-06 08:52:53 +0200 | [diff] [blame] | 12793 | return ACT_RET_CONT; |
Thierry FOURNIER | e80fada | 2015-05-26 18:06:31 +0200 | [diff] [blame] | 12794 | |
| 12795 | if (cap[h->index] == NULL) |
| 12796 | cap[h->index] = pool_alloc2(h->pool); |
| 12797 | |
| 12798 | if (cap[h->index] == NULL) /* no more capture memory */ |
Thierry FOURNIER | 24ff6c6 | 2015-08-06 08:52:53 +0200 | [diff] [blame] | 12799 | return ACT_RET_CONT; |
Thierry FOURNIER | e80fada | 2015-05-26 18:06:31 +0200 | [diff] [blame] | 12800 | |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 12801 | len = key->data.u.str.len; |
Thierry FOURNIER | e80fada | 2015-05-26 18:06:31 +0200 | [diff] [blame] | 12802 | if (len > h->len) |
| 12803 | len = h->len; |
| 12804 | |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 12805 | memcpy(cap[h->index], key->data.u.str.str, len); |
Thierry FOURNIER | e80fada | 2015-05-26 18:06:31 +0200 | [diff] [blame] | 12806 | cap[h->index][len] = 0; |
Thierry FOURNIER | 24ff6c6 | 2015-08-06 08:52:53 +0200 | [diff] [blame] | 12807 | return ACT_RET_CONT; |
Thierry FOURNIER | e80fada | 2015-05-26 18:06:31 +0200 | [diff] [blame] | 12808 | } |
| 12809 | |
| 12810 | /* parse an "http-response capture" action. It takes a single argument which is |
| 12811 | * a sample fetch expression. It stores the expression into arg->act.p[0] and |
| 12812 | * the allocated hdr_cap struct od the preallocated id into arg->act.p[1]. |
Thierry FOURNIER | afa8049 | 2015-08-19 09:04:15 +0200 | [diff] [blame] | 12813 | * It returns ACT_RET_PRS_OK on success, ACT_RET_PRS_ERR on error. |
Thierry FOURNIER | e80fada | 2015-05-26 18:06:31 +0200 | [diff] [blame] | 12814 | */ |
Thierry FOURNIER | afa8049 | 2015-08-19 09:04:15 +0200 | [diff] [blame] | 12815 | enum act_parse_ret parse_http_res_capture(const char **args, int *orig_arg, struct proxy *px, |
| 12816 | struct act_rule *rule, char **err) |
Thierry FOURNIER | e80fada | 2015-05-26 18:06:31 +0200 | [diff] [blame] | 12817 | { |
| 12818 | struct sample_expr *expr; |
| 12819 | int cur_arg; |
| 12820 | int id; |
| 12821 | char *error; |
| 12822 | |
| 12823 | for (cur_arg = *orig_arg; cur_arg < *orig_arg + 3 && *args[cur_arg]; cur_arg++) |
| 12824 | if (strcmp(args[cur_arg], "if") == 0 || |
| 12825 | strcmp(args[cur_arg], "unless") == 0) |
| 12826 | break; |
| 12827 | |
| 12828 | if (cur_arg < *orig_arg + 3) { |
Willy Tarreau | 29bdb1c | 2016-06-24 15:36:34 +0200 | [diff] [blame] | 12829 | memprintf(err, "expects <expression> id <idx>"); |
Thierry FOURNIER | afa8049 | 2015-08-19 09:04:15 +0200 | [diff] [blame] | 12830 | return ACT_RET_PRS_ERR; |
Thierry FOURNIER | e80fada | 2015-05-26 18:06:31 +0200 | [diff] [blame] | 12831 | } |
| 12832 | |
| 12833 | cur_arg = *orig_arg; |
| 12834 | expr = sample_parse_expr((char **)args, &cur_arg, px->conf.args.file, px->conf.args.line, err, &px->conf.args); |
| 12835 | if (!expr) |
Thierry FOURNIER | afa8049 | 2015-08-19 09:04:15 +0200 | [diff] [blame] | 12836 | return ACT_RET_PRS_ERR; |
Thierry FOURNIER | e80fada | 2015-05-26 18:06:31 +0200 | [diff] [blame] | 12837 | |
| 12838 | if (!(expr->fetch->val & SMP_VAL_FE_HRS_HDR)) { |
| 12839 | memprintf(err, |
| 12840 | "fetch method '%s' extracts information from '%s', none of which is available here", |
| 12841 | args[cur_arg-1], sample_src_names(expr->fetch->use)); |
| 12842 | free(expr); |
Thierry FOURNIER | afa8049 | 2015-08-19 09:04:15 +0200 | [diff] [blame] | 12843 | return ACT_RET_PRS_ERR; |
Thierry FOURNIER | e80fada | 2015-05-26 18:06:31 +0200 | [diff] [blame] | 12844 | } |
| 12845 | |
| 12846 | if (!args[cur_arg] || !*args[cur_arg]) { |
Willy Tarreau | 29bdb1c | 2016-06-24 15:36:34 +0200 | [diff] [blame] | 12847 | memprintf(err, "expects 'id'"); |
Thierry FOURNIER | e80fada | 2015-05-26 18:06:31 +0200 | [diff] [blame] | 12848 | free(expr); |
Thierry FOURNIER | afa8049 | 2015-08-19 09:04:15 +0200 | [diff] [blame] | 12849 | return ACT_RET_PRS_ERR; |
Thierry FOURNIER | e80fada | 2015-05-26 18:06:31 +0200 | [diff] [blame] | 12850 | } |
| 12851 | |
| 12852 | if (strcmp(args[cur_arg], "id") != 0) { |
| 12853 | memprintf(err, "expects 'id', found '%s'", args[cur_arg]); |
| 12854 | free(expr); |
Thierry FOURNIER | afa8049 | 2015-08-19 09:04:15 +0200 | [diff] [blame] | 12855 | return ACT_RET_PRS_ERR; |
Thierry FOURNIER | e80fada | 2015-05-26 18:06:31 +0200 | [diff] [blame] | 12856 | } |
| 12857 | |
| 12858 | cur_arg++; |
| 12859 | |
| 12860 | if (!args[cur_arg]) { |
| 12861 | memprintf(err, "missing id value"); |
| 12862 | free(expr); |
Thierry FOURNIER | afa8049 | 2015-08-19 09:04:15 +0200 | [diff] [blame] | 12863 | return ACT_RET_PRS_ERR; |
Thierry FOURNIER | e80fada | 2015-05-26 18:06:31 +0200 | [diff] [blame] | 12864 | } |
| 12865 | |
| 12866 | id = strtol(args[cur_arg], &error, 10); |
| 12867 | if (*error != '\0') { |
| 12868 | memprintf(err, "cannot parse id '%s'", args[cur_arg]); |
| 12869 | free(expr); |
Thierry FOURNIER | afa8049 | 2015-08-19 09:04:15 +0200 | [diff] [blame] | 12870 | return ACT_RET_PRS_ERR; |
Thierry FOURNIER | e80fada | 2015-05-26 18:06:31 +0200 | [diff] [blame] | 12871 | } |
| 12872 | cur_arg++; |
| 12873 | |
Thierry FOURNIER | e80fada | 2015-05-26 18:06:31 +0200 | [diff] [blame] | 12874 | proxy->conf.args.ctx = ARGC_CAP; |
| 12875 | |
Thierry FOURNIER | 4214873 | 2015-09-02 17:17:33 +0200 | [diff] [blame] | 12876 | rule->action = ACT_CUSTOM; |
Thierry FOURNIER | e80fada | 2015-05-26 18:06:31 +0200 | [diff] [blame] | 12877 | rule->action_ptr = http_action_res_capture_by_id; |
Thierry FOURNIER | e209797 | 2015-07-31 08:56:35 +0200 | [diff] [blame] | 12878 | rule->arg.capid.expr = expr; |
| 12879 | rule->arg.capid.idx = id; |
Thierry FOURNIER | e80fada | 2015-05-26 18:06:31 +0200 | [diff] [blame] | 12880 | |
| 12881 | *orig_arg = cur_arg; |
Thierry FOURNIER | afa8049 | 2015-08-19 09:04:15 +0200 | [diff] [blame] | 12882 | return ACT_RET_PRS_OK; |
Thierry FOURNIER | e80fada | 2015-05-26 18:06:31 +0200 | [diff] [blame] | 12883 | } |
| 12884 | |
William Lallemand | 73025dd | 2014-04-24 14:38:37 +0200 | [diff] [blame] | 12885 | /* |
| 12886 | * Return the struct http_req_action_kw associated to a keyword. |
| 12887 | */ |
Thierry FOURNIER | 36481b8 | 2015-08-19 09:01:53 +0200 | [diff] [blame] | 12888 | struct action_kw *action_http_req_custom(const char *kw) |
William Lallemand | 73025dd | 2014-04-24 14:38:37 +0200 | [diff] [blame] | 12889 | { |
Thierry FOURNIER | 322a124 | 2015-08-19 09:07:47 +0200 | [diff] [blame] | 12890 | return action_lookup(&http_req_keywords.list, kw); |
William Lallemand | 73025dd | 2014-04-24 14:38:37 +0200 | [diff] [blame] | 12891 | } |
| 12892 | |
| 12893 | /* |
| 12894 | * Return the struct http_res_action_kw associated to a keyword. |
| 12895 | */ |
Thierry FOURNIER | 36481b8 | 2015-08-19 09:01:53 +0200 | [diff] [blame] | 12896 | struct action_kw *action_http_res_custom(const char *kw) |
William Lallemand | 73025dd | 2014-04-24 14:38:37 +0200 | [diff] [blame] | 12897 | { |
Thierry FOURNIER | 322a124 | 2015-08-19 09:07:47 +0200 | [diff] [blame] | 12898 | return action_lookup(&http_res_keywords.list, kw); |
William Lallemand | 73025dd | 2014-04-24 14:38:37 +0200 | [diff] [blame] | 12899 | } |
| 12900 | |
Willy Tarreau | 12207b3 | 2016-11-22 19:48:51 +0100 | [diff] [blame] | 12901 | |
| 12902 | /* "show errors" handler for the CLI. Returns 0 if wants to continue, 1 to stop |
| 12903 | * now. |
| 12904 | */ |
| 12905 | static int cli_parse_show_errors(char **args, struct appctx *appctx, void *private) |
| 12906 | { |
| 12907 | if (!cli_has_level(appctx, ACCESS_LVL_OPER)) |
| 12908 | return 1; |
| 12909 | |
Willy Tarreau | 234ba2d | 2016-11-25 08:39:10 +0100 | [diff] [blame] | 12910 | if (*args[2]) { |
| 12911 | struct proxy *px; |
| 12912 | |
| 12913 | px = proxy_find_by_name(args[2], 0, 0); |
| 12914 | if (px) |
| 12915 | appctx->ctx.errors.iid = px->uuid; |
| 12916 | else |
| 12917 | appctx->ctx.errors.iid = atoi(args[2]); |
| 12918 | |
| 12919 | if (!appctx->ctx.errors.iid) { |
| 12920 | appctx->ctx.cli.msg = "No such proxy.\n"; |
| 12921 | appctx->st0 = CLI_ST_PRINT; |
| 12922 | return 1; |
| 12923 | } |
| 12924 | } |
Willy Tarreau | 12207b3 | 2016-11-22 19:48:51 +0100 | [diff] [blame] | 12925 | else |
Willy Tarreau | 234ba2d | 2016-11-25 08:39:10 +0100 | [diff] [blame] | 12926 | appctx->ctx.errors.iid = -1; // dump all proxies |
| 12927 | |
Willy Tarreau | 35069f8 | 2016-11-25 09:16:37 +0100 | [diff] [blame^] | 12928 | appctx->ctx.errors.flag = 0; |
| 12929 | if (strcmp(args[3], "request") == 0) |
| 12930 | appctx->ctx.errors.flag |= 4; // ignore response |
| 12931 | else if (strcmp(args[3], "response") == 0) |
| 12932 | appctx->ctx.errors.flag |= 2; // ignore request |
Willy Tarreau | 12207b3 | 2016-11-22 19:48:51 +0100 | [diff] [blame] | 12933 | appctx->ctx.errors.px = NULL; |
Willy Tarreau | 12207b3 | 2016-11-22 19:48:51 +0100 | [diff] [blame] | 12934 | return 0; |
| 12935 | } |
| 12936 | |
| 12937 | /* This function dumps all captured errors onto the stream interface's |
| 12938 | * read buffer. It returns 0 if the output buffer is full and it needs |
| 12939 | * to be called again, otherwise non-zero. |
| 12940 | */ |
| 12941 | static int cli_io_handler_show_errors(struct appctx *appctx) |
| 12942 | { |
| 12943 | struct stream_interface *si = appctx->owner; |
| 12944 | extern const char *monthname[12]; |
| 12945 | |
| 12946 | if (unlikely(si_ic(si)->flags & (CF_WRITE_ERROR|CF_SHUTW))) |
| 12947 | return 1; |
| 12948 | |
| 12949 | chunk_reset(&trash); |
| 12950 | |
| 12951 | if (!appctx->ctx.errors.px) { |
| 12952 | /* the function had not been called yet, let's prepare the |
| 12953 | * buffer for a response. |
| 12954 | */ |
| 12955 | struct tm tm; |
| 12956 | |
| 12957 | get_localtime(date.tv_sec, &tm); |
| 12958 | chunk_appendf(&trash, "Total events captured on [%02d/%s/%04d:%02d:%02d:%02d.%03d] : %u\n", |
| 12959 | tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900, |
| 12960 | tm.tm_hour, tm.tm_min, tm.tm_sec, (int)(date.tv_usec/1000), |
| 12961 | error_snapshot_id); |
| 12962 | |
| 12963 | if (bi_putchk(si_ic(si), &trash) == -1) { |
| 12964 | /* Socket buffer full. Let's try again later from the same point */ |
| 12965 | si_applet_cant_put(si); |
| 12966 | return 0; |
| 12967 | } |
| 12968 | |
| 12969 | appctx->ctx.errors.px = proxy; |
Willy Tarreau | 12207b3 | 2016-11-22 19:48:51 +0100 | [diff] [blame] | 12970 | appctx->ctx.errors.bol = 0; |
| 12971 | appctx->ctx.errors.ptr = -1; |
| 12972 | } |
| 12973 | |
| 12974 | /* we have two inner loops here, one for the proxy, the other one for |
| 12975 | * the buffer. |
| 12976 | */ |
| 12977 | while (appctx->ctx.errors.px) { |
| 12978 | struct error_snapshot *es; |
| 12979 | |
Willy Tarreau | 35069f8 | 2016-11-25 09:16:37 +0100 | [diff] [blame^] | 12980 | if ((appctx->ctx.errors.flag & 1) == 0) { |
Willy Tarreau | 12207b3 | 2016-11-22 19:48:51 +0100 | [diff] [blame] | 12981 | es = &appctx->ctx.errors.px->invalid_req; |
Willy Tarreau | 35069f8 | 2016-11-25 09:16:37 +0100 | [diff] [blame^] | 12982 | if (appctx->ctx.errors.flag & 2) // skip req |
| 12983 | goto next; |
| 12984 | } |
| 12985 | else { |
Willy Tarreau | 12207b3 | 2016-11-22 19:48:51 +0100 | [diff] [blame] | 12986 | es = &appctx->ctx.errors.px->invalid_rep; |
Willy Tarreau | 35069f8 | 2016-11-25 09:16:37 +0100 | [diff] [blame^] | 12987 | if (appctx->ctx.errors.flag & 4) // skip resp |
| 12988 | goto next; |
| 12989 | } |
Willy Tarreau | 12207b3 | 2016-11-22 19:48:51 +0100 | [diff] [blame] | 12990 | |
| 12991 | if (!es->when.tv_sec) |
| 12992 | goto next; |
| 12993 | |
| 12994 | if (appctx->ctx.errors.iid >= 0 && |
| 12995 | appctx->ctx.errors.px->uuid != appctx->ctx.errors.iid && |
| 12996 | es->oe->uuid != appctx->ctx.errors.iid) |
| 12997 | goto next; |
| 12998 | |
| 12999 | if (appctx->ctx.errors.ptr < 0) { |
| 13000 | /* just print headers now */ |
| 13001 | |
| 13002 | char pn[INET6_ADDRSTRLEN]; |
| 13003 | struct tm tm; |
| 13004 | int port; |
| 13005 | |
| 13006 | get_localtime(es->when.tv_sec, &tm); |
| 13007 | chunk_appendf(&trash, " \n[%02d/%s/%04d:%02d:%02d:%02d.%03d]", |
| 13008 | tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900, |
| 13009 | tm.tm_hour, tm.tm_min, tm.tm_sec, (int)(es->when.tv_usec/1000)); |
| 13010 | |
| 13011 | switch (addr_to_str(&es->src, pn, sizeof(pn))) { |
| 13012 | case AF_INET: |
| 13013 | case AF_INET6: |
| 13014 | port = get_host_port(&es->src); |
| 13015 | break; |
| 13016 | default: |
| 13017 | port = 0; |
| 13018 | } |
| 13019 | |
Willy Tarreau | 35069f8 | 2016-11-25 09:16:37 +0100 | [diff] [blame^] | 13020 | switch (appctx->ctx.errors.flag & 1) { |
Willy Tarreau | 12207b3 | 2016-11-22 19:48:51 +0100 | [diff] [blame] | 13021 | case 0: |
| 13022 | chunk_appendf(&trash, |
| 13023 | " frontend %s (#%d): invalid request\n" |
| 13024 | " backend %s (#%d)", |
| 13025 | appctx->ctx.errors.px->id, appctx->ctx.errors.px->uuid, |
| 13026 | (es->oe->cap & PR_CAP_BE) ? es->oe->id : "<NONE>", |
| 13027 | (es->oe->cap & PR_CAP_BE) ? es->oe->uuid : -1); |
| 13028 | break; |
| 13029 | case 1: |
| 13030 | chunk_appendf(&trash, |
| 13031 | " backend %s (#%d): invalid response\n" |
| 13032 | " frontend %s (#%d)", |
| 13033 | appctx->ctx.errors.px->id, appctx->ctx.errors.px->uuid, |
| 13034 | es->oe->id, es->oe->uuid); |
| 13035 | break; |
| 13036 | } |
| 13037 | |
| 13038 | chunk_appendf(&trash, |
| 13039 | ", server %s (#%d), event #%u\n" |
| 13040 | " src %s:%d, session #%d, session flags 0x%08x\n" |
| 13041 | " HTTP msg state %d, msg flags 0x%08x, tx flags 0x%08x\n" |
| 13042 | " HTTP chunk len %lld bytes, HTTP body len %lld bytes\n" |
| 13043 | " buffer flags 0x%08x, out %d bytes, total %lld bytes\n" |
| 13044 | " pending %d bytes, wrapping at %d, error at position %d:\n \n", |
| 13045 | es->srv ? es->srv->id : "<NONE>", es->srv ? es->srv->puid : -1, |
| 13046 | es->ev_id, |
| 13047 | pn, port, es->sid, es->s_flags, |
| 13048 | es->state, es->m_flags, es->t_flags, |
| 13049 | es->m_clen, es->m_blen, |
| 13050 | es->b_flags, es->b_out, es->b_tot, |
| 13051 | es->len, es->b_wrap, es->pos); |
| 13052 | |
| 13053 | if (bi_putchk(si_ic(si), &trash) == -1) { |
| 13054 | /* Socket buffer full. Let's try again later from the same point */ |
| 13055 | si_applet_cant_put(si); |
| 13056 | return 0; |
| 13057 | } |
| 13058 | appctx->ctx.errors.ptr = 0; |
| 13059 | appctx->ctx.errors.sid = es->sid; |
| 13060 | } |
| 13061 | |
| 13062 | if (appctx->ctx.errors.sid != es->sid) { |
| 13063 | /* the snapshot changed while we were dumping it */ |
| 13064 | chunk_appendf(&trash, |
| 13065 | " WARNING! update detected on this snapshot, dump interrupted. Please re-check!\n"); |
| 13066 | if (bi_putchk(si_ic(si), &trash) == -1) { |
| 13067 | si_applet_cant_put(si); |
| 13068 | return 0; |
| 13069 | } |
| 13070 | goto next; |
| 13071 | } |
| 13072 | |
| 13073 | /* OK, ptr >= 0, so we have to dump the current line */ |
| 13074 | while (es->buf && appctx->ctx.errors.ptr < es->len && appctx->ctx.errors.ptr < global.tune.bufsize) { |
| 13075 | int newptr; |
| 13076 | int newline; |
| 13077 | |
| 13078 | newline = appctx->ctx.errors.bol; |
| 13079 | newptr = dump_text_line(&trash, es->buf, global.tune.bufsize, es->len, &newline, appctx->ctx.errors.ptr); |
| 13080 | if (newptr == appctx->ctx.errors.ptr) |
| 13081 | return 0; |
| 13082 | |
| 13083 | if (bi_putchk(si_ic(si), &trash) == -1) { |
| 13084 | /* Socket buffer full. Let's try again later from the same point */ |
| 13085 | si_applet_cant_put(si); |
| 13086 | return 0; |
| 13087 | } |
| 13088 | appctx->ctx.errors.ptr = newptr; |
| 13089 | appctx->ctx.errors.bol = newline; |
| 13090 | }; |
| 13091 | next: |
| 13092 | appctx->ctx.errors.bol = 0; |
| 13093 | appctx->ctx.errors.ptr = -1; |
Willy Tarreau | 35069f8 | 2016-11-25 09:16:37 +0100 | [diff] [blame^] | 13094 | appctx->ctx.errors.flag ^= 1; |
| 13095 | if (!(appctx->ctx.errors.flag & 1)) |
Willy Tarreau | 12207b3 | 2016-11-22 19:48:51 +0100 | [diff] [blame] | 13096 | appctx->ctx.errors.px = appctx->ctx.errors.px->next; |
Willy Tarreau | 12207b3 | 2016-11-22 19:48:51 +0100 | [diff] [blame] | 13097 | } |
| 13098 | |
| 13099 | /* dump complete */ |
| 13100 | return 1; |
| 13101 | } |
| 13102 | |
| 13103 | /* register cli keywords */ |
| 13104 | static struct cli_kw_list cli_kws = {{ },{ |
| 13105 | { { "show", "errors", NULL }, |
| 13106 | "show errors : report last request and response errors for each proxy", |
| 13107 | cli_parse_show_errors, cli_io_handler_show_errors, NULL, |
| 13108 | }, |
| 13109 | {{},} |
| 13110 | }}; |
| 13111 | |
Willy Tarreau | 4a56897 | 2010-05-12 08:08:50 +0200 | [diff] [blame] | 13112 | /************************************************************************/ |
Willy Tarreau | 25c1ebc | 2012-04-25 16:21:44 +0200 | [diff] [blame] | 13113 | /* All supported ACL keywords must be declared here. */ |
| 13114 | /************************************************************************/ |
| 13115 | |
| 13116 | /* Note: must not be declared <const> as its list will be overwritten. |
| 13117 | * Please take care of keeping this list alphabetically sorted. |
| 13118 | */ |
Willy Tarreau | dc13c11 | 2013-06-21 23:16:39 +0200 | [diff] [blame] | 13119 | static struct acl_kw_list acl_kws = {ILH, { |
Thierry FOURNIER | c5a4e98 | 2014-03-05 16:07:08 +0100 | [diff] [blame] | 13120 | { "base", "base", PAT_MATCH_STR }, |
| 13121 | { "base_beg", "base", PAT_MATCH_BEG }, |
| 13122 | { "base_dir", "base", PAT_MATCH_DIR }, |
| 13123 | { "base_dom", "base", PAT_MATCH_DOM }, |
| 13124 | { "base_end", "base", PAT_MATCH_END }, |
| 13125 | { "base_len", "base", PAT_MATCH_LEN }, |
| 13126 | { "base_reg", "base", PAT_MATCH_REG }, |
| 13127 | { "base_sub", "base", PAT_MATCH_SUB }, |
Willy Tarreau | a7ad50c | 2012-04-29 15:39:40 +0200 | [diff] [blame] | 13128 | |
Thierry FOURNIER | c5a4e98 | 2014-03-05 16:07:08 +0100 | [diff] [blame] | 13129 | { "cook", "req.cook", PAT_MATCH_STR }, |
| 13130 | { "cook_beg", "req.cook", PAT_MATCH_BEG }, |
| 13131 | { "cook_dir", "req.cook", PAT_MATCH_DIR }, |
| 13132 | { "cook_dom", "req.cook", PAT_MATCH_DOM }, |
| 13133 | { "cook_end", "req.cook", PAT_MATCH_END }, |
| 13134 | { "cook_len", "req.cook", PAT_MATCH_LEN }, |
| 13135 | { "cook_reg", "req.cook", PAT_MATCH_REG }, |
| 13136 | { "cook_sub", "req.cook", PAT_MATCH_SUB }, |
Willy Tarreau | 25c1ebc | 2012-04-25 16:21:44 +0200 | [diff] [blame] | 13137 | |
Thierry FOURNIER | c5a4e98 | 2014-03-05 16:07:08 +0100 | [diff] [blame] | 13138 | { "hdr", "req.hdr", PAT_MATCH_STR }, |
| 13139 | { "hdr_beg", "req.hdr", PAT_MATCH_BEG }, |
| 13140 | { "hdr_dir", "req.hdr", PAT_MATCH_DIR }, |
| 13141 | { "hdr_dom", "req.hdr", PAT_MATCH_DOM }, |
| 13142 | { "hdr_end", "req.hdr", PAT_MATCH_END }, |
| 13143 | { "hdr_len", "req.hdr", PAT_MATCH_LEN }, |
| 13144 | { "hdr_reg", "req.hdr", PAT_MATCH_REG }, |
| 13145 | { "hdr_sub", "req.hdr", PAT_MATCH_SUB }, |
Willy Tarreau | 25c1ebc | 2012-04-25 16:21:44 +0200 | [diff] [blame] | 13146 | |
Thierry FOURNIER | c5a4e98 | 2014-03-05 16:07:08 +0100 | [diff] [blame] | 13147 | /* these two declarations uses strings with list storage (in place |
| 13148 | * of tree storage). The basic match is PAT_MATCH_STR, but the indexation |
| 13149 | * and delete functions are relative to the list management. The parse |
| 13150 | * and match method are related to the corresponding fetch methods. This |
| 13151 | * is very particular ACL declaration mode. |
| 13152 | */ |
| 13153 | { "http_auth_group", NULL, PAT_MATCH_STR, NULL, pat_idx_list_str, pat_del_list_ptr, NULL, pat_match_auth }, |
| 13154 | { "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] | 13155 | |
Thierry FOURNIER | c5a4e98 | 2014-03-05 16:07:08 +0100 | [diff] [blame] | 13156 | { "path", "path", PAT_MATCH_STR }, |
| 13157 | { "path_beg", "path", PAT_MATCH_BEG }, |
| 13158 | { "path_dir", "path", PAT_MATCH_DIR }, |
| 13159 | { "path_dom", "path", PAT_MATCH_DOM }, |
| 13160 | { "path_end", "path", PAT_MATCH_END }, |
| 13161 | { "path_len", "path", PAT_MATCH_LEN }, |
| 13162 | { "path_reg", "path", PAT_MATCH_REG }, |
| 13163 | { "path_sub", "path", PAT_MATCH_SUB }, |
Willy Tarreau | 25c1ebc | 2012-04-25 16:21:44 +0200 | [diff] [blame] | 13164 | |
Thierry FOURNIER | c5a4e98 | 2014-03-05 16:07:08 +0100 | [diff] [blame] | 13165 | { "req_ver", "req.ver", PAT_MATCH_STR }, |
| 13166 | { "resp_ver", "res.ver", PAT_MATCH_STR }, |
Willy Tarreau | 25c1ebc | 2012-04-25 16:21:44 +0200 | [diff] [blame] | 13167 | |
Thierry FOURNIER | c5a4e98 | 2014-03-05 16:07:08 +0100 | [diff] [blame] | 13168 | { "scook", "res.cook", PAT_MATCH_STR }, |
| 13169 | { "scook_beg", "res.cook", PAT_MATCH_BEG }, |
| 13170 | { "scook_dir", "res.cook", PAT_MATCH_DIR }, |
| 13171 | { "scook_dom", "res.cook", PAT_MATCH_DOM }, |
| 13172 | { "scook_end", "res.cook", PAT_MATCH_END }, |
| 13173 | { "scook_len", "res.cook", PAT_MATCH_LEN }, |
| 13174 | { "scook_reg", "res.cook", PAT_MATCH_REG }, |
| 13175 | { "scook_sub", "res.cook", PAT_MATCH_SUB }, |
Willy Tarreau | 25c1ebc | 2012-04-25 16:21:44 +0200 | [diff] [blame] | 13176 | |
Thierry FOURNIER | c5a4e98 | 2014-03-05 16:07:08 +0100 | [diff] [blame] | 13177 | { "shdr", "res.hdr", PAT_MATCH_STR }, |
| 13178 | { "shdr_beg", "res.hdr", PAT_MATCH_BEG }, |
| 13179 | { "shdr_dir", "res.hdr", PAT_MATCH_DIR }, |
| 13180 | { "shdr_dom", "res.hdr", PAT_MATCH_DOM }, |
| 13181 | { "shdr_end", "res.hdr", PAT_MATCH_END }, |
| 13182 | { "shdr_len", "res.hdr", PAT_MATCH_LEN }, |
| 13183 | { "shdr_reg", "res.hdr", PAT_MATCH_REG }, |
| 13184 | { "shdr_sub", "res.hdr", PAT_MATCH_SUB }, |
Willy Tarreau | 25c1ebc | 2012-04-25 16:21:44 +0200 | [diff] [blame] | 13185 | |
Thierry FOURNIER | c5a4e98 | 2014-03-05 16:07:08 +0100 | [diff] [blame] | 13186 | { "url", "url", PAT_MATCH_STR }, |
| 13187 | { "url_beg", "url", PAT_MATCH_BEG }, |
| 13188 | { "url_dir", "url", PAT_MATCH_DIR }, |
| 13189 | { "url_dom", "url", PAT_MATCH_DOM }, |
| 13190 | { "url_end", "url", PAT_MATCH_END }, |
| 13191 | { "url_len", "url", PAT_MATCH_LEN }, |
| 13192 | { "url_reg", "url", PAT_MATCH_REG }, |
| 13193 | { "url_sub", "url", PAT_MATCH_SUB }, |
Willy Tarreau | 25c1ebc | 2012-04-25 16:21:44 +0200 | [diff] [blame] | 13194 | |
Thierry FOURNIER | c5a4e98 | 2014-03-05 16:07:08 +0100 | [diff] [blame] | 13195 | { "urlp", "urlp", PAT_MATCH_STR }, |
| 13196 | { "urlp_beg", "urlp", PAT_MATCH_BEG }, |
| 13197 | { "urlp_dir", "urlp", PAT_MATCH_DIR }, |
| 13198 | { "urlp_dom", "urlp", PAT_MATCH_DOM }, |
| 13199 | { "urlp_end", "urlp", PAT_MATCH_END }, |
| 13200 | { "urlp_len", "urlp", PAT_MATCH_LEN }, |
| 13201 | { "urlp_reg", "urlp", PAT_MATCH_REG }, |
| 13202 | { "urlp_sub", "urlp", PAT_MATCH_SUB }, |
Willy Tarreau | 25c1ebc | 2012-04-25 16:21:44 +0200 | [diff] [blame] | 13203 | |
Willy Tarreau | 8ed669b | 2013-01-11 15:49:37 +0100 | [diff] [blame] | 13204 | { /* END */ }, |
Willy Tarreau | 25c1ebc | 2012-04-25 16:21:44 +0200 | [diff] [blame] | 13205 | }}; |
| 13206 | |
| 13207 | /************************************************************************/ |
| 13208 | /* All supported pattern keywords must be declared here. */ |
Willy Tarreau | 4a56897 | 2010-05-12 08:08:50 +0200 | [diff] [blame] | 13209 | /************************************************************************/ |
| 13210 | /* Note: must not be declared <const> as its list will be overwritten */ |
Willy Tarreau | dc13c11 | 2013-06-21 23:16:39 +0200 | [diff] [blame] | 13211 | static struct sample_fetch_kw_list sample_fetch_keywords = {ILH, { |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 13212 | { "base", smp_fetch_base, 0, NULL, SMP_T_STR, SMP_USE_HRQHV }, |
Thierry FOURNIER | 07ee64e | 2015-07-06 23:43:03 +0200 | [diff] [blame] | 13213 | { "base32", smp_fetch_base32, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV }, |
Willy Tarreau | 409bcde | 2013-01-08 00:31:00 +0100 | [diff] [blame] | 13214 | { "base32+src", smp_fetch_base32_src, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV }, |
| 13215 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 13216 | /* capture are allocated and are permanent in the stream */ |
Thierry FOURNIER | bf65cd4 | 2015-07-20 17:45:02 +0200 | [diff] [blame] | 13217 | { "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] | 13218 | |
| 13219 | /* retrieve these captures from the HTTP logs */ |
Thierry FOURNIER | bf65cd4 | 2015-07-20 17:45:02 +0200 | [diff] [blame] | 13220 | { "capture.req.method", smp_fetch_capture_req_method, 0, NULL, SMP_T_STR, SMP_USE_HRQHP }, |
| 13221 | { "capture.req.uri", smp_fetch_capture_req_uri, 0, NULL, SMP_T_STR, SMP_USE_HRQHP }, |
| 13222 | { "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] | 13223 | |
Thierry FOURNIER | bf65cd4 | 2015-07-20 17:45:02 +0200 | [diff] [blame] | 13224 | { "capture.res.hdr", smp_fetch_capture_header_res, ARG1(1,SINT), NULL, SMP_T_STR, SMP_USE_HRSHP }, |
| 13225 | { "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] | 13226 | |
Willy Tarreau | 409bcde | 2013-01-08 00:31:00 +0100 | [diff] [blame] | 13227 | /* cookie is valid in both directions (eg: for "stick ...") but cook* |
| 13228 | * are only here to match the ACL's name, are request-only and are used |
| 13229 | * for ACL compatibility only. |
| 13230 | */ |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 13231 | { "cook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRQHV }, |
| 13232 | { "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] | 13233 | { "cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV }, |
| 13234 | { "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] | 13235 | |
| 13236 | /* hdr is valid in both directions (eg: for "stick ...") but hdr_* are |
| 13237 | * only here to match the ACL's name, are request-only and are used for |
| 13238 | * ACL compatibility only. |
| 13239 | */ |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 13240 | { "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] | 13241 | { "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] | 13242 | { "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] | 13243 | { "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] | 13244 | |
Willy Tarreau | 0a0daec | 2013-04-02 22:44:58 +0200 | [diff] [blame] | 13245 | { "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] | 13246 | { "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] | 13247 | { "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] | 13248 | { "method", smp_fetch_meth, 0, NULL, SMP_T_METH, SMP_USE_HRQHP }, |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 13249 | { "path", smp_fetch_path, 0, NULL, SMP_T_STR, SMP_USE_HRQHV }, |
Willy Tarreau | 49ad95c | 2015-01-19 15:06:26 +0100 | [diff] [blame] | 13250 | { "query", smp_fetch_query, 0, NULL, SMP_T_STR, SMP_USE_HRQHV }, |
Willy Tarreau | 18ed256 | 2013-01-14 15:56:36 +0100 | [diff] [blame] | 13251 | |
| 13252 | /* HTTP protocol on the request path */ |
| 13253 | { "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] | 13254 | { "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] | 13255 | |
| 13256 | /* HTTP version on the request path */ |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 13257 | { "req.ver", smp_fetch_rqver, 0, NULL, SMP_T_STR, SMP_USE_HRQHV }, |
| 13258 | { "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] | 13259 | |
Willy Tarreau | a5910cc | 2015-05-02 00:46:08 +0200 | [diff] [blame] | 13260 | { "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] | 13261 | { "req.body_len", smp_fetch_body_len, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV }, |
| 13262 | { "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] | 13263 | { "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] | 13264 | |
Willy Tarreau | 18ed256 | 2013-01-14 15:56:36 +0100 | [diff] [blame] | 13265 | /* HTTP version on the response path */ |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 13266 | { "res.ver", smp_fetch_stver, 0, NULL, SMP_T_STR, SMP_USE_HRSHV }, |
| 13267 | { "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] | 13268 | |
Willy Tarreau | 18ed256 | 2013-01-14 15:56:36 +0100 | [diff] [blame] | 13269 | /* 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] | 13270 | { "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] | 13271 | { "req.cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV }, |
| 13272 | { "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] | 13273 | |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 13274 | { "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] | 13275 | { "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] | 13276 | { "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] | 13277 | { "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] | 13278 | { "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] | 13279 | { "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] | 13280 | { "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] | 13281 | |
| 13282 | /* 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] | 13283 | { "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] | 13284 | { "res.cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV }, |
| 13285 | { "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] | 13286 | |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 13287 | { "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] | 13288 | { "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] | 13289 | { "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] | 13290 | { "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] | 13291 | { "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] | 13292 | { "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] | 13293 | { "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] | 13294 | |
Willy Tarreau | 409bcde | 2013-01-08 00:31:00 +0100 | [diff] [blame] | 13295 | /* 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] | 13296 | { "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] | 13297 | { "scook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV }, |
| 13298 | { "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] | 13299 | { "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] | 13300 | |
| 13301 | /* 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] | 13302 | { "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] | 13303 | { "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] | 13304 | { "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] | 13305 | { "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] | 13306 | |
Thierry FOURNIER | 07ee64e | 2015-07-06 23:43:03 +0200 | [diff] [blame] | 13307 | { "status", smp_fetch_stcode, 0, NULL, SMP_T_SINT, SMP_USE_HRSHP }, |
Thierry Fournier | 0e00dca | 2016-04-07 15:47:40 +0200 | [diff] [blame] | 13308 | { "unique-id", smp_fetch_uniqueid, 0, NULL, SMP_T_STR, SMP_SRC_L4SRV }, |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 13309 | { "url", smp_fetch_url, 0, NULL, SMP_T_STR, SMP_USE_HRQHV }, |
Thierry FOURNIER | 07ee64e | 2015-07-06 23:43:03 +0200 | [diff] [blame] | 13310 | { "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] | 13311 | { "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] | 13312 | { "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] | 13313 | { "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] | 13314 | { "url_param", smp_fetch_url_param, ARG2(0,STR,STR), NULL, SMP_T_STR, SMP_USE_HRQHV }, |
| 13315 | { "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] | 13316 | { "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] | 13317 | { /* END */ }, |
Willy Tarreau | 4a56897 | 2010-05-12 08:08:50 +0200 | [diff] [blame] | 13318 | }}; |
| 13319 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 13320 | |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 13321 | /************************************************************************/ |
| 13322 | /* All supported converter keywords must be declared here. */ |
| 13323 | /************************************************************************/ |
Willy Tarreau | 276fae9 | 2013-07-25 14:36:01 +0200 | [diff] [blame] | 13324 | /* Note: must not be declared <const> as its list will be overwritten */ |
| 13325 | static struct sample_conv_kw_list sample_conv_kws = {ILH, { |
Thierry FOURNIER | 07ee64e | 2015-07-06 23:43:03 +0200 | [diff] [blame] | 13326 | { "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] | 13327 | { "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] | 13328 | { "capture-req", smp_conv_req_capture, ARG1(1,SINT), NULL, SMP_T_STR, SMP_T_STR}, |
| 13329 | { "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] | 13330 | { "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] | 13331 | { NULL, NULL, 0, 0, 0 }, |
| 13332 | }}; |
| 13333 | |
Thierry FOURNIER | 35ab275 | 2015-05-28 13:22:03 +0200 | [diff] [blame] | 13334 | |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 13335 | /************************************************************************/ |
| 13336 | /* All supported http-request action keywords must be declared here. */ |
| 13337 | /************************************************************************/ |
Thierry FOURNIER | 36481b8 | 2015-08-19 09:01:53 +0200 | [diff] [blame] | 13338 | struct action_kw_list http_req_actions = { |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 13339 | .kw = { |
Willy Tarreau | a9083d0 | 2015-05-08 15:27:59 +0200 | [diff] [blame] | 13340 | { "capture", parse_http_req_capture }, |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 13341 | { "set-method", parse_set_req_line }, |
| 13342 | { "set-path", parse_set_req_line }, |
| 13343 | { "set-query", parse_set_req_line }, |
| 13344 | { "set-uri", parse_set_req_line }, |
Willy Tarreau | cb703b0 | 2015-04-03 09:52:01 +0200 | [diff] [blame] | 13345 | { NULL, NULL } |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 13346 | } |
| 13347 | }; |
| 13348 | |
Thierry FOURNIER | 36481b8 | 2015-08-19 09:01:53 +0200 | [diff] [blame] | 13349 | struct action_kw_list http_res_actions = { |
Thierry FOURNIER | e80fada | 2015-05-26 18:06:31 +0200 | [diff] [blame] | 13350 | .kw = { |
| 13351 | { "capture", parse_http_res_capture }, |
Thierry FOURNIER | 35d70ef | 2015-08-26 16:21:56 +0200 | [diff] [blame] | 13352 | { "set-status", parse_http_set_status }, |
Thierry FOURNIER | e80fada | 2015-05-26 18:06:31 +0200 | [diff] [blame] | 13353 | { NULL, NULL } |
| 13354 | } |
| 13355 | }; |
| 13356 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 13357 | __attribute__((constructor)) |
| 13358 | static void __http_protocol_init(void) |
| 13359 | { |
| 13360 | acl_register_keywords(&acl_kws); |
Willy Tarreau | 1278578 | 2012-04-27 21:37:17 +0200 | [diff] [blame] | 13361 | sample_register_fetches(&sample_fetch_keywords); |
Willy Tarreau | 276fae9 | 2013-07-25 14:36:01 +0200 | [diff] [blame] | 13362 | sample_register_convs(&sample_conv_kws); |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 13363 | http_req_keywords_register(&http_req_actions); |
Thierry FOURNIER | e80fada | 2015-05-26 18:06:31 +0200 | [diff] [blame] | 13364 | http_res_keywords_register(&http_res_actions); |
Willy Tarreau | 12207b3 | 2016-11-22 19:48:51 +0100 | [diff] [blame] | 13365 | cli_register_kw(&cli_kws); |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 13366 | } |
| 13367 | |
| 13368 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 13369 | /* |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 13370 | * Local variables: |
| 13371 | * c-indent-level: 8 |
| 13372 | * c-basic-offset: 8 |
| 13373 | * End: |
| 13374 | */ |