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] = |
Jarno Huuskonen | 16ad94a | 2017-01-09 14:17:10 +0200 | [diff] [blame] | 199 | "HTTP/1.0 500 Internal Server Error\r\n" |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 200 | "Cache-Control: no-cache\r\n" |
| 201 | "Connection: close\r\n" |
| 202 | "Content-Type: text/html\r\n" |
| 203 | "\r\n" |
Jarno Huuskonen | 16ad94a | 2017-01-09 14:17:10 +0200 | [diff] [blame] | 204 | "<html><body><h1>500 Internal Server Error</h1>\nAn internal server error occured.\n</body></html>\n", |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 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"; |
Jarno Huuskonen | 59af2df | 2016-12-28 10:49:01 +0200 | [diff] [blame] | 346 | case 502: return "Bad Gateway or Proxy Error"; |
Thierry FOURNIER | 35d70ef | 2015-08-26 16:21:56 +0200 | [diff] [blame] | 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 | |
Jarno Huuskonen | 9e6906b | 2017-03-06 14:21:49 +0200 | [diff] [blame] | 369 | /* This function returns HTTP_ERR_<num> (enum) matching http status code. |
| 370 | * Returned value should match codes from http_err_codes. |
| 371 | */ |
| 372 | static const int http_get_status_idx(unsigned int status) |
| 373 | { |
| 374 | switch (status) { |
| 375 | case 200: return HTTP_ERR_200; |
| 376 | case 400: return HTTP_ERR_400; |
| 377 | case 403: return HTTP_ERR_403; |
| 378 | case 405: return HTTP_ERR_405; |
| 379 | case 408: return HTTP_ERR_408; |
| 380 | case 429: return HTTP_ERR_429; |
| 381 | case 500: return HTTP_ERR_500; |
| 382 | case 502: return HTTP_ERR_502; |
| 383 | case 503: return HTTP_ERR_503; |
| 384 | case 504: return HTTP_ERR_504; |
| 385 | default: return HTTP_ERR_500; |
| 386 | } |
| 387 | } |
| 388 | |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 389 | void init_proto_http() |
| 390 | { |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 391 | int i; |
| 392 | char *tmp; |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 393 | int msg; |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 394 | |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 395 | for (msg = 0; msg < HTTP_ERR_SIZE; msg++) { |
| 396 | if (!http_err_msgs[msg]) { |
| 397 | Alert("Internal error: no message defined for HTTP return code %d. Aborting.\n", msg); |
| 398 | abort(); |
| 399 | } |
| 400 | |
| 401 | http_err_chunks[msg].str = (char *)http_err_msgs[msg]; |
| 402 | http_err_chunks[msg].len = strlen(http_err_msgs[msg]); |
| 403 | } |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 404 | |
| 405 | /* initialize the log header encoding map : '{|}"#' should be encoded with |
| 406 | * '#' as prefix, as well as non-printable characters ( <32 or >= 127 ). |
| 407 | * URL encoding only requires '"', '#' to be encoded as well as non- |
| 408 | * printable characters above. |
| 409 | */ |
| 410 | memset(hdr_encode_map, 0, sizeof(hdr_encode_map)); |
| 411 | memset(url_encode_map, 0, sizeof(url_encode_map)); |
Thierry FOURNIER | d048d8b | 2014-03-13 16:46:18 +0100 | [diff] [blame] | 412 | memset(http_encode_map, 0, sizeof(url_encode_map)); |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 413 | for (i = 0; i < 32; i++) { |
| 414 | FD_SET(i, hdr_encode_map); |
| 415 | FD_SET(i, url_encode_map); |
| 416 | } |
| 417 | for (i = 127; i < 256; i++) { |
| 418 | FD_SET(i, hdr_encode_map); |
| 419 | FD_SET(i, url_encode_map); |
| 420 | } |
| 421 | |
| 422 | tmp = "\"#{|}"; |
| 423 | while (*tmp) { |
| 424 | FD_SET(*tmp, hdr_encode_map); |
| 425 | tmp++; |
| 426 | } |
| 427 | |
| 428 | tmp = "\"#"; |
| 429 | while (*tmp) { |
| 430 | FD_SET(*tmp, url_encode_map); |
| 431 | tmp++; |
| 432 | } |
Willy Tarreau | 332f8bf | 2007-05-13 21:36:56 +0200 | [diff] [blame] | 433 | |
Thierry FOURNIER | d048d8b | 2014-03-13 16:46:18 +0100 | [diff] [blame] | 434 | /* initialize the http header encoding map. The draft httpbis define the |
| 435 | * header content as: |
| 436 | * |
| 437 | * HTTP-message = start-line |
| 438 | * *( header-field CRLF ) |
| 439 | * CRLF |
| 440 | * [ message-body ] |
| 441 | * header-field = field-name ":" OWS field-value OWS |
| 442 | * field-value = *( field-content / obs-fold ) |
| 443 | * field-content = field-vchar [ 1*( SP / HTAB ) field-vchar ] |
| 444 | * obs-fold = CRLF 1*( SP / HTAB ) |
| 445 | * field-vchar = VCHAR / obs-text |
| 446 | * VCHAR = %x21-7E |
| 447 | * obs-text = %x80-FF |
| 448 | * |
| 449 | * All the chars are encoded except "VCHAR", "obs-text", SP and HTAB. |
| 450 | * The encoded chars are form 0x00 to 0x08, 0x0a to 0x1f and 0x7f. The |
| 451 | * "obs-fold" is volontary forgotten because haproxy remove this. |
| 452 | */ |
| 453 | memset(http_encode_map, 0, sizeof(http_encode_map)); |
| 454 | for (i = 0x00; i <= 0x08; i++) |
| 455 | FD_SET(i, http_encode_map); |
| 456 | for (i = 0x0a; i <= 0x1f; i++) |
| 457 | FD_SET(i, http_encode_map); |
| 458 | FD_SET(0x7f, http_encode_map); |
| 459 | |
Willy Tarreau | 332f8bf | 2007-05-13 21:36:56 +0200 | [diff] [blame] | 460 | /* memory allocations */ |
Willy Tarreau | 63986c7 | 2015-04-03 22:55:33 +0200 | [diff] [blame] | 461 | pool2_http_txn = create_pool("http_txn", sizeof(struct http_txn), MEM_F_SHARED); |
William Lallemand | a73203e | 2012-03-12 12:48:57 +0100 | [diff] [blame] | 462 | pool2_uniqueid = create_pool("uniqueid", UNIQUEID_LEN, MEM_F_SHARED); |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 463 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 464 | |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 465 | /* |
| 466 | * We have 26 list of methods (1 per first letter), each of which can have |
| 467 | * up to 3 entries (2 valid, 1 null). |
| 468 | */ |
| 469 | struct http_method_desc { |
Willy Tarreau | c8987b3 | 2013-12-06 23:43:17 +0100 | [diff] [blame] | 470 | enum http_meth_t meth; |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 471 | int len; |
| 472 | const char text[8]; |
| 473 | }; |
| 474 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 475 | const struct http_method_desc http_methods[26][3] = { |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 476 | ['C' - 'A'] = { |
| 477 | [0] = { .meth = HTTP_METH_CONNECT , .len=7, .text="CONNECT" }, |
| 478 | }, |
| 479 | ['D' - 'A'] = { |
| 480 | [0] = { .meth = HTTP_METH_DELETE , .len=6, .text="DELETE" }, |
| 481 | }, |
| 482 | ['G' - 'A'] = { |
| 483 | [0] = { .meth = HTTP_METH_GET , .len=3, .text="GET" }, |
| 484 | }, |
| 485 | ['H' - 'A'] = { |
| 486 | [0] = { .meth = HTTP_METH_HEAD , .len=4, .text="HEAD" }, |
| 487 | }, |
Christopher Faulet | d57ad64 | 2015-07-31 14:26:57 +0200 | [diff] [blame] | 488 | ['O' - 'A'] = { |
| 489 | [0] = { .meth = HTTP_METH_OPTIONS , .len=7, .text="OPTIONS" }, |
| 490 | }, |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 491 | ['P' - 'A'] = { |
| 492 | [0] = { .meth = HTTP_METH_POST , .len=4, .text="POST" }, |
| 493 | [1] = { .meth = HTTP_METH_PUT , .len=3, .text="PUT" }, |
| 494 | }, |
| 495 | ['T' - 'A'] = { |
| 496 | [0] = { .meth = HTTP_METH_TRACE , .len=5, .text="TRACE" }, |
| 497 | }, |
| 498 | /* rest is empty like this : |
Willy Tarreau | b7ce424 | 2015-09-03 17:15:21 +0200 | [diff] [blame] | 499 | * [0] = { .meth = HTTP_METH_OTHER , .len=0, .text="" }, |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 500 | */ |
| 501 | }; |
| 502 | |
Thierry FOURNIER | d437314 | 2013-12-17 01:10:10 +0100 | [diff] [blame] | 503 | const struct http_method_name http_known_methods[HTTP_METH_OTHER] = { |
Thierry FOURNIER | d437314 | 2013-12-17 01:10:10 +0100 | [diff] [blame] | 504 | [HTTP_METH_OPTIONS] = { "OPTIONS", 7 }, |
| 505 | [HTTP_METH_GET] = { "GET", 3 }, |
| 506 | [HTTP_METH_HEAD] = { "HEAD", 4 }, |
| 507 | [HTTP_METH_POST] = { "POST", 4 }, |
| 508 | [HTTP_METH_PUT] = { "PUT", 3 }, |
| 509 | [HTTP_METH_DELETE] = { "DELETE", 6 }, |
| 510 | [HTTP_METH_TRACE] = { "TRACE", 5 }, |
| 511 | [HTTP_METH_CONNECT] = { "CONNECT", 7 }, |
| 512 | }; |
| 513 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 514 | /* 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] | 515 | * 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] | 516 | * RFC2616/RFC5234/RFC7230 for those chars. A token is any ASCII char that is |
| 517 | * neither a separator nor a CTL char. An http ver_token is any ASCII which can |
| 518 | * be found in an HTTP version, which includes 'H', 'T', 'P', '/', '.' and any |
| 519 | * digit. Note: please do not overwrite values in assignment since gcc-2.95 |
| 520 | * will not handle them correctly. It's worth noting that chars 128..255 are |
| 521 | * nothing, not even control chars. |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 522 | */ |
Willy Tarreau | 2235b26 | 2016-11-05 15:50:20 +0100 | [diff] [blame] | 523 | const unsigned char http_char_classes[256] = { |
| 524 | [ 0] = HTTP_FLG_CTL, |
| 525 | [ 1] = HTTP_FLG_CTL, |
| 526 | [ 2] = HTTP_FLG_CTL, |
| 527 | [ 3] = HTTP_FLG_CTL, |
| 528 | [ 4] = HTTP_FLG_CTL, |
| 529 | [ 5] = HTTP_FLG_CTL, |
| 530 | [ 6] = HTTP_FLG_CTL, |
| 531 | [ 7] = HTTP_FLG_CTL, |
| 532 | [ 8] = HTTP_FLG_CTL, |
| 533 | [ 9] = HTTP_FLG_SPHT | HTTP_FLG_LWS | HTTP_FLG_SEP | HTTP_FLG_CTL, |
| 534 | [ 10] = HTTP_FLG_CRLF | HTTP_FLG_LWS | HTTP_FLG_CTL, |
| 535 | [ 11] = HTTP_FLG_CTL, |
| 536 | [ 12] = HTTP_FLG_CTL, |
| 537 | [ 13] = HTTP_FLG_CRLF | HTTP_FLG_LWS | HTTP_FLG_CTL, |
| 538 | [ 14] = HTTP_FLG_CTL, |
| 539 | [ 15] = HTTP_FLG_CTL, |
| 540 | [ 16] = HTTP_FLG_CTL, |
| 541 | [ 17] = HTTP_FLG_CTL, |
| 542 | [ 18] = HTTP_FLG_CTL, |
| 543 | [ 19] = HTTP_FLG_CTL, |
| 544 | [ 20] = HTTP_FLG_CTL, |
| 545 | [ 21] = HTTP_FLG_CTL, |
| 546 | [ 22] = HTTP_FLG_CTL, |
| 547 | [ 23] = HTTP_FLG_CTL, |
| 548 | [ 24] = HTTP_FLG_CTL, |
| 549 | [ 25] = HTTP_FLG_CTL, |
| 550 | [ 26] = HTTP_FLG_CTL, |
| 551 | [ 27] = HTTP_FLG_CTL, |
| 552 | [ 28] = HTTP_FLG_CTL, |
| 553 | [ 29] = HTTP_FLG_CTL, |
| 554 | [ 30] = HTTP_FLG_CTL, |
| 555 | [ 31] = HTTP_FLG_CTL, |
| 556 | [' '] = HTTP_FLG_SPHT | HTTP_FLG_LWS | HTTP_FLG_SEP, |
| 557 | ['!'] = HTTP_FLG_TOK, |
| 558 | ['"'] = HTTP_FLG_SEP, |
| 559 | ['#'] = HTTP_FLG_TOK, |
| 560 | ['$'] = HTTP_FLG_TOK, |
| 561 | ['%'] = HTTP_FLG_TOK, |
| 562 | ['&'] = HTTP_FLG_TOK, |
| 563 | [ 39] = HTTP_FLG_TOK, |
| 564 | ['('] = HTTP_FLG_SEP, |
| 565 | [')'] = HTTP_FLG_SEP, |
| 566 | ['*'] = HTTP_FLG_TOK, |
| 567 | ['+'] = HTTP_FLG_TOK, |
| 568 | [','] = HTTP_FLG_SEP, |
| 569 | ['-'] = HTTP_FLG_TOK, |
| 570 | ['.'] = HTTP_FLG_TOK | HTTP_FLG_VER, |
| 571 | ['/'] = HTTP_FLG_SEP | HTTP_FLG_VER, |
| 572 | ['0'] = HTTP_FLG_TOK | HTTP_FLG_VER, |
| 573 | ['1'] = HTTP_FLG_TOK | HTTP_FLG_VER, |
| 574 | ['2'] = HTTP_FLG_TOK | HTTP_FLG_VER, |
| 575 | ['3'] = HTTP_FLG_TOK | HTTP_FLG_VER, |
| 576 | ['4'] = HTTP_FLG_TOK | HTTP_FLG_VER, |
| 577 | ['5'] = HTTP_FLG_TOK | HTTP_FLG_VER, |
| 578 | ['6'] = HTTP_FLG_TOK | HTTP_FLG_VER, |
| 579 | ['7'] = HTTP_FLG_TOK | HTTP_FLG_VER, |
| 580 | ['8'] = HTTP_FLG_TOK | HTTP_FLG_VER, |
| 581 | ['9'] = HTTP_FLG_TOK | HTTP_FLG_VER, |
| 582 | [':'] = HTTP_FLG_SEP, |
| 583 | [';'] = HTTP_FLG_SEP, |
| 584 | ['<'] = HTTP_FLG_SEP, |
| 585 | ['='] = HTTP_FLG_SEP, |
| 586 | ['>'] = HTTP_FLG_SEP, |
| 587 | ['?'] = HTTP_FLG_SEP, |
| 588 | ['@'] = HTTP_FLG_SEP, |
| 589 | ['A'] = HTTP_FLG_TOK, |
| 590 | ['B'] = HTTP_FLG_TOK, |
| 591 | ['C'] = HTTP_FLG_TOK, |
| 592 | ['D'] = HTTP_FLG_TOK, |
| 593 | ['E'] = HTTP_FLG_TOK, |
| 594 | ['F'] = HTTP_FLG_TOK, |
| 595 | ['G'] = HTTP_FLG_TOK, |
| 596 | ['H'] = HTTP_FLG_TOK | HTTP_FLG_VER, |
| 597 | ['I'] = HTTP_FLG_TOK, |
| 598 | ['J'] = HTTP_FLG_TOK, |
| 599 | ['K'] = HTTP_FLG_TOK, |
| 600 | ['L'] = HTTP_FLG_TOK, |
| 601 | ['M'] = HTTP_FLG_TOK, |
| 602 | ['N'] = HTTP_FLG_TOK, |
| 603 | ['O'] = HTTP_FLG_TOK, |
| 604 | ['P'] = HTTP_FLG_TOK | HTTP_FLG_VER, |
| 605 | ['Q'] = HTTP_FLG_TOK, |
| 606 | ['R'] = HTTP_FLG_TOK | HTTP_FLG_VER, |
| 607 | ['S'] = HTTP_FLG_TOK | HTTP_FLG_VER, |
| 608 | ['T'] = HTTP_FLG_TOK | HTTP_FLG_VER, |
| 609 | ['U'] = HTTP_FLG_TOK, |
| 610 | ['V'] = HTTP_FLG_TOK, |
| 611 | ['W'] = HTTP_FLG_TOK, |
| 612 | ['X'] = HTTP_FLG_TOK, |
| 613 | ['Y'] = HTTP_FLG_TOK, |
| 614 | ['Z'] = HTTP_FLG_TOK, |
| 615 | ['['] = HTTP_FLG_SEP, |
| 616 | [ 92] = HTTP_FLG_SEP, |
| 617 | [']'] = HTTP_FLG_SEP, |
| 618 | ['^'] = HTTP_FLG_TOK, |
| 619 | ['_'] = HTTP_FLG_TOK, |
| 620 | ['`'] = HTTP_FLG_TOK, |
| 621 | ['a'] = HTTP_FLG_TOK, |
| 622 | ['b'] = HTTP_FLG_TOK, |
| 623 | ['c'] = HTTP_FLG_TOK, |
| 624 | ['d'] = HTTP_FLG_TOK, |
| 625 | ['e'] = HTTP_FLG_TOK, |
| 626 | ['f'] = HTTP_FLG_TOK, |
| 627 | ['g'] = HTTP_FLG_TOK, |
| 628 | ['h'] = HTTP_FLG_TOK, |
| 629 | ['i'] = HTTP_FLG_TOK, |
| 630 | ['j'] = HTTP_FLG_TOK, |
| 631 | ['k'] = HTTP_FLG_TOK, |
| 632 | ['l'] = HTTP_FLG_TOK, |
| 633 | ['m'] = HTTP_FLG_TOK, |
| 634 | ['n'] = HTTP_FLG_TOK, |
| 635 | ['o'] = HTTP_FLG_TOK, |
| 636 | ['p'] = HTTP_FLG_TOK, |
| 637 | ['q'] = HTTP_FLG_TOK, |
| 638 | ['r'] = HTTP_FLG_TOK, |
| 639 | ['s'] = HTTP_FLG_TOK, |
| 640 | ['t'] = HTTP_FLG_TOK, |
| 641 | ['u'] = HTTP_FLG_TOK, |
| 642 | ['v'] = HTTP_FLG_TOK, |
| 643 | ['w'] = HTTP_FLG_TOK, |
| 644 | ['x'] = HTTP_FLG_TOK, |
| 645 | ['y'] = HTTP_FLG_TOK, |
| 646 | ['z'] = HTTP_FLG_TOK, |
| 647 | ['{'] = HTTP_FLG_SEP, |
| 648 | ['|'] = HTTP_FLG_TOK, |
| 649 | ['}'] = HTTP_FLG_SEP, |
| 650 | ['~'] = HTTP_FLG_TOK, |
| 651 | [127] = HTTP_FLG_CTL, |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 652 | }; |
| 653 | |
Willy Tarreau | 4af6f3a | 2007-03-18 22:36:26 +0100 | [diff] [blame] | 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. Text length is measured first, so it cannot be NULL. |
Willy Tarreau | 4af6f3a | 2007-03-18 22:36:26 +0100 | [diff] [blame] | 657 | * The header is also automatically added to the index <hdr_idx>, and the end |
| 658 | * of headers is automatically adjusted. The number of bytes added is returned |
| 659 | * on success, otherwise <0 is returned indicating an error. |
| 660 | */ |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 661 | 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] | 662 | { |
| 663 | int bytes, len; |
| 664 | |
| 665 | len = strlen(text); |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 666 | 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] | 667 | if (!bytes) |
| 668 | return -1; |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 669 | http_msg_move_end(msg, bytes); |
Willy Tarreau | 4af6f3a | 2007-03-18 22:36:26 +0100 | [diff] [blame] | 670 | return hdr_idx_add(len, 1, hdr_idx, hdr_idx->tail); |
| 671 | } |
| 672 | |
| 673 | /* |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 674 | * Adds a header and its CRLF at the tail of the message's buffer, just before |
| 675 | * the last CRLF. <len> bytes are copied, not counting the CRLF. If <text> is NULL, then |
Willy Tarreau | 4af6f3a | 2007-03-18 22:36:26 +0100 | [diff] [blame] | 676 | * the buffer is only opened and the space reserved, but nothing is copied. |
| 677 | * The header is also automatically added to the index <hdr_idx>, and the end |
| 678 | * of headers is automatically adjusted. The number of bytes added is returned |
| 679 | * on success, otherwise <0 is returned indicating an error. |
| 680 | */ |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 681 | int http_header_add_tail2(struct http_msg *msg, |
| 682 | struct hdr_idx *hdr_idx, const char *text, int len) |
Willy Tarreau | 4af6f3a | 2007-03-18 22:36:26 +0100 | [diff] [blame] | 683 | { |
| 684 | int bytes; |
| 685 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 686 | 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] | 687 | if (!bytes) |
| 688 | return -1; |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 689 | http_msg_move_end(msg, bytes); |
Willy Tarreau | 4af6f3a | 2007-03-18 22:36:26 +0100 | [diff] [blame] | 690 | return hdr_idx_add(len, 1, hdr_idx, hdr_idx->tail); |
| 691 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 692 | |
| 693 | /* |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 694 | * Checks if <hdr> is exactly <name> for <len> chars, and ends with a colon. |
| 695 | * If so, returns the position of the first non-space character relative to |
| 696 | * <hdr>, or <end>-<hdr> if not found before. If no value is found, it tries |
| 697 | * to return a pointer to the place after the first space. Returns 0 if the |
| 698 | * header name does not match. Checks are case-insensitive. |
| 699 | */ |
| 700 | int http_header_match2(const char *hdr, const char *end, |
| 701 | const char *name, int len) |
| 702 | { |
| 703 | const char *val; |
| 704 | |
| 705 | if (hdr + len >= end) |
| 706 | return 0; |
| 707 | if (hdr[len] != ':') |
| 708 | return 0; |
| 709 | if (strncasecmp(hdr, name, len) != 0) |
| 710 | return 0; |
| 711 | val = hdr + len + 1; |
| 712 | while (val < end && HTTP_IS_SPHT(*val)) |
| 713 | val++; |
| 714 | if ((val >= end) && (len + 2 <= end - hdr)) |
| 715 | return len + 2; /* we may replace starting from second space */ |
| 716 | return val - hdr; |
| 717 | } |
| 718 | |
Willy Tarreau | 04ff9f1 | 2013-06-10 18:39:42 +0200 | [diff] [blame] | 719 | /* Find the first or next occurrence of header <name> in message buffer <sol> |
| 720 | * using headers index <idx>, and return it in the <ctx> structure. This |
| 721 | * structure holds everything necessary to use the header and find next |
| 722 | * occurrence. If its <idx> member is 0, the header is searched from the |
| 723 | * beginning. Otherwise, the next occurrence is returned. The function returns |
| 724 | * 1 when it finds a value, and 0 when there is no more. It is very similar to |
| 725 | * http_find_header2() except that it is designed to work with full-line headers |
| 726 | * whose comma is not a delimiter but is part of the syntax. As a special case, |
| 727 | * if ctx->val is NULL when searching for a new values of a header, the current |
| 728 | * header is rescanned. This allows rescanning after a header deletion. |
| 729 | */ |
| 730 | int http_find_full_header2(const char *name, int len, |
| 731 | char *sol, struct hdr_idx *idx, |
| 732 | struct hdr_ctx *ctx) |
| 733 | { |
| 734 | char *eol, *sov; |
| 735 | int cur_idx, old_idx; |
| 736 | |
| 737 | cur_idx = ctx->idx; |
| 738 | if (cur_idx) { |
| 739 | /* We have previously returned a header, let's search another one */ |
| 740 | sol = ctx->line; |
| 741 | eol = sol + idx->v[cur_idx].len; |
| 742 | goto next_hdr; |
| 743 | } |
| 744 | |
| 745 | /* first request for this header */ |
| 746 | sol += hdr_idx_first_pos(idx); |
| 747 | old_idx = 0; |
| 748 | cur_idx = hdr_idx_first_idx(idx); |
| 749 | while (cur_idx) { |
| 750 | eol = sol + idx->v[cur_idx].len; |
| 751 | |
| 752 | if (len == 0) { |
| 753 | /* No argument was passed, we want any header. |
| 754 | * To achieve this, we simply build a fake request. */ |
| 755 | while (sol + len < eol && sol[len] != ':') |
| 756 | len++; |
| 757 | name = sol; |
| 758 | } |
| 759 | |
| 760 | if ((len < eol - sol) && |
| 761 | (sol[len] == ':') && |
| 762 | (strncasecmp(sol, name, len) == 0)) { |
| 763 | ctx->del = len; |
| 764 | sov = sol + len + 1; |
Willy Tarreau | 2235b26 | 2016-11-05 15:50:20 +0100 | [diff] [blame] | 765 | while (sov < eol && HTTP_IS_LWS(*sov)) |
Willy Tarreau | 04ff9f1 | 2013-06-10 18:39:42 +0200 | [diff] [blame] | 766 | sov++; |
| 767 | |
| 768 | ctx->line = sol; |
| 769 | ctx->prev = old_idx; |
| 770 | ctx->idx = cur_idx; |
| 771 | ctx->val = sov - sol; |
| 772 | ctx->tws = 0; |
Willy Tarreau | 2235b26 | 2016-11-05 15:50:20 +0100 | [diff] [blame] | 773 | while (eol > sov && HTTP_IS_LWS(*(eol - 1))) { |
Willy Tarreau | 04ff9f1 | 2013-06-10 18:39:42 +0200 | [diff] [blame] | 774 | eol--; |
| 775 | ctx->tws++; |
| 776 | } |
| 777 | ctx->vlen = eol - sov; |
| 778 | return 1; |
| 779 | } |
| 780 | next_hdr: |
| 781 | sol = eol + idx->v[cur_idx].cr + 1; |
| 782 | old_idx = cur_idx; |
| 783 | cur_idx = idx->v[cur_idx].next; |
| 784 | } |
| 785 | return 0; |
| 786 | } |
| 787 | |
Willy Tarreau | c90dc23 | 2015-02-20 13:51:36 +0100 | [diff] [blame] | 788 | /* Find the first or next header field in message buffer <sol> using headers |
| 789 | * index <idx>, and return it in the <ctx> structure. This structure holds |
| 790 | * everything necessary to use the header and find next occurrence. If its |
| 791 | * <idx> member is 0, the first header is retrieved. Otherwise, the next |
| 792 | * occurrence is returned. The function returns 1 when it finds a value, and |
| 793 | * 0 when there is no more. It is equivalent to http_find_full_header2() with |
| 794 | * no header name. |
| 795 | */ |
| 796 | int http_find_next_header(char *sol, struct hdr_idx *idx, struct hdr_ctx *ctx) |
| 797 | { |
| 798 | char *eol, *sov; |
| 799 | int cur_idx, old_idx; |
| 800 | int len; |
| 801 | |
| 802 | cur_idx = ctx->idx; |
| 803 | if (cur_idx) { |
| 804 | /* We have previously returned a header, let's search another one */ |
| 805 | sol = ctx->line; |
| 806 | eol = sol + idx->v[cur_idx].len; |
| 807 | goto next_hdr; |
| 808 | } |
| 809 | |
| 810 | /* first request for this header */ |
| 811 | sol += hdr_idx_first_pos(idx); |
| 812 | old_idx = 0; |
| 813 | cur_idx = hdr_idx_first_idx(idx); |
| 814 | while (cur_idx) { |
| 815 | eol = sol + idx->v[cur_idx].len; |
| 816 | |
| 817 | len = 0; |
| 818 | while (1) { |
| 819 | if (len >= eol - sol) |
| 820 | goto next_hdr; |
| 821 | if (sol[len] == ':') |
| 822 | break; |
| 823 | len++; |
| 824 | } |
| 825 | |
| 826 | ctx->del = len; |
| 827 | sov = sol + len + 1; |
Willy Tarreau | 2235b26 | 2016-11-05 15:50:20 +0100 | [diff] [blame] | 828 | while (sov < eol && HTTP_IS_LWS(*sov)) |
Willy Tarreau | c90dc23 | 2015-02-20 13:51:36 +0100 | [diff] [blame] | 829 | sov++; |
| 830 | |
| 831 | ctx->line = sol; |
| 832 | ctx->prev = old_idx; |
| 833 | ctx->idx = cur_idx; |
| 834 | ctx->val = sov - sol; |
| 835 | ctx->tws = 0; |
| 836 | |
Willy Tarreau | 2235b26 | 2016-11-05 15:50:20 +0100 | [diff] [blame] | 837 | while (eol > sov && HTTP_IS_LWS(*(eol - 1))) { |
Willy Tarreau | c90dc23 | 2015-02-20 13:51:36 +0100 | [diff] [blame] | 838 | eol--; |
| 839 | ctx->tws++; |
| 840 | } |
| 841 | ctx->vlen = eol - sov; |
| 842 | return 1; |
| 843 | |
| 844 | next_hdr: |
| 845 | sol = eol + idx->v[cur_idx].cr + 1; |
| 846 | old_idx = cur_idx; |
| 847 | cur_idx = idx->v[cur_idx].next; |
| 848 | } |
| 849 | return 0; |
| 850 | } |
| 851 | |
Lukas Tribus | 2395368 | 2017-04-28 13:24:30 +0000 | [diff] [blame] | 852 | /* Find the end of the header value contained between <s> and <e>. See RFC7230, |
| 853 | * par 3.2 for more information. Note that it requires a valid header to return |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 854 | * a valid result. This works for headers defined as comma-separated lists. |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 855 | */ |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 856 | char *find_hdr_value_end(char *s, const char *e) |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 857 | { |
| 858 | int quoted, qdpair; |
| 859 | |
| 860 | quoted = qdpair = 0; |
Willy Tarreau | e6d9c21 | 2016-11-05 18:23:38 +0100 | [diff] [blame] | 861 | |
| 862 | #if defined(__x86_64__) || \ |
| 863 | defined(__i386__) || defined(__i486__) || defined(__i586__) || defined(__i686__) || \ |
| 864 | defined(__ARM_ARCH_7A__) |
| 865 | /* speedup: skip everything not a comma nor a double quote */ |
| 866 | for (; s <= e - sizeof(int); s += sizeof(int)) { |
| 867 | unsigned int c = *(int *)s; // comma |
| 868 | unsigned int q = c; // quote |
| 869 | |
| 870 | c ^= 0x2c2c2c2c; // contains one zero on a comma |
| 871 | q ^= 0x22222222; // contains one zero on a quote |
| 872 | |
| 873 | c = (c - 0x01010101) & ~c; // contains 0x80 below a comma |
| 874 | q = (q - 0x01010101) & ~q; // contains 0x80 below a quote |
| 875 | |
| 876 | if ((c | q) & 0x80808080) |
| 877 | break; // found a comma or a quote |
| 878 | } |
| 879 | #endif |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 880 | for (; s < e; s++) { |
| 881 | if (qdpair) qdpair = 0; |
Willy Tarreau | 0f7f51f | 2010-08-30 11:06:34 +0200 | [diff] [blame] | 882 | else if (quoted) { |
| 883 | if (*s == '\\') qdpair = 1; |
| 884 | else if (*s == '"') quoted = 0; |
| 885 | } |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 886 | else if (*s == '"') quoted = 1; |
| 887 | else if (*s == ',') return s; |
| 888 | } |
| 889 | return s; |
| 890 | } |
| 891 | |
| 892 | /* Find the first or next occurrence of header <name> in message buffer <sol> |
| 893 | * using headers index <idx>, and return it in the <ctx> structure. This |
| 894 | * structure holds everything necessary to use the header and find next |
| 895 | * occurrence. If its <idx> member is 0, the header is searched from the |
| 896 | * beginning. Otherwise, the next occurrence is returned. The function returns |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 897 | * 1 when it finds a value, and 0 when there is no more. It is designed to work |
| 898 | * with headers defined as comma-separated lists. As a special case, if ctx->val |
| 899 | * is NULL when searching for a new values of a header, the current header is |
| 900 | * rescanned. This allows rescanning after a header deletion. |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 901 | */ |
| 902 | int http_find_header2(const char *name, int len, |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 903 | char *sol, struct hdr_idx *idx, |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 904 | struct hdr_ctx *ctx) |
| 905 | { |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 906 | char *eol, *sov; |
| 907 | int cur_idx, old_idx; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 908 | |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 909 | cur_idx = ctx->idx; |
| 910 | if (cur_idx) { |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 911 | /* We have previously returned a value, let's search |
| 912 | * another one on the same line. |
| 913 | */ |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 914 | sol = ctx->line; |
Willy Tarreau | 588bd4f | 2011-09-01 22:22:28 +0200 | [diff] [blame] | 915 | ctx->del = ctx->val + ctx->vlen + ctx->tws; |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 916 | sov = sol + ctx->del; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 917 | eol = sol + idx->v[cur_idx].len; |
| 918 | |
| 919 | if (sov >= eol) |
| 920 | /* no more values in this header */ |
| 921 | goto next_hdr; |
| 922 | |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 923 | /* values remaining for this header, skip the comma but save it |
| 924 | * for later use (eg: for header deletion). |
| 925 | */ |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 926 | sov++; |
Willy Tarreau | 2235b26 | 2016-11-05 15:50:20 +0100 | [diff] [blame] | 927 | while (sov < eol && HTTP_IS_LWS((*sov))) |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 928 | sov++; |
| 929 | |
| 930 | goto return_hdr; |
| 931 | } |
| 932 | |
| 933 | /* first request for this header */ |
| 934 | sol += hdr_idx_first_pos(idx); |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 935 | old_idx = 0; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 936 | cur_idx = hdr_idx_first_idx(idx); |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 937 | while (cur_idx) { |
| 938 | eol = sol + idx->v[cur_idx].len; |
| 939 | |
Willy Tarreau | 1ad7c6d | 2007-06-10 21:42:55 +0200 | [diff] [blame] | 940 | if (len == 0) { |
| 941 | /* No argument was passed, we want any header. |
| 942 | * To achieve this, we simply build a fake request. */ |
| 943 | while (sol + len < eol && sol[len] != ':') |
| 944 | len++; |
| 945 | name = sol; |
| 946 | } |
| 947 | |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 948 | if ((len < eol - sol) && |
| 949 | (sol[len] == ':') && |
| 950 | (strncasecmp(sol, name, len) == 0)) { |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 951 | ctx->del = len; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 952 | sov = sol + len + 1; |
Willy Tarreau | 2235b26 | 2016-11-05 15:50:20 +0100 | [diff] [blame] | 953 | while (sov < eol && HTTP_IS_LWS(*sov)) |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 954 | sov++; |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 955 | |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 956 | ctx->line = sol; |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 957 | ctx->prev = old_idx; |
| 958 | return_hdr: |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 959 | ctx->idx = cur_idx; |
| 960 | ctx->val = sov - sol; |
| 961 | |
| 962 | eol = find_hdr_value_end(sov, eol); |
Willy Tarreau | 588bd4f | 2011-09-01 22:22:28 +0200 | [diff] [blame] | 963 | ctx->tws = 0; |
Willy Tarreau | 2235b26 | 2016-11-05 15:50:20 +0100 | [diff] [blame] | 964 | while (eol > sov && HTTP_IS_LWS(*(eol - 1))) { |
Willy Tarreau | 588bd4f | 2011-09-01 22:22:28 +0200 | [diff] [blame] | 965 | eol--; |
| 966 | ctx->tws++; |
| 967 | } |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 968 | ctx->vlen = eol - sov; |
| 969 | return 1; |
| 970 | } |
| 971 | next_hdr: |
| 972 | sol = eol + idx->v[cur_idx].cr + 1; |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 973 | old_idx = cur_idx; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 974 | cur_idx = idx->v[cur_idx].next; |
| 975 | } |
| 976 | return 0; |
| 977 | } |
| 978 | |
| 979 | int http_find_header(const char *name, |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 980 | char *sol, struct hdr_idx *idx, |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 981 | struct hdr_ctx *ctx) |
| 982 | { |
| 983 | return http_find_header2(name, strlen(name), sol, idx, ctx); |
| 984 | } |
| 985 | |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 986 | /* Remove one value of a header. This only works on a <ctx> returned by one of |
| 987 | * the http_find_header functions. The value is removed, as well as surrounding |
| 988 | * commas if any. If the removed value was alone, the whole header is removed. |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 989 | * 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] | 990 | * message <msg>. The new index is returned. If it is zero, it means there is |
| 991 | * no more header, so any processing may stop. The ctx is always left in a form |
| 992 | * that can be handled by http_find_header2() to find next occurrence. |
| 993 | */ |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 994 | 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] | 995 | { |
| 996 | int cur_idx = ctx->idx; |
| 997 | char *sol = ctx->line; |
| 998 | struct hdr_idx_elem *hdr; |
| 999 | int delta, skip_comma; |
| 1000 | |
| 1001 | if (!cur_idx) |
| 1002 | return 0; |
| 1003 | |
| 1004 | hdr = &idx->v[cur_idx]; |
Willy Tarreau | 588bd4f | 2011-09-01 22:22:28 +0200 | [diff] [blame] | 1005 | if (sol[ctx->del] == ':' && ctx->val + ctx->vlen + ctx->tws == hdr->len) { |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 1006 | /* 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] | 1007 | 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] | 1008 | http_msg_move_end(msg, delta); |
| 1009 | idx->used--; |
| 1010 | hdr->len = 0; /* unused entry */ |
| 1011 | idx->v[ctx->prev].next = idx->v[ctx->idx].next; |
Willy Tarreau | 5c4784f | 2011-02-12 13:07:35 +0100 | [diff] [blame] | 1012 | if (idx->tail == ctx->idx) |
| 1013 | idx->tail = ctx->prev; |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 1014 | ctx->idx = ctx->prev; /* walk back to the end of previous header */ |
Willy Tarreau | 7c1c217 | 2015-01-07 17:23:50 +0100 | [diff] [blame] | 1015 | 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] | 1016 | 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] | 1017 | ctx->tws = ctx->vlen = 0; |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 1018 | return ctx->idx; |
| 1019 | } |
| 1020 | |
| 1021 | /* 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] | 1022 | * ctx->del+1 and ctx->val+ctx->vlen+ctx->tws+1 included. If it is the |
| 1023 | * last entry of the list, we remove the last separator. |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 1024 | */ |
| 1025 | |
Willy Tarreau | 588bd4f | 2011-09-01 22:22:28 +0200 | [diff] [blame] | 1026 | skip_comma = (ctx->val + ctx->vlen + ctx->tws == hdr->len) ? 0 : 1; |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 1027 | delta = buffer_replace2(msg->chn->buf, sol + ctx->del + skip_comma, |
Willy Tarreau | 588bd4f | 2011-09-01 22:22:28 +0200 | [diff] [blame] | 1028 | sol + ctx->val + ctx->vlen + ctx->tws + skip_comma, |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 1029 | NULL, 0); |
| 1030 | hdr->len += delta; |
| 1031 | http_msg_move_end(msg, delta); |
| 1032 | ctx->val = ctx->del; |
Willy Tarreau | 588bd4f | 2011-09-01 22:22:28 +0200 | [diff] [blame] | 1033 | ctx->tws = ctx->vlen = 0; |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 1034 | return ctx->idx; |
| 1035 | } |
| 1036 | |
Willy Tarreau | 2d3d94c | 2008-11-30 20:20:08 +0100 | [diff] [blame] | 1037 | /* This function handles a server error at the stream interface level. The |
| 1038 | * stream interface is assumed to be already in a closed state. An optional |
Willy Tarreau | 2019f95 | 2017-03-14 11:07:31 +0100 | [diff] [blame] | 1039 | * message is copied into the input buffer. |
Willy Tarreau | 2d3d94c | 2008-11-30 20:20:08 +0100 | [diff] [blame] | 1040 | * 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] | 1041 | * in this buffer will be lost. |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1042 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 1043 | static void http_server_error(struct stream *s, struct stream_interface *si, |
Willy Tarreau | 2019f95 | 2017-03-14 11:07:31 +0100 | [diff] [blame] | 1044 | int err, int finst, const struct chunk *msg) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1045 | { |
Willy Tarreau | 2019f95 | 2017-03-14 11:07:31 +0100 | [diff] [blame] | 1046 | FLT_STRM_CB(s, flt_http_reply(s, s->txn->status, msg)); |
Willy Tarreau | 2bb4a96 | 2014-11-28 11:11:05 +0100 | [diff] [blame] | 1047 | channel_auto_read(si_oc(si)); |
| 1048 | channel_abort(si_oc(si)); |
| 1049 | channel_auto_close(si_oc(si)); |
| 1050 | channel_erase(si_oc(si)); |
| 1051 | channel_auto_close(si_ic(si)); |
| 1052 | channel_auto_read(si_ic(si)); |
Jarno Huuskonen | 9e6906b | 2017-03-06 14:21:49 +0200 | [diff] [blame] | 1053 | if (msg) |
Willy Tarreau | 2bb4a96 | 2014-11-28 11:11:05 +0100 | [diff] [blame] | 1054 | bo_inject(si_ic(si), msg->str, msg->len); |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 1055 | if (!(s->flags & SF_ERR_MASK)) |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 1056 | s->flags |= err; |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 1057 | if (!(s->flags & SF_FINST_MASK)) |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 1058 | s->flags |= finst; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1059 | } |
| 1060 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 1061 | /* This function returns the appropriate error location for the given stream |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 1062 | * and message. |
| 1063 | */ |
| 1064 | |
Jarno Huuskonen | 9e6906b | 2017-03-06 14:21:49 +0200 | [diff] [blame] | 1065 | struct chunk *http_error_message(struct stream *s) |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 1066 | { |
Jarno Huuskonen | 9e6906b | 2017-03-06 14:21:49 +0200 | [diff] [blame] | 1067 | const int msgnum = http_get_status_idx(s->txn->status); |
| 1068 | |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 1069 | if (s->be->errmsg[msgnum].str) |
| 1070 | return &s->be->errmsg[msgnum]; |
Willy Tarreau | d0d8da9 | 2015-04-04 02:10:38 +0200 | [diff] [blame] | 1071 | else if (strm_fe(s)->errmsg[msgnum].str) |
| 1072 | return &strm_fe(s)->errmsg[msgnum]; |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 1073 | else |
| 1074 | return &http_err_chunks[msgnum]; |
| 1075 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1076 | |
Christopher Faulet | a94e5a5 | 2015-12-09 15:55:06 +0100 | [diff] [blame] | 1077 | void |
| 1078 | http_reply_and_close(struct stream *s, short status, struct chunk *msg) |
| 1079 | { |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 1080 | s->txn->flags &= ~TX_WAIT_NEXT_RQ; |
Christopher Faulet | 3e34429 | 2015-11-24 16:24:13 +0100 | [diff] [blame] | 1081 | FLT_STRM_CB(s, flt_http_reply(s, status, msg)); |
Christopher Faulet | a94e5a5 | 2015-12-09 15:55:06 +0100 | [diff] [blame] | 1082 | stream_int_retnclose(&s->si[0], msg); |
| 1083 | } |
| 1084 | |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 1085 | /* |
Willy Tarreau | b7ce424 | 2015-09-03 17:15:21 +0200 | [diff] [blame] | 1086 | * returns a known method among HTTP_METH_* or HTTP_METH_OTHER for all unknown |
| 1087 | * ones. |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 1088 | */ |
Thierry FOURNIER | d437314 | 2013-12-17 01:10:10 +0100 | [diff] [blame] | 1089 | 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] | 1090 | { |
| 1091 | unsigned char m; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1092 | const struct http_method_desc *h; |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 1093 | |
| 1094 | m = ((unsigned)*str - 'A'); |
| 1095 | |
| 1096 | if (m < 26) { |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1097 | for (h = http_methods[m]; h->len > 0; h++) { |
| 1098 | if (unlikely(h->len != len)) |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 1099 | continue; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1100 | if (likely(memcmp(str, h->text, h->len) == 0)) |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 1101 | return h->meth; |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 1102 | }; |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 1103 | } |
Willy Tarreau | b7ce424 | 2015-09-03 17:15:21 +0200 | [diff] [blame] | 1104 | return HTTP_METH_OTHER; |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 1105 | } |
| 1106 | |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 1107 | /* Parse the URI from the given transaction (which is assumed to be in request |
| 1108 | * phase) and look for the "/" beginning the PATH. If not found, return NULL. |
| 1109 | * It is returned otherwise. |
| 1110 | */ |
Thierry FOURNIER | 3c33178 | 2015-09-17 19:33:35 +0200 | [diff] [blame] | 1111 | char *http_get_path(struct http_txn *txn) |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 1112 | { |
| 1113 | char *ptr, *end; |
| 1114 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 1115 | ptr = txn->req.chn->buf->p + txn->req.sl.rq.u; |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 1116 | end = ptr + txn->req.sl.rq.u_l; |
| 1117 | |
| 1118 | if (ptr >= end) |
| 1119 | return NULL; |
| 1120 | |
Lukas Tribus | 2395368 | 2017-04-28 13:24:30 +0000 | [diff] [blame] | 1121 | /* RFC7230, par. 2.7 : |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 1122 | * Request-URI = "*" | absuri | abspath | authority |
| 1123 | */ |
| 1124 | |
| 1125 | if (*ptr == '*') |
| 1126 | return NULL; |
| 1127 | |
| 1128 | if (isalpha((unsigned char)*ptr)) { |
| 1129 | /* this is a scheme as described by RFC3986, par. 3.1 */ |
| 1130 | ptr++; |
| 1131 | while (ptr < end && |
| 1132 | (isalnum((unsigned char)*ptr) || *ptr == '+' || *ptr == '-' || *ptr == '.')) |
| 1133 | ptr++; |
| 1134 | /* skip '://' */ |
| 1135 | if (ptr == end || *ptr++ != ':') |
| 1136 | return NULL; |
| 1137 | if (ptr == end || *ptr++ != '/') |
| 1138 | return NULL; |
| 1139 | if (ptr == end || *ptr++ != '/') |
| 1140 | return NULL; |
| 1141 | } |
| 1142 | /* skip [user[:passwd]@]host[:[port]] */ |
| 1143 | |
| 1144 | while (ptr < end && *ptr != '/') |
| 1145 | ptr++; |
| 1146 | |
| 1147 | if (ptr == end) |
| 1148 | return NULL; |
| 1149 | |
| 1150 | /* OK, we got the '/' ! */ |
| 1151 | return ptr; |
| 1152 | } |
| 1153 | |
William Lallemand | 65ad6e1 | 2014-01-31 15:08:02 +0100 | [diff] [blame] | 1154 | /* Parse the URI from the given string and look for the "/" beginning the PATH. |
| 1155 | * If not found, return NULL. It is returned otherwise. |
| 1156 | */ |
| 1157 | static char * |
| 1158 | http_get_path_from_string(char *str) |
| 1159 | { |
| 1160 | char *ptr = str; |
| 1161 | |
| 1162 | /* RFC2616, par. 5.1.2 : |
| 1163 | * Request-URI = "*" | absuri | abspath | authority |
| 1164 | */ |
| 1165 | |
| 1166 | if (*ptr == '*') |
| 1167 | return NULL; |
| 1168 | |
| 1169 | if (isalpha((unsigned char)*ptr)) { |
| 1170 | /* this is a scheme as described by RFC3986, par. 3.1 */ |
| 1171 | ptr++; |
| 1172 | while (isalnum((unsigned char)*ptr) || *ptr == '+' || *ptr == '-' || *ptr == '.') |
| 1173 | ptr++; |
| 1174 | /* skip '://' */ |
| 1175 | if (*ptr == '\0' || *ptr++ != ':') |
| 1176 | return NULL; |
| 1177 | if (*ptr == '\0' || *ptr++ != '/') |
| 1178 | return NULL; |
| 1179 | if (*ptr == '\0' || *ptr++ != '/') |
| 1180 | return NULL; |
| 1181 | } |
| 1182 | /* skip [user[:passwd]@]host[:[port]] */ |
| 1183 | |
| 1184 | while (*ptr != '\0' && *ptr != ' ' && *ptr != '/') |
| 1185 | ptr++; |
| 1186 | |
| 1187 | if (*ptr == '\0' || *ptr == ' ') |
| 1188 | return NULL; |
| 1189 | |
| 1190 | /* OK, we got the '/' ! */ |
| 1191 | return ptr; |
| 1192 | } |
| 1193 | |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 1194 | /* Returns a 302 for a redirectable request that reaches a server working in |
| 1195 | * in redirect mode. This may only be called just after the stream interface |
| 1196 | * has moved to SI_ST_ASS. Unprocessable requests are left unchanged and will |
| 1197 | * follow normal proxy processing. NOTE: this function is designed to support |
| 1198 | * being called once data are scheduled for forwarding. |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 1199 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 1200 | void http_perform_server_redirect(struct stream *s, struct stream_interface *si) |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 1201 | { |
| 1202 | struct http_txn *txn; |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 1203 | struct server *srv; |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 1204 | char *path; |
Willy Tarreau | cde18fc | 2012-05-30 07:59:54 +0200 | [diff] [blame] | 1205 | int len, rewind; |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 1206 | |
| 1207 | /* 1: create the response header */ |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 1208 | trash.len = strlen(HTTP_302); |
| 1209 | memcpy(trash.str, HTTP_302, trash.len); |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 1210 | |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 1211 | srv = objt_server(s->target); |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 1212 | |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 1213 | /* 2: add the server's prefix */ |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 1214 | if (trash.len + srv->rdr_len > trash.size) |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 1215 | return; |
| 1216 | |
Willy Tarreau | dcb75c4 | 2010-01-10 00:24:22 +0100 | [diff] [blame] | 1217 | /* special prefix "/" means don't change URL */ |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 1218 | if (srv->rdr_len != 1 || *srv->rdr_pfx != '/') { |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 1219 | memcpy(trash.str + trash.len, srv->rdr_pfx, srv->rdr_len); |
| 1220 | trash.len += srv->rdr_len; |
Willy Tarreau | dcb75c4 | 2010-01-10 00:24:22 +0100 | [diff] [blame] | 1221 | } |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 1222 | |
Willy Tarreau | cde18fc | 2012-05-30 07:59:54 +0200 | [diff] [blame] | 1223 | /* 3: add the request URI. Since it was already forwarded, we need |
| 1224 | * to temporarily rewind the buffer. |
| 1225 | */ |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 1226 | txn = s->txn; |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 1227 | b_rew(s->req.buf, rewind = http_hdr_rewind(&txn->req)); |
Willy Tarreau | cde18fc | 2012-05-30 07:59:54 +0200 | [diff] [blame] | 1228 | |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 1229 | path = http_get_path(txn); |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 1230 | 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] | 1231 | |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 1232 | b_adv(s->req.buf, rewind); |
Willy Tarreau | cde18fc | 2012-05-30 07:59:54 +0200 | [diff] [blame] | 1233 | |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 1234 | if (!path) |
| 1235 | return; |
| 1236 | |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 1237 | if (trash.len + len > trash.size - 4) /* 4 for CRLF-CRLF */ |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 1238 | return; |
| 1239 | |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 1240 | memcpy(trash.str + trash.len, path, len); |
| 1241 | trash.len += len; |
Willy Tarreau | 88d349d | 2010-01-25 12:15:43 +0100 | [diff] [blame] | 1242 | |
| 1243 | if (unlikely(txn->flags & TX_USE_PX_CONN)) { |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 1244 | memcpy(trash.str + trash.len, "\r\nProxy-Connection: close\r\n\r\n", 29); |
| 1245 | trash.len += 29; |
Willy Tarreau | 88d349d | 2010-01-25 12:15:43 +0100 | [diff] [blame] | 1246 | } else { |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 1247 | memcpy(trash.str + trash.len, "\r\nConnection: close\r\n\r\n", 23); |
| 1248 | trash.len += 23; |
Willy Tarreau | 88d349d | 2010-01-25 12:15:43 +0100 | [diff] [blame] | 1249 | } |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 1250 | |
| 1251 | /* prepare to return without error. */ |
Willy Tarreau | 73b013b | 2012-05-21 16:31:45 +0200 | [diff] [blame] | 1252 | si_shutr(si); |
| 1253 | si_shutw(si); |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 1254 | si->err_type = SI_ET_NONE; |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 1255 | si->state = SI_ST_CLO; |
| 1256 | |
| 1257 | /* send the message */ |
Willy Tarreau | 2019f95 | 2017-03-14 11:07:31 +0100 | [diff] [blame] | 1258 | txn->status = 302; |
| 1259 | http_server_error(s, si, SF_ERR_LOCAL, SF_FINST_C, &trash); |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 1260 | |
| 1261 | /* 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] | 1262 | srv_inc_sess_ctr(srv); |
Bhaskar Maddala | a20cb85 | 2014-02-03 16:26:46 -0500 | [diff] [blame] | 1263 | srv_set_sess_last(srv); |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 1264 | } |
| 1265 | |
Willy Tarreau | 0cac36f | 2008-11-30 20:44:17 +0100 | [diff] [blame] | 1266 | /* Return the error message corresponding to si->err_type. It is assumed |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 1267 | * that the server side is closed. Note that err_type is actually a |
| 1268 | * bitmask, where almost only aborts may be cumulated with other |
| 1269 | * values. We consider that aborted operations are more important |
| 1270 | * than timeouts or errors due to the fact that nobody else in the |
| 1271 | * logs might explain incomplete retries. All others should avoid |
| 1272 | * being cumulated. It should normally not be possible to have multiple |
| 1273 | * aborts at once, but just in case, the first one in sequence is reported. |
Willy Tarreau | 6b726ad | 2013-12-15 19:31:37 +0100 | [diff] [blame] | 1274 | * Note that connection errors appearing on the second request of a keep-alive |
| 1275 | * connection are not reported since this allows the client to retry. |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 1276 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 1277 | void http_return_srv_error(struct stream *s, struct stream_interface *si) |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 1278 | { |
Willy Tarreau | 0cac36f | 2008-11-30 20:44:17 +0100 | [diff] [blame] | 1279 | int err_type = si->err_type; |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 1280 | |
Jarno Huuskonen | 9e6906b | 2017-03-06 14:21:49 +0200 | [diff] [blame] | 1281 | /* set s->txn->status for http_error_message(s) */ |
| 1282 | s->txn->status = 503; |
| 1283 | |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 1284 | if (err_type & SI_ET_QUEUE_ABRT) |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 1285 | http_server_error(s, si, SF_ERR_CLICL, SF_FINST_Q, |
Willy Tarreau | 2019f95 | 2017-03-14 11:07:31 +0100 | [diff] [blame] | 1286 | http_error_message(s)); |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 1287 | else if (err_type & SI_ET_CONN_ABRT) |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 1288 | http_server_error(s, si, SF_ERR_CLICL, SF_FINST_C, |
Willy Tarreau | 2019f95 | 2017-03-14 11:07:31 +0100 | [diff] [blame] | 1289 | (s->txn->flags & TX_NOT_FIRST) ? NULL : |
Jarno Huuskonen | 9e6906b | 2017-03-06 14:21:49 +0200 | [diff] [blame] | 1290 | http_error_message(s)); |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 1291 | else if (err_type & SI_ET_QUEUE_TO) |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 1292 | http_server_error(s, si, SF_ERR_SRVTO, SF_FINST_Q, |
Willy Tarreau | 2019f95 | 2017-03-14 11:07:31 +0100 | [diff] [blame] | 1293 | http_error_message(s)); |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 1294 | else if (err_type & SI_ET_QUEUE_ERR) |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 1295 | http_server_error(s, si, SF_ERR_SRVCL, SF_FINST_Q, |
Willy Tarreau | 2019f95 | 2017-03-14 11:07:31 +0100 | [diff] [blame] | 1296 | http_error_message(s)); |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 1297 | else if (err_type & SI_ET_CONN_TO) |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 1298 | http_server_error(s, si, SF_ERR_SRVTO, SF_FINST_C, |
Willy Tarreau | 2019f95 | 2017-03-14 11:07:31 +0100 | [diff] [blame] | 1299 | (s->txn->flags & TX_NOT_FIRST) ? NULL : |
Jarno Huuskonen | 9e6906b | 2017-03-06 14:21:49 +0200 | [diff] [blame] | 1300 | http_error_message(s)); |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 1301 | else if (err_type & SI_ET_CONN_ERR) |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 1302 | http_server_error(s, si, SF_ERR_SRVCL, SF_FINST_C, |
Willy Tarreau | 2019f95 | 2017-03-14 11:07:31 +0100 | [diff] [blame] | 1303 | (s->flags & SF_SRV_REUSED) ? NULL : |
Jarno Huuskonen | 9e6906b | 2017-03-06 14:21:49 +0200 | [diff] [blame] | 1304 | http_error_message(s)); |
Willy Tarreau | 2d400bb | 2012-05-14 12:11:47 +0200 | [diff] [blame] | 1305 | else if (err_type & SI_ET_CONN_RES) |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 1306 | http_server_error(s, si, SF_ERR_RESOURCE, SF_FINST_C, |
Willy Tarreau | 2019f95 | 2017-03-14 11:07:31 +0100 | [diff] [blame] | 1307 | (s->txn->flags & TX_NOT_FIRST) ? NULL : |
Jarno Huuskonen | 9e6906b | 2017-03-06 14:21:49 +0200 | [diff] [blame] | 1308 | http_error_message(s)); |
| 1309 | else { /* SI_ET_CONN_OTHER and others */ |
| 1310 | s->txn->status = 500; |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 1311 | http_server_error(s, si, SF_ERR_INTERNAL, SF_FINST_C, |
Willy Tarreau | 2019f95 | 2017-03-14 11:07:31 +0100 | [diff] [blame] | 1312 | http_error_message(s)); |
Jarno Huuskonen | 9e6906b | 2017-03-06 14:21:49 +0200 | [diff] [blame] | 1313 | } |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 1314 | } |
| 1315 | |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 1316 | extern const char sess_term_cond[8]; |
| 1317 | extern const char sess_fin_state[8]; |
| 1318 | extern const char *monthname[12]; |
Willy Tarreau | 63986c7 | 2015-04-03 22:55:33 +0200 | [diff] [blame] | 1319 | struct pool_head *pool2_http_txn; |
Willy Tarreau | 332f8bf | 2007-05-13 21:36:56 +0200 | [diff] [blame] | 1320 | struct pool_head *pool2_requri; |
Willy Tarreau | 193b8c6 | 2012-11-22 00:17:38 +0100 | [diff] [blame] | 1321 | struct pool_head *pool2_capture = NULL; |
William Lallemand | a73203e | 2012-03-12 12:48:57 +0100 | [diff] [blame] | 1322 | struct pool_head *pool2_uniqueid; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1323 | |
Willy Tarreau | 117f59e | 2007-03-04 18:17:17 +0100 | [diff] [blame] | 1324 | /* |
| 1325 | * Capture headers from message starting at <som> according to header list |
Willy Tarreau | 54da8db | 2014-06-13 16:11:48 +0200 | [diff] [blame] | 1326 | * <cap_hdr>, and fill the <cap> pointers appropriately. |
Willy Tarreau | 117f59e | 2007-03-04 18:17:17 +0100 | [diff] [blame] | 1327 | */ |
| 1328 | void capture_headers(char *som, struct hdr_idx *idx, |
| 1329 | char **cap, struct cap_hdr *cap_hdr) |
| 1330 | { |
| 1331 | char *eol, *sol, *col, *sov; |
| 1332 | int cur_idx; |
| 1333 | struct cap_hdr *h; |
| 1334 | int len; |
| 1335 | |
| 1336 | sol = som + hdr_idx_first_pos(idx); |
| 1337 | cur_idx = hdr_idx_first_idx(idx); |
| 1338 | |
| 1339 | while (cur_idx) { |
| 1340 | eol = sol + idx->v[cur_idx].len; |
| 1341 | |
| 1342 | col = sol; |
| 1343 | while (col < eol && *col != ':') |
| 1344 | col++; |
| 1345 | |
| 1346 | sov = col + 1; |
Willy Tarreau | 2235b26 | 2016-11-05 15:50:20 +0100 | [diff] [blame] | 1347 | while (sov < eol && HTTP_IS_LWS(*sov)) |
Willy Tarreau | 117f59e | 2007-03-04 18:17:17 +0100 | [diff] [blame] | 1348 | sov++; |
| 1349 | |
| 1350 | for (h = cap_hdr; h; h = h->next) { |
Willy Tarreau | 54da8db | 2014-06-13 16:11:48 +0200 | [diff] [blame] | 1351 | if (h->namelen && (h->namelen == col - sol) && |
Willy Tarreau | 117f59e | 2007-03-04 18:17:17 +0100 | [diff] [blame] | 1352 | (strncasecmp(sol, h->name, h->namelen) == 0)) { |
| 1353 | if (cap[h->index] == NULL) |
| 1354 | cap[h->index] = |
Willy Tarreau | cf7f320 | 2007-05-13 22:46:04 +0200 | [diff] [blame] | 1355 | pool_alloc2(h->pool); |
Willy Tarreau | 117f59e | 2007-03-04 18:17:17 +0100 | [diff] [blame] | 1356 | |
| 1357 | if (cap[h->index] == NULL) { |
| 1358 | Alert("HTTP capture : out of memory.\n"); |
| 1359 | continue; |
| 1360 | } |
| 1361 | |
| 1362 | len = eol - sov; |
| 1363 | if (len > h->len) |
| 1364 | len = h->len; |
| 1365 | |
| 1366 | memcpy(cap[h->index], sov, len); |
| 1367 | cap[h->index][len]=0; |
| 1368 | } |
| 1369 | } |
| 1370 | sol = eol + idx->v[cur_idx].cr + 1; |
| 1371 | cur_idx = idx->v[cur_idx].next; |
| 1372 | } |
| 1373 | } |
| 1374 | |
| 1375 | |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 1376 | /* either we find an LF at <ptr> or we jump to <bad>. |
| 1377 | */ |
Willy Tarreau | 10e61cb | 2017-01-04 14:51:22 +0100 | [diff] [blame] | 1378 | #define EXPECT_LF_HERE(ptr, bad, st) do { if (unlikely(*(ptr) != '\n')) { state = st; goto bad;}; } while (0) |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 1379 | |
| 1380 | /* plays with variables <ptr>, <end> and <state>. Jumps to <good> if OK, |
| 1381 | * otherwise to <http_msg_ood> with <state> set to <st>. |
| 1382 | */ |
| 1383 | #define EAT_AND_JUMP_OR_RETURN(good, st) do { \ |
| 1384 | ptr++; \ |
| 1385 | if (likely(ptr < end)) \ |
| 1386 | goto good; \ |
| 1387 | else { \ |
| 1388 | state = (st); \ |
| 1389 | goto http_msg_ood; \ |
| 1390 | } \ |
| 1391 | } while (0) |
| 1392 | |
| 1393 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1394 | /* |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 1395 | * This function parses a status line between <ptr> and <end>, starting with |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1396 | * parser state <state>. Only states HTTP_MSG_RPVER, HTTP_MSG_RPVER_SP, |
| 1397 | * HTTP_MSG_RPCODE, HTTP_MSG_RPCODE_SP and HTTP_MSG_RPREASON are handled. Others |
| 1398 | * will give undefined results. |
| 1399 | * Note that it is upon the caller's responsibility to ensure that ptr < end, |
| 1400 | * and that msg->sol points to the beginning of the response. |
| 1401 | * If a complete line is found (which implies that at least one CR or LF is |
| 1402 | * found before <end>, the updated <ptr> is returned, otherwise NULL is |
| 1403 | * returned indicating an incomplete line (which does not mean that parts have |
| 1404 | * not been updated). In the incomplete case, if <ret_ptr> or <ret_state> are |
| 1405 | * non-NULL, they are fed with the new <ptr> and <state> values to be passed |
| 1406 | * upon next call. |
| 1407 | * |
Willy Tarreau | 9cdde23 | 2007-05-02 20:58:19 +0200 | [diff] [blame] | 1408 | * This function was intentionally designed to be called from |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1409 | * http_msg_analyzer() with the lowest overhead. It should integrate perfectly |
| 1410 | * 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] | 1411 | * labels and variable names. Note that msg->sol is left unchanged. |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1412 | */ |
Willy Tarreau | 69d8c5d | 2012-05-08 09:44:41 +0200 | [diff] [blame] | 1413 | const char *http_parse_stsline(struct http_msg *msg, |
Willy Tarreau | 3770f23 | 2013-12-07 00:01:53 +0100 | [diff] [blame] | 1414 | enum ht_state state, const char *ptr, const char *end, |
| 1415 | unsigned int *ret_ptr, enum ht_state *ret_state) |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1416 | { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 1417 | const char *msg_start = msg->chn->buf->p; |
Willy Tarreau | 62f791e | 2012-03-09 11:32:30 +0100 | [diff] [blame] | 1418 | |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1419 | switch (state) { |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1420 | case HTTP_MSG_RPVER: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1421 | http_msg_rpver: |
Willy Tarreau | 4b89ad4 | 2007-03-04 18:13:58 +0100 | [diff] [blame] | 1422 | if (likely(HTTP_IS_VER_TOKEN(*ptr))) |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1423 | EAT_AND_JUMP_OR_RETURN(http_msg_rpver, HTTP_MSG_RPVER); |
| 1424 | |
| 1425 | if (likely(HTTP_IS_SPHT(*ptr))) { |
Willy Tarreau | ea1175a | 2012-03-05 15:52:30 +0100 | [diff] [blame] | 1426 | msg->sl.st.v_l = ptr - msg_start; |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1427 | EAT_AND_JUMP_OR_RETURN(http_msg_rpver_sp, HTTP_MSG_RPVER_SP); |
| 1428 | } |
Willy Tarreau | 10e61cb | 2017-01-04 14:51:22 +0100 | [diff] [blame] | 1429 | msg->err_state = HTTP_MSG_RPVER; |
Willy Tarreau | 7552c03 | 2009-03-01 11:10:40 +0100 | [diff] [blame] | 1430 | state = HTTP_MSG_ERROR; |
| 1431 | break; |
| 1432 | |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1433 | case HTTP_MSG_RPVER_SP: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1434 | http_msg_rpver_sp: |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1435 | if (likely(!HTTP_IS_LWS(*ptr))) { |
Willy Tarreau | ea1175a | 2012-03-05 15:52:30 +0100 | [diff] [blame] | 1436 | msg->sl.st.c = ptr - msg_start; |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1437 | goto http_msg_rpcode; |
| 1438 | } |
| 1439 | if (likely(HTTP_IS_SPHT(*ptr))) |
| 1440 | EAT_AND_JUMP_OR_RETURN(http_msg_rpver_sp, HTTP_MSG_RPVER_SP); |
| 1441 | /* so it's a CR/LF, this is invalid */ |
Willy Tarreau | 10e61cb | 2017-01-04 14:51:22 +0100 | [diff] [blame] | 1442 | msg->err_state = HTTP_MSG_RPVER_SP; |
Willy Tarreau | 7552c03 | 2009-03-01 11:10:40 +0100 | [diff] [blame] | 1443 | state = HTTP_MSG_ERROR; |
| 1444 | break; |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1445 | |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1446 | case HTTP_MSG_RPCODE: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1447 | http_msg_rpcode: |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1448 | if (likely(!HTTP_IS_LWS(*ptr))) |
| 1449 | EAT_AND_JUMP_OR_RETURN(http_msg_rpcode, HTTP_MSG_RPCODE); |
| 1450 | |
| 1451 | if (likely(HTTP_IS_SPHT(*ptr))) { |
Willy Tarreau | ea1175a | 2012-03-05 15:52:30 +0100 | [diff] [blame] | 1452 | msg->sl.st.c_l = ptr - msg_start - msg->sl.st.c; |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1453 | EAT_AND_JUMP_OR_RETURN(http_msg_rpcode_sp, HTTP_MSG_RPCODE_SP); |
| 1454 | } |
| 1455 | |
| 1456 | /* so it's a CR/LF, so there is no reason phrase */ |
Willy Tarreau | ea1175a | 2012-03-05 15:52:30 +0100 | [diff] [blame] | 1457 | msg->sl.st.c_l = ptr - msg_start - msg->sl.st.c; |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1458 | http_msg_rsp_reason: |
| 1459 | /* FIXME: should we support HTTP responses without any reason phrase ? */ |
Willy Tarreau | ea1175a | 2012-03-05 15:52:30 +0100 | [diff] [blame] | 1460 | msg->sl.st.r = ptr - msg_start; |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1461 | msg->sl.st.r_l = 0; |
| 1462 | goto http_msg_rpline_eol; |
| 1463 | |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1464 | case HTTP_MSG_RPCODE_SP: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1465 | http_msg_rpcode_sp: |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1466 | if (likely(!HTTP_IS_LWS(*ptr))) { |
Willy Tarreau | ea1175a | 2012-03-05 15:52:30 +0100 | [diff] [blame] | 1467 | msg->sl.st.r = ptr - msg_start; |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1468 | goto http_msg_rpreason; |
| 1469 | } |
| 1470 | if (likely(HTTP_IS_SPHT(*ptr))) |
| 1471 | EAT_AND_JUMP_OR_RETURN(http_msg_rpcode_sp, HTTP_MSG_RPCODE_SP); |
| 1472 | /* so it's a CR/LF, so there is no reason phrase */ |
| 1473 | goto http_msg_rsp_reason; |
| 1474 | |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1475 | case HTTP_MSG_RPREASON: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1476 | http_msg_rpreason: |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1477 | if (likely(!HTTP_IS_CRLF(*ptr))) |
| 1478 | EAT_AND_JUMP_OR_RETURN(http_msg_rpreason, HTTP_MSG_RPREASON); |
Willy Tarreau | ea1175a | 2012-03-05 15:52:30 +0100 | [diff] [blame] | 1479 | msg->sl.st.r_l = ptr - msg_start - msg->sl.st.r; |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1480 | http_msg_rpline_eol: |
| 1481 | /* We have seen the end of line. Note that we do not |
| 1482 | * necessarily have the \n yet, but at least we know that we |
| 1483 | * have EITHER \r OR \n, otherwise the response would not be |
| 1484 | * complete. We can then record the response length and return |
| 1485 | * to the caller which will be able to register it. |
| 1486 | */ |
Willy Tarreau | 3a215be | 2012-03-09 21:39:51 +0100 | [diff] [blame] | 1487 | msg->sl.st.l = ptr - msg_start - msg->sol; |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1488 | return ptr; |
| 1489 | |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1490 | default: |
Willy Tarreau | 3770f23 | 2013-12-07 00:01:53 +0100 | [diff] [blame] | 1491 | #ifdef DEBUG_FULL |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1492 | fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state); |
| 1493 | exit(1); |
| 1494 | #endif |
Willy Tarreau | 3770f23 | 2013-12-07 00:01:53 +0100 | [diff] [blame] | 1495 | ; |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1496 | } |
| 1497 | |
| 1498 | http_msg_ood: |
Willy Tarreau | 7552c03 | 2009-03-01 11:10:40 +0100 | [diff] [blame] | 1499 | /* out of valid data */ |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1500 | if (ret_state) |
| 1501 | *ret_state = state; |
| 1502 | if (ret_ptr) |
Willy Tarreau | a458b67 | 2012-03-05 11:17:50 +0100 | [diff] [blame] | 1503 | *ret_ptr = ptr - msg_start; |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1504 | return NULL; |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1505 | } |
| 1506 | |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1507 | /* |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1508 | * This function parses a request line between <ptr> and <end>, starting with |
| 1509 | * parser state <state>. Only states HTTP_MSG_RQMETH, HTTP_MSG_RQMETH_SP, |
| 1510 | * HTTP_MSG_RQURI, HTTP_MSG_RQURI_SP and HTTP_MSG_RQVER are handled. Others |
| 1511 | * will give undefined results. |
| 1512 | * Note that it is upon the caller's responsibility to ensure that ptr < end, |
| 1513 | * and that msg->sol points to the beginning of the request. |
| 1514 | * If a complete line is found (which implies that at least one CR or LF is |
| 1515 | * found before <end>, the updated <ptr> is returned, otherwise NULL is |
| 1516 | * returned indicating an incomplete line (which does not mean that parts have |
| 1517 | * not been updated). In the incomplete case, if <ret_ptr> or <ret_state> are |
| 1518 | * non-NULL, they are fed with the new <ptr> and <state> values to be passed |
| 1519 | * upon next call. |
| 1520 | * |
Willy Tarreau | 9cdde23 | 2007-05-02 20:58:19 +0200 | [diff] [blame] | 1521 | * This function was intentionally designed to be called from |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1522 | * http_msg_analyzer() with the lowest overhead. It should integrate perfectly |
| 1523 | * 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] | 1524 | * labels and variable names. Note that msg->sol is left unchanged. |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1525 | */ |
Willy Tarreau | 69d8c5d | 2012-05-08 09:44:41 +0200 | [diff] [blame] | 1526 | const char *http_parse_reqline(struct http_msg *msg, |
Willy Tarreau | 3770f23 | 2013-12-07 00:01:53 +0100 | [diff] [blame] | 1527 | enum ht_state state, const char *ptr, const char *end, |
| 1528 | unsigned int *ret_ptr, enum ht_state *ret_state) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1529 | { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 1530 | const char *msg_start = msg->chn->buf->p; |
Willy Tarreau | 62f791e | 2012-03-09 11:32:30 +0100 | [diff] [blame] | 1531 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1532 | switch (state) { |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1533 | case HTTP_MSG_RQMETH: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1534 | http_msg_rqmeth: |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1535 | if (likely(HTTP_IS_TOKEN(*ptr))) |
| 1536 | EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth, HTTP_MSG_RQMETH); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1537 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1538 | if (likely(HTTP_IS_SPHT(*ptr))) { |
Willy Tarreau | ea1175a | 2012-03-05 15:52:30 +0100 | [diff] [blame] | 1539 | msg->sl.rq.m_l = ptr - msg_start; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1540 | EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth_sp, HTTP_MSG_RQMETH_SP); |
| 1541 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1542 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1543 | if (likely(HTTP_IS_CRLF(*ptr))) { |
| 1544 | /* HTTP 0.9 request */ |
Willy Tarreau | ea1175a | 2012-03-05 15:52:30 +0100 | [diff] [blame] | 1545 | msg->sl.rq.m_l = ptr - msg_start; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1546 | http_msg_req09_uri: |
Willy Tarreau | ea1175a | 2012-03-05 15:52:30 +0100 | [diff] [blame] | 1547 | msg->sl.rq.u = ptr - msg_start; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1548 | http_msg_req09_uri_e: |
Willy Tarreau | ea1175a | 2012-03-05 15:52:30 +0100 | [diff] [blame] | 1549 | msg->sl.rq.u_l = ptr - msg_start - msg->sl.rq.u; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1550 | http_msg_req09_ver: |
Willy Tarreau | ea1175a | 2012-03-05 15:52:30 +0100 | [diff] [blame] | 1551 | msg->sl.rq.v = ptr - msg_start; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1552 | msg->sl.rq.v_l = 0; |
| 1553 | goto http_msg_rqline_eol; |
| 1554 | } |
Willy Tarreau | 10e61cb | 2017-01-04 14:51:22 +0100 | [diff] [blame] | 1555 | msg->err_state = HTTP_MSG_RQMETH; |
Willy Tarreau | 7552c03 | 2009-03-01 11:10:40 +0100 | [diff] [blame] | 1556 | state = HTTP_MSG_ERROR; |
| 1557 | break; |
| 1558 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1559 | case HTTP_MSG_RQMETH_SP: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1560 | http_msg_rqmeth_sp: |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1561 | if (likely(!HTTP_IS_LWS(*ptr))) { |
Willy Tarreau | ea1175a | 2012-03-05 15:52:30 +0100 | [diff] [blame] | 1562 | msg->sl.rq.u = ptr - msg_start; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1563 | goto http_msg_rquri; |
| 1564 | } |
| 1565 | if (likely(HTTP_IS_SPHT(*ptr))) |
| 1566 | EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth_sp, HTTP_MSG_RQMETH_SP); |
| 1567 | /* so it's a CR/LF, meaning an HTTP 0.9 request */ |
| 1568 | goto http_msg_req09_uri; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1569 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1570 | case HTTP_MSG_RQURI: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1571 | http_msg_rquri: |
Willy Tarreau | 5f10ea3 | 2016-11-05 17:52:06 +0100 | [diff] [blame] | 1572 | #if defined(__x86_64__) || \ |
| 1573 | defined(__i386__) || defined(__i486__) || defined(__i586__) || defined(__i686__) || \ |
| 1574 | defined(__ARM_ARCH_7A__) |
| 1575 | /* speedup: skip bytes not between 0x21 and 0x7e inclusive */ |
| 1576 | while (ptr <= end - sizeof(int)) { |
| 1577 | int x = *(int *)ptr - 0x21212121; |
| 1578 | if (x & 0x80808080) |
| 1579 | break; |
| 1580 | |
| 1581 | x -= 0x5e5e5e5e; |
| 1582 | if (!(x & 0x80808080)) |
| 1583 | break; |
| 1584 | |
| 1585 | ptr += sizeof(int); |
| 1586 | } |
| 1587 | #endif |
Willy Tarreau | 2afff9c | 2017-01-04 14:44:46 +0100 | [diff] [blame] | 1588 | if (ptr >= end) { |
| 1589 | state = HTTP_MSG_RQURI; |
| 1590 | goto http_msg_ood; |
| 1591 | } |
Willy Tarreau | 5f10ea3 | 2016-11-05 17:52:06 +0100 | [diff] [blame] | 1592 | http_msg_rquri2: |
Willy Tarreau | 2e9506d | 2012-01-07 23:22:31 +0100 | [diff] [blame] | 1593 | if (likely((unsigned char)(*ptr - 33) <= 93)) /* 33 to 126 included */ |
Willy Tarreau | 5f10ea3 | 2016-11-05 17:52:06 +0100 | [diff] [blame] | 1594 | EAT_AND_JUMP_OR_RETURN(http_msg_rquri2, HTTP_MSG_RQURI); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1595 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1596 | if (likely(HTTP_IS_SPHT(*ptr))) { |
Willy Tarreau | ea1175a | 2012-03-05 15:52:30 +0100 | [diff] [blame] | 1597 | msg->sl.rq.u_l = ptr - msg_start - msg->sl.rq.u; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1598 | EAT_AND_JUMP_OR_RETURN(http_msg_rquri_sp, HTTP_MSG_RQURI_SP); |
| 1599 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1600 | |
Willy Tarreau | 2e9506d | 2012-01-07 23:22:31 +0100 | [diff] [blame] | 1601 | if (likely((unsigned char)*ptr >= 128)) { |
Willy Tarreau | 422246e | 2012-01-07 23:54:13 +0100 | [diff] [blame] | 1602 | /* non-ASCII chars are forbidden unless option |
| 1603 | * accept-invalid-http-request is enabled in the frontend. |
| 1604 | * In any case, we capture the faulty char. |
Willy Tarreau | 2e9506d | 2012-01-07 23:22:31 +0100 | [diff] [blame] | 1605 | */ |
Willy Tarreau | 422246e | 2012-01-07 23:54:13 +0100 | [diff] [blame] | 1606 | if (msg->err_pos < -1) |
| 1607 | goto invalid_char; |
| 1608 | if (msg->err_pos == -1) |
Willy Tarreau | 69d8c5d | 2012-05-08 09:44:41 +0200 | [diff] [blame] | 1609 | msg->err_pos = ptr - msg_start; |
Willy Tarreau | 2e9506d | 2012-01-07 23:22:31 +0100 | [diff] [blame] | 1610 | EAT_AND_JUMP_OR_RETURN(http_msg_rquri, HTTP_MSG_RQURI); |
| 1611 | } |
| 1612 | |
| 1613 | if (likely(HTTP_IS_CRLF(*ptr))) { |
| 1614 | /* so it's a CR/LF, meaning an HTTP 0.9 request */ |
| 1615 | goto http_msg_req09_uri_e; |
| 1616 | } |
| 1617 | |
| 1618 | /* OK forbidden chars, 0..31 or 127 */ |
Willy Tarreau | 422246e | 2012-01-07 23:54:13 +0100 | [diff] [blame] | 1619 | invalid_char: |
Willy Tarreau | 69d8c5d | 2012-05-08 09:44:41 +0200 | [diff] [blame] | 1620 | msg->err_pos = ptr - msg_start; |
Willy Tarreau | 10e61cb | 2017-01-04 14:51:22 +0100 | [diff] [blame] | 1621 | msg->err_state = HTTP_MSG_RQURI; |
Willy Tarreau | 2e9506d | 2012-01-07 23:22:31 +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 | case HTTP_MSG_RQURI_SP: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1626 | http_msg_rquri_sp: |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1627 | if (likely(!HTTP_IS_LWS(*ptr))) { |
Willy Tarreau | ea1175a | 2012-03-05 15:52:30 +0100 | [diff] [blame] | 1628 | msg->sl.rq.v = ptr - msg_start; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1629 | goto http_msg_rqver; |
| 1630 | } |
| 1631 | if (likely(HTTP_IS_SPHT(*ptr))) |
| 1632 | EAT_AND_JUMP_OR_RETURN(http_msg_rquri_sp, HTTP_MSG_RQURI_SP); |
| 1633 | /* so it's a CR/LF, meaning an HTTP 0.9 request */ |
| 1634 | goto http_msg_req09_ver; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1635 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1636 | case HTTP_MSG_RQVER: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1637 | http_msg_rqver: |
Willy Tarreau | 4b89ad4 | 2007-03-04 18:13:58 +0100 | [diff] [blame] | 1638 | if (likely(HTTP_IS_VER_TOKEN(*ptr))) |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1639 | EAT_AND_JUMP_OR_RETURN(http_msg_rqver, HTTP_MSG_RQVER); |
Willy Tarreau | 4b89ad4 | 2007-03-04 18:13:58 +0100 | [diff] [blame] | 1640 | |
| 1641 | if (likely(HTTP_IS_CRLF(*ptr))) { |
Willy Tarreau | ea1175a | 2012-03-05 15:52:30 +0100 | [diff] [blame] | 1642 | msg->sl.rq.v_l = ptr - msg_start - msg->sl.rq.v; |
Willy Tarreau | 4b89ad4 | 2007-03-04 18:13:58 +0100 | [diff] [blame] | 1643 | http_msg_rqline_eol: |
| 1644 | /* We have seen the end of line. Note that we do not |
| 1645 | * necessarily have the \n yet, but at least we know that we |
| 1646 | * have EITHER \r OR \n, otherwise the request would not be |
| 1647 | * complete. We can then record the request length and return |
| 1648 | * to the caller which will be able to register it. |
| 1649 | */ |
Willy Tarreau | 3a215be | 2012-03-09 21:39:51 +0100 | [diff] [blame] | 1650 | msg->sl.rq.l = ptr - msg_start - msg->sol; |
Willy Tarreau | 4b89ad4 | 2007-03-04 18:13:58 +0100 | [diff] [blame] | 1651 | return ptr; |
| 1652 | } |
| 1653 | |
| 1654 | /* neither an HTTP_VER token nor a CRLF */ |
Willy Tarreau | 10e61cb | 2017-01-04 14:51:22 +0100 | [diff] [blame] | 1655 | msg->err_state = HTTP_MSG_RQVER; |
Willy Tarreau | 7552c03 | 2009-03-01 11:10:40 +0100 | [diff] [blame] | 1656 | state = HTTP_MSG_ERROR; |
| 1657 | break; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1658 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1659 | default: |
Willy Tarreau | 3770f23 | 2013-12-07 00:01:53 +0100 | [diff] [blame] | 1660 | #ifdef DEBUG_FULL |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1661 | fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state); |
| 1662 | exit(1); |
| 1663 | #endif |
Willy Tarreau | 3770f23 | 2013-12-07 00:01:53 +0100 | [diff] [blame] | 1664 | ; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1665 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1666 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1667 | http_msg_ood: |
Willy Tarreau | 7552c03 | 2009-03-01 11:10:40 +0100 | [diff] [blame] | 1668 | /* out of valid data */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1669 | if (ret_state) |
| 1670 | *ret_state = state; |
| 1671 | if (ret_ptr) |
Willy Tarreau | a458b67 | 2012-03-05 11:17:50 +0100 | [diff] [blame] | 1672 | *ret_ptr = ptr - msg_start; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1673 | return NULL; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1674 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1675 | |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 1676 | /* |
| 1677 | * Returns the data from Authorization header. Function may be called more |
| 1678 | * than once so data is stored in txn->auth_data. When no header is found |
| 1679 | * 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] | 1680 | * searching again for something we are unable to find anyway. However, if |
| 1681 | * 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] | 1682 | * have the credentials overwritten by another stream in parallel. |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 1683 | */ |
| 1684 | |
Thierry FOURNIER | 9eec0a6 | 2014-01-22 18:38:02 +0100 | [diff] [blame] | 1685 | /* This bufffer is initialized in the file 'src/haproxy.c'. This length is |
| 1686 | * set according to global.tune.bufsize. |
| 1687 | */ |
Willy Tarreau | 7e2c647 | 2012-10-29 20:44:36 +0100 | [diff] [blame] | 1688 | char *get_http_auth_buff; |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 1689 | |
| 1690 | int |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 1691 | get_http_auth(struct stream *s) |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 1692 | { |
| 1693 | |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 1694 | struct http_txn *txn = s->txn; |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 1695 | struct chunk auth_method; |
| 1696 | struct hdr_ctx ctx; |
| 1697 | char *h, *p; |
| 1698 | int len; |
| 1699 | |
| 1700 | #ifdef DEBUG_AUTH |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 1701 | printf("Auth for stream %p: %d\n", s, txn->auth.method); |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 1702 | #endif |
| 1703 | |
| 1704 | if (txn->auth.method == HTTP_AUTH_WRONG) |
| 1705 | return 0; |
| 1706 | |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 1707 | txn->auth.method = HTTP_AUTH_WRONG; |
| 1708 | |
| 1709 | ctx.idx = 0; |
Willy Tarreau | 844a7e7 | 2010-01-31 21:46:18 +0100 | [diff] [blame] | 1710 | |
| 1711 | if (txn->flags & TX_USE_PX_CONN) { |
| 1712 | h = "Proxy-Authorization"; |
| 1713 | len = strlen(h); |
| 1714 | } else { |
| 1715 | h = "Authorization"; |
| 1716 | len = strlen(h); |
| 1717 | } |
| 1718 | |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 1719 | 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] | 1720 | return 0; |
| 1721 | |
| 1722 | h = ctx.line + ctx.val; |
| 1723 | |
| 1724 | p = memchr(h, ' ', ctx.vlen); |
Willy Tarreau | 5c557d1 | 2016-03-13 08:17:02 +0100 | [diff] [blame] | 1725 | len = p - h; |
| 1726 | if (!p || len <= 0) |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 1727 | return 0; |
| 1728 | |
David Carlier | 7365f7d | 2016-04-04 11:54:42 +0100 | [diff] [blame] | 1729 | if (chunk_initlen(&auth_method, h, 0, len) != 1) |
| 1730 | return 0; |
| 1731 | |
Willy Tarreau | 5c557d1 | 2016-03-13 08:17:02 +0100 | [diff] [blame] | 1732 | 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] | 1733 | |
| 1734 | if (!strncasecmp("Basic", auth_method.str, auth_method.len)) { |
| 1735 | |
| 1736 | len = base64dec(txn->auth.method_data.str, txn->auth.method_data.len, |
Willy Tarreau | 7e2c647 | 2012-10-29 20:44:36 +0100 | [diff] [blame] | 1737 | get_http_auth_buff, global.tune.bufsize - 1); |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 1738 | |
| 1739 | if (len < 0) |
| 1740 | return 0; |
| 1741 | |
| 1742 | |
| 1743 | get_http_auth_buff[len] = '\0'; |
| 1744 | |
| 1745 | p = strchr(get_http_auth_buff, ':'); |
| 1746 | |
| 1747 | if (!p) |
| 1748 | return 0; |
| 1749 | |
| 1750 | txn->auth.user = get_http_auth_buff; |
| 1751 | *p = '\0'; |
| 1752 | txn->auth.pass = p+1; |
| 1753 | |
| 1754 | txn->auth.method = HTTP_AUTH_BASIC; |
| 1755 | return 1; |
| 1756 | } |
| 1757 | |
| 1758 | return 0; |
| 1759 | } |
| 1760 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1761 | |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1762 | /* |
| 1763 | * This function parses an HTTP message, either a request or a response, |
Willy Tarreau | 8b1323e | 2012-03-09 14:46:19 +0100 | [diff] [blame] | 1764 | * depending on the initial msg->msg_state. The caller is responsible for |
| 1765 | * ensuring that the message does not wrap. The function can be preempted |
| 1766 | * everywhere when data are missing and recalled at the exact same location |
| 1767 | * with no information loss. The message may even be realigned between two |
| 1768 | * calls. The header index is re-initialized when switching from |
Willy Tarreau | 9cdde23 | 2007-05-02 20:58:19 +0200 | [diff] [blame] | 1769 | * 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] | 1770 | * fields. Note that msg->sol will be initialized after completing the first |
| 1771 | * state, so that none of the msg pointers has to be initialized prior to the |
| 1772 | * first call. |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1773 | */ |
Willy Tarreau | a560c21 | 2012-03-09 13:50:57 +0100 | [diff] [blame] | 1774 | void http_msg_analyzer(struct http_msg *msg, struct hdr_idx *idx) |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1775 | { |
Willy Tarreau | 3770f23 | 2013-12-07 00:01:53 +0100 | [diff] [blame] | 1776 | enum ht_state state; /* updated only when leaving the FSM */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1777 | register char *ptr, *end; /* request pointers, to avoid dereferences */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1778 | struct buffer *buf; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1779 | |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1780 | state = msg->msg_state; |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 1781 | buf = msg->chn->buf; |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1782 | ptr = buf->p + msg->next; |
| 1783 | end = buf->p + buf->i; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1784 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1785 | if (unlikely(ptr >= end)) |
| 1786 | goto http_msg_ood; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1787 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1788 | switch (state) { |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1789 | /* |
| 1790 | * First, states that are specific to the response only. |
| 1791 | * We check them first so that request and headers are |
| 1792 | * closer to each other (accessed more often). |
| 1793 | */ |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1794 | case HTTP_MSG_RPBEFORE: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1795 | http_msg_rpbefore: |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1796 | if (likely(HTTP_IS_TOKEN(*ptr))) { |
Willy Tarreau | 15de77e | 2010-01-02 21:59:16 +0100 | [diff] [blame] | 1797 | /* we have a start of message, but we have to check |
| 1798 | * first if we need to remove some CRLF. We can only |
Willy Tarreau | 2e046c6 | 2012-03-01 16:08:30 +0100 | [diff] [blame] | 1799 | * do this when o=0. |
Willy Tarreau | 15de77e | 2010-01-02 21:59:16 +0100 | [diff] [blame] | 1800 | */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1801 | if (unlikely(ptr != buf->p)) { |
| 1802 | if (buf->o) |
Willy Tarreau | 15de77e | 2010-01-02 21:59:16 +0100 | [diff] [blame] | 1803 | goto http_msg_ood; |
Willy Tarreau | 1d3bcce | 2009-12-27 15:50:06 +0100 | [diff] [blame] | 1804 | /* Remove empty leading lines, as recommended by RFC2616. */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1805 | bi_fast_delete(buf, ptr - buf->p); |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1806 | } |
Willy Tarreau | 2692736 | 2012-05-18 23:22:52 +0200 | [diff] [blame] | 1807 | msg->sol = 0; |
Willy Tarreau | e92693a | 2012-09-24 21:13:39 +0200 | [diff] [blame] | 1808 | msg->sl.st.l = 0; /* used in debug mode */ |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1809 | hdr_idx_init(idx); |
| 1810 | state = HTTP_MSG_RPVER; |
| 1811 | goto http_msg_rpver; |
| 1812 | } |
| 1813 | |
Willy Tarreau | 10e61cb | 2017-01-04 14:51:22 +0100 | [diff] [blame] | 1814 | if (unlikely(!HTTP_IS_CRLF(*ptr))) { |
| 1815 | state = HTTP_MSG_RPBEFORE; |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1816 | goto http_msg_invalid; |
Willy Tarreau | 10e61cb | 2017-01-04 14:51:22 +0100 | [diff] [blame] | 1817 | } |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1818 | |
| 1819 | if (unlikely(*ptr == '\n')) |
| 1820 | EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore, HTTP_MSG_RPBEFORE); |
| 1821 | EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore_cr, HTTP_MSG_RPBEFORE_CR); |
| 1822 | /* stop here */ |
| 1823 | |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1824 | case HTTP_MSG_RPBEFORE_CR: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1825 | http_msg_rpbefore_cr: |
Willy Tarreau | 10e61cb | 2017-01-04 14:51:22 +0100 | [diff] [blame] | 1826 | EXPECT_LF_HERE(ptr, http_msg_invalid, HTTP_MSG_RPBEFORE_CR); |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1827 | EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore, HTTP_MSG_RPBEFORE); |
| 1828 | /* stop here */ |
| 1829 | |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1830 | case HTTP_MSG_RPVER: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1831 | http_msg_rpver: |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1832 | case HTTP_MSG_RPVER_SP: |
| 1833 | case HTTP_MSG_RPCODE: |
| 1834 | case HTTP_MSG_RPCODE_SP: |
| 1835 | case HTTP_MSG_RPREASON: |
Willy Tarreau | 69d8c5d | 2012-05-08 09:44:41 +0200 | [diff] [blame] | 1836 | ptr = (char *)http_parse_stsline(msg, |
Willy Tarreau | a458b67 | 2012-03-05 11:17:50 +0100 | [diff] [blame] | 1837 | state, ptr, end, |
| 1838 | &msg->next, &msg->msg_state); |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1839 | if (unlikely(!ptr)) |
| 1840 | return; |
| 1841 | |
| 1842 | /* we have a full response and we know that we have either a CR |
| 1843 | * or an LF at <ptr>. |
| 1844 | */ |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1845 | hdr_idx_set_start(idx, msg->sl.st.l, *ptr == '\r'); |
| 1846 | |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1847 | msg->sol = ptr - buf->p; |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1848 | if (likely(*ptr == '\r')) |
| 1849 | EAT_AND_JUMP_OR_RETURN(http_msg_rpline_end, HTTP_MSG_RPLINE_END); |
| 1850 | goto http_msg_rpline_end; |
| 1851 | |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1852 | case HTTP_MSG_RPLINE_END: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1853 | http_msg_rpline_end: |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1854 | /* msg->sol must point to the first of CR or LF. */ |
Willy Tarreau | 10e61cb | 2017-01-04 14:51:22 +0100 | [diff] [blame] | 1855 | EXPECT_LF_HERE(ptr, http_msg_invalid, HTTP_MSG_RPLINE_END); |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1856 | EAT_AND_JUMP_OR_RETURN(http_msg_hdr_first, HTTP_MSG_HDR_FIRST); |
| 1857 | /* stop here */ |
| 1858 | |
| 1859 | /* |
| 1860 | * Second, states that are specific to the request only |
| 1861 | */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1862 | case HTTP_MSG_RQBEFORE: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1863 | http_msg_rqbefore: |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1864 | if (likely(HTTP_IS_TOKEN(*ptr))) { |
Willy Tarreau | 15de77e | 2010-01-02 21:59:16 +0100 | [diff] [blame] | 1865 | /* we have a start of message, but we have to check |
| 1866 | * first if we need to remove some CRLF. We can only |
Willy Tarreau | 2e046c6 | 2012-03-01 16:08:30 +0100 | [diff] [blame] | 1867 | * do this when o=0. |
Willy Tarreau | 15de77e | 2010-01-02 21:59:16 +0100 | [diff] [blame] | 1868 | */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1869 | if (likely(ptr != buf->p)) { |
| 1870 | if (buf->o) |
Willy Tarreau | 15de77e | 2010-01-02 21:59:16 +0100 | [diff] [blame] | 1871 | goto http_msg_ood; |
Willy Tarreau | 1d3bcce | 2009-12-27 15:50:06 +0100 | [diff] [blame] | 1872 | /* Remove empty leading lines, as recommended by RFC2616. */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1873 | bi_fast_delete(buf, ptr - buf->p); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1874 | } |
Willy Tarreau | 2692736 | 2012-05-18 23:22:52 +0200 | [diff] [blame] | 1875 | msg->sol = 0; |
Willy Tarreau | e92693a | 2012-09-24 21:13:39 +0200 | [diff] [blame] | 1876 | msg->sl.rq.l = 0; /* used in debug mode */ |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1877 | state = HTTP_MSG_RQMETH; |
| 1878 | goto http_msg_rqmeth; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1879 | } |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1880 | |
Willy Tarreau | 10e61cb | 2017-01-04 14:51:22 +0100 | [diff] [blame] | 1881 | if (unlikely(!HTTP_IS_CRLF(*ptr))) { |
| 1882 | state = HTTP_MSG_RQBEFORE; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1883 | goto http_msg_invalid; |
Willy Tarreau | 10e61cb | 2017-01-04 14:51:22 +0100 | [diff] [blame] | 1884 | } |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1885 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1886 | if (unlikely(*ptr == '\n')) |
| 1887 | EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore, HTTP_MSG_RQBEFORE); |
| 1888 | 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] | 1889 | /* stop here */ |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1890 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1891 | case HTTP_MSG_RQBEFORE_CR: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1892 | http_msg_rqbefore_cr: |
Willy Tarreau | 10e61cb | 2017-01-04 14:51:22 +0100 | [diff] [blame] | 1893 | EXPECT_LF_HERE(ptr, http_msg_invalid, HTTP_MSG_RQBEFORE_CR); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1894 | EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore, HTTP_MSG_RQBEFORE); |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1895 | /* stop here */ |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1896 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1897 | case HTTP_MSG_RQMETH: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1898 | http_msg_rqmeth: |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1899 | case HTTP_MSG_RQMETH_SP: |
| 1900 | case HTTP_MSG_RQURI: |
| 1901 | case HTTP_MSG_RQURI_SP: |
| 1902 | case HTTP_MSG_RQVER: |
Willy Tarreau | 69d8c5d | 2012-05-08 09:44:41 +0200 | [diff] [blame] | 1903 | ptr = (char *)http_parse_reqline(msg, |
Willy Tarreau | a458b67 | 2012-03-05 11:17:50 +0100 | [diff] [blame] | 1904 | state, ptr, end, |
| 1905 | &msg->next, &msg->msg_state); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1906 | if (unlikely(!ptr)) |
| 1907 | return; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1908 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1909 | /* we have a full request and we know that we have either a CR |
| 1910 | * or an LF at <ptr>. |
| 1911 | */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1912 | hdr_idx_set_start(idx, msg->sl.rq.l, *ptr == '\r'); |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1913 | |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1914 | msg->sol = ptr - buf->p; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1915 | if (likely(*ptr == '\r')) |
| 1916 | 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] | 1917 | goto http_msg_rqline_end; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1918 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1919 | case HTTP_MSG_RQLINE_END: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1920 | http_msg_rqline_end: |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1921 | /* check for HTTP/0.9 request : no version information available. |
| 1922 | * msg->sol must point to the first of CR or LF. |
| 1923 | */ |
| 1924 | if (unlikely(msg->sl.rq.v_l == 0)) |
| 1925 | goto http_msg_last_lf; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1926 | |
Willy Tarreau | 10e61cb | 2017-01-04 14:51:22 +0100 | [diff] [blame] | 1927 | EXPECT_LF_HERE(ptr, http_msg_invalid, HTTP_MSG_RQLINE_END); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1928 | 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] | 1929 | /* stop here */ |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1930 | |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1931 | /* |
| 1932 | * Common states below |
| 1933 | */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1934 | case HTTP_MSG_HDR_FIRST: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1935 | http_msg_hdr_first: |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1936 | msg->sol = ptr - buf->p; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1937 | if (likely(!HTTP_IS_CRLF(*ptr))) { |
| 1938 | goto http_msg_hdr_name; |
| 1939 | } |
| 1940 | |
| 1941 | if (likely(*ptr == '\r')) |
| 1942 | EAT_AND_JUMP_OR_RETURN(http_msg_last_lf, HTTP_MSG_LAST_LF); |
| 1943 | goto http_msg_last_lf; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1944 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1945 | case HTTP_MSG_HDR_NAME: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1946 | http_msg_hdr_name: |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1947 | /* assumes msg->sol points to the first char */ |
| 1948 | if (likely(HTTP_IS_TOKEN(*ptr))) |
| 1949 | 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] | 1950 | |
Willy Tarreau | fa4a03c | 2012-03-09 21:28:54 +0100 | [diff] [blame] | 1951 | if (likely(*ptr == ':')) |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1952 | 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] | 1953 | |
Willy Tarreau | 10e61cb | 2017-01-04 14:51:22 +0100 | [diff] [blame] | 1954 | if (likely(msg->err_pos < -1) || *ptr == '\n') { |
| 1955 | state = HTTP_MSG_HDR_NAME; |
Willy Tarreau | 32a4ec0 | 2009-04-02 11:35:18 +0200 | [diff] [blame] | 1956 | goto http_msg_invalid; |
Willy Tarreau | 10e61cb | 2017-01-04 14:51:22 +0100 | [diff] [blame] | 1957 | } |
Willy Tarreau | 32a4ec0 | 2009-04-02 11:35:18 +0200 | [diff] [blame] | 1958 | |
| 1959 | if (msg->err_pos == -1) /* capture error pointer */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1960 | msg->err_pos = ptr - buf->p; /* >= 0 now */ |
Willy Tarreau | 32a4ec0 | 2009-04-02 11:35:18 +0200 | [diff] [blame] | 1961 | |
| 1962 | /* and we still accept this non-token character */ |
| 1963 | 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] | 1964 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1965 | case HTTP_MSG_HDR_L1_SP: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1966 | http_msg_hdr_l1_sp: |
Willy Tarreau | fa4a03c | 2012-03-09 21:28:54 +0100 | [diff] [blame] | 1967 | /* assumes msg->sol points to the first char */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1968 | if (likely(HTTP_IS_SPHT(*ptr))) |
| 1969 | 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] | 1970 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1971 | /* header value can be basically anything except CR/LF */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1972 | msg->sov = ptr - buf->p; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1973 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1974 | if (likely(!HTTP_IS_CRLF(*ptr))) { |
| 1975 | goto http_msg_hdr_val; |
| 1976 | } |
| 1977 | |
| 1978 | if (likely(*ptr == '\r')) |
| 1979 | EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_lf, HTTP_MSG_HDR_L1_LF); |
| 1980 | goto http_msg_hdr_l1_lf; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1981 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1982 | case HTTP_MSG_HDR_L1_LF: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1983 | http_msg_hdr_l1_lf: |
Willy Tarreau | 10e61cb | 2017-01-04 14:51:22 +0100 | [diff] [blame] | 1984 | EXPECT_LF_HERE(ptr, http_msg_invalid, HTTP_MSG_HDR_L1_LF); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1985 | 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] | 1986 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1987 | case HTTP_MSG_HDR_L1_LWS: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1988 | http_msg_hdr_l1_lws: |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1989 | if (likely(HTTP_IS_SPHT(*ptr))) { |
| 1990 | /* replace HT,CR,LF with spaces */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1991 | for (; buf->p + msg->sov < ptr; msg->sov++) |
| 1992 | buf->p[msg->sov] = ' '; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1993 | goto http_msg_hdr_l1_sp; |
| 1994 | } |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 1995 | /* we had a header consisting only in spaces ! */ |
Willy Tarreau | 12e48b3 | 2012-03-05 16:57:34 +0100 | [diff] [blame] | 1996 | msg->eol = msg->sov; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1997 | goto http_msg_complete_header; |
| 1998 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1999 | case HTTP_MSG_HDR_VAL: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 2000 | http_msg_hdr_val: |
Willy Tarreau | fa4a03c | 2012-03-09 21:28:54 +0100 | [diff] [blame] | 2001 | /* assumes msg->sol points to the first char, and msg->sov |
| 2002 | * points to the first character of the value. |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2003 | */ |
Willy Tarreau | 0431f9d | 2016-11-05 17:35:40 +0100 | [diff] [blame] | 2004 | |
| 2005 | /* speedup: we'll skip packs of 4 or 8 bytes not containing bytes 0x0D |
| 2006 | * and lower. In fact since most of the time is spent in the loop, we |
| 2007 | * also remove the sign bit test so that bytes 0x8e..0x0d break the |
| 2008 | * loop, but we don't care since they're very rare in header values. |
| 2009 | */ |
| 2010 | #if defined(__x86_64__) |
| 2011 | while (ptr <= end - sizeof(long)) { |
| 2012 | if ((*(long *)ptr - 0x0e0e0e0e0e0e0e0eULL) & 0x8080808080808080ULL) |
| 2013 | goto http_msg_hdr_val2; |
| 2014 | ptr += sizeof(long); |
| 2015 | } |
| 2016 | #endif |
| 2017 | #if defined(__x86_64__) || \ |
| 2018 | defined(__i386__) || defined(__i486__) || defined(__i586__) || defined(__i686__) || \ |
| 2019 | defined(__ARM_ARCH_7A__) |
| 2020 | while (ptr <= end - sizeof(int)) { |
| 2021 | if ((*(int*)ptr - 0x0e0e0e0e) & 0x80808080) |
| 2022 | goto http_msg_hdr_val2; |
| 2023 | ptr += sizeof(int); |
| 2024 | } |
| 2025 | #endif |
Willy Tarreau | 2afff9c | 2017-01-04 14:44:46 +0100 | [diff] [blame] | 2026 | if (ptr >= end) { |
| 2027 | state = HTTP_MSG_HDR_VAL; |
| 2028 | goto http_msg_ood; |
| 2029 | } |
Willy Tarreau | 0431f9d | 2016-11-05 17:35:40 +0100 | [diff] [blame] | 2030 | http_msg_hdr_val2: |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2031 | if (likely(!HTTP_IS_CRLF(*ptr))) |
Willy Tarreau | 0431f9d | 2016-11-05 17:35:40 +0100 | [diff] [blame] | 2032 | 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] | 2033 | |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 2034 | msg->eol = ptr - buf->p; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2035 | /* Note: we could also copy eol into ->eoh so that we have the |
| 2036 | * real header end in case it ends with lots of LWS, but is this |
| 2037 | * really needed ? |
| 2038 | */ |
| 2039 | if (likely(*ptr == '\r')) |
| 2040 | EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l2_lf, HTTP_MSG_HDR_L2_LF); |
| 2041 | goto http_msg_hdr_l2_lf; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 2042 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2043 | case HTTP_MSG_HDR_L2_LF: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 2044 | http_msg_hdr_l2_lf: |
Willy Tarreau | 10e61cb | 2017-01-04 14:51:22 +0100 | [diff] [blame] | 2045 | EXPECT_LF_HERE(ptr, http_msg_invalid, HTTP_MSG_HDR_L2_LF); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2046 | 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] | 2047 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2048 | case HTTP_MSG_HDR_L2_LWS: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 2049 | http_msg_hdr_l2_lws: |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2050 | if (unlikely(HTTP_IS_SPHT(*ptr))) { |
| 2051 | /* LWS: replace HT,CR,LF with spaces */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 2052 | for (; buf->p + msg->eol < ptr; msg->eol++) |
| 2053 | buf->p[msg->eol] = ' '; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2054 | goto http_msg_hdr_val; |
| 2055 | } |
| 2056 | http_msg_complete_header: |
| 2057 | /* |
| 2058 | * It was a new header, so the last one is finished. |
Willy Tarreau | fa4a03c | 2012-03-09 21:28:54 +0100 | [diff] [blame] | 2059 | * Assumes msg->sol points to the first char, msg->sov points |
| 2060 | * to the first character of the value and msg->eol to the |
| 2061 | * first CR or LF so we know how the line ends. We insert last |
| 2062 | * header into the index. |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2063 | */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 2064 | if (unlikely(hdr_idx_add(msg->eol - msg->sol, buf->p[msg->eol] == '\r', |
Willy Tarreau | 10e61cb | 2017-01-04 14:51:22 +0100 | [diff] [blame] | 2065 | idx, idx->tail) < 0)) { |
| 2066 | state = HTTP_MSG_HDR_L2_LWS; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2067 | goto http_msg_invalid; |
Willy Tarreau | 10e61cb | 2017-01-04 14:51:22 +0100 | [diff] [blame] | 2068 | } |
Willy Tarreau | 230fd0b | 2006-12-17 12:05:00 +0100 | [diff] [blame] | 2069 | |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 2070 | msg->sol = ptr - buf->p; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2071 | if (likely(!HTTP_IS_CRLF(*ptr))) { |
| 2072 | goto http_msg_hdr_name; |
| 2073 | } |
| 2074 | |
| 2075 | if (likely(*ptr == '\r')) |
| 2076 | EAT_AND_JUMP_OR_RETURN(http_msg_last_lf, HTTP_MSG_LAST_LF); |
| 2077 | goto http_msg_last_lf; |
Willy Tarreau | 230fd0b | 2006-12-17 12:05:00 +0100 | [diff] [blame] | 2078 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2079 | case HTTP_MSG_LAST_LF: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 2080 | http_msg_last_lf: |
Willy Tarreau | 0558a02 | 2014-03-13 15:48:45 +0100 | [diff] [blame] | 2081 | /* Assumes msg->sol points to the first of either CR or LF. |
| 2082 | * Sets ->sov and ->next to the total header length, ->eoh to |
| 2083 | * the last CRLF, and ->eol to the last CRLF length (1 or 2). |
| 2084 | */ |
Willy Tarreau | 10e61cb | 2017-01-04 14:51:22 +0100 | [diff] [blame] | 2085 | EXPECT_LF_HERE(ptr, http_msg_invalid, HTTP_MSG_LAST_LF); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2086 | ptr++; |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 2087 | msg->sov = msg->next = ptr - buf->p; |
Willy Tarreau | 3a215be | 2012-03-09 21:39:51 +0100 | [diff] [blame] | 2088 | msg->eoh = msg->sol; |
| 2089 | msg->sol = 0; |
Willy Tarreau | 0558a02 | 2014-03-13 15:48:45 +0100 | [diff] [blame] | 2090 | msg->eol = msg->sov - msg->eoh; |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 2091 | msg->msg_state = HTTP_MSG_BODY; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2092 | return; |
Willy Tarreau | b56928a | 2012-04-16 14:51:55 +0200 | [diff] [blame] | 2093 | |
| 2094 | case HTTP_MSG_ERROR: |
| 2095 | /* this may only happen if we call http_msg_analyser() twice with an error */ |
| 2096 | break; |
| 2097 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2098 | default: |
Willy Tarreau | 3770f23 | 2013-12-07 00:01:53 +0100 | [diff] [blame] | 2099 | #ifdef DEBUG_FULL |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2100 | fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state); |
| 2101 | exit(1); |
Willy Tarreau | 230fd0b | 2006-12-17 12:05:00 +0100 | [diff] [blame] | 2102 | #endif |
Willy Tarreau | 3770f23 | 2013-12-07 00:01:53 +0100 | [diff] [blame] | 2103 | ; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2104 | } |
| 2105 | http_msg_ood: |
| 2106 | /* out of data */ |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 2107 | msg->msg_state = state; |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 2108 | msg->next = ptr - buf->p; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2109 | return; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2110 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2111 | http_msg_invalid: |
| 2112 | /* invalid message */ |
Willy Tarreau | 10e61cb | 2017-01-04 14:51:22 +0100 | [diff] [blame] | 2113 | msg->err_state = state; |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 2114 | msg->msg_state = HTTP_MSG_ERROR; |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 2115 | msg->next = ptr - buf->p; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2116 | return; |
| 2117 | } |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 2118 | |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 2119 | /* convert an HTTP/0.9 request into an HTTP/1.0 request. Returns 1 if the |
| 2120 | * conversion succeeded, 0 in case of error. If the request was already 1.X, |
| 2121 | * nothing is done and 1 is returned. |
| 2122 | */ |
Willy Tarreau | 418bfcc | 2012-03-09 13:56:20 +0100 | [diff] [blame] | 2123 | static int http_upgrade_v09_to_v10(struct http_txn *txn) |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 2124 | { |
| 2125 | int delta; |
| 2126 | char *cur_end; |
Willy Tarreau | 418bfcc | 2012-03-09 13:56:20 +0100 | [diff] [blame] | 2127 | struct http_msg *msg = &txn->req; |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 2128 | |
| 2129 | if (msg->sl.rq.v_l != 0) |
| 2130 | return 1; |
| 2131 | |
Apollon Oikonomopoulos | 25a1522 | 2014-04-06 02:46:00 +0300 | [diff] [blame] | 2132 | /* RFC 1945 allows only GET for HTTP/0.9 requests */ |
| 2133 | if (txn->meth != HTTP_METH_GET) |
| 2134 | return 0; |
| 2135 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2136 | cur_end = msg->chn->buf->p + msg->sl.rq.l; |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 2137 | |
| 2138 | if (msg->sl.rq.u_l == 0) { |
Apollon Oikonomopoulos | 25a1522 | 2014-04-06 02:46:00 +0300 | [diff] [blame] | 2139 | /* HTTP/0.9 requests *must* have a request URI, per RFC 1945 */ |
| 2140 | return 0; |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 2141 | } |
| 2142 | /* add HTTP version */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2143 | 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] | 2144 | http_msg_move_end(msg, delta); |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 2145 | cur_end += delta; |
Willy Tarreau | 69d8c5d | 2012-05-08 09:44:41 +0200 | [diff] [blame] | 2146 | cur_end = (char *)http_parse_reqline(msg, |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 2147 | HTTP_MSG_RQMETH, |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2148 | msg->chn->buf->p, cur_end + 1, |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 2149 | NULL, NULL); |
| 2150 | if (unlikely(!cur_end)) |
| 2151 | return 0; |
| 2152 | |
| 2153 | /* we have a full HTTP/1.0 request now and we know that |
| 2154 | * we have either a CR or an LF at <ptr>. |
| 2155 | */ |
| 2156 | hdr_idx_set_start(&txn->hdr_idx, msg->sl.rq.l, *cur_end == '\r'); |
| 2157 | return 1; |
| 2158 | } |
| 2159 | |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 2160 | /* Parse the Connection: header of an HTTP request, looking for both "close" |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 2161 | * and "keep-alive" values. If we already know that some headers may safely |
| 2162 | * 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] | 2163 | * - bit 0 means remove "close" headers (in HTTP/1.0 requests/responses) |
| 2164 | * - 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] | 2165 | * Presence of the "Upgrade" token is also checked and reported. |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 2166 | * The TX_HDR_CONN_* flags are adjusted in txn->flags depending on what was |
| 2167 | * found, and TX_CON_*_SET is adjusted depending on what is left so only |
| 2168 | * harmless combinations may be removed. Do not call that after changes have |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 2169 | * been processed. |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 2170 | */ |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 2171 | 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] | 2172 | { |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 2173 | struct hdr_ctx ctx; |
Willy Tarreau | 88d349d | 2010-01-25 12:15:43 +0100 | [diff] [blame] | 2174 | const char *hdr_val = "Connection"; |
| 2175 | int hdr_len = 10; |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 2176 | |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 2177 | if (txn->flags & TX_HDR_CONN_PRS) |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 2178 | return; |
| 2179 | |
Willy Tarreau | 88d349d | 2010-01-25 12:15:43 +0100 | [diff] [blame] | 2180 | if (unlikely(txn->flags & TX_USE_PX_CONN)) { |
| 2181 | hdr_val = "Proxy-Connection"; |
| 2182 | hdr_len = 16; |
| 2183 | } |
| 2184 | |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 2185 | ctx.idx = 0; |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 2186 | txn->flags &= ~(TX_CON_KAL_SET|TX_CON_CLO_SET); |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2187 | 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] | 2188 | if (ctx.vlen >= 10 && word_match(ctx.line + ctx.val, ctx.vlen, "keep-alive", 10)) { |
| 2189 | txn->flags |= TX_HDR_CONN_KAL; |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 2190 | if (to_del & 2) |
| 2191 | http_remove_header2(msg, &txn->hdr_idx, &ctx); |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 2192 | else |
| 2193 | txn->flags |= TX_CON_KAL_SET; |
| 2194 | } |
| 2195 | else if (ctx.vlen >= 5 && word_match(ctx.line + ctx.val, ctx.vlen, "close", 5)) { |
| 2196 | txn->flags |= TX_HDR_CONN_CLO; |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 2197 | if (to_del & 1) |
| 2198 | http_remove_header2(msg, &txn->hdr_idx, &ctx); |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 2199 | else |
| 2200 | txn->flags |= TX_CON_CLO_SET; |
| 2201 | } |
Willy Tarreau | 50fc777 | 2012-11-11 22:19:57 +0100 | [diff] [blame] | 2202 | else if (ctx.vlen >= 7 && word_match(ctx.line + ctx.val, ctx.vlen, "upgrade", 7)) { |
| 2203 | txn->flags |= TX_HDR_CONN_UPG; |
| 2204 | } |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 2205 | } |
| 2206 | |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 2207 | txn->flags |= TX_HDR_CONN_PRS; |
| 2208 | return; |
| 2209 | } |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 2210 | |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 2211 | /* Apply desired changes on the Connection: header. Values may be removed and/or |
| 2212 | * added depending on the <wanted> flags, which are exclusively composed of |
| 2213 | * TX_CON_CLO_SET and TX_CON_KAL_SET, depending on what flags are desired. The |
| 2214 | * TX_CON_*_SET flags are adjusted in txn->flags depending on what is left. |
| 2215 | */ |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 2216 | 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] | 2217 | { |
| 2218 | struct hdr_ctx ctx; |
Willy Tarreau | 88d349d | 2010-01-25 12:15:43 +0100 | [diff] [blame] | 2219 | const char *hdr_val = "Connection"; |
| 2220 | int hdr_len = 10; |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 2221 | |
| 2222 | ctx.idx = 0; |
| 2223 | |
Willy Tarreau | 88d349d | 2010-01-25 12:15:43 +0100 | [diff] [blame] | 2224 | |
| 2225 | if (unlikely(txn->flags & TX_USE_PX_CONN)) { |
| 2226 | hdr_val = "Proxy-Connection"; |
| 2227 | hdr_len = 16; |
| 2228 | } |
| 2229 | |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 2230 | txn->flags &= ~(TX_CON_CLO_SET | TX_CON_KAL_SET); |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2231 | 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] | 2232 | if (ctx.vlen >= 10 && word_match(ctx.line + ctx.val, ctx.vlen, "keep-alive", 10)) { |
| 2233 | if (wanted & TX_CON_KAL_SET) |
| 2234 | txn->flags |= TX_CON_KAL_SET; |
| 2235 | else |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 2236 | http_remove_header2(msg, &txn->hdr_idx, &ctx); |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 2237 | } |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 2238 | else if (ctx.vlen >= 5 && word_match(ctx.line + ctx.val, ctx.vlen, "close", 5)) { |
| 2239 | if (wanted & TX_CON_CLO_SET) |
| 2240 | txn->flags |= TX_CON_CLO_SET; |
| 2241 | else |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 2242 | http_remove_header2(msg, &txn->hdr_idx, &ctx); |
Willy Tarreau | 0dfdf19 | 2010-01-05 11:33:11 +0100 | [diff] [blame] | 2243 | } |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 2244 | } |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 2245 | |
| 2246 | if (wanted == (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET))) |
| 2247 | return; |
| 2248 | |
| 2249 | if ((wanted & TX_CON_CLO_SET) && !(txn->flags & TX_CON_CLO_SET)) { |
| 2250 | txn->flags |= TX_CON_CLO_SET; |
Willy Tarreau | 88d349d | 2010-01-25 12:15:43 +0100 | [diff] [blame] | 2251 | hdr_val = "Connection: close"; |
| 2252 | hdr_len = 17; |
| 2253 | if (unlikely(txn->flags & TX_USE_PX_CONN)) { |
| 2254 | hdr_val = "Proxy-Connection: close"; |
| 2255 | hdr_len = 23; |
| 2256 | } |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 2257 | http_header_add_tail2(msg, &txn->hdr_idx, hdr_val, hdr_len); |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 2258 | } |
| 2259 | |
| 2260 | if ((wanted & TX_CON_KAL_SET) && !(txn->flags & TX_CON_KAL_SET)) { |
| 2261 | txn->flags |= TX_CON_KAL_SET; |
Willy Tarreau | 88d349d | 2010-01-25 12:15:43 +0100 | [diff] [blame] | 2262 | hdr_val = "Connection: keep-alive"; |
| 2263 | hdr_len = 22; |
| 2264 | if (unlikely(txn->flags & TX_USE_PX_CONN)) { |
| 2265 | hdr_val = "Proxy-Connection: keep-alive"; |
| 2266 | hdr_len = 28; |
| 2267 | } |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 2268 | http_header_add_tail2(msg, &txn->hdr_idx, hdr_val, hdr_len); |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 2269 | } |
| 2270 | return; |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 2271 | } |
| 2272 | |
Christopher Faulet | 113f7de | 2015-12-14 14:52:13 +0100 | [diff] [blame] | 2273 | /* Parse the chunk size at msg->next. Once done, caller should adjust ->next to |
| 2274 | * point to the first byte of data after the chunk size, so that we know we can |
| 2275 | * forward exactly msg->next bytes. msg->sol contains the exact number of bytes |
| 2276 | * forming the chunk size. That way it is always possible to differentiate |
| 2277 | * between the start of the body and the start of the data. Return the number |
| 2278 | * of byte parsed on success, 0 when some data is missing, <0 on error. Note: |
| 2279 | * 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] | 2280 | */ |
Willy Tarreau | 24e6d97 | 2012-10-26 00:49:52 +0200 | [diff] [blame] | 2281 | static inline int http_parse_chunk_size(struct http_msg *msg) |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 2282 | { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2283 | const struct buffer *buf = msg->chn->buf; |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 2284 | const char *ptr = b_ptr(buf, msg->next); |
Willy Tarreau | 4baf44b | 2012-03-09 14:10:20 +0100 | [diff] [blame] | 2285 | const char *ptr_old = ptr; |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 2286 | const char *end = buf->data + buf->size; |
| 2287 | const char *stop = bi_end(buf); |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 2288 | unsigned int chunk = 0; |
| 2289 | |
| 2290 | /* The chunk size is in the following form, though we are only |
| 2291 | * interested in the size and CRLF : |
| 2292 | * 1*HEXDIGIT *WSP *[ ';' extensions ] CRLF |
| 2293 | */ |
| 2294 | while (1) { |
| 2295 | int c; |
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 | c = hex2i(*ptr); |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 2299 | if (c < 0) /* not a hex digit anymore */ |
| 2300 | break; |
Willy Tarreau | 0161d62 | 2013-04-02 01:26:55 +0200 | [diff] [blame] | 2301 | if (unlikely(++ptr >= end)) |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 2302 | ptr = buf->data; |
Willy Tarreau | 431946e | 2012-02-24 19:20:12 +0100 | [diff] [blame] | 2303 | if (chunk & 0xF8000000) /* integer overflow will occur if result >= 2GB */ |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 2304 | goto error; |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 2305 | chunk = (chunk << 4) + c; |
| 2306 | } |
| 2307 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2308 | /* empty size not allowed */ |
Willy Tarreau | 0161d62 | 2013-04-02 01:26:55 +0200 | [diff] [blame] | 2309 | if (unlikely(ptr == ptr_old)) |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 2310 | goto error; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2311 | |
Willy Tarreau | 2235b26 | 2016-11-05 15:50:20 +0100 | [diff] [blame] | 2312 | while (HTTP_IS_SPHT(*ptr)) { |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2313 | if (++ptr >= end) |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 2314 | ptr = buf->data; |
Willy Tarreau | 0161d62 | 2013-04-02 01:26:55 +0200 | [diff] [blame] | 2315 | if (unlikely(ptr == stop)) |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 2316 | return 0; |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 2317 | } |
| 2318 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2319 | /* Up to there, we know that at least one byte is present at *ptr. Check |
| 2320 | * for the end of chunk size. |
| 2321 | */ |
| 2322 | while (1) { |
| 2323 | if (likely(HTTP_IS_CRLF(*ptr))) { |
| 2324 | /* we now have a CR or an LF at ptr */ |
| 2325 | if (likely(*ptr == '\r')) { |
| 2326 | if (++ptr >= end) |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 2327 | ptr = buf->data; |
Willy Tarreau | 363a5bb | 2012-03-02 20:14:45 +0100 | [diff] [blame] | 2328 | if (ptr == stop) |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2329 | return 0; |
| 2330 | } |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 2331 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2332 | if (*ptr != '\n') |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 2333 | goto error; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2334 | if (++ptr >= end) |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 2335 | ptr = buf->data; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2336 | /* done */ |
| 2337 | break; |
| 2338 | } |
| 2339 | else if (*ptr == ';') { |
| 2340 | /* chunk extension, ends at next CRLF */ |
| 2341 | if (++ptr >= end) |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 2342 | ptr = buf->data; |
Willy Tarreau | 363a5bb | 2012-03-02 20:14:45 +0100 | [diff] [blame] | 2343 | if (ptr == stop) |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 2344 | return 0; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2345 | |
| 2346 | while (!HTTP_IS_CRLF(*ptr)) { |
| 2347 | if (++ptr >= end) |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 2348 | ptr = buf->data; |
Willy Tarreau | 363a5bb | 2012-03-02 20:14:45 +0100 | [diff] [blame] | 2349 | if (ptr == stop) |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2350 | return 0; |
| 2351 | } |
| 2352 | /* we have a CRLF now, loop above */ |
| 2353 | continue; |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 2354 | } |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2355 | else |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 2356 | goto error; |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 2357 | } |
| 2358 | |
Christopher Faulet | 113f7de | 2015-12-14 14:52:13 +0100 | [diff] [blame] | 2359 | /* OK we found our CRLF and now <ptr> points to the next byte, which may |
| 2360 | * or may not be present. We save the number of bytes parsed into |
| 2361 | * msg->sol. |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 2362 | */ |
Willy Tarreau | c24715e | 2014-04-17 15:21:20 +0200 | [diff] [blame] | 2363 | msg->sol = ptr - ptr_old; |
Willy Tarreau | 0161d62 | 2013-04-02 01:26:55 +0200 | [diff] [blame] | 2364 | if (unlikely(ptr < ptr_old)) |
Willy Tarreau | c24715e | 2014-04-17 15:21:20 +0200 | [diff] [blame] | 2365 | msg->sol += buf->size; |
Willy Tarreau | 124d991 | 2011-03-01 20:30:48 +0100 | [diff] [blame] | 2366 | msg->chunk_len = chunk; |
| 2367 | msg->body_len += chunk; |
Christopher Faulet | 113f7de | 2015-12-14 14:52:13 +0100 | [diff] [blame] | 2368 | return msg->sol; |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 2369 | error: |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 2370 | msg->err_pos = buffer_count(buf, buf->p, ptr); |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 2371 | return -1; |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 2372 | } |
| 2373 | |
Christopher Faulet | 113f7de | 2015-12-14 14:52:13 +0100 | [diff] [blame] | 2374 | /* This function skips trailers in the buffer associated with HTTP message |
| 2375 | * <msg>. The first visited position is msg->next. If the end of the trailers is |
| 2376 | * found, the function returns >0. So, the caller can automatically schedul it |
| 2377 | * to be forwarded, and switch msg->msg_state to HTTP_MSG_DONE. If not enough |
| 2378 | * data are available, the function does not change anything except maybe |
| 2379 | * msg->sol if it could parse some lines, and returns zero. If a parse error |
| 2380 | * is encountered, the function returns < 0 and does not change anything except |
| 2381 | * maybe msg->sol. Note that the message must already be in HTTP_MSG_TRAILERS |
| 2382 | * state before calling this function, which implies that all non-trailers data |
| 2383 | * have already been scheduled for forwarding, and that msg->next exactly |
| 2384 | * matches the length of trailers already parsed and not forwarded. It is also |
| 2385 | * important to note that this function is designed to be able to parse wrapped |
| 2386 | * headers at end of buffer. |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2387 | */ |
Willy Tarreau | 24e6d97 | 2012-10-26 00:49:52 +0200 | [diff] [blame] | 2388 | static int http_forward_trailers(struct http_msg *msg) |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2389 | { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2390 | const struct buffer *buf = msg->chn->buf; |
Willy Tarreau | 4baf44b | 2012-03-09 14:10:20 +0100 | [diff] [blame] | 2391 | |
Christopher Faulet | 113f7de | 2015-12-14 14:52:13 +0100 | [diff] [blame] | 2392 | /* we have msg->next which points to next line. Look for CRLF. But |
| 2393 | * first, we reset msg->sol */ |
| 2394 | msg->sol = 0; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2395 | while (1) { |
Willy Tarreau | 4baf44b | 2012-03-09 14:10:20 +0100 | [diff] [blame] | 2396 | const char *p1 = NULL, *p2 = NULL; |
Christopher Faulet | 2fb2880 | 2015-12-01 10:40:57 +0100 | [diff] [blame] | 2397 | const char *start = b_ptr(buf, msg->next + msg->sol); |
| 2398 | const char *stop = bi_end(buf); |
| 2399 | const char *ptr = start; |
| 2400 | int bytes = 0; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2401 | |
| 2402 | /* scan current line and stop at LF or CRLF */ |
| 2403 | while (1) { |
Willy Tarreau | 363a5bb | 2012-03-02 20:14:45 +0100 | [diff] [blame] | 2404 | if (ptr == stop) |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2405 | return 0; |
| 2406 | |
| 2407 | if (*ptr == '\n') { |
| 2408 | if (!p1) |
| 2409 | p1 = ptr; |
| 2410 | p2 = ptr; |
| 2411 | break; |
| 2412 | } |
| 2413 | |
| 2414 | if (*ptr == '\r') { |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 2415 | if (p1) { |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 2416 | msg->err_pos = buffer_count(buf, buf->p, ptr); |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2417 | return -1; |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 2418 | } |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2419 | p1 = ptr; |
| 2420 | } |
| 2421 | |
| 2422 | ptr++; |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 2423 | if (ptr >= buf->data + buf->size) |
| 2424 | ptr = buf->data; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2425 | } |
| 2426 | |
| 2427 | /* after LF; point to beginning of next line */ |
| 2428 | p2++; |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 2429 | if (p2 >= buf->data + buf->size) |
| 2430 | p2 = buf->data; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2431 | |
Christopher Faulet | 2fb2880 | 2015-12-01 10:40:57 +0100 | [diff] [blame] | 2432 | bytes = p2 - start; |
Willy Tarreau | 638cd02 | 2010-01-03 07:42:04 +0100 | [diff] [blame] | 2433 | if (bytes < 0) |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 2434 | bytes += buf->size; |
Christopher Faulet | 2fb2880 | 2015-12-01 10:40:57 +0100 | [diff] [blame] | 2435 | msg->sol += bytes; |
Willy Tarreau | 638cd02 | 2010-01-03 07:42:04 +0100 | [diff] [blame] | 2436 | |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 2437 | /* LF/CRLF at beginning of line => end of trailers at p2. |
| 2438 | * Everything was scheduled for forwarding, there's nothing left |
Christopher Faulet | 2fb2880 | 2015-12-01 10:40:57 +0100 | [diff] [blame] | 2439 | * from this message. */ |
| 2440 | if (p1 == start) |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2441 | return 1; |
Christopher Faulet | 2fb2880 | 2015-12-01 10:40:57 +0100 | [diff] [blame] | 2442 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2443 | /* OK, next line then */ |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2444 | } |
| 2445 | } |
| 2446 | |
Christopher Faulet | 113f7de | 2015-12-14 14:52:13 +0100 | [diff] [blame] | 2447 | /* This function may be called only in HTTP_MSG_CHUNK_CRLF. It reads the CRLF or |
| 2448 | * a possible LF alone at the end of a chunk. The caller should adjust msg->next |
| 2449 | * in order to include this part into the next forwarding phase. Note that the |
| 2450 | * caller must ensure that ->p points to the first byte to parse. It returns |
| 2451 | * the number of bytes parsed on success, so the caller can set msg_state to |
| 2452 | * HTTP_MSG_CHUNK_SIZE. If not enough data are available, the function does not |
| 2453 | * change anything and returns zero. If a parse error is encountered, the |
| 2454 | * function returns < 0. Note: this function is designed to parse wrapped CRLF |
| 2455 | * at the end of the buffer. |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2456 | */ |
Willy Tarreau | 24e6d97 | 2012-10-26 00:49:52 +0200 | [diff] [blame] | 2457 | static inline int http_skip_chunk_crlf(struct http_msg *msg) |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2458 | { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2459 | const struct buffer *buf = msg->chn->buf; |
Willy Tarreau | 4baf44b | 2012-03-09 14:10:20 +0100 | [diff] [blame] | 2460 | const char *ptr; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2461 | int bytes; |
| 2462 | |
| 2463 | /* NB: we'll check data availabilty at the end. It's not a |
| 2464 | * problem because whatever we match first will be checked |
| 2465 | * against the correct length. |
| 2466 | */ |
| 2467 | bytes = 1; |
Willy Tarreau | 0669d7d | 2014-04-17 11:40:10 +0200 | [diff] [blame] | 2468 | ptr = b_ptr(buf, msg->next); |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2469 | if (*ptr == '\r') { |
| 2470 | bytes++; |
| 2471 | ptr++; |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 2472 | if (ptr >= buf->data + buf->size) |
| 2473 | ptr = buf->data; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2474 | } |
| 2475 | |
Willy Tarreau | 0669d7d | 2014-04-17 11:40:10 +0200 | [diff] [blame] | 2476 | if (msg->next + bytes > buf->i) |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2477 | return 0; |
| 2478 | |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 2479 | if (*ptr != '\n') { |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 2480 | msg->err_pos = buffer_count(buf, buf->p, ptr); |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2481 | return -1; |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 2482 | } |
Christopher Faulet | 113f7de | 2015-12-14 14:52:13 +0100 | [diff] [blame] | 2483 | return bytes; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2484 | } |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 2485 | |
Willy Tarreau | 0e9b1b4 | 2014-03-19 12:07:52 +0100 | [diff] [blame] | 2486 | /* Parses a qvalue and returns it multipled by 1000, from 0 to 1000. If the |
| 2487 | * value is larger than 1000, it is bound to 1000. The parser consumes up to |
| 2488 | * 1 digit, one dot and 3 digits and stops on the first invalid character. |
| 2489 | * Unparsable qvalues return 1000 as "q=1.000". |
| 2490 | */ |
Thierry FOURNIER | ad90351 | 2014-04-11 17:51:01 +0200 | [diff] [blame] | 2491 | int parse_qvalue(const char *qvalue, const char **end) |
Willy Tarreau | 0e9b1b4 | 2014-03-19 12:07:52 +0100 | [diff] [blame] | 2492 | { |
| 2493 | int q = 1000; |
| 2494 | |
Willy Tarreau | 506c69a | 2014-07-08 00:59:48 +0200 | [diff] [blame] | 2495 | if (!isdigit((unsigned char)*qvalue)) |
Willy Tarreau | 0e9b1b4 | 2014-03-19 12:07:52 +0100 | [diff] [blame] | 2496 | goto out; |
| 2497 | q = (*qvalue++ - '0') * 1000; |
| 2498 | |
| 2499 | if (*qvalue++ != '.') |
| 2500 | goto out; |
| 2501 | |
Willy Tarreau | 506c69a | 2014-07-08 00:59:48 +0200 | [diff] [blame] | 2502 | if (!isdigit((unsigned char)*qvalue)) |
Willy Tarreau | 0e9b1b4 | 2014-03-19 12:07:52 +0100 | [diff] [blame] | 2503 | goto out; |
| 2504 | q += (*qvalue++ - '0') * 100; |
| 2505 | |
Willy Tarreau | 506c69a | 2014-07-08 00:59:48 +0200 | [diff] [blame] | 2506 | if (!isdigit((unsigned char)*qvalue)) |
Willy Tarreau | 0e9b1b4 | 2014-03-19 12:07:52 +0100 | [diff] [blame] | 2507 | goto out; |
| 2508 | q += (*qvalue++ - '0') * 10; |
| 2509 | |
Willy Tarreau | 506c69a | 2014-07-08 00:59:48 +0200 | [diff] [blame] | 2510 | if (!isdigit((unsigned char)*qvalue)) |
Willy Tarreau | 0e9b1b4 | 2014-03-19 12:07:52 +0100 | [diff] [blame] | 2511 | goto out; |
| 2512 | q += (*qvalue++ - '0') * 1; |
| 2513 | out: |
| 2514 | if (q > 1000) |
| 2515 | q = 1000; |
Willy Tarreau | 38b3aa5 | 2014-04-22 23:32:05 +0200 | [diff] [blame] | 2516 | if (end) |
Thierry FOURNIER | ad90351 | 2014-04-11 17:51:01 +0200 | [diff] [blame] | 2517 | *end = qvalue; |
Willy Tarreau | 0e9b1b4 | 2014-03-19 12:07:52 +0100 | [diff] [blame] | 2518 | return q; |
| 2519 | } |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 2520 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 2521 | 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] | 2522 | { |
Willy Tarreau | d0d8da9 | 2015-04-04 02:10:38 +0200 | [diff] [blame] | 2523 | struct proxy *fe = strm_fe(s); |
Willy Tarreau | 4e21ff9 | 2014-09-30 18:44:22 +0200 | [diff] [blame] | 2524 | int tmp = TX_CON_WANT_KAL; |
| 2525 | |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 2526 | if (!((fe->options2|s->be->options2) & PR_O2_FAKE_KA)) { |
| 2527 | if ((fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_TUN || |
Willy Tarreau | 4e21ff9 | 2014-09-30 18:44:22 +0200 | [diff] [blame] | 2528 | (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_TUN) |
| 2529 | tmp = TX_CON_WANT_TUN; |
| 2530 | |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 2531 | if ((fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL || |
Willy Tarreau | 4e21ff9 | 2014-09-30 18:44:22 +0200 | [diff] [blame] | 2532 | (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL) |
| 2533 | tmp = TX_CON_WANT_TUN; |
| 2534 | } |
| 2535 | |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 2536 | if ((fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_SCL || |
Willy Tarreau | 4e21ff9 | 2014-09-30 18:44:22 +0200 | [diff] [blame] | 2537 | (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_SCL) { |
| 2538 | /* option httpclose + server_close => forceclose */ |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 2539 | if ((fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL || |
Willy Tarreau | 4e21ff9 | 2014-09-30 18:44:22 +0200 | [diff] [blame] | 2540 | (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL) |
| 2541 | tmp = TX_CON_WANT_CLO; |
| 2542 | else |
| 2543 | tmp = TX_CON_WANT_SCL; |
| 2544 | } |
| 2545 | |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 2546 | if ((fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_FCL || |
Willy Tarreau | 4e21ff9 | 2014-09-30 18:44:22 +0200 | [diff] [blame] | 2547 | (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_FCL) |
| 2548 | tmp = TX_CON_WANT_CLO; |
| 2549 | |
| 2550 | if ((txn->flags & TX_CON_WANT_MSK) < tmp) |
| 2551 | txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | tmp; |
| 2552 | |
| 2553 | if (!(txn->flags & TX_HDR_CONN_PRS) && |
| 2554 | (txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) { |
| 2555 | /* parse the Connection header and possibly clean it */ |
| 2556 | int to_del = 0; |
| 2557 | if ((msg->flags & HTTP_MSGF_VER_11) || |
| 2558 | ((txn->flags & TX_CON_WANT_MSK) >= TX_CON_WANT_SCL && |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 2559 | !((fe->options2|s->be->options2) & PR_O2_FAKE_KA))) |
Willy Tarreau | 4e21ff9 | 2014-09-30 18:44:22 +0200 | [diff] [blame] | 2560 | to_del |= 2; /* remove "keep-alive" */ |
| 2561 | if (!(msg->flags & HTTP_MSGF_VER_11)) |
| 2562 | to_del |= 1; /* remove "close" */ |
| 2563 | http_parse_connection_header(txn, msg, to_del); |
| 2564 | } |
| 2565 | |
| 2566 | /* check if client or config asks for explicit close in KAL/SCL */ |
| 2567 | if (((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL || |
| 2568 | (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) && |
| 2569 | ((txn->flags & TX_HDR_CONN_CLO) || /* "connection: close" */ |
| 2570 | (!(msg->flags & HTTP_MSGF_VER_11) && !(txn->flags & TX_HDR_CONN_KAL)) || /* no "connection: k-a" in 1.0 */ |
| 2571 | !(msg->flags & HTTP_MSGF_XFER_LEN) || /* no length known => close */ |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 2572 | fe->state == PR_STSTOPPED)) /* frontend is stopping */ |
Willy Tarreau | 4e21ff9 | 2014-09-30 18:44:22 +0200 | [diff] [blame] | 2573 | txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO; |
| 2574 | } |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 2575 | |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 2576 | /* This stream analyser waits for a complete HTTP request. It returns 1 if the |
| 2577 | * processing can continue on next analysers, or zero if it either needs more |
| 2578 | * data or wants to immediately abort the request (eg: timeout, error, ...). It |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 2579 | * 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] | 2580 | * when it has nothing left to do, and may remove any analyser when it wants to |
| 2581 | * abort. |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2582 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 2583 | 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] | 2584 | { |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2585 | /* |
| 2586 | * We will parse the partial (or complete) lines. |
| 2587 | * We will check the request syntax, and also join multi-line |
| 2588 | * headers. An index of all the lines will be elaborated while |
| 2589 | * parsing. |
| 2590 | * |
| 2591 | * For the parsing, we use a 28 states FSM. |
| 2592 | * |
| 2593 | * Here is the information we currently have : |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2594 | * req->buf->p = beginning of request |
| 2595 | * req->buf->p + msg->eoh = end of processed headers / start of current one |
| 2596 | * req->buf->p + req->buf->i = end of input data |
Willy Tarreau | 2692736 | 2012-05-18 23:22:52 +0200 | [diff] [blame] | 2597 | * msg->eol = end of current header or line (LF or CRLF) |
| 2598 | * msg->next = first non-visited byte |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 2599 | * |
| 2600 | * 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] | 2601 | * we will set a few fields (txn->meth, sn->flags/SF_REDIRECTABLE). |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 2602 | * We also check for monitor-uri, logging, HTTP/0.9 to 1.0 conversion, and |
| 2603 | * finally headers capture. |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2604 | */ |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 2605 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2606 | int cur_idx; |
Willy Tarreau | fb0afa7 | 2015-04-03 14:46:27 +0200 | [diff] [blame] | 2607 | struct session *sess = s->sess; |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 2608 | struct http_txn *txn = s->txn; |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2609 | struct http_msg *msg = &txn->req; |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 2610 | struct hdr_ctx ctx; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 2611 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 2612 | 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] | 2613 | now_ms, __FUNCTION__, |
| 2614 | s, |
| 2615 | req, |
| 2616 | req->rex, req->wex, |
| 2617 | req->flags, |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2618 | req->buf->i, |
Willy Tarreau | 6bf1736 | 2009-02-24 10:48:35 +0100 | [diff] [blame] | 2619 | req->analysers); |
| 2620 | |
Willy Tarreau | 52a0c60 | 2009-08-16 22:45:38 +0200 | [diff] [blame] | 2621 | /* we're speaking HTTP here, so let's speak HTTP to the client */ |
| 2622 | s->srv_error = http_return_srv_error; |
| 2623 | |
Willy Tarreau | 83e3af0 | 2009-12-28 17:39:57 +0100 | [diff] [blame] | 2624 | /* There's a protected area at the end of the buffer for rewriting |
| 2625 | * purposes. We don't want to start to parse the request if the |
| 2626 | * protected area is affected, because we may have to move processed |
| 2627 | * data later, which is much more complicated. |
| 2628 | */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2629 | 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] | 2630 | |
| 2631 | /* This point is executed when some data is avalaible for analysis, |
| 2632 | * so we log the end of the idle time. */ |
| 2633 | if (s->logs.t_idle == -1) |
| 2634 | s->logs.t_idle = tv_ms_elapsed(&s->logs.tv_accept, &now) - s->logs.t_handshake; |
| 2635 | |
Willy Tarreau | 379357a | 2013-06-08 12:55:46 +0200 | [diff] [blame] | 2636 | if (txn->flags & TX_NOT_FIRST) { |
Willy Tarreau | ba0902e | 2015-01-13 14:39:16 +0100 | [diff] [blame] | 2637 | if (unlikely(!channel_is_rewritable(req))) { |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 2638 | 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] | 2639 | goto failed_keep_alive; |
Willy Tarreau | 2ab6eb1 | 2010-01-02 22:04:45 +0100 | [diff] [blame] | 2640 | /* 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] | 2641 | channel_dont_connect(req); |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 2642 | req->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */ |
Willy Tarreau | d7ad9f5 | 2013-12-31 17:26:25 +0100 | [diff] [blame] | 2643 | req->flags |= CF_WAKE_WRITE; |
Willy Tarreau | 2ab6eb1 | 2010-01-02 22:04:45 +0100 | [diff] [blame] | 2644 | return 0; |
| 2645 | } |
Willy Tarreau | 379357a | 2013-06-08 12:55:46 +0200 | [diff] [blame] | 2646 | if (unlikely(bi_end(req->buf) < b_ptr(req->buf, msg->next) || |
| 2647 | bi_end(req->buf) > req->buf->data + req->buf->size - global.tune.maxrewrite)) |
| 2648 | buffer_slow_realign(req->buf); |
Willy Tarreau | 83e3af0 | 2009-12-28 17:39:57 +0100 | [diff] [blame] | 2649 | } |
| 2650 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2651 | if (likely(msg->next < req->buf->i)) /* some unparsed data are available */ |
Willy Tarreau | a560c21 | 2012-03-09 13:50:57 +0100 | [diff] [blame] | 2652 | http_msg_analyzer(msg, &txn->hdr_idx); |
Willy Tarreau | 83e3af0 | 2009-12-28 17:39:57 +0100 | [diff] [blame] | 2653 | } |
| 2654 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2655 | /* 1: we might have to print this header in debug mode */ |
| 2656 | if (unlikely((global.mode & MODE_DEBUG) && |
| 2657 | (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) && |
Willy Tarreau | 7d59e90 | 2014-10-21 19:36:09 +0200 | [diff] [blame] | 2658 | msg->msg_state >= HTTP_MSG_BODY)) { |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2659 | char *eol, *sol; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2660 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2661 | sol = req->buf->p; |
Willy Tarreau | e92693a | 2012-09-24 21:13:39 +0200 | [diff] [blame] | 2662 | /* this is a bit complex : in case of error on the request line, |
| 2663 | * we know that rq.l is still zero, so we display only the part |
| 2664 | * up to the end of the line (truncated by debug_hdr). |
| 2665 | */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2666 | 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] | 2667 | debug_hdr("clireq", s, sol, eol); |
Willy Tarreau | 45e73e3 | 2006-12-17 00:05:15 +0100 | [diff] [blame] | 2668 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2669 | sol += hdr_idx_first_pos(&txn->hdr_idx); |
| 2670 | cur_idx = hdr_idx_first_idx(&txn->hdr_idx); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2671 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2672 | while (cur_idx) { |
| 2673 | eol = sol + txn->hdr_idx.v[cur_idx].len; |
| 2674 | debug_hdr("clihdr", s, sol, eol); |
| 2675 | sol = eol + txn->hdr_idx.v[cur_idx].cr + 1; |
| 2676 | cur_idx = txn->hdr_idx.v[cur_idx].next; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2677 | } |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2678 | } |
| 2679 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2680 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2681 | /* |
| 2682 | * Now we quickly check if we have found a full valid request. |
| 2683 | * If not so, we check the FD and buffer states before leaving. |
| 2684 | * A full request is indicated by the fact that we have seen |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 2685 | * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid |
Willy Tarreau | d3c343f | 2010-01-16 10:26:19 +0100 | [diff] [blame] | 2686 | * requests are checked first. When waiting for a second request |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 2687 | * on a keep-alive stream, if we encounter and error, close, t/o, |
| 2688 | * 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] | 2689 | * 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] | 2690 | * process_stream() as a frontend error. |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 2691 | * Last, we may increase some tracked counters' http request errors on |
| 2692 | * the cases that are deliberately the client's fault. For instance, |
| 2693 | * a timeout or connection reset is not counted as an error. However |
| 2694 | * a bad request is. |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2695 | */ |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2696 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 2697 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2698 | /* |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2699 | * First, let's catch bad requests. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2700 | */ |
Willy Tarreau | 3e1b6d1 | 2010-03-04 23:02:38 +0100 | [diff] [blame] | 2701 | if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) { |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 2702 | stream_inc_http_req_ctr(s); |
| 2703 | stream_inc_http_err_ctr(s); |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 2704 | proxy_inc_fe_req_ctr(sess->fe); |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2705 | goto return_bad_req; |
Willy Tarreau | 3e1b6d1 | 2010-03-04 23:02:38 +0100 | [diff] [blame] | 2706 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2707 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2708 | /* 1: Since we are in header mode, if there's no space |
| 2709 | * left for headers, we won't be able to free more |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 2710 | * later, so the stream will never terminate. We |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2711 | * must terminate it now. |
| 2712 | */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2713 | if (unlikely(buffer_full(req->buf, global.tune.maxrewrite))) { |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2714 | /* FIXME: check if URI is set and return Status |
| 2715 | * 414 Request URI too long instead. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2716 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 2717 | stream_inc_http_req_ctr(s); |
| 2718 | stream_inc_http_err_ctr(s); |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 2719 | proxy_inc_fe_req_ctr(sess->fe); |
Willy Tarreau | fec4d89 | 2011-09-02 20:04:57 +0200 | [diff] [blame] | 2720 | if (msg->err_pos < 0) |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2721 | msg->err_pos = req->buf->i; |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2722 | goto return_bad_req; |
| 2723 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2724 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2725 | /* 2: have we encountered a read error ? */ |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 2726 | else if (req->flags & CF_READ_ERROR) { |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 2727 | if (!(s->flags & SF_ERR_MASK)) |
| 2728 | s->flags |= SF_ERR_CLICL; |
Willy Tarreau | d3c343f | 2010-01-16 10:26:19 +0100 | [diff] [blame] | 2729 | |
Willy Tarreau | fcffa69 | 2010-01-10 14:21:19 +0100 | [diff] [blame] | 2730 | if (txn->flags & TX_WAIT_NEXT_RQ) |
Willy Tarreau | b608feb | 2010-01-02 22:47:18 +0100 | [diff] [blame] | 2731 | goto failed_keep_alive; |
| 2732 | |
Willy Tarreau | 0f228a0 | 2015-05-01 15:37:53 +0200 | [diff] [blame] | 2733 | if (sess->fe->options & PR_O_IGNORE_PRB) |
| 2734 | goto failed_keep_alive; |
| 2735 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2736 | /* we cannot return any message on error */ |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 2737 | if (msg->err_pos >= 0) { |
Willy Tarreau | 10e61cb | 2017-01-04 14:51:22 +0100 | [diff] [blame] | 2738 | http_capture_bad_message(&sess->fe->invalid_req, s, msg, msg->err_state, sess->fe); |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 2739 | stream_inc_http_err_ctr(s); |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 2740 | } |
| 2741 | |
Willy Tarreau | dc979f2 | 2012-12-04 10:39:01 +0100 | [diff] [blame] | 2742 | txn->status = 400; |
Willy Tarreau | 10e61cb | 2017-01-04 14:51:22 +0100 | [diff] [blame] | 2743 | msg->err_state = msg->msg_state; |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2744 | msg->msg_state = HTTP_MSG_ERROR; |
Christopher Faulet | a94e5a5 | 2015-12-09 15:55:06 +0100 | [diff] [blame] | 2745 | http_reply_and_close(s, txn->status, NULL); |
Christopher Faulet | 0184ea7 | 2017-01-05 14:06:34 +0100 | [diff] [blame] | 2746 | req->analysers &= AN_REQ_FLT_END; |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 2747 | stream_inc_http_req_ctr(s); |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 2748 | proxy_inc_fe_req_ctr(sess->fe); |
| 2749 | sess->fe->fe_counters.failed_req++; |
Willy Tarreau | fb0afa7 | 2015-04-03 14:46:27 +0200 | [diff] [blame] | 2750 | if (sess->listener->counters) |
| 2751 | sess->listener->counters->failed_req++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 2752 | |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 2753 | if (!(s->flags & SF_FINST_MASK)) |
| 2754 | s->flags |= SF_FINST_R; |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2755 | return 0; |
| 2756 | } |
Willy Tarreau | f9839bd | 2008-08-27 23:57:16 +0200 | [diff] [blame] | 2757 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2758 | /* 3: has the read timeout expired ? */ |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 2759 | 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] | 2760 | if (!(s->flags & SF_ERR_MASK)) |
| 2761 | s->flags |= SF_ERR_CLITO; |
Willy Tarreau | d3c343f | 2010-01-16 10:26:19 +0100 | [diff] [blame] | 2762 | |
Willy Tarreau | fcffa69 | 2010-01-10 14:21:19 +0100 | [diff] [blame] | 2763 | if (txn->flags & TX_WAIT_NEXT_RQ) |
Willy Tarreau | b608feb | 2010-01-02 22:47:18 +0100 | [diff] [blame] | 2764 | goto failed_keep_alive; |
| 2765 | |
Willy Tarreau | 0f228a0 | 2015-05-01 15:37:53 +0200 | [diff] [blame] | 2766 | if (sess->fe->options & PR_O_IGNORE_PRB) |
| 2767 | goto failed_keep_alive; |
| 2768 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2769 | /* read timeout : give up with an error message. */ |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 2770 | if (msg->err_pos >= 0) { |
Willy Tarreau | 10e61cb | 2017-01-04 14:51:22 +0100 | [diff] [blame] | 2771 | http_capture_bad_message(&sess->fe->invalid_req, s, msg, msg->err_state, sess->fe); |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 2772 | stream_inc_http_err_ctr(s); |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 2773 | } |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2774 | txn->status = 408; |
Willy Tarreau | 10e61cb | 2017-01-04 14:51:22 +0100 | [diff] [blame] | 2775 | msg->err_state = msg->msg_state; |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2776 | msg->msg_state = HTTP_MSG_ERROR; |
Jarno Huuskonen | 9e6906b | 2017-03-06 14:21:49 +0200 | [diff] [blame] | 2777 | http_reply_and_close(s, txn->status, http_error_message(s)); |
Christopher Faulet | 0184ea7 | 2017-01-05 14:06:34 +0100 | [diff] [blame] | 2778 | req->analysers &= AN_REQ_FLT_END; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 2779 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 2780 | stream_inc_http_req_ctr(s); |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 2781 | proxy_inc_fe_req_ctr(sess->fe); |
| 2782 | sess->fe->fe_counters.failed_req++; |
Willy Tarreau | fb0afa7 | 2015-04-03 14:46:27 +0200 | [diff] [blame] | 2783 | if (sess->listener->counters) |
| 2784 | sess->listener->counters->failed_req++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 2785 | |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 2786 | if (!(s->flags & SF_FINST_MASK)) |
| 2787 | s->flags |= SF_FINST_R; |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2788 | return 0; |
| 2789 | } |
Willy Tarreau | c65a3ba | 2008-08-11 23:42:50 +0200 | [diff] [blame] | 2790 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2791 | /* 4: have we encountered a close ? */ |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 2792 | else if (req->flags & CF_SHUTR) { |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 2793 | if (!(s->flags & SF_ERR_MASK)) |
| 2794 | s->flags |= SF_ERR_CLICL; |
Willy Tarreau | d3c343f | 2010-01-16 10:26:19 +0100 | [diff] [blame] | 2795 | |
Willy Tarreau | fcffa69 | 2010-01-10 14:21:19 +0100 | [diff] [blame] | 2796 | if (txn->flags & TX_WAIT_NEXT_RQ) |
Willy Tarreau | b608feb | 2010-01-02 22:47:18 +0100 | [diff] [blame] | 2797 | goto failed_keep_alive; |
| 2798 | |
Willy Tarreau | 0f228a0 | 2015-05-01 15:37:53 +0200 | [diff] [blame] | 2799 | if (sess->fe->options & PR_O_IGNORE_PRB) |
| 2800 | goto failed_keep_alive; |
| 2801 | |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 2802 | if (msg->err_pos >= 0) |
Willy Tarreau | 10e61cb | 2017-01-04 14:51:22 +0100 | [diff] [blame] | 2803 | http_capture_bad_message(&sess->fe->invalid_req, s, msg, msg->err_state, sess->fe); |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2804 | txn->status = 400; |
Willy Tarreau | 10e61cb | 2017-01-04 14:51:22 +0100 | [diff] [blame] | 2805 | msg->err_state = msg->msg_state; |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2806 | msg->msg_state = HTTP_MSG_ERROR; |
Jarno Huuskonen | 9e6906b | 2017-03-06 14:21:49 +0200 | [diff] [blame] | 2807 | http_reply_and_close(s, txn->status, http_error_message(s)); |
Christopher Faulet | 0184ea7 | 2017-01-05 14:06:34 +0100 | [diff] [blame] | 2808 | req->analysers &= AN_REQ_FLT_END; |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 2809 | stream_inc_http_err_ctr(s); |
| 2810 | stream_inc_http_req_ctr(s); |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 2811 | proxy_inc_fe_req_ctr(sess->fe); |
| 2812 | sess->fe->fe_counters.failed_req++; |
Willy Tarreau | fb0afa7 | 2015-04-03 14:46:27 +0200 | [diff] [blame] | 2813 | if (sess->listener->counters) |
| 2814 | sess->listener->counters->failed_req++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 2815 | |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 2816 | if (!(s->flags & SF_FINST_MASK)) |
| 2817 | s->flags |= SF_FINST_R; |
Willy Tarreau | dafde43 | 2008-08-17 01:00:46 +0200 | [diff] [blame] | 2818 | return 0; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2819 | } |
| 2820 | |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 2821 | channel_dont_connect(req); |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 2822 | req->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */ |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 2823 | s->res.flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */ |
Willy Tarreau | 5e20552 | 2011-12-17 16:34:27 +0100 | [diff] [blame] | 2824 | #ifdef TCP_QUICKACK |
Willy Tarreau | 9ad7bd4 | 2015-04-03 19:19:59 +0200 | [diff] [blame] | 2825 | if (sess->listener->options & LI_O_NOQUICKACK && req->buf->i && |
| 2826 | objt_conn(sess->origin) && conn_ctrl_ready(__objt_conn(sess->origin))) { |
Willy Tarreau | 5e20552 | 2011-12-17 16:34:27 +0100 | [diff] [blame] | 2827 | /* We need more data, we have to re-enable quick-ack in case we |
| 2828 | * previously disabled it, otherwise we might cause the client |
| 2829 | * to delay next data. |
| 2830 | */ |
Willy Tarreau | 9ad7bd4 | 2015-04-03 19:19:59 +0200 | [diff] [blame] | 2831 | 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] | 2832 | } |
| 2833 | #endif |
Willy Tarreau | 1b194fe | 2009-03-21 21:10:04 +0100 | [diff] [blame] | 2834 | |
Willy Tarreau | fcffa69 | 2010-01-10 14:21:19 +0100 | [diff] [blame] | 2835 | if ((msg->msg_state != HTTP_MSG_RQBEFORE) && (txn->flags & TX_WAIT_NEXT_RQ)) { |
| 2836 | /* If the client starts to talk, let's fall back to |
| 2837 | * request timeout processing. |
| 2838 | */ |
| 2839 | txn->flags &= ~TX_WAIT_NEXT_RQ; |
Willy Tarreau | b16a574 | 2010-01-10 14:46:16 +0100 | [diff] [blame] | 2840 | req->analyse_exp = TICK_ETERNITY; |
Willy Tarreau | fcffa69 | 2010-01-10 14:21:19 +0100 | [diff] [blame] | 2841 | } |
| 2842 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2843 | /* just set the request timeout once at the beginning of the request */ |
Willy Tarreau | b16a574 | 2010-01-10 14:46:16 +0100 | [diff] [blame] | 2844 | if (!tick_isset(req->analyse_exp)) { |
| 2845 | if ((msg->msg_state == HTTP_MSG_RQBEFORE) && |
| 2846 | (txn->flags & TX_WAIT_NEXT_RQ) && |
| 2847 | tick_isset(s->be->timeout.httpka)) |
| 2848 | req->analyse_exp = tick_add(now_ms, s->be->timeout.httpka); |
| 2849 | else |
| 2850 | req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.httpreq); |
| 2851 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2852 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2853 | /* we're not ready yet */ |
| 2854 | return 0; |
Willy Tarreau | b608feb | 2010-01-02 22:47:18 +0100 | [diff] [blame] | 2855 | |
| 2856 | failed_keep_alive: |
| 2857 | /* Here we process low-level errors for keep-alive requests. In |
| 2858 | * short, if the request is not the first one and it experiences |
| 2859 | * a timeout, read error or shutdown, we just silently close so |
| 2860 | * that the client can try again. |
| 2861 | */ |
| 2862 | txn->status = 0; |
| 2863 | msg->msg_state = HTTP_MSG_RQBEFORE; |
Christopher Faulet | 0184ea7 | 2017-01-05 14:06:34 +0100 | [diff] [blame] | 2864 | req->analysers &= AN_REQ_FLT_END; |
Willy Tarreau | b608feb | 2010-01-02 22:47:18 +0100 | [diff] [blame] | 2865 | s->logs.logwait = 0; |
Willy Tarreau | abcd514 | 2013-06-11 17:18:02 +0200 | [diff] [blame] | 2866 | s->logs.level = 0; |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 2867 | s->res.flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */ |
Christopher Faulet | a94e5a5 | 2015-12-09 15:55:06 +0100 | [diff] [blame] | 2868 | http_reply_and_close(s, txn->status, NULL); |
Willy Tarreau | b608feb | 2010-01-02 22:47:18 +0100 | [diff] [blame] | 2869 | return 0; |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2870 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2871 | |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 2872 | /* OK now we have a complete HTTP request with indexed headers. Let's |
| 2873 | * complete the request parsing by setting a few fields we will need |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2874 | * 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] | 2875 | * 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] | 2876 | * 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] | 2877 | * byte after the last LF. msg->sov points to the first byte of data. |
| 2878 | * msg->eol cannot be trusted because it may have been left uninitialized |
| 2879 | * (for instance in the absence of headers). |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 2880 | */ |
Willy Tarreau | 9cdde23 | 2007-05-02 20:58:19 +0200 | [diff] [blame] | 2881 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 2882 | stream_inc_http_req_ctr(s); |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 2883 | 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] | 2884 | |
Willy Tarreau | b16a574 | 2010-01-10 14:46:16 +0100 | [diff] [blame] | 2885 | if (txn->flags & TX_WAIT_NEXT_RQ) { |
| 2886 | /* kill the pending keep-alive timeout */ |
| 2887 | txn->flags &= ~TX_WAIT_NEXT_RQ; |
| 2888 | req->analyse_exp = TICK_ETERNITY; |
| 2889 | } |
| 2890 | |
| 2891 | |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 2892 | /* Maybe we found in invalid header name while we were configured not |
| 2893 | * to block on that, so we have to capture it now. |
| 2894 | */ |
| 2895 | if (unlikely(msg->err_pos >= 0)) |
Willy Tarreau | 10e61cb | 2017-01-04 14:51:22 +0100 | [diff] [blame] | 2896 | http_capture_bad_message(&sess->fe->invalid_req, s, msg, msg->err_state, sess->fe); |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 2897 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2898 | /* |
| 2899 | * 1: identify the method |
| 2900 | */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2901 | 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] | 2902 | |
| 2903 | /* we can make use of server redirect on GET and HEAD */ |
| 2904 | if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD) |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 2905 | s->flags |= SF_REDIRECTABLE; |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 2906 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2907 | /* |
| 2908 | * 2: check if the URI matches the monitor_uri. |
| 2909 | * We have to do this for every request which gets in, because |
| 2910 | * the monitor-uri is defined by the frontend. |
| 2911 | */ |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 2912 | if (unlikely((sess->fe->monitor_uri_len != 0) && |
| 2913 | (sess->fe->monitor_uri_len == msg->sl.rq.u_l) && |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2914 | !memcmp(req->buf->p + msg->sl.rq.u, |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 2915 | sess->fe->monitor_uri, |
| 2916 | sess->fe->monitor_uri_len))) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2917 | /* |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2918 | * We have found the monitor URI |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2919 | */ |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2920 | struct acl_cond *cond; |
Willy Tarreau | b80c230 | 2007-11-30 20:51:32 +0100 | [diff] [blame] | 2921 | |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 2922 | s->flags |= SF_MONITOR; |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 2923 | sess->fe->fe_counters.intercepted_req++; |
Willy Tarreau | b80c230 | 2007-11-30 20:51:32 +0100 | [diff] [blame] | 2924 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2925 | /* Check if we want to fail this monitor request or not */ |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 2926 | list_for_each_entry(cond, &sess->fe->mon_fail_cond, list) { |
Willy Tarreau | 192252e | 2015-04-04 01:47:55 +0200 | [diff] [blame] | 2927 | 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] | 2928 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2929 | ret = acl_pass(ret); |
| 2930 | if (cond->pol == ACL_COND_UNLESS) |
| 2931 | ret = !ret; |
Willy Tarreau | b80c230 | 2007-11-30 20:51:32 +0100 | [diff] [blame] | 2932 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2933 | if (ret) { |
| 2934 | /* we fail this request, let's return 503 service unavail */ |
| 2935 | txn->status = 503; |
Jarno Huuskonen | 9e6906b | 2017-03-06 14:21:49 +0200 | [diff] [blame] | 2936 | http_reply_and_close(s, txn->status, http_error_message(s)); |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 2937 | if (!(s->flags & SF_ERR_MASK)) |
| 2938 | 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] | 2939 | goto return_prx_cond; |
Willy Tarreau | b80c230 | 2007-11-30 20:51:32 +0100 | [diff] [blame] | 2940 | } |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2941 | } |
Willy Tarreau | a5555ec | 2008-11-30 19:02:32 +0100 | [diff] [blame] | 2942 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2943 | /* nothing to fail, let's reply normaly */ |
| 2944 | txn->status = 200; |
Jarno Huuskonen | 9e6906b | 2017-03-06 14:21:49 +0200 | [diff] [blame] | 2945 | http_reply_and_close(s, txn->status, http_error_message(s)); |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 2946 | if (!(s->flags & SF_ERR_MASK)) |
| 2947 | 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] | 2948 | goto return_prx_cond; |
| 2949 | } |
| 2950 | |
| 2951 | /* |
| 2952 | * 3: Maybe we have to copy the original REQURI for the logs ? |
| 2953 | * Note: we cannot log anymore if the request has been |
| 2954 | * classified as invalid. |
| 2955 | */ |
| 2956 | if (unlikely(s->logs.logwait & LW_REQ)) { |
| 2957 | /* we have a complete HTTP request that we must log */ |
| 2958 | if ((txn->uri = pool_alloc2(pool2_requri)) != NULL) { |
| 2959 | int urilen = msg->sl.rq.l; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2960 | |
Stéphane Cottin | 23e9e93 | 2017-05-18 08:58:41 +0200 | [diff] [blame] | 2961 | if (urilen >= global.tune.requri_len ) |
| 2962 | urilen = global.tune.requri_len - 1; |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2963 | memcpy(txn->uri, req->buf->p, urilen); |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2964 | txn->uri[urilen] = 0; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2965 | |
Willy Tarreau | d79a3b2 | 2012-12-28 09:40:16 +0100 | [diff] [blame] | 2966 | if (!(s->logs.logwait &= ~(LW_REQ|LW_INIT))) |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2967 | s->do_log(s); |
| 2968 | } else { |
| 2969 | Alert("HTTP logging : out of memory.\n"); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2970 | } |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2971 | } |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 2972 | |
Willy Tarreau | 91852eb | 2015-05-01 13:26:00 +0200 | [diff] [blame] | 2973 | /* RFC7230#2.6 has enforced the format of the HTTP version string to be |
| 2974 | * exactly one digit "." one digit. This check may be disabled using |
| 2975 | * option accept-invalid-http-request. |
| 2976 | */ |
| 2977 | if (!(sess->fe->options2 & PR_O2_REQBUG_OK)) { |
| 2978 | if (msg->sl.rq.v_l != 8) { |
| 2979 | msg->err_pos = msg->sl.rq.v; |
| 2980 | goto return_bad_req; |
| 2981 | } |
| 2982 | |
| 2983 | if (req->buf->p[msg->sl.rq.v + 4] != '/' || |
| 2984 | !isdigit((unsigned char)req->buf->p[msg->sl.rq.v + 5]) || |
| 2985 | req->buf->p[msg->sl.rq.v + 6] != '.' || |
| 2986 | !isdigit((unsigned char)req->buf->p[msg->sl.rq.v + 7])) { |
| 2987 | msg->err_pos = msg->sl.rq.v + 4; |
| 2988 | goto return_bad_req; |
| 2989 | } |
| 2990 | } |
Willy Tarreau | 1331766 | 2015-05-01 13:47:08 +0200 | [diff] [blame] | 2991 | else { |
| 2992 | /* 4. We may have to convert HTTP/0.9 requests to HTTP/1.0 */ |
| 2993 | if (unlikely(msg->sl.rq.v_l == 0) && !http_upgrade_v09_to_v10(txn)) |
| 2994 | goto return_bad_req; |
| 2995 | } |
Willy Tarreau | 91852eb | 2015-05-01 13:26:00 +0200 | [diff] [blame] | 2996 | |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 2997 | /* ... and check if the request is HTTP/1.1 or above */ |
| 2998 | if ((msg->sl.rq.v_l == 8) && |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2999 | ((req->buf->p[msg->sl.rq.v + 5] > '1') || |
| 3000 | ((req->buf->p[msg->sl.rq.v + 5] == '1') && |
| 3001 | (req->buf->p[msg->sl.rq.v + 7] >= '1')))) |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 3002 | msg->flags |= HTTP_MSGF_VER_11; |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 3003 | |
| 3004 | /* "connection" has not been parsed yet */ |
Willy Tarreau | 50fc777 | 2012-11-11 22:19:57 +0100 | [diff] [blame] | 3005 | 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] | 3006 | |
Willy Tarreau | 88d349d | 2010-01-25 12:15:43 +0100 | [diff] [blame] | 3007 | /* if the frontend has "option http-use-proxy-header", we'll check if |
| 3008 | * we have what looks like a proxied connection instead of a connection, |
| 3009 | * and in this case set the TX_USE_PX_CONN flag to use Proxy-connection. |
| 3010 | * Note that this is *not* RFC-compliant, however browsers and proxies |
| 3011 | * happen to do that despite being non-standard :-( |
| 3012 | * We consider that a request not beginning with either '/' or '*' is |
| 3013 | * a proxied connection, which covers both "scheme://location" and |
| 3014 | * CONNECT ip:port. |
| 3015 | */ |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 3016 | if ((sess->fe->options2 & PR_O2_USE_PXHDR) && |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 3017 | 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] | 3018 | txn->flags |= TX_USE_PX_CONN; |
| 3019 | |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 3020 | /* transfer length unknown*/ |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 3021 | msg->flags &= ~HTTP_MSGF_XFER_LEN; |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 3022 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3023 | /* 5: we may need to capture headers */ |
Willy Tarreau | cb7dd01 | 2015-04-03 22:16:32 +0200 | [diff] [blame] | 3024 | if (unlikely((s->logs.logwait & LW_REQHDR) && s->req_cap)) |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 3025 | capture_headers(req->buf->p, &txn->hdr_idx, |
Willy Tarreau | cb7dd01 | 2015-04-03 22:16:32 +0200 | [diff] [blame] | 3026 | s->req_cap, sess->fe->req_cap); |
Willy Tarreau | 1138281 | 2008-07-09 16:18:21 +0200 | [diff] [blame] | 3027 | |
Willy Tarreau | 557f199 | 2015-05-01 10:05:17 +0200 | [diff] [blame] | 3028 | /* 6: determine the transfer-length according to RFC2616 #4.4, updated |
| 3029 | * by RFC7230#3.3.3 : |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 3030 | * |
Willy Tarreau | 557f199 | 2015-05-01 10:05:17 +0200 | [diff] [blame] | 3031 | * The length of a message body is determined by one of the following |
| 3032 | * (in order of precedence): |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 3033 | * |
Willy Tarreau | 557f199 | 2015-05-01 10:05:17 +0200 | [diff] [blame] | 3034 | * 1. Any response to a HEAD request and any response with a 1xx |
| 3035 | * (Informational), 204 (No Content), or 304 (Not Modified) status |
| 3036 | * code is always terminated by the first empty line after the |
| 3037 | * header fields, regardless of the header fields present in the |
| 3038 | * message, and thus cannot contain a message body. |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 3039 | * |
Willy Tarreau | 557f199 | 2015-05-01 10:05:17 +0200 | [diff] [blame] | 3040 | * 2. Any 2xx (Successful) response to a CONNECT request implies that |
| 3041 | * the connection will become a tunnel immediately after the empty |
| 3042 | * line that concludes the header fields. A client MUST ignore any |
| 3043 | * Content-Length or Transfer-Encoding header fields received in |
| 3044 | * such a message. |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 3045 | * |
Willy Tarreau | 557f199 | 2015-05-01 10:05:17 +0200 | [diff] [blame] | 3046 | * 3. If a Transfer-Encoding header field is present and the chunked |
| 3047 | * transfer coding (Section 4.1) is the final encoding, the message |
| 3048 | * body length is determined by reading and decoding the chunked |
| 3049 | * data until the transfer coding indicates the data is complete. |
| 3050 | * |
| 3051 | * If a Transfer-Encoding header field is present in a response and |
| 3052 | * the chunked transfer coding is not the final encoding, the |
| 3053 | * message body length is determined by reading the connection until |
| 3054 | * it is closed by the server. If a Transfer-Encoding header field |
| 3055 | * is present in a request and the chunked transfer coding is not |
| 3056 | * the final encoding, the message body length cannot be determined |
| 3057 | * reliably; the server MUST respond with the 400 (Bad Request) |
| 3058 | * status code and then close the connection. |
| 3059 | * |
| 3060 | * If a message is received with both a Transfer-Encoding and a |
| 3061 | * Content-Length header field, the Transfer-Encoding overrides the |
| 3062 | * Content-Length. Such a message might indicate an attempt to |
| 3063 | * perform request smuggling (Section 9.5) or response splitting |
| 3064 | * (Section 9.4) and ought to be handled as an error. A sender MUST |
| 3065 | * remove the received Content-Length field prior to forwarding such |
| 3066 | * a message downstream. |
| 3067 | * |
| 3068 | * 4. If a message is received without Transfer-Encoding and with |
| 3069 | * either multiple Content-Length header fields having differing |
| 3070 | * field-values or a single Content-Length header field having an |
| 3071 | * invalid value, then the message framing is invalid and the |
| 3072 | * recipient MUST treat it as an unrecoverable error. If this is a |
| 3073 | * request message, the server MUST respond with a 400 (Bad Request) |
| 3074 | * status code and then close the connection. If this is a response |
| 3075 | * message received by a proxy, the proxy MUST close the connection |
| 3076 | * to the server, discard the received response, and send a 502 (Bad |
| 3077 | * Gateway) response to the client. If this is a response message |
| 3078 | * received by a user agent, the user agent MUST close the |
| 3079 | * connection to the server and discard the received response. |
| 3080 | * |
| 3081 | * 5. If a valid Content-Length header field is present without |
| 3082 | * Transfer-Encoding, its decimal value defines the expected message |
| 3083 | * body length in octets. If the sender closes the connection or |
| 3084 | * the recipient times out before the indicated number of octets are |
| 3085 | * received, the recipient MUST consider the message to be |
| 3086 | * incomplete and close the connection. |
| 3087 | * |
| 3088 | * 6. If this is a request message and none of the above are true, then |
| 3089 | * the message body length is zero (no message body is present). |
| 3090 | * |
| 3091 | * 7. Otherwise, this is a response message without a declared message |
| 3092 | * body length, so the message body length is determined by the |
| 3093 | * number of octets received prior to the server closing the |
| 3094 | * connection. |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 3095 | */ |
| 3096 | |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 3097 | ctx.idx = 0; |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 3098 | /* set TE_CHNK and XFER_LEN only if "chunked" is seen last */ |
Willy Tarreau | 4979d5c | 2015-05-01 10:06:30 +0200 | [diff] [blame] | 3099 | 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] | 3100 | if (ctx.vlen == 7 && strncasecmp(ctx.line + ctx.val, "chunked", 7) == 0) |
Christopher Faulet | be821b9 | 2017-03-30 11:21:53 +0200 | [diff] [blame] | 3101 | msg->flags |= HTTP_MSGF_TE_CHNK; |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 3102 | else if (msg->flags & HTTP_MSGF_TE_CHNK) { |
Willy Tarreau | 34dfc60 | 2015-05-01 10:09:49 +0200 | [diff] [blame] | 3103 | /* chunked not last, return badreq */ |
| 3104 | goto return_bad_req; |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 3105 | } |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 3106 | } |
| 3107 | |
Willy Tarreau | 1c91391 | 2015-04-30 10:57:51 +0200 | [diff] [blame] | 3108 | /* Chunked requests must have their content-length removed */ |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 3109 | ctx.idx = 0; |
Willy Tarreau | 1c91391 | 2015-04-30 10:57:51 +0200 | [diff] [blame] | 3110 | if (msg->flags & HTTP_MSGF_TE_CHNK) { |
| 3111 | while (http_find_header2("Content-Length", 14, req->buf->p, &txn->hdr_idx, &ctx)) |
| 3112 | http_remove_header2(msg, &txn->hdr_idx, &ctx); |
| 3113 | } |
Willy Tarreau | 34dfc60 | 2015-05-01 10:09:49 +0200 | [diff] [blame] | 3114 | 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] | 3115 | signed long long cl; |
| 3116 | |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 3117 | if (!ctx.vlen) { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 3118 | msg->err_pos = ctx.line + ctx.val - req->buf->p; |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 3119 | goto return_bad_req; |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 3120 | } |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 3121 | |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 3122 | if (strl2llrc(ctx.line + ctx.val, ctx.vlen, &cl)) { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 3123 | msg->err_pos = ctx.line + ctx.val - req->buf->p; |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 3124 | goto return_bad_req; /* parse failure */ |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 3125 | } |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 3126 | |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 3127 | if (cl < 0) { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 3128 | msg->err_pos = ctx.line + ctx.val - req->buf->p; |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 3129 | goto return_bad_req; |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 3130 | } |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 3131 | |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 3132 | if ((msg->flags & HTTP_MSGF_CNT_LEN) && (msg->chunk_len != cl)) { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 3133 | msg->err_pos = ctx.line + ctx.val - req->buf->p; |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 3134 | goto return_bad_req; /* already specified, was different */ |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 3135 | } |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 3136 | |
Christopher Faulet | be821b9 | 2017-03-30 11:21:53 +0200 | [diff] [blame] | 3137 | msg->flags |= HTTP_MSGF_CNT_LEN; |
Willy Tarreau | 124d991 | 2011-03-01 20:30:48 +0100 | [diff] [blame] | 3138 | msg->body_len = msg->chunk_len = cl; |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 3139 | } |
| 3140 | |
Willy Tarreau | 34dfc60 | 2015-05-01 10:09:49 +0200 | [diff] [blame] | 3141 | /* even bodyless requests have a known length */ |
| 3142 | msg->flags |= HTTP_MSGF_XFER_LEN; |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 3143 | |
Willy Tarreau | 179085c | 2014-04-28 16:48:56 +0200 | [diff] [blame] | 3144 | /* Until set to anything else, the connection mode is set as Keep-Alive. It will |
| 3145 | * only change if both the request and the config reference something else. |
| 3146 | * Option httpclose by itself sets tunnel mode where headers are mangled. |
| 3147 | * However, if another mode is set, it will affect it (eg: server-close/ |
| 3148 | * keep-alive + httpclose = close). Note that we avoid to redo the same work |
| 3149 | * if FE and BE have the same settings (common). The method consists in |
| 3150 | * checking if options changed between the two calls (implying that either |
| 3151 | * one is non-null, or one of them is non-null and we are there for the first |
| 3152 | * time. |
| 3153 | */ |
| 3154 | if (!(txn->flags & TX_HDR_CONN_PRS) || |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 3155 | ((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] | 3156 | http_adjust_conn_mode(s, txn, msg); |
Willy Tarreau | 179085c | 2014-04-28 16:48:56 +0200 | [diff] [blame] | 3157 | |
Willy Tarreau | 9fbe18e | 2015-05-01 22:42:08 +0200 | [diff] [blame] | 3158 | /* we may have to wait for the request's body */ |
| 3159 | if ((s->be->options & PR_O_WREQ_BODY) && |
| 3160 | (msg->body_len || (msg->flags & HTTP_MSGF_TE_CHNK))) |
| 3161 | req->analysers |= AN_REQ_HTTP_BODY; |
| 3162 | |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 3163 | /* end of job, return OK */ |
Willy Tarreau | 3a81629 | 2009-07-07 10:55:49 +0200 | [diff] [blame] | 3164 | req->analysers &= ~an_bit; |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 3165 | req->analyse_exp = TICK_ETERNITY; |
| 3166 | return 1; |
| 3167 | |
| 3168 | return_bad_req: |
| 3169 | /* We centralize bad requests processing here */ |
| 3170 | if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) { |
| 3171 | /* we detected a parsing error. We want to archive this request |
| 3172 | * in the dedicated proxy area for later troubleshooting. |
| 3173 | */ |
Willy Tarreau | 10e61cb | 2017-01-04 14:51:22 +0100 | [diff] [blame] | 3174 | http_capture_bad_message(&sess->fe->invalid_req, s, msg, msg->err_state, sess->fe); |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 3175 | } |
| 3176 | |
Willy Tarreau | 10e61cb | 2017-01-04 14:51:22 +0100 | [diff] [blame] | 3177 | txn->req.err_state = txn->req.msg_state; |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 3178 | txn->req.msg_state = HTTP_MSG_ERROR; |
| 3179 | txn->status = 400; |
Jarno Huuskonen | 9e6906b | 2017-03-06 14:21:49 +0200 | [diff] [blame] | 3180 | http_reply_and_close(s, txn->status, http_error_message(s)); |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 3181 | |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 3182 | sess->fe->fe_counters.failed_req++; |
Willy Tarreau | fb0afa7 | 2015-04-03 14:46:27 +0200 | [diff] [blame] | 3183 | if (sess->listener->counters) |
| 3184 | sess->listener->counters->failed_req++; |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 3185 | |
| 3186 | return_prx_cond: |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 3187 | if (!(s->flags & SF_ERR_MASK)) |
| 3188 | s->flags |= SF_ERR_PRXCOND; |
| 3189 | if (!(s->flags & SF_FINST_MASK)) |
| 3190 | s->flags |= SF_FINST_R; |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 3191 | |
Christopher Faulet | 0184ea7 | 2017-01-05 14:06:34 +0100 | [diff] [blame] | 3192 | req->analysers &= AN_REQ_FLT_END; |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 3193 | req->analyse_exp = TICK_ETERNITY; |
| 3194 | return 0; |
| 3195 | } |
| 3196 | |
Willy Tarreau | 4f8a83c | 2012-06-04 00:26:23 +0200 | [diff] [blame] | 3197 | |
Willy Tarreau | 347a35d | 2013-11-22 17:51:09 +0100 | [diff] [blame] | 3198 | /* This function prepares an applet to handle the stats. It can deal with the |
| 3199 | * "100-continue" expectation, check that admin rules are met for POST requests, |
| 3200 | * and program a response message if something was unexpected. It cannot fail |
| 3201 | * 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] | 3202 | * 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] | 3203 | * 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] | 3204 | * is expected to have already assigned an appctx to the stream. |
Willy Tarreau | 1facd6d | 2012-12-22 22:03:39 +0100 | [diff] [blame] | 3205 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 3206 | int http_handle_stats(struct stream *s, struct channel *req) |
Willy Tarreau | 1facd6d | 2012-12-22 22:03:39 +0100 | [diff] [blame] | 3207 | { |
| 3208 | struct stats_admin_rule *stats_admin_rule; |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 3209 | struct stream_interface *si = &s->si[1]; |
Willy Tarreau | 192252e | 2015-04-04 01:47:55 +0200 | [diff] [blame] | 3210 | struct session *sess = s->sess; |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 3211 | struct http_txn *txn = s->txn; |
Willy Tarreau | 1facd6d | 2012-12-22 22:03:39 +0100 | [diff] [blame] | 3212 | struct http_msg *msg = &txn->req; |
Willy Tarreau | 414e9bb | 2013-11-23 00:30:38 +0100 | [diff] [blame] | 3213 | struct uri_auth *uri_auth = s->be->uri_auth; |
| 3214 | const char *uri, *h, *lookup; |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 3215 | struct appctx *appctx; |
Willy Tarreau | 414e9bb | 2013-11-23 00:30:38 +0100 | [diff] [blame] | 3216 | |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 3217 | appctx = si_appctx(si); |
| 3218 | memset(&appctx->ctx.stats, 0, sizeof(appctx->ctx.stats)); |
| 3219 | appctx->st1 = appctx->st2 = 0; |
| 3220 | appctx->ctx.stats.st_code = STAT_STATUS_INIT; |
| 3221 | appctx->ctx.stats.flags |= STAT_FMT_HTML; /* assume HTML mode by default */ |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 3222 | 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] | 3223 | appctx->ctx.stats.flags |= STAT_CHUNKED; |
Willy Tarreau | 414e9bb | 2013-11-23 00:30:38 +0100 | [diff] [blame] | 3224 | |
| 3225 | uri = msg->chn->buf->p + msg->sl.rq.u; |
| 3226 | lookup = uri + uri_auth->uri_len; |
| 3227 | |
| 3228 | for (h = lookup; h <= uri + msg->sl.rq.u_l - 3; h++) { |
| 3229 | if (memcmp(h, ";up", 3) == 0) { |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 3230 | appctx->ctx.stats.flags |= STAT_HIDE_DOWN; |
Willy Tarreau | 414e9bb | 2013-11-23 00:30:38 +0100 | [diff] [blame] | 3231 | break; |
| 3232 | } |
| 3233 | } |
| 3234 | |
| 3235 | if (uri_auth->refresh) { |
| 3236 | for (h = lookup; h <= uri + msg->sl.rq.u_l - 10; h++) { |
| 3237 | if (memcmp(h, ";norefresh", 10) == 0) { |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 3238 | appctx->ctx.stats.flags |= STAT_NO_REFRESH; |
Willy Tarreau | 414e9bb | 2013-11-23 00:30:38 +0100 | [diff] [blame] | 3239 | break; |
| 3240 | } |
| 3241 | } |
| 3242 | } |
| 3243 | |
| 3244 | for (h = lookup; h <= uri + msg->sl.rq.u_l - 4; h++) { |
| 3245 | if (memcmp(h, ";csv", 4) == 0) { |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 3246 | appctx->ctx.stats.flags &= ~STAT_FMT_HTML; |
Willy Tarreau | 414e9bb | 2013-11-23 00:30:38 +0100 | [diff] [blame] | 3247 | break; |
| 3248 | } |
| 3249 | } |
| 3250 | |
Willy Tarreau | 1e62df9 | 2016-01-11 18:57:53 +0100 | [diff] [blame] | 3251 | for (h = lookup; h <= uri + msg->sl.rq.u_l - 6; h++) { |
| 3252 | if (memcmp(h, ";typed", 6) == 0) { |
| 3253 | appctx->ctx.stats.flags &= ~STAT_FMT_HTML; |
| 3254 | appctx->ctx.stats.flags |= STAT_FMT_TYPED; |
| 3255 | break; |
| 3256 | } |
| 3257 | } |
| 3258 | |
Willy Tarreau | 414e9bb | 2013-11-23 00:30:38 +0100 | [diff] [blame] | 3259 | for (h = lookup; h <= uri + msg->sl.rq.u_l - 8; h++) { |
| 3260 | if (memcmp(h, ";st=", 4) == 0) { |
| 3261 | int i; |
| 3262 | h += 4; |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 3263 | appctx->ctx.stats.st_code = STAT_STATUS_UNKN; |
Willy Tarreau | 414e9bb | 2013-11-23 00:30:38 +0100 | [diff] [blame] | 3264 | for (i = STAT_STATUS_INIT + 1; i < STAT_STATUS_SIZE; i++) { |
| 3265 | if (strncmp(stat_status_codes[i], h, 4) == 0) { |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 3266 | appctx->ctx.stats.st_code = i; |
Willy Tarreau | 414e9bb | 2013-11-23 00:30:38 +0100 | [diff] [blame] | 3267 | break; |
| 3268 | } |
| 3269 | } |
| 3270 | break; |
| 3271 | } |
| 3272 | } |
| 3273 | |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 3274 | appctx->ctx.stats.scope_str = 0; |
| 3275 | appctx->ctx.stats.scope_len = 0; |
Willy Tarreau | 414e9bb | 2013-11-23 00:30:38 +0100 | [diff] [blame] | 3276 | for (h = lookup; h <= uri + msg->sl.rq.u_l - 8; h++) { |
| 3277 | if (memcmp(h, STAT_SCOPE_INPUT_NAME "=", strlen(STAT_SCOPE_INPUT_NAME) + 1) == 0) { |
| 3278 | int itx = 0; |
| 3279 | const char *h2; |
| 3280 | char scope_txt[STAT_SCOPE_TXT_MAXLEN + 1]; |
| 3281 | const char *err; |
| 3282 | |
| 3283 | h += strlen(STAT_SCOPE_INPUT_NAME) + 1; |
| 3284 | h2 = h; |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 3285 | appctx->ctx.stats.scope_str = h2 - msg->chn->buf->p; |
Willy Tarreau | 414e9bb | 2013-11-23 00:30:38 +0100 | [diff] [blame] | 3286 | while (*h != ';' && *h != '\0' && *h != '&' && *h != ' ' && *h != '\n') { |
| 3287 | itx++; |
| 3288 | h++; |
| 3289 | } |
| 3290 | |
| 3291 | if (itx > STAT_SCOPE_TXT_MAXLEN) |
| 3292 | itx = STAT_SCOPE_TXT_MAXLEN; |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 3293 | appctx->ctx.stats.scope_len = itx; |
Willy Tarreau | 414e9bb | 2013-11-23 00:30:38 +0100 | [diff] [blame] | 3294 | |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 3295 | /* 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] | 3296 | memcpy(scope_txt, h2, itx); |
| 3297 | scope_txt[itx] = '\0'; |
| 3298 | err = invalid_char(scope_txt); |
| 3299 | if (err) { |
| 3300 | /* bad char in search text => clear scope */ |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 3301 | appctx->ctx.stats.scope_str = 0; |
| 3302 | appctx->ctx.stats.scope_len = 0; |
Willy Tarreau | 414e9bb | 2013-11-23 00:30:38 +0100 | [diff] [blame] | 3303 | } |
| 3304 | break; |
| 3305 | } |
| 3306 | } |
Willy Tarreau | 1facd6d | 2012-12-22 22:03:39 +0100 | [diff] [blame] | 3307 | |
| 3308 | /* now check whether we have some admin rules for this request */ |
Willy Tarreau | 414e9bb | 2013-11-23 00:30:38 +0100 | [diff] [blame] | 3309 | list_for_each_entry(stats_admin_rule, &uri_auth->admin_rules, list) { |
Willy Tarreau | 1facd6d | 2012-12-22 22:03:39 +0100 | [diff] [blame] | 3310 | int ret = 1; |
| 3311 | |
| 3312 | if (stats_admin_rule->cond) { |
Willy Tarreau | 192252e | 2015-04-04 01:47:55 +0200 | [diff] [blame] | 3313 | 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] | 3314 | ret = acl_pass(ret); |
| 3315 | if (stats_admin_rule->cond->pol == ACL_COND_UNLESS) |
| 3316 | ret = !ret; |
| 3317 | } |
| 3318 | |
| 3319 | if (ret) { |
| 3320 | /* no rule, or the rule matches */ |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 3321 | appctx->ctx.stats.flags |= STAT_ADMIN; |
Willy Tarreau | 1facd6d | 2012-12-22 22:03:39 +0100 | [diff] [blame] | 3322 | break; |
| 3323 | } |
| 3324 | } |
| 3325 | |
| 3326 | /* Was the status page requested with a POST ? */ |
Willy Tarreau | b8cdf52 | 2015-05-29 01:09:15 +0200 | [diff] [blame] | 3327 | if (unlikely(txn->meth == HTTP_METH_POST && txn->req.body_len > 0)) { |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 3328 | if (appctx->ctx.stats.flags & STAT_ADMIN) { |
Willy Tarreau | cfe7fdd | 2014-04-26 22:08:32 +0200 | [diff] [blame] | 3329 | /* we'll need the request body, possibly after sending 100-continue */ |
Willy Tarreau | b8cdf52 | 2015-05-29 01:09:15 +0200 | [diff] [blame] | 3330 | if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) |
| 3331 | req->analysers |= AN_REQ_HTTP_BODY; |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 3332 | appctx->st0 = STAT_HTTP_POST; |
Willy Tarreau | 1facd6d | 2012-12-22 22:03:39 +0100 | [diff] [blame] | 3333 | } |
Willy Tarreau | 347a35d | 2013-11-22 17:51:09 +0100 | [diff] [blame] | 3334 | else { |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 3335 | appctx->ctx.stats.st_code = STAT_STATUS_DENY; |
| 3336 | appctx->st0 = STAT_HTTP_LAST; |
de Lafond Guillaume | 88c278f | 2013-04-15 19:27:10 +0200 | [diff] [blame] | 3337 | } |
Willy Tarreau | 1facd6d | 2012-12-22 22:03:39 +0100 | [diff] [blame] | 3338 | } |
Willy Tarreau | 347a35d | 2013-11-22 17:51:09 +0100 | [diff] [blame] | 3339 | else { |
| 3340 | /* So it was another method (GET/HEAD) */ |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 3341 | appctx->st0 = STAT_HTTP_HEAD; |
Willy Tarreau | 1facd6d | 2012-12-22 22:03:39 +0100 | [diff] [blame] | 3342 | } |
| 3343 | |
Willy Tarreau | 1facd6d | 2012-12-22 22:03:39 +0100 | [diff] [blame] | 3344 | s->task->nice = -32; /* small boost for HTTP statistics */ |
Willy Tarreau | 1facd6d | 2012-12-22 22:03:39 +0100 | [diff] [blame] | 3345 | return 1; |
| 3346 | } |
| 3347 | |
Lukas Tribus | 67db8df | 2013-06-23 17:37:13 +0200 | [diff] [blame] | 3348 | /* Sets the TOS header in IPv4 and the traffic class header in IPv6 packets |
| 3349 | * (as per RFC3260 #4 and BCP37 #4.2 and #5.2). |
| 3350 | */ |
Vincent Bernat | 6e61589 | 2016-05-18 16:17:44 +0200 | [diff] [blame] | 3351 | 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] | 3352 | { |
| 3353 | #ifdef IP_TOS |
Vincent Bernat | 6e61589 | 2016-05-18 16:17:44 +0200 | [diff] [blame] | 3354 | if (from->ss_family == AF_INET) |
Lukas Tribus | 67db8df | 2013-06-23 17:37:13 +0200 | [diff] [blame] | 3355 | setsockopt(fd, IPPROTO_IP, IP_TOS, &tos, sizeof(tos)); |
| 3356 | #endif |
| 3357 | #ifdef IPV6_TCLASS |
Vincent Bernat | 6e61589 | 2016-05-18 16:17:44 +0200 | [diff] [blame] | 3358 | if (from->ss_family == AF_INET6) { |
| 3359 | if (IN6_IS_ADDR_V4MAPPED(&((struct sockaddr_in6 *)from)->sin6_addr)) |
Lukas Tribus | 67db8df | 2013-06-23 17:37:13 +0200 | [diff] [blame] | 3360 | /* v4-mapped addresses need IP_TOS */ |
| 3361 | setsockopt(fd, IPPROTO_IP, IP_TOS, &tos, sizeof(tos)); |
| 3362 | else |
| 3363 | setsockopt(fd, IPPROTO_IPV6, IPV6_TCLASS, &tos, sizeof(tos)); |
| 3364 | } |
| 3365 | #endif |
| 3366 | } |
| 3367 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 3368 | int http_transform_header_str(struct stream* s, struct http_msg *msg, |
Thierry FOURNIER | 5531f87 | 2015-03-16 11:15:50 +0100 | [diff] [blame] | 3369 | const char* name, unsigned int name_len, |
| 3370 | const char *str, struct my_regex *re, |
| 3371 | int action) |
Sasha Pachev | 218f064 | 2014-06-16 12:05:59 -0600 | [diff] [blame] | 3372 | { |
Thierry FOURNIER | 5a33ac7 | 2015-03-16 23:54:35 +0100 | [diff] [blame] | 3373 | struct hdr_ctx ctx; |
| 3374 | char *buf = msg->chn->buf->p; |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 3375 | struct hdr_idx *idx = &s->txn->hdr_idx; |
Thierry FOURNIER | 5531f87 | 2015-03-16 11:15:50 +0100 | [diff] [blame] | 3376 | int (*http_find_hdr_func)(const char *name, int len, char *sol, |
| 3377 | struct hdr_idx *idx, struct hdr_ctx *ctx); |
Thierry FOURNIER | 191f9ef | 2015-03-16 23:23:53 +0100 | [diff] [blame] | 3378 | struct chunk *output = get_trash_chunk(); |
| 3379 | |
Thierry FOURNIER | 5a33ac7 | 2015-03-16 23:54:35 +0100 | [diff] [blame] | 3380 | ctx.idx = 0; |
Sasha Pachev | 218f064 | 2014-06-16 12:05:59 -0600 | [diff] [blame] | 3381 | |
Thierry FOURNIER | 191f9ef | 2015-03-16 23:23:53 +0100 | [diff] [blame] | 3382 | /* Choose the header browsing function. */ |
| 3383 | switch (action) { |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 3384 | case ACT_HTTP_REPLACE_VAL: |
Thierry FOURNIER | 191f9ef | 2015-03-16 23:23:53 +0100 | [diff] [blame] | 3385 | http_find_hdr_func = http_find_header2; |
| 3386 | break; |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 3387 | case ACT_HTTP_REPLACE_HDR: |
Thierry FOURNIER | 191f9ef | 2015-03-16 23:23:53 +0100 | [diff] [blame] | 3388 | http_find_hdr_func = http_find_full_header2; |
| 3389 | break; |
| 3390 | default: /* impossible */ |
| 3391 | return -1; |
| 3392 | } |
| 3393 | |
Thierry FOURNIER | 5a33ac7 | 2015-03-16 23:54:35 +0100 | [diff] [blame] | 3394 | while (http_find_hdr_func(name, name_len, buf, idx, &ctx)) { |
| 3395 | struct hdr_idx_elem *hdr = idx->v + ctx.idx; |
Sasha Pachev | 218f064 | 2014-06-16 12:05:59 -0600 | [diff] [blame] | 3396 | int delta; |
Thierry FOURNIER | 5a33ac7 | 2015-03-16 23:54:35 +0100 | [diff] [blame] | 3397 | char *val = ctx.line + ctx.val; |
| 3398 | char* val_end = val + ctx.vlen; |
Sasha Pachev | 218f064 | 2014-06-16 12:05:59 -0600 | [diff] [blame] | 3399 | |
Thierry FOURNIER | 191f9ef | 2015-03-16 23:23:53 +0100 | [diff] [blame] | 3400 | if (!regex_exec_match2(re, val, val_end-val, MAX_MATCH, pmatch, 0)) |
| 3401 | continue; |
Sasha Pachev | 218f064 | 2014-06-16 12:05:59 -0600 | [diff] [blame] | 3402 | |
Thierry FOURNIER | 5531f87 | 2015-03-16 11:15:50 +0100 | [diff] [blame] | 3403 | output->len = exp_replace(output->str, output->size, val, str, pmatch); |
Thierry FOURNIER | 191f9ef | 2015-03-16 23:23:53 +0100 | [diff] [blame] | 3404 | if (output->len == -1) |
Sasha Pachev | 218f064 | 2014-06-16 12:05:59 -0600 | [diff] [blame] | 3405 | return -1; |
Sasha Pachev | 218f064 | 2014-06-16 12:05:59 -0600 | [diff] [blame] | 3406 | |
Thierry FOURNIER | 191f9ef | 2015-03-16 23:23:53 +0100 | [diff] [blame] | 3407 | 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] | 3408 | |
| 3409 | hdr->len += delta; |
| 3410 | http_msg_move_end(msg, delta); |
Thierry FOURNIER | 191f9ef | 2015-03-16 23:23:53 +0100 | [diff] [blame] | 3411 | |
| 3412 | /* Adjust the length of the current value of the index. */ |
Thierry FOURNIER | 5a33ac7 | 2015-03-16 23:54:35 +0100 | [diff] [blame] | 3413 | ctx.vlen += delta; |
Sasha Pachev | 218f064 | 2014-06-16 12:05:59 -0600 | [diff] [blame] | 3414 | } |
| 3415 | |
| 3416 | return 0; |
| 3417 | } |
| 3418 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 3419 | static int http_transform_header(struct stream* s, struct http_msg *msg, |
Thierry FOURNIER | 5531f87 | 2015-03-16 11:15:50 +0100 | [diff] [blame] | 3420 | const char* name, unsigned int name_len, |
| 3421 | struct list *fmt, struct my_regex *re, |
| 3422 | int action) |
| 3423 | { |
Christopher Faulet | 07a0fec | 2017-02-08 12:17:07 +0100 | [diff] [blame] | 3424 | struct chunk *replace; |
| 3425 | int ret = -1; |
| 3426 | |
| 3427 | replace = alloc_trash_chunk(); |
| 3428 | if (!replace) |
| 3429 | goto leave; |
Thierry FOURNIER | 5531f87 | 2015-03-16 11:15:50 +0100 | [diff] [blame] | 3430 | |
| 3431 | replace->len = build_logline(s, replace->str, replace->size, fmt); |
| 3432 | if (replace->len >= replace->size - 1) |
Christopher Faulet | 07a0fec | 2017-02-08 12:17:07 +0100 | [diff] [blame] | 3433 | goto leave; |
| 3434 | |
| 3435 | ret = http_transform_header_str(s, msg, name, name_len, replace->str, re, action); |
Thierry FOURNIER | 5531f87 | 2015-03-16 11:15:50 +0100 | [diff] [blame] | 3436 | |
Christopher Faulet | 07a0fec | 2017-02-08 12:17:07 +0100 | [diff] [blame] | 3437 | leave: |
| 3438 | free_trash_chunk(replace); |
| 3439 | return ret; |
Thierry FOURNIER | 5531f87 | 2015-03-16 11:15:50 +0100 | [diff] [blame] | 3440 | } |
| 3441 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 3442 | /* Executes the http-request rules <rules> for stream <s>, proxy <px> and |
Willy Tarreau | 0b74833 | 2014-04-29 00:13:29 +0200 | [diff] [blame] | 3443 | * transaction <txn>. Returns the verdict of the first rule that prevents |
| 3444 | * further processing of the request (auth, deny, ...), and defaults to |
| 3445 | * HTTP_RULE_RES_STOP if it executed all rules or stopped on an allow, or |
| 3446 | * 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] | 3447 | * on txn->flags if it encounters a tarpit rule. If <deny_status> is not NULL |
| 3448 | * and a deny/tarpit rule is matched, it will be filled with this rule's deny |
| 3449 | * status. |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 3450 | */ |
Willy Tarreau | 0b74833 | 2014-04-29 00:13:29 +0200 | [diff] [blame] | 3451 | enum rule_result |
Willy Tarreau | 58727ec | 2016-05-25 16:23:59 +0200 | [diff] [blame] | 3452 | 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] | 3453 | { |
Willy Tarreau | 9ad7bd4 | 2015-04-03 19:19:59 +0200 | [diff] [blame] | 3454 | struct session *sess = strm_sess(s); |
Willy Tarreau | 987e3fb | 2015-04-04 01:09:08 +0200 | [diff] [blame] | 3455 | struct http_txn *txn = s->txn; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 3456 | struct connection *cli_conn; |
Thierry FOURNIER | a28a942 | 2015-08-04 19:35:46 +0200 | [diff] [blame] | 3457 | struct act_rule *rule; |
Willy Tarreau | 20b0de5 | 2012-12-24 15:45:22 +0100 | [diff] [blame] | 3458 | struct hdr_ctx ctx; |
Willy Tarreau | ae3c010 | 2014-04-28 23:22:08 +0200 | [diff] [blame] | 3459 | const char *auth_realm; |
Willy Tarreau | acc9800 | 2015-09-27 23:34:39 +0200 | [diff] [blame] | 3460 | int act_flags = 0; |
Thierry Fournier | 4b788f7 | 2016-06-01 13:35:36 +0200 | [diff] [blame] | 3461 | int len; |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 3462 | |
Thierry FOURNIER | bc4c1ac | 2015-02-25 13:36:14 +0100 | [diff] [blame] | 3463 | /* If "the current_rule_list" match the executed rule list, we are in |
| 3464 | * resume condition. If a resume is needed it is always in the action |
| 3465 | * and never in the ACL or converters. In this case, we initialise the |
| 3466 | * current rule, and go to the action execution point. |
| 3467 | */ |
Willy Tarreau | 152b81e | 2015-04-20 13:26:17 +0200 | [diff] [blame] | 3468 | if (s->current_rule) { |
| 3469 | rule = s->current_rule; |
| 3470 | s->current_rule = NULL; |
| 3471 | if (s->current_rule_list == rules) |
| 3472 | goto resume_execution; |
Thierry FOURNIER | bc4c1ac | 2015-02-25 13:36:14 +0100 | [diff] [blame] | 3473 | } |
| 3474 | s->current_rule_list = rules; |
Willy Tarreau | 152b81e | 2015-04-20 13:26:17 +0200 | [diff] [blame] | 3475 | |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 3476 | list_for_each_entry(rule, rules, list) { |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 3477 | |
Willy Tarreau | 96257ec | 2012-12-27 10:46:37 +0100 | [diff] [blame] | 3478 | /* check optional condition */ |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 3479 | if (rule->cond) { |
Willy Tarreau | 96257ec | 2012-12-27 10:46:37 +0100 | [diff] [blame] | 3480 | int ret; |
| 3481 | |
Willy Tarreau | 192252e | 2015-04-04 01:47:55 +0200 | [diff] [blame] | 3482 | 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] | 3483 | ret = acl_pass(ret); |
| 3484 | |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 3485 | if (rule->cond->pol == ACL_COND_UNLESS) |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 3486 | ret = !ret; |
Willy Tarreau | 96257ec | 2012-12-27 10:46:37 +0100 | [diff] [blame] | 3487 | |
| 3488 | if (!ret) /* condition not matched */ |
| 3489 | continue; |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 3490 | } |
| 3491 | |
Willy Tarreau | acc9800 | 2015-09-27 23:34:39 +0200 | [diff] [blame] | 3492 | act_flags |= ACT_FLAG_FIRST; |
Thierry FOURNIER | bc4c1ac | 2015-02-25 13:36:14 +0100 | [diff] [blame] | 3493 | resume_execution: |
Willy Tarreau | 96257ec | 2012-12-27 10:46:37 +0100 | [diff] [blame] | 3494 | switch (rule->action) { |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 3495 | case ACT_ACTION_ALLOW: |
Willy Tarreau | 0b74833 | 2014-04-29 00:13:29 +0200 | [diff] [blame] | 3496 | return HTTP_RULE_RES_STOP; |
Willy Tarreau | 96257ec | 2012-12-27 10:46:37 +0100 | [diff] [blame] | 3497 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 3498 | case ACT_ACTION_DENY: |
Willy Tarreau | 58727ec | 2016-05-25 16:23:59 +0200 | [diff] [blame] | 3499 | if (deny_status) |
| 3500 | *deny_status = rule->deny_status; |
Willy Tarreau | 0b74833 | 2014-04-29 00:13:29 +0200 | [diff] [blame] | 3501 | return HTTP_RULE_RES_DENY; |
Willy Tarreau | 96257ec | 2012-12-27 10:46:37 +0100 | [diff] [blame] | 3502 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 3503 | case ACT_HTTP_REQ_TARPIT: |
Willy Tarreau | ccbcc37 | 2012-12-27 12:37:57 +0100 | [diff] [blame] | 3504 | txn->flags |= TX_CLTARPIT; |
Willy Tarreau | 58727ec | 2016-05-25 16:23:59 +0200 | [diff] [blame] | 3505 | if (deny_status) |
| 3506 | *deny_status = rule->deny_status; |
Willy Tarreau | 0b74833 | 2014-04-29 00:13:29 +0200 | [diff] [blame] | 3507 | return HTTP_RULE_RES_DENY; |
Willy Tarreau | ccbcc37 | 2012-12-27 12:37:57 +0100 | [diff] [blame] | 3508 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 3509 | case ACT_HTTP_REQ_AUTH: |
Willy Tarreau | ae3c010 | 2014-04-28 23:22:08 +0200 | [diff] [blame] | 3510 | /* Auth might be performed on regular http-req rules as well as on stats */ |
| 3511 | auth_realm = rule->arg.auth.realm; |
| 3512 | if (!auth_realm) { |
| 3513 | if (px->uri_auth && rules == &px->uri_auth->http_req_rules) |
| 3514 | auth_realm = STATS_DEFAULT_REALM; |
| 3515 | else |
| 3516 | auth_realm = px->id; |
| 3517 | } |
| 3518 | /* send 401/407 depending on whether we use a proxy or not. We still |
| 3519 | * count one error, because normal browsing won't significantly |
| 3520 | * increase the counter but brute force attempts will. |
| 3521 | */ |
| 3522 | chunk_printf(&trash, (txn->flags & TX_USE_PX_CONN) ? HTTP_407_fmt : HTTP_401_fmt, auth_realm); |
| 3523 | txn->status = (txn->flags & TX_USE_PX_CONN) ? 407 : 401; |
Christopher Faulet | a94e5a5 | 2015-12-09 15:55:06 +0100 | [diff] [blame] | 3524 | http_reply_and_close(s, txn->status, &trash); |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 3525 | stream_inc_http_err_ctr(s); |
Willy Tarreau | 0b74833 | 2014-04-29 00:13:29 +0200 | [diff] [blame] | 3526 | return HTTP_RULE_RES_ABRT; |
Willy Tarreau | 96257ec | 2012-12-27 10:46:37 +0100 | [diff] [blame] | 3527 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 3528 | case ACT_HTTP_REDIR: |
Willy Tarreau | 0b74833 | 2014-04-29 00:13:29 +0200 | [diff] [blame] | 3529 | if (!http_apply_redirect_rule(rule->arg.redir, s, txn)) |
| 3530 | return HTTP_RULE_RES_BADREQ; |
| 3531 | return HTTP_RULE_RES_DONE; |
Willy Tarreau | 81499eb | 2012-12-27 12:19:02 +0100 | [diff] [blame] | 3532 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 3533 | case ACT_HTTP_SET_NICE: |
Willy Tarreau | f4c43c1 | 2013-06-11 17:01:13 +0200 | [diff] [blame] | 3534 | s->task->nice = rule->arg.nice; |
| 3535 | break; |
| 3536 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 3537 | case ACT_HTTP_SET_TOS: |
Willy Tarreau | 9ad7bd4 | 2015-04-03 19:19:59 +0200 | [diff] [blame] | 3538 | if ((cli_conn = objt_conn(sess->origin)) && conn_ctrl_ready(cli_conn)) |
Vincent Bernat | 6e61589 | 2016-05-18 16:17:44 +0200 | [diff] [blame] | 3539 | 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] | 3540 | break; |
| 3541 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 3542 | case ACT_HTTP_SET_MARK: |
Willy Tarreau | 51347ed | 2013-06-11 19:34:13 +0200 | [diff] [blame] | 3543 | #ifdef SO_MARK |
Willy Tarreau | 9ad7bd4 | 2015-04-03 19:19:59 +0200 | [diff] [blame] | 3544 | if ((cli_conn = objt_conn(sess->origin)) && conn_ctrl_ready(cli_conn)) |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 3545 | 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] | 3546 | #endif |
| 3547 | break; |
| 3548 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 3549 | case ACT_HTTP_SET_LOGL: |
Willy Tarreau | 9a355ec | 2013-06-11 17:45:46 +0200 | [diff] [blame] | 3550 | s->logs.level = rule->arg.loglevel; |
| 3551 | break; |
| 3552 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 3553 | case ACT_HTTP_REPLACE_HDR: |
| 3554 | case ACT_HTTP_REPLACE_VAL: |
Thierry FOURNIER | 5a33ac7 | 2015-03-16 23:54:35 +0100 | [diff] [blame] | 3555 | if (http_transform_header(s, &txn->req, rule->arg.hdr_add.name, |
| 3556 | rule->arg.hdr_add.name_len, |
| 3557 | &rule->arg.hdr_add.fmt, |
| 3558 | &rule->arg.hdr_add.re, rule->action)) |
Sasha Pachev | 218f064 | 2014-06-16 12:05:59 -0600 | [diff] [blame] | 3559 | return HTTP_RULE_RES_BADREQ; |
| 3560 | break; |
| 3561 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 3562 | case ACT_HTTP_DEL_HDR: |
Willy Tarreau | 96257ec | 2012-12-27 10:46:37 +0100 | [diff] [blame] | 3563 | ctx.idx = 0; |
| 3564 | /* remove all occurrences of the header */ |
| 3565 | while (http_find_header2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len, |
| 3566 | txn->req.chn->buf->p, &txn->hdr_idx, &ctx)) { |
| 3567 | http_remove_header2(&txn->req, &txn->hdr_idx, &ctx); |
Willy Tarreau | 20b0de5 | 2012-12-24 15:45:22 +0100 | [diff] [blame] | 3568 | } |
Willy Tarreau | 8560328 | 2015-01-21 20:39:27 +0100 | [diff] [blame] | 3569 | break; |
Willy Tarreau | 96257ec | 2012-12-27 10:46:37 +0100 | [diff] [blame] | 3570 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 3571 | case ACT_HTTP_SET_HDR: |
| 3572 | case ACT_HTTP_ADD_HDR: |
Thierry Fournier | 4b788f7 | 2016-06-01 13:35:36 +0200 | [diff] [blame] | 3573 | /* The scope of the trash buffer must be limited to this function. The |
| 3574 | * build_logline() function can execute a lot of other function which |
| 3575 | * can use the trash buffer. So for limiting the scope of this global |
| 3576 | * buffer, we build first the header value using build_logline, and |
| 3577 | * after we store the header name. |
| 3578 | */ |
| 3579 | len = rule->arg.hdr_add.name_len + 2, |
| 3580 | 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] | 3581 | 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] | 3582 | trash.str[rule->arg.hdr_add.name_len] = ':'; |
| 3583 | trash.str[rule->arg.hdr_add.name_len + 1] = ' '; |
| 3584 | trash.len = len; |
Willy Tarreau | 8560328 | 2015-01-21 20:39:27 +0100 | [diff] [blame] | 3585 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 3586 | if (rule->action == ACT_HTTP_SET_HDR) { |
Willy Tarreau | 8560328 | 2015-01-21 20:39:27 +0100 | [diff] [blame] | 3587 | /* remove all occurrences of the header */ |
| 3588 | ctx.idx = 0; |
| 3589 | while (http_find_header2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len, |
| 3590 | txn->req.chn->buf->p, &txn->hdr_idx, &ctx)) { |
| 3591 | http_remove_header2(&txn->req, &txn->hdr_idx, &ctx); |
| 3592 | } |
| 3593 | } |
| 3594 | |
Willy Tarreau | 96257ec | 2012-12-27 10:46:37 +0100 | [diff] [blame] | 3595 | http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, trash.len); |
| 3596 | break; |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 3597 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 3598 | case ACT_HTTP_DEL_ACL: |
| 3599 | case ACT_HTTP_DEL_MAP: { |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 3600 | struct pat_ref *ref; |
| 3601 | char *key; |
| 3602 | int len; |
| 3603 | |
| 3604 | /* collect reference */ |
| 3605 | ref = pat_ref_lookup(rule->arg.map.ref); |
| 3606 | if (!ref) |
| 3607 | continue; |
| 3608 | |
| 3609 | /* collect key */ |
| 3610 | len = build_logline(s, trash.str, trash.size, &rule->arg.map.key); |
| 3611 | key = trash.str; |
| 3612 | key[len] = '\0'; |
| 3613 | |
| 3614 | /* perform update */ |
| 3615 | /* returned code: 1=ok, 0=ko */ |
| 3616 | pat_ref_delete(ref, key); |
| 3617 | |
| 3618 | break; |
| 3619 | } |
| 3620 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 3621 | case ACT_HTTP_ADD_ACL: { |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 3622 | struct pat_ref *ref; |
| 3623 | char *key; |
| 3624 | struct chunk *trash_key; |
| 3625 | int len; |
| 3626 | |
| 3627 | trash_key = get_trash_chunk(); |
| 3628 | |
| 3629 | /* collect reference */ |
| 3630 | ref = pat_ref_lookup(rule->arg.map.ref); |
| 3631 | if (!ref) |
| 3632 | continue; |
| 3633 | |
| 3634 | /* collect key */ |
| 3635 | len = build_logline(s, trash_key->str, trash_key->size, &rule->arg.map.key); |
| 3636 | key = trash_key->str; |
| 3637 | key[len] = '\0'; |
| 3638 | |
| 3639 | /* perform update */ |
| 3640 | /* add entry only if it does not already exist */ |
| 3641 | if (pat_ref_find_elt(ref, key) == NULL) |
Thierry FOURNIER | e47e4e2 | 2014-04-28 11:18:57 +0200 | [diff] [blame] | 3642 | pat_ref_add(ref, key, NULL, NULL); |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 3643 | |
| 3644 | break; |
| 3645 | } |
| 3646 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 3647 | case ACT_HTTP_SET_MAP: { |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 3648 | struct pat_ref *ref; |
| 3649 | char *key, *value; |
| 3650 | struct chunk *trash_key, *trash_value; |
| 3651 | int len; |
| 3652 | |
| 3653 | trash_key = get_trash_chunk(); |
| 3654 | trash_value = get_trash_chunk(); |
| 3655 | |
| 3656 | /* collect reference */ |
| 3657 | ref = pat_ref_lookup(rule->arg.map.ref); |
| 3658 | if (!ref) |
| 3659 | continue; |
| 3660 | |
| 3661 | /* collect key */ |
| 3662 | len = build_logline(s, trash_key->str, trash_key->size, &rule->arg.map.key); |
| 3663 | key = trash_key->str; |
| 3664 | key[len] = '\0'; |
| 3665 | |
| 3666 | /* collect value */ |
| 3667 | len = build_logline(s, trash_value->str, trash_value->size, &rule->arg.map.value); |
| 3668 | value = trash_value->str; |
| 3669 | value[len] = '\0'; |
| 3670 | |
| 3671 | /* perform update */ |
| 3672 | if (pat_ref_find_elt(ref, key) != NULL) |
| 3673 | /* update entry if it exists */ |
| 3674 | pat_ref_set(ref, key, value, NULL); |
| 3675 | else |
| 3676 | /* insert a new entry */ |
Thierry FOURNIER | e47e4e2 | 2014-04-28 11:18:57 +0200 | [diff] [blame] | 3677 | pat_ref_add(ref, key, value, NULL); |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 3678 | |
| 3679 | break; |
| 3680 | } |
William Lallemand | 73025dd | 2014-04-24 14:38:37 +0200 | [diff] [blame] | 3681 | |
Thierry FOURNIER | 4214873 | 2015-09-02 17:17:33 +0200 | [diff] [blame] | 3682 | case ACT_CUSTOM: |
Willy Tarreau | acc9800 | 2015-09-27 23:34:39 +0200 | [diff] [blame] | 3683 | if ((px->options & PR_O_ABRT_CLOSE) && (s->req.flags & (CF_SHUTR|CF_READ_NULL|CF_READ_ERROR))) |
| 3684 | act_flags |= ACT_FLAG_FINAL; |
Willy Tarreau | 3945868 | 2015-09-27 10:33:15 +0200 | [diff] [blame] | 3685 | |
Willy Tarreau | acc9800 | 2015-09-27 23:34:39 +0200 | [diff] [blame] | 3686 | switch (rule->action_ptr(rule, px, s->sess, s, act_flags)) { |
Thierry FOURNIER | 24ff6c6 | 2015-08-06 08:52:53 +0200 | [diff] [blame] | 3687 | case ACT_RET_ERR: |
| 3688 | case ACT_RET_CONT: |
| 3689 | break; |
Thierry FOURNIER | 4214873 | 2015-09-02 17:17:33 +0200 | [diff] [blame] | 3690 | case ACT_RET_STOP: |
| 3691 | return HTTP_RULE_RES_DONE; |
Thierry FOURNIER | 24ff6c6 | 2015-08-06 08:52:53 +0200 | [diff] [blame] | 3692 | case ACT_RET_YIELD: |
Willy Tarreau | 152b81e | 2015-04-20 13:26:17 +0200 | [diff] [blame] | 3693 | s->current_rule = rule; |
Thierry FOURNIER | bc4c1ac | 2015-02-25 13:36:14 +0100 | [diff] [blame] | 3694 | return HTTP_RULE_RES_YIELD; |
| 3695 | } |
William Lallemand | 73025dd | 2014-04-24 14:38:37 +0200 | [diff] [blame] | 3696 | break; |
| 3697 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 3698 | case ACT_ACTION_TRK_SC0 ... ACT_ACTION_TRK_SCMAX: |
Willy Tarreau | 09448f7 | 2014-06-25 18:12:15 +0200 | [diff] [blame] | 3699 | /* Note: only the first valid tracking parameter of each |
| 3700 | * applies. |
| 3701 | */ |
| 3702 | |
Ruoshan Huang | e4edc6b | 2016-07-14 15:07:45 +0800 | [diff] [blame] | 3703 | if (stkctr_entry(&s->stkctr[http_trk_idx(rule->action)]) == NULL) { |
Willy Tarreau | 09448f7 | 2014-06-25 18:12:15 +0200 | [diff] [blame] | 3704 | struct stktable *t; |
| 3705 | struct stksess *ts; |
| 3706 | struct stktable_key *key; |
| 3707 | void *ptr; |
| 3708 | |
Thierry FOURNIER | 5ec63e0 | 2015-08-04 09:09:48 +0200 | [diff] [blame] | 3709 | t = rule->arg.trk_ctr.table.t; |
| 3710 | 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] | 3711 | |
| 3712 | if (key && (ts = stktable_get_entry(t, key))) { |
Ruoshan Huang | e4edc6b | 2016-07-14 15:07:45 +0800 | [diff] [blame] | 3713 | stream_track_stkctr(&s->stkctr[http_trk_idx(rule->action)], t, ts); |
Willy Tarreau | 09448f7 | 2014-06-25 18:12:15 +0200 | [diff] [blame] | 3714 | |
| 3715 | /* let's count a new HTTP request as it's the first time we do it */ |
| 3716 | ptr = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_REQ_CNT); |
| 3717 | if (ptr) |
| 3718 | stktable_data_cast(ptr, http_req_cnt)++; |
| 3719 | |
| 3720 | ptr = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_REQ_RATE); |
| 3721 | if (ptr) |
| 3722 | update_freq_ctr_period(&stktable_data_cast(ptr, http_req_rate), |
| 3723 | t->data_arg[STKTABLE_DT_HTTP_REQ_RATE].u, 1); |
| 3724 | |
Ruoshan Huang | e4edc6b | 2016-07-14 15:07:45 +0800 | [diff] [blame] | 3725 | 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] | 3726 | if (sess->fe != s->be) |
Ruoshan Huang | e4edc6b | 2016-07-14 15:07:45 +0800 | [diff] [blame] | 3727 | 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] | 3728 | } |
| 3729 | } |
Adis Nezirovic | 2fbcafc | 2015-07-06 15:44:30 +0200 | [diff] [blame] | 3730 | break; |
| 3731 | |
Thierry FOURNIER | 22e4901 | 2015-08-05 19:13:48 +0200 | [diff] [blame] | 3732 | /* other flags exists, but normaly, they never be matched. */ |
| 3733 | default: |
| 3734 | break; |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 3735 | } |
| 3736 | } |
Willy Tarreau | 96257ec | 2012-12-27 10:46:37 +0100 | [diff] [blame] | 3737 | |
| 3738 | /* we reached the end of the rules, nothing to report */ |
Willy Tarreau | 0b74833 | 2014-04-29 00:13:29 +0200 | [diff] [blame] | 3739 | return HTTP_RULE_RES_CONT; |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 3740 | } |
| 3741 | |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 3742 | |
Willy Tarreau | 51d861a | 2015-05-22 17:30:48 +0200 | [diff] [blame] | 3743 | /* Executes the http-response rules <rules> for stream <s> and proxy <px>. It |
| 3744 | * returns one of 5 possible statuses: HTTP_RULE_RES_CONT, HTTP_RULE_RES_STOP, |
| 3745 | * HTTP_RULE_RES_DONE, HTTP_RULE_RES_YIELD, or HTTP_RULE_RES_BADREQ. If *CONT |
| 3746 | * is returned, the process can continue the evaluation of next rule list. If |
| 3747 | * *STOP or *DONE is returned, the process must stop the evaluation. If *BADREQ |
| 3748 | * is returned, it means the operation could not be processed and a server error |
| 3749 | * must be returned. It may set the TX_SVDENY on txn->flags if it encounters a |
| 3750 | * deny rule. If *YIELD is returned, the caller must call again the function |
| 3751 | * with the same context. |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 3752 | */ |
Thierry FOURNIER | 9e2ef99 | 2015-02-25 13:51:19 +0100 | [diff] [blame] | 3753 | static enum rule_result |
Willy Tarreau | 987e3fb | 2015-04-04 01:09:08 +0200 | [diff] [blame] | 3754 | 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] | 3755 | { |
Willy Tarreau | 9ad7bd4 | 2015-04-03 19:19:59 +0200 | [diff] [blame] | 3756 | struct session *sess = strm_sess(s); |
Willy Tarreau | 987e3fb | 2015-04-04 01:09:08 +0200 | [diff] [blame] | 3757 | struct http_txn *txn = s->txn; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 3758 | struct connection *cli_conn; |
Thierry FOURNIER | a28a942 | 2015-08-04 19:35:46 +0200 | [diff] [blame] | 3759 | struct act_rule *rule; |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 3760 | struct hdr_ctx ctx; |
Willy Tarreau | acc9800 | 2015-09-27 23:34:39 +0200 | [diff] [blame] | 3761 | int act_flags = 0; |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 3762 | |
Thierry FOURNIER | bc4c1ac | 2015-02-25 13:36:14 +0100 | [diff] [blame] | 3763 | /* If "the current_rule_list" match the executed rule list, we are in |
| 3764 | * resume condition. If a resume is needed it is always in the action |
| 3765 | * and never in the ACL or converters. In this case, we initialise the |
| 3766 | * current rule, and go to the action execution point. |
| 3767 | */ |
Willy Tarreau | 152b81e | 2015-04-20 13:26:17 +0200 | [diff] [blame] | 3768 | if (s->current_rule) { |
| 3769 | rule = s->current_rule; |
| 3770 | s->current_rule = NULL; |
| 3771 | if (s->current_rule_list == rules) |
| 3772 | goto resume_execution; |
Thierry FOURNIER | bc4c1ac | 2015-02-25 13:36:14 +0100 | [diff] [blame] | 3773 | } |
| 3774 | s->current_rule_list = rules; |
Willy Tarreau | 152b81e | 2015-04-20 13:26:17 +0200 | [diff] [blame] | 3775 | |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 3776 | list_for_each_entry(rule, rules, list) { |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 3777 | |
| 3778 | /* check optional condition */ |
| 3779 | if (rule->cond) { |
| 3780 | int ret; |
| 3781 | |
Willy Tarreau | 192252e | 2015-04-04 01:47:55 +0200 | [diff] [blame] | 3782 | 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] | 3783 | ret = acl_pass(ret); |
| 3784 | |
| 3785 | if (rule->cond->pol == ACL_COND_UNLESS) |
| 3786 | ret = !ret; |
| 3787 | |
| 3788 | if (!ret) /* condition not matched */ |
| 3789 | continue; |
| 3790 | } |
| 3791 | |
Willy Tarreau | acc9800 | 2015-09-27 23:34:39 +0200 | [diff] [blame] | 3792 | act_flags |= ACT_FLAG_FIRST; |
Thierry FOURNIER | bc4c1ac | 2015-02-25 13:36:14 +0100 | [diff] [blame] | 3793 | resume_execution: |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 3794 | switch (rule->action) { |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 3795 | case ACT_ACTION_ALLOW: |
Thierry FOURNIER | 9e2ef99 | 2015-02-25 13:51:19 +0100 | [diff] [blame] | 3796 | return HTTP_RULE_RES_STOP; /* "allow" rules are OK */ |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 3797 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 3798 | case ACT_ACTION_DENY: |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 3799 | txn->flags |= TX_SVDENY; |
Thierry FOURNIER | 9e2ef99 | 2015-02-25 13:51:19 +0100 | [diff] [blame] | 3800 | return HTTP_RULE_RES_STOP; |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 3801 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 3802 | case ACT_HTTP_SET_NICE: |
Willy Tarreau | f4c43c1 | 2013-06-11 17:01:13 +0200 | [diff] [blame] | 3803 | s->task->nice = rule->arg.nice; |
| 3804 | break; |
| 3805 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 3806 | case ACT_HTTP_SET_TOS: |
Willy Tarreau | 9ad7bd4 | 2015-04-03 19:19:59 +0200 | [diff] [blame] | 3807 | if ((cli_conn = objt_conn(sess->origin)) && conn_ctrl_ready(cli_conn)) |
Vincent Bernat | 6e61589 | 2016-05-18 16:17:44 +0200 | [diff] [blame] | 3808 | 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] | 3809 | break; |
| 3810 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 3811 | case ACT_HTTP_SET_MARK: |
Willy Tarreau | 51347ed | 2013-06-11 19:34:13 +0200 | [diff] [blame] | 3812 | #ifdef SO_MARK |
Willy Tarreau | 9ad7bd4 | 2015-04-03 19:19:59 +0200 | [diff] [blame] | 3813 | if ((cli_conn = objt_conn(sess->origin)) && conn_ctrl_ready(cli_conn)) |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 3814 | 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] | 3815 | #endif |
| 3816 | break; |
| 3817 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 3818 | case ACT_HTTP_SET_LOGL: |
Willy Tarreau | 9a355ec | 2013-06-11 17:45:46 +0200 | [diff] [blame] | 3819 | s->logs.level = rule->arg.loglevel; |
| 3820 | break; |
| 3821 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 3822 | case ACT_HTTP_REPLACE_HDR: |
| 3823 | case ACT_HTTP_REPLACE_VAL: |
Thierry FOURNIER | 5a33ac7 | 2015-03-16 23:54:35 +0100 | [diff] [blame] | 3824 | if (http_transform_header(s, &txn->rsp, rule->arg.hdr_add.name, |
| 3825 | rule->arg.hdr_add.name_len, |
| 3826 | &rule->arg.hdr_add.fmt, |
| 3827 | &rule->arg.hdr_add.re, rule->action)) |
Christopher Faulet | cdade94 | 2017-02-08 12:41:31 +0100 | [diff] [blame] | 3828 | return HTTP_RULE_RES_BADREQ; |
Sasha Pachev | 218f064 | 2014-06-16 12:05:59 -0600 | [diff] [blame] | 3829 | break; |
| 3830 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 3831 | case ACT_HTTP_DEL_HDR: |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 3832 | ctx.idx = 0; |
| 3833 | /* remove all occurrences of the header */ |
| 3834 | while (http_find_header2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len, |
| 3835 | txn->rsp.chn->buf->p, &txn->hdr_idx, &ctx)) { |
| 3836 | http_remove_header2(&txn->rsp, &txn->hdr_idx, &ctx); |
| 3837 | } |
Willy Tarreau | 8560328 | 2015-01-21 20:39:27 +0100 | [diff] [blame] | 3838 | break; |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 3839 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 3840 | case ACT_HTTP_SET_HDR: |
| 3841 | case ACT_HTTP_ADD_HDR: |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 3842 | chunk_printf(&trash, "%s: ", rule->arg.hdr_add.name); |
| 3843 | memcpy(trash.str, rule->arg.hdr_add.name, rule->arg.hdr_add.name_len); |
| 3844 | trash.len = rule->arg.hdr_add.name_len; |
| 3845 | trash.str[trash.len++] = ':'; |
| 3846 | trash.str[trash.len++] = ' '; |
| 3847 | 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] | 3848 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 3849 | if (rule->action == ACT_HTTP_SET_HDR) { |
Willy Tarreau | 8560328 | 2015-01-21 20:39:27 +0100 | [diff] [blame] | 3850 | /* remove all occurrences of the header */ |
| 3851 | ctx.idx = 0; |
| 3852 | while (http_find_header2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len, |
| 3853 | txn->rsp.chn->buf->p, &txn->hdr_idx, &ctx)) { |
| 3854 | http_remove_header2(&txn->rsp, &txn->hdr_idx, &ctx); |
| 3855 | } |
| 3856 | } |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 3857 | http_header_add_tail2(&txn->rsp, &txn->hdr_idx, trash.str, trash.len); |
| 3858 | break; |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 3859 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 3860 | case ACT_HTTP_DEL_ACL: |
| 3861 | case ACT_HTTP_DEL_MAP: { |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 3862 | struct pat_ref *ref; |
| 3863 | char *key; |
| 3864 | int len; |
| 3865 | |
| 3866 | /* collect reference */ |
| 3867 | ref = pat_ref_lookup(rule->arg.map.ref); |
| 3868 | if (!ref) |
| 3869 | continue; |
| 3870 | |
| 3871 | /* collect key */ |
| 3872 | len = build_logline(s, trash.str, trash.size, &rule->arg.map.key); |
| 3873 | key = trash.str; |
| 3874 | key[len] = '\0'; |
| 3875 | |
| 3876 | /* perform update */ |
| 3877 | /* returned code: 1=ok, 0=ko */ |
| 3878 | pat_ref_delete(ref, key); |
| 3879 | |
| 3880 | break; |
| 3881 | } |
| 3882 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 3883 | case ACT_HTTP_ADD_ACL: { |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 3884 | struct pat_ref *ref; |
| 3885 | char *key; |
| 3886 | struct chunk *trash_key; |
| 3887 | int len; |
| 3888 | |
| 3889 | trash_key = get_trash_chunk(); |
| 3890 | |
| 3891 | /* collect reference */ |
| 3892 | ref = pat_ref_lookup(rule->arg.map.ref); |
| 3893 | if (!ref) |
| 3894 | continue; |
| 3895 | |
| 3896 | /* collect key */ |
| 3897 | len = build_logline(s, trash_key->str, trash_key->size, &rule->arg.map.key); |
| 3898 | key = trash_key->str; |
| 3899 | key[len] = '\0'; |
| 3900 | |
| 3901 | /* perform update */ |
| 3902 | /* check if the entry already exists */ |
| 3903 | if (pat_ref_find_elt(ref, key) == NULL) |
Thierry FOURNIER | e47e4e2 | 2014-04-28 11:18:57 +0200 | [diff] [blame] | 3904 | pat_ref_add(ref, key, NULL, NULL); |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 3905 | |
| 3906 | break; |
| 3907 | } |
| 3908 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 3909 | case ACT_HTTP_SET_MAP: { |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 3910 | struct pat_ref *ref; |
| 3911 | char *key, *value; |
| 3912 | struct chunk *trash_key, *trash_value; |
| 3913 | int len; |
| 3914 | |
| 3915 | trash_key = get_trash_chunk(); |
| 3916 | trash_value = get_trash_chunk(); |
| 3917 | |
| 3918 | /* collect reference */ |
| 3919 | ref = pat_ref_lookup(rule->arg.map.ref); |
| 3920 | if (!ref) |
| 3921 | continue; |
| 3922 | |
| 3923 | /* collect key */ |
| 3924 | len = build_logline(s, trash_key->str, trash_key->size, &rule->arg.map.key); |
| 3925 | key = trash_key->str; |
| 3926 | key[len] = '\0'; |
| 3927 | |
| 3928 | /* collect value */ |
| 3929 | len = build_logline(s, trash_value->str, trash_value->size, &rule->arg.map.value); |
| 3930 | value = trash_value->str; |
| 3931 | value[len] = '\0'; |
| 3932 | |
| 3933 | /* perform update */ |
| 3934 | if (pat_ref_find_elt(ref, key) != NULL) |
| 3935 | /* update entry if it exists */ |
| 3936 | pat_ref_set(ref, key, value, NULL); |
| 3937 | else |
| 3938 | /* insert a new entry */ |
Thierry FOURNIER | e47e4e2 | 2014-04-28 11:18:57 +0200 | [diff] [blame] | 3939 | pat_ref_add(ref, key, value, NULL); |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 3940 | |
| 3941 | break; |
| 3942 | } |
William Lallemand | 73025dd | 2014-04-24 14:38:37 +0200 | [diff] [blame] | 3943 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 3944 | case ACT_HTTP_REDIR: |
Willy Tarreau | 51d861a | 2015-05-22 17:30:48 +0200 | [diff] [blame] | 3945 | if (!http_apply_redirect_rule(rule->arg.redir, s, txn)) |
| 3946 | return HTTP_RULE_RES_BADREQ; |
| 3947 | return HTTP_RULE_RES_DONE; |
| 3948 | |
Ruoshan Huang | e4edc6b | 2016-07-14 15:07:45 +0800 | [diff] [blame] | 3949 | case ACT_ACTION_TRK_SC0 ... ACT_ACTION_TRK_SCMAX: |
| 3950 | /* Note: only the first valid tracking parameter of each |
| 3951 | * applies. |
| 3952 | */ |
| 3953 | |
| 3954 | if (stkctr_entry(&s->stkctr[http_trk_idx(rule->action)]) == NULL) { |
| 3955 | struct stktable *t; |
| 3956 | struct stksess *ts; |
| 3957 | struct stktable_key *key; |
| 3958 | void *ptr; |
| 3959 | |
| 3960 | t = rule->arg.trk_ctr.table.t; |
| 3961 | key = stktable_fetch_key(t, s->be, sess, s, SMP_OPT_DIR_RES | SMP_OPT_FINAL, rule->arg.trk_ctr.expr, NULL); |
| 3962 | |
| 3963 | if (key && (ts = stktable_get_entry(t, key))) { |
| 3964 | stream_track_stkctr(&s->stkctr[http_trk_idx(rule->action)], t, ts); |
| 3965 | |
| 3966 | /* let's count a new HTTP request as it's the first time we do it */ |
| 3967 | ptr = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_REQ_CNT); |
| 3968 | if (ptr) |
| 3969 | stktable_data_cast(ptr, http_req_cnt)++; |
| 3970 | |
| 3971 | ptr = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_REQ_RATE); |
| 3972 | if (ptr) |
| 3973 | update_freq_ctr_period(&stktable_data_cast(ptr, http_req_rate), |
| 3974 | t->data_arg[STKTABLE_DT_HTTP_REQ_RATE].u, 1); |
| 3975 | |
| 3976 | stkctr_set_flags(&s->stkctr[http_trk_idx(rule->action)], STKCTR_TRACK_CONTENT); |
| 3977 | if (sess->fe != s->be) |
| 3978 | stkctr_set_flags(&s->stkctr[http_trk_idx(rule->action)], STKCTR_TRACK_BACKEND); |
| 3979 | |
| 3980 | /* When the client triggers a 4xx from the server, it's most often due |
| 3981 | * to a missing object or permission. These events should be tracked |
| 3982 | * because if they happen often, it may indicate a brute force or a |
| 3983 | * vulnerability scan. Normally this is done when receiving the response |
| 3984 | * but here we're tracking after this ought to have been done so we have |
| 3985 | * to do it on purpose. |
| 3986 | */ |
Willy Tarreau | 3146a4c | 2016-07-26 15:22:33 +0200 | [diff] [blame] | 3987 | if ((unsigned)(txn->status - 400) < 100) { |
| 3988 | ptr = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_ERR_CNT); |
| 3989 | if (ptr) |
| 3990 | stktable_data_cast(ptr, http_err_cnt)++; |
| 3991 | |
| 3992 | ptr = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_ERR_RATE); |
| 3993 | if (ptr) |
| 3994 | update_freq_ctr_period(&stktable_data_cast(ptr, http_err_rate), |
| 3995 | t->data_arg[STKTABLE_DT_HTTP_ERR_RATE].u, 1); |
| 3996 | } |
Ruoshan Huang | e4edc6b | 2016-07-14 15:07:45 +0800 | [diff] [blame] | 3997 | } |
| 3998 | } |
| 3999 | break; |
| 4000 | |
Thierry FOURNIER | 4214873 | 2015-09-02 17:17:33 +0200 | [diff] [blame] | 4001 | case ACT_CUSTOM: |
Willy Tarreau | acc9800 | 2015-09-27 23:34:39 +0200 | [diff] [blame] | 4002 | if ((px->options & PR_O_ABRT_CLOSE) && (s->req.flags & (CF_SHUTR|CF_READ_NULL|CF_READ_ERROR))) |
| 4003 | act_flags |= ACT_FLAG_FINAL; |
Willy Tarreau | 3945868 | 2015-09-27 10:33:15 +0200 | [diff] [blame] | 4004 | |
Willy Tarreau | acc9800 | 2015-09-27 23:34:39 +0200 | [diff] [blame] | 4005 | switch (rule->action_ptr(rule, px, s->sess, s, act_flags)) { |
Thierry FOURNIER | 24ff6c6 | 2015-08-06 08:52:53 +0200 | [diff] [blame] | 4006 | case ACT_RET_ERR: |
| 4007 | case ACT_RET_CONT: |
| 4008 | break; |
Thierry FOURNIER | 4214873 | 2015-09-02 17:17:33 +0200 | [diff] [blame] | 4009 | case ACT_RET_STOP: |
| 4010 | return HTTP_RULE_RES_STOP; |
Thierry FOURNIER | 24ff6c6 | 2015-08-06 08:52:53 +0200 | [diff] [blame] | 4011 | case ACT_RET_YIELD: |
Willy Tarreau | 152b81e | 2015-04-20 13:26:17 +0200 | [diff] [blame] | 4012 | s->current_rule = rule; |
Thierry FOURNIER | bc4c1ac | 2015-02-25 13:36:14 +0100 | [diff] [blame] | 4013 | return HTTP_RULE_RES_YIELD; |
| 4014 | } |
William Lallemand | 73025dd | 2014-04-24 14:38:37 +0200 | [diff] [blame] | 4015 | break; |
| 4016 | |
Thierry FOURNIER | 22e4901 | 2015-08-05 19:13:48 +0200 | [diff] [blame] | 4017 | /* other flags exists, but normaly, they never be matched. */ |
| 4018 | default: |
| 4019 | break; |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 4020 | } |
| 4021 | } |
| 4022 | |
| 4023 | /* we reached the end of the rules, nothing to report */ |
Thierry FOURNIER | 9e2ef99 | 2015-02-25 13:51:19 +0100 | [diff] [blame] | 4024 | return HTTP_RULE_RES_CONT; |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 4025 | } |
| 4026 | |
| 4027 | |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 4028 | /* Perform an HTTP redirect based on the information in <rule>. The function |
| 4029 | * returns non-zero on success, or zero in case of a, irrecoverable error such |
| 4030 | * as too large a request to build a valid response. |
| 4031 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 4032 | 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] | 4033 | { |
Willy Tarreau | b329a31 | 2015-05-22 16:27:37 +0200 | [diff] [blame] | 4034 | struct http_msg *req = &txn->req; |
| 4035 | struct http_msg *res = &txn->rsp; |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 4036 | const char *msg_fmt; |
Thierry FOURNIER | 0d94576 | 2017-01-28 07:39:53 +0100 | [diff] [blame] | 4037 | struct chunk *chunk; |
| 4038 | int ret = 0; |
| 4039 | |
| 4040 | chunk = alloc_trash_chunk(); |
| 4041 | if (!chunk) |
| 4042 | goto leave; |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 4043 | |
| 4044 | /* build redirect message */ |
| 4045 | switch(rule->code) { |
Yves Lafon | 3e8d1ae | 2013-03-11 11:06:05 -0400 | [diff] [blame] | 4046 | case 308: |
| 4047 | msg_fmt = HTTP_308; |
| 4048 | break; |
| 4049 | case 307: |
| 4050 | msg_fmt = HTTP_307; |
| 4051 | break; |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 4052 | case 303: |
| 4053 | msg_fmt = HTTP_303; |
| 4054 | break; |
| 4055 | case 301: |
| 4056 | msg_fmt = HTTP_301; |
| 4057 | break; |
| 4058 | case 302: |
| 4059 | default: |
| 4060 | msg_fmt = HTTP_302; |
| 4061 | break; |
| 4062 | } |
| 4063 | |
Thierry FOURNIER | 0d94576 | 2017-01-28 07:39:53 +0100 | [diff] [blame] | 4064 | if (unlikely(!chunk_strcpy(chunk, msg_fmt))) |
| 4065 | goto leave; |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 4066 | |
| 4067 | switch(rule->type) { |
| 4068 | case REDIRECT_TYPE_SCHEME: { |
| 4069 | const char *path; |
| 4070 | const char *host; |
| 4071 | struct hdr_ctx ctx; |
| 4072 | int pathlen; |
| 4073 | int hostlen; |
| 4074 | |
| 4075 | host = ""; |
| 4076 | hostlen = 0; |
| 4077 | ctx.idx = 0; |
Willy Tarreau | b329a31 | 2015-05-22 16:27:37 +0200 | [diff] [blame] | 4078 | 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] | 4079 | host = ctx.line + ctx.val; |
| 4080 | hostlen = ctx.vlen; |
| 4081 | } |
| 4082 | |
| 4083 | path = http_get_path(txn); |
| 4084 | /* build message using path */ |
| 4085 | if (path) { |
Willy Tarreau | b329a31 | 2015-05-22 16:27:37 +0200 | [diff] [blame] | 4086 | 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] | 4087 | if (rule->flags & REDIRECT_FLAG_DROP_QS) { |
| 4088 | int qs = 0; |
| 4089 | while (qs < pathlen) { |
| 4090 | if (path[qs] == '?') { |
| 4091 | pathlen = qs; |
| 4092 | break; |
| 4093 | } |
| 4094 | qs++; |
| 4095 | } |
| 4096 | } |
| 4097 | } else { |
| 4098 | path = "/"; |
| 4099 | pathlen = 1; |
| 4100 | } |
| 4101 | |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 4102 | if (rule->rdr_str) { /* this is an old "redirect" rule */ |
| 4103 | /* check if we can add scheme + "://" + host + path */ |
Thierry FOURNIER | 0d94576 | 2017-01-28 07:39:53 +0100 | [diff] [blame] | 4104 | if (chunk->len + rule->rdr_len + 3 + hostlen + pathlen > chunk->size - 4) |
| 4105 | goto leave; |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 4106 | |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 4107 | /* add scheme */ |
Thierry FOURNIER | 0d94576 | 2017-01-28 07:39:53 +0100 | [diff] [blame] | 4108 | memcpy(chunk->str + chunk->len, rule->rdr_str, rule->rdr_len); |
| 4109 | chunk->len += rule->rdr_len; |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 4110 | } |
| 4111 | else { |
| 4112 | /* add scheme with executing log format */ |
Thierry FOURNIER | 0d94576 | 2017-01-28 07:39:53 +0100 | [diff] [blame] | 4113 | chunk->len += build_logline(s, chunk->str + chunk->len, chunk->size - chunk->len, &rule->rdr_fmt); |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 4114 | |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 4115 | /* check if we can add scheme + "://" + host + path */ |
Thierry FOURNIER | 0d94576 | 2017-01-28 07:39:53 +0100 | [diff] [blame] | 4116 | if (chunk->len + 3 + hostlen + pathlen > chunk->size - 4) |
| 4117 | goto leave; |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 4118 | } |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 4119 | /* add "://" */ |
Thierry FOURNIER | 0d94576 | 2017-01-28 07:39:53 +0100 | [diff] [blame] | 4120 | memcpy(chunk->str + chunk->len, "://", 3); |
| 4121 | chunk->len += 3; |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 4122 | |
| 4123 | /* add host */ |
Thierry FOURNIER | 0d94576 | 2017-01-28 07:39:53 +0100 | [diff] [blame] | 4124 | memcpy(chunk->str + chunk->len, host, hostlen); |
| 4125 | chunk->len += hostlen; |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 4126 | |
| 4127 | /* add path */ |
Thierry FOURNIER | 0d94576 | 2017-01-28 07:39:53 +0100 | [diff] [blame] | 4128 | memcpy(chunk->str + chunk->len, path, pathlen); |
| 4129 | chunk->len += pathlen; |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 4130 | |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 4131 | /* append a slash at the end of the location if needed and missing */ |
Thierry FOURNIER | 0d94576 | 2017-01-28 07:39:53 +0100 | [diff] [blame] | 4132 | if (chunk->len && chunk->str[chunk->len - 1] != '/' && |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 4133 | (rule->flags & REDIRECT_FLAG_APPEND_SLASH)) { |
Thierry FOURNIER | 0d94576 | 2017-01-28 07:39:53 +0100 | [diff] [blame] | 4134 | if (chunk->len > chunk->size - 5) |
| 4135 | goto leave; |
| 4136 | chunk->str[chunk->len] = '/'; |
| 4137 | chunk->len++; |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 4138 | } |
| 4139 | |
| 4140 | break; |
| 4141 | } |
| 4142 | case REDIRECT_TYPE_PREFIX: { |
| 4143 | const char *path; |
| 4144 | int pathlen; |
| 4145 | |
| 4146 | path = http_get_path(txn); |
| 4147 | /* build message using path */ |
| 4148 | if (path) { |
Willy Tarreau | b329a31 | 2015-05-22 16:27:37 +0200 | [diff] [blame] | 4149 | 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] | 4150 | if (rule->flags & REDIRECT_FLAG_DROP_QS) { |
| 4151 | int qs = 0; |
| 4152 | while (qs < pathlen) { |
| 4153 | if (path[qs] == '?') { |
| 4154 | pathlen = qs; |
| 4155 | break; |
| 4156 | } |
| 4157 | qs++; |
| 4158 | } |
| 4159 | } |
| 4160 | } else { |
| 4161 | path = "/"; |
| 4162 | pathlen = 1; |
| 4163 | } |
| 4164 | |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 4165 | if (rule->rdr_str) { /* this is an old "redirect" rule */ |
Thierry FOURNIER | 0d94576 | 2017-01-28 07:39:53 +0100 | [diff] [blame] | 4166 | if (chunk->len + rule->rdr_len + pathlen > chunk->size - 4) |
| 4167 | goto leave; |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 4168 | |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 4169 | /* add prefix. Note that if prefix == "/", we don't want to |
| 4170 | * add anything, otherwise it makes it hard for the user to |
| 4171 | * configure a self-redirection. |
| 4172 | */ |
| 4173 | if (rule->rdr_len != 1 || *rule->rdr_str != '/') { |
Thierry FOURNIER | 0d94576 | 2017-01-28 07:39:53 +0100 | [diff] [blame] | 4174 | memcpy(chunk->str + chunk->len, rule->rdr_str, rule->rdr_len); |
| 4175 | chunk->len += rule->rdr_len; |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 4176 | } |
| 4177 | } |
| 4178 | else { |
| 4179 | /* add prefix with executing log format */ |
Thierry FOURNIER | 0d94576 | 2017-01-28 07:39:53 +0100 | [diff] [blame] | 4180 | chunk->len += build_logline(s, chunk->str + chunk->len, chunk->size - chunk->len, &rule->rdr_fmt); |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 4181 | |
| 4182 | /* Check length */ |
Thierry FOURNIER | 0d94576 | 2017-01-28 07:39:53 +0100 | [diff] [blame] | 4183 | if (chunk->len + pathlen > chunk->size - 4) |
| 4184 | goto leave; |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 4185 | } |
| 4186 | |
| 4187 | /* add path */ |
Thierry FOURNIER | 0d94576 | 2017-01-28 07:39:53 +0100 | [diff] [blame] | 4188 | memcpy(chunk->str + chunk->len, path, pathlen); |
| 4189 | chunk->len += pathlen; |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 4190 | |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 4191 | /* append a slash at the end of the location if needed and missing */ |
Thierry FOURNIER | 0d94576 | 2017-01-28 07:39:53 +0100 | [diff] [blame] | 4192 | if (chunk->len && chunk->str[chunk->len - 1] != '/' && |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 4193 | (rule->flags & REDIRECT_FLAG_APPEND_SLASH)) { |
Thierry FOURNIER | 0d94576 | 2017-01-28 07:39:53 +0100 | [diff] [blame] | 4194 | if (chunk->len > chunk->size - 5) |
| 4195 | goto leave; |
| 4196 | chunk->str[chunk->len] = '/'; |
| 4197 | chunk->len++; |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 4198 | } |
| 4199 | |
| 4200 | break; |
| 4201 | } |
| 4202 | case REDIRECT_TYPE_LOCATION: |
| 4203 | default: |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 4204 | if (rule->rdr_str) { /* this is an old "redirect" rule */ |
Thierry FOURNIER | 0d94576 | 2017-01-28 07:39:53 +0100 | [diff] [blame] | 4205 | if (chunk->len + rule->rdr_len > chunk->size - 4) |
| 4206 | goto leave; |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 4207 | |
| 4208 | /* add location */ |
Thierry FOURNIER | 0d94576 | 2017-01-28 07:39:53 +0100 | [diff] [blame] | 4209 | memcpy(chunk->str + chunk->len, rule->rdr_str, rule->rdr_len); |
| 4210 | chunk->len += rule->rdr_len; |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 4211 | } |
| 4212 | else { |
| 4213 | /* add location with executing log format */ |
Thierry FOURNIER | 0d94576 | 2017-01-28 07:39:53 +0100 | [diff] [blame] | 4214 | chunk->len += build_logline(s, chunk->str + chunk->len, chunk->size - chunk->len, &rule->rdr_fmt); |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 4215 | |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 4216 | /* Check left length */ |
Thierry FOURNIER | 0d94576 | 2017-01-28 07:39:53 +0100 | [diff] [blame] | 4217 | if (chunk->len > chunk->size - 4) |
| 4218 | goto leave; |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 4219 | } |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 4220 | break; |
| 4221 | } |
| 4222 | |
| 4223 | if (rule->cookie_len) { |
Thierry FOURNIER | 0d94576 | 2017-01-28 07:39:53 +0100 | [diff] [blame] | 4224 | memcpy(chunk->str + chunk->len, "\r\nSet-Cookie: ", 14); |
| 4225 | chunk->len += 14; |
| 4226 | memcpy(chunk->str + chunk->len, rule->cookie_str, rule->cookie_len); |
| 4227 | chunk->len += rule->cookie_len; |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 4228 | } |
| 4229 | |
Willy Tarreau | 19b1412 | 2017-02-28 09:48:11 +0100 | [diff] [blame] | 4230 | /* add end of headers and the keep-alive/close status. */ |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 4231 | txn->status = rule->code; |
| 4232 | /* let's log the request time */ |
| 4233 | s->logs.tv_request = now; |
| 4234 | |
Christopher Faulet | be821b9 | 2017-03-30 11:21:53 +0200 | [diff] [blame] | 4235 | if (((!(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] | 4236 | ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL || |
| 4237 | (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL)) { |
| 4238 | /* keep-alive possible */ |
Willy Tarreau | b329a31 | 2015-05-22 16:27:37 +0200 | [diff] [blame] | 4239 | if (!(req->flags & HTTP_MSGF_VER_11)) { |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 4240 | if (unlikely(txn->flags & TX_USE_PX_CONN)) { |
Thierry FOURNIER | 0d94576 | 2017-01-28 07:39:53 +0100 | [diff] [blame] | 4241 | memcpy(chunk->str + chunk->len, "\r\nProxy-Connection: keep-alive", 30); |
| 4242 | chunk->len += 30; |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 4243 | } else { |
Thierry FOURNIER | 0d94576 | 2017-01-28 07:39:53 +0100 | [diff] [blame] | 4244 | memcpy(chunk->str + chunk->len, "\r\nConnection: keep-alive", 24); |
| 4245 | chunk->len += 24; |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 4246 | } |
| 4247 | } |
Thierry FOURNIER | 0d94576 | 2017-01-28 07:39:53 +0100 | [diff] [blame] | 4248 | memcpy(chunk->str + chunk->len, "\r\n\r\n", 4); |
| 4249 | chunk->len += 4; |
| 4250 | FLT_STRM_CB(s, flt_http_reply(s, txn->status, chunk)); |
| 4251 | bo_inject(res->chn, chunk->str, chunk->len); |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 4252 | /* "eat" the request */ |
Willy Tarreau | b329a31 | 2015-05-22 16:27:37 +0200 | [diff] [blame] | 4253 | bi_fast_delete(req->chn->buf, req->sov); |
| 4254 | req->next -= req->sov; |
| 4255 | req->sov = 0; |
Christopher Faulet | 0184ea7 | 2017-01-05 14:06:34 +0100 | [diff] [blame] | 4256 | s->req.analysers = AN_REQ_HTTP_XFER_BODY | (s->req.analysers & AN_REQ_FLT_END); |
Christopher Faulet | 014e39c | 2017-03-10 13:52:30 +0100 | [diff] [blame] | 4257 | s->res.analysers = AN_RES_HTTP_XFER_BODY | (s->res.analysers & AN_RES_FLT_END); |
Willy Tarreau | b329a31 | 2015-05-22 16:27:37 +0200 | [diff] [blame] | 4258 | req->msg_state = HTTP_MSG_CLOSED; |
| 4259 | res->msg_state = HTTP_MSG_DONE; |
Willy Tarreau | 51d861a | 2015-05-22 17:30:48 +0200 | [diff] [blame] | 4260 | /* Trim any possible response */ |
| 4261 | res->chn->buf->i = 0; |
| 4262 | res->next = res->sov = 0; |
Christopher Faulet | 9f724ed | 2017-04-20 14:16:13 +0200 | [diff] [blame] | 4263 | /* If not already done, don't perform any connection establishment */ |
| 4264 | channel_dont_connect(req->chn); |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 4265 | } else { |
| 4266 | /* keep-alive not possible */ |
| 4267 | if (unlikely(txn->flags & TX_USE_PX_CONN)) { |
Thierry FOURNIER | 0d94576 | 2017-01-28 07:39:53 +0100 | [diff] [blame] | 4268 | memcpy(chunk->str + chunk->len, "\r\nProxy-Connection: close\r\n\r\n", 29); |
| 4269 | chunk->len += 29; |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 4270 | } else { |
Thierry FOURNIER | 0d94576 | 2017-01-28 07:39:53 +0100 | [diff] [blame] | 4271 | memcpy(chunk->str + chunk->len, "\r\nConnection: close\r\n\r\n", 23); |
| 4272 | chunk->len += 23; |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 4273 | } |
Thierry FOURNIER | 0d94576 | 2017-01-28 07:39:53 +0100 | [diff] [blame] | 4274 | http_reply_and_close(s, txn->status, chunk); |
Christopher Faulet | 0184ea7 | 2017-01-05 14:06:34 +0100 | [diff] [blame] | 4275 | req->chn->analysers &= AN_REQ_FLT_END; |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 4276 | } |
| 4277 | |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 4278 | if (!(s->flags & SF_ERR_MASK)) |
| 4279 | s->flags |= SF_ERR_LOCAL; |
| 4280 | if (!(s->flags & SF_FINST_MASK)) |
| 4281 | s->flags |= SF_FINST_R; |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 4282 | |
Thierry FOURNIER | 0d94576 | 2017-01-28 07:39:53 +0100 | [diff] [blame] | 4283 | ret = 1; |
| 4284 | leave: |
| 4285 | free_trash_chunk(chunk); |
| 4286 | return ret; |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 4287 | } |
| 4288 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 4289 | /* This stream analyser runs all HTTP request processing which is common to |
| 4290 | * frontends and backends, which means blocking ACLs, filters, connection-close, |
| 4291 | * reqadd, stats and redirects. This is performed for the designated proxy. |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 4292 | * 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] | 4293 | * either needs more data or wants to immediately abort the request (eg: deny, |
| 4294 | * error, ...). |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 4295 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 4296 | 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] | 4297 | { |
Willy Tarreau | fb0afa7 | 2015-04-03 14:46:27 +0200 | [diff] [blame] | 4298 | struct session *sess = s->sess; |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 4299 | struct http_txn *txn = s->txn; |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 4300 | struct http_msg *msg = &txn->req; |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 4301 | struct redirect_rule *rule; |
Willy Tarreau | f4f0412 | 2010-01-28 18:10:50 +0100 | [diff] [blame] | 4302 | struct cond_wordlist *wl; |
Willy Tarreau | 0b74833 | 2014-04-29 00:13:29 +0200 | [diff] [blame] | 4303 | enum rule_result verdict; |
Willy Tarreau | 58727ec | 2016-05-25 16:23:59 +0200 | [diff] [blame] | 4304 | int deny_status = HTTP_ERR_403; |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 4305 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 4306 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) { |
Willy Tarreau | 51aecc7 | 2009-07-12 09:47:04 +0200 | [diff] [blame] | 4307 | /* we need more data */ |
Thierry FOURNIER | bc4c1ac | 2015-02-25 13:36:14 +0100 | [diff] [blame] | 4308 | goto return_prx_yield; |
Willy Tarreau | 51aecc7 | 2009-07-12 09:47:04 +0200 | [diff] [blame] | 4309 | } |
| 4310 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 4311 | 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] | 4312 | now_ms, __FUNCTION__, |
| 4313 | s, |
| 4314 | req, |
| 4315 | req->rex, req->wex, |
| 4316 | req->flags, |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 4317 | req->buf->i, |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 4318 | req->analysers); |
| 4319 | |
Willy Tarreau | 6541083 | 2014-04-28 21:25:43 +0200 | [diff] [blame] | 4320 | /* just in case we have some per-backend tracking */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 4321 | stream_inc_be_http_req_ctr(s); |
Willy Tarreau | 6541083 | 2014-04-28 21:25:43 +0200 | [diff] [blame] | 4322 | |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 4323 | /* evaluate http-request rules */ |
Willy Tarreau | 0b74833 | 2014-04-29 00:13:29 +0200 | [diff] [blame] | 4324 | if (!LIST_ISEMPTY(&px->http_req_rules)) { |
Willy Tarreau | 58727ec | 2016-05-25 16:23:59 +0200 | [diff] [blame] | 4325 | 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] | 4326 | |
Willy Tarreau | 0b74833 | 2014-04-29 00:13:29 +0200 | [diff] [blame] | 4327 | switch (verdict) { |
Thierry FOURNIER | bc4c1ac | 2015-02-25 13:36:14 +0100 | [diff] [blame] | 4328 | case HTTP_RULE_RES_YIELD: /* some data miss, call the function later. */ |
| 4329 | goto return_prx_yield; |
| 4330 | |
Willy Tarreau | 0b74833 | 2014-04-29 00:13:29 +0200 | [diff] [blame] | 4331 | case HTTP_RULE_RES_CONT: |
| 4332 | case HTTP_RULE_RES_STOP: /* nothing to do */ |
| 4333 | break; |
Willy Tarreau | 5254259 | 2014-04-28 18:33:26 +0200 | [diff] [blame] | 4334 | |
Willy Tarreau | 0b74833 | 2014-04-29 00:13:29 +0200 | [diff] [blame] | 4335 | case HTTP_RULE_RES_DENY: /* deny or tarpit */ |
| 4336 | if (txn->flags & TX_CLTARPIT) |
| 4337 | goto tarpit; |
| 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 | case HTTP_RULE_RES_ABRT: /* abort request, response already sent. Eg: auth */ |
| 4341 | goto return_prx_cond; |
Willy Tarreau | 5254259 | 2014-04-28 18:33:26 +0200 | [diff] [blame] | 4342 | |
Willy Tarreau | 0b74833 | 2014-04-29 00:13:29 +0200 | [diff] [blame] | 4343 | case HTTP_RULE_RES_DONE: /* OK, but terminate request processing (eg: redirect) */ |
Willy Tarreau | 5254259 | 2014-04-28 18:33:26 +0200 | [diff] [blame] | 4344 | goto done; |
| 4345 | |
Willy Tarreau | 0b74833 | 2014-04-29 00:13:29 +0200 | [diff] [blame] | 4346 | case HTTP_RULE_RES_BADREQ: /* failed with a bad request */ |
| 4347 | goto return_bad_req; |
| 4348 | } |
Willy Tarreau | bbba2a8 | 2014-04-28 13:57:26 +0200 | [diff] [blame] | 4349 | } |
| 4350 | |
Willy Tarreau | 5254259 | 2014-04-28 18:33:26 +0200 | [diff] [blame] | 4351 | /* OK at this stage, we know that the request was accepted according to |
| 4352 | * the http-request rules, we can check for the stats. Note that the |
| 4353 | * URI is detected *before* the req* rules in order not to be affected |
| 4354 | * by a possible reqrep, while they are processed *after* so that a |
| 4355 | * reqdeny can still block them. This clearly needs to change in 1.6! |
| 4356 | */ |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 4357 | if (stats_check_uri(&s->si[1], txn, px)) { |
Willy Tarreau | 5254259 | 2014-04-28 18:33:26 +0200 | [diff] [blame] | 4358 | s->target = &http_stats_applet.obj_type; |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 4359 | 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] | 4360 | txn->status = 500; |
| 4361 | s->logs.tv_request = now; |
Jarno Huuskonen | 9e6906b | 2017-03-06 14:21:49 +0200 | [diff] [blame] | 4362 | http_reply_and_close(s, txn->status, http_error_message(s)); |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 4363 | |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 4364 | if (!(s->flags & SF_ERR_MASK)) |
| 4365 | s->flags |= SF_ERR_RESOURCE; |
Willy Tarreau | 5254259 | 2014-04-28 18:33:26 +0200 | [diff] [blame] | 4366 | goto return_prx_cond; |
| 4367 | } |
| 4368 | |
| 4369 | /* parse the whole stats request and extract the relevant information */ |
| 4370 | http_handle_stats(s, req); |
Willy Tarreau | 58727ec | 2016-05-25 16:23:59 +0200 | [diff] [blame] | 4371 | 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] | 4372 | /* not all actions implemented: deny, allow, auth */ |
Willy Tarreau | bbba2a8 | 2014-04-28 13:57:26 +0200 | [diff] [blame] | 4373 | |
Willy Tarreau | 0b74833 | 2014-04-29 00:13:29 +0200 | [diff] [blame] | 4374 | if (verdict == HTTP_RULE_RES_DENY) /* stats http-request deny */ |
| 4375 | goto deny; |
Willy Tarreau | 5254259 | 2014-04-28 18:33:26 +0200 | [diff] [blame] | 4376 | |
Willy Tarreau | 0b74833 | 2014-04-29 00:13:29 +0200 | [diff] [blame] | 4377 | if (verdict == HTTP_RULE_RES_ABRT) /* stats auth / stats http-request auth */ |
| 4378 | goto return_prx_cond; |
Krzysztof Piotr Oledzki | 59bb218 | 2010-01-29 17:58:21 +0100 | [diff] [blame] | 4379 | } |
| 4380 | |
Willy Tarreau | bbba2a8 | 2014-04-28 13:57:26 +0200 | [diff] [blame] | 4381 | /* evaluate the req* rules except reqadd */ |
| 4382 | if (px->req_exp != NULL) { |
Willy Tarreau | 6c123b1 | 2010-01-28 20:22:06 +0100 | [diff] [blame] | 4383 | if (apply_filters_to_request(s, req, px) < 0) |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 4384 | goto return_bad_req; |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 4385 | |
Willy Tarreau | bbba2a8 | 2014-04-28 13:57:26 +0200 | [diff] [blame] | 4386 | if (txn->flags & TX_CLDENY) |
| 4387 | goto deny; |
Willy Tarreau | c465fd7 | 2009-08-31 00:17:18 +0200 | [diff] [blame] | 4388 | |
Jarno Huuskonen | 800d176 | 2017-03-06 14:56:36 +0200 | [diff] [blame] | 4389 | if (txn->flags & TX_CLTARPIT) { |
| 4390 | deny_status = HTTP_ERR_500; |
Willy Tarreau | bbba2a8 | 2014-04-28 13:57:26 +0200 | [diff] [blame] | 4391 | goto tarpit; |
Jarno Huuskonen | 800d176 | 2017-03-06 14:56:36 +0200 | [diff] [blame] | 4392 | } |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 4393 | } |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 4394 | |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 4395 | /* add request headers from the rule sets in the same order */ |
| 4396 | list_for_each_entry(wl, &px->req_add, list) { |
| 4397 | if (wl->cond) { |
Willy Tarreau | 192252e | 2015-04-04 01:47:55 +0200 | [diff] [blame] | 4398 | 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] | 4399 | ret = acl_pass(ret); |
| 4400 | if (((struct acl_cond *)wl->cond)->pol == ACL_COND_UNLESS) |
| 4401 | ret = !ret; |
| 4402 | if (!ret) |
| 4403 | continue; |
| 4404 | } |
| 4405 | |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 4406 | 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] | 4407 | goto return_bad_req; |
Willy Tarreau | 81499eb | 2012-12-27 12:19:02 +0100 | [diff] [blame] | 4408 | } |
| 4409 | |
Willy Tarreau | 5254259 | 2014-04-28 18:33:26 +0200 | [diff] [blame] | 4410 | |
| 4411 | /* Proceed with the stats now. */ |
Willy Tarreau | 414e9bb | 2013-11-23 00:30:38 +0100 | [diff] [blame] | 4412 | if (unlikely(objt_applet(s->target) == &http_stats_applet)) { |
Willy Tarreau | 1facd6d | 2012-12-22 22:03:39 +0100 | [diff] [blame] | 4413 | /* process the stats request now */ |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 4414 | if (sess->fe == s->be) /* report it if the request was intercepted by the frontend */ |
| 4415 | sess->fe->fe_counters.intercepted_req++; |
Willy Tarreau | 347a35d | 2013-11-22 17:51:09 +0100 | [diff] [blame] | 4416 | |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 4417 | if (!(s->flags & SF_ERR_MASK)) // this is not really an error but it is |
| 4418 | s->flags |= SF_ERR_LOCAL; // to mark that it comes from the proxy |
| 4419 | if (!(s->flags & SF_FINST_MASK)) |
| 4420 | s->flags |= SF_FINST_R; |
Willy Tarreau | 347a35d | 2013-11-22 17:51:09 +0100 | [diff] [blame] | 4421 | |
Willy Tarreau | 70730dd | 2014-04-24 18:06:27 +0200 | [diff] [blame] | 4422 | /* enable the minimally required analyzers to handle keep-alive and compression on the HTTP response */ |
Christopher Faulet | 0184ea7 | 2017-01-05 14:06:34 +0100 | [diff] [blame] | 4423 | req->analysers &= (AN_REQ_HTTP_BODY | AN_REQ_FLT_HTTP_HDRS | AN_REQ_FLT_END); |
| 4424 | req->analysers &= ~AN_REQ_FLT_XFER_DATA; |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 4425 | req->analysers |= AN_REQ_HTTP_XFER_BODY; |
Willy Tarreau | bbba2a8 | 2014-04-28 13:57:26 +0200 | [diff] [blame] | 4426 | goto done; |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 4427 | } |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 4428 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 4429 | /* check whether we have some ACLs set to redirect this request */ |
| 4430 | list_for_each_entry(rule, &px->redirect_rules, list) { |
Willy Tarreau | f285f54 | 2010-01-03 20:03:03 +0100 | [diff] [blame] | 4431 | if (rule->cond) { |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 4432 | int ret; |
| 4433 | |
Willy Tarreau | 192252e | 2015-04-04 01:47:55 +0200 | [diff] [blame] | 4434 | 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] | 4435 | ret = acl_pass(ret); |
| 4436 | if (rule->cond->pol == ACL_COND_UNLESS) |
| 4437 | ret = !ret; |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 4438 | if (!ret) |
| 4439 | continue; |
Willy Tarreau | f285f54 | 2010-01-03 20:03:03 +0100 | [diff] [blame] | 4440 | } |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 4441 | if (!http_apply_redirect_rule(rule, s, txn)) |
| 4442 | goto return_bad_req; |
Willy Tarreau | bbba2a8 | 2014-04-28 13:57:26 +0200 | [diff] [blame] | 4443 | goto done; |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 4444 | } |
Willy Tarreau | 55ea757 | 2007-06-17 19:56:27 +0200 | [diff] [blame] | 4445 | |
Willy Tarreau | 2be3939 | 2010-01-03 17:24:51 +0100 | [diff] [blame] | 4446 | /* POST requests may be accompanied with an "Expect: 100-Continue" header. |
| 4447 | * If this happens, then the data will not come immediately, so we must |
| 4448 | * send all what we have without waiting. Note that due to the small gain |
| 4449 | * 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] | 4450 | * 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] | 4451 | * itself once used. |
| 4452 | */ |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 4453 | req->flags |= CF_SEND_DONTWAIT; |
Willy Tarreau | 2be3939 | 2010-01-03 17:24:51 +0100 | [diff] [blame] | 4454 | |
Willy Tarreau | bbba2a8 | 2014-04-28 13:57:26 +0200 | [diff] [blame] | 4455 | done: /* done with this analyser, continue with next ones that the calling |
| 4456 | * points will have set, if any. |
| 4457 | */ |
Willy Tarreau | bbba2a8 | 2014-04-28 13:57:26 +0200 | [diff] [blame] | 4458 | req->analyse_exp = TICK_ETERNITY; |
Thierry FOURNIER | 7566e30 | 2014-08-22 06:55:26 +0200 | [diff] [blame] | 4459 | done_without_exp: /* done with this analyser, but dont reset the analyse_exp. */ |
| 4460 | req->analysers &= ~an_bit; |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 4461 | return 1; |
Willy Tarreau | 1138281 | 2008-07-09 16:18:21 +0200 | [diff] [blame] | 4462 | |
Willy Tarreau | bbba2a8 | 2014-04-28 13:57:26 +0200 | [diff] [blame] | 4463 | tarpit: |
Willy Tarreau | 6a0bca9 | 2017-06-11 17:56:27 +0200 | [diff] [blame] | 4464 | /* Allow cookie logging |
| 4465 | */ |
| 4466 | if (s->be->cookie_name || sess->fe->capture_name) |
| 4467 | manage_client_side_cookies(s, req); |
| 4468 | |
Willy Tarreau | bbba2a8 | 2014-04-28 13:57:26 +0200 | [diff] [blame] | 4469 | /* When a connection is tarpitted, we use the tarpit timeout, |
| 4470 | * which may be the same as the connect timeout if unspecified. |
| 4471 | * If unset, then set it to zero because we really want it to |
| 4472 | * eventually expire. We build the tarpit as an analyser. |
| 4473 | */ |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 4474 | channel_erase(&s->req); |
Willy Tarreau | bbba2a8 | 2014-04-28 13:57:26 +0200 | [diff] [blame] | 4475 | |
| 4476 | /* wipe the request out so that we can drop the connection early |
| 4477 | * if the client closes first. |
| 4478 | */ |
| 4479 | channel_dont_connect(req); |
Bertrand Paquet | 83a2c3d | 2016-04-06 11:58:31 +0200 | [diff] [blame] | 4480 | |
Jarno Huuskonen | 800d176 | 2017-03-06 14:56:36 +0200 | [diff] [blame] | 4481 | txn->status = http_err_codes[deny_status]; |
| 4482 | |
Christopher Faulet | 0184ea7 | 2017-01-05 14:06:34 +0100 | [diff] [blame] | 4483 | req->analysers &= AN_REQ_FLT_END; /* remove switching rules etc... */ |
Willy Tarreau | bbba2a8 | 2014-04-28 13:57:26 +0200 | [diff] [blame] | 4484 | req->analysers |= AN_REQ_HTTP_TARPIT; |
| 4485 | req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.tarpit); |
| 4486 | if (!req->analyse_exp) |
| 4487 | req->analyse_exp = tick_add(now_ms, 0); |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 4488 | stream_inc_http_err_ctr(s); |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 4489 | sess->fe->fe_counters.denied_req++; |
| 4490 | if (sess->fe != s->be) |
Willy Tarreau | bbba2a8 | 2014-04-28 13:57:26 +0200 | [diff] [blame] | 4491 | s->be->be_counters.denied_req++; |
Willy Tarreau | fb0afa7 | 2015-04-03 14:46:27 +0200 | [diff] [blame] | 4492 | if (sess->listener->counters) |
| 4493 | sess->listener->counters->denied_req++; |
Thierry FOURNIER | 7566e30 | 2014-08-22 06:55:26 +0200 | [diff] [blame] | 4494 | goto done_without_exp; |
Willy Tarreau | bbba2a8 | 2014-04-28 13:57:26 +0200 | [diff] [blame] | 4495 | |
| 4496 | deny: /* this request was blocked (denied) */ |
Bertrand Paquet | 83a2c3d | 2016-04-06 11:58:31 +0200 | [diff] [blame] | 4497 | |
| 4498 | /* Allow cookie logging |
| 4499 | */ |
| 4500 | if (s->be->cookie_name || sess->fe->capture_name) |
| 4501 | manage_client_side_cookies(s, req); |
| 4502 | |
Willy Tarreau | 0b74833 | 2014-04-29 00:13:29 +0200 | [diff] [blame] | 4503 | txn->flags |= TX_CLDENY; |
Willy Tarreau | 58727ec | 2016-05-25 16:23:59 +0200 | [diff] [blame] | 4504 | txn->status = http_err_codes[deny_status]; |
Willy Tarreau | bbba2a8 | 2014-04-28 13:57:26 +0200 | [diff] [blame] | 4505 | s->logs.tv_request = now; |
Jarno Huuskonen | 9e6906b | 2017-03-06 14:21:49 +0200 | [diff] [blame] | 4506 | http_reply_and_close(s, txn->status, http_error_message(s)); |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 4507 | stream_inc_http_err_ctr(s); |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 4508 | sess->fe->fe_counters.denied_req++; |
| 4509 | if (sess->fe != s->be) |
Willy Tarreau | bbba2a8 | 2014-04-28 13:57:26 +0200 | [diff] [blame] | 4510 | s->be->be_counters.denied_req++; |
Willy Tarreau | fb0afa7 | 2015-04-03 14:46:27 +0200 | [diff] [blame] | 4511 | if (sess->listener->counters) |
| 4512 | sess->listener->counters->denied_req++; |
Willy Tarreau | bbba2a8 | 2014-04-28 13:57:26 +0200 | [diff] [blame] | 4513 | goto return_prx_cond; |
| 4514 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 4515 | return_bad_req: |
| 4516 | /* We centralize bad requests processing here */ |
| 4517 | if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) { |
| 4518 | /* we detected a parsing error. We want to archive this request |
| 4519 | * in the dedicated proxy area for later troubleshooting. |
| 4520 | */ |
Willy Tarreau | 10e61cb | 2017-01-04 14:51:22 +0100 | [diff] [blame] | 4521 | http_capture_bad_message(&sess->fe->invalid_req, s, msg, msg->err_state, sess->fe); |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 4522 | } |
Willy Tarreau | 55ea757 | 2007-06-17 19:56:27 +0200 | [diff] [blame] | 4523 | |
Willy Tarreau | 10e61cb | 2017-01-04 14:51:22 +0100 | [diff] [blame] | 4524 | txn->req.err_state = txn->req.msg_state; |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 4525 | txn->req.msg_state = HTTP_MSG_ERROR; |
| 4526 | txn->status = 400; |
Jarno Huuskonen | 9e6906b | 2017-03-06 14:21:49 +0200 | [diff] [blame] | 4527 | http_reply_and_close(s, txn->status, http_error_message(s)); |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 4528 | |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 4529 | sess->fe->fe_counters.failed_req++; |
Willy Tarreau | fb0afa7 | 2015-04-03 14:46:27 +0200 | [diff] [blame] | 4530 | if (sess->listener->counters) |
| 4531 | sess->listener->counters->failed_req++; |
Willy Tarreau | 6e4261e | 2007-09-18 18:36:05 +0200 | [diff] [blame] | 4532 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 4533 | return_prx_cond: |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 4534 | if (!(s->flags & SF_ERR_MASK)) |
| 4535 | s->flags |= SF_ERR_PRXCOND; |
| 4536 | if (!(s->flags & SF_FINST_MASK)) |
| 4537 | s->flags |= SF_FINST_R; |
Willy Tarreau | f1221aa | 2006-12-17 22:14:12 +0100 | [diff] [blame] | 4538 | |
Christopher Faulet | 0184ea7 | 2017-01-05 14:06:34 +0100 | [diff] [blame] | 4539 | req->analysers &= AN_REQ_FLT_END; |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 4540 | req->analyse_exp = TICK_ETERNITY; |
| 4541 | return 0; |
Thierry FOURNIER | bc4c1ac | 2015-02-25 13:36:14 +0100 | [diff] [blame] | 4542 | |
| 4543 | return_prx_yield: |
| 4544 | channel_dont_connect(req); |
| 4545 | return 0; |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 4546 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4547 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 4548 | /* This function performs all the processing enabled for the current request. |
| 4549 | * It returns 1 if the processing can continue on next analysers, or zero if it |
| 4550 | * needs more data, encounters an error, or wants to immediately abort the |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 4551 | * request. It relies on buffers flags, and updates s->req.analysers. |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 4552 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 4553 | 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] | 4554 | { |
Willy Tarreau | fb0afa7 | 2015-04-03 14:46:27 +0200 | [diff] [blame] | 4555 | struct session *sess = s->sess; |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 4556 | struct http_txn *txn = s->txn; |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 4557 | struct http_msg *msg = &txn->req; |
Willy Tarreau | ee335e6 | 2015-04-21 18:15:13 +0200 | [diff] [blame] | 4558 | struct connection *cli_conn = objt_conn(strm_sess(s)->origin); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4559 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 4560 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) { |
Willy Tarreau | 51aecc7 | 2009-07-12 09:47:04 +0200 | [diff] [blame] | 4561 | /* we need more data */ |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 4562 | channel_dont_connect(req); |
Willy Tarreau | 51aecc7 | 2009-07-12 09:47:04 +0200 | [diff] [blame] | 4563 | return 0; |
| 4564 | } |
| 4565 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 4566 | 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] | 4567 | now_ms, __FUNCTION__, |
| 4568 | s, |
| 4569 | req, |
| 4570 | req->rex, req->wex, |
| 4571 | req->flags, |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 4572 | req->buf->i, |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 4573 | req->analysers); |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 4574 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4575 | /* |
| 4576 | * Right now, we know that we have processed the entire headers |
| 4577 | * and that unwanted requests have been filtered out. We can do |
| 4578 | * whatever we want with the remaining request. Also, now we |
| 4579 | * may have separate values for ->fe, ->be. |
| 4580 | */ |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 4581 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4582 | /* |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 4583 | * If HTTP PROXY is set we simply get remote server address parsing |
| 4584 | * incoming request. Note that this requires that a connection is |
| 4585 | * allocated on the server side. |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4586 | */ |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 4587 | 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] | 4588 | struct connection *conn; |
Willy Tarreau | e8df1e1 | 2013-12-16 14:30:55 +0100 | [diff] [blame] | 4589 | char *path; |
Willy Tarreau | 32e3c6a | 2013-10-11 19:34:20 +0200 | [diff] [blame] | 4590 | |
Willy Tarreau | 9471b8c | 2013-12-15 13:31:35 +0100 | [diff] [blame] | 4591 | /* Note that for now we don't reuse existing proxy connections */ |
Willy Tarreau | 973a542 | 2015-08-05 21:47:23 +0200 | [diff] [blame] | 4592 | if (unlikely((conn = si_alloc_conn(&s->si[1])) == NULL)) { |
Willy Tarreau | 10e61cb | 2017-01-04 14:51:22 +0100 | [diff] [blame] | 4593 | txn->req.err_state = txn->req.msg_state; |
Willy Tarreau | 32e3c6a | 2013-10-11 19:34:20 +0200 | [diff] [blame] | 4594 | txn->req.msg_state = HTTP_MSG_ERROR; |
| 4595 | txn->status = 500; |
Christopher Faulet | 0184ea7 | 2017-01-05 14:06:34 +0100 | [diff] [blame] | 4596 | req->analysers &= AN_REQ_FLT_END; |
Jarno Huuskonen | 9e6906b | 2017-03-06 14:21:49 +0200 | [diff] [blame] | 4597 | http_reply_and_close(s, txn->status, http_error_message(s)); |
Willy Tarreau | 32e3c6a | 2013-10-11 19:34:20 +0200 | [diff] [blame] | 4598 | |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 4599 | if (!(s->flags & SF_ERR_MASK)) |
| 4600 | s->flags |= SF_ERR_RESOURCE; |
| 4601 | if (!(s->flags & SF_FINST_MASK)) |
| 4602 | s->flags |= SF_FINST_R; |
Willy Tarreau | 32e3c6a | 2013-10-11 19:34:20 +0200 | [diff] [blame] | 4603 | |
| 4604 | return 0; |
| 4605 | } |
Willy Tarreau | e8df1e1 | 2013-12-16 14:30:55 +0100 | [diff] [blame] | 4606 | |
| 4607 | path = http_get_path(txn); |
| 4608 | url2sa(req->buf->p + msg->sl.rq.u, |
| 4609 | 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] | 4610 | &conn->addr.to, NULL); |
Willy Tarreau | e8df1e1 | 2013-12-16 14:30:55 +0100 | [diff] [blame] | 4611 | /* if the path was found, we have to remove everything between |
| 4612 | * req->buf->p + msg->sl.rq.u and path (excluded). If it was not |
| 4613 | * found, we need to replace from req->buf->p + msg->sl.rq.u for |
| 4614 | * u_l characters by a single "/". |
| 4615 | */ |
| 4616 | if (path) { |
| 4617 | char *cur_ptr = req->buf->p; |
| 4618 | char *cur_end = cur_ptr + txn->req.sl.rq.l; |
| 4619 | int delta; |
| 4620 | |
| 4621 | delta = buffer_replace2(req->buf, req->buf->p + msg->sl.rq.u, path, NULL, 0); |
| 4622 | http_msg_move_end(&txn->req, delta); |
| 4623 | cur_end += delta; |
| 4624 | if (http_parse_reqline(&txn->req, HTTP_MSG_RQMETH, cur_ptr, cur_end + 1, NULL, NULL) == NULL) |
| 4625 | goto return_bad_req; |
| 4626 | } |
| 4627 | else { |
| 4628 | char *cur_ptr = req->buf->p; |
| 4629 | char *cur_end = cur_ptr + txn->req.sl.rq.l; |
| 4630 | int delta; |
| 4631 | |
| 4632 | delta = buffer_replace2(req->buf, req->buf->p + msg->sl.rq.u, |
| 4633 | req->buf->p + msg->sl.rq.u + msg->sl.rq.u_l, "/", 1); |
| 4634 | http_msg_move_end(&txn->req, delta); |
| 4635 | cur_end += delta; |
| 4636 | if (http_parse_reqline(&txn->req, HTTP_MSG_RQMETH, cur_ptr, cur_end + 1, NULL, NULL) == NULL) |
| 4637 | goto return_bad_req; |
| 4638 | } |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4639 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4640 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4641 | /* |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 4642 | * 7: Now we can work with the cookies. |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4643 | * Note that doing so might move headers in the request, but |
| 4644 | * the fields will stay coherent and the URI will not move. |
| 4645 | * This should only be performed in the backend. |
| 4646 | */ |
Bertrand Paquet | 83a2c3d | 2016-04-06 11:58:31 +0200 | [diff] [blame] | 4647 | if (s->be->cookie_name || sess->fe->capture_name) |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4648 | manage_client_side_cookies(s, req); |
Willy Tarreau | 7ac51f6 | 2007-03-25 16:00:04 +0200 | [diff] [blame] | 4649 | |
William Lallemand | a73203e | 2012-03-12 12:48:57 +0100 | [diff] [blame] | 4650 | /* add unique-id if "header-unique-id" is specified */ |
| 4651 | |
Thierry Fournier | f4011dd | 2016-03-29 17:23:51 +0200 | [diff] [blame] | 4652 | if (!LIST_ISEMPTY(&sess->fe->format_unique_id) && !s->unique_id) { |
William Lallemand | 5b7ea3a | 2013-08-28 15:44:19 +0200 | [diff] [blame] | 4653 | if ((s->unique_id = pool_alloc2(pool2_uniqueid)) == NULL) |
| 4654 | goto return_bad_req; |
| 4655 | s->unique_id[0] = '\0'; |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 4656 | 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] | 4657 | } |
William Lallemand | a73203e | 2012-03-12 12:48:57 +0100 | [diff] [blame] | 4658 | |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 4659 | if (sess->fe->header_unique_id && s->unique_id) { |
| 4660 | 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] | 4661 | if (trash.len < 0) |
William Lallemand | a73203e | 2012-03-12 12:48:57 +0100 | [diff] [blame] | 4662 | goto return_bad_req; |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 4663 | 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] | 4664 | goto return_bad_req; |
| 4665 | } |
| 4666 | |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 4667 | /* |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4668 | * 9: add X-Forwarded-For if either the frontend or the backend |
| 4669 | * asks for it. |
| 4670 | */ |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 4671 | if ((sess->fe->options | s->be->options) & PR_O_FWDFOR) { |
Willy Tarreau | 87cf514 | 2011-08-19 22:57:24 +0200 | [diff] [blame] | 4672 | struct hdr_ctx ctx = { .idx = 0 }; |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 4673 | if (!((sess->fe->options | s->be->options) & PR_O_FF_ALWAYS) && |
| 4674 | http_find_header2(s->be->fwdfor_hdr_len ? s->be->fwdfor_hdr_name : sess->fe->fwdfor_hdr_name, |
| 4675 | 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] | 4676 | req->buf->p, &txn->hdr_idx, &ctx)) { |
Willy Tarreau | 87cf514 | 2011-08-19 22:57:24 +0200 | [diff] [blame] | 4677 | /* The header is set to be added only if none is present |
| 4678 | * and we found it, so don't do anything. |
| 4679 | */ |
| 4680 | } |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 4681 | else if (cli_conn && cli_conn->addr.from.ss_family == AF_INET) { |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4682 | /* Add an X-Forwarded-For header unless the source IP is |
| 4683 | * in the 'except' network range. |
| 4684 | */ |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 4685 | if ((!sess->fe->except_mask.s_addr || |
| 4686 | (((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr.s_addr & sess->fe->except_mask.s_addr) |
| 4687 | != sess->fe->except_net.s_addr) && |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4688 | (!s->be->except_mask.s_addr || |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 4689 | (((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] | 4690 | != s->be->except_net.s_addr)) { |
Willy Tarreau | 2a32428 | 2006-12-05 00:05:46 +0100 | [diff] [blame] | 4691 | int len; |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4692 | unsigned char *pn; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 4693 | pn = (unsigned char *)&((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr; |
Ross West | af72a1d | 2008-08-03 10:51:45 +0200 | [diff] [blame] | 4694 | |
| 4695 | /* Note: we rely on the backend to get the header name to be used for |
| 4696 | * x-forwarded-for, because the header is really meant for the backends. |
| 4697 | * However, if the backend did not specify any option, we have to rely |
| 4698 | * on the frontend's header name. |
| 4699 | */ |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4700 | if (s->be->fwdfor_hdr_len) { |
| 4701 | len = s->be->fwdfor_hdr_len; |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 4702 | memcpy(trash.str, s->be->fwdfor_hdr_name, len); |
Ross West | af72a1d | 2008-08-03 10:51:45 +0200 | [diff] [blame] | 4703 | } else { |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 4704 | len = sess->fe->fwdfor_hdr_len; |
| 4705 | memcpy(trash.str, sess->fe->fwdfor_hdr_name, len); |
Willy Tarreau | b86db34 | 2009-11-30 11:50:16 +0100 | [diff] [blame] | 4706 | } |
Willy Tarreau | e9187f8 | 2014-04-14 15:27:14 +0200 | [diff] [blame] | 4707 | 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] | 4708 | |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 4709 | 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] | 4710 | goto return_bad_req; |
Willy Tarreau | 2a32428 | 2006-12-05 00:05:46 +0100 | [diff] [blame] | 4711 | } |
| 4712 | } |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 4713 | else if (cli_conn && cli_conn->addr.from.ss_family == AF_INET6) { |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4714 | /* FIXME: for the sake of completeness, we should also support |
| 4715 | * 'except' here, although it is mostly useless in this case. |
matt.farnsworth@nokia.com | 1c2ab96 | 2008-04-14 20:47:37 +0200 | [diff] [blame] | 4716 | */ |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4717 | int len; |
| 4718 | char pn[INET6_ADDRSTRLEN]; |
| 4719 | inet_ntop(AF_INET6, |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 4720 | (const void *)&((struct sockaddr_in6 *)(&cli_conn->addr.from))->sin6_addr, |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4721 | pn, sizeof(pn)); |
matt.farnsworth@nokia.com | 1c2ab96 | 2008-04-14 20:47:37 +0200 | [diff] [blame] | 4722 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4723 | /* Note: we rely on the backend to get the header name to be used for |
| 4724 | * x-forwarded-for, 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. |
matt.farnsworth@nokia.com | 1c2ab96 | 2008-04-14 20:47:37 +0200 | [diff] [blame] | 4727 | */ |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4728 | if (s->be->fwdfor_hdr_len) { |
| 4729 | len = s->be->fwdfor_hdr_len; |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 4730 | memcpy(trash.str, s->be->fwdfor_hdr_name, len); |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4731 | } else { |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 4732 | len = sess->fe->fwdfor_hdr_len; |
| 4733 | memcpy(trash.str, sess->fe->fwdfor_hdr_name, len); |
matt.farnsworth@nokia.com | 1c2ab96 | 2008-04-14 20:47:37 +0200 | [diff] [blame] | 4734 | } |
Willy Tarreau | e9187f8 | 2014-04-14 15:27:14 +0200 | [diff] [blame] | 4735 | len += snprintf(trash.str + len, trash.size - len, ": %s", pn); |
Willy Tarreau | adfb856 | 2008-08-11 15:24:42 +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)) |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4738 | goto return_bad_req; |
| 4739 | } |
| 4740 | } |
| 4741 | |
| 4742 | /* |
Maik Broemme | 2850cb4 | 2009-04-17 18:53:21 +0200 | [diff] [blame] | 4743 | * 10: add X-Original-To if either the frontend or the backend |
| 4744 | * asks for it. |
| 4745 | */ |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 4746 | if ((sess->fe->options | s->be->options) & PR_O_ORGTO) { |
Maik Broemme | 2850cb4 | 2009-04-17 18:53:21 +0200 | [diff] [blame] | 4747 | |
| 4748 | /* FIXME: don't know if IPv6 can handle that case too. */ |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 4749 | if (cli_conn && cli_conn->addr.from.ss_family == AF_INET) { |
Maik Broemme | 2850cb4 | 2009-04-17 18:53:21 +0200 | [diff] [blame] | 4750 | /* Add an X-Original-To header unless the destination IP is |
| 4751 | * in the 'except' network range. |
| 4752 | */ |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 4753 | conn_get_to_addr(cli_conn); |
Maik Broemme | 2850cb4 | 2009-04-17 18:53:21 +0200 | [diff] [blame] | 4754 | |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 4755 | if (cli_conn->addr.to.ss_family == AF_INET && |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 4756 | ((!sess->fe->except_mask_to.s_addr || |
| 4757 | (((struct sockaddr_in *)&cli_conn->addr.to)->sin_addr.s_addr & sess->fe->except_mask_to.s_addr) |
| 4758 | != sess->fe->except_to.s_addr) && |
Emeric Brun | 5bd86a8 | 2010-10-22 17:23:04 +0200 | [diff] [blame] | 4759 | (!s->be->except_mask_to.s_addr || |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 4760 | (((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] | 4761 | != s->be->except_to.s_addr))) { |
Maik Broemme | 2850cb4 | 2009-04-17 18:53:21 +0200 | [diff] [blame] | 4762 | int len; |
| 4763 | unsigned char *pn; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 4764 | pn = (unsigned char *)&((struct sockaddr_in *)&cli_conn->addr.to)->sin_addr; |
Maik Broemme | 2850cb4 | 2009-04-17 18:53:21 +0200 | [diff] [blame] | 4765 | |
| 4766 | /* Note: we rely on the backend to get the header name to be used for |
| 4767 | * x-original-to, because the header is really meant for the backends. |
| 4768 | * However, if the backend did not specify any option, we have to rely |
| 4769 | * on the frontend's header name. |
| 4770 | */ |
| 4771 | if (s->be->orgto_hdr_len) { |
| 4772 | len = s->be->orgto_hdr_len; |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 4773 | memcpy(trash.str, s->be->orgto_hdr_name, len); |
Maik Broemme | 2850cb4 | 2009-04-17 18:53:21 +0200 | [diff] [blame] | 4774 | } else { |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 4775 | len = sess->fe->orgto_hdr_len; |
| 4776 | memcpy(trash.str, sess->fe->orgto_hdr_name, len); |
Willy Tarreau | b86db34 | 2009-11-30 11:50:16 +0100 | [diff] [blame] | 4777 | } |
Willy Tarreau | e9187f8 | 2014-04-14 15:27:14 +0200 | [diff] [blame] | 4778 | 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] | 4779 | |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 4780 | 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] | 4781 | goto return_bad_req; |
| 4782 | } |
| 4783 | } |
| 4784 | } |
| 4785 | |
Willy Tarreau | 50fc777 | 2012-11-11 22:19:57 +0100 | [diff] [blame] | 4786 | /* 11: add "Connection: close" or "Connection: keep-alive" if needed and not yet set. |
| 4787 | * If an "Upgrade" token is found, the header is left untouched in order not to have |
| 4788 | * to deal with some servers bugs : some of them fail an Upgrade if anything but |
| 4789 | * "Upgrade" is present in the Connection header. |
| 4790 | */ |
| 4791 | if (!(txn->flags & TX_HDR_CONN_UPG) && |
| 4792 | (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) || |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 4793 | ((sess->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL || |
Willy Tarreau | 02bce8b | 2014-01-30 00:15:28 +0100 | [diff] [blame] | 4794 | (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL))) { |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 4795 | unsigned int want_flags = 0; |
| 4796 | |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 4797 | if (msg->flags & HTTP_MSGF_VER_11) { |
Willy Tarreau | 22a9534 | 2010-09-29 14:31:41 +0200 | [diff] [blame] | 4798 | if (((txn->flags & TX_CON_WANT_MSK) >= TX_CON_WANT_SCL || |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 4799 | ((sess->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL || |
Willy Tarreau | 02bce8b | 2014-01-30 00:15:28 +0100 | [diff] [blame] | 4800 | (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL)) && |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 4801 | !((sess->fe->options2|s->be->options2) & PR_O2_FAKE_KA)) |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 4802 | want_flags |= TX_CON_CLO_SET; |
| 4803 | } else { |
Willy Tarreau | 22a9534 | 2010-09-29 14:31:41 +0200 | [diff] [blame] | 4804 | if (((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL && |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 4805 | ((sess->fe->options & PR_O_HTTP_MODE) != PR_O_HTTP_PCL && |
Willy Tarreau | 02bce8b | 2014-01-30 00:15:28 +0100 | [diff] [blame] | 4806 | (s->be->options & PR_O_HTTP_MODE) != PR_O_HTTP_PCL)) || |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 4807 | ((sess->fe->options2|s->be->options2) & PR_O2_FAKE_KA)) |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 4808 | want_flags |= TX_CON_KAL_SET; |
| 4809 | } |
| 4810 | |
| 4811 | 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] | 4812 | http_change_connection_header(txn, msg, want_flags); |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4813 | } |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 4814 | |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 4815 | |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 4816 | /* If we have no server assigned yet and we're balancing on url_param |
| 4817 | * with a POST request, we may be interested in checking the body for |
| 4818 | * that parameter. This will be done in another analyser. |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4819 | */ |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 4820 | if (!(s->flags & (SF_ASSIGNED|SF_DIRECT)) && |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 4821 | s->txn->meth == HTTP_METH_POST && s->be->url_param_name != NULL && |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 4822 | (msg->flags & (HTTP_MSGF_CNT_LEN|HTTP_MSGF_TE_CHNK))) { |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 4823 | channel_dont_connect(req); |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 4824 | req->analysers |= AN_REQ_HTTP_BODY; |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4825 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 4826 | |
Christopher Faulet | be821b9 | 2017-03-30 11:21:53 +0200 | [diff] [blame] | 4827 | req->analysers &= ~AN_REQ_FLT_XFER_DATA; |
| 4828 | req->analysers |= AN_REQ_HTTP_XFER_BODY; |
Willy Tarreau | 5e20552 | 2011-12-17 16:34:27 +0100 | [diff] [blame] | 4829 | #ifdef TCP_QUICKACK |
Christopher Faulet | be821b9 | 2017-03-30 11:21:53 +0200 | [diff] [blame] | 4830 | /* We expect some data from the client. Unless we know for sure |
| 4831 | * we already have a full request, we have to re-enable quick-ack |
| 4832 | * in case we previously disabled it, otherwise we might cause |
| 4833 | * the client to delay further data. |
| 4834 | */ |
| 4835 | if ((sess->listener->options & LI_O_NOQUICKACK) && |
| 4836 | cli_conn && conn_ctrl_ready(cli_conn) && |
| 4837 | ((msg->flags & HTTP_MSGF_TE_CHNK) || |
| 4838 | (msg->body_len > req->buf->i - txn->req.eoh - 2))) |
| 4839 | 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] | 4840 | #endif |
Willy Tarreau | 0394594 | 2009-12-22 16:50:27 +0100 | [diff] [blame] | 4841 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4842 | /************************************************************* |
| 4843 | * OK, that's finished for the headers. We have done what we * |
| 4844 | * could. Let's switch to the DATA state. * |
| 4845 | ************************************************************/ |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 4846 | req->analyse_exp = TICK_ETERNITY; |
| 4847 | req->analysers &= ~an_bit; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 4848 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4849 | s->logs.tv_request = now; |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4850 | /* OK let's go on with the BODY now */ |
| 4851 | return 1; |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 4852 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4853 | return_bad_req: /* let's centralize all bad requests */ |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 4854 | if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) { |
Willy Tarreau | f073a83 | 2009-03-01 23:21:47 +0100 | [diff] [blame] | 4855 | /* we detected a parsing error. We want to archive this request |
| 4856 | * in the dedicated proxy area for later troubleshooting. |
| 4857 | */ |
Willy Tarreau | 10e61cb | 2017-01-04 14:51:22 +0100 | [diff] [blame] | 4858 | http_capture_bad_message(&sess->fe->invalid_req, s, msg, msg->err_state, sess->fe); |
Willy Tarreau | f073a83 | 2009-03-01 23:21:47 +0100 | [diff] [blame] | 4859 | } |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 4860 | |
Willy Tarreau | 10e61cb | 2017-01-04 14:51:22 +0100 | [diff] [blame] | 4861 | txn->req.err_state = txn->req.msg_state; |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4862 | txn->req.msg_state = HTTP_MSG_ERROR; |
| 4863 | txn->status = 400; |
Christopher Faulet | 0184ea7 | 2017-01-05 14:06:34 +0100 | [diff] [blame] | 4864 | req->analysers &= AN_REQ_FLT_END; |
Jarno Huuskonen | 9e6906b | 2017-03-06 14:21:49 +0200 | [diff] [blame] | 4865 | http_reply_and_close(s, txn->status, http_error_message(s)); |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 4866 | |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 4867 | sess->fe->fe_counters.failed_req++; |
Willy Tarreau | fb0afa7 | 2015-04-03 14:46:27 +0200 | [diff] [blame] | 4868 | if (sess->listener->counters) |
| 4869 | sess->listener->counters->failed_req++; |
Willy Tarreau | adfb856 | 2008-08-11 15:24:42 +0200 | [diff] [blame] | 4870 | |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 4871 | if (!(s->flags & SF_ERR_MASK)) |
| 4872 | s->flags |= SF_ERR_PRXCOND; |
| 4873 | if (!(s->flags & SF_FINST_MASK)) |
| 4874 | s->flags |= SF_FINST_R; |
Willy Tarreau | dafde43 | 2008-08-17 01:00:46 +0200 | [diff] [blame] | 4875 | return 0; |
Willy Tarreau | c65a3ba | 2008-08-11 23:42:50 +0200 | [diff] [blame] | 4876 | } |
Willy Tarreau | adfb856 | 2008-08-11 15:24:42 +0200 | [diff] [blame] | 4877 | |
Willy Tarreau | 60b85b0 | 2008-11-30 23:28:40 +0100 | [diff] [blame] | 4878 | /* This function is an analyser which processes the HTTP tarpit. It always |
| 4879 | * returns zero, at the beginning because it prevents any other processing |
| 4880 | * from occurring, and at the end because it terminates the request. |
| 4881 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 4882 | 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] | 4883 | { |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 4884 | struct http_txn *txn = s->txn; |
Willy Tarreau | 60b85b0 | 2008-11-30 23:28:40 +0100 | [diff] [blame] | 4885 | |
| 4886 | /* This connection is being tarpitted. The CLIENT side has |
| 4887 | * already set the connect expiration date to the right |
| 4888 | * timeout. We just have to check that the client is still |
| 4889 | * there and that the timeout has not expired. |
| 4890 | */ |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 4891 | channel_dont_connect(req); |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 4892 | if ((req->flags & (CF_SHUTR|CF_READ_ERROR)) == 0 && |
Willy Tarreau | 60b85b0 | 2008-11-30 23:28:40 +0100 | [diff] [blame] | 4893 | !tick_is_expired(req->analyse_exp, now_ms)) |
| 4894 | return 0; |
| 4895 | |
| 4896 | /* We will set the queue timer to the time spent, just for |
| 4897 | * logging purposes. We fake a 500 server error, so that the |
| 4898 | * attacker will not suspect his connection has been tarpitted. |
| 4899 | * It will not cause trouble to the logs because we can exclude |
| 4900 | * the tarpitted connections by filtering on the 'PT' status flags. |
| 4901 | */ |
Willy Tarreau | 60b85b0 | 2008-11-30 23:28:40 +0100 | [diff] [blame] | 4902 | s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now); |
| 4903 | |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 4904 | if (!(req->flags & CF_READ_ERROR)) |
Jarno Huuskonen | 9e6906b | 2017-03-06 14:21:49 +0200 | [diff] [blame] | 4905 | http_reply_and_close(s, txn->status, http_error_message(s)); |
Willy Tarreau | 60b85b0 | 2008-11-30 23:28:40 +0100 | [diff] [blame] | 4906 | |
Christopher Faulet | 0184ea7 | 2017-01-05 14:06:34 +0100 | [diff] [blame] | 4907 | req->analysers &= AN_REQ_FLT_END; |
Willy Tarreau | 60b85b0 | 2008-11-30 23:28:40 +0100 | [diff] [blame] | 4908 | req->analyse_exp = TICK_ETERNITY; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 4909 | |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 4910 | if (!(s->flags & SF_ERR_MASK)) |
| 4911 | s->flags |= SF_ERR_PRXCOND; |
| 4912 | if (!(s->flags & SF_FINST_MASK)) |
| 4913 | s->flags |= SF_FINST_T; |
Willy Tarreau | 60b85b0 | 2008-11-30 23:28:40 +0100 | [diff] [blame] | 4914 | return 0; |
| 4915 | } |
| 4916 | |
Willy Tarreau | 5a8f947 | 2014-04-10 11:16:06 +0200 | [diff] [blame] | 4917 | /* This function is an analyser which waits for the HTTP request body. It waits |
| 4918 | * for either the buffer to be full, or the full advertised contents to have |
| 4919 | * reached the buffer. It must only be called after the standard HTTP request |
| 4920 | * processing has occurred, because it expects the request to be parsed and will |
| 4921 | * look for the Expect header. It may send a 100-Continue interim response. It |
| 4922 | * takes in input any state starting from HTTP_MSG_BODY and leaves with one of |
| 4923 | * HTTP_MSG_CHK_SIZE, HTTP_MSG_DATA or HTTP_MSG_TRAILERS. It returns zero if it |
| 4924 | * 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] | 4925 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 4926 | 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] | 4927 | { |
Willy Tarreau | fb0afa7 | 2015-04-03 14:46:27 +0200 | [diff] [blame] | 4928 | struct session *sess = s->sess; |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 4929 | struct http_txn *txn = s->txn; |
| 4930 | struct http_msg *msg = &s->txn->req; |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 4931 | |
| 4932 | /* We have to parse the HTTP request body to find any required data. |
| 4933 | * "balance url_param check_post" should have been the only way to get |
| 4934 | * into this. We were brought here after HTTP header analysis, so all |
| 4935 | * related structures are ready. |
| 4936 | */ |
| 4937 | |
Willy Tarreau | 890988f | 2014-04-10 11:59:33 +0200 | [diff] [blame] | 4938 | if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) { |
| 4939 | /* This is the first call */ |
| 4940 | if (msg->msg_state < HTTP_MSG_BODY) |
| 4941 | goto missing_data; |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 4942 | |
Willy Tarreau | 890988f | 2014-04-10 11:59:33 +0200 | [diff] [blame] | 4943 | if (msg->msg_state < HTTP_MSG_100_SENT) { |
| 4944 | /* If we have HTTP/1.1 and Expect: 100-continue, then we must |
| 4945 | * send an HTTP/1.1 100 Continue intermediate response. |
| 4946 | */ |
| 4947 | if (msg->flags & HTTP_MSGF_VER_11) { |
| 4948 | struct hdr_ctx ctx; |
| 4949 | ctx.idx = 0; |
| 4950 | /* Expect is allowed in 1.1, look for it */ |
| 4951 | if (http_find_header2("Expect", 6, req->buf->p, &txn->hdr_idx, &ctx) && |
| 4952 | 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] | 4953 | bo_inject(&s->res, http_100_chunk.str, http_100_chunk.len); |
Thierry FOURNIER / OZON.IO | 43ad11d | 2016-12-12 15:19:58 +0100 | [diff] [blame] | 4954 | http_remove_header2(&txn->req, &txn->hdr_idx, &ctx); |
Willy Tarreau | 890988f | 2014-04-10 11:59:33 +0200 | [diff] [blame] | 4955 | } |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 4956 | } |
Willy Tarreau | 890988f | 2014-04-10 11:59:33 +0200 | [diff] [blame] | 4957 | msg->msg_state = HTTP_MSG_100_SENT; |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 4958 | } |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 4959 | |
Willy Tarreau | fa4a03c | 2012-03-09 21:28:54 +0100 | [diff] [blame] | 4960 | /* 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] | 4961 | * req->buf->p still points to the beginning of the message. We |
| 4962 | * must save the body in msg->next because it survives buffer |
| 4963 | * re-alignments. |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4964 | */ |
Willy Tarreau | ea1175a | 2012-03-05 15:52:30 +0100 | [diff] [blame] | 4965 | msg->next = msg->sov; |
Willy Tarreau | a458b67 | 2012-03-05 11:17:50 +0100 | [diff] [blame] | 4966 | |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 4967 | if (msg->flags & HTTP_MSGF_TE_CHNK) |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 4968 | msg->msg_state = HTTP_MSG_CHUNK_SIZE; |
| 4969 | else |
| 4970 | msg->msg_state = HTTP_MSG_DATA; |
| 4971 | } |
| 4972 | |
Willy Tarreau | 890988f | 2014-04-10 11:59:33 +0200 | [diff] [blame] | 4973 | if (!(msg->flags & HTTP_MSGF_TE_CHNK)) { |
| 4974 | /* 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] | 4975 | if (http_body_bytes(msg) < msg->body_len) |
Willy Tarreau | 890988f | 2014-04-10 11:59:33 +0200 | [diff] [blame] | 4976 | goto missing_data; |
| 4977 | |
| 4978 | /* OK we have everything we need now */ |
| 4979 | goto http_end; |
| 4980 | } |
| 4981 | |
| 4982 | /* OK here we're parsing a chunked-encoded message */ |
| 4983 | |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 4984 | if (msg->msg_state == HTTP_MSG_CHUNK_SIZE) { |
Willy Tarreau | 124d991 | 2011-03-01 20:30:48 +0100 | [diff] [blame] | 4985 | /* read the chunk size and assign it to ->chunk_len, then |
Willy Tarreau | a458b67 | 2012-03-05 11:17:50 +0100 | [diff] [blame] | 4986 | * 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] | 4987 | * TRAILERS state. |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 4988 | */ |
Willy Tarreau | 4baf44b | 2012-03-09 14:10:20 +0100 | [diff] [blame] | 4989 | int ret = http_parse_chunk_size(msg); |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 4990 | |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 4991 | if (!ret) |
| 4992 | goto missing_data; |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 4993 | else if (ret < 0) { |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 4994 | stream_inc_http_err_ctr(s); |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 4995 | goto return_bad_req; |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 4996 | } |
Christopher Faulet | 113f7de | 2015-12-14 14:52:13 +0100 | [diff] [blame] | 4997 | msg->next += ret; |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 4998 | msg->msg_state = msg->chunk_len ? HTTP_MSG_DATA : HTTP_MSG_TRAILERS; |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 4999 | } |
| 5000 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5001 | /* Now we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state. |
Willy Tarreau | e115b49 | 2015-05-01 23:05:14 +0200 | [diff] [blame] | 5002 | * We have the first data byte is in msg->sov + msg->sol. We're waiting |
| 5003 | * for at least a whole chunk or the whole content length bytes after |
| 5004 | * msg->sov + msg->sol. |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 5005 | */ |
Willy Tarreau | 890988f | 2014-04-10 11:59:33 +0200 | [diff] [blame] | 5006 | if (msg->msg_state == HTTP_MSG_TRAILERS) |
| 5007 | goto http_end; |
| 5008 | |
Willy Tarreau | e115b49 | 2015-05-01 23:05:14 +0200 | [diff] [blame] | 5009 | 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] | 5010 | goto http_end; |
| 5011 | |
| 5012 | missing_data: |
Willy Tarreau | 31a1995 | 2014-04-10 11:50:37 +0200 | [diff] [blame] | 5013 | /* we get here if we need to wait for more data. If the buffer is full, |
| 5014 | * we have the maximum we can expect. |
| 5015 | */ |
| 5016 | if (buffer_full(req->buf, global.tune.maxrewrite)) |
| 5017 | goto http_end; |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 5018 | |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 5019 | 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] | 5020 | txn->status = 408; |
Jarno Huuskonen | 9e6906b | 2017-03-06 14:21:49 +0200 | [diff] [blame] | 5021 | http_reply_and_close(s, txn->status, http_error_message(s)); |
Willy Tarreau | 79ebac6 | 2010-06-07 13:47:49 +0200 | [diff] [blame] | 5022 | |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 5023 | if (!(s->flags & SF_ERR_MASK)) |
| 5024 | s->flags |= SF_ERR_CLITO; |
| 5025 | if (!(s->flags & SF_FINST_MASK)) |
| 5026 | s->flags |= SF_FINST_D; |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 5027 | goto return_err_msg; |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 5028 | } |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 5029 | |
| 5030 | /* we get here if we need to wait for more data */ |
Willy Tarreau | 31a1995 | 2014-04-10 11:50:37 +0200 | [diff] [blame] | 5031 | if (!(req->flags & (CF_SHUTR | CF_READ_ERROR))) { |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 5032 | /* Not enough data. We'll re-use the http-request |
| 5033 | * timeout here. Ideally, we should set the timeout |
| 5034 | * relative to the accept() date. We just set the |
| 5035 | * request timeout once at the beginning of the |
| 5036 | * request. |
| 5037 | */ |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 5038 | channel_dont_connect(req); |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 5039 | if (!tick_isset(req->analyse_exp)) |
Willy Tarreau | cd7afc0 | 2009-07-12 10:03:17 +0200 | [diff] [blame] | 5040 | req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.httpreq); |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 5041 | return 0; |
| 5042 | } |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 5043 | |
| 5044 | http_end: |
| 5045 | /* The situation will not evolve, so let's give up on the analysis. */ |
| 5046 | s->logs.tv_request = now; /* update the request timer to reflect full request */ |
| 5047 | req->analysers &= ~an_bit; |
| 5048 | req->analyse_exp = TICK_ETERNITY; |
| 5049 | return 1; |
| 5050 | |
| 5051 | return_bad_req: /* let's centralize all bad requests */ |
Willy Tarreau | 10e61cb | 2017-01-04 14:51:22 +0100 | [diff] [blame] | 5052 | txn->req.err_state = txn->req.msg_state; |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 5053 | txn->req.msg_state = HTTP_MSG_ERROR; |
| 5054 | txn->status = 400; |
Jarno Huuskonen | 9e6906b | 2017-03-06 14:21:49 +0200 | [diff] [blame] | 5055 | http_reply_and_close(s, txn->status, http_error_message(s)); |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 5056 | |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 5057 | if (!(s->flags & SF_ERR_MASK)) |
| 5058 | s->flags |= SF_ERR_PRXCOND; |
| 5059 | if (!(s->flags & SF_FINST_MASK)) |
| 5060 | s->flags |= SF_FINST_R; |
Willy Tarreau | 79ebac6 | 2010-06-07 13:47:49 +0200 | [diff] [blame] | 5061 | |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 5062 | return_err_msg: |
Christopher Faulet | 0184ea7 | 2017-01-05 14:06:34 +0100 | [diff] [blame] | 5063 | req->analysers &= AN_REQ_FLT_END; |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 5064 | sess->fe->fe_counters.failed_req++; |
Willy Tarreau | fb0afa7 | 2015-04-03 14:46:27 +0200 | [diff] [blame] | 5065 | if (sess->listener->counters) |
| 5066 | sess->listener->counters->failed_req++; |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 5067 | return 0; |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 5068 | } |
| 5069 | |
Willy Tarreau | d1de8af | 2012-05-18 22:12:14 +0200 | [diff] [blame] | 5070 | /* send a server's name with an outgoing request over an established connection. |
| 5071 | * Note: this function is designed to be called once the request has been scheduled |
| 5072 | * for being forwarded. This is the reason why it rewinds the buffer before |
| 5073 | * proceeding. |
| 5074 | */ |
Willy Tarreau | 45c0d98 | 2012-03-09 12:11:57 +0100 | [diff] [blame] | 5075 | 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] | 5076 | |
| 5077 | struct hdr_ctx ctx; |
| 5078 | |
Mark Lamourine | c2247f0 | 2012-01-04 13:02:01 -0500 | [diff] [blame] | 5079 | char *hdr_name = be->server_id_hdr_name; |
| 5080 | int hdr_name_len = be->server_id_hdr_len; |
Willy Tarreau | 394db37 | 2012-10-12 22:40:39 +0200 | [diff] [blame] | 5081 | struct channel *chn = txn->req.chn; |
Mark Lamourine | c2247f0 | 2012-01-04 13:02:01 -0500 | [diff] [blame] | 5082 | char *hdr_val; |
Willy Tarreau | d1de8af | 2012-05-18 22:12:14 +0200 | [diff] [blame] | 5083 | unsigned int old_o, old_i; |
Mark Lamourine | c2247f0 | 2012-01-04 13:02:01 -0500 | [diff] [blame] | 5084 | |
William Lallemand | d9e9066 | 2012-01-30 17:27:17 +0100 | [diff] [blame] | 5085 | ctx.idx = 0; |
| 5086 | |
Willy Tarreau | 211cdec | 2014-04-17 20:18:08 +0200 | [diff] [blame] | 5087 | old_o = http_hdr_rewind(&txn->req); |
Willy Tarreau | d1de8af | 2012-05-18 22:12:14 +0200 | [diff] [blame] | 5088 | if (old_o) { |
| 5089 | /* The request was already skipped, let's restore it */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 5090 | b_rew(chn->buf, old_o); |
Willy Tarreau | 877e78d | 2013-04-07 18:48:08 +0200 | [diff] [blame] | 5091 | txn->req.next += old_o; |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 5092 | txn->req.sov += old_o; |
Willy Tarreau | d1de8af | 2012-05-18 22:12:14 +0200 | [diff] [blame] | 5093 | } |
| 5094 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 5095 | old_i = chn->buf->i; |
| 5096 | 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] | 5097 | /* remove any existing values from the header */ |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 5098 | http_remove_header2(&txn->req, &txn->hdr_idx, &ctx); |
Mark Lamourine | c2247f0 | 2012-01-04 13:02:01 -0500 | [diff] [blame] | 5099 | } |
| 5100 | |
| 5101 | /* Add the new header requested with the server value */ |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 5102 | hdr_val = trash.str; |
Mark Lamourine | c2247f0 | 2012-01-04 13:02:01 -0500 | [diff] [blame] | 5103 | memcpy(hdr_val, hdr_name, hdr_name_len); |
| 5104 | hdr_val += hdr_name_len; |
| 5105 | *hdr_val++ = ':'; |
| 5106 | *hdr_val++ = ' '; |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 5107 | hdr_val += strlcpy2(hdr_val, srv_name, trash.str + trash.size - hdr_val); |
| 5108 | 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] | 5109 | |
Willy Tarreau | d1de8af | 2012-05-18 22:12:14 +0200 | [diff] [blame] | 5110 | if (old_o) { |
| 5111 | /* If this was a forwarded request, we must readjust the amount of |
| 5112 | * data to be forwarded in order to take into account the size |
Willy Tarreau | 877e78d | 2013-04-07 18:48:08 +0200 | [diff] [blame] | 5113 | * variations. Note that the current state is >= HTTP_MSG_BODY, |
| 5114 | * so we don't have to adjust ->sol. |
Willy Tarreau | d1de8af | 2012-05-18 22:12:14 +0200 | [diff] [blame] | 5115 | */ |
Willy Tarreau | 877e78d | 2013-04-07 18:48:08 +0200 | [diff] [blame] | 5116 | old_o += chn->buf->i - old_i; |
| 5117 | b_adv(chn->buf, old_o); |
| 5118 | txn->req.next -= old_o; |
| 5119 | txn->req.sov -= old_o; |
Willy Tarreau | d1de8af | 2012-05-18 22:12:14 +0200 | [diff] [blame] | 5120 | } |
| 5121 | |
Mark Lamourine | c2247f0 | 2012-01-04 13:02:01 -0500 | [diff] [blame] | 5122 | return 0; |
| 5123 | } |
| 5124 | |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5125 | /* Terminate current transaction and prepare a new one. This is very tricky |
| 5126 | * right now but it works. |
| 5127 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 5128 | void http_end_txn_clean_session(struct stream *s) |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5129 | { |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 5130 | int prev_status = s->txn->status; |
Willy Tarreau | d0d8da9 | 2015-04-04 02:10:38 +0200 | [diff] [blame] | 5131 | struct proxy *fe = strm_fe(s); |
Willy Tarreau | 858b103 | 2015-12-07 17:04:59 +0100 | [diff] [blame] | 5132 | struct proxy *be = s->be; |
Willy Tarreau | 323a2d9 | 2015-08-04 19:00:17 +0200 | [diff] [blame] | 5133 | struct connection *srv_conn; |
| 5134 | struct server *srv; |
Willy Tarreau | 449d74a | 2015-08-05 17:16:33 +0200 | [diff] [blame] | 5135 | unsigned int prev_flags = s->txn->flags; |
Willy Tarreau | 068621e | 2013-12-23 15:11:25 +0100 | [diff] [blame] | 5136 | |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5137 | /* FIXME: We need a more portable way of releasing a backend's and a |
| 5138 | * server's connections. We need a safer way to reinitialize buffer |
| 5139 | * flags. We also need a more accurate method for computing per-request |
| 5140 | * data. |
| 5141 | */ |
Willy Tarreau | 323a2d9 | 2015-08-04 19:00:17 +0200 | [diff] [blame] | 5142 | srv_conn = objt_conn(s->si[1].end); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5143 | |
Willy Tarreau | 4213a11 | 2013-12-15 10:25:42 +0100 | [diff] [blame] | 5144 | /* unless we're doing keep-alive, we want to quickly close the connection |
| 5145 | * to the server. |
| 5146 | */ |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 5147 | if (((s->txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) || |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 5148 | !si_conn_ready(&s->si[1])) { |
| 5149 | s->si[1].flags |= SI_FL_NOLINGER | SI_FL_NOHALF; |
| 5150 | si_shutr(&s->si[1]); |
| 5151 | si_shutw(&s->si[1]); |
Willy Tarreau | 4213a11 | 2013-12-15 10:25:42 +0100 | [diff] [blame] | 5152 | } |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5153 | |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 5154 | if (s->flags & SF_BE_ASSIGNED) { |
Willy Tarreau | 858b103 | 2015-12-07 17:04:59 +0100 | [diff] [blame] | 5155 | be->beconn--; |
Willy Tarreau | 2d5cd47 | 2012-03-01 23:34:37 +0100 | [diff] [blame] | 5156 | if (unlikely(s->srv_conn)) |
| 5157 | sess_change_server(s, NULL); |
| 5158 | } |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5159 | |
| 5160 | s->logs.t_close = tv_ms_elapsed(&s->logs.tv_accept, &now); |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 5161 | stream_process_counters(s); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5162 | |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 5163 | if (s->txn->status) { |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5164 | int n; |
| 5165 | |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 5166 | n = s->txn->status / 100; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5167 | if (n < 1 || n > 5) |
| 5168 | n = 0; |
| 5169 | |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 5170 | if (fe->mode == PR_MODE_HTTP) { |
| 5171 | fe->fe_counters.p.http.rsp[n]++; |
Willy Tarreau | 5e16cbc | 2012-11-24 14:54:13 +0100 | [diff] [blame] | 5172 | } |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 5173 | if ((s->flags & SF_BE_ASSIGNED) && |
Willy Tarreau | 858b103 | 2015-12-07 17:04:59 +0100 | [diff] [blame] | 5174 | (be->mode == PR_MODE_HTTP)) { |
| 5175 | be->be_counters.p.http.rsp[n]++; |
| 5176 | be->be_counters.p.http.cum_req++; |
Willy Tarreau | 5e16cbc | 2012-11-24 14:54:13 +0100 | [diff] [blame] | 5177 | } |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5178 | } |
| 5179 | |
| 5180 | /* don't count other requests' data */ |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 5181 | s->logs.bytes_in -= s->req.buf->i; |
| 5182 | s->logs.bytes_out -= s->res.buf->i; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5183 | |
| 5184 | /* let's do a final log if we need it */ |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 5185 | if (!LIST_ISEMPTY(&fe->logformat) && s->logs.logwait && |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 5186 | !(s->flags & SF_MONITOR) && |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 5187 | (!(fe->options & PR_O_NULLNOLOG) || s->req.total)) { |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5188 | s->do_log(s); |
| 5189 | } |
| 5190 | |
Willy Tarreau | d713bcc | 2014-06-25 15:36:04 +0200 | [diff] [blame] | 5191 | /* stop tracking content-based counters */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 5192 | stream_stop_content_counters(s); |
| 5193 | stream_update_time_stats(s); |
Willy Tarreau | 4bfc580 | 2014-06-17 12:19:18 +0200 | [diff] [blame] | 5194 | |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5195 | s->logs.accept_date = date; /* user-visible date for logging */ |
| 5196 | s->logs.tv_accept = now; /* corrected date for internal use */ |
Thierry FOURNIER / OZON.IO | 4cac359 | 2016-07-28 17:19:45 +0200 | [diff] [blame] | 5197 | s->logs.t_handshake = 0; /* There are no handshake in keep alive connection. */ |
| 5198 | s->logs.t_idle = -1; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5199 | tv_zero(&s->logs.tv_request); |
| 5200 | s->logs.t_queue = -1; |
| 5201 | s->logs.t_connect = -1; |
| 5202 | s->logs.t_data = -1; |
| 5203 | s->logs.t_close = 0; |
| 5204 | s->logs.prx_queue_size = 0; /* we get the number of pending conns before us */ |
| 5205 | s->logs.srv_queue_size = 0; /* we will get this number soon */ |
| 5206 | |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 5207 | s->logs.bytes_in = s->req.total = s->req.buf->i; |
| 5208 | s->logs.bytes_out = s->res.total = s->res.buf->i; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5209 | |
| 5210 | if (s->pend_pos) |
| 5211 | pendconn_free(s->pend_pos); |
| 5212 | |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 5213 | if (objt_server(s->target)) { |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 5214 | if (s->flags & SF_CURR_SESS) { |
| 5215 | s->flags &= ~SF_CURR_SESS; |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 5216 | objt_server(s->target)->cur_sess--; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5217 | } |
Willy Tarreau | 858b103 | 2015-12-07 17:04:59 +0100 | [diff] [blame] | 5218 | if (may_dequeue_tasks(objt_server(s->target), be)) |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 5219 | process_srv_queue(objt_server(s->target)); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5220 | } |
| 5221 | |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 5222 | s->target = NULL; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5223 | |
Willy Tarreau | 4213a11 | 2013-12-15 10:25:42 +0100 | [diff] [blame] | 5224 | /* only release our endpoint if we don't intend to reuse the |
| 5225 | * connection. |
| 5226 | */ |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 5227 | if (((s->txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) || |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 5228 | !si_conn_ready(&s->si[1])) { |
| 5229 | si_release_endpoint(&s->si[1]); |
Willy Tarreau | 323a2d9 | 2015-08-04 19:00:17 +0200 | [diff] [blame] | 5230 | srv_conn = NULL; |
Willy Tarreau | 4213a11 | 2013-12-15 10:25:42 +0100 | [diff] [blame] | 5231 | } |
| 5232 | |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 5233 | s->si[1].state = s->si[1].prev_state = SI_ST_INI; |
| 5234 | s->si[1].err_type = SI_ET_NONE; |
| 5235 | s->si[1].conn_retries = 0; /* used for logging too */ |
| 5236 | s->si[1].exp = TICK_ETERNITY; |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 5237 | 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] | 5238 | 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); |
| 5239 | 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] | 5240 | s->flags &= ~(SF_DIRECT|SF_ASSIGNED|SF_ADDR_SET|SF_BE_ASSIGNED|SF_FORCE_PRST|SF_IGNORE_PRST); |
| 5241 | s->flags &= ~(SF_CURR_SESS|SF_REDIRECTABLE|SF_SRV_REUSED); |
| 5242 | s->flags &= ~(SF_ERR_MASK|SF_FINST_MASK|SF_REDISP); |
Willy Tarreau | 543db62 | 2012-11-15 16:41:22 +0100 | [diff] [blame] | 5243 | |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 5244 | s->txn->meth = 0; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5245 | http_reset_txn(s); |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 5246 | s->txn->flags |= TX_NOT_FIRST | TX_WAIT_NEXT_RQ; |
Willy Tarreau | 068621e | 2013-12-23 15:11:25 +0100 | [diff] [blame] | 5247 | |
| 5248 | if (prev_status == 401 || prev_status == 407) { |
| 5249 | /* In HTTP keep-alive mode, if we receive a 401, we still have |
| 5250 | * a chance of being able to send the visitor again to the same |
| 5251 | * server over the same connection. This is required by some |
| 5252 | * broken protocols such as NTLM, and anyway whenever there is |
| 5253 | * an opportunity for sending the challenge to the proper place, |
| 5254 | * it's better to do it (at least it helps with debugging). |
| 5255 | */ |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 5256 | s->txn->flags |= TX_PREFER_LAST; |
Willy Tarreau | bd99d58 | 2015-09-02 10:40:43 +0200 | [diff] [blame] | 5257 | if (srv_conn) |
| 5258 | srv_conn->flags |= CO_FL_PRIVATE; |
Willy Tarreau | 068621e | 2013-12-23 15:11:25 +0100 | [diff] [blame] | 5259 | } |
| 5260 | |
Willy Tarreau | 53f9685 | 2016-02-02 18:50:47 +0100 | [diff] [blame] | 5261 | /* Never ever allow to reuse a connection from a non-reuse backend */ |
| 5262 | if (srv_conn && (be->options & PR_O_REUSE_MASK) == PR_O_REUSE_NEVR) |
| 5263 | srv_conn->flags |= CO_FL_PRIVATE; |
| 5264 | |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 5265 | if (fe->options2 & PR_O2_INDEPSTR) |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 5266 | s->si[1].flags |= SI_FL_INDEP_STR; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5267 | |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 5268 | if (fe->options2 & PR_O2_NODELAY) { |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 5269 | s->req.flags |= CF_NEVER_WAIT; |
| 5270 | s->res.flags |= CF_NEVER_WAIT; |
Willy Tarreau | 96e3121 | 2011-05-30 18:10:30 +0200 | [diff] [blame] | 5271 | } |
| 5272 | |
Willy Tarreau | 714ea78 | 2015-11-25 20:11:11 +0100 | [diff] [blame] | 5273 | /* we're removing the analysers, we MUST re-enable events detection. |
| 5274 | * We don't enable close on the response channel since it's either |
| 5275 | * already closed, or in keep-alive with an idle connection handler. |
| 5276 | */ |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 5277 | channel_auto_read(&s->req); |
| 5278 | channel_auto_close(&s->req); |
| 5279 | channel_auto_read(&s->res); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5280 | |
Willy Tarreau | 1c59bd5 | 2015-11-02 20:20:11 +0100 | [diff] [blame] | 5281 | /* we're in keep-alive with an idle connection, monitor it if not already done */ |
| 5282 | if (srv_conn && LIST_ISEMPTY(&srv_conn->list)) { |
Willy Tarreau | 323a2d9 | 2015-08-04 19:00:17 +0200 | [diff] [blame] | 5283 | srv = objt_server(srv_conn->target); |
Willy Tarreau | 8dff998 | 2015-08-04 20:45:52 +0200 | [diff] [blame] | 5284 | if (!srv) |
| 5285 | si_idle_conn(&s->si[1], NULL); |
Willy Tarreau | 53f9685 | 2016-02-02 18:50:47 +0100 | [diff] [blame] | 5286 | else if (srv_conn->flags & CO_FL_PRIVATE) |
Willy Tarreau | 8dff998 | 2015-08-04 20:45:52 +0200 | [diff] [blame] | 5287 | si_idle_conn(&s->si[1], &srv->priv_conns); |
Willy Tarreau | 449d74a | 2015-08-05 17:16:33 +0200 | [diff] [blame] | 5288 | else if (prev_flags & TX_NOT_FIRST) |
| 5289 | /* note: we check the request, not the connection, but |
| 5290 | * this is valid for strategies SAFE and AGGR, and in |
| 5291 | * case of ALWS, we don't care anyway. |
| 5292 | */ |
| 5293 | si_idle_conn(&s->si[1], &srv->safe_conns); |
Willy Tarreau | 8dff998 | 2015-08-04 20:45:52 +0200 | [diff] [blame] | 5294 | else |
| 5295 | si_idle_conn(&s->si[1], &srv->idle_conns); |
Willy Tarreau | 4320eaa | 2015-08-05 11:08:30 +0200 | [diff] [blame] | 5296 | } |
Christopher Faulet | e600624 | 2017-03-10 11:52:44 +0100 | [diff] [blame] | 5297 | s->req.analysers = strm_li(s) ? strm_li(s)->analysers : 0; |
| 5298 | s->res.analysers = 0; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5299 | } |
| 5300 | |
| 5301 | |
| 5302 | /* This function updates the request state machine according to the response |
| 5303 | * state machine and buffer flags. It returns 1 if it changes anything (flag |
| 5304 | * or state), otherwise zero. It ignores any state before HTTP_MSG_DONE, as |
| 5305 | * it is only used to find when a request/response couple is complete. Both |
| 5306 | * this function and its equivalent should loop until both return zero. It |
| 5307 | * can set its own state to DONE, CLOSING, CLOSED, TUNNEL, ERROR. |
| 5308 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 5309 | int http_sync_req_state(struct stream *s) |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5310 | { |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 5311 | struct channel *chn = &s->req; |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 5312 | struct http_txn *txn = s->txn; |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 5313 | unsigned int old_flags = chn->flags; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5314 | unsigned int old_state = txn->req.msg_state; |
| 5315 | |
Christopher Faulet | 4be9803 | 2017-07-18 10:48:24 +0200 | [diff] [blame] | 5316 | if (unlikely(txn->req.msg_state < HTTP_MSG_DONE)) |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5317 | return 0; |
| 5318 | |
| 5319 | if (txn->req.msg_state == HTTP_MSG_DONE) { |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 5320 | /* No need to read anymore, the request was completely parsed. |
Willy Tarreau | 58bd8fd | 2010-09-28 14:16:41 +0200 | [diff] [blame] | 5321 | * We can shut the read side unless we want to abort_on_close, |
| 5322 | * or we have a POST request. The issue with POST requests is |
| 5323 | * that some browsers still send a CRLF after the request, and |
| 5324 | * this CRLF must be read so that it does not remain in the kernel |
| 5325 | * buffers, otherwise a close could cause an RST on some systems |
| 5326 | * (eg: Linux). |
Willy Tarreau | 3988d93 | 2013-12-27 23:03:08 +0100 | [diff] [blame] | 5327 | * Note that if we're using keep-alive on the client side, we'd |
| 5328 | * rather poll now and keep the polling enabled for the whole |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 5329 | * stream's life than enabling/disabling it between each |
Willy Tarreau | 3988d93 | 2013-12-27 23:03:08 +0100 | [diff] [blame] | 5330 | * response and next request. |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 5331 | */ |
Willy Tarreau | 3988d93 | 2013-12-27 23:03:08 +0100 | [diff] [blame] | 5332 | if (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_SCL) && |
| 5333 | ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) && |
| 5334 | !(s->be->options & PR_O_ABRT_CLOSE) && |
| 5335 | txn->meth != HTTP_METH_POST) |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 5336 | channel_dont_read(chn); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5337 | |
Willy Tarreau | 40f151a | 2012-12-20 12:10:09 +0100 | [diff] [blame] | 5338 | /* if the server closes the connection, we want to immediately react |
| 5339 | * and close the socket to save packets and syscalls. |
| 5340 | */ |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 5341 | s->si[1].flags |= SI_FL_NOHALF; |
Willy Tarreau | 40f151a | 2012-12-20 12:10:09 +0100 | [diff] [blame] | 5342 | |
Willy Tarreau | 7f876a1 | 2015-11-18 11:59:55 +0100 | [diff] [blame] | 5343 | /* In any case we've finished parsing the request so we must |
| 5344 | * disable Nagle when sending data because 1) we're not going |
| 5345 | * to shut this side, and 2) the server is waiting for us to |
| 5346 | * send pending data. |
| 5347 | */ |
| 5348 | chn->flags |= CF_NEVER_WAIT; |
| 5349 | |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5350 | if (txn->rsp.msg_state == HTTP_MSG_ERROR) |
| 5351 | goto wait_other_side; |
| 5352 | |
| 5353 | if (txn->rsp.msg_state < HTTP_MSG_DONE) { |
| 5354 | /* The server has not finished to respond, so we |
| 5355 | * don't want to move in order not to upset it. |
| 5356 | */ |
| 5357 | goto wait_other_side; |
| 5358 | } |
| 5359 | |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5360 | /* When we get here, it means that both the request and the |
| 5361 | * response have finished receiving. Depending on the connection |
| 5362 | * mode, we'll have to wait for the last bytes to leave in either |
| 5363 | * direction, and sometimes for a close to be effective. |
| 5364 | */ |
| 5365 | |
Willy Tarreau | cce7fa4 | 2010-01-16 23:19:39 +0100 | [diff] [blame] | 5366 | if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) { |
| 5367 | /* Server-close mode : queue a connection close to the server */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 5368 | if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW))) |
| 5369 | channel_shutw_now(chn); |
Willy Tarreau | cce7fa4 | 2010-01-16 23:19:39 +0100 | [diff] [blame] | 5370 | } |
| 5371 | else if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) { |
| 5372 | /* Option forceclose is set, or either side wants to close, |
| 5373 | * let's enforce it now that we're not expecting any new |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 5374 | * data to come. The caller knows the stream is complete |
Willy Tarreau | cce7fa4 | 2010-01-16 23:19:39 +0100 | [diff] [blame] | 5375 | * once both states are CLOSED. |
Christopher Faulet | 1486b0a | 2017-07-18 11:42:08 +0200 | [diff] [blame] | 5376 | * |
| 5377 | * However, there is an exception if the response |
| 5378 | * length is undefined. In this case, we need to wait |
| 5379 | * the close from the server. The response will be |
| 5380 | * switched in TUNNEL mode until the end. |
Willy Tarreau | cce7fa4 | 2010-01-16 23:19:39 +0100 | [diff] [blame] | 5381 | */ |
Christopher Faulet | 1486b0a | 2017-07-18 11:42:08 +0200 | [diff] [blame] | 5382 | if (!(txn->rsp.flags & HTTP_MSGF_XFER_LEN) && |
| 5383 | txn->rsp.msg_state != HTTP_MSG_CLOSED) |
| 5384 | goto check_channel_flags; |
| 5385 | |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 5386 | if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW))) { |
| 5387 | channel_shutr_now(chn); |
| 5388 | channel_shutw_now(chn); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5389 | } |
Willy Tarreau | cce7fa4 | 2010-01-16 23:19:39 +0100 | [diff] [blame] | 5390 | } |
| 5391 | else { |
Willy Tarreau | 4213a11 | 2013-12-15 10:25:42 +0100 | [diff] [blame] | 5392 | /* The last possible modes are keep-alive and tunnel. Tunnel mode |
| 5393 | * will not have any analyser so it needs to poll for reads. |
Willy Tarreau | cce7fa4 | 2010-01-16 23:19:39 +0100 | [diff] [blame] | 5394 | */ |
Willy Tarreau | 4213a11 | 2013-12-15 10:25:42 +0100 | [diff] [blame] | 5395 | if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN) { |
| 5396 | channel_auto_read(chn); |
| 5397 | txn->req.msg_state = HTTP_MSG_TUNNEL; |
Willy Tarreau | 4213a11 | 2013-12-15 10:25:42 +0100 | [diff] [blame] | 5398 | } |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5399 | } |
| 5400 | |
Christopher Faulet | 4be9803 | 2017-07-18 10:48:24 +0200 | [diff] [blame] | 5401 | goto check_channel_flags; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5402 | } |
| 5403 | |
| 5404 | if (txn->req.msg_state == HTTP_MSG_CLOSING) { |
| 5405 | http_msg_closing: |
| 5406 | /* nothing else to forward, just waiting for the output buffer |
| 5407 | * to be empty and for the shutw_now to take effect. |
| 5408 | */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 5409 | if (channel_is_empty(chn)) { |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5410 | txn->req.msg_state = HTTP_MSG_CLOSED; |
| 5411 | goto http_msg_closed; |
| 5412 | } |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 5413 | else if (chn->flags & CF_SHUTW) { |
Willy Tarreau | 10e61cb | 2017-01-04 14:51:22 +0100 | [diff] [blame] | 5414 | txn->req.err_state = txn->req.msg_state; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5415 | txn->req.msg_state = HTTP_MSG_ERROR; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5416 | } |
Christopher Faulet | 56d2609 | 2017-07-20 11:05:10 +0200 | [diff] [blame] | 5417 | goto wait_other_side; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5418 | } |
| 5419 | |
| 5420 | if (txn->req.msg_state == HTTP_MSG_CLOSED) { |
| 5421 | http_msg_closed: |
Willy Tarreau | 3988d93 | 2013-12-27 23:03:08 +0100 | [diff] [blame] | 5422 | /* see above in MSG_DONE why we only do this in these states */ |
| 5423 | if (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_SCL) && |
| 5424 | ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) && |
| 5425 | !(s->be->options & PR_O_ABRT_CLOSE)) |
Willy Tarreau | 2e7a165 | 2013-12-15 15:32:10 +0100 | [diff] [blame] | 5426 | channel_dont_read(chn); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5427 | goto wait_other_side; |
| 5428 | } |
| 5429 | |
Christopher Faulet | 4be9803 | 2017-07-18 10:48:24 +0200 | [diff] [blame] | 5430 | check_channel_flags: |
| 5431 | /* Here, we are in HTTP_MSG_DONE or HTTP_MSG_TUNNEL */ |
| 5432 | if (chn->flags & (CF_SHUTW|CF_SHUTW_NOW)) { |
| 5433 | /* if we've just closed an output, let's switch */ |
| 5434 | s->si[1].flags |= SI_FL_NOLINGER; /* we want to close ASAP */ |
| 5435 | txn->req.msg_state = HTTP_MSG_CLOSING; |
| 5436 | goto http_msg_closing; |
| 5437 | } |
| 5438 | |
| 5439 | |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5440 | wait_other_side: |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 5441 | return txn->req.msg_state != old_state || chn->flags != old_flags; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5442 | } |
| 5443 | |
| 5444 | |
| 5445 | /* This function updates the response state machine according to the request |
| 5446 | * state machine and buffer flags. It returns 1 if it changes anything (flag |
| 5447 | * or state), otherwise zero. It ignores any state before HTTP_MSG_DONE, as |
| 5448 | * it is only used to find when a request/response couple is complete. Both |
| 5449 | * this function and its equivalent should loop until both return zero. It |
| 5450 | * can set its own state to DONE, CLOSING, CLOSED, TUNNEL, ERROR. |
| 5451 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 5452 | int http_sync_res_state(struct stream *s) |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5453 | { |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 5454 | struct channel *chn = &s->res; |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 5455 | struct http_txn *txn = s->txn; |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 5456 | unsigned int old_flags = chn->flags; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5457 | unsigned int old_state = txn->rsp.msg_state; |
| 5458 | |
Christopher Faulet | 4be9803 | 2017-07-18 10:48:24 +0200 | [diff] [blame] | 5459 | if (unlikely(txn->rsp.msg_state < HTTP_MSG_DONE)) |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5460 | return 0; |
| 5461 | |
| 5462 | if (txn->rsp.msg_state == HTTP_MSG_DONE) { |
| 5463 | /* In theory, we don't need to read anymore, but we must |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 5464 | * still monitor the server connection for a possible close |
| 5465 | * while the request is being uploaded, so we don't disable |
| 5466 | * reading. |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5467 | */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 5468 | /* channel_dont_read(chn); */ |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5469 | |
| 5470 | if (txn->req.msg_state == HTTP_MSG_ERROR) |
| 5471 | goto wait_other_side; |
| 5472 | |
| 5473 | if (txn->req.msg_state < HTTP_MSG_DONE) { |
| 5474 | /* The client seems to still be sending data, probably |
| 5475 | * because we got an error response during an upload. |
| 5476 | * We have the choice of either breaking the connection |
| 5477 | * or letting it pass through. Let's do the later. |
| 5478 | */ |
| 5479 | goto wait_other_side; |
| 5480 | } |
| 5481 | |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5482 | /* When we get here, it means that both the request and the |
| 5483 | * response have finished receiving. Depending on the connection |
| 5484 | * mode, we'll have to wait for the last bytes to leave in either |
| 5485 | * direction, and sometimes for a close to be effective. |
| 5486 | */ |
| 5487 | |
| 5488 | if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) { |
| 5489 | /* Server-close mode : shut read and wait for the request |
| 5490 | * side to close its output buffer. The caller will detect |
| 5491 | * when we're in DONE and the other is in CLOSED and will |
| 5492 | * catch that for the final cleanup. |
| 5493 | */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 5494 | if (!(chn->flags & (CF_SHUTR|CF_SHUTR_NOW))) |
| 5495 | channel_shutr_now(chn); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5496 | } |
Willy Tarreau | cce7fa4 | 2010-01-16 23:19:39 +0100 | [diff] [blame] | 5497 | else if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) { |
| 5498 | /* Option forceclose is set, or either side wants to close, |
| 5499 | * let's enforce it now that we're not expecting any new |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 5500 | * data to come. The caller knows the stream is complete |
Willy Tarreau | cce7fa4 | 2010-01-16 23:19:39 +0100 | [diff] [blame] | 5501 | * once both states are CLOSED. |
Christopher Faulet | 1486b0a | 2017-07-18 11:42:08 +0200 | [diff] [blame] | 5502 | * |
| 5503 | * However, there is an exception if the response length |
| 5504 | * is undefined. In this case, we switch in TUNNEL mode. |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5505 | */ |
Christopher Faulet | 1486b0a | 2017-07-18 11:42:08 +0200 | [diff] [blame] | 5506 | if (!(txn->rsp.flags & HTTP_MSGF_XFER_LEN)) { |
| 5507 | channel_auto_read(chn); |
| 5508 | txn->rsp.msg_state = HTTP_MSG_TUNNEL; |
| 5509 | chn->flags |= CF_NEVER_WAIT; |
| 5510 | } |
| 5511 | else if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW))) { |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 5512 | channel_shutr_now(chn); |
| 5513 | channel_shutw_now(chn); |
Willy Tarreau | cce7fa4 | 2010-01-16 23:19:39 +0100 | [diff] [blame] | 5514 | } |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5515 | } |
| 5516 | else { |
Willy Tarreau | 4213a11 | 2013-12-15 10:25:42 +0100 | [diff] [blame] | 5517 | /* The last possible modes are keep-alive and tunnel. Tunnel will |
| 5518 | * need to forward remaining data. Keep-alive will need to monitor |
| 5519 | * for connection closing. |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5520 | */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 5521 | channel_auto_read(chn); |
Willy Tarreau | fc47f91 | 2012-10-20 10:38:09 +0200 | [diff] [blame] | 5522 | chn->flags |= CF_NEVER_WAIT; |
Willy Tarreau | 4213a11 | 2013-12-15 10:25:42 +0100 | [diff] [blame] | 5523 | if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN) |
| 5524 | txn->rsp.msg_state = HTTP_MSG_TUNNEL; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5525 | } |
| 5526 | |
Christopher Faulet | 4be9803 | 2017-07-18 10:48:24 +0200 | [diff] [blame] | 5527 | goto check_channel_flags; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5528 | } |
| 5529 | |
| 5530 | if (txn->rsp.msg_state == HTTP_MSG_CLOSING) { |
| 5531 | http_msg_closing: |
| 5532 | /* nothing else to forward, just waiting for the output buffer |
| 5533 | * to be empty and for the shutw_now to take effect. |
| 5534 | */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 5535 | if (channel_is_empty(chn)) { |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5536 | txn->rsp.msg_state = HTTP_MSG_CLOSED; |
| 5537 | goto http_msg_closed; |
| 5538 | } |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 5539 | else if (chn->flags & CF_SHUTW) { |
Christopher Faulet | a3992e0 | 2017-07-18 10:35:55 +0200 | [diff] [blame] | 5540 | txn->rsp.err_state = txn->rsp.msg_state; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5541 | txn->rsp.msg_state = HTTP_MSG_ERROR; |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 5542 | s->be->be_counters.cli_aborts++; |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 5543 | if (objt_server(s->target)) |
| 5544 | objt_server(s->target)->counters.cli_aborts++; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5545 | } |
Christopher Faulet | 56d2609 | 2017-07-20 11:05:10 +0200 | [diff] [blame] | 5546 | goto wait_other_side; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5547 | } |
| 5548 | |
| 5549 | if (txn->rsp.msg_state == HTTP_MSG_CLOSED) { |
| 5550 | http_msg_closed: |
| 5551 | /* drop any pending data */ |
Willy Tarreau | 319f745 | 2015-01-14 20:32:59 +0100 | [diff] [blame] | 5552 | channel_truncate(chn); |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 5553 | channel_auto_close(chn); |
| 5554 | channel_auto_read(chn); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5555 | goto wait_other_side; |
| 5556 | } |
| 5557 | |
Christopher Faulet | 4be9803 | 2017-07-18 10:48:24 +0200 | [diff] [blame] | 5558 | check_channel_flags: |
| 5559 | /* Here, we are in HTTP_MSG_DONE or HTTP_MSG_TUNNEL */ |
| 5560 | if (chn->flags & (CF_SHUTW|CF_SHUTW_NOW)) { |
| 5561 | /* if we've just closed an output, let's switch */ |
| 5562 | txn->rsp.msg_state = HTTP_MSG_CLOSING; |
| 5563 | goto http_msg_closing; |
| 5564 | } |
| 5565 | |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5566 | wait_other_side: |
Willy Tarreau | fc47f91 | 2012-10-20 10:38:09 +0200 | [diff] [blame] | 5567 | /* We force the response to leave immediately if we're waiting for the |
| 5568 | * other side, since there is no pending shutdown to push it out. |
| 5569 | */ |
| 5570 | if (!channel_is_empty(chn)) |
| 5571 | chn->flags |= CF_SEND_DONTWAIT; |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 5572 | return txn->rsp.msg_state != old_state || chn->flags != old_flags; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5573 | } |
| 5574 | |
| 5575 | |
Christopher Faulet | 894da4c | 2017-07-18 11:29:07 +0200 | [diff] [blame] | 5576 | /* Resync the request and response state machines. */ |
| 5577 | void http_resync_states(struct stream *s) |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5578 | { |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 5579 | struct http_txn *txn = s->txn; |
Christopher Faulet | 894da4c | 2017-07-18 11:29:07 +0200 | [diff] [blame] | 5580 | #ifdef DEBUG_FULL |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5581 | int old_req_state = txn->req.msg_state; |
| 5582 | int old_res_state = txn->rsp.msg_state; |
Christopher Faulet | 894da4c | 2017-07-18 11:29:07 +0200 | [diff] [blame] | 5583 | #endif |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5584 | |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5585 | http_sync_req_state(s); |
| 5586 | while (1) { |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5587 | if (!http_sync_res_state(s)) |
| 5588 | break; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5589 | if (!http_sync_req_state(s)) |
| 5590 | break; |
| 5591 | } |
Willy Tarreau | 3ce10ff | 2014-04-22 08:24:38 +0200 | [diff] [blame] | 5592 | |
Christopher Faulet | 894da4c | 2017-07-18 11:29:07 +0200 | [diff] [blame] | 5593 | DPRINTF(stderr,"[%u] %s: stream=%p old=%s,%s cur=%s,%s " |
| 5594 | "req->analysers=0x%08x res->analysers=0x%08x\n", |
| 5595 | now_ms, __FUNCTION__, s, |
| 5596 | http_msg_state_str(old_req_state), http_msg_state_str(old_res_state), |
| 5597 | http_msg_state_str(txn->req.msg_state), http_msg_state_str(txn->rsp.msg_state), |
| 5598 | s->req.analysers, s->res.analysers); |
Christopher Faulet | 814d270 | 2017-03-30 11:33:44 +0200 | [diff] [blame] | 5599 | |
| 5600 | |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5601 | /* OK, both state machines agree on a compatible state. |
| 5602 | * There are a few cases we're interested in : |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5603 | * - HTTP_MSG_CLOSED on both sides means we've reached the end in both |
| 5604 | * directions, so let's simply disable both analysers. |
Christopher Faulet | f77bb53 | 2017-07-18 11:18:46 +0200 | [diff] [blame] | 5605 | * - HTTP_MSG_CLOSED on the response only or HTTP_MSG_ERROR on either |
| 5606 | * means we must abort the request. |
| 5607 | * - HTTP_MSG_TUNNEL on either means we have to disable analyser on |
| 5608 | * corresponding channel. |
| 5609 | * - HTTP_MSG_DONE or HTTP_MSG_CLOSED on the request and HTTP_MSG_DONE |
| 5610 | * on the response with server-close mode means we've completed one |
| 5611 | * request and we must re-initialize the server connection. |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5612 | */ |
Christopher Faulet | f77bb53 | 2017-07-18 11:18:46 +0200 | [diff] [blame] | 5613 | if (txn->req.msg_state == HTTP_MSG_CLOSED && |
| 5614 | txn->rsp.msg_state == HTTP_MSG_CLOSED) { |
Christopher Faulet | 0184ea7 | 2017-01-05 14:06:34 +0100 | [diff] [blame] | 5615 | s->req.analysers &= AN_REQ_FLT_END; |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 5616 | channel_auto_close(&s->req); |
| 5617 | channel_auto_read(&s->req); |
Christopher Faulet | 0184ea7 | 2017-01-05 14:06:34 +0100 | [diff] [blame] | 5618 | s->res.analysers &= AN_RES_FLT_END; |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 5619 | channel_auto_close(&s->res); |
| 5620 | channel_auto_read(&s->res); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5621 | } |
Christopher Faulet | f77bb53 | 2017-07-18 11:18:46 +0200 | [diff] [blame] | 5622 | else if (txn->rsp.msg_state == HTTP_MSG_CLOSED || |
| 5623 | txn->rsp.msg_state == HTTP_MSG_ERROR || |
Willy Tarreau | 40f151a | 2012-12-20 12:10:09 +0100 | [diff] [blame] | 5624 | txn->req.msg_state == HTTP_MSG_ERROR) { |
Christopher Faulet | 0184ea7 | 2017-01-05 14:06:34 +0100 | [diff] [blame] | 5625 | s->res.analysers &= AN_RES_FLT_END; |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 5626 | channel_auto_close(&s->res); |
| 5627 | channel_auto_read(&s->res); |
Christopher Faulet | 0184ea7 | 2017-01-05 14:06:34 +0100 | [diff] [blame] | 5628 | s->req.analysers &= AN_REQ_FLT_END; |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 5629 | channel_abort(&s->req); |
| 5630 | channel_auto_close(&s->req); |
| 5631 | channel_auto_read(&s->req); |
| 5632 | channel_truncate(&s->req); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5633 | } |
Christopher Faulet | f77bb53 | 2017-07-18 11:18:46 +0200 | [diff] [blame] | 5634 | else if (txn->req.msg_state == HTTP_MSG_TUNNEL || |
| 5635 | txn->rsp.msg_state == HTTP_MSG_TUNNEL) { |
| 5636 | if (txn->req.msg_state == HTTP_MSG_TUNNEL) { |
| 5637 | s->req.analysers &= AN_REQ_FLT_END; |
| 5638 | if (HAS_REQ_DATA_FILTERS(s)) |
| 5639 | s->req.analysers |= AN_REQ_FLT_XFER_DATA; |
| 5640 | } |
| 5641 | if (txn->rsp.msg_state == HTTP_MSG_TUNNEL) { |
| 5642 | s->res.analysers &= AN_RES_FLT_END; |
| 5643 | if (HAS_RSP_DATA_FILTERS(s)) |
| 5644 | s->res.analysers |= AN_RES_FLT_XFER_DATA; |
| 5645 | } |
| 5646 | channel_auto_close(&s->req); |
| 5647 | channel_auto_read(&s->req); |
| 5648 | channel_auto_close(&s->res); |
| 5649 | channel_auto_read(&s->res); |
| 5650 | } |
Willy Tarreau | 4213a11 | 2013-12-15 10:25:42 +0100 | [diff] [blame] | 5651 | else if ((txn->req.msg_state == HTTP_MSG_DONE || |
| 5652 | txn->req.msg_state == HTTP_MSG_CLOSED) && |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5653 | txn->rsp.msg_state == HTTP_MSG_DONE && |
Willy Tarreau | 4213a11 | 2013-12-15 10:25:42 +0100 | [diff] [blame] | 5654 | ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL || |
| 5655 | (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL)) { |
| 5656 | /* server-close/keep-alive: terminate this transaction, |
| 5657 | * possibly killing the server connection and reinitialize |
Willy Tarreau | 3de5bd6 | 2016-05-02 16:07:07 +0200 | [diff] [blame] | 5658 | * a fresh-new transaction, but only once we're sure there's |
| 5659 | * enough room in the request and response buffer to process |
Christopher Faulet | c0c672a | 2017-03-28 11:51:33 +0200 | [diff] [blame] | 5660 | * another request. They must not hold any pending output data |
| 5661 | * and the response buffer must realigned |
| 5662 | * (realign is done is http_end_txn_clean_session). |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5663 | */ |
Christopher Faulet | 894da4c | 2017-07-18 11:29:07 +0200 | [diff] [blame] | 5664 | if (s->req.buf->o) |
Willy Tarreau | 3de5bd6 | 2016-05-02 16:07:07 +0200 | [diff] [blame] | 5665 | s->req.flags |= CF_WAKE_WRITE; |
Christopher Faulet | 894da4c | 2017-07-18 11:29:07 +0200 | [diff] [blame] | 5666 | else if (s->res.buf->o) |
Willy Tarreau | 3de5bd6 | 2016-05-02 16:07:07 +0200 | [diff] [blame] | 5667 | s->res.flags |= CF_WAKE_WRITE; |
Christopher Faulet | a81ff60 | 2017-07-18 22:01:05 +0200 | [diff] [blame] | 5668 | else { |
| 5669 | s->req.analysers = AN_REQ_FLT_END; |
| 5670 | s->res.analysers = AN_RES_FLT_END; |
| 5671 | txn->flags |= TX_WAIT_CLEANUP; |
| 5672 | } |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5673 | } |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5674 | } |
| 5675 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5676 | /* This function is an analyser which forwards request body (including chunk |
| 5677 | * sizes if any). It is called as soon as we must forward, even if we forward |
| 5678 | * zero byte. The only situation where it must not be called is when we're in |
| 5679 | * tunnel mode and we want to forward till the close. It's used both to forward |
| 5680 | * remaining data and to resync after end of body. It expects the msg_state to |
| 5681 | * 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] | 5682 | * 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] | 5683 | * 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] | 5684 | * bytes of pending data + the headers if not already done. |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5685 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 5686 | 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] | 5687 | { |
Willy Tarreau | fb0afa7 | 2015-04-03 14:46:27 +0200 | [diff] [blame] | 5688 | struct session *sess = s->sess; |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 5689 | struct http_txn *txn = s->txn; |
| 5690 | struct http_msg *msg = &s->txn->req; |
Christopher Faulet | 3e34429 | 2015-11-24 16:24:13 +0100 | [diff] [blame] | 5691 | int ret; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5692 | |
Christopher Faulet | 814d270 | 2017-03-30 11:33:44 +0200 | [diff] [blame] | 5693 | DPRINTF(stderr,"[%u] %s: stream=%p b=%p, exp(r,w)=%u,%u bf=%08x bh=%d analysers=%02x\n", |
| 5694 | now_ms, __FUNCTION__, |
| 5695 | s, |
| 5696 | req, |
| 5697 | req->rex, req->wex, |
| 5698 | req->flags, |
| 5699 | req->buf->i, |
| 5700 | req->analysers); |
| 5701 | |
Willy Tarreau | f5c8bd6 | 2010-01-04 07:10:34 +0100 | [diff] [blame] | 5702 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) |
| 5703 | return 0; |
| 5704 | |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 5705 | 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] | 5706 | ((req->flags & CF_SHUTW) && (req->to_forward || req->buf->o))) { |
Willy Tarreau | 4fe4190 | 2010-06-07 22:27:41 +0200 | [diff] [blame] | 5707 | /* Output closed while we were sending data. We must abort and |
| 5708 | * wake the other side up. |
| 5709 | */ |
Willy Tarreau | 10e61cb | 2017-01-04 14:51:22 +0100 | [diff] [blame] | 5710 | msg->err_state = msg->msg_state; |
Willy Tarreau | 4fe4190 | 2010-06-07 22:27:41 +0200 | [diff] [blame] | 5711 | msg->msg_state = HTTP_MSG_ERROR; |
| 5712 | http_resync_states(s); |
Willy Tarreau | 082b01c | 2010-01-02 23:58:04 +0100 | [diff] [blame] | 5713 | return 1; |
| 5714 | } |
| 5715 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5716 | /* Note that we don't have to send 100-continue back because we don't |
| 5717 | * need the data to complete our job, and it's up to the server to |
| 5718 | * decide whether to return 100, 417 or anything else in return of |
| 5719 | * an "Expect: 100-continue" header. |
| 5720 | */ |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 5721 | if (msg->msg_state == HTTP_MSG_BODY) { |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 5722 | msg->msg_state = ((msg->flags & HTTP_MSGF_TE_CHNK) |
| 5723 | ? HTTP_MSG_CHUNK_SIZE |
| 5724 | : HTTP_MSG_DATA); |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 5725 | |
| 5726 | /* TODO/filters: when http-buffer-request option is set or if a |
| 5727 | * rule on url_param exists, the first chunk size could be |
| 5728 | * already parsed. In that case, msg->next is after the chunk |
| 5729 | * size (including the CRLF after the size). So this case should |
| 5730 | * be handled to */ |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5731 | } |
| 5732 | |
Willy Tarreau | 7ba2354 | 2014-04-17 21:50:00 +0200 | [diff] [blame] | 5733 | /* Some post-connect processing might want us to refrain from starting to |
| 5734 | * forward data. Currently, the only reason for this is "balance url_param" |
| 5735 | * whichs need to parse/process the request after we've enabled forwarding. |
| 5736 | */ |
| 5737 | if (unlikely(msg->flags & HTTP_MSGF_WAIT_CONN)) { |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 5738 | if (!(s->res.flags & CF_READ_ATTACHED)) { |
Willy Tarreau | 7ba2354 | 2014-04-17 21:50:00 +0200 | [diff] [blame] | 5739 | channel_auto_connect(req); |
Willy Tarreau | 644c101 | 2014-04-30 18:11:11 +0200 | [diff] [blame] | 5740 | req->flags |= CF_WAKE_CONNECT; |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 5741 | goto missing_data_or_waiting; |
Willy Tarreau | 7ba2354 | 2014-04-17 21:50:00 +0200 | [diff] [blame] | 5742 | } |
| 5743 | msg->flags &= ~HTTP_MSGF_WAIT_CONN; |
| 5744 | } |
| 5745 | |
Willy Tarreau | 80a92c0 | 2014-03-12 10:41:13 +0100 | [diff] [blame] | 5746 | /* in most states, we should abort in case of early close */ |
| 5747 | channel_auto_close(req); |
| 5748 | |
Willy Tarreau | efdf094 | 2014-04-24 20:08:57 +0200 | [diff] [blame] | 5749 | if (req->to_forward) { |
| 5750 | /* We can't process the buffer's contents yet */ |
| 5751 | req->flags |= CF_WAKE_WRITE; |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 5752 | goto missing_data_or_waiting; |
Willy Tarreau | efdf094 | 2014-04-24 20:08:57 +0200 | [diff] [blame] | 5753 | } |
| 5754 | |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 5755 | if (msg->msg_state < HTTP_MSG_DONE) { |
| 5756 | ret = ((msg->flags & HTTP_MSGF_TE_CHNK) |
| 5757 | ? http_msg_forward_chunked_body(s, msg) |
| 5758 | : http_msg_forward_body(s, msg)); |
| 5759 | if (!ret) |
| 5760 | goto missing_data_or_waiting; |
| 5761 | if (ret < 0) |
| 5762 | goto return_bad_req; |
| 5763 | } |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 5764 | |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 5765 | /* other states, DONE...TUNNEL */ |
| 5766 | /* we don't want to forward closes on DONE except in tunnel mode. */ |
| 5767 | if ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) |
| 5768 | channel_dont_close(req); |
Willy Tarreau | 5c54c71 | 2010-07-17 08:02:58 +0200 | [diff] [blame] | 5769 | |
Christopher Faulet | 894da4c | 2017-07-18 11:29:07 +0200 | [diff] [blame] | 5770 | http_resync_states(s); |
| 5771 | if (!(req->analysers & an_bit)) { |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 5772 | if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) { |
| 5773 | if (req->flags & CF_SHUTW) { |
| 5774 | /* request errors are most likely due to the |
| 5775 | * server aborting the transfer. */ |
| 5776 | goto aborted_xfer; |
Willy Tarreau | 58bd8fd | 2010-09-28 14:16:41 +0200 | [diff] [blame] | 5777 | } |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 5778 | if (msg->err_pos >= 0) |
Willy Tarreau | 10e61cb | 2017-01-04 14:51:22 +0100 | [diff] [blame] | 5779 | http_capture_bad_message(&sess->fe->invalid_req, s, msg, msg->err_state, s->be); |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 5780 | goto return_bad_req; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5781 | } |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 5782 | return 1; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5783 | } |
| 5784 | |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 5785 | /* If "option abortonclose" is set on the backend, we want to monitor |
| 5786 | * the client's connection and forward any shutdown notification to the |
| 5787 | * server, which will decide whether to close or to go on processing the |
| 5788 | * request. We only do that in tunnel mode, and not in other modes since |
| 5789 | * it can be abused to exhaust source ports. */ |
| 5790 | if (s->be->options & PR_O_ABRT_CLOSE) { |
| 5791 | channel_auto_read(req); |
| 5792 | if ((req->flags & (CF_SHUTR|CF_READ_NULL)) && |
| 5793 | ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN)) |
| 5794 | s->si[1].flags |= SI_FL_NOLINGER; |
| 5795 | channel_auto_close(req); |
| 5796 | } |
| 5797 | else if (s->txn->meth == HTTP_METH_POST) { |
| 5798 | /* POST requests may require to read extra CRLF sent by broken |
| 5799 | * browsers and which could cause an RST to be sent upon close |
| 5800 | * on some systems (eg: Linux). */ |
| 5801 | channel_auto_read(req); |
| 5802 | } |
| 5803 | return 0; |
Willy Tarreau | bed410e | 2014-04-22 08:19:34 +0200 | [diff] [blame] | 5804 | |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 5805 | missing_data_or_waiting: |
Willy Tarreau | f5c8bd6 | 2010-01-04 07:10:34 +0100 | [diff] [blame] | 5806 | /* stop waiting for data if the input is closed before the end */ |
Christopher Faulet | a33510b | 2017-03-31 15:37:29 +0200 | [diff] [blame] | 5807 | if (msg->msg_state < HTTP_MSG_ENDING && req->flags & CF_SHUTR) { |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 5808 | if (!(s->flags & SF_ERR_MASK)) |
| 5809 | s->flags |= SF_ERR_CLICL; |
| 5810 | if (!(s->flags & SF_FINST_MASK)) { |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 5811 | if (txn->rsp.msg_state < HTTP_MSG_ERROR) |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 5812 | s->flags |= SF_FINST_H; |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 5813 | else |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 5814 | s->flags |= SF_FINST_D; |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 5815 | } |
| 5816 | |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 5817 | sess->fe->fe_counters.cli_aborts++; |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 5818 | s->be->be_counters.cli_aborts++; |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 5819 | if (objt_server(s->target)) |
| 5820 | objt_server(s->target)->counters.cli_aborts++; |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 5821 | |
| 5822 | goto return_bad_req_stats_ok; |
Willy Tarreau | 79ebac6 | 2010-06-07 13:47:49 +0200 | [diff] [blame] | 5823 | } |
Willy Tarreau | f5c8bd6 | 2010-01-04 07:10:34 +0100 | [diff] [blame] | 5824 | |
Willy Tarreau | f5c8bd6 | 2010-01-04 07:10:34 +0100 | [diff] [blame] | 5825 | /* waiting for the last bits to leave the buffer */ |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 5826 | if (req->flags & CF_SHUTW) |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 5827 | goto aborted_xfer; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5828 | |
Willy Tarreau | 92aa1fa | 2010-08-28 18:57:20 +0200 | [diff] [blame] | 5829 | /* 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] | 5830 | * 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] | 5831 | */ |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 5832 | if (msg->flags & HTTP_MSGF_TE_CHNK) |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 5833 | channel_dont_close(req); |
Willy Tarreau | 92aa1fa | 2010-08-28 18:57:20 +0200 | [diff] [blame] | 5834 | |
Willy Tarreau | 5c62092 | 2011-05-11 19:56:11 +0200 | [diff] [blame] | 5835 | /* 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] | 5836 | * 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] | 5837 | * 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] | 5838 | * modes are already handled by the stream sock layer. We must not do |
| 5839 | * this in content-length mode because it could present the MSG_MORE |
| 5840 | * flag with the last block of forwarded data, which would cause an |
| 5841 | * additional delay to be observed by the receiver. |
Willy Tarreau | 5c62092 | 2011-05-11 19:56:11 +0200 | [diff] [blame] | 5842 | */ |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 5843 | if (msg->flags & HTTP_MSGF_TE_CHNK) |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 5844 | req->flags |= CF_EXPECT_MORE; |
Willy Tarreau | 5c62092 | 2011-05-11 19:56:11 +0200 | [diff] [blame] | 5845 | |
Willy Tarreau | f5c8bd6 | 2010-01-04 07:10:34 +0100 | [diff] [blame] | 5846 | return 0; |
| 5847 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5848 | return_bad_req: /* let's centralize all bad requests */ |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 5849 | sess->fe->fe_counters.failed_req++; |
Willy Tarreau | fb0afa7 | 2015-04-03 14:46:27 +0200 | [diff] [blame] | 5850 | if (sess->listener->counters) |
| 5851 | sess->listener->counters->failed_req++; |
Willy Tarreau | bed410e | 2014-04-22 08:19:34 +0200 | [diff] [blame] | 5852 | |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 5853 | return_bad_req_stats_ok: |
Willy Tarreau | 10e61cb | 2017-01-04 14:51:22 +0100 | [diff] [blame] | 5854 | txn->req.err_state = txn->req.msg_state; |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 5855 | txn->req.msg_state = HTTP_MSG_ERROR; |
| 5856 | if (txn->status) { |
| 5857 | /* Note: we don't send any error if some data were already sent */ |
Christopher Faulet | a94e5a5 | 2015-12-09 15:55:06 +0100 | [diff] [blame] | 5858 | http_reply_and_close(s, txn->status, NULL); |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 5859 | } else { |
| 5860 | txn->status = 400; |
Jarno Huuskonen | 9e6906b | 2017-03-06 14:21:49 +0200 | [diff] [blame] | 5861 | http_reply_and_close(s, txn->status, http_error_message(s)); |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 5862 | } |
Christopher Faulet | 0184ea7 | 2017-01-05 14:06:34 +0100 | [diff] [blame] | 5863 | req->analysers &= AN_REQ_FLT_END; |
| 5864 | s->res.analysers &= AN_RES_FLT_END; /* we're in data phase, we want to abort both directions */ |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5865 | |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 5866 | if (!(s->flags & SF_ERR_MASK)) |
| 5867 | s->flags |= SF_ERR_PRXCOND; |
| 5868 | if (!(s->flags & SF_FINST_MASK)) { |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 5869 | if (txn->rsp.msg_state < HTTP_MSG_ERROR) |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 5870 | s->flags |= SF_FINST_H; |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 5871 | else |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 5872 | s->flags |= SF_FINST_D; |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 5873 | } |
| 5874 | return 0; |
| 5875 | |
| 5876 | aborted_xfer: |
Willy Tarreau | 10e61cb | 2017-01-04 14:51:22 +0100 | [diff] [blame] | 5877 | txn->req.err_state = txn->req.msg_state; |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 5878 | txn->req.msg_state = HTTP_MSG_ERROR; |
| 5879 | if (txn->status) { |
| 5880 | /* 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] | 5881 | http_reply_and_close(s, txn->status, NULL); |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 5882 | } else { |
| 5883 | txn->status = 502; |
Jarno Huuskonen | 9e6906b | 2017-03-06 14:21:49 +0200 | [diff] [blame] | 5884 | http_reply_and_close(s, txn->status, http_error_message(s)); |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 5885 | } |
Christopher Faulet | 0184ea7 | 2017-01-05 14:06:34 +0100 | [diff] [blame] | 5886 | req->analysers &= AN_REQ_FLT_END; |
| 5887 | s->res.analysers &= AN_RES_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] | 5888 | |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 5889 | sess->fe->fe_counters.srv_aborts++; |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 5890 | s->be->be_counters.srv_aborts++; |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 5891 | if (objt_server(s->target)) |
| 5892 | objt_server(s->target)->counters.srv_aborts++; |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 5893 | |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 5894 | if (!(s->flags & SF_ERR_MASK)) |
| 5895 | s->flags |= SF_ERR_SRVCL; |
| 5896 | if (!(s->flags & SF_FINST_MASK)) { |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 5897 | if (txn->rsp.msg_state < HTTP_MSG_ERROR) |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 5898 | s->flags |= SF_FINST_H; |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 5899 | else |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 5900 | s->flags |= SF_FINST_D; |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 5901 | } |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5902 | return 0; |
| 5903 | } |
| 5904 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5905 | /* This stream analyser waits for a complete HTTP response. It returns 1 if the |
| 5906 | * processing can continue on next analysers, or zero if it either needs more |
| 5907 | * data or wants to immediately abort the response (eg: timeout, error, ...). It |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 5908 | * 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] | 5909 | * when it has nothing left to do, and may remove any analyser when it wants to |
| 5910 | * abort. |
Willy Tarreau | c65a3ba | 2008-08-11 23:42:50 +0200 | [diff] [blame] | 5911 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 5912 | 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] | 5913 | { |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 5914 | struct session *sess = s->sess; |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 5915 | struct http_txn *txn = s->txn; |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5916 | struct http_msg *msg = &txn->rsp; |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 5917 | struct hdr_ctx ctx; |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 5918 | int use_close_only; |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5919 | int cur_idx; |
Krzysztof Piotr Oledzki | 5fb1882 | 2009-10-13 21:14:09 +0200 | [diff] [blame] | 5920 | int n; |
Willy Tarreau | adfb856 | 2008-08-11 15:24:42 +0200 | [diff] [blame] | 5921 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 5922 | 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] | 5923 | now_ms, __FUNCTION__, |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5924 | s, |
Willy Tarreau | 3a16b2c | 2008-08-28 08:54:27 +0200 | [diff] [blame] | 5925 | rep, |
| 5926 | rep->rex, rep->wex, |
| 5927 | rep->flags, |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 5928 | rep->buf->i, |
Willy Tarreau | 3a16b2c | 2008-08-28 08:54:27 +0200 | [diff] [blame] | 5929 | rep->analysers); |
Willy Tarreau | 67f0eea | 2008-08-10 22:55:22 +0200 | [diff] [blame] | 5930 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5931 | /* |
| 5932 | * Now parse the partial (or complete) lines. |
| 5933 | * We will check the response syntax, and also join multi-line |
| 5934 | * headers. An index of all the lines will be elaborated while |
| 5935 | * parsing. |
| 5936 | * |
| 5937 | * For the parsing, we use a 28 states FSM. |
| 5938 | * |
| 5939 | * Here is the information we currently have : |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 5940 | * rep->buf->p = beginning of response |
| 5941 | * rep->buf->p + msg->eoh = end of processed headers / start of current one |
| 5942 | * rep->buf->p + rep->buf->i = end of input data |
Willy Tarreau | 2692736 | 2012-05-18 23:22:52 +0200 | [diff] [blame] | 5943 | * msg->eol = end of current header or line (LF or CRLF) |
| 5944 | * msg->next = first non-visited byte |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5945 | */ |
| 5946 | |
Willy Tarreau | 628c40c | 2014-04-24 19:11:26 +0200 | [diff] [blame] | 5947 | next_one: |
Willy Tarreau | 83e3af0 | 2009-12-28 17:39:57 +0100 | [diff] [blame] | 5948 | /* There's a protected area at the end of the buffer for rewriting |
| 5949 | * purposes. We don't want to start to parse the request if the |
| 5950 | * protected area is affected, because we may have to move processed |
| 5951 | * data later, which is much more complicated. |
| 5952 | */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 5953 | if (buffer_not_empty(rep->buf) && msg->msg_state < HTTP_MSG_ERROR) { |
Willy Tarreau | ba0902e | 2015-01-13 14:39:16 +0100 | [diff] [blame] | 5954 | if (unlikely(!channel_is_rewritable(rep))) { |
Willy Tarreau | 379357a | 2013-06-08 12:55:46 +0200 | [diff] [blame] | 5955 | /* some data has still not left the buffer, wake us once that's done */ |
| 5956 | if (rep->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_WRITE_ERROR|CF_WRITE_TIMEOUT)) |
| 5957 | goto abort_response; |
| 5958 | channel_dont_close(rep); |
| 5959 | rep->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */ |
Willy Tarreau | d7ad9f5 | 2013-12-31 17:26:25 +0100 | [diff] [blame] | 5960 | rep->flags |= CF_WAKE_WRITE; |
Willy Tarreau | 379357a | 2013-06-08 12:55:46 +0200 | [diff] [blame] | 5961 | return 0; |
Willy Tarreau | 83e3af0 | 2009-12-28 17:39:57 +0100 | [diff] [blame] | 5962 | } |
| 5963 | |
Willy Tarreau | 379357a | 2013-06-08 12:55:46 +0200 | [diff] [blame] | 5964 | if (unlikely(bi_end(rep->buf) < b_ptr(rep->buf, msg->next) || |
| 5965 | bi_end(rep->buf) > rep->buf->data + rep->buf->size - global.tune.maxrewrite)) |
| 5966 | buffer_slow_realign(rep->buf); |
| 5967 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 5968 | if (likely(msg->next < rep->buf->i)) |
Willy Tarreau | a560c21 | 2012-03-09 13:50:57 +0100 | [diff] [blame] | 5969 | http_msg_analyzer(msg, &txn->hdr_idx); |
Willy Tarreau | 83e3af0 | 2009-12-28 17:39:57 +0100 | [diff] [blame] | 5970 | } |
| 5971 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5972 | /* 1: we might have to print this header in debug mode */ |
| 5973 | if (unlikely((global.mode & MODE_DEBUG) && |
| 5974 | (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) && |
Willy Tarreau | 7d59e90 | 2014-10-21 19:36:09 +0200 | [diff] [blame] | 5975 | msg->msg_state >= HTTP_MSG_BODY)) { |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5976 | char *eol, *sol; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5977 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 5978 | sol = rep->buf->p; |
| 5979 | 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] | 5980 | debug_hdr("srvrep", s, sol, eol); |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5981 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5982 | sol += hdr_idx_first_pos(&txn->hdr_idx); |
| 5983 | cur_idx = hdr_idx_first_idx(&txn->hdr_idx); |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5984 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5985 | while (cur_idx) { |
| 5986 | eol = sol + txn->hdr_idx.v[cur_idx].len; |
| 5987 | debug_hdr("srvhdr", s, sol, eol); |
| 5988 | sol = eol + txn->hdr_idx.v[cur_idx].cr + 1; |
| 5989 | cur_idx = txn->hdr_idx.v[cur_idx].next; |
| 5990 | } |
| 5991 | } |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5992 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5993 | /* |
| 5994 | * Now we quickly check if we have found a full valid response. |
| 5995 | * If not so, we check the FD and buffer states before leaving. |
| 5996 | * A full response is indicated by the fact that we have seen |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 5997 | * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5998 | * responses are checked first. |
| 5999 | * |
| 6000 | * Depending on whether the client is still there or not, we |
| 6001 | * may send an error response back or not. Note that normally |
| 6002 | * we should only check for HTTP status there, and check I/O |
| 6003 | * errors somewhere else. |
| 6004 | */ |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 6005 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 6006 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) { |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 6007 | /* Invalid response */ |
| 6008 | if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) { |
| 6009 | /* we detected a parsing error. We want to archive this response |
| 6010 | * in the dedicated proxy area for later troubleshooting. |
| 6011 | */ |
| 6012 | hdr_response_bad: |
| 6013 | if (msg->msg_state == HTTP_MSG_ERROR || msg->err_pos >= 0) |
Willy Tarreau | 10e61cb | 2017-01-04 14:51:22 +0100 | [diff] [blame] | 6014 | http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->err_state, sess->fe); |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 6015 | |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 6016 | s->be->be_counters.failed_resp++; |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 6017 | if (objt_server(s->target)) { |
| 6018 | objt_server(s->target)->counters.failed_resp++; |
| 6019 | health_adjust(objt_server(s->target), HANA_STATUS_HTTP_HDRRSP); |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 6020 | } |
Willy Tarreau | 6464841 | 2010-03-05 10:41:54 +0100 | [diff] [blame] | 6021 | abort_response: |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 6022 | channel_auto_close(rep); |
Christopher Faulet | 0184ea7 | 2017-01-05 14:06:34 +0100 | [diff] [blame] | 6023 | rep->analysers &= AN_RES_FLT_END; |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 6024 | txn->status = 502; |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 6025 | s->si[1].flags |= SI_FL_NOLINGER; |
Willy Tarreau | 319f745 | 2015-01-14 20:32:59 +0100 | [diff] [blame] | 6026 | channel_truncate(rep); |
Jarno Huuskonen | 9e6906b | 2017-03-06 14:21:49 +0200 | [diff] [blame] | 6027 | http_reply_and_close(s, txn->status, http_error_message(s)); |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 6028 | |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 6029 | if (!(s->flags & SF_ERR_MASK)) |
| 6030 | s->flags |= SF_ERR_PRXCOND; |
| 6031 | if (!(s->flags & SF_FINST_MASK)) |
| 6032 | s->flags |= SF_FINST_H; |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 6033 | |
| 6034 | return 0; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 6035 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 6036 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 6037 | /* too large response does not fit in buffer. */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 6038 | else if (buffer_full(rep->buf, global.tune.maxrewrite)) { |
Willy Tarreau | fec4d89 | 2011-09-02 20:04:57 +0200 | [diff] [blame] | 6039 | if (msg->err_pos < 0) |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 6040 | msg->err_pos = rep->buf->i; |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 6041 | goto hdr_response_bad; |
| 6042 | } |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 6043 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 6044 | /* read error */ |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 6045 | else if (rep->flags & CF_READ_ERROR) { |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 6046 | if (msg->err_pos >= 0) |
Willy Tarreau | 10e61cb | 2017-01-04 14:51:22 +0100 | [diff] [blame] | 6047 | http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->err_state, sess->fe); |
Willy Tarreau | 6b726ad | 2013-12-15 19:31:37 +0100 | [diff] [blame] | 6048 | else if (txn->flags & TX_NOT_FIRST) |
| 6049 | goto abort_keep_alive; |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 6050 | |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 6051 | s->be->be_counters.failed_resp++; |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 6052 | if (objt_server(s->target)) { |
| 6053 | objt_server(s->target)->counters.failed_resp++; |
| 6054 | health_adjust(objt_server(s->target), HANA_STATUS_HTTP_READ_ERROR); |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 6055 | } |
Willy Tarreau | 461f662 | 2008-08-15 23:43:19 +0200 | [diff] [blame] | 6056 | |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 6057 | channel_auto_close(rep); |
Christopher Faulet | 0184ea7 | 2017-01-05 14:06:34 +0100 | [diff] [blame] | 6058 | rep->analysers &= AN_RES_FLT_END; |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 6059 | txn->status = 502; |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 6060 | s->si[1].flags |= SI_FL_NOLINGER; |
Willy Tarreau | 319f745 | 2015-01-14 20:32:59 +0100 | [diff] [blame] | 6061 | channel_truncate(rep); |
Jarno Huuskonen | 9e6906b | 2017-03-06 14:21:49 +0200 | [diff] [blame] | 6062 | http_reply_and_close(s, txn->status, http_error_message(s)); |
Willy Tarreau | 816b979 | 2009-09-15 21:25:21 +0200 | [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_SRVCL; |
| 6066 | if (!(s->flags & SF_FINST_MASK)) |
| 6067 | s->flags |= SF_FINST_H; |
Willy Tarreau | cebf57e | 2008-08-15 18:16:37 +0200 | [diff] [blame] | 6068 | return 0; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 6069 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 6070 | |
Willy Tarreau | 6f0a7ba | 2014-06-23 15:22:31 +0200 | [diff] [blame] | 6071 | /* read timeout : return a 504 to the client. */ |
| 6072 | else if (rep->flags & CF_READ_TIMEOUT) { |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 6073 | if (msg->err_pos >= 0) |
Willy Tarreau | 10e61cb | 2017-01-04 14:51:22 +0100 | [diff] [blame] | 6074 | http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->err_state, sess->fe); |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 6075 | |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 6076 | s->be->be_counters.failed_resp++; |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 6077 | if (objt_server(s->target)) { |
| 6078 | objt_server(s->target)->counters.failed_resp++; |
| 6079 | health_adjust(objt_server(s->target), HANA_STATUS_HTTP_READ_TIMEOUT); |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 6080 | } |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 6081 | |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 6082 | channel_auto_close(rep); |
Christopher Faulet | 0184ea7 | 2017-01-05 14:06:34 +0100 | [diff] [blame] | 6083 | rep->analysers &= AN_RES_FLT_END; |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 6084 | txn->status = 504; |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 6085 | s->si[1].flags |= SI_FL_NOLINGER; |
Willy Tarreau | 319f745 | 2015-01-14 20:32:59 +0100 | [diff] [blame] | 6086 | channel_truncate(rep); |
Jarno Huuskonen | 9e6906b | 2017-03-06 14:21:49 +0200 | [diff] [blame] | 6087 | http_reply_and_close(s, txn->status, http_error_message(s)); |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 6088 | |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 6089 | if (!(s->flags & SF_ERR_MASK)) |
| 6090 | s->flags |= SF_ERR_SRVTO; |
| 6091 | if (!(s->flags & SF_FINST_MASK)) |
| 6092 | s->flags |= SF_FINST_H; |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 6093 | return 0; |
| 6094 | } |
Willy Tarreau | a7c5276 | 2008-08-16 18:40:18 +0200 | [diff] [blame] | 6095 | |
Willy Tarreau | f003d37 | 2012-11-26 13:35:37 +0100 | [diff] [blame] | 6096 | /* client abort with an abortonclose */ |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 6097 | 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] | 6098 | sess->fe->fe_counters.cli_aborts++; |
Willy Tarreau | f003d37 | 2012-11-26 13:35:37 +0100 | [diff] [blame] | 6099 | s->be->be_counters.cli_aborts++; |
| 6100 | if (objt_server(s->target)) |
| 6101 | objt_server(s->target)->counters.cli_aborts++; |
| 6102 | |
Christopher Faulet | 0184ea7 | 2017-01-05 14:06:34 +0100 | [diff] [blame] | 6103 | rep->analysers &= AN_RES_FLT_END; |
Willy Tarreau | f003d37 | 2012-11-26 13:35:37 +0100 | [diff] [blame] | 6104 | channel_auto_close(rep); |
| 6105 | |
| 6106 | txn->status = 400; |
Willy Tarreau | 319f745 | 2015-01-14 20:32:59 +0100 | [diff] [blame] | 6107 | channel_truncate(rep); |
Jarno Huuskonen | 9e6906b | 2017-03-06 14:21:49 +0200 | [diff] [blame] | 6108 | http_reply_and_close(s, txn->status, http_error_message(s)); |
Willy Tarreau | f003d37 | 2012-11-26 13:35:37 +0100 | [diff] [blame] | 6109 | |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 6110 | if (!(s->flags & SF_ERR_MASK)) |
| 6111 | s->flags |= SF_ERR_CLICL; |
| 6112 | if (!(s->flags & SF_FINST_MASK)) |
| 6113 | s->flags |= SF_FINST_H; |
Willy Tarreau | f003d37 | 2012-11-26 13:35:37 +0100 | [diff] [blame] | 6114 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 6115 | /* process_stream() will take care of the error */ |
Willy Tarreau | f003d37 | 2012-11-26 13:35:37 +0100 | [diff] [blame] | 6116 | return 0; |
| 6117 | } |
| 6118 | |
Willy Tarreau | 3b8c08a | 2011-09-02 20:16:24 +0200 | [diff] [blame] | 6119 | /* 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] | 6120 | else if (rep->flags & CF_SHUTR) { |
Willy Tarreau | 3b8c08a | 2011-09-02 20:16:24 +0200 | [diff] [blame] | 6121 | if (msg->msg_state >= HTTP_MSG_RPVER || msg->err_pos >= 0) |
Willy Tarreau | 10e61cb | 2017-01-04 14:51:22 +0100 | [diff] [blame] | 6122 | http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->err_state, sess->fe); |
Willy Tarreau | 6b726ad | 2013-12-15 19:31:37 +0100 | [diff] [blame] | 6123 | else if (txn->flags & TX_NOT_FIRST) |
| 6124 | goto abort_keep_alive; |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 6125 | |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 6126 | s->be->be_counters.failed_resp++; |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 6127 | if (objt_server(s->target)) { |
| 6128 | objt_server(s->target)->counters.failed_resp++; |
| 6129 | health_adjust(objt_server(s->target), HANA_STATUS_HTTP_BROKEN_PIPE); |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 6130 | } |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 6131 | |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 6132 | channel_auto_close(rep); |
Christopher Faulet | 0184ea7 | 2017-01-05 14:06:34 +0100 | [diff] [blame] | 6133 | rep->analysers &= AN_RES_FLT_END; |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 6134 | txn->status = 502; |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 6135 | s->si[1].flags |= SI_FL_NOLINGER; |
Willy Tarreau | 319f745 | 2015-01-14 20:32:59 +0100 | [diff] [blame] | 6136 | channel_truncate(rep); |
Jarno Huuskonen | 9e6906b | 2017-03-06 14:21:49 +0200 | [diff] [blame] | 6137 | http_reply_and_close(s, txn->status, http_error_message(s)); |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 6138 | |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 6139 | if (!(s->flags & SF_ERR_MASK)) |
| 6140 | s->flags |= SF_ERR_SRVCL; |
| 6141 | if (!(s->flags & SF_FINST_MASK)) |
| 6142 | s->flags |= SF_FINST_H; |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 6143 | return 0; |
| 6144 | } |
Krzysztof Piotr Oledzki | 5fb1882 | 2009-10-13 21:14:09 +0200 | [diff] [blame] | 6145 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 6146 | /* write error to client (we don't send any message then) */ |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 6147 | else if (rep->flags & CF_WRITE_ERROR) { |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 6148 | if (msg->err_pos >= 0) |
Willy Tarreau | 10e61cb | 2017-01-04 14:51:22 +0100 | [diff] [blame] | 6149 | http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->err_state, sess->fe); |
Willy Tarreau | 6b726ad | 2013-12-15 19:31:37 +0100 | [diff] [blame] | 6150 | else if (txn->flags & TX_NOT_FIRST) |
| 6151 | goto abort_keep_alive; |
Krzysztof Piotr Oledzki | 5fb1882 | 2009-10-13 21:14:09 +0200 | [diff] [blame] | 6152 | |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 6153 | s->be->be_counters.failed_resp++; |
Christopher Faulet | 0184ea7 | 2017-01-05 14:06:34 +0100 | [diff] [blame] | 6154 | rep->analysers &= AN_RES_FLT_END; |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 6155 | channel_auto_close(rep); |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 6156 | |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 6157 | if (!(s->flags & SF_ERR_MASK)) |
| 6158 | s->flags |= SF_ERR_CLICL; |
| 6159 | if (!(s->flags & SF_FINST_MASK)) |
| 6160 | s->flags |= SF_FINST_H; |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 6161 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 6162 | /* process_stream() will take care of the error */ |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 6163 | return 0; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 6164 | } |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 6165 | |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 6166 | channel_dont_close(rep); |
Willy Tarreau | 3f3997e | 2013-12-15 15:21:32 +0100 | [diff] [blame] | 6167 | rep->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */ |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 6168 | return 0; |
| 6169 | } |
| 6170 | |
| 6171 | /* More interesting part now : we know that we have a complete |
| 6172 | * response which at least looks like HTTP. We have an indicator |
| 6173 | * of each header's length, so we can parse them quickly. |
| 6174 | */ |
| 6175 | |
| 6176 | if (unlikely(msg->err_pos >= 0)) |
Willy Tarreau | 10e61cb | 2017-01-04 14:51:22 +0100 | [diff] [blame] | 6177 | http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->err_state, sess->fe); |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 6178 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 6179 | /* |
| 6180 | * 1: get the status code |
| 6181 | */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 6182 | n = rep->buf->p[msg->sl.st.c] - '0'; |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 6183 | if (n < 1 || n > 5) |
| 6184 | n = 0; |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 6185 | /* when the client triggers a 4xx from the server, it's most often due |
| 6186 | * to a missing object or permission. These events should be tracked |
| 6187 | * because if they happen often, it may indicate a brute force or a |
| 6188 | * vulnerability scan. |
| 6189 | */ |
| 6190 | if (n == 4) |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 6191 | stream_inc_http_err_ctr(s); |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 6192 | |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 6193 | if (objt_server(s->target)) |
| 6194 | objt_server(s->target)->counters.p.http.rsp[n]++; |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 6195 | |
Willy Tarreau | 91852eb | 2015-05-01 13:26:00 +0200 | [diff] [blame] | 6196 | /* RFC7230#2.6 has enforced the format of the HTTP version string to be |
| 6197 | * exactly one digit "." one digit. This check may be disabled using |
| 6198 | * option accept-invalid-http-response. |
| 6199 | */ |
| 6200 | if (!(s->be->options2 & PR_O2_RSPBUG_OK)) { |
| 6201 | if (msg->sl.st.v_l != 8) { |
| 6202 | msg->err_pos = 0; |
| 6203 | goto hdr_response_bad; |
| 6204 | } |
| 6205 | |
| 6206 | if (rep->buf->p[4] != '/' || |
| 6207 | !isdigit((unsigned char)rep->buf->p[5]) || |
| 6208 | rep->buf->p[6] != '.' || |
| 6209 | !isdigit((unsigned char)rep->buf->p[7])) { |
| 6210 | msg->err_pos = 4; |
| 6211 | goto hdr_response_bad; |
| 6212 | } |
| 6213 | } |
| 6214 | |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 6215 | /* check if the response is HTTP/1.1 or above */ |
| 6216 | if ((msg->sl.st.v_l == 8) && |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 6217 | ((rep->buf->p[5] > '1') || |
| 6218 | ((rep->buf->p[5] == '1') && (rep->buf->p[7] >= '1')))) |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 6219 | msg->flags |= HTTP_MSGF_VER_11; |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 6220 | |
| 6221 | /* "connection" has not been parsed yet */ |
Willy Tarreau | 50fc777 | 2012-11-11 22:19:57 +0100 | [diff] [blame] | 6222 | 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] | 6223 | |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 6224 | /* transfer length unknown*/ |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 6225 | msg->flags &= ~HTTP_MSGF_XFER_LEN; |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 6226 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 6227 | 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] | 6228 | |
Willy Tarreau | 3965040 | 2010-03-15 19:44:39 +0100 | [diff] [blame] | 6229 | /* Adjust server's health based on status code. Note: status codes 501 |
| 6230 | * and 505 are triggered on demand by client request, so we must not |
| 6231 | * count them as server failures. |
| 6232 | */ |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 6233 | if (objt_server(s->target)) { |
Willy Tarreau | d45b3d5 | 2010-05-20 11:49:03 +0200 | [diff] [blame] | 6234 | 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] | 6235 | health_adjust(objt_server(s->target), HANA_STATUS_HTTP_OK); |
Willy Tarreau | d45b3d5 | 2010-05-20 11:49:03 +0200 | [diff] [blame] | 6236 | else |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 6237 | health_adjust(objt_server(s->target), HANA_STATUS_HTTP_STS); |
Willy Tarreau | d45b3d5 | 2010-05-20 11:49:03 +0200 | [diff] [blame] | 6238 | } |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 6239 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 6240 | /* |
Willy Tarreau | a14ad72 | 2017-07-07 11:36:32 +0200 | [diff] [blame] | 6241 | * We may be facing a 100-continue response, or any other informational |
| 6242 | * 1xx response which is non-final, in which case this is not the right |
| 6243 | * response, and we're waiting for the next one. Let's allow this response |
| 6244 | * to go to the client and wait for the next one. There's an exception for |
| 6245 | * 101 which is used later in the code to switch protocols. |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 6246 | */ |
Willy Tarreau | a14ad72 | 2017-07-07 11:36:32 +0200 | [diff] [blame] | 6247 | if (txn->status < 200 && |
| 6248 | (txn->status == 100 || txn->status >= 102)) { |
Willy Tarreau | 628c40c | 2014-04-24 19:11:26 +0200 | [diff] [blame] | 6249 | hdr_idx_init(&txn->hdr_idx); |
| 6250 | msg->next -= channel_forward(rep, msg->next); |
| 6251 | msg->msg_state = HTTP_MSG_RPBEFORE; |
| 6252 | txn->status = 0; |
| 6253 | s->logs.t_data = -1; /* was not a response yet */ |
Christopher Faulet | 3e34429 | 2015-11-24 16:24:13 +0100 | [diff] [blame] | 6254 | FLT_STRM_CB(s, flt_http_reset(s, msg)); |
Willy Tarreau | 628c40c | 2014-04-24 19:11:26 +0200 | [diff] [blame] | 6255 | goto next_one; |
Willy Tarreau | a14ad72 | 2017-07-07 11:36:32 +0200 | [diff] [blame] | 6256 | } |
Willy Tarreau | 628c40c | 2014-04-24 19:11:26 +0200 | [diff] [blame] | 6257 | |
Willy Tarreau | a14ad72 | 2017-07-07 11:36:32 +0200 | [diff] [blame] | 6258 | /* |
| 6259 | * 2: check for cacheability. |
| 6260 | */ |
| 6261 | |
| 6262 | switch (txn->status) { |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 6263 | case 200: |
| 6264 | case 203: |
| 6265 | case 206: |
| 6266 | case 300: |
| 6267 | case 301: |
| 6268 | case 410: |
| 6269 | /* RFC2616 @13.4: |
| 6270 | * "A response received with a status code of |
| 6271 | * 200, 203, 206, 300, 301 or 410 MAY be stored |
| 6272 | * by a cache (...) unless a cache-control |
| 6273 | * directive prohibits caching." |
| 6274 | * |
| 6275 | * RFC2616 @9.5: POST method : |
| 6276 | * "Responses to this method are not cacheable, |
| 6277 | * unless the response includes appropriate |
| 6278 | * Cache-Control or Expires header fields." |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 6279 | */ |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 6280 | if (likely(txn->meth != HTTP_METH_POST) && |
Willy Tarreau | 6740213 | 2012-05-31 20:40:20 +0200 | [diff] [blame] | 6281 | ((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] | 6282 | txn->flags |= TX_CACHEABLE | TX_CACHE_COOK; |
| 6283 | break; |
| 6284 | default: |
| 6285 | break; |
| 6286 | } |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 6287 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 6288 | /* |
| 6289 | * 3: we may need to capture headers |
| 6290 | */ |
| 6291 | s->logs.logwait &= ~LW_RESP; |
Willy Tarreau | cb7dd01 | 2015-04-03 22:16:32 +0200 | [diff] [blame] | 6292 | if (unlikely((s->logs.logwait & LW_RSPHDR) && s->res_cap)) |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 6293 | capture_headers(rep->buf->p, &txn->hdr_idx, |
Willy Tarreau | cb7dd01 | 2015-04-03 22:16:32 +0200 | [diff] [blame] | 6294 | s->res_cap, sess->fe->rsp_cap); |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 6295 | |
Willy Tarreau | 557f199 | 2015-05-01 10:05:17 +0200 | [diff] [blame] | 6296 | /* 4: determine the transfer-length according to RFC2616 #4.4, updated |
| 6297 | * by RFC7230#3.3.3 : |
| 6298 | * |
| 6299 | * The length of a message body is determined by one of the following |
| 6300 | * (in order of precedence): |
| 6301 | * |
Christopher Faulet | d1cd209 | 2016-11-28 10:14:03 +0100 | [diff] [blame] | 6302 | * 1. Any 2xx (Successful) response to a CONNECT request implies that |
| 6303 | * the connection will become a tunnel immediately after the empty |
| 6304 | * line that concludes the header fields. A client MUST ignore |
| 6305 | * any Content-Length or Transfer-Encoding header fields received |
| 6306 | * in such a message. Any 101 response (Switching Protocols) is |
| 6307 | * managed in the same manner. |
| 6308 | * |
| 6309 | * 2. Any response to a HEAD request and any response with a 1xx |
Willy Tarreau | 557f199 | 2015-05-01 10:05:17 +0200 | [diff] [blame] | 6310 | * (Informational), 204 (No Content), or 304 (Not Modified) status |
| 6311 | * code is always terminated by the first empty line after the |
| 6312 | * header fields, regardless of the header fields present in the |
| 6313 | * message, and thus cannot contain a message body. |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 6314 | * |
Willy Tarreau | 557f199 | 2015-05-01 10:05:17 +0200 | [diff] [blame] | 6315 | * 3. If a Transfer-Encoding header field is present and the chunked |
| 6316 | * transfer coding (Section 4.1) is the final encoding, the message |
| 6317 | * body length is determined by reading and decoding the chunked |
| 6318 | * data until the transfer coding indicates the data is complete. |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 6319 | * |
Willy Tarreau | 557f199 | 2015-05-01 10:05:17 +0200 | [diff] [blame] | 6320 | * If a Transfer-Encoding header field is present in a response and |
| 6321 | * the chunked transfer coding is not the final encoding, the |
| 6322 | * message body length is determined by reading the connection until |
| 6323 | * it is closed by the server. If a Transfer-Encoding header field |
| 6324 | * is present in a request and the chunked transfer coding is not |
| 6325 | * the final encoding, the message body length cannot be determined |
| 6326 | * reliably; the server MUST respond with the 400 (Bad Request) |
| 6327 | * status code and then close the connection. |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 6328 | * |
Willy Tarreau | 557f199 | 2015-05-01 10:05:17 +0200 | [diff] [blame] | 6329 | * If a message is received with both a Transfer-Encoding and a |
| 6330 | * Content-Length header field, the Transfer-Encoding overrides the |
| 6331 | * Content-Length. Such a message might indicate an attempt to |
| 6332 | * perform request smuggling (Section 9.5) or response splitting |
| 6333 | * (Section 9.4) and ought to be handled as an error. A sender MUST |
| 6334 | * remove the received Content-Length field prior to forwarding such |
| 6335 | * a message downstream. |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 6336 | * |
Willy Tarreau | 557f199 | 2015-05-01 10:05:17 +0200 | [diff] [blame] | 6337 | * 4. If a message is received without Transfer-Encoding and with |
| 6338 | * either multiple Content-Length header fields having differing |
| 6339 | * field-values or a single Content-Length header field having an |
| 6340 | * invalid value, then the message framing is invalid and the |
| 6341 | * recipient MUST treat it as an unrecoverable error. If this is a |
| 6342 | * request message, the server MUST respond with a 400 (Bad Request) |
| 6343 | * status code and then close the connection. If this is a response |
| 6344 | * message received by a proxy, the proxy MUST close the connection |
| 6345 | * to the server, discard the received response, and send a 502 (Bad |
| 6346 | * Gateway) response to the client. If this is a response message |
| 6347 | * received by a user agent, the user agent MUST close the |
| 6348 | * connection to the server and discard the received response. |
| 6349 | * |
| 6350 | * 5. If a valid Content-Length header field is present without |
| 6351 | * Transfer-Encoding, its decimal value defines the expected message |
| 6352 | * body length in octets. If the sender closes the connection or |
| 6353 | * the recipient times out before the indicated number of octets are |
| 6354 | * received, the recipient MUST consider the message to be |
| 6355 | * incomplete and close the connection. |
| 6356 | * |
| 6357 | * 6. If this is a request message and none of the above are true, then |
| 6358 | * the message body length is zero (no message body is present). |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 6359 | * |
Willy Tarreau | 557f199 | 2015-05-01 10:05:17 +0200 | [diff] [blame] | 6360 | * 7. Otherwise, this is a response message without a declared message |
| 6361 | * body length, so the message body length is determined by the |
| 6362 | * number of octets received prior to the server closing the |
| 6363 | * connection. |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 6364 | */ |
| 6365 | |
| 6366 | /* Skip parsing if no content length is possible. The response flags |
Willy Tarreau | 124d991 | 2011-03-01 20:30:48 +0100 | [diff] [blame] | 6367 | * 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] | 6368 | * 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] | 6369 | * FIXME: should we parse anyway and return an error on chunked encoding ? |
| 6370 | */ |
Christopher Faulet | d1cd209 | 2016-11-28 10:14:03 +0100 | [diff] [blame] | 6371 | if (unlikely((txn->meth == HTTP_METH_CONNECT && txn->status == 200) || |
| 6372 | txn->status == 101)) { |
| 6373 | /* Either we've established an explicit tunnel, or we're |
| 6374 | * switching the protocol. In both cases, we're very unlikely |
| 6375 | * to understand the next protocols. We have to switch to tunnel |
| 6376 | * mode, so that we transfer the request and responses then let |
| 6377 | * this protocol pass unmodified. When we later implement specific |
| 6378 | * parsers for such protocols, we'll want to check the Upgrade |
| 6379 | * header which contains information about that protocol for |
| 6380 | * responses with status 101 (eg: see RFC2817 about TLS). |
| 6381 | */ |
| 6382 | txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_TUN; |
| 6383 | msg->flags |= HTTP_MSGF_XFER_LEN; |
| 6384 | goto end; |
| 6385 | } |
| 6386 | |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 6387 | if (txn->meth == HTTP_METH_HEAD || |
| 6388 | (txn->status >= 100 && txn->status < 200) || |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 6389 | txn->status == 204 || txn->status == 304) { |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 6390 | msg->flags |= HTTP_MSGF_XFER_LEN; |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 6391 | goto skip_content_length; |
| 6392 | } |
| 6393 | |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 6394 | use_close_only = 0; |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 6395 | ctx.idx = 0; |
Willy Tarreau | 4979d5c | 2015-05-01 10:06:30 +0200 | [diff] [blame] | 6396 | 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] | 6397 | if (ctx.vlen == 7 && strncasecmp(ctx.line + ctx.val, "chunked", 7) == 0) |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 6398 | msg->flags |= (HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN); |
| 6399 | else if (msg->flags & HTTP_MSGF_TE_CHNK) { |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 6400 | /* bad transfer-encoding (chunked followed by something else) */ |
| 6401 | use_close_only = 1; |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 6402 | msg->flags &= ~(HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN); |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 6403 | break; |
| 6404 | } |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 6405 | } |
| 6406 | |
Willy Tarreau | 1c91391 | 2015-04-30 10:57:51 +0200 | [diff] [blame] | 6407 | /* Chunked responses must have their content-length removed */ |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 6408 | ctx.idx = 0; |
Willy Tarreau | b4d0c03 | 2015-05-01 10:25:45 +0200 | [diff] [blame] | 6409 | if (use_close_only || (msg->flags & HTTP_MSGF_TE_CHNK)) { |
Willy Tarreau | 1c91391 | 2015-04-30 10:57:51 +0200 | [diff] [blame] | 6410 | while (http_find_header2("Content-Length", 14, rep->buf->p, &txn->hdr_idx, &ctx)) |
| 6411 | http_remove_header2(msg, &txn->hdr_idx, &ctx); |
| 6412 | } |
Willy Tarreau | b4d0c03 | 2015-05-01 10:25:45 +0200 | [diff] [blame] | 6413 | 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] | 6414 | signed long long cl; |
| 6415 | |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 6416 | if (!ctx.vlen) { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 6417 | msg->err_pos = ctx.line + ctx.val - rep->buf->p; |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 6418 | goto hdr_response_bad; |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 6419 | } |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 6420 | |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 6421 | if (strl2llrc(ctx.line + ctx.val, ctx.vlen, &cl)) { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 6422 | msg->err_pos = ctx.line + ctx.val - rep->buf->p; |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 6423 | goto hdr_response_bad; /* parse failure */ |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 6424 | } |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 6425 | |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 6426 | if (cl < 0) { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 6427 | msg->err_pos = ctx.line + ctx.val - rep->buf->p; |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 6428 | goto hdr_response_bad; |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 6429 | } |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 6430 | |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 6431 | if ((msg->flags & HTTP_MSGF_CNT_LEN) && (msg->chunk_len != cl)) { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 6432 | msg->err_pos = ctx.line + ctx.val - rep->buf->p; |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 6433 | goto hdr_response_bad; /* already specified, was different */ |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 6434 | } |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 6435 | |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 6436 | msg->flags |= HTTP_MSGF_CNT_LEN | HTTP_MSGF_XFER_LEN; |
Willy Tarreau | 124d991 | 2011-03-01 20:30:48 +0100 | [diff] [blame] | 6437 | msg->body_len = msg->chunk_len = cl; |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 6438 | } |
| 6439 | |
Christopher Faulet | d1cd209 | 2016-11-28 10:14:03 +0100 | [diff] [blame] | 6440 | skip_content_length: |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 6441 | /* Now we have to check if we need to modify the Connection header. |
| 6442 | * This is more difficult on the response than it is on the request, |
| 6443 | * because we can have two different HTTP versions and we don't know |
| 6444 | * how the client will interprete a response. For instance, let's say |
| 6445 | * that the client sends a keep-alive request in HTTP/1.0 and gets an |
| 6446 | * HTTP/1.1 response without any header. Maybe it will bound itself to |
| 6447 | * HTTP/1.0 because it only knows about it, and will consider the lack |
| 6448 | * of header as a close, or maybe it knows HTTP/1.1 and can consider |
| 6449 | * the lack of header as a keep-alive. Thus we will use two flags |
| 6450 | * indicating how a request MAY be understood by the client. In case |
| 6451 | * of multiple possibilities, we'll fix the header to be explicit. If |
| 6452 | * ambiguous cases such as both close and keepalive are seen, then we |
| 6453 | * will fall back to explicit close. Note that we won't take risks with |
| 6454 | * HTTP/1.0 clients which may not necessarily understand keep-alive. |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 6455 | * See doc/internals/connection-header.txt for the complete matrix. |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 6456 | */ |
Christopher Faulet | d1cd209 | 2016-11-28 10:14:03 +0100 | [diff] [blame] | 6457 | if ((txn->status >= 200) && !(txn->flags & TX_HDR_CONN_PRS) && |
| 6458 | ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN || |
| 6459 | ((sess->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL || |
| 6460 | (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL))) { |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 6461 | int to_del = 0; |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 6462 | |
Willy Tarreau | 70dffda | 2014-01-30 03:07:23 +0100 | [diff] [blame] | 6463 | /* this situation happens when combining pretend-keepalive with httpclose. */ |
| 6464 | if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL && |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 6465 | ((sess->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL || |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 6466 | (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL)) |
Willy Tarreau | 70dffda | 2014-01-30 03:07:23 +0100 | [diff] [blame] | 6467 | txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO; |
| 6468 | |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 6469 | /* on unknown transfer length, we must close */ |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 6470 | if (!(msg->flags & HTTP_MSGF_XFER_LEN) && |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 6471 | (txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) |
| 6472 | txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO; |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 6473 | |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 6474 | /* now adjust header transformations depending on current state */ |
| 6475 | if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN || |
| 6476 | (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) { |
| 6477 | to_del |= 2; /* remove "keep-alive" on any response */ |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 6478 | if (!(msg->flags & HTTP_MSGF_VER_11)) |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 6479 | to_del |= 1; /* remove "close" for HTTP/1.0 responses */ |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 6480 | } |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 6481 | else { /* SCL / KAL */ |
| 6482 | to_del |= 1; /* remove "close" on any response */ |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 6483 | if (txn->req.flags & msg->flags & HTTP_MSGF_VER_11) |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 6484 | to_del |= 2; /* remove "keep-alive" on pure 1.1 responses */ |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 6485 | } |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 6486 | |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 6487 | /* Parse and remove some headers from the connection header */ |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 6488 | http_parse_connection_header(txn, msg, to_del); |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 6489 | |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 6490 | /* Some keep-alive responses are converted to Server-close if |
| 6491 | * the server wants to close. |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 6492 | */ |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 6493 | if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL) { |
| 6494 | if ((txn->flags & TX_HDR_CONN_CLO) || |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 6495 | (!(txn->flags & TX_HDR_CONN_KAL) && !(msg->flags & HTTP_MSGF_VER_11))) |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 6496 | txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_SCL; |
Willy Tarreau | b608feb | 2010-01-02 22:47:18 +0100 | [diff] [blame] | 6497 | } |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 6498 | } |
| 6499 | |
Christopher Faulet | d1cd209 | 2016-11-28 10:14:03 +0100 | [diff] [blame] | 6500 | end: |
Willy Tarreau | 7959a55 | 2013-09-23 16:44:27 +0200 | [diff] [blame] | 6501 | /* we want to have the response time before we start processing it */ |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 6502 | s->logs.t_data = tv_ms_elapsed(&s->logs.tv_accept, &now); |
Willy Tarreau | 7959a55 | 2013-09-23 16:44:27 +0200 | [diff] [blame] | 6503 | |
Willy Tarreau | f118d9f | 2014-04-24 18:26:08 +0200 | [diff] [blame] | 6504 | /* end of job, return OK */ |
| 6505 | rep->analysers &= ~an_bit; |
| 6506 | rep->analyse_exp = TICK_ETERNITY; |
| 6507 | channel_auto_close(rep); |
| 6508 | return 1; |
| 6509 | |
| 6510 | abort_keep_alive: |
| 6511 | /* A keep-alive request to the server failed on a network error. |
| 6512 | * The client is required to retry. We need to close without returning |
| 6513 | * any other information so that the client retries. |
| 6514 | */ |
| 6515 | txn->status = 0; |
Christopher Faulet | 0184ea7 | 2017-01-05 14:06:34 +0100 | [diff] [blame] | 6516 | rep->analysers &= AN_RES_FLT_END; |
| 6517 | s->req.analysers &= AN_REQ_FLT_END; |
Willy Tarreau | f118d9f | 2014-04-24 18:26:08 +0200 | [diff] [blame] | 6518 | channel_auto_close(rep); |
| 6519 | s->logs.logwait = 0; |
| 6520 | s->logs.level = 0; |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 6521 | s->res.flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */ |
Willy Tarreau | 319f745 | 2015-01-14 20:32:59 +0100 | [diff] [blame] | 6522 | channel_truncate(rep); |
Christopher Faulet | a94e5a5 | 2015-12-09 15:55:06 +0100 | [diff] [blame] | 6523 | http_reply_and_close(s, txn->status, NULL); |
Willy Tarreau | f118d9f | 2014-04-24 18:26:08 +0200 | [diff] [blame] | 6524 | return 0; |
| 6525 | } |
| 6526 | |
| 6527 | /* This function performs all the processing enabled for the current response. |
| 6528 | * 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] | 6529 | * 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] | 6530 | * other functions. It works like process_request (see indications above). |
| 6531 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 6532 | 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] | 6533 | { |
Willy Tarreau | fb0afa7 | 2015-04-03 14:46:27 +0200 | [diff] [blame] | 6534 | struct session *sess = s->sess; |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 6535 | struct http_txn *txn = s->txn; |
Willy Tarreau | f118d9f | 2014-04-24 18:26:08 +0200 | [diff] [blame] | 6536 | struct http_msg *msg = &txn->rsp; |
| 6537 | struct proxy *cur_proxy; |
| 6538 | struct cond_wordlist *wl; |
Thierry FOURNIER | 9e2ef99 | 2015-02-25 13:51:19 +0100 | [diff] [blame] | 6539 | enum rule_result ret = HTTP_RULE_RES_CONT; |
Willy Tarreau | f118d9f | 2014-04-24 18:26:08 +0200 | [diff] [blame] | 6540 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 6541 | 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] | 6542 | now_ms, __FUNCTION__, |
| 6543 | s, |
| 6544 | rep, |
| 6545 | rep->rex, rep->wex, |
| 6546 | rep->flags, |
| 6547 | rep->buf->i, |
| 6548 | rep->analysers); |
| 6549 | |
| 6550 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) /* we need more data */ |
| 6551 | return 0; |
| 6552 | |
Willy Tarreau | 70730dd | 2014-04-24 18:06:27 +0200 | [diff] [blame] | 6553 | /* The stats applet needs to adjust the Connection header but we don't |
| 6554 | * apply any filter there. |
| 6555 | */ |
Willy Tarreau | 612adb8 | 2015-03-10 15:25:54 +0100 | [diff] [blame] | 6556 | if (unlikely(objt_applet(s->target) == &http_stats_applet)) { |
| 6557 | rep->analysers &= ~an_bit; |
| 6558 | rep->analyse_exp = TICK_ETERNITY; |
Willy Tarreau | 70730dd | 2014-04-24 18:06:27 +0200 | [diff] [blame] | 6559 | goto skip_filters; |
Willy Tarreau | 612adb8 | 2015-03-10 15:25:54 +0100 | [diff] [blame] | 6560 | } |
Willy Tarreau | 70730dd | 2014-04-24 18:06:27 +0200 | [diff] [blame] | 6561 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6562 | /* |
| 6563 | * We will have to evaluate the filters. |
| 6564 | * As opposed to version 1.2, now they will be evaluated in the |
| 6565 | * filters order and not in the header order. This means that |
| 6566 | * each filter has to be validated among all headers. |
| 6567 | * |
| 6568 | * Filters are tried with ->be first, then with ->fe if it is |
| 6569 | * different from ->be. |
Thierry FOURNIER | bc4c1ac | 2015-02-25 13:36:14 +0100 | [diff] [blame] | 6570 | * |
| 6571 | * Maybe we are in resume condiion. In this case I choose the |
| 6572 | * "struct proxy" which contains the rule list matching the resume |
| 6573 | * pointer. If none of theses "struct proxy" match, I initialise |
| 6574 | * the process with the first one. |
| 6575 | * |
| 6576 | * In fact, I check only correspondance betwwen the current list |
| 6577 | * pointer and the ->fe rule list. If it doesn't match, I initialize |
| 6578 | * the loop with the ->be. |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6579 | */ |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 6580 | if (s->current_rule_list == &sess->fe->http_res_rules) |
| 6581 | cur_proxy = sess->fe; |
Thierry FOURNIER | bc4c1ac | 2015-02-25 13:36:14 +0100 | [diff] [blame] | 6582 | else |
| 6583 | cur_proxy = s->be; |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6584 | while (1) { |
| 6585 | struct proxy *rule_set = cur_proxy; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 6586 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6587 | /* evaluate http-response rules */ |
Willy Tarreau | 51d861a | 2015-05-22 17:30:48 +0200 | [diff] [blame] | 6588 | if (ret == HTTP_RULE_RES_CONT) { |
Willy Tarreau | 987e3fb | 2015-04-04 01:09:08 +0200 | [diff] [blame] | 6589 | 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] | 6590 | |
Willy Tarreau | 51d861a | 2015-05-22 17:30:48 +0200 | [diff] [blame] | 6591 | if (ret == HTTP_RULE_RES_BADREQ) |
| 6592 | goto return_srv_prx_502; |
| 6593 | |
| 6594 | if (ret == HTTP_RULE_RES_DONE) { |
| 6595 | rep->analysers &= ~an_bit; |
| 6596 | rep->analyse_exp = TICK_ETERNITY; |
| 6597 | return 1; |
| 6598 | } |
| 6599 | } |
| 6600 | |
Thierry FOURNIER | bc4c1ac | 2015-02-25 13:36:14 +0100 | [diff] [blame] | 6601 | /* we need to be called again. */ |
| 6602 | if (ret == HTTP_RULE_RES_YIELD) { |
| 6603 | channel_dont_close(rep); |
| 6604 | return 0; |
| 6605 | } |
| 6606 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6607 | /* try headers filters */ |
| 6608 | if (rule_set->rsp_exp != NULL) { |
| 6609 | if (apply_filters_to_response(s, rep, rule_set) < 0) { |
| 6610 | return_bad_resp: |
| 6611 | if (objt_server(s->target)) { |
| 6612 | objt_server(s->target)->counters.failed_resp++; |
| 6613 | health_adjust(objt_server(s->target), HANA_STATUS_HTTP_RSP); |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 6614 | } |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6615 | s->be->be_counters.failed_resp++; |
| 6616 | return_srv_prx_502: |
Christopher Faulet | 0184ea7 | 2017-01-05 14:06:34 +0100 | [diff] [blame] | 6617 | rep->analysers &= AN_RES_FLT_END; |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6618 | txn->status = 502; |
| 6619 | s->logs.t_data = -1; /* was not a valid response */ |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 6620 | s->si[1].flags |= SI_FL_NOLINGER; |
Willy Tarreau | 319f745 | 2015-01-14 20:32:59 +0100 | [diff] [blame] | 6621 | channel_truncate(rep); |
Jarno Huuskonen | 9e6906b | 2017-03-06 14:21:49 +0200 | [diff] [blame] | 6622 | http_reply_and_close(s, txn->status, http_error_message(s)); |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 6623 | if (!(s->flags & SF_ERR_MASK)) |
| 6624 | s->flags |= SF_ERR_PRXCOND; |
| 6625 | if (!(s->flags & SF_FINST_MASK)) |
| 6626 | s->flags |= SF_FINST_H; |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6627 | return 0; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 6628 | } |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6629 | } |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 6630 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6631 | /* has the response been denied ? */ |
| 6632 | if (txn->flags & TX_SVDENY) { |
| 6633 | if (objt_server(s->target)) |
| 6634 | objt_server(s->target)->counters.failed_secu++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 6635 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6636 | s->be->be_counters.denied_resp++; |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 6637 | sess->fe->fe_counters.denied_resp++; |
Willy Tarreau | fb0afa7 | 2015-04-03 14:46:27 +0200 | [diff] [blame] | 6638 | if (sess->listener->counters) |
| 6639 | sess->listener->counters->denied_resp++; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 6640 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6641 | goto return_srv_prx_502; |
| 6642 | } |
Willy Tarreau | 0bbc3cf | 2006-10-15 14:26:02 +0200 | [diff] [blame] | 6643 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6644 | /* add response headers from the rule sets in the same order */ |
| 6645 | list_for_each_entry(wl, &rule_set->rsp_add, list) { |
Willy Tarreau | ce730de | 2014-09-16 10:40:38 +0200 | [diff] [blame] | 6646 | if (txn->status < 200 && txn->status != 101) |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 6647 | break; |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6648 | if (wl->cond) { |
Willy Tarreau | 192252e | 2015-04-04 01:47:55 +0200 | [diff] [blame] | 6649 | 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] | 6650 | ret = acl_pass(ret); |
| 6651 | if (((struct acl_cond *)wl->cond)->pol == ACL_COND_UNLESS) |
| 6652 | ret = !ret; |
| 6653 | if (!ret) |
| 6654 | continue; |
| 6655 | } |
| 6656 | if (unlikely(http_header_add_tail(&txn->rsp, &txn->hdr_idx, wl->s) < 0)) |
| 6657 | goto return_bad_resp; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 6658 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 6659 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6660 | /* check whether we're already working on the frontend */ |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 6661 | if (cur_proxy == sess->fe) |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6662 | break; |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 6663 | cur_proxy = sess->fe; |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6664 | } |
Willy Tarreau | 63c9e5f | 2009-12-22 16:01:27 +0100 | [diff] [blame] | 6665 | |
Thierry FOURNIER | bc4c1ac | 2015-02-25 13:36:14 +0100 | [diff] [blame] | 6666 | /* After this point, this anayzer can't return yield, so we can |
| 6667 | * remove the bit corresponding to this analyzer from the list. |
| 6668 | * |
| 6669 | * Note that the intermediate returns and goto found previously |
| 6670 | * reset the analyzers. |
| 6671 | */ |
| 6672 | rep->analysers &= ~an_bit; |
| 6673 | rep->analyse_exp = TICK_ETERNITY; |
| 6674 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6675 | /* OK that's all we can do for 1xx responses */ |
Willy Tarreau | ce730de | 2014-09-16 10:40:38 +0200 | [diff] [blame] | 6676 | if (unlikely(txn->status < 200 && txn->status != 101)) |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6677 | goto skip_header_mangling; |
Willy Tarreau | 63c9e5f | 2009-12-22 16:01:27 +0100 | [diff] [blame] | 6678 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6679 | /* |
| 6680 | * Now check for a server cookie. |
| 6681 | */ |
Willy Tarreau | 53a09d5 | 2015-08-10 18:59:40 +0200 | [diff] [blame] | 6682 | 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] | 6683 | manage_server_side_cookies(s, rep); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 6684 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6685 | /* |
| 6686 | * Check for cache-control or pragma headers if required. |
| 6687 | */ |
Willy Tarreau | ce730de | 2014-09-16 10:40:38 +0200 | [diff] [blame] | 6688 | 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] | 6689 | check_response_for_cacheability(s, rep); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 6690 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6691 | /* |
| 6692 | * Add server cookie in the response if needed |
| 6693 | */ |
| 6694 | if (objt_server(s->target) && (s->be->ck_opts & PR_CK_INS) && |
| 6695 | !((txn->flags & TX_SCK_FOUND) && (s->be->ck_opts & PR_CK_PSV)) && |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 6696 | (!(s->flags & SF_DIRECT) || |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6697 | ((s->be->cookie_maxidle || txn->cookie_last_date) && |
| 6698 | (!txn->cookie_last_date || (txn->cookie_last_date - date.tv_sec) < 0)) || |
| 6699 | (s->be->cookie_maxlife && !txn->cookie_first_date) || // set the first_date |
| 6700 | (!s->be->cookie_maxlife && txn->cookie_first_date)) && // remove the first_date |
| 6701 | (!(s->be->ck_opts & PR_CK_POST) || (txn->meth == HTTP_METH_POST)) && |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 6702 | !(s->flags & SF_IGNORE_PRST)) { |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6703 | /* the server is known, it's not the one the client requested, or the |
| 6704 | * cookie's last seen date needs to be refreshed. We have to |
| 6705 | * insert a set-cookie here, except if we want to insert only on POST |
| 6706 | * requests and this one isn't. Note that servers which don't have cookies |
| 6707 | * (eg: some backup servers) will return a full cookie removal request. |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6708 | */ |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6709 | if (!objt_server(s->target)->cookie) { |
| 6710 | chunk_printf(&trash, |
| 6711 | "Set-Cookie: %s=; Expires=Thu, 01-Jan-1970 00:00:01 GMT; path=/", |
| 6712 | s->be->cookie_name); |
| 6713 | } |
| 6714 | else { |
| 6715 | 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] | 6716 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6717 | if (s->be->cookie_maxidle || s->be->cookie_maxlife) { |
| 6718 | /* emit last_date, which is mandatory */ |
| 6719 | trash.str[trash.len++] = COOKIE_DELIM_DATE; |
| 6720 | s30tob64((date.tv_sec+3) >> 2, trash.str + trash.len); |
| 6721 | trash.len += 5; |
Willy Tarreau | ef4f391 | 2010-10-07 21:00:29 +0200 | [diff] [blame] | 6722 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6723 | if (s->be->cookie_maxlife) { |
| 6724 | /* emit first_date, which is either the original one or |
| 6725 | * the current date. |
| 6726 | */ |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 6727 | trash.str[trash.len++] = COOKIE_DELIM_DATE; |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6728 | s30tob64(txn->cookie_first_date ? |
| 6729 | txn->cookie_first_date >> 2 : |
| 6730 | (date.tv_sec+3) >> 2, trash.str + trash.len); |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 6731 | trash.len += 5; |
Willy Tarreau | ef4f391 | 2010-10-07 21:00:29 +0200 | [diff] [blame] | 6732 | } |
Willy Tarreau | ef4f391 | 2010-10-07 21:00:29 +0200 | [diff] [blame] | 6733 | } |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6734 | chunk_appendf(&trash, "; path=/"); |
| 6735 | } |
Willy Tarreau | 4992dd2 | 2012-05-31 21:02:17 +0200 | [diff] [blame] | 6736 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6737 | if (s->be->cookie_domain) |
| 6738 | chunk_appendf(&trash, "; domain=%s", s->be->cookie_domain); |
Willy Tarreau | ef4f391 | 2010-10-07 21:00:29 +0200 | [diff] [blame] | 6739 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6740 | if (s->be->ck_opts & PR_CK_HTTPONLY) |
| 6741 | chunk_appendf(&trash, "; HttpOnly"); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 6742 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6743 | if (s->be->ck_opts & PR_CK_SECURE) |
| 6744 | chunk_appendf(&trash, "; Secure"); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 6745 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6746 | if (unlikely(http_header_add_tail2(&txn->rsp, &txn->hdr_idx, trash.str, trash.len) < 0)) |
| 6747 | goto return_bad_resp; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 6748 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6749 | txn->flags &= ~TX_SCK_MASK; |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 6750 | if (objt_server(s->target)->cookie && (s->flags & SF_DIRECT)) |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6751 | /* the server did not change, only the date was updated */ |
| 6752 | txn->flags |= TX_SCK_UPDATED; |
| 6753 | else |
| 6754 | txn->flags |= TX_SCK_INSERTED; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 6755 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6756 | /* Here, we will tell an eventual cache on the client side that we don't |
| 6757 | * want it to cache this reply because HTTP/1.0 caches also cache cookies ! |
| 6758 | * Some caches understand the correct form: 'no-cache="set-cookie"', but |
| 6759 | * 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] | 6760 | */ |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6761 | if ((s->be->ck_opts & PR_CK_NOC) && (txn->flags & TX_CACHEABLE)) { |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 6762 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6763 | txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 6764 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6765 | if (unlikely(http_header_add_tail2(&txn->rsp, &txn->hdr_idx, |
| 6766 | "Cache-control: private", 22) < 0)) |
| 6767 | goto return_bad_resp; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 6768 | } |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6769 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6770 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6771 | /* |
| 6772 | * Check if result will be cacheable with a cookie. |
| 6773 | * We'll block the response if security checks have caught |
| 6774 | * nasty things such as a cacheable cookie. |
| 6775 | */ |
| 6776 | if (((txn->flags & (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) == |
| 6777 | (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) && |
| 6778 | (s->be->options & PR_O_CHK_CACHE)) { |
| 6779 | /* we're in presence of a cacheable response containing |
| 6780 | * a set-cookie header. We'll block it as requested by |
| 6781 | * the 'checkcache' option, and send an alert. |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6782 | */ |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6783 | if (objt_server(s->target)) |
| 6784 | objt_server(s->target)->counters.failed_secu++; |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 6785 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6786 | s->be->be_counters.denied_resp++; |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 6787 | sess->fe->fe_counters.denied_resp++; |
Willy Tarreau | fb0afa7 | 2015-04-03 14:46:27 +0200 | [diff] [blame] | 6788 | if (sess->listener->counters) |
| 6789 | sess->listener->counters->denied_resp++; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6790 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6791 | Alert("Blocking cacheable cookie in response from instance %s, server %s.\n", |
| 6792 | s->be->id, objt_server(s->target) ? objt_server(s->target)->id : "<dispatch>"); |
| 6793 | send_log(s->be, LOG_ALERT, |
| 6794 | "Blocking cacheable cookie in response from instance %s, server %s.\n", |
| 6795 | s->be->id, objt_server(s->target) ? objt_server(s->target)->id : "<dispatch>"); |
| 6796 | goto return_srv_prx_502; |
| 6797 | } |
Willy Tarreau | 0394594 | 2009-12-22 16:50:27 +0100 | [diff] [blame] | 6798 | |
Willy Tarreau | 70730dd | 2014-04-24 18:06:27 +0200 | [diff] [blame] | 6799 | skip_filters: |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6800 | /* |
| 6801 | * Adjust "Connection: close" or "Connection: keep-alive" if needed. |
| 6802 | * If an "Upgrade" token is found, the header is left untouched in order |
| 6803 | * 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] | 6804 | * if anything but "Upgrade" is present in the Connection header. We don't |
| 6805 | * want to touch any 101 response either since it's switching to another |
| 6806 | * protocol. |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6807 | */ |
Willy Tarreau | ce730de | 2014-09-16 10:40:38 +0200 | [diff] [blame] | 6808 | if ((txn->status != 101) && !(txn->flags & TX_HDR_CONN_UPG) && |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6809 | (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) || |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 6810 | ((sess->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL || |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6811 | (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL))) { |
| 6812 | unsigned int want_flags = 0; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 6813 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6814 | if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL || |
| 6815 | (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) { |
| 6816 | /* we want a keep-alive response here. Keep-alive header |
| 6817 | * required if either side is not 1.1. |
| 6818 | */ |
| 6819 | if (!(txn->req.flags & msg->flags & HTTP_MSGF_VER_11)) |
| 6820 | want_flags |= TX_CON_KAL_SET; |
| 6821 | } |
| 6822 | else { |
| 6823 | /* we want a close response here. Close header required if |
| 6824 | * the server is 1.1, regardless of the client. |
| 6825 | */ |
| 6826 | if (msg->flags & HTTP_MSGF_VER_11) |
| 6827 | want_flags |= TX_CON_CLO_SET; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 6828 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6829 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6830 | if (want_flags != (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET))) |
| 6831 | http_change_connection_header(txn, msg, want_flags); |
| 6832 | } |
| 6833 | |
| 6834 | skip_header_mangling: |
Christopher Faulet | 69744d9 | 2017-03-30 10:54:35 +0200 | [diff] [blame] | 6835 | /* Always enter in the body analyzer */ |
| 6836 | rep->analysers &= ~AN_RES_FLT_XFER_DATA; |
| 6837 | rep->analysers |= AN_RES_HTTP_XFER_BODY; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6838 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6839 | /* if the user wants to log as soon as possible, without counting |
| 6840 | * bytes from the server, then this is the right moment. We have |
| 6841 | * to temporarily assign bytes_out to log what we currently have. |
| 6842 | */ |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 6843 | if (!LIST_ISEMPTY(&sess->fe->logformat) && !(s->logs.logwait & LW_BYTES)) { |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6844 | s->logs.t_close = s->logs.t_data; /* to get a valid end date */ |
| 6845 | s->logs.bytes_out = txn->rsp.eoh; |
| 6846 | s->do_log(s); |
| 6847 | s->logs.bytes_out = 0; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 6848 | } |
Willy Tarreau | e3fa6e5 | 2010-01-04 22:57:43 +0100 | [diff] [blame] | 6849 | return 1; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 6850 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6851 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 6852 | /* This function is an analyser which forwards response body (including chunk |
| 6853 | * sizes if any). It is called as soon as we must forward, even if we forward |
| 6854 | * zero byte. The only situation where it must not be called is when we're in |
| 6855 | * tunnel mode and we want to forward till the close. It's used both to forward |
| 6856 | * remaining data and to resync after end of body. It expects the msg_state to |
| 6857 | * 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] | 6858 | * 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] | 6859 | * |
| 6860 | * It is capable of compressing response data both in content-length mode and |
| 6861 | * in chunked mode. The state machines follows different flows depending on |
| 6862 | * whether content-length and chunked modes are used, since there are no |
| 6863 | * trailers in content-length : |
| 6864 | * |
| 6865 | * chk-mode cl-mode |
| 6866 | * ,----- BODY -----. |
| 6867 | * / \ |
| 6868 | * V size > 0 V chk-mode |
| 6869 | * .--> SIZE -------------> DATA -------------> CRLF |
| 6870 | * | | size == 0 | last byte | |
| 6871 | * | v final crlf v inspected | |
| 6872 | * | TRAILERS -----------> DONE | |
| 6873 | * | | |
| 6874 | * `----------------------------------------------' |
| 6875 | * |
| 6876 | * Compression only happens in the DATA state, and must be flushed in final |
| 6877 | * states (TRAILERS/DONE) or when leaving on missing data. Normal forwarding |
| 6878 | * is performed at once on final states for all bytes parsed, or when leaving |
| 6879 | * on missing data. |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 6880 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 6881 | 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] | 6882 | { |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 6883 | struct session *sess = s->sess; |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 6884 | struct http_txn *txn = s->txn; |
| 6885 | struct http_msg *msg = &s->txn->rsp; |
Christopher Faulet | 3e34429 | 2015-11-24 16:24:13 +0100 | [diff] [blame] | 6886 | int ret; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 6887 | |
Christopher Faulet | 814d270 | 2017-03-30 11:33:44 +0200 | [diff] [blame] | 6888 | DPRINTF(stderr,"[%u] %s: stream=%p b=%p, exp(r,w)=%u,%u bf=%08x bh=%d analysers=%02x\n", |
| 6889 | now_ms, __FUNCTION__, |
| 6890 | s, |
| 6891 | res, |
| 6892 | res->rex, res->wex, |
| 6893 | res->flags, |
| 6894 | res->buf->i, |
| 6895 | res->analysers); |
| 6896 | |
Willy Tarreau | f5c8bd6 | 2010-01-04 07:10:34 +0100 | [diff] [blame] | 6897 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) |
| 6898 | return 0; |
| 6899 | |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 6900 | 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] | 6901 | ((res->flags & CF_SHUTW) && (res->to_forward || res->buf->o)) || |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 6902 | !s->req.analysers) { |
Willy Tarreau | 4fe4190 | 2010-06-07 22:27:41 +0200 | [diff] [blame] | 6903 | /* Output closed while we were sending data. We must abort and |
| 6904 | * wake the other side up. |
| 6905 | */ |
Willy Tarreau | 10e61cb | 2017-01-04 14:51:22 +0100 | [diff] [blame] | 6906 | msg->err_state = msg->msg_state; |
Willy Tarreau | 4fe4190 | 2010-06-07 22:27:41 +0200 | [diff] [blame] | 6907 | msg->msg_state = HTTP_MSG_ERROR; |
| 6908 | http_resync_states(s); |
Willy Tarreau | 082b01c | 2010-01-02 23:58:04 +0100 | [diff] [blame] | 6909 | return 1; |
| 6910 | } |
| 6911 | |
Willy Tarreau | 4fe4190 | 2010-06-07 22:27:41 +0200 | [diff] [blame] | 6912 | /* in most states, we should abort in case of early close */ |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 6913 | channel_auto_close(res); |
Willy Tarreau | b608feb | 2010-01-02 22:47:18 +0100 | [diff] [blame] | 6914 | |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 6915 | if (msg->msg_state == HTTP_MSG_BODY) { |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 6916 | msg->msg_state = ((msg->flags & HTTP_MSGF_TE_CHNK) |
| 6917 | ? HTTP_MSG_CHUNK_SIZE |
| 6918 | : HTTP_MSG_DATA); |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 6919 | } |
| 6920 | |
Willy Tarreau | efdf094 | 2014-04-24 20:08:57 +0200 | [diff] [blame] | 6921 | if (res->to_forward) { |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 6922 | /* We can't process the buffer's contents yet */ |
Willy Tarreau | efdf094 | 2014-04-24 20:08:57 +0200 | [diff] [blame] | 6923 | res->flags |= CF_WAKE_WRITE; |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 6924 | goto missing_data_or_waiting; |
Willy Tarreau | efdf094 | 2014-04-24 20:08:57 +0200 | [diff] [blame] | 6925 | } |
| 6926 | |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 6927 | if (msg->msg_state < HTTP_MSG_DONE) { |
| 6928 | ret = ((msg->flags & HTTP_MSGF_TE_CHNK) |
| 6929 | ? http_msg_forward_chunked_body(s, msg) |
| 6930 | : http_msg_forward_body(s, msg)); |
| 6931 | if (!ret) |
| 6932 | goto missing_data_or_waiting; |
| 6933 | if (ret < 0) |
| 6934 | goto return_bad_res; |
| 6935 | } |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 6936 | |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 6937 | /* other states, DONE...TUNNEL */ |
| 6938 | /* for keep-alive we don't want to forward closes on DONE */ |
| 6939 | if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL || |
| 6940 | (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) |
| 6941 | channel_dont_close(res); |
Willy Tarreau | 3ce10ff | 2014-04-22 08:24:38 +0200 | [diff] [blame] | 6942 | |
Christopher Faulet | 894da4c | 2017-07-18 11:29:07 +0200 | [diff] [blame] | 6943 | http_resync_states(s); |
| 6944 | if (!(res->analysers & an_bit)) { |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 6945 | if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) { |
| 6946 | if (res->flags & CF_SHUTW) { |
| 6947 | /* response errors are most likely due to the |
| 6948 | * client aborting the transfer. */ |
| 6949 | goto aborted_xfer; |
Willy Tarreau | 5523b32 | 2009-12-29 12:05:52 +0100 | [diff] [blame] | 6950 | } |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 6951 | if (msg->err_pos >= 0) |
Willy Tarreau | 10e61cb | 2017-01-04 14:51:22 +0100 | [diff] [blame] | 6952 | http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->err_state, strm_fe(s)); |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 6953 | goto return_bad_res; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 6954 | } |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 6955 | return 1; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 6956 | } |
Willy Tarreau | f51d03c | 2016-05-02 15:25:15 +0200 | [diff] [blame] | 6957 | return 0; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 6958 | |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 6959 | missing_data_or_waiting: |
Willy Tarreau | f003d37 | 2012-11-26 13:35:37 +0100 | [diff] [blame] | 6960 | if (res->flags & CF_SHUTW) |
| 6961 | goto aborted_xfer; |
| 6962 | |
| 6963 | /* stop waiting for data if the input is closed before the end. If the |
| 6964 | * client side was already closed, it means that the client has aborted, |
| 6965 | * so we don't want to count this as a server abort. Otherwise it's a |
| 6966 | * server abort. |
| 6967 | */ |
Christopher Faulet | a33510b | 2017-03-31 15:37:29 +0200 | [diff] [blame] | 6968 | if (msg->msg_state < HTTP_MSG_ENDING && res->flags & CF_SHUTR) { |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 6969 | if ((s->req.flags & (CF_SHUTR|CF_SHUTW)) == (CF_SHUTR|CF_SHUTW)) |
Willy Tarreau | f003d37 | 2012-11-26 13:35:37 +0100 | [diff] [blame] | 6970 | goto aborted_xfer; |
Christopher Faulet | a46bbd8 | 2015-06-19 09:00:58 +0200 | [diff] [blame] | 6971 | /* If we have some pending data, we continue the processing */ |
| 6972 | if (!buffer_pending(res->buf)) { |
| 6973 | if (!(s->flags & SF_ERR_MASK)) |
| 6974 | s->flags |= SF_ERR_SRVCL; |
| 6975 | s->be->be_counters.srv_aborts++; |
| 6976 | if (objt_server(s->target)) |
| 6977 | objt_server(s->target)->counters.srv_aborts++; |
| 6978 | goto return_bad_res_stats_ok; |
| 6979 | } |
Willy Tarreau | 40dba09 | 2010-03-04 18:14:51 +0100 | [diff] [blame] | 6980 | } |
Willy Tarreau | f5c8bd6 | 2010-01-04 07:10:34 +0100 | [diff] [blame] | 6981 | |
Willy Tarreau | 40dba09 | 2010-03-04 18:14:51 +0100 | [diff] [blame] | 6982 | /* 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] | 6983 | if (!s->req.analysers) |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 6984 | goto return_bad_res; |
| 6985 | |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 6986 | /* When TE: chunked is used, we need to get there again to parse |
| 6987 | * remaining chunks even if the server has closed, so we don't want to |
Christopher Faulet | 69744d9 | 2017-03-30 10:54:35 +0200 | [diff] [blame] | 6988 | * set CF_DONTCLOSE. Similarly, if keep-alive is set on the client side |
| 6989 | * or if there are filters registered on the stream, we don't want to |
| 6990 | * forward a close |
Willy Tarreau | 92aa1fa | 2010-08-28 18:57:20 +0200 | [diff] [blame] | 6991 | */ |
Christopher Faulet | 69744d9 | 2017-03-30 10:54:35 +0200 | [diff] [blame] | 6992 | if ((msg->flags & HTTP_MSGF_TE_CHNK) || |
Christopher Faulet | f1cc5d0 | 2017-02-08 09:45:13 +0100 | [diff] [blame] | 6993 | HAS_DATA_FILTERS(s, res) || |
Willy Tarreau | 92aa1fa | 2010-08-28 18:57:20 +0200 | [diff] [blame] | 6994 | (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL || |
| 6995 | (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 6996 | channel_dont_close(res); |
Willy Tarreau | 92aa1fa | 2010-08-28 18:57:20 +0200 | [diff] [blame] | 6997 | |
Willy Tarreau | 5c62092 | 2011-05-11 19:56:11 +0200 | [diff] [blame] | 6998 | /* 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] | 6999 | * 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] | 7000 | * 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] | 7001 | * modes are already handled by the stream sock layer. We must not do |
| 7002 | * this in content-length mode because it could present the MSG_MORE |
| 7003 | * flag with the last block of forwarded data, which would cause an |
| 7004 | * additional delay to be observed by the receiver. |
Willy Tarreau | 5c62092 | 2011-05-11 19:56:11 +0200 | [diff] [blame] | 7005 | */ |
Christopher Faulet | 92d3638 | 2015-11-05 13:35:03 +0100 | [diff] [blame] | 7006 | if ((msg->flags & HTTP_MSGF_TE_CHNK) || (msg->flags & HTTP_MSGF_COMPRESSING)) |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 7007 | res->flags |= CF_EXPECT_MORE; |
Willy Tarreau | 5c62092 | 2011-05-11 19:56:11 +0200 | [diff] [blame] | 7008 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 7009 | /* the stream handler will take care of timeouts and errors */ |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 7010 | return 0; |
| 7011 | |
Willy Tarreau | 40dba09 | 2010-03-04 18:14:51 +0100 | [diff] [blame] | 7012 | return_bad_res: /* let's centralize all bad responses */ |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 7013 | s->be->be_counters.failed_resp++; |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 7014 | if (objt_server(s->target)) |
| 7015 | objt_server(s->target)->counters.failed_resp++; |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 7016 | |
| 7017 | return_bad_res_stats_ok: |
Willy Tarreau | 10e61cb | 2017-01-04 14:51:22 +0100 | [diff] [blame] | 7018 | txn->rsp.err_state = txn->rsp.msg_state; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 7019 | txn->rsp.msg_state = HTTP_MSG_ERROR; |
Willy Tarreau | 148d099 | 2010-01-10 10:21:21 +0100 | [diff] [blame] | 7020 | /* don't send any error message as we're in the body */ |
Christopher Faulet | a94e5a5 | 2015-12-09 15:55:06 +0100 | [diff] [blame] | 7021 | http_reply_and_close(s, txn->status, NULL); |
Christopher Faulet | 0184ea7 | 2017-01-05 14:06:34 +0100 | [diff] [blame] | 7022 | res->analysers &= AN_RES_FLT_END; |
| 7023 | s->req.analysers &= AN_REQ_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] | 7024 | if (objt_server(s->target)) |
| 7025 | health_adjust(objt_server(s->target), HANA_STATUS_HTTP_HDRRSP); |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 7026 | |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 7027 | if (!(s->flags & SF_ERR_MASK)) |
| 7028 | s->flags |= SF_ERR_PRXCOND; |
| 7029 | if (!(s->flags & SF_FINST_MASK)) |
| 7030 | s->flags |= SF_FINST_D; |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 7031 | return 0; |
| 7032 | |
| 7033 | aborted_xfer: |
Willy Tarreau | 10e61cb | 2017-01-04 14:51:22 +0100 | [diff] [blame] | 7034 | txn->rsp.err_state = txn->rsp.msg_state; |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 7035 | txn->rsp.msg_state = HTTP_MSG_ERROR; |
| 7036 | /* don't send any error message as we're in the body */ |
Christopher Faulet | a94e5a5 | 2015-12-09 15:55:06 +0100 | [diff] [blame] | 7037 | http_reply_and_close(s, txn->status, NULL); |
Christopher Faulet | 0184ea7 | 2017-01-05 14:06:34 +0100 | [diff] [blame] | 7038 | res->analysers &= AN_RES_FLT_END; |
| 7039 | s->req.analysers &= AN_REQ_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] | 7040 | |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 7041 | sess->fe->fe_counters.cli_aborts++; |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 7042 | s->be->be_counters.cli_aborts++; |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 7043 | if (objt_server(s->target)) |
| 7044 | objt_server(s->target)->counters.cli_aborts++; |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 7045 | |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 7046 | if (!(s->flags & SF_ERR_MASK)) |
| 7047 | s->flags |= SF_ERR_CLICL; |
| 7048 | if (!(s->flags & SF_FINST_MASK)) |
| 7049 | s->flags |= SF_FINST_D; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 7050 | return 0; |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 7051 | } |
| 7052 | |
| 7053 | |
| 7054 | static inline int |
| 7055 | http_msg_forward_body(struct stream *s, struct http_msg *msg) |
| 7056 | { |
| 7057 | struct channel *chn = msg->chn; |
| 7058 | int ret; |
| 7059 | |
| 7060 | /* Here we have the guarantee to be in HTTP_MSG_DATA or HTTP_MSG_ENDING state */ |
| 7061 | |
| 7062 | if (msg->msg_state == HTTP_MSG_ENDING) |
| 7063 | goto ending; |
| 7064 | |
| 7065 | /* Neither content-length, nor transfer-encoding was found, so we must |
| 7066 | * read the body until the server connection is closed. In that case, we |
| 7067 | * eat data as they come. Of course, this happens for response only. */ |
| 7068 | if (!(msg->flags & HTTP_MSGF_XFER_LEN)) { |
| 7069 | unsigned long long len = (chn->buf->i - msg->next); |
| 7070 | msg->chunk_len += len; |
| 7071 | msg->body_len += len; |
| 7072 | } |
Christopher Faulet | da02e17 | 2015-12-04 09:25:05 +0100 | [diff] [blame] | 7073 | ret = FLT_STRM_DATA_CB(s, chn, flt_http_data(s, msg), |
| 7074 | /* default_ret */ MIN(msg->chunk_len, chn->buf->i - msg->next), |
| 7075 | /* on_error */ goto error); |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 7076 | msg->next += ret; |
| 7077 | msg->chunk_len -= ret; |
| 7078 | if (msg->chunk_len) { |
| 7079 | /* input empty or output full */ |
| 7080 | if (chn->buf->i > msg->next) |
| 7081 | chn->flags |= CF_WAKE_WRITE; |
| 7082 | goto missing_data_or_waiting; |
| 7083 | } |
| 7084 | |
Christopher Faulet | 1486b0a | 2017-07-18 11:42:08 +0200 | [diff] [blame] | 7085 | /* This check can only be true for a response. HTTP_MSGF_XFER_LEN is |
| 7086 | * always set for a request. */ |
| 7087 | if (!(msg->flags & HTTP_MSGF_XFER_LEN)) { |
| 7088 | /* The server still sending data that should be filtered */ |
| 7089 | if (!(chn->flags & CF_SHUTR) && HAS_DATA_FILTERS(s, chn)) |
| 7090 | goto missing_data_or_waiting; |
| 7091 | } |
Christopher Faulet | f1cc5d0 | 2017-02-08 09:45:13 +0100 | [diff] [blame] | 7092 | |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 7093 | msg->msg_state = HTTP_MSG_ENDING; |
| 7094 | |
| 7095 | ending: |
| 7096 | /* we may have some pending data starting at res->buf->p such as a last |
| 7097 | * chunk of data or trailers. */ |
Christopher Faulet | da02e17 | 2015-12-04 09:25:05 +0100 | [diff] [blame] | 7098 | ret = FLT_STRM_DATA_CB(s, chn, flt_http_forward_data(s, msg, msg->next), |
| 7099 | /* default_ret */ msg->next, |
| 7100 | /* on_error */ goto error); |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 7101 | b_adv(chn->buf, ret); |
| 7102 | msg->next -= ret; |
Willy Tarreau | 9962f8f | 2016-06-28 11:52:08 +0200 | [diff] [blame] | 7103 | if (unlikely(!(chn->flags & CF_WROTE_DATA) || msg->sov > 0)) |
| 7104 | msg->sov -= ret; |
Christopher Faulet | a9300a3 | 2016-06-28 15:54:44 +0200 | [diff] [blame] | 7105 | if (msg->next) |
| 7106 | goto waiting; |
Willy Tarreau | 9962f8f | 2016-06-28 11:52:08 +0200 | [diff] [blame] | 7107 | |
Christopher Faulet | da02e17 | 2015-12-04 09:25:05 +0100 | [diff] [blame] | 7108 | FLT_STRM_DATA_CB(s, chn, flt_http_end(s, msg), |
| 7109 | /* default_ret */ 1, |
| 7110 | /* on_error */ goto error, |
| 7111 | /* on_wait */ goto waiting); |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 7112 | msg->msg_state = HTTP_MSG_DONE; |
| 7113 | return 1; |
| 7114 | |
| 7115 | missing_data_or_waiting: |
| 7116 | /* we may have some pending data starting at chn->buf->p */ |
Christopher Faulet | da02e17 | 2015-12-04 09:25:05 +0100 | [diff] [blame] | 7117 | ret = FLT_STRM_DATA_CB(s, chn, flt_http_forward_data(s, msg, msg->next), |
| 7118 | /* default_ret */ msg->next, |
| 7119 | /* on_error */ goto error); |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 7120 | b_adv(chn->buf, ret); |
| 7121 | msg->next -= ret; |
| 7122 | if (!(chn->flags & CF_WROTE_DATA) || msg->sov > 0) |
| 7123 | msg->sov -= ret; |
Christopher Faulet | 75e2eb6 | 2015-12-15 10:41:47 +0100 | [diff] [blame] | 7124 | if (!HAS_DATA_FILTERS(s, chn)) |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 7125 | msg->chunk_len -= channel_forward(chn, msg->chunk_len); |
Christopher Faulet | a9300a3 | 2016-06-28 15:54:44 +0200 | [diff] [blame] | 7126 | waiting: |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 7127 | return 0; |
| 7128 | error: |
| 7129 | return -1; |
| 7130 | } |
| 7131 | |
| 7132 | static inline int |
| 7133 | http_msg_forward_chunked_body(struct stream *s, struct http_msg *msg) |
| 7134 | { |
| 7135 | struct channel *chn = msg->chn; |
| 7136 | int ret; |
| 7137 | |
| 7138 | /* Here we have the guarantee to be in one of the following state: |
| 7139 | * HTTP_MSG_DATA, HTTP_MSG_CHUNK_SIZE, HTTP_MSG_CHUNK_CRLF, |
| 7140 | * HTTP_MSG_TRAILERS or HTTP_MSG_ENDING. */ |
| 7141 | |
| 7142 | switch_states: |
| 7143 | switch (msg->msg_state) { |
| 7144 | case HTTP_MSG_DATA: |
Christopher Faulet | da02e17 | 2015-12-04 09:25:05 +0100 | [diff] [blame] | 7145 | ret = FLT_STRM_DATA_CB(s, chn, flt_http_data(s, msg), |
| 7146 | /* default_ret */ MIN(msg->chunk_len, chn->buf->i - msg->next), |
| 7147 | /* on_error */ goto error); |
| 7148 | msg->next += ret; |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 7149 | msg->chunk_len -= ret; |
| 7150 | if (msg->chunk_len) { |
| 7151 | /* input empty or output full */ |
| 7152 | if (chn->buf->i > msg->next) |
| 7153 | chn->flags |= CF_WAKE_WRITE; |
| 7154 | goto missing_data_or_waiting; |
| 7155 | } |
| 7156 | |
| 7157 | /* nothing left to forward for this chunk*/ |
| 7158 | msg->msg_state = HTTP_MSG_CHUNK_CRLF; |
| 7159 | /* fall through for HTTP_MSG_CHUNK_CRLF */ |
| 7160 | |
| 7161 | case HTTP_MSG_CHUNK_CRLF: |
| 7162 | /* we want the CRLF after the data */ |
| 7163 | ret = http_skip_chunk_crlf(msg); |
| 7164 | if (ret == 0) |
| 7165 | goto missing_data_or_waiting; |
| 7166 | if (ret < 0) |
| 7167 | goto chunk_parsing_error; |
Christopher Faulet | 113f7de | 2015-12-14 14:52:13 +0100 | [diff] [blame] | 7168 | msg->next += ret; |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 7169 | msg->msg_state = HTTP_MSG_CHUNK_SIZE; |
| 7170 | /* fall through for HTTP_MSG_CHUNK_SIZE */ |
| 7171 | |
| 7172 | case HTTP_MSG_CHUNK_SIZE: |
| 7173 | /* read the chunk size and assign it to ->chunk_len, |
| 7174 | * then set ->next to point to the body and switch to |
| 7175 | * DATA or TRAILERS state. |
| 7176 | */ |
| 7177 | ret = http_parse_chunk_size(msg); |
| 7178 | if (ret == 0) |
| 7179 | goto missing_data_or_waiting; |
| 7180 | if (ret < 0) |
| 7181 | goto chunk_parsing_error; |
Christopher Faulet | 113f7de | 2015-12-14 14:52:13 +0100 | [diff] [blame] | 7182 | msg->next += ret; |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 7183 | if (msg->chunk_len) { |
| 7184 | msg->msg_state = HTTP_MSG_DATA; |
| 7185 | goto switch_states; |
| 7186 | } |
| 7187 | msg->msg_state = HTTP_MSG_TRAILERS; |
| 7188 | /* fall through for HTTP_MSG_TRAILERS */ |
| 7189 | |
| 7190 | case HTTP_MSG_TRAILERS: |
| 7191 | ret = http_forward_trailers(msg); |
| 7192 | if (ret < 0) |
| 7193 | goto chunk_parsing_error; |
Christopher Faulet | da02e17 | 2015-12-04 09:25:05 +0100 | [diff] [blame] | 7194 | FLT_STRM_DATA_CB(s, chn, flt_http_chunk_trailers(s, msg), |
| 7195 | /* default_ret */ 1, |
| 7196 | /* on_error */ goto error); |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 7197 | msg->next += msg->sol; |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 7198 | if (!ret) |
| 7199 | goto missing_data_or_waiting; |
| 7200 | break; |
| 7201 | |
| 7202 | case HTTP_MSG_ENDING: |
| 7203 | goto ending; |
| 7204 | |
| 7205 | default: |
| 7206 | /* This should no happen in this function */ |
| 7207 | goto error; |
| 7208 | } |
| 7209 | |
| 7210 | msg->msg_state = HTTP_MSG_ENDING; |
| 7211 | ending: |
| 7212 | /* we may have some pending data starting at res->buf->p such as a last |
| 7213 | * chunk of data or trailers. */ |
Christopher Faulet | da02e17 | 2015-12-04 09:25:05 +0100 | [diff] [blame] | 7214 | 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] | 7215 | /* default_ret */ msg->next, |
| 7216 | /* on_error */ goto error); |
| 7217 | b_adv(chn->buf, ret); |
| 7218 | msg->next -= ret; |
Willy Tarreau | 9962f8f | 2016-06-28 11:52:08 +0200 | [diff] [blame] | 7219 | if (unlikely(!(chn->flags & CF_WROTE_DATA) || msg->sov > 0)) |
| 7220 | msg->sov -= ret; |
Christopher Faulet | a9300a3 | 2016-06-28 15:54:44 +0200 | [diff] [blame] | 7221 | if (msg->next) |
| 7222 | goto waiting; |
Willy Tarreau | 9962f8f | 2016-06-28 11:52:08 +0200 | [diff] [blame] | 7223 | |
Christopher Faulet | da02e17 | 2015-12-04 09:25:05 +0100 | [diff] [blame] | 7224 | FLT_STRM_DATA_CB(s, chn, flt_http_end(s, msg), |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 7225 | /* default_ret */ 1, |
| 7226 | /* on_error */ goto error, |
| 7227 | /* on_wait */ goto waiting); |
| 7228 | msg->msg_state = HTTP_MSG_DONE; |
| 7229 | return 1; |
| 7230 | |
| 7231 | missing_data_or_waiting: |
| 7232 | /* we may have some pending data starting at chn->buf->p */ |
Christopher Faulet | da02e17 | 2015-12-04 09:25:05 +0100 | [diff] [blame] | 7233 | 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] | 7234 | /* default_ret */ msg->next, |
| 7235 | /* on_error */ goto error); |
| 7236 | b_adv(chn->buf, ret); |
| 7237 | msg->next -= ret; |
| 7238 | if (!(chn->flags & CF_WROTE_DATA) || msg->sov > 0) |
| 7239 | msg->sov -= ret; |
Christopher Faulet | 75e2eb6 | 2015-12-15 10:41:47 +0100 | [diff] [blame] | 7240 | if (!HAS_DATA_FILTERS(s, chn)) |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 7241 | msg->chunk_len -= channel_forward(chn, msg->chunk_len); |
Christopher Faulet | a9300a3 | 2016-06-28 15:54:44 +0200 | [diff] [blame] | 7242 | waiting: |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 7243 | return 0; |
| 7244 | |
| 7245 | chunk_parsing_error: |
| 7246 | if (msg->err_pos >= 0) { |
| 7247 | if (chn->flags & CF_ISRESP) |
| 7248 | http_capture_bad_message(&s->be->invalid_rep, s, msg, |
| 7249 | msg->msg_state, strm_fe(s)); |
| 7250 | else |
| 7251 | http_capture_bad_message(&strm_fe(s)->invalid_req, s, |
| 7252 | msg, msg->msg_state, s->be); |
| 7253 | } |
| 7254 | error: |
| 7255 | return -1; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 7256 | } |
| 7257 | |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 7258 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7259 | /* Iterate the same filter through all request headers. |
| 7260 | * Returns 1 if this filter can be stopped upon return, otherwise 0. |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7261 | * Since it can manage the switch to another backend, it updates the per-proxy |
| 7262 | * DENY stats. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7263 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 7264 | 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] | 7265 | { |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7266 | char *cur_ptr, *cur_end, *cur_next; |
| 7267 | int cur_idx, old_idx, last_hdr; |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 7268 | struct http_txn *txn = s->txn; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7269 | struct hdr_idx_elem *cur_hdr; |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 7270 | int delta; |
Willy Tarreau | 0f7562b | 2007-01-07 15:46:13 +0100 | [diff] [blame] | 7271 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7272 | last_hdr = 0; |
| 7273 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 7274 | cur_next = req->buf->p + hdr_idx_first_pos(&txn->hdr_idx); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7275 | old_idx = 0; |
| 7276 | |
| 7277 | while (!last_hdr) { |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 7278 | if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT))) |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7279 | return 1; |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 7280 | else if (unlikely(txn->flags & TX_CLALLOW) && |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7281 | (exp->action == ACT_ALLOW || |
| 7282 | exp->action == ACT_DENY || |
| 7283 | exp->action == ACT_TARPIT)) |
| 7284 | return 0; |
| 7285 | |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 7286 | cur_idx = txn->hdr_idx.v[old_idx].next; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7287 | if (!cur_idx) |
| 7288 | break; |
| 7289 | |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 7290 | cur_hdr = &txn->hdr_idx.v[cur_idx]; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7291 | cur_ptr = cur_next; |
| 7292 | cur_end = cur_ptr + cur_hdr->len; |
| 7293 | cur_next = cur_end + cur_hdr->cr + 1; |
| 7294 | |
| 7295 | /* Now we have one header between cur_ptr and cur_end, |
| 7296 | * and the next header starts at cur_next. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7297 | */ |
| 7298 | |
Willy Tarreau | 15a53a4 | 2015-01-21 13:39:42 +0100 | [diff] [blame] | 7299 | 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] | 7300 | switch (exp->action) { |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7301 | case ACT_ALLOW: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 7302 | txn->flags |= TX_CLALLOW; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7303 | last_hdr = 1; |
| 7304 | break; |
| 7305 | |
| 7306 | case ACT_DENY: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 7307 | txn->flags |= TX_CLDENY; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7308 | last_hdr = 1; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7309 | break; |
| 7310 | |
| 7311 | case ACT_TARPIT: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 7312 | txn->flags |= TX_CLTARPIT; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7313 | last_hdr = 1; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7314 | break; |
| 7315 | |
| 7316 | case ACT_REPLACE: |
Sasha Pachev | c600204 | 2014-05-26 12:33:48 -0600 | [diff] [blame] | 7317 | trash.len = exp_replace(trash.str, trash.size, cur_ptr, exp->replace, pmatch); |
| 7318 | if (trash.len < 0) |
| 7319 | return -1; |
| 7320 | |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 7321 | 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] | 7322 | /* FIXME: if the user adds a newline in the replacement, the |
| 7323 | * index will not be recalculated for now, and the new line |
| 7324 | * will not be counted as a new header. |
| 7325 | */ |
| 7326 | |
| 7327 | cur_end += delta; |
| 7328 | cur_next += delta; |
| 7329 | cur_hdr->len += delta; |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 7330 | http_msg_move_end(&txn->req, delta); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7331 | break; |
| 7332 | |
| 7333 | case ACT_REMOVE: |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 7334 | delta = buffer_replace2(req->buf, cur_ptr, cur_next, NULL, 0); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7335 | cur_next += delta; |
| 7336 | |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 7337 | http_msg_move_end(&txn->req, delta); |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 7338 | txn->hdr_idx.v[old_idx].next = cur_hdr->next; |
| 7339 | txn->hdr_idx.used--; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7340 | cur_hdr->len = 0; |
| 7341 | cur_end = NULL; /* null-term has been rewritten */ |
Willy Tarreau | 26db59e | 2010-11-28 06:57:24 +0100 | [diff] [blame] | 7342 | cur_idx = old_idx; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7343 | break; |
| 7344 | |
| 7345 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7346 | } |
| 7347 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7348 | /* keep the link from this header to next one in case of later |
| 7349 | * removal of next header. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7350 | */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7351 | old_idx = cur_idx; |
| 7352 | } |
| 7353 | return 0; |
| 7354 | } |
| 7355 | |
| 7356 | |
| 7357 | /* Apply the filter to the request line. |
| 7358 | * Returns 0 if nothing has been done, 1 if the filter has been applied, |
| 7359 | * or -1 if a replacement resulted in an invalid request line. |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7360 | * Since it can manage the switch to another backend, it updates the per-proxy |
| 7361 | * DENY stats. |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7362 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 7363 | 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] | 7364 | { |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7365 | char *cur_ptr, *cur_end; |
| 7366 | int done; |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 7367 | struct http_txn *txn = s->txn; |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 7368 | int delta; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7369 | |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 7370 | if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT))) |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7371 | return 1; |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 7372 | else if (unlikely(txn->flags & TX_CLALLOW) && |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7373 | (exp->action == ACT_ALLOW || |
| 7374 | exp->action == ACT_DENY || |
| 7375 | exp->action == ACT_TARPIT)) |
| 7376 | return 0; |
| 7377 | else if (exp->action == ACT_REMOVE) |
| 7378 | return 0; |
| 7379 | |
| 7380 | done = 0; |
| 7381 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 7382 | cur_ptr = req->buf->p; |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 7383 | cur_end = cur_ptr + txn->req.sl.rq.l; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7384 | |
| 7385 | /* Now we have the request line between cur_ptr and cur_end */ |
| 7386 | |
Willy Tarreau | 15a53a4 | 2015-01-21 13:39:42 +0100 | [diff] [blame] | 7387 | 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] | 7388 | switch (exp->action) { |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7389 | case ACT_ALLOW: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 7390 | txn->flags |= TX_CLALLOW; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7391 | done = 1; |
| 7392 | break; |
Willy Tarreau | a496b60 | 2006-12-17 23:15:24 +0100 | [diff] [blame] | 7393 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7394 | case ACT_DENY: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 7395 | txn->flags |= TX_CLDENY; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7396 | done = 1; |
| 7397 | break; |
Willy Tarreau | a496b60 | 2006-12-17 23:15:24 +0100 | [diff] [blame] | 7398 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7399 | case ACT_TARPIT: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 7400 | txn->flags |= TX_CLTARPIT; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7401 | done = 1; |
| 7402 | break; |
Willy Tarreau | a496b60 | 2006-12-17 23:15:24 +0100 | [diff] [blame] | 7403 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7404 | case ACT_REPLACE: |
Sasha Pachev | c600204 | 2014-05-26 12:33:48 -0600 | [diff] [blame] | 7405 | trash.len = exp_replace(trash.str, trash.size, cur_ptr, exp->replace, pmatch); |
| 7406 | if (trash.len < 0) |
| 7407 | return -1; |
| 7408 | |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 7409 | 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] | 7410 | /* FIXME: if the user adds a newline in the replacement, the |
| 7411 | * index will not be recalculated for now, and the new line |
| 7412 | * will not be counted as a new header. |
| 7413 | */ |
Willy Tarreau | a496b60 | 2006-12-17 23:15:24 +0100 | [diff] [blame] | 7414 | |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 7415 | http_msg_move_end(&txn->req, delta); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7416 | cur_end += delta; |
Willy Tarreau | 69d8c5d | 2012-05-08 09:44:41 +0200 | [diff] [blame] | 7417 | cur_end = (char *)http_parse_reqline(&txn->req, |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7418 | HTTP_MSG_RQMETH, |
| 7419 | cur_ptr, cur_end + 1, |
| 7420 | NULL, NULL); |
| 7421 | if (unlikely(!cur_end)) |
| 7422 | return -1; |
Willy Tarreau | a496b60 | 2006-12-17 23:15:24 +0100 | [diff] [blame] | 7423 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7424 | /* we have a full request and we know that we have either a CR |
| 7425 | * or an LF at <ptr>. |
| 7426 | */ |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 7427 | txn->meth = find_http_meth(cur_ptr, txn->req.sl.rq.m_l); |
| 7428 | 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] | 7429 | /* there is no point trying this regex on headers */ |
| 7430 | return 1; |
| 7431 | } |
| 7432 | } |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7433 | return done; |
| 7434 | } |
Willy Tarreau | 97de624 | 2006-12-27 17:18:38 +0100 | [diff] [blame] | 7435 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7436 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7437 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7438 | /* |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 7439 | * 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] | 7440 | * 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] | 7441 | * unparsable request. Since it can manage the switch to another backend, it |
| 7442 | * updates the per-proxy DENY stats. |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7443 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 7444 | 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] | 7445 | { |
Willy Tarreau | 192252e | 2015-04-04 01:47:55 +0200 | [diff] [blame] | 7446 | struct session *sess = s->sess; |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 7447 | struct http_txn *txn = s->txn; |
Willy Tarreau | 6c123b1 | 2010-01-28 20:22:06 +0100 | [diff] [blame] | 7448 | struct hdr_exp *exp; |
| 7449 | |
| 7450 | for (exp = px->req_exp; exp; exp = exp->next) { |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7451 | int ret; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7452 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7453 | /* |
| 7454 | * The interleaving of transformations and verdicts |
| 7455 | * makes it difficult to decide to continue or stop |
| 7456 | * the evaluation. |
| 7457 | */ |
| 7458 | |
Willy Tarreau | 6c123b1 | 2010-01-28 20:22:06 +0100 | [diff] [blame] | 7459 | if (txn->flags & (TX_CLDENY|TX_CLTARPIT)) |
| 7460 | break; |
| 7461 | |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 7462 | if ((txn->flags & TX_CLALLOW) && |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7463 | (exp->action == ACT_ALLOW || exp->action == ACT_DENY || |
Willy Tarreau | 6c123b1 | 2010-01-28 20:22:06 +0100 | [diff] [blame] | 7464 | exp->action == ACT_TARPIT || exp->action == ACT_PASS)) |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7465 | continue; |
Willy Tarreau | 6c123b1 | 2010-01-28 20:22:06 +0100 | [diff] [blame] | 7466 | |
| 7467 | /* if this filter had a condition, evaluate it now and skip to |
| 7468 | * next filter if the condition does not match. |
| 7469 | */ |
| 7470 | if (exp->cond) { |
Willy Tarreau | 192252e | 2015-04-04 01:47:55 +0200 | [diff] [blame] | 7471 | 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] | 7472 | ret = acl_pass(ret); |
| 7473 | if (((struct acl_cond *)exp->cond)->pol == ACL_COND_UNLESS) |
| 7474 | ret = !ret; |
| 7475 | |
| 7476 | if (!ret) |
| 7477 | continue; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7478 | } |
| 7479 | |
| 7480 | /* Apply the filter to the request line. */ |
Willy Tarreau | 6c123b1 | 2010-01-28 20:22:06 +0100 | [diff] [blame] | 7481 | ret = apply_filter_to_req_line(s, req, exp); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7482 | if (unlikely(ret < 0)) |
| 7483 | return -1; |
| 7484 | |
| 7485 | if (likely(ret == 0)) { |
| 7486 | /* The filter did not match the request, it can be |
| 7487 | * iterated through all headers. |
| 7488 | */ |
Willy Tarreau | 34d4c3c | 2015-01-30 20:58:58 +0100 | [diff] [blame] | 7489 | if (unlikely(apply_filter_to_req_headers(s, req, exp) < 0)) |
| 7490 | return -1; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7491 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7492 | } |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7493 | return 0; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7494 | } |
| 7495 | |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 7496 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7497 | /* Find the end of a cookie value contained between <s> and <e>. It works the |
| 7498 | * same way as with headers above except that the semi-colon also ends a token. |
| 7499 | * See RFC2965 for more information. Note that it requires a valid header to |
| 7500 | * return a valid result. |
| 7501 | */ |
| 7502 | char *find_cookie_value_end(char *s, const char *e) |
| 7503 | { |
| 7504 | int quoted, qdpair; |
| 7505 | |
| 7506 | quoted = qdpair = 0; |
| 7507 | for (; s < e; s++) { |
| 7508 | if (qdpair) qdpair = 0; |
| 7509 | else if (quoted) { |
| 7510 | if (*s == '\\') qdpair = 1; |
| 7511 | else if (*s == '"') quoted = 0; |
| 7512 | } |
| 7513 | else if (*s == '"') quoted = 1; |
| 7514 | else if (*s == ',' || *s == ';') return s; |
| 7515 | } |
| 7516 | return s; |
| 7517 | } |
| 7518 | |
| 7519 | /* Delete a value in a header between delimiters <from> and <next> in buffer |
| 7520 | * <buf>. The number of characters displaced is returned, and the pointer to |
| 7521 | * the first delimiter is updated if required. The function tries as much as |
| 7522 | * possible to respect the following principles : |
| 7523 | * - replace <from> delimiter by the <next> one unless <from> points to a |
| 7524 | * colon, in which case <next> is simply removed |
| 7525 | * - set exactly one space character after the new first delimiter, unless |
| 7526 | * there are not enough characters in the block being moved to do so. |
| 7527 | * - remove unneeded spaces before the previous delimiter and after the new |
| 7528 | * one. |
| 7529 | * |
| 7530 | * It is the caller's responsibility to ensure that : |
| 7531 | * - <from> points to a valid delimiter or the colon ; |
| 7532 | * - <next> points to a valid delimiter or the final CR/LF ; |
| 7533 | * - there are non-space chars before <from> ; |
| 7534 | * - there is a CR/LF at or after <next>. |
| 7535 | */ |
Willy Tarreau | af81935 | 2012-08-27 22:08:00 +0200 | [diff] [blame] | 7536 | int del_hdr_value(struct buffer *buf, char **from, char *next) |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7537 | { |
| 7538 | char *prev = *from; |
| 7539 | |
| 7540 | if (*prev == ':') { |
| 7541 | /* We're removing the first value, preserve the colon and add a |
| 7542 | * space if possible. |
| 7543 | */ |
Willy Tarreau | 2235b26 | 2016-11-05 15:50:20 +0100 | [diff] [blame] | 7544 | if (!HTTP_IS_CRLF(*next)) |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7545 | next++; |
| 7546 | prev++; |
| 7547 | if (prev < next) |
| 7548 | *prev++ = ' '; |
| 7549 | |
Willy Tarreau | 2235b26 | 2016-11-05 15:50:20 +0100 | [diff] [blame] | 7550 | while (HTTP_IS_SPHT(*next)) |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7551 | next++; |
| 7552 | } else { |
| 7553 | /* Remove useless spaces before the old delimiter. */ |
Willy Tarreau | 2235b26 | 2016-11-05 15:50:20 +0100 | [diff] [blame] | 7554 | while (HTTP_IS_SPHT(*(prev-1))) |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7555 | prev--; |
| 7556 | *from = prev; |
| 7557 | |
| 7558 | /* copy the delimiter and if possible a space if we're |
| 7559 | * not at the end of the line. |
| 7560 | */ |
Willy Tarreau | 2235b26 | 2016-11-05 15:50:20 +0100 | [diff] [blame] | 7561 | if (!HTTP_IS_CRLF(*next)) { |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7562 | *prev++ = *next++; |
| 7563 | if (prev + 1 < next) |
| 7564 | *prev++ = ' '; |
Willy Tarreau | 2235b26 | 2016-11-05 15:50:20 +0100 | [diff] [blame] | 7565 | while (HTTP_IS_SPHT(*next)) |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7566 | next++; |
| 7567 | } |
| 7568 | } |
| 7569 | return buffer_replace2(buf, prev, next, NULL, 0); |
| 7570 | } |
| 7571 | |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 7572 | /* |
Willy Tarreau | 396d2c6 | 2007-11-04 19:30:00 +0100 | [diff] [blame] | 7573 | * 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] | 7574 | * desirable to call it only when needed. This code is quite complex because |
| 7575 | * of the multiple very crappy and ambiguous syntaxes we have to support. it |
| 7576 | * highly recommended not to touch this part without a good reason ! |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7577 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 7578 | void manage_client_side_cookies(struct stream *s, struct channel *req) |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7579 | { |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 7580 | struct http_txn *txn = s->txn; |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 7581 | struct session *sess = s->sess; |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7582 | int preserve_hdr; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7583 | int cur_idx, old_idx; |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7584 | char *hdr_beg, *hdr_end, *hdr_next, *del_from; |
| 7585 | char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7586 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7587 | /* Iterate through the headers, we start with the start line. */ |
Willy Tarreau | 83969f4 | 2007-01-22 08:55:47 +0100 | [diff] [blame] | 7588 | old_idx = 0; |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 7589 | hdr_next = req->buf->p + hdr_idx_first_pos(&txn->hdr_idx); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7590 | |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 7591 | while ((cur_idx = txn->hdr_idx.v[old_idx].next)) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7592 | struct hdr_idx_elem *cur_hdr; |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 7593 | int val; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7594 | |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 7595 | cur_hdr = &txn->hdr_idx.v[cur_idx]; |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7596 | hdr_beg = hdr_next; |
| 7597 | hdr_end = hdr_beg + cur_hdr->len; |
| 7598 | hdr_next = hdr_end + cur_hdr->cr + 1; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7599 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7600 | /* We have one full header between hdr_beg and hdr_end, and the |
| 7601 | * next header starts at hdr_next. We're only interested in |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7602 | * "Cookie:" headers. |
| 7603 | */ |
| 7604 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7605 | val = http_header_match2(hdr_beg, hdr_end, "Cookie", 6); |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 7606 | if (!val) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7607 | old_idx = cur_idx; |
| 7608 | continue; |
| 7609 | } |
| 7610 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7611 | del_from = NULL; /* nothing to be deleted */ |
| 7612 | preserve_hdr = 0; /* assume we may kill the whole header */ |
| 7613 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7614 | /* Now look for cookies. Conforming to RFC2109, we have to support |
| 7615 | * attributes whose name begin with a '$', and associate them with |
| 7616 | * the right cookie, if we want to delete this cookie. |
| 7617 | * So there are 3 cases for each cookie read : |
| 7618 | * 1) it's a special attribute, beginning with a '$' : ignore it. |
| 7619 | * 2) it's a server id cookie that we *MAY* want to delete : save |
| 7620 | * some pointers on it (last semi-colon, beginning of cookie...) |
| 7621 | * 3) it's an application cookie : we *MAY* have to delete a previous |
| 7622 | * "special" cookie. |
| 7623 | * At the end of loop, if a "special" cookie remains, we may have to |
| 7624 | * remove it. If no application cookie persists in the header, we |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7625 | * *MUST* delete it. |
| 7626 | * |
| 7627 | * Note: RFC2965 is unclear about the processing of spaces around |
| 7628 | * the equal sign in the ATTR=VALUE form. A careful inspection of |
| 7629 | * the RFC explicitly allows spaces before it, and not within the |
| 7630 | * tokens (attrs or values). An inspection of RFC2109 allows that |
| 7631 | * too but section 10.1.3 lets one think that spaces may be allowed |
| 7632 | * after the equal sign too, resulting in some (rare) buggy |
| 7633 | * implementations trying to do that. So let's do what servers do. |
| 7634 | * Latest ietf draft forbids spaces all around. Also, earlier RFCs |
| 7635 | * allowed quoted strings in values, with any possible character |
| 7636 | * after a backslash, including control chars and delimitors, which |
| 7637 | * causes parsing to become ambiguous. Browsers also allow spaces |
| 7638 | * within values even without quotes. |
| 7639 | * |
| 7640 | * We have to keep multiple pointers in order to support cookie |
| 7641 | * removal at the beginning, middle or end of header without |
| 7642 | * corrupting the header. All of these headers are valid : |
| 7643 | * |
| 7644 | * Cookie:NAME1=VALUE1;NAME2=VALUE2;NAME3=VALUE3\r\n |
| 7645 | * Cookie:NAME1=VALUE1;NAME2_ONLY ;NAME3=VALUE3\r\n |
| 7646 | * Cookie: NAME1 = VALUE 1 ; NAME2 = VALUE2 ; NAME3 = VALUE3\r\n |
| 7647 | * | | | | | | | | | |
| 7648 | * | | | | | | | | hdr_end <--+ |
| 7649 | * | | | | | | | +--> next |
| 7650 | * | | | | | | +----> val_end |
| 7651 | * | | | | | +-----------> val_beg |
| 7652 | * | | | | +--------------> equal |
| 7653 | * | | | +----------------> att_end |
| 7654 | * | | +---------------------> att_beg |
| 7655 | * | +--------------------------> prev |
| 7656 | * +--------------------------------> hdr_beg |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7657 | */ |
| 7658 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7659 | for (prev = hdr_beg + 6; prev < hdr_end; prev = next) { |
| 7660 | /* Iterate through all cookies on this line */ |
| 7661 | |
| 7662 | /* find att_beg */ |
| 7663 | att_beg = prev + 1; |
Willy Tarreau | 2235b26 | 2016-11-05 15:50:20 +0100 | [diff] [blame] | 7664 | while (att_beg < hdr_end && HTTP_IS_SPHT(*att_beg)) |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7665 | att_beg++; |
| 7666 | |
| 7667 | /* find att_end : this is the first character after the last non |
| 7668 | * space before the equal. It may be equal to hdr_end. |
| 7669 | */ |
| 7670 | equal = att_end = att_beg; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7671 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7672 | while (equal < hdr_end) { |
| 7673 | if (*equal == '=' || *equal == ',' || *equal == ';') |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7674 | break; |
Willy Tarreau | 2235b26 | 2016-11-05 15:50:20 +0100 | [diff] [blame] | 7675 | if (HTTP_IS_SPHT(*equal++)) |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7676 | continue; |
| 7677 | att_end = equal; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7678 | } |
| 7679 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7680 | /* here, <equal> points to '=', a delimitor or the end. <att_end> |
| 7681 | * is between <att_beg> and <equal>, both may be identical. |
| 7682 | */ |
| 7683 | |
| 7684 | /* look for end of cookie if there is an equal sign */ |
| 7685 | if (equal < hdr_end && *equal == '=') { |
| 7686 | /* look for the beginning of the value */ |
| 7687 | val_beg = equal + 1; |
Willy Tarreau | 2235b26 | 2016-11-05 15:50:20 +0100 | [diff] [blame] | 7688 | while (val_beg < hdr_end && HTTP_IS_SPHT(*val_beg)) |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7689 | val_beg++; |
| 7690 | |
| 7691 | /* find the end of the value, respecting quotes */ |
| 7692 | next = find_cookie_value_end(val_beg, hdr_end); |
| 7693 | |
| 7694 | /* make val_end point to the first white space or delimitor after the value */ |
| 7695 | val_end = next; |
Willy Tarreau | 2235b26 | 2016-11-05 15:50:20 +0100 | [diff] [blame] | 7696 | while (val_end > val_beg && HTTP_IS_SPHT(*(val_end - 1))) |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7697 | val_end--; |
| 7698 | } else { |
| 7699 | val_beg = val_end = next = equal; |
Willy Tarreau | 305ae85 | 2010-01-03 19:45:54 +0100 | [diff] [blame] | 7700 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7701 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7702 | /* We have nothing to do with attributes beginning with '$'. However, |
| 7703 | * they will automatically be removed if a header before them is removed, |
| 7704 | * since they're supposed to be linked together. |
| 7705 | */ |
| 7706 | if (*att_beg == '$') |
| 7707 | continue; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7708 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7709 | /* Ignore cookies with no equal sign */ |
| 7710 | if (equal == next) { |
| 7711 | /* This is not our cookie, so we must preserve it. But if we already |
| 7712 | * scheduled another cookie for removal, we cannot remove the |
| 7713 | * complete header, but we can remove the previous block itself. |
| 7714 | */ |
| 7715 | preserve_hdr = 1; |
| 7716 | if (del_from != NULL) { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 7717 | int delta = del_hdr_value(req->buf, &del_from, prev); |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7718 | val_end += delta; |
| 7719 | next += delta; |
| 7720 | hdr_end += delta; |
| 7721 | hdr_next += delta; |
| 7722 | cur_hdr->len += delta; |
| 7723 | http_msg_move_end(&txn->req, delta); |
| 7724 | prev = del_from; |
| 7725 | del_from = NULL; |
| 7726 | } |
| 7727 | continue; |
Willy Tarreau | 305ae85 | 2010-01-03 19:45:54 +0100 | [diff] [blame] | 7728 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7729 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7730 | /* if there are spaces around the equal sign, we need to |
| 7731 | * strip them otherwise we'll get trouble for cookie captures, |
| 7732 | * or even for rewrites. Since this happens extremely rarely, |
| 7733 | * it does not hurt performance. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7734 | */ |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7735 | if (unlikely(att_end != equal || val_beg > equal + 1)) { |
| 7736 | int stripped_before = 0; |
| 7737 | int stripped_after = 0; |
| 7738 | |
| 7739 | if (att_end != equal) { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 7740 | stripped_before = buffer_replace2(req->buf, att_end, equal, NULL, 0); |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7741 | equal += stripped_before; |
| 7742 | val_beg += stripped_before; |
| 7743 | } |
| 7744 | |
| 7745 | if (val_beg > equal + 1) { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 7746 | stripped_after = buffer_replace2(req->buf, equal + 1, val_beg, NULL, 0); |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7747 | val_beg += stripped_after; |
| 7748 | stripped_before += stripped_after; |
| 7749 | } |
| 7750 | |
| 7751 | val_end += stripped_before; |
| 7752 | next += stripped_before; |
| 7753 | hdr_end += stripped_before; |
| 7754 | hdr_next += stripped_before; |
| 7755 | cur_hdr->len += stripped_before; |
| 7756 | http_msg_move_end(&txn->req, stripped_before); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7757 | } |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7758 | /* now everything is as on the diagram above */ |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7759 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7760 | /* First, let's see if we want to capture this cookie. We check |
| 7761 | * that we don't already have a client side cookie, because we |
| 7762 | * can only capture one. Also as an optimisation, we ignore |
| 7763 | * cookies shorter than the declared name. |
| 7764 | */ |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 7765 | if (sess->fe->capture_name != NULL && txn->cli_cookie == NULL && |
| 7766 | (val_end - att_beg >= sess->fe->capture_namelen) && |
| 7767 | memcmp(att_beg, sess->fe->capture_name, sess->fe->capture_namelen) == 0) { |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7768 | int log_len = val_end - att_beg; |
| 7769 | |
| 7770 | if ((txn->cli_cookie = pool_alloc2(pool2_capture)) == NULL) { |
| 7771 | Alert("HTTP logging : out of memory.\n"); |
| 7772 | } else { |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 7773 | if (log_len > sess->fe->capture_len) |
| 7774 | log_len = sess->fe->capture_len; |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7775 | memcpy(txn->cli_cookie, att_beg, log_len); |
| 7776 | txn->cli_cookie[log_len] = 0; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7777 | } |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7778 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7779 | |
Willy Tarreau | bca9969 | 2010-10-06 19:25:55 +0200 | [diff] [blame] | 7780 | /* Persistence cookies in passive, rewrite or insert mode have the |
| 7781 | * following form : |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7782 | * |
Willy Tarreau | bca9969 | 2010-10-06 19:25:55 +0200 | [diff] [blame] | 7783 | * Cookie: NAME=SRV[|<lastseen>[|<firstseen>]] |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7784 | * |
Willy Tarreau | bca9969 | 2010-10-06 19:25:55 +0200 | [diff] [blame] | 7785 | * For cookies in prefix mode, the form is : |
| 7786 | * |
| 7787 | * Cookie: NAME=SRV~VALUE |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7788 | */ |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 7789 | if ((att_end - att_beg == s->be->cookie_len) && (s->be->cookie_name != NULL) && |
| 7790 | (memcmp(att_beg, s->be->cookie_name, att_end - att_beg) == 0)) { |
| 7791 | struct server *srv = s->be->srv; |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7792 | char *delim; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7793 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7794 | /* if we're in cookie prefix mode, we'll search the delimitor so that we |
| 7795 | * have the server ID between val_beg and delim, and the original cookie between |
| 7796 | * delim+1 and val_end. Otherwise, delim==val_end : |
| 7797 | * |
| 7798 | * Cookie: NAME=SRV; # in all but prefix modes |
| 7799 | * Cookie: NAME=SRV~OPAQUE ; # in prefix mode |
| 7800 | * | || || | |+-> next |
| 7801 | * | || || | +--> val_end |
| 7802 | * | || || +---------> delim |
| 7803 | * | || |+------------> val_beg |
| 7804 | * | || +-------------> att_end = equal |
| 7805 | * | |+-----------------> att_beg |
| 7806 | * | +------------------> prev |
| 7807 | * +-------------------------> hdr_beg |
| 7808 | */ |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7809 | |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 7810 | if (s->be->ck_opts & PR_CK_PFX) { |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7811 | for (delim = val_beg; delim < val_end; delim++) |
| 7812 | if (*delim == COOKIE_DELIM) |
| 7813 | break; |
Willy Tarreau | bca9969 | 2010-10-06 19:25:55 +0200 | [diff] [blame] | 7814 | } else { |
| 7815 | char *vbar1; |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7816 | delim = val_end; |
Willy Tarreau | bca9969 | 2010-10-06 19:25:55 +0200 | [diff] [blame] | 7817 | /* Now check if the cookie contains a date field, which would |
| 7818 | * appear after a vertical bar ('|') just after the server name |
| 7819 | * and before the delimiter. |
| 7820 | */ |
| 7821 | vbar1 = memchr(val_beg, COOKIE_DELIM_DATE, val_end - val_beg); |
| 7822 | if (vbar1) { |
| 7823 | /* OK, so left of the bar is the server's cookie and |
Willy Tarreau | f64d141 | 2010-10-07 20:06:11 +0200 | [diff] [blame] | 7824 | * right is the last seen date. It is a base64 encoded |
| 7825 | * 30-bit value representing the UNIX date since the |
| 7826 | * epoch in 4-second quantities. |
Willy Tarreau | bca9969 | 2010-10-06 19:25:55 +0200 | [diff] [blame] | 7827 | */ |
Willy Tarreau | f64d141 | 2010-10-07 20:06:11 +0200 | [diff] [blame] | 7828 | int val; |
Willy Tarreau | bca9969 | 2010-10-06 19:25:55 +0200 | [diff] [blame] | 7829 | delim = vbar1++; |
Willy Tarreau | f64d141 | 2010-10-07 20:06:11 +0200 | [diff] [blame] | 7830 | if (val_end - vbar1 >= 5) { |
| 7831 | val = b64tos30(vbar1); |
| 7832 | if (val > 0) |
| 7833 | txn->cookie_last_date = val << 2; |
| 7834 | } |
| 7835 | /* look for a second vertical bar */ |
| 7836 | vbar1 = memchr(vbar1, COOKIE_DELIM_DATE, val_end - vbar1); |
| 7837 | if (vbar1 && (val_end - vbar1 > 5)) { |
| 7838 | val = b64tos30(vbar1 + 1); |
| 7839 | if (val > 0) |
| 7840 | txn->cookie_first_date = val << 2; |
| 7841 | } |
Willy Tarreau | bca9969 | 2010-10-06 19:25:55 +0200 | [diff] [blame] | 7842 | } |
| 7843 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7844 | |
Willy Tarreau | f64d141 | 2010-10-07 20:06:11 +0200 | [diff] [blame] | 7845 | /* if the cookie has an expiration date and the proxy wants to check |
| 7846 | * it, then we do that now. We first check if the cookie is too old, |
| 7847 | * then only if it has expired. We detect strict overflow because the |
| 7848 | * time resolution here is not great (4 seconds). Cookies with dates |
| 7849 | * in the future are ignored if their offset is beyond one day. This |
| 7850 | * allows an admin to fix timezone issues without expiring everyone |
| 7851 | * and at the same time avoids keeping unwanted side effects for too |
| 7852 | * long. |
| 7853 | */ |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 7854 | if (txn->cookie_first_date && s->be->cookie_maxlife && |
| 7855 | (((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] | 7856 | ((signed)(txn->cookie_first_date - date.tv_sec) > 86400))) { |
Willy Tarreau | f64d141 | 2010-10-07 20:06:11 +0200 | [diff] [blame] | 7857 | txn->flags &= ~TX_CK_MASK; |
| 7858 | txn->flags |= TX_CK_OLD; |
| 7859 | delim = val_beg; // let's pretend we have not found the cookie |
| 7860 | txn->cookie_first_date = 0; |
| 7861 | txn->cookie_last_date = 0; |
| 7862 | } |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 7863 | else if (txn->cookie_last_date && s->be->cookie_maxidle && |
| 7864 | (((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] | 7865 | ((signed)(txn->cookie_last_date - date.tv_sec) > 86400))) { |
Willy Tarreau | f64d141 | 2010-10-07 20:06:11 +0200 | [diff] [blame] | 7866 | txn->flags &= ~TX_CK_MASK; |
| 7867 | txn->flags |= TX_CK_EXPIRED; |
| 7868 | delim = val_beg; // let's pretend we have not found the cookie |
| 7869 | txn->cookie_first_date = 0; |
| 7870 | txn->cookie_last_date = 0; |
| 7871 | } |
| 7872 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7873 | /* Here, we'll look for the first running server which supports the cookie. |
| 7874 | * This allows to share a same cookie between several servers, for example |
| 7875 | * to dedicate backup servers to specific servers only. |
| 7876 | * However, to prevent clients from sticking to cookie-less backup server |
| 7877 | * when they have incidentely learned an empty cookie, we simply ignore |
| 7878 | * empty cookies and mark them as invalid. |
| 7879 | * The same behaviour is applied when persistence must be ignored. |
| 7880 | */ |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 7881 | if ((delim == val_beg) || (s->flags & (SF_IGNORE_PRST | SF_ASSIGNED))) |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7882 | srv = NULL; |
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 | while (srv) { |
| 7885 | if (srv->cookie && (srv->cklen == delim - val_beg) && |
| 7886 | !memcmp(val_beg, srv->cookie, delim - val_beg)) { |
Willy Tarreau | 892337c | 2014-05-13 23:41:20 +0200 | [diff] [blame] | 7887 | if ((srv->state != SRV_ST_STOPPED) || |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 7888 | (s->be->options & PR_O_PERSIST) || |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 7889 | (s->flags & SF_FORCE_PRST)) { |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7890 | /* we found the server and we can use it */ |
| 7891 | txn->flags &= ~TX_CK_MASK; |
Willy Tarreau | 892337c | 2014-05-13 23:41:20 +0200 | [diff] [blame] | 7892 | 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] | 7893 | s->flags |= SF_DIRECT | SF_ASSIGNED; |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 7894 | s->target = &srv->obj_type; |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7895 | break; |
| 7896 | } else { |
| 7897 | /* we found a server, but it's down, |
| 7898 | * mark it as such and go on in case |
| 7899 | * another one is available. |
| 7900 | */ |
| 7901 | txn->flags &= ~TX_CK_MASK; |
| 7902 | txn->flags |= TX_CK_DOWN; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7903 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7904 | } |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7905 | srv = srv->next; |
| 7906 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7907 | |
Willy Tarreau | f64d141 | 2010-10-07 20:06:11 +0200 | [diff] [blame] | 7908 | 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] | 7909 | /* no server matched this cookie or we deliberately skipped it */ |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7910 | txn->flags &= ~TX_CK_MASK; |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 7911 | if ((s->flags & (SF_IGNORE_PRST | SF_ASSIGNED))) |
Willy Tarreau | c89ccb6 | 2012-04-05 21:18:22 +0200 | [diff] [blame] | 7912 | txn->flags |= TX_CK_UNUSED; |
| 7913 | else |
| 7914 | txn->flags |= TX_CK_INVALID; |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7915 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7916 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7917 | /* depending on the cookie mode, we may have to either : |
| 7918 | * - delete the complete cookie if we're in insert+indirect mode, so that |
| 7919 | * the server never sees it ; |
| 7920 | * - remove the server id from the cookie value, and tag the cookie as an |
| 7921 | * application cookie so that it does not get accidentely removed later, |
| 7922 | * if we're in cookie prefix mode |
| 7923 | */ |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 7924 | if ((s->be->ck_opts & PR_CK_PFX) && (delim != val_end)) { |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7925 | int delta; /* negative */ |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7926 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 7927 | delta = buffer_replace2(req->buf, val_beg, delim + 1, NULL, 0); |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7928 | val_end += delta; |
| 7929 | next += delta; |
| 7930 | hdr_end += delta; |
| 7931 | hdr_next += delta; |
| 7932 | cur_hdr->len += delta; |
| 7933 | http_msg_move_end(&txn->req, delta); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7934 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7935 | del_from = NULL; |
| 7936 | preserve_hdr = 1; /* we want to keep this cookie */ |
| 7937 | } |
| 7938 | else if (del_from == NULL && |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 7939 | (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] | 7940 | del_from = prev; |
| 7941 | } |
| 7942 | } else { |
| 7943 | /* This is not our cookie, so we must preserve it. But if we already |
| 7944 | * scheduled another cookie for removal, we cannot remove the |
| 7945 | * complete header, but we can remove the previous block itself. |
| 7946 | */ |
| 7947 | preserve_hdr = 1; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7948 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7949 | if (del_from != NULL) { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 7950 | int delta = del_hdr_value(req->buf, &del_from, prev); |
Willy Tarreau | b810554 | 2010-11-24 18:31:28 +0100 | [diff] [blame] | 7951 | if (att_beg >= del_from) |
| 7952 | att_beg += delta; |
| 7953 | if (att_end >= del_from) |
| 7954 | att_end += delta; |
| 7955 | val_beg += delta; |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7956 | val_end += delta; |
| 7957 | next += delta; |
| 7958 | hdr_end += delta; |
| 7959 | hdr_next += delta; |
| 7960 | cur_hdr->len += delta; |
| 7961 | http_msg_move_end(&txn->req, delta); |
| 7962 | prev = del_from; |
| 7963 | del_from = NULL; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7964 | } |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7965 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7966 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7967 | /* continue with next cookie on this header line */ |
| 7968 | att_beg = next; |
| 7969 | } /* for each cookie */ |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7970 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7971 | /* There are no more cookies on this line. |
| 7972 | * We may still have one (or several) marked for deletion at the |
| 7973 | * end of the line. We must do this now in two ways : |
| 7974 | * - if some cookies must be preserved, we only delete from the |
| 7975 | * mark to the end of line ; |
| 7976 | * - if nothing needs to be preserved, simply delete the whole header |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7977 | */ |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7978 | if (del_from) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7979 | int delta; |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7980 | if (preserve_hdr) { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 7981 | delta = del_hdr_value(req->buf, &del_from, hdr_end); |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7982 | hdr_end = del_from; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7983 | cur_hdr->len += delta; |
| 7984 | } else { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 7985 | delta = buffer_replace2(req->buf, hdr_beg, hdr_next, NULL, 0); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7986 | |
| 7987 | /* FIXME: this should be a separate function */ |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 7988 | txn->hdr_idx.v[old_idx].next = cur_hdr->next; |
| 7989 | txn->hdr_idx.used--; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7990 | cur_hdr->len = 0; |
Willy Tarreau | 26db59e | 2010-11-28 06:57:24 +0100 | [diff] [blame] | 7991 | cur_idx = old_idx; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7992 | } |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7993 | hdr_next += delta; |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 7994 | http_msg_move_end(&txn->req, delta); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7995 | } |
| 7996 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7997 | /* check next header */ |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7998 | old_idx = cur_idx; |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7999 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 8000 | } |
| 8001 | |
| 8002 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8003 | /* Iterate the same filter through all response headers contained in <rtr>. |
| 8004 | * Returns 1 if this filter can be stopped upon return, otherwise 0. |
| 8005 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 8006 | 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] | 8007 | { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8008 | char *cur_ptr, *cur_end, *cur_next; |
| 8009 | int cur_idx, old_idx, last_hdr; |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 8010 | struct http_txn *txn = s->txn; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8011 | struct hdr_idx_elem *cur_hdr; |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 8012 | int delta; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8013 | |
| 8014 | last_hdr = 0; |
| 8015 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 8016 | cur_next = rtr->buf->p + hdr_idx_first_pos(&txn->hdr_idx); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8017 | old_idx = 0; |
| 8018 | |
| 8019 | while (!last_hdr) { |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 8020 | if (unlikely(txn->flags & TX_SVDENY)) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8021 | return 1; |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 8022 | else if (unlikely(txn->flags & TX_SVALLOW) && |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8023 | (exp->action == ACT_ALLOW || |
| 8024 | exp->action == ACT_DENY)) |
| 8025 | return 0; |
| 8026 | |
| 8027 | cur_idx = txn->hdr_idx.v[old_idx].next; |
| 8028 | if (!cur_idx) |
| 8029 | break; |
| 8030 | |
| 8031 | cur_hdr = &txn->hdr_idx.v[cur_idx]; |
| 8032 | cur_ptr = cur_next; |
| 8033 | cur_end = cur_ptr + cur_hdr->len; |
| 8034 | cur_next = cur_end + cur_hdr->cr + 1; |
| 8035 | |
| 8036 | /* Now we have one header between cur_ptr and cur_end, |
| 8037 | * and the next header starts at cur_next. |
| 8038 | */ |
| 8039 | |
Willy Tarreau | 15a53a4 | 2015-01-21 13:39:42 +0100 | [diff] [blame] | 8040 | 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] | 8041 | switch (exp->action) { |
| 8042 | case ACT_ALLOW: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 8043 | txn->flags |= TX_SVALLOW; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8044 | last_hdr = 1; |
| 8045 | break; |
| 8046 | |
| 8047 | case ACT_DENY: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 8048 | txn->flags |= TX_SVDENY; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8049 | last_hdr = 1; |
| 8050 | break; |
| 8051 | |
| 8052 | case ACT_REPLACE: |
Sasha Pachev | c600204 | 2014-05-26 12:33:48 -0600 | [diff] [blame] | 8053 | trash.len = exp_replace(trash.str, trash.size, cur_ptr, exp->replace, pmatch); |
| 8054 | if (trash.len < 0) |
| 8055 | return -1; |
| 8056 | |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 8057 | 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] | 8058 | /* FIXME: if the user adds a newline in the replacement, the |
| 8059 | * index will not be recalculated for now, and the new line |
| 8060 | * will not be counted as a new header. |
| 8061 | */ |
| 8062 | |
| 8063 | cur_end += delta; |
| 8064 | cur_next += delta; |
| 8065 | cur_hdr->len += delta; |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 8066 | http_msg_move_end(&txn->rsp, delta); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8067 | break; |
| 8068 | |
| 8069 | case ACT_REMOVE: |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 8070 | delta = buffer_replace2(rtr->buf, cur_ptr, cur_next, NULL, 0); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8071 | cur_next += delta; |
| 8072 | |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 8073 | http_msg_move_end(&txn->rsp, delta); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8074 | txn->hdr_idx.v[old_idx].next = cur_hdr->next; |
| 8075 | txn->hdr_idx.used--; |
| 8076 | cur_hdr->len = 0; |
| 8077 | cur_end = NULL; /* null-term has been rewritten */ |
Willy Tarreau | 26db59e | 2010-11-28 06:57:24 +0100 | [diff] [blame] | 8078 | cur_idx = old_idx; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8079 | break; |
| 8080 | |
| 8081 | } |
| 8082 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8083 | |
| 8084 | /* keep the link from this header to next one in case of later |
| 8085 | * removal of next header. |
| 8086 | */ |
| 8087 | old_idx = cur_idx; |
| 8088 | } |
| 8089 | return 0; |
| 8090 | } |
| 8091 | |
| 8092 | |
| 8093 | /* Apply the filter to the status line in the response buffer <rtr>. |
| 8094 | * Returns 0 if nothing has been done, 1 if the filter has been applied, |
| 8095 | * or -1 if a replacement resulted in an invalid status line. |
| 8096 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 8097 | 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] | 8098 | { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8099 | char *cur_ptr, *cur_end; |
| 8100 | int done; |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 8101 | struct http_txn *txn = s->txn; |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 8102 | int delta; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8103 | |
| 8104 | |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 8105 | if (unlikely(txn->flags & TX_SVDENY)) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8106 | return 1; |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 8107 | else if (unlikely(txn->flags & TX_SVALLOW) && |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8108 | (exp->action == ACT_ALLOW || |
| 8109 | exp->action == ACT_DENY)) |
| 8110 | return 0; |
| 8111 | else if (exp->action == ACT_REMOVE) |
| 8112 | return 0; |
| 8113 | |
| 8114 | done = 0; |
| 8115 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 8116 | cur_ptr = rtr->buf->p; |
Willy Tarreau | 1ba0e5f | 2010-06-07 13:57:32 +0200 | [diff] [blame] | 8117 | cur_end = cur_ptr + txn->rsp.sl.st.l; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8118 | |
| 8119 | /* Now we have the status line between cur_ptr and cur_end */ |
| 8120 | |
Willy Tarreau | 15a53a4 | 2015-01-21 13:39:42 +0100 | [diff] [blame] | 8121 | 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] | 8122 | switch (exp->action) { |
| 8123 | case ACT_ALLOW: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 8124 | txn->flags |= TX_SVALLOW; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8125 | done = 1; |
| 8126 | break; |
| 8127 | |
| 8128 | case ACT_DENY: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 8129 | txn->flags |= TX_SVDENY; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8130 | done = 1; |
| 8131 | break; |
| 8132 | |
| 8133 | case ACT_REPLACE: |
Sasha Pachev | c600204 | 2014-05-26 12:33:48 -0600 | [diff] [blame] | 8134 | trash.len = exp_replace(trash.str, trash.size, cur_ptr, exp->replace, pmatch); |
| 8135 | if (trash.len < 0) |
| 8136 | return -1; |
| 8137 | |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 8138 | 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] | 8139 | /* FIXME: if the user adds a newline in the replacement, the |
| 8140 | * index will not be recalculated for now, and the new line |
| 8141 | * will not be counted as a new header. |
| 8142 | */ |
| 8143 | |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 8144 | http_msg_move_end(&txn->rsp, delta); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8145 | cur_end += delta; |
Willy Tarreau | 69d8c5d | 2012-05-08 09:44:41 +0200 | [diff] [blame] | 8146 | cur_end = (char *)http_parse_stsline(&txn->rsp, |
Willy Tarreau | 0278576 | 2007-04-03 14:45:44 +0200 | [diff] [blame] | 8147 | HTTP_MSG_RPVER, |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8148 | cur_ptr, cur_end + 1, |
| 8149 | NULL, NULL); |
| 8150 | if (unlikely(!cur_end)) |
| 8151 | return -1; |
| 8152 | |
| 8153 | /* we have a full respnse and we know that we have either a CR |
| 8154 | * or an LF at <ptr>. |
| 8155 | */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 8156 | 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] | 8157 | 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] | 8158 | /* there is no point trying this regex on headers */ |
| 8159 | return 1; |
| 8160 | } |
| 8161 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8162 | return done; |
| 8163 | } |
| 8164 | |
| 8165 | |
| 8166 | |
| 8167 | /* |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 8168 | * 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] | 8169 | * Returns 0 if everything is alright, or -1 in case a replacement lead to an |
| 8170 | * unparsable response. |
| 8171 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 8172 | 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] | 8173 | { |
Willy Tarreau | 192252e | 2015-04-04 01:47:55 +0200 | [diff] [blame] | 8174 | struct session *sess = s->sess; |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 8175 | struct http_txn *txn = s->txn; |
Willy Tarreau | fdb563c | 2010-01-31 15:43:27 +0100 | [diff] [blame] | 8176 | struct hdr_exp *exp; |
| 8177 | |
| 8178 | for (exp = px->rsp_exp; exp; exp = exp->next) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8179 | int ret; |
| 8180 | |
| 8181 | /* |
| 8182 | * The interleaving of transformations and verdicts |
| 8183 | * makes it difficult to decide to continue or stop |
| 8184 | * the evaluation. |
| 8185 | */ |
| 8186 | |
Willy Tarreau | fdb563c | 2010-01-31 15:43:27 +0100 | [diff] [blame] | 8187 | if (txn->flags & TX_SVDENY) |
| 8188 | break; |
| 8189 | |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 8190 | if ((txn->flags & TX_SVALLOW) && |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8191 | (exp->action == ACT_ALLOW || exp->action == ACT_DENY || |
| 8192 | exp->action == ACT_PASS)) { |
| 8193 | exp = exp->next; |
| 8194 | continue; |
| 8195 | } |
| 8196 | |
Willy Tarreau | fdb563c | 2010-01-31 15:43:27 +0100 | [diff] [blame] | 8197 | /* if this filter had a condition, evaluate it now and skip to |
| 8198 | * next filter if the condition does not match. |
| 8199 | */ |
| 8200 | if (exp->cond) { |
Willy Tarreau | 192252e | 2015-04-04 01:47:55 +0200 | [diff] [blame] | 8201 | 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] | 8202 | ret = acl_pass(ret); |
| 8203 | if (((struct acl_cond *)exp->cond)->pol == ACL_COND_UNLESS) |
| 8204 | ret = !ret; |
| 8205 | if (!ret) |
| 8206 | continue; |
| 8207 | } |
| 8208 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8209 | /* Apply the filter to the status line. */ |
Willy Tarreau | fdb563c | 2010-01-31 15:43:27 +0100 | [diff] [blame] | 8210 | ret = apply_filter_to_sts_line(s, rtr, exp); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8211 | if (unlikely(ret < 0)) |
| 8212 | return -1; |
| 8213 | |
| 8214 | if (likely(ret == 0)) { |
| 8215 | /* The filter did not match the response, it can be |
| 8216 | * iterated through all headers. |
| 8217 | */ |
Sasha Pachev | c600204 | 2014-05-26 12:33:48 -0600 | [diff] [blame] | 8218 | if (unlikely(apply_filter_to_resp_headers(s, rtr, exp) < 0)) |
| 8219 | return -1; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8220 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8221 | } |
| 8222 | return 0; |
| 8223 | } |
| 8224 | |
| 8225 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8226 | /* |
Willy Tarreau | 396d2c6 | 2007-11-04 19:30:00 +0100 | [diff] [blame] | 8227 | * 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] | 8228 | * desirable to call it only when needed. This function is also used when we |
| 8229 | * 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] | 8230 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 8231 | void manage_server_side_cookies(struct stream *s, struct channel *res) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8232 | { |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 8233 | struct http_txn *txn = s->txn; |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 8234 | struct session *sess = s->sess; |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 8235 | struct server *srv; |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 8236 | int is_cookie2; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8237 | int cur_idx, old_idx, delta; |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 8238 | char *hdr_beg, *hdr_end, *hdr_next; |
| 8239 | char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8240 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8241 | /* Iterate through the headers. |
| 8242 | * we start with the start line. |
| 8243 | */ |
| 8244 | old_idx = 0; |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 8245 | hdr_next = res->buf->p + hdr_idx_first_pos(&txn->hdr_idx); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8246 | |
| 8247 | while ((cur_idx = txn->hdr_idx.v[old_idx].next)) { |
| 8248 | struct hdr_idx_elem *cur_hdr; |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 8249 | int val; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8250 | |
| 8251 | cur_hdr = &txn->hdr_idx.v[cur_idx]; |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 8252 | hdr_beg = hdr_next; |
| 8253 | hdr_end = hdr_beg + cur_hdr->len; |
| 8254 | hdr_next = hdr_end + cur_hdr->cr + 1; |
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 | /* We have one full header between hdr_beg and hdr_end, and the |
| 8257 | * next header starts at hdr_next. We're only interested in |
| 8258 | * "Set-Cookie" and "Set-Cookie2" headers. |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8259 | */ |
| 8260 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 8261 | is_cookie2 = 0; |
| 8262 | prev = hdr_beg + 10; |
| 8263 | val = http_header_match2(hdr_beg, hdr_end, "Set-Cookie", 10); |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 8264 | if (!val) { |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 8265 | val = http_header_match2(hdr_beg, hdr_end, "Set-Cookie2", 11); |
| 8266 | if (!val) { |
| 8267 | old_idx = cur_idx; |
| 8268 | continue; |
| 8269 | } |
| 8270 | is_cookie2 = 1; |
| 8271 | prev = hdr_beg + 11; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8272 | } |
| 8273 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 8274 | /* OK, right now we know we have a Set-Cookie* at hdr_beg, and |
| 8275 | * <prev> points to the colon. |
| 8276 | */ |
Willy Tarreau | f134831 | 2010-10-07 15:54:11 +0200 | [diff] [blame] | 8277 | txn->flags |= TX_SCK_PRESENT; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8278 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 8279 | /* Maybe we only wanted to see if there was a Set-Cookie (eg: |
| 8280 | * check-cache is enabled) and we are not interested in checking |
| 8281 | * them. Warning, the cookie capture is declared in the frontend. |
Willy Tarreau | fd39dda | 2008-10-17 12:01:58 +0200 | [diff] [blame] | 8282 | */ |
Willy Tarreau | 53a09d5 | 2015-08-10 18:59:40 +0200 | [diff] [blame] | 8283 | if (s->be->cookie_name == NULL && sess->fe->capture_name == NULL) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8284 | return; |
| 8285 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 8286 | /* OK so now we know we have to process this response cookie. |
| 8287 | * The format of the Set-Cookie header is slightly different |
| 8288 | * from the format of the Cookie header in that it does not |
| 8289 | * support the comma as a cookie delimiter (thus the header |
| 8290 | * cannot be folded) because the Expires attribute described in |
| 8291 | * the original Netscape's spec may contain an unquoted date |
| 8292 | * with a comma inside. We have to live with this because |
| 8293 | * many browsers don't support Max-Age and some browsers don't |
| 8294 | * support quoted strings. However the Set-Cookie2 header is |
| 8295 | * clean. |
| 8296 | * |
| 8297 | * We have to keep multiple pointers in order to support cookie |
| 8298 | * removal at the beginning, middle or end of header without |
| 8299 | * corrupting the header (in case of set-cookie2). A special |
| 8300 | * pointer, <scav> points to the beginning of the set-cookie-av |
| 8301 | * fields after the first semi-colon. The <next> pointer points |
| 8302 | * either to the end of line (set-cookie) or next unquoted comma |
| 8303 | * (set-cookie2). All of these headers are valid : |
| 8304 | * |
| 8305 | * Set-Cookie: NAME1 = VALUE 1 ; Secure; Path="/"\r\n |
| 8306 | * Set-Cookie:NAME=VALUE; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT\r\n |
| 8307 | * Set-Cookie: NAME = VALUE ; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT\r\n |
| 8308 | * Set-Cookie2: NAME1 = VALUE 1 ; Max-Age=0, NAME2=VALUE2; Discard\r\n |
| 8309 | * | | | | | | | | | | |
| 8310 | * | | | | | | | | +-> next hdr_end <--+ |
| 8311 | * | | | | | | | +------------> scav |
| 8312 | * | | | | | | +--------------> val_end |
| 8313 | * | | | | | +--------------------> val_beg |
| 8314 | * | | | | +----------------------> equal |
| 8315 | * | | | +------------------------> att_end |
| 8316 | * | | +----------------------------> att_beg |
| 8317 | * | +------------------------------> prev |
| 8318 | * +-----------------------------------------> hdr_beg |
| 8319 | */ |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8320 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 8321 | for (; prev < hdr_end; prev = next) { |
| 8322 | /* Iterate through all cookies on this line */ |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8323 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 8324 | /* find att_beg */ |
| 8325 | att_beg = prev + 1; |
Willy Tarreau | 2235b26 | 2016-11-05 15:50:20 +0100 | [diff] [blame] | 8326 | while (att_beg < hdr_end && HTTP_IS_SPHT(*att_beg)) |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 8327 | att_beg++; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8328 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 8329 | /* find att_end : this is the first character after the last non |
| 8330 | * space before the equal. It may be equal to hdr_end. |
| 8331 | */ |
| 8332 | equal = att_end = att_beg; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8333 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 8334 | while (equal < hdr_end) { |
| 8335 | if (*equal == '=' || *equal == ';' || (is_cookie2 && *equal == ',')) |
| 8336 | break; |
Willy Tarreau | 2235b26 | 2016-11-05 15:50:20 +0100 | [diff] [blame] | 8337 | if (HTTP_IS_SPHT(*equal++)) |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 8338 | continue; |
| 8339 | att_end = equal; |
| 8340 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8341 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 8342 | /* here, <equal> points to '=', a delimitor or the end. <att_end> |
| 8343 | * is between <att_beg> and <equal>, both may be identical. |
| 8344 | */ |
| 8345 | |
| 8346 | /* look for end of cookie if there is an equal sign */ |
| 8347 | if (equal < hdr_end && *equal == '=') { |
| 8348 | /* look for the beginning of the value */ |
| 8349 | val_beg = equal + 1; |
Willy Tarreau | 2235b26 | 2016-11-05 15:50:20 +0100 | [diff] [blame] | 8350 | while (val_beg < hdr_end && HTTP_IS_SPHT(*val_beg)) |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 8351 | val_beg++; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8352 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 8353 | /* find the end of the value, respecting quotes */ |
| 8354 | next = find_cookie_value_end(val_beg, hdr_end); |
| 8355 | |
| 8356 | /* make val_end point to the first white space or delimitor after the value */ |
| 8357 | val_end = next; |
Willy Tarreau | 2235b26 | 2016-11-05 15:50:20 +0100 | [diff] [blame] | 8358 | while (val_end > val_beg && HTTP_IS_SPHT(*(val_end - 1))) |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 8359 | val_end--; |
| 8360 | } else { |
| 8361 | /* <equal> points to next comma, semi-colon or EOL */ |
| 8362 | val_beg = val_end = next = equal; |
| 8363 | } |
| 8364 | |
| 8365 | if (next < hdr_end) { |
| 8366 | /* Set-Cookie2 supports multiple cookies, and <next> points to |
| 8367 | * a colon or semi-colon before the end. So skip all attr-value |
| 8368 | * pairs and look for the next comma. For Set-Cookie, since |
| 8369 | * commas are permitted in values, skip to the end. |
| 8370 | */ |
| 8371 | if (is_cookie2) |
| 8372 | next = find_hdr_value_end(next, hdr_end); |
| 8373 | else |
| 8374 | next = hdr_end; |
| 8375 | } |
| 8376 | |
| 8377 | /* Now everything is as on the diagram above */ |
| 8378 | |
| 8379 | /* Ignore cookies with no equal sign */ |
| 8380 | if (equal == val_end) |
| 8381 | continue; |
| 8382 | |
| 8383 | /* If there are spaces around the equal sign, we need to |
| 8384 | * strip them otherwise we'll get trouble for cookie captures, |
| 8385 | * or even for rewrites. Since this happens extremely rarely, |
| 8386 | * it does not hurt performance. |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8387 | */ |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 8388 | if (unlikely(att_end != equal || val_beg > equal + 1)) { |
| 8389 | int stripped_before = 0; |
| 8390 | int stripped_after = 0; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8391 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 8392 | if (att_end != equal) { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 8393 | stripped_before = buffer_replace2(res->buf, att_end, equal, NULL, 0); |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 8394 | equal += stripped_before; |
| 8395 | val_beg += stripped_before; |
| 8396 | } |
| 8397 | |
| 8398 | if (val_beg > equal + 1) { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 8399 | stripped_after = buffer_replace2(res->buf, equal + 1, val_beg, NULL, 0); |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 8400 | val_beg += stripped_after; |
| 8401 | stripped_before += stripped_after; |
| 8402 | } |
| 8403 | |
| 8404 | val_end += stripped_before; |
| 8405 | next += stripped_before; |
| 8406 | hdr_end += stripped_before; |
| 8407 | hdr_next += stripped_before; |
| 8408 | cur_hdr->len += stripped_before; |
Willy Tarreau | 1fc1f45 | 2011-04-07 22:35:37 +0200 | [diff] [blame] | 8409 | http_msg_move_end(&txn->rsp, stripped_before); |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 8410 | } |
| 8411 | |
| 8412 | /* First, let's see if we want to capture this cookie. We check |
| 8413 | * that we don't already have a server side cookie, because we |
| 8414 | * can only capture one. Also as an optimisation, we ignore |
| 8415 | * cookies shorter than the declared name. |
| 8416 | */ |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 8417 | if (sess->fe->capture_name != NULL && |
Willy Tarreau | 3bac9ff | 2007-03-18 17:31:28 +0100 | [diff] [blame] | 8418 | txn->srv_cookie == NULL && |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 8419 | (val_end - att_beg >= sess->fe->capture_namelen) && |
| 8420 | memcmp(att_beg, sess->fe->capture_name, sess->fe->capture_namelen) == 0) { |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 8421 | int log_len = val_end - att_beg; |
Willy Tarreau | 086b3b4 | 2007-05-13 21:45:51 +0200 | [diff] [blame] | 8422 | if ((txn->srv_cookie = pool_alloc2(pool2_capture)) == NULL) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8423 | Alert("HTTP logging : out of memory.\n"); |
| 8424 | } |
Willy Tarreau | f70fc75 | 2010-11-19 11:27:18 +0100 | [diff] [blame] | 8425 | else { |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 8426 | if (log_len > sess->fe->capture_len) |
| 8427 | log_len = sess->fe->capture_len; |
Willy Tarreau | f70fc75 | 2010-11-19 11:27:18 +0100 | [diff] [blame] | 8428 | memcpy(txn->srv_cookie, att_beg, log_len); |
| 8429 | txn->srv_cookie[log_len] = 0; |
| 8430 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8431 | } |
| 8432 | |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 8433 | srv = objt_server(s->target); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8434 | /* now check if we need to process it for persistence */ |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 8435 | if (!(s->flags & SF_IGNORE_PRST) && |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 8436 | (att_end - att_beg == s->be->cookie_len) && (s->be->cookie_name != NULL) && |
| 8437 | (memcmp(att_beg, s->be->cookie_name, att_end - att_beg) == 0)) { |
Willy Tarreau | f134831 | 2010-10-07 15:54:11 +0200 | [diff] [blame] | 8438 | /* assume passive cookie by default */ |
| 8439 | txn->flags &= ~TX_SCK_MASK; |
| 8440 | txn->flags |= TX_SCK_FOUND; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8441 | |
| 8442 | /* If the cookie is in insert mode on a known server, we'll delete |
| 8443 | * this occurrence because we'll insert another one later. |
| 8444 | * 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] | 8445 | * a direct access. |
| 8446 | */ |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 8447 | if (s->be->ck_opts & PR_CK_PSV) { |
Willy Tarreau | ba4c5be | 2010-10-23 12:46:42 +0200 | [diff] [blame] | 8448 | /* The "preserve" flag was set, we don't want to touch the |
| 8449 | * server's cookie. |
| 8450 | */ |
| 8451 | } |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 8452 | else if ((srv && (s->be->ck_opts & PR_CK_INS)) || |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 8453 | ((s->flags & SF_DIRECT) && (s->be->ck_opts & PR_CK_IND))) { |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 8454 | /* this cookie must be deleted */ |
| 8455 | if (*prev == ':' && next == hdr_end) { |
| 8456 | /* whole header */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 8457 | delta = buffer_replace2(res->buf, hdr_beg, hdr_next, NULL, 0); |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 8458 | txn->hdr_idx.v[old_idx].next = cur_hdr->next; |
| 8459 | txn->hdr_idx.used--; |
| 8460 | cur_hdr->len = 0; |
Willy Tarreau | 26db59e | 2010-11-28 06:57:24 +0100 | [diff] [blame] | 8461 | cur_idx = old_idx; |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 8462 | hdr_next += delta; |
| 8463 | http_msg_move_end(&txn->rsp, delta); |
| 8464 | /* note: while both invalid now, <next> and <hdr_end> |
| 8465 | * are still equal, so the for() will stop as expected. |
| 8466 | */ |
| 8467 | } else { |
| 8468 | /* just remove the value */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 8469 | int delta = del_hdr_value(res->buf, &prev, next); |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 8470 | next = prev; |
| 8471 | hdr_end += delta; |
| 8472 | hdr_next += delta; |
| 8473 | cur_hdr->len += delta; |
| 8474 | http_msg_move_end(&txn->rsp, delta); |
| 8475 | } |
Willy Tarreau | f134831 | 2010-10-07 15:54:11 +0200 | [diff] [blame] | 8476 | txn->flags &= ~TX_SCK_MASK; |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 8477 | txn->flags |= TX_SCK_DELETED; |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 8478 | /* and go on with next cookie */ |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8479 | } |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 8480 | else if (srv && srv->cookie && (s->be->ck_opts & PR_CK_RW)) { |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 8481 | /* replace bytes val_beg->val_end with the cookie name associated |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8482 | * with this server since we know it. |
| 8483 | */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 8484 | 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] | 8485 | next += delta; |
| 8486 | hdr_end += delta; |
| 8487 | hdr_next += delta; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8488 | cur_hdr->len += delta; |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 8489 | http_msg_move_end(&txn->rsp, delta); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8490 | |
Willy Tarreau | f134831 | 2010-10-07 15:54:11 +0200 | [diff] [blame] | 8491 | txn->flags &= ~TX_SCK_MASK; |
| 8492 | txn->flags |= TX_SCK_REPLACED; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8493 | } |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 8494 | else if (srv && srv->cookie && (s->be->ck_opts & PR_CK_PFX)) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8495 | /* insert the cookie name associated with this server |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 8496 | * before existing cookie, and insert a delimiter between them.. |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8497 | */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 8498 | 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] | 8499 | next += delta; |
| 8500 | hdr_end += delta; |
| 8501 | hdr_next += delta; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8502 | cur_hdr->len += delta; |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 8503 | http_msg_move_end(&txn->rsp, delta); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8504 | |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 8505 | val_beg[srv->cklen] = COOKIE_DELIM; |
Willy Tarreau | f134831 | 2010-10-07 15:54:11 +0200 | [diff] [blame] | 8506 | txn->flags &= ~TX_SCK_MASK; |
| 8507 | txn->flags |= TX_SCK_REPLACED; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8508 | } |
| 8509 | } |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 8510 | /* that's done for this cookie, check the next one on the same |
| 8511 | * line when next != hdr_end (only if is_cookie2). |
| 8512 | */ |
| 8513 | } |
| 8514 | /* check next header */ |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8515 | old_idx = cur_idx; |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 8516 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8517 | } |
| 8518 | |
| 8519 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8520 | /* |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 8521 | * Check if response is cacheable or not. Updates s->flags. |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8522 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 8523 | void check_response_for_cacheability(struct stream *s, struct channel *rtr) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8524 | { |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 8525 | struct http_txn *txn = s->txn; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8526 | char *p1, *p2; |
| 8527 | |
| 8528 | char *cur_ptr, *cur_end, *cur_next; |
| 8529 | int cur_idx; |
| 8530 | |
Willy Tarreau | 5df5187 | 2007-11-25 16:20:08 +0100 | [diff] [blame] | 8531 | if (!(txn->flags & TX_CACHEABLE)) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8532 | return; |
| 8533 | |
| 8534 | /* Iterate through the headers. |
| 8535 | * we start with the start line. |
| 8536 | */ |
| 8537 | cur_idx = 0; |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 8538 | cur_next = rtr->buf->p + hdr_idx_first_pos(&txn->hdr_idx); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8539 | |
| 8540 | while ((cur_idx = txn->hdr_idx.v[cur_idx].next)) { |
| 8541 | struct hdr_idx_elem *cur_hdr; |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 8542 | int val; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8543 | |
| 8544 | cur_hdr = &txn->hdr_idx.v[cur_idx]; |
| 8545 | cur_ptr = cur_next; |
| 8546 | cur_end = cur_ptr + cur_hdr->len; |
| 8547 | cur_next = cur_end + cur_hdr->cr + 1; |
| 8548 | |
| 8549 | /* We have one full header between cur_ptr and cur_end, and the |
| 8550 | * next header starts at cur_next. We're only interested in |
| 8551 | * "Cookie:" headers. |
| 8552 | */ |
| 8553 | |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 8554 | val = http_header_match2(cur_ptr, cur_end, "Pragma", 6); |
| 8555 | if (val) { |
| 8556 | if ((cur_end - (cur_ptr + val) >= 8) && |
| 8557 | strncasecmp(cur_ptr + val, "no-cache", 8) == 0) { |
| 8558 | txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK; |
| 8559 | return; |
| 8560 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8561 | } |
| 8562 | |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 8563 | val = http_header_match2(cur_ptr, cur_end, "Cache-control", 13); |
| 8564 | if (!val) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8565 | continue; |
| 8566 | |
| 8567 | /* OK, right now we know we have a cache-control header at cur_ptr */ |
| 8568 | |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 8569 | p1 = cur_ptr + val; /* first non-space char after 'cache-control:' */ |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8570 | |
| 8571 | if (p1 >= cur_end) /* no more info */ |
| 8572 | continue; |
| 8573 | |
| 8574 | /* p1 is at the beginning of the value */ |
| 8575 | p2 = p1; |
| 8576 | |
Willy Tarreau | 8f8e645 | 2007-06-17 21:51:38 +0200 | [diff] [blame] | 8577 | while (p2 < cur_end && *p2 != '=' && *p2 != ',' && !isspace((unsigned char)*p2)) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8578 | p2++; |
| 8579 | |
| 8580 | /* we have a complete value between p1 and p2 */ |
| 8581 | if (p2 < cur_end && *p2 == '=') { |
| 8582 | /* we have something of the form no-cache="set-cookie" */ |
| 8583 | if ((cur_end - p1 >= 21) && |
| 8584 | strncasecmp(p1, "no-cache=\"set-cookie", 20) == 0 |
| 8585 | && (p1[20] == '"' || p1[20] == ',')) |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 8586 | txn->flags &= ~TX_CACHE_COOK; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8587 | continue; |
| 8588 | } |
| 8589 | |
| 8590 | /* OK, so we know that either p2 points to the end of string or to a comma */ |
| 8591 | if (((p2 - p1 == 7) && strncasecmp(p1, "private", 7) == 0) || |
Willy Tarreau | 5b15f90 | 2013-07-04 12:46:56 +0200 | [diff] [blame] | 8592 | ((p2 - p1 == 8) && strncasecmp(p1, "no-cache", 8) == 0) || |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8593 | ((p2 - p1 == 8) && strncasecmp(p1, "no-store", 8) == 0) || |
| 8594 | ((p2 - p1 == 9) && strncasecmp(p1, "max-age=0", 9) == 0) || |
| 8595 | ((p2 - p1 == 10) && strncasecmp(p1, "s-maxage=0", 10) == 0)) { |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 8596 | txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8597 | return; |
| 8598 | } |
| 8599 | |
| 8600 | if ((p2 - p1 == 6) && strncasecmp(p1, "public", 6) == 0) { |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 8601 | txn->flags |= TX_CACHEABLE | TX_CACHE_COOK; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8602 | continue; |
| 8603 | } |
| 8604 | } |
| 8605 | } |
| 8606 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 8607 | |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 8608 | /* |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 8609 | * 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] | 8610 | * for the current backend. |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 8611 | * |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 8612 | * 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] | 8613 | * uri_auth field is valid. |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 8614 | * |
Krzysztof Piotr Oledzki | 8c8bd45 | 2010-01-29 19:29:32 +0100 | [diff] [blame] | 8615 | * Returns 1 if stats should be provided, otherwise 0. |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 8616 | */ |
Willy Tarreau | 295a837 | 2011-03-10 11:25:07 +0100 | [diff] [blame] | 8617 | 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] | 8618 | { |
| 8619 | struct uri_auth *uri_auth = backend->uri_auth; |
Willy Tarreau | 3a215be | 2012-03-09 21:39:51 +0100 | [diff] [blame] | 8620 | struct http_msg *msg = &txn->req; |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 8621 | const char *uri = msg->chn->buf->p+ msg->sl.rq.u; |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 8622 | |
Krzysztof Piotr Oledzki | 8c8bd45 | 2010-01-29 19:29:32 +0100 | [diff] [blame] | 8623 | if (!uri_auth) |
| 8624 | return 0; |
| 8625 | |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 8626 | 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] | 8627 | return 0; |
| 8628 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 8629 | /* check URI size */ |
Willy Tarreau | 3a215be | 2012-03-09 21:39:51 +0100 | [diff] [blame] | 8630 | if (uri_auth->uri_len > msg->sl.rq.u_l) |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 8631 | return 0; |
| 8632 | |
Willy Tarreau | 414e9bb | 2013-11-23 00:30:38 +0100 | [diff] [blame] | 8633 | if (memcmp(uri, uri_auth->uri_prefix, uri_auth->uri_len) != 0) |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 8634 | return 0; |
| 8635 | |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 8636 | return 1; |
| 8637 | } |
| 8638 | |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 8639 | /* |
| 8640 | * 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] | 8641 | * By default it tries to report the error position as msg->err_pos. However if |
| 8642 | * this one is not set, it will then report msg->next, which is the last known |
| 8643 | * 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] | 8644 | * displays buffers as a contiguous area starting at buf->p. |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 8645 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 8646 | void http_capture_bad_message(struct error_snapshot *es, struct stream *s, |
Willy Tarreau | 8a0cef2 | 2012-03-09 13:39:23 +0100 | [diff] [blame] | 8647 | struct http_msg *msg, |
Willy Tarreau | 3770f23 | 2013-12-07 00:01:53 +0100 | [diff] [blame] | 8648 | enum ht_state state, struct proxy *other_end) |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 8649 | { |
Willy Tarreau | 9ad7bd4 | 2015-04-03 19:19:59 +0200 | [diff] [blame] | 8650 | struct session *sess = strm_sess(s); |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 8651 | struct channel *chn = msg->chn; |
Willy Tarreau | 69d8c5d | 2012-05-08 09:44:41 +0200 | [diff] [blame] | 8652 | int len1, len2; |
Willy Tarreau | 8a0cef2 | 2012-03-09 13:39:23 +0100 | [diff] [blame] | 8653 | |
Willy Tarreau | f3764b7 | 2016-03-31 13:45:10 +0200 | [diff] [blame] | 8654 | es->len = MIN(chn->buf->i, global.tune.bufsize); |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 8655 | len1 = chn->buf->data + chn->buf->size - chn->buf->p; |
Willy Tarreau | 69d8c5d | 2012-05-08 09:44:41 +0200 | [diff] [blame] | 8656 | len1 = MIN(len1, es->len); |
| 8657 | len2 = es->len - len1; /* remaining data if buffer wraps */ |
| 8658 | |
Willy Tarreau | f3764b7 | 2016-03-31 13:45:10 +0200 | [diff] [blame] | 8659 | if (!es->buf) |
| 8660 | es->buf = malloc(global.tune.bufsize); |
| 8661 | |
| 8662 | if (es->buf) { |
| 8663 | memcpy(es->buf, chn->buf->p, len1); |
| 8664 | if (len2) |
| 8665 | memcpy(es->buf + len1, chn->buf->data, len2); |
| 8666 | } |
Willy Tarreau | 81f2fb9 | 2010-12-12 13:09:08 +0100 | [diff] [blame] | 8667 | |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 8668 | if (msg->err_pos >= 0) |
Willy Tarreau | 69d8c5d | 2012-05-08 09:44:41 +0200 | [diff] [blame] | 8669 | es->pos = msg->err_pos; |
Willy Tarreau | 81f2fb9 | 2010-12-12 13:09:08 +0100 | [diff] [blame] | 8670 | else |
Willy Tarreau | 69d8c5d | 2012-05-08 09:44:41 +0200 | [diff] [blame] | 8671 | es->pos = msg->next; |
Willy Tarreau | 81f2fb9 | 2010-12-12 13:09:08 +0100 | [diff] [blame] | 8672 | |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 8673 | es->when = date; // user-visible date |
| 8674 | es->sid = s->uniq_id; |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 8675 | es->srv = objt_server(s->target); |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 8676 | es->oe = other_end; |
Willy Tarreau | 9ad7bd4 | 2015-04-03 19:19:59 +0200 | [diff] [blame] | 8677 | if (objt_conn(sess->origin)) |
| 8678 | es->src = __objt_conn(sess->origin)->addr.from; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 8679 | else |
| 8680 | memset(&es->src, 0, sizeof(es->src)); |
| 8681 | |
Willy Tarreau | 078272e | 2010-12-12 12:46:33 +0100 | [diff] [blame] | 8682 | es->state = state; |
Willy Tarreau | 10479e4 | 2010-12-12 14:00:34 +0100 | [diff] [blame] | 8683 | es->ev_id = error_snapshot_id++; |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 8684 | es->b_flags = chn->flags; |
Willy Tarreau | d04b1bc | 2012-05-08 11:03:10 +0200 | [diff] [blame] | 8685 | es->s_flags = s->flags; |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 8686 | es->t_flags = s->txn->flags; |
Willy Tarreau | d04b1bc | 2012-05-08 11:03:10 +0200 | [diff] [blame] | 8687 | es->m_flags = msg->flags; |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 8688 | es->b_out = chn->buf->o; |
| 8689 | es->b_wrap = chn->buf->data + chn->buf->size - chn->buf->p; |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 8690 | es->b_tot = chn->total; |
Willy Tarreau | d04b1bc | 2012-05-08 11:03:10 +0200 | [diff] [blame] | 8691 | es->m_clen = msg->chunk_len; |
| 8692 | es->m_blen = msg->body_len; |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 8693 | } |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 8694 | |
Willy Tarreau | 294c473 | 2011-12-16 21:35:50 +0100 | [diff] [blame] | 8695 | /* Return in <vptr> and <vlen> the pointer and length of occurrence <occ> of |
| 8696 | * header whose name is <hname> of length <hlen>. If <ctx> is null, lookup is |
| 8697 | * performed over the whole headers. Otherwise it must contain a valid header |
| 8698 | * context, initialised with ctx->idx=0 for the first lookup in a series. If |
| 8699 | * <occ> is positive or null, occurrence #occ from the beginning (or last ctx) |
| 8700 | * is returned. Occ #0 and #1 are equivalent. If <occ> is negative (and no less |
| 8701 | * 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] | 8702 | * -1. The value fetch stops at commas, so this function is suited for use with |
| 8703 | * list headers. |
Willy Tarreau | 294c473 | 2011-12-16 21:35:50 +0100 | [diff] [blame] | 8704 | * 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] | 8705 | */ |
Willy Tarreau | 185b5c4 | 2012-04-26 15:11:51 +0200 | [diff] [blame] | 8706 | 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] | 8707 | struct hdr_idx *idx, int occ, |
| 8708 | struct hdr_ctx *ctx, char **vptr, int *vlen) |
Willy Tarreau | bce7088 | 2009-09-07 11:51:47 +0200 | [diff] [blame] | 8709 | { |
Willy Tarreau | 294c473 | 2011-12-16 21:35:50 +0100 | [diff] [blame] | 8710 | struct hdr_ctx local_ctx; |
| 8711 | char *ptr_hist[MAX_HDR_HISTORY]; |
| 8712 | int len_hist[MAX_HDR_HISTORY]; |
Willy Tarreau | bce7088 | 2009-09-07 11:51:47 +0200 | [diff] [blame] | 8713 | unsigned int hist_ptr; |
Willy Tarreau | 294c473 | 2011-12-16 21:35:50 +0100 | [diff] [blame] | 8714 | int found; |
Willy Tarreau | bce7088 | 2009-09-07 11:51:47 +0200 | [diff] [blame] | 8715 | |
Willy Tarreau | 294c473 | 2011-12-16 21:35:50 +0100 | [diff] [blame] | 8716 | if (!ctx) { |
| 8717 | local_ctx.idx = 0; |
| 8718 | ctx = &local_ctx; |
| 8719 | } |
| 8720 | |
Willy Tarreau | bce7088 | 2009-09-07 11:51:47 +0200 | [diff] [blame] | 8721 | if (occ >= 0) { |
Willy Tarreau | 294c473 | 2011-12-16 21:35:50 +0100 | [diff] [blame] | 8722 | /* search from the beginning */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 8723 | while (http_find_header2(hname, hlen, msg->chn->buf->p, idx, ctx)) { |
Willy Tarreau | bce7088 | 2009-09-07 11:51:47 +0200 | [diff] [blame] | 8724 | occ--; |
| 8725 | if (occ <= 0) { |
Willy Tarreau | 294c473 | 2011-12-16 21:35:50 +0100 | [diff] [blame] | 8726 | *vptr = ctx->line + ctx->val; |
| 8727 | *vlen = ctx->vlen; |
| 8728 | return 1; |
Willy Tarreau | bce7088 | 2009-09-07 11:51:47 +0200 | [diff] [blame] | 8729 | } |
| 8730 | } |
Willy Tarreau | 294c473 | 2011-12-16 21:35:50 +0100 | [diff] [blame] | 8731 | return 0; |
Willy Tarreau | bce7088 | 2009-09-07 11:51:47 +0200 | [diff] [blame] | 8732 | } |
| 8733 | |
| 8734 | /* negative occurrence, we scan all the list then walk back */ |
| 8735 | if (-occ > MAX_HDR_HISTORY) |
| 8736 | return 0; |
| 8737 | |
Willy Tarreau | 294c473 | 2011-12-16 21:35:50 +0100 | [diff] [blame] | 8738 | found = hist_ptr = 0; |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 8739 | while (http_find_header2(hname, hlen, msg->chn->buf->p, idx, ctx)) { |
Willy Tarreau | 294c473 | 2011-12-16 21:35:50 +0100 | [diff] [blame] | 8740 | ptr_hist[hist_ptr] = ctx->line + ctx->val; |
| 8741 | len_hist[hist_ptr] = ctx->vlen; |
| 8742 | if (++hist_ptr >= MAX_HDR_HISTORY) |
Willy Tarreau | bce7088 | 2009-09-07 11:51:47 +0200 | [diff] [blame] | 8743 | hist_ptr = 0; |
| 8744 | found++; |
| 8745 | } |
| 8746 | if (-occ > found) |
| 8747 | return 0; |
| 8748 | /* 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] | 8749 | * find occurrence -occ. 0 <= hist_ptr < MAX_HDR_HISTORY, and we have |
| 8750 | * -10 <= occ <= -1. So we have to check [hist_ptr%MAX_HDR_HISTORY+occ] |
| 8751 | * to remain in the 0..9 range. |
Willy Tarreau | bce7088 | 2009-09-07 11:51:47 +0200 | [diff] [blame] | 8752 | */ |
Willy Tarreau | 67dad27 | 2013-06-12 22:27:44 +0200 | [diff] [blame] | 8753 | hist_ptr += occ + MAX_HDR_HISTORY; |
Willy Tarreau | bce7088 | 2009-09-07 11:51:47 +0200 | [diff] [blame] | 8754 | if (hist_ptr >= MAX_HDR_HISTORY) |
| 8755 | hist_ptr -= MAX_HDR_HISTORY; |
Willy Tarreau | 294c473 | 2011-12-16 21:35:50 +0100 | [diff] [blame] | 8756 | *vptr = ptr_hist[hist_ptr]; |
| 8757 | *vlen = len_hist[hist_ptr]; |
| 8758 | return 1; |
Willy Tarreau | bce7088 | 2009-09-07 11:51:47 +0200 | [diff] [blame] | 8759 | } |
| 8760 | |
Willy Tarreau | 04ff9f1 | 2013-06-10 18:39:42 +0200 | [diff] [blame] | 8761 | /* Return in <vptr> and <vlen> the pointer and length of occurrence <occ> of |
| 8762 | * header whose name is <hname> of length <hlen>. If <ctx> is null, lookup is |
| 8763 | * performed over the whole headers. Otherwise it must contain a valid header |
| 8764 | * context, initialised with ctx->idx=0 for the first lookup in a series. If |
| 8765 | * <occ> is positive or null, occurrence #occ from the beginning (or last ctx) |
| 8766 | * is returned. Occ #0 and #1 are equivalent. If <occ> is negative (and no less |
| 8767 | * than -MAX_HDR_HISTORY), the occurrence is counted from the last one which is |
| 8768 | * -1. This function differs from http_get_hdr() in that it only returns full |
| 8769 | * line header values and does not stop at commas. |
| 8770 | * The return value is 0 if nothing was found, or non-zero otherwise. |
| 8771 | */ |
| 8772 | unsigned int http_get_fhdr(const struct http_msg *msg, const char *hname, int hlen, |
| 8773 | struct hdr_idx *idx, int occ, |
| 8774 | struct hdr_ctx *ctx, char **vptr, int *vlen) |
| 8775 | { |
| 8776 | struct hdr_ctx local_ctx; |
| 8777 | char *ptr_hist[MAX_HDR_HISTORY]; |
| 8778 | int len_hist[MAX_HDR_HISTORY]; |
| 8779 | unsigned int hist_ptr; |
| 8780 | int found; |
| 8781 | |
| 8782 | if (!ctx) { |
| 8783 | local_ctx.idx = 0; |
| 8784 | ctx = &local_ctx; |
| 8785 | } |
| 8786 | |
| 8787 | if (occ >= 0) { |
| 8788 | /* search from the beginning */ |
| 8789 | while (http_find_full_header2(hname, hlen, msg->chn->buf->p, idx, ctx)) { |
| 8790 | occ--; |
| 8791 | if (occ <= 0) { |
| 8792 | *vptr = ctx->line + ctx->val; |
| 8793 | *vlen = ctx->vlen; |
| 8794 | return 1; |
| 8795 | } |
| 8796 | } |
| 8797 | return 0; |
| 8798 | } |
| 8799 | |
| 8800 | /* negative occurrence, we scan all the list then walk back */ |
| 8801 | if (-occ > MAX_HDR_HISTORY) |
| 8802 | return 0; |
| 8803 | |
| 8804 | found = hist_ptr = 0; |
| 8805 | while (http_find_full_header2(hname, hlen, msg->chn->buf->p, idx, ctx)) { |
| 8806 | ptr_hist[hist_ptr] = ctx->line + ctx->val; |
| 8807 | len_hist[hist_ptr] = ctx->vlen; |
| 8808 | if (++hist_ptr >= MAX_HDR_HISTORY) |
| 8809 | hist_ptr = 0; |
| 8810 | found++; |
| 8811 | } |
| 8812 | if (-occ > found) |
| 8813 | return 0; |
Nenad Merdanovic | 69ad4b9 | 2016-03-29 13:14:30 +0200 | [diff] [blame] | 8814 | |
Willy Tarreau | 04ff9f1 | 2013-06-10 18:39:42 +0200 | [diff] [blame] | 8815 | /* 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] | 8816 | * find occurrence -occ. 0 <= hist_ptr < MAX_HDR_HISTORY, and we have |
| 8817 | * -10 <= occ <= -1. So we have to check [hist_ptr%MAX_HDR_HISTORY+occ] |
| 8818 | * to remain in the 0..9 range. |
Willy Tarreau | 04ff9f1 | 2013-06-10 18:39:42 +0200 | [diff] [blame] | 8819 | */ |
Nenad Merdanovic | 69ad4b9 | 2016-03-29 13:14:30 +0200 | [diff] [blame] | 8820 | hist_ptr += occ + MAX_HDR_HISTORY; |
Willy Tarreau | 04ff9f1 | 2013-06-10 18:39:42 +0200 | [diff] [blame] | 8821 | if (hist_ptr >= MAX_HDR_HISTORY) |
| 8822 | hist_ptr -= MAX_HDR_HISTORY; |
| 8823 | *vptr = ptr_hist[hist_ptr]; |
| 8824 | *vlen = len_hist[hist_ptr]; |
| 8825 | return 1; |
| 8826 | } |
| 8827 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 8828 | /* |
Willy Tarreau | e92693a | 2012-09-24 21:13:39 +0200 | [diff] [blame] | 8829 | * Print a debug line with a header. Always stop at the first CR or LF char, |
| 8830 | * so it is safe to pass it a full buffer if needed. If <err> is not NULL, an |
| 8831 | * arrow is printed after the line which contains the pointer. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 8832 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 8833 | 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] | 8834 | { |
Willy Tarreau | 9ad7bd4 | 2015-04-03 19:19:59 +0200 | [diff] [blame] | 8835 | struct session *sess = strm_sess(s); |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 8836 | int max; |
Willy Tarreau | 9ad7bd4 | 2015-04-03 19:19:59 +0200 | [diff] [blame] | 8837 | |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 8838 | 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] | 8839 | dir, |
Willy Tarreau | 9ad7bd4 | 2015-04-03 19:19:59 +0200 | [diff] [blame] | 8840 | 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] | 8841 | 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] | 8842 | |
| 8843 | for (max = 0; start + max < end; max++) |
| 8844 | if (start[max] == '\r' || start[max] == '\n') |
| 8845 | break; |
| 8846 | |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 8847 | UBOUND(max, trash.size - trash.len - 3); |
| 8848 | trash.len += strlcpy2(trash.str + trash.len, start, max + 1); |
| 8849 | trash.str[trash.len++] = '\n'; |
Willy Tarreau | 89efaed | 2013-12-13 15:14:55 +0100 | [diff] [blame] | 8850 | shut_your_big_mouth_gcc(write(1, trash.str, trash.len)); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 8851 | } |
| 8852 | |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 8853 | |
| 8854 | /* Allocate a new HTTP transaction for stream <s> unless there is one already. |
| 8855 | * The hdr_idx is allocated as well. In case of allocation failure, everything |
| 8856 | * allocated is freed and NULL is returned. Otherwise the new transaction is |
| 8857 | * assigned to the stream and returned. |
| 8858 | */ |
| 8859 | struct http_txn *http_alloc_txn(struct stream *s) |
| 8860 | { |
| 8861 | struct http_txn *txn = s->txn; |
| 8862 | |
| 8863 | if (txn) |
| 8864 | return txn; |
| 8865 | |
| 8866 | txn = pool_alloc2(pool2_http_txn); |
| 8867 | if (!txn) |
| 8868 | return txn; |
| 8869 | |
| 8870 | txn->hdr_idx.size = global.tune.max_http_hdr; |
| 8871 | txn->hdr_idx.v = pool_alloc2(pool2_hdr_idx); |
| 8872 | if (!txn->hdr_idx.v) { |
| 8873 | pool_free2(pool2_http_txn, txn); |
| 8874 | return NULL; |
| 8875 | } |
| 8876 | |
| 8877 | s->txn = txn; |
| 8878 | return txn; |
| 8879 | } |
| 8880 | |
Thierry FOURNIER | fd50f0b | 2015-09-25 18:53:18 +0200 | [diff] [blame] | 8881 | void http_txn_reset_req(struct http_txn *txn) |
| 8882 | { |
| 8883 | txn->req.flags = 0; |
| 8884 | txn->req.sol = txn->req.eol = txn->req.eoh = 0; /* relative to the buffer */ |
| 8885 | txn->req.next = 0; |
| 8886 | txn->req.chunk_len = 0LL; |
| 8887 | txn->req.body_len = 0LL; |
| 8888 | txn->req.msg_state = HTTP_MSG_RQBEFORE; /* at the very beginning of the request */ |
| 8889 | } |
| 8890 | |
| 8891 | void http_txn_reset_res(struct http_txn *txn) |
| 8892 | { |
| 8893 | txn->rsp.flags = 0; |
| 8894 | txn->rsp.sol = txn->rsp.eol = txn->rsp.eoh = 0; /* relative to the buffer */ |
| 8895 | txn->rsp.next = 0; |
| 8896 | txn->rsp.chunk_len = 0LL; |
| 8897 | txn->rsp.body_len = 0LL; |
| 8898 | txn->rsp.msg_state = HTTP_MSG_RPBEFORE; /* at the very beginning of the response */ |
| 8899 | } |
| 8900 | |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 8901 | /* |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 8902 | * 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] | 8903 | * the required fields are properly allocated and that we only need to (re)init |
| 8904 | * them. This should be used before processing any new request. |
| 8905 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 8906 | void http_init_txn(struct stream *s) |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 8907 | { |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 8908 | struct http_txn *txn = s->txn; |
Willy Tarreau | d0d8da9 | 2015-04-04 02:10:38 +0200 | [diff] [blame] | 8909 | struct proxy *fe = strm_fe(s); |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 8910 | |
| 8911 | txn->flags = 0; |
| 8912 | txn->status = -1; |
| 8913 | |
Willy Tarreau | f64d141 | 2010-10-07 20:06:11 +0200 | [diff] [blame] | 8914 | txn->cookie_first_date = 0; |
| 8915 | txn->cookie_last_date = 0; |
| 8916 | |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 8917 | txn->srv_cookie = NULL; |
| 8918 | txn->cli_cookie = NULL; |
| 8919 | txn->uri = NULL; |
| 8920 | |
Thierry FOURNIER | fd50f0b | 2015-09-25 18:53:18 +0200 | [diff] [blame] | 8921 | http_txn_reset_req(txn); |
| 8922 | http_txn_reset_res(txn); |
| 8923 | |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 8924 | txn->req.chn = &s->req; |
| 8925 | txn->rsp.chn = &s->res; |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 8926 | |
| 8927 | txn->auth.method = HTTP_AUTH_UNKNOWN; |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 8928 | |
| 8929 | txn->req.err_pos = txn->rsp.err_pos = -2; /* block buggy requests/responses */ |
| 8930 | if (fe->options2 & PR_O2_REQBUG_OK) |
| 8931 | txn->req.err_pos = -1; /* let buggy requests pass */ |
| 8932 | |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 8933 | if (txn->hdr_idx.v) |
| 8934 | hdr_idx_init(&txn->hdr_idx); |
Thierry FOURNIER | 4834bc7 | 2015-06-06 19:29:07 +0200 | [diff] [blame] | 8935 | |
| 8936 | vars_init(&s->vars_txn, SCOPE_TXN); |
| 8937 | vars_init(&s->vars_reqres, SCOPE_REQ); |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 8938 | } |
| 8939 | |
| 8940 | /* to be used at the end of a transaction */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 8941 | void http_end_txn(struct stream *s) |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 8942 | { |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 8943 | struct http_txn *txn = s->txn; |
Willy Tarreau | d0d8da9 | 2015-04-04 02:10:38 +0200 | [diff] [blame] | 8944 | struct proxy *fe = strm_fe(s); |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 8945 | |
| 8946 | /* these ones will have been dynamically allocated */ |
| 8947 | pool_free2(pool2_requri, txn->uri); |
| 8948 | pool_free2(pool2_capture, txn->cli_cookie); |
| 8949 | pool_free2(pool2_capture, txn->srv_cookie); |
William Lallemand | a73203e | 2012-03-12 12:48:57 +0100 | [diff] [blame] | 8950 | pool_free2(pool2_uniqueid, s->unique_id); |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 8951 | |
William Lallemand | a73203e | 2012-03-12 12:48:57 +0100 | [diff] [blame] | 8952 | s->unique_id = NULL; |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 8953 | txn->uri = NULL; |
| 8954 | txn->srv_cookie = NULL; |
| 8955 | txn->cli_cookie = NULL; |
Willy Tarreau | 4602363 | 2010-01-07 22:51:47 +0100 | [diff] [blame] | 8956 | |
Willy Tarreau | cb7dd01 | 2015-04-03 22:16:32 +0200 | [diff] [blame] | 8957 | if (s->req_cap) { |
Willy Tarreau | 4602363 | 2010-01-07 22:51:47 +0100 | [diff] [blame] | 8958 | struct cap_hdr *h; |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 8959 | for (h = fe->req_cap; h; h = h->next) |
Willy Tarreau | cb7dd01 | 2015-04-03 22:16:32 +0200 | [diff] [blame] | 8960 | pool_free2(h->pool, s->req_cap[h->index]); |
| 8961 | memset(s->req_cap, 0, fe->nb_req_cap * sizeof(void *)); |
Willy Tarreau | 4602363 | 2010-01-07 22:51:47 +0100 | [diff] [blame] | 8962 | } |
| 8963 | |
Willy Tarreau | cb7dd01 | 2015-04-03 22:16:32 +0200 | [diff] [blame] | 8964 | if (s->res_cap) { |
Willy Tarreau | 4602363 | 2010-01-07 22:51:47 +0100 | [diff] [blame] | 8965 | struct cap_hdr *h; |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 8966 | for (h = fe->rsp_cap; h; h = h->next) |
Willy Tarreau | cb7dd01 | 2015-04-03 22:16:32 +0200 | [diff] [blame] | 8967 | pool_free2(h->pool, s->res_cap[h->index]); |
| 8968 | memset(s->res_cap, 0, fe->nb_rsp_cap * sizeof(void *)); |
Willy Tarreau | 4602363 | 2010-01-07 22:51:47 +0100 | [diff] [blame] | 8969 | } |
| 8970 | |
Willy Tarreau | 6204cd9 | 2016-03-10 16:33:04 +0100 | [diff] [blame] | 8971 | vars_prune(&s->vars_txn, s->sess, s); |
| 8972 | vars_prune(&s->vars_reqres, s->sess, s); |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 8973 | } |
| 8974 | |
| 8975 | /* 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] | 8976 | void http_reset_txn(struct stream *s) |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 8977 | { |
| 8978 | http_end_txn(s); |
| 8979 | http_init_txn(s); |
| 8980 | |
Thierry FOURNIER | bc4c1ac | 2015-02-25 13:36:14 +0100 | [diff] [blame] | 8981 | /* reinitialise the current rule list pointer to NULL. We are sure that |
| 8982 | * any rulelist match the NULL pointer. |
| 8983 | */ |
| 8984 | s->current_rule_list = NULL; |
| 8985 | |
Willy Tarreau | d0d8da9 | 2015-04-04 02:10:38 +0200 | [diff] [blame] | 8986 | s->be = strm_fe(s); |
| 8987 | s->logs.logwait = strm_fe(s)->to_log; |
Willy Tarreau | abcd514 | 2013-06-11 17:18:02 +0200 | [diff] [blame] | 8988 | s->logs.level = 0; |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 8989 | stream_del_srv_conn(s); |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 8990 | s->target = NULL; |
Emeric Brun | b982a3d | 2010-01-04 15:45:53 +0100 | [diff] [blame] | 8991 | /* re-init store persistence */ |
| 8992 | s->store_count = 0; |
Willy Tarreau | 1f0da24 | 2014-01-25 11:01:50 +0100 | [diff] [blame] | 8993 | s->uniq_id = global.req_count++; |
Emeric Brun | b982a3d | 2010-01-04 15:45:53 +0100 | [diff] [blame] | 8994 | |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 8995 | s->pend_pos = NULL; |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 8996 | |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 8997 | s->req.flags |= CF_READ_DONTWAIT; /* one read is usually enough */ |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 8998 | |
Willy Tarreau | 739cfba | 2010-01-25 23:11:14 +0100 | [diff] [blame] | 8999 | /* We must trim any excess data from the response buffer, because we |
| 9000 | * may have blocked an invalid response from a server that we don't |
| 9001 | * want to accidentely forward once we disable the analysers, nor do |
| 9002 | * we want those data to come along with next response. A typical |
| 9003 | * example of such data would be from a buggy server responding to |
| 9004 | * a HEAD with some data, or sending more than the advertised |
| 9005 | * content-length. |
| 9006 | */ |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 9007 | if (unlikely(s->res.buf->i)) |
| 9008 | s->res.buf->i = 0; |
Willy Tarreau | 739cfba | 2010-01-25 23:11:14 +0100 | [diff] [blame] | 9009 | |
Christopher Faulet | c0c672a | 2017-03-28 11:51:33 +0200 | [diff] [blame] | 9010 | /* Now we can realign the response buffer */ |
| 9011 | buffer_realign(s->res.buf); |
| 9012 | |
Willy Tarreau | d0d8da9 | 2015-04-04 02:10:38 +0200 | [diff] [blame] | 9013 | s->req.rto = strm_fe(s)->timeout.client; |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 9014 | s->req.wto = TICK_ETERNITY; |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 9015 | |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 9016 | s->res.rto = TICK_ETERNITY; |
Willy Tarreau | d0d8da9 | 2015-04-04 02:10:38 +0200 | [diff] [blame] | 9017 | s->res.wto = strm_fe(s)->timeout.client; |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 9018 | |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 9019 | s->req.rex = TICK_ETERNITY; |
| 9020 | s->req.wex = TICK_ETERNITY; |
| 9021 | s->req.analyse_exp = TICK_ETERNITY; |
| 9022 | s->res.rex = TICK_ETERNITY; |
| 9023 | s->res.wex = TICK_ETERNITY; |
| 9024 | s->res.analyse_exp = TICK_ETERNITY; |
Hongbo Long | e39683c | 2017-03-10 18:41:51 +0100 | [diff] [blame] | 9025 | s->si[1].hcto = TICK_ETERNITY; |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 9026 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 9027 | |
Sasha Pachev | 218f064 | 2014-06-16 12:05:59 -0600 | [diff] [blame] | 9028 | void free_http_res_rules(struct list *r) |
| 9029 | { |
Thierry FOURNIER | a28a942 | 2015-08-04 19:35:46 +0200 | [diff] [blame] | 9030 | struct act_rule *tr, *pr; |
Sasha Pachev | 218f064 | 2014-06-16 12:05:59 -0600 | [diff] [blame] | 9031 | |
| 9032 | list_for_each_entry_safe(pr, tr, r, list) { |
| 9033 | LIST_DEL(&pr->list); |
Thierry FOURNIER | 09af0d6 | 2014-06-18 11:35:54 +0200 | [diff] [blame] | 9034 | regex_free(&pr->arg.hdr_add.re); |
Sasha Pachev | 218f064 | 2014-06-16 12:05:59 -0600 | [diff] [blame] | 9035 | free(pr); |
| 9036 | } |
| 9037 | } |
| 9038 | |
| 9039 | void free_http_req_rules(struct list *r) |
| 9040 | { |
Thierry FOURNIER | a28a942 | 2015-08-04 19:35:46 +0200 | [diff] [blame] | 9041 | struct act_rule *tr, *pr; |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 9042 | |
| 9043 | list_for_each_entry_safe(pr, tr, r, list) { |
| 9044 | LIST_DEL(&pr->list); |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 9045 | if (pr->action == ACT_HTTP_REQ_AUTH) |
Willy Tarreau | 5c2e198 | 2012-12-24 12:00:25 +0100 | [diff] [blame] | 9046 | free(pr->arg.auth.realm); |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 9047 | |
Thierry FOURNIER | 09af0d6 | 2014-06-18 11:35:54 +0200 | [diff] [blame] | 9048 | regex_free(&pr->arg.hdr_add.re); |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 9049 | free(pr); |
| 9050 | } |
| 9051 | } |
| 9052 | |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 9053 | /* parse an "http-request" rule */ |
Thierry FOURNIER | a28a942 | 2015-08-04 19:35:46 +0200 | [diff] [blame] | 9054 | 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] | 9055 | { |
Thierry FOURNIER | a28a942 | 2015-08-04 19:35:46 +0200 | [diff] [blame] | 9056 | struct act_rule *rule; |
Thierry FOURNIER | 36481b8 | 2015-08-19 09:01:53 +0200 | [diff] [blame] | 9057 | struct action_kw *custom = NULL; |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 9058 | int cur_arg; |
Thierry FOURNIER | 09af0d6 | 2014-06-18 11:35:54 +0200 | [diff] [blame] | 9059 | char *error; |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 9060 | |
Vincent Bernat | 02779b6 | 2016-04-03 13:48:43 +0200 | [diff] [blame] | 9061 | rule = calloc(1, sizeof(*rule)); |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 9062 | if (!rule) { |
| 9063 | Alert("parsing [%s:%d]: out of memory.\n", file, linenum); |
Willy Tarreau | 81499eb | 2012-12-27 12:19:02 +0100 | [diff] [blame] | 9064 | goto out_err; |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 9065 | } |
| 9066 | |
Willy Tarreau | 5c2e198 | 2012-12-24 12:00:25 +0100 | [diff] [blame] | 9067 | if (!strcmp(args[0], "allow")) { |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 9068 | rule->action = ACT_ACTION_ALLOW; |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 9069 | cur_arg = 1; |
Jarno Huuskonen | 800d176 | 2017-03-06 14:56:36 +0200 | [diff] [blame] | 9070 | } else if (!strcmp(args[0], "deny") || !strcmp(args[0], "block") || !strcmp(args[0], "tarpit")) { |
CJ Ess | 108b1dd | 2015-04-07 12:03:37 -0400 | [diff] [blame] | 9071 | int code; |
| 9072 | int hc; |
| 9073 | |
Jarno Huuskonen | 800d176 | 2017-03-06 14:56:36 +0200 | [diff] [blame] | 9074 | if (!strcmp(args[0], "tarpit")) { |
| 9075 | rule->action = ACT_HTTP_REQ_TARPIT; |
| 9076 | rule->deny_status = HTTP_ERR_500; |
| 9077 | } |
| 9078 | else { |
| 9079 | rule->action = ACT_ACTION_DENY; |
| 9080 | rule->deny_status = HTTP_ERR_403; |
| 9081 | } |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 9082 | cur_arg = 1; |
CJ Ess | 108b1dd | 2015-04-07 12:03:37 -0400 | [diff] [blame] | 9083 | if (strcmp(args[cur_arg], "deny_status") == 0) { |
| 9084 | cur_arg++; |
| 9085 | if (!args[cur_arg]) { |
| 9086 | Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : missing status code.\n", |
| 9087 | file, linenum, proxy_type_str(proxy), proxy->id, args[0]); |
| 9088 | goto out_err; |
| 9089 | } |
| 9090 | |
| 9091 | code = atol(args[cur_arg]); |
| 9092 | cur_arg++; |
| 9093 | for (hc = 0; hc < HTTP_ERR_SIZE; hc++) { |
| 9094 | if (http_err_codes[hc] == code) { |
| 9095 | rule->deny_status = hc; |
| 9096 | break; |
| 9097 | } |
| 9098 | } |
| 9099 | |
| 9100 | if (hc >= HTTP_ERR_SIZE) { |
Jarno Huuskonen | 800d176 | 2017-03-06 14:56:36 +0200 | [diff] [blame] | 9101 | Warning("parsing [%s:%d] : status code %d not handled, using default code %d.\n", |
| 9102 | file, linenum, code, http_err_codes[rule->deny_status]); |
CJ Ess | 108b1dd | 2015-04-07 12:03:37 -0400 | [diff] [blame] | 9103 | } |
| 9104 | } |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 9105 | } else if (!strcmp(args[0], "auth")) { |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 9106 | rule->action = ACT_HTTP_REQ_AUTH; |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 9107 | cur_arg = 1; |
| 9108 | |
| 9109 | while(*args[cur_arg]) { |
| 9110 | if (!strcmp(args[cur_arg], "realm")) { |
Willy Tarreau | 5c2e198 | 2012-12-24 12:00:25 +0100 | [diff] [blame] | 9111 | rule->arg.auth.realm = strdup(args[cur_arg + 1]); |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 9112 | cur_arg+=2; |
| 9113 | continue; |
| 9114 | } else |
| 9115 | break; |
| 9116 | } |
Willy Tarreau | f4c43c1 | 2013-06-11 17:01:13 +0200 | [diff] [blame] | 9117 | } else if (!strcmp(args[0], "set-nice")) { |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 9118 | rule->action = ACT_HTTP_SET_NICE; |
Willy Tarreau | f4c43c1 | 2013-06-11 17:01:13 +0200 | [diff] [blame] | 9119 | cur_arg = 1; |
| 9120 | |
| 9121 | if (!*args[cur_arg] || |
| 9122 | (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) { |
| 9123 | Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (integer value).\n", |
| 9124 | file, linenum, args[0]); |
| 9125 | goto out_err; |
| 9126 | } |
| 9127 | rule->arg.nice = atoi(args[cur_arg]); |
| 9128 | if (rule->arg.nice < -1024) |
| 9129 | rule->arg.nice = -1024; |
| 9130 | else if (rule->arg.nice > 1024) |
| 9131 | rule->arg.nice = 1024; |
| 9132 | cur_arg++; |
Willy Tarreau | 42cf39e | 2013-06-11 18:51:32 +0200 | [diff] [blame] | 9133 | } else if (!strcmp(args[0], "set-tos")) { |
| 9134 | #ifdef IP_TOS |
| 9135 | char *err; |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 9136 | rule->action = ACT_HTTP_SET_TOS; |
Willy Tarreau | 42cf39e | 2013-06-11 18:51:32 +0200 | [diff] [blame] | 9137 | cur_arg = 1; |
| 9138 | |
| 9139 | if (!*args[cur_arg] || |
| 9140 | (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) { |
| 9141 | Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (integer/hex value).\n", |
| 9142 | file, linenum, args[0]); |
| 9143 | goto out_err; |
| 9144 | } |
| 9145 | |
| 9146 | rule->arg.tos = strtol(args[cur_arg], &err, 0); |
| 9147 | if (err && *err != '\0') { |
| 9148 | Alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-request %s' (integer/hex value expected).\n", |
| 9149 | file, linenum, err, args[0]); |
| 9150 | goto out_err; |
| 9151 | } |
| 9152 | cur_arg++; |
| 9153 | #else |
| 9154 | Alert("parsing [%s:%d]: 'http-request %s' is not supported on this platform (IP_TOS undefined).\n", file, linenum, args[0]); |
| 9155 | goto out_err; |
| 9156 | #endif |
Willy Tarreau | 51347ed | 2013-06-11 19:34:13 +0200 | [diff] [blame] | 9157 | } else if (!strcmp(args[0], "set-mark")) { |
| 9158 | #ifdef SO_MARK |
| 9159 | char *err; |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 9160 | rule->action = ACT_HTTP_SET_MARK; |
Willy Tarreau | 51347ed | 2013-06-11 19:34:13 +0200 | [diff] [blame] | 9161 | cur_arg = 1; |
| 9162 | |
| 9163 | if (!*args[cur_arg] || |
| 9164 | (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) { |
| 9165 | Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (integer/hex value).\n", |
| 9166 | file, linenum, args[0]); |
| 9167 | goto out_err; |
| 9168 | } |
| 9169 | |
| 9170 | rule->arg.mark = strtoul(args[cur_arg], &err, 0); |
| 9171 | if (err && *err != '\0') { |
| 9172 | Alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-request %s' (integer/hex value expected).\n", |
| 9173 | file, linenum, err, args[0]); |
| 9174 | goto out_err; |
| 9175 | } |
| 9176 | cur_arg++; |
| 9177 | global.last_checks |= LSTCHK_NETADM; |
| 9178 | #else |
| 9179 | Alert("parsing [%s:%d]: 'http-request %s' is not supported on this platform (SO_MARK undefined).\n", file, linenum, args[0]); |
| 9180 | goto out_err; |
| 9181 | #endif |
Willy Tarreau | 9a355ec | 2013-06-11 17:45:46 +0200 | [diff] [blame] | 9182 | } else if (!strcmp(args[0], "set-log-level")) { |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 9183 | rule->action = ACT_HTTP_SET_LOGL; |
Willy Tarreau | 9a355ec | 2013-06-11 17:45:46 +0200 | [diff] [blame] | 9184 | cur_arg = 1; |
| 9185 | |
| 9186 | if (!*args[cur_arg] || |
| 9187 | (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) { |
| 9188 | bad_log_level: |
| 9189 | Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (log level name or 'silent').\n", |
| 9190 | file, linenum, args[0]); |
| 9191 | goto out_err; |
| 9192 | } |
| 9193 | if (strcmp(args[cur_arg], "silent") == 0) |
| 9194 | rule->arg.loglevel = -1; |
| 9195 | else if ((rule->arg.loglevel = get_log_level(args[cur_arg]) + 1) == 0) |
| 9196 | goto bad_log_level; |
| 9197 | cur_arg++; |
Willy Tarreau | 20b0de5 | 2012-12-24 15:45:22 +0100 | [diff] [blame] | 9198 | } 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] | 9199 | 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] | 9200 | cur_arg = 1; |
| 9201 | |
Willy Tarreau | 8d1c516 | 2013-04-03 14:13:58 +0200 | [diff] [blame] | 9202 | if (!*args[cur_arg] || !*args[cur_arg+1] || |
| 9203 | (*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] | 9204 | Alert("parsing [%s:%d]: 'http-request %s' expects exactly 2 arguments.\n", |
| 9205 | file, linenum, args[0]); |
Willy Tarreau | 81499eb | 2012-12-27 12:19:02 +0100 | [diff] [blame] | 9206 | goto out_err; |
Willy Tarreau | 20b0de5 | 2012-12-24 15:45:22 +0100 | [diff] [blame] | 9207 | } |
| 9208 | |
| 9209 | rule->arg.hdr_add.name = strdup(args[cur_arg]); |
| 9210 | rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name); |
| 9211 | LIST_INIT(&rule->arg.hdr_add.fmt); |
Willy Tarreau | a4312fa | 2013-04-02 16:34:32 +0200 | [diff] [blame] | 9212 | |
Thierry FOURNIER | 1c0054f | 2013-11-20 15:09:52 +0100 | [diff] [blame] | 9213 | proxy->conf.args.ctx = ARGC_HRQ; |
Thierry FOURNIER / OZON.IO | 8a4e442 | 2016-11-23 00:41:28 +0100 | [diff] [blame] | 9214 | error = NULL; |
Thierry FOURNIER / OZON.IO | 59fd511 | 2016-11-22 23:50:02 +0100 | [diff] [blame] | 9215 | 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] | 9216 | (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR, &error)) { |
| 9217 | Alert("parsing [%s:%d]: 'http-request %s': %s.\n", |
| 9218 | file, linenum, args[0], error); |
| 9219 | free(error); |
Thierry FOURNIER / OZON.IO | 59fd511 | 2016-11-22 23:50:02 +0100 | [diff] [blame] | 9220 | goto out_err; |
| 9221 | } |
Willy Tarreau | 59ad1a2 | 2014-01-29 14:39:58 +0100 | [diff] [blame] | 9222 | free(proxy->conf.lfs_file); |
| 9223 | proxy->conf.lfs_file = strdup(proxy->conf.args.file); |
| 9224 | proxy->conf.lfs_line = proxy->conf.args.line; |
Willy Tarreau | 20b0de5 | 2012-12-24 15:45:22 +0100 | [diff] [blame] | 9225 | cur_arg += 2; |
Willy Tarreau | b854392 | 2014-06-17 18:58:26 +0200 | [diff] [blame] | 9226 | } 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] | 9227 | 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] | 9228 | cur_arg = 1; |
| 9229 | |
| 9230 | if (!*args[cur_arg] || !*args[cur_arg+1] || !*args[cur_arg+2] || |
Baptiste Assmann | 92df370 | 2014-06-24 11:10:00 +0200 | [diff] [blame] | 9231 | (*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] | 9232 | Alert("parsing [%s:%d]: 'http-request %s' expects exactly 3 arguments.\n", |
| 9233 | file, linenum, args[0]); |
| 9234 | goto out_err; |
| 9235 | } |
| 9236 | |
| 9237 | rule->arg.hdr_add.name = strdup(args[cur_arg]); |
| 9238 | rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name); |
| 9239 | LIST_INIT(&rule->arg.hdr_add.fmt); |
| 9240 | |
Thierry FOURNIER | 09af0d6 | 2014-06-18 11:35:54 +0200 | [diff] [blame] | 9241 | error = NULL; |
| 9242 | if (!regex_comp(args[cur_arg + 1], &rule->arg.hdr_add.re, 1, 1, &error)) { |
| 9243 | Alert("parsing [%s:%d] : '%s' : %s.\n", file, linenum, |
| 9244 | args[cur_arg + 1], error); |
| 9245 | free(error); |
Sasha Pachev | 218f064 | 2014-06-16 12:05:59 -0600 | [diff] [blame] | 9246 | goto out_err; |
| 9247 | } |
| 9248 | |
| 9249 | proxy->conf.args.ctx = ARGC_HRQ; |
Thierry FOURNIER / OZON.IO | 8a4e442 | 2016-11-23 00:41:28 +0100 | [diff] [blame] | 9250 | error = NULL; |
Thierry FOURNIER / OZON.IO | 59fd511 | 2016-11-22 23:50:02 +0100 | [diff] [blame] | 9251 | 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] | 9252 | (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR, &error)) { |
| 9253 | Alert("parsing [%s:%d]: 'http-request %s': %s.\n", |
| 9254 | file, linenum, args[0], error); |
| 9255 | free(error); |
Thierry FOURNIER / OZON.IO | 59fd511 | 2016-11-22 23:50:02 +0100 | [diff] [blame] | 9256 | goto out_err; |
| 9257 | } |
Sasha Pachev | 218f064 | 2014-06-16 12:05:59 -0600 | [diff] [blame] | 9258 | |
| 9259 | free(proxy->conf.lfs_file); |
| 9260 | proxy->conf.lfs_file = strdup(proxy->conf.args.file); |
| 9261 | proxy->conf.lfs_line = proxy->conf.args.line; |
| 9262 | cur_arg += 3; |
Thierry FOURNIER | dad3d1d | 2014-04-22 18:07:25 +0200 | [diff] [blame] | 9263 | } else if (strcmp(args[0], "del-header") == 0) { |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 9264 | rule->action = ACT_HTTP_DEL_HDR; |
Thierry FOURNIER | dad3d1d | 2014-04-22 18:07:25 +0200 | [diff] [blame] | 9265 | cur_arg = 1; |
| 9266 | |
| 9267 | if (!*args[cur_arg] || |
| 9268 | (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) { |
| 9269 | Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument.\n", |
| 9270 | file, linenum, args[0]); |
| 9271 | goto out_err; |
| 9272 | } |
| 9273 | |
| 9274 | rule->arg.hdr_add.name = strdup(args[cur_arg]); |
| 9275 | rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name); |
| 9276 | |
| 9277 | proxy->conf.args.ctx = ARGC_HRQ; |
| 9278 | free(proxy->conf.lfs_file); |
| 9279 | proxy->conf.lfs_file = strdup(proxy->conf.args.file); |
| 9280 | proxy->conf.lfs_line = proxy->conf.args.line; |
| 9281 | cur_arg += 1; |
Willy Tarreau | 09448f7 | 2014-06-25 18:12:15 +0200 | [diff] [blame] | 9282 | } else if (strncmp(args[0], "track-sc", 8) == 0 && |
| 9283 | args[0][9] == '\0' && args[0][8] >= '0' && |
Willy Tarreau | e1cfc1f | 2014-10-17 11:53:05 +0200 | [diff] [blame] | 9284 | args[0][8] < '0' + MAX_SESS_STKCTR) { /* track-sc 0..9 */ |
Willy Tarreau | 09448f7 | 2014-06-25 18:12:15 +0200 | [diff] [blame] | 9285 | struct sample_expr *expr; |
| 9286 | unsigned int where; |
| 9287 | char *err = NULL; |
| 9288 | |
| 9289 | cur_arg = 1; |
| 9290 | proxy->conf.args.ctx = ARGC_TRK; |
| 9291 | |
| 9292 | expr = sample_parse_expr((char **)args, &cur_arg, file, linenum, &err, &proxy->conf.args); |
| 9293 | if (!expr) { |
| 9294 | Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : %s.\n", |
| 9295 | file, linenum, proxy_type_str(proxy), proxy->id, args[0], err); |
| 9296 | free(err); |
| 9297 | goto out_err; |
| 9298 | } |
| 9299 | |
| 9300 | where = 0; |
| 9301 | if (proxy->cap & PR_CAP_FE) |
| 9302 | where |= SMP_VAL_FE_HRQ_HDR; |
| 9303 | if (proxy->cap & PR_CAP_BE) |
| 9304 | where |= SMP_VAL_BE_HRQ_HDR; |
| 9305 | |
| 9306 | if (!(expr->fetch->val & where)) { |
| 9307 | Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule :" |
| 9308 | " fetch method '%s' extracts information from '%s', none of which is available here.\n", |
| 9309 | file, linenum, proxy_type_str(proxy), proxy->id, args[0], |
| 9310 | args[cur_arg-1], sample_src_names(expr->fetch->use)); |
| 9311 | free(expr); |
| 9312 | goto out_err; |
| 9313 | } |
| 9314 | |
| 9315 | if (strcmp(args[cur_arg], "table") == 0) { |
| 9316 | cur_arg++; |
| 9317 | if (!args[cur_arg]) { |
| 9318 | Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : missing table name.\n", |
| 9319 | file, linenum, proxy_type_str(proxy), proxy->id, args[0]); |
| 9320 | free(expr); |
| 9321 | goto out_err; |
| 9322 | } |
| 9323 | /* we copy the table name for now, it will be resolved later */ |
Thierry FOURNIER | 5ec63e0 | 2015-08-04 09:09:48 +0200 | [diff] [blame] | 9324 | rule->arg.trk_ctr.table.n = strdup(args[cur_arg]); |
Willy Tarreau | 09448f7 | 2014-06-25 18:12:15 +0200 | [diff] [blame] | 9325 | cur_arg++; |
| 9326 | } |
Thierry FOURNIER | 5ec63e0 | 2015-08-04 09:09:48 +0200 | [diff] [blame] | 9327 | rule->arg.trk_ctr.expr = expr; |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 9328 | rule->action = ACT_ACTION_TRK_SC0 + args[0][8] - '0'; |
Willy Tarreau | 81499eb | 2012-12-27 12:19:02 +0100 | [diff] [blame] | 9329 | } else if (strcmp(args[0], "redirect") == 0) { |
| 9330 | struct redirect_rule *redir; |
Willy Tarreau | 6d4890c | 2013-11-18 18:04:25 +0100 | [diff] [blame] | 9331 | char *errmsg = NULL; |
Willy Tarreau | 81499eb | 2012-12-27 12:19:02 +0100 | [diff] [blame] | 9332 | |
Willy Tarreau | be4653b | 2015-05-28 15:26:58 +0200 | [diff] [blame] | 9333 | 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] | 9334 | Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : %s.\n", |
| 9335 | file, linenum, proxy_type_str(proxy), proxy->id, args[0], errmsg); |
| 9336 | goto out_err; |
| 9337 | } |
| 9338 | |
| 9339 | /* this redirect rule might already contain a parsed condition which |
| 9340 | * we'll pass to the http-request rule. |
| 9341 | */ |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 9342 | rule->action = ACT_HTTP_REDIR; |
Willy Tarreau | 81499eb | 2012-12-27 12:19:02 +0100 | [diff] [blame] | 9343 | rule->arg.redir = redir; |
| 9344 | rule->cond = redir->cond; |
| 9345 | redir->cond = NULL; |
| 9346 | cur_arg = 2; |
| 9347 | return rule; |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 9348 | } else if (strncmp(args[0], "add-acl", 7) == 0) { |
| 9349 | /* http-request add-acl(<reference (acl name)>) <key pattern> */ |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 9350 | rule->action = ACT_HTTP_ADD_ACL; |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 9351 | /* |
| 9352 | * '+ 8' for 'add-acl(' |
| 9353 | * '- 9' for 'add-acl(' + trailing ')' |
| 9354 | */ |
Willy Tarreau | 6c09c2c | 2014-04-25 21:38:08 +0200 | [diff] [blame] | 9355 | 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] | 9356 | |
| 9357 | cur_arg = 1; |
| 9358 | |
| 9359 | if (!*args[cur_arg] || |
| 9360 | (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) { |
| 9361 | Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument.\n", |
| 9362 | file, linenum, args[0]); |
| 9363 | goto out_err; |
| 9364 | } |
| 9365 | |
| 9366 | LIST_INIT(&rule->arg.map.key); |
| 9367 | proxy->conf.args.ctx = ARGC_HRQ; |
Thierry FOURNIER / OZON.IO | 8a4e442 | 2016-11-23 00:41:28 +0100 | [diff] [blame] | 9368 | error = NULL; |
Thierry FOURNIER / OZON.IO | 59fd511 | 2016-11-22 23:50:02 +0100 | [diff] [blame] | 9369 | 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] | 9370 | (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR, &error)) { |
| 9371 | Alert("parsing [%s:%d]: 'http-request %s': %s.\n", |
| 9372 | file, linenum, args[0], error); |
| 9373 | free(error); |
Thierry FOURNIER / OZON.IO | 59fd511 | 2016-11-22 23:50:02 +0100 | [diff] [blame] | 9374 | goto out_err; |
| 9375 | } |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 9376 | free(proxy->conf.lfs_file); |
| 9377 | proxy->conf.lfs_file = strdup(proxy->conf.args.file); |
| 9378 | proxy->conf.lfs_line = proxy->conf.args.line; |
| 9379 | cur_arg += 1; |
| 9380 | } else if (strncmp(args[0], "del-acl", 7) == 0) { |
| 9381 | /* http-request del-acl(<reference (acl name)>) <key pattern> */ |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 9382 | rule->action = ACT_HTTP_DEL_ACL; |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 9383 | /* |
| 9384 | * '+ 8' for 'del-acl(' |
| 9385 | * '- 9' for 'del-acl(' + trailing ')' |
| 9386 | */ |
Willy Tarreau | 6c09c2c | 2014-04-25 21:38:08 +0200 | [diff] [blame] | 9387 | 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] | 9388 | |
| 9389 | cur_arg = 1; |
| 9390 | |
| 9391 | if (!*args[cur_arg] || |
| 9392 | (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) { |
| 9393 | Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument.\n", |
| 9394 | file, linenum, args[0]); |
| 9395 | goto out_err; |
| 9396 | } |
| 9397 | |
| 9398 | LIST_INIT(&rule->arg.map.key); |
| 9399 | proxy->conf.args.ctx = ARGC_HRQ; |
Thierry FOURNIER / OZON.IO | 8a4e442 | 2016-11-23 00:41:28 +0100 | [diff] [blame] | 9400 | error = NULL; |
Thierry FOURNIER / OZON.IO | 59fd511 | 2016-11-22 23:50:02 +0100 | [diff] [blame] | 9401 | 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] | 9402 | (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR, &error)) { |
| 9403 | Alert("parsing [%s:%d]: 'http-request %s': %s.\n", |
| 9404 | file, linenum, args[0], error); |
| 9405 | free(error); |
Thierry FOURNIER / OZON.IO | 59fd511 | 2016-11-22 23:50:02 +0100 | [diff] [blame] | 9406 | goto out_err; |
| 9407 | } |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 9408 | free(proxy->conf.lfs_file); |
| 9409 | proxy->conf.lfs_file = strdup(proxy->conf.args.file); |
| 9410 | proxy->conf.lfs_line = proxy->conf.args.line; |
| 9411 | cur_arg += 1; |
| 9412 | } else if (strncmp(args[0], "del-map", 7) == 0) { |
| 9413 | /* http-request del-map(<reference (map name)>) <key pattern> */ |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 9414 | rule->action = ACT_HTTP_DEL_MAP; |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 9415 | /* |
| 9416 | * '+ 8' for 'del-map(' |
| 9417 | * '- 9' for 'del-map(' + trailing ')' |
| 9418 | */ |
Willy Tarreau | 6c09c2c | 2014-04-25 21:38:08 +0200 | [diff] [blame] | 9419 | 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] | 9420 | |
| 9421 | cur_arg = 1; |
| 9422 | |
| 9423 | if (!*args[cur_arg] || |
| 9424 | (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) { |
| 9425 | Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument.\n", |
| 9426 | file, linenum, args[0]); |
| 9427 | goto out_err; |
| 9428 | } |
| 9429 | |
| 9430 | LIST_INIT(&rule->arg.map.key); |
| 9431 | proxy->conf.args.ctx = ARGC_HRQ; |
Thierry FOURNIER / OZON.IO | 8a4e442 | 2016-11-23 00:41:28 +0100 | [diff] [blame] | 9432 | error = NULL; |
Thierry FOURNIER / OZON.IO | 59fd511 | 2016-11-22 23:50:02 +0100 | [diff] [blame] | 9433 | 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] | 9434 | (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR, &error)) { |
| 9435 | Alert("parsing [%s:%d]: 'http-request %s': %s.\n", |
| 9436 | file, linenum, args[0], error); |
| 9437 | free(error); |
Thierry FOURNIER / OZON.IO | 59fd511 | 2016-11-22 23:50:02 +0100 | [diff] [blame] | 9438 | goto out_err; |
| 9439 | } |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 9440 | free(proxy->conf.lfs_file); |
| 9441 | proxy->conf.lfs_file = strdup(proxy->conf.args.file); |
| 9442 | proxy->conf.lfs_line = proxy->conf.args.line; |
| 9443 | cur_arg += 1; |
| 9444 | } else if (strncmp(args[0], "set-map", 7) == 0) { |
| 9445 | /* http-request set-map(<reference (map name)>) <key pattern> <value pattern> */ |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 9446 | rule->action = ACT_HTTP_SET_MAP; |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 9447 | /* |
| 9448 | * '+ 8' for 'set-map(' |
| 9449 | * '- 9' for 'set-map(' + trailing ')' |
| 9450 | */ |
Willy Tarreau | 6c09c2c | 2014-04-25 21:38:08 +0200 | [diff] [blame] | 9451 | 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] | 9452 | |
| 9453 | cur_arg = 1; |
| 9454 | |
| 9455 | if (!*args[cur_arg] || !*args[cur_arg+1] || |
| 9456 | (*args[cur_arg+2] && strcmp(args[cur_arg+2], "if") != 0 && strcmp(args[cur_arg+2], "unless") != 0)) { |
| 9457 | Alert("parsing [%s:%d]: 'http-request %s' expects exactly 2 arguments.\n", |
| 9458 | file, linenum, args[0]); |
| 9459 | goto out_err; |
| 9460 | } |
| 9461 | |
| 9462 | LIST_INIT(&rule->arg.map.key); |
| 9463 | LIST_INIT(&rule->arg.map.value); |
| 9464 | proxy->conf.args.ctx = ARGC_HRQ; |
| 9465 | |
| 9466 | /* key pattern */ |
Thierry FOURNIER / OZON.IO | 8a4e442 | 2016-11-23 00:41:28 +0100 | [diff] [blame] | 9467 | error = NULL; |
Thierry FOURNIER / OZON.IO | 59fd511 | 2016-11-22 23:50:02 +0100 | [diff] [blame] | 9468 | 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] | 9469 | (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR, &error)) { |
| 9470 | Alert("parsing [%s:%d]: 'http-request %s' key: %s.\n", |
| 9471 | file, linenum, args[0], error); |
| 9472 | free(error); |
Thierry FOURNIER / OZON.IO | 59fd511 | 2016-11-22 23:50:02 +0100 | [diff] [blame] | 9473 | goto out_err; |
| 9474 | } |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 9475 | |
| 9476 | /* value pattern */ |
Thierry FOURNIER / OZON.IO | 8a4e442 | 2016-11-23 00:41:28 +0100 | [diff] [blame] | 9477 | error = NULL; |
Thierry FOURNIER / OZON.IO | 59fd511 | 2016-11-22 23:50:02 +0100 | [diff] [blame] | 9478 | 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] | 9479 | (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR, &error)) { |
| 9480 | Alert("parsing [%s:%d]: 'http-request %s' pattern: %s.\n", |
| 9481 | file, linenum, args[0], error); |
| 9482 | free(error); |
Thierry FOURNIER / OZON.IO | 59fd511 | 2016-11-22 23:50:02 +0100 | [diff] [blame] | 9483 | goto out_err; |
| 9484 | } |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 9485 | free(proxy->conf.lfs_file); |
| 9486 | proxy->conf.lfs_file = strdup(proxy->conf.args.file); |
| 9487 | proxy->conf.lfs_line = proxy->conf.args.line; |
| 9488 | |
| 9489 | cur_arg += 2; |
William Lallemand | 73025dd | 2014-04-24 14:38:37 +0200 | [diff] [blame] | 9490 | } else if (((custom = action_http_req_custom(args[0])) != NULL)) { |
| 9491 | char *errmsg = NULL; |
| 9492 | cur_arg = 1; |
| 9493 | /* try in the module list */ |
Thierry FOURNIER | 5563e4b | 2015-08-14 19:20:07 +0200 | [diff] [blame] | 9494 | rule->from = ACT_F_HTTP_REQ; |
Thierry FOURNIER | 85c6c97 | 2015-09-22 19:14:35 +0200 | [diff] [blame] | 9495 | rule->kw = custom; |
Thierry FOURNIER | afa8049 | 2015-08-19 09:04:15 +0200 | [diff] [blame] | 9496 | 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] | 9497 | Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : %s.\n", |
| 9498 | file, linenum, proxy_type_str(proxy), proxy->id, args[0], errmsg); |
| 9499 | free(errmsg); |
| 9500 | goto out_err; |
| 9501 | } |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 9502 | } else { |
Thierry FOURNIER | ab95e65 | 2015-10-02 08:24:51 +0200 | [diff] [blame] | 9503 | action_build_list(&http_req_keywords.list, &trash); |
| 9504 | Alert("parsing [%s:%d]: 'http-request' expects 'allow', 'deny', 'auth', 'redirect', " |
| 9505 | "'tarpit', 'add-header', 'set-header', 'replace-header', 'replace-value', 'set-nice', " |
Ruoshan Huang | e4edc6b | 2016-07-14 15:07:45 +0800 | [diff] [blame] | 9506 | "'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] | 9507 | "%s%s, but got '%s'%s.\n", |
Thierry FOURNIER | ab95e65 | 2015-10-02 08:24:51 +0200 | [diff] [blame] | 9508 | file, linenum, *trash.str ? ", " : "", trash.str, args[0], *args[0] ? "" : " (missing argument)"); |
Willy Tarreau | 81499eb | 2012-12-27 12:19:02 +0100 | [diff] [blame] | 9509 | goto out_err; |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 9510 | } |
| 9511 | |
| 9512 | if (strcmp(args[cur_arg], "if") == 0 || strcmp(args[cur_arg], "unless") == 0) { |
| 9513 | struct acl_cond *cond; |
Willy Tarreau | b7451bb | 2012-04-27 12:38:15 +0200 | [diff] [blame] | 9514 | char *errmsg = NULL; |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 9515 | |
Willy Tarreau | b7451bb | 2012-04-27 12:38:15 +0200 | [diff] [blame] | 9516 | if ((cond = build_acl_cond(file, linenum, proxy, args+cur_arg, &errmsg)) == NULL) { |
| 9517 | Alert("parsing [%s:%d] : error detected while parsing an 'http-request %s' condition : %s.\n", |
| 9518 | file, linenum, args[0], errmsg); |
| 9519 | free(errmsg); |
Willy Tarreau | 81499eb | 2012-12-27 12:19:02 +0100 | [diff] [blame] | 9520 | goto out_err; |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 9521 | } |
| 9522 | rule->cond = cond; |
| 9523 | } |
| 9524 | else if (*args[cur_arg]) { |
| 9525 | Alert("parsing [%s:%d]: 'http-request %s' expects 'realm' for 'auth' or" |
| 9526 | " either 'if' or 'unless' followed by a condition but found '%s'.\n", |
| 9527 | file, linenum, args[0], args[cur_arg]); |
Willy Tarreau | 81499eb | 2012-12-27 12:19:02 +0100 | [diff] [blame] | 9528 | goto out_err; |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 9529 | } |
| 9530 | |
| 9531 | return rule; |
Willy Tarreau | 81499eb | 2012-12-27 12:19:02 +0100 | [diff] [blame] | 9532 | out_err: |
| 9533 | free(rule); |
| 9534 | return NULL; |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 9535 | } |
| 9536 | |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 9537 | /* parse an "http-respose" rule */ |
Thierry FOURNIER | a28a942 | 2015-08-04 19:35:46 +0200 | [diff] [blame] | 9538 | 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] | 9539 | { |
Thierry FOURNIER | a28a942 | 2015-08-04 19:35:46 +0200 | [diff] [blame] | 9540 | struct act_rule *rule; |
Thierry FOURNIER | 36481b8 | 2015-08-19 09:01:53 +0200 | [diff] [blame] | 9541 | struct action_kw *custom = NULL; |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 9542 | int cur_arg; |
Thierry FOURNIER | 09af0d6 | 2014-06-18 11:35:54 +0200 | [diff] [blame] | 9543 | char *error; |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 9544 | |
| 9545 | rule = calloc(1, sizeof(*rule)); |
| 9546 | if (!rule) { |
| 9547 | Alert("parsing [%s:%d]: out of memory.\n", file, linenum); |
| 9548 | goto out_err; |
| 9549 | } |
| 9550 | |
| 9551 | if (!strcmp(args[0], "allow")) { |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 9552 | rule->action = ACT_ACTION_ALLOW; |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 9553 | cur_arg = 1; |
| 9554 | } else if (!strcmp(args[0], "deny")) { |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 9555 | rule->action = ACT_ACTION_DENY; |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 9556 | cur_arg = 1; |
Willy Tarreau | f4c43c1 | 2013-06-11 17:01:13 +0200 | [diff] [blame] | 9557 | } else if (!strcmp(args[0], "set-nice")) { |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 9558 | rule->action = ACT_HTTP_SET_NICE; |
Willy Tarreau | f4c43c1 | 2013-06-11 17:01:13 +0200 | [diff] [blame] | 9559 | cur_arg = 1; |
| 9560 | |
| 9561 | if (!*args[cur_arg] || |
| 9562 | (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) { |
| 9563 | Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (integer value).\n", |
| 9564 | file, linenum, args[0]); |
| 9565 | goto out_err; |
| 9566 | } |
| 9567 | rule->arg.nice = atoi(args[cur_arg]); |
| 9568 | if (rule->arg.nice < -1024) |
| 9569 | rule->arg.nice = -1024; |
| 9570 | else if (rule->arg.nice > 1024) |
| 9571 | rule->arg.nice = 1024; |
| 9572 | cur_arg++; |
Willy Tarreau | 42cf39e | 2013-06-11 18:51:32 +0200 | [diff] [blame] | 9573 | } else if (!strcmp(args[0], "set-tos")) { |
| 9574 | #ifdef IP_TOS |
| 9575 | char *err; |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 9576 | rule->action = ACT_HTTP_SET_TOS; |
Willy Tarreau | 42cf39e | 2013-06-11 18:51:32 +0200 | [diff] [blame] | 9577 | cur_arg = 1; |
| 9578 | |
| 9579 | if (!*args[cur_arg] || |
| 9580 | (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) { |
| 9581 | Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (integer/hex value).\n", |
| 9582 | file, linenum, args[0]); |
| 9583 | goto out_err; |
| 9584 | } |
| 9585 | |
| 9586 | rule->arg.tos = strtol(args[cur_arg], &err, 0); |
| 9587 | if (err && *err != '\0') { |
| 9588 | Alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-response %s' (integer/hex value expected).\n", |
| 9589 | file, linenum, err, args[0]); |
| 9590 | goto out_err; |
| 9591 | } |
| 9592 | cur_arg++; |
| 9593 | #else |
| 9594 | Alert("parsing [%s:%d]: 'http-response %s' is not supported on this platform (IP_TOS undefined).\n", file, linenum, args[0]); |
| 9595 | goto out_err; |
| 9596 | #endif |
Willy Tarreau | 51347ed | 2013-06-11 19:34:13 +0200 | [diff] [blame] | 9597 | } else if (!strcmp(args[0], "set-mark")) { |
| 9598 | #ifdef SO_MARK |
| 9599 | char *err; |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 9600 | rule->action = ACT_HTTP_SET_MARK; |
Willy Tarreau | 51347ed | 2013-06-11 19:34:13 +0200 | [diff] [blame] | 9601 | cur_arg = 1; |
| 9602 | |
| 9603 | if (!*args[cur_arg] || |
| 9604 | (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) { |
| 9605 | Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (integer/hex value).\n", |
| 9606 | file, linenum, args[0]); |
| 9607 | goto out_err; |
| 9608 | } |
| 9609 | |
| 9610 | rule->arg.mark = strtoul(args[cur_arg], &err, 0); |
| 9611 | if (err && *err != '\0') { |
| 9612 | Alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-response %s' (integer/hex value expected).\n", |
| 9613 | file, linenum, err, args[0]); |
| 9614 | goto out_err; |
| 9615 | } |
| 9616 | cur_arg++; |
| 9617 | global.last_checks |= LSTCHK_NETADM; |
| 9618 | #else |
| 9619 | Alert("parsing [%s:%d]: 'http-response %s' is not supported on this platform (SO_MARK undefined).\n", file, linenum, args[0]); |
| 9620 | goto out_err; |
| 9621 | #endif |
Willy Tarreau | 9a355ec | 2013-06-11 17:45:46 +0200 | [diff] [blame] | 9622 | } else if (!strcmp(args[0], "set-log-level")) { |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 9623 | rule->action = ACT_HTTP_SET_LOGL; |
Willy Tarreau | 9a355ec | 2013-06-11 17:45:46 +0200 | [diff] [blame] | 9624 | cur_arg = 1; |
| 9625 | |
| 9626 | if (!*args[cur_arg] || |
| 9627 | (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) { |
| 9628 | bad_log_level: |
| 9629 | Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (log level name or 'silent').\n", |
| 9630 | file, linenum, args[0]); |
| 9631 | goto out_err; |
| 9632 | } |
| 9633 | if (strcmp(args[cur_arg], "silent") == 0) |
| 9634 | rule->arg.loglevel = -1; |
Ruoshan Huang | dd01678 | 2016-06-15 22:16:03 +0800 | [diff] [blame] | 9635 | 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] | 9636 | goto bad_log_level; |
| 9637 | cur_arg++; |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 9638 | } 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] | 9639 | 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] | 9640 | cur_arg = 1; |
| 9641 | |
| 9642 | if (!*args[cur_arg] || !*args[cur_arg+1] || |
| 9643 | (*args[cur_arg+2] && strcmp(args[cur_arg+2], "if") != 0 && strcmp(args[cur_arg+2], "unless") != 0)) { |
| 9644 | Alert("parsing [%s:%d]: 'http-response %s' expects exactly 2 arguments.\n", |
| 9645 | file, linenum, args[0]); |
| 9646 | goto out_err; |
| 9647 | } |
| 9648 | |
| 9649 | rule->arg.hdr_add.name = strdup(args[cur_arg]); |
| 9650 | rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name); |
| 9651 | LIST_INIT(&rule->arg.hdr_add.fmt); |
| 9652 | |
Thierry FOURNIER | 1c0054f | 2013-11-20 15:09:52 +0100 | [diff] [blame] | 9653 | proxy->conf.args.ctx = ARGC_HRS; |
Thierry FOURNIER / OZON.IO | 8a4e442 | 2016-11-23 00:41:28 +0100 | [diff] [blame] | 9654 | error = NULL; |
Thierry FOURNIER / OZON.IO | 59fd511 | 2016-11-22 23:50:02 +0100 | [diff] [blame] | 9655 | 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] | 9656 | (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR, &error)) { |
| 9657 | Alert("parsing [%s:%d]: 'http-response %s': %s.\n", |
| 9658 | file, linenum, args[0], error); |
| 9659 | free(error); |
Thierry FOURNIER / OZON.IO | 59fd511 | 2016-11-22 23:50:02 +0100 | [diff] [blame] | 9660 | goto out_err; |
| 9661 | } |
Willy Tarreau | 59ad1a2 | 2014-01-29 14:39:58 +0100 | [diff] [blame] | 9662 | free(proxy->conf.lfs_file); |
| 9663 | proxy->conf.lfs_file = strdup(proxy->conf.args.file); |
| 9664 | proxy->conf.lfs_line = proxy->conf.args.line; |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 9665 | cur_arg += 2; |
Sasha Pachev | 218f064 | 2014-06-16 12:05:59 -0600 | [diff] [blame] | 9666 | } 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] | 9667 | 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] | 9668 | cur_arg = 1; |
| 9669 | |
| 9670 | if (!*args[cur_arg] || !*args[cur_arg+1] || !*args[cur_arg+2] || |
Baptiste Assmann | 12cb00b | 2014-08-08 17:29:06 +0200 | [diff] [blame] | 9671 | (*args[cur_arg+3] && strcmp(args[cur_arg+3], "if") != 0 && strcmp(args[cur_arg+3], "unless") != 0)) { |
| 9672 | Alert("parsing [%s:%d]: 'http-response %s' expects exactly 3 arguments.\n", |
Sasha Pachev | 218f064 | 2014-06-16 12:05:59 -0600 | [diff] [blame] | 9673 | file, linenum, args[0]); |
| 9674 | goto out_err; |
| 9675 | } |
| 9676 | |
| 9677 | rule->arg.hdr_add.name = strdup(args[cur_arg]); |
| 9678 | rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name); |
| 9679 | LIST_INIT(&rule->arg.hdr_add.fmt); |
| 9680 | |
Thierry FOURNIER | 09af0d6 | 2014-06-18 11:35:54 +0200 | [diff] [blame] | 9681 | error = NULL; |
| 9682 | if (!regex_comp(args[cur_arg + 1], &rule->arg.hdr_add.re, 1, 1, &error)) { |
| 9683 | Alert("parsing [%s:%d] : '%s' : %s.\n", file, linenum, |
| 9684 | args[cur_arg + 1], error); |
| 9685 | free(error); |
Sasha Pachev | 218f064 | 2014-06-16 12:05:59 -0600 | [diff] [blame] | 9686 | goto out_err; |
| 9687 | } |
| 9688 | |
| 9689 | proxy->conf.args.ctx = ARGC_HRQ; |
Thierry FOURNIER / OZON.IO | 8a4e442 | 2016-11-23 00:41:28 +0100 | [diff] [blame] | 9690 | error = NULL; |
Thierry FOURNIER / OZON.IO | 59fd511 | 2016-11-22 23:50:02 +0100 | [diff] [blame] | 9691 | 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] | 9692 | (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR, &error)) { |
| 9693 | Alert("parsing [%s:%d]: 'http-response %s': %s.\n", |
| 9694 | file, linenum, args[0], error); |
| 9695 | free(error); |
Thierry FOURNIER / OZON.IO | 59fd511 | 2016-11-22 23:50:02 +0100 | [diff] [blame] | 9696 | goto out_err; |
| 9697 | } |
Sasha Pachev | 218f064 | 2014-06-16 12:05:59 -0600 | [diff] [blame] | 9698 | |
| 9699 | free(proxy->conf.lfs_file); |
| 9700 | proxy->conf.lfs_file = strdup(proxy->conf.args.file); |
| 9701 | proxy->conf.lfs_line = proxy->conf.args.line; |
| 9702 | cur_arg += 3; |
Thierry FOURNIER | dad3d1d | 2014-04-22 18:07:25 +0200 | [diff] [blame] | 9703 | } else if (strcmp(args[0], "del-header") == 0) { |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 9704 | rule->action = ACT_HTTP_DEL_HDR; |
Thierry FOURNIER | dad3d1d | 2014-04-22 18:07:25 +0200 | [diff] [blame] | 9705 | cur_arg = 1; |
| 9706 | |
| 9707 | if (!*args[cur_arg] || |
| 9708 | (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) { |
| 9709 | Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument.\n", |
| 9710 | file, linenum, args[0]); |
| 9711 | goto out_err; |
| 9712 | } |
| 9713 | |
| 9714 | rule->arg.hdr_add.name = strdup(args[cur_arg]); |
| 9715 | rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name); |
| 9716 | |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 9717 | proxy->conf.args.ctx = ARGC_HRS; |
| 9718 | free(proxy->conf.lfs_file); |
| 9719 | proxy->conf.lfs_file = strdup(proxy->conf.args.file); |
| 9720 | proxy->conf.lfs_line = proxy->conf.args.line; |
| 9721 | cur_arg += 1; |
| 9722 | } else if (strncmp(args[0], "add-acl", 7) == 0) { |
| 9723 | /* http-request add-acl(<reference (acl name)>) <key pattern> */ |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 9724 | rule->action = ACT_HTTP_ADD_ACL; |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 9725 | /* |
| 9726 | * '+ 8' for 'add-acl(' |
| 9727 | * '- 9' for 'add-acl(' + trailing ')' |
| 9728 | */ |
Willy Tarreau | 6c09c2c | 2014-04-25 21:38:08 +0200 | [diff] [blame] | 9729 | 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] | 9730 | |
| 9731 | cur_arg = 1; |
| 9732 | |
| 9733 | if (!*args[cur_arg] || |
| 9734 | (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) { |
| 9735 | Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument.\n", |
| 9736 | file, linenum, args[0]); |
| 9737 | goto out_err; |
| 9738 | } |
| 9739 | |
| 9740 | LIST_INIT(&rule->arg.map.key); |
Thierry FOURNIER | dad3d1d | 2014-04-22 18:07:25 +0200 | [diff] [blame] | 9741 | proxy->conf.args.ctx = ARGC_HRS; |
Thierry FOURNIER / OZON.IO | 8a4e442 | 2016-11-23 00:41:28 +0100 | [diff] [blame] | 9742 | error = NULL; |
Thierry FOURNIER / OZON.IO | 59fd511 | 2016-11-22 23:50:02 +0100 | [diff] [blame] | 9743 | 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] | 9744 | (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR, &error)) { |
| 9745 | Alert("parsing [%s:%d]: 'http-response %s': %s.\n", |
| 9746 | file, linenum, args[0], error); |
| 9747 | free(error); |
Thierry FOURNIER / OZON.IO | 59fd511 | 2016-11-22 23:50:02 +0100 | [diff] [blame] | 9748 | goto out_err; |
| 9749 | } |
Thierry FOURNIER | dad3d1d | 2014-04-22 18:07:25 +0200 | [diff] [blame] | 9750 | free(proxy->conf.lfs_file); |
| 9751 | proxy->conf.lfs_file = strdup(proxy->conf.args.file); |
| 9752 | proxy->conf.lfs_line = proxy->conf.args.line; |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 9753 | |
Thierry FOURNIER | dad3d1d | 2014-04-22 18:07:25 +0200 | [diff] [blame] | 9754 | cur_arg += 1; |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 9755 | } else if (strncmp(args[0], "del-acl", 7) == 0) { |
| 9756 | /* http-response del-acl(<reference (acl name)>) <key pattern> */ |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 9757 | rule->action = ACT_HTTP_DEL_ACL; |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 9758 | /* |
| 9759 | * '+ 8' for 'del-acl(' |
| 9760 | * '- 9' for 'del-acl(' + trailing ')' |
| 9761 | */ |
Willy Tarreau | 6c09c2c | 2014-04-25 21:38:08 +0200 | [diff] [blame] | 9762 | 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] | 9763 | |
| 9764 | cur_arg = 1; |
| 9765 | |
| 9766 | if (!*args[cur_arg] || |
| 9767 | (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) { |
| 9768 | Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument.\n", |
| 9769 | file, linenum, args[0]); |
| 9770 | goto out_err; |
| 9771 | } |
| 9772 | |
| 9773 | LIST_INIT(&rule->arg.map.key); |
| 9774 | proxy->conf.args.ctx = ARGC_HRS; |
Thierry FOURNIER / OZON.IO | 8a4e442 | 2016-11-23 00:41:28 +0100 | [diff] [blame] | 9775 | error = NULL; |
Thierry FOURNIER / OZON.IO | 59fd511 | 2016-11-22 23:50:02 +0100 | [diff] [blame] | 9776 | 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] | 9777 | (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR, &error)) { |
| 9778 | Alert("parsing [%s:%d]: 'http-response %s': %s.\n", |
| 9779 | file, linenum, args[0], error); |
| 9780 | free(error); |
Thierry FOURNIER / OZON.IO | 59fd511 | 2016-11-22 23:50:02 +0100 | [diff] [blame] | 9781 | goto out_err; |
| 9782 | } |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 9783 | free(proxy->conf.lfs_file); |
| 9784 | proxy->conf.lfs_file = strdup(proxy->conf.args.file); |
| 9785 | proxy->conf.lfs_line = proxy->conf.args.line; |
| 9786 | cur_arg += 1; |
| 9787 | } else if (strncmp(args[0], "del-map", 7) == 0) { |
| 9788 | /* http-response del-map(<reference (map name)>) <key pattern> */ |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 9789 | rule->action = ACT_HTTP_DEL_MAP; |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 9790 | /* |
| 9791 | * '+ 8' for 'del-map(' |
| 9792 | * '- 9' for 'del-map(' + trailing ')' |
| 9793 | */ |
Willy Tarreau | 6c09c2c | 2014-04-25 21:38:08 +0200 | [diff] [blame] | 9794 | 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] | 9795 | |
| 9796 | cur_arg = 1; |
| 9797 | |
| 9798 | if (!*args[cur_arg] || |
| 9799 | (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) { |
| 9800 | Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument.\n", |
| 9801 | file, linenum, args[0]); |
| 9802 | goto out_err; |
| 9803 | } |
| 9804 | |
| 9805 | LIST_INIT(&rule->arg.map.key); |
| 9806 | proxy->conf.args.ctx = ARGC_HRS; |
Thierry FOURNIER / OZON.IO | 8a4e442 | 2016-11-23 00:41:28 +0100 | [diff] [blame] | 9807 | error = NULL; |
Thierry FOURNIER / OZON.IO | 59fd511 | 2016-11-22 23:50:02 +0100 | [diff] [blame] | 9808 | 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] | 9809 | (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR, &error)) { |
| 9810 | Alert("parsing [%s:%d]: 'http-response %s' %s.\n", |
| 9811 | file, linenum, args[0], error); |
| 9812 | free(error); |
Thierry FOURNIER / OZON.IO | 59fd511 | 2016-11-22 23:50:02 +0100 | [diff] [blame] | 9813 | goto out_err; |
| 9814 | } |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 9815 | free(proxy->conf.lfs_file); |
| 9816 | proxy->conf.lfs_file = strdup(proxy->conf.args.file); |
| 9817 | proxy->conf.lfs_line = proxy->conf.args.line; |
| 9818 | cur_arg += 1; |
| 9819 | } else if (strncmp(args[0], "set-map", 7) == 0) { |
| 9820 | /* http-response set-map(<reference (map name)>) <key pattern> <value pattern> */ |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 9821 | rule->action = ACT_HTTP_SET_MAP; |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 9822 | /* |
| 9823 | * '+ 8' for 'set-map(' |
| 9824 | * '- 9' for 'set-map(' + trailing ')' |
| 9825 | */ |
Willy Tarreau | 6c09c2c | 2014-04-25 21:38:08 +0200 | [diff] [blame] | 9826 | 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] | 9827 | |
| 9828 | cur_arg = 1; |
| 9829 | |
| 9830 | if (!*args[cur_arg] || !*args[cur_arg+1] || |
| 9831 | (*args[cur_arg+2] && strcmp(args[cur_arg+2], "if") != 0 && strcmp(args[cur_arg+2], "unless") != 0)) { |
| 9832 | Alert("parsing [%s:%d]: 'http-response %s' expects exactly 2 arguments.\n", |
| 9833 | file, linenum, args[0]); |
| 9834 | goto out_err; |
| 9835 | } |
| 9836 | |
| 9837 | LIST_INIT(&rule->arg.map.key); |
| 9838 | LIST_INIT(&rule->arg.map.value); |
| 9839 | |
| 9840 | proxy->conf.args.ctx = ARGC_HRS; |
| 9841 | |
| 9842 | /* key pattern */ |
Thierry FOURNIER / OZON.IO | 8a4e442 | 2016-11-23 00:41:28 +0100 | [diff] [blame] | 9843 | error = NULL; |
Thierry FOURNIER / OZON.IO | 59fd511 | 2016-11-22 23:50:02 +0100 | [diff] [blame] | 9844 | 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] | 9845 | (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR, &error)) { |
| 9846 | Alert("parsing [%s:%d]: 'http-response %s' name: %s.\n", |
| 9847 | file, linenum, args[0], error); |
| 9848 | free(error); |
Thierry FOURNIER / OZON.IO | 59fd511 | 2016-11-22 23:50:02 +0100 | [diff] [blame] | 9849 | goto out_err; |
| 9850 | } |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 9851 | |
| 9852 | /* value pattern */ |
Thierry FOURNIER / OZON.IO | 8a4e442 | 2016-11-23 00:41:28 +0100 | [diff] [blame] | 9853 | error = NULL; |
Thierry FOURNIER / OZON.IO | 59fd511 | 2016-11-22 23:50:02 +0100 | [diff] [blame] | 9854 | 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] | 9855 | (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR, &error)) { |
| 9856 | Alert("parsing [%s:%d]: 'http-response %s' value: %s.\n", |
| 9857 | file, linenum, args[0], error); |
| 9858 | free(error); |
Thierry FOURNIER / OZON.IO | 59fd511 | 2016-11-22 23:50:02 +0100 | [diff] [blame] | 9859 | goto out_err; |
| 9860 | } |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 9861 | |
| 9862 | free(proxy->conf.lfs_file); |
| 9863 | proxy->conf.lfs_file = strdup(proxy->conf.args.file); |
| 9864 | proxy->conf.lfs_line = proxy->conf.args.line; |
| 9865 | |
| 9866 | cur_arg += 2; |
Willy Tarreau | 51d861a | 2015-05-22 17:30:48 +0200 | [diff] [blame] | 9867 | } else if (strcmp(args[0], "redirect") == 0) { |
| 9868 | struct redirect_rule *redir; |
| 9869 | char *errmsg = NULL; |
| 9870 | |
| 9871 | if ((redir = http_parse_redirect_rule(file, linenum, proxy, (const char **)args + 1, &errmsg, 1, 1)) == NULL) { |
| 9872 | Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-response %s' rule : %s.\n", |
| 9873 | file, linenum, proxy_type_str(proxy), proxy->id, args[0], errmsg); |
| 9874 | goto out_err; |
| 9875 | } |
| 9876 | |
| 9877 | /* this redirect rule might already contain a parsed condition which |
| 9878 | * we'll pass to the http-request rule. |
| 9879 | */ |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 9880 | rule->action = ACT_HTTP_REDIR; |
Willy Tarreau | 51d861a | 2015-05-22 17:30:48 +0200 | [diff] [blame] | 9881 | rule->arg.redir = redir; |
| 9882 | rule->cond = redir->cond; |
| 9883 | redir->cond = NULL; |
| 9884 | cur_arg = 2; |
| 9885 | return rule; |
Ruoshan Huang | e4edc6b | 2016-07-14 15:07:45 +0800 | [diff] [blame] | 9886 | } else if (strncmp(args[0], "track-sc", 8) == 0 && |
| 9887 | args[0][9] == '\0' && args[0][8] >= '0' && |
| 9888 | args[0][8] < '0' + MAX_SESS_STKCTR) { /* track-sc 0..9 */ |
| 9889 | struct sample_expr *expr; |
| 9890 | unsigned int where; |
| 9891 | char *err = NULL; |
| 9892 | |
| 9893 | cur_arg = 1; |
| 9894 | proxy->conf.args.ctx = ARGC_TRK; |
| 9895 | |
| 9896 | expr = sample_parse_expr((char **)args, &cur_arg, file, linenum, &err, &proxy->conf.args); |
| 9897 | if (!expr) { |
| 9898 | Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-response %s' rule : %s.\n", |
| 9899 | file, linenum, proxy_type_str(proxy), proxy->id, args[0], err); |
| 9900 | free(err); |
| 9901 | goto out_err; |
| 9902 | } |
| 9903 | |
| 9904 | where = 0; |
| 9905 | if (proxy->cap & PR_CAP_FE) |
| 9906 | where |= SMP_VAL_FE_HRS_HDR; |
| 9907 | if (proxy->cap & PR_CAP_BE) |
| 9908 | where |= SMP_VAL_BE_HRS_HDR; |
| 9909 | |
| 9910 | if (!(expr->fetch->val & where)) { |
| 9911 | Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-response %s' rule :" |
| 9912 | " fetch method '%s' extracts information from '%s', none of which is available here.\n", |
| 9913 | file, linenum, proxy_type_str(proxy), proxy->id, args[0], |
| 9914 | args[cur_arg-1], sample_src_names(expr->fetch->use)); |
| 9915 | free(expr); |
| 9916 | goto out_err; |
| 9917 | } |
| 9918 | |
| 9919 | if (strcmp(args[cur_arg], "table") == 0) { |
| 9920 | cur_arg++; |
| 9921 | if (!args[cur_arg]) { |
| 9922 | Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-response %s' rule : missing table name.\n", |
| 9923 | file, linenum, proxy_type_str(proxy), proxy->id, args[0]); |
| 9924 | free(expr); |
| 9925 | goto out_err; |
| 9926 | } |
| 9927 | /* we copy the table name for now, it will be resolved later */ |
| 9928 | rule->arg.trk_ctr.table.n = strdup(args[cur_arg]); |
| 9929 | cur_arg++; |
| 9930 | } |
| 9931 | rule->arg.trk_ctr.expr = expr; |
| 9932 | rule->action = ACT_ACTION_TRK_SC0 + args[0][8] - '0'; |
William Lallemand | 73025dd | 2014-04-24 14:38:37 +0200 | [diff] [blame] | 9933 | } else if (((custom = action_http_res_custom(args[0])) != NULL)) { |
| 9934 | char *errmsg = NULL; |
| 9935 | cur_arg = 1; |
| 9936 | /* try in the module list */ |
Thierry FOURNIER | 5563e4b | 2015-08-14 19:20:07 +0200 | [diff] [blame] | 9937 | rule->from = ACT_F_HTTP_RES; |
Thierry FOURNIER | 85c6c97 | 2015-09-22 19:14:35 +0200 | [diff] [blame] | 9938 | rule->kw = custom; |
Thierry FOURNIER | afa8049 | 2015-08-19 09:04:15 +0200 | [diff] [blame] | 9939 | 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] | 9940 | Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-response %s' rule : %s.\n", |
| 9941 | file, linenum, proxy_type_str(proxy), proxy->id, args[0], errmsg); |
| 9942 | free(errmsg); |
| 9943 | goto out_err; |
| 9944 | } |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 9945 | } else { |
Thierry FOURNIER | ab95e65 | 2015-10-02 08:24:51 +0200 | [diff] [blame] | 9946 | action_build_list(&http_res_keywords.list, &trash); |
| 9947 | Alert("parsing [%s:%d]: 'http-response' expects 'allow', 'deny', 'redirect', " |
| 9948 | "'add-header', 'del-header', 'set-header', 'replace-header', 'replace-value', 'set-nice', " |
Ruoshan Huang | e4edc6b | 2016-07-14 15:07:45 +0800 | [diff] [blame] | 9949 | "'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] | 9950 | "%s%s, but got '%s'%s.\n", |
Thierry FOURNIER | ab95e65 | 2015-10-02 08:24:51 +0200 | [diff] [blame] | 9951 | file, linenum, *trash.str ? ", " : "", trash.str, args[0], *args[0] ? "" : " (missing argument)"); |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 9952 | goto out_err; |
| 9953 | } |
| 9954 | |
| 9955 | if (strcmp(args[cur_arg], "if") == 0 || strcmp(args[cur_arg], "unless") == 0) { |
| 9956 | struct acl_cond *cond; |
| 9957 | char *errmsg = NULL; |
| 9958 | |
| 9959 | if ((cond = build_acl_cond(file, linenum, proxy, args+cur_arg, &errmsg)) == NULL) { |
| 9960 | Alert("parsing [%s:%d] : error detected while parsing an 'http-response %s' condition : %s.\n", |
| 9961 | file, linenum, args[0], errmsg); |
| 9962 | free(errmsg); |
| 9963 | goto out_err; |
| 9964 | } |
| 9965 | rule->cond = cond; |
| 9966 | } |
| 9967 | else if (*args[cur_arg]) { |
| 9968 | Alert("parsing [%s:%d]: 'http-response %s' expects" |
| 9969 | " either 'if' or 'unless' followed by a condition but found '%s'.\n", |
| 9970 | file, linenum, args[0], args[cur_arg]); |
| 9971 | goto out_err; |
| 9972 | } |
| 9973 | |
| 9974 | return rule; |
| 9975 | out_err: |
| 9976 | free(rule); |
| 9977 | return NULL; |
| 9978 | } |
| 9979 | |
Willy Tarreau | 4baae24 | 2012-12-27 12:00:31 +0100 | [diff] [blame] | 9980 | /* 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] | 9981 | * 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] | 9982 | * dynamic log-format rule instead of a static string. Parameter <dir> indicates |
| 9983 | * 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] | 9984 | */ |
| 9985 | 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] | 9986 | const char **args, char **errmsg, int use_fmt, int dir) |
Willy Tarreau | 4baae24 | 2012-12-27 12:00:31 +0100 | [diff] [blame] | 9987 | { |
| 9988 | struct redirect_rule *rule; |
| 9989 | int cur_arg; |
| 9990 | int type = REDIRECT_TYPE_NONE; |
| 9991 | int code = 302; |
| 9992 | const char *destination = NULL; |
| 9993 | const char *cookie = NULL; |
| 9994 | int cookie_set = 0; |
| 9995 | unsigned int flags = REDIRECT_FLAG_NONE; |
| 9996 | struct acl_cond *cond = NULL; |
| 9997 | |
| 9998 | cur_arg = 0; |
| 9999 | while (*(args[cur_arg])) { |
| 10000 | if (strcmp(args[cur_arg], "location") == 0) { |
| 10001 | if (!*args[cur_arg + 1]) |
| 10002 | goto missing_arg; |
| 10003 | |
| 10004 | type = REDIRECT_TYPE_LOCATION; |
| 10005 | cur_arg++; |
| 10006 | destination = args[cur_arg]; |
| 10007 | } |
| 10008 | else if (strcmp(args[cur_arg], "prefix") == 0) { |
| 10009 | if (!*args[cur_arg + 1]) |
| 10010 | goto missing_arg; |
Willy Tarreau | 4baae24 | 2012-12-27 12:00:31 +0100 | [diff] [blame] | 10011 | type = REDIRECT_TYPE_PREFIX; |
| 10012 | cur_arg++; |
| 10013 | destination = args[cur_arg]; |
| 10014 | } |
| 10015 | else if (strcmp(args[cur_arg], "scheme") == 0) { |
| 10016 | if (!*args[cur_arg + 1]) |
| 10017 | goto missing_arg; |
| 10018 | |
| 10019 | type = REDIRECT_TYPE_SCHEME; |
| 10020 | cur_arg++; |
| 10021 | destination = args[cur_arg]; |
| 10022 | } |
| 10023 | else if (strcmp(args[cur_arg], "set-cookie") == 0) { |
| 10024 | if (!*args[cur_arg + 1]) |
| 10025 | goto missing_arg; |
| 10026 | |
| 10027 | cur_arg++; |
| 10028 | cookie = args[cur_arg]; |
| 10029 | cookie_set = 1; |
| 10030 | } |
| 10031 | else if (strcmp(args[cur_arg], "clear-cookie") == 0) { |
| 10032 | if (!*args[cur_arg + 1]) |
| 10033 | goto missing_arg; |
| 10034 | |
| 10035 | cur_arg++; |
| 10036 | cookie = args[cur_arg]; |
| 10037 | cookie_set = 0; |
| 10038 | } |
| 10039 | else if (strcmp(args[cur_arg], "code") == 0) { |
| 10040 | if (!*args[cur_arg + 1]) |
| 10041 | goto missing_arg; |
| 10042 | |
| 10043 | cur_arg++; |
| 10044 | code = atol(args[cur_arg]); |
Yves Lafon | 3e8d1ae | 2013-03-11 11:06:05 -0400 | [diff] [blame] | 10045 | if (code < 301 || code > 308 || (code > 303 && code < 307)) { |
Willy Tarreau | 4baae24 | 2012-12-27 12:00:31 +0100 | [diff] [blame] | 10046 | memprintf(errmsg, |
Yves Lafon | 3e8d1ae | 2013-03-11 11:06:05 -0400 | [diff] [blame] | 10047 | "'%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] | 10048 | args[cur_arg - 1], args[cur_arg]); |
| 10049 | return NULL; |
| 10050 | } |
| 10051 | } |
| 10052 | else if (!strcmp(args[cur_arg],"drop-query")) { |
| 10053 | flags |= REDIRECT_FLAG_DROP_QS; |
| 10054 | } |
| 10055 | else if (!strcmp(args[cur_arg],"append-slash")) { |
| 10056 | flags |= REDIRECT_FLAG_APPEND_SLASH; |
| 10057 | } |
| 10058 | else if (strcmp(args[cur_arg], "if") == 0 || |
| 10059 | strcmp(args[cur_arg], "unless") == 0) { |
| 10060 | cond = build_acl_cond(file, linenum, curproxy, (const char **)args + cur_arg, errmsg); |
| 10061 | if (!cond) { |
| 10062 | memprintf(errmsg, "error in condition: %s", *errmsg); |
| 10063 | return NULL; |
| 10064 | } |
| 10065 | break; |
| 10066 | } |
| 10067 | else { |
| 10068 | memprintf(errmsg, |
| 10069 | "expects 'code', 'prefix', 'location', 'scheme', 'set-cookie', 'clear-cookie', 'drop-query' or 'append-slash' (was '%s')", |
| 10070 | args[cur_arg]); |
| 10071 | return NULL; |
| 10072 | } |
| 10073 | cur_arg++; |
| 10074 | } |
| 10075 | |
| 10076 | if (type == REDIRECT_TYPE_NONE) { |
| 10077 | memprintf(errmsg, "redirection type expected ('prefix', 'location', or 'scheme')"); |
| 10078 | return NULL; |
| 10079 | } |
| 10080 | |
Willy Tarreau | be4653b | 2015-05-28 15:26:58 +0200 | [diff] [blame] | 10081 | if (dir && type != REDIRECT_TYPE_LOCATION) { |
| 10082 | memprintf(errmsg, "response only supports redirect type 'location'"); |
| 10083 | return NULL; |
| 10084 | } |
| 10085 | |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 10086 | rule = calloc(1, sizeof(*rule)); |
Willy Tarreau | 4baae24 | 2012-12-27 12:00:31 +0100 | [diff] [blame] | 10087 | rule->cond = cond; |
Willy Tarreau | 60e0838 | 2013-12-03 00:48:45 +0100 | [diff] [blame] | 10088 | LIST_INIT(&rule->rdr_fmt); |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 10089 | |
| 10090 | if (!use_fmt) { |
| 10091 | /* old-style static redirect rule */ |
| 10092 | rule->rdr_str = strdup(destination); |
| 10093 | rule->rdr_len = strlen(destination); |
| 10094 | } |
| 10095 | else { |
| 10096 | /* log-format based redirect rule */ |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 10097 | |
| 10098 | /* Parse destination. Note that in the REDIRECT_TYPE_PREFIX case, |
| 10099 | * if prefix == "/", we don't want to add anything, otherwise it |
| 10100 | * makes it hard for the user to configure a self-redirection. |
| 10101 | */ |
Godbach | d972203 | 2014-12-18 15:44:58 +0800 | [diff] [blame] | 10102 | curproxy->conf.args.ctx = ARGC_RDR; |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 10103 | if (!(type == REDIRECT_TYPE_PREFIX && destination[0] == '/' && destination[1] == '\0')) { |
Thierry FOURNIER / OZON.IO | 59fd511 | 2016-11-22 23:50:02 +0100 | [diff] [blame] | 10104 | if (!parse_logformat_string(destination, curproxy, &rule->rdr_fmt, LOG_OPT_HTTP, |
| 10105 | 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] | 10106 | : (curproxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR, |
| 10107 | errmsg)) { |
Thierry FOURNIER / OZON.IO | 59fd511 | 2016-11-22 23:50:02 +0100 | [diff] [blame] | 10108 | return NULL; |
| 10109 | } |
Willy Tarreau | 59ad1a2 | 2014-01-29 14:39:58 +0100 | [diff] [blame] | 10110 | free(curproxy->conf.lfs_file); |
| 10111 | curproxy->conf.lfs_file = strdup(curproxy->conf.args.file); |
| 10112 | curproxy->conf.lfs_line = curproxy->conf.args.line; |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 10113 | } |
| 10114 | } |
| 10115 | |
Willy Tarreau | 4baae24 | 2012-12-27 12:00:31 +0100 | [diff] [blame] | 10116 | if (cookie) { |
| 10117 | /* depending on cookie_set, either we want to set the cookie, or to clear it. |
| 10118 | * a clear consists in appending "; path=/; Max-Age=0;" at the end. |
| 10119 | */ |
| 10120 | rule->cookie_len = strlen(cookie); |
| 10121 | if (cookie_set) { |
| 10122 | rule->cookie_str = malloc(rule->cookie_len + 10); |
| 10123 | memcpy(rule->cookie_str, cookie, rule->cookie_len); |
| 10124 | memcpy(rule->cookie_str + rule->cookie_len, "; path=/;", 10); |
| 10125 | rule->cookie_len += 9; |
| 10126 | } else { |
| 10127 | rule->cookie_str = malloc(rule->cookie_len + 21); |
| 10128 | memcpy(rule->cookie_str, cookie, rule->cookie_len); |
| 10129 | memcpy(rule->cookie_str + rule->cookie_len, "; path=/; Max-Age=0;", 21); |
| 10130 | rule->cookie_len += 20; |
| 10131 | } |
| 10132 | } |
| 10133 | rule->type = type; |
| 10134 | rule->code = code; |
| 10135 | rule->flags = flags; |
| 10136 | LIST_INIT(&rule->list); |
| 10137 | return rule; |
| 10138 | |
| 10139 | missing_arg: |
| 10140 | memprintf(errmsg, "missing argument for '%s'", args[cur_arg]); |
| 10141 | return NULL; |
| 10142 | } |
| 10143 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 10144 | /************************************************************************/ |
| 10145 | /* The code below is dedicated to ACL parsing and matching */ |
| 10146 | /************************************************************************/ |
| 10147 | |
| 10148 | |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 10149 | /* This function ensures that the prerequisites for an L7 fetch are ready, |
| 10150 | * which means that a request or response is ready. If some data is missing, |
| 10151 | * 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] | 10152 | * to extract data from L7. If <req_vol> is non-null during a request prefetch, |
| 10153 | * another test is made to ensure the required information is not gone. |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 10154 | * |
| 10155 | * The function returns : |
Willy Tarreau | 506d050 | 2013-07-06 13:29:24 +0200 | [diff] [blame] | 10156 | * 0 with SMP_F_MAY_CHANGE in the sample flags if some data is missing to |
| 10157 | * decide whether or not an HTTP message is present ; |
| 10158 | * 0 if the requested data cannot be fetched or if it is certain that |
| 10159 | * we'll never have any HTTP message there ; |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 10160 | * 1 if an HTTP message is ready |
| 10161 | */ |
James Rosewell | 91a41cb | 2015-09-18 17:11:16 +0100 | [diff] [blame] | 10162 | 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] | 10163 | const struct arg *args, struct sample *smp, int req_vol) |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 10164 | { |
Willy Tarreau | 192252e | 2015-04-04 01:47:55 +0200 | [diff] [blame] | 10165 | struct http_txn *txn; |
| 10166 | struct http_msg *msg; |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 10167 | |
Willy Tarreau | be508f1 | 2016-03-10 11:47:01 +0100 | [diff] [blame] | 10168 | /* Note: it is possible that <s> is NULL when called before stream |
| 10169 | * initialization (eg: tcp-request connection), so this function is the |
| 10170 | * one responsible for guarding against this case for all HTTP users. |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 10171 | */ |
Willy Tarreau | 192252e | 2015-04-04 01:47:55 +0200 | [diff] [blame] | 10172 | if (!s) |
| 10173 | return 0; |
Willy Tarreau | be508f1 | 2016-03-10 11:47:01 +0100 | [diff] [blame] | 10174 | |
Thierry FOURNIER | ed08d6a | 2015-09-24 08:40:18 +0200 | [diff] [blame] | 10175 | if (!s->txn) { |
| 10176 | if (unlikely(!http_alloc_txn(s))) |
| 10177 | return 0; /* not enough memory */ |
| 10178 | http_init_txn(s); |
| 10179 | } |
Willy Tarreau | 192252e | 2015-04-04 01:47:55 +0200 | [diff] [blame] | 10180 | txn = s->txn; |
Willy Tarreau | 192252e | 2015-04-04 01:47:55 +0200 | [diff] [blame] | 10181 | msg = &txn->req; |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 10182 | |
| 10183 | /* Check for a dependency on a request */ |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 10184 | smp->data.type = SMP_T_BOOL; |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 10185 | |
Willy Tarreau | 32a6f2e | 2012-04-25 10:13:36 +0200 | [diff] [blame] | 10186 | if ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) { |
Willy Tarreau | aae75e3 | 2013-03-29 12:31:49 +0100 | [diff] [blame] | 10187 | /* If the buffer does not leave enough free space at the end, |
| 10188 | * we must first realign it. |
| 10189 | */ |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 10190 | if (s->req.buf->p > s->req.buf->data && |
| 10191 | s->req.buf->i + s->req.buf->p > s->req.buf->data + s->req.buf->size - global.tune.maxrewrite) |
| 10192 | buffer_slow_realign(s->req.buf); |
Willy Tarreau | aae75e3 | 2013-03-29 12:31:49 +0100 | [diff] [blame] | 10193 | |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 10194 | if (unlikely(txn->req.msg_state < HTTP_MSG_BODY)) { |
Willy Tarreau | 472b1ee | 2013-10-14 22:41:30 +0200 | [diff] [blame] | 10195 | if (msg->msg_state == HTTP_MSG_ERROR) |
Willy Tarreau | 506d050 | 2013-07-06 13:29:24 +0200 | [diff] [blame] | 10196 | return 0; |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 10197 | |
| 10198 | /* Try to decode HTTP request */ |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 10199 | if (likely(msg->next < s->req.buf->i)) |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 10200 | http_msg_analyzer(msg, &txn->hdr_idx); |
| 10201 | |
| 10202 | /* Still no valid request ? */ |
| 10203 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) { |
Willy Tarreau | 3bf1b2b | 2012-08-27 20:46:07 +0200 | [diff] [blame] | 10204 | if ((msg->msg_state == HTTP_MSG_ERROR) || |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 10205 | buffer_full(s->req.buf, global.tune.maxrewrite)) { |
Willy Tarreau | 506d050 | 2013-07-06 13:29:24 +0200 | [diff] [blame] | 10206 | return 0; |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 10207 | } |
| 10208 | /* wait for final state */ |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 10209 | smp->flags |= SMP_F_MAY_CHANGE; |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 10210 | return 0; |
| 10211 | } |
| 10212 | |
| 10213 | /* OK we just got a valid HTTP request. We have some minor |
| 10214 | * preparation to perform so that further checks can rely |
| 10215 | * on HTTP tests. |
| 10216 | */ |
Willy Tarreau | aae75e3 | 2013-03-29 12:31:49 +0100 | [diff] [blame] | 10217 | |
| 10218 | /* If the request was parsed but was too large, we must absolutely |
| 10219 | * return an error so that it is not processed. At the moment this |
| 10220 | * cannot happen, but if the parsers are to change in the future, |
| 10221 | * we want this check to be maintained. |
| 10222 | */ |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 10223 | if (unlikely(s->req.buf->i + s->req.buf->p > |
| 10224 | s->req.buf->data + s->req.buf->size - global.tune.maxrewrite)) { |
Willy Tarreau | 10e61cb | 2017-01-04 14:51:22 +0100 | [diff] [blame] | 10225 | msg->err_state = msg->msg_state; |
Willy Tarreau | aae75e3 | 2013-03-29 12:31:49 +0100 | [diff] [blame] | 10226 | msg->msg_state = HTTP_MSG_ERROR; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 10227 | smp->data.u.sint = 1; |
Willy Tarreau | aae75e3 | 2013-03-29 12:31:49 +0100 | [diff] [blame] | 10228 | return 1; |
| 10229 | } |
| 10230 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 10231 | 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] | 10232 | if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD) |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 10233 | s->flags |= SF_REDIRECTABLE; |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 10234 | |
Willy Tarreau | 506d050 | 2013-07-06 13:29:24 +0200 | [diff] [blame] | 10235 | if (unlikely(msg->sl.rq.v_l == 0) && !http_upgrade_v09_to_v10(txn)) |
| 10236 | return 0; |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 10237 | } |
| 10238 | |
Willy Tarreau | 506d050 | 2013-07-06 13:29:24 +0200 | [diff] [blame] | 10239 | if (req_vol && txn->rsp.msg_state != HTTP_MSG_RPBEFORE) { |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 10240 | return 0; /* data might have moved and indexes changed */ |
Willy Tarreau | 506d050 | 2013-07-06 13:29:24 +0200 | [diff] [blame] | 10241 | } |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 10242 | |
| 10243 | /* otherwise everything's ready for the request */ |
| 10244 | } |
Willy Tarreau | 24e32d8 | 2012-04-23 23:55:44 +0200 | [diff] [blame] | 10245 | else { |
| 10246 | /* Check for a dependency on a response */ |
Willy Tarreau | 506d050 | 2013-07-06 13:29:24 +0200 | [diff] [blame] | 10247 | if (txn->rsp.msg_state < HTTP_MSG_BODY) { |
| 10248 | smp->flags |= SMP_F_MAY_CHANGE; |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 10249 | return 0; |
Willy Tarreau | 506d050 | 2013-07-06 13:29:24 +0200 | [diff] [blame] | 10250 | } |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 10251 | } |
| 10252 | |
| 10253 | /* everything's OK */ |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 10254 | smp->data.u.sint = 1; |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 10255 | return 1; |
| 10256 | } |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 10257 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 10258 | /* 1. Check on METHOD |
| 10259 | * We use the pre-parsed method if it is known, and store its number as an |
| 10260 | * integer. If it is unknown, we use the pointer and the length. |
| 10261 | */ |
Thierry FOURNIER | e47e4e2 | 2014-04-28 11:18:57 +0200 | [diff] [blame] | 10262 | 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] | 10263 | { |
| 10264 | int len, meth; |
| 10265 | |
Thierry FOURNIER | 580c32c | 2014-01-24 10:58:12 +0100 | [diff] [blame] | 10266 | len = strlen(text); |
| 10267 | meth = find_http_meth(text, len); |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 10268 | |
| 10269 | pattern->val.i = meth; |
| 10270 | if (meth == HTTP_METH_OTHER) { |
Willy Tarreau | 912c119 | 2014-08-29 15:15:50 +0200 | [diff] [blame] | 10271 | pattern->ptr.str = (char *)text; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 10272 | pattern->len = len; |
| 10273 | } |
Thierry FOURNIER | d437314 | 2013-12-17 01:10:10 +0100 | [diff] [blame] | 10274 | else { |
| 10275 | pattern->ptr.str = NULL; |
| 10276 | pattern->len = 0; |
Thierry FOURNIER | d437314 | 2013-12-17 01:10:10 +0100 | [diff] [blame] | 10277 | } |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 10278 | return 1; |
| 10279 | } |
| 10280 | |
Willy Tarreau | 8e5e955 | 2011-12-16 15:38:49 +0100 | [diff] [blame] | 10281 | /* This function fetches the method of current HTTP request and stores |
| 10282 | * it in the global pattern struct as a chunk. There are two possibilities : |
| 10283 | * - if the method is known (not HTTP_METH_OTHER), its identifier is stored |
| 10284 | * in <len> and <ptr> is NULL ; |
| 10285 | * - if the method is unknown (HTTP_METH_OTHER), <ptr> points to the text and |
| 10286 | * <len> to its length. |
Thierry FOURNIER | a65b343 | 2013-11-28 18:22:00 +0100 | [diff] [blame] | 10287 | * This is intended to be used with pat_match_meth() only. |
Willy Tarreau | 8e5e955 | 2011-12-16 15:38:49 +0100 | [diff] [blame] | 10288 | */ |
Willy Tarreau | d41f8d8 | 2007-06-10 10:06:18 +0200 | [diff] [blame] | 10289 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10290 | 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] | 10291 | { |
| 10292 | int meth; |
Willy Tarreau | be508f1 | 2016-03-10 11:47:01 +0100 | [diff] [blame] | 10293 | struct http_txn *txn; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 10294 | |
Willy Tarreau | 24e32d8 | 2012-04-23 23:55:44 +0200 | [diff] [blame] | 10295 | CHECK_HTTP_MESSAGE_FIRST_PERM(); |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 10296 | |
Willy Tarreau | be508f1 | 2016-03-10 11:47:01 +0100 | [diff] [blame] | 10297 | txn = smp->strm->txn; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 10298 | meth = txn->meth; |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 10299 | smp->data.type = SMP_T_METH; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 10300 | smp->data.u.meth.meth = meth; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 10301 | if (meth == HTTP_METH_OTHER) { |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 10302 | if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE) |
| 10303 | /* ensure the indexes are not affected */ |
| 10304 | return 0; |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 10305 | smp->flags |= SMP_F_CONST; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 10306 | smp->data.u.meth.str.len = txn->req.sl.rq.m_l; |
| 10307 | smp->data.u.meth.str.str = txn->req.chn->buf->p; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 10308 | } |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 10309 | smp->flags |= SMP_F_VOL_1ST; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 10310 | return 1; |
| 10311 | } |
| 10312 | |
Willy Tarreau | 8e5e955 | 2011-12-16 15:38:49 +0100 | [diff] [blame] | 10313 | /* See above how the method is stored in the global pattern */ |
Thierry FOURNIER | 5338eea | 2013-12-16 14:22:13 +0100 | [diff] [blame] | 10314 | 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] | 10315 | { |
Willy Tarreau | c8d7c96 | 2007-06-17 08:20:33 +0200 | [diff] [blame] | 10316 | int icase; |
Thierry FOURNIER | 5338eea | 2013-12-16 14:22:13 +0100 | [diff] [blame] | 10317 | struct pattern_list *lst; |
| 10318 | struct pattern *pattern; |
Willy Tarreau | c8d7c96 | 2007-06-17 08:20:33 +0200 | [diff] [blame] | 10319 | |
Thierry FOURNIER | 5338eea | 2013-12-16 14:22:13 +0100 | [diff] [blame] | 10320 | list_for_each_entry(lst, &expr->patterns, list) { |
| 10321 | pattern = &lst->pat; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 10322 | |
Thierry FOURNIER | 5338eea | 2013-12-16 14:22:13 +0100 | [diff] [blame] | 10323 | /* well-known method */ |
| 10324 | if (pattern->val.i != HTTP_METH_OTHER) { |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 10325 | if (smp->data.u.meth.meth == pattern->val.i) |
Thierry FOURNIER | 5338eea | 2013-12-16 14:22:13 +0100 | [diff] [blame] | 10326 | return pattern; |
| 10327 | else |
| 10328 | continue; |
| 10329 | } |
Willy Tarreau | c8d7c96 | 2007-06-17 08:20:33 +0200 | [diff] [blame] | 10330 | |
Thierry FOURNIER | 5338eea | 2013-12-16 14:22:13 +0100 | [diff] [blame] | 10331 | /* Other method, we must compare the strings */ |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 10332 | if (pattern->len != smp->data.u.meth.str.len) |
Thierry FOURNIER | 5338eea | 2013-12-16 14:22:13 +0100 | [diff] [blame] | 10333 | continue; |
| 10334 | |
Thierry FOURNIER | e47e4e2 | 2014-04-28 11:18:57 +0200 | [diff] [blame] | 10335 | icase = expr->mflags & PAT_MF_IGNORE_CASE; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 10336 | if ((icase && strncasecmp(pattern->ptr.str, smp->data.u.meth.str.str, smp->data.u.meth.str.len) == 0) || |
| 10337 | (!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] | 10338 | return pattern; |
| 10339 | } |
| 10340 | return NULL; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 10341 | } |
| 10342 | |
Willy Tarreau | d41f8d8 | 2007-06-10 10:06:18 +0200 | [diff] [blame] | 10343 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10344 | 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] | 10345 | { |
Willy Tarreau | be508f1 | 2016-03-10 11:47:01 +0100 | [diff] [blame] | 10346 | struct http_txn *txn; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 10347 | char *ptr; |
| 10348 | int len; |
| 10349 | |
Willy Tarreau | c0239e0 | 2012-04-16 14:42:55 +0200 | [diff] [blame] | 10350 | CHECK_HTTP_MESSAGE_FIRST(); |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 10351 | |
Willy Tarreau | be508f1 | 2016-03-10 11:47:01 +0100 | [diff] [blame] | 10352 | txn = smp->strm->txn; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 10353 | len = txn->req.sl.rq.v_l; |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 10354 | ptr = txn->req.chn->buf->p + txn->req.sl.rq.v; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 10355 | |
| 10356 | while ((len-- > 0) && (*ptr++ != '/')); |
| 10357 | if (len <= 0) |
| 10358 | return 0; |
| 10359 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 10360 | smp->data.type = SMP_T_STR; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 10361 | smp->data.u.str.str = ptr; |
| 10362 | smp->data.u.str.len = len; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 10363 | |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 10364 | smp->flags = SMP_F_VOL_1ST | SMP_F_CONST; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 10365 | return 1; |
| 10366 | } |
| 10367 | |
Willy Tarreau | d41f8d8 | 2007-06-10 10:06:18 +0200 | [diff] [blame] | 10368 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10369 | 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] | 10370 | { |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 10371 | struct http_txn *txn; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 10372 | char *ptr; |
| 10373 | int len; |
| 10374 | |
Willy Tarreau | c0239e0 | 2012-04-16 14:42:55 +0200 | [diff] [blame] | 10375 | CHECK_HTTP_MESSAGE_FIRST(); |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 10376 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 10377 | txn = smp->strm->txn; |
Willy Tarreau | f26b252 | 2012-12-14 08:33:14 +0100 | [diff] [blame] | 10378 | if (txn->rsp.msg_state < HTTP_MSG_BODY) |
| 10379 | return 0; |
| 10380 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 10381 | len = txn->rsp.sl.st.v_l; |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 10382 | ptr = txn->rsp.chn->buf->p; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 10383 | |
| 10384 | while ((len-- > 0) && (*ptr++ != '/')); |
| 10385 | if (len <= 0) |
| 10386 | return 0; |
| 10387 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 10388 | smp->data.type = SMP_T_STR; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 10389 | smp->data.u.str.str = ptr; |
| 10390 | smp->data.u.str.len = len; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 10391 | |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 10392 | smp->flags = SMP_F_VOL_1ST | SMP_F_CONST; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 10393 | return 1; |
| 10394 | } |
| 10395 | |
| 10396 | /* 3. Check on Status Code. We manipulate integers here. */ |
Willy Tarreau | d41f8d8 | 2007-06-10 10:06:18 +0200 | [diff] [blame] | 10397 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10398 | 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] | 10399 | { |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 10400 | struct http_txn *txn; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 10401 | char *ptr; |
| 10402 | int len; |
| 10403 | |
Willy Tarreau | c0239e0 | 2012-04-16 14:42:55 +0200 | [diff] [blame] | 10404 | CHECK_HTTP_MESSAGE_FIRST(); |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 10405 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 10406 | txn = smp->strm->txn; |
Willy Tarreau | f26b252 | 2012-12-14 08:33:14 +0100 | [diff] [blame] | 10407 | if (txn->rsp.msg_state < HTTP_MSG_BODY) |
| 10408 | return 0; |
| 10409 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 10410 | len = txn->rsp.sl.st.c_l; |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 10411 | ptr = txn->rsp.chn->buf->p + txn->rsp.sl.st.c; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 10412 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 10413 | smp->data.type = SMP_T_SINT; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 10414 | smp->data.u.sint = __strl2ui(ptr, len); |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 10415 | smp->flags = SMP_F_VOL_1ST; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 10416 | return 1; |
| 10417 | } |
| 10418 | |
Thierry Fournier | f4011dd | 2016-03-29 17:23:51 +0200 | [diff] [blame] | 10419 | static int |
| 10420 | smp_fetch_uniqueid(const struct arg *args, struct sample *smp, const char *kw, void *private) |
| 10421 | { |
| 10422 | if (LIST_ISEMPTY(&smp->sess->fe->format_unique_id)) |
| 10423 | return 0; |
| 10424 | |
| 10425 | if (!smp->strm->unique_id) { |
| 10426 | if ((smp->strm->unique_id = pool_alloc2(pool2_uniqueid)) == NULL) |
| 10427 | return 0; |
| 10428 | smp->strm->unique_id[0] = '\0'; |
| 10429 | } |
| 10430 | smp->data.u.str.len = build_logline(smp->strm, smp->strm->unique_id, |
| 10431 | UNIQUEID_LEN, &smp->sess->fe->format_unique_id); |
| 10432 | |
| 10433 | smp->data.type = SMP_T_STR; |
| 10434 | smp->data.u.str.str = smp->strm->unique_id; |
| 10435 | smp->flags = SMP_F_CONST; |
| 10436 | return 1; |
| 10437 | } |
| 10438 | |
Thierry FOURNIER | d7d8881 | 2017-04-19 15:15:14 +0200 | [diff] [blame] | 10439 | /* Returns a string block containing all headers including the |
| 10440 | * empty line wich separes headers from the body. This is useful |
| 10441 | * form some headers analysis. |
| 10442 | */ |
| 10443 | static int |
| 10444 | smp_fetch_hdrs(const struct arg *args, struct sample *smp, const char *kw, void *private) |
| 10445 | { |
| 10446 | struct http_msg *msg; |
| 10447 | struct hdr_idx *idx; |
| 10448 | struct http_txn *txn; |
| 10449 | |
| 10450 | CHECK_HTTP_MESSAGE_FIRST(); |
| 10451 | |
| 10452 | txn = smp->strm->txn; |
| 10453 | idx = &txn->hdr_idx; |
| 10454 | msg = &txn->req; |
| 10455 | |
| 10456 | smp->data.type = SMP_T_STR; |
| 10457 | smp->data.u.str.str = msg->chn->buf->p + hdr_idx_first_pos(idx); |
| 10458 | smp->data.u.str.len = msg->eoh - hdr_idx_first_pos(idx) + 1 + |
| 10459 | (msg->chn->buf->p[msg->eoh] == '\r'); |
| 10460 | |
| 10461 | return 1; |
| 10462 | } |
| 10463 | |
Thierry FOURNIER | 5617dce | 2017-04-09 05:38:19 +0200 | [diff] [blame] | 10464 | /* Returns the header request in a length/value encoded format. |
| 10465 | * This is useful for exchanges with the SPOE. |
| 10466 | * |
| 10467 | * A "length value" is a multibyte code encoding numbers. It uses the |
| 10468 | * SPOE format. The encoding is the following: |
| 10469 | * |
| 10470 | * Each couple "header name" / "header value" is composed |
| 10471 | * like this: |
| 10472 | * "length value" "header name bytes" |
| 10473 | * "length value" "header value bytes" |
| 10474 | * When the last header is reached, the header name and the header |
| 10475 | * value are empty. Their length are 0 |
| 10476 | */ |
| 10477 | static int |
| 10478 | smp_fetch_hdrs_bin(const struct arg *args, struct sample *smp, const char *kw, void *private) |
| 10479 | { |
| 10480 | struct http_msg *msg; |
| 10481 | struct chunk *temp; |
| 10482 | struct hdr_idx *idx; |
| 10483 | const char *cur_ptr, *cur_next, *p; |
| 10484 | int old_idx, cur_idx; |
| 10485 | struct hdr_idx_elem *cur_hdr; |
| 10486 | const char *hn, *hv; |
| 10487 | int hnl, hvl; |
| 10488 | int ret; |
| 10489 | struct http_txn *txn; |
| 10490 | char *buf; |
| 10491 | char *end; |
| 10492 | |
| 10493 | CHECK_HTTP_MESSAGE_FIRST(); |
| 10494 | |
| 10495 | temp = get_trash_chunk(); |
| 10496 | buf = temp->str; |
| 10497 | end = temp->str + temp->size; |
| 10498 | |
| 10499 | txn = smp->strm->txn; |
| 10500 | idx = &txn->hdr_idx; |
| 10501 | msg = &txn->req; |
| 10502 | |
| 10503 | /* Build array of headers. */ |
| 10504 | old_idx = 0; |
| 10505 | cur_next = msg->chn->buf->p + hdr_idx_first_pos(idx); |
| 10506 | while (1) { |
| 10507 | cur_idx = idx->v[old_idx].next; |
| 10508 | if (!cur_idx) |
| 10509 | break; |
| 10510 | old_idx = cur_idx; |
| 10511 | |
| 10512 | cur_hdr = &idx->v[cur_idx]; |
| 10513 | cur_ptr = cur_next; |
| 10514 | cur_next = cur_ptr + cur_hdr->len + cur_hdr->cr + 1; |
| 10515 | |
| 10516 | /* Now we have one full header at cur_ptr of len cur_hdr->len, |
| 10517 | * and the next header starts at cur_next. We'll check |
| 10518 | * this header in the list as well as against the default |
| 10519 | * rule. |
| 10520 | */ |
| 10521 | |
| 10522 | /* look for ': *'. */ |
| 10523 | hn = cur_ptr; |
| 10524 | for (p = cur_ptr; p < cur_ptr + cur_hdr->len && *p != ':'; p++); |
| 10525 | if (p >= cur_ptr+cur_hdr->len) |
| 10526 | continue; |
| 10527 | hnl = p - hn; |
| 10528 | p++; |
| 10529 | while (p < cur_ptr + cur_hdr->len && (*p == ' ' || *p == '\t')) |
| 10530 | p++; |
| 10531 | if (p >= cur_ptr + cur_hdr->len) |
| 10532 | continue; |
| 10533 | hv = p; |
| 10534 | hvl = cur_ptr + cur_hdr->len-p; |
| 10535 | |
| 10536 | /* encode the header name. */ |
| 10537 | ret = encode_varint(hnl, &buf, end); |
| 10538 | if (ret == -1) |
| 10539 | return 0; |
| 10540 | if (buf + hnl > end) |
| 10541 | return 0; |
| 10542 | memcpy(buf, hn, hnl); |
| 10543 | buf += hnl; |
| 10544 | |
| 10545 | /* encode and copy the value. */ |
| 10546 | ret = encode_varint(hvl, &buf, end); |
| 10547 | if (ret == -1) |
| 10548 | return 0; |
| 10549 | if (buf + hvl > end) |
| 10550 | return 0; |
| 10551 | memcpy(buf, hv, hvl); |
| 10552 | buf += hvl; |
| 10553 | } |
| 10554 | |
| 10555 | /* encode the end of the header list with empty |
| 10556 | * header name and header value. |
| 10557 | */ |
| 10558 | ret = encode_varint(0, &buf, end); |
| 10559 | if (ret == -1) |
| 10560 | return 0; |
| 10561 | ret = encode_varint(0, &buf, end); |
| 10562 | if (ret == -1) |
| 10563 | return 0; |
| 10564 | |
| 10565 | /* Initialise sample data which will be filled. */ |
| 10566 | smp->data.type = SMP_T_BIN; |
| 10567 | smp->data.u.str.str = temp->str; |
| 10568 | smp->data.u.str.len = buf - temp->str; |
| 10569 | smp->data.u.str.size = temp->size; |
| 10570 | |
| 10571 | return 1; |
| 10572 | } |
| 10573 | |
Willy Tarreau | a5910cc | 2015-05-02 00:46:08 +0200 | [diff] [blame] | 10574 | /* returns the longest available part of the body. This requires that the body |
| 10575 | * has been waited for using http-buffer-request. |
| 10576 | */ |
| 10577 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10578 | 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] | 10579 | { |
Willy Tarreau | a5910cc | 2015-05-02 00:46:08 +0200 | [diff] [blame] | 10580 | struct http_msg *msg; |
| 10581 | unsigned long len; |
| 10582 | unsigned long block1; |
| 10583 | char *body; |
| 10584 | struct chunk *temp; |
| 10585 | |
| 10586 | CHECK_HTTP_MESSAGE_FIRST(); |
| 10587 | |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10588 | if ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) |
Willy Tarreau | be508f1 | 2016-03-10 11:47:01 +0100 | [diff] [blame] | 10589 | msg = &smp->strm->txn->req; |
Willy Tarreau | a5910cc | 2015-05-02 00:46:08 +0200 | [diff] [blame] | 10590 | else |
Willy Tarreau | be508f1 | 2016-03-10 11:47:01 +0100 | [diff] [blame] | 10591 | msg = &smp->strm->txn->rsp; |
Willy Tarreau | a5910cc | 2015-05-02 00:46:08 +0200 | [diff] [blame] | 10592 | |
| 10593 | len = http_body_bytes(msg); |
| 10594 | body = b_ptr(msg->chn->buf, -http_data_rewind(msg)); |
| 10595 | |
| 10596 | block1 = len; |
| 10597 | if (block1 > msg->chn->buf->data + msg->chn->buf->size - body) |
| 10598 | block1 = msg->chn->buf->data + msg->chn->buf->size - body; |
| 10599 | |
| 10600 | if (block1 == len) { |
| 10601 | /* buffer is not wrapped (or empty) */ |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 10602 | smp->data.type = SMP_T_BIN; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 10603 | smp->data.u.str.str = body; |
| 10604 | smp->data.u.str.len = len; |
Willy Tarreau | a5910cc | 2015-05-02 00:46:08 +0200 | [diff] [blame] | 10605 | smp->flags = SMP_F_VOL_TEST | SMP_F_CONST; |
| 10606 | } |
| 10607 | else { |
| 10608 | /* buffer is wrapped, we need to defragment it */ |
| 10609 | temp = get_trash_chunk(); |
| 10610 | memcpy(temp->str, body, block1); |
| 10611 | memcpy(temp->str + block1, msg->chn->buf->data, len - block1); |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 10612 | smp->data.type = SMP_T_BIN; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 10613 | smp->data.u.str.str = temp->str; |
| 10614 | smp->data.u.str.len = len; |
Willy Tarreau | a5910cc | 2015-05-02 00:46:08 +0200 | [diff] [blame] | 10615 | smp->flags = SMP_F_VOL_TEST; |
| 10616 | } |
| 10617 | return 1; |
| 10618 | } |
| 10619 | |
| 10620 | |
| 10621 | /* returns the available length of the body. This requires that the body |
| 10622 | * has been waited for using http-buffer-request. |
| 10623 | */ |
| 10624 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10625 | 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] | 10626 | { |
Willy Tarreau | a5910cc | 2015-05-02 00:46:08 +0200 | [diff] [blame] | 10627 | struct http_msg *msg; |
| 10628 | |
| 10629 | CHECK_HTTP_MESSAGE_FIRST(); |
| 10630 | |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10631 | if ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) |
Willy Tarreau | be508f1 | 2016-03-10 11:47:01 +0100 | [diff] [blame] | 10632 | msg = &smp->strm->txn->req; |
Willy Tarreau | a5910cc | 2015-05-02 00:46:08 +0200 | [diff] [blame] | 10633 | else |
Willy Tarreau | be508f1 | 2016-03-10 11:47:01 +0100 | [diff] [blame] | 10634 | msg = &smp->strm->txn->rsp; |
Willy Tarreau | a5910cc | 2015-05-02 00:46:08 +0200 | [diff] [blame] | 10635 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 10636 | smp->data.type = SMP_T_SINT; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 10637 | smp->data.u.sint = http_body_bytes(msg); |
Willy Tarreau | a5910cc | 2015-05-02 00:46:08 +0200 | [diff] [blame] | 10638 | |
| 10639 | smp->flags = SMP_F_VOL_TEST; |
| 10640 | return 1; |
| 10641 | } |
| 10642 | |
| 10643 | |
| 10644 | /* returns the advertised length of the body, or the advertised size of the |
| 10645 | * chunks available in the buffer. This requires that the body has been waited |
| 10646 | * for using http-buffer-request. |
| 10647 | */ |
| 10648 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10649 | 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] | 10650 | { |
Willy Tarreau | a5910cc | 2015-05-02 00:46:08 +0200 | [diff] [blame] | 10651 | struct http_msg *msg; |
| 10652 | |
| 10653 | CHECK_HTTP_MESSAGE_FIRST(); |
| 10654 | |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10655 | if ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) |
Willy Tarreau | be508f1 | 2016-03-10 11:47:01 +0100 | [diff] [blame] | 10656 | msg = &smp->strm->txn->req; |
Willy Tarreau | a5910cc | 2015-05-02 00:46:08 +0200 | [diff] [blame] | 10657 | else |
Willy Tarreau | be508f1 | 2016-03-10 11:47:01 +0100 | [diff] [blame] | 10658 | msg = &smp->strm->txn->rsp; |
Willy Tarreau | a5910cc | 2015-05-02 00:46:08 +0200 | [diff] [blame] | 10659 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 10660 | smp->data.type = SMP_T_SINT; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 10661 | smp->data.u.sint = msg->body_len; |
Willy Tarreau | a5910cc | 2015-05-02 00:46:08 +0200 | [diff] [blame] | 10662 | |
| 10663 | smp->flags = SMP_F_VOL_TEST; |
| 10664 | return 1; |
| 10665 | } |
| 10666 | |
| 10667 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 10668 | /* 4. Check on URL/URI. A pointer to the URI is stored. */ |
Willy Tarreau | d41f8d8 | 2007-06-10 10:06:18 +0200 | [diff] [blame] | 10669 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10670 | 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] | 10671 | { |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 10672 | struct http_txn *txn; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 10673 | |
Willy Tarreau | c0239e0 | 2012-04-16 14:42:55 +0200 | [diff] [blame] | 10674 | CHECK_HTTP_MESSAGE_FIRST(); |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 10675 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 10676 | txn = smp->strm->txn; |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 10677 | smp->data.type = SMP_T_STR; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 10678 | smp->data.u.str.len = txn->req.sl.rq.u_l; |
| 10679 | 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] | 10680 | smp->flags = SMP_F_VOL_1ST | SMP_F_CONST; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 10681 | return 1; |
| 10682 | } |
| 10683 | |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 10684 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10685 | 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] | 10686 | { |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 10687 | struct http_txn *txn; |
Willy Tarreau | 4c804ec | 2013-09-30 14:37:14 +0200 | [diff] [blame] | 10688 | struct sockaddr_storage addr; |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 10689 | |
Willy Tarreau | c0239e0 | 2012-04-16 14:42:55 +0200 | [diff] [blame] | 10690 | CHECK_HTTP_MESSAGE_FIRST(); |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 10691 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 10692 | txn = smp->strm->txn; |
Thierry FOURNIER | 9f95e40 | 2014-03-21 14:51:46 +0100 | [diff] [blame] | 10693 | 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] | 10694 | if (((struct sockaddr_in *)&addr)->sin_family != AF_INET) |
Willy Tarreau | f4362b3 | 2011-12-16 17:49:52 +0100 | [diff] [blame] | 10695 | return 0; |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 10696 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 10697 | smp->data.type = SMP_T_IPV4; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 10698 | smp->data.u.ipv4 = ((struct sockaddr_in *)&addr)->sin_addr; |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 10699 | smp->flags = 0; |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 10700 | return 1; |
| 10701 | } |
| 10702 | |
| 10703 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10704 | 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] | 10705 | { |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 10706 | struct http_txn *txn; |
Willy Tarreau | 4c804ec | 2013-09-30 14:37:14 +0200 | [diff] [blame] | 10707 | struct sockaddr_storage addr; |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 10708 | |
Willy Tarreau | c0239e0 | 2012-04-16 14:42:55 +0200 | [diff] [blame] | 10709 | CHECK_HTTP_MESSAGE_FIRST(); |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 10710 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 10711 | txn = smp->strm->txn; |
Thierry FOURNIER | 9f95e40 | 2014-03-21 14:51:46 +0100 | [diff] [blame] | 10712 | 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] | 10713 | if (((struct sockaddr_in *)&addr)->sin_family != AF_INET) |
| 10714 | return 0; |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 10715 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 10716 | smp->data.type = SMP_T_SINT; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 10717 | smp->data.u.sint = ntohs(((struct sockaddr_in *)&addr)->sin_port); |
Willy Tarreau | 21e5b0e | 2012-04-23 19:25:44 +0200 | [diff] [blame] | 10718 | smp->flags = 0; |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 10719 | return 1; |
| 10720 | } |
| 10721 | |
Willy Tarreau | 185b5c4 | 2012-04-26 15:11:51 +0200 | [diff] [blame] | 10722 | /* Fetch an HTTP header. A pointer to the beginning of the value is returned. |
| 10723 | * Accepts an optional argument of type string containing the header field name, |
| 10724 | * and an optional argument of type signed or unsigned integer to request an |
| 10725 | * 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] | 10726 | * headers are considered from the first one. It does not stop on commas and |
| 10727 | * returns full lines instead (useful for User-Agent or Date for example). |
| 10728 | */ |
| 10729 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10730 | 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] | 10731 | { |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 10732 | struct hdr_idx *idx; |
Willy Tarreau | 04ff9f1 | 2013-06-10 18:39:42 +0200 | [diff] [blame] | 10733 | struct hdr_ctx *ctx = smp->ctx.a[0]; |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 10734 | const struct http_msg *msg; |
Willy Tarreau | 04ff9f1 | 2013-06-10 18:39:42 +0200 | [diff] [blame] | 10735 | int occ = 0; |
| 10736 | const char *name_str = NULL; |
| 10737 | int name_len = 0; |
| 10738 | |
| 10739 | if (!ctx) { |
| 10740 | /* first call */ |
| 10741 | ctx = &static_hdr_ctx; |
| 10742 | ctx->idx = 0; |
| 10743 | smp->ctx.a[0] = ctx; |
| 10744 | } |
| 10745 | |
| 10746 | if (args) { |
| 10747 | if (args[0].type != ARGT_STR) |
| 10748 | return 0; |
| 10749 | name_str = args[0].data.str.str; |
| 10750 | name_len = args[0].data.str.len; |
| 10751 | |
Thierry FOURNIER | bf65cd4 | 2015-07-20 17:45:02 +0200 | [diff] [blame] | 10752 | if (args[1].type == ARGT_SINT) |
| 10753 | occ = args[1].data.sint; |
Willy Tarreau | 04ff9f1 | 2013-06-10 18:39:42 +0200 | [diff] [blame] | 10754 | } |
| 10755 | |
| 10756 | CHECK_HTTP_MESSAGE_FIRST(); |
| 10757 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 10758 | idx = &smp->strm->txn->hdr_idx; |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10759 | 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] | 10760 | |
Willy Tarreau | 04ff9f1 | 2013-06-10 18:39:42 +0200 | [diff] [blame] | 10761 | if (ctx && !(smp->flags & SMP_F_NOT_LAST)) |
| 10762 | /* search for header from the beginning */ |
| 10763 | ctx->idx = 0; |
| 10764 | |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10765 | if (!occ && !(smp->opt & SMP_OPT_ITERATE)) |
Willy Tarreau | 04ff9f1 | 2013-06-10 18:39:42 +0200 | [diff] [blame] | 10766 | /* no explicit occurrence and single fetch => last header by default */ |
| 10767 | occ = -1; |
| 10768 | |
| 10769 | if (!occ) |
| 10770 | /* prepare to report multiple occurrences for ACL fetches */ |
| 10771 | smp->flags |= SMP_F_NOT_LAST; |
| 10772 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 10773 | smp->data.type = SMP_T_STR; |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 10774 | smp->flags |= SMP_F_VOL_HDR | SMP_F_CONST; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 10775 | 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] | 10776 | return 1; |
| 10777 | |
| 10778 | smp->flags &= ~SMP_F_NOT_LAST; |
| 10779 | return 0; |
| 10780 | } |
| 10781 | |
| 10782 | /* 6. Check on HTTP header count. The number of occurrences is returned. |
| 10783 | * Accepts exactly 1 argument of type string. It does not stop on commas and |
| 10784 | * returns full lines instead (useful for User-Agent or Date for example). |
| 10785 | */ |
| 10786 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10787 | 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] | 10788 | { |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 10789 | struct hdr_idx *idx; |
Willy Tarreau | 04ff9f1 | 2013-06-10 18:39:42 +0200 | [diff] [blame] | 10790 | struct hdr_ctx ctx; |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 10791 | const struct http_msg *msg; |
Willy Tarreau | 04ff9f1 | 2013-06-10 18:39:42 +0200 | [diff] [blame] | 10792 | int cnt; |
Willy Tarreau | 601a4d1 | 2015-04-01 19:16:09 +0200 | [diff] [blame] | 10793 | const char *name = NULL; |
| 10794 | int len = 0; |
Willy Tarreau | 04ff9f1 | 2013-06-10 18:39:42 +0200 | [diff] [blame] | 10795 | |
Willy Tarreau | 601a4d1 | 2015-04-01 19:16:09 +0200 | [diff] [blame] | 10796 | if (args && args->type == ARGT_STR) { |
| 10797 | name = args->data.str.str; |
| 10798 | len = args->data.str.len; |
| 10799 | } |
Willy Tarreau | 04ff9f1 | 2013-06-10 18:39:42 +0200 | [diff] [blame] | 10800 | |
| 10801 | CHECK_HTTP_MESSAGE_FIRST(); |
| 10802 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 10803 | idx = &smp->strm->txn->hdr_idx; |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10804 | 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] | 10805 | |
Willy Tarreau | 04ff9f1 | 2013-06-10 18:39:42 +0200 | [diff] [blame] | 10806 | ctx.idx = 0; |
| 10807 | cnt = 0; |
Willy Tarreau | 601a4d1 | 2015-04-01 19:16:09 +0200 | [diff] [blame] | 10808 | 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] | 10809 | cnt++; |
| 10810 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 10811 | smp->data.type = SMP_T_SINT; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 10812 | smp->data.u.sint = cnt; |
Willy Tarreau | 04ff9f1 | 2013-06-10 18:39:42 +0200 | [diff] [blame] | 10813 | smp->flags = SMP_F_VOL_HDR; |
| 10814 | return 1; |
| 10815 | } |
| 10816 | |
Willy Tarreau | eb27ec7 | 2015-02-20 13:55:29 +0100 | [diff] [blame] | 10817 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10818 | 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] | 10819 | { |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 10820 | struct hdr_idx *idx; |
Willy Tarreau | eb27ec7 | 2015-02-20 13:55:29 +0100 | [diff] [blame] | 10821 | struct hdr_ctx ctx; |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 10822 | const struct http_msg *msg; |
Willy Tarreau | eb27ec7 | 2015-02-20 13:55:29 +0100 | [diff] [blame] | 10823 | struct chunk *temp; |
| 10824 | char del = ','; |
| 10825 | |
| 10826 | if (args && args->type == ARGT_STR) |
| 10827 | del = *args[0].data.str.str; |
| 10828 | |
| 10829 | CHECK_HTTP_MESSAGE_FIRST(); |
| 10830 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 10831 | idx = &smp->strm->txn->hdr_idx; |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10832 | 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] | 10833 | |
Willy Tarreau | eb27ec7 | 2015-02-20 13:55:29 +0100 | [diff] [blame] | 10834 | temp = get_trash_chunk(); |
| 10835 | |
| 10836 | ctx.idx = 0; |
| 10837 | while (http_find_next_header(msg->chn->buf->p, idx, &ctx)) { |
| 10838 | if (temp->len) |
| 10839 | temp->str[temp->len++] = del; |
| 10840 | memcpy(temp->str + temp->len, ctx.line, ctx.del); |
| 10841 | temp->len += ctx.del; |
| 10842 | } |
| 10843 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 10844 | smp->data.type = SMP_T_STR; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 10845 | smp->data.u.str.str = temp->str; |
| 10846 | smp->data.u.str.len = temp->len; |
Willy Tarreau | eb27ec7 | 2015-02-20 13:55:29 +0100 | [diff] [blame] | 10847 | smp->flags = SMP_F_VOL_HDR; |
| 10848 | return 1; |
| 10849 | } |
| 10850 | |
Willy Tarreau | 04ff9f1 | 2013-06-10 18:39:42 +0200 | [diff] [blame] | 10851 | /* Fetch an HTTP header. A pointer to the beginning of the value is returned. |
| 10852 | * Accepts an optional argument of type string containing the header field name, |
| 10853 | * and an optional argument of type signed or unsigned integer to request an |
| 10854 | * 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] | 10855 | * headers are considered from the first one. |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 10856 | */ |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 10857 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10858 | 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] | 10859 | { |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 10860 | struct hdr_idx *idx; |
Willy Tarreau | a890d07 | 2013-04-02 12:01:06 +0200 | [diff] [blame] | 10861 | struct hdr_ctx *ctx = smp->ctx.a[0]; |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 10862 | const struct http_msg *msg; |
Willy Tarreau | 185b5c4 | 2012-04-26 15:11:51 +0200 | [diff] [blame] | 10863 | int occ = 0; |
| 10864 | const char *name_str = NULL; |
| 10865 | int name_len = 0; |
Willy Tarreau | e333ec9 | 2012-04-16 16:26:40 +0200 | [diff] [blame] | 10866 | |
Willy Tarreau | a890d07 | 2013-04-02 12:01:06 +0200 | [diff] [blame] | 10867 | if (!ctx) { |
| 10868 | /* first call */ |
| 10869 | ctx = &static_hdr_ctx; |
| 10870 | ctx->idx = 0; |
Willy Tarreau | ffb6f08 | 2013-04-02 23:16:53 +0200 | [diff] [blame] | 10871 | smp->ctx.a[0] = ctx; |
Willy Tarreau | a890d07 | 2013-04-02 12:01:06 +0200 | [diff] [blame] | 10872 | } |
| 10873 | |
Willy Tarreau | 185b5c4 | 2012-04-26 15:11:51 +0200 | [diff] [blame] | 10874 | if (args) { |
| 10875 | if (args[0].type != ARGT_STR) |
| 10876 | return 0; |
| 10877 | name_str = args[0].data.str.str; |
| 10878 | name_len = args[0].data.str.len; |
| 10879 | |
Thierry FOURNIER | bf65cd4 | 2015-07-20 17:45:02 +0200 | [diff] [blame] | 10880 | if (args[1].type == ARGT_SINT) |
| 10881 | occ = args[1].data.sint; |
Willy Tarreau | 185b5c4 | 2012-04-26 15:11:51 +0200 | [diff] [blame] | 10882 | } |
Willy Tarreau | 34db108 | 2012-04-19 17:16:54 +0200 | [diff] [blame] | 10883 | |
Willy Tarreau | e333ec9 | 2012-04-16 16:26:40 +0200 | [diff] [blame] | 10884 | CHECK_HTTP_MESSAGE_FIRST(); |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 10885 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 10886 | idx = &smp->strm->txn->hdr_idx; |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10887 | 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] | 10888 | |
Willy Tarreau | 185b5c4 | 2012-04-26 15:11:51 +0200 | [diff] [blame] | 10889 | if (ctx && !(smp->flags & SMP_F_NOT_LAST)) |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 10890 | /* search for header from the beginning */ |
| 10891 | ctx->idx = 0; |
| 10892 | |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10893 | if (!occ && !(smp->opt & SMP_OPT_ITERATE)) |
Willy Tarreau | 185b5c4 | 2012-04-26 15:11:51 +0200 | [diff] [blame] | 10894 | /* no explicit occurrence and single fetch => last header by default */ |
| 10895 | occ = -1; |
| 10896 | |
| 10897 | if (!occ) |
| 10898 | /* prepare to report multiple occurrences for ACL fetches */ |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 10899 | smp->flags |= SMP_F_NOT_LAST; |
Willy Tarreau | 664092c | 2011-12-16 19:11:42 +0100 | [diff] [blame] | 10900 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 10901 | smp->data.type = SMP_T_STR; |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 10902 | smp->flags |= SMP_F_VOL_HDR | SMP_F_CONST; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 10903 | 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] | 10904 | return 1; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 10905 | |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 10906 | smp->flags &= ~SMP_F_NOT_LAST; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 10907 | return 0; |
| 10908 | } |
| 10909 | |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 10910 | /* 6. Check on HTTP header count. The number of occurrences is returned. |
Willy Tarreau | 34db108 | 2012-04-19 17:16:54 +0200 | [diff] [blame] | 10911 | * Accepts exactly 1 argument of type string. |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 10912 | */ |
| 10913 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10914 | 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] | 10915 | { |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 10916 | struct hdr_idx *idx; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 10917 | struct hdr_ctx ctx; |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 10918 | const struct http_msg *msg; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 10919 | int cnt; |
Willy Tarreau | 601a4d1 | 2015-04-01 19:16:09 +0200 | [diff] [blame] | 10920 | const char *name = NULL; |
| 10921 | int len = 0; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 10922 | |
Willy Tarreau | 601a4d1 | 2015-04-01 19:16:09 +0200 | [diff] [blame] | 10923 | if (args && args->type == ARGT_STR) { |
| 10924 | name = args->data.str.str; |
| 10925 | len = args->data.str.len; |
| 10926 | } |
Willy Tarreau | 34db108 | 2012-04-19 17:16:54 +0200 | [diff] [blame] | 10927 | |
Willy Tarreau | e333ec9 | 2012-04-16 16:26:40 +0200 | [diff] [blame] | 10928 | CHECK_HTTP_MESSAGE_FIRST(); |
| 10929 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 10930 | idx = &smp->strm->txn->hdr_idx; |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10931 | 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] | 10932 | |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 10933 | ctx.idx = 0; |
| 10934 | cnt = 0; |
Willy Tarreau | 601a4d1 | 2015-04-01 19:16:09 +0200 | [diff] [blame] | 10935 | while (http_find_header2(name, len, msg->chn->buf->p, idx, &ctx)) |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 10936 | cnt++; |
| 10937 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 10938 | smp->data.type = SMP_T_SINT; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 10939 | smp->data.u.sint = cnt; |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 10940 | smp->flags = SMP_F_VOL_HDR; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 10941 | return 1; |
| 10942 | } |
| 10943 | |
Willy Tarreau | 185b5c4 | 2012-04-26 15:11:51 +0200 | [diff] [blame] | 10944 | /* Fetch an HTTP header's integer value. The integer value is returned. It |
| 10945 | * takes a mandatory argument of type string and an optional one of type int |
| 10946 | * to designate a specific occurrence. It returns an unsigned integer, which |
| 10947 | * may or may not be appropriate for everything. |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 10948 | */ |
| 10949 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10950 | 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] | 10951 | { |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10952 | int ret = smp_fetch_hdr(args, smp, kw, private); |
Willy Tarreau | e333ec9 | 2012-04-16 16:26:40 +0200 | [diff] [blame] | 10953 | |
Willy Tarreau | f853c46 | 2012-04-23 18:53:56 +0200 | [diff] [blame] | 10954 | if (ret > 0) { |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 10955 | smp->data.type = SMP_T_SINT; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 10956 | 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] | 10957 | } |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 10958 | |
Willy Tarreau | d53e242 | 2012-04-16 17:21:11 +0200 | [diff] [blame] | 10959 | return ret; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 10960 | } |
| 10961 | |
Cyril Bonté | 69fa992 | 2012-10-25 00:01:06 +0200 | [diff] [blame] | 10962 | /* Fetch an HTTP header's IP value. takes a mandatory argument of type string |
| 10963 | * and an optional one of type int to designate a specific occurrence. |
| 10964 | * It returns an IPv4 or IPv6 address. |
Willy Tarreau | 106f979 | 2009-09-19 07:54:16 +0200 | [diff] [blame] | 10965 | */ |
| 10966 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10967 | 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] | 10968 | { |
Willy Tarreau | d53e242 | 2012-04-16 17:21:11 +0200 | [diff] [blame] | 10969 | int ret; |
Willy Tarreau | e333ec9 | 2012-04-16 16:26:40 +0200 | [diff] [blame] | 10970 | |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10971 | while ((ret = smp_fetch_hdr(args, smp, kw, private)) > 0) { |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 10972 | if (url2ipv4((char *)smp->data.u.str.str, &smp->data.u.ipv4)) { |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 10973 | smp->data.type = SMP_T_IPV4; |
Willy Tarreau | d53e242 | 2012-04-16 17:21:11 +0200 | [diff] [blame] | 10974 | break; |
Cyril Bonté | 69fa992 | 2012-10-25 00:01:06 +0200 | [diff] [blame] | 10975 | } else { |
Willy Tarreau | 47ca545 | 2012-12-23 20:22:19 +0100 | [diff] [blame] | 10976 | struct chunk *temp = get_trash_chunk(); |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 10977 | if (smp->data.u.str.len < temp->size - 1) { |
| 10978 | memcpy(temp->str, smp->data.u.str.str, smp->data.u.str.len); |
| 10979 | temp->str[smp->data.u.str.len] = '\0'; |
| 10980 | if (inet_pton(AF_INET6, temp->str, &smp->data.u.ipv6)) { |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 10981 | smp->data.type = SMP_T_IPV6; |
Cyril Bonté | 69fa992 | 2012-10-25 00:01:06 +0200 | [diff] [blame] | 10982 | break; |
| 10983 | } |
| 10984 | } |
| 10985 | } |
| 10986 | |
Willy Tarreau | d53e242 | 2012-04-16 17:21:11 +0200 | [diff] [blame] | 10987 | /* if the header doesn't match an IP address, fetch next one */ |
Willy Tarreau | 185b5c4 | 2012-04-26 15:11:51 +0200 | [diff] [blame] | 10988 | if (!(smp->flags & SMP_F_NOT_LAST)) |
| 10989 | return 0; |
Willy Tarreau | 106f979 | 2009-09-19 07:54:16 +0200 | [diff] [blame] | 10990 | } |
Willy Tarreau | d53e242 | 2012-04-16 17:21:11 +0200 | [diff] [blame] | 10991 | return ret; |
Willy Tarreau | 106f979 | 2009-09-19 07:54:16 +0200 | [diff] [blame] | 10992 | } |
| 10993 | |
Willy Tarreau | 737b0c1 | 2007-06-10 21:28:46 +0200 | [diff] [blame] | 10994 | /* 8. Check on URI PATH. A pointer to the PATH is stored. The path starts at |
| 10995 | * the first '/' after the possible hostname, and ends before the possible '?'. |
| 10996 | */ |
| 10997 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10998 | 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] | 10999 | { |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 11000 | struct http_txn *txn; |
Willy Tarreau | 737b0c1 | 2007-06-10 21:28:46 +0200 | [diff] [blame] | 11001 | char *ptr, *end; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 11002 | |
Willy Tarreau | c0239e0 | 2012-04-16 14:42:55 +0200 | [diff] [blame] | 11003 | CHECK_HTTP_MESSAGE_FIRST(); |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 11004 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 11005 | txn = smp->strm->txn; |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 11006 | 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] | 11007 | ptr = http_get_path(txn); |
| 11008 | if (!ptr) |
Willy Tarreau | 737b0c1 | 2007-06-10 21:28:46 +0200 | [diff] [blame] | 11009 | return 0; |
| 11010 | |
| 11011 | /* OK, we got the '/' ! */ |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 11012 | smp->data.type = SMP_T_STR; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 11013 | smp->data.u.str.str = ptr; |
Willy Tarreau | 737b0c1 | 2007-06-10 21:28:46 +0200 | [diff] [blame] | 11014 | |
| 11015 | while (ptr < end && *ptr != '?') |
| 11016 | ptr++; |
| 11017 | |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 11018 | smp->data.u.str.len = ptr - smp->data.u.str.str; |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 11019 | smp->flags = SMP_F_VOL_1ST | SMP_F_CONST; |
Willy Tarreau | 737b0c1 | 2007-06-10 21:28:46 +0200 | [diff] [blame] | 11020 | return 1; |
| 11021 | } |
| 11022 | |
Willy Tarreau | a7ad50c | 2012-04-29 15:39:40 +0200 | [diff] [blame] | 11023 | /* This produces a concatenation of the first occurrence of the Host header |
| 11024 | * followed by the path component if it begins with a slash ('/'). This means |
| 11025 | * that '*' will not be added, resulting in exactly the first Host entry. |
| 11026 | * If no Host header is found, then the path is returned as-is. The returned |
| 11027 | * 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] | 11028 | * The returned sample is of type string. |
Willy Tarreau | a7ad50c | 2012-04-29 15:39:40 +0200 | [diff] [blame] | 11029 | */ |
| 11030 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 11031 | 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] | 11032 | { |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 11033 | struct http_txn *txn; |
Willy Tarreau | a7ad50c | 2012-04-29 15:39:40 +0200 | [diff] [blame] | 11034 | char *ptr, *end, *beg; |
| 11035 | struct hdr_ctx ctx; |
Willy Tarreau | 3caf2af | 2014-06-24 17:27:02 +0200 | [diff] [blame] | 11036 | struct chunk *temp; |
Willy Tarreau | a7ad50c | 2012-04-29 15:39:40 +0200 | [diff] [blame] | 11037 | |
| 11038 | CHECK_HTTP_MESSAGE_FIRST(); |
| 11039 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 11040 | txn = smp->strm->txn; |
Willy Tarreau | a7ad50c | 2012-04-29 15:39:40 +0200 | [diff] [blame] | 11041 | ctx.idx = 0; |
Willy Tarreau | 877e78d | 2013-04-07 18:48:08 +0200 | [diff] [blame] | 11042 | 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] | 11043 | return smp_fetch_path(args, smp, kw, private); |
Willy Tarreau | a7ad50c | 2012-04-29 15:39:40 +0200 | [diff] [blame] | 11044 | |
| 11045 | /* 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] | 11046 | temp = get_trash_chunk(); |
| 11047 | memcpy(temp->str, ctx.line + ctx.val, ctx.vlen); |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 11048 | smp->data.type = SMP_T_STR; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 11049 | smp->data.u.str.str = temp->str; |
| 11050 | smp->data.u.str.len = ctx.vlen; |
Willy Tarreau | a7ad50c | 2012-04-29 15:39:40 +0200 | [diff] [blame] | 11051 | |
| 11052 | /* now retrieve the path */ |
Willy Tarreau | 877e78d | 2013-04-07 18:48:08 +0200 | [diff] [blame] | 11053 | 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] | 11054 | beg = http_get_path(txn); |
| 11055 | if (!beg) |
| 11056 | beg = end; |
| 11057 | |
| 11058 | for (ptr = beg; ptr < end && *ptr != '?'; ptr++); |
| 11059 | |
| 11060 | if (beg < ptr && *beg == '/') { |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 11061 | memcpy(smp->data.u.str.str + smp->data.u.str.len, beg, ptr - beg); |
| 11062 | smp->data.u.str.len += ptr - beg; |
Willy Tarreau | a7ad50c | 2012-04-29 15:39:40 +0200 | [diff] [blame] | 11063 | } |
| 11064 | |
| 11065 | smp->flags = SMP_F_VOL_1ST; |
| 11066 | return 1; |
| 11067 | } |
| 11068 | |
Willy Tarreau | ab1f7b7 | 2012-12-09 13:38:54 +0100 | [diff] [blame] | 11069 | /* This produces a 32-bit hash of the concatenation of the first occurrence of |
| 11070 | * the Host header followed by the path component if it begins with a slash ('/'). |
| 11071 | * This means that '*' will not be added, resulting in exactly the first Host |
| 11072 | * 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] | 11073 | * is hashed using the path hash followed by a full avalanche hash and provides a |
| 11074 | * 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] | 11075 | * high-traffic sites without having to store whole paths. |
| 11076 | */ |
Thierry FOURNIER | 055b9d5 | 2014-07-15 16:11:07 +0200 | [diff] [blame] | 11077 | int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 11078 | 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] | 11079 | { |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 11080 | struct http_txn *txn; |
Willy Tarreau | ab1f7b7 | 2012-12-09 13:38:54 +0100 | [diff] [blame] | 11081 | struct hdr_ctx ctx; |
| 11082 | unsigned int hash = 0; |
| 11083 | char *ptr, *beg, *end; |
| 11084 | int len; |
| 11085 | |
| 11086 | CHECK_HTTP_MESSAGE_FIRST(); |
| 11087 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 11088 | txn = smp->strm->txn; |
Willy Tarreau | ab1f7b7 | 2012-12-09 13:38:54 +0100 | [diff] [blame] | 11089 | ctx.idx = 0; |
Willy Tarreau | 877e78d | 2013-04-07 18:48:08 +0200 | [diff] [blame] | 11090 | 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] | 11091 | /* OK we have the header value in ctx.line+ctx.val for ctx.vlen bytes */ |
| 11092 | ptr = ctx.line + ctx.val; |
| 11093 | len = ctx.vlen; |
| 11094 | while (len--) |
| 11095 | hash = *(ptr++) + (hash << 6) + (hash << 16) - hash; |
| 11096 | } |
| 11097 | |
| 11098 | /* now retrieve the path */ |
Willy Tarreau | 877e78d | 2013-04-07 18:48:08 +0200 | [diff] [blame] | 11099 | 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] | 11100 | beg = http_get_path(txn); |
| 11101 | if (!beg) |
| 11102 | beg = end; |
| 11103 | |
| 11104 | for (ptr = beg; ptr < end && *ptr != '?'; ptr++); |
| 11105 | |
| 11106 | if (beg < ptr && *beg == '/') { |
| 11107 | while (beg < ptr) |
| 11108 | hash = *(beg++) + (hash << 6) + (hash << 16) - hash; |
| 11109 | } |
| 11110 | hash = full_hash(hash); |
| 11111 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 11112 | smp->data.type = SMP_T_SINT; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 11113 | smp->data.u.sint = hash; |
Willy Tarreau | ab1f7b7 | 2012-12-09 13:38:54 +0100 | [diff] [blame] | 11114 | smp->flags = SMP_F_VOL_1ST; |
| 11115 | return 1; |
| 11116 | } |
| 11117 | |
Willy Tarreau | 4a55060 | 2012-12-09 14:53:32 +0100 | [diff] [blame] | 11118 | /* 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] | 11119 | * path as returned by smp_fetch_base32(). The idea is to have per-source and |
| 11120 | * per-path counters. The result is a binary block from 8 to 20 bytes depending |
| 11121 | * 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] | 11122 | * that in environments where IPv6 is insignificant, truncating the output to |
| 11123 | * 8 bytes would still work. |
| 11124 | */ |
| 11125 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 11126 | 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] | 11127 | { |
Willy Tarreau | 47ca545 | 2012-12-23 20:22:19 +0100 | [diff] [blame] | 11128 | struct chunk *temp; |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 11129 | struct connection *cli_conn = objt_conn(smp->sess->origin); |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 11130 | |
| 11131 | if (!cli_conn) |
| 11132 | return 0; |
Willy Tarreau | 4a55060 | 2012-12-09 14:53:32 +0100 | [diff] [blame] | 11133 | |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 11134 | if (!smp_fetch_base32(args, smp, kw, private)) |
Willy Tarreau | 4a55060 | 2012-12-09 14:53:32 +0100 | [diff] [blame] | 11135 | return 0; |
| 11136 | |
Willy Tarreau | 47ca545 | 2012-12-23 20:22:19 +0100 | [diff] [blame] | 11137 | temp = get_trash_chunk(); |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 11138 | *(unsigned int *)temp->str = htonl(smp->data.u.sint); |
Willy Tarreau | 5ad6e1d | 2014-07-15 21:34:06 +0200 | [diff] [blame] | 11139 | temp->len += sizeof(unsigned int); |
Willy Tarreau | 4a55060 | 2012-12-09 14:53:32 +0100 | [diff] [blame] | 11140 | |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 11141 | switch (cli_conn->addr.from.ss_family) { |
Willy Tarreau | 4a55060 | 2012-12-09 14:53:32 +0100 | [diff] [blame] | 11142 | case AF_INET: |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 11143 | 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] | 11144 | temp->len += 4; |
| 11145 | break; |
| 11146 | case AF_INET6: |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 11147 | 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] | 11148 | temp->len += 16; |
| 11149 | break; |
| 11150 | default: |
| 11151 | return 0; |
| 11152 | } |
| 11153 | |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 11154 | smp->data.u.str = *temp; |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 11155 | smp->data.type = SMP_T_BIN; |
Willy Tarreau | 4a55060 | 2012-12-09 14:53:32 +0100 | [diff] [blame] | 11156 | return 1; |
| 11157 | } |
| 11158 | |
Willy Tarreau | 49ad95c | 2015-01-19 15:06:26 +0100 | [diff] [blame] | 11159 | /* Extracts the query string, which comes after the question mark '?'. If no |
| 11160 | * question mark is found, nothing is returned. Otherwise it returns a sample |
| 11161 | * of type string carrying the whole query string. |
| 11162 | */ |
| 11163 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 11164 | 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] | 11165 | { |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 11166 | struct http_txn *txn; |
Willy Tarreau | 49ad95c | 2015-01-19 15:06:26 +0100 | [diff] [blame] | 11167 | char *ptr, *end; |
| 11168 | |
| 11169 | CHECK_HTTP_MESSAGE_FIRST(); |
| 11170 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 11171 | txn = smp->strm->txn; |
Willy Tarreau | 49ad95c | 2015-01-19 15:06:26 +0100 | [diff] [blame] | 11172 | ptr = txn->req.chn->buf->p + txn->req.sl.rq.u; |
| 11173 | end = ptr + txn->req.sl.rq.u_l; |
| 11174 | |
| 11175 | /* look up the '?' */ |
| 11176 | do { |
| 11177 | if (ptr == end) |
| 11178 | return 0; |
| 11179 | } while (*ptr++ != '?'); |
| 11180 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 11181 | smp->data.type = SMP_T_STR; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 11182 | smp->data.u.str.str = ptr; |
| 11183 | smp->data.u.str.len = end - ptr; |
Willy Tarreau | 49ad95c | 2015-01-19 15:06:26 +0100 | [diff] [blame] | 11184 | smp->flags = SMP_F_VOL_1ST | SMP_F_CONST; |
| 11185 | return 1; |
| 11186 | } |
| 11187 | |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 11188 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 11189 | 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] | 11190 | { |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 11191 | /* Note: hdr_idx.v cannot be NULL in this ACL because the ACL is tagged |
| 11192 | * as a layer7 ACL, which involves automatic allocation of hdr_idx. |
| 11193 | */ |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 11194 | |
Willy Tarreau | 24e32d8 | 2012-04-23 23:55:44 +0200 | [diff] [blame] | 11195 | CHECK_HTTP_MESSAGE_FIRST_PERM(); |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 11196 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 11197 | smp->data.type = SMP_T_BOOL; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 11198 | smp->data.u.sint = 1; |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 11199 | return 1; |
| 11200 | } |
| 11201 | |
Willy Tarreau | 7f18e52 | 2010-10-22 20:04:13 +0200 | [diff] [blame] | 11202 | /* return a valid test if the current request is the first one on the connection */ |
| 11203 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 11204 | 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] | 11205 | { |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 11206 | smp->data.type = SMP_T_BOOL; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 11207 | smp->data.u.sint = !(smp->strm->txn->flags & TX_NOT_FIRST); |
Willy Tarreau | 7f18e52 | 2010-10-22 20:04:13 +0200 | [diff] [blame] | 11208 | return 1; |
| 11209 | } |
| 11210 | |
Willy Tarreau | 34db108 | 2012-04-19 17:16:54 +0200 | [diff] [blame] | 11211 | /* Accepts exactly 1 argument of type userlist */ |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 11212 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 11213 | 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] | 11214 | { |
| 11215 | |
Willy Tarreau | 24e32d8 | 2012-04-23 23:55:44 +0200 | [diff] [blame] | 11216 | if (!args || args->type != ARGT_USR) |
Willy Tarreau | 34db108 | 2012-04-19 17:16:54 +0200 | [diff] [blame] | 11217 | return 0; |
| 11218 | |
Willy Tarreau | c0239e0 | 2012-04-16 14:42:55 +0200 | [diff] [blame] | 11219 | CHECK_HTTP_MESSAGE_FIRST(); |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 11220 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 11221 | if (!get_http_auth(smp->strm)) |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 11222 | return 0; |
| 11223 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 11224 | smp->data.type = SMP_T_BOOL; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 11225 | 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] | 11226 | smp->strm->txn->auth.pass); |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 11227 | return 1; |
| 11228 | } |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 11229 | |
Willy Tarreau | 4a3fd4c | 2012-05-10 23:18:26 +0200 | [diff] [blame] | 11230 | /* Accepts exactly 1 argument of type userlist */ |
| 11231 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 11232 | 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] | 11233 | { |
Willy Tarreau | 4a3fd4c | 2012-05-10 23:18:26 +0200 | [diff] [blame] | 11234 | if (!args || args->type != ARGT_USR) |
| 11235 | return 0; |
| 11236 | |
| 11237 | CHECK_HTTP_MESSAGE_FIRST(); |
| 11238 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 11239 | if (!get_http_auth(smp->strm)) |
Willy Tarreau | 4a3fd4c | 2012-05-10 23:18:26 +0200 | [diff] [blame] | 11240 | return 0; |
| 11241 | |
Willy Tarreau | 4a3fd4c | 2012-05-10 23:18:26 +0200 | [diff] [blame] | 11242 | /* if the user does not belong to the userlist or has a wrong password, |
| 11243 | * report that it unconditionally does not match. Otherwise we return |
Thierry FOURNIER | 9eec0a6 | 2014-01-22 18:38:02 +0100 | [diff] [blame] | 11244 | * a string containing the username. |
Willy Tarreau | 4a3fd4c | 2012-05-10 23:18:26 +0200 | [diff] [blame] | 11245 | */ |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 11246 | if (!check_user(args->data.usr, smp->strm->txn->auth.user, |
| 11247 | smp->strm->txn->auth.pass)) |
Thierry FOURNIER | 9eec0a6 | 2014-01-22 18:38:02 +0100 | [diff] [blame] | 11248 | return 0; |
| 11249 | |
| 11250 | /* pat_match_auth() will need the user list */ |
| 11251 | smp->ctx.a[0] = args->data.usr; |
| 11252 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 11253 | smp->data.type = SMP_T_STR; |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 11254 | smp->flags = SMP_F_CONST; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 11255 | smp->data.u.str.str = smp->strm->txn->auth.user; |
| 11256 | smp->data.u.str.len = strlen(smp->strm->txn->auth.user); |
Willy Tarreau | 4a3fd4c | 2012-05-10 23:18:26 +0200 | [diff] [blame] | 11257 | |
| 11258 | return 1; |
| 11259 | } |
| 11260 | |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 11261 | /* Try to find the next occurrence of a cookie name in a cookie header value. |
| 11262 | * The lookup begins at <hdr>. The pointer and size of the next occurrence of |
| 11263 | * the cookie value is returned into *value and *value_l, and the function |
| 11264 | * returns a pointer to the next pointer to search from if the value was found. |
| 11265 | * Otherwise if the cookie was not found, NULL is returned and neither value |
| 11266 | * nor value_l are touched. The input <hdr> string should first point to the |
| 11267 | * header's value, and the <hdr_end> pointer must point to the first character |
| 11268 | * not part of the value. <list> must be non-zero if value may represent a list |
| 11269 | * of values (cookie headers). This makes it faster to abort parsing when no |
| 11270 | * list is expected. |
| 11271 | */ |
David Carlier | 4686f79 | 2015-09-25 14:10:50 +0100 | [diff] [blame] | 11272 | char * |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 11273 | extract_cookie_value(char *hdr, const char *hdr_end, |
| 11274 | char *cookie_name, size_t cookie_name_l, int list, |
Willy Tarreau | 3fb818c | 2012-04-11 17:21:08 +0200 | [diff] [blame] | 11275 | char **value, int *value_l) |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 11276 | { |
| 11277 | char *equal, *att_end, *att_beg, *val_beg, *val_end; |
| 11278 | char *next; |
| 11279 | |
| 11280 | /* we search at least a cookie name followed by an equal, and more |
| 11281 | * generally something like this : |
| 11282 | * Cookie: NAME1 = VALUE 1 ; NAME2 = VALUE2 ; NAME3 = VALUE3\r\n |
| 11283 | */ |
| 11284 | for (att_beg = hdr; att_beg + cookie_name_l + 1 < hdr_end; att_beg = next + 1) { |
| 11285 | /* Iterate through all cookies on this line */ |
| 11286 | |
Willy Tarreau | 2235b26 | 2016-11-05 15:50:20 +0100 | [diff] [blame] | 11287 | while (att_beg < hdr_end && HTTP_IS_SPHT(*att_beg)) |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 11288 | att_beg++; |
| 11289 | |
| 11290 | /* find att_end : this is the first character after the last non |
| 11291 | * space before the equal. It may be equal to hdr_end. |
| 11292 | */ |
| 11293 | equal = att_end = att_beg; |
| 11294 | |
| 11295 | while (equal < hdr_end) { |
| 11296 | if (*equal == '=' || *equal == ';' || (list && *equal == ',')) |
| 11297 | break; |
Willy Tarreau | 2235b26 | 2016-11-05 15:50:20 +0100 | [diff] [blame] | 11298 | if (HTTP_IS_SPHT(*equal++)) |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 11299 | continue; |
| 11300 | att_end = equal; |
| 11301 | } |
| 11302 | |
| 11303 | /* here, <equal> points to '=', a delimitor or the end. <att_end> |
| 11304 | * is between <att_beg> and <equal>, both may be identical. |
| 11305 | */ |
| 11306 | |
| 11307 | /* look for end of cookie if there is an equal sign */ |
| 11308 | if (equal < hdr_end && *equal == '=') { |
| 11309 | /* look for the beginning of the value */ |
| 11310 | val_beg = equal + 1; |
Willy Tarreau | 2235b26 | 2016-11-05 15:50:20 +0100 | [diff] [blame] | 11311 | while (val_beg < hdr_end && HTTP_IS_SPHT(*val_beg)) |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 11312 | val_beg++; |
| 11313 | |
| 11314 | /* find the end of the value, respecting quotes */ |
| 11315 | next = find_cookie_value_end(val_beg, hdr_end); |
| 11316 | |
| 11317 | /* make val_end point to the first white space or delimitor after the value */ |
| 11318 | val_end = next; |
Willy Tarreau | 2235b26 | 2016-11-05 15:50:20 +0100 | [diff] [blame] | 11319 | while (val_end > val_beg && HTTP_IS_SPHT(*(val_end - 1))) |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 11320 | val_end--; |
| 11321 | } else { |
| 11322 | val_beg = val_end = next = equal; |
| 11323 | } |
| 11324 | |
| 11325 | /* We have nothing to do with attributes beginning with '$'. However, |
| 11326 | * they will automatically be removed if a header before them is removed, |
| 11327 | * since they're supposed to be linked together. |
| 11328 | */ |
| 11329 | if (*att_beg == '$') |
| 11330 | continue; |
| 11331 | |
| 11332 | /* Ignore cookies with no equal sign */ |
| 11333 | if (equal == next) |
| 11334 | continue; |
| 11335 | |
| 11336 | /* Now we have the cookie name between att_beg and att_end, and |
| 11337 | * its value between val_beg and val_end. |
| 11338 | */ |
| 11339 | |
| 11340 | if (att_end - att_beg == cookie_name_l && |
| 11341 | memcmp(att_beg, cookie_name, cookie_name_l) == 0) { |
| 11342 | /* let's return this value and indicate where to go on from */ |
| 11343 | *value = val_beg; |
| 11344 | *value_l = val_end - val_beg; |
| 11345 | return next + 1; |
| 11346 | } |
| 11347 | |
| 11348 | /* Set-Cookie headers only have the name in the first attr=value part */ |
| 11349 | if (!list) |
| 11350 | break; |
| 11351 | } |
| 11352 | |
| 11353 | return NULL; |
| 11354 | } |
| 11355 | |
William Lallemand | a43ba4e | 2014-01-28 18:14:25 +0100 | [diff] [blame] | 11356 | /* Fetch a captured HTTP request header. The index is the position of |
| 11357 | * the "capture" option in the configuration file |
| 11358 | */ |
| 11359 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 11360 | 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] | 11361 | { |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 11362 | struct proxy *fe = strm_fe(smp->strm); |
William Lallemand | a43ba4e | 2014-01-28 18:14:25 +0100 | [diff] [blame] | 11363 | int idx; |
| 11364 | |
Thierry FOURNIER | bf65cd4 | 2015-07-20 17:45:02 +0200 | [diff] [blame] | 11365 | if (!args || args->type != ARGT_SINT) |
William Lallemand | a43ba4e | 2014-01-28 18:14:25 +0100 | [diff] [blame] | 11366 | return 0; |
| 11367 | |
Thierry FOURNIER | bf65cd4 | 2015-07-20 17:45:02 +0200 | [diff] [blame] | 11368 | idx = args->data.sint; |
William Lallemand | a43ba4e | 2014-01-28 18:14:25 +0100 | [diff] [blame] | 11369 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 11370 | 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] | 11371 | return 0; |
| 11372 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 11373 | smp->data.type = SMP_T_STR; |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 11374 | smp->flags |= SMP_F_CONST; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 11375 | smp->data.u.str.str = smp->strm->req_cap[idx]; |
| 11376 | smp->data.u.str.len = strlen(smp->strm->req_cap[idx]); |
William Lallemand | a43ba4e | 2014-01-28 18:14:25 +0100 | [diff] [blame] | 11377 | |
| 11378 | return 1; |
| 11379 | } |
| 11380 | |
| 11381 | /* Fetch a captured HTTP response header. The index is the position of |
| 11382 | * the "capture" option in the configuration file |
| 11383 | */ |
| 11384 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 11385 | 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] | 11386 | { |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 11387 | struct proxy *fe = strm_fe(smp->strm); |
William Lallemand | a43ba4e | 2014-01-28 18:14:25 +0100 | [diff] [blame] | 11388 | int idx; |
| 11389 | |
Thierry FOURNIER | bf65cd4 | 2015-07-20 17:45:02 +0200 | [diff] [blame] | 11390 | if (!args || args->type != ARGT_SINT) |
William Lallemand | a43ba4e | 2014-01-28 18:14:25 +0100 | [diff] [blame] | 11391 | return 0; |
| 11392 | |
Thierry FOURNIER | bf65cd4 | 2015-07-20 17:45:02 +0200 | [diff] [blame] | 11393 | idx = args->data.sint; |
William Lallemand | a43ba4e | 2014-01-28 18:14:25 +0100 | [diff] [blame] | 11394 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 11395 | 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] | 11396 | return 0; |
| 11397 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 11398 | smp->data.type = SMP_T_STR; |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 11399 | smp->flags |= SMP_F_CONST; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 11400 | smp->data.u.str.str = smp->strm->res_cap[idx]; |
| 11401 | smp->data.u.str.len = strlen(smp->strm->res_cap[idx]); |
William Lallemand | a43ba4e | 2014-01-28 18:14:25 +0100 | [diff] [blame] | 11402 | |
| 11403 | return 1; |
| 11404 | } |
| 11405 | |
William Lallemand | 65ad6e1 | 2014-01-31 15:08:02 +0100 | [diff] [blame] | 11406 | /* Extracts the METHOD in the HTTP request, the txn->uri should be filled before the call */ |
| 11407 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 11408 | 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] | 11409 | { |
| 11410 | struct chunk *temp; |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 11411 | struct http_txn *txn = smp->strm->txn; |
William Lallemand | 96a7785 | 2014-02-05 00:30:02 +0100 | [diff] [blame] | 11412 | char *ptr; |
William Lallemand | 65ad6e1 | 2014-01-31 15:08:02 +0100 | [diff] [blame] | 11413 | |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 11414 | if (!txn || !txn->uri) |
William Lallemand | 65ad6e1 | 2014-01-31 15:08:02 +0100 | [diff] [blame] | 11415 | return 0; |
| 11416 | |
William Lallemand | 96a7785 | 2014-02-05 00:30:02 +0100 | [diff] [blame] | 11417 | ptr = txn->uri; |
William Lallemand | 65ad6e1 | 2014-01-31 15:08:02 +0100 | [diff] [blame] | 11418 | |
William Lallemand | 96a7785 | 2014-02-05 00:30:02 +0100 | [diff] [blame] | 11419 | while (*ptr != ' ' && *ptr != '\0') /* find first space */ |
| 11420 | ptr++; |
William Lallemand | 65ad6e1 | 2014-01-31 15:08:02 +0100 | [diff] [blame] | 11421 | |
William Lallemand | 96a7785 | 2014-02-05 00:30:02 +0100 | [diff] [blame] | 11422 | temp = get_trash_chunk(); |
| 11423 | temp->str = txn->uri; |
| 11424 | temp->len = ptr - txn->uri; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 11425 | smp->data.u.str = *temp; |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 11426 | smp->data.type = SMP_T_STR; |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 11427 | smp->flags = SMP_F_CONST; |
William Lallemand | 65ad6e1 | 2014-01-31 15:08:02 +0100 | [diff] [blame] | 11428 | |
| 11429 | return 1; |
| 11430 | |
| 11431 | } |
| 11432 | |
| 11433 | /* Extracts the path in the HTTP request, the txn->uri should be filled before the call */ |
| 11434 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 11435 | 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] | 11436 | { |
| 11437 | struct chunk *temp; |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 11438 | struct http_txn *txn = smp->strm->txn; |
William Lallemand | 65ad6e1 | 2014-01-31 15:08:02 +0100 | [diff] [blame] | 11439 | char *ptr; |
William Lallemand | 65ad6e1 | 2014-01-31 15:08:02 +0100 | [diff] [blame] | 11440 | |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 11441 | if (!txn || !txn->uri) |
William Lallemand | 65ad6e1 | 2014-01-31 15:08:02 +0100 | [diff] [blame] | 11442 | return 0; |
William Lallemand | 96a7785 | 2014-02-05 00:30:02 +0100 | [diff] [blame] | 11443 | |
William Lallemand | 65ad6e1 | 2014-01-31 15:08:02 +0100 | [diff] [blame] | 11444 | ptr = txn->uri; |
| 11445 | |
| 11446 | while (*ptr != ' ' && *ptr != '\0') /* find first space */ |
| 11447 | ptr++; |
William Lallemand | 96a7785 | 2014-02-05 00:30:02 +0100 | [diff] [blame] | 11448 | |
William Lallemand | 65ad6e1 | 2014-01-31 15:08:02 +0100 | [diff] [blame] | 11449 | if (!*ptr) |
| 11450 | return 0; |
| 11451 | |
| 11452 | ptr++; /* skip the space */ |
| 11453 | |
| 11454 | temp = get_trash_chunk(); |
William Lallemand | 96a7785 | 2014-02-05 00:30:02 +0100 | [diff] [blame] | 11455 | ptr = temp->str = http_get_path_from_string(ptr); |
William Lallemand | 65ad6e1 | 2014-01-31 15:08:02 +0100 | [diff] [blame] | 11456 | if (!ptr) |
| 11457 | return 0; |
| 11458 | while (*ptr != ' ' && *ptr != '\0') /* find space after URI */ |
| 11459 | ptr++; |
William Lallemand | 65ad6e1 | 2014-01-31 15:08:02 +0100 | [diff] [blame] | 11460 | |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 11461 | smp->data.u.str = *temp; |
| 11462 | smp->data.u.str.len = ptr - temp->str; |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 11463 | smp->data.type = SMP_T_STR; |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 11464 | smp->flags = SMP_F_CONST; |
William Lallemand | 65ad6e1 | 2014-01-31 15:08:02 +0100 | [diff] [blame] | 11465 | |
| 11466 | return 1; |
| 11467 | } |
| 11468 | |
Willy Tarreau | 3c1b5ec | 2014-04-24 23:41:57 +0200 | [diff] [blame] | 11469 | /* Retrieves the HTTP version from the request (either 1.0 or 1.1) and emits it |
| 11470 | * as a string (either "HTTP/1.0" or "HTTP/1.1"). |
| 11471 | */ |
| 11472 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 11473 | 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] | 11474 | { |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 11475 | struct http_txn *txn = smp->strm->txn; |
Willy Tarreau | 3c1b5ec | 2014-04-24 23:41:57 +0200 | [diff] [blame] | 11476 | |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 11477 | if (!txn || txn->req.msg_state < HTTP_MSG_HDR_FIRST) |
Willy Tarreau | 3c1b5ec | 2014-04-24 23:41:57 +0200 | [diff] [blame] | 11478 | return 0; |
| 11479 | |
| 11480 | if (txn->req.flags & HTTP_MSGF_VER_11) |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 11481 | smp->data.u.str.str = "HTTP/1.1"; |
Willy Tarreau | 3c1b5ec | 2014-04-24 23:41:57 +0200 | [diff] [blame] | 11482 | else |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 11483 | smp->data.u.str.str = "HTTP/1.0"; |
Willy Tarreau | 3c1b5ec | 2014-04-24 23:41:57 +0200 | [diff] [blame] | 11484 | |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 11485 | smp->data.u.str.len = 8; |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 11486 | smp->data.type = SMP_T_STR; |
Willy Tarreau | 3c1b5ec | 2014-04-24 23:41:57 +0200 | [diff] [blame] | 11487 | smp->flags = SMP_F_CONST; |
| 11488 | return 1; |
| 11489 | |
| 11490 | } |
| 11491 | |
| 11492 | /* Retrieves the HTTP version from the response (either 1.0 or 1.1) and emits it |
| 11493 | * as a string (either "HTTP/1.0" or "HTTP/1.1"). |
| 11494 | */ |
| 11495 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 11496 | 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] | 11497 | { |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 11498 | struct http_txn *txn = smp->strm->txn; |
Willy Tarreau | 3c1b5ec | 2014-04-24 23:41:57 +0200 | [diff] [blame] | 11499 | |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 11500 | if (!txn || txn->rsp.msg_state < HTTP_MSG_HDR_FIRST) |
Willy Tarreau | 3c1b5ec | 2014-04-24 23:41:57 +0200 | [diff] [blame] | 11501 | return 0; |
| 11502 | |
| 11503 | if (txn->rsp.flags & HTTP_MSGF_VER_11) |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 11504 | smp->data.u.str.str = "HTTP/1.1"; |
Willy Tarreau | 3c1b5ec | 2014-04-24 23:41:57 +0200 | [diff] [blame] | 11505 | else |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 11506 | smp->data.u.str.str = "HTTP/1.0"; |
Willy Tarreau | 3c1b5ec | 2014-04-24 23:41:57 +0200 | [diff] [blame] | 11507 | |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 11508 | smp->data.u.str.len = 8; |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 11509 | smp->data.type = SMP_T_STR; |
Willy Tarreau | 3c1b5ec | 2014-04-24 23:41:57 +0200 | [diff] [blame] | 11510 | smp->flags = SMP_F_CONST; |
| 11511 | return 1; |
| 11512 | |
| 11513 | } |
| 11514 | |
William Lallemand | 65ad6e1 | 2014-01-31 15:08:02 +0100 | [diff] [blame] | 11515 | |
Willy Tarreau | e333ec9 | 2012-04-16 16:26:40 +0200 | [diff] [blame] | 11516 | /* 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] | 11517 | * 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] | 11518 | * 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] | 11519 | * 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] | 11520 | * 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] | 11521 | * Accepts exactly 1 argument of type string. If the input options indicate |
| 11522 | * 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] | 11523 | * The returned sample is of type CSTR. Can be used to parse cookies in other |
| 11524 | * files. |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 11525 | */ |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 11526 | 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] | 11527 | { |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 11528 | struct http_txn *txn; |
| 11529 | struct hdr_idx *idx; |
Willy Tarreau | a890d07 | 2013-04-02 12:01:06 +0200 | [diff] [blame] | 11530 | struct hdr_ctx *ctx = smp->ctx.a[2]; |
Willy Tarreau | e333ec9 | 2012-04-16 16:26:40 +0200 | [diff] [blame] | 11531 | const struct http_msg *msg; |
| 11532 | const char *hdr_name; |
| 11533 | int hdr_name_len; |
| 11534 | char *sol; |
Willy Tarreau | 28376d6 | 2012-04-26 21:26:10 +0200 | [diff] [blame] | 11535 | int occ = 0; |
| 11536 | int found = 0; |
Willy Tarreau | e333ec9 | 2012-04-16 16:26:40 +0200 | [diff] [blame] | 11537 | |
Willy Tarreau | 24e32d8 | 2012-04-23 23:55:44 +0200 | [diff] [blame] | 11538 | if (!args || args->type != ARGT_STR) |
Willy Tarreau | 34db108 | 2012-04-19 17:16:54 +0200 | [diff] [blame] | 11539 | return 0; |
| 11540 | |
Willy Tarreau | a890d07 | 2013-04-02 12:01:06 +0200 | [diff] [blame] | 11541 | if (!ctx) { |
| 11542 | /* first call */ |
| 11543 | ctx = &static_hdr_ctx; |
| 11544 | ctx->idx = 0; |
| 11545 | smp->ctx.a[2] = ctx; |
| 11546 | } |
| 11547 | |
Willy Tarreau | e333ec9 | 2012-04-16 16:26:40 +0200 | [diff] [blame] | 11548 | CHECK_HTTP_MESSAGE_FIRST(); |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 11549 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 11550 | txn = smp->strm->txn; |
| 11551 | idx = &smp->strm->txn->hdr_idx; |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 11552 | |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 11553 | if ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) { |
Willy Tarreau | e333ec9 | 2012-04-16 16:26:40 +0200 | [diff] [blame] | 11554 | msg = &txn->req; |
| 11555 | hdr_name = "Cookie"; |
| 11556 | hdr_name_len = 6; |
| 11557 | } else { |
| 11558 | msg = &txn->rsp; |
| 11559 | hdr_name = "Set-Cookie"; |
| 11560 | hdr_name_len = 10; |
| 11561 | } |
| 11562 | |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 11563 | if (!occ && !(smp->opt & SMP_OPT_ITERATE)) |
Willy Tarreau | 28376d6 | 2012-04-26 21:26:10 +0200 | [diff] [blame] | 11564 | /* no explicit occurrence and single fetch => last cookie by default */ |
| 11565 | occ = -1; |
| 11566 | |
| 11567 | /* OK so basically here, either we want only one value and it's the |
| 11568 | * last one, or we want to iterate over all of them and we fetch the |
| 11569 | * next one. |
| 11570 | */ |
| 11571 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 11572 | sol = msg->chn->buf->p; |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 11573 | if (!(smp->flags & SMP_F_NOT_LAST)) { |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 11574 | /* search for the header from the beginning, we must first initialize |
| 11575 | * the search parameters. |
| 11576 | */ |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 11577 | smp->ctx.a[0] = NULL; |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 11578 | ctx->idx = 0; |
| 11579 | } |
| 11580 | |
Willy Tarreau | 28376d6 | 2012-04-26 21:26:10 +0200 | [diff] [blame] | 11581 | smp->flags |= SMP_F_VOL_HDR; |
| 11582 | |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 11583 | while (1) { |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 11584 | /* Note: smp->ctx.a[0] == NULL every time we need to fetch a new header */ |
| 11585 | if (!smp->ctx.a[0]) { |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 11586 | if (!http_find_header2(hdr_name, hdr_name_len, sol, idx, ctx)) |
| 11587 | goto out; |
| 11588 | |
Willy Tarreau | 24e32d8 | 2012-04-23 23:55:44 +0200 | [diff] [blame] | 11589 | if (ctx->vlen < args->data.str.len + 1) |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 11590 | continue; |
| 11591 | |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 11592 | smp->ctx.a[0] = ctx->line + ctx->val; |
| 11593 | smp->ctx.a[1] = smp->ctx.a[0] + ctx->vlen; |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 11594 | } |
| 11595 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 11596 | smp->data.type = SMP_T_STR; |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 11597 | smp->flags |= SMP_F_CONST; |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 11598 | 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] | 11599 | args->data.str.str, args->data.str.len, |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 11600 | (smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ, |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 11601 | &smp->data.u.str.str, |
| 11602 | &smp->data.u.str.len); |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 11603 | if (smp->ctx.a[0]) { |
Willy Tarreau | 28376d6 | 2012-04-26 21:26:10 +0200 | [diff] [blame] | 11604 | found = 1; |
| 11605 | if (occ >= 0) { |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 11606 | /* one value was returned into smp->data.u.str.{str,len} */ |
Willy Tarreau | 28376d6 | 2012-04-26 21:26:10 +0200 | [diff] [blame] | 11607 | smp->flags |= SMP_F_NOT_LAST; |
| 11608 | return 1; |
| 11609 | } |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 11610 | } |
Willy Tarreau | 28376d6 | 2012-04-26 21:26:10 +0200 | [diff] [blame] | 11611 | /* if we're looking for last occurrence, let's loop */ |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 11612 | } |
Willy Tarreau | 28376d6 | 2012-04-26 21:26:10 +0200 | [diff] [blame] | 11613 | /* all cookie headers and values were scanned. If we're looking for the |
| 11614 | * last occurrence, we may return it now. |
| 11615 | */ |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 11616 | out: |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 11617 | smp->flags &= ~SMP_F_NOT_LAST; |
Willy Tarreau | 28376d6 | 2012-04-26 21:26:10 +0200 | [diff] [blame] | 11618 | return found; |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 11619 | } |
| 11620 | |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 11621 | /* 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] | 11622 | * 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] | 11623 | * multiple cookies may be parsed on the same line. The returned sample is of |
| 11624 | * type UINT. Accepts exactly 1 argument of type string. |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 11625 | */ |
| 11626 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 11627 | 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] | 11628 | { |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 11629 | struct http_txn *txn; |
| 11630 | struct hdr_idx *idx; |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 11631 | struct hdr_ctx ctx; |
Willy Tarreau | e333ec9 | 2012-04-16 16:26:40 +0200 | [diff] [blame] | 11632 | const struct http_msg *msg; |
| 11633 | const char *hdr_name; |
| 11634 | int hdr_name_len; |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 11635 | int cnt; |
| 11636 | char *val_beg, *val_end; |
Willy Tarreau | e333ec9 | 2012-04-16 16:26:40 +0200 | [diff] [blame] | 11637 | char *sol; |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 11638 | |
Willy Tarreau | 24e32d8 | 2012-04-23 23:55:44 +0200 | [diff] [blame] | 11639 | if (!args || args->type != ARGT_STR) |
Willy Tarreau | 34db108 | 2012-04-19 17:16:54 +0200 | [diff] [blame] | 11640 | return 0; |
| 11641 | |
Willy Tarreau | e333ec9 | 2012-04-16 16:26:40 +0200 | [diff] [blame] | 11642 | CHECK_HTTP_MESSAGE_FIRST(); |
| 11643 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 11644 | txn = smp->strm->txn; |
| 11645 | idx = &smp->strm->txn->hdr_idx; |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 11646 | |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 11647 | if ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) { |
Willy Tarreau | e333ec9 | 2012-04-16 16:26:40 +0200 | [diff] [blame] | 11648 | msg = &txn->req; |
| 11649 | hdr_name = "Cookie"; |
| 11650 | hdr_name_len = 6; |
| 11651 | } else { |
| 11652 | msg = &txn->rsp; |
| 11653 | hdr_name = "Set-Cookie"; |
| 11654 | hdr_name_len = 10; |
| 11655 | } |
| 11656 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 11657 | sol = msg->chn->buf->p; |
Willy Tarreau | 46787ed | 2012-04-11 17:28:40 +0200 | [diff] [blame] | 11658 | val_end = val_beg = NULL; |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 11659 | ctx.idx = 0; |
| 11660 | cnt = 0; |
| 11661 | |
| 11662 | while (1) { |
| 11663 | /* Note: val_beg == NULL every time we need to fetch a new header */ |
| 11664 | if (!val_beg) { |
| 11665 | if (!http_find_header2(hdr_name, hdr_name_len, sol, idx, &ctx)) |
| 11666 | break; |
| 11667 | |
Willy Tarreau | 24e32d8 | 2012-04-23 23:55:44 +0200 | [diff] [blame] | 11668 | if (ctx.vlen < args->data.str.len + 1) |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 11669 | continue; |
| 11670 | |
| 11671 | val_beg = ctx.line + ctx.val; |
| 11672 | val_end = val_beg + ctx.vlen; |
| 11673 | } |
| 11674 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 11675 | smp->data.type = SMP_T_STR; |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 11676 | smp->flags |= SMP_F_CONST; |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 11677 | while ((val_beg = extract_cookie_value(val_beg, val_end, |
Willy Tarreau | 24e32d8 | 2012-04-23 23:55:44 +0200 | [diff] [blame] | 11678 | args->data.str.str, args->data.str.len, |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 11679 | (smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ, |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 11680 | &smp->data.u.str.str, |
| 11681 | &smp->data.u.str.len))) { |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 11682 | cnt++; |
| 11683 | } |
| 11684 | } |
| 11685 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 11686 | smp->data.type = SMP_T_SINT; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 11687 | smp->data.u.sint = cnt; |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 11688 | smp->flags |= SMP_F_VOL_HDR; |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 11689 | return 1; |
| 11690 | } |
| 11691 | |
Willy Tarreau | 5153936 | 2012-05-08 12:46:28 +0200 | [diff] [blame] | 11692 | /* Fetch an cookie's integer value. The integer value is returned. It |
| 11693 | * takes a mandatory argument of type string. It relies on smp_fetch_cookie(). |
| 11694 | */ |
| 11695 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 11696 | 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] | 11697 | { |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 11698 | int ret = smp_fetch_cookie(args, smp, kw, private); |
Willy Tarreau | 5153936 | 2012-05-08 12:46:28 +0200 | [diff] [blame] | 11699 | |
| 11700 | if (ret > 0) { |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 11701 | smp->data.type = SMP_T_SINT; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 11702 | 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] | 11703 | } |
| 11704 | |
| 11705 | return ret; |
| 11706 | } |
| 11707 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 11708 | /************************************************************************/ |
Willy Tarreau | 1278578 | 2012-04-27 21:37:17 +0200 | [diff] [blame] | 11709 | /* The code below is dedicated to sample fetches */ |
Willy Tarreau | 4a56897 | 2010-05-12 08:08:50 +0200 | [diff] [blame] | 11710 | /************************************************************************/ |
| 11711 | |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 11712 | /* |
| 11713 | * Given a path string and its length, find the position of beginning of the |
| 11714 | * query string. Returns NULL if no query string is found in the path. |
| 11715 | * |
| 11716 | * Example: if path = "/foo/bar/fubar?yo=mama;ye=daddy", and n = 22: |
| 11717 | * |
Willy Tarreau | 1ede1da | 2015-05-07 16:06:18 +0200 | [diff] [blame] | 11718 | * find_query_string(path, n, '?') points to "yo=mama;ye=daddy" string. |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 11719 | */ |
bedis | 4c75cca | 2012-10-05 08:38:24 +0200 | [diff] [blame] | 11720 | 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] | 11721 | { |
| 11722 | char *p; |
Emeric Brun | 485479d | 2010-09-23 18:02:19 +0200 | [diff] [blame] | 11723 | |
bedis | 4c75cca | 2012-10-05 08:38:24 +0200 | [diff] [blame] | 11724 | p = memchr(path, delim, path_l); |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 11725 | return p ? p + 1 : NULL; |
| 11726 | } |
| 11727 | |
bedis | 4c75cca | 2012-10-05 08:38:24 +0200 | [diff] [blame] | 11728 | static inline int is_param_delimiter(char c, char delim) |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 11729 | { |
bedis | 4c75cca | 2012-10-05 08:38:24 +0200 | [diff] [blame] | 11730 | return c == '&' || c == ';' || c == delim; |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 11731 | } |
| 11732 | |
Thierry FOURNIER | 8be451c | 2015-05-20 15:28:12 +0200 | [diff] [blame] | 11733 | /* after increasing a pointer value, it can exceed the first buffer |
| 11734 | * size. This function transform the value of <ptr> according with |
| 11735 | * the expected position. <chunks> is an array of the one or two |
| 11736 | * avalaible chunks. The first value is the start of the first chunk, |
| 11737 | * the second value if the end+1 of the first chunks. The third value |
| 11738 | * is NULL or the start of the second chunk and the fourth value is |
| 11739 | * the end+1 of the second chunk. The function returns 1 if does a |
| 11740 | * wrap, else returns 0. |
| 11741 | */ |
| 11742 | static inline int fix_pointer_if_wrap(const char **chunks, const char **ptr) |
| 11743 | { |
| 11744 | if (*ptr < chunks[1]) |
| 11745 | return 0; |
| 11746 | if (!chunks[2]) |
| 11747 | return 0; |
| 11748 | *ptr = chunks[2] + ( *ptr - chunks[1] ); |
| 11749 | return 1; |
| 11750 | } |
| 11751 | |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 11752 | /* |
| 11753 | * Given a url parameter, find the starting position of the first occurence, |
| 11754 | * or NULL if the parameter is not found. |
| 11755 | * |
| 11756 | * Example: if query_string is "yo=mama;ye=daddy" and url_param_name is "ye", |
| 11757 | * the function will return query_string+8. |
Thierry FOURNIER | 8be451c | 2015-05-20 15:28:12 +0200 | [diff] [blame] | 11758 | * |
Willy Tarreau | f662582 | 2015-12-27 14:51:01 +0100 | [diff] [blame] | 11759 | * 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] | 11760 | * or the second chunk. The caller must be check the position before using the |
| 11761 | * result. |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 11762 | */ |
Thierry FOURNIER | 8be451c | 2015-05-20 15:28:12 +0200 | [diff] [blame] | 11763 | static const char * |
| 11764 | find_url_param_pos(const char **chunks, |
Willy Tarreau | 1ede1da | 2015-05-07 16:06:18 +0200 | [diff] [blame] | 11765 | const char* url_param_name, size_t url_param_name_l, |
bedis | 4c75cca | 2012-10-05 08:38:24 +0200 | [diff] [blame] | 11766 | char delim) |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 11767 | { |
Thierry FOURNIER | 8be451c | 2015-05-20 15:28:12 +0200 | [diff] [blame] | 11768 | const char *pos, *last, *equal; |
| 11769 | const char **bufs = chunks; |
| 11770 | int l1, l2; |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 11771 | |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 11772 | |
Thierry FOURNIER | 8be451c | 2015-05-20 15:28:12 +0200 | [diff] [blame] | 11773 | pos = bufs[0]; |
| 11774 | last = bufs[1]; |
Willy Tarreau | f662582 | 2015-12-27 14:51:01 +0100 | [diff] [blame] | 11775 | while (pos < last) { |
Thierry FOURNIER | 8be451c | 2015-05-20 15:28:12 +0200 | [diff] [blame] | 11776 | /* Check the equal. */ |
| 11777 | equal = pos + url_param_name_l; |
| 11778 | if (fix_pointer_if_wrap(chunks, &equal)) { |
| 11779 | if (equal >= chunks[3]) |
| 11780 | return NULL; |
| 11781 | } else { |
| 11782 | if (equal >= chunks[1]) |
| 11783 | return NULL; |
| 11784 | } |
| 11785 | if (*equal == '=') { |
| 11786 | if (pos + url_param_name_l > last) { |
| 11787 | /* process wrap case, we detect a wrap. In this case, the |
| 11788 | * comparison is performed in two parts. |
| 11789 | */ |
| 11790 | |
| 11791 | /* This is the end, we dont have any other chunk. */ |
| 11792 | if (bufs != chunks || !bufs[2]) |
| 11793 | return NULL; |
| 11794 | |
| 11795 | /* Compute the length of each part of the comparison. */ |
| 11796 | l1 = last - pos; |
| 11797 | l2 = url_param_name_l - l1; |
| 11798 | |
| 11799 | /* The second buffer is too short to contain the compared string. */ |
| 11800 | if (bufs[2] + l2 > bufs[3]) |
| 11801 | return NULL; |
| 11802 | |
| 11803 | if (memcmp(pos, url_param_name, l1) == 0 && |
| 11804 | memcmp(bufs[2], url_param_name+l1, l2) == 0) |
| 11805 | return pos; |
| 11806 | |
| 11807 | /* Perform wrapping and jump the string who fail the comparison. */ |
| 11808 | bufs += 2; |
| 11809 | pos = bufs[0] + l2; |
| 11810 | last = bufs[1]; |
| 11811 | |
| 11812 | } else { |
| 11813 | /* process a simple comparison. */ |
Willy Tarreau | f662582 | 2015-12-27 14:51:01 +0100 | [diff] [blame] | 11814 | if (memcmp(pos, url_param_name, url_param_name_l) == 0) |
| 11815 | return pos; |
Thierry FOURNIER | 8be451c | 2015-05-20 15:28:12 +0200 | [diff] [blame] | 11816 | pos += url_param_name_l + 1; |
| 11817 | if (fix_pointer_if_wrap(chunks, &pos)) |
| 11818 | last = bufs[2]; |
| 11819 | } |
| 11820 | } |
| 11821 | |
| 11822 | while (1) { |
| 11823 | /* Look for the next delimiter. */ |
Willy Tarreau | f662582 | 2015-12-27 14:51:01 +0100 | [diff] [blame] | 11824 | while (pos < last && !is_param_delimiter(*pos, delim)) |
Thierry FOURNIER | 8be451c | 2015-05-20 15:28:12 +0200 | [diff] [blame] | 11825 | pos++; |
| 11826 | if (pos < last) |
| 11827 | break; |
| 11828 | /* process buffer wrapping. */ |
| 11829 | if (bufs != chunks || !bufs[2]) |
| 11830 | return NULL; |
| 11831 | bufs += 2; |
| 11832 | pos = bufs[0]; |
| 11833 | last = bufs[1]; |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 11834 | } |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 11835 | pos++; |
| 11836 | } |
| 11837 | return NULL; |
| 11838 | } |
| 11839 | |
| 11840 | /* |
Cyril Bonté | ce1ef4d | 2015-11-26 21:39:56 +0100 | [diff] [blame] | 11841 | * Given a url parameter name and a query string, find the next value. |
| 11842 | * An empty url_param_name matches the first available parameter. |
| 11843 | * If the parameter is found, 1 is returned and *vstart / *vend are updated to |
| 11844 | * respectively provide a pointer to the value and its end. |
| 11845 | * Otherwise, 0 is returned and vstart/vend are not modified. |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 11846 | */ |
| 11847 | static int |
Thierry FOURNIER | 8be451c | 2015-05-20 15:28:12 +0200 | [diff] [blame] | 11848 | find_next_url_param(const char **chunks, |
Willy Tarreau | 1ede1da | 2015-05-07 16:06:18 +0200 | [diff] [blame] | 11849 | const char* url_param_name, size_t url_param_name_l, |
Thierry FOURNIER | 8be451c | 2015-05-20 15:28:12 +0200 | [diff] [blame] | 11850 | const char **vstart, const char **vend, char delim) |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 11851 | { |
Thierry FOURNIER | 8be451c | 2015-05-20 15:28:12 +0200 | [diff] [blame] | 11852 | const char *arg_start, *qs_end; |
| 11853 | const char *value_start, *value_end; |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 11854 | |
Thierry FOURNIER | 8be451c | 2015-05-20 15:28:12 +0200 | [diff] [blame] | 11855 | arg_start = chunks[0]; |
| 11856 | qs_end = chunks[1]; |
Willy Tarreau | 1ede1da | 2015-05-07 16:06:18 +0200 | [diff] [blame] | 11857 | if (url_param_name_l) { |
Thierry FOURNIER | 8be451c | 2015-05-20 15:28:12 +0200 | [diff] [blame] | 11858 | /* Looks for an argument name. */ |
| 11859 | arg_start = find_url_param_pos(chunks, |
Willy Tarreau | 1ede1da | 2015-05-07 16:06:18 +0200 | [diff] [blame] | 11860 | url_param_name, url_param_name_l, |
| 11861 | delim); |
Thierry FOURNIER | 8be451c | 2015-05-20 15:28:12 +0200 | [diff] [blame] | 11862 | /* Check for wrapping. */ |
Willy Tarreau | f662582 | 2015-12-27 14:51:01 +0100 | [diff] [blame] | 11863 | if (arg_start >= qs_end) |
Thierry FOURNIER | 8be451c | 2015-05-20 15:28:12 +0200 | [diff] [blame] | 11864 | qs_end = chunks[3]; |
Willy Tarreau | 1ede1da | 2015-05-07 16:06:18 +0200 | [diff] [blame] | 11865 | } |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 11866 | if (!arg_start) |
| 11867 | return 0; |
| 11868 | |
Willy Tarreau | 1ede1da | 2015-05-07 16:06:18 +0200 | [diff] [blame] | 11869 | if (!url_param_name_l) { |
Thierry FOURNIER | 8be451c | 2015-05-20 15:28:12 +0200 | [diff] [blame] | 11870 | while (1) { |
| 11871 | /* looks for the first argument. */ |
| 11872 | value_start = memchr(arg_start, '=', qs_end - arg_start); |
| 11873 | if (!value_start) { |
Thierry FOURNIER | 8be451c | 2015-05-20 15:28:12 +0200 | [diff] [blame] | 11874 | /* Check for wrapping. */ |
| 11875 | if (arg_start >= chunks[0] && |
Willy Tarreau | f662582 | 2015-12-27 14:51:01 +0100 | [diff] [blame] | 11876 | arg_start < chunks[1] && |
Thierry FOURNIER | 8be451c | 2015-05-20 15:28:12 +0200 | [diff] [blame] | 11877 | chunks[2]) { |
| 11878 | arg_start = chunks[2]; |
| 11879 | qs_end = chunks[3]; |
| 11880 | continue; |
| 11881 | } |
| 11882 | return 0; |
| 11883 | } |
| 11884 | break; |
| 11885 | } |
Willy Tarreau | 1ede1da | 2015-05-07 16:06:18 +0200 | [diff] [blame] | 11886 | value_start++; |
| 11887 | } |
Thierry FOURNIER | 8be451c | 2015-05-20 15:28:12 +0200 | [diff] [blame] | 11888 | else { |
| 11889 | /* Jump the argument length. */ |
Willy Tarreau | 1ede1da | 2015-05-07 16:06:18 +0200 | [diff] [blame] | 11890 | value_start = arg_start + url_param_name_l + 1; |
| 11891 | |
Thierry FOURNIER | 8be451c | 2015-05-20 15:28:12 +0200 | [diff] [blame] | 11892 | /* Check for pointer wrapping. */ |
| 11893 | if (fix_pointer_if_wrap(chunks, &value_start)) { |
| 11894 | /* Update the end pointer. */ |
| 11895 | qs_end = chunks[3]; |
| 11896 | |
| 11897 | /* Check for overflow. */ |
Willy Tarreau | f662582 | 2015-12-27 14:51:01 +0100 | [diff] [blame] | 11898 | if (value_start >= qs_end) |
Thierry FOURNIER | 8be451c | 2015-05-20 15:28:12 +0200 | [diff] [blame] | 11899 | return 0; |
| 11900 | } |
| 11901 | } |
| 11902 | |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 11903 | value_end = value_start; |
| 11904 | |
Thierry FOURNIER | 8be451c | 2015-05-20 15:28:12 +0200 | [diff] [blame] | 11905 | while (1) { |
| 11906 | while ((value_end < qs_end) && !is_param_delimiter(*value_end, delim)) |
| 11907 | value_end++; |
| 11908 | if (value_end < qs_end) |
| 11909 | break; |
| 11910 | /* process buffer wrapping. */ |
| 11911 | if (value_end >= chunks[0] && |
Willy Tarreau | f662582 | 2015-12-27 14:51:01 +0100 | [diff] [blame] | 11912 | value_end < chunks[1] && |
Thierry FOURNIER | 8be451c | 2015-05-20 15:28:12 +0200 | [diff] [blame] | 11913 | chunks[2]) { |
| 11914 | value_end = chunks[2]; |
| 11915 | qs_end = chunks[3]; |
| 11916 | continue; |
| 11917 | } |
| 11918 | break; |
| 11919 | } |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 11920 | |
Thierry FOURNIER | 8be451c | 2015-05-20 15:28:12 +0200 | [diff] [blame] | 11921 | *vstart = value_start; |
| 11922 | *vend = value_end; |
Cyril Bonté | ce1ef4d | 2015-11-26 21:39:56 +0100 | [diff] [blame] | 11923 | return 1; |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 11924 | } |
| 11925 | |
Thierry FOURNIER | 8be451c | 2015-05-20 15:28:12 +0200 | [diff] [blame] | 11926 | /* This scans a URL-encoded query string. It takes an optionally wrapping |
| 11927 | * string whose first contigous chunk has its beginning in ctx->a[0] and end |
| 11928 | * in ctx->a[1], and the optional second part in (ctx->a[2]..ctx->a[3]). The |
| 11929 | * pointers are updated for next iteration before leaving. |
Thierry FOURNIER | 4fdc74c | 2015-05-19 14:46:23 +0200 | [diff] [blame] | 11930 | */ |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 11931 | static int |
Thierry FOURNIER | 4fdc74c | 2015-05-19 14:46:23 +0200 | [diff] [blame] | 11932 | 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] | 11933 | { |
Thierry FOURNIER | 8be451c | 2015-05-20 15:28:12 +0200 | [diff] [blame] | 11934 | const char *vstart, *vend; |
| 11935 | struct chunk *temp; |
| 11936 | const char **chunks = (const char **)smp->ctx.a; |
bedis | 4c75cca | 2012-10-05 08:38:24 +0200 | [diff] [blame] | 11937 | |
Thierry FOURNIER | 8be451c | 2015-05-20 15:28:12 +0200 | [diff] [blame] | 11938 | if (!find_next_url_param(chunks, |
Thierry FOURNIER | 0948d41 | 2015-05-20 15:22:37 +0200 | [diff] [blame] | 11939 | name, name_len, |
Thierry FOURNIER | 8be451c | 2015-05-20 15:28:12 +0200 | [diff] [blame] | 11940 | &vstart, &vend, |
Thierry FOURNIER | 0948d41 | 2015-05-20 15:22:37 +0200 | [diff] [blame] | 11941 | delim)) |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 11942 | return 0; |
| 11943 | |
Thierry FOURNIER | 8be451c | 2015-05-20 15:28:12 +0200 | [diff] [blame] | 11944 | /* Create sample. If the value is contiguous, return the pointer as CONST, |
| 11945 | * if the value is wrapped, copy-it in a buffer. |
| 11946 | */ |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 11947 | smp->data.type = SMP_T_STR; |
Thierry FOURNIER | 8be451c | 2015-05-20 15:28:12 +0200 | [diff] [blame] | 11948 | if (chunks[2] && |
| 11949 | vstart >= chunks[0] && vstart <= chunks[1] && |
| 11950 | vend >= chunks[2] && vend <= chunks[3]) { |
| 11951 | /* Wrapped case. */ |
| 11952 | temp = get_trash_chunk(); |
| 11953 | memcpy(temp->str, vstart, chunks[1] - vstart); |
| 11954 | memcpy(temp->str + ( chunks[1] - vstart ), chunks[2], vend - chunks[2]); |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 11955 | smp->data.u.str.str = temp->str; |
| 11956 | smp->data.u.str.len = ( chunks[1] - vstart ) + ( vend - chunks[2] ); |
Thierry FOURNIER | 8be451c | 2015-05-20 15:28:12 +0200 | [diff] [blame] | 11957 | } else { |
| 11958 | /* Contiguous case. */ |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 11959 | smp->data.u.str.str = (char *)vstart; |
| 11960 | smp->data.u.str.len = vend - vstart; |
Thierry FOURNIER | 8be451c | 2015-05-20 15:28:12 +0200 | [diff] [blame] | 11961 | smp->flags = SMP_F_VOL_1ST | SMP_F_CONST; |
| 11962 | } |
| 11963 | |
| 11964 | /* Update context, check wrapping. */ |
| 11965 | chunks[0] = vend; |
| 11966 | if (chunks[2] && vend >= chunks[2] && vend <= chunks[3]) { |
| 11967 | chunks[1] = chunks[3]; |
| 11968 | chunks[2] = NULL; |
| 11969 | } |
Willy Tarreau | 1ede1da | 2015-05-07 16:06:18 +0200 | [diff] [blame] | 11970 | |
Thierry FOURNIER | 8be451c | 2015-05-20 15:28:12 +0200 | [diff] [blame] | 11971 | if (chunks[0] < chunks[1]) |
Willy Tarreau | 1ede1da | 2015-05-07 16:06:18 +0200 | [diff] [blame] | 11972 | smp->flags |= SMP_F_NOT_LAST; |
| 11973 | |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 11974 | return 1; |
| 11975 | } |
| 11976 | |
Thierry FOURNIER | 4fdc74c | 2015-05-19 14:46:23 +0200 | [diff] [blame] | 11977 | /* This function iterates over each parameter of the query string. It uses |
| 11978 | * 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] | 11979 | * parameter. Since it uses smp_fetch_param(), ctx->a[2..3] are both NULL. |
| 11980 | * An optional parameter name is passed in args[0], otherwise any parameter is |
| 11981 | * considered. It supports an optional delimiter argument for the beginning of |
| 11982 | * the string in args[1], which defaults to "?". |
Thierry FOURNIER | 4fdc74c | 2015-05-19 14:46:23 +0200 | [diff] [blame] | 11983 | */ |
| 11984 | static int |
| 11985 | smp_fetch_url_param(const struct arg *args, struct sample *smp, const char *kw, void *private) |
| 11986 | { |
| 11987 | struct http_msg *msg; |
| 11988 | char delim = '?'; |
| 11989 | const char *name; |
| 11990 | int name_len; |
| 11991 | |
Dragan Dosen | 26f77e5 | 2015-05-25 10:02:11 +0200 | [diff] [blame] | 11992 | if (!args || |
| 11993 | (args[0].type && args[0].type != ARGT_STR) || |
| 11994 | (args[1].type && args[1].type != ARGT_STR)) |
| 11995 | return 0; |
Thierry FOURNIER | 4fdc74c | 2015-05-19 14:46:23 +0200 | [diff] [blame] | 11996 | |
Dragan Dosen | 26f77e5 | 2015-05-25 10:02:11 +0200 | [diff] [blame] | 11997 | name = ""; |
| 11998 | name_len = 0; |
| 11999 | if (args->type == ARGT_STR) { |
| 12000 | name = args->data.str.str; |
| 12001 | name_len = args->data.str.len; |
| 12002 | } |
Thierry FOURNIER | 4fdc74c | 2015-05-19 14:46:23 +0200 | [diff] [blame] | 12003 | |
Dragan Dosen | 26f77e5 | 2015-05-25 10:02:11 +0200 | [diff] [blame] | 12004 | if (args[1].type) |
| 12005 | delim = *args[1].data.str.str; |
Thierry FOURNIER | 4fdc74c | 2015-05-19 14:46:23 +0200 | [diff] [blame] | 12006 | |
Dragan Dosen | 26f77e5 | 2015-05-25 10:02:11 +0200 | [diff] [blame] | 12007 | if (!smp->ctx.a[0]) { // first call, find the query string |
Thierry FOURNIER | 4fdc74c | 2015-05-19 14:46:23 +0200 | [diff] [blame] | 12008 | CHECK_HTTP_MESSAGE_FIRST(); |
| 12009 | |
| 12010 | msg = &smp->strm->txn->req; |
| 12011 | |
| 12012 | smp->ctx.a[0] = find_param_list(msg->chn->buf->p + msg->sl.rq.u, |
| 12013 | msg->sl.rq.u_l, delim); |
| 12014 | if (!smp->ctx.a[0]) |
| 12015 | return 0; |
| 12016 | |
| 12017 | 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] | 12018 | |
| 12019 | /* Assume that the context is filled with NULL pointer |
| 12020 | * before the first call. |
| 12021 | * smp->ctx.a[2] = NULL; |
| 12022 | * smp->ctx.a[3] = NULL; |
| 12023 | */ |
Thierry FOURNIER | 4fdc74c | 2015-05-19 14:46:23 +0200 | [diff] [blame] | 12024 | } |
| 12025 | |
| 12026 | return smp_fetch_param(delim, name, name_len, args, smp, kw, private); |
| 12027 | } |
| 12028 | |
Thierry FOURNIER | e28c499 | 2015-05-19 14:45:09 +0200 | [diff] [blame] | 12029 | /* This function iterates over each parameter of the body. This requires |
| 12030 | * 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] | 12031 | * ctx->a[0] and ctx->a[1] to store the beginning and end of the first |
| 12032 | * contigous part of the body, and optionally ctx->a[2..3] to reference the |
| 12033 | * optional second part if the body wraps at the end of the buffer. An optional |
| 12034 | * parameter name is passed in args[0], otherwise any parameter is considered. |
Thierry FOURNIER | e28c499 | 2015-05-19 14:45:09 +0200 | [diff] [blame] | 12035 | */ |
| 12036 | static int |
| 12037 | smp_fetch_body_param(const struct arg *args, struct sample *smp, const char *kw, void *private) |
| 12038 | { |
Thierry FOURNIER | e28c499 | 2015-05-19 14:45:09 +0200 | [diff] [blame] | 12039 | struct http_msg *msg; |
| 12040 | unsigned long len; |
| 12041 | unsigned long block1; |
| 12042 | char *body; |
| 12043 | const char *name; |
| 12044 | int name_len; |
| 12045 | |
| 12046 | if (!args || (args[0].type && args[0].type != ARGT_STR)) |
| 12047 | return 0; |
| 12048 | |
| 12049 | name = ""; |
| 12050 | name_len = 0; |
| 12051 | if (args[0].type == ARGT_STR) { |
| 12052 | name = args[0].data.str.str; |
| 12053 | name_len = args[0].data.str.len; |
| 12054 | } |
| 12055 | |
| 12056 | if (!smp->ctx.a[0]) { // first call, find the query string |
| 12057 | CHECK_HTTP_MESSAGE_FIRST(); |
| 12058 | |
| 12059 | if ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) |
Willy Tarreau | be508f1 | 2016-03-10 11:47:01 +0100 | [diff] [blame] | 12060 | msg = &smp->strm->txn->req; |
Thierry FOURNIER | e28c499 | 2015-05-19 14:45:09 +0200 | [diff] [blame] | 12061 | else |
Willy Tarreau | be508f1 | 2016-03-10 11:47:01 +0100 | [diff] [blame] | 12062 | msg = &smp->strm->txn->rsp; |
Thierry FOURNIER | e28c499 | 2015-05-19 14:45:09 +0200 | [diff] [blame] | 12063 | |
| 12064 | len = http_body_bytes(msg); |
| 12065 | body = b_ptr(msg->chn->buf, -http_data_rewind(msg)); |
| 12066 | |
| 12067 | block1 = len; |
| 12068 | if (block1 > msg->chn->buf->data + msg->chn->buf->size - body) |
| 12069 | block1 = msg->chn->buf->data + msg->chn->buf->size - body; |
| 12070 | |
| 12071 | if (block1 == len) { |
| 12072 | /* buffer is not wrapped (or empty) */ |
| 12073 | smp->ctx.a[0] = body; |
| 12074 | smp->ctx.a[1] = body + len; |
Thierry FOURNIER | 8be451c | 2015-05-20 15:28:12 +0200 | [diff] [blame] | 12075 | |
| 12076 | /* Assume that the context is filled with NULL pointer |
| 12077 | * before the first call. |
| 12078 | * smp->ctx.a[2] = NULL; |
| 12079 | * smp->ctx.a[3] = NULL; |
| 12080 | */ |
Thierry FOURNIER | e28c499 | 2015-05-19 14:45:09 +0200 | [diff] [blame] | 12081 | } |
| 12082 | else { |
| 12083 | /* buffer is wrapped, we need to defragment it */ |
| 12084 | smp->ctx.a[0] = body; |
| 12085 | smp->ctx.a[1] = body + block1; |
Thierry FOURNIER | 8be451c | 2015-05-20 15:28:12 +0200 | [diff] [blame] | 12086 | smp->ctx.a[2] = msg->chn->buf->data; |
| 12087 | smp->ctx.a[3] = msg->chn->buf->data + ( len - block1 ); |
Thierry FOURNIER | e28c499 | 2015-05-19 14:45:09 +0200 | [diff] [blame] | 12088 | } |
| 12089 | } |
| 12090 | return smp_fetch_param('&', name, name_len, args, smp, kw, private); |
| 12091 | } |
| 12092 | |
Willy Tarreau | a9fddca | 2012-07-31 07:51:48 +0200 | [diff] [blame] | 12093 | /* Return the signed integer value for the specified url parameter (see url_param |
| 12094 | * above). |
| 12095 | */ |
| 12096 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 12097 | 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] | 12098 | { |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 12099 | int ret = smp_fetch_url_param(args, smp, kw, private); |
Willy Tarreau | a9fddca | 2012-07-31 07:51:48 +0200 | [diff] [blame] | 12100 | |
| 12101 | if (ret > 0) { |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 12102 | smp->data.type = SMP_T_SINT; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 12103 | 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] | 12104 | } |
| 12105 | |
| 12106 | return ret; |
| 12107 | } |
| 12108 | |
Neil - HAProxy List | 39c63c5 | 2013-11-04 13:48:42 +0000 | [diff] [blame] | 12109 | /* This produces a 32-bit hash of the concatenation of the first occurrence of |
| 12110 | * the Host header followed by the path component if it begins with a slash ('/'). |
| 12111 | * This means that '*' will not be added, resulting in exactly the first Host |
| 12112 | * entry. If no Host header is found, then the path is used. The resulting value |
| 12113 | * is hashed using the url hash followed by a full avalanche hash and provides a |
| 12114 | * 32-bit integer value. This fetch is useful for tracking per-URL activity on |
| 12115 | * high-traffic sites without having to store whole paths. |
| 12116 | * this differs from the base32 functions in that it includes the url parameters |
| 12117 | * as well as the path |
| 12118 | */ |
| 12119 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 12120 | 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] | 12121 | { |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 12122 | struct http_txn *txn; |
Neil - HAProxy List | 39c63c5 | 2013-11-04 13:48:42 +0000 | [diff] [blame] | 12123 | struct hdr_ctx ctx; |
| 12124 | unsigned int hash = 0; |
| 12125 | char *ptr, *beg, *end; |
| 12126 | int len; |
| 12127 | |
| 12128 | CHECK_HTTP_MESSAGE_FIRST(); |
| 12129 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 12130 | txn = smp->strm->txn; |
Neil - HAProxy List | 39c63c5 | 2013-11-04 13:48:42 +0000 | [diff] [blame] | 12131 | ctx.idx = 0; |
Willy Tarreau | 877e78d | 2013-04-07 18:48:08 +0200 | [diff] [blame] | 12132 | 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] | 12133 | /* OK we have the header value in ctx.line+ctx.val for ctx.vlen bytes */ |
| 12134 | ptr = ctx.line + ctx.val; |
| 12135 | len = ctx.vlen; |
| 12136 | while (len--) |
| 12137 | hash = *(ptr++) + (hash << 6) + (hash << 16) - hash; |
| 12138 | } |
| 12139 | |
| 12140 | /* now retrieve the path */ |
Willy Tarreau | 877e78d | 2013-04-07 18:48:08 +0200 | [diff] [blame] | 12141 | 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] | 12142 | beg = http_get_path(txn); |
| 12143 | if (!beg) |
| 12144 | beg = end; |
| 12145 | |
| 12146 | for (ptr = beg; ptr < end ; ptr++); |
| 12147 | |
| 12148 | if (beg < ptr && *beg == '/') { |
| 12149 | while (beg < ptr) |
| 12150 | hash = *(beg++) + (hash << 6) + (hash << 16) - hash; |
| 12151 | } |
| 12152 | hash = full_hash(hash); |
| 12153 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 12154 | smp->data.type = SMP_T_SINT; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 12155 | smp->data.u.sint = hash; |
Neil - HAProxy List | 39c63c5 | 2013-11-04 13:48:42 +0000 | [diff] [blame] | 12156 | smp->flags = SMP_F_VOL_1ST; |
| 12157 | return 1; |
| 12158 | } |
| 12159 | |
| 12160 | /* This concatenates the source address with the 32-bit hash of the Host and |
| 12161 | * URL as returned by smp_fetch_base32(). The idea is to have per-source and |
| 12162 | * per-url counters. The result is a binary block from 8 to 20 bytes depending |
| 12163 | * on the source address length. The URL hash is stored before the address so |
| 12164 | * that in environments where IPv6 is insignificant, truncating the output to |
| 12165 | * 8 bytes would still work. |
| 12166 | */ |
| 12167 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 12168 | 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] | 12169 | { |
| 12170 | struct chunk *temp; |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 12171 | struct connection *cli_conn = objt_conn(smp->sess->origin); |
Neil - HAProxy List | 39c63c5 | 2013-11-04 13:48:42 +0000 | [diff] [blame] | 12172 | |
Dragan Dosen | db5af61 | 2016-06-16 11:23:01 +0200 | [diff] [blame] | 12173 | if (!cli_conn) |
| 12174 | return 0; |
| 12175 | |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 12176 | if (!smp_fetch_url32(args, smp, kw, private)) |
Neil - HAProxy List | 39c63c5 | 2013-11-04 13:48:42 +0000 | [diff] [blame] | 12177 | return 0; |
| 12178 | |
| 12179 | temp = get_trash_chunk(); |
Dragan Dosen | e5f4133 | 2016-06-16 11:08:08 +0200 | [diff] [blame] | 12180 | *(unsigned int *)temp->str = htonl(smp->data.u.sint); |
| 12181 | temp->len += sizeof(unsigned int); |
Neil - HAProxy List | 39c63c5 | 2013-11-04 13:48:42 +0000 | [diff] [blame] | 12182 | |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 12183 | switch (cli_conn->addr.from.ss_family) { |
Neil - HAProxy List | 39c63c5 | 2013-11-04 13:48:42 +0000 | [diff] [blame] | 12184 | case AF_INET: |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 12185 | 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] | 12186 | temp->len += 4; |
| 12187 | break; |
| 12188 | case AF_INET6: |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 12189 | 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] | 12190 | temp->len += 16; |
| 12191 | break; |
| 12192 | default: |
| 12193 | return 0; |
| 12194 | } |
| 12195 | |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 12196 | smp->data.u.str = *temp; |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 12197 | smp->data.type = SMP_T_BIN; |
Neil - HAProxy List | 39c63c5 | 2013-11-04 13:48:42 +0000 | [diff] [blame] | 12198 | return 1; |
| 12199 | } |
| 12200 | |
Willy Tarreau | 185b5c4 | 2012-04-26 15:11:51 +0200 | [diff] [blame] | 12201 | /* This function is used to validate the arguments passed to any "hdr" fetch |
| 12202 | * keyword. These keywords support an optional positive or negative occurrence |
| 12203 | * number. We must ensure that the number is greater than -MAX_HDR_HISTORY. It |
| 12204 | * is assumed that the types are already the correct ones. Returns 0 on error, |
| 12205 | * non-zero if OK. If <err> is not NULL, it will be filled with a pointer to an |
| 12206 | * error message in case of error, that the caller is responsible for freeing. |
| 12207 | * The initial location must either be freeable or NULL. |
| 12208 | */ |
Thierry FOURNIER | 49f45af | 2014-12-08 19:50:43 +0100 | [diff] [blame] | 12209 | int val_hdr(struct arg *arg, char **err_msg) |
Willy Tarreau | 185b5c4 | 2012-04-26 15:11:51 +0200 | [diff] [blame] | 12210 | { |
| 12211 | 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] | 12212 | memprintf(err_msg, "header occurrence must be >= %d", -MAX_HDR_HISTORY); |
Willy Tarreau | 185b5c4 | 2012-04-26 15:11:51 +0200 | [diff] [blame] | 12213 | return 0; |
| 12214 | } |
| 12215 | return 1; |
| 12216 | } |
| 12217 | |
Willy Tarreau | 276fae9 | 2013-07-25 14:36:01 +0200 | [diff] [blame] | 12218 | /* takes an UINT value on input supposed to represent the time since EPOCH, |
| 12219 | * adds an optional offset found in args[0] and emits a string representing |
| 12220 | * the date in RFC-1123/5322 format. |
| 12221 | */ |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 12222 | 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] | 12223 | { |
Cyril Bonté | f78d896 | 2016-01-22 19:40:28 +0100 | [diff] [blame] | 12224 | const char day[7][4] = { "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" }; |
Willy Tarreau | 276fae9 | 2013-07-25 14:36:01 +0200 | [diff] [blame] | 12225 | const char mon[12][4] = { "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" }; |
| 12226 | struct chunk *temp; |
| 12227 | struct tm *tm; |
Thierry FOURNIER | bf65cd4 | 2015-07-20 17:45:02 +0200 | [diff] [blame] | 12228 | /* 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] | 12229 | time_t curr_date = smp->data.u.sint & 0x007fffffffffffffLL; |
Willy Tarreau | 276fae9 | 2013-07-25 14:36:01 +0200 | [diff] [blame] | 12230 | |
| 12231 | /* add offset */ |
Thierry FOURNIER | bf65cd4 | 2015-07-20 17:45:02 +0200 | [diff] [blame] | 12232 | if (args && (args[0].type == ARGT_SINT)) |
Willy Tarreau | 276fae9 | 2013-07-25 14:36:01 +0200 | [diff] [blame] | 12233 | curr_date += args[0].data.sint; |
| 12234 | |
| 12235 | tm = gmtime(&curr_date); |
Thierry FOURNIER | fac9ccf | 2015-07-08 00:15:20 +0200 | [diff] [blame] | 12236 | if (!tm) |
| 12237 | return 0; |
Willy Tarreau | 276fae9 | 2013-07-25 14:36:01 +0200 | [diff] [blame] | 12238 | |
| 12239 | temp = get_trash_chunk(); |
| 12240 | temp->len = snprintf(temp->str, temp->size - temp->len, |
| 12241 | "%s, %02d %s %04d %02d:%02d:%02d GMT", |
| 12242 | day[tm->tm_wday], tm->tm_mday, mon[tm->tm_mon], 1900+tm->tm_year, |
| 12243 | tm->tm_hour, tm->tm_min, tm->tm_sec); |
| 12244 | |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 12245 | smp->data.u.str = *temp; |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 12246 | smp->data.type = SMP_T_STR; |
Willy Tarreau | 276fae9 | 2013-07-25 14:36:01 +0200 | [diff] [blame] | 12247 | return 1; |
| 12248 | } |
| 12249 | |
Thierry FOURNIER | ad90351 | 2014-04-11 17:51:01 +0200 | [diff] [blame] | 12250 | /* Match language range with language tag. RFC2616 14.4: |
| 12251 | * |
| 12252 | * A language-range matches a language-tag if it exactly equals |
| 12253 | * the tag, or if it exactly equals a prefix of the tag such |
| 12254 | * that the first tag character following the prefix is "-". |
| 12255 | * |
| 12256 | * Return 1 if the strings match, else return 0. |
| 12257 | */ |
| 12258 | static inline int language_range_match(const char *range, int range_len, |
| 12259 | const char *tag, int tag_len) |
| 12260 | { |
| 12261 | const char *end = range + range_len; |
| 12262 | const char *tend = tag + tag_len; |
| 12263 | while (range < end) { |
| 12264 | if (*range == '-' && tag == tend) |
| 12265 | return 1; |
| 12266 | if (*range != *tag || tag == tend) |
| 12267 | return 0; |
| 12268 | range++; |
| 12269 | tag++; |
| 12270 | } |
| 12271 | /* Return true only if the last char of the tag is matched. */ |
| 12272 | return tag == tend; |
| 12273 | } |
| 12274 | |
| 12275 | /* 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] | 12276 | 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] | 12277 | { |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 12278 | const char *al = smp->data.u.str.str; |
| 12279 | const char *end = al + smp->data.u.str.len; |
Thierry FOURNIER | ad90351 | 2014-04-11 17:51:01 +0200 | [diff] [blame] | 12280 | const char *token; |
| 12281 | int toklen; |
| 12282 | int qvalue; |
| 12283 | const char *str; |
| 12284 | const char *w; |
| 12285 | int best_q = 0; |
| 12286 | |
| 12287 | /* Set the constant to the sample, because the output of the |
| 12288 | * function will be peek in the constant configuration string. |
| 12289 | */ |
| 12290 | smp->flags |= SMP_F_CONST; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 12291 | smp->data.u.str.size = 0; |
| 12292 | smp->data.u.str.str = ""; |
| 12293 | smp->data.u.str.len = 0; |
Thierry FOURNIER | ad90351 | 2014-04-11 17:51:01 +0200 | [diff] [blame] | 12294 | |
| 12295 | /* Parse the accept language */ |
| 12296 | while (1) { |
| 12297 | |
| 12298 | /* Jump spaces, quit if the end is detected. */ |
Willy Tarreau | 506c69a | 2014-07-08 00:59:48 +0200 | [diff] [blame] | 12299 | while (al < end && isspace((unsigned char)*al)) |
Thierry FOURNIER | ad90351 | 2014-04-11 17:51:01 +0200 | [diff] [blame] | 12300 | al++; |
| 12301 | if (al >= end) |
| 12302 | break; |
| 12303 | |
| 12304 | /* Start of the fisrt word. */ |
| 12305 | token = al; |
| 12306 | |
| 12307 | /* Look for separator: isspace(), ',' or ';'. Next value if 0 length word. */ |
Willy Tarreau | 506c69a | 2014-07-08 00:59:48 +0200 | [diff] [blame] | 12308 | while (al < end && *al != ';' && *al != ',' && !isspace((unsigned char)*al)) |
Thierry FOURNIER | ad90351 | 2014-04-11 17:51:01 +0200 | [diff] [blame] | 12309 | al++; |
| 12310 | if (al == token) |
| 12311 | goto expect_comma; |
| 12312 | |
| 12313 | /* Length of the token. */ |
| 12314 | toklen = al - token; |
| 12315 | qvalue = 1000; |
| 12316 | |
| 12317 | /* Check if the token exists in the list. If the token not exists, |
| 12318 | * jump to the next token. |
| 12319 | */ |
| 12320 | str = args[0].data.str.str; |
| 12321 | w = str; |
| 12322 | while (1) { |
| 12323 | if (*str == ';' || *str == '\0') { |
| 12324 | if (language_range_match(token, toklen, w, str-w)) |
| 12325 | goto look_for_q; |
| 12326 | if (*str == '\0') |
| 12327 | goto expect_comma; |
| 12328 | w = str + 1; |
| 12329 | } |
| 12330 | str++; |
| 12331 | } |
| 12332 | goto expect_comma; |
| 12333 | |
| 12334 | look_for_q: |
| 12335 | |
| 12336 | /* Jump spaces, quit if the end is detected. */ |
Willy Tarreau | 506c69a | 2014-07-08 00:59:48 +0200 | [diff] [blame] | 12337 | while (al < end && isspace((unsigned char)*al)) |
Thierry FOURNIER | ad90351 | 2014-04-11 17:51:01 +0200 | [diff] [blame] | 12338 | al++; |
| 12339 | if (al >= end) |
| 12340 | goto process_value; |
| 12341 | |
| 12342 | /* If ',' is found, process the result */ |
| 12343 | if (*al == ',') |
| 12344 | goto process_value; |
| 12345 | |
| 12346 | /* If the character is different from ';', look |
| 12347 | * for the end of the header part in best effort. |
| 12348 | */ |
| 12349 | if (*al != ';') |
| 12350 | goto expect_comma; |
| 12351 | |
| 12352 | /* Assumes that the char is ';', now expect "q=". */ |
| 12353 | al++; |
| 12354 | |
| 12355 | /* Jump spaces, process value if the end is detected. */ |
Willy Tarreau | 506c69a | 2014-07-08 00:59:48 +0200 | [diff] [blame] | 12356 | while (al < end && isspace((unsigned char)*al)) |
Thierry FOURNIER | ad90351 | 2014-04-11 17:51:01 +0200 | [diff] [blame] | 12357 | al++; |
| 12358 | if (al >= end) |
| 12359 | goto process_value; |
| 12360 | |
| 12361 | /* Expect 'q'. If no 'q', continue in best effort */ |
| 12362 | if (*al != 'q') |
| 12363 | goto process_value; |
| 12364 | al++; |
| 12365 | |
| 12366 | /* Jump spaces, process value if the end is detected. */ |
Willy Tarreau | 506c69a | 2014-07-08 00:59:48 +0200 | [diff] [blame] | 12367 | while (al < end && isspace((unsigned char)*al)) |
Thierry FOURNIER | ad90351 | 2014-04-11 17:51:01 +0200 | [diff] [blame] | 12368 | al++; |
| 12369 | if (al >= end) |
| 12370 | goto process_value; |
| 12371 | |
| 12372 | /* Expect '='. If no '=', continue in best effort */ |
| 12373 | if (*al != '=') |
| 12374 | goto process_value; |
| 12375 | al++; |
| 12376 | |
| 12377 | /* Jump spaces, process value if the end is detected. */ |
Willy Tarreau | 506c69a | 2014-07-08 00:59:48 +0200 | [diff] [blame] | 12378 | while (al < end && isspace((unsigned char)*al)) |
Thierry FOURNIER | ad90351 | 2014-04-11 17:51:01 +0200 | [diff] [blame] | 12379 | al++; |
| 12380 | if (al >= end) |
| 12381 | goto process_value; |
| 12382 | |
| 12383 | /* Parse the q value. */ |
| 12384 | qvalue = parse_qvalue(al, &al); |
| 12385 | |
| 12386 | process_value: |
| 12387 | |
| 12388 | /* If the new q value is the best q value, then store the associated |
| 12389 | * language in the response. If qvalue is the biggest value (1000), |
| 12390 | * break the process. |
| 12391 | */ |
| 12392 | if (qvalue > best_q) { |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 12393 | smp->data.u.str.str = (char *)w; |
| 12394 | smp->data.u.str.len = str - w; |
Thierry FOURNIER | ad90351 | 2014-04-11 17:51:01 +0200 | [diff] [blame] | 12395 | if (qvalue >= 1000) |
| 12396 | break; |
| 12397 | best_q = qvalue; |
| 12398 | } |
| 12399 | |
| 12400 | expect_comma: |
| 12401 | |
| 12402 | /* Expect comma or end. If the end is detected, quit the loop. */ |
| 12403 | while (al < end && *al != ',') |
| 12404 | al++; |
| 12405 | if (al >= end) |
| 12406 | break; |
| 12407 | |
| 12408 | /* Comma is found, jump it and restart the analyzer. */ |
| 12409 | al++; |
| 12410 | } |
| 12411 | |
| 12412 | /* Set default value if required. */ |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 12413 | if (smp->data.u.str.len == 0 && args[1].type == ARGT_STR) { |
| 12414 | smp->data.u.str.str = args[1].data.str.str; |
| 12415 | smp->data.u.str.len = args[1].data.str.len; |
Thierry FOURNIER | ad90351 | 2014-04-11 17:51:01 +0200 | [diff] [blame] | 12416 | } |
| 12417 | |
| 12418 | /* Return true only if a matching language was found. */ |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 12419 | return smp->data.u.str.len != 0; |
Thierry FOURNIER | ad90351 | 2014-04-11 17:51:01 +0200 | [diff] [blame] | 12420 | } |
| 12421 | |
Thierry FOURNIER | 82ff3c9 | 2015-05-07 15:46:20 +0200 | [diff] [blame] | 12422 | /* This fetch url-decode any input string. */ |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 12423 | 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] | 12424 | { |
| 12425 | /* If the constant flag is set or if not size is avalaible at |
| 12426 | * the end of the buffer, copy the string in other buffer |
| 12427 | * before decoding. |
| 12428 | */ |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 12429 | 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] | 12430 | struct chunk *str = get_trash_chunk(); |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 12431 | memcpy(str->str, smp->data.u.str.str, smp->data.u.str.len); |
| 12432 | smp->data.u.str.str = str->str; |
| 12433 | smp->data.u.str.size = str->size; |
Thierry FOURNIER | 82ff3c9 | 2015-05-07 15:46:20 +0200 | [diff] [blame] | 12434 | smp->flags &= ~SMP_F_CONST; |
| 12435 | } |
| 12436 | |
| 12437 | /* Add final \0 required by url_decode(), and convert the input string. */ |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 12438 | smp->data.u.str.str[smp->data.u.str.len] = '\0'; |
| 12439 | smp->data.u.str.len = url_decode(smp->data.u.str.str); |
Thierry FOURNIER | 82ff3c9 | 2015-05-07 15:46:20 +0200 | [diff] [blame] | 12440 | return 1; |
| 12441 | } |
| 12442 | |
Thierry FOURNIER | 35ab275 | 2015-05-28 13:22:03 +0200 | [diff] [blame] | 12443 | static int smp_conv_req_capture(const struct arg *args, struct sample *smp, void *private) |
| 12444 | { |
| 12445 | struct proxy *fe = strm_fe(smp->strm); |
| 12446 | int idx, i; |
| 12447 | struct cap_hdr *hdr; |
| 12448 | int len; |
| 12449 | |
Thierry FOURNIER | bf65cd4 | 2015-07-20 17:45:02 +0200 | [diff] [blame] | 12450 | if (!args || args->type != ARGT_SINT) |
Thierry FOURNIER | 35ab275 | 2015-05-28 13:22:03 +0200 | [diff] [blame] | 12451 | return 0; |
| 12452 | |
Thierry FOURNIER | bf65cd4 | 2015-07-20 17:45:02 +0200 | [diff] [blame] | 12453 | idx = args->data.sint; |
Thierry FOURNIER | 35ab275 | 2015-05-28 13:22:03 +0200 | [diff] [blame] | 12454 | |
| 12455 | /* Check the availibity of the capture id. */ |
| 12456 | if (idx > fe->nb_req_cap - 1) |
| 12457 | return 0; |
| 12458 | |
| 12459 | /* Look for the original configuration. */ |
| 12460 | for (hdr = fe->req_cap, i = fe->nb_req_cap - 1; |
| 12461 | hdr != NULL && i != idx ; |
| 12462 | i--, hdr = hdr->next); |
| 12463 | if (!hdr) |
| 12464 | return 0; |
| 12465 | |
| 12466 | /* check for the memory allocation */ |
| 12467 | if (smp->strm->req_cap[hdr->index] == NULL) |
| 12468 | smp->strm->req_cap[hdr->index] = pool_alloc2(hdr->pool); |
| 12469 | if (smp->strm->req_cap[hdr->index] == NULL) |
| 12470 | return 0; |
| 12471 | |
| 12472 | /* Check length. */ |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 12473 | len = smp->data.u.str.len; |
Thierry FOURNIER | 35ab275 | 2015-05-28 13:22:03 +0200 | [diff] [blame] | 12474 | if (len > hdr->len) |
| 12475 | len = hdr->len; |
| 12476 | |
| 12477 | /* Capture input data. */ |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 12478 | memcpy(smp->strm->req_cap[idx], smp->data.u.str.str, len); |
Thierry FOURNIER | 35ab275 | 2015-05-28 13:22:03 +0200 | [diff] [blame] | 12479 | smp->strm->req_cap[idx][len] = '\0'; |
| 12480 | |
| 12481 | return 1; |
| 12482 | } |
| 12483 | |
| 12484 | static int smp_conv_res_capture(const struct arg *args, struct sample *smp, void *private) |
| 12485 | { |
| 12486 | struct proxy *fe = strm_fe(smp->strm); |
| 12487 | int idx, i; |
| 12488 | struct cap_hdr *hdr; |
| 12489 | int len; |
| 12490 | |
Thierry FOURNIER | bf65cd4 | 2015-07-20 17:45:02 +0200 | [diff] [blame] | 12491 | if (!args || args->type != ARGT_SINT) |
Thierry FOURNIER | 35ab275 | 2015-05-28 13:22:03 +0200 | [diff] [blame] | 12492 | return 0; |
| 12493 | |
Thierry FOURNIER | bf65cd4 | 2015-07-20 17:45:02 +0200 | [diff] [blame] | 12494 | idx = args->data.sint; |
Thierry FOURNIER | 35ab275 | 2015-05-28 13:22:03 +0200 | [diff] [blame] | 12495 | |
| 12496 | /* Check the availibity of the capture id. */ |
| 12497 | if (idx > fe->nb_rsp_cap - 1) |
| 12498 | return 0; |
| 12499 | |
| 12500 | /* Look for the original configuration. */ |
| 12501 | for (hdr = fe->rsp_cap, i = fe->nb_rsp_cap - 1; |
| 12502 | hdr != NULL && i != idx ; |
| 12503 | i--, hdr = hdr->next); |
| 12504 | if (!hdr) |
| 12505 | return 0; |
| 12506 | |
| 12507 | /* check for the memory allocation */ |
| 12508 | if (smp->strm->res_cap[hdr->index] == NULL) |
| 12509 | smp->strm->res_cap[hdr->index] = pool_alloc2(hdr->pool); |
| 12510 | if (smp->strm->res_cap[hdr->index] == NULL) |
| 12511 | return 0; |
| 12512 | |
| 12513 | /* Check length. */ |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 12514 | len = smp->data.u.str.len; |
Thierry FOURNIER | 35ab275 | 2015-05-28 13:22:03 +0200 | [diff] [blame] | 12515 | if (len > hdr->len) |
| 12516 | len = hdr->len; |
| 12517 | |
| 12518 | /* Capture input data. */ |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 12519 | memcpy(smp->strm->res_cap[idx], smp->data.u.str.str, len); |
Thierry FOURNIER | 35ab275 | 2015-05-28 13:22:03 +0200 | [diff] [blame] | 12520 | smp->strm->res_cap[idx][len] = '\0'; |
| 12521 | |
| 12522 | return 1; |
| 12523 | } |
| 12524 | |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 12525 | /* 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] | 12526 | * takes the string from the variable 'replace' with length 'len', then modifies |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 12527 | * the relevant part of the request line accordingly. Then it updates various |
| 12528 | * pointers to the next elements which were moved, and the total buffer length. |
| 12529 | * 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] | 12530 | * parse_set_req_line(). It returns 0 in case of success, -1 in case of internal |
| 12531 | * error, though this can be revisited when this code is finally exploited. |
| 12532 | * |
| 12533 | * 'action' can be '0' to replace method, '1' to replace path, '2' to replace |
| 12534 | * query string and 3 to replace uri. |
| 12535 | * |
| 12536 | * In query string case, the mark question '?' must be set at the start of the |
| 12537 | * string by the caller, event if the replacement query string is empty. |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 12538 | */ |
Thierry FOURNIER | b77aece | 2015-03-14 13:55:46 +0100 | [diff] [blame] | 12539 | int http_replace_req_line(int action, const char *replace, int len, |
Willy Tarreau | 987e3fb | 2015-04-04 01:09:08 +0200 | [diff] [blame] | 12540 | struct proxy *px, struct stream *s) |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 12541 | { |
Willy Tarreau | 987e3fb | 2015-04-04 01:09:08 +0200 | [diff] [blame] | 12542 | struct http_txn *txn = s->txn; |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 12543 | char *cur_ptr, *cur_end; |
Thierry FOURNIER | b77aece | 2015-03-14 13:55:46 +0100 | [diff] [blame] | 12544 | int offset = 0; |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 12545 | int delta; |
| 12546 | |
Thierry FOURNIER | b77aece | 2015-03-14 13:55:46 +0100 | [diff] [blame] | 12547 | switch (action) { |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 12548 | case 0: // method |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 12549 | cur_ptr = s->req.buf->p; |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 12550 | cur_end = cur_ptr + txn->req.sl.rq.m_l; |
| 12551 | |
| 12552 | /* adjust req line offsets and lengths */ |
Thierry FOURNIER | b77aece | 2015-03-14 13:55:46 +0100 | [diff] [blame] | 12553 | delta = len - offset - (cur_end - cur_ptr); |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 12554 | txn->req.sl.rq.m_l += delta; |
| 12555 | txn->req.sl.rq.u += delta; |
| 12556 | txn->req.sl.rq.v += delta; |
| 12557 | break; |
| 12558 | |
| 12559 | case 1: // path |
| 12560 | cur_ptr = http_get_path(txn); |
| 12561 | if (!cur_ptr) |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 12562 | cur_ptr = s->req.buf->p + txn->req.sl.rq.u; |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 12563 | |
| 12564 | cur_end = cur_ptr; |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 12565 | 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] | 12566 | cur_end++; |
| 12567 | |
| 12568 | /* adjust req line offsets and lengths */ |
Thierry FOURNIER | b77aece | 2015-03-14 13:55:46 +0100 | [diff] [blame] | 12569 | delta = len - offset - (cur_end - cur_ptr); |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 12570 | txn->req.sl.rq.u_l += delta; |
| 12571 | txn->req.sl.rq.v += delta; |
| 12572 | break; |
| 12573 | |
| 12574 | case 2: // query |
Thierry FOURNIER | b77aece | 2015-03-14 13:55:46 +0100 | [diff] [blame] | 12575 | offset = 1; |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 12576 | cur_ptr = s->req.buf->p + txn->req.sl.rq.u; |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 12577 | cur_end = cur_ptr + txn->req.sl.rq.u_l; |
| 12578 | while (cur_ptr < cur_end && *cur_ptr != '?') |
| 12579 | cur_ptr++; |
| 12580 | |
| 12581 | /* skip the question mark or indicate that we must insert it |
| 12582 | * (but only if the format string is not empty then). |
| 12583 | */ |
| 12584 | if (cur_ptr < cur_end) |
| 12585 | cur_ptr++; |
Thierry FOURNIER | b77aece | 2015-03-14 13:55:46 +0100 | [diff] [blame] | 12586 | else if (len > 1) |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 12587 | offset = 0; |
| 12588 | |
| 12589 | /* adjust req line offsets and lengths */ |
Thierry FOURNIER | b77aece | 2015-03-14 13:55:46 +0100 | [diff] [blame] | 12590 | delta = len - offset - (cur_end - cur_ptr); |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 12591 | txn->req.sl.rq.u_l += delta; |
| 12592 | txn->req.sl.rq.v += delta; |
| 12593 | break; |
| 12594 | |
| 12595 | case 3: // uri |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 12596 | cur_ptr = s->req.buf->p + txn->req.sl.rq.u; |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 12597 | cur_end = cur_ptr + txn->req.sl.rq.u_l; |
| 12598 | |
| 12599 | /* adjust req line offsets and lengths */ |
Thierry FOURNIER | b77aece | 2015-03-14 13:55:46 +0100 | [diff] [blame] | 12600 | delta = len - offset - (cur_end - cur_ptr); |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 12601 | txn->req.sl.rq.u_l += delta; |
| 12602 | txn->req.sl.rq.v += delta; |
| 12603 | break; |
| 12604 | |
| 12605 | default: |
Thierry FOURNIER | b77aece | 2015-03-14 13:55:46 +0100 | [diff] [blame] | 12606 | return -1; |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 12607 | } |
| 12608 | |
| 12609 | /* commit changes and adjust end of message */ |
Thierry FOURNIER | b77aece | 2015-03-14 13:55:46 +0100 | [diff] [blame] | 12610 | 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] | 12611 | txn->req.sl.rq.l += delta; |
| 12612 | txn->hdr_idx.v[0].len += delta; |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 12613 | http_msg_move_end(&txn->req, delta); |
Thierry FOURNIER | b77aece | 2015-03-14 13:55:46 +0100 | [diff] [blame] | 12614 | return 0; |
| 12615 | } |
| 12616 | |
Thierry FOURNIER | 35d70ef | 2015-08-26 16:21:56 +0200 | [diff] [blame] | 12617 | /* This function replace the HTTP status code and the associated message. The |
| 12618 | * variable <status> contains the new status code. This function never fails. |
| 12619 | */ |
Robin H. Johnson | 52f5db2 | 2017-01-01 13:10:52 -0800 | [diff] [blame] | 12620 | void http_set_status(unsigned int status, const char *reason, struct stream *s) |
Thierry FOURNIER | 35d70ef | 2015-08-26 16:21:56 +0200 | [diff] [blame] | 12621 | { |
| 12622 | struct http_txn *txn = s->txn; |
| 12623 | char *cur_ptr, *cur_end; |
| 12624 | int delta; |
| 12625 | char *res; |
| 12626 | int c_l; |
Robin H. Johnson | 52f5db2 | 2017-01-01 13:10:52 -0800 | [diff] [blame] | 12627 | const char *msg = reason; |
Thierry FOURNIER | 35d70ef | 2015-08-26 16:21:56 +0200 | [diff] [blame] | 12628 | int msg_len; |
| 12629 | |
| 12630 | chunk_reset(&trash); |
| 12631 | |
| 12632 | res = ultoa_o(status, trash.str, trash.size); |
| 12633 | c_l = res - trash.str; |
| 12634 | |
| 12635 | trash.str[c_l] = ' '; |
| 12636 | trash.len = c_l + 1; |
| 12637 | |
Robin H. Johnson | 52f5db2 | 2017-01-01 13:10:52 -0800 | [diff] [blame] | 12638 | /* Do we have a custom reason format string? */ |
| 12639 | if (msg == NULL) |
| 12640 | msg = get_reason(status); |
Thierry FOURNIER | 35d70ef | 2015-08-26 16:21:56 +0200 | [diff] [blame] | 12641 | msg_len = strlen(msg); |
Thierry FOURNIER | 35d70ef | 2015-08-26 16:21:56 +0200 | [diff] [blame] | 12642 | strncpy(&trash.str[trash.len], msg, trash.size - trash.len); |
| 12643 | trash.len += msg_len; |
| 12644 | |
| 12645 | cur_ptr = s->res.buf->p + txn->rsp.sl.st.c; |
| 12646 | cur_end = s->res.buf->p + txn->rsp.sl.st.r + txn->rsp.sl.st.r_l; |
| 12647 | |
| 12648 | /* commit changes and adjust message */ |
| 12649 | delta = buffer_replace2(s->res.buf, cur_ptr, cur_end, trash.str, trash.len); |
| 12650 | |
| 12651 | /* adjust res line offsets and lengths */ |
| 12652 | txn->rsp.sl.st.r += c_l - txn->rsp.sl.st.c_l; |
| 12653 | txn->rsp.sl.st.c_l = c_l; |
| 12654 | txn->rsp.sl.st.r_l = msg_len; |
| 12655 | |
| 12656 | delta = trash.len - (cur_end - cur_ptr); |
| 12657 | txn->rsp.sl.st.l += delta; |
| 12658 | txn->hdr_idx.v[0].len += delta; |
| 12659 | http_msg_move_end(&txn->rsp, delta); |
| 12660 | } |
| 12661 | |
Thierry FOURNIER | b77aece | 2015-03-14 13:55:46 +0100 | [diff] [blame] | 12662 | /* This function executes one of the set-{method,path,query,uri} actions. It |
| 12663 | * 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] | 12664 | * the action to be performed in <http.action>, previously filled by function |
Thierry FOURNIER | b77aece | 2015-03-14 13:55:46 +0100 | [diff] [blame] | 12665 | * parse_set_req_line(). The replacement action is excuted by the function |
Thierry FOURNIER | 3f4bc65 | 2015-08-26 16:23:34 +0200 | [diff] [blame] | 12666 | * http_action_set_req_line(). It always returns ACT_RET_CONT. If an error |
| 12667 | * occurs the action is canceled, but the rule processing continue. |
Thierry FOURNIER | b77aece | 2015-03-14 13:55:46 +0100 | [diff] [blame] | 12668 | */ |
Thierry FOURNIER | 24ff6c6 | 2015-08-06 08:52:53 +0200 | [diff] [blame] | 12669 | 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] | 12670 | struct session *sess, struct stream *s, int flags) |
Thierry FOURNIER | b77aece | 2015-03-14 13:55:46 +0100 | [diff] [blame] | 12671 | { |
| 12672 | chunk_reset(&trash); |
| 12673 | |
| 12674 | /* If we have to create a query string, prepare a '?'. */ |
Thierry FOURNIER | 8855a92 | 2015-07-31 08:54:25 +0200 | [diff] [blame] | 12675 | if (rule->arg.http.action == 2) |
Thierry FOURNIER | b77aece | 2015-03-14 13:55:46 +0100 | [diff] [blame] | 12676 | trash.str[trash.len++] = '?'; |
Thierry FOURNIER | 8855a92 | 2015-07-31 08:54:25 +0200 | [diff] [blame] | 12677 | 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] | 12678 | |
Thierry FOURNIER | 8855a92 | 2015-07-31 08:54:25 +0200 | [diff] [blame] | 12679 | 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] | 12680 | return ACT_RET_CONT; |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 12681 | } |
| 12682 | |
Thierry FOURNIER | 35d70ef | 2015-08-26 16:21:56 +0200 | [diff] [blame] | 12683 | /* This function is just a compliant action wrapper for "set-status". */ |
| 12684 | 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] | 12685 | struct session *sess, struct stream *s, int flags) |
Thierry FOURNIER | 35d70ef | 2015-08-26 16:21:56 +0200 | [diff] [blame] | 12686 | { |
Robin H. Johnson | 52f5db2 | 2017-01-01 13:10:52 -0800 | [diff] [blame] | 12687 | http_set_status(rule->arg.status.code, rule->arg.status.reason, s); |
Thierry FOURNIER | 35d70ef | 2015-08-26 16:21:56 +0200 | [diff] [blame] | 12688 | return ACT_RET_CONT; |
| 12689 | } |
| 12690 | |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 12691 | /* parse an http-request action among : |
| 12692 | * set-method |
| 12693 | * set-path |
| 12694 | * set-query |
| 12695 | * set-uri |
| 12696 | * |
| 12697 | * All of them accept a single argument of type string representing a log-format. |
| 12698 | * The resulting rule makes use of arg->act.p[0..1] to store the log-format list |
| 12699 | * 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] | 12700 | * 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] | 12701 | */ |
Thierry FOURNIER | afa8049 | 2015-08-19 09:04:15 +0200 | [diff] [blame] | 12702 | enum act_parse_ret parse_set_req_line(const char **args, int *orig_arg, struct proxy *px, |
| 12703 | struct act_rule *rule, char **err) |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 12704 | { |
| 12705 | int cur_arg = *orig_arg; |
| 12706 | |
Thierry FOURNIER | 4214873 | 2015-09-02 17:17:33 +0200 | [diff] [blame] | 12707 | rule->action = ACT_CUSTOM; |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 12708 | |
| 12709 | switch (args[0][4]) { |
| 12710 | case 'm' : |
Thierry FOURNIER | 8855a92 | 2015-07-31 08:54:25 +0200 | [diff] [blame] | 12711 | rule->arg.http.action = 0; |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 12712 | rule->action_ptr = http_action_set_req_line; |
| 12713 | break; |
| 12714 | case 'p' : |
Thierry FOURNIER | 8855a92 | 2015-07-31 08:54:25 +0200 | [diff] [blame] | 12715 | rule->arg.http.action = 1; |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 12716 | rule->action_ptr = http_action_set_req_line; |
| 12717 | break; |
| 12718 | case 'q' : |
Thierry FOURNIER | 8855a92 | 2015-07-31 08:54:25 +0200 | [diff] [blame] | 12719 | rule->arg.http.action = 2; |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 12720 | rule->action_ptr = http_action_set_req_line; |
| 12721 | break; |
| 12722 | case 'u' : |
Thierry FOURNIER | 8855a92 | 2015-07-31 08:54:25 +0200 | [diff] [blame] | 12723 | rule->arg.http.action = 3; |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 12724 | rule->action_ptr = http_action_set_req_line; |
| 12725 | break; |
| 12726 | default: |
| 12727 | memprintf(err, "internal error: unhandled action '%s'", args[0]); |
Thierry FOURNIER | afa8049 | 2015-08-19 09:04:15 +0200 | [diff] [blame] | 12728 | return ACT_RET_PRS_ERR; |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 12729 | } |
| 12730 | |
| 12731 | if (!*args[cur_arg] || |
| 12732 | (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) { |
| 12733 | memprintf(err, "expects exactly 1 argument <format>"); |
Thierry FOURNIER | afa8049 | 2015-08-19 09:04:15 +0200 | [diff] [blame] | 12734 | return ACT_RET_PRS_ERR; |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 12735 | } |
| 12736 | |
Thierry FOURNIER | 8855a92 | 2015-07-31 08:54:25 +0200 | [diff] [blame] | 12737 | LIST_INIT(&rule->arg.http.logfmt); |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 12738 | proxy->conf.args.ctx = ARGC_HRQ; |
Thierry FOURNIER / OZON.IO | 59fd511 | 2016-11-22 23:50:02 +0100 | [diff] [blame] | 12739 | 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] | 12740 | (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] | 12741 | return ACT_RET_PRS_ERR; |
| 12742 | } |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 12743 | |
| 12744 | (*orig_arg)++; |
Thierry FOURNIER | afa8049 | 2015-08-19 09:04:15 +0200 | [diff] [blame] | 12745 | return ACT_RET_PRS_OK; |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 12746 | } |
| 12747 | |
Thierry FOURNIER | 35d70ef | 2015-08-26 16:21:56 +0200 | [diff] [blame] | 12748 | /* parse set-status action: |
| 12749 | * This action accepts a single argument of type int representing |
| 12750 | * an http status code. It returns ACT_RET_PRS_OK on success, |
| 12751 | * ACT_RET_PRS_ERR on error. |
| 12752 | */ |
| 12753 | enum act_parse_ret parse_http_set_status(const char **args, int *orig_arg, struct proxy *px, |
| 12754 | struct act_rule *rule, char **err) |
| 12755 | { |
| 12756 | char *error; |
| 12757 | |
Thierry FOURNIER | 4214873 | 2015-09-02 17:17:33 +0200 | [diff] [blame] | 12758 | rule->action = ACT_CUSTOM; |
Thierry FOURNIER | 35d70ef | 2015-08-26 16:21:56 +0200 | [diff] [blame] | 12759 | rule->action_ptr = action_http_set_status; |
| 12760 | |
| 12761 | /* Check if an argument is available */ |
| 12762 | if (!*args[*orig_arg]) { |
Robin H. Johnson | 52f5db2 | 2017-01-01 13:10:52 -0800 | [diff] [blame] | 12763 | memprintf(err, "expects 1 argument: <status>; or 3 arguments: <status> reason <fmt>"); |
Thierry FOURNIER | 35d70ef | 2015-08-26 16:21:56 +0200 | [diff] [blame] | 12764 | return ACT_RET_PRS_ERR; |
| 12765 | } |
| 12766 | |
| 12767 | /* convert status code as integer */ |
| 12768 | rule->arg.status.code = strtol(args[*orig_arg], &error, 10); |
| 12769 | if (*error != '\0' || rule->arg.status.code < 100 || rule->arg.status.code > 999) { |
| 12770 | memprintf(err, "expects an integer status code between 100 and 999"); |
| 12771 | return ACT_RET_PRS_ERR; |
| 12772 | } |
| 12773 | |
| 12774 | (*orig_arg)++; |
Robin H. Johnson | 52f5db2 | 2017-01-01 13:10:52 -0800 | [diff] [blame] | 12775 | |
| 12776 | /* set custom reason string */ |
| 12777 | rule->arg.status.reason = NULL; // If null, we use the default reason for the status code. |
| 12778 | if (*args[*orig_arg] && strcmp(args[*orig_arg], "reason") == 0 && |
| 12779 | (*args[*orig_arg + 1] && strcmp(args[*orig_arg + 1], "if") != 0 && strcmp(args[*orig_arg + 1], "unless") != 0)) { |
| 12780 | (*orig_arg)++; |
| 12781 | rule->arg.status.reason = strdup(args[*orig_arg]); |
| 12782 | (*orig_arg)++; |
| 12783 | } |
| 12784 | |
Thierry FOURNIER | 35d70ef | 2015-08-26 16:21:56 +0200 | [diff] [blame] | 12785 | return ACT_RET_PRS_OK; |
| 12786 | } |
| 12787 | |
Willy Tarreau | a9083d0 | 2015-05-08 15:27:59 +0200 | [diff] [blame] | 12788 | /* This function executes the "capture" action. It executes a fetch expression, |
| 12789 | * turns the result into a string and puts it in a capture slot. It always |
| 12790 | * returns 1. If an error occurs the action is cancelled, but the rule |
| 12791 | * processing continues. |
| 12792 | */ |
Thierry FOURNIER | 24ff6c6 | 2015-08-06 08:52:53 +0200 | [diff] [blame] | 12793 | 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] | 12794 | struct session *sess, struct stream *s, int flags) |
Willy Tarreau | a9083d0 | 2015-05-08 15:27:59 +0200 | [diff] [blame] | 12795 | { |
Willy Tarreau | a9083d0 | 2015-05-08 15:27:59 +0200 | [diff] [blame] | 12796 | struct sample *key; |
Thierry FOURNIER | 32b1500 | 2015-07-31 08:56:16 +0200 | [diff] [blame] | 12797 | struct cap_hdr *h = rule->arg.cap.hdr; |
Willy Tarreau | a9083d0 | 2015-05-08 15:27:59 +0200 | [diff] [blame] | 12798 | char **cap = s->req_cap; |
| 12799 | int len; |
| 12800 | |
Thierry FOURNIER | 32b1500 | 2015-07-31 08:56:16 +0200 | [diff] [blame] | 12801 | 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] | 12802 | if (!key) |
Thierry FOURNIER | 24ff6c6 | 2015-08-06 08:52:53 +0200 | [diff] [blame] | 12803 | return ACT_RET_CONT; |
Willy Tarreau | a9083d0 | 2015-05-08 15:27:59 +0200 | [diff] [blame] | 12804 | |
| 12805 | if (cap[h->index] == NULL) |
| 12806 | cap[h->index] = pool_alloc2(h->pool); |
| 12807 | |
| 12808 | if (cap[h->index] == NULL) /* no more capture memory */ |
Thierry FOURNIER | 24ff6c6 | 2015-08-06 08:52:53 +0200 | [diff] [blame] | 12809 | return ACT_RET_CONT; |
Willy Tarreau | a9083d0 | 2015-05-08 15:27:59 +0200 | [diff] [blame] | 12810 | |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 12811 | len = key->data.u.str.len; |
Willy Tarreau | a9083d0 | 2015-05-08 15:27:59 +0200 | [diff] [blame] | 12812 | if (len > h->len) |
| 12813 | len = h->len; |
| 12814 | |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 12815 | memcpy(cap[h->index], key->data.u.str.str, len); |
Willy Tarreau | a9083d0 | 2015-05-08 15:27:59 +0200 | [diff] [blame] | 12816 | cap[h->index][len] = 0; |
Thierry FOURNIER | 24ff6c6 | 2015-08-06 08:52:53 +0200 | [diff] [blame] | 12817 | return ACT_RET_CONT; |
Willy Tarreau | a9083d0 | 2015-05-08 15:27:59 +0200 | [diff] [blame] | 12818 | } |
| 12819 | |
Thierry FOURNIER | 82bf70d | 2015-05-26 17:58:29 +0200 | [diff] [blame] | 12820 | /* This function executes the "capture" action and store the result in a |
| 12821 | * capture slot if exists. It executes a fetch expression, turns the result |
| 12822 | * into a string and puts it in a capture slot. It always returns 1. If an |
| 12823 | * error occurs the action is cancelled, but the rule processing continues. |
| 12824 | */ |
Thierry FOURNIER | 24ff6c6 | 2015-08-06 08:52:53 +0200 | [diff] [blame] | 12825 | 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] | 12826 | struct session *sess, struct stream *s, int flags) |
Thierry FOURNIER | 82bf70d | 2015-05-26 17:58:29 +0200 | [diff] [blame] | 12827 | { |
Thierry FOURNIER | 82bf70d | 2015-05-26 17:58:29 +0200 | [diff] [blame] | 12828 | struct sample *key; |
Thierry FOURNIER | 82bf70d | 2015-05-26 17:58:29 +0200 | [diff] [blame] | 12829 | struct cap_hdr *h; |
Thierry FOURNIER | 82bf70d | 2015-05-26 17:58:29 +0200 | [diff] [blame] | 12830 | char **cap = s->req_cap; |
| 12831 | struct proxy *fe = strm_fe(s); |
| 12832 | int len; |
| 12833 | int i; |
| 12834 | |
| 12835 | /* Look for the original configuration. */ |
| 12836 | for (h = fe->req_cap, i = fe->nb_req_cap - 1; |
Thierry FOURNIER | e209797 | 2015-07-31 08:56:35 +0200 | [diff] [blame] | 12837 | h != NULL && i != rule->arg.capid.idx ; |
Thierry FOURNIER | 82bf70d | 2015-05-26 17:58:29 +0200 | [diff] [blame] | 12838 | i--, h = h->next); |
| 12839 | if (!h) |
Thierry FOURNIER | 24ff6c6 | 2015-08-06 08:52:53 +0200 | [diff] [blame] | 12840 | return ACT_RET_CONT; |
Thierry FOURNIER | 82bf70d | 2015-05-26 17:58:29 +0200 | [diff] [blame] | 12841 | |
Thierry FOURNIER | e209797 | 2015-07-31 08:56:35 +0200 | [diff] [blame] | 12842 | 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] | 12843 | if (!key) |
Thierry FOURNIER | 24ff6c6 | 2015-08-06 08:52:53 +0200 | [diff] [blame] | 12844 | return ACT_RET_CONT; |
Thierry FOURNIER | 82bf70d | 2015-05-26 17:58:29 +0200 | [diff] [blame] | 12845 | |
| 12846 | if (cap[h->index] == NULL) |
| 12847 | cap[h->index] = pool_alloc2(h->pool); |
| 12848 | |
| 12849 | if (cap[h->index] == NULL) /* no more capture memory */ |
Thierry FOURNIER | 24ff6c6 | 2015-08-06 08:52:53 +0200 | [diff] [blame] | 12850 | return ACT_RET_CONT; |
Thierry FOURNIER | 82bf70d | 2015-05-26 17:58:29 +0200 | [diff] [blame] | 12851 | |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 12852 | len = key->data.u.str.len; |
Thierry FOURNIER | 82bf70d | 2015-05-26 17:58:29 +0200 | [diff] [blame] | 12853 | if (len > h->len) |
| 12854 | len = h->len; |
| 12855 | |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 12856 | memcpy(cap[h->index], key->data.u.str.str, len); |
Thierry FOURNIER | 82bf70d | 2015-05-26 17:58:29 +0200 | [diff] [blame] | 12857 | cap[h->index][len] = 0; |
Thierry FOURNIER | 24ff6c6 | 2015-08-06 08:52:53 +0200 | [diff] [blame] | 12858 | return ACT_RET_CONT; |
Thierry FOURNIER | 82bf70d | 2015-05-26 17:58:29 +0200 | [diff] [blame] | 12859 | } |
| 12860 | |
Willy Tarreau | a9083d0 | 2015-05-08 15:27:59 +0200 | [diff] [blame] | 12861 | /* parse an "http-request capture" action. It takes a single argument which is |
| 12862 | * 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] | 12863 | * 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] | 12864 | * 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] | 12865 | */ |
Thierry FOURNIER | afa8049 | 2015-08-19 09:04:15 +0200 | [diff] [blame] | 12866 | enum act_parse_ret parse_http_req_capture(const char **args, int *orig_arg, struct proxy *px, |
| 12867 | struct act_rule *rule, char **err) |
Willy Tarreau | a9083d0 | 2015-05-08 15:27:59 +0200 | [diff] [blame] | 12868 | { |
| 12869 | struct sample_expr *expr; |
| 12870 | struct cap_hdr *hdr; |
| 12871 | int cur_arg; |
Willy Tarreau | 3986ac1 | 2015-05-08 16:13:42 +0200 | [diff] [blame] | 12872 | int len = 0; |
Willy Tarreau | a9083d0 | 2015-05-08 15:27:59 +0200 | [diff] [blame] | 12873 | |
| 12874 | for (cur_arg = *orig_arg; cur_arg < *orig_arg + 3 && *args[cur_arg]; cur_arg++) |
| 12875 | if (strcmp(args[cur_arg], "if") == 0 || |
| 12876 | strcmp(args[cur_arg], "unless") == 0) |
| 12877 | break; |
| 12878 | |
| 12879 | if (cur_arg < *orig_arg + 3) { |
Thierry FOURNIER | 82bf70d | 2015-05-26 17:58:29 +0200 | [diff] [blame] | 12880 | memprintf(err, "expects <expression> [ 'len' <length> | id <idx> ]"); |
Thierry FOURNIER | afa8049 | 2015-08-19 09:04:15 +0200 | [diff] [blame] | 12881 | return ACT_RET_PRS_ERR; |
Willy Tarreau | a9083d0 | 2015-05-08 15:27:59 +0200 | [diff] [blame] | 12882 | } |
| 12883 | |
Willy Tarreau | a9083d0 | 2015-05-08 15:27:59 +0200 | [diff] [blame] | 12884 | cur_arg = *orig_arg; |
| 12885 | expr = sample_parse_expr((char **)args, &cur_arg, px->conf.args.file, px->conf.args.line, err, &px->conf.args); |
| 12886 | if (!expr) |
Thierry FOURNIER | afa8049 | 2015-08-19 09:04:15 +0200 | [diff] [blame] | 12887 | return ACT_RET_PRS_ERR; |
Willy Tarreau | a9083d0 | 2015-05-08 15:27:59 +0200 | [diff] [blame] | 12888 | |
| 12889 | if (!(expr->fetch->val & SMP_VAL_FE_HRQ_HDR)) { |
| 12890 | memprintf(err, |
| 12891 | "fetch method '%s' extracts information from '%s', none of which is available here", |
| 12892 | args[cur_arg-1], sample_src_names(expr->fetch->use)); |
| 12893 | free(expr); |
Thierry FOURNIER | afa8049 | 2015-08-19 09:04:15 +0200 | [diff] [blame] | 12894 | return ACT_RET_PRS_ERR; |
Willy Tarreau | a9083d0 | 2015-05-08 15:27:59 +0200 | [diff] [blame] | 12895 | } |
| 12896 | |
Thierry FOURNIER | 82bf70d | 2015-05-26 17:58:29 +0200 | [diff] [blame] | 12897 | if (!args[cur_arg] || !*args[cur_arg]) { |
| 12898 | memprintf(err, "expects 'len or 'id'"); |
| 12899 | free(expr); |
Thierry FOURNIER | afa8049 | 2015-08-19 09:04:15 +0200 | [diff] [blame] | 12900 | return ACT_RET_PRS_ERR; |
Thierry FOURNIER | 82bf70d | 2015-05-26 17:58:29 +0200 | [diff] [blame] | 12901 | } |
| 12902 | |
Willy Tarreau | a9083d0 | 2015-05-08 15:27:59 +0200 | [diff] [blame] | 12903 | if (strcmp(args[cur_arg], "len") == 0) { |
| 12904 | cur_arg++; |
Thierry FOURNIER | 82bf70d | 2015-05-26 17:58:29 +0200 | [diff] [blame] | 12905 | |
| 12906 | if (!(px->cap & PR_CAP_FE)) { |
| 12907 | memprintf(err, "proxy '%s' has no frontend capability", px->id); |
Thierry FOURNIER | afa8049 | 2015-08-19 09:04:15 +0200 | [diff] [blame] | 12908 | return ACT_RET_PRS_ERR; |
Thierry FOURNIER | 82bf70d | 2015-05-26 17:58:29 +0200 | [diff] [blame] | 12909 | } |
| 12910 | |
| 12911 | proxy->conf.args.ctx = ARGC_CAP; |
| 12912 | |
Willy Tarreau | a9083d0 | 2015-05-08 15:27:59 +0200 | [diff] [blame] | 12913 | if (!args[cur_arg]) { |
| 12914 | memprintf(err, "missing length value"); |
| 12915 | free(expr); |
Thierry FOURNIER | afa8049 | 2015-08-19 09:04:15 +0200 | [diff] [blame] | 12916 | return ACT_RET_PRS_ERR; |
Willy Tarreau | a9083d0 | 2015-05-08 15:27:59 +0200 | [diff] [blame] | 12917 | } |
| 12918 | /* we copy the table name for now, it will be resolved later */ |
| 12919 | len = atoi(args[cur_arg]); |
| 12920 | if (len <= 0) { |
| 12921 | memprintf(err, "length must be > 0"); |
| 12922 | free(expr); |
Thierry FOURNIER | afa8049 | 2015-08-19 09:04:15 +0200 | [diff] [blame] | 12923 | return ACT_RET_PRS_ERR; |
Willy Tarreau | a9083d0 | 2015-05-08 15:27:59 +0200 | [diff] [blame] | 12924 | } |
| 12925 | cur_arg++; |
Willy Tarreau | a9083d0 | 2015-05-08 15:27:59 +0200 | [diff] [blame] | 12926 | |
Thierry FOURNIER | 82bf70d | 2015-05-26 17:58:29 +0200 | [diff] [blame] | 12927 | if (!len) { |
| 12928 | memprintf(err, "a positive 'len' argument is mandatory"); |
| 12929 | free(expr); |
Thierry FOURNIER | afa8049 | 2015-08-19 09:04:15 +0200 | [diff] [blame] | 12930 | return ACT_RET_PRS_ERR; |
Thierry FOURNIER | 82bf70d | 2015-05-26 17:58:29 +0200 | [diff] [blame] | 12931 | } |
| 12932 | |
Vincent Bernat | 02779b6 | 2016-04-03 13:48:43 +0200 | [diff] [blame] | 12933 | hdr = calloc(1, sizeof(*hdr)); |
Thierry FOURNIER | 82bf70d | 2015-05-26 17:58:29 +0200 | [diff] [blame] | 12934 | hdr->next = px->req_cap; |
| 12935 | hdr->name = NULL; /* not a header capture */ |
| 12936 | hdr->namelen = 0; |
| 12937 | hdr->len = len; |
| 12938 | hdr->pool = create_pool("caphdr", hdr->len + 1, MEM_F_SHARED); |
| 12939 | hdr->index = px->nb_req_cap++; |
| 12940 | |
| 12941 | px->req_cap = hdr; |
| 12942 | px->to_log |= LW_REQHDR; |
| 12943 | |
Thierry FOURNIER | 4214873 | 2015-09-02 17:17:33 +0200 | [diff] [blame] | 12944 | rule->action = ACT_CUSTOM; |
Thierry FOURNIER | 82bf70d | 2015-05-26 17:58:29 +0200 | [diff] [blame] | 12945 | rule->action_ptr = http_action_req_capture; |
Thierry FOURNIER | 32b1500 | 2015-07-31 08:56:16 +0200 | [diff] [blame] | 12946 | rule->arg.cap.expr = expr; |
| 12947 | rule->arg.cap.hdr = hdr; |
Willy Tarreau | a9083d0 | 2015-05-08 15:27:59 +0200 | [diff] [blame] | 12948 | } |
| 12949 | |
Thierry FOURNIER | 82bf70d | 2015-05-26 17:58:29 +0200 | [diff] [blame] | 12950 | else if (strcmp(args[cur_arg], "id") == 0) { |
| 12951 | int id; |
| 12952 | char *error; |
Willy Tarreau | a9083d0 | 2015-05-08 15:27:59 +0200 | [diff] [blame] | 12953 | |
Thierry FOURNIER | 82bf70d | 2015-05-26 17:58:29 +0200 | [diff] [blame] | 12954 | cur_arg++; |
Willy Tarreau | a9083d0 | 2015-05-08 15:27:59 +0200 | [diff] [blame] | 12955 | |
Thierry FOURNIER | 82bf70d | 2015-05-26 17:58:29 +0200 | [diff] [blame] | 12956 | if (!args[cur_arg]) { |
| 12957 | memprintf(err, "missing id value"); |
| 12958 | free(expr); |
Thierry FOURNIER | afa8049 | 2015-08-19 09:04:15 +0200 | [diff] [blame] | 12959 | return ACT_RET_PRS_ERR; |
Thierry FOURNIER | 82bf70d | 2015-05-26 17:58:29 +0200 | [diff] [blame] | 12960 | } |
Willy Tarreau | a9083d0 | 2015-05-08 15:27:59 +0200 | [diff] [blame] | 12961 | |
Thierry FOURNIER | 82bf70d | 2015-05-26 17:58:29 +0200 | [diff] [blame] | 12962 | id = strtol(args[cur_arg], &error, 10); |
| 12963 | if (*error != '\0') { |
| 12964 | memprintf(err, "cannot parse id '%s'", args[cur_arg]); |
| 12965 | free(expr); |
Thierry FOURNIER | afa8049 | 2015-08-19 09:04:15 +0200 | [diff] [blame] | 12966 | return ACT_RET_PRS_ERR; |
Thierry FOURNIER | 82bf70d | 2015-05-26 17:58:29 +0200 | [diff] [blame] | 12967 | } |
| 12968 | cur_arg++; |
| 12969 | |
| 12970 | proxy->conf.args.ctx = ARGC_CAP; |
| 12971 | |
Thierry FOURNIER | 4214873 | 2015-09-02 17:17:33 +0200 | [diff] [blame] | 12972 | rule->action = ACT_CUSTOM; |
Thierry FOURNIER | 82bf70d | 2015-05-26 17:58:29 +0200 | [diff] [blame] | 12973 | rule->action_ptr = http_action_req_capture_by_id; |
Thierry FOURNIER | e209797 | 2015-07-31 08:56:35 +0200 | [diff] [blame] | 12974 | rule->arg.capid.expr = expr; |
| 12975 | rule->arg.capid.idx = id; |
Thierry FOURNIER | 82bf70d | 2015-05-26 17:58:29 +0200 | [diff] [blame] | 12976 | } |
| 12977 | |
| 12978 | else { |
| 12979 | memprintf(err, "expects 'len' or 'id', found '%s'", args[cur_arg]); |
| 12980 | free(expr); |
Thierry FOURNIER | afa8049 | 2015-08-19 09:04:15 +0200 | [diff] [blame] | 12981 | return ACT_RET_PRS_ERR; |
Thierry FOURNIER | 82bf70d | 2015-05-26 17:58:29 +0200 | [diff] [blame] | 12982 | } |
Willy Tarreau | a9083d0 | 2015-05-08 15:27:59 +0200 | [diff] [blame] | 12983 | |
| 12984 | *orig_arg = cur_arg; |
Thierry FOURNIER | afa8049 | 2015-08-19 09:04:15 +0200 | [diff] [blame] | 12985 | return ACT_RET_PRS_OK; |
Willy Tarreau | a9083d0 | 2015-05-08 15:27:59 +0200 | [diff] [blame] | 12986 | } |
| 12987 | |
Thierry FOURNIER | e80fada | 2015-05-26 18:06:31 +0200 | [diff] [blame] | 12988 | /* This function executes the "capture" action and store the result in a |
| 12989 | * capture slot if exists. It executes a fetch expression, turns the result |
| 12990 | * into a string and puts it in a capture slot. It always returns 1. If an |
| 12991 | * error occurs the action is cancelled, but the rule processing continues. |
| 12992 | */ |
Thierry FOURNIER | 24ff6c6 | 2015-08-06 08:52:53 +0200 | [diff] [blame] | 12993 | 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] | 12994 | struct session *sess, struct stream *s, int flags) |
Thierry FOURNIER | e80fada | 2015-05-26 18:06:31 +0200 | [diff] [blame] | 12995 | { |
Thierry FOURNIER | e80fada | 2015-05-26 18:06:31 +0200 | [diff] [blame] | 12996 | struct sample *key; |
Thierry FOURNIER | e80fada | 2015-05-26 18:06:31 +0200 | [diff] [blame] | 12997 | struct cap_hdr *h; |
Thierry FOURNIER | e80fada | 2015-05-26 18:06:31 +0200 | [diff] [blame] | 12998 | char **cap = s->res_cap; |
| 12999 | struct proxy *fe = strm_fe(s); |
| 13000 | int len; |
| 13001 | int i; |
| 13002 | |
| 13003 | /* Look for the original configuration. */ |
| 13004 | for (h = fe->rsp_cap, i = fe->nb_rsp_cap - 1; |
Thierry FOURNIER | e209797 | 2015-07-31 08:56:35 +0200 | [diff] [blame] | 13005 | h != NULL && i != rule->arg.capid.idx ; |
Thierry FOURNIER | e80fada | 2015-05-26 18:06:31 +0200 | [diff] [blame] | 13006 | i--, h = h->next); |
| 13007 | if (!h) |
Thierry FOURNIER | 24ff6c6 | 2015-08-06 08:52:53 +0200 | [diff] [blame] | 13008 | return ACT_RET_CONT; |
Thierry FOURNIER | e80fada | 2015-05-26 18:06:31 +0200 | [diff] [blame] | 13009 | |
Thierry FOURNIER | e209797 | 2015-07-31 08:56:35 +0200 | [diff] [blame] | 13010 | 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] | 13011 | if (!key) |
Thierry FOURNIER | 24ff6c6 | 2015-08-06 08:52:53 +0200 | [diff] [blame] | 13012 | return ACT_RET_CONT; |
Thierry FOURNIER | e80fada | 2015-05-26 18:06:31 +0200 | [diff] [blame] | 13013 | |
| 13014 | if (cap[h->index] == NULL) |
| 13015 | cap[h->index] = pool_alloc2(h->pool); |
| 13016 | |
| 13017 | if (cap[h->index] == NULL) /* no more capture memory */ |
Thierry FOURNIER | 24ff6c6 | 2015-08-06 08:52:53 +0200 | [diff] [blame] | 13018 | return ACT_RET_CONT; |
Thierry FOURNIER | e80fada | 2015-05-26 18:06:31 +0200 | [diff] [blame] | 13019 | |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 13020 | len = key->data.u.str.len; |
Thierry FOURNIER | e80fada | 2015-05-26 18:06:31 +0200 | [diff] [blame] | 13021 | if (len > h->len) |
| 13022 | len = h->len; |
| 13023 | |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 13024 | memcpy(cap[h->index], key->data.u.str.str, len); |
Thierry FOURNIER | e80fada | 2015-05-26 18:06:31 +0200 | [diff] [blame] | 13025 | cap[h->index][len] = 0; |
Thierry FOURNIER | 24ff6c6 | 2015-08-06 08:52:53 +0200 | [diff] [blame] | 13026 | return ACT_RET_CONT; |
Thierry FOURNIER | e80fada | 2015-05-26 18:06:31 +0200 | [diff] [blame] | 13027 | } |
| 13028 | |
| 13029 | /* parse an "http-response capture" action. It takes a single argument which is |
| 13030 | * a sample fetch expression. It stores the expression into arg->act.p[0] and |
| 13031 | * 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] | 13032 | * 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] | 13033 | */ |
Thierry FOURNIER | afa8049 | 2015-08-19 09:04:15 +0200 | [diff] [blame] | 13034 | enum act_parse_ret parse_http_res_capture(const char **args, int *orig_arg, struct proxy *px, |
| 13035 | struct act_rule *rule, char **err) |
Thierry FOURNIER | e80fada | 2015-05-26 18:06:31 +0200 | [diff] [blame] | 13036 | { |
| 13037 | struct sample_expr *expr; |
| 13038 | int cur_arg; |
| 13039 | int id; |
| 13040 | char *error; |
| 13041 | |
| 13042 | for (cur_arg = *orig_arg; cur_arg < *orig_arg + 3 && *args[cur_arg]; cur_arg++) |
| 13043 | if (strcmp(args[cur_arg], "if") == 0 || |
| 13044 | strcmp(args[cur_arg], "unless") == 0) |
| 13045 | break; |
| 13046 | |
| 13047 | if (cur_arg < *orig_arg + 3) { |
Willy Tarreau | 29bdb1c | 2016-06-24 15:36:34 +0200 | [diff] [blame] | 13048 | memprintf(err, "expects <expression> id <idx>"); |
Thierry FOURNIER | afa8049 | 2015-08-19 09:04:15 +0200 | [diff] [blame] | 13049 | return ACT_RET_PRS_ERR; |
Thierry FOURNIER | e80fada | 2015-05-26 18:06:31 +0200 | [diff] [blame] | 13050 | } |
| 13051 | |
| 13052 | cur_arg = *orig_arg; |
| 13053 | expr = sample_parse_expr((char **)args, &cur_arg, px->conf.args.file, px->conf.args.line, err, &px->conf.args); |
| 13054 | if (!expr) |
Thierry FOURNIER | afa8049 | 2015-08-19 09:04:15 +0200 | [diff] [blame] | 13055 | return ACT_RET_PRS_ERR; |
Thierry FOURNIER | e80fada | 2015-05-26 18:06:31 +0200 | [diff] [blame] | 13056 | |
| 13057 | if (!(expr->fetch->val & SMP_VAL_FE_HRS_HDR)) { |
| 13058 | memprintf(err, |
| 13059 | "fetch method '%s' extracts information from '%s', none of which is available here", |
| 13060 | args[cur_arg-1], sample_src_names(expr->fetch->use)); |
| 13061 | free(expr); |
Thierry FOURNIER | afa8049 | 2015-08-19 09:04:15 +0200 | [diff] [blame] | 13062 | return ACT_RET_PRS_ERR; |
Thierry FOURNIER | e80fada | 2015-05-26 18:06:31 +0200 | [diff] [blame] | 13063 | } |
| 13064 | |
| 13065 | if (!args[cur_arg] || !*args[cur_arg]) { |
Willy Tarreau | 29bdb1c | 2016-06-24 15:36:34 +0200 | [diff] [blame] | 13066 | memprintf(err, "expects 'id'"); |
Thierry FOURNIER | e80fada | 2015-05-26 18:06:31 +0200 | [diff] [blame] | 13067 | free(expr); |
Thierry FOURNIER | afa8049 | 2015-08-19 09:04:15 +0200 | [diff] [blame] | 13068 | return ACT_RET_PRS_ERR; |
Thierry FOURNIER | e80fada | 2015-05-26 18:06:31 +0200 | [diff] [blame] | 13069 | } |
| 13070 | |
| 13071 | if (strcmp(args[cur_arg], "id") != 0) { |
| 13072 | memprintf(err, "expects 'id', found '%s'", args[cur_arg]); |
| 13073 | free(expr); |
Thierry FOURNIER | afa8049 | 2015-08-19 09:04:15 +0200 | [diff] [blame] | 13074 | return ACT_RET_PRS_ERR; |
Thierry FOURNIER | e80fada | 2015-05-26 18:06:31 +0200 | [diff] [blame] | 13075 | } |
| 13076 | |
| 13077 | cur_arg++; |
| 13078 | |
| 13079 | if (!args[cur_arg]) { |
| 13080 | memprintf(err, "missing id value"); |
| 13081 | free(expr); |
Thierry FOURNIER | afa8049 | 2015-08-19 09:04:15 +0200 | [diff] [blame] | 13082 | return ACT_RET_PRS_ERR; |
Thierry FOURNIER | e80fada | 2015-05-26 18:06:31 +0200 | [diff] [blame] | 13083 | } |
| 13084 | |
| 13085 | id = strtol(args[cur_arg], &error, 10); |
| 13086 | if (*error != '\0') { |
| 13087 | memprintf(err, "cannot parse id '%s'", args[cur_arg]); |
| 13088 | free(expr); |
Thierry FOURNIER | afa8049 | 2015-08-19 09:04:15 +0200 | [diff] [blame] | 13089 | return ACT_RET_PRS_ERR; |
Thierry FOURNIER | e80fada | 2015-05-26 18:06:31 +0200 | [diff] [blame] | 13090 | } |
| 13091 | cur_arg++; |
| 13092 | |
Thierry FOURNIER | e80fada | 2015-05-26 18:06:31 +0200 | [diff] [blame] | 13093 | proxy->conf.args.ctx = ARGC_CAP; |
| 13094 | |
Thierry FOURNIER | 4214873 | 2015-09-02 17:17:33 +0200 | [diff] [blame] | 13095 | rule->action = ACT_CUSTOM; |
Thierry FOURNIER | e80fada | 2015-05-26 18:06:31 +0200 | [diff] [blame] | 13096 | rule->action_ptr = http_action_res_capture_by_id; |
Thierry FOURNIER | e209797 | 2015-07-31 08:56:35 +0200 | [diff] [blame] | 13097 | rule->arg.capid.expr = expr; |
| 13098 | rule->arg.capid.idx = id; |
Thierry FOURNIER | e80fada | 2015-05-26 18:06:31 +0200 | [diff] [blame] | 13099 | |
| 13100 | *orig_arg = cur_arg; |
Thierry FOURNIER | afa8049 | 2015-08-19 09:04:15 +0200 | [diff] [blame] | 13101 | return ACT_RET_PRS_OK; |
Thierry FOURNIER | e80fada | 2015-05-26 18:06:31 +0200 | [diff] [blame] | 13102 | } |
| 13103 | |
William Lallemand | 73025dd | 2014-04-24 14:38:37 +0200 | [diff] [blame] | 13104 | /* |
| 13105 | * Return the struct http_req_action_kw associated to a keyword. |
| 13106 | */ |
Thierry FOURNIER | 36481b8 | 2015-08-19 09:01:53 +0200 | [diff] [blame] | 13107 | struct action_kw *action_http_req_custom(const char *kw) |
William Lallemand | 73025dd | 2014-04-24 14:38:37 +0200 | [diff] [blame] | 13108 | { |
Thierry FOURNIER | 322a124 | 2015-08-19 09:07:47 +0200 | [diff] [blame] | 13109 | return action_lookup(&http_req_keywords.list, kw); |
William Lallemand | 73025dd | 2014-04-24 14:38:37 +0200 | [diff] [blame] | 13110 | } |
| 13111 | |
| 13112 | /* |
| 13113 | * Return the struct http_res_action_kw associated to a keyword. |
| 13114 | */ |
Thierry FOURNIER | 36481b8 | 2015-08-19 09:01:53 +0200 | [diff] [blame] | 13115 | struct action_kw *action_http_res_custom(const char *kw) |
William Lallemand | 73025dd | 2014-04-24 14:38:37 +0200 | [diff] [blame] | 13116 | { |
Thierry FOURNIER | 322a124 | 2015-08-19 09:07:47 +0200 | [diff] [blame] | 13117 | return action_lookup(&http_res_keywords.list, kw); |
William Lallemand | 73025dd | 2014-04-24 14:38:37 +0200 | [diff] [blame] | 13118 | } |
| 13119 | |
Willy Tarreau | 12207b3 | 2016-11-22 19:48:51 +0100 | [diff] [blame] | 13120 | |
| 13121 | /* "show errors" handler for the CLI. Returns 0 if wants to continue, 1 to stop |
| 13122 | * now. |
| 13123 | */ |
| 13124 | static int cli_parse_show_errors(char **args, struct appctx *appctx, void *private) |
| 13125 | { |
| 13126 | if (!cli_has_level(appctx, ACCESS_LVL_OPER)) |
| 13127 | return 1; |
| 13128 | |
Willy Tarreau | 234ba2d | 2016-11-25 08:39:10 +0100 | [diff] [blame] | 13129 | if (*args[2]) { |
| 13130 | struct proxy *px; |
| 13131 | |
| 13132 | px = proxy_find_by_name(args[2], 0, 0); |
| 13133 | if (px) |
| 13134 | appctx->ctx.errors.iid = px->uuid; |
| 13135 | else |
| 13136 | appctx->ctx.errors.iid = atoi(args[2]); |
| 13137 | |
| 13138 | if (!appctx->ctx.errors.iid) { |
| 13139 | appctx->ctx.cli.msg = "No such proxy.\n"; |
| 13140 | appctx->st0 = CLI_ST_PRINT; |
| 13141 | return 1; |
| 13142 | } |
| 13143 | } |
Willy Tarreau | 12207b3 | 2016-11-22 19:48:51 +0100 | [diff] [blame] | 13144 | else |
Willy Tarreau | 234ba2d | 2016-11-25 08:39:10 +0100 | [diff] [blame] | 13145 | appctx->ctx.errors.iid = -1; // dump all proxies |
| 13146 | |
Willy Tarreau | 35069f8 | 2016-11-25 09:16:37 +0100 | [diff] [blame] | 13147 | appctx->ctx.errors.flag = 0; |
| 13148 | if (strcmp(args[3], "request") == 0) |
| 13149 | appctx->ctx.errors.flag |= 4; // ignore response |
| 13150 | else if (strcmp(args[3], "response") == 0) |
| 13151 | appctx->ctx.errors.flag |= 2; // ignore request |
Willy Tarreau | 12207b3 | 2016-11-22 19:48:51 +0100 | [diff] [blame] | 13152 | appctx->ctx.errors.px = NULL; |
Willy Tarreau | 12207b3 | 2016-11-22 19:48:51 +0100 | [diff] [blame] | 13153 | return 0; |
| 13154 | } |
| 13155 | |
| 13156 | /* This function dumps all captured errors onto the stream interface's |
| 13157 | * read buffer. It returns 0 if the output buffer is full and it needs |
| 13158 | * to be called again, otherwise non-zero. |
| 13159 | */ |
| 13160 | static int cli_io_handler_show_errors(struct appctx *appctx) |
| 13161 | { |
| 13162 | struct stream_interface *si = appctx->owner; |
| 13163 | extern const char *monthname[12]; |
| 13164 | |
| 13165 | if (unlikely(si_ic(si)->flags & (CF_WRITE_ERROR|CF_SHUTW))) |
| 13166 | return 1; |
| 13167 | |
| 13168 | chunk_reset(&trash); |
| 13169 | |
| 13170 | if (!appctx->ctx.errors.px) { |
| 13171 | /* the function had not been called yet, let's prepare the |
| 13172 | * buffer for a response. |
| 13173 | */ |
| 13174 | struct tm tm; |
| 13175 | |
| 13176 | get_localtime(date.tv_sec, &tm); |
| 13177 | chunk_appendf(&trash, "Total events captured on [%02d/%s/%04d:%02d:%02d:%02d.%03d] : %u\n", |
| 13178 | tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900, |
| 13179 | tm.tm_hour, tm.tm_min, tm.tm_sec, (int)(date.tv_usec/1000), |
| 13180 | error_snapshot_id); |
| 13181 | |
| 13182 | if (bi_putchk(si_ic(si), &trash) == -1) { |
| 13183 | /* Socket buffer full. Let's try again later from the same point */ |
| 13184 | si_applet_cant_put(si); |
| 13185 | return 0; |
| 13186 | } |
| 13187 | |
| 13188 | appctx->ctx.errors.px = proxy; |
Willy Tarreau | 12207b3 | 2016-11-22 19:48:51 +0100 | [diff] [blame] | 13189 | appctx->ctx.errors.bol = 0; |
| 13190 | appctx->ctx.errors.ptr = -1; |
| 13191 | } |
| 13192 | |
| 13193 | /* we have two inner loops here, one for the proxy, the other one for |
| 13194 | * the buffer. |
| 13195 | */ |
| 13196 | while (appctx->ctx.errors.px) { |
| 13197 | struct error_snapshot *es; |
| 13198 | |
Willy Tarreau | 35069f8 | 2016-11-25 09:16:37 +0100 | [diff] [blame] | 13199 | if ((appctx->ctx.errors.flag & 1) == 0) { |
Willy Tarreau | 12207b3 | 2016-11-22 19:48:51 +0100 | [diff] [blame] | 13200 | es = &appctx->ctx.errors.px->invalid_req; |
Willy Tarreau | 35069f8 | 2016-11-25 09:16:37 +0100 | [diff] [blame] | 13201 | if (appctx->ctx.errors.flag & 2) // skip req |
| 13202 | goto next; |
| 13203 | } |
| 13204 | else { |
Willy Tarreau | 12207b3 | 2016-11-22 19:48:51 +0100 | [diff] [blame] | 13205 | es = &appctx->ctx.errors.px->invalid_rep; |
Willy Tarreau | 35069f8 | 2016-11-25 09:16:37 +0100 | [diff] [blame] | 13206 | if (appctx->ctx.errors.flag & 4) // skip resp |
| 13207 | goto next; |
| 13208 | } |
Willy Tarreau | 12207b3 | 2016-11-22 19:48:51 +0100 | [diff] [blame] | 13209 | |
| 13210 | if (!es->when.tv_sec) |
| 13211 | goto next; |
| 13212 | |
| 13213 | if (appctx->ctx.errors.iid >= 0 && |
| 13214 | appctx->ctx.errors.px->uuid != appctx->ctx.errors.iid && |
| 13215 | es->oe->uuid != appctx->ctx.errors.iid) |
| 13216 | goto next; |
| 13217 | |
| 13218 | if (appctx->ctx.errors.ptr < 0) { |
| 13219 | /* just print headers now */ |
| 13220 | |
| 13221 | char pn[INET6_ADDRSTRLEN]; |
| 13222 | struct tm tm; |
| 13223 | int port; |
| 13224 | |
| 13225 | get_localtime(es->when.tv_sec, &tm); |
| 13226 | chunk_appendf(&trash, " \n[%02d/%s/%04d:%02d:%02d:%02d.%03d]", |
| 13227 | tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900, |
| 13228 | tm.tm_hour, tm.tm_min, tm.tm_sec, (int)(es->when.tv_usec/1000)); |
| 13229 | |
| 13230 | switch (addr_to_str(&es->src, pn, sizeof(pn))) { |
| 13231 | case AF_INET: |
| 13232 | case AF_INET6: |
| 13233 | port = get_host_port(&es->src); |
| 13234 | break; |
| 13235 | default: |
| 13236 | port = 0; |
| 13237 | } |
| 13238 | |
Willy Tarreau | 35069f8 | 2016-11-25 09:16:37 +0100 | [diff] [blame] | 13239 | switch (appctx->ctx.errors.flag & 1) { |
Willy Tarreau | 12207b3 | 2016-11-22 19:48:51 +0100 | [diff] [blame] | 13240 | case 0: |
| 13241 | chunk_appendf(&trash, |
| 13242 | " frontend %s (#%d): invalid request\n" |
| 13243 | " backend %s (#%d)", |
| 13244 | appctx->ctx.errors.px->id, appctx->ctx.errors.px->uuid, |
| 13245 | (es->oe->cap & PR_CAP_BE) ? es->oe->id : "<NONE>", |
| 13246 | (es->oe->cap & PR_CAP_BE) ? es->oe->uuid : -1); |
| 13247 | break; |
| 13248 | case 1: |
| 13249 | chunk_appendf(&trash, |
| 13250 | " backend %s (#%d): invalid response\n" |
| 13251 | " frontend %s (#%d)", |
| 13252 | appctx->ctx.errors.px->id, appctx->ctx.errors.px->uuid, |
| 13253 | es->oe->id, es->oe->uuid); |
| 13254 | break; |
| 13255 | } |
| 13256 | |
| 13257 | chunk_appendf(&trash, |
| 13258 | ", server %s (#%d), event #%u\n" |
| 13259 | " src %s:%d, session #%d, session flags 0x%08x\n" |
Willy Tarreau | 10e61cb | 2017-01-04 14:51:22 +0100 | [diff] [blame] | 13260 | " HTTP msg state %s(%d), msg flags 0x%08x, tx flags 0x%08x\n" |
Willy Tarreau | 12207b3 | 2016-11-22 19:48:51 +0100 | [diff] [blame] | 13261 | " HTTP chunk len %lld bytes, HTTP body len %lld bytes\n" |
| 13262 | " buffer flags 0x%08x, out %d bytes, total %lld bytes\n" |
| 13263 | " pending %d bytes, wrapping at %d, error at position %d:\n \n", |
| 13264 | es->srv ? es->srv->id : "<NONE>", es->srv ? es->srv->puid : -1, |
| 13265 | es->ev_id, |
| 13266 | pn, port, es->sid, es->s_flags, |
Willy Tarreau | 10e61cb | 2017-01-04 14:51:22 +0100 | [diff] [blame] | 13267 | http_msg_state_str(es->state), es->state, es->m_flags, es->t_flags, |
Willy Tarreau | 12207b3 | 2016-11-22 19:48:51 +0100 | [diff] [blame] | 13268 | es->m_clen, es->m_blen, |
| 13269 | es->b_flags, es->b_out, es->b_tot, |
| 13270 | es->len, es->b_wrap, es->pos); |
| 13271 | |
| 13272 | if (bi_putchk(si_ic(si), &trash) == -1) { |
| 13273 | /* Socket buffer full. Let's try again later from the same point */ |
| 13274 | si_applet_cant_put(si); |
| 13275 | return 0; |
| 13276 | } |
| 13277 | appctx->ctx.errors.ptr = 0; |
| 13278 | appctx->ctx.errors.sid = es->sid; |
| 13279 | } |
| 13280 | |
| 13281 | if (appctx->ctx.errors.sid != es->sid) { |
| 13282 | /* the snapshot changed while we were dumping it */ |
| 13283 | chunk_appendf(&trash, |
| 13284 | " WARNING! update detected on this snapshot, dump interrupted. Please re-check!\n"); |
| 13285 | if (bi_putchk(si_ic(si), &trash) == -1) { |
| 13286 | si_applet_cant_put(si); |
| 13287 | return 0; |
| 13288 | } |
| 13289 | goto next; |
| 13290 | } |
| 13291 | |
| 13292 | /* OK, ptr >= 0, so we have to dump the current line */ |
| 13293 | while (es->buf && appctx->ctx.errors.ptr < es->len && appctx->ctx.errors.ptr < global.tune.bufsize) { |
| 13294 | int newptr; |
| 13295 | int newline; |
| 13296 | |
| 13297 | newline = appctx->ctx.errors.bol; |
| 13298 | newptr = dump_text_line(&trash, es->buf, global.tune.bufsize, es->len, &newline, appctx->ctx.errors.ptr); |
| 13299 | if (newptr == appctx->ctx.errors.ptr) |
| 13300 | return 0; |
| 13301 | |
| 13302 | if (bi_putchk(si_ic(si), &trash) == -1) { |
| 13303 | /* Socket buffer full. Let's try again later from the same point */ |
| 13304 | si_applet_cant_put(si); |
| 13305 | return 0; |
| 13306 | } |
| 13307 | appctx->ctx.errors.ptr = newptr; |
| 13308 | appctx->ctx.errors.bol = newline; |
| 13309 | }; |
| 13310 | next: |
| 13311 | appctx->ctx.errors.bol = 0; |
| 13312 | appctx->ctx.errors.ptr = -1; |
Willy Tarreau | 35069f8 | 2016-11-25 09:16:37 +0100 | [diff] [blame] | 13313 | appctx->ctx.errors.flag ^= 1; |
| 13314 | if (!(appctx->ctx.errors.flag & 1)) |
Willy Tarreau | 12207b3 | 2016-11-22 19:48:51 +0100 | [diff] [blame] | 13315 | appctx->ctx.errors.px = appctx->ctx.errors.px->next; |
Willy Tarreau | 12207b3 | 2016-11-22 19:48:51 +0100 | [diff] [blame] | 13316 | } |
| 13317 | |
| 13318 | /* dump complete */ |
| 13319 | return 1; |
| 13320 | } |
| 13321 | |
| 13322 | /* register cli keywords */ |
| 13323 | static struct cli_kw_list cli_kws = {{ },{ |
| 13324 | { { "show", "errors", NULL }, |
| 13325 | "show errors : report last request and response errors for each proxy", |
| 13326 | cli_parse_show_errors, cli_io_handler_show_errors, NULL, |
| 13327 | }, |
| 13328 | {{},} |
| 13329 | }}; |
| 13330 | |
Willy Tarreau | 4a56897 | 2010-05-12 08:08:50 +0200 | [diff] [blame] | 13331 | /************************************************************************/ |
Willy Tarreau | 25c1ebc | 2012-04-25 16:21:44 +0200 | [diff] [blame] | 13332 | /* All supported ACL keywords must be declared here. */ |
| 13333 | /************************************************************************/ |
| 13334 | |
| 13335 | /* Note: must not be declared <const> as its list will be overwritten. |
| 13336 | * Please take care of keeping this list alphabetically sorted. |
| 13337 | */ |
Willy Tarreau | dc13c11 | 2013-06-21 23:16:39 +0200 | [diff] [blame] | 13338 | static struct acl_kw_list acl_kws = {ILH, { |
Thierry FOURNIER | c5a4e98 | 2014-03-05 16:07:08 +0100 | [diff] [blame] | 13339 | { "base", "base", PAT_MATCH_STR }, |
| 13340 | { "base_beg", "base", PAT_MATCH_BEG }, |
| 13341 | { "base_dir", "base", PAT_MATCH_DIR }, |
| 13342 | { "base_dom", "base", PAT_MATCH_DOM }, |
| 13343 | { "base_end", "base", PAT_MATCH_END }, |
| 13344 | { "base_len", "base", PAT_MATCH_LEN }, |
| 13345 | { "base_reg", "base", PAT_MATCH_REG }, |
| 13346 | { "base_sub", "base", PAT_MATCH_SUB }, |
Willy Tarreau | a7ad50c | 2012-04-29 15:39:40 +0200 | [diff] [blame] | 13347 | |
Thierry FOURNIER | c5a4e98 | 2014-03-05 16:07:08 +0100 | [diff] [blame] | 13348 | { "cook", "req.cook", PAT_MATCH_STR }, |
| 13349 | { "cook_beg", "req.cook", PAT_MATCH_BEG }, |
| 13350 | { "cook_dir", "req.cook", PAT_MATCH_DIR }, |
| 13351 | { "cook_dom", "req.cook", PAT_MATCH_DOM }, |
| 13352 | { "cook_end", "req.cook", PAT_MATCH_END }, |
| 13353 | { "cook_len", "req.cook", PAT_MATCH_LEN }, |
| 13354 | { "cook_reg", "req.cook", PAT_MATCH_REG }, |
| 13355 | { "cook_sub", "req.cook", PAT_MATCH_SUB }, |
Willy Tarreau | 25c1ebc | 2012-04-25 16:21:44 +0200 | [diff] [blame] | 13356 | |
Thierry FOURNIER | c5a4e98 | 2014-03-05 16:07:08 +0100 | [diff] [blame] | 13357 | { "hdr", "req.hdr", PAT_MATCH_STR }, |
| 13358 | { "hdr_beg", "req.hdr", PAT_MATCH_BEG }, |
| 13359 | { "hdr_dir", "req.hdr", PAT_MATCH_DIR }, |
| 13360 | { "hdr_dom", "req.hdr", PAT_MATCH_DOM }, |
| 13361 | { "hdr_end", "req.hdr", PAT_MATCH_END }, |
| 13362 | { "hdr_len", "req.hdr", PAT_MATCH_LEN }, |
| 13363 | { "hdr_reg", "req.hdr", PAT_MATCH_REG }, |
| 13364 | { "hdr_sub", "req.hdr", PAT_MATCH_SUB }, |
Willy Tarreau | 25c1ebc | 2012-04-25 16:21:44 +0200 | [diff] [blame] | 13365 | |
Thierry FOURNIER | c5a4e98 | 2014-03-05 16:07:08 +0100 | [diff] [blame] | 13366 | /* these two declarations uses strings with list storage (in place |
| 13367 | * of tree storage). The basic match is PAT_MATCH_STR, but the indexation |
| 13368 | * and delete functions are relative to the list management. The parse |
| 13369 | * and match method are related to the corresponding fetch methods. This |
| 13370 | * is very particular ACL declaration mode. |
| 13371 | */ |
| 13372 | { "http_auth_group", NULL, PAT_MATCH_STR, NULL, pat_idx_list_str, pat_del_list_ptr, NULL, pat_match_auth }, |
| 13373 | { "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] | 13374 | |
Thierry FOURNIER | c5a4e98 | 2014-03-05 16:07:08 +0100 | [diff] [blame] | 13375 | { "path", "path", PAT_MATCH_STR }, |
| 13376 | { "path_beg", "path", PAT_MATCH_BEG }, |
| 13377 | { "path_dir", "path", PAT_MATCH_DIR }, |
| 13378 | { "path_dom", "path", PAT_MATCH_DOM }, |
| 13379 | { "path_end", "path", PAT_MATCH_END }, |
| 13380 | { "path_len", "path", PAT_MATCH_LEN }, |
| 13381 | { "path_reg", "path", PAT_MATCH_REG }, |
| 13382 | { "path_sub", "path", PAT_MATCH_SUB }, |
Willy Tarreau | 25c1ebc | 2012-04-25 16:21:44 +0200 | [diff] [blame] | 13383 | |
Thierry FOURNIER | c5a4e98 | 2014-03-05 16:07:08 +0100 | [diff] [blame] | 13384 | { "req_ver", "req.ver", PAT_MATCH_STR }, |
| 13385 | { "resp_ver", "res.ver", PAT_MATCH_STR }, |
Willy Tarreau | 25c1ebc | 2012-04-25 16:21:44 +0200 | [diff] [blame] | 13386 | |
Thierry FOURNIER | c5a4e98 | 2014-03-05 16:07:08 +0100 | [diff] [blame] | 13387 | { "scook", "res.cook", PAT_MATCH_STR }, |
| 13388 | { "scook_beg", "res.cook", PAT_MATCH_BEG }, |
| 13389 | { "scook_dir", "res.cook", PAT_MATCH_DIR }, |
| 13390 | { "scook_dom", "res.cook", PAT_MATCH_DOM }, |
| 13391 | { "scook_end", "res.cook", PAT_MATCH_END }, |
| 13392 | { "scook_len", "res.cook", PAT_MATCH_LEN }, |
| 13393 | { "scook_reg", "res.cook", PAT_MATCH_REG }, |
| 13394 | { "scook_sub", "res.cook", PAT_MATCH_SUB }, |
Willy Tarreau | 25c1ebc | 2012-04-25 16:21:44 +0200 | [diff] [blame] | 13395 | |
Thierry FOURNIER | c5a4e98 | 2014-03-05 16:07:08 +0100 | [diff] [blame] | 13396 | { "shdr", "res.hdr", PAT_MATCH_STR }, |
| 13397 | { "shdr_beg", "res.hdr", PAT_MATCH_BEG }, |
| 13398 | { "shdr_dir", "res.hdr", PAT_MATCH_DIR }, |
| 13399 | { "shdr_dom", "res.hdr", PAT_MATCH_DOM }, |
| 13400 | { "shdr_end", "res.hdr", PAT_MATCH_END }, |
| 13401 | { "shdr_len", "res.hdr", PAT_MATCH_LEN }, |
| 13402 | { "shdr_reg", "res.hdr", PAT_MATCH_REG }, |
| 13403 | { "shdr_sub", "res.hdr", PAT_MATCH_SUB }, |
Willy Tarreau | 25c1ebc | 2012-04-25 16:21:44 +0200 | [diff] [blame] | 13404 | |
Thierry FOURNIER | c5a4e98 | 2014-03-05 16:07:08 +0100 | [diff] [blame] | 13405 | { "url", "url", PAT_MATCH_STR }, |
| 13406 | { "url_beg", "url", PAT_MATCH_BEG }, |
| 13407 | { "url_dir", "url", PAT_MATCH_DIR }, |
| 13408 | { "url_dom", "url", PAT_MATCH_DOM }, |
| 13409 | { "url_end", "url", PAT_MATCH_END }, |
| 13410 | { "url_len", "url", PAT_MATCH_LEN }, |
| 13411 | { "url_reg", "url", PAT_MATCH_REG }, |
| 13412 | { "url_sub", "url", PAT_MATCH_SUB }, |
Willy Tarreau | 25c1ebc | 2012-04-25 16:21:44 +0200 | [diff] [blame] | 13413 | |
Thierry FOURNIER | c5a4e98 | 2014-03-05 16:07:08 +0100 | [diff] [blame] | 13414 | { "urlp", "urlp", PAT_MATCH_STR }, |
| 13415 | { "urlp_beg", "urlp", PAT_MATCH_BEG }, |
| 13416 | { "urlp_dir", "urlp", PAT_MATCH_DIR }, |
| 13417 | { "urlp_dom", "urlp", PAT_MATCH_DOM }, |
| 13418 | { "urlp_end", "urlp", PAT_MATCH_END }, |
| 13419 | { "urlp_len", "urlp", PAT_MATCH_LEN }, |
| 13420 | { "urlp_reg", "urlp", PAT_MATCH_REG }, |
| 13421 | { "urlp_sub", "urlp", PAT_MATCH_SUB }, |
Willy Tarreau | 25c1ebc | 2012-04-25 16:21:44 +0200 | [diff] [blame] | 13422 | |
Willy Tarreau | 8ed669b | 2013-01-11 15:49:37 +0100 | [diff] [blame] | 13423 | { /* END */ }, |
Willy Tarreau | 25c1ebc | 2012-04-25 16:21:44 +0200 | [diff] [blame] | 13424 | }}; |
| 13425 | |
| 13426 | /************************************************************************/ |
| 13427 | /* All supported pattern keywords must be declared here. */ |
Willy Tarreau | 4a56897 | 2010-05-12 08:08:50 +0200 | [diff] [blame] | 13428 | /************************************************************************/ |
| 13429 | /* Note: must not be declared <const> as its list will be overwritten */ |
Willy Tarreau | dc13c11 | 2013-06-21 23:16:39 +0200 | [diff] [blame] | 13430 | static struct sample_fetch_kw_list sample_fetch_keywords = {ILH, { |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 13431 | { "base", smp_fetch_base, 0, NULL, SMP_T_STR, SMP_USE_HRQHV }, |
Thierry FOURNIER | 07ee64e | 2015-07-06 23:43:03 +0200 | [diff] [blame] | 13432 | { "base32", smp_fetch_base32, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV }, |
Willy Tarreau | 409bcde | 2013-01-08 00:31:00 +0100 | [diff] [blame] | 13433 | { "base32+src", smp_fetch_base32_src, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV }, |
| 13434 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 13435 | /* capture are allocated and are permanent in the stream */ |
Thierry FOURNIER | bf65cd4 | 2015-07-20 17:45:02 +0200 | [diff] [blame] | 13436 | { "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] | 13437 | |
| 13438 | /* retrieve these captures from the HTTP logs */ |
Thierry FOURNIER | bf65cd4 | 2015-07-20 17:45:02 +0200 | [diff] [blame] | 13439 | { "capture.req.method", smp_fetch_capture_req_method, 0, NULL, SMP_T_STR, SMP_USE_HRQHP }, |
| 13440 | { "capture.req.uri", smp_fetch_capture_req_uri, 0, NULL, SMP_T_STR, SMP_USE_HRQHP }, |
| 13441 | { "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] | 13442 | |
Thierry FOURNIER | bf65cd4 | 2015-07-20 17:45:02 +0200 | [diff] [blame] | 13443 | { "capture.res.hdr", smp_fetch_capture_header_res, ARG1(1,SINT), NULL, SMP_T_STR, SMP_USE_HRSHP }, |
| 13444 | { "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] | 13445 | |
Willy Tarreau | 409bcde | 2013-01-08 00:31:00 +0100 | [diff] [blame] | 13446 | /* cookie is valid in both directions (eg: for "stick ...") but cook* |
| 13447 | * are only here to match the ACL's name, are request-only and are used |
| 13448 | * for ACL compatibility only. |
| 13449 | */ |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 13450 | { "cook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRQHV }, |
| 13451 | { "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] | 13452 | { "cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV }, |
| 13453 | { "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] | 13454 | |
| 13455 | /* hdr is valid in both directions (eg: for "stick ...") but hdr_* are |
| 13456 | * only here to match the ACL's name, are request-only and are used for |
| 13457 | * ACL compatibility only. |
| 13458 | */ |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 13459 | { "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] | 13460 | { "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] | 13461 | { "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] | 13462 | { "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] | 13463 | |
Willy Tarreau | 0a0daec | 2013-04-02 22:44:58 +0200 | [diff] [blame] | 13464 | { "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] | 13465 | { "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] | 13466 | { "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] | 13467 | { "method", smp_fetch_meth, 0, NULL, SMP_T_METH, SMP_USE_HRQHP }, |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 13468 | { "path", smp_fetch_path, 0, NULL, SMP_T_STR, SMP_USE_HRQHV }, |
Willy Tarreau | 49ad95c | 2015-01-19 15:06:26 +0100 | [diff] [blame] | 13469 | { "query", smp_fetch_query, 0, NULL, SMP_T_STR, SMP_USE_HRQHV }, |
Willy Tarreau | 18ed256 | 2013-01-14 15:56:36 +0100 | [diff] [blame] | 13470 | |
| 13471 | /* HTTP protocol on the request path */ |
| 13472 | { "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] | 13473 | { "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] | 13474 | |
| 13475 | /* HTTP version on the request path */ |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 13476 | { "req.ver", smp_fetch_rqver, 0, NULL, SMP_T_STR, SMP_USE_HRQHV }, |
| 13477 | { "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] | 13478 | |
Willy Tarreau | a5910cc | 2015-05-02 00:46:08 +0200 | [diff] [blame] | 13479 | { "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] | 13480 | { "req.body_len", smp_fetch_body_len, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV }, |
| 13481 | { "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] | 13482 | { "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] | 13483 | |
Thierry FOURNIER | d7d8881 | 2017-04-19 15:15:14 +0200 | [diff] [blame] | 13484 | { "req.hdrs", smp_fetch_hdrs, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV }, |
Thierry FOURNIER | 5617dce | 2017-04-09 05:38:19 +0200 | [diff] [blame] | 13485 | { "req.hdrs_bin", smp_fetch_hdrs_bin, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV }, |
| 13486 | |
Willy Tarreau | 18ed256 | 2013-01-14 15:56:36 +0100 | [diff] [blame] | 13487 | /* HTTP version on the response path */ |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 13488 | { "res.ver", smp_fetch_stver, 0, NULL, SMP_T_STR, SMP_USE_HRSHV }, |
| 13489 | { "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] | 13490 | |
Willy Tarreau | 18ed256 | 2013-01-14 15:56:36 +0100 | [diff] [blame] | 13491 | /* 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] | 13492 | { "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] | 13493 | { "req.cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV }, |
| 13494 | { "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] | 13495 | |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 13496 | { "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] | 13497 | { "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] | 13498 | { "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] | 13499 | { "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] | 13500 | { "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] | 13501 | { "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] | 13502 | { "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] | 13503 | |
| 13504 | /* 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] | 13505 | { "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] | 13506 | { "res.cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV }, |
| 13507 | { "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] | 13508 | |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 13509 | { "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] | 13510 | { "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] | 13511 | { "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] | 13512 | { "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] | 13513 | { "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] | 13514 | { "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] | 13515 | { "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] | 13516 | |
Willy Tarreau | 409bcde | 2013-01-08 00:31:00 +0100 | [diff] [blame] | 13517 | /* 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] | 13518 | { "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] | 13519 | { "scook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV }, |
| 13520 | { "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] | 13521 | { "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] | 13522 | |
| 13523 | /* 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] | 13524 | { "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] | 13525 | { "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] | 13526 | { "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] | 13527 | { "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] | 13528 | |
Thierry FOURNIER | 07ee64e | 2015-07-06 23:43:03 +0200 | [diff] [blame] | 13529 | { "status", smp_fetch_stcode, 0, NULL, SMP_T_SINT, SMP_USE_HRSHP }, |
Thierry Fournier | 0e00dca | 2016-04-07 15:47:40 +0200 | [diff] [blame] | 13530 | { "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] | 13531 | { "url", smp_fetch_url, 0, NULL, SMP_T_STR, SMP_USE_HRQHV }, |
Thierry FOURNIER | 07ee64e | 2015-07-06 23:43:03 +0200 | [diff] [blame] | 13532 | { "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] | 13533 | { "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] | 13534 | { "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] | 13535 | { "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] | 13536 | { "url_param", smp_fetch_url_param, ARG2(0,STR,STR), NULL, SMP_T_STR, SMP_USE_HRQHV }, |
| 13537 | { "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] | 13538 | { "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] | 13539 | { /* END */ }, |
Willy Tarreau | 4a56897 | 2010-05-12 08:08:50 +0200 | [diff] [blame] | 13540 | }}; |
| 13541 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 13542 | |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 13543 | /************************************************************************/ |
| 13544 | /* All supported converter keywords must be declared here. */ |
| 13545 | /************************************************************************/ |
Willy Tarreau | 276fae9 | 2013-07-25 14:36:01 +0200 | [diff] [blame] | 13546 | /* Note: must not be declared <const> as its list will be overwritten */ |
| 13547 | static struct sample_conv_kw_list sample_conv_kws = {ILH, { |
Thierry FOURNIER | 07ee64e | 2015-07-06 23:43:03 +0200 | [diff] [blame] | 13548 | { "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] | 13549 | { "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] | 13550 | { "capture-req", smp_conv_req_capture, ARG1(1,SINT), NULL, SMP_T_STR, SMP_T_STR}, |
| 13551 | { "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] | 13552 | { "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] | 13553 | { NULL, NULL, 0, 0, 0 }, |
| 13554 | }}; |
| 13555 | |
Thierry FOURNIER | 35ab275 | 2015-05-28 13:22:03 +0200 | [diff] [blame] | 13556 | |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 13557 | /************************************************************************/ |
| 13558 | /* All supported http-request action keywords must be declared here. */ |
| 13559 | /************************************************************************/ |
Thierry FOURNIER | 36481b8 | 2015-08-19 09:01:53 +0200 | [diff] [blame] | 13560 | struct action_kw_list http_req_actions = { |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 13561 | .kw = { |
Willy Tarreau | a9083d0 | 2015-05-08 15:27:59 +0200 | [diff] [blame] | 13562 | { "capture", parse_http_req_capture }, |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 13563 | { "set-method", parse_set_req_line }, |
| 13564 | { "set-path", parse_set_req_line }, |
| 13565 | { "set-query", parse_set_req_line }, |
| 13566 | { "set-uri", parse_set_req_line }, |
Willy Tarreau | cb703b0 | 2015-04-03 09:52:01 +0200 | [diff] [blame] | 13567 | { NULL, NULL } |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 13568 | } |
| 13569 | }; |
| 13570 | |
Thierry FOURNIER | 36481b8 | 2015-08-19 09:01:53 +0200 | [diff] [blame] | 13571 | struct action_kw_list http_res_actions = { |
Thierry FOURNIER | e80fada | 2015-05-26 18:06:31 +0200 | [diff] [blame] | 13572 | .kw = { |
| 13573 | { "capture", parse_http_res_capture }, |
Thierry FOURNIER | 35d70ef | 2015-08-26 16:21:56 +0200 | [diff] [blame] | 13574 | { "set-status", parse_http_set_status }, |
Thierry FOURNIER | e80fada | 2015-05-26 18:06:31 +0200 | [diff] [blame] | 13575 | { NULL, NULL } |
| 13576 | } |
| 13577 | }; |
| 13578 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 13579 | __attribute__((constructor)) |
| 13580 | static void __http_protocol_init(void) |
| 13581 | { |
| 13582 | acl_register_keywords(&acl_kws); |
Willy Tarreau | 1278578 | 2012-04-27 21:37:17 +0200 | [diff] [blame] | 13583 | sample_register_fetches(&sample_fetch_keywords); |
Willy Tarreau | 276fae9 | 2013-07-25 14:36:01 +0200 | [diff] [blame] | 13584 | sample_register_convs(&sample_conv_kws); |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 13585 | http_req_keywords_register(&http_req_actions); |
Thierry FOURNIER | e80fada | 2015-05-26 18:06:31 +0200 | [diff] [blame] | 13586 | http_res_keywords_register(&http_res_actions); |
Willy Tarreau | 12207b3 | 2016-11-22 19:48:51 +0100 | [diff] [blame] | 13587 | cli_register_kw(&cli_kws); |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 13588 | } |
| 13589 | |
| 13590 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 13591 | /* |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 13592 | * Local variables: |
| 13593 | * c-indent-level: 8 |
| 13594 | * c-basic-offset: 8 |
| 13595 | * End: |
| 13596 | */ |