Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1 | /* |
| 2 | * HTTP protocol analyzer |
| 3 | * |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 4 | * Copyright 2000-2011 Willy Tarreau <w@1wt.eu> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 5 | * |
| 6 | * This program is free software; you can redistribute it and/or |
| 7 | * modify it under the terms of the GNU General Public License |
| 8 | * as published by the Free Software Foundation; either version |
| 9 | * 2 of the License, or (at your option) any later version. |
| 10 | * |
| 11 | */ |
| 12 | |
| 13 | #include <ctype.h> |
| 14 | #include <errno.h> |
| 15 | #include <fcntl.h> |
| 16 | #include <stdio.h> |
| 17 | #include <stdlib.h> |
| 18 | #include <string.h> |
| 19 | #include <syslog.h> |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 20 | #include <time.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 21 | |
| 22 | #include <sys/socket.h> |
| 23 | #include <sys/stat.h> |
| 24 | #include <sys/types.h> |
| 25 | |
Willy Tarreau | b05405a | 2012-01-23 15:35:52 +0100 | [diff] [blame] | 26 | #include <netinet/tcp.h> |
| 27 | |
Willy Tarreau | 2dd0d47 | 2006-06-29 17:53:05 +0200 | [diff] [blame] | 28 | #include <common/appsession.h> |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 29 | #include <common/base64.h> |
Willy Tarreau | c7e4238 | 2012-08-24 19:22:53 +0200 | [diff] [blame] | 30 | #include <common/chunk.h> |
Willy Tarreau | 2dd0d47 | 2006-06-29 17:53:05 +0200 | [diff] [blame] | 31 | #include <common/compat.h> |
| 32 | #include <common/config.h> |
Willy Tarreau | a4cd1f5 | 2006-12-16 19:57:26 +0100 | [diff] [blame] | 33 | #include <common/debug.h> |
Willy Tarreau | 2dd0d47 | 2006-06-29 17:53:05 +0200 | [diff] [blame] | 34 | #include <common/memory.h> |
| 35 | #include <common/mini-clist.h> |
| 36 | #include <common/standard.h> |
Willy Tarreau | 0c303ee | 2008-07-07 00:09:58 +0200 | [diff] [blame] | 37 | #include <common/ticks.h> |
Willy Tarreau | 2dd0d47 | 2006-06-29 17:53:05 +0200 | [diff] [blame] | 38 | #include <common/time.h> |
| 39 | #include <common/uri_auth.h> |
| 40 | #include <common/version.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 41 | |
| 42 | #include <types/capture.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 43 | #include <types/global.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 44 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 45 | #include <proto/acl.h> |
Willy Tarreau | 61612d4 | 2012-04-19 18:42:05 +0200 | [diff] [blame] | 46 | #include <proto/arg.h> |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 47 | #include <proto/auth.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 48 | #include <proto/backend.h> |
Willy Tarreau | c7e4238 | 2012-08-24 19:22:53 +0200 | [diff] [blame] | 49 | #include <proto/channel.h> |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 50 | #include <proto/checks.h> |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 51 | #include <proto/compression.h> |
Willy Tarreau | 9186126 | 2007-10-17 17:06:05 +0200 | [diff] [blame] | 52 | #include <proto/dumpstats.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 53 | #include <proto/fd.h> |
Willy Tarreau | 03fa5df | 2010-05-24 21:02:37 +0200 | [diff] [blame] | 54 | #include <proto/frontend.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 55 | #include <proto/log.h> |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 56 | #include <proto/hdr_idx.h> |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 57 | #include <proto/pattern.h> |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 58 | #include <proto/proto_tcp.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 59 | #include <proto/proto_http.h> |
Willy Tarreau | 7f062c4 | 2009-03-05 18:43:00 +0100 | [diff] [blame] | 60 | #include <proto/proxy.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 61 | #include <proto/queue.h> |
Willy Tarreau | cd3b094 | 2012-04-27 21:52:18 +0200 | [diff] [blame] | 62 | #include <proto/sample.h> |
Willy Tarreau | 7f062c4 | 2009-03-05 18:43:00 +0100 | [diff] [blame] | 63 | #include <proto/server.h> |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 64 | #include <proto/stream.h> |
Willy Tarreau | cff6411 | 2008-11-03 06:26:53 +0100 | [diff] [blame] | 65 | #include <proto/stream_interface.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 66 | #include <proto/task.h> |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 67 | #include <proto/pattern.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 68 | |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 69 | const char HTTP_100[] = |
| 70 | "HTTP/1.1 100 Continue\r\n\r\n"; |
| 71 | |
| 72 | const struct chunk http_100_chunk = { |
| 73 | .str = (char *)&HTTP_100, |
| 74 | .len = sizeof(HTTP_100)-1 |
| 75 | }; |
| 76 | |
Willy Tarreau | a9679ac | 2010-01-03 17:32:57 +0100 | [diff] [blame] | 77 | /* Warning: no "connection" header is provided with the 3xx messages below */ |
Willy Tarreau | b463dfb | 2008-06-07 23:08:56 +0200 | [diff] [blame] | 78 | const char *HTTP_301 = |
Willy Tarreau | bc5aa19 | 2010-01-03 15:09:36 +0100 | [diff] [blame] | 79 | "HTTP/1.1 301 Moved Permanently\r\n" |
Willy Tarreau | bc5aa19 | 2010-01-03 15:09:36 +0100 | [diff] [blame] | 80 | "Content-length: 0\r\n" |
Willy Tarreau | b463dfb | 2008-06-07 23:08:56 +0200 | [diff] [blame] | 81 | "Location: "; /* not terminated since it will be concatenated with the URL */ |
| 82 | |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 83 | const char *HTTP_302 = |
Willy Tarreau | bc5aa19 | 2010-01-03 15:09:36 +0100 | [diff] [blame] | 84 | "HTTP/1.1 302 Found\r\n" |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 85 | "Cache-Control: no-cache\r\n" |
Willy Tarreau | bc5aa19 | 2010-01-03 15:09:36 +0100 | [diff] [blame] | 86 | "Content-length: 0\r\n" |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 87 | "Location: "; /* not terminated since it will be concatenated with the URL */ |
| 88 | |
| 89 | /* same as 302 except that the browser MUST retry with the GET method */ |
| 90 | const char *HTTP_303 = |
Willy Tarreau | bc5aa19 | 2010-01-03 15:09:36 +0100 | [diff] [blame] | 91 | "HTTP/1.1 303 See Other\r\n" |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 92 | "Cache-Control: no-cache\r\n" |
Willy Tarreau | bc5aa19 | 2010-01-03 15:09:36 +0100 | [diff] [blame] | 93 | "Content-length: 0\r\n" |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 94 | "Location: "; /* not terminated since it will be concatenated with the URL */ |
| 95 | |
Yves Lafon | 3e8d1ae | 2013-03-11 11:06:05 -0400 | [diff] [blame] | 96 | |
| 97 | /* same as 302 except that the browser MUST retry with the same method */ |
| 98 | const char *HTTP_307 = |
| 99 | "HTTP/1.1 307 Temporary Redirect\r\n" |
| 100 | "Cache-Control: no-cache\r\n" |
| 101 | "Content-length: 0\r\n" |
| 102 | "Location: "; /* not terminated since it will be concatenated with the URL */ |
| 103 | |
| 104 | /* same as 301 except that the browser MUST retry with the same method */ |
| 105 | const char *HTTP_308 = |
| 106 | "HTTP/1.1 308 Permanent Redirect\r\n" |
| 107 | "Content-length: 0\r\n" |
| 108 | "Location: "; /* not terminated since it will be concatenated with the URL */ |
| 109 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 110 | /* Warning: this one is an sprintf() fmt string, with <realm> as its only argument */ |
| 111 | const char *HTTP_401_fmt = |
| 112 | "HTTP/1.0 401 Unauthorized\r\n" |
| 113 | "Cache-Control: no-cache\r\n" |
| 114 | "Connection: close\r\n" |
Willy Tarreau | 791d66d | 2006-07-08 16:53:38 +0200 | [diff] [blame] | 115 | "Content-Type: text/html\r\n" |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 116 | "WWW-Authenticate: Basic realm=\"%s\"\r\n" |
| 117 | "\r\n" |
| 118 | "<html><body><h1>401 Unauthorized</h1>\nYou need a valid user and password to access this content.\n</body></html>\n"; |
| 119 | |
Willy Tarreau | 844a7e7 | 2010-01-31 21:46:18 +0100 | [diff] [blame] | 120 | const char *HTTP_407_fmt = |
| 121 | "HTTP/1.0 407 Unauthorized\r\n" |
| 122 | "Cache-Control: no-cache\r\n" |
| 123 | "Connection: close\r\n" |
| 124 | "Content-Type: text/html\r\n" |
| 125 | "Proxy-Authenticate: Basic realm=\"%s\"\r\n" |
| 126 | "\r\n" |
Godbach | 1f1fae6 | 2014-12-17 16:32:05 +0800 | [diff] [blame] | 127 | "<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] | 128 | |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 129 | |
| 130 | const int http_err_codes[HTTP_ERR_SIZE] = { |
Willy Tarreau | ae94d4d | 2011-05-11 16:28:49 +0200 | [diff] [blame] | 131 | [HTTP_ERR_200] = 200, /* used by "monitor-uri" */ |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 132 | [HTTP_ERR_400] = 400, |
| 133 | [HTTP_ERR_403] = 403, |
CJ Ess | 108b1dd | 2015-04-07 12:03:37 -0400 | [diff] [blame] | 134 | [HTTP_ERR_405] = 405, |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 135 | [HTTP_ERR_408] = 408, |
CJ Ess | 108b1dd | 2015-04-07 12:03:37 -0400 | [diff] [blame] | 136 | [HTTP_ERR_429] = 429, |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 137 | [HTTP_ERR_500] = 500, |
| 138 | [HTTP_ERR_502] = 502, |
| 139 | [HTTP_ERR_503] = 503, |
| 140 | [HTTP_ERR_504] = 504, |
| 141 | }; |
| 142 | |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 143 | static const char *http_err_msgs[HTTP_ERR_SIZE] = { |
Willy Tarreau | ae94d4d | 2011-05-11 16:28:49 +0200 | [diff] [blame] | 144 | [HTTP_ERR_200] = |
| 145 | "HTTP/1.0 200 OK\r\n" |
| 146 | "Cache-Control: no-cache\r\n" |
| 147 | "Connection: close\r\n" |
| 148 | "Content-Type: text/html\r\n" |
| 149 | "\r\n" |
| 150 | "<html><body><h1>200 OK</h1>\nService ready.\n</body></html>\n", |
| 151 | |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 152 | [HTTP_ERR_400] = |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 153 | "HTTP/1.0 400 Bad request\r\n" |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 154 | "Cache-Control: no-cache\r\n" |
| 155 | "Connection: close\r\n" |
| 156 | "Content-Type: text/html\r\n" |
| 157 | "\r\n" |
| 158 | "<html><body><h1>400 Bad request</h1>\nYour browser sent an invalid request.\n</body></html>\n", |
| 159 | |
| 160 | [HTTP_ERR_403] = |
| 161 | "HTTP/1.0 403 Forbidden\r\n" |
| 162 | "Cache-Control: no-cache\r\n" |
| 163 | "Connection: close\r\n" |
| 164 | "Content-Type: text/html\r\n" |
| 165 | "\r\n" |
| 166 | "<html><body><h1>403 Forbidden</h1>\nRequest forbidden by administrative rules.\n</body></html>\n", |
| 167 | |
CJ Ess | 108b1dd | 2015-04-07 12:03:37 -0400 | [diff] [blame] | 168 | [HTTP_ERR_405] = |
| 169 | "HTTP/1.0 405 Method Not Allowed\r\n" |
| 170 | "Cache-Control: no-cache\r\n" |
| 171 | "Connection: close\r\n" |
| 172 | "Content-Type: text/html\r\n" |
| 173 | "\r\n" |
| 174 | "<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", |
| 175 | |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 176 | [HTTP_ERR_408] = |
| 177 | "HTTP/1.0 408 Request Time-out\r\n" |
| 178 | "Cache-Control: no-cache\r\n" |
| 179 | "Connection: close\r\n" |
| 180 | "Content-Type: text/html\r\n" |
| 181 | "\r\n" |
| 182 | "<html><body><h1>408 Request Time-out</h1>\nYour browser didn't send a complete request in time.\n</body></html>\n", |
| 183 | |
CJ Ess | 108b1dd | 2015-04-07 12:03:37 -0400 | [diff] [blame] | 184 | [HTTP_ERR_429] = |
| 185 | "HTTP/1.0 429 Too Many Requests\r\n" |
| 186 | "Cache-Control: no-cache\r\n" |
| 187 | "Connection: close\r\n" |
| 188 | "Content-Type: text/html\r\n" |
| 189 | "\r\n" |
| 190 | "<html><body><h1>429 Too Many Requests</h1>\nYou have sent too many requests in a given amount of time.\n</body></html>\n", |
| 191 | |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 192 | [HTTP_ERR_500] = |
| 193 | "HTTP/1.0 500 Server Error\r\n" |
| 194 | "Cache-Control: no-cache\r\n" |
| 195 | "Connection: close\r\n" |
| 196 | "Content-Type: text/html\r\n" |
| 197 | "\r\n" |
| 198 | "<html><body><h1>500 Server Error</h1>\nAn internal server error occured.\n</body></html>\n", |
| 199 | |
| 200 | [HTTP_ERR_502] = |
| 201 | "HTTP/1.0 502 Bad Gateway\r\n" |
| 202 | "Cache-Control: no-cache\r\n" |
| 203 | "Connection: close\r\n" |
| 204 | "Content-Type: text/html\r\n" |
| 205 | "\r\n" |
| 206 | "<html><body><h1>502 Bad Gateway</h1>\nThe server returned an invalid or incomplete response.\n</body></html>\n", |
| 207 | |
| 208 | [HTTP_ERR_503] = |
| 209 | "HTTP/1.0 503 Service Unavailable\r\n" |
| 210 | "Cache-Control: no-cache\r\n" |
| 211 | "Connection: close\r\n" |
| 212 | "Content-Type: text/html\r\n" |
| 213 | "\r\n" |
| 214 | "<html><body><h1>503 Service Unavailable</h1>\nNo server is available to handle this request.\n</body></html>\n", |
| 215 | |
| 216 | [HTTP_ERR_504] = |
| 217 | "HTTP/1.0 504 Gateway Time-out\r\n" |
| 218 | "Cache-Control: no-cache\r\n" |
| 219 | "Connection: close\r\n" |
| 220 | "Content-Type: text/html\r\n" |
| 221 | "\r\n" |
| 222 | "<html><body><h1>504 Gateway Time-out</h1>\nThe server didn't respond in time.\n</body></html>\n", |
| 223 | |
| 224 | }; |
| 225 | |
Cyril Bonté | 19979e1 | 2012-04-04 12:57:21 +0200 | [diff] [blame] | 226 | /* status codes available for the stats admin page (strictly 4 chars length) */ |
| 227 | const char *stat_status_codes[STAT_STATUS_SIZE] = { |
| 228 | [STAT_STATUS_DENY] = "DENY", |
| 229 | [STAT_STATUS_DONE] = "DONE", |
| 230 | [STAT_STATUS_ERRP] = "ERRP", |
| 231 | [STAT_STATUS_EXCD] = "EXCD", |
| 232 | [STAT_STATUS_NONE] = "NONE", |
| 233 | [STAT_STATUS_PART] = "PART", |
| 234 | [STAT_STATUS_UNKN] = "UNKN", |
| 235 | }; |
| 236 | |
| 237 | |
William Lallemand | 73025dd | 2014-04-24 14:38:37 +0200 | [diff] [blame] | 238 | /* List head of all known action keywords for "http-request" */ |
| 239 | struct http_req_action_kw_list http_req_keywords = { |
| 240 | .list = LIST_HEAD_INIT(http_req_keywords.list) |
| 241 | }; |
| 242 | |
| 243 | /* List head of all known action keywords for "http-response" */ |
| 244 | struct http_res_action_kw_list http_res_keywords = { |
| 245 | .list = LIST_HEAD_INIT(http_res_keywords.list) |
| 246 | }; |
| 247 | |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 248 | /* We must put the messages here since GCC cannot initialize consts depending |
| 249 | * on strlen(). |
| 250 | */ |
| 251 | struct chunk http_err_chunks[HTTP_ERR_SIZE]; |
| 252 | |
Willy Tarreau | a890d07 | 2013-04-02 12:01:06 +0200 | [diff] [blame] | 253 | /* this struct is used between calls to smp_fetch_hdr() or smp_fetch_cookie() */ |
| 254 | static struct hdr_ctx static_hdr_ctx; |
| 255 | |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 256 | #define FD_SETS_ARE_BITFIELDS |
| 257 | #ifdef FD_SETS_ARE_BITFIELDS |
| 258 | /* |
| 259 | * This map is used with all the FD_* macros to check whether a particular bit |
| 260 | * is set or not. Each bit represents an ACSII code. FD_SET() sets those bytes |
| 261 | * which should be encoded. When FD_ISSET() returns non-zero, it means that the |
| 262 | * byte should be encoded. Be careful to always pass bytes from 0 to 255 |
| 263 | * exclusively to the macros. |
| 264 | */ |
| 265 | fd_set hdr_encode_map[(sizeof(fd_set) > (256/8)) ? 1 : ((256/8) / sizeof(fd_set))]; |
| 266 | 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] | 267 | 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] | 268 | |
| 269 | #else |
| 270 | #error "Check if your OS uses bitfields for fd_sets" |
| 271 | #endif |
| 272 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 273 | 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] | 274 | |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 275 | void init_proto_http() |
| 276 | { |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 277 | int i; |
| 278 | char *tmp; |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 279 | int msg; |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 280 | |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 281 | for (msg = 0; msg < HTTP_ERR_SIZE; msg++) { |
| 282 | if (!http_err_msgs[msg]) { |
| 283 | Alert("Internal error: no message defined for HTTP return code %d. Aborting.\n", msg); |
| 284 | abort(); |
| 285 | } |
| 286 | |
| 287 | http_err_chunks[msg].str = (char *)http_err_msgs[msg]; |
| 288 | http_err_chunks[msg].len = strlen(http_err_msgs[msg]); |
| 289 | } |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 290 | |
| 291 | /* initialize the log header encoding map : '{|}"#' should be encoded with |
| 292 | * '#' as prefix, as well as non-printable characters ( <32 or >= 127 ). |
| 293 | * URL encoding only requires '"', '#' to be encoded as well as non- |
| 294 | * printable characters above. |
| 295 | */ |
| 296 | memset(hdr_encode_map, 0, sizeof(hdr_encode_map)); |
| 297 | memset(url_encode_map, 0, sizeof(url_encode_map)); |
Thierry FOURNIER | d048d8b | 2014-03-13 16:46:18 +0100 | [diff] [blame] | 298 | memset(http_encode_map, 0, sizeof(url_encode_map)); |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 299 | for (i = 0; i < 32; i++) { |
| 300 | FD_SET(i, hdr_encode_map); |
| 301 | FD_SET(i, url_encode_map); |
| 302 | } |
| 303 | for (i = 127; i < 256; i++) { |
| 304 | FD_SET(i, hdr_encode_map); |
| 305 | FD_SET(i, url_encode_map); |
| 306 | } |
| 307 | |
| 308 | tmp = "\"#{|}"; |
| 309 | while (*tmp) { |
| 310 | FD_SET(*tmp, hdr_encode_map); |
| 311 | tmp++; |
| 312 | } |
| 313 | |
| 314 | tmp = "\"#"; |
| 315 | while (*tmp) { |
| 316 | FD_SET(*tmp, url_encode_map); |
| 317 | tmp++; |
| 318 | } |
Willy Tarreau | 332f8bf | 2007-05-13 21:36:56 +0200 | [diff] [blame] | 319 | |
Thierry FOURNIER | d048d8b | 2014-03-13 16:46:18 +0100 | [diff] [blame] | 320 | /* initialize the http header encoding map. The draft httpbis define the |
| 321 | * header content as: |
| 322 | * |
| 323 | * HTTP-message = start-line |
| 324 | * *( header-field CRLF ) |
| 325 | * CRLF |
| 326 | * [ message-body ] |
| 327 | * header-field = field-name ":" OWS field-value OWS |
| 328 | * field-value = *( field-content / obs-fold ) |
| 329 | * field-content = field-vchar [ 1*( SP / HTAB ) field-vchar ] |
| 330 | * obs-fold = CRLF 1*( SP / HTAB ) |
| 331 | * field-vchar = VCHAR / obs-text |
| 332 | * VCHAR = %x21-7E |
| 333 | * obs-text = %x80-FF |
| 334 | * |
| 335 | * All the chars are encoded except "VCHAR", "obs-text", SP and HTAB. |
| 336 | * The encoded chars are form 0x00 to 0x08, 0x0a to 0x1f and 0x7f. The |
| 337 | * "obs-fold" is volontary forgotten because haproxy remove this. |
| 338 | */ |
| 339 | memset(http_encode_map, 0, sizeof(http_encode_map)); |
| 340 | for (i = 0x00; i <= 0x08; i++) |
| 341 | FD_SET(i, http_encode_map); |
| 342 | for (i = 0x0a; i <= 0x1f; i++) |
| 343 | FD_SET(i, http_encode_map); |
| 344 | FD_SET(0x7f, http_encode_map); |
| 345 | |
Willy Tarreau | 332f8bf | 2007-05-13 21:36:56 +0200 | [diff] [blame] | 346 | /* memory allocations */ |
Willy Tarreau | 63986c7 | 2015-04-03 22:55:33 +0200 | [diff] [blame] | 347 | pool2_http_txn = create_pool("http_txn", sizeof(struct http_txn), MEM_F_SHARED); |
Willy Tarreau | 332f8bf | 2007-05-13 21:36:56 +0200 | [diff] [blame] | 348 | pool2_requri = create_pool("requri", REQURI_LEN, MEM_F_SHARED); |
William Lallemand | a73203e | 2012-03-12 12:48:57 +0100 | [diff] [blame] | 349 | pool2_uniqueid = create_pool("uniqueid", UNIQUEID_LEN, MEM_F_SHARED); |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 350 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 351 | |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 352 | /* |
| 353 | * We have 26 list of methods (1 per first letter), each of which can have |
| 354 | * up to 3 entries (2 valid, 1 null). |
| 355 | */ |
| 356 | struct http_method_desc { |
Willy Tarreau | c8987b3 | 2013-12-06 23:43:17 +0100 | [diff] [blame] | 357 | enum http_meth_t meth; |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 358 | int len; |
| 359 | const char text[8]; |
| 360 | }; |
| 361 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 362 | const struct http_method_desc http_methods[26][3] = { |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 363 | ['C' - 'A'] = { |
| 364 | [0] = { .meth = HTTP_METH_CONNECT , .len=7, .text="CONNECT" }, |
| 365 | }, |
| 366 | ['D' - 'A'] = { |
| 367 | [0] = { .meth = HTTP_METH_DELETE , .len=6, .text="DELETE" }, |
| 368 | }, |
| 369 | ['G' - 'A'] = { |
| 370 | [0] = { .meth = HTTP_METH_GET , .len=3, .text="GET" }, |
| 371 | }, |
| 372 | ['H' - 'A'] = { |
| 373 | [0] = { .meth = HTTP_METH_HEAD , .len=4, .text="HEAD" }, |
| 374 | }, |
| 375 | ['P' - 'A'] = { |
| 376 | [0] = { .meth = HTTP_METH_POST , .len=4, .text="POST" }, |
| 377 | [1] = { .meth = HTTP_METH_PUT , .len=3, .text="PUT" }, |
| 378 | }, |
| 379 | ['T' - 'A'] = { |
| 380 | [0] = { .meth = HTTP_METH_TRACE , .len=5, .text="TRACE" }, |
| 381 | }, |
| 382 | /* rest is empty like this : |
| 383 | * [1] = { .meth = HTTP_METH_NONE , .len=0, .text="" }, |
| 384 | */ |
| 385 | }; |
| 386 | |
Thierry FOURNIER | d437314 | 2013-12-17 01:10:10 +0100 | [diff] [blame] | 387 | const struct http_method_name http_known_methods[HTTP_METH_OTHER] = { |
| 388 | [HTTP_METH_NONE] = { "", 0 }, |
| 389 | [HTTP_METH_OPTIONS] = { "OPTIONS", 7 }, |
| 390 | [HTTP_METH_GET] = { "GET", 3 }, |
| 391 | [HTTP_METH_HEAD] = { "HEAD", 4 }, |
| 392 | [HTTP_METH_POST] = { "POST", 4 }, |
| 393 | [HTTP_METH_PUT] = { "PUT", 3 }, |
| 394 | [HTTP_METH_DELETE] = { "DELETE", 6 }, |
| 395 | [HTTP_METH_TRACE] = { "TRACE", 5 }, |
| 396 | [HTTP_METH_CONNECT] = { "CONNECT", 7 }, |
| 397 | }; |
| 398 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 399 | /* 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] | 400 | * table than to perform a boolean AND or OR between two tests. Refer to |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 401 | * RFC2616 for those chars. |
| 402 | */ |
| 403 | |
| 404 | const char http_is_spht[256] = { |
| 405 | [' '] = 1, ['\t'] = 1, |
| 406 | }; |
| 407 | |
| 408 | const char http_is_crlf[256] = { |
| 409 | ['\r'] = 1, ['\n'] = 1, |
| 410 | }; |
| 411 | |
| 412 | const char http_is_lws[256] = { |
| 413 | [' '] = 1, ['\t'] = 1, |
| 414 | ['\r'] = 1, ['\n'] = 1, |
| 415 | }; |
| 416 | |
| 417 | const char http_is_sep[256] = { |
| 418 | ['('] = 1, [')'] = 1, ['<'] = 1, ['>'] = 1, |
| 419 | ['@'] = 1, [','] = 1, [';'] = 1, [':'] = 1, |
| 420 | ['"'] = 1, ['/'] = 1, ['['] = 1, [']'] = 1, |
| 421 | ['{'] = 1, ['}'] = 1, ['?'] = 1, ['='] = 1, |
| 422 | [' '] = 1, ['\t'] = 1, ['\\'] = 1, |
| 423 | }; |
| 424 | |
| 425 | const char http_is_ctl[256] = { |
| 426 | [0 ... 31] = 1, |
| 427 | [127] = 1, |
| 428 | }; |
| 429 | |
| 430 | /* |
| 431 | * A token is any ASCII char that is neither a separator nor a CTL char. |
| 432 | * Do not overwrite values in assignment since gcc-2.95 will not handle |
| 433 | * them correctly. Instead, define every non-CTL char's status. |
| 434 | */ |
| 435 | const char http_is_token[256] = { |
| 436 | [' '] = 0, ['!'] = 1, ['"'] = 0, ['#'] = 1, |
| 437 | ['$'] = 1, ['%'] = 1, ['&'] = 1, ['\''] = 1, |
| 438 | ['('] = 0, [')'] = 0, ['*'] = 1, ['+'] = 1, |
| 439 | [','] = 0, ['-'] = 1, ['.'] = 1, ['/'] = 0, |
| 440 | ['0'] = 1, ['1'] = 1, ['2'] = 1, ['3'] = 1, |
| 441 | ['4'] = 1, ['5'] = 1, ['6'] = 1, ['7'] = 1, |
| 442 | ['8'] = 1, ['9'] = 1, [':'] = 0, [';'] = 0, |
| 443 | ['<'] = 0, ['='] = 0, ['>'] = 0, ['?'] = 0, |
| 444 | ['@'] = 0, ['A'] = 1, ['B'] = 1, ['C'] = 1, |
| 445 | ['D'] = 1, ['E'] = 1, ['F'] = 1, ['G'] = 1, |
| 446 | ['H'] = 1, ['I'] = 1, ['J'] = 1, ['K'] = 1, |
| 447 | ['L'] = 1, ['M'] = 1, ['N'] = 1, ['O'] = 1, |
| 448 | ['P'] = 1, ['Q'] = 1, ['R'] = 1, ['S'] = 1, |
| 449 | ['T'] = 1, ['U'] = 1, ['V'] = 1, ['W'] = 1, |
| 450 | ['X'] = 1, ['Y'] = 1, ['Z'] = 1, ['['] = 0, |
| 451 | ['\\'] = 0, [']'] = 0, ['^'] = 1, ['_'] = 1, |
| 452 | ['`'] = 1, ['a'] = 1, ['b'] = 1, ['c'] = 1, |
| 453 | ['d'] = 1, ['e'] = 1, ['f'] = 1, ['g'] = 1, |
| 454 | ['h'] = 1, ['i'] = 1, ['j'] = 1, ['k'] = 1, |
| 455 | ['l'] = 1, ['m'] = 1, ['n'] = 1, ['o'] = 1, |
| 456 | ['p'] = 1, ['q'] = 1, ['r'] = 1, ['s'] = 1, |
| 457 | ['t'] = 1, ['u'] = 1, ['v'] = 1, ['w'] = 1, |
| 458 | ['x'] = 1, ['y'] = 1, ['z'] = 1, ['{'] = 0, |
| 459 | ['|'] = 1, ['}'] = 0, ['~'] = 1, |
| 460 | }; |
| 461 | |
| 462 | |
Willy Tarreau | 4b89ad4 | 2007-03-04 18:13:58 +0100 | [diff] [blame] | 463 | /* |
| 464 | * An http ver_token is any ASCII which can be found in an HTTP version, |
| 465 | * which includes 'H', 'T', 'P', '/', '.' and any digit. |
| 466 | */ |
| 467 | const char http_is_ver_token[256] = { |
| 468 | ['.'] = 1, ['/'] = 1, |
| 469 | ['0'] = 1, ['1'] = 1, ['2'] = 1, ['3'] = 1, ['4'] = 1, |
| 470 | ['5'] = 1, ['6'] = 1, ['7'] = 1, ['8'] = 1, ['9'] = 1, |
Thierry FOURNIER | 63d692c | 2015-02-28 19:03:56 +0100 | [diff] [blame] | 471 | ['H'] = 1, ['P'] = 1, ['R'] = 1, ['S'] = 1, ['T'] = 1, |
Willy Tarreau | 4b89ad4 | 2007-03-04 18:13:58 +0100 | [diff] [blame] | 472 | }; |
| 473 | |
| 474 | |
Willy Tarreau | 4af6f3a | 2007-03-18 22:36:26 +0100 | [diff] [blame] | 475 | /* |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 476 | * Adds a header and its CRLF at the tail of the message's buffer, just before |
| 477 | * 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] | 478 | * The header is also automatically added to the index <hdr_idx>, and the end |
| 479 | * of headers is automatically adjusted. The number of bytes added is returned |
| 480 | * on success, otherwise <0 is returned indicating an error. |
| 481 | */ |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 482 | 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] | 483 | { |
| 484 | int bytes, len; |
| 485 | |
| 486 | len = strlen(text); |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 487 | 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] | 488 | if (!bytes) |
| 489 | return -1; |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 490 | http_msg_move_end(msg, bytes); |
Willy Tarreau | 4af6f3a | 2007-03-18 22:36:26 +0100 | [diff] [blame] | 491 | return hdr_idx_add(len, 1, hdr_idx, hdr_idx->tail); |
| 492 | } |
| 493 | |
| 494 | /* |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 495 | * Adds a header and its CRLF at the tail of the message's buffer, just before |
| 496 | * 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] | 497 | * the buffer is only opened and the space reserved, but nothing is copied. |
| 498 | * The header is also automatically added to the index <hdr_idx>, and the end |
| 499 | * of headers is automatically adjusted. The number of bytes added is returned |
| 500 | * on success, otherwise <0 is returned indicating an error. |
| 501 | */ |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 502 | int http_header_add_tail2(struct http_msg *msg, |
| 503 | struct hdr_idx *hdr_idx, const char *text, int len) |
Willy Tarreau | 4af6f3a | 2007-03-18 22:36:26 +0100 | [diff] [blame] | 504 | { |
| 505 | int bytes; |
| 506 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 507 | 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] | 508 | if (!bytes) |
| 509 | return -1; |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 510 | http_msg_move_end(msg, bytes); |
Willy Tarreau | 4af6f3a | 2007-03-18 22:36:26 +0100 | [diff] [blame] | 511 | return hdr_idx_add(len, 1, hdr_idx, hdr_idx->tail); |
| 512 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 513 | |
| 514 | /* |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 515 | * Checks if <hdr> is exactly <name> for <len> chars, and ends with a colon. |
| 516 | * If so, returns the position of the first non-space character relative to |
| 517 | * <hdr>, or <end>-<hdr> if not found before. If no value is found, it tries |
| 518 | * to return a pointer to the place after the first space. Returns 0 if the |
| 519 | * header name does not match. Checks are case-insensitive. |
| 520 | */ |
| 521 | int http_header_match2(const char *hdr, const char *end, |
| 522 | const char *name, int len) |
| 523 | { |
| 524 | const char *val; |
| 525 | |
| 526 | if (hdr + len >= end) |
| 527 | return 0; |
| 528 | if (hdr[len] != ':') |
| 529 | return 0; |
| 530 | if (strncasecmp(hdr, name, len) != 0) |
| 531 | return 0; |
| 532 | val = hdr + len + 1; |
| 533 | while (val < end && HTTP_IS_SPHT(*val)) |
| 534 | val++; |
| 535 | if ((val >= end) && (len + 2 <= end - hdr)) |
| 536 | return len + 2; /* we may replace starting from second space */ |
| 537 | return val - hdr; |
| 538 | } |
| 539 | |
Willy Tarreau | 04ff9f1 | 2013-06-10 18:39:42 +0200 | [diff] [blame] | 540 | /* Find the first or next occurrence of header <name> in message buffer <sol> |
| 541 | * using headers index <idx>, and return it in the <ctx> structure. This |
| 542 | * structure holds everything necessary to use the header and find next |
| 543 | * occurrence. If its <idx> member is 0, the header is searched from the |
| 544 | * beginning. Otherwise, the next occurrence is returned. The function returns |
| 545 | * 1 when it finds a value, and 0 when there is no more. It is very similar to |
| 546 | * http_find_header2() except that it is designed to work with full-line headers |
| 547 | * whose comma is not a delimiter but is part of the syntax. As a special case, |
| 548 | * if ctx->val is NULL when searching for a new values of a header, the current |
| 549 | * header is rescanned. This allows rescanning after a header deletion. |
| 550 | */ |
| 551 | int http_find_full_header2(const char *name, int len, |
| 552 | char *sol, struct hdr_idx *idx, |
| 553 | struct hdr_ctx *ctx) |
| 554 | { |
| 555 | char *eol, *sov; |
| 556 | int cur_idx, old_idx; |
| 557 | |
| 558 | cur_idx = ctx->idx; |
| 559 | if (cur_idx) { |
| 560 | /* We have previously returned a header, let's search another one */ |
| 561 | sol = ctx->line; |
| 562 | eol = sol + idx->v[cur_idx].len; |
| 563 | goto next_hdr; |
| 564 | } |
| 565 | |
| 566 | /* first request for this header */ |
| 567 | sol += hdr_idx_first_pos(idx); |
| 568 | old_idx = 0; |
| 569 | cur_idx = hdr_idx_first_idx(idx); |
| 570 | while (cur_idx) { |
| 571 | eol = sol + idx->v[cur_idx].len; |
| 572 | |
| 573 | if (len == 0) { |
| 574 | /* No argument was passed, we want any header. |
| 575 | * To achieve this, we simply build a fake request. */ |
| 576 | while (sol + len < eol && sol[len] != ':') |
| 577 | len++; |
| 578 | name = sol; |
| 579 | } |
| 580 | |
| 581 | if ((len < eol - sol) && |
| 582 | (sol[len] == ':') && |
| 583 | (strncasecmp(sol, name, len) == 0)) { |
| 584 | ctx->del = len; |
| 585 | sov = sol + len + 1; |
| 586 | while (sov < eol && http_is_lws[(unsigned char)*sov]) |
| 587 | sov++; |
| 588 | |
| 589 | ctx->line = sol; |
| 590 | ctx->prev = old_idx; |
| 591 | ctx->idx = cur_idx; |
| 592 | ctx->val = sov - sol; |
| 593 | ctx->tws = 0; |
| 594 | while (eol > sov && http_is_lws[(unsigned char)*(eol - 1)]) { |
| 595 | eol--; |
| 596 | ctx->tws++; |
| 597 | } |
| 598 | ctx->vlen = eol - sov; |
| 599 | return 1; |
| 600 | } |
| 601 | next_hdr: |
| 602 | sol = eol + idx->v[cur_idx].cr + 1; |
| 603 | old_idx = cur_idx; |
| 604 | cur_idx = idx->v[cur_idx].next; |
| 605 | } |
| 606 | return 0; |
| 607 | } |
| 608 | |
Willy Tarreau | c90dc23 | 2015-02-20 13:51:36 +0100 | [diff] [blame] | 609 | /* Find the first or next header field in message buffer <sol> using headers |
| 610 | * index <idx>, and return it in the <ctx> structure. This structure holds |
| 611 | * everything necessary to use the header and find next occurrence. If its |
| 612 | * <idx> member is 0, the first header is retrieved. Otherwise, the next |
| 613 | * occurrence is returned. The function returns 1 when it finds a value, and |
| 614 | * 0 when there is no more. It is equivalent to http_find_full_header2() with |
| 615 | * no header name. |
| 616 | */ |
| 617 | int http_find_next_header(char *sol, struct hdr_idx *idx, struct hdr_ctx *ctx) |
| 618 | { |
| 619 | char *eol, *sov; |
| 620 | int cur_idx, old_idx; |
| 621 | int len; |
| 622 | |
| 623 | cur_idx = ctx->idx; |
| 624 | if (cur_idx) { |
| 625 | /* We have previously returned a header, let's search another one */ |
| 626 | sol = ctx->line; |
| 627 | eol = sol + idx->v[cur_idx].len; |
| 628 | goto next_hdr; |
| 629 | } |
| 630 | |
| 631 | /* first request for this header */ |
| 632 | sol += hdr_idx_first_pos(idx); |
| 633 | old_idx = 0; |
| 634 | cur_idx = hdr_idx_first_idx(idx); |
| 635 | while (cur_idx) { |
| 636 | eol = sol + idx->v[cur_idx].len; |
| 637 | |
| 638 | len = 0; |
| 639 | while (1) { |
| 640 | if (len >= eol - sol) |
| 641 | goto next_hdr; |
| 642 | if (sol[len] == ':') |
| 643 | break; |
| 644 | len++; |
| 645 | } |
| 646 | |
| 647 | ctx->del = len; |
| 648 | sov = sol + len + 1; |
| 649 | while (sov < eol && http_is_lws[(unsigned char)*sov]) |
| 650 | sov++; |
| 651 | |
| 652 | ctx->line = sol; |
| 653 | ctx->prev = old_idx; |
| 654 | ctx->idx = cur_idx; |
| 655 | ctx->val = sov - sol; |
| 656 | ctx->tws = 0; |
| 657 | |
| 658 | while (eol > sov && http_is_lws[(unsigned char)*(eol - 1)]) { |
| 659 | eol--; |
| 660 | ctx->tws++; |
| 661 | } |
| 662 | ctx->vlen = eol - sov; |
| 663 | return 1; |
| 664 | |
| 665 | next_hdr: |
| 666 | sol = eol + idx->v[cur_idx].cr + 1; |
| 667 | old_idx = cur_idx; |
| 668 | cur_idx = idx->v[cur_idx].next; |
| 669 | } |
| 670 | return 0; |
| 671 | } |
| 672 | |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 673 | /* Find the end of the header value contained between <s> and <e>. See RFC2616, |
| 674 | * par 2.2 for more information. Note that it requires a valid header to return |
| 675 | * a valid result. This works for headers defined as comma-separated lists. |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 676 | */ |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 677 | char *find_hdr_value_end(char *s, const char *e) |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 678 | { |
| 679 | int quoted, qdpair; |
| 680 | |
| 681 | quoted = qdpair = 0; |
| 682 | for (; s < e; s++) { |
| 683 | if (qdpair) qdpair = 0; |
Willy Tarreau | 0f7f51f | 2010-08-30 11:06:34 +0200 | [diff] [blame] | 684 | else if (quoted) { |
| 685 | if (*s == '\\') qdpair = 1; |
| 686 | else if (*s == '"') quoted = 0; |
| 687 | } |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 688 | else if (*s == '"') quoted = 1; |
| 689 | else if (*s == ',') return s; |
| 690 | } |
| 691 | return s; |
| 692 | } |
| 693 | |
| 694 | /* Find the first or next occurrence of header <name> in message buffer <sol> |
| 695 | * using headers index <idx>, and return it in the <ctx> structure. This |
| 696 | * structure holds everything necessary to use the header and find next |
| 697 | * occurrence. If its <idx> member is 0, the header is searched from the |
| 698 | * beginning. Otherwise, the next occurrence is returned. The function returns |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 699 | * 1 when it finds a value, and 0 when there is no more. It is designed to work |
| 700 | * with headers defined as comma-separated lists. As a special case, if ctx->val |
| 701 | * is NULL when searching for a new values of a header, the current header is |
| 702 | * rescanned. This allows rescanning after a header deletion. |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 703 | */ |
| 704 | int http_find_header2(const char *name, int len, |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 705 | char *sol, struct hdr_idx *idx, |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 706 | struct hdr_ctx *ctx) |
| 707 | { |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 708 | char *eol, *sov; |
| 709 | int cur_idx, old_idx; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 710 | |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 711 | cur_idx = ctx->idx; |
| 712 | if (cur_idx) { |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 713 | /* We have previously returned a value, let's search |
| 714 | * another one on the same line. |
| 715 | */ |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 716 | sol = ctx->line; |
Willy Tarreau | 588bd4f | 2011-09-01 22:22:28 +0200 | [diff] [blame] | 717 | ctx->del = ctx->val + ctx->vlen + ctx->tws; |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 718 | sov = sol + ctx->del; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 719 | eol = sol + idx->v[cur_idx].len; |
| 720 | |
| 721 | if (sov >= eol) |
| 722 | /* no more values in this header */ |
| 723 | goto next_hdr; |
| 724 | |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 725 | /* values remaining for this header, skip the comma but save it |
| 726 | * for later use (eg: for header deletion). |
| 727 | */ |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 728 | sov++; |
| 729 | while (sov < eol && http_is_lws[(unsigned char)*sov]) |
| 730 | sov++; |
| 731 | |
| 732 | goto return_hdr; |
| 733 | } |
| 734 | |
| 735 | /* first request for this header */ |
| 736 | sol += hdr_idx_first_pos(idx); |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 737 | old_idx = 0; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 738 | cur_idx = hdr_idx_first_idx(idx); |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 739 | while (cur_idx) { |
| 740 | eol = sol + idx->v[cur_idx].len; |
| 741 | |
Willy Tarreau | 1ad7c6d | 2007-06-10 21:42:55 +0200 | [diff] [blame] | 742 | if (len == 0) { |
| 743 | /* No argument was passed, we want any header. |
| 744 | * To achieve this, we simply build a fake request. */ |
| 745 | while (sol + len < eol && sol[len] != ':') |
| 746 | len++; |
| 747 | name = sol; |
| 748 | } |
| 749 | |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 750 | if ((len < eol - sol) && |
| 751 | (sol[len] == ':') && |
| 752 | (strncasecmp(sol, name, len) == 0)) { |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 753 | ctx->del = len; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 754 | sov = sol + len + 1; |
| 755 | while (sov < eol && http_is_lws[(unsigned char)*sov]) |
| 756 | sov++; |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 757 | |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 758 | ctx->line = sol; |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 759 | ctx->prev = old_idx; |
| 760 | return_hdr: |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 761 | ctx->idx = cur_idx; |
| 762 | ctx->val = sov - sol; |
| 763 | |
| 764 | eol = find_hdr_value_end(sov, eol); |
Willy Tarreau | 588bd4f | 2011-09-01 22:22:28 +0200 | [diff] [blame] | 765 | ctx->tws = 0; |
Willy Tarreau | 275600b | 2011-09-16 08:11:26 +0200 | [diff] [blame] | 766 | while (eol > sov && http_is_lws[(unsigned char)*(eol - 1)]) { |
Willy Tarreau | 588bd4f | 2011-09-01 22:22:28 +0200 | [diff] [blame] | 767 | eol--; |
| 768 | ctx->tws++; |
| 769 | } |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 770 | ctx->vlen = eol - sov; |
| 771 | return 1; |
| 772 | } |
| 773 | next_hdr: |
| 774 | sol = eol + idx->v[cur_idx].cr + 1; |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 775 | old_idx = cur_idx; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 776 | cur_idx = idx->v[cur_idx].next; |
| 777 | } |
| 778 | return 0; |
| 779 | } |
| 780 | |
| 781 | int http_find_header(const char *name, |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 782 | char *sol, struct hdr_idx *idx, |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 783 | struct hdr_ctx *ctx) |
| 784 | { |
| 785 | return http_find_header2(name, strlen(name), sol, idx, ctx); |
| 786 | } |
| 787 | |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 788 | /* Remove one value of a header. This only works on a <ctx> returned by one of |
| 789 | * the http_find_header functions. The value is removed, as well as surrounding |
| 790 | * 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] | 791 | * 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] | 792 | * message <msg>. The new index is returned. If it is zero, it means there is |
| 793 | * no more header, so any processing may stop. The ctx is always left in a form |
| 794 | * that can be handled by http_find_header2() to find next occurrence. |
| 795 | */ |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 796 | 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] | 797 | { |
| 798 | int cur_idx = ctx->idx; |
| 799 | char *sol = ctx->line; |
| 800 | struct hdr_idx_elem *hdr; |
| 801 | int delta, skip_comma; |
| 802 | |
| 803 | if (!cur_idx) |
| 804 | return 0; |
| 805 | |
| 806 | hdr = &idx->v[cur_idx]; |
Willy Tarreau | 588bd4f | 2011-09-01 22:22:28 +0200 | [diff] [blame] | 807 | if (sol[ctx->del] == ':' && ctx->val + ctx->vlen + ctx->tws == hdr->len) { |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 808 | /* 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] | 809 | 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] | 810 | http_msg_move_end(msg, delta); |
| 811 | idx->used--; |
| 812 | hdr->len = 0; /* unused entry */ |
| 813 | idx->v[ctx->prev].next = idx->v[ctx->idx].next; |
Willy Tarreau | 5c4784f | 2011-02-12 13:07:35 +0100 | [diff] [blame] | 814 | if (idx->tail == ctx->idx) |
| 815 | idx->tail = ctx->prev; |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 816 | ctx->idx = ctx->prev; /* walk back to the end of previous header */ |
Willy Tarreau | 7c1c217 | 2015-01-07 17:23:50 +0100 | [diff] [blame] | 817 | 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] | 818 | 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] | 819 | ctx->tws = ctx->vlen = 0; |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 820 | return ctx->idx; |
| 821 | } |
| 822 | |
| 823 | /* 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] | 824 | * ctx->del+1 and ctx->val+ctx->vlen+ctx->tws+1 included. If it is the |
| 825 | * last entry of the list, we remove the last separator. |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 826 | */ |
| 827 | |
Willy Tarreau | 588bd4f | 2011-09-01 22:22:28 +0200 | [diff] [blame] | 828 | skip_comma = (ctx->val + ctx->vlen + ctx->tws == hdr->len) ? 0 : 1; |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 829 | delta = buffer_replace2(msg->chn->buf, sol + ctx->del + skip_comma, |
Willy Tarreau | 588bd4f | 2011-09-01 22:22:28 +0200 | [diff] [blame] | 830 | sol + ctx->val + ctx->vlen + ctx->tws + skip_comma, |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 831 | NULL, 0); |
| 832 | hdr->len += delta; |
| 833 | http_msg_move_end(msg, delta); |
| 834 | ctx->val = ctx->del; |
Willy Tarreau | 588bd4f | 2011-09-01 22:22:28 +0200 | [diff] [blame] | 835 | ctx->tws = ctx->vlen = 0; |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 836 | return ctx->idx; |
| 837 | } |
| 838 | |
Willy Tarreau | 2d3d94c | 2008-11-30 20:20:08 +0100 | [diff] [blame] | 839 | /* This function handles a server error at the stream interface level. The |
| 840 | * stream interface is assumed to be already in a closed state. An optional |
| 841 | * message is copied into the input buffer, and an HTTP status code stored. |
| 842 | * 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] | 843 | * in this buffer will be lost. |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 844 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 845 | static void http_server_error(struct stream *s, struct stream_interface *si, |
Willy Tarreau | 2d3d94c | 2008-11-30 20:20:08 +0100 | [diff] [blame] | 846 | int err, int finst, int status, const struct chunk *msg) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 847 | { |
Willy Tarreau | 2bb4a96 | 2014-11-28 11:11:05 +0100 | [diff] [blame] | 848 | channel_auto_read(si_oc(si)); |
| 849 | channel_abort(si_oc(si)); |
| 850 | channel_auto_close(si_oc(si)); |
| 851 | channel_erase(si_oc(si)); |
| 852 | channel_auto_close(si_ic(si)); |
| 853 | channel_auto_read(si_ic(si)); |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 854 | if (status > 0 && msg) { |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 855 | s->txn->status = status; |
Willy Tarreau | 2bb4a96 | 2014-11-28 11:11:05 +0100 | [diff] [blame] | 856 | bo_inject(si_ic(si), msg->str, msg->len); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 857 | } |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 858 | if (!(s->flags & SF_ERR_MASK)) |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 859 | s->flags |= err; |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 860 | if (!(s->flags & SF_FINST_MASK)) |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 861 | s->flags |= finst; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 862 | } |
| 863 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 864 | /* This function returns the appropriate error location for the given stream |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 865 | * and message. |
| 866 | */ |
| 867 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 868 | struct chunk *http_error_message(struct stream *s, int msgnum) |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 869 | { |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 870 | if (s->be->errmsg[msgnum].str) |
| 871 | return &s->be->errmsg[msgnum]; |
Willy Tarreau | d0d8da9 | 2015-04-04 02:10:38 +0200 | [diff] [blame] | 872 | else if (strm_fe(s)->errmsg[msgnum].str) |
| 873 | return &strm_fe(s)->errmsg[msgnum]; |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 874 | else |
| 875 | return &http_err_chunks[msgnum]; |
| 876 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 877 | |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 878 | /* |
| 879 | * returns HTTP_METH_NONE if there is nothing valid to read (empty or non-text |
| 880 | * string), HTTP_METH_OTHER for unknown methods, or the identified method. |
| 881 | */ |
Thierry FOURNIER | d437314 | 2013-12-17 01:10:10 +0100 | [diff] [blame] | 882 | 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] | 883 | { |
| 884 | unsigned char m; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 885 | const struct http_method_desc *h; |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 886 | |
| 887 | m = ((unsigned)*str - 'A'); |
| 888 | |
| 889 | if (m < 26) { |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 890 | for (h = http_methods[m]; h->len > 0; h++) { |
| 891 | if (unlikely(h->len != len)) |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 892 | continue; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 893 | if (likely(memcmp(str, h->text, h->len) == 0)) |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 894 | return h->meth; |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 895 | }; |
| 896 | return HTTP_METH_OTHER; |
| 897 | } |
| 898 | return HTTP_METH_NONE; |
| 899 | |
| 900 | } |
| 901 | |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 902 | /* Parse the URI from the given transaction (which is assumed to be in request |
| 903 | * phase) and look for the "/" beginning the PATH. If not found, return NULL. |
| 904 | * It is returned otherwise. |
| 905 | */ |
| 906 | static char * |
| 907 | http_get_path(struct http_txn *txn) |
| 908 | { |
| 909 | char *ptr, *end; |
| 910 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 911 | ptr = txn->req.chn->buf->p + txn->req.sl.rq.u; |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 912 | end = ptr + txn->req.sl.rq.u_l; |
| 913 | |
| 914 | if (ptr >= end) |
| 915 | return NULL; |
| 916 | |
| 917 | /* RFC2616, par. 5.1.2 : |
| 918 | * Request-URI = "*" | absuri | abspath | authority |
| 919 | */ |
| 920 | |
| 921 | if (*ptr == '*') |
| 922 | return NULL; |
| 923 | |
| 924 | if (isalpha((unsigned char)*ptr)) { |
| 925 | /* this is a scheme as described by RFC3986, par. 3.1 */ |
| 926 | ptr++; |
| 927 | while (ptr < end && |
| 928 | (isalnum((unsigned char)*ptr) || *ptr == '+' || *ptr == '-' || *ptr == '.')) |
| 929 | ptr++; |
| 930 | /* skip '://' */ |
| 931 | if (ptr == end || *ptr++ != ':') |
| 932 | return NULL; |
| 933 | if (ptr == end || *ptr++ != '/') |
| 934 | return NULL; |
| 935 | if (ptr == end || *ptr++ != '/') |
| 936 | return NULL; |
| 937 | } |
| 938 | /* skip [user[:passwd]@]host[:[port]] */ |
| 939 | |
| 940 | while (ptr < end && *ptr != '/') |
| 941 | ptr++; |
| 942 | |
| 943 | if (ptr == end) |
| 944 | return NULL; |
| 945 | |
| 946 | /* OK, we got the '/' ! */ |
| 947 | return ptr; |
| 948 | } |
| 949 | |
William Lallemand | 65ad6e1 | 2014-01-31 15:08:02 +0100 | [diff] [blame] | 950 | /* Parse the URI from the given string and look for the "/" beginning the PATH. |
| 951 | * If not found, return NULL. It is returned otherwise. |
| 952 | */ |
| 953 | static char * |
| 954 | http_get_path_from_string(char *str) |
| 955 | { |
| 956 | char *ptr = str; |
| 957 | |
| 958 | /* RFC2616, par. 5.1.2 : |
| 959 | * Request-URI = "*" | absuri | abspath | authority |
| 960 | */ |
| 961 | |
| 962 | if (*ptr == '*') |
| 963 | return NULL; |
| 964 | |
| 965 | if (isalpha((unsigned char)*ptr)) { |
| 966 | /* this is a scheme as described by RFC3986, par. 3.1 */ |
| 967 | ptr++; |
| 968 | while (isalnum((unsigned char)*ptr) || *ptr == '+' || *ptr == '-' || *ptr == '.') |
| 969 | ptr++; |
| 970 | /* skip '://' */ |
| 971 | if (*ptr == '\0' || *ptr++ != ':') |
| 972 | return NULL; |
| 973 | if (*ptr == '\0' || *ptr++ != '/') |
| 974 | return NULL; |
| 975 | if (*ptr == '\0' || *ptr++ != '/') |
| 976 | return NULL; |
| 977 | } |
| 978 | /* skip [user[:passwd]@]host[:[port]] */ |
| 979 | |
| 980 | while (*ptr != '\0' && *ptr != ' ' && *ptr != '/') |
| 981 | ptr++; |
| 982 | |
| 983 | if (*ptr == '\0' || *ptr == ' ') |
| 984 | return NULL; |
| 985 | |
| 986 | /* OK, we got the '/' ! */ |
| 987 | return ptr; |
| 988 | } |
| 989 | |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 990 | /* Returns a 302 for a redirectable request that reaches a server working in |
| 991 | * in redirect mode. This may only be called just after the stream interface |
| 992 | * has moved to SI_ST_ASS. Unprocessable requests are left unchanged and will |
| 993 | * follow normal proxy processing. NOTE: this function is designed to support |
| 994 | * being called once data are scheduled for forwarding. |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 995 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 996 | void http_perform_server_redirect(struct stream *s, struct stream_interface *si) |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 997 | { |
| 998 | struct http_txn *txn; |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 999 | struct server *srv; |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 1000 | char *path; |
Willy Tarreau | cde18fc | 2012-05-30 07:59:54 +0200 | [diff] [blame] | 1001 | int len, rewind; |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 1002 | |
| 1003 | /* 1: create the response header */ |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 1004 | trash.len = strlen(HTTP_302); |
| 1005 | memcpy(trash.str, HTTP_302, trash.len); |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 1006 | |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 1007 | srv = objt_server(s->target); |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 1008 | |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 1009 | /* 2: add the server's prefix */ |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 1010 | if (trash.len + srv->rdr_len > trash.size) |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 1011 | return; |
| 1012 | |
Willy Tarreau | dcb75c4 | 2010-01-10 00:24:22 +0100 | [diff] [blame] | 1013 | /* special prefix "/" means don't change URL */ |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 1014 | if (srv->rdr_len != 1 || *srv->rdr_pfx != '/') { |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 1015 | memcpy(trash.str + trash.len, srv->rdr_pfx, srv->rdr_len); |
| 1016 | trash.len += srv->rdr_len; |
Willy Tarreau | dcb75c4 | 2010-01-10 00:24:22 +0100 | [diff] [blame] | 1017 | } |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 1018 | |
Willy Tarreau | cde18fc | 2012-05-30 07:59:54 +0200 | [diff] [blame] | 1019 | /* 3: add the request URI. Since it was already forwarded, we need |
| 1020 | * to temporarily rewind the buffer. |
| 1021 | */ |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 1022 | txn = s->txn; |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 1023 | b_rew(s->req.buf, rewind = http_hdr_rewind(&txn->req)); |
Willy Tarreau | cde18fc | 2012-05-30 07:59:54 +0200 | [diff] [blame] | 1024 | |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 1025 | path = http_get_path(txn); |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 1026 | 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] | 1027 | |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 1028 | b_adv(s->req.buf, rewind); |
Willy Tarreau | cde18fc | 2012-05-30 07:59:54 +0200 | [diff] [blame] | 1029 | |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 1030 | if (!path) |
| 1031 | return; |
| 1032 | |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 1033 | if (trash.len + len > trash.size - 4) /* 4 for CRLF-CRLF */ |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 1034 | return; |
| 1035 | |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 1036 | memcpy(trash.str + trash.len, path, len); |
| 1037 | trash.len += len; |
Willy Tarreau | 88d349d | 2010-01-25 12:15:43 +0100 | [diff] [blame] | 1038 | |
| 1039 | if (unlikely(txn->flags & TX_USE_PX_CONN)) { |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 1040 | memcpy(trash.str + trash.len, "\r\nProxy-Connection: close\r\n\r\n", 29); |
| 1041 | trash.len += 29; |
Willy Tarreau | 88d349d | 2010-01-25 12:15:43 +0100 | [diff] [blame] | 1042 | } else { |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 1043 | memcpy(trash.str + trash.len, "\r\nConnection: close\r\n\r\n", 23); |
| 1044 | trash.len += 23; |
Willy Tarreau | 88d349d | 2010-01-25 12:15:43 +0100 | [diff] [blame] | 1045 | } |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 1046 | |
| 1047 | /* prepare to return without error. */ |
Willy Tarreau | 73b013b | 2012-05-21 16:31:45 +0200 | [diff] [blame] | 1048 | si_shutr(si); |
| 1049 | si_shutw(si); |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 1050 | si->err_type = SI_ET_NONE; |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 1051 | si->state = SI_ST_CLO; |
| 1052 | |
| 1053 | /* send the message */ |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 1054 | http_server_error(s, si, SF_ERR_LOCAL, SF_FINST_C, 302, &trash); |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 1055 | |
| 1056 | /* 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] | 1057 | srv_inc_sess_ctr(srv); |
Bhaskar Maddala | a20cb85 | 2014-02-03 16:26:46 -0500 | [diff] [blame] | 1058 | srv_set_sess_last(srv); |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 1059 | } |
| 1060 | |
Willy Tarreau | 0cac36f | 2008-11-30 20:44:17 +0100 | [diff] [blame] | 1061 | /* Return the error message corresponding to si->err_type. It is assumed |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 1062 | * that the server side is closed. Note that err_type is actually a |
| 1063 | * bitmask, where almost only aborts may be cumulated with other |
| 1064 | * values. We consider that aborted operations are more important |
| 1065 | * than timeouts or errors due to the fact that nobody else in the |
| 1066 | * logs might explain incomplete retries. All others should avoid |
| 1067 | * being cumulated. It should normally not be possible to have multiple |
| 1068 | * 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] | 1069 | * Note that connection errors appearing on the second request of a keep-alive |
| 1070 | * connection are not reported since this allows the client to retry. |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 1071 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 1072 | void http_return_srv_error(struct stream *s, struct stream_interface *si) |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 1073 | { |
Willy Tarreau | 0cac36f | 2008-11-30 20:44:17 +0100 | [diff] [blame] | 1074 | int err_type = si->err_type; |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 1075 | |
| 1076 | if (err_type & SI_ET_QUEUE_ABRT) |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 1077 | http_server_error(s, si, SF_ERR_CLICL, SF_FINST_Q, |
Willy Tarreau | 783f258 | 2012-09-04 12:19:04 +0200 | [diff] [blame] | 1078 | 503, http_error_message(s, HTTP_ERR_503)); |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 1079 | else if (err_type & SI_ET_CONN_ABRT) |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 1080 | http_server_error(s, si, SF_ERR_CLICL, SF_FINST_C, |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 1081 | 503, (s->txn->flags & TX_NOT_FIRST) ? NULL : |
Willy Tarreau | 6b726ad | 2013-12-15 19:31:37 +0100 | [diff] [blame] | 1082 | http_error_message(s, HTTP_ERR_503)); |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 1083 | else if (err_type & SI_ET_QUEUE_TO) |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 1084 | http_server_error(s, si, SF_ERR_SRVTO, SF_FINST_Q, |
Willy Tarreau | 783f258 | 2012-09-04 12:19:04 +0200 | [diff] [blame] | 1085 | 503, http_error_message(s, HTTP_ERR_503)); |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 1086 | else if (err_type & SI_ET_QUEUE_ERR) |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 1087 | http_server_error(s, si, SF_ERR_SRVCL, SF_FINST_Q, |
Willy Tarreau | 783f258 | 2012-09-04 12:19:04 +0200 | [diff] [blame] | 1088 | 503, http_error_message(s, HTTP_ERR_503)); |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 1089 | else if (err_type & SI_ET_CONN_TO) |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 1090 | http_server_error(s, si, SF_ERR_SRVTO, SF_FINST_C, |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 1091 | 503, (s->txn->flags & TX_NOT_FIRST) ? NULL : |
Willy Tarreau | 6b726ad | 2013-12-15 19:31:37 +0100 | [diff] [blame] | 1092 | http_error_message(s, HTTP_ERR_503)); |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 1093 | else if (err_type & SI_ET_CONN_ERR) |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 1094 | http_server_error(s, si, SF_ERR_SRVCL, SF_FINST_C, |
| 1095 | 503, (s->flags & SF_SRV_REUSED) ? NULL : |
Willy Tarreau | 6b726ad | 2013-12-15 19:31:37 +0100 | [diff] [blame] | 1096 | http_error_message(s, HTTP_ERR_503)); |
Willy Tarreau | 2d400bb | 2012-05-14 12:11:47 +0200 | [diff] [blame] | 1097 | else if (err_type & SI_ET_CONN_RES) |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 1098 | http_server_error(s, si, SF_ERR_RESOURCE, SF_FINST_C, |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 1099 | 503, (s->txn->flags & TX_NOT_FIRST) ? NULL : |
Willy Tarreau | 6b726ad | 2013-12-15 19:31:37 +0100 | [diff] [blame] | 1100 | http_error_message(s, HTTP_ERR_503)); |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 1101 | else /* SI_ET_CONN_OTHER and others */ |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 1102 | http_server_error(s, si, SF_ERR_INTERNAL, SF_FINST_C, |
Willy Tarreau | 783f258 | 2012-09-04 12:19:04 +0200 | [diff] [blame] | 1103 | 500, http_error_message(s, HTTP_ERR_500)); |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 1104 | } |
| 1105 | |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 1106 | extern const char sess_term_cond[8]; |
| 1107 | extern const char sess_fin_state[8]; |
| 1108 | extern const char *monthname[12]; |
Willy Tarreau | 63986c7 | 2015-04-03 22:55:33 +0200 | [diff] [blame] | 1109 | struct pool_head *pool2_http_txn; |
Willy Tarreau | 332f8bf | 2007-05-13 21:36:56 +0200 | [diff] [blame] | 1110 | struct pool_head *pool2_requri; |
Willy Tarreau | 193b8c6 | 2012-11-22 00:17:38 +0100 | [diff] [blame] | 1111 | struct pool_head *pool2_capture = NULL; |
William Lallemand | a73203e | 2012-03-12 12:48:57 +0100 | [diff] [blame] | 1112 | struct pool_head *pool2_uniqueid; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1113 | |
Willy Tarreau | 117f59e | 2007-03-04 18:17:17 +0100 | [diff] [blame] | 1114 | /* |
| 1115 | * Capture headers from message starting at <som> according to header list |
Willy Tarreau | 54da8db | 2014-06-13 16:11:48 +0200 | [diff] [blame] | 1116 | * <cap_hdr>, and fill the <cap> pointers appropriately. |
Willy Tarreau | 117f59e | 2007-03-04 18:17:17 +0100 | [diff] [blame] | 1117 | */ |
| 1118 | void capture_headers(char *som, struct hdr_idx *idx, |
| 1119 | char **cap, struct cap_hdr *cap_hdr) |
| 1120 | { |
| 1121 | char *eol, *sol, *col, *sov; |
| 1122 | int cur_idx; |
| 1123 | struct cap_hdr *h; |
| 1124 | int len; |
| 1125 | |
| 1126 | sol = som + hdr_idx_first_pos(idx); |
| 1127 | cur_idx = hdr_idx_first_idx(idx); |
| 1128 | |
| 1129 | while (cur_idx) { |
| 1130 | eol = sol + idx->v[cur_idx].len; |
| 1131 | |
| 1132 | col = sol; |
| 1133 | while (col < eol && *col != ':') |
| 1134 | col++; |
| 1135 | |
| 1136 | sov = col + 1; |
| 1137 | while (sov < eol && http_is_lws[(unsigned char)*sov]) |
| 1138 | sov++; |
| 1139 | |
| 1140 | for (h = cap_hdr; h; h = h->next) { |
Willy Tarreau | 54da8db | 2014-06-13 16:11:48 +0200 | [diff] [blame] | 1141 | if (h->namelen && (h->namelen == col - sol) && |
Willy Tarreau | 117f59e | 2007-03-04 18:17:17 +0100 | [diff] [blame] | 1142 | (strncasecmp(sol, h->name, h->namelen) == 0)) { |
| 1143 | if (cap[h->index] == NULL) |
| 1144 | cap[h->index] = |
Willy Tarreau | cf7f320 | 2007-05-13 22:46:04 +0200 | [diff] [blame] | 1145 | pool_alloc2(h->pool); |
Willy Tarreau | 117f59e | 2007-03-04 18:17:17 +0100 | [diff] [blame] | 1146 | |
| 1147 | if (cap[h->index] == NULL) { |
| 1148 | Alert("HTTP capture : out of memory.\n"); |
| 1149 | continue; |
| 1150 | } |
| 1151 | |
| 1152 | len = eol - sov; |
| 1153 | if (len > h->len) |
| 1154 | len = h->len; |
| 1155 | |
| 1156 | memcpy(cap[h->index], sov, len); |
| 1157 | cap[h->index][len]=0; |
| 1158 | } |
| 1159 | } |
| 1160 | sol = eol + idx->v[cur_idx].cr + 1; |
| 1161 | cur_idx = idx->v[cur_idx].next; |
| 1162 | } |
| 1163 | } |
| 1164 | |
| 1165 | |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 1166 | /* either we find an LF at <ptr> or we jump to <bad>. |
| 1167 | */ |
| 1168 | #define EXPECT_LF_HERE(ptr, bad) do { if (unlikely(*(ptr) != '\n')) goto bad; } while (0) |
| 1169 | |
| 1170 | /* plays with variables <ptr>, <end> and <state>. Jumps to <good> if OK, |
| 1171 | * otherwise to <http_msg_ood> with <state> set to <st>. |
| 1172 | */ |
| 1173 | #define EAT_AND_JUMP_OR_RETURN(good, st) do { \ |
| 1174 | ptr++; \ |
| 1175 | if (likely(ptr < end)) \ |
| 1176 | goto good; \ |
| 1177 | else { \ |
| 1178 | state = (st); \ |
| 1179 | goto http_msg_ood; \ |
| 1180 | } \ |
| 1181 | } while (0) |
| 1182 | |
| 1183 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1184 | /* |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 1185 | * This function parses a status line between <ptr> and <end>, starting with |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1186 | * parser state <state>. Only states HTTP_MSG_RPVER, HTTP_MSG_RPVER_SP, |
| 1187 | * HTTP_MSG_RPCODE, HTTP_MSG_RPCODE_SP and HTTP_MSG_RPREASON are handled. Others |
| 1188 | * will give undefined results. |
| 1189 | * Note that it is upon the caller's responsibility to ensure that ptr < end, |
| 1190 | * and that msg->sol points to the beginning of the response. |
| 1191 | * If a complete line is found (which implies that at least one CR or LF is |
| 1192 | * found before <end>, the updated <ptr> is returned, otherwise NULL is |
| 1193 | * returned indicating an incomplete line (which does not mean that parts have |
| 1194 | * not been updated). In the incomplete case, if <ret_ptr> or <ret_state> are |
| 1195 | * non-NULL, they are fed with the new <ptr> and <state> values to be passed |
| 1196 | * upon next call. |
| 1197 | * |
Willy Tarreau | 9cdde23 | 2007-05-02 20:58:19 +0200 | [diff] [blame] | 1198 | * This function was intentionally designed to be called from |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1199 | * http_msg_analyzer() with the lowest overhead. It should integrate perfectly |
| 1200 | * 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] | 1201 | * labels and variable names. Note that msg->sol is left unchanged. |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1202 | */ |
Willy Tarreau | 69d8c5d | 2012-05-08 09:44:41 +0200 | [diff] [blame] | 1203 | const char *http_parse_stsline(struct http_msg *msg, |
Willy Tarreau | 3770f23 | 2013-12-07 00:01:53 +0100 | [diff] [blame] | 1204 | enum ht_state state, const char *ptr, const char *end, |
| 1205 | unsigned int *ret_ptr, enum ht_state *ret_state) |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1206 | { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 1207 | const char *msg_start = msg->chn->buf->p; |
Willy Tarreau | 62f791e | 2012-03-09 11:32:30 +0100 | [diff] [blame] | 1208 | |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1209 | switch (state) { |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1210 | case HTTP_MSG_RPVER: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1211 | http_msg_rpver: |
Willy Tarreau | 4b89ad4 | 2007-03-04 18:13:58 +0100 | [diff] [blame] | 1212 | if (likely(HTTP_IS_VER_TOKEN(*ptr))) |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1213 | EAT_AND_JUMP_OR_RETURN(http_msg_rpver, HTTP_MSG_RPVER); |
| 1214 | |
| 1215 | if (likely(HTTP_IS_SPHT(*ptr))) { |
Willy Tarreau | ea1175a | 2012-03-05 15:52:30 +0100 | [diff] [blame] | 1216 | msg->sl.st.v_l = ptr - msg_start; |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1217 | EAT_AND_JUMP_OR_RETURN(http_msg_rpver_sp, HTTP_MSG_RPVER_SP); |
| 1218 | } |
Willy Tarreau | 7552c03 | 2009-03-01 11:10:40 +0100 | [diff] [blame] | 1219 | state = HTTP_MSG_ERROR; |
| 1220 | break; |
| 1221 | |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1222 | case HTTP_MSG_RPVER_SP: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1223 | http_msg_rpver_sp: |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1224 | if (likely(!HTTP_IS_LWS(*ptr))) { |
Willy Tarreau | ea1175a | 2012-03-05 15:52:30 +0100 | [diff] [blame] | 1225 | msg->sl.st.c = ptr - msg_start; |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1226 | goto http_msg_rpcode; |
| 1227 | } |
| 1228 | if (likely(HTTP_IS_SPHT(*ptr))) |
| 1229 | EAT_AND_JUMP_OR_RETURN(http_msg_rpver_sp, HTTP_MSG_RPVER_SP); |
| 1230 | /* so it's a CR/LF, this is invalid */ |
Willy Tarreau | 7552c03 | 2009-03-01 11:10:40 +0100 | [diff] [blame] | 1231 | state = HTTP_MSG_ERROR; |
| 1232 | break; |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1233 | |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1234 | case HTTP_MSG_RPCODE: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1235 | http_msg_rpcode: |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1236 | if (likely(!HTTP_IS_LWS(*ptr))) |
| 1237 | EAT_AND_JUMP_OR_RETURN(http_msg_rpcode, HTTP_MSG_RPCODE); |
| 1238 | |
| 1239 | if (likely(HTTP_IS_SPHT(*ptr))) { |
Willy Tarreau | ea1175a | 2012-03-05 15:52:30 +0100 | [diff] [blame] | 1240 | msg->sl.st.c_l = ptr - msg_start - msg->sl.st.c; |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1241 | EAT_AND_JUMP_OR_RETURN(http_msg_rpcode_sp, HTTP_MSG_RPCODE_SP); |
| 1242 | } |
| 1243 | |
| 1244 | /* so it's a CR/LF, so there is no reason phrase */ |
Willy Tarreau | ea1175a | 2012-03-05 15:52:30 +0100 | [diff] [blame] | 1245 | msg->sl.st.c_l = ptr - msg_start - msg->sl.st.c; |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1246 | http_msg_rsp_reason: |
| 1247 | /* FIXME: should we support HTTP responses without any reason phrase ? */ |
Willy Tarreau | ea1175a | 2012-03-05 15:52:30 +0100 | [diff] [blame] | 1248 | msg->sl.st.r = ptr - msg_start; |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1249 | msg->sl.st.r_l = 0; |
| 1250 | goto http_msg_rpline_eol; |
| 1251 | |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1252 | case HTTP_MSG_RPCODE_SP: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1253 | http_msg_rpcode_sp: |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1254 | if (likely(!HTTP_IS_LWS(*ptr))) { |
Willy Tarreau | ea1175a | 2012-03-05 15:52:30 +0100 | [diff] [blame] | 1255 | msg->sl.st.r = ptr - msg_start; |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1256 | goto http_msg_rpreason; |
| 1257 | } |
| 1258 | if (likely(HTTP_IS_SPHT(*ptr))) |
| 1259 | EAT_AND_JUMP_OR_RETURN(http_msg_rpcode_sp, HTTP_MSG_RPCODE_SP); |
| 1260 | /* so it's a CR/LF, so there is no reason phrase */ |
| 1261 | goto http_msg_rsp_reason; |
| 1262 | |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1263 | case HTTP_MSG_RPREASON: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1264 | http_msg_rpreason: |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1265 | if (likely(!HTTP_IS_CRLF(*ptr))) |
| 1266 | EAT_AND_JUMP_OR_RETURN(http_msg_rpreason, HTTP_MSG_RPREASON); |
Willy Tarreau | ea1175a | 2012-03-05 15:52:30 +0100 | [diff] [blame] | 1267 | msg->sl.st.r_l = ptr - msg_start - msg->sl.st.r; |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1268 | http_msg_rpline_eol: |
| 1269 | /* We have seen the end of line. Note that we do not |
| 1270 | * necessarily have the \n yet, but at least we know that we |
| 1271 | * have EITHER \r OR \n, otherwise the response would not be |
| 1272 | * complete. We can then record the response length and return |
| 1273 | * to the caller which will be able to register it. |
| 1274 | */ |
Willy Tarreau | 3a215be | 2012-03-09 21:39:51 +0100 | [diff] [blame] | 1275 | msg->sl.st.l = ptr - msg_start - msg->sol; |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1276 | return ptr; |
| 1277 | |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1278 | default: |
Willy Tarreau | 3770f23 | 2013-12-07 00:01:53 +0100 | [diff] [blame] | 1279 | #ifdef DEBUG_FULL |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1280 | fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state); |
| 1281 | exit(1); |
| 1282 | #endif |
Willy Tarreau | 3770f23 | 2013-12-07 00:01:53 +0100 | [diff] [blame] | 1283 | ; |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1284 | } |
| 1285 | |
| 1286 | http_msg_ood: |
Willy Tarreau | 7552c03 | 2009-03-01 11:10:40 +0100 | [diff] [blame] | 1287 | /* out of valid data */ |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1288 | if (ret_state) |
| 1289 | *ret_state = state; |
| 1290 | if (ret_ptr) |
Willy Tarreau | a458b67 | 2012-03-05 11:17:50 +0100 | [diff] [blame] | 1291 | *ret_ptr = ptr - msg_start; |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1292 | return NULL; |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1293 | } |
| 1294 | |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1295 | /* |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1296 | * This function parses a request line between <ptr> and <end>, starting with |
| 1297 | * parser state <state>. Only states HTTP_MSG_RQMETH, HTTP_MSG_RQMETH_SP, |
| 1298 | * HTTP_MSG_RQURI, HTTP_MSG_RQURI_SP and HTTP_MSG_RQVER are handled. Others |
| 1299 | * will give undefined results. |
| 1300 | * Note that it is upon the caller's responsibility to ensure that ptr < end, |
| 1301 | * and that msg->sol points to the beginning of the request. |
| 1302 | * If a complete line is found (which implies that at least one CR or LF is |
| 1303 | * found before <end>, the updated <ptr> is returned, otherwise NULL is |
| 1304 | * returned indicating an incomplete line (which does not mean that parts have |
| 1305 | * not been updated). In the incomplete case, if <ret_ptr> or <ret_state> are |
| 1306 | * non-NULL, they are fed with the new <ptr> and <state> values to be passed |
| 1307 | * upon next call. |
| 1308 | * |
Willy Tarreau | 9cdde23 | 2007-05-02 20:58:19 +0200 | [diff] [blame] | 1309 | * This function was intentionally designed to be called from |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1310 | * http_msg_analyzer() with the lowest overhead. It should integrate perfectly |
| 1311 | * 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] | 1312 | * labels and variable names. Note that msg->sol is left unchanged. |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1313 | */ |
Willy Tarreau | 69d8c5d | 2012-05-08 09:44:41 +0200 | [diff] [blame] | 1314 | const char *http_parse_reqline(struct http_msg *msg, |
Willy Tarreau | 3770f23 | 2013-12-07 00:01:53 +0100 | [diff] [blame] | 1315 | enum ht_state state, const char *ptr, const char *end, |
| 1316 | unsigned int *ret_ptr, enum ht_state *ret_state) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1317 | { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 1318 | const char *msg_start = msg->chn->buf->p; |
Willy Tarreau | 62f791e | 2012-03-09 11:32:30 +0100 | [diff] [blame] | 1319 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1320 | switch (state) { |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1321 | case HTTP_MSG_RQMETH: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1322 | http_msg_rqmeth: |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1323 | if (likely(HTTP_IS_TOKEN(*ptr))) |
| 1324 | EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth, HTTP_MSG_RQMETH); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1325 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1326 | if (likely(HTTP_IS_SPHT(*ptr))) { |
Willy Tarreau | ea1175a | 2012-03-05 15:52:30 +0100 | [diff] [blame] | 1327 | msg->sl.rq.m_l = ptr - msg_start; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1328 | EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth_sp, HTTP_MSG_RQMETH_SP); |
| 1329 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1330 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1331 | if (likely(HTTP_IS_CRLF(*ptr))) { |
| 1332 | /* HTTP 0.9 request */ |
Willy Tarreau | ea1175a | 2012-03-05 15:52:30 +0100 | [diff] [blame] | 1333 | msg->sl.rq.m_l = ptr - msg_start; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1334 | http_msg_req09_uri: |
Willy Tarreau | ea1175a | 2012-03-05 15:52:30 +0100 | [diff] [blame] | 1335 | msg->sl.rq.u = ptr - msg_start; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1336 | http_msg_req09_uri_e: |
Willy Tarreau | ea1175a | 2012-03-05 15:52:30 +0100 | [diff] [blame] | 1337 | msg->sl.rq.u_l = ptr - msg_start - msg->sl.rq.u; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1338 | http_msg_req09_ver: |
Willy Tarreau | ea1175a | 2012-03-05 15:52:30 +0100 | [diff] [blame] | 1339 | msg->sl.rq.v = ptr - msg_start; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1340 | msg->sl.rq.v_l = 0; |
| 1341 | goto http_msg_rqline_eol; |
| 1342 | } |
Willy Tarreau | 7552c03 | 2009-03-01 11:10:40 +0100 | [diff] [blame] | 1343 | state = HTTP_MSG_ERROR; |
| 1344 | break; |
| 1345 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1346 | case HTTP_MSG_RQMETH_SP: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1347 | http_msg_rqmeth_sp: |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1348 | if (likely(!HTTP_IS_LWS(*ptr))) { |
Willy Tarreau | ea1175a | 2012-03-05 15:52:30 +0100 | [diff] [blame] | 1349 | msg->sl.rq.u = ptr - msg_start; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1350 | goto http_msg_rquri; |
| 1351 | } |
| 1352 | if (likely(HTTP_IS_SPHT(*ptr))) |
| 1353 | EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth_sp, HTTP_MSG_RQMETH_SP); |
| 1354 | /* so it's a CR/LF, meaning an HTTP 0.9 request */ |
| 1355 | goto http_msg_req09_uri; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1356 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1357 | case HTTP_MSG_RQURI: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1358 | http_msg_rquri: |
Willy Tarreau | 2e9506d | 2012-01-07 23:22:31 +0100 | [diff] [blame] | 1359 | if (likely((unsigned char)(*ptr - 33) <= 93)) /* 33 to 126 included */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1360 | EAT_AND_JUMP_OR_RETURN(http_msg_rquri, HTTP_MSG_RQURI); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1361 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1362 | if (likely(HTTP_IS_SPHT(*ptr))) { |
Willy Tarreau | ea1175a | 2012-03-05 15:52:30 +0100 | [diff] [blame] | 1363 | msg->sl.rq.u_l = ptr - msg_start - msg->sl.rq.u; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1364 | EAT_AND_JUMP_OR_RETURN(http_msg_rquri_sp, HTTP_MSG_RQURI_SP); |
| 1365 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1366 | |
Willy Tarreau | 2e9506d | 2012-01-07 23:22:31 +0100 | [diff] [blame] | 1367 | if (likely((unsigned char)*ptr >= 128)) { |
Willy Tarreau | 422246e | 2012-01-07 23:54:13 +0100 | [diff] [blame] | 1368 | /* non-ASCII chars are forbidden unless option |
| 1369 | * accept-invalid-http-request is enabled in the frontend. |
| 1370 | * In any case, we capture the faulty char. |
Willy Tarreau | 2e9506d | 2012-01-07 23:22:31 +0100 | [diff] [blame] | 1371 | */ |
Willy Tarreau | 422246e | 2012-01-07 23:54:13 +0100 | [diff] [blame] | 1372 | if (msg->err_pos < -1) |
| 1373 | goto invalid_char; |
| 1374 | if (msg->err_pos == -1) |
Willy Tarreau | 69d8c5d | 2012-05-08 09:44:41 +0200 | [diff] [blame] | 1375 | msg->err_pos = ptr - msg_start; |
Willy Tarreau | 2e9506d | 2012-01-07 23:22:31 +0100 | [diff] [blame] | 1376 | EAT_AND_JUMP_OR_RETURN(http_msg_rquri, HTTP_MSG_RQURI); |
| 1377 | } |
| 1378 | |
| 1379 | if (likely(HTTP_IS_CRLF(*ptr))) { |
| 1380 | /* so it's a CR/LF, meaning an HTTP 0.9 request */ |
| 1381 | goto http_msg_req09_uri_e; |
| 1382 | } |
| 1383 | |
| 1384 | /* OK forbidden chars, 0..31 or 127 */ |
Willy Tarreau | 422246e | 2012-01-07 23:54:13 +0100 | [diff] [blame] | 1385 | invalid_char: |
Willy Tarreau | 69d8c5d | 2012-05-08 09:44:41 +0200 | [diff] [blame] | 1386 | msg->err_pos = ptr - msg_start; |
Willy Tarreau | 2e9506d | 2012-01-07 23:22:31 +0100 | [diff] [blame] | 1387 | state = HTTP_MSG_ERROR; |
| 1388 | break; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1389 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1390 | case HTTP_MSG_RQURI_SP: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1391 | http_msg_rquri_sp: |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1392 | if (likely(!HTTP_IS_LWS(*ptr))) { |
Willy Tarreau | ea1175a | 2012-03-05 15:52:30 +0100 | [diff] [blame] | 1393 | msg->sl.rq.v = ptr - msg_start; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1394 | goto http_msg_rqver; |
| 1395 | } |
| 1396 | if (likely(HTTP_IS_SPHT(*ptr))) |
| 1397 | EAT_AND_JUMP_OR_RETURN(http_msg_rquri_sp, HTTP_MSG_RQURI_SP); |
| 1398 | /* so it's a CR/LF, meaning an HTTP 0.9 request */ |
| 1399 | goto http_msg_req09_ver; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1400 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1401 | case HTTP_MSG_RQVER: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1402 | http_msg_rqver: |
Willy Tarreau | 4b89ad4 | 2007-03-04 18:13:58 +0100 | [diff] [blame] | 1403 | if (likely(HTTP_IS_VER_TOKEN(*ptr))) |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1404 | EAT_AND_JUMP_OR_RETURN(http_msg_rqver, HTTP_MSG_RQVER); |
Willy Tarreau | 4b89ad4 | 2007-03-04 18:13:58 +0100 | [diff] [blame] | 1405 | |
| 1406 | if (likely(HTTP_IS_CRLF(*ptr))) { |
Willy Tarreau | ea1175a | 2012-03-05 15:52:30 +0100 | [diff] [blame] | 1407 | msg->sl.rq.v_l = ptr - msg_start - msg->sl.rq.v; |
Willy Tarreau | 4b89ad4 | 2007-03-04 18:13:58 +0100 | [diff] [blame] | 1408 | http_msg_rqline_eol: |
| 1409 | /* We have seen the end of line. Note that we do not |
| 1410 | * necessarily have the \n yet, but at least we know that we |
| 1411 | * have EITHER \r OR \n, otherwise the request would not be |
| 1412 | * complete. We can then record the request length and return |
| 1413 | * to the caller which will be able to register it. |
| 1414 | */ |
Willy Tarreau | 3a215be | 2012-03-09 21:39:51 +0100 | [diff] [blame] | 1415 | msg->sl.rq.l = ptr - msg_start - msg->sol; |
Willy Tarreau | 4b89ad4 | 2007-03-04 18:13:58 +0100 | [diff] [blame] | 1416 | return ptr; |
| 1417 | } |
| 1418 | |
| 1419 | /* neither an HTTP_VER token nor a CRLF */ |
Willy Tarreau | 7552c03 | 2009-03-01 11:10:40 +0100 | [diff] [blame] | 1420 | state = HTTP_MSG_ERROR; |
| 1421 | break; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1422 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1423 | default: |
Willy Tarreau | 3770f23 | 2013-12-07 00:01:53 +0100 | [diff] [blame] | 1424 | #ifdef DEBUG_FULL |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1425 | fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state); |
| 1426 | exit(1); |
| 1427 | #endif |
Willy Tarreau | 3770f23 | 2013-12-07 00:01:53 +0100 | [diff] [blame] | 1428 | ; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1429 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1430 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1431 | http_msg_ood: |
Willy Tarreau | 7552c03 | 2009-03-01 11:10:40 +0100 | [diff] [blame] | 1432 | /* out of valid data */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1433 | if (ret_state) |
| 1434 | *ret_state = state; |
| 1435 | if (ret_ptr) |
Willy Tarreau | a458b67 | 2012-03-05 11:17:50 +0100 | [diff] [blame] | 1436 | *ret_ptr = ptr - msg_start; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1437 | return NULL; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1438 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1439 | |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 1440 | /* |
| 1441 | * Returns the data from Authorization header. Function may be called more |
| 1442 | * than once so data is stored in txn->auth_data. When no header is found |
| 1443 | * 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] | 1444 | * searching again for something we are unable to find anyway. However, if |
| 1445 | * 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] | 1446 | * have the credentials overwritten by another stream in parallel. |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 1447 | */ |
| 1448 | |
Thierry FOURNIER | 9eec0a6 | 2014-01-22 18:38:02 +0100 | [diff] [blame] | 1449 | /* This bufffer is initialized in the file 'src/haproxy.c'. This length is |
| 1450 | * set according to global.tune.bufsize. |
| 1451 | */ |
Willy Tarreau | 7e2c647 | 2012-10-29 20:44:36 +0100 | [diff] [blame] | 1452 | char *get_http_auth_buff; |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 1453 | |
| 1454 | int |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 1455 | get_http_auth(struct stream *s) |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 1456 | { |
| 1457 | |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 1458 | struct http_txn *txn = s->txn; |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 1459 | struct chunk auth_method; |
| 1460 | struct hdr_ctx ctx; |
| 1461 | char *h, *p; |
| 1462 | int len; |
| 1463 | |
| 1464 | #ifdef DEBUG_AUTH |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 1465 | printf("Auth for stream %p: %d\n", s, txn->auth.method); |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 1466 | #endif |
| 1467 | |
| 1468 | if (txn->auth.method == HTTP_AUTH_WRONG) |
| 1469 | return 0; |
| 1470 | |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 1471 | txn->auth.method = HTTP_AUTH_WRONG; |
| 1472 | |
| 1473 | ctx.idx = 0; |
Willy Tarreau | 844a7e7 | 2010-01-31 21:46:18 +0100 | [diff] [blame] | 1474 | |
| 1475 | if (txn->flags & TX_USE_PX_CONN) { |
| 1476 | h = "Proxy-Authorization"; |
| 1477 | len = strlen(h); |
| 1478 | } else { |
| 1479 | h = "Authorization"; |
| 1480 | len = strlen(h); |
| 1481 | } |
| 1482 | |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 1483 | 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] | 1484 | return 0; |
| 1485 | |
| 1486 | h = ctx.line + ctx.val; |
| 1487 | |
| 1488 | p = memchr(h, ' ', ctx.vlen); |
| 1489 | if (!p || p == h) |
| 1490 | return 0; |
| 1491 | |
| 1492 | chunk_initlen(&auth_method, h, 0, p-h); |
| 1493 | chunk_initlen(&txn->auth.method_data, p+1, 0, ctx.vlen-(p-h)-1); |
| 1494 | |
| 1495 | if (!strncasecmp("Basic", auth_method.str, auth_method.len)) { |
| 1496 | |
| 1497 | len = base64dec(txn->auth.method_data.str, txn->auth.method_data.len, |
Willy Tarreau | 7e2c647 | 2012-10-29 20:44:36 +0100 | [diff] [blame] | 1498 | get_http_auth_buff, global.tune.bufsize - 1); |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 1499 | |
| 1500 | if (len < 0) |
| 1501 | return 0; |
| 1502 | |
| 1503 | |
| 1504 | get_http_auth_buff[len] = '\0'; |
| 1505 | |
| 1506 | p = strchr(get_http_auth_buff, ':'); |
| 1507 | |
| 1508 | if (!p) |
| 1509 | return 0; |
| 1510 | |
| 1511 | txn->auth.user = get_http_auth_buff; |
| 1512 | *p = '\0'; |
| 1513 | txn->auth.pass = p+1; |
| 1514 | |
| 1515 | txn->auth.method = HTTP_AUTH_BASIC; |
| 1516 | return 1; |
| 1517 | } |
| 1518 | |
| 1519 | return 0; |
| 1520 | } |
| 1521 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1522 | |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1523 | /* |
| 1524 | * This function parses an HTTP message, either a request or a response, |
Willy Tarreau | 8b1323e | 2012-03-09 14:46:19 +0100 | [diff] [blame] | 1525 | * depending on the initial msg->msg_state. The caller is responsible for |
| 1526 | * ensuring that the message does not wrap. The function can be preempted |
| 1527 | * everywhere when data are missing and recalled at the exact same location |
| 1528 | * with no information loss. The message may even be realigned between two |
| 1529 | * calls. The header index is re-initialized when switching from |
Willy Tarreau | 9cdde23 | 2007-05-02 20:58:19 +0200 | [diff] [blame] | 1530 | * 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] | 1531 | * fields. Note that msg->sol will be initialized after completing the first |
| 1532 | * state, so that none of the msg pointers has to be initialized prior to the |
| 1533 | * first call. |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1534 | */ |
Willy Tarreau | a560c21 | 2012-03-09 13:50:57 +0100 | [diff] [blame] | 1535 | void http_msg_analyzer(struct http_msg *msg, struct hdr_idx *idx) |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1536 | { |
Willy Tarreau | 3770f23 | 2013-12-07 00:01:53 +0100 | [diff] [blame] | 1537 | enum ht_state state; /* updated only when leaving the FSM */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1538 | register char *ptr, *end; /* request pointers, to avoid dereferences */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1539 | struct buffer *buf; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1540 | |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1541 | state = msg->msg_state; |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 1542 | buf = msg->chn->buf; |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1543 | ptr = buf->p + msg->next; |
| 1544 | end = buf->p + buf->i; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1545 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1546 | if (unlikely(ptr >= end)) |
| 1547 | goto http_msg_ood; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1548 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1549 | switch (state) { |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1550 | /* |
| 1551 | * First, states that are specific to the response only. |
| 1552 | * We check them first so that request and headers are |
| 1553 | * closer to each other (accessed more often). |
| 1554 | */ |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1555 | case HTTP_MSG_RPBEFORE: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1556 | http_msg_rpbefore: |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1557 | if (likely(HTTP_IS_TOKEN(*ptr))) { |
Willy Tarreau | 15de77e | 2010-01-02 21:59:16 +0100 | [diff] [blame] | 1558 | /* we have a start of message, but we have to check |
| 1559 | * first if we need to remove some CRLF. We can only |
Willy Tarreau | 2e046c6 | 2012-03-01 16:08:30 +0100 | [diff] [blame] | 1560 | * do this when o=0. |
Willy Tarreau | 15de77e | 2010-01-02 21:59:16 +0100 | [diff] [blame] | 1561 | */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1562 | if (unlikely(ptr != buf->p)) { |
| 1563 | if (buf->o) |
Willy Tarreau | 15de77e | 2010-01-02 21:59:16 +0100 | [diff] [blame] | 1564 | goto http_msg_ood; |
Willy Tarreau | 1d3bcce | 2009-12-27 15:50:06 +0100 | [diff] [blame] | 1565 | /* Remove empty leading lines, as recommended by RFC2616. */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1566 | bi_fast_delete(buf, ptr - buf->p); |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1567 | } |
Willy Tarreau | 2692736 | 2012-05-18 23:22:52 +0200 | [diff] [blame] | 1568 | msg->sol = 0; |
Willy Tarreau | e92693a | 2012-09-24 21:13:39 +0200 | [diff] [blame] | 1569 | msg->sl.st.l = 0; /* used in debug mode */ |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1570 | hdr_idx_init(idx); |
| 1571 | state = HTTP_MSG_RPVER; |
| 1572 | goto http_msg_rpver; |
| 1573 | } |
| 1574 | |
| 1575 | if (unlikely(!HTTP_IS_CRLF(*ptr))) |
| 1576 | goto http_msg_invalid; |
| 1577 | |
| 1578 | if (unlikely(*ptr == '\n')) |
| 1579 | EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore, HTTP_MSG_RPBEFORE); |
| 1580 | EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore_cr, HTTP_MSG_RPBEFORE_CR); |
| 1581 | /* stop here */ |
| 1582 | |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1583 | case HTTP_MSG_RPBEFORE_CR: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1584 | http_msg_rpbefore_cr: |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1585 | EXPECT_LF_HERE(ptr, http_msg_invalid); |
| 1586 | EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore, HTTP_MSG_RPBEFORE); |
| 1587 | /* stop here */ |
| 1588 | |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1589 | case HTTP_MSG_RPVER: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1590 | http_msg_rpver: |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1591 | case HTTP_MSG_RPVER_SP: |
| 1592 | case HTTP_MSG_RPCODE: |
| 1593 | case HTTP_MSG_RPCODE_SP: |
| 1594 | case HTTP_MSG_RPREASON: |
Willy Tarreau | 69d8c5d | 2012-05-08 09:44:41 +0200 | [diff] [blame] | 1595 | ptr = (char *)http_parse_stsline(msg, |
Willy Tarreau | a458b67 | 2012-03-05 11:17:50 +0100 | [diff] [blame] | 1596 | state, ptr, end, |
| 1597 | &msg->next, &msg->msg_state); |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1598 | if (unlikely(!ptr)) |
| 1599 | return; |
| 1600 | |
| 1601 | /* we have a full response and we know that we have either a CR |
| 1602 | * or an LF at <ptr>. |
| 1603 | */ |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1604 | hdr_idx_set_start(idx, msg->sl.st.l, *ptr == '\r'); |
| 1605 | |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1606 | msg->sol = ptr - buf->p; |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1607 | if (likely(*ptr == '\r')) |
| 1608 | EAT_AND_JUMP_OR_RETURN(http_msg_rpline_end, HTTP_MSG_RPLINE_END); |
| 1609 | goto http_msg_rpline_end; |
| 1610 | |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1611 | case HTTP_MSG_RPLINE_END: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1612 | http_msg_rpline_end: |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1613 | /* msg->sol must point to the first of CR or LF. */ |
| 1614 | EXPECT_LF_HERE(ptr, http_msg_invalid); |
| 1615 | EAT_AND_JUMP_OR_RETURN(http_msg_hdr_first, HTTP_MSG_HDR_FIRST); |
| 1616 | /* stop here */ |
| 1617 | |
| 1618 | /* |
| 1619 | * Second, states that are specific to the request only |
| 1620 | */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1621 | case HTTP_MSG_RQBEFORE: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1622 | http_msg_rqbefore: |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1623 | if (likely(HTTP_IS_TOKEN(*ptr))) { |
Willy Tarreau | 15de77e | 2010-01-02 21:59:16 +0100 | [diff] [blame] | 1624 | /* we have a start of message, but we have to check |
| 1625 | * first if we need to remove some CRLF. We can only |
Willy Tarreau | 2e046c6 | 2012-03-01 16:08:30 +0100 | [diff] [blame] | 1626 | * do this when o=0. |
Willy Tarreau | 15de77e | 2010-01-02 21:59:16 +0100 | [diff] [blame] | 1627 | */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1628 | if (likely(ptr != buf->p)) { |
| 1629 | if (buf->o) |
Willy Tarreau | 15de77e | 2010-01-02 21:59:16 +0100 | [diff] [blame] | 1630 | goto http_msg_ood; |
Willy Tarreau | 1d3bcce | 2009-12-27 15:50:06 +0100 | [diff] [blame] | 1631 | /* Remove empty leading lines, as recommended by RFC2616. */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1632 | bi_fast_delete(buf, ptr - buf->p); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1633 | } |
Willy Tarreau | 2692736 | 2012-05-18 23:22:52 +0200 | [diff] [blame] | 1634 | msg->sol = 0; |
Willy Tarreau | e92693a | 2012-09-24 21:13:39 +0200 | [diff] [blame] | 1635 | msg->sl.rq.l = 0; /* used in debug mode */ |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1636 | state = HTTP_MSG_RQMETH; |
| 1637 | goto http_msg_rqmeth; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1638 | } |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1639 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1640 | if (unlikely(!HTTP_IS_CRLF(*ptr))) |
| 1641 | goto http_msg_invalid; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1642 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1643 | if (unlikely(*ptr == '\n')) |
| 1644 | EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore, HTTP_MSG_RQBEFORE); |
| 1645 | 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] | 1646 | /* stop here */ |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1647 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1648 | case HTTP_MSG_RQBEFORE_CR: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1649 | http_msg_rqbefore_cr: |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1650 | EXPECT_LF_HERE(ptr, http_msg_invalid); |
| 1651 | EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore, HTTP_MSG_RQBEFORE); |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1652 | /* stop here */ |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1653 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1654 | case HTTP_MSG_RQMETH: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1655 | http_msg_rqmeth: |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1656 | case HTTP_MSG_RQMETH_SP: |
| 1657 | case HTTP_MSG_RQURI: |
| 1658 | case HTTP_MSG_RQURI_SP: |
| 1659 | case HTTP_MSG_RQVER: |
Willy Tarreau | 69d8c5d | 2012-05-08 09:44:41 +0200 | [diff] [blame] | 1660 | ptr = (char *)http_parse_reqline(msg, |
Willy Tarreau | a458b67 | 2012-03-05 11:17:50 +0100 | [diff] [blame] | 1661 | state, ptr, end, |
| 1662 | &msg->next, &msg->msg_state); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1663 | if (unlikely(!ptr)) |
| 1664 | return; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1665 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1666 | /* we have a full request and we know that we have either a CR |
| 1667 | * or an LF at <ptr>. |
| 1668 | */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1669 | hdr_idx_set_start(idx, msg->sl.rq.l, *ptr == '\r'); |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1670 | |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1671 | msg->sol = ptr - buf->p; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1672 | if (likely(*ptr == '\r')) |
| 1673 | 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] | 1674 | goto http_msg_rqline_end; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1675 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1676 | case HTTP_MSG_RQLINE_END: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1677 | http_msg_rqline_end: |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1678 | /* check for HTTP/0.9 request : no version information available. |
| 1679 | * msg->sol must point to the first of CR or LF. |
| 1680 | */ |
| 1681 | if (unlikely(msg->sl.rq.v_l == 0)) |
| 1682 | goto http_msg_last_lf; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1683 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1684 | EXPECT_LF_HERE(ptr, http_msg_invalid); |
| 1685 | 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] | 1686 | /* stop here */ |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1687 | |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1688 | /* |
| 1689 | * Common states below |
| 1690 | */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1691 | case HTTP_MSG_HDR_FIRST: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1692 | http_msg_hdr_first: |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1693 | msg->sol = ptr - buf->p; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1694 | if (likely(!HTTP_IS_CRLF(*ptr))) { |
| 1695 | goto http_msg_hdr_name; |
| 1696 | } |
| 1697 | |
| 1698 | if (likely(*ptr == '\r')) |
| 1699 | EAT_AND_JUMP_OR_RETURN(http_msg_last_lf, HTTP_MSG_LAST_LF); |
| 1700 | goto http_msg_last_lf; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1701 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1702 | case HTTP_MSG_HDR_NAME: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1703 | http_msg_hdr_name: |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1704 | /* assumes msg->sol points to the first char */ |
| 1705 | if (likely(HTTP_IS_TOKEN(*ptr))) |
| 1706 | 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] | 1707 | |
Willy Tarreau | fa4a03c | 2012-03-09 21:28:54 +0100 | [diff] [blame] | 1708 | if (likely(*ptr == ':')) |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1709 | 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] | 1710 | |
Willy Tarreau | 32a4ec0 | 2009-04-02 11:35:18 +0200 | [diff] [blame] | 1711 | if (likely(msg->err_pos < -1) || *ptr == '\n') |
| 1712 | goto http_msg_invalid; |
| 1713 | |
| 1714 | if (msg->err_pos == -1) /* capture error pointer */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1715 | msg->err_pos = ptr - buf->p; /* >= 0 now */ |
Willy Tarreau | 32a4ec0 | 2009-04-02 11:35:18 +0200 | [diff] [blame] | 1716 | |
| 1717 | /* and we still accept this non-token character */ |
| 1718 | 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] | 1719 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1720 | case HTTP_MSG_HDR_L1_SP: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1721 | http_msg_hdr_l1_sp: |
Willy Tarreau | fa4a03c | 2012-03-09 21:28:54 +0100 | [diff] [blame] | 1722 | /* assumes msg->sol points to the first char */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1723 | if (likely(HTTP_IS_SPHT(*ptr))) |
| 1724 | 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] | 1725 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1726 | /* header value can be basically anything except CR/LF */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1727 | msg->sov = ptr - buf->p; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1728 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1729 | if (likely(!HTTP_IS_CRLF(*ptr))) { |
| 1730 | goto http_msg_hdr_val; |
| 1731 | } |
| 1732 | |
| 1733 | if (likely(*ptr == '\r')) |
| 1734 | EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_lf, HTTP_MSG_HDR_L1_LF); |
| 1735 | goto http_msg_hdr_l1_lf; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1736 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1737 | case HTTP_MSG_HDR_L1_LF: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1738 | http_msg_hdr_l1_lf: |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1739 | EXPECT_LF_HERE(ptr, http_msg_invalid); |
| 1740 | 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] | 1741 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1742 | case HTTP_MSG_HDR_L1_LWS: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1743 | http_msg_hdr_l1_lws: |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1744 | if (likely(HTTP_IS_SPHT(*ptr))) { |
| 1745 | /* replace HT,CR,LF with spaces */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1746 | for (; buf->p + msg->sov < ptr; msg->sov++) |
| 1747 | buf->p[msg->sov] = ' '; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1748 | goto http_msg_hdr_l1_sp; |
| 1749 | } |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 1750 | /* we had a header consisting only in spaces ! */ |
Willy Tarreau | 12e48b3 | 2012-03-05 16:57:34 +0100 | [diff] [blame] | 1751 | msg->eol = msg->sov; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1752 | goto http_msg_complete_header; |
| 1753 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1754 | case HTTP_MSG_HDR_VAL: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1755 | http_msg_hdr_val: |
Willy Tarreau | fa4a03c | 2012-03-09 21:28:54 +0100 | [diff] [blame] | 1756 | /* assumes msg->sol points to the first char, and msg->sov |
| 1757 | * points to the first character of the value. |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1758 | */ |
| 1759 | if (likely(!HTTP_IS_CRLF(*ptr))) |
| 1760 | EAT_AND_JUMP_OR_RETURN(http_msg_hdr_val, HTTP_MSG_HDR_VAL); |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1761 | |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1762 | msg->eol = ptr - buf->p; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1763 | /* Note: we could also copy eol into ->eoh so that we have the |
| 1764 | * real header end in case it ends with lots of LWS, but is this |
| 1765 | * really needed ? |
| 1766 | */ |
| 1767 | if (likely(*ptr == '\r')) |
| 1768 | EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l2_lf, HTTP_MSG_HDR_L2_LF); |
| 1769 | goto http_msg_hdr_l2_lf; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1770 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1771 | case HTTP_MSG_HDR_L2_LF: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1772 | http_msg_hdr_l2_lf: |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1773 | EXPECT_LF_HERE(ptr, http_msg_invalid); |
| 1774 | 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] | 1775 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1776 | case HTTP_MSG_HDR_L2_LWS: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1777 | http_msg_hdr_l2_lws: |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1778 | if (unlikely(HTTP_IS_SPHT(*ptr))) { |
| 1779 | /* LWS: replace HT,CR,LF with spaces */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1780 | for (; buf->p + msg->eol < ptr; msg->eol++) |
| 1781 | buf->p[msg->eol] = ' '; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1782 | goto http_msg_hdr_val; |
| 1783 | } |
| 1784 | http_msg_complete_header: |
| 1785 | /* |
| 1786 | * It was a new header, so the last one is finished. |
Willy Tarreau | fa4a03c | 2012-03-09 21:28:54 +0100 | [diff] [blame] | 1787 | * Assumes msg->sol points to the first char, msg->sov points |
| 1788 | * to the first character of the value and msg->eol to the |
| 1789 | * first CR or LF so we know how the line ends. We insert last |
| 1790 | * header into the index. |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1791 | */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1792 | if (unlikely(hdr_idx_add(msg->eol - msg->sol, buf->p[msg->eol] == '\r', |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1793 | idx, idx->tail) < 0)) |
| 1794 | goto http_msg_invalid; |
Willy Tarreau | 230fd0b | 2006-12-17 12:05:00 +0100 | [diff] [blame] | 1795 | |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1796 | msg->sol = ptr - buf->p; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1797 | if (likely(!HTTP_IS_CRLF(*ptr))) { |
| 1798 | goto http_msg_hdr_name; |
| 1799 | } |
| 1800 | |
| 1801 | if (likely(*ptr == '\r')) |
| 1802 | EAT_AND_JUMP_OR_RETURN(http_msg_last_lf, HTTP_MSG_LAST_LF); |
| 1803 | goto http_msg_last_lf; |
Willy Tarreau | 230fd0b | 2006-12-17 12:05:00 +0100 | [diff] [blame] | 1804 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1805 | case HTTP_MSG_LAST_LF: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1806 | http_msg_last_lf: |
Willy Tarreau | 0558a02 | 2014-03-13 15:48:45 +0100 | [diff] [blame] | 1807 | /* Assumes msg->sol points to the first of either CR or LF. |
| 1808 | * Sets ->sov and ->next to the total header length, ->eoh to |
| 1809 | * the last CRLF, and ->eol to the last CRLF length (1 or 2). |
| 1810 | */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1811 | EXPECT_LF_HERE(ptr, http_msg_invalid); |
| 1812 | ptr++; |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1813 | msg->sov = msg->next = ptr - buf->p; |
Willy Tarreau | 3a215be | 2012-03-09 21:39:51 +0100 | [diff] [blame] | 1814 | msg->eoh = msg->sol; |
| 1815 | msg->sol = 0; |
Willy Tarreau | 0558a02 | 2014-03-13 15:48:45 +0100 | [diff] [blame] | 1816 | msg->eol = msg->sov - msg->eoh; |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1817 | msg->msg_state = HTTP_MSG_BODY; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1818 | return; |
Willy Tarreau | b56928a | 2012-04-16 14:51:55 +0200 | [diff] [blame] | 1819 | |
| 1820 | case HTTP_MSG_ERROR: |
| 1821 | /* this may only happen if we call http_msg_analyser() twice with an error */ |
| 1822 | break; |
| 1823 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1824 | default: |
Willy Tarreau | 3770f23 | 2013-12-07 00:01:53 +0100 | [diff] [blame] | 1825 | #ifdef DEBUG_FULL |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1826 | fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state); |
| 1827 | exit(1); |
Willy Tarreau | 230fd0b | 2006-12-17 12:05:00 +0100 | [diff] [blame] | 1828 | #endif |
Willy Tarreau | 3770f23 | 2013-12-07 00:01:53 +0100 | [diff] [blame] | 1829 | ; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1830 | } |
| 1831 | http_msg_ood: |
| 1832 | /* out of data */ |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1833 | msg->msg_state = state; |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1834 | msg->next = ptr - buf->p; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1835 | return; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1836 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1837 | http_msg_invalid: |
| 1838 | /* invalid message */ |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1839 | msg->msg_state = HTTP_MSG_ERROR; |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1840 | msg->next = ptr - buf->p; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1841 | return; |
| 1842 | } |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 1843 | |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 1844 | /* convert an HTTP/0.9 request into an HTTP/1.0 request. Returns 1 if the |
| 1845 | * conversion succeeded, 0 in case of error. If the request was already 1.X, |
| 1846 | * nothing is done and 1 is returned. |
| 1847 | */ |
Willy Tarreau | 418bfcc | 2012-03-09 13:56:20 +0100 | [diff] [blame] | 1848 | static int http_upgrade_v09_to_v10(struct http_txn *txn) |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 1849 | { |
| 1850 | int delta; |
| 1851 | char *cur_end; |
Willy Tarreau | 418bfcc | 2012-03-09 13:56:20 +0100 | [diff] [blame] | 1852 | struct http_msg *msg = &txn->req; |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 1853 | |
| 1854 | if (msg->sl.rq.v_l != 0) |
| 1855 | return 1; |
| 1856 | |
Apollon Oikonomopoulos | 25a1522 | 2014-04-06 02:46:00 +0300 | [diff] [blame] | 1857 | /* RFC 1945 allows only GET for HTTP/0.9 requests */ |
| 1858 | if (txn->meth != HTTP_METH_GET) |
| 1859 | return 0; |
| 1860 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 1861 | cur_end = msg->chn->buf->p + msg->sl.rq.l; |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 1862 | delta = 0; |
| 1863 | |
| 1864 | if (msg->sl.rq.u_l == 0) { |
Apollon Oikonomopoulos | 25a1522 | 2014-04-06 02:46:00 +0300 | [diff] [blame] | 1865 | /* HTTP/0.9 requests *must* have a request URI, per RFC 1945 */ |
| 1866 | return 0; |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 1867 | } |
| 1868 | /* add HTTP version */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 1869 | 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] | 1870 | http_msg_move_end(msg, delta); |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 1871 | cur_end += delta; |
Willy Tarreau | 69d8c5d | 2012-05-08 09:44:41 +0200 | [diff] [blame] | 1872 | cur_end = (char *)http_parse_reqline(msg, |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 1873 | HTTP_MSG_RQMETH, |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 1874 | msg->chn->buf->p, cur_end + 1, |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 1875 | NULL, NULL); |
| 1876 | if (unlikely(!cur_end)) |
| 1877 | return 0; |
| 1878 | |
| 1879 | /* we have a full HTTP/1.0 request now and we know that |
| 1880 | * we have either a CR or an LF at <ptr>. |
| 1881 | */ |
| 1882 | hdr_idx_set_start(&txn->hdr_idx, msg->sl.rq.l, *cur_end == '\r'); |
| 1883 | return 1; |
| 1884 | } |
| 1885 | |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 1886 | /* Parse the Connection: header of an HTTP request, looking for both "close" |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 1887 | * and "keep-alive" values. If we already know that some headers may safely |
| 1888 | * 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] | 1889 | * - bit 0 means remove "close" headers (in HTTP/1.0 requests/responses) |
| 1890 | * - 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] | 1891 | * Presence of the "Upgrade" token is also checked and reported. |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 1892 | * The TX_HDR_CONN_* flags are adjusted in txn->flags depending on what was |
| 1893 | * found, and TX_CON_*_SET is adjusted depending on what is left so only |
| 1894 | * harmless combinations may be removed. Do not call that after changes have |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 1895 | * been processed. |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 1896 | */ |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 1897 | 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] | 1898 | { |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 1899 | struct hdr_ctx ctx; |
Willy Tarreau | 88d349d | 2010-01-25 12:15:43 +0100 | [diff] [blame] | 1900 | const char *hdr_val = "Connection"; |
| 1901 | int hdr_len = 10; |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 1902 | |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 1903 | if (txn->flags & TX_HDR_CONN_PRS) |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 1904 | return; |
| 1905 | |
Willy Tarreau | 88d349d | 2010-01-25 12:15:43 +0100 | [diff] [blame] | 1906 | if (unlikely(txn->flags & TX_USE_PX_CONN)) { |
| 1907 | hdr_val = "Proxy-Connection"; |
| 1908 | hdr_len = 16; |
| 1909 | } |
| 1910 | |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 1911 | ctx.idx = 0; |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 1912 | txn->flags &= ~(TX_CON_KAL_SET|TX_CON_CLO_SET); |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 1913 | 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] | 1914 | if (ctx.vlen >= 10 && word_match(ctx.line + ctx.val, ctx.vlen, "keep-alive", 10)) { |
| 1915 | txn->flags |= TX_HDR_CONN_KAL; |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 1916 | if (to_del & 2) |
| 1917 | http_remove_header2(msg, &txn->hdr_idx, &ctx); |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 1918 | else |
| 1919 | txn->flags |= TX_CON_KAL_SET; |
| 1920 | } |
| 1921 | else if (ctx.vlen >= 5 && word_match(ctx.line + ctx.val, ctx.vlen, "close", 5)) { |
| 1922 | txn->flags |= TX_HDR_CONN_CLO; |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 1923 | if (to_del & 1) |
| 1924 | http_remove_header2(msg, &txn->hdr_idx, &ctx); |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 1925 | else |
| 1926 | txn->flags |= TX_CON_CLO_SET; |
| 1927 | } |
Willy Tarreau | 50fc777 | 2012-11-11 22:19:57 +0100 | [diff] [blame] | 1928 | else if (ctx.vlen >= 7 && word_match(ctx.line + ctx.val, ctx.vlen, "upgrade", 7)) { |
| 1929 | txn->flags |= TX_HDR_CONN_UPG; |
| 1930 | } |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 1931 | } |
| 1932 | |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 1933 | txn->flags |= TX_HDR_CONN_PRS; |
| 1934 | return; |
| 1935 | } |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 1936 | |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 1937 | /* Apply desired changes on the Connection: header. Values may be removed and/or |
| 1938 | * added depending on the <wanted> flags, which are exclusively composed of |
| 1939 | * TX_CON_CLO_SET and TX_CON_KAL_SET, depending on what flags are desired. The |
| 1940 | * TX_CON_*_SET flags are adjusted in txn->flags depending on what is left. |
| 1941 | */ |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 1942 | 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] | 1943 | { |
| 1944 | struct hdr_ctx ctx; |
Willy Tarreau | 88d349d | 2010-01-25 12:15:43 +0100 | [diff] [blame] | 1945 | const char *hdr_val = "Connection"; |
| 1946 | int hdr_len = 10; |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 1947 | |
| 1948 | ctx.idx = 0; |
| 1949 | |
Willy Tarreau | 88d349d | 2010-01-25 12:15:43 +0100 | [diff] [blame] | 1950 | |
| 1951 | if (unlikely(txn->flags & TX_USE_PX_CONN)) { |
| 1952 | hdr_val = "Proxy-Connection"; |
| 1953 | hdr_len = 16; |
| 1954 | } |
| 1955 | |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 1956 | txn->flags &= ~(TX_CON_CLO_SET | TX_CON_KAL_SET); |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 1957 | 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] | 1958 | if (ctx.vlen >= 10 && word_match(ctx.line + ctx.val, ctx.vlen, "keep-alive", 10)) { |
| 1959 | if (wanted & TX_CON_KAL_SET) |
| 1960 | txn->flags |= TX_CON_KAL_SET; |
| 1961 | else |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 1962 | http_remove_header2(msg, &txn->hdr_idx, &ctx); |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 1963 | } |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 1964 | else if (ctx.vlen >= 5 && word_match(ctx.line + ctx.val, ctx.vlen, "close", 5)) { |
| 1965 | if (wanted & TX_CON_CLO_SET) |
| 1966 | txn->flags |= TX_CON_CLO_SET; |
| 1967 | else |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 1968 | http_remove_header2(msg, &txn->hdr_idx, &ctx); |
Willy Tarreau | 0dfdf19 | 2010-01-05 11:33:11 +0100 | [diff] [blame] | 1969 | } |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 1970 | } |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 1971 | |
| 1972 | if (wanted == (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET))) |
| 1973 | return; |
| 1974 | |
| 1975 | if ((wanted & TX_CON_CLO_SET) && !(txn->flags & TX_CON_CLO_SET)) { |
| 1976 | txn->flags |= TX_CON_CLO_SET; |
Willy Tarreau | 88d349d | 2010-01-25 12:15:43 +0100 | [diff] [blame] | 1977 | hdr_val = "Connection: close"; |
| 1978 | hdr_len = 17; |
| 1979 | if (unlikely(txn->flags & TX_USE_PX_CONN)) { |
| 1980 | hdr_val = "Proxy-Connection: close"; |
| 1981 | hdr_len = 23; |
| 1982 | } |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 1983 | http_header_add_tail2(msg, &txn->hdr_idx, hdr_val, hdr_len); |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 1984 | } |
| 1985 | |
| 1986 | if ((wanted & TX_CON_KAL_SET) && !(txn->flags & TX_CON_KAL_SET)) { |
| 1987 | txn->flags |= TX_CON_KAL_SET; |
Willy Tarreau | 88d349d | 2010-01-25 12:15:43 +0100 | [diff] [blame] | 1988 | hdr_val = "Connection: keep-alive"; |
| 1989 | hdr_len = 22; |
| 1990 | if (unlikely(txn->flags & TX_USE_PX_CONN)) { |
| 1991 | hdr_val = "Proxy-Connection: keep-alive"; |
| 1992 | hdr_len = 28; |
| 1993 | } |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 1994 | http_header_add_tail2(msg, &txn->hdr_idx, hdr_val, hdr_len); |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 1995 | } |
| 1996 | return; |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 1997 | } |
| 1998 | |
Willy Tarreau | c24715e | 2014-04-17 15:21:20 +0200 | [diff] [blame] | 1999 | /* Parse the chunk size at msg->next. Once done, it adjusts ->next to point to |
| 2000 | * the first byte of data after the chunk size, so that we know we can forward |
| 2001 | * exactly msg->next bytes. msg->sol contains the exact number of bytes forming |
| 2002 | * the chunk size. That way it is always possible to differentiate between the |
| 2003 | * start of the body and the start of the data. |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 2004 | * Return >0 on success, 0 when some data is missing, <0 on error. |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2005 | * Note: this function is designed to parse wrapped CRLF at the end of the buffer. |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 2006 | */ |
Willy Tarreau | 24e6d97 | 2012-10-26 00:49:52 +0200 | [diff] [blame] | 2007 | static inline int http_parse_chunk_size(struct http_msg *msg) |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 2008 | { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2009 | const struct buffer *buf = msg->chn->buf; |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 2010 | const char *ptr = b_ptr(buf, msg->next); |
Willy Tarreau | 4baf44b | 2012-03-09 14:10:20 +0100 | [diff] [blame] | 2011 | const char *ptr_old = ptr; |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 2012 | const char *end = buf->data + buf->size; |
| 2013 | const char *stop = bi_end(buf); |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 2014 | unsigned int chunk = 0; |
| 2015 | |
| 2016 | /* The chunk size is in the following form, though we are only |
| 2017 | * interested in the size and CRLF : |
| 2018 | * 1*HEXDIGIT *WSP *[ ';' extensions ] CRLF |
| 2019 | */ |
| 2020 | while (1) { |
| 2021 | int c; |
Willy Tarreau | 363a5bb | 2012-03-02 20:14:45 +0100 | [diff] [blame] | 2022 | if (ptr == stop) |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 2023 | return 0; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2024 | c = hex2i(*ptr); |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 2025 | if (c < 0) /* not a hex digit anymore */ |
| 2026 | break; |
Willy Tarreau | 0161d62 | 2013-04-02 01:26:55 +0200 | [diff] [blame] | 2027 | if (unlikely(++ptr >= end)) |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 2028 | ptr = buf->data; |
Willy Tarreau | 431946e | 2012-02-24 19:20:12 +0100 | [diff] [blame] | 2029 | if (chunk & 0xF8000000) /* integer overflow will occur if result >= 2GB */ |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 2030 | goto error; |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 2031 | chunk = (chunk << 4) + c; |
| 2032 | } |
| 2033 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2034 | /* empty size not allowed */ |
Willy Tarreau | 0161d62 | 2013-04-02 01:26:55 +0200 | [diff] [blame] | 2035 | if (unlikely(ptr == ptr_old)) |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 2036 | goto error; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2037 | |
| 2038 | while (http_is_spht[(unsigned char)*ptr]) { |
| 2039 | if (++ptr >= end) |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 2040 | ptr = buf->data; |
Willy Tarreau | 0161d62 | 2013-04-02 01:26:55 +0200 | [diff] [blame] | 2041 | if (unlikely(ptr == stop)) |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 2042 | return 0; |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 2043 | } |
| 2044 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2045 | /* Up to there, we know that at least one byte is present at *ptr. Check |
| 2046 | * for the end of chunk size. |
| 2047 | */ |
| 2048 | while (1) { |
| 2049 | if (likely(HTTP_IS_CRLF(*ptr))) { |
| 2050 | /* we now have a CR or an LF at ptr */ |
| 2051 | if (likely(*ptr == '\r')) { |
| 2052 | if (++ptr >= end) |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 2053 | ptr = buf->data; |
Willy Tarreau | 363a5bb | 2012-03-02 20:14:45 +0100 | [diff] [blame] | 2054 | if (ptr == stop) |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2055 | return 0; |
| 2056 | } |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 2057 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2058 | if (*ptr != '\n') |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 2059 | goto error; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2060 | if (++ptr >= end) |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 2061 | ptr = buf->data; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2062 | /* done */ |
| 2063 | break; |
| 2064 | } |
| 2065 | else if (*ptr == ';') { |
| 2066 | /* chunk extension, ends at next CRLF */ |
| 2067 | if (++ptr >= end) |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 2068 | ptr = buf->data; |
Willy Tarreau | 363a5bb | 2012-03-02 20:14:45 +0100 | [diff] [blame] | 2069 | if (ptr == stop) |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 2070 | return 0; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2071 | |
| 2072 | while (!HTTP_IS_CRLF(*ptr)) { |
| 2073 | if (++ptr >= end) |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 2074 | ptr = buf->data; |
Willy Tarreau | 363a5bb | 2012-03-02 20:14:45 +0100 | [diff] [blame] | 2075 | if (ptr == stop) |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2076 | return 0; |
| 2077 | } |
| 2078 | /* we have a CRLF now, loop above */ |
| 2079 | continue; |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 2080 | } |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2081 | else |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 2082 | goto error; |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 2083 | } |
| 2084 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2085 | /* OK we found our CRLF and now <ptr> points to the next byte, |
Willy Tarreau | c24715e | 2014-04-17 15:21:20 +0200 | [diff] [blame] | 2086 | * which may or may not be present. We save that into ->next, |
| 2087 | * and the number of bytes parsed into msg->sol. |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 2088 | */ |
Willy Tarreau | c24715e | 2014-04-17 15:21:20 +0200 | [diff] [blame] | 2089 | msg->sol = ptr - ptr_old; |
Willy Tarreau | 0161d62 | 2013-04-02 01:26:55 +0200 | [diff] [blame] | 2090 | if (unlikely(ptr < ptr_old)) |
Willy Tarreau | c24715e | 2014-04-17 15:21:20 +0200 | [diff] [blame] | 2091 | msg->sol += buf->size; |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 2092 | msg->next = buffer_count(buf, buf->p, ptr); |
Willy Tarreau | 124d991 | 2011-03-01 20:30:48 +0100 | [diff] [blame] | 2093 | msg->chunk_len = chunk; |
| 2094 | msg->body_len += chunk; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2095 | msg->msg_state = chunk ? HTTP_MSG_DATA : HTTP_MSG_TRAILERS; |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 2096 | return 1; |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 2097 | error: |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 2098 | msg->err_pos = buffer_count(buf, buf->p, ptr); |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 2099 | return -1; |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 2100 | } |
| 2101 | |
Willy Tarreau | 4baf44b | 2012-03-09 14:10:20 +0100 | [diff] [blame] | 2102 | /* This function skips trailers in the buffer associated with HTTP |
Willy Tarreau | a458b67 | 2012-03-05 11:17:50 +0100 | [diff] [blame] | 2103 | * message <msg>. The first visited position is msg->next. If the end of |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2104 | * the trailers is found, it is automatically scheduled to be forwarded, |
| 2105 | * msg->msg_state switches to HTTP_MSG_DONE, and the function returns >0. |
| 2106 | * If not enough data are available, the function does not change anything |
Willy Tarreau | c24715e | 2014-04-17 15:21:20 +0200 | [diff] [blame] | 2107 | * except maybe msg->next if it could parse some lines, and returns zero. |
| 2108 | * If a parse error is encountered, the function returns < 0 and does not |
| 2109 | * change anything except maybe msg->next. Note that the message must |
| 2110 | * already be in HTTP_MSG_TRAILERS state before calling this function, |
Willy Tarreau | 638cd02 | 2010-01-03 07:42:04 +0100 | [diff] [blame] | 2111 | * which implies that all non-trailers data have already been scheduled for |
Willy Tarreau | c24715e | 2014-04-17 15:21:20 +0200 | [diff] [blame] | 2112 | * forwarding, and that msg->next exactly matches the length of trailers |
| 2113 | * already parsed and not forwarded. It is also important to note that this |
| 2114 | * function is designed to be able to parse wrapped headers at end of buffer. |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2115 | */ |
Willy Tarreau | 24e6d97 | 2012-10-26 00:49:52 +0200 | [diff] [blame] | 2116 | static int http_forward_trailers(struct http_msg *msg) |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2117 | { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2118 | const struct buffer *buf = msg->chn->buf; |
Willy Tarreau | 4baf44b | 2012-03-09 14:10:20 +0100 | [diff] [blame] | 2119 | |
Willy Tarreau | a458b67 | 2012-03-05 11:17:50 +0100 | [diff] [blame] | 2120 | /* we have msg->next which points to next line. Look for CRLF. */ |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2121 | while (1) { |
Willy Tarreau | 4baf44b | 2012-03-09 14:10:20 +0100 | [diff] [blame] | 2122 | const char *p1 = NULL, *p2 = NULL; |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 2123 | const char *ptr = b_ptr(buf, msg->next); |
| 2124 | const char *stop = bi_end(buf); |
Willy Tarreau | 638cd02 | 2010-01-03 07:42:04 +0100 | [diff] [blame] | 2125 | int bytes; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2126 | |
| 2127 | /* scan current line and stop at LF or CRLF */ |
| 2128 | while (1) { |
Willy Tarreau | 363a5bb | 2012-03-02 20:14:45 +0100 | [diff] [blame] | 2129 | if (ptr == stop) |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2130 | return 0; |
| 2131 | |
| 2132 | if (*ptr == '\n') { |
| 2133 | if (!p1) |
| 2134 | p1 = ptr; |
| 2135 | p2 = ptr; |
| 2136 | break; |
| 2137 | } |
| 2138 | |
| 2139 | if (*ptr == '\r') { |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 2140 | if (p1) { |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 2141 | msg->err_pos = buffer_count(buf, buf->p, ptr); |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2142 | return -1; |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 2143 | } |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2144 | p1 = ptr; |
| 2145 | } |
| 2146 | |
| 2147 | ptr++; |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 2148 | if (ptr >= buf->data + buf->size) |
| 2149 | ptr = buf->data; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2150 | } |
| 2151 | |
| 2152 | /* after LF; point to beginning of next line */ |
| 2153 | p2++; |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 2154 | if (p2 >= buf->data + buf->size) |
| 2155 | p2 = buf->data; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2156 | |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 2157 | bytes = p2 - b_ptr(buf, msg->next); |
Willy Tarreau | 638cd02 | 2010-01-03 07:42:04 +0100 | [diff] [blame] | 2158 | if (bytes < 0) |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 2159 | bytes += buf->size; |
Willy Tarreau | 638cd02 | 2010-01-03 07:42:04 +0100 | [diff] [blame] | 2160 | |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 2161 | if (p1 == b_ptr(buf, msg->next)) { |
Willy Tarreau | 638cd02 | 2010-01-03 07:42:04 +0100 | [diff] [blame] | 2162 | /* LF/CRLF at beginning of line => end of trailers at p2. |
| 2163 | * Everything was scheduled for forwarding, there's nothing |
| 2164 | * left from this message. |
Willy Tarreau | 5523b32 | 2009-12-29 12:05:52 +0100 | [diff] [blame] | 2165 | */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 2166 | msg->next = buffer_count(buf, buf->p, p2); |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2167 | msg->msg_state = HTTP_MSG_DONE; |
| 2168 | return 1; |
| 2169 | } |
| 2170 | /* OK, next line then */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 2171 | msg->next = buffer_count(buf, buf->p, p2); |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2172 | } |
| 2173 | } |
| 2174 | |
Willy Tarreau | c24715e | 2014-04-17 15:21:20 +0200 | [diff] [blame] | 2175 | /* This function may be called only in HTTP_MSG_CHUNK_CRLF. It reads the CRLF |
| 2176 | * or a possible LF alone at the end of a chunk. It automatically adjusts |
| 2177 | * msg->next in order to include this part into the next forwarding phase. |
Willy Tarreau | a458b67 | 2012-03-05 11:17:50 +0100 | [diff] [blame] | 2178 | * Note that the caller must ensure that ->p points to the first byte to parse. |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2179 | * It also sets msg_state to HTTP_MSG_CHUNK_SIZE and returns >0 on success. If |
| 2180 | * not enough data are available, the function does not change anything and |
| 2181 | * returns zero. If a parse error is encountered, the function returns < 0 and |
| 2182 | * does not change anything. Note: this function is designed to parse wrapped |
| 2183 | * CRLF at the end of the buffer. |
| 2184 | */ |
Willy Tarreau | 24e6d97 | 2012-10-26 00:49:52 +0200 | [diff] [blame] | 2185 | static inline int http_skip_chunk_crlf(struct http_msg *msg) |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2186 | { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2187 | const struct buffer *buf = msg->chn->buf; |
Willy Tarreau | 4baf44b | 2012-03-09 14:10:20 +0100 | [diff] [blame] | 2188 | const char *ptr; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2189 | int bytes; |
| 2190 | |
| 2191 | /* NB: we'll check data availabilty at the end. It's not a |
| 2192 | * problem because whatever we match first will be checked |
| 2193 | * against the correct length. |
| 2194 | */ |
| 2195 | bytes = 1; |
Willy Tarreau | 0669d7d | 2014-04-17 11:40:10 +0200 | [diff] [blame] | 2196 | ptr = b_ptr(buf, msg->next); |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2197 | if (*ptr == '\r') { |
| 2198 | bytes++; |
| 2199 | ptr++; |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 2200 | if (ptr >= buf->data + buf->size) |
| 2201 | ptr = buf->data; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2202 | } |
| 2203 | |
Willy Tarreau | 0669d7d | 2014-04-17 11:40:10 +0200 | [diff] [blame] | 2204 | if (msg->next + bytes > buf->i) |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2205 | return 0; |
| 2206 | |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 2207 | if (*ptr != '\n') { |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 2208 | msg->err_pos = buffer_count(buf, buf->p, ptr); |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2209 | return -1; |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 2210 | } |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2211 | |
| 2212 | ptr++; |
Willy Tarreau | 0161d62 | 2013-04-02 01:26:55 +0200 | [diff] [blame] | 2213 | if (unlikely(ptr >= buf->data + buf->size)) |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 2214 | ptr = buf->data; |
Willy Tarreau | c24715e | 2014-04-17 15:21:20 +0200 | [diff] [blame] | 2215 | /* Advance ->next to allow the CRLF to be forwarded */ |
Willy Tarreau | 0669d7d | 2014-04-17 11:40:10 +0200 | [diff] [blame] | 2216 | msg->next += bytes; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2217 | msg->msg_state = HTTP_MSG_CHUNK_SIZE; |
| 2218 | return 1; |
| 2219 | } |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 2220 | |
Willy Tarreau | 0e9b1b4 | 2014-03-19 12:07:52 +0100 | [diff] [blame] | 2221 | /* Parses a qvalue and returns it multipled by 1000, from 0 to 1000. If the |
| 2222 | * value is larger than 1000, it is bound to 1000. The parser consumes up to |
| 2223 | * 1 digit, one dot and 3 digits and stops on the first invalid character. |
| 2224 | * Unparsable qvalues return 1000 as "q=1.000". |
| 2225 | */ |
Thierry FOURNIER | ad90351 | 2014-04-11 17:51:01 +0200 | [diff] [blame] | 2226 | int parse_qvalue(const char *qvalue, const char **end) |
Willy Tarreau | 0e9b1b4 | 2014-03-19 12:07:52 +0100 | [diff] [blame] | 2227 | { |
| 2228 | int q = 1000; |
| 2229 | |
Willy Tarreau | 506c69a | 2014-07-08 00:59:48 +0200 | [diff] [blame] | 2230 | if (!isdigit((unsigned char)*qvalue)) |
Willy Tarreau | 0e9b1b4 | 2014-03-19 12:07:52 +0100 | [diff] [blame] | 2231 | goto out; |
| 2232 | q = (*qvalue++ - '0') * 1000; |
| 2233 | |
| 2234 | if (*qvalue++ != '.') |
| 2235 | goto out; |
| 2236 | |
Willy Tarreau | 506c69a | 2014-07-08 00:59:48 +0200 | [diff] [blame] | 2237 | if (!isdigit((unsigned char)*qvalue)) |
Willy Tarreau | 0e9b1b4 | 2014-03-19 12:07:52 +0100 | [diff] [blame] | 2238 | goto out; |
| 2239 | q += (*qvalue++ - '0') * 100; |
| 2240 | |
Willy Tarreau | 506c69a | 2014-07-08 00:59:48 +0200 | [diff] [blame] | 2241 | if (!isdigit((unsigned char)*qvalue)) |
Willy Tarreau | 0e9b1b4 | 2014-03-19 12:07:52 +0100 | [diff] [blame] | 2242 | goto out; |
| 2243 | q += (*qvalue++ - '0') * 10; |
| 2244 | |
Willy Tarreau | 506c69a | 2014-07-08 00:59:48 +0200 | [diff] [blame] | 2245 | if (!isdigit((unsigned char)*qvalue)) |
Willy Tarreau | 0e9b1b4 | 2014-03-19 12:07:52 +0100 | [diff] [blame] | 2246 | goto out; |
| 2247 | q += (*qvalue++ - '0') * 1; |
| 2248 | out: |
| 2249 | if (q > 1000) |
| 2250 | q = 1000; |
Willy Tarreau | 38b3aa5 | 2014-04-22 23:32:05 +0200 | [diff] [blame] | 2251 | if (end) |
Thierry FOURNIER | ad90351 | 2014-04-11 17:51:01 +0200 | [diff] [blame] | 2252 | *end = qvalue; |
Willy Tarreau | 0e9b1b4 | 2014-03-19 12:07:52 +0100 | [diff] [blame] | 2253 | return q; |
| 2254 | } |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 2255 | |
| 2256 | /* |
| 2257 | * Selects a compression algorithm depending on the client request. |
Willy Tarreau | 05d8460 | 2012-10-26 02:11:25 +0200 | [diff] [blame] | 2258 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 2259 | int select_compression_request_header(struct stream *s, struct buffer *req) |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 2260 | { |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 2261 | struct http_txn *txn = s->txn; |
Willy Tarreau | 70737d1 | 2012-10-27 00:34:28 +0200 | [diff] [blame] | 2262 | struct http_msg *msg = &txn->req; |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 2263 | struct hdr_ctx ctx; |
| 2264 | struct comp_algo *comp_algo = NULL; |
Willy Tarreau | 3c7b97b | 2012-10-26 14:50:26 +0200 | [diff] [blame] | 2265 | struct comp_algo *comp_algo_back = NULL; |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 2266 | |
Finn Arne Gangstad | cbb9a4b | 2012-10-29 21:43:01 +0100 | [diff] [blame] | 2267 | /* Disable compression for older user agents announcing themselves as "Mozilla/4" |
| 2268 | * unless they are known good (MSIE 6 with XP SP2, or MSIE 7 and later). |
Willy Tarreau | 05d8460 | 2012-10-26 02:11:25 +0200 | [diff] [blame] | 2269 | * See http://zoompf.com/2012/02/lose-the-wait-http-compression for more details. |
| 2270 | */ |
| 2271 | ctx.idx = 0; |
| 2272 | if (http_find_header2("User-Agent", 10, req->p, &txn->hdr_idx, &ctx) && |
| 2273 | ctx.vlen >= 9 && |
Finn Arne Gangstad | cbb9a4b | 2012-10-29 21:43:01 +0100 | [diff] [blame] | 2274 | memcmp(ctx.line + ctx.val, "Mozilla/4", 9) == 0 && |
| 2275 | (ctx.vlen < 31 || |
| 2276 | memcmp(ctx.line + ctx.val + 25, "MSIE ", 5) != 0 || |
| 2277 | ctx.line[ctx.val + 30] < '6' || |
| 2278 | (ctx.line[ctx.val + 30] == '6' && |
| 2279 | (ctx.vlen < 54 || memcmp(ctx.line + 51, "SV1", 3) != 0)))) { |
| 2280 | s->comp_algo = NULL; |
| 2281 | return 0; |
Willy Tarreau | 05d8460 | 2012-10-26 02:11:25 +0200 | [diff] [blame] | 2282 | } |
| 2283 | |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 2284 | /* search for the algo in the backend in priority or the frontend */ |
Willy Tarreau | d0d8da9 | 2015-04-04 02:10:38 +0200 | [diff] [blame] | 2285 | if ((s->be->comp && (comp_algo_back = s->be->comp->algos)) || (strm_fe(s)->comp && (comp_algo_back = strm_fe(s)->comp->algos))) { |
Willy Tarreau | 0e9b1b4 | 2014-03-19 12:07:52 +0100 | [diff] [blame] | 2286 | int best_q = 0; |
| 2287 | |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 2288 | ctx.idx = 0; |
| 2289 | while (http_find_header2("Accept-Encoding", 15, req->p, &txn->hdr_idx, &ctx)) { |
Willy Tarreau | 0e9b1b4 | 2014-03-19 12:07:52 +0100 | [diff] [blame] | 2290 | const char *qval; |
| 2291 | int q; |
| 2292 | int toklen; |
| 2293 | |
| 2294 | /* try to isolate the token from the optional q-value */ |
| 2295 | toklen = 0; |
| 2296 | while (toklen < ctx.vlen && http_is_token[(unsigned char)*(ctx.line + ctx.val + toklen)]) |
| 2297 | toklen++; |
| 2298 | |
| 2299 | qval = ctx.line + ctx.val + toklen; |
| 2300 | while (1) { |
| 2301 | while (qval < ctx.line + ctx.val + ctx.vlen && http_is_lws[(unsigned char)*qval]) |
| 2302 | qval++; |
| 2303 | |
| 2304 | if (qval >= ctx.line + ctx.val + ctx.vlen || *qval != ';') { |
| 2305 | qval = NULL; |
| 2306 | break; |
| 2307 | } |
| 2308 | qval++; |
Willy Tarreau | 70737d1 | 2012-10-27 00:34:28 +0200 | [diff] [blame] | 2309 | |
Willy Tarreau | 0e9b1b4 | 2014-03-19 12:07:52 +0100 | [diff] [blame] | 2310 | while (qval < ctx.line + ctx.val + ctx.vlen && http_is_lws[(unsigned char)*qval]) |
| 2311 | qval++; |
Willy Tarreau | 70737d1 | 2012-10-27 00:34:28 +0200 | [diff] [blame] | 2312 | |
Willy Tarreau | 0e9b1b4 | 2014-03-19 12:07:52 +0100 | [diff] [blame] | 2313 | if (qval >= ctx.line + ctx.val + ctx.vlen) { |
| 2314 | qval = NULL; |
| 2315 | break; |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 2316 | } |
Willy Tarreau | 0e9b1b4 | 2014-03-19 12:07:52 +0100 | [diff] [blame] | 2317 | if (strncmp(qval, "q=", MIN(ctx.line + ctx.val + ctx.vlen - qval, 2)) == 0) |
| 2318 | break; |
| 2319 | |
| 2320 | while (qval < ctx.line + ctx.val + ctx.vlen && *qval != ';') |
| 2321 | qval++; |
| 2322 | } |
| 2323 | |
| 2324 | /* here we have qval pointing to the first "q=" attribute or NULL if not found */ |
Thierry FOURNIER | ad90351 | 2014-04-11 17:51:01 +0200 | [diff] [blame] | 2325 | q = qval ? parse_qvalue(qval + 2, NULL) : 1000; |
Willy Tarreau | 0e9b1b4 | 2014-03-19 12:07:52 +0100 | [diff] [blame] | 2326 | |
| 2327 | if (q <= best_q) |
| 2328 | continue; |
| 2329 | |
| 2330 | for (comp_algo = comp_algo_back; comp_algo; comp_algo = comp_algo->next) { |
| 2331 | if (*(ctx.line + ctx.val) == '*' || |
Willy Tarreau | 615105e | 2015-03-28 16:40:46 +0100 | [diff] [blame] | 2332 | word_match(ctx.line + ctx.val, toklen, comp_algo->ua_name, comp_algo->ua_name_len)) { |
Willy Tarreau | 0e9b1b4 | 2014-03-19 12:07:52 +0100 | [diff] [blame] | 2333 | s->comp_algo = comp_algo; |
| 2334 | best_q = q; |
| 2335 | break; |
| 2336 | } |
| 2337 | } |
| 2338 | } |
| 2339 | } |
| 2340 | |
| 2341 | /* remove all occurrences of the header when "compression offload" is set */ |
| 2342 | if (s->comp_algo) { |
Willy Tarreau | d0d8da9 | 2015-04-04 02:10:38 +0200 | [diff] [blame] | 2343 | if ((s->be->comp && s->be->comp->offload) || (strm_fe(s)->comp && strm_fe(s)->comp->offload)) { |
Willy Tarreau | 0e9b1b4 | 2014-03-19 12:07:52 +0100 | [diff] [blame] | 2344 | http_remove_header2(msg, &txn->hdr_idx, &ctx); |
| 2345 | ctx.idx = 0; |
| 2346 | while (http_find_header2("Accept-Encoding", 15, req->p, &txn->hdr_idx, &ctx)) { |
| 2347 | http_remove_header2(msg, &txn->hdr_idx, &ctx); |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 2348 | } |
| 2349 | } |
Willy Tarreau | 0e9b1b4 | 2014-03-19 12:07:52 +0100 | [diff] [blame] | 2350 | return 1; |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 2351 | } |
| 2352 | |
| 2353 | /* identity is implicit does not require headers */ |
Willy Tarreau | d0d8da9 | 2015-04-04 02:10:38 +0200 | [diff] [blame] | 2354 | if ((s->be->comp && (comp_algo_back = s->be->comp->algos)) || (strm_fe(s)->comp && (comp_algo_back = strm_fe(s)->comp->algos))) { |
Willy Tarreau | 3c7b97b | 2012-10-26 14:50:26 +0200 | [diff] [blame] | 2355 | for (comp_algo = comp_algo_back; comp_algo; comp_algo = comp_algo->next) { |
Willy Tarreau | 615105e | 2015-03-28 16:40:46 +0100 | [diff] [blame] | 2356 | if (comp_algo->cfg_name_len == 8 && memcmp(comp_algo->cfg_name, "identity", 8) == 0) { |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 2357 | s->comp_algo = comp_algo; |
| 2358 | return 1; |
| 2359 | } |
| 2360 | } |
| 2361 | } |
| 2362 | |
| 2363 | s->comp_algo = NULL; |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 2364 | return 0; |
| 2365 | } |
| 2366 | |
| 2367 | /* |
| 2368 | * Selects a comression algorithm depending of the server response. |
| 2369 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 2370 | int select_compression_response_header(struct stream *s, struct buffer *res) |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 2371 | { |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 2372 | struct http_txn *txn = s->txn; |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 2373 | struct http_msg *msg = &txn->rsp; |
| 2374 | struct hdr_ctx ctx; |
| 2375 | struct comp_type *comp_type; |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 2376 | |
| 2377 | /* no common compression algorithm was found in request header */ |
| 2378 | if (s->comp_algo == NULL) |
| 2379 | goto fail; |
| 2380 | |
| 2381 | /* HTTP < 1.1 should not be compressed */ |
Willy Tarreau | 7257550 | 2013-12-24 14:41:35 +0100 | [diff] [blame] | 2382 | if (!(msg->flags & HTTP_MSGF_VER_11) || !(txn->req.flags & HTTP_MSGF_VER_11)) |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 2383 | goto fail; |
| 2384 | |
Jesse Hathaway | 2468d4e | 2015-03-06 20:16:15 +0000 | [diff] [blame] | 2385 | /* compress 200,201,202,203 responses only */ |
| 2386 | if ((txn->status != 200) && |
| 2387 | (txn->status != 201) && |
| 2388 | (txn->status != 202) && |
| 2389 | (txn->status != 203)) |
William Lallemand | d300261 | 2012-11-26 14:34:47 +0100 | [diff] [blame] | 2390 | goto fail; |
| 2391 | |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 2392 | /* Content-Length is null */ |
| 2393 | if (!(msg->flags & HTTP_MSGF_TE_CHNK) && msg->body_len == 0) |
| 2394 | goto fail; |
| 2395 | |
| 2396 | /* content is already compressed */ |
Willy Tarreau | 0a80a8d | 2012-11-26 16:33:37 +0100 | [diff] [blame] | 2397 | ctx.idx = 0; |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 2398 | if (http_find_header2("Content-Encoding", 16, res->p, &txn->hdr_idx, &ctx)) |
| 2399 | goto fail; |
| 2400 | |
Willy Tarreau | 56e9ffa | 2013-01-05 16:20:35 +0100 | [diff] [blame] | 2401 | /* no compression when Cache-Control: no-transform is present in the message */ |
| 2402 | ctx.idx = 0; |
| 2403 | while (http_find_header2("Cache-Control", 13, res->p, &txn->hdr_idx, &ctx)) { |
| 2404 | if (word_match(ctx.line + ctx.val, ctx.vlen, "no-transform", 12)) |
| 2405 | goto fail; |
| 2406 | } |
| 2407 | |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 2408 | comp_type = NULL; |
| 2409 | |
Willy Tarreau | 0a80a8d | 2012-11-26 16:33:37 +0100 | [diff] [blame] | 2410 | /* we don't want to compress multipart content-types, nor content-types that are |
| 2411 | * not listed in the "compression type" directive if any. If no content-type was |
| 2412 | * found but configuration requires one, we don't compress either. Backend has |
| 2413 | * the priority. |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 2414 | */ |
Willy Tarreau | 0a80a8d | 2012-11-26 16:33:37 +0100 | [diff] [blame] | 2415 | ctx.idx = 0; |
| 2416 | if (http_find_header2("Content-Type", 12, res->p, &txn->hdr_idx, &ctx)) { |
| 2417 | if (ctx.vlen >= 9 && strncasecmp("multipart", ctx.line+ctx.val, 9) == 0) |
| 2418 | goto fail; |
| 2419 | |
| 2420 | if ((s->be->comp && (comp_type = s->be->comp->types)) || |
Willy Tarreau | d0d8da9 | 2015-04-04 02:10:38 +0200 | [diff] [blame] | 2421 | (strm_fe(s)->comp && (comp_type = strm_fe(s)->comp->types))) { |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 2422 | for (; comp_type; comp_type = comp_type->next) { |
Willy Tarreau | 0a80a8d | 2012-11-26 16:33:37 +0100 | [diff] [blame] | 2423 | if (ctx.vlen >= comp_type->name_len && |
| 2424 | strncasecmp(ctx.line+ctx.val, comp_type->name, comp_type->name_len) == 0) |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 2425 | /* this Content-Type should be compressed */ |
| 2426 | break; |
| 2427 | } |
Willy Tarreau | 0a80a8d | 2012-11-26 16:33:37 +0100 | [diff] [blame] | 2428 | /* this Content-Type should not be compressed */ |
| 2429 | if (comp_type == NULL) |
| 2430 | goto fail; |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 2431 | } |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 2432 | } |
Willy Tarreau | 0a80a8d | 2012-11-26 16:33:37 +0100 | [diff] [blame] | 2433 | else { /* no content-type header */ |
Willy Tarreau | d0d8da9 | 2015-04-04 02:10:38 +0200 | [diff] [blame] | 2434 | if ((s->be->comp && s->be->comp->types) || (strm_fe(s)->comp && strm_fe(s)->comp->types)) |
Willy Tarreau | 0a80a8d | 2012-11-26 16:33:37 +0100 | [diff] [blame] | 2435 | goto fail; /* a content-type was required */ |
William Lallemand | d300261 | 2012-11-26 14:34:47 +0100 | [diff] [blame] | 2436 | } |
| 2437 | |
William Lallemand | d85f917 | 2012-11-09 17:05:39 +0100 | [diff] [blame] | 2438 | /* limit compression rate */ |
| 2439 | if (global.comp_rate_lim > 0) |
| 2440 | if (read_freq_ctr(&global.comp_bps_in) > global.comp_rate_lim) |
| 2441 | goto fail; |
| 2442 | |
William Lallemand | 072a2bf | 2012-11-20 17:01:01 +0100 | [diff] [blame] | 2443 | /* limit cpu usage */ |
| 2444 | if (idle_pct < compress_min_idle) |
| 2445 | goto fail; |
| 2446 | |
William Lallemand | 4c49fae | 2012-11-07 15:00:23 +0100 | [diff] [blame] | 2447 | /* initialize compression */ |
William Lallemand | f374783 | 2012-11-09 12:33:10 +0100 | [diff] [blame] | 2448 | if (s->comp_algo->init(&s->comp_ctx, global.tune.comp_maxlevel) < 0) |
William Lallemand | 4c49fae | 2012-11-07 15:00:23 +0100 | [diff] [blame] | 2449 | goto fail; |
| 2450 | |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 2451 | s->flags |= SF_COMP_READY; |
William Lallemand | ec3e389 | 2012-11-12 17:02:18 +0100 | [diff] [blame] | 2452 | |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 2453 | /* remove Content-Length header */ |
Willy Tarreau | 0a80a8d | 2012-11-26 16:33:37 +0100 | [diff] [blame] | 2454 | ctx.idx = 0; |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 2455 | if ((msg->flags & HTTP_MSGF_CNT_LEN) && http_find_header2("Content-Length", 14, res->p, &txn->hdr_idx, &ctx)) |
| 2456 | http_remove_header2(msg, &txn->hdr_idx, &ctx); |
| 2457 | |
| 2458 | /* add Transfer-Encoding header */ |
| 2459 | if (!(msg->flags & HTTP_MSGF_TE_CHNK)) |
| 2460 | http_header_add_tail2(&txn->rsp, &txn->hdr_idx, "Transfer-Encoding: chunked", 26); |
| 2461 | |
| 2462 | /* |
| 2463 | * Add Content-Encoding header when it's not identity encoding. |
| 2464 | * RFC 2616 : Identity encoding: This content-coding is used only in the |
| 2465 | * Accept-Encoding header, and SHOULD NOT be used in the Content-Encoding |
| 2466 | * header. |
| 2467 | */ |
Willy Tarreau | 615105e | 2015-03-28 16:40:46 +0100 | [diff] [blame] | 2468 | if (s->comp_algo->cfg_name_len != 8 || memcmp(s->comp_algo->cfg_name, "identity", 8) != 0) { |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 2469 | trash.len = 18; |
| 2470 | memcpy(trash.str, "Content-Encoding: ", trash.len); |
Willy Tarreau | 615105e | 2015-03-28 16:40:46 +0100 | [diff] [blame] | 2471 | memcpy(trash.str + trash.len, s->comp_algo->ua_name, s->comp_algo->ua_name_len); |
| 2472 | trash.len += s->comp_algo->ua_name_len; |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 2473 | trash.str[trash.len] = '\0'; |
| 2474 | http_header_add_tail2(&txn->rsp, &txn->hdr_idx, trash.str, trash.len); |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 2475 | } |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 2476 | return 1; |
| 2477 | |
| 2478 | fail: |
Willy Tarreau | b97b619 | 2012-11-19 14:55:02 +0100 | [diff] [blame] | 2479 | s->comp_algo = NULL; |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 2480 | return 0; |
| 2481 | } |
| 2482 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 2483 | 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] | 2484 | { |
Willy Tarreau | d0d8da9 | 2015-04-04 02:10:38 +0200 | [diff] [blame] | 2485 | struct proxy *fe = strm_fe(s); |
Willy Tarreau | 4e21ff9 | 2014-09-30 18:44:22 +0200 | [diff] [blame] | 2486 | int tmp = TX_CON_WANT_KAL; |
| 2487 | |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 2488 | if (!((fe->options2|s->be->options2) & PR_O2_FAKE_KA)) { |
| 2489 | if ((fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_TUN || |
Willy Tarreau | 4e21ff9 | 2014-09-30 18:44:22 +0200 | [diff] [blame] | 2490 | (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_TUN) |
| 2491 | tmp = TX_CON_WANT_TUN; |
| 2492 | |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 2493 | if ((fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL || |
Willy Tarreau | 4e21ff9 | 2014-09-30 18:44:22 +0200 | [diff] [blame] | 2494 | (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL) |
| 2495 | tmp = TX_CON_WANT_TUN; |
| 2496 | } |
| 2497 | |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 2498 | if ((fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_SCL || |
Willy Tarreau | 4e21ff9 | 2014-09-30 18:44:22 +0200 | [diff] [blame] | 2499 | (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_SCL) { |
| 2500 | /* option httpclose + server_close => forceclose */ |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 2501 | if ((fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL || |
Willy Tarreau | 4e21ff9 | 2014-09-30 18:44:22 +0200 | [diff] [blame] | 2502 | (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL) |
| 2503 | tmp = TX_CON_WANT_CLO; |
| 2504 | else |
| 2505 | tmp = TX_CON_WANT_SCL; |
| 2506 | } |
| 2507 | |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 2508 | if ((fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_FCL || |
Willy Tarreau | 4e21ff9 | 2014-09-30 18:44:22 +0200 | [diff] [blame] | 2509 | (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_FCL) |
| 2510 | tmp = TX_CON_WANT_CLO; |
| 2511 | |
| 2512 | if ((txn->flags & TX_CON_WANT_MSK) < tmp) |
| 2513 | txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | tmp; |
| 2514 | |
| 2515 | if (!(txn->flags & TX_HDR_CONN_PRS) && |
| 2516 | (txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) { |
| 2517 | /* parse the Connection header and possibly clean it */ |
| 2518 | int to_del = 0; |
| 2519 | if ((msg->flags & HTTP_MSGF_VER_11) || |
| 2520 | ((txn->flags & TX_CON_WANT_MSK) >= TX_CON_WANT_SCL && |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 2521 | !((fe->options2|s->be->options2) & PR_O2_FAKE_KA))) |
Willy Tarreau | 4e21ff9 | 2014-09-30 18:44:22 +0200 | [diff] [blame] | 2522 | to_del |= 2; /* remove "keep-alive" */ |
| 2523 | if (!(msg->flags & HTTP_MSGF_VER_11)) |
| 2524 | to_del |= 1; /* remove "close" */ |
| 2525 | http_parse_connection_header(txn, msg, to_del); |
| 2526 | } |
| 2527 | |
| 2528 | /* check if client or config asks for explicit close in KAL/SCL */ |
| 2529 | if (((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL || |
| 2530 | (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) && |
| 2531 | ((txn->flags & TX_HDR_CONN_CLO) || /* "connection: close" */ |
| 2532 | (!(msg->flags & HTTP_MSGF_VER_11) && !(txn->flags & TX_HDR_CONN_KAL)) || /* no "connection: k-a" in 1.0 */ |
| 2533 | !(msg->flags & HTTP_MSGF_XFER_LEN) || /* no length known => close */ |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 2534 | fe->state == PR_STSTOPPED)) /* frontend is stopping */ |
Willy Tarreau | 4e21ff9 | 2014-09-30 18:44:22 +0200 | [diff] [blame] | 2535 | txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO; |
| 2536 | } |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 2537 | |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 2538 | /* This stream analyser waits for a complete HTTP request. It returns 1 if the |
| 2539 | * processing can continue on next analysers, or zero if it either needs more |
| 2540 | * data or wants to immediately abort the request (eg: timeout, error, ...). It |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 2541 | * 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] | 2542 | * when it has nothing left to do, and may remove any analyser when it wants to |
| 2543 | * abort. |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2544 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 2545 | 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] | 2546 | { |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2547 | /* |
| 2548 | * We will parse the partial (or complete) lines. |
| 2549 | * We will check the request syntax, and also join multi-line |
| 2550 | * headers. An index of all the lines will be elaborated while |
| 2551 | * parsing. |
| 2552 | * |
| 2553 | * For the parsing, we use a 28 states FSM. |
| 2554 | * |
| 2555 | * Here is the information we currently have : |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2556 | * req->buf->p = beginning of request |
| 2557 | * req->buf->p + msg->eoh = end of processed headers / start of current one |
| 2558 | * req->buf->p + req->buf->i = end of input data |
Willy Tarreau | 2692736 | 2012-05-18 23:22:52 +0200 | [diff] [blame] | 2559 | * msg->eol = end of current header or line (LF or CRLF) |
| 2560 | * msg->next = first non-visited byte |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 2561 | * |
| 2562 | * 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] | 2563 | * we will set a few fields (txn->meth, sn->flags/SF_REDIRECTABLE). |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 2564 | * We also check for monitor-uri, logging, HTTP/0.9 to 1.0 conversion, and |
| 2565 | * finally headers capture. |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2566 | */ |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 2567 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2568 | int cur_idx; |
Willy Tarreau | fb0afa7 | 2015-04-03 14:46:27 +0200 | [diff] [blame] | 2569 | struct session *sess = s->sess; |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 2570 | struct http_txn *txn = s->txn; |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2571 | struct http_msg *msg = &txn->req; |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 2572 | struct hdr_ctx ctx; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 2573 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 2574 | 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] | 2575 | now_ms, __FUNCTION__, |
| 2576 | s, |
| 2577 | req, |
| 2578 | req->rex, req->wex, |
| 2579 | req->flags, |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2580 | req->buf->i, |
Willy Tarreau | 6bf1736 | 2009-02-24 10:48:35 +0100 | [diff] [blame] | 2581 | req->analysers); |
| 2582 | |
Willy Tarreau | 52a0c60 | 2009-08-16 22:45:38 +0200 | [diff] [blame] | 2583 | /* we're speaking HTTP here, so let's speak HTTP to the client */ |
| 2584 | s->srv_error = http_return_srv_error; |
| 2585 | |
Willy Tarreau | 83e3af0 | 2009-12-28 17:39:57 +0100 | [diff] [blame] | 2586 | /* There's a protected area at the end of the buffer for rewriting |
| 2587 | * purposes. We don't want to start to parse the request if the |
| 2588 | * protected area is affected, because we may have to move processed |
| 2589 | * data later, which is much more complicated. |
| 2590 | */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2591 | if (buffer_not_empty(req->buf) && msg->msg_state < HTTP_MSG_ERROR) { |
Willy Tarreau | 379357a | 2013-06-08 12:55:46 +0200 | [diff] [blame] | 2592 | if (txn->flags & TX_NOT_FIRST) { |
Willy Tarreau | ba0902e | 2015-01-13 14:39:16 +0100 | [diff] [blame] | 2593 | if (unlikely(!channel_is_rewritable(req))) { |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 2594 | 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] | 2595 | goto failed_keep_alive; |
Willy Tarreau | 2ab6eb1 | 2010-01-02 22:04:45 +0100 | [diff] [blame] | 2596 | /* 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] | 2597 | channel_dont_connect(req); |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 2598 | req->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */ |
Willy Tarreau | d7ad9f5 | 2013-12-31 17:26:25 +0100 | [diff] [blame] | 2599 | req->flags |= CF_WAKE_WRITE; |
Willy Tarreau | 2ab6eb1 | 2010-01-02 22:04:45 +0100 | [diff] [blame] | 2600 | return 0; |
| 2601 | } |
Willy Tarreau | 379357a | 2013-06-08 12:55:46 +0200 | [diff] [blame] | 2602 | if (unlikely(bi_end(req->buf) < b_ptr(req->buf, msg->next) || |
| 2603 | bi_end(req->buf) > req->buf->data + req->buf->size - global.tune.maxrewrite)) |
| 2604 | buffer_slow_realign(req->buf); |
Willy Tarreau | 83e3af0 | 2009-12-28 17:39:57 +0100 | [diff] [blame] | 2605 | } |
| 2606 | |
Willy Tarreau | 065e833 | 2010-01-08 00:30:20 +0100 | [diff] [blame] | 2607 | /* Note that we have the same problem with the response ; we |
| 2608 | * may want to send a redirect, error or anything which requires |
| 2609 | * some spare space. So we'll ensure that we have at least |
| 2610 | * maxrewrite bytes available in the response buffer before |
| 2611 | * processing that one. This will only affect pipelined |
| 2612 | * keep-alive requests. |
| 2613 | */ |
| 2614 | if ((txn->flags & TX_NOT_FIRST) && |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 2615 | unlikely(!channel_is_rewritable(&s->res) || |
| 2616 | bi_end(s->res.buf) < b_ptr(s->res.buf, txn->rsp.next) || |
| 2617 | bi_end(s->res.buf) > s->res.buf->data + s->res.buf->size - global.tune.maxrewrite)) { |
| 2618 | if (s->res.buf->o) { |
| 2619 | if (s->res.flags & (CF_SHUTW|CF_SHUTW_NOW|CF_WRITE_ERROR|CF_WRITE_TIMEOUT)) |
Willy Tarreau | 6464841 | 2010-03-05 10:41:54 +0100 | [diff] [blame] | 2620 | goto failed_keep_alive; |
Willy Tarreau | 065e833 | 2010-01-08 00:30:20 +0100 | [diff] [blame] | 2621 | /* don't let a connection request be initiated */ |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 2622 | channel_dont_connect(req); |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 2623 | s->res.flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */ |
| 2624 | s->res.flags |= CF_WAKE_WRITE; |
| 2625 | s->res.analysers |= an_bit; /* wake us up once it changes */ |
Willy Tarreau | 065e833 | 2010-01-08 00:30:20 +0100 | [diff] [blame] | 2626 | return 0; |
| 2627 | } |
| 2628 | } |
| 2629 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2630 | if (likely(msg->next < req->buf->i)) /* some unparsed data are available */ |
Willy Tarreau | a560c21 | 2012-03-09 13:50:57 +0100 | [diff] [blame] | 2631 | http_msg_analyzer(msg, &txn->hdr_idx); |
Willy Tarreau | 83e3af0 | 2009-12-28 17:39:57 +0100 | [diff] [blame] | 2632 | } |
| 2633 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2634 | /* 1: we might have to print this header in debug mode */ |
| 2635 | if (unlikely((global.mode & MODE_DEBUG) && |
| 2636 | (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) && |
Willy Tarreau | 7d59e90 | 2014-10-21 19:36:09 +0200 | [diff] [blame] | 2637 | msg->msg_state >= HTTP_MSG_BODY)) { |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2638 | char *eol, *sol; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2639 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2640 | sol = req->buf->p; |
Willy Tarreau | e92693a | 2012-09-24 21:13:39 +0200 | [diff] [blame] | 2641 | /* this is a bit complex : in case of error on the request line, |
| 2642 | * we know that rq.l is still zero, so we display only the part |
| 2643 | * up to the end of the line (truncated by debug_hdr). |
| 2644 | */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2645 | 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] | 2646 | debug_hdr("clireq", s, sol, eol); |
Willy Tarreau | 45e73e3 | 2006-12-17 00:05:15 +0100 | [diff] [blame] | 2647 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2648 | sol += hdr_idx_first_pos(&txn->hdr_idx); |
| 2649 | cur_idx = hdr_idx_first_idx(&txn->hdr_idx); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2650 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2651 | while (cur_idx) { |
| 2652 | eol = sol + txn->hdr_idx.v[cur_idx].len; |
| 2653 | debug_hdr("clihdr", s, sol, eol); |
| 2654 | sol = eol + txn->hdr_idx.v[cur_idx].cr + 1; |
| 2655 | cur_idx = txn->hdr_idx.v[cur_idx].next; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2656 | } |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2657 | } |
| 2658 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2659 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2660 | /* |
| 2661 | * Now we quickly check if we have found a full valid request. |
| 2662 | * If not so, we check the FD and buffer states before leaving. |
| 2663 | * A full request is indicated by the fact that we have seen |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 2664 | * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid |
Willy Tarreau | d3c343f | 2010-01-16 10:26:19 +0100 | [diff] [blame] | 2665 | * requests are checked first. When waiting for a second request |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 2666 | * on a keep-alive stream, if we encounter and error, close, t/o, |
| 2667 | * 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] | 2668 | * 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] | 2669 | * process_stream() as a frontend error. |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 2670 | * Last, we may increase some tracked counters' http request errors on |
| 2671 | * the cases that are deliberately the client's fault. For instance, |
| 2672 | * a timeout or connection reset is not counted as an error. However |
| 2673 | * a bad request is. |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2674 | */ |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2675 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 2676 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2677 | /* |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2678 | * First, let's catch bad requests. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2679 | */ |
Willy Tarreau | 3e1b6d1 | 2010-03-04 23:02:38 +0100 | [diff] [blame] | 2680 | if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) { |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 2681 | stream_inc_http_req_ctr(s); |
| 2682 | stream_inc_http_err_ctr(s); |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 2683 | proxy_inc_fe_req_ctr(sess->fe); |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2684 | goto return_bad_req; |
Willy Tarreau | 3e1b6d1 | 2010-03-04 23:02:38 +0100 | [diff] [blame] | 2685 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2686 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2687 | /* 1: Since we are in header mode, if there's no space |
| 2688 | * left for headers, we won't be able to free more |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 2689 | * later, so the stream will never terminate. We |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2690 | * must terminate it now. |
| 2691 | */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2692 | if (unlikely(buffer_full(req->buf, global.tune.maxrewrite))) { |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2693 | /* FIXME: check if URI is set and return Status |
| 2694 | * 414 Request URI too long instead. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2695 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 2696 | stream_inc_http_req_ctr(s); |
| 2697 | stream_inc_http_err_ctr(s); |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 2698 | proxy_inc_fe_req_ctr(sess->fe); |
Willy Tarreau | fec4d89 | 2011-09-02 20:04:57 +0200 | [diff] [blame] | 2699 | if (msg->err_pos < 0) |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2700 | msg->err_pos = req->buf->i; |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2701 | goto return_bad_req; |
| 2702 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2703 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2704 | /* 2: have we encountered a read error ? */ |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 2705 | else if (req->flags & CF_READ_ERROR) { |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 2706 | if (!(s->flags & SF_ERR_MASK)) |
| 2707 | s->flags |= SF_ERR_CLICL; |
Willy Tarreau | d3c343f | 2010-01-16 10:26:19 +0100 | [diff] [blame] | 2708 | |
Willy Tarreau | fcffa69 | 2010-01-10 14:21:19 +0100 | [diff] [blame] | 2709 | if (txn->flags & TX_WAIT_NEXT_RQ) |
Willy Tarreau | b608feb | 2010-01-02 22:47:18 +0100 | [diff] [blame] | 2710 | goto failed_keep_alive; |
| 2711 | |
Willy Tarreau | 0f228a0 | 2015-05-01 15:37:53 +0200 | [diff] [blame] | 2712 | if (sess->fe->options & PR_O_IGNORE_PRB) |
| 2713 | goto failed_keep_alive; |
| 2714 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2715 | /* we cannot return any message on error */ |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 2716 | if (msg->err_pos >= 0) { |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 2717 | http_capture_bad_message(&sess->fe->invalid_req, s, msg, msg->msg_state, sess->fe); |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 2718 | stream_inc_http_err_ctr(s); |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 2719 | } |
| 2720 | |
Willy Tarreau | dc979f2 | 2012-12-04 10:39:01 +0100 | [diff] [blame] | 2721 | txn->status = 400; |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 2722 | stream_int_retnclose(&s->si[0], NULL); |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2723 | msg->msg_state = HTTP_MSG_ERROR; |
| 2724 | req->analysers = 0; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 2725 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 2726 | stream_inc_http_req_ctr(s); |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 2727 | proxy_inc_fe_req_ctr(sess->fe); |
| 2728 | sess->fe->fe_counters.failed_req++; |
Willy Tarreau | fb0afa7 | 2015-04-03 14:46:27 +0200 | [diff] [blame] | 2729 | if (sess->listener->counters) |
| 2730 | sess->listener->counters->failed_req++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 2731 | |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 2732 | if (!(s->flags & SF_FINST_MASK)) |
| 2733 | s->flags |= SF_FINST_R; |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2734 | return 0; |
| 2735 | } |
Willy Tarreau | f9839bd | 2008-08-27 23:57:16 +0200 | [diff] [blame] | 2736 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2737 | /* 3: has the read timeout expired ? */ |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 2738 | 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] | 2739 | if (!(s->flags & SF_ERR_MASK)) |
| 2740 | s->flags |= SF_ERR_CLITO; |
Willy Tarreau | d3c343f | 2010-01-16 10:26:19 +0100 | [diff] [blame] | 2741 | |
Willy Tarreau | fcffa69 | 2010-01-10 14:21:19 +0100 | [diff] [blame] | 2742 | if (txn->flags & TX_WAIT_NEXT_RQ) |
Willy Tarreau | b608feb | 2010-01-02 22:47:18 +0100 | [diff] [blame] | 2743 | goto failed_keep_alive; |
| 2744 | |
Willy Tarreau | 0f228a0 | 2015-05-01 15:37:53 +0200 | [diff] [blame] | 2745 | if (sess->fe->options & PR_O_IGNORE_PRB) |
| 2746 | goto failed_keep_alive; |
| 2747 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2748 | /* read timeout : give up with an error message. */ |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 2749 | if (msg->err_pos >= 0) { |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 2750 | http_capture_bad_message(&sess->fe->invalid_req, s, msg, msg->msg_state, sess->fe); |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 2751 | stream_inc_http_err_ctr(s); |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 2752 | } |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2753 | txn->status = 408; |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 2754 | stream_int_retnclose(&s->si[0], http_error_message(s, HTTP_ERR_408)); |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2755 | msg->msg_state = HTTP_MSG_ERROR; |
| 2756 | req->analysers = 0; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 2757 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 2758 | stream_inc_http_req_ctr(s); |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 2759 | proxy_inc_fe_req_ctr(sess->fe); |
| 2760 | sess->fe->fe_counters.failed_req++; |
Willy Tarreau | fb0afa7 | 2015-04-03 14:46:27 +0200 | [diff] [blame] | 2761 | if (sess->listener->counters) |
| 2762 | sess->listener->counters->failed_req++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 2763 | |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 2764 | if (!(s->flags & SF_FINST_MASK)) |
| 2765 | s->flags |= SF_FINST_R; |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2766 | return 0; |
| 2767 | } |
Willy Tarreau | c65a3ba | 2008-08-11 23:42:50 +0200 | [diff] [blame] | 2768 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2769 | /* 4: have we encountered a close ? */ |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 2770 | else if (req->flags & CF_SHUTR) { |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 2771 | if (!(s->flags & SF_ERR_MASK)) |
| 2772 | s->flags |= SF_ERR_CLICL; |
Willy Tarreau | d3c343f | 2010-01-16 10:26:19 +0100 | [diff] [blame] | 2773 | |
Willy Tarreau | fcffa69 | 2010-01-10 14:21:19 +0100 | [diff] [blame] | 2774 | if (txn->flags & TX_WAIT_NEXT_RQ) |
Willy Tarreau | b608feb | 2010-01-02 22:47:18 +0100 | [diff] [blame] | 2775 | goto failed_keep_alive; |
| 2776 | |
Willy Tarreau | 0f228a0 | 2015-05-01 15:37:53 +0200 | [diff] [blame] | 2777 | if (sess->fe->options & PR_O_IGNORE_PRB) |
| 2778 | goto failed_keep_alive; |
| 2779 | |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 2780 | if (msg->err_pos >= 0) |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 2781 | http_capture_bad_message(&sess->fe->invalid_req, s, msg, msg->msg_state, sess->fe); |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2782 | txn->status = 400; |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 2783 | stream_int_retnclose(&s->si[0], http_error_message(s, HTTP_ERR_400)); |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2784 | msg->msg_state = HTTP_MSG_ERROR; |
| 2785 | req->analysers = 0; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 2786 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 2787 | stream_inc_http_err_ctr(s); |
| 2788 | stream_inc_http_req_ctr(s); |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 2789 | proxy_inc_fe_req_ctr(sess->fe); |
| 2790 | sess->fe->fe_counters.failed_req++; |
Willy Tarreau | fb0afa7 | 2015-04-03 14:46:27 +0200 | [diff] [blame] | 2791 | if (sess->listener->counters) |
| 2792 | sess->listener->counters->failed_req++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 2793 | |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 2794 | if (!(s->flags & SF_FINST_MASK)) |
| 2795 | s->flags |= SF_FINST_R; |
Willy Tarreau | dafde43 | 2008-08-17 01:00:46 +0200 | [diff] [blame] | 2796 | return 0; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2797 | } |
| 2798 | |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 2799 | channel_dont_connect(req); |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 2800 | req->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */ |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 2801 | s->res.flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */ |
Willy Tarreau | 5e20552 | 2011-12-17 16:34:27 +0100 | [diff] [blame] | 2802 | #ifdef TCP_QUICKACK |
Willy Tarreau | 9ad7bd4 | 2015-04-03 19:19:59 +0200 | [diff] [blame] | 2803 | if (sess->listener->options & LI_O_NOQUICKACK && req->buf->i && |
| 2804 | objt_conn(sess->origin) && conn_ctrl_ready(__objt_conn(sess->origin))) { |
Willy Tarreau | 5e20552 | 2011-12-17 16:34:27 +0100 | [diff] [blame] | 2805 | /* We need more data, we have to re-enable quick-ack in case we |
| 2806 | * previously disabled it, otherwise we might cause the client |
| 2807 | * to delay next data. |
| 2808 | */ |
Willy Tarreau | 9ad7bd4 | 2015-04-03 19:19:59 +0200 | [diff] [blame] | 2809 | 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] | 2810 | } |
| 2811 | #endif |
Willy Tarreau | 1b194fe | 2009-03-21 21:10:04 +0100 | [diff] [blame] | 2812 | |
Willy Tarreau | fcffa69 | 2010-01-10 14:21:19 +0100 | [diff] [blame] | 2813 | if ((msg->msg_state != HTTP_MSG_RQBEFORE) && (txn->flags & TX_WAIT_NEXT_RQ)) { |
| 2814 | /* If the client starts to talk, let's fall back to |
| 2815 | * request timeout processing. |
| 2816 | */ |
| 2817 | txn->flags &= ~TX_WAIT_NEXT_RQ; |
Willy Tarreau | b16a574 | 2010-01-10 14:46:16 +0100 | [diff] [blame] | 2818 | req->analyse_exp = TICK_ETERNITY; |
Willy Tarreau | fcffa69 | 2010-01-10 14:21:19 +0100 | [diff] [blame] | 2819 | } |
| 2820 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2821 | /* just set the request timeout once at the beginning of the request */ |
Willy Tarreau | b16a574 | 2010-01-10 14:46:16 +0100 | [diff] [blame] | 2822 | if (!tick_isset(req->analyse_exp)) { |
| 2823 | if ((msg->msg_state == HTTP_MSG_RQBEFORE) && |
| 2824 | (txn->flags & TX_WAIT_NEXT_RQ) && |
| 2825 | tick_isset(s->be->timeout.httpka)) |
| 2826 | req->analyse_exp = tick_add(now_ms, s->be->timeout.httpka); |
| 2827 | else |
| 2828 | req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.httpreq); |
| 2829 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2830 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2831 | /* we're not ready yet */ |
| 2832 | return 0; |
Willy Tarreau | b608feb | 2010-01-02 22:47:18 +0100 | [diff] [blame] | 2833 | |
| 2834 | failed_keep_alive: |
| 2835 | /* Here we process low-level errors for keep-alive requests. In |
| 2836 | * short, if the request is not the first one and it experiences |
| 2837 | * a timeout, read error or shutdown, we just silently close so |
| 2838 | * that the client can try again. |
| 2839 | */ |
| 2840 | txn->status = 0; |
| 2841 | msg->msg_state = HTTP_MSG_RQBEFORE; |
| 2842 | req->analysers = 0; |
| 2843 | s->logs.logwait = 0; |
Willy Tarreau | abcd514 | 2013-06-11 17:18:02 +0200 | [diff] [blame] | 2844 | s->logs.level = 0; |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 2845 | s->res.flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */ |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 2846 | stream_int_retnclose(&s->si[0], NULL); |
Willy Tarreau | b608feb | 2010-01-02 22:47:18 +0100 | [diff] [blame] | 2847 | return 0; |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2848 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2849 | |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 2850 | /* OK now we have a complete HTTP request with indexed headers. Let's |
| 2851 | * complete the request parsing by setting a few fields we will need |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2852 | * 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] | 2853 | * 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] | 2854 | * 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] | 2855 | * byte after the last LF. msg->sov points to the first byte of data. |
| 2856 | * msg->eol cannot be trusted because it may have been left uninitialized |
| 2857 | * (for instance in the absence of headers). |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 2858 | */ |
Willy Tarreau | 9cdde23 | 2007-05-02 20:58:19 +0200 | [diff] [blame] | 2859 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 2860 | stream_inc_http_req_ctr(s); |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 2861 | 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] | 2862 | |
Willy Tarreau | b16a574 | 2010-01-10 14:46:16 +0100 | [diff] [blame] | 2863 | if (txn->flags & TX_WAIT_NEXT_RQ) { |
| 2864 | /* kill the pending keep-alive timeout */ |
| 2865 | txn->flags &= ~TX_WAIT_NEXT_RQ; |
| 2866 | req->analyse_exp = TICK_ETERNITY; |
| 2867 | } |
| 2868 | |
| 2869 | |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 2870 | /* Maybe we found in invalid header name while we were configured not |
| 2871 | * to block on that, so we have to capture it now. |
| 2872 | */ |
| 2873 | if (unlikely(msg->err_pos >= 0)) |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 2874 | http_capture_bad_message(&sess->fe->invalid_req, s, msg, msg->msg_state, sess->fe); |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 2875 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2876 | /* |
| 2877 | * 1: identify the method |
| 2878 | */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2879 | 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] | 2880 | |
| 2881 | /* we can make use of server redirect on GET and HEAD */ |
| 2882 | if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD) |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 2883 | s->flags |= SF_REDIRECTABLE; |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 2884 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2885 | /* |
| 2886 | * 2: check if the URI matches the monitor_uri. |
| 2887 | * We have to do this for every request which gets in, because |
| 2888 | * the monitor-uri is defined by the frontend. |
| 2889 | */ |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 2890 | if (unlikely((sess->fe->monitor_uri_len != 0) && |
| 2891 | (sess->fe->monitor_uri_len == msg->sl.rq.u_l) && |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2892 | !memcmp(req->buf->p + msg->sl.rq.u, |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 2893 | sess->fe->monitor_uri, |
| 2894 | sess->fe->monitor_uri_len))) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2895 | /* |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2896 | * We have found the monitor URI |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2897 | */ |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2898 | struct acl_cond *cond; |
Willy Tarreau | b80c230 | 2007-11-30 20:51:32 +0100 | [diff] [blame] | 2899 | |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 2900 | s->flags |= SF_MONITOR; |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 2901 | sess->fe->fe_counters.intercepted_req++; |
Willy Tarreau | b80c230 | 2007-11-30 20:51:32 +0100 | [diff] [blame] | 2902 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2903 | /* Check if we want to fail this monitor request or not */ |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 2904 | list_for_each_entry(cond, &sess->fe->mon_fail_cond, list) { |
Willy Tarreau | 192252e | 2015-04-04 01:47:55 +0200 | [diff] [blame] | 2905 | 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] | 2906 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2907 | ret = acl_pass(ret); |
| 2908 | if (cond->pol == ACL_COND_UNLESS) |
| 2909 | ret = !ret; |
Willy Tarreau | b80c230 | 2007-11-30 20:51:32 +0100 | [diff] [blame] | 2910 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2911 | if (ret) { |
| 2912 | /* we fail this request, let's return 503 service unavail */ |
| 2913 | txn->status = 503; |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 2914 | stream_int_retnclose(&s->si[0], http_error_message(s, HTTP_ERR_503)); |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 2915 | if (!(s->flags & SF_ERR_MASK)) |
| 2916 | 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] | 2917 | goto return_prx_cond; |
Willy Tarreau | b80c230 | 2007-11-30 20:51:32 +0100 | [diff] [blame] | 2918 | } |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2919 | } |
Willy Tarreau | a5555ec | 2008-11-30 19:02:32 +0100 | [diff] [blame] | 2920 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2921 | /* nothing to fail, let's reply normaly */ |
| 2922 | txn->status = 200; |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 2923 | stream_int_retnclose(&s->si[0], http_error_message(s, HTTP_ERR_200)); |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 2924 | if (!(s->flags & SF_ERR_MASK)) |
| 2925 | 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] | 2926 | goto return_prx_cond; |
| 2927 | } |
| 2928 | |
| 2929 | /* |
| 2930 | * 3: Maybe we have to copy the original REQURI for the logs ? |
| 2931 | * Note: we cannot log anymore if the request has been |
| 2932 | * classified as invalid. |
| 2933 | */ |
| 2934 | if (unlikely(s->logs.logwait & LW_REQ)) { |
| 2935 | /* we have a complete HTTP request that we must log */ |
| 2936 | if ((txn->uri = pool_alloc2(pool2_requri)) != NULL) { |
| 2937 | int urilen = msg->sl.rq.l; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2938 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2939 | if (urilen >= REQURI_LEN) |
| 2940 | urilen = REQURI_LEN - 1; |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2941 | memcpy(txn->uri, req->buf->p, urilen); |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2942 | txn->uri[urilen] = 0; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2943 | |
Willy Tarreau | d79a3b2 | 2012-12-28 09:40:16 +0100 | [diff] [blame] | 2944 | if (!(s->logs.logwait &= ~(LW_REQ|LW_INIT))) |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2945 | s->do_log(s); |
| 2946 | } else { |
| 2947 | Alert("HTTP logging : out of memory.\n"); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2948 | } |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2949 | } |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 2950 | |
Willy Tarreau | 91852eb | 2015-05-01 13:26:00 +0200 | [diff] [blame] | 2951 | /* RFC7230#2.6 has enforced the format of the HTTP version string to be |
| 2952 | * exactly one digit "." one digit. This check may be disabled using |
| 2953 | * option accept-invalid-http-request. |
| 2954 | */ |
| 2955 | if (!(sess->fe->options2 & PR_O2_REQBUG_OK)) { |
| 2956 | if (msg->sl.rq.v_l != 8) { |
| 2957 | msg->err_pos = msg->sl.rq.v; |
| 2958 | goto return_bad_req; |
| 2959 | } |
| 2960 | |
| 2961 | if (req->buf->p[msg->sl.rq.v + 4] != '/' || |
| 2962 | !isdigit((unsigned char)req->buf->p[msg->sl.rq.v + 5]) || |
| 2963 | req->buf->p[msg->sl.rq.v + 6] != '.' || |
| 2964 | !isdigit((unsigned char)req->buf->p[msg->sl.rq.v + 7])) { |
| 2965 | msg->err_pos = msg->sl.rq.v + 4; |
| 2966 | goto return_bad_req; |
| 2967 | } |
| 2968 | } |
Willy Tarreau | 1331766 | 2015-05-01 13:47:08 +0200 | [diff] [blame] | 2969 | else { |
| 2970 | /* 4. We may have to convert HTTP/0.9 requests to HTTP/1.0 */ |
| 2971 | if (unlikely(msg->sl.rq.v_l == 0) && !http_upgrade_v09_to_v10(txn)) |
| 2972 | goto return_bad_req; |
| 2973 | } |
Willy Tarreau | 91852eb | 2015-05-01 13:26:00 +0200 | [diff] [blame] | 2974 | |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 2975 | /* ... and check if the request is HTTP/1.1 or above */ |
| 2976 | if ((msg->sl.rq.v_l == 8) && |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2977 | ((req->buf->p[msg->sl.rq.v + 5] > '1') || |
| 2978 | ((req->buf->p[msg->sl.rq.v + 5] == '1') && |
| 2979 | (req->buf->p[msg->sl.rq.v + 7] >= '1')))) |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 2980 | msg->flags |= HTTP_MSGF_VER_11; |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 2981 | |
| 2982 | /* "connection" has not been parsed yet */ |
Willy Tarreau | 50fc777 | 2012-11-11 22:19:57 +0100 | [diff] [blame] | 2983 | 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] | 2984 | |
Willy Tarreau | 88d349d | 2010-01-25 12:15:43 +0100 | [diff] [blame] | 2985 | /* if the frontend has "option http-use-proxy-header", we'll check if |
| 2986 | * we have what looks like a proxied connection instead of a connection, |
| 2987 | * and in this case set the TX_USE_PX_CONN flag to use Proxy-connection. |
| 2988 | * Note that this is *not* RFC-compliant, however browsers and proxies |
| 2989 | * happen to do that despite being non-standard :-( |
| 2990 | * We consider that a request not beginning with either '/' or '*' is |
| 2991 | * a proxied connection, which covers both "scheme://location" and |
| 2992 | * CONNECT ip:port. |
| 2993 | */ |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 2994 | if ((sess->fe->options2 & PR_O2_USE_PXHDR) && |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2995 | 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] | 2996 | txn->flags |= TX_USE_PX_CONN; |
| 2997 | |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 2998 | /* transfer length unknown*/ |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 2999 | msg->flags &= ~HTTP_MSGF_XFER_LEN; |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 3000 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3001 | /* 5: we may need to capture headers */ |
Willy Tarreau | cb7dd01 | 2015-04-03 22:16:32 +0200 | [diff] [blame] | 3002 | if (unlikely((s->logs.logwait & LW_REQHDR) && s->req_cap)) |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 3003 | capture_headers(req->buf->p, &txn->hdr_idx, |
Willy Tarreau | cb7dd01 | 2015-04-03 22:16:32 +0200 | [diff] [blame] | 3004 | s->req_cap, sess->fe->req_cap); |
Willy Tarreau | 1138281 | 2008-07-09 16:18:21 +0200 | [diff] [blame] | 3005 | |
Willy Tarreau | 557f199 | 2015-05-01 10:05:17 +0200 | [diff] [blame] | 3006 | /* 6: determine the transfer-length according to RFC2616 #4.4, updated |
| 3007 | * by RFC7230#3.3.3 : |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 3008 | * |
Willy Tarreau | 557f199 | 2015-05-01 10:05:17 +0200 | [diff] [blame] | 3009 | * The length of a message body is determined by one of the following |
| 3010 | * (in order of precedence): |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 3011 | * |
Willy Tarreau | 557f199 | 2015-05-01 10:05:17 +0200 | [diff] [blame] | 3012 | * 1. Any response to a HEAD request and any response with a 1xx |
| 3013 | * (Informational), 204 (No Content), or 304 (Not Modified) status |
| 3014 | * code is always terminated by the first empty line after the |
| 3015 | * header fields, regardless of the header fields present in the |
| 3016 | * message, and thus cannot contain a message body. |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 3017 | * |
Willy Tarreau | 557f199 | 2015-05-01 10:05:17 +0200 | [diff] [blame] | 3018 | * 2. Any 2xx (Successful) response to a CONNECT request implies that |
| 3019 | * the connection will become a tunnel immediately after the empty |
| 3020 | * line that concludes the header fields. A client MUST ignore any |
| 3021 | * Content-Length or Transfer-Encoding header fields received in |
| 3022 | * such a message. |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 3023 | * |
Willy Tarreau | 557f199 | 2015-05-01 10:05:17 +0200 | [diff] [blame] | 3024 | * 3. If a Transfer-Encoding header field is present and the chunked |
| 3025 | * transfer coding (Section 4.1) is the final encoding, the message |
| 3026 | * body length is determined by reading and decoding the chunked |
| 3027 | * data until the transfer coding indicates the data is complete. |
| 3028 | * |
| 3029 | * If a Transfer-Encoding header field is present in a response and |
| 3030 | * the chunked transfer coding is not the final encoding, the |
| 3031 | * message body length is determined by reading the connection until |
| 3032 | * it is closed by the server. If a Transfer-Encoding header field |
| 3033 | * is present in a request and the chunked transfer coding is not |
| 3034 | * the final encoding, the message body length cannot be determined |
| 3035 | * reliably; the server MUST respond with the 400 (Bad Request) |
| 3036 | * status code and then close the connection. |
| 3037 | * |
| 3038 | * If a message is received with both a Transfer-Encoding and a |
| 3039 | * Content-Length header field, the Transfer-Encoding overrides the |
| 3040 | * Content-Length. Such a message might indicate an attempt to |
| 3041 | * perform request smuggling (Section 9.5) or response splitting |
| 3042 | * (Section 9.4) and ought to be handled as an error. A sender MUST |
| 3043 | * remove the received Content-Length field prior to forwarding such |
| 3044 | * a message downstream. |
| 3045 | * |
| 3046 | * 4. If a message is received without Transfer-Encoding and with |
| 3047 | * either multiple Content-Length header fields having differing |
| 3048 | * field-values or a single Content-Length header field having an |
| 3049 | * invalid value, then the message framing is invalid and the |
| 3050 | * recipient MUST treat it as an unrecoverable error. If this is a |
| 3051 | * request message, the server MUST respond with a 400 (Bad Request) |
| 3052 | * status code and then close the connection. If this is a response |
| 3053 | * message received by a proxy, the proxy MUST close the connection |
| 3054 | * to the server, discard the received response, and send a 502 (Bad |
| 3055 | * Gateway) response to the client. If this is a response message |
| 3056 | * received by a user agent, the user agent MUST close the |
| 3057 | * connection to the server and discard the received response. |
| 3058 | * |
| 3059 | * 5. If a valid Content-Length header field is present without |
| 3060 | * Transfer-Encoding, its decimal value defines the expected message |
| 3061 | * body length in octets. If the sender closes the connection or |
| 3062 | * the recipient times out before the indicated number of octets are |
| 3063 | * received, the recipient MUST consider the message to be |
| 3064 | * incomplete and close the connection. |
| 3065 | * |
| 3066 | * 6. If this is a request message and none of the above are true, then |
| 3067 | * the message body length is zero (no message body is present). |
| 3068 | * |
| 3069 | * 7. Otherwise, this is a response message without a declared message |
| 3070 | * body length, so the message body length is determined by the |
| 3071 | * number of octets received prior to the server closing the |
| 3072 | * connection. |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 3073 | */ |
| 3074 | |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 3075 | ctx.idx = 0; |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 3076 | /* set TE_CHNK and XFER_LEN only if "chunked" is seen last */ |
Willy Tarreau | 4979d5c | 2015-05-01 10:06:30 +0200 | [diff] [blame] | 3077 | 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] | 3078 | if (ctx.vlen == 7 && strncasecmp(ctx.line + ctx.val, "chunked", 7) == 0) |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 3079 | msg->flags |= (HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN); |
| 3080 | else if (msg->flags & HTTP_MSGF_TE_CHNK) { |
Willy Tarreau | 34dfc60 | 2015-05-01 10:09:49 +0200 | [diff] [blame] | 3081 | /* chunked not last, return badreq */ |
| 3082 | goto return_bad_req; |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 3083 | } |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 3084 | } |
| 3085 | |
Willy Tarreau | 1c91391 | 2015-04-30 10:57:51 +0200 | [diff] [blame] | 3086 | /* Chunked requests must have their content-length removed */ |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 3087 | ctx.idx = 0; |
Willy Tarreau | 1c91391 | 2015-04-30 10:57:51 +0200 | [diff] [blame] | 3088 | if (msg->flags & HTTP_MSGF_TE_CHNK) { |
| 3089 | while (http_find_header2("Content-Length", 14, req->buf->p, &txn->hdr_idx, &ctx)) |
| 3090 | http_remove_header2(msg, &txn->hdr_idx, &ctx); |
| 3091 | } |
Willy Tarreau | 34dfc60 | 2015-05-01 10:09:49 +0200 | [diff] [blame] | 3092 | 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] | 3093 | signed long long cl; |
| 3094 | |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 3095 | if (!ctx.vlen) { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 3096 | msg->err_pos = ctx.line + ctx.val - req->buf->p; |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 3097 | goto return_bad_req; |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 3098 | } |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 3099 | |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 3100 | if (strl2llrc(ctx.line + ctx.val, ctx.vlen, &cl)) { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 3101 | msg->err_pos = ctx.line + ctx.val - req->buf->p; |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 3102 | goto return_bad_req; /* parse failure */ |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 3103 | } |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 3104 | |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 3105 | if (cl < 0) { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 3106 | msg->err_pos = ctx.line + ctx.val - req->buf->p; |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 3107 | goto return_bad_req; |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 3108 | } |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 3109 | |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 3110 | if ((msg->flags & HTTP_MSGF_CNT_LEN) && (msg->chunk_len != cl)) { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 3111 | msg->err_pos = ctx.line + ctx.val - req->buf->p; |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 3112 | goto return_bad_req; /* already specified, was different */ |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 3113 | } |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 3114 | |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 3115 | msg->flags |= HTTP_MSGF_CNT_LEN | HTTP_MSGF_XFER_LEN; |
Willy Tarreau | 124d991 | 2011-03-01 20:30:48 +0100 | [diff] [blame] | 3116 | msg->body_len = msg->chunk_len = cl; |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 3117 | } |
| 3118 | |
Willy Tarreau | 34dfc60 | 2015-05-01 10:09:49 +0200 | [diff] [blame] | 3119 | /* even bodyless requests have a known length */ |
| 3120 | msg->flags |= HTTP_MSGF_XFER_LEN; |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 3121 | |
Willy Tarreau | 179085c | 2014-04-28 16:48:56 +0200 | [diff] [blame] | 3122 | /* Until set to anything else, the connection mode is set as Keep-Alive. It will |
| 3123 | * only change if both the request and the config reference something else. |
| 3124 | * Option httpclose by itself sets tunnel mode where headers are mangled. |
| 3125 | * However, if another mode is set, it will affect it (eg: server-close/ |
| 3126 | * keep-alive + httpclose = close). Note that we avoid to redo the same work |
| 3127 | * if FE and BE have the same settings (common). The method consists in |
| 3128 | * checking if options changed between the two calls (implying that either |
| 3129 | * one is non-null, or one of them is non-null and we are there for the first |
| 3130 | * time. |
| 3131 | */ |
| 3132 | if (!(txn->flags & TX_HDR_CONN_PRS) || |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 3133 | ((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] | 3134 | http_adjust_conn_mode(s, txn, msg); |
Willy Tarreau | 179085c | 2014-04-28 16:48:56 +0200 | [diff] [blame] | 3135 | |
Willy Tarreau | 9fbe18e | 2015-05-01 22:42:08 +0200 | [diff] [blame] | 3136 | /* we may have to wait for the request's body */ |
| 3137 | if ((s->be->options & PR_O_WREQ_BODY) && |
| 3138 | (msg->body_len || (msg->flags & HTTP_MSGF_TE_CHNK))) |
| 3139 | req->analysers |= AN_REQ_HTTP_BODY; |
| 3140 | |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 3141 | /* end of job, return OK */ |
Willy Tarreau | 3a81629 | 2009-07-07 10:55:49 +0200 | [diff] [blame] | 3142 | req->analysers &= ~an_bit; |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 3143 | req->analyse_exp = TICK_ETERNITY; |
| 3144 | return 1; |
| 3145 | |
| 3146 | return_bad_req: |
| 3147 | /* We centralize bad requests processing here */ |
| 3148 | if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) { |
| 3149 | /* we detected a parsing error. We want to archive this request |
| 3150 | * in the dedicated proxy area for later troubleshooting. |
| 3151 | */ |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 3152 | http_capture_bad_message(&sess->fe->invalid_req, s, msg, msg->msg_state, sess->fe); |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 3153 | } |
| 3154 | |
| 3155 | txn->req.msg_state = HTTP_MSG_ERROR; |
| 3156 | txn->status = 400; |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 3157 | stream_int_retnclose(&s->si[0], http_error_message(s, HTTP_ERR_400)); |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 3158 | |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 3159 | sess->fe->fe_counters.failed_req++; |
Willy Tarreau | fb0afa7 | 2015-04-03 14:46:27 +0200 | [diff] [blame] | 3160 | if (sess->listener->counters) |
| 3161 | sess->listener->counters->failed_req++; |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 3162 | |
| 3163 | return_prx_cond: |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 3164 | if (!(s->flags & SF_ERR_MASK)) |
| 3165 | s->flags |= SF_ERR_PRXCOND; |
| 3166 | if (!(s->flags & SF_FINST_MASK)) |
| 3167 | s->flags |= SF_FINST_R; |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 3168 | |
| 3169 | req->analysers = 0; |
| 3170 | req->analyse_exp = TICK_ETERNITY; |
| 3171 | return 0; |
| 3172 | } |
| 3173 | |
Willy Tarreau | 4f8a83c | 2012-06-04 00:26:23 +0200 | [diff] [blame] | 3174 | |
Willy Tarreau | 347a35d | 2013-11-22 17:51:09 +0100 | [diff] [blame] | 3175 | /* This function prepares an applet to handle the stats. It can deal with the |
| 3176 | * "100-continue" expectation, check that admin rules are met for POST requests, |
| 3177 | * and program a response message if something was unexpected. It cannot fail |
| 3178 | * 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] | 3179 | * 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] | 3180 | * 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] | 3181 | * is expected to have already assigned an appctx to the stream. |
Willy Tarreau | 1facd6d | 2012-12-22 22:03:39 +0100 | [diff] [blame] | 3182 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 3183 | int http_handle_stats(struct stream *s, struct channel *req) |
Willy Tarreau | 1facd6d | 2012-12-22 22:03:39 +0100 | [diff] [blame] | 3184 | { |
| 3185 | struct stats_admin_rule *stats_admin_rule; |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 3186 | struct stream_interface *si = &s->si[1]; |
Willy Tarreau | 192252e | 2015-04-04 01:47:55 +0200 | [diff] [blame] | 3187 | struct session *sess = s->sess; |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 3188 | struct http_txn *txn = s->txn; |
Willy Tarreau | 1facd6d | 2012-12-22 22:03:39 +0100 | [diff] [blame] | 3189 | struct http_msg *msg = &txn->req; |
Willy Tarreau | 414e9bb | 2013-11-23 00:30:38 +0100 | [diff] [blame] | 3190 | struct uri_auth *uri_auth = s->be->uri_auth; |
| 3191 | const char *uri, *h, *lookup; |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 3192 | struct appctx *appctx; |
Willy Tarreau | 414e9bb | 2013-11-23 00:30:38 +0100 | [diff] [blame] | 3193 | |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 3194 | appctx = si_appctx(si); |
| 3195 | memset(&appctx->ctx.stats, 0, sizeof(appctx->ctx.stats)); |
| 3196 | appctx->st1 = appctx->st2 = 0; |
| 3197 | appctx->ctx.stats.st_code = STAT_STATUS_INIT; |
| 3198 | appctx->ctx.stats.flags |= STAT_FMT_HTML; /* assume HTML mode by default */ |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 3199 | 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] | 3200 | appctx->ctx.stats.flags |= STAT_CHUNKED; |
Willy Tarreau | 414e9bb | 2013-11-23 00:30:38 +0100 | [diff] [blame] | 3201 | |
| 3202 | uri = msg->chn->buf->p + msg->sl.rq.u; |
| 3203 | lookup = uri + uri_auth->uri_len; |
| 3204 | |
| 3205 | for (h = lookup; h <= uri + msg->sl.rq.u_l - 3; h++) { |
| 3206 | if (memcmp(h, ";up", 3) == 0) { |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 3207 | appctx->ctx.stats.flags |= STAT_HIDE_DOWN; |
Willy Tarreau | 414e9bb | 2013-11-23 00:30:38 +0100 | [diff] [blame] | 3208 | break; |
| 3209 | } |
| 3210 | } |
| 3211 | |
| 3212 | if (uri_auth->refresh) { |
| 3213 | for (h = lookup; h <= uri + msg->sl.rq.u_l - 10; h++) { |
| 3214 | if (memcmp(h, ";norefresh", 10) == 0) { |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 3215 | appctx->ctx.stats.flags |= STAT_NO_REFRESH; |
Willy Tarreau | 414e9bb | 2013-11-23 00:30:38 +0100 | [diff] [blame] | 3216 | break; |
| 3217 | } |
| 3218 | } |
| 3219 | } |
| 3220 | |
| 3221 | for (h = lookup; h <= uri + msg->sl.rq.u_l - 4; h++) { |
| 3222 | if (memcmp(h, ";csv", 4) == 0) { |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 3223 | appctx->ctx.stats.flags &= ~STAT_FMT_HTML; |
Willy Tarreau | 414e9bb | 2013-11-23 00:30:38 +0100 | [diff] [blame] | 3224 | break; |
| 3225 | } |
| 3226 | } |
| 3227 | |
| 3228 | for (h = lookup; h <= uri + msg->sl.rq.u_l - 8; h++) { |
| 3229 | if (memcmp(h, ";st=", 4) == 0) { |
| 3230 | int i; |
| 3231 | h += 4; |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 3232 | appctx->ctx.stats.st_code = STAT_STATUS_UNKN; |
Willy Tarreau | 414e9bb | 2013-11-23 00:30:38 +0100 | [diff] [blame] | 3233 | for (i = STAT_STATUS_INIT + 1; i < STAT_STATUS_SIZE; i++) { |
| 3234 | if (strncmp(stat_status_codes[i], h, 4) == 0) { |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 3235 | appctx->ctx.stats.st_code = i; |
Willy Tarreau | 414e9bb | 2013-11-23 00:30:38 +0100 | [diff] [blame] | 3236 | break; |
| 3237 | } |
| 3238 | } |
| 3239 | break; |
| 3240 | } |
| 3241 | } |
| 3242 | |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 3243 | appctx->ctx.stats.scope_str = 0; |
| 3244 | appctx->ctx.stats.scope_len = 0; |
Willy Tarreau | 414e9bb | 2013-11-23 00:30:38 +0100 | [diff] [blame] | 3245 | for (h = lookup; h <= uri + msg->sl.rq.u_l - 8; h++) { |
| 3246 | if (memcmp(h, STAT_SCOPE_INPUT_NAME "=", strlen(STAT_SCOPE_INPUT_NAME) + 1) == 0) { |
| 3247 | int itx = 0; |
| 3248 | const char *h2; |
| 3249 | char scope_txt[STAT_SCOPE_TXT_MAXLEN + 1]; |
| 3250 | const char *err; |
| 3251 | |
| 3252 | h += strlen(STAT_SCOPE_INPUT_NAME) + 1; |
| 3253 | h2 = h; |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 3254 | appctx->ctx.stats.scope_str = h2 - msg->chn->buf->p; |
Willy Tarreau | 414e9bb | 2013-11-23 00:30:38 +0100 | [diff] [blame] | 3255 | while (*h != ';' && *h != '\0' && *h != '&' && *h != ' ' && *h != '\n') { |
| 3256 | itx++; |
| 3257 | h++; |
| 3258 | } |
| 3259 | |
| 3260 | if (itx > STAT_SCOPE_TXT_MAXLEN) |
| 3261 | itx = STAT_SCOPE_TXT_MAXLEN; |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 3262 | appctx->ctx.stats.scope_len = itx; |
Willy Tarreau | 414e9bb | 2013-11-23 00:30:38 +0100 | [diff] [blame] | 3263 | |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 3264 | /* 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] | 3265 | memcpy(scope_txt, h2, itx); |
| 3266 | scope_txt[itx] = '\0'; |
| 3267 | err = invalid_char(scope_txt); |
| 3268 | if (err) { |
| 3269 | /* bad char in search text => clear scope */ |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 3270 | appctx->ctx.stats.scope_str = 0; |
| 3271 | appctx->ctx.stats.scope_len = 0; |
Willy Tarreau | 414e9bb | 2013-11-23 00:30:38 +0100 | [diff] [blame] | 3272 | } |
| 3273 | break; |
| 3274 | } |
| 3275 | } |
Willy Tarreau | 1facd6d | 2012-12-22 22:03:39 +0100 | [diff] [blame] | 3276 | |
| 3277 | /* now check whether we have some admin rules for this request */ |
Willy Tarreau | 414e9bb | 2013-11-23 00:30:38 +0100 | [diff] [blame] | 3278 | list_for_each_entry(stats_admin_rule, &uri_auth->admin_rules, list) { |
Willy Tarreau | 1facd6d | 2012-12-22 22:03:39 +0100 | [diff] [blame] | 3279 | int ret = 1; |
| 3280 | |
| 3281 | if (stats_admin_rule->cond) { |
Willy Tarreau | 192252e | 2015-04-04 01:47:55 +0200 | [diff] [blame] | 3282 | 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] | 3283 | ret = acl_pass(ret); |
| 3284 | if (stats_admin_rule->cond->pol == ACL_COND_UNLESS) |
| 3285 | ret = !ret; |
| 3286 | } |
| 3287 | |
| 3288 | if (ret) { |
| 3289 | /* no rule, or the rule matches */ |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 3290 | appctx->ctx.stats.flags |= STAT_ADMIN; |
Willy Tarreau | 1facd6d | 2012-12-22 22:03:39 +0100 | [diff] [blame] | 3291 | break; |
| 3292 | } |
| 3293 | } |
| 3294 | |
| 3295 | /* Was the status page requested with a POST ? */ |
Willy Tarreau | b8cdf52 | 2015-05-29 01:09:15 +0200 | [diff] [blame] | 3296 | if (unlikely(txn->meth == HTTP_METH_POST && txn->req.body_len > 0)) { |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 3297 | if (appctx->ctx.stats.flags & STAT_ADMIN) { |
Willy Tarreau | cfe7fdd | 2014-04-26 22:08:32 +0200 | [diff] [blame] | 3298 | /* we'll need the request body, possibly after sending 100-continue */ |
Willy Tarreau | b8cdf52 | 2015-05-29 01:09:15 +0200 | [diff] [blame] | 3299 | if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) |
| 3300 | req->analysers |= AN_REQ_HTTP_BODY; |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 3301 | appctx->st0 = STAT_HTTP_POST; |
Willy Tarreau | 1facd6d | 2012-12-22 22:03:39 +0100 | [diff] [blame] | 3302 | } |
Willy Tarreau | 347a35d | 2013-11-22 17:51:09 +0100 | [diff] [blame] | 3303 | else { |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 3304 | appctx->ctx.stats.st_code = STAT_STATUS_DENY; |
| 3305 | appctx->st0 = STAT_HTTP_LAST; |
de Lafond Guillaume | 88c278f | 2013-04-15 19:27:10 +0200 | [diff] [blame] | 3306 | } |
Willy Tarreau | 1facd6d | 2012-12-22 22:03:39 +0100 | [diff] [blame] | 3307 | } |
Willy Tarreau | 347a35d | 2013-11-22 17:51:09 +0100 | [diff] [blame] | 3308 | else { |
| 3309 | /* So it was another method (GET/HEAD) */ |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 3310 | appctx->st0 = STAT_HTTP_HEAD; |
Willy Tarreau | 1facd6d | 2012-12-22 22:03:39 +0100 | [diff] [blame] | 3311 | } |
| 3312 | |
Willy Tarreau | 1facd6d | 2012-12-22 22:03:39 +0100 | [diff] [blame] | 3313 | s->task->nice = -32; /* small boost for HTTP statistics */ |
Willy Tarreau | 1facd6d | 2012-12-22 22:03:39 +0100 | [diff] [blame] | 3314 | return 1; |
| 3315 | } |
| 3316 | |
Lukas Tribus | 67db8df | 2013-06-23 17:37:13 +0200 | [diff] [blame] | 3317 | /* Sets the TOS header in IPv4 and the traffic class header in IPv6 packets |
| 3318 | * (as per RFC3260 #4 and BCP37 #4.2 and #5.2). |
| 3319 | */ |
Thierry FOURNIER | 7fe75e0 | 2015-03-16 12:03:44 +0100 | [diff] [blame] | 3320 | void inet_set_tos(int fd, struct sockaddr_storage from, int tos) |
Lukas Tribus | 67db8df | 2013-06-23 17:37:13 +0200 | [diff] [blame] | 3321 | { |
| 3322 | #ifdef IP_TOS |
| 3323 | if (from.ss_family == AF_INET) |
| 3324 | setsockopt(fd, IPPROTO_IP, IP_TOS, &tos, sizeof(tos)); |
| 3325 | #endif |
| 3326 | #ifdef IPV6_TCLASS |
| 3327 | if (from.ss_family == AF_INET6) { |
| 3328 | if (IN6_IS_ADDR_V4MAPPED(&((struct sockaddr_in6 *)&from)->sin6_addr)) |
| 3329 | /* v4-mapped addresses need IP_TOS */ |
| 3330 | setsockopt(fd, IPPROTO_IP, IP_TOS, &tos, sizeof(tos)); |
| 3331 | else |
| 3332 | setsockopt(fd, IPPROTO_IPV6, IPV6_TCLASS, &tos, sizeof(tos)); |
| 3333 | } |
| 3334 | #endif |
| 3335 | } |
| 3336 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 3337 | int http_transform_header_str(struct stream* s, struct http_msg *msg, |
Thierry FOURNIER | 5531f87 | 2015-03-16 11:15:50 +0100 | [diff] [blame] | 3338 | const char* name, unsigned int name_len, |
| 3339 | const char *str, struct my_regex *re, |
| 3340 | int action) |
Sasha Pachev | 218f064 | 2014-06-16 12:05:59 -0600 | [diff] [blame] | 3341 | { |
Thierry FOURNIER | 5a33ac7 | 2015-03-16 23:54:35 +0100 | [diff] [blame] | 3342 | struct hdr_ctx ctx; |
| 3343 | char *buf = msg->chn->buf->p; |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 3344 | struct hdr_idx *idx = &s->txn->hdr_idx; |
Thierry FOURNIER | 5531f87 | 2015-03-16 11:15:50 +0100 | [diff] [blame] | 3345 | int (*http_find_hdr_func)(const char *name, int len, char *sol, |
| 3346 | struct hdr_idx *idx, struct hdr_ctx *ctx); |
Thierry FOURNIER | 191f9ef | 2015-03-16 23:23:53 +0100 | [diff] [blame] | 3347 | struct chunk *output = get_trash_chunk(); |
| 3348 | |
Thierry FOURNIER | 5a33ac7 | 2015-03-16 23:54:35 +0100 | [diff] [blame] | 3349 | ctx.idx = 0; |
Sasha Pachev | 218f064 | 2014-06-16 12:05:59 -0600 | [diff] [blame] | 3350 | |
Thierry FOURNIER | 191f9ef | 2015-03-16 23:23:53 +0100 | [diff] [blame] | 3351 | /* Choose the header browsing function. */ |
| 3352 | switch (action) { |
| 3353 | case HTTP_REQ_ACT_REPLACE_VAL: |
| 3354 | case HTTP_RES_ACT_REPLACE_VAL: |
| 3355 | http_find_hdr_func = http_find_header2; |
| 3356 | break; |
| 3357 | case HTTP_REQ_ACT_REPLACE_HDR: |
| 3358 | case HTTP_RES_ACT_REPLACE_HDR: |
| 3359 | http_find_hdr_func = http_find_full_header2; |
| 3360 | break; |
| 3361 | default: /* impossible */ |
| 3362 | return -1; |
| 3363 | } |
| 3364 | |
Thierry FOURNIER | 5a33ac7 | 2015-03-16 23:54:35 +0100 | [diff] [blame] | 3365 | while (http_find_hdr_func(name, name_len, buf, idx, &ctx)) { |
| 3366 | struct hdr_idx_elem *hdr = idx->v + ctx.idx; |
Sasha Pachev | 218f064 | 2014-06-16 12:05:59 -0600 | [diff] [blame] | 3367 | int delta; |
Thierry FOURNIER | 5a33ac7 | 2015-03-16 23:54:35 +0100 | [diff] [blame] | 3368 | char *val = ctx.line + ctx.val; |
| 3369 | char* val_end = val + ctx.vlen; |
Sasha Pachev | 218f064 | 2014-06-16 12:05:59 -0600 | [diff] [blame] | 3370 | |
Thierry FOURNIER | 191f9ef | 2015-03-16 23:23:53 +0100 | [diff] [blame] | 3371 | if (!regex_exec_match2(re, val, val_end-val, MAX_MATCH, pmatch, 0)) |
| 3372 | continue; |
Sasha Pachev | 218f064 | 2014-06-16 12:05:59 -0600 | [diff] [blame] | 3373 | |
Thierry FOURNIER | 5531f87 | 2015-03-16 11:15:50 +0100 | [diff] [blame] | 3374 | output->len = exp_replace(output->str, output->size, val, str, pmatch); |
Thierry FOURNIER | 191f9ef | 2015-03-16 23:23:53 +0100 | [diff] [blame] | 3375 | if (output->len == -1) |
Sasha Pachev | 218f064 | 2014-06-16 12:05:59 -0600 | [diff] [blame] | 3376 | return -1; |
Sasha Pachev | 218f064 | 2014-06-16 12:05:59 -0600 | [diff] [blame] | 3377 | |
Thierry FOURNIER | 191f9ef | 2015-03-16 23:23:53 +0100 | [diff] [blame] | 3378 | 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] | 3379 | |
| 3380 | hdr->len += delta; |
| 3381 | http_msg_move_end(msg, delta); |
Thierry FOURNIER | 191f9ef | 2015-03-16 23:23:53 +0100 | [diff] [blame] | 3382 | |
| 3383 | /* Adjust the length of the current value of the index. */ |
Thierry FOURNIER | 5a33ac7 | 2015-03-16 23:54:35 +0100 | [diff] [blame] | 3384 | ctx.vlen += delta; |
Sasha Pachev | 218f064 | 2014-06-16 12:05:59 -0600 | [diff] [blame] | 3385 | } |
| 3386 | |
| 3387 | return 0; |
| 3388 | } |
| 3389 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 3390 | static int http_transform_header(struct stream* s, struct http_msg *msg, |
Thierry FOURNIER | 5531f87 | 2015-03-16 11:15:50 +0100 | [diff] [blame] | 3391 | const char* name, unsigned int name_len, |
| 3392 | struct list *fmt, struct my_regex *re, |
| 3393 | int action) |
| 3394 | { |
| 3395 | struct chunk *replace = get_trash_chunk(); |
| 3396 | |
| 3397 | replace->len = build_logline(s, replace->str, replace->size, fmt); |
| 3398 | if (replace->len >= replace->size - 1) |
| 3399 | return -1; |
| 3400 | |
| 3401 | return http_transform_header_str(s, msg, name, name_len, replace->str, re, action); |
| 3402 | } |
| 3403 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 3404 | /* Executes the http-request rules <rules> for stream <s>, proxy <px> and |
Willy Tarreau | 0b74833 | 2014-04-29 00:13:29 +0200 | [diff] [blame] | 3405 | * transaction <txn>. Returns the verdict of the first rule that prevents |
| 3406 | * further processing of the request (auth, deny, ...), and defaults to |
| 3407 | * HTTP_RULE_RES_STOP if it executed all rules or stopped on an allow, or |
| 3408 | * HTTP_RULE_RES_CONT if the last rule was reached. It may set the TX_CLTARPIT |
| 3409 | * on txn->flags if it encounters a tarpit rule. |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 3410 | */ |
Willy Tarreau | 0b74833 | 2014-04-29 00:13:29 +0200 | [diff] [blame] | 3411 | enum rule_result |
Willy Tarreau | 987e3fb | 2015-04-04 01:09:08 +0200 | [diff] [blame] | 3412 | http_req_get_intercept_rule(struct proxy *px, struct list *rules, struct stream *s) |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 3413 | { |
Willy Tarreau | 9ad7bd4 | 2015-04-03 19:19:59 +0200 | [diff] [blame] | 3414 | struct session *sess = strm_sess(s); |
Willy Tarreau | 987e3fb | 2015-04-04 01:09:08 +0200 | [diff] [blame] | 3415 | struct http_txn *txn = s->txn; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 3416 | struct connection *cli_conn; |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 3417 | struct http_req_rule *rule; |
Willy Tarreau | 20b0de5 | 2012-12-24 15:45:22 +0100 | [diff] [blame] | 3418 | struct hdr_ctx ctx; |
Willy Tarreau | ae3c010 | 2014-04-28 23:22:08 +0200 | [diff] [blame] | 3419 | const char *auth_realm; |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 3420 | |
Thierry FOURNIER | bc4c1ac | 2015-02-25 13:36:14 +0100 | [diff] [blame] | 3421 | /* If "the current_rule_list" match the executed rule list, we are in |
| 3422 | * resume condition. If a resume is needed it is always in the action |
| 3423 | * and never in the ACL or converters. In this case, we initialise the |
| 3424 | * current rule, and go to the action execution point. |
| 3425 | */ |
Willy Tarreau | 152b81e | 2015-04-20 13:26:17 +0200 | [diff] [blame] | 3426 | if (s->current_rule) { |
| 3427 | rule = s->current_rule; |
| 3428 | s->current_rule = NULL; |
| 3429 | if (s->current_rule_list == rules) |
| 3430 | goto resume_execution; |
Thierry FOURNIER | bc4c1ac | 2015-02-25 13:36:14 +0100 | [diff] [blame] | 3431 | } |
| 3432 | s->current_rule_list = rules; |
Willy Tarreau | 152b81e | 2015-04-20 13:26:17 +0200 | [diff] [blame] | 3433 | |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 3434 | list_for_each_entry(rule, rules, list) { |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 3435 | if (rule->action >= HTTP_REQ_ACT_MAX) |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 3436 | continue; |
| 3437 | |
Willy Tarreau | 96257ec | 2012-12-27 10:46:37 +0100 | [diff] [blame] | 3438 | /* check optional condition */ |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 3439 | if (rule->cond) { |
Willy Tarreau | 96257ec | 2012-12-27 10:46:37 +0100 | [diff] [blame] | 3440 | int ret; |
| 3441 | |
Willy Tarreau | 192252e | 2015-04-04 01:47:55 +0200 | [diff] [blame] | 3442 | 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] | 3443 | ret = acl_pass(ret); |
| 3444 | |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 3445 | if (rule->cond->pol == ACL_COND_UNLESS) |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 3446 | ret = !ret; |
Willy Tarreau | 96257ec | 2012-12-27 10:46:37 +0100 | [diff] [blame] | 3447 | |
| 3448 | if (!ret) /* condition not matched */ |
| 3449 | continue; |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 3450 | } |
| 3451 | |
Thierry FOURNIER | bc4c1ac | 2015-02-25 13:36:14 +0100 | [diff] [blame] | 3452 | resume_execution: |
Willy Tarreau | 96257ec | 2012-12-27 10:46:37 +0100 | [diff] [blame] | 3453 | switch (rule->action) { |
| 3454 | case HTTP_REQ_ACT_ALLOW: |
Willy Tarreau | 0b74833 | 2014-04-29 00:13:29 +0200 | [diff] [blame] | 3455 | return HTTP_RULE_RES_STOP; |
Willy Tarreau | 96257ec | 2012-12-27 10:46:37 +0100 | [diff] [blame] | 3456 | |
| 3457 | case HTTP_REQ_ACT_DENY: |
CJ Ess | 108b1dd | 2015-04-07 12:03:37 -0400 | [diff] [blame] | 3458 | txn->rule_deny_status = rule->deny_status; |
Willy Tarreau | 0b74833 | 2014-04-29 00:13:29 +0200 | [diff] [blame] | 3459 | return HTTP_RULE_RES_DENY; |
Willy Tarreau | 96257ec | 2012-12-27 10:46:37 +0100 | [diff] [blame] | 3460 | |
Willy Tarreau | ccbcc37 | 2012-12-27 12:37:57 +0100 | [diff] [blame] | 3461 | case HTTP_REQ_ACT_TARPIT: |
| 3462 | txn->flags |= TX_CLTARPIT; |
CJ Ess | 108b1dd | 2015-04-07 12:03:37 -0400 | [diff] [blame] | 3463 | txn->rule_deny_status = rule->deny_status; |
Willy Tarreau | 0b74833 | 2014-04-29 00:13:29 +0200 | [diff] [blame] | 3464 | return HTTP_RULE_RES_DENY; |
Willy Tarreau | ccbcc37 | 2012-12-27 12:37:57 +0100 | [diff] [blame] | 3465 | |
Willy Tarreau | 96257ec | 2012-12-27 10:46:37 +0100 | [diff] [blame] | 3466 | case HTTP_REQ_ACT_AUTH: |
Willy Tarreau | ae3c010 | 2014-04-28 23:22:08 +0200 | [diff] [blame] | 3467 | /* Auth might be performed on regular http-req rules as well as on stats */ |
| 3468 | auth_realm = rule->arg.auth.realm; |
| 3469 | if (!auth_realm) { |
| 3470 | if (px->uri_auth && rules == &px->uri_auth->http_req_rules) |
| 3471 | auth_realm = STATS_DEFAULT_REALM; |
| 3472 | else |
| 3473 | auth_realm = px->id; |
| 3474 | } |
| 3475 | /* send 401/407 depending on whether we use a proxy or not. We still |
| 3476 | * count one error, because normal browsing won't significantly |
| 3477 | * increase the counter but brute force attempts will. |
| 3478 | */ |
| 3479 | chunk_printf(&trash, (txn->flags & TX_USE_PX_CONN) ? HTTP_407_fmt : HTTP_401_fmt, auth_realm); |
| 3480 | txn->status = (txn->flags & TX_USE_PX_CONN) ? 407 : 401; |
| 3481 | stream_int_retnclose(&s->si[0], &trash); |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 3482 | stream_inc_http_err_ctr(s); |
Willy Tarreau | 0b74833 | 2014-04-29 00:13:29 +0200 | [diff] [blame] | 3483 | return HTTP_RULE_RES_ABRT; |
Willy Tarreau | 96257ec | 2012-12-27 10:46:37 +0100 | [diff] [blame] | 3484 | |
Willy Tarreau | 81499eb | 2012-12-27 12:19:02 +0100 | [diff] [blame] | 3485 | case HTTP_REQ_ACT_REDIR: |
Willy Tarreau | 0b74833 | 2014-04-29 00:13:29 +0200 | [diff] [blame] | 3486 | if (!http_apply_redirect_rule(rule->arg.redir, s, txn)) |
| 3487 | return HTTP_RULE_RES_BADREQ; |
| 3488 | return HTTP_RULE_RES_DONE; |
Willy Tarreau | 81499eb | 2012-12-27 12:19:02 +0100 | [diff] [blame] | 3489 | |
Willy Tarreau | f4c43c1 | 2013-06-11 17:01:13 +0200 | [diff] [blame] | 3490 | case HTTP_REQ_ACT_SET_NICE: |
| 3491 | s->task->nice = rule->arg.nice; |
| 3492 | break; |
| 3493 | |
Willy Tarreau | 42cf39e | 2013-06-11 18:51:32 +0200 | [diff] [blame] | 3494 | case HTTP_REQ_ACT_SET_TOS: |
Willy Tarreau | 9ad7bd4 | 2015-04-03 19:19:59 +0200 | [diff] [blame] | 3495 | if ((cli_conn = objt_conn(sess->origin)) && conn_ctrl_ready(cli_conn)) |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 3496 | 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] | 3497 | break; |
| 3498 | |
Willy Tarreau | 51347ed | 2013-06-11 19:34:13 +0200 | [diff] [blame] | 3499 | case HTTP_REQ_ACT_SET_MARK: |
| 3500 | #ifdef SO_MARK |
Willy Tarreau | 9ad7bd4 | 2015-04-03 19:19:59 +0200 | [diff] [blame] | 3501 | if ((cli_conn = objt_conn(sess->origin)) && conn_ctrl_ready(cli_conn)) |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 3502 | 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] | 3503 | #endif |
| 3504 | break; |
| 3505 | |
Willy Tarreau | 9a355ec | 2013-06-11 17:45:46 +0200 | [diff] [blame] | 3506 | case HTTP_REQ_ACT_SET_LOGL: |
| 3507 | s->logs.level = rule->arg.loglevel; |
| 3508 | break; |
| 3509 | |
Sasha Pachev | 218f064 | 2014-06-16 12:05:59 -0600 | [diff] [blame] | 3510 | case HTTP_REQ_ACT_REPLACE_HDR: |
| 3511 | case HTTP_REQ_ACT_REPLACE_VAL: |
Thierry FOURNIER | 5a33ac7 | 2015-03-16 23:54:35 +0100 | [diff] [blame] | 3512 | if (http_transform_header(s, &txn->req, rule->arg.hdr_add.name, |
| 3513 | rule->arg.hdr_add.name_len, |
| 3514 | &rule->arg.hdr_add.fmt, |
| 3515 | &rule->arg.hdr_add.re, rule->action)) |
Sasha Pachev | 218f064 | 2014-06-16 12:05:59 -0600 | [diff] [blame] | 3516 | return HTTP_RULE_RES_BADREQ; |
| 3517 | break; |
| 3518 | |
Thierry FOURNIER | dad3d1d | 2014-04-22 18:07:25 +0200 | [diff] [blame] | 3519 | case HTTP_REQ_ACT_DEL_HDR: |
Willy Tarreau | 96257ec | 2012-12-27 10:46:37 +0100 | [diff] [blame] | 3520 | ctx.idx = 0; |
| 3521 | /* remove all occurrences of the header */ |
| 3522 | while (http_find_header2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len, |
| 3523 | txn->req.chn->buf->p, &txn->hdr_idx, &ctx)) { |
| 3524 | http_remove_header2(&txn->req, &txn->hdr_idx, &ctx); |
Willy Tarreau | 20b0de5 | 2012-12-24 15:45:22 +0100 | [diff] [blame] | 3525 | } |
Willy Tarreau | 8560328 | 2015-01-21 20:39:27 +0100 | [diff] [blame] | 3526 | break; |
Willy Tarreau | 96257ec | 2012-12-27 10:46:37 +0100 | [diff] [blame] | 3527 | |
Willy Tarreau | 8560328 | 2015-01-21 20:39:27 +0100 | [diff] [blame] | 3528 | case HTTP_REQ_ACT_SET_HDR: |
Willy Tarreau | 96257ec | 2012-12-27 10:46:37 +0100 | [diff] [blame] | 3529 | case HTTP_REQ_ACT_ADD_HDR: |
| 3530 | chunk_printf(&trash, "%s: ", rule->arg.hdr_add.name); |
| 3531 | memcpy(trash.str, rule->arg.hdr_add.name, rule->arg.hdr_add.name_len); |
| 3532 | trash.len = rule->arg.hdr_add.name_len; |
| 3533 | trash.str[trash.len++] = ':'; |
| 3534 | trash.str[trash.len++] = ' '; |
| 3535 | 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] | 3536 | |
| 3537 | if (rule->action == HTTP_REQ_ACT_SET_HDR) { |
| 3538 | /* remove all occurrences of the header */ |
| 3539 | ctx.idx = 0; |
| 3540 | while (http_find_header2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len, |
| 3541 | txn->req.chn->buf->p, &txn->hdr_idx, &ctx)) { |
| 3542 | http_remove_header2(&txn->req, &txn->hdr_idx, &ctx); |
| 3543 | } |
| 3544 | } |
| 3545 | |
Willy Tarreau | 96257ec | 2012-12-27 10:46:37 +0100 | [diff] [blame] | 3546 | http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, trash.len); |
| 3547 | break; |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 3548 | |
| 3549 | case HTTP_REQ_ACT_DEL_ACL: |
| 3550 | case HTTP_REQ_ACT_DEL_MAP: { |
| 3551 | struct pat_ref *ref; |
| 3552 | char *key; |
| 3553 | int len; |
| 3554 | |
| 3555 | /* collect reference */ |
| 3556 | ref = pat_ref_lookup(rule->arg.map.ref); |
| 3557 | if (!ref) |
| 3558 | continue; |
| 3559 | |
| 3560 | /* collect key */ |
| 3561 | len = build_logline(s, trash.str, trash.size, &rule->arg.map.key); |
| 3562 | key = trash.str; |
| 3563 | key[len] = '\0'; |
| 3564 | |
| 3565 | /* perform update */ |
| 3566 | /* returned code: 1=ok, 0=ko */ |
| 3567 | pat_ref_delete(ref, key); |
| 3568 | |
| 3569 | break; |
| 3570 | } |
| 3571 | |
| 3572 | case HTTP_REQ_ACT_ADD_ACL: { |
| 3573 | struct pat_ref *ref; |
| 3574 | char *key; |
| 3575 | struct chunk *trash_key; |
| 3576 | int len; |
| 3577 | |
| 3578 | trash_key = get_trash_chunk(); |
| 3579 | |
| 3580 | /* collect reference */ |
| 3581 | ref = pat_ref_lookup(rule->arg.map.ref); |
| 3582 | if (!ref) |
| 3583 | continue; |
| 3584 | |
| 3585 | /* collect key */ |
| 3586 | len = build_logline(s, trash_key->str, trash_key->size, &rule->arg.map.key); |
| 3587 | key = trash_key->str; |
| 3588 | key[len] = '\0'; |
| 3589 | |
| 3590 | /* perform update */ |
| 3591 | /* add entry only if it does not already exist */ |
| 3592 | if (pat_ref_find_elt(ref, key) == NULL) |
Thierry FOURNIER | e47e4e2 | 2014-04-28 11:18:57 +0200 | [diff] [blame] | 3593 | pat_ref_add(ref, key, NULL, NULL); |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 3594 | |
| 3595 | break; |
| 3596 | } |
| 3597 | |
| 3598 | case HTTP_REQ_ACT_SET_MAP: { |
| 3599 | struct pat_ref *ref; |
| 3600 | char *key, *value; |
| 3601 | struct chunk *trash_key, *trash_value; |
| 3602 | int len; |
| 3603 | |
| 3604 | trash_key = get_trash_chunk(); |
| 3605 | trash_value = get_trash_chunk(); |
| 3606 | |
| 3607 | /* collect reference */ |
| 3608 | ref = pat_ref_lookup(rule->arg.map.ref); |
| 3609 | if (!ref) |
| 3610 | continue; |
| 3611 | |
| 3612 | /* collect key */ |
| 3613 | len = build_logline(s, trash_key->str, trash_key->size, &rule->arg.map.key); |
| 3614 | key = trash_key->str; |
| 3615 | key[len] = '\0'; |
| 3616 | |
| 3617 | /* collect value */ |
| 3618 | len = build_logline(s, trash_value->str, trash_value->size, &rule->arg.map.value); |
| 3619 | value = trash_value->str; |
| 3620 | value[len] = '\0'; |
| 3621 | |
| 3622 | /* perform update */ |
| 3623 | if (pat_ref_find_elt(ref, key) != NULL) |
| 3624 | /* update entry if it exists */ |
| 3625 | pat_ref_set(ref, key, value, NULL); |
| 3626 | else |
| 3627 | /* insert a new entry */ |
Thierry FOURNIER | e47e4e2 | 2014-04-28 11:18:57 +0200 | [diff] [blame] | 3628 | pat_ref_add(ref, key, value, NULL); |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 3629 | |
| 3630 | break; |
| 3631 | } |
William Lallemand | 73025dd | 2014-04-24 14:38:37 +0200 | [diff] [blame] | 3632 | |
| 3633 | case HTTP_REQ_ACT_CUSTOM_CONT: |
Willy Tarreau | 987e3fb | 2015-04-04 01:09:08 +0200 | [diff] [blame] | 3634 | if (!rule->action_ptr(rule, px, s)) { |
Willy Tarreau | 152b81e | 2015-04-20 13:26:17 +0200 | [diff] [blame] | 3635 | s->current_rule = rule; |
Thierry FOURNIER | bc4c1ac | 2015-02-25 13:36:14 +0100 | [diff] [blame] | 3636 | return HTTP_RULE_RES_YIELD; |
| 3637 | } |
William Lallemand | 73025dd | 2014-04-24 14:38:37 +0200 | [diff] [blame] | 3638 | break; |
| 3639 | |
| 3640 | case HTTP_REQ_ACT_CUSTOM_STOP: |
Willy Tarreau | 987e3fb | 2015-04-04 01:09:08 +0200 | [diff] [blame] | 3641 | rule->action_ptr(rule, px, s); |
Willy Tarreau | 0b74833 | 2014-04-29 00:13:29 +0200 | [diff] [blame] | 3642 | return HTTP_RULE_RES_DONE; |
Willy Tarreau | 09448f7 | 2014-06-25 18:12:15 +0200 | [diff] [blame] | 3643 | |
| 3644 | case HTTP_REQ_ACT_TRK_SC0 ... HTTP_REQ_ACT_TRK_SCMAX: |
| 3645 | /* Note: only the first valid tracking parameter of each |
| 3646 | * applies. |
| 3647 | */ |
| 3648 | |
| 3649 | if (stkctr_entry(&s->stkctr[http_req_trk_idx(rule->action)]) == NULL) { |
| 3650 | struct stktable *t; |
| 3651 | struct stksess *ts; |
| 3652 | struct stktable_key *key; |
| 3653 | void *ptr; |
| 3654 | |
| 3655 | t = rule->act_prm.trk_ctr.table.t; |
Willy Tarreau | 192252e | 2015-04-04 01:47:55 +0200 | [diff] [blame] | 3656 | key = stktable_fetch_key(t, s->be, sess, s, SMP_OPT_DIR_REQ | SMP_OPT_FINAL, rule->act_prm.trk_ctr.expr, NULL); |
Willy Tarreau | 09448f7 | 2014-06-25 18:12:15 +0200 | [diff] [blame] | 3657 | |
| 3658 | if (key && (ts = stktable_get_entry(t, key))) { |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 3659 | stream_track_stkctr(&s->stkctr[http_req_trk_idx(rule->action)], t, ts); |
Willy Tarreau | 09448f7 | 2014-06-25 18:12:15 +0200 | [diff] [blame] | 3660 | |
| 3661 | /* let's count a new HTTP request as it's the first time we do it */ |
| 3662 | ptr = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_REQ_CNT); |
| 3663 | if (ptr) |
| 3664 | stktable_data_cast(ptr, http_req_cnt)++; |
| 3665 | |
| 3666 | ptr = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_REQ_RATE); |
| 3667 | if (ptr) |
| 3668 | update_freq_ctr_period(&stktable_data_cast(ptr, http_req_rate), |
| 3669 | t->data_arg[STKTABLE_DT_HTTP_REQ_RATE].u, 1); |
| 3670 | |
| 3671 | stkctr_set_flags(&s->stkctr[http_req_trk_idx(rule->action)], STKCTR_TRACK_CONTENT); |
Willy Tarreau | 9ad7bd4 | 2015-04-03 19:19:59 +0200 | [diff] [blame] | 3672 | if (sess->fe != s->be) |
Willy Tarreau | 09448f7 | 2014-06-25 18:12:15 +0200 | [diff] [blame] | 3673 | stkctr_set_flags(&s->stkctr[http_req_trk_idx(rule->action)], STKCTR_TRACK_BACKEND); |
| 3674 | } |
| 3675 | } |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 3676 | } |
| 3677 | } |
Willy Tarreau | 96257ec | 2012-12-27 10:46:37 +0100 | [diff] [blame] | 3678 | |
| 3679 | /* we reached the end of the rules, nothing to report */ |
Willy Tarreau | 0b74833 | 2014-04-29 00:13:29 +0200 | [diff] [blame] | 3680 | return HTTP_RULE_RES_CONT; |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 3681 | } |
| 3682 | |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 3683 | |
Willy Tarreau | 51d861a | 2015-05-22 17:30:48 +0200 | [diff] [blame] | 3684 | /* Executes the http-response rules <rules> for stream <s> and proxy <px>. It |
| 3685 | * returns one of 5 possible statuses: HTTP_RULE_RES_CONT, HTTP_RULE_RES_STOP, |
| 3686 | * HTTP_RULE_RES_DONE, HTTP_RULE_RES_YIELD, or HTTP_RULE_RES_BADREQ. If *CONT |
| 3687 | * is returned, the process can continue the evaluation of next rule list. If |
| 3688 | * *STOP or *DONE is returned, the process must stop the evaluation. If *BADREQ |
| 3689 | * is returned, it means the operation could not be processed and a server error |
| 3690 | * must be returned. It may set the TX_SVDENY on txn->flags if it encounters a |
| 3691 | * deny rule. If *YIELD is returned, the caller must call again the function |
| 3692 | * with the same context. |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 3693 | */ |
Thierry FOURNIER | 9e2ef99 | 2015-02-25 13:51:19 +0100 | [diff] [blame] | 3694 | static enum rule_result |
Willy Tarreau | 987e3fb | 2015-04-04 01:09:08 +0200 | [diff] [blame] | 3695 | 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] | 3696 | { |
Willy Tarreau | 9ad7bd4 | 2015-04-03 19:19:59 +0200 | [diff] [blame] | 3697 | struct session *sess = strm_sess(s); |
Willy Tarreau | 987e3fb | 2015-04-04 01:09:08 +0200 | [diff] [blame] | 3698 | struct http_txn *txn = s->txn; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 3699 | struct connection *cli_conn; |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 3700 | struct http_res_rule *rule; |
| 3701 | struct hdr_ctx ctx; |
| 3702 | |
Thierry FOURNIER | bc4c1ac | 2015-02-25 13:36:14 +0100 | [diff] [blame] | 3703 | /* If "the current_rule_list" match the executed rule list, we are in |
| 3704 | * resume condition. If a resume is needed it is always in the action |
| 3705 | * and never in the ACL or converters. In this case, we initialise the |
| 3706 | * current rule, and go to the action execution point. |
| 3707 | */ |
Willy Tarreau | 152b81e | 2015-04-20 13:26:17 +0200 | [diff] [blame] | 3708 | if (s->current_rule) { |
| 3709 | rule = s->current_rule; |
| 3710 | s->current_rule = NULL; |
| 3711 | if (s->current_rule_list == rules) |
| 3712 | goto resume_execution; |
Thierry FOURNIER | bc4c1ac | 2015-02-25 13:36:14 +0100 | [diff] [blame] | 3713 | } |
| 3714 | s->current_rule_list = rules; |
Willy Tarreau | 152b81e | 2015-04-20 13:26:17 +0200 | [diff] [blame] | 3715 | |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 3716 | list_for_each_entry(rule, rules, list) { |
| 3717 | if (rule->action >= HTTP_RES_ACT_MAX) |
| 3718 | continue; |
| 3719 | |
| 3720 | /* check optional condition */ |
| 3721 | if (rule->cond) { |
| 3722 | int ret; |
| 3723 | |
Willy Tarreau | 192252e | 2015-04-04 01:47:55 +0200 | [diff] [blame] | 3724 | 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] | 3725 | ret = acl_pass(ret); |
| 3726 | |
| 3727 | if (rule->cond->pol == ACL_COND_UNLESS) |
| 3728 | ret = !ret; |
| 3729 | |
| 3730 | if (!ret) /* condition not matched */ |
| 3731 | continue; |
| 3732 | } |
| 3733 | |
Thierry FOURNIER | bc4c1ac | 2015-02-25 13:36:14 +0100 | [diff] [blame] | 3734 | resume_execution: |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 3735 | switch (rule->action) { |
| 3736 | case HTTP_RES_ACT_ALLOW: |
Thierry FOURNIER | 9e2ef99 | 2015-02-25 13:51:19 +0100 | [diff] [blame] | 3737 | return HTTP_RULE_RES_STOP; /* "allow" rules are OK */ |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 3738 | |
| 3739 | case HTTP_RES_ACT_DENY: |
| 3740 | txn->flags |= TX_SVDENY; |
Thierry FOURNIER | 9e2ef99 | 2015-02-25 13:51:19 +0100 | [diff] [blame] | 3741 | return HTTP_RULE_RES_STOP; |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 3742 | |
Willy Tarreau | f4c43c1 | 2013-06-11 17:01:13 +0200 | [diff] [blame] | 3743 | case HTTP_RES_ACT_SET_NICE: |
| 3744 | s->task->nice = rule->arg.nice; |
| 3745 | break; |
| 3746 | |
Willy Tarreau | 42cf39e | 2013-06-11 18:51:32 +0200 | [diff] [blame] | 3747 | case HTTP_RES_ACT_SET_TOS: |
Willy Tarreau | 9ad7bd4 | 2015-04-03 19:19:59 +0200 | [diff] [blame] | 3748 | if ((cli_conn = objt_conn(sess->origin)) && conn_ctrl_ready(cli_conn)) |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 3749 | 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] | 3750 | break; |
| 3751 | |
Willy Tarreau | 51347ed | 2013-06-11 19:34:13 +0200 | [diff] [blame] | 3752 | case HTTP_RES_ACT_SET_MARK: |
| 3753 | #ifdef SO_MARK |
Willy Tarreau | 9ad7bd4 | 2015-04-03 19:19:59 +0200 | [diff] [blame] | 3754 | if ((cli_conn = objt_conn(sess->origin)) && conn_ctrl_ready(cli_conn)) |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 3755 | 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] | 3756 | #endif |
| 3757 | break; |
| 3758 | |
Willy Tarreau | 9a355ec | 2013-06-11 17:45:46 +0200 | [diff] [blame] | 3759 | case HTTP_RES_ACT_SET_LOGL: |
| 3760 | s->logs.level = rule->arg.loglevel; |
| 3761 | break; |
| 3762 | |
Sasha Pachev | 218f064 | 2014-06-16 12:05:59 -0600 | [diff] [blame] | 3763 | case HTTP_RES_ACT_REPLACE_HDR: |
| 3764 | case HTTP_RES_ACT_REPLACE_VAL: |
Thierry FOURNIER | 5a33ac7 | 2015-03-16 23:54:35 +0100 | [diff] [blame] | 3765 | if (http_transform_header(s, &txn->rsp, rule->arg.hdr_add.name, |
| 3766 | rule->arg.hdr_add.name_len, |
| 3767 | &rule->arg.hdr_add.fmt, |
| 3768 | &rule->arg.hdr_add.re, rule->action)) |
Thierry FOURNIER | 9e2ef99 | 2015-02-25 13:51:19 +0100 | [diff] [blame] | 3769 | return HTTP_RULE_RES_STOP; /* note: we should report an error here */ |
Sasha Pachev | 218f064 | 2014-06-16 12:05:59 -0600 | [diff] [blame] | 3770 | break; |
| 3771 | |
Thierry FOURNIER | dad3d1d | 2014-04-22 18:07:25 +0200 | [diff] [blame] | 3772 | case HTTP_RES_ACT_DEL_HDR: |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 3773 | ctx.idx = 0; |
| 3774 | /* remove all occurrences of the header */ |
| 3775 | while (http_find_header2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len, |
| 3776 | txn->rsp.chn->buf->p, &txn->hdr_idx, &ctx)) { |
| 3777 | http_remove_header2(&txn->rsp, &txn->hdr_idx, &ctx); |
| 3778 | } |
Willy Tarreau | 8560328 | 2015-01-21 20:39:27 +0100 | [diff] [blame] | 3779 | break; |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 3780 | |
Willy Tarreau | 8560328 | 2015-01-21 20:39:27 +0100 | [diff] [blame] | 3781 | case HTTP_RES_ACT_SET_HDR: |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 3782 | case HTTP_RES_ACT_ADD_HDR: |
| 3783 | chunk_printf(&trash, "%s: ", rule->arg.hdr_add.name); |
| 3784 | memcpy(trash.str, rule->arg.hdr_add.name, rule->arg.hdr_add.name_len); |
| 3785 | trash.len = rule->arg.hdr_add.name_len; |
| 3786 | trash.str[trash.len++] = ':'; |
| 3787 | trash.str[trash.len++] = ' '; |
| 3788 | 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] | 3789 | |
| 3790 | if (rule->action == HTTP_RES_ACT_SET_HDR) { |
| 3791 | /* remove all occurrences of the header */ |
| 3792 | ctx.idx = 0; |
| 3793 | while (http_find_header2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len, |
| 3794 | txn->rsp.chn->buf->p, &txn->hdr_idx, &ctx)) { |
| 3795 | http_remove_header2(&txn->rsp, &txn->hdr_idx, &ctx); |
| 3796 | } |
| 3797 | } |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 3798 | http_header_add_tail2(&txn->rsp, &txn->hdr_idx, trash.str, trash.len); |
| 3799 | break; |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 3800 | |
| 3801 | case HTTP_RES_ACT_DEL_ACL: |
| 3802 | case HTTP_RES_ACT_DEL_MAP: { |
| 3803 | struct pat_ref *ref; |
| 3804 | char *key; |
| 3805 | int len; |
| 3806 | |
| 3807 | /* collect reference */ |
| 3808 | ref = pat_ref_lookup(rule->arg.map.ref); |
| 3809 | if (!ref) |
| 3810 | continue; |
| 3811 | |
| 3812 | /* collect key */ |
| 3813 | len = build_logline(s, trash.str, trash.size, &rule->arg.map.key); |
| 3814 | key = trash.str; |
| 3815 | key[len] = '\0'; |
| 3816 | |
| 3817 | /* perform update */ |
| 3818 | /* returned code: 1=ok, 0=ko */ |
| 3819 | pat_ref_delete(ref, key); |
| 3820 | |
| 3821 | break; |
| 3822 | } |
| 3823 | |
| 3824 | case HTTP_RES_ACT_ADD_ACL: { |
| 3825 | struct pat_ref *ref; |
| 3826 | char *key; |
| 3827 | struct chunk *trash_key; |
| 3828 | int len; |
| 3829 | |
| 3830 | trash_key = get_trash_chunk(); |
| 3831 | |
| 3832 | /* collect reference */ |
| 3833 | ref = pat_ref_lookup(rule->arg.map.ref); |
| 3834 | if (!ref) |
| 3835 | continue; |
| 3836 | |
| 3837 | /* collect key */ |
| 3838 | len = build_logline(s, trash_key->str, trash_key->size, &rule->arg.map.key); |
| 3839 | key = trash_key->str; |
| 3840 | key[len] = '\0'; |
| 3841 | |
| 3842 | /* perform update */ |
| 3843 | /* check if the entry already exists */ |
| 3844 | if (pat_ref_find_elt(ref, key) == NULL) |
Thierry FOURNIER | e47e4e2 | 2014-04-28 11:18:57 +0200 | [diff] [blame] | 3845 | pat_ref_add(ref, key, NULL, NULL); |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 3846 | |
| 3847 | break; |
| 3848 | } |
| 3849 | |
| 3850 | case HTTP_RES_ACT_SET_MAP: { |
| 3851 | struct pat_ref *ref; |
| 3852 | char *key, *value; |
| 3853 | struct chunk *trash_key, *trash_value; |
| 3854 | int len; |
| 3855 | |
| 3856 | trash_key = get_trash_chunk(); |
| 3857 | trash_value = get_trash_chunk(); |
| 3858 | |
| 3859 | /* collect reference */ |
| 3860 | ref = pat_ref_lookup(rule->arg.map.ref); |
| 3861 | if (!ref) |
| 3862 | continue; |
| 3863 | |
| 3864 | /* collect key */ |
| 3865 | len = build_logline(s, trash_key->str, trash_key->size, &rule->arg.map.key); |
| 3866 | key = trash_key->str; |
| 3867 | key[len] = '\0'; |
| 3868 | |
| 3869 | /* collect value */ |
| 3870 | len = build_logline(s, trash_value->str, trash_value->size, &rule->arg.map.value); |
| 3871 | value = trash_value->str; |
| 3872 | value[len] = '\0'; |
| 3873 | |
| 3874 | /* perform update */ |
| 3875 | if (pat_ref_find_elt(ref, key) != NULL) |
| 3876 | /* update entry if it exists */ |
| 3877 | pat_ref_set(ref, key, value, NULL); |
| 3878 | else |
| 3879 | /* insert a new entry */ |
Thierry FOURNIER | e47e4e2 | 2014-04-28 11:18:57 +0200 | [diff] [blame] | 3880 | pat_ref_add(ref, key, value, NULL); |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 3881 | |
| 3882 | break; |
| 3883 | } |
William Lallemand | 73025dd | 2014-04-24 14:38:37 +0200 | [diff] [blame] | 3884 | |
Willy Tarreau | 51d861a | 2015-05-22 17:30:48 +0200 | [diff] [blame] | 3885 | case HTTP_RES_ACT_REDIR: |
| 3886 | if (!http_apply_redirect_rule(rule->arg.redir, s, txn)) |
| 3887 | return HTTP_RULE_RES_BADREQ; |
| 3888 | return HTTP_RULE_RES_DONE; |
| 3889 | |
William Lallemand | 73025dd | 2014-04-24 14:38:37 +0200 | [diff] [blame] | 3890 | case HTTP_RES_ACT_CUSTOM_CONT: |
Willy Tarreau | 987e3fb | 2015-04-04 01:09:08 +0200 | [diff] [blame] | 3891 | if (!rule->action_ptr(rule, px, s)) { |
Willy Tarreau | 152b81e | 2015-04-20 13:26:17 +0200 | [diff] [blame] | 3892 | s->current_rule = rule; |
Thierry FOURNIER | bc4c1ac | 2015-02-25 13:36:14 +0100 | [diff] [blame] | 3893 | return HTTP_RULE_RES_YIELD; |
| 3894 | } |
William Lallemand | 73025dd | 2014-04-24 14:38:37 +0200 | [diff] [blame] | 3895 | break; |
| 3896 | |
| 3897 | case HTTP_RES_ACT_CUSTOM_STOP: |
Willy Tarreau | 987e3fb | 2015-04-04 01:09:08 +0200 | [diff] [blame] | 3898 | rule->action_ptr(rule, px, s); |
Thierry FOURNIER | 9e2ef99 | 2015-02-25 13:51:19 +0100 | [diff] [blame] | 3899 | return HTTP_RULE_RES_STOP; |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 3900 | } |
| 3901 | } |
| 3902 | |
| 3903 | /* we reached the end of the rules, nothing to report */ |
Thierry FOURNIER | 9e2ef99 | 2015-02-25 13:51:19 +0100 | [diff] [blame] | 3904 | return HTTP_RULE_RES_CONT; |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 3905 | } |
| 3906 | |
| 3907 | |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 3908 | /* Perform an HTTP redirect based on the information in <rule>. The function |
| 3909 | * returns non-zero on success, or zero in case of a, irrecoverable error such |
| 3910 | * as too large a request to build a valid response. |
| 3911 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 3912 | 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] | 3913 | { |
Willy Tarreau | b329a31 | 2015-05-22 16:27:37 +0200 | [diff] [blame] | 3914 | struct http_msg *req = &txn->req; |
| 3915 | struct http_msg *res = &txn->rsp; |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 3916 | const char *msg_fmt; |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 3917 | const char *location; |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 3918 | |
| 3919 | /* build redirect message */ |
| 3920 | switch(rule->code) { |
Yves Lafon | 3e8d1ae | 2013-03-11 11:06:05 -0400 | [diff] [blame] | 3921 | case 308: |
| 3922 | msg_fmt = HTTP_308; |
| 3923 | break; |
| 3924 | case 307: |
| 3925 | msg_fmt = HTTP_307; |
| 3926 | break; |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 3927 | case 303: |
| 3928 | msg_fmt = HTTP_303; |
| 3929 | break; |
| 3930 | case 301: |
| 3931 | msg_fmt = HTTP_301; |
| 3932 | break; |
| 3933 | case 302: |
| 3934 | default: |
| 3935 | msg_fmt = HTTP_302; |
| 3936 | break; |
| 3937 | } |
| 3938 | |
| 3939 | if (unlikely(!chunk_strcpy(&trash, msg_fmt))) |
| 3940 | return 0; |
| 3941 | |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 3942 | location = trash.str + trash.len; |
| 3943 | |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 3944 | switch(rule->type) { |
| 3945 | case REDIRECT_TYPE_SCHEME: { |
| 3946 | const char *path; |
| 3947 | const char *host; |
| 3948 | struct hdr_ctx ctx; |
| 3949 | int pathlen; |
| 3950 | int hostlen; |
| 3951 | |
| 3952 | host = ""; |
| 3953 | hostlen = 0; |
| 3954 | ctx.idx = 0; |
Willy Tarreau | b329a31 | 2015-05-22 16:27:37 +0200 | [diff] [blame] | 3955 | 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] | 3956 | host = ctx.line + ctx.val; |
| 3957 | hostlen = ctx.vlen; |
| 3958 | } |
| 3959 | |
| 3960 | path = http_get_path(txn); |
| 3961 | /* build message using path */ |
| 3962 | if (path) { |
Willy Tarreau | b329a31 | 2015-05-22 16:27:37 +0200 | [diff] [blame] | 3963 | 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] | 3964 | if (rule->flags & REDIRECT_FLAG_DROP_QS) { |
| 3965 | int qs = 0; |
| 3966 | while (qs < pathlen) { |
| 3967 | if (path[qs] == '?') { |
| 3968 | pathlen = qs; |
| 3969 | break; |
| 3970 | } |
| 3971 | qs++; |
| 3972 | } |
| 3973 | } |
| 3974 | } else { |
| 3975 | path = "/"; |
| 3976 | pathlen = 1; |
| 3977 | } |
| 3978 | |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 3979 | if (rule->rdr_str) { /* this is an old "redirect" rule */ |
| 3980 | /* check if we can add scheme + "://" + host + path */ |
| 3981 | if (trash.len + rule->rdr_len + 3 + hostlen + pathlen > trash.size - 4) |
| 3982 | return 0; |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 3983 | |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 3984 | /* add scheme */ |
| 3985 | memcpy(trash.str + trash.len, rule->rdr_str, rule->rdr_len); |
| 3986 | trash.len += rule->rdr_len; |
| 3987 | } |
| 3988 | else { |
| 3989 | /* add scheme with executing log format */ |
| 3990 | trash.len += build_logline(s, trash.str + trash.len, trash.size - trash.len, &rule->rdr_fmt); |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 3991 | |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 3992 | /* check if we can add scheme + "://" + host + path */ |
| 3993 | if (trash.len + 3 + hostlen + pathlen > trash.size - 4) |
| 3994 | return 0; |
| 3995 | } |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 3996 | /* add "://" */ |
| 3997 | memcpy(trash.str + trash.len, "://", 3); |
| 3998 | trash.len += 3; |
| 3999 | |
| 4000 | /* add host */ |
| 4001 | memcpy(trash.str + trash.len, host, hostlen); |
| 4002 | trash.len += hostlen; |
| 4003 | |
| 4004 | /* add path */ |
| 4005 | memcpy(trash.str + trash.len, path, pathlen); |
| 4006 | trash.len += pathlen; |
| 4007 | |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 4008 | /* append a slash at the end of the location if needed and missing */ |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 4009 | if (trash.len && trash.str[trash.len - 1] != '/' && |
| 4010 | (rule->flags & REDIRECT_FLAG_APPEND_SLASH)) { |
| 4011 | if (trash.len > trash.size - 5) |
| 4012 | return 0; |
| 4013 | trash.str[trash.len] = '/'; |
| 4014 | trash.len++; |
| 4015 | } |
| 4016 | |
| 4017 | break; |
| 4018 | } |
| 4019 | case REDIRECT_TYPE_PREFIX: { |
| 4020 | const char *path; |
| 4021 | int pathlen; |
| 4022 | |
| 4023 | path = http_get_path(txn); |
| 4024 | /* build message using path */ |
| 4025 | if (path) { |
Willy Tarreau | b329a31 | 2015-05-22 16:27:37 +0200 | [diff] [blame] | 4026 | 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] | 4027 | if (rule->flags & REDIRECT_FLAG_DROP_QS) { |
| 4028 | int qs = 0; |
| 4029 | while (qs < pathlen) { |
| 4030 | if (path[qs] == '?') { |
| 4031 | pathlen = qs; |
| 4032 | break; |
| 4033 | } |
| 4034 | qs++; |
| 4035 | } |
| 4036 | } |
| 4037 | } else { |
| 4038 | path = "/"; |
| 4039 | pathlen = 1; |
| 4040 | } |
| 4041 | |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 4042 | if (rule->rdr_str) { /* this is an old "redirect" rule */ |
| 4043 | if (trash.len + rule->rdr_len + pathlen > trash.size - 4) |
| 4044 | return 0; |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 4045 | |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 4046 | /* add prefix. Note that if prefix == "/", we don't want to |
| 4047 | * add anything, otherwise it makes it hard for the user to |
| 4048 | * configure a self-redirection. |
| 4049 | */ |
| 4050 | if (rule->rdr_len != 1 || *rule->rdr_str != '/') { |
| 4051 | memcpy(trash.str + trash.len, rule->rdr_str, rule->rdr_len); |
| 4052 | trash.len += rule->rdr_len; |
| 4053 | } |
| 4054 | } |
| 4055 | else { |
| 4056 | /* add prefix with executing log format */ |
| 4057 | trash.len += build_logline(s, trash.str + trash.len, trash.size - trash.len, &rule->rdr_fmt); |
| 4058 | |
| 4059 | /* Check length */ |
| 4060 | if (trash.len + pathlen > trash.size - 4) |
| 4061 | return 0; |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 4062 | } |
| 4063 | |
| 4064 | /* add path */ |
| 4065 | memcpy(trash.str + trash.len, path, pathlen); |
| 4066 | trash.len += pathlen; |
| 4067 | |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 4068 | /* append a slash at the end of the location if needed and missing */ |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 4069 | if (trash.len && trash.str[trash.len - 1] != '/' && |
| 4070 | (rule->flags & REDIRECT_FLAG_APPEND_SLASH)) { |
| 4071 | if (trash.len > trash.size - 5) |
| 4072 | return 0; |
| 4073 | trash.str[trash.len] = '/'; |
| 4074 | trash.len++; |
| 4075 | } |
| 4076 | |
| 4077 | break; |
| 4078 | } |
| 4079 | case REDIRECT_TYPE_LOCATION: |
| 4080 | default: |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 4081 | if (rule->rdr_str) { /* this is an old "redirect" rule */ |
| 4082 | if (trash.len + rule->rdr_len > trash.size - 4) |
| 4083 | return 0; |
| 4084 | |
| 4085 | /* add location */ |
| 4086 | memcpy(trash.str + trash.len, rule->rdr_str, rule->rdr_len); |
| 4087 | trash.len += rule->rdr_len; |
| 4088 | } |
| 4089 | else { |
| 4090 | /* add location with executing log format */ |
| 4091 | trash.len += build_logline(s, trash.str + trash.len, trash.size - trash.len, &rule->rdr_fmt); |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 4092 | |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 4093 | /* Check left length */ |
| 4094 | if (trash.len > trash.size - 4) |
| 4095 | return 0; |
| 4096 | } |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 4097 | break; |
| 4098 | } |
| 4099 | |
| 4100 | if (rule->cookie_len) { |
| 4101 | memcpy(trash.str + trash.len, "\r\nSet-Cookie: ", 14); |
| 4102 | trash.len += 14; |
| 4103 | memcpy(trash.str + trash.len, rule->cookie_str, rule->cookie_len); |
| 4104 | trash.len += rule->cookie_len; |
| 4105 | memcpy(trash.str + trash.len, "\r\n", 2); |
| 4106 | trash.len += 2; |
| 4107 | } |
| 4108 | |
| 4109 | /* add end of headers and the keep-alive/close status. |
| 4110 | * We may choose to set keep-alive if the Location begins |
| 4111 | * with a slash, because the client will come back to the |
| 4112 | * same server. |
| 4113 | */ |
| 4114 | txn->status = rule->code; |
| 4115 | /* let's log the request time */ |
| 4116 | s->logs.tv_request = now; |
| 4117 | |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 4118 | if (*location == '/' && |
Willy Tarreau | b329a31 | 2015-05-22 16:27:37 +0200 | [diff] [blame] | 4119 | (req->flags & HTTP_MSGF_XFER_LEN) && |
Willy Tarreau | 2de8a50 | 2015-05-28 17:23:54 +0200 | [diff] [blame] | 4120 | ((!(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] | 4121 | ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL || |
| 4122 | (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL)) { |
| 4123 | /* keep-alive possible */ |
Willy Tarreau | b329a31 | 2015-05-22 16:27:37 +0200 | [diff] [blame] | 4124 | if (!(req->flags & HTTP_MSGF_VER_11)) { |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 4125 | if (unlikely(txn->flags & TX_USE_PX_CONN)) { |
| 4126 | memcpy(trash.str + trash.len, "\r\nProxy-Connection: keep-alive", 30); |
| 4127 | trash.len += 30; |
| 4128 | } else { |
| 4129 | memcpy(trash.str + trash.len, "\r\nConnection: keep-alive", 24); |
| 4130 | trash.len += 24; |
| 4131 | } |
| 4132 | } |
| 4133 | memcpy(trash.str + trash.len, "\r\n\r\n", 4); |
| 4134 | trash.len += 4; |
Willy Tarreau | b329a31 | 2015-05-22 16:27:37 +0200 | [diff] [blame] | 4135 | bo_inject(res->chn, trash.str, trash.len); |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 4136 | /* "eat" the request */ |
Willy Tarreau | b329a31 | 2015-05-22 16:27:37 +0200 | [diff] [blame] | 4137 | bi_fast_delete(req->chn->buf, req->sov); |
| 4138 | req->next -= req->sov; |
| 4139 | req->sov = 0; |
| 4140 | s->req.analysers = AN_REQ_HTTP_XFER_BODY; |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 4141 | s->res.analysers = AN_RES_HTTP_XFER_BODY; |
Willy Tarreau | b329a31 | 2015-05-22 16:27:37 +0200 | [diff] [blame] | 4142 | req->msg_state = HTTP_MSG_CLOSED; |
| 4143 | res->msg_state = HTTP_MSG_DONE; |
Willy Tarreau | 51d861a | 2015-05-22 17:30:48 +0200 | [diff] [blame] | 4144 | /* Trim any possible response */ |
| 4145 | res->chn->buf->i = 0; |
| 4146 | res->next = res->sov = 0; |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 4147 | } else { |
| 4148 | /* keep-alive not possible */ |
| 4149 | if (unlikely(txn->flags & TX_USE_PX_CONN)) { |
| 4150 | memcpy(trash.str + trash.len, "\r\nProxy-Connection: close\r\n\r\n", 29); |
| 4151 | trash.len += 29; |
| 4152 | } else { |
| 4153 | memcpy(trash.str + trash.len, "\r\nConnection: close\r\n\r\n", 23); |
| 4154 | trash.len += 23; |
| 4155 | } |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 4156 | stream_int_retnclose(&s->si[0], &trash); |
Willy Tarreau | b329a31 | 2015-05-22 16:27:37 +0200 | [diff] [blame] | 4157 | req->chn->analysers = 0; |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 4158 | } |
| 4159 | |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 4160 | if (!(s->flags & SF_ERR_MASK)) |
| 4161 | s->flags |= SF_ERR_LOCAL; |
| 4162 | if (!(s->flags & SF_FINST_MASK)) |
| 4163 | s->flags |= SF_FINST_R; |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 4164 | |
| 4165 | return 1; |
| 4166 | } |
| 4167 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 4168 | /* This stream analyser runs all HTTP request processing which is common to |
| 4169 | * frontends and backends, which means blocking ACLs, filters, connection-close, |
| 4170 | * reqadd, stats and redirects. This is performed for the designated proxy. |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 4171 | * 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] | 4172 | * either needs more data or wants to immediately abort the request (eg: deny, |
| 4173 | * error, ...). |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 4174 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 4175 | 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] | 4176 | { |
Willy Tarreau | fb0afa7 | 2015-04-03 14:46:27 +0200 | [diff] [blame] | 4177 | struct session *sess = s->sess; |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 4178 | struct http_txn *txn = s->txn; |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 4179 | struct http_msg *msg = &txn->req; |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 4180 | struct redirect_rule *rule; |
Willy Tarreau | f4f0412 | 2010-01-28 18:10:50 +0100 | [diff] [blame] | 4181 | struct cond_wordlist *wl; |
Willy Tarreau | 0b74833 | 2014-04-29 00:13:29 +0200 | [diff] [blame] | 4182 | enum rule_result verdict; |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 4183 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 4184 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) { |
Willy Tarreau | 51aecc7 | 2009-07-12 09:47:04 +0200 | [diff] [blame] | 4185 | /* we need more data */ |
Thierry FOURNIER | bc4c1ac | 2015-02-25 13:36:14 +0100 | [diff] [blame] | 4186 | goto return_prx_yield; |
Willy Tarreau | 51aecc7 | 2009-07-12 09:47:04 +0200 | [diff] [blame] | 4187 | } |
| 4188 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 4189 | 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] | 4190 | now_ms, __FUNCTION__, |
| 4191 | s, |
| 4192 | req, |
| 4193 | req->rex, req->wex, |
| 4194 | req->flags, |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 4195 | req->buf->i, |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 4196 | req->analysers); |
| 4197 | |
Willy Tarreau | 6541083 | 2014-04-28 21:25:43 +0200 | [diff] [blame] | 4198 | /* just in case we have some per-backend tracking */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 4199 | stream_inc_be_http_req_ctr(s); |
Willy Tarreau | 6541083 | 2014-04-28 21:25:43 +0200 | [diff] [blame] | 4200 | |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 4201 | /* evaluate http-request rules */ |
Willy Tarreau | 0b74833 | 2014-04-29 00:13:29 +0200 | [diff] [blame] | 4202 | if (!LIST_ISEMPTY(&px->http_req_rules)) { |
Willy Tarreau | 987e3fb | 2015-04-04 01:09:08 +0200 | [diff] [blame] | 4203 | verdict = http_req_get_intercept_rule(px, &px->http_req_rules, s); |
Willy Tarreau | 5142594 | 2010-02-01 10:40:19 +0100 | [diff] [blame] | 4204 | |
Willy Tarreau | 0b74833 | 2014-04-29 00:13:29 +0200 | [diff] [blame] | 4205 | switch (verdict) { |
Thierry FOURNIER | bc4c1ac | 2015-02-25 13:36:14 +0100 | [diff] [blame] | 4206 | case HTTP_RULE_RES_YIELD: /* some data miss, call the function later. */ |
| 4207 | goto return_prx_yield; |
| 4208 | |
Willy Tarreau | 0b74833 | 2014-04-29 00:13:29 +0200 | [diff] [blame] | 4209 | case HTTP_RULE_RES_CONT: |
| 4210 | case HTTP_RULE_RES_STOP: /* nothing to do */ |
| 4211 | break; |
Willy Tarreau | 5254259 | 2014-04-28 18:33:26 +0200 | [diff] [blame] | 4212 | |
Willy Tarreau | 0b74833 | 2014-04-29 00:13:29 +0200 | [diff] [blame] | 4213 | case HTTP_RULE_RES_DENY: /* deny or tarpit */ |
| 4214 | if (txn->flags & TX_CLTARPIT) |
| 4215 | goto tarpit; |
| 4216 | goto deny; |
Willy Tarreau | 5254259 | 2014-04-28 18:33:26 +0200 | [diff] [blame] | 4217 | |
Willy Tarreau | 0b74833 | 2014-04-29 00:13:29 +0200 | [diff] [blame] | 4218 | case HTTP_RULE_RES_ABRT: /* abort request, response already sent. Eg: auth */ |
| 4219 | goto return_prx_cond; |
Willy Tarreau | 5254259 | 2014-04-28 18:33:26 +0200 | [diff] [blame] | 4220 | |
Willy Tarreau | 0b74833 | 2014-04-29 00:13:29 +0200 | [diff] [blame] | 4221 | case HTTP_RULE_RES_DONE: /* OK, but terminate request processing (eg: redirect) */ |
Willy Tarreau | 5254259 | 2014-04-28 18:33:26 +0200 | [diff] [blame] | 4222 | goto done; |
| 4223 | |
Willy Tarreau | 0b74833 | 2014-04-29 00:13:29 +0200 | [diff] [blame] | 4224 | case HTTP_RULE_RES_BADREQ: /* failed with a bad request */ |
| 4225 | goto return_bad_req; |
| 4226 | } |
Willy Tarreau | bbba2a8 | 2014-04-28 13:57:26 +0200 | [diff] [blame] | 4227 | } |
| 4228 | |
Willy Tarreau | 5254259 | 2014-04-28 18:33:26 +0200 | [diff] [blame] | 4229 | /* OK at this stage, we know that the request was accepted according to |
| 4230 | * the http-request rules, we can check for the stats. Note that the |
| 4231 | * URI is detected *before* the req* rules in order not to be affected |
| 4232 | * by a possible reqrep, while they are processed *after* so that a |
| 4233 | * reqdeny can still block them. This clearly needs to change in 1.6! |
| 4234 | */ |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 4235 | if (stats_check_uri(&s->si[1], txn, px)) { |
Willy Tarreau | 5254259 | 2014-04-28 18:33:26 +0200 | [diff] [blame] | 4236 | s->target = &http_stats_applet.obj_type; |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 4237 | 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] | 4238 | txn->status = 500; |
| 4239 | s->logs.tv_request = now; |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 4240 | stream_int_retnclose(&s->si[0], http_error_message(s, HTTP_ERR_500)); |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 4241 | |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 4242 | if (!(s->flags & SF_ERR_MASK)) |
| 4243 | s->flags |= SF_ERR_RESOURCE; |
Willy Tarreau | 5254259 | 2014-04-28 18:33:26 +0200 | [diff] [blame] | 4244 | goto return_prx_cond; |
| 4245 | } |
| 4246 | |
| 4247 | /* parse the whole stats request and extract the relevant information */ |
| 4248 | http_handle_stats(s, req); |
Willy Tarreau | 987e3fb | 2015-04-04 01:09:08 +0200 | [diff] [blame] | 4249 | verdict = http_req_get_intercept_rule(px, &px->uri_auth->http_req_rules, s); |
Willy Tarreau | 0b74833 | 2014-04-29 00:13:29 +0200 | [diff] [blame] | 4250 | /* not all actions implemented: deny, allow, auth */ |
Willy Tarreau | bbba2a8 | 2014-04-28 13:57:26 +0200 | [diff] [blame] | 4251 | |
Willy Tarreau | 0b74833 | 2014-04-29 00:13:29 +0200 | [diff] [blame] | 4252 | if (verdict == HTTP_RULE_RES_DENY) /* stats http-request deny */ |
| 4253 | goto deny; |
Willy Tarreau | 5254259 | 2014-04-28 18:33:26 +0200 | [diff] [blame] | 4254 | |
Willy Tarreau | 0b74833 | 2014-04-29 00:13:29 +0200 | [diff] [blame] | 4255 | if (verdict == HTTP_RULE_RES_ABRT) /* stats auth / stats http-request auth */ |
| 4256 | goto return_prx_cond; |
Krzysztof Piotr Oledzki | 59bb218 | 2010-01-29 17:58:21 +0100 | [diff] [blame] | 4257 | } |
| 4258 | |
Willy Tarreau | bbba2a8 | 2014-04-28 13:57:26 +0200 | [diff] [blame] | 4259 | /* evaluate the req* rules except reqadd */ |
| 4260 | if (px->req_exp != NULL) { |
Willy Tarreau | 6c123b1 | 2010-01-28 20:22:06 +0100 | [diff] [blame] | 4261 | if (apply_filters_to_request(s, req, px) < 0) |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 4262 | goto return_bad_req; |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 4263 | |
Willy Tarreau | bbba2a8 | 2014-04-28 13:57:26 +0200 | [diff] [blame] | 4264 | if (txn->flags & TX_CLDENY) |
| 4265 | goto deny; |
Willy Tarreau | c465fd7 | 2009-08-31 00:17:18 +0200 | [diff] [blame] | 4266 | |
Willy Tarreau | bbba2a8 | 2014-04-28 13:57:26 +0200 | [diff] [blame] | 4267 | if (txn->flags & TX_CLTARPIT) |
| 4268 | goto tarpit; |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 4269 | } |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 4270 | |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 4271 | /* add request headers from the rule sets in the same order */ |
| 4272 | list_for_each_entry(wl, &px->req_add, list) { |
| 4273 | if (wl->cond) { |
Willy Tarreau | 192252e | 2015-04-04 01:47:55 +0200 | [diff] [blame] | 4274 | 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] | 4275 | ret = acl_pass(ret); |
| 4276 | if (((struct acl_cond *)wl->cond)->pol == ACL_COND_UNLESS) |
| 4277 | ret = !ret; |
| 4278 | if (!ret) |
| 4279 | continue; |
| 4280 | } |
| 4281 | |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 4282 | 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] | 4283 | goto return_bad_req; |
Willy Tarreau | 81499eb | 2012-12-27 12:19:02 +0100 | [diff] [blame] | 4284 | } |
| 4285 | |
Willy Tarreau | 5254259 | 2014-04-28 18:33:26 +0200 | [diff] [blame] | 4286 | |
| 4287 | /* Proceed with the stats now. */ |
Willy Tarreau | 414e9bb | 2013-11-23 00:30:38 +0100 | [diff] [blame] | 4288 | if (unlikely(objt_applet(s->target) == &http_stats_applet)) { |
Willy Tarreau | 1facd6d | 2012-12-22 22:03:39 +0100 | [diff] [blame] | 4289 | /* process the stats request now */ |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 4290 | if (sess->fe == s->be) /* report it if the request was intercepted by the frontend */ |
| 4291 | sess->fe->fe_counters.intercepted_req++; |
Willy Tarreau | 347a35d | 2013-11-22 17:51:09 +0100 | [diff] [blame] | 4292 | |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 4293 | if (!(s->flags & SF_ERR_MASK)) // this is not really an error but it is |
| 4294 | s->flags |= SF_ERR_LOCAL; // to mark that it comes from the proxy |
| 4295 | if (!(s->flags & SF_FINST_MASK)) |
| 4296 | s->flags |= SF_FINST_R; |
Willy Tarreau | 347a35d | 2013-11-22 17:51:09 +0100 | [diff] [blame] | 4297 | |
Willy Tarreau | 70730dd | 2014-04-24 18:06:27 +0200 | [diff] [blame] | 4298 | /* we may want to compress the stats page */ |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 4299 | if (sess->fe->comp || s->be->comp) |
Willy Tarreau | 70730dd | 2014-04-24 18:06:27 +0200 | [diff] [blame] | 4300 | select_compression_request_header(s, req->buf); |
| 4301 | |
| 4302 | /* enable the minimally required analyzers to handle keep-alive and compression on the HTTP response */ |
Willy Tarreau | 5506e3f | 2014-11-20 22:23:10 +0100 | [diff] [blame] | 4303 | req->analysers = (req->analysers & AN_REQ_HTTP_BODY) | AN_REQ_HTTP_XFER_BODY; |
Willy Tarreau | bbba2a8 | 2014-04-28 13:57:26 +0200 | [diff] [blame] | 4304 | goto done; |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 4305 | } |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 4306 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 4307 | /* check whether we have some ACLs set to redirect this request */ |
| 4308 | list_for_each_entry(rule, &px->redirect_rules, list) { |
Willy Tarreau | f285f54 | 2010-01-03 20:03:03 +0100 | [diff] [blame] | 4309 | if (rule->cond) { |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 4310 | int ret; |
| 4311 | |
Willy Tarreau | 192252e | 2015-04-04 01:47:55 +0200 | [diff] [blame] | 4312 | 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] | 4313 | ret = acl_pass(ret); |
| 4314 | if (rule->cond->pol == ACL_COND_UNLESS) |
| 4315 | ret = !ret; |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 4316 | if (!ret) |
| 4317 | continue; |
Willy Tarreau | f285f54 | 2010-01-03 20:03:03 +0100 | [diff] [blame] | 4318 | } |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 4319 | if (!http_apply_redirect_rule(rule, s, txn)) |
| 4320 | goto return_bad_req; |
Willy Tarreau | bbba2a8 | 2014-04-28 13:57:26 +0200 | [diff] [blame] | 4321 | goto done; |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 4322 | } |
Willy Tarreau | 55ea757 | 2007-06-17 19:56:27 +0200 | [diff] [blame] | 4323 | |
Willy Tarreau | 2be3939 | 2010-01-03 17:24:51 +0100 | [diff] [blame] | 4324 | /* POST requests may be accompanied with an "Expect: 100-Continue" header. |
| 4325 | * If this happens, then the data will not come immediately, so we must |
| 4326 | * send all what we have without waiting. Note that due to the small gain |
| 4327 | * 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] | 4328 | * 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] | 4329 | * itself once used. |
| 4330 | */ |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 4331 | req->flags |= CF_SEND_DONTWAIT; |
Willy Tarreau | 2be3939 | 2010-01-03 17:24:51 +0100 | [diff] [blame] | 4332 | |
Willy Tarreau | bbba2a8 | 2014-04-28 13:57:26 +0200 | [diff] [blame] | 4333 | done: /* done with this analyser, continue with next ones that the calling |
| 4334 | * points will have set, if any. |
| 4335 | */ |
Willy Tarreau | bbba2a8 | 2014-04-28 13:57:26 +0200 | [diff] [blame] | 4336 | req->analyse_exp = TICK_ETERNITY; |
Thierry FOURNIER | 7566e30 | 2014-08-22 06:55:26 +0200 | [diff] [blame] | 4337 | done_without_exp: /* done with this analyser, but dont reset the analyse_exp. */ |
| 4338 | req->analysers &= ~an_bit; |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 4339 | return 1; |
Willy Tarreau | 1138281 | 2008-07-09 16:18:21 +0200 | [diff] [blame] | 4340 | |
Willy Tarreau | bbba2a8 | 2014-04-28 13:57:26 +0200 | [diff] [blame] | 4341 | tarpit: |
| 4342 | /* When a connection is tarpitted, we use the tarpit timeout, |
| 4343 | * which may be the same as the connect timeout if unspecified. |
| 4344 | * If unset, then set it to zero because we really want it to |
| 4345 | * eventually expire. We build the tarpit as an analyser. |
| 4346 | */ |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 4347 | channel_erase(&s->req); |
Willy Tarreau | bbba2a8 | 2014-04-28 13:57:26 +0200 | [diff] [blame] | 4348 | |
| 4349 | /* wipe the request out so that we can drop the connection early |
| 4350 | * if the client closes first. |
| 4351 | */ |
| 4352 | channel_dont_connect(req); |
| 4353 | req->analysers = 0; /* remove switching rules etc... */ |
| 4354 | req->analysers |= AN_REQ_HTTP_TARPIT; |
| 4355 | req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.tarpit); |
| 4356 | if (!req->analyse_exp) |
| 4357 | req->analyse_exp = tick_add(now_ms, 0); |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 4358 | stream_inc_http_err_ctr(s); |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 4359 | sess->fe->fe_counters.denied_req++; |
| 4360 | if (sess->fe != s->be) |
Willy Tarreau | bbba2a8 | 2014-04-28 13:57:26 +0200 | [diff] [blame] | 4361 | s->be->be_counters.denied_req++; |
Willy Tarreau | fb0afa7 | 2015-04-03 14:46:27 +0200 | [diff] [blame] | 4362 | if (sess->listener->counters) |
| 4363 | sess->listener->counters->denied_req++; |
Thierry FOURNIER | 7566e30 | 2014-08-22 06:55:26 +0200 | [diff] [blame] | 4364 | goto done_without_exp; |
Willy Tarreau | bbba2a8 | 2014-04-28 13:57:26 +0200 | [diff] [blame] | 4365 | |
| 4366 | deny: /* this request was blocked (denied) */ |
Willy Tarreau | 0b74833 | 2014-04-29 00:13:29 +0200 | [diff] [blame] | 4367 | txn->flags |= TX_CLDENY; |
CJ Ess | 108b1dd | 2015-04-07 12:03:37 -0400 | [diff] [blame] | 4368 | txn->status = http_err_codes[txn->rule_deny_status]; |
Willy Tarreau | bbba2a8 | 2014-04-28 13:57:26 +0200 | [diff] [blame] | 4369 | s->logs.tv_request = now; |
CJ Ess | 108b1dd | 2015-04-07 12:03:37 -0400 | [diff] [blame] | 4370 | stream_int_retnclose(&s->si[0], http_error_message(s, txn->rule_deny_status)); |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 4371 | stream_inc_http_err_ctr(s); |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 4372 | sess->fe->fe_counters.denied_req++; |
| 4373 | if (sess->fe != s->be) |
Willy Tarreau | bbba2a8 | 2014-04-28 13:57:26 +0200 | [diff] [blame] | 4374 | s->be->be_counters.denied_req++; |
Willy Tarreau | fb0afa7 | 2015-04-03 14:46:27 +0200 | [diff] [blame] | 4375 | if (sess->listener->counters) |
| 4376 | sess->listener->counters->denied_req++; |
Willy Tarreau | bbba2a8 | 2014-04-28 13:57:26 +0200 | [diff] [blame] | 4377 | goto return_prx_cond; |
| 4378 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 4379 | return_bad_req: |
| 4380 | /* We centralize bad requests processing here */ |
| 4381 | if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) { |
| 4382 | /* we detected a parsing error. We want to archive this request |
| 4383 | * in the dedicated proxy area for later troubleshooting. |
| 4384 | */ |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 4385 | http_capture_bad_message(&sess->fe->invalid_req, s, msg, msg->msg_state, sess->fe); |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 4386 | } |
Willy Tarreau | 55ea757 | 2007-06-17 19:56:27 +0200 | [diff] [blame] | 4387 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 4388 | txn->req.msg_state = HTTP_MSG_ERROR; |
| 4389 | txn->status = 400; |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 4390 | stream_int_retnclose(&s->si[0], http_error_message(s, HTTP_ERR_400)); |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 4391 | |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 4392 | sess->fe->fe_counters.failed_req++; |
Willy Tarreau | fb0afa7 | 2015-04-03 14:46:27 +0200 | [diff] [blame] | 4393 | if (sess->listener->counters) |
| 4394 | sess->listener->counters->failed_req++; |
Willy Tarreau | 6e4261e | 2007-09-18 18:36:05 +0200 | [diff] [blame] | 4395 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 4396 | return_prx_cond: |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 4397 | if (!(s->flags & SF_ERR_MASK)) |
| 4398 | s->flags |= SF_ERR_PRXCOND; |
| 4399 | if (!(s->flags & SF_FINST_MASK)) |
| 4400 | s->flags |= SF_FINST_R; |
Willy Tarreau | f1221aa | 2006-12-17 22:14:12 +0100 | [diff] [blame] | 4401 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 4402 | req->analysers = 0; |
| 4403 | req->analyse_exp = TICK_ETERNITY; |
| 4404 | return 0; |
Thierry FOURNIER | bc4c1ac | 2015-02-25 13:36:14 +0100 | [diff] [blame] | 4405 | |
| 4406 | return_prx_yield: |
| 4407 | channel_dont_connect(req); |
| 4408 | return 0; |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 4409 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4410 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 4411 | /* This function performs all the processing enabled for the current request. |
| 4412 | * It returns 1 if the processing can continue on next analysers, or zero if it |
| 4413 | * needs more data, encounters an error, or wants to immediately abort the |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 4414 | * request. It relies on buffers flags, and updates s->req.analysers. |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 4415 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 4416 | 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] | 4417 | { |
Willy Tarreau | fb0afa7 | 2015-04-03 14:46:27 +0200 | [diff] [blame] | 4418 | struct session *sess = s->sess; |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 4419 | struct http_txn *txn = s->txn; |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 4420 | struct http_msg *msg = &txn->req; |
Willy Tarreau | ee335e6 | 2015-04-21 18:15:13 +0200 | [diff] [blame] | 4421 | struct connection *cli_conn = objt_conn(strm_sess(s)->origin); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4422 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 4423 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) { |
Willy Tarreau | 51aecc7 | 2009-07-12 09:47:04 +0200 | [diff] [blame] | 4424 | /* we need more data */ |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 4425 | channel_dont_connect(req); |
Willy Tarreau | 51aecc7 | 2009-07-12 09:47:04 +0200 | [diff] [blame] | 4426 | return 0; |
| 4427 | } |
| 4428 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 4429 | 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] | 4430 | now_ms, __FUNCTION__, |
| 4431 | s, |
| 4432 | req, |
| 4433 | req->rex, req->wex, |
| 4434 | req->flags, |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 4435 | req->buf->i, |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 4436 | req->analysers); |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 4437 | |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 4438 | if (sess->fe->comp || s->be->comp) |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 4439 | select_compression_request_header(s, req->buf); |
| 4440 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4441 | /* |
| 4442 | * Right now, we know that we have processed the entire headers |
| 4443 | * and that unwanted requests have been filtered out. We can do |
| 4444 | * whatever we want with the remaining request. Also, now we |
| 4445 | * may have separate values for ->fe, ->be. |
| 4446 | */ |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 4447 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4448 | /* |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 4449 | * If HTTP PROXY is set we simply get remote server address parsing |
| 4450 | * incoming request. Note that this requires that a connection is |
| 4451 | * allocated on the server side. |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4452 | */ |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 4453 | 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] | 4454 | struct connection *conn; |
Willy Tarreau | e8df1e1 | 2013-12-16 14:30:55 +0100 | [diff] [blame] | 4455 | char *path; |
Willy Tarreau | 32e3c6a | 2013-10-11 19:34:20 +0200 | [diff] [blame] | 4456 | |
Willy Tarreau | 9471b8c | 2013-12-15 13:31:35 +0100 | [diff] [blame] | 4457 | /* Note that for now we don't reuse existing proxy connections */ |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 4458 | if (unlikely((conn = si_alloc_conn(&s->si[1], 0)) == NULL)) { |
Willy Tarreau | 32e3c6a | 2013-10-11 19:34:20 +0200 | [diff] [blame] | 4459 | txn->req.msg_state = HTTP_MSG_ERROR; |
| 4460 | txn->status = 500; |
| 4461 | req->analysers = 0; |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 4462 | stream_int_retnclose(&s->si[0], http_error_message(s, HTTP_ERR_500)); |
Willy Tarreau | 32e3c6a | 2013-10-11 19:34:20 +0200 | [diff] [blame] | 4463 | |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 4464 | if (!(s->flags & SF_ERR_MASK)) |
| 4465 | s->flags |= SF_ERR_RESOURCE; |
| 4466 | if (!(s->flags & SF_FINST_MASK)) |
| 4467 | s->flags |= SF_FINST_R; |
Willy Tarreau | 32e3c6a | 2013-10-11 19:34:20 +0200 | [diff] [blame] | 4468 | |
| 4469 | return 0; |
| 4470 | } |
Willy Tarreau | e8df1e1 | 2013-12-16 14:30:55 +0100 | [diff] [blame] | 4471 | |
| 4472 | path = http_get_path(txn); |
| 4473 | url2sa(req->buf->p + msg->sl.rq.u, |
| 4474 | 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] | 4475 | &conn->addr.to, NULL); |
Willy Tarreau | e8df1e1 | 2013-12-16 14:30:55 +0100 | [diff] [blame] | 4476 | /* if the path was found, we have to remove everything between |
| 4477 | * req->buf->p + msg->sl.rq.u and path (excluded). If it was not |
| 4478 | * found, we need to replace from req->buf->p + msg->sl.rq.u for |
| 4479 | * u_l characters by a single "/". |
| 4480 | */ |
| 4481 | if (path) { |
| 4482 | char *cur_ptr = req->buf->p; |
| 4483 | char *cur_end = cur_ptr + txn->req.sl.rq.l; |
| 4484 | int delta; |
| 4485 | |
| 4486 | delta = buffer_replace2(req->buf, req->buf->p + msg->sl.rq.u, path, NULL, 0); |
| 4487 | http_msg_move_end(&txn->req, delta); |
| 4488 | cur_end += delta; |
| 4489 | if (http_parse_reqline(&txn->req, HTTP_MSG_RQMETH, cur_ptr, cur_end + 1, NULL, NULL) == NULL) |
| 4490 | goto return_bad_req; |
| 4491 | } |
| 4492 | else { |
| 4493 | char *cur_ptr = req->buf->p; |
| 4494 | char *cur_end = cur_ptr + txn->req.sl.rq.l; |
| 4495 | int delta; |
| 4496 | |
| 4497 | delta = buffer_replace2(req->buf, req->buf->p + msg->sl.rq.u, |
| 4498 | req->buf->p + msg->sl.rq.u + msg->sl.rq.u_l, "/", 1); |
| 4499 | http_msg_move_end(&txn->req, delta); |
| 4500 | cur_end += delta; |
| 4501 | if (http_parse_reqline(&txn->req, HTTP_MSG_RQMETH, cur_ptr, cur_end + 1, NULL, NULL) == NULL) |
| 4502 | goto return_bad_req; |
| 4503 | } |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4504 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4505 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4506 | /* |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 4507 | * 7: Now we can work with the cookies. |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4508 | * Note that doing so might move headers in the request, but |
| 4509 | * the fields will stay coherent and the URI will not move. |
| 4510 | * This should only be performed in the backend. |
| 4511 | */ |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 4512 | if ((s->be->cookie_name || s->be->appsession_name || sess->fe->capture_name) |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4513 | && !(txn->flags & (TX_CLDENY|TX_CLTARPIT))) |
| 4514 | manage_client_side_cookies(s, req); |
Willy Tarreau | 7ac51f6 | 2007-03-25 16:00:04 +0200 | [diff] [blame] | 4515 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4516 | /* |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 4517 | * 8: the appsession cookie was looked up very early in 1.2, |
| 4518 | * so let's do the same now. |
| 4519 | */ |
| 4520 | |
Cyril Bonté | 47fdd8e | 2010-04-25 00:00:51 +0200 | [diff] [blame] | 4521 | /* It needs to look into the URI unless persistence must be ignored */ |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 4522 | if ((txn->sessid == NULL) && s->be->appsession_name && !(s->flags & SF_IGNORE_PRST)) { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 4523 | get_srv_from_appsession(s, req->buf->p + msg->sl.rq.u, msg->sl.rq.u_l); |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 4524 | } |
| 4525 | |
William Lallemand | a73203e | 2012-03-12 12:48:57 +0100 | [diff] [blame] | 4526 | /* add unique-id if "header-unique-id" is specified */ |
| 4527 | |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 4528 | if (!LIST_ISEMPTY(&sess->fe->format_unique_id)) { |
William Lallemand | 5b7ea3a | 2013-08-28 15:44:19 +0200 | [diff] [blame] | 4529 | if ((s->unique_id = pool_alloc2(pool2_uniqueid)) == NULL) |
| 4530 | goto return_bad_req; |
| 4531 | s->unique_id[0] = '\0'; |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 4532 | 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] | 4533 | } |
William Lallemand | a73203e | 2012-03-12 12:48:57 +0100 | [diff] [blame] | 4534 | |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 4535 | if (sess->fe->header_unique_id && s->unique_id) { |
| 4536 | 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] | 4537 | if (trash.len < 0) |
William Lallemand | a73203e | 2012-03-12 12:48:57 +0100 | [diff] [blame] | 4538 | goto return_bad_req; |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 4539 | 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] | 4540 | goto return_bad_req; |
| 4541 | } |
| 4542 | |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 4543 | /* |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4544 | * 9: add X-Forwarded-For if either the frontend or the backend |
| 4545 | * asks for it. |
| 4546 | */ |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 4547 | if ((sess->fe->options | s->be->options) & PR_O_FWDFOR) { |
Willy Tarreau | 87cf514 | 2011-08-19 22:57:24 +0200 | [diff] [blame] | 4548 | struct hdr_ctx ctx = { .idx = 0 }; |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 4549 | if (!((sess->fe->options | s->be->options) & PR_O_FF_ALWAYS) && |
| 4550 | http_find_header2(s->be->fwdfor_hdr_len ? s->be->fwdfor_hdr_name : sess->fe->fwdfor_hdr_name, |
| 4551 | 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] | 4552 | req->buf->p, &txn->hdr_idx, &ctx)) { |
Willy Tarreau | 87cf514 | 2011-08-19 22:57:24 +0200 | [diff] [blame] | 4553 | /* The header is set to be added only if none is present |
| 4554 | * and we found it, so don't do anything. |
| 4555 | */ |
| 4556 | } |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 4557 | else if (cli_conn && cli_conn->addr.from.ss_family == AF_INET) { |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4558 | /* Add an X-Forwarded-For header unless the source IP is |
| 4559 | * in the 'except' network range. |
| 4560 | */ |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 4561 | if ((!sess->fe->except_mask.s_addr || |
| 4562 | (((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr.s_addr & sess->fe->except_mask.s_addr) |
| 4563 | != sess->fe->except_net.s_addr) && |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4564 | (!s->be->except_mask.s_addr || |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 4565 | (((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] | 4566 | != s->be->except_net.s_addr)) { |
Willy Tarreau | 2a32428 | 2006-12-05 00:05:46 +0100 | [diff] [blame] | 4567 | int len; |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4568 | unsigned char *pn; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 4569 | pn = (unsigned char *)&((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr; |
Ross West | af72a1d | 2008-08-03 10:51:45 +0200 | [diff] [blame] | 4570 | |
| 4571 | /* Note: we rely on the backend to get the header name to be used for |
| 4572 | * x-forwarded-for, because the header is really meant for the backends. |
| 4573 | * However, if the backend did not specify any option, we have to rely |
| 4574 | * on the frontend's header name. |
| 4575 | */ |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4576 | if (s->be->fwdfor_hdr_len) { |
| 4577 | len = s->be->fwdfor_hdr_len; |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 4578 | memcpy(trash.str, s->be->fwdfor_hdr_name, len); |
Ross West | af72a1d | 2008-08-03 10:51:45 +0200 | [diff] [blame] | 4579 | } else { |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 4580 | len = sess->fe->fwdfor_hdr_len; |
| 4581 | memcpy(trash.str, sess->fe->fwdfor_hdr_name, len); |
Willy Tarreau | b86db34 | 2009-11-30 11:50:16 +0100 | [diff] [blame] | 4582 | } |
Willy Tarreau | e9187f8 | 2014-04-14 15:27:14 +0200 | [diff] [blame] | 4583 | 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] | 4584 | |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 4585 | 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] | 4586 | goto return_bad_req; |
Willy Tarreau | 2a32428 | 2006-12-05 00:05:46 +0100 | [diff] [blame] | 4587 | } |
| 4588 | } |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 4589 | else if (cli_conn && cli_conn->addr.from.ss_family == AF_INET6) { |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4590 | /* FIXME: for the sake of completeness, we should also support |
| 4591 | * 'except' here, although it is mostly useless in this case. |
matt.farnsworth@nokia.com | 1c2ab96 | 2008-04-14 20:47:37 +0200 | [diff] [blame] | 4592 | */ |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4593 | int len; |
| 4594 | char pn[INET6_ADDRSTRLEN]; |
| 4595 | inet_ntop(AF_INET6, |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 4596 | (const void *)&((struct sockaddr_in6 *)(&cli_conn->addr.from))->sin6_addr, |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4597 | pn, sizeof(pn)); |
matt.farnsworth@nokia.com | 1c2ab96 | 2008-04-14 20:47:37 +0200 | [diff] [blame] | 4598 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4599 | /* Note: we rely on the backend to get the header name to be used for |
| 4600 | * x-forwarded-for, because the header is really meant for the backends. |
| 4601 | * However, if the backend did not specify any option, we have to rely |
| 4602 | * on the frontend's header name. |
matt.farnsworth@nokia.com | 1c2ab96 | 2008-04-14 20:47:37 +0200 | [diff] [blame] | 4603 | */ |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4604 | if (s->be->fwdfor_hdr_len) { |
| 4605 | len = s->be->fwdfor_hdr_len; |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 4606 | memcpy(trash.str, s->be->fwdfor_hdr_name, len); |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4607 | } else { |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 4608 | len = sess->fe->fwdfor_hdr_len; |
| 4609 | memcpy(trash.str, sess->fe->fwdfor_hdr_name, len); |
matt.farnsworth@nokia.com | 1c2ab96 | 2008-04-14 20:47:37 +0200 | [diff] [blame] | 4610 | } |
Willy Tarreau | e9187f8 | 2014-04-14 15:27:14 +0200 | [diff] [blame] | 4611 | len += snprintf(trash.str + len, trash.size - len, ": %s", pn); |
Willy Tarreau | adfb856 | 2008-08-11 15:24:42 +0200 | [diff] [blame] | 4612 | |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 4613 | 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] | 4614 | goto return_bad_req; |
| 4615 | } |
| 4616 | } |
| 4617 | |
| 4618 | /* |
Maik Broemme | 2850cb4 | 2009-04-17 18:53:21 +0200 | [diff] [blame] | 4619 | * 10: add X-Original-To if either the frontend or the backend |
| 4620 | * asks for it. |
| 4621 | */ |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 4622 | if ((sess->fe->options | s->be->options) & PR_O_ORGTO) { |
Maik Broemme | 2850cb4 | 2009-04-17 18:53:21 +0200 | [diff] [blame] | 4623 | |
| 4624 | /* FIXME: don't know if IPv6 can handle that case too. */ |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 4625 | if (cli_conn && cli_conn->addr.from.ss_family == AF_INET) { |
Maik Broemme | 2850cb4 | 2009-04-17 18:53:21 +0200 | [diff] [blame] | 4626 | /* Add an X-Original-To header unless the destination IP is |
| 4627 | * in the 'except' network range. |
| 4628 | */ |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 4629 | conn_get_to_addr(cli_conn); |
Maik Broemme | 2850cb4 | 2009-04-17 18:53:21 +0200 | [diff] [blame] | 4630 | |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 4631 | if (cli_conn->addr.to.ss_family == AF_INET && |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 4632 | ((!sess->fe->except_mask_to.s_addr || |
| 4633 | (((struct sockaddr_in *)&cli_conn->addr.to)->sin_addr.s_addr & sess->fe->except_mask_to.s_addr) |
| 4634 | != sess->fe->except_to.s_addr) && |
Emeric Brun | 5bd86a8 | 2010-10-22 17:23:04 +0200 | [diff] [blame] | 4635 | (!s->be->except_mask_to.s_addr || |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 4636 | (((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] | 4637 | != s->be->except_to.s_addr))) { |
Maik Broemme | 2850cb4 | 2009-04-17 18:53:21 +0200 | [diff] [blame] | 4638 | int len; |
| 4639 | unsigned char *pn; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 4640 | pn = (unsigned char *)&((struct sockaddr_in *)&cli_conn->addr.to)->sin_addr; |
Maik Broemme | 2850cb4 | 2009-04-17 18:53:21 +0200 | [diff] [blame] | 4641 | |
| 4642 | /* Note: we rely on the backend to get the header name to be used for |
| 4643 | * x-original-to, because the header is really meant for the backends. |
| 4644 | * However, if the backend did not specify any option, we have to rely |
| 4645 | * on the frontend's header name. |
| 4646 | */ |
| 4647 | if (s->be->orgto_hdr_len) { |
| 4648 | len = s->be->orgto_hdr_len; |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 4649 | memcpy(trash.str, s->be->orgto_hdr_name, len); |
Maik Broemme | 2850cb4 | 2009-04-17 18:53:21 +0200 | [diff] [blame] | 4650 | } else { |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 4651 | len = sess->fe->orgto_hdr_len; |
| 4652 | memcpy(trash.str, sess->fe->orgto_hdr_name, len); |
Willy Tarreau | b86db34 | 2009-11-30 11:50:16 +0100 | [diff] [blame] | 4653 | } |
Willy Tarreau | e9187f8 | 2014-04-14 15:27:14 +0200 | [diff] [blame] | 4654 | 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] | 4655 | |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 4656 | 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] | 4657 | goto return_bad_req; |
| 4658 | } |
| 4659 | } |
| 4660 | } |
| 4661 | |
Willy Tarreau | 50fc777 | 2012-11-11 22:19:57 +0100 | [diff] [blame] | 4662 | /* 11: add "Connection: close" or "Connection: keep-alive" if needed and not yet set. |
| 4663 | * If an "Upgrade" token is found, the header is left untouched in order not to have |
| 4664 | * to deal with some servers bugs : some of them fail an Upgrade if anything but |
| 4665 | * "Upgrade" is present in the Connection header. |
| 4666 | */ |
| 4667 | if (!(txn->flags & TX_HDR_CONN_UPG) && |
| 4668 | (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) || |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 4669 | ((sess->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL || |
Willy Tarreau | 02bce8b | 2014-01-30 00:15:28 +0100 | [diff] [blame] | 4670 | (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL))) { |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 4671 | unsigned int want_flags = 0; |
| 4672 | |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 4673 | if (msg->flags & HTTP_MSGF_VER_11) { |
Willy Tarreau | 22a9534 | 2010-09-29 14:31:41 +0200 | [diff] [blame] | 4674 | if (((txn->flags & TX_CON_WANT_MSK) >= TX_CON_WANT_SCL || |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 4675 | ((sess->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL || |
Willy Tarreau | 02bce8b | 2014-01-30 00:15:28 +0100 | [diff] [blame] | 4676 | (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL)) && |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 4677 | !((sess->fe->options2|s->be->options2) & PR_O2_FAKE_KA)) |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 4678 | want_flags |= TX_CON_CLO_SET; |
| 4679 | } else { |
Willy Tarreau | 22a9534 | 2010-09-29 14:31:41 +0200 | [diff] [blame] | 4680 | if (((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL && |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 4681 | ((sess->fe->options & PR_O_HTTP_MODE) != PR_O_HTTP_PCL && |
Willy Tarreau | 02bce8b | 2014-01-30 00:15:28 +0100 | [diff] [blame] | 4682 | (s->be->options & PR_O_HTTP_MODE) != PR_O_HTTP_PCL)) || |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 4683 | ((sess->fe->options2|s->be->options2) & PR_O2_FAKE_KA)) |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 4684 | want_flags |= TX_CON_KAL_SET; |
| 4685 | } |
| 4686 | |
| 4687 | 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] | 4688 | http_change_connection_header(txn, msg, want_flags); |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4689 | } |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 4690 | |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 4691 | |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 4692 | /* If we have no server assigned yet and we're balancing on url_param |
| 4693 | * with a POST request, we may be interested in checking the body for |
| 4694 | * that parameter. This will be done in another analyser. |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4695 | */ |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 4696 | if (!(s->flags & (SF_ASSIGNED|SF_DIRECT)) && |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 4697 | s->txn->meth == HTTP_METH_POST && s->be->url_param_name != NULL && |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 4698 | (msg->flags & (HTTP_MSGF_CNT_LEN|HTTP_MSGF_TE_CHNK))) { |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 4699 | channel_dont_connect(req); |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 4700 | req->analysers |= AN_REQ_HTTP_BODY; |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4701 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 4702 | |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 4703 | if (msg->flags & HTTP_MSGF_XFER_LEN) { |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4704 | req->analysers |= AN_REQ_HTTP_XFER_BODY; |
Willy Tarreau | 5e20552 | 2011-12-17 16:34:27 +0100 | [diff] [blame] | 4705 | #ifdef TCP_QUICKACK |
| 4706 | /* We expect some data from the client. Unless we know for sure |
| 4707 | * we already have a full request, we have to re-enable quick-ack |
| 4708 | * in case we previously disabled it, otherwise we might cause |
| 4709 | * the client to delay further data. |
| 4710 | */ |
Willy Tarreau | fb0afa7 | 2015-04-03 14:46:27 +0200 | [diff] [blame] | 4711 | if ((sess->listener->options & LI_O_NOQUICKACK) && |
Willy Tarreau | 3c72872 | 2014-01-23 13:50:42 +0100 | [diff] [blame] | 4712 | cli_conn && conn_ctrl_ready(cli_conn) && |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 4713 | ((msg->flags & HTTP_MSGF_TE_CHNK) || |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 4714 | (msg->body_len > req->buf->i - txn->req.eoh - 2))) |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 4715 | 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] | 4716 | #endif |
| 4717 | } |
Willy Tarreau | 0394594 | 2009-12-22 16:50:27 +0100 | [diff] [blame] | 4718 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4719 | /************************************************************* |
| 4720 | * OK, that's finished for the headers. We have done what we * |
| 4721 | * could. Let's switch to the DATA state. * |
| 4722 | ************************************************************/ |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 4723 | req->analyse_exp = TICK_ETERNITY; |
| 4724 | req->analysers &= ~an_bit; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 4725 | |
Willy Tarreau | 7bb68ab | 2012-05-13 14:48:59 +0200 | [diff] [blame] | 4726 | /* if the server closes the connection, we want to immediately react |
| 4727 | * and close the socket to save packets and syscalls. |
| 4728 | */ |
Willy Tarreau | 40f151a | 2012-12-20 12:10:09 +0100 | [diff] [blame] | 4729 | if (!(req->analysers & AN_REQ_HTTP_XFER_BODY)) |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 4730 | s->si[1].flags |= SI_FL_NOHALF; |
Willy Tarreau | 7bb68ab | 2012-05-13 14:48:59 +0200 | [diff] [blame] | 4731 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4732 | s->logs.tv_request = now; |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4733 | /* OK let's go on with the BODY now */ |
| 4734 | return 1; |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 4735 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4736 | return_bad_req: /* let's centralize all bad requests */ |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 4737 | if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) { |
Willy Tarreau | f073a83 | 2009-03-01 23:21:47 +0100 | [diff] [blame] | 4738 | /* we detected a parsing error. We want to archive this request |
| 4739 | * in the dedicated proxy area for later troubleshooting. |
| 4740 | */ |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 4741 | http_capture_bad_message(&sess->fe->invalid_req, s, msg, msg->msg_state, sess->fe); |
Willy Tarreau | f073a83 | 2009-03-01 23:21:47 +0100 | [diff] [blame] | 4742 | } |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 4743 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4744 | txn->req.msg_state = HTTP_MSG_ERROR; |
| 4745 | txn->status = 400; |
| 4746 | req->analysers = 0; |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 4747 | stream_int_retnclose(&s->si[0], http_error_message(s, HTTP_ERR_400)); |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 4748 | |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 4749 | sess->fe->fe_counters.failed_req++; |
Willy Tarreau | fb0afa7 | 2015-04-03 14:46:27 +0200 | [diff] [blame] | 4750 | if (sess->listener->counters) |
| 4751 | sess->listener->counters->failed_req++; |
Willy Tarreau | adfb856 | 2008-08-11 15:24:42 +0200 | [diff] [blame] | 4752 | |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 4753 | if (!(s->flags & SF_ERR_MASK)) |
| 4754 | s->flags |= SF_ERR_PRXCOND; |
| 4755 | if (!(s->flags & SF_FINST_MASK)) |
| 4756 | s->flags |= SF_FINST_R; |
Willy Tarreau | dafde43 | 2008-08-17 01:00:46 +0200 | [diff] [blame] | 4757 | return 0; |
Willy Tarreau | c65a3ba | 2008-08-11 23:42:50 +0200 | [diff] [blame] | 4758 | } |
Willy Tarreau | adfb856 | 2008-08-11 15:24:42 +0200 | [diff] [blame] | 4759 | |
Willy Tarreau | 60b85b0 | 2008-11-30 23:28:40 +0100 | [diff] [blame] | 4760 | /* This function is an analyser which processes the HTTP tarpit. It always |
| 4761 | * returns zero, at the beginning because it prevents any other processing |
| 4762 | * from occurring, and at the end because it terminates the request. |
| 4763 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 4764 | 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] | 4765 | { |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 4766 | struct http_txn *txn = s->txn; |
Willy Tarreau | 60b85b0 | 2008-11-30 23:28:40 +0100 | [diff] [blame] | 4767 | |
| 4768 | /* This connection is being tarpitted. The CLIENT side has |
| 4769 | * already set the connect expiration date to the right |
| 4770 | * timeout. We just have to check that the client is still |
| 4771 | * there and that the timeout has not expired. |
| 4772 | */ |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 4773 | channel_dont_connect(req); |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 4774 | if ((req->flags & (CF_SHUTR|CF_READ_ERROR)) == 0 && |
Willy Tarreau | 60b85b0 | 2008-11-30 23:28:40 +0100 | [diff] [blame] | 4775 | !tick_is_expired(req->analyse_exp, now_ms)) |
| 4776 | return 0; |
| 4777 | |
| 4778 | /* We will set the queue timer to the time spent, just for |
| 4779 | * logging purposes. We fake a 500 server error, so that the |
| 4780 | * attacker will not suspect his connection has been tarpitted. |
| 4781 | * It will not cause trouble to the logs because we can exclude |
| 4782 | * the tarpitted connections by filtering on the 'PT' status flags. |
| 4783 | */ |
Willy Tarreau | 60b85b0 | 2008-11-30 23:28:40 +0100 | [diff] [blame] | 4784 | s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now); |
| 4785 | |
| 4786 | txn->status = 500; |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 4787 | if (!(req->flags & CF_READ_ERROR)) |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 4788 | stream_int_retnclose(&s->si[0], http_error_message(s, HTTP_ERR_500)); |
Willy Tarreau | 60b85b0 | 2008-11-30 23:28:40 +0100 | [diff] [blame] | 4789 | |
| 4790 | req->analysers = 0; |
| 4791 | req->analyse_exp = TICK_ETERNITY; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 4792 | |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 4793 | if (!(s->flags & SF_ERR_MASK)) |
| 4794 | s->flags |= SF_ERR_PRXCOND; |
| 4795 | if (!(s->flags & SF_FINST_MASK)) |
| 4796 | s->flags |= SF_FINST_T; |
Willy Tarreau | 60b85b0 | 2008-11-30 23:28:40 +0100 | [diff] [blame] | 4797 | return 0; |
| 4798 | } |
| 4799 | |
Willy Tarreau | 5a8f947 | 2014-04-10 11:16:06 +0200 | [diff] [blame] | 4800 | /* This function is an analyser which waits for the HTTP request body. It waits |
| 4801 | * for either the buffer to be full, or the full advertised contents to have |
| 4802 | * reached the buffer. It must only be called after the standard HTTP request |
| 4803 | * processing has occurred, because it expects the request to be parsed and will |
| 4804 | * look for the Expect header. It may send a 100-Continue interim response. It |
| 4805 | * takes in input any state starting from HTTP_MSG_BODY and leaves with one of |
| 4806 | * HTTP_MSG_CHK_SIZE, HTTP_MSG_DATA or HTTP_MSG_TRAILERS. It returns zero if it |
| 4807 | * 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] | 4808 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 4809 | 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] | 4810 | { |
Willy Tarreau | fb0afa7 | 2015-04-03 14:46:27 +0200 | [diff] [blame] | 4811 | struct session *sess = s->sess; |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 4812 | struct http_txn *txn = s->txn; |
| 4813 | struct http_msg *msg = &s->txn->req; |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 4814 | |
| 4815 | /* We have to parse the HTTP request body to find any required data. |
| 4816 | * "balance url_param check_post" should have been the only way to get |
| 4817 | * into this. We were brought here after HTTP header analysis, so all |
| 4818 | * related structures are ready. |
| 4819 | */ |
| 4820 | |
Willy Tarreau | 890988f | 2014-04-10 11:59:33 +0200 | [diff] [blame] | 4821 | if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) { |
| 4822 | /* This is the first call */ |
| 4823 | if (msg->msg_state < HTTP_MSG_BODY) |
| 4824 | goto missing_data; |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 4825 | |
Willy Tarreau | 890988f | 2014-04-10 11:59:33 +0200 | [diff] [blame] | 4826 | if (msg->msg_state < HTTP_MSG_100_SENT) { |
| 4827 | /* If we have HTTP/1.1 and Expect: 100-continue, then we must |
| 4828 | * send an HTTP/1.1 100 Continue intermediate response. |
| 4829 | */ |
| 4830 | if (msg->flags & HTTP_MSGF_VER_11) { |
| 4831 | struct hdr_ctx ctx; |
| 4832 | ctx.idx = 0; |
| 4833 | /* Expect is allowed in 1.1, look for it */ |
| 4834 | if (http_find_header2("Expect", 6, req->buf->p, &txn->hdr_idx, &ctx) && |
| 4835 | 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] | 4836 | bo_inject(&s->res, http_100_chunk.str, http_100_chunk.len); |
Willy Tarreau | 890988f | 2014-04-10 11:59:33 +0200 | [diff] [blame] | 4837 | } |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 4838 | } |
Willy Tarreau | 890988f | 2014-04-10 11:59:33 +0200 | [diff] [blame] | 4839 | msg->msg_state = HTTP_MSG_100_SENT; |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 4840 | } |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 4841 | |
Willy Tarreau | fa4a03c | 2012-03-09 21:28:54 +0100 | [diff] [blame] | 4842 | /* 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] | 4843 | * req->buf->p still points to the beginning of the message. We |
| 4844 | * must save the body in msg->next because it survives buffer |
| 4845 | * re-alignments. |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4846 | */ |
Willy Tarreau | ea1175a | 2012-03-05 15:52:30 +0100 | [diff] [blame] | 4847 | msg->next = msg->sov; |
Willy Tarreau | a458b67 | 2012-03-05 11:17:50 +0100 | [diff] [blame] | 4848 | |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 4849 | if (msg->flags & HTTP_MSGF_TE_CHNK) |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 4850 | msg->msg_state = HTTP_MSG_CHUNK_SIZE; |
| 4851 | else |
| 4852 | msg->msg_state = HTTP_MSG_DATA; |
| 4853 | } |
| 4854 | |
Willy Tarreau | 890988f | 2014-04-10 11:59:33 +0200 | [diff] [blame] | 4855 | if (!(msg->flags & HTTP_MSGF_TE_CHNK)) { |
| 4856 | /* 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] | 4857 | if (http_body_bytes(msg) < msg->body_len) |
Willy Tarreau | 890988f | 2014-04-10 11:59:33 +0200 | [diff] [blame] | 4858 | goto missing_data; |
| 4859 | |
| 4860 | /* OK we have everything we need now */ |
| 4861 | goto http_end; |
| 4862 | } |
| 4863 | |
| 4864 | /* OK here we're parsing a chunked-encoded message */ |
| 4865 | |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 4866 | if (msg->msg_state == HTTP_MSG_CHUNK_SIZE) { |
Willy Tarreau | 124d991 | 2011-03-01 20:30:48 +0100 | [diff] [blame] | 4867 | /* read the chunk size and assign it to ->chunk_len, then |
Willy Tarreau | a458b67 | 2012-03-05 11:17:50 +0100 | [diff] [blame] | 4868 | * 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] | 4869 | * TRAILERS state. |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 4870 | */ |
Willy Tarreau | 4baf44b | 2012-03-09 14:10:20 +0100 | [diff] [blame] | 4871 | int ret = http_parse_chunk_size(msg); |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 4872 | |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 4873 | if (!ret) |
| 4874 | goto missing_data; |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 4875 | else if (ret < 0) { |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 4876 | stream_inc_http_err_ctr(s); |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 4877 | goto return_bad_req; |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 4878 | } |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 4879 | } |
| 4880 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4881 | /* Now we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state. |
Willy Tarreau | e115b49 | 2015-05-01 23:05:14 +0200 | [diff] [blame] | 4882 | * We have the first data byte is in msg->sov + msg->sol. We're waiting |
| 4883 | * for at least a whole chunk or the whole content length bytes after |
| 4884 | * msg->sov + msg->sol. |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 4885 | */ |
Willy Tarreau | 890988f | 2014-04-10 11:59:33 +0200 | [diff] [blame] | 4886 | if (msg->msg_state == HTTP_MSG_TRAILERS) |
| 4887 | goto http_end; |
| 4888 | |
Willy Tarreau | e115b49 | 2015-05-01 23:05:14 +0200 | [diff] [blame] | 4889 | 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] | 4890 | goto http_end; |
| 4891 | |
| 4892 | missing_data: |
Willy Tarreau | 31a1995 | 2014-04-10 11:50:37 +0200 | [diff] [blame] | 4893 | /* we get here if we need to wait for more data. If the buffer is full, |
| 4894 | * we have the maximum we can expect. |
| 4895 | */ |
| 4896 | if (buffer_full(req->buf, global.tune.maxrewrite)) |
| 4897 | goto http_end; |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 4898 | |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 4899 | 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] | 4900 | txn->status = 408; |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 4901 | stream_int_retnclose(&s->si[0], http_error_message(s, HTTP_ERR_408)); |
Willy Tarreau | 79ebac6 | 2010-06-07 13:47:49 +0200 | [diff] [blame] | 4902 | |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 4903 | if (!(s->flags & SF_ERR_MASK)) |
| 4904 | s->flags |= SF_ERR_CLITO; |
| 4905 | if (!(s->flags & SF_FINST_MASK)) |
| 4906 | s->flags |= SF_FINST_D; |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 4907 | goto return_err_msg; |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 4908 | } |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 4909 | |
| 4910 | /* we get here if we need to wait for more data */ |
Willy Tarreau | 31a1995 | 2014-04-10 11:50:37 +0200 | [diff] [blame] | 4911 | if (!(req->flags & (CF_SHUTR | CF_READ_ERROR))) { |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 4912 | /* Not enough data. We'll re-use the http-request |
| 4913 | * timeout here. Ideally, we should set the timeout |
| 4914 | * relative to the accept() date. We just set the |
| 4915 | * request timeout once at the beginning of the |
| 4916 | * request. |
| 4917 | */ |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 4918 | channel_dont_connect(req); |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 4919 | if (!tick_isset(req->analyse_exp)) |
Willy Tarreau | cd7afc0 | 2009-07-12 10:03:17 +0200 | [diff] [blame] | 4920 | req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.httpreq); |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 4921 | return 0; |
| 4922 | } |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 4923 | |
| 4924 | http_end: |
| 4925 | /* The situation will not evolve, so let's give up on the analysis. */ |
| 4926 | s->logs.tv_request = now; /* update the request timer to reflect full request */ |
| 4927 | req->analysers &= ~an_bit; |
| 4928 | req->analyse_exp = TICK_ETERNITY; |
| 4929 | return 1; |
| 4930 | |
| 4931 | return_bad_req: /* let's centralize all bad requests */ |
| 4932 | txn->req.msg_state = HTTP_MSG_ERROR; |
| 4933 | txn->status = 400; |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 4934 | stream_int_retnclose(&s->si[0], http_error_message(s, HTTP_ERR_400)); |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 4935 | |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 4936 | if (!(s->flags & SF_ERR_MASK)) |
| 4937 | s->flags |= SF_ERR_PRXCOND; |
| 4938 | if (!(s->flags & SF_FINST_MASK)) |
| 4939 | s->flags |= SF_FINST_R; |
Willy Tarreau | 79ebac6 | 2010-06-07 13:47:49 +0200 | [diff] [blame] | 4940 | |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 4941 | return_err_msg: |
| 4942 | req->analysers = 0; |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 4943 | sess->fe->fe_counters.failed_req++; |
Willy Tarreau | fb0afa7 | 2015-04-03 14:46:27 +0200 | [diff] [blame] | 4944 | if (sess->listener->counters) |
| 4945 | sess->listener->counters->failed_req++; |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 4946 | return 0; |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 4947 | } |
| 4948 | |
Willy Tarreau | d1de8af | 2012-05-18 22:12:14 +0200 | [diff] [blame] | 4949 | /* send a server's name with an outgoing request over an established connection. |
| 4950 | * Note: this function is designed to be called once the request has been scheduled |
| 4951 | * for being forwarded. This is the reason why it rewinds the buffer before |
| 4952 | * proceeding. |
| 4953 | */ |
Willy Tarreau | 45c0d98 | 2012-03-09 12:11:57 +0100 | [diff] [blame] | 4954 | 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] | 4955 | |
| 4956 | struct hdr_ctx ctx; |
| 4957 | |
Mark Lamourine | c2247f0 | 2012-01-04 13:02:01 -0500 | [diff] [blame] | 4958 | char *hdr_name = be->server_id_hdr_name; |
| 4959 | int hdr_name_len = be->server_id_hdr_len; |
Willy Tarreau | 394db37 | 2012-10-12 22:40:39 +0200 | [diff] [blame] | 4960 | struct channel *chn = txn->req.chn; |
Mark Lamourine | c2247f0 | 2012-01-04 13:02:01 -0500 | [diff] [blame] | 4961 | char *hdr_val; |
Willy Tarreau | d1de8af | 2012-05-18 22:12:14 +0200 | [diff] [blame] | 4962 | unsigned int old_o, old_i; |
Mark Lamourine | c2247f0 | 2012-01-04 13:02:01 -0500 | [diff] [blame] | 4963 | |
William Lallemand | d9e9066 | 2012-01-30 17:27:17 +0100 | [diff] [blame] | 4964 | ctx.idx = 0; |
| 4965 | |
Willy Tarreau | 211cdec | 2014-04-17 20:18:08 +0200 | [diff] [blame] | 4966 | old_o = http_hdr_rewind(&txn->req); |
Willy Tarreau | d1de8af | 2012-05-18 22:12:14 +0200 | [diff] [blame] | 4967 | if (old_o) { |
| 4968 | /* The request was already skipped, let's restore it */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 4969 | b_rew(chn->buf, old_o); |
Willy Tarreau | 877e78d | 2013-04-07 18:48:08 +0200 | [diff] [blame] | 4970 | txn->req.next += old_o; |
| 4971 | txn->req.sov += old_o; |
Willy Tarreau | d1de8af | 2012-05-18 22:12:14 +0200 | [diff] [blame] | 4972 | } |
| 4973 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 4974 | old_i = chn->buf->i; |
| 4975 | 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] | 4976 | /* remove any existing values from the header */ |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 4977 | http_remove_header2(&txn->req, &txn->hdr_idx, &ctx); |
Mark Lamourine | c2247f0 | 2012-01-04 13:02:01 -0500 | [diff] [blame] | 4978 | } |
| 4979 | |
| 4980 | /* Add the new header requested with the server value */ |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 4981 | hdr_val = trash.str; |
Mark Lamourine | c2247f0 | 2012-01-04 13:02:01 -0500 | [diff] [blame] | 4982 | memcpy(hdr_val, hdr_name, hdr_name_len); |
| 4983 | hdr_val += hdr_name_len; |
| 4984 | *hdr_val++ = ':'; |
| 4985 | *hdr_val++ = ' '; |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 4986 | hdr_val += strlcpy2(hdr_val, srv_name, trash.str + trash.size - hdr_val); |
| 4987 | 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] | 4988 | |
Willy Tarreau | d1de8af | 2012-05-18 22:12:14 +0200 | [diff] [blame] | 4989 | if (old_o) { |
| 4990 | /* If this was a forwarded request, we must readjust the amount of |
| 4991 | * data to be forwarded in order to take into account the size |
Willy Tarreau | 877e78d | 2013-04-07 18:48:08 +0200 | [diff] [blame] | 4992 | * variations. Note that the current state is >= HTTP_MSG_BODY, |
| 4993 | * so we don't have to adjust ->sol. |
Willy Tarreau | d1de8af | 2012-05-18 22:12:14 +0200 | [diff] [blame] | 4994 | */ |
Willy Tarreau | 877e78d | 2013-04-07 18:48:08 +0200 | [diff] [blame] | 4995 | old_o += chn->buf->i - old_i; |
| 4996 | b_adv(chn->buf, old_o); |
| 4997 | txn->req.next -= old_o; |
| 4998 | txn->req.sov -= old_o; |
Willy Tarreau | d1de8af | 2012-05-18 22:12:14 +0200 | [diff] [blame] | 4999 | } |
| 5000 | |
Mark Lamourine | c2247f0 | 2012-01-04 13:02:01 -0500 | [diff] [blame] | 5001 | return 0; |
| 5002 | } |
| 5003 | |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5004 | /* Terminate current transaction and prepare a new one. This is very tricky |
| 5005 | * right now but it works. |
| 5006 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 5007 | void http_end_txn_clean_session(struct stream *s) |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5008 | { |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 5009 | int prev_status = s->txn->status; |
Willy Tarreau | d0d8da9 | 2015-04-04 02:10:38 +0200 | [diff] [blame] | 5010 | struct proxy *fe = strm_fe(s); |
Willy Tarreau | 068621e | 2013-12-23 15:11:25 +0100 | [diff] [blame] | 5011 | |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5012 | /* FIXME: We need a more portable way of releasing a backend's and a |
| 5013 | * server's connections. We need a safer way to reinitialize buffer |
| 5014 | * flags. We also need a more accurate method for computing per-request |
| 5015 | * data. |
| 5016 | */ |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5017 | |
Willy Tarreau | 4213a11 | 2013-12-15 10:25:42 +0100 | [diff] [blame] | 5018 | /* unless we're doing keep-alive, we want to quickly close the connection |
| 5019 | * to the server. |
| 5020 | */ |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 5021 | if (((s->txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) || |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 5022 | !si_conn_ready(&s->si[1])) { |
| 5023 | s->si[1].flags |= SI_FL_NOLINGER | SI_FL_NOHALF; |
| 5024 | si_shutr(&s->si[1]); |
| 5025 | si_shutw(&s->si[1]); |
Willy Tarreau | 4213a11 | 2013-12-15 10:25:42 +0100 | [diff] [blame] | 5026 | } |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5027 | |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 5028 | if (s->flags & SF_BE_ASSIGNED) { |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5029 | s->be->beconn--; |
Willy Tarreau | 2d5cd47 | 2012-03-01 23:34:37 +0100 | [diff] [blame] | 5030 | if (unlikely(s->srv_conn)) |
| 5031 | sess_change_server(s, NULL); |
| 5032 | } |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5033 | |
| 5034 | s->logs.t_close = tv_ms_elapsed(&s->logs.tv_accept, &now); |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 5035 | stream_process_counters(s); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5036 | |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 5037 | if (s->txn->status) { |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5038 | int n; |
| 5039 | |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 5040 | n = s->txn->status / 100; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5041 | if (n < 1 || n > 5) |
| 5042 | n = 0; |
| 5043 | |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 5044 | if (fe->mode == PR_MODE_HTTP) { |
| 5045 | fe->fe_counters.p.http.rsp[n]++; |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 5046 | if (s->comp_algo && (s->flags & SF_COMP_READY)) |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 5047 | fe->fe_counters.p.http.comp_rsp++; |
Willy Tarreau | 5e16cbc | 2012-11-24 14:54:13 +0100 | [diff] [blame] | 5048 | } |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 5049 | if ((s->flags & SF_BE_ASSIGNED) && |
Willy Tarreau | 5e16cbc | 2012-11-24 14:54:13 +0100 | [diff] [blame] | 5050 | (s->be->mode == PR_MODE_HTTP)) { |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 5051 | s->be->be_counters.p.http.rsp[n]++; |
Willy Tarreau | 5e16cbc | 2012-11-24 14:54:13 +0100 | [diff] [blame] | 5052 | s->be->be_counters.p.http.cum_req++; |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 5053 | if (s->comp_algo && (s->flags & SF_COMP_READY)) |
Willy Tarreau | 5e16cbc | 2012-11-24 14:54:13 +0100 | [diff] [blame] | 5054 | s->be->be_counters.p.http.comp_rsp++; |
| 5055 | } |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5056 | } |
| 5057 | |
| 5058 | /* don't count other requests' data */ |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 5059 | s->logs.bytes_in -= s->req.buf->i; |
| 5060 | s->logs.bytes_out -= s->res.buf->i; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5061 | |
| 5062 | /* let's do a final log if we need it */ |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 5063 | if (!LIST_ISEMPTY(&fe->logformat) && s->logs.logwait && |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 5064 | !(s->flags & SF_MONITOR) && |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 5065 | (!(fe->options & PR_O_NULLNOLOG) || s->req.total)) { |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5066 | s->do_log(s); |
| 5067 | } |
| 5068 | |
Willy Tarreau | d713bcc | 2014-06-25 15:36:04 +0200 | [diff] [blame] | 5069 | /* stop tracking content-based counters */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 5070 | stream_stop_content_counters(s); |
| 5071 | stream_update_time_stats(s); |
Willy Tarreau | 4bfc580 | 2014-06-17 12:19:18 +0200 | [diff] [blame] | 5072 | |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5073 | s->logs.accept_date = date; /* user-visible date for logging */ |
| 5074 | s->logs.tv_accept = now; /* corrected date for internal use */ |
| 5075 | tv_zero(&s->logs.tv_request); |
| 5076 | s->logs.t_queue = -1; |
| 5077 | s->logs.t_connect = -1; |
| 5078 | s->logs.t_data = -1; |
| 5079 | s->logs.t_close = 0; |
| 5080 | s->logs.prx_queue_size = 0; /* we get the number of pending conns before us */ |
| 5081 | s->logs.srv_queue_size = 0; /* we will get this number soon */ |
| 5082 | |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 5083 | s->logs.bytes_in = s->req.total = s->req.buf->i; |
| 5084 | s->logs.bytes_out = s->res.total = s->res.buf->i; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5085 | |
| 5086 | if (s->pend_pos) |
| 5087 | pendconn_free(s->pend_pos); |
| 5088 | |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 5089 | if (objt_server(s->target)) { |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 5090 | if (s->flags & SF_CURR_SESS) { |
| 5091 | s->flags &= ~SF_CURR_SESS; |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 5092 | objt_server(s->target)->cur_sess--; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5093 | } |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 5094 | if (may_dequeue_tasks(objt_server(s->target), s->be)) |
| 5095 | process_srv_queue(objt_server(s->target)); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5096 | } |
| 5097 | |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 5098 | s->target = NULL; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5099 | |
Willy Tarreau | 4213a11 | 2013-12-15 10:25:42 +0100 | [diff] [blame] | 5100 | /* only release our endpoint if we don't intend to reuse the |
| 5101 | * connection. |
| 5102 | */ |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 5103 | if (((s->txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) || |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 5104 | !si_conn_ready(&s->si[1])) { |
| 5105 | si_release_endpoint(&s->si[1]); |
Willy Tarreau | 4213a11 | 2013-12-15 10:25:42 +0100 | [diff] [blame] | 5106 | } |
| 5107 | |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 5108 | s->si[1].state = s->si[1].prev_state = SI_ST_INI; |
| 5109 | s->si[1].err_type = SI_ET_NONE; |
| 5110 | s->si[1].conn_retries = 0; /* used for logging too */ |
| 5111 | s->si[1].exp = TICK_ETERNITY; |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 5112 | 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] | 5113 | 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); |
| 5114 | 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] | 5115 | s->flags &= ~(SF_DIRECT|SF_ASSIGNED|SF_ADDR_SET|SF_BE_ASSIGNED|SF_FORCE_PRST|SF_IGNORE_PRST); |
| 5116 | s->flags &= ~(SF_CURR_SESS|SF_REDIRECTABLE|SF_SRV_REUSED); |
| 5117 | s->flags &= ~(SF_ERR_MASK|SF_FINST_MASK|SF_REDISP); |
Willy Tarreau | 543db62 | 2012-11-15 16:41:22 +0100 | [diff] [blame] | 5118 | |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 5119 | s->txn->meth = 0; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5120 | http_reset_txn(s); |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 5121 | s->txn->flags |= TX_NOT_FIRST | TX_WAIT_NEXT_RQ; |
Willy Tarreau | 068621e | 2013-12-23 15:11:25 +0100 | [diff] [blame] | 5122 | |
| 5123 | if (prev_status == 401 || prev_status == 407) { |
| 5124 | /* In HTTP keep-alive mode, if we receive a 401, we still have |
| 5125 | * a chance of being able to send the visitor again to the same |
| 5126 | * server over the same connection. This is required by some |
| 5127 | * broken protocols such as NTLM, and anyway whenever there is |
| 5128 | * an opportunity for sending the challenge to the proper place, |
| 5129 | * it's better to do it (at least it helps with debugging). |
| 5130 | */ |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 5131 | s->txn->flags |= TX_PREFER_LAST; |
Willy Tarreau | 068621e | 2013-12-23 15:11:25 +0100 | [diff] [blame] | 5132 | } |
| 5133 | |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 5134 | if (fe->options2 & PR_O2_INDEPSTR) |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 5135 | s->si[1].flags |= SI_FL_INDEP_STR; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5136 | |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 5137 | if (fe->options2 & PR_O2_NODELAY) { |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 5138 | s->req.flags |= CF_NEVER_WAIT; |
| 5139 | s->res.flags |= CF_NEVER_WAIT; |
Willy Tarreau | 96e3121 | 2011-05-30 18:10:30 +0200 | [diff] [blame] | 5140 | } |
| 5141 | |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5142 | /* if the request buffer is not empty, it means we're |
| 5143 | * about to process another request, so send pending |
| 5144 | * data with MSG_MORE to merge TCP packets when possible. |
Willy Tarreau | 065e833 | 2010-01-08 00:30:20 +0100 | [diff] [blame] | 5145 | * Just don't do this if the buffer is close to be full, |
| 5146 | * because the request will wait for it to flush a little |
| 5147 | * bit before proceeding. |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5148 | */ |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 5149 | if (s->req.buf->i) { |
| 5150 | if (s->res.buf->o && |
| 5151 | !buffer_full(s->res.buf, global.tune.maxrewrite) && |
| 5152 | bi_end(s->res.buf) <= s->res.buf->data + s->res.buf->size - global.tune.maxrewrite) |
| 5153 | s->res.flags |= CF_EXPECT_MORE; |
Willy Tarreau | 065e833 | 2010-01-08 00:30:20 +0100 | [diff] [blame] | 5154 | } |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 5155 | |
| 5156 | /* we're removing the analysers, we MUST re-enable events detection */ |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 5157 | channel_auto_read(&s->req); |
| 5158 | channel_auto_close(&s->req); |
| 5159 | channel_auto_read(&s->res); |
| 5160 | channel_auto_close(&s->res); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5161 | |
Willy Tarreau | 2737562 | 2013-12-17 00:00:28 +0100 | [diff] [blame] | 5162 | /* we're in keep-alive with an idle connection, monitor it */ |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 5163 | si_idle_conn(&s->si[1]); |
Willy Tarreau | 2737562 | 2013-12-17 00:00:28 +0100 | [diff] [blame] | 5164 | |
Willy Tarreau | d0d8da9 | 2015-04-04 02:10:38 +0200 | [diff] [blame] | 5165 | s->req.analysers = strm_li(s)->analysers; |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 5166 | s->res.analysers = 0; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5167 | } |
| 5168 | |
| 5169 | |
| 5170 | /* This function updates the request state machine according to the response |
| 5171 | * state machine and buffer flags. It returns 1 if it changes anything (flag |
| 5172 | * or state), otherwise zero. It ignores any state before HTTP_MSG_DONE, as |
| 5173 | * it is only used to find when a request/response couple is complete. Both |
| 5174 | * this function and its equivalent should loop until both return zero. It |
| 5175 | * can set its own state to DONE, CLOSING, CLOSED, TUNNEL, ERROR. |
| 5176 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 5177 | int http_sync_req_state(struct stream *s) |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5178 | { |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 5179 | struct channel *chn = &s->req; |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 5180 | struct http_txn *txn = s->txn; |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 5181 | unsigned int old_flags = chn->flags; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5182 | unsigned int old_state = txn->req.msg_state; |
| 5183 | |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5184 | if (unlikely(txn->req.msg_state < HTTP_MSG_BODY)) |
| 5185 | return 0; |
| 5186 | |
| 5187 | if (txn->req.msg_state == HTTP_MSG_DONE) { |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 5188 | /* No need to read anymore, the request was completely parsed. |
Willy Tarreau | 58bd8fd | 2010-09-28 14:16:41 +0200 | [diff] [blame] | 5189 | * We can shut the read side unless we want to abort_on_close, |
| 5190 | * or we have a POST request. The issue with POST requests is |
| 5191 | * that some browsers still send a CRLF after the request, and |
| 5192 | * this CRLF must be read so that it does not remain in the kernel |
| 5193 | * buffers, otherwise a close could cause an RST on some systems |
| 5194 | * (eg: Linux). |
Willy Tarreau | 3988d93 | 2013-12-27 23:03:08 +0100 | [diff] [blame] | 5195 | * Note that if we're using keep-alive on the client side, we'd |
| 5196 | * rather poll now and keep the polling enabled for the whole |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 5197 | * stream's life than enabling/disabling it between each |
Willy Tarreau | 3988d93 | 2013-12-27 23:03:08 +0100 | [diff] [blame] | 5198 | * response and next request. |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 5199 | */ |
Willy Tarreau | 3988d93 | 2013-12-27 23:03:08 +0100 | [diff] [blame] | 5200 | if (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_SCL) && |
| 5201 | ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) && |
| 5202 | !(s->be->options & PR_O_ABRT_CLOSE) && |
| 5203 | txn->meth != HTTP_METH_POST) |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 5204 | channel_dont_read(chn); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5205 | |
Willy Tarreau | 40f151a | 2012-12-20 12:10:09 +0100 | [diff] [blame] | 5206 | /* if the server closes the connection, we want to immediately react |
| 5207 | * and close the socket to save packets and syscalls. |
| 5208 | */ |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 5209 | s->si[1].flags |= SI_FL_NOHALF; |
Willy Tarreau | 40f151a | 2012-12-20 12:10:09 +0100 | [diff] [blame] | 5210 | |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5211 | if (txn->rsp.msg_state == HTTP_MSG_ERROR) |
| 5212 | goto wait_other_side; |
| 5213 | |
| 5214 | if (txn->rsp.msg_state < HTTP_MSG_DONE) { |
| 5215 | /* The server has not finished to respond, so we |
| 5216 | * don't want to move in order not to upset it. |
| 5217 | */ |
| 5218 | goto wait_other_side; |
| 5219 | } |
| 5220 | |
| 5221 | if (txn->rsp.msg_state == HTTP_MSG_TUNNEL) { |
| 5222 | /* if any side switches to tunnel mode, the other one does too */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 5223 | channel_auto_read(chn); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5224 | txn->req.msg_state = HTTP_MSG_TUNNEL; |
Willy Tarreau | fc47f91 | 2012-10-20 10:38:09 +0200 | [diff] [blame] | 5225 | chn->flags |= CF_NEVER_WAIT; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5226 | goto wait_other_side; |
| 5227 | } |
| 5228 | |
| 5229 | /* When we get here, it means that both the request and the |
| 5230 | * response have finished receiving. Depending on the connection |
| 5231 | * mode, we'll have to wait for the last bytes to leave in either |
| 5232 | * direction, and sometimes for a close to be effective. |
| 5233 | */ |
| 5234 | |
Willy Tarreau | cce7fa4 | 2010-01-16 23:19:39 +0100 | [diff] [blame] | 5235 | if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) { |
| 5236 | /* Server-close mode : queue a connection close to the server */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 5237 | if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW))) |
| 5238 | channel_shutw_now(chn); |
Willy Tarreau | cce7fa4 | 2010-01-16 23:19:39 +0100 | [diff] [blame] | 5239 | } |
| 5240 | else if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) { |
| 5241 | /* Option forceclose is set, or either side wants to close, |
| 5242 | * let's enforce it now that we're not expecting any new |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 5243 | * data to come. The caller knows the stream is complete |
Willy Tarreau | cce7fa4 | 2010-01-16 23:19:39 +0100 | [diff] [blame] | 5244 | * once both states are CLOSED. |
| 5245 | */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 5246 | if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW))) { |
| 5247 | channel_shutr_now(chn); |
| 5248 | channel_shutw_now(chn); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5249 | } |
Willy Tarreau | cce7fa4 | 2010-01-16 23:19:39 +0100 | [diff] [blame] | 5250 | } |
| 5251 | else { |
Willy Tarreau | 4213a11 | 2013-12-15 10:25:42 +0100 | [diff] [blame] | 5252 | /* The last possible modes are keep-alive and tunnel. Tunnel mode |
| 5253 | * will not have any analyser so it needs to poll for reads. |
Willy Tarreau | cce7fa4 | 2010-01-16 23:19:39 +0100 | [diff] [blame] | 5254 | */ |
Willy Tarreau | 4213a11 | 2013-12-15 10:25:42 +0100 | [diff] [blame] | 5255 | if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN) { |
| 5256 | channel_auto_read(chn); |
| 5257 | txn->req.msg_state = HTTP_MSG_TUNNEL; |
| 5258 | chn->flags |= CF_NEVER_WAIT; |
| 5259 | } |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5260 | } |
| 5261 | |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 5262 | if (chn->flags & (CF_SHUTW|CF_SHUTW_NOW)) { |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5263 | /* if we've just closed an output, let's switch */ |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 5264 | s->si[1].flags |= SI_FL_NOLINGER; /* we want to close ASAP */ |
Willy Tarreau | cce7fa4 | 2010-01-16 23:19:39 +0100 | [diff] [blame] | 5265 | |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 5266 | if (!channel_is_empty(chn)) { |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5267 | txn->req.msg_state = HTTP_MSG_CLOSING; |
| 5268 | goto http_msg_closing; |
| 5269 | } |
| 5270 | else { |
| 5271 | txn->req.msg_state = HTTP_MSG_CLOSED; |
| 5272 | goto http_msg_closed; |
| 5273 | } |
| 5274 | } |
Willy Tarreau | cce7fa4 | 2010-01-16 23:19:39 +0100 | [diff] [blame] | 5275 | goto wait_other_side; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5276 | } |
| 5277 | |
| 5278 | if (txn->req.msg_state == HTTP_MSG_CLOSING) { |
| 5279 | http_msg_closing: |
| 5280 | /* nothing else to forward, just waiting for the output buffer |
| 5281 | * to be empty and for the shutw_now to take effect. |
| 5282 | */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 5283 | if (channel_is_empty(chn)) { |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5284 | txn->req.msg_state = HTTP_MSG_CLOSED; |
| 5285 | goto http_msg_closed; |
| 5286 | } |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 5287 | else if (chn->flags & CF_SHUTW) { |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5288 | txn->req.msg_state = HTTP_MSG_ERROR; |
| 5289 | goto wait_other_side; |
| 5290 | } |
| 5291 | } |
| 5292 | |
| 5293 | if (txn->req.msg_state == HTTP_MSG_CLOSED) { |
| 5294 | http_msg_closed: |
Willy Tarreau | 3988d93 | 2013-12-27 23:03:08 +0100 | [diff] [blame] | 5295 | /* see above in MSG_DONE why we only do this in these states */ |
| 5296 | if (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_SCL) && |
| 5297 | ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) && |
| 5298 | !(s->be->options & PR_O_ABRT_CLOSE)) |
Willy Tarreau | 2e7a165 | 2013-12-15 15:32:10 +0100 | [diff] [blame] | 5299 | channel_dont_read(chn); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5300 | goto wait_other_side; |
| 5301 | } |
| 5302 | |
| 5303 | wait_other_side: |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 5304 | return txn->req.msg_state != old_state || chn->flags != old_flags; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5305 | } |
| 5306 | |
| 5307 | |
| 5308 | /* This function updates the response state machine according to the request |
| 5309 | * state machine and buffer flags. It returns 1 if it changes anything (flag |
| 5310 | * or state), otherwise zero. It ignores any state before HTTP_MSG_DONE, as |
| 5311 | * it is only used to find when a request/response couple is complete. Both |
| 5312 | * this function and its equivalent should loop until both return zero. It |
| 5313 | * can set its own state to DONE, CLOSING, CLOSED, TUNNEL, ERROR. |
| 5314 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 5315 | int http_sync_res_state(struct stream *s) |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5316 | { |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 5317 | struct channel *chn = &s->res; |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 5318 | struct http_txn *txn = s->txn; |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 5319 | unsigned int old_flags = chn->flags; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5320 | unsigned int old_state = txn->rsp.msg_state; |
| 5321 | |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5322 | if (unlikely(txn->rsp.msg_state < HTTP_MSG_BODY)) |
| 5323 | return 0; |
| 5324 | |
| 5325 | if (txn->rsp.msg_state == HTTP_MSG_DONE) { |
| 5326 | /* In theory, we don't need to read anymore, but we must |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 5327 | * still monitor the server connection for a possible close |
| 5328 | * while the request is being uploaded, so we don't disable |
| 5329 | * reading. |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5330 | */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 5331 | /* channel_dont_read(chn); */ |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5332 | |
| 5333 | if (txn->req.msg_state == HTTP_MSG_ERROR) |
| 5334 | goto wait_other_side; |
| 5335 | |
| 5336 | if (txn->req.msg_state < HTTP_MSG_DONE) { |
| 5337 | /* The client seems to still be sending data, probably |
| 5338 | * because we got an error response during an upload. |
| 5339 | * We have the choice of either breaking the connection |
| 5340 | * or letting it pass through. Let's do the later. |
| 5341 | */ |
| 5342 | goto wait_other_side; |
| 5343 | } |
| 5344 | |
| 5345 | if (txn->req.msg_state == HTTP_MSG_TUNNEL) { |
| 5346 | /* if any side switches to tunnel mode, the other one does too */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 5347 | channel_auto_read(chn); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5348 | txn->rsp.msg_state = HTTP_MSG_TUNNEL; |
Willy Tarreau | fc47f91 | 2012-10-20 10:38:09 +0200 | [diff] [blame] | 5349 | chn->flags |= CF_NEVER_WAIT; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5350 | goto wait_other_side; |
| 5351 | } |
| 5352 | |
| 5353 | /* When we get here, it means that both the request and the |
| 5354 | * response have finished receiving. Depending on the connection |
| 5355 | * mode, we'll have to wait for the last bytes to leave in either |
| 5356 | * direction, and sometimes for a close to be effective. |
| 5357 | */ |
| 5358 | |
| 5359 | if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) { |
| 5360 | /* Server-close mode : shut read and wait for the request |
| 5361 | * side to close its output buffer. The caller will detect |
| 5362 | * when we're in DONE and the other is in CLOSED and will |
| 5363 | * catch that for the final cleanup. |
| 5364 | */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 5365 | if (!(chn->flags & (CF_SHUTR|CF_SHUTR_NOW))) |
| 5366 | channel_shutr_now(chn); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5367 | } |
Willy Tarreau | cce7fa4 | 2010-01-16 23:19:39 +0100 | [diff] [blame] | 5368 | else if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) { |
| 5369 | /* Option forceclose is set, or either side wants to close, |
| 5370 | * let's enforce it now that we're not expecting any new |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 5371 | * data to come. The caller knows the stream is complete |
Willy Tarreau | cce7fa4 | 2010-01-16 23:19:39 +0100 | [diff] [blame] | 5372 | * once both states are CLOSED. |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5373 | */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 5374 | if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW))) { |
| 5375 | channel_shutr_now(chn); |
| 5376 | channel_shutw_now(chn); |
Willy Tarreau | cce7fa4 | 2010-01-16 23:19:39 +0100 | [diff] [blame] | 5377 | } |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5378 | } |
| 5379 | else { |
Willy Tarreau | 4213a11 | 2013-12-15 10:25:42 +0100 | [diff] [blame] | 5380 | /* The last possible modes are keep-alive and tunnel. Tunnel will |
| 5381 | * need to forward remaining data. Keep-alive will need to monitor |
| 5382 | * for connection closing. |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5383 | */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 5384 | channel_auto_read(chn); |
Willy Tarreau | fc47f91 | 2012-10-20 10:38:09 +0200 | [diff] [blame] | 5385 | chn->flags |= CF_NEVER_WAIT; |
Willy Tarreau | 4213a11 | 2013-12-15 10:25:42 +0100 | [diff] [blame] | 5386 | if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN) |
| 5387 | txn->rsp.msg_state = HTTP_MSG_TUNNEL; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5388 | } |
| 5389 | |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 5390 | if (chn->flags & (CF_SHUTW|CF_SHUTW_NOW)) { |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5391 | /* if we've just closed an output, let's switch */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 5392 | if (!channel_is_empty(chn)) { |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5393 | txn->rsp.msg_state = HTTP_MSG_CLOSING; |
| 5394 | goto http_msg_closing; |
| 5395 | } |
| 5396 | else { |
| 5397 | txn->rsp.msg_state = HTTP_MSG_CLOSED; |
| 5398 | goto http_msg_closed; |
| 5399 | } |
| 5400 | } |
| 5401 | goto wait_other_side; |
| 5402 | } |
| 5403 | |
| 5404 | if (txn->rsp.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->rsp.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 | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5414 | txn->rsp.msg_state = HTTP_MSG_ERROR; |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 5415 | s->be->be_counters.cli_aborts++; |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 5416 | if (objt_server(s->target)) |
| 5417 | objt_server(s->target)->counters.cli_aborts++; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5418 | goto wait_other_side; |
| 5419 | } |
| 5420 | } |
| 5421 | |
| 5422 | if (txn->rsp.msg_state == HTTP_MSG_CLOSED) { |
| 5423 | http_msg_closed: |
| 5424 | /* drop any pending data */ |
Willy Tarreau | 319f745 | 2015-01-14 20:32:59 +0100 | [diff] [blame] | 5425 | channel_truncate(chn); |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 5426 | channel_auto_close(chn); |
| 5427 | channel_auto_read(chn); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5428 | goto wait_other_side; |
| 5429 | } |
| 5430 | |
| 5431 | wait_other_side: |
Willy Tarreau | fc47f91 | 2012-10-20 10:38:09 +0200 | [diff] [blame] | 5432 | /* We force the response to leave immediately if we're waiting for the |
| 5433 | * other side, since there is no pending shutdown to push it out. |
| 5434 | */ |
| 5435 | if (!channel_is_empty(chn)) |
| 5436 | chn->flags |= CF_SEND_DONTWAIT; |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 5437 | return txn->rsp.msg_state != old_state || chn->flags != old_flags; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5438 | } |
| 5439 | |
| 5440 | |
| 5441 | /* Resync the request and response state machines. Return 1 if either state |
| 5442 | * changes. |
| 5443 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 5444 | int http_resync_states(struct stream *s) |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5445 | { |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 5446 | struct http_txn *txn = s->txn; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5447 | int old_req_state = txn->req.msg_state; |
| 5448 | int old_res_state = txn->rsp.msg_state; |
| 5449 | |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5450 | http_sync_req_state(s); |
| 5451 | while (1) { |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5452 | if (!http_sync_res_state(s)) |
| 5453 | break; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5454 | if (!http_sync_req_state(s)) |
| 5455 | break; |
| 5456 | } |
Willy Tarreau | 3ce10ff | 2014-04-22 08:24:38 +0200 | [diff] [blame] | 5457 | |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5458 | /* OK, both state machines agree on a compatible state. |
| 5459 | * There are a few cases we're interested in : |
| 5460 | * - HTTP_MSG_TUNNEL on either means we have to disable both analysers |
| 5461 | * - HTTP_MSG_CLOSED on both sides means we've reached the end in both |
| 5462 | * directions, so let's simply disable both analysers. |
| 5463 | * - HTTP_MSG_CLOSED on the response only means we must abort the |
| 5464 | * request. |
| 5465 | * - HTTP_MSG_CLOSED on the request and HTTP_MSG_DONE on the response |
| 5466 | * with server-close mode means we've completed one request and we |
| 5467 | * must re-initialize the server connection. |
| 5468 | */ |
| 5469 | |
| 5470 | if (txn->req.msg_state == HTTP_MSG_TUNNEL || |
| 5471 | txn->rsp.msg_state == HTTP_MSG_TUNNEL || |
| 5472 | (txn->req.msg_state == HTTP_MSG_CLOSED && |
| 5473 | txn->rsp.msg_state == HTTP_MSG_CLOSED)) { |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 5474 | s->req.analysers = 0; |
| 5475 | channel_auto_close(&s->req); |
| 5476 | channel_auto_read(&s->req); |
| 5477 | s->res.analysers = 0; |
| 5478 | channel_auto_close(&s->res); |
| 5479 | channel_auto_read(&s->res); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5480 | } |
Willy Tarreau | 40f151a | 2012-12-20 12:10:09 +0100 | [diff] [blame] | 5481 | else if ((txn->req.msg_state >= HTTP_MSG_DONE && |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 5482 | (txn->rsp.msg_state == HTTP_MSG_CLOSED || (s->res.flags & CF_SHUTW))) || |
Willy Tarreau | 2fa144c | 2010-01-04 23:13:26 +0100 | [diff] [blame] | 5483 | txn->rsp.msg_state == HTTP_MSG_ERROR || |
Willy Tarreau | 40f151a | 2012-12-20 12:10:09 +0100 | [diff] [blame] | 5484 | txn->req.msg_state == HTTP_MSG_ERROR) { |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 5485 | s->res.analysers = 0; |
| 5486 | channel_auto_close(&s->res); |
| 5487 | channel_auto_read(&s->res); |
| 5488 | s->req.analysers = 0; |
| 5489 | channel_abort(&s->req); |
| 5490 | channel_auto_close(&s->req); |
| 5491 | channel_auto_read(&s->req); |
| 5492 | channel_truncate(&s->req); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5493 | } |
Willy Tarreau | 4213a11 | 2013-12-15 10:25:42 +0100 | [diff] [blame] | 5494 | else if ((txn->req.msg_state == HTTP_MSG_DONE || |
| 5495 | txn->req.msg_state == HTTP_MSG_CLOSED) && |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5496 | txn->rsp.msg_state == HTTP_MSG_DONE && |
Willy Tarreau | 4213a11 | 2013-12-15 10:25:42 +0100 | [diff] [blame] | 5497 | ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL || |
| 5498 | (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL)) { |
| 5499 | /* server-close/keep-alive: terminate this transaction, |
| 5500 | * possibly killing the server connection and reinitialize |
| 5501 | * a fresh-new transaction. |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5502 | */ |
| 5503 | http_end_txn_clean_session(s); |
| 5504 | } |
| 5505 | |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5506 | return txn->req.msg_state != old_req_state || |
| 5507 | txn->rsp.msg_state != old_res_state; |
| 5508 | } |
| 5509 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5510 | /* This function is an analyser which forwards request body (including chunk |
| 5511 | * sizes if any). It is called as soon as we must forward, even if we forward |
| 5512 | * zero byte. The only situation where it must not be called is when we're in |
| 5513 | * tunnel mode and we want to forward till the close. It's used both to forward |
| 5514 | * remaining data and to resync after end of body. It expects the msg_state to |
| 5515 | * 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] | 5516 | * 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] | 5517 | * 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] | 5518 | * bytes of pending data + the headers if not already done. |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5519 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 5520 | 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] | 5521 | { |
Willy Tarreau | fb0afa7 | 2015-04-03 14:46:27 +0200 | [diff] [blame] | 5522 | struct session *sess = s->sess; |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 5523 | struct http_txn *txn = s->txn; |
| 5524 | struct http_msg *msg = &s->txn->req; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5525 | |
Willy Tarreau | f5c8bd6 | 2010-01-04 07:10:34 +0100 | [diff] [blame] | 5526 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) |
| 5527 | return 0; |
| 5528 | |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 5529 | 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] | 5530 | ((req->flags & CF_SHUTW) && (req->to_forward || req->buf->o))) { |
Willy Tarreau | 4fe4190 | 2010-06-07 22:27:41 +0200 | [diff] [blame] | 5531 | /* Output closed while we were sending data. We must abort and |
| 5532 | * wake the other side up. |
| 5533 | */ |
| 5534 | msg->msg_state = HTTP_MSG_ERROR; |
| 5535 | http_resync_states(s); |
Willy Tarreau | 082b01c | 2010-01-02 23:58:04 +0100 | [diff] [blame] | 5536 | return 1; |
| 5537 | } |
| 5538 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5539 | /* Note that we don't have to send 100-continue back because we don't |
| 5540 | * need the data to complete our job, and it's up to the server to |
| 5541 | * decide whether to return 100, 417 or anything else in return of |
| 5542 | * an "Expect: 100-continue" header. |
| 5543 | */ |
| 5544 | |
Willy Tarreau | bb2e669 | 2014-07-10 19:06:10 +0200 | [diff] [blame] | 5545 | if (msg->sov > 0) { |
Willy Tarreau | b59c7bf | 2014-04-22 14:29:58 +0200 | [diff] [blame] | 5546 | /* we have msg->sov which points to the first byte of message |
| 5547 | * body, and req->buf.p still points to the beginning of the |
| 5548 | * message. We forward the headers now, as we don't need them |
| 5549 | * anymore, and we want to flush them. |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5550 | */ |
Willy Tarreau | b59c7bf | 2014-04-22 14:29:58 +0200 | [diff] [blame] | 5551 | b_adv(req->buf, msg->sov); |
| 5552 | msg->next -= msg->sov; |
| 5553 | msg->sov = 0; |
Willy Tarreau | a458b67 | 2012-03-05 11:17:50 +0100 | [diff] [blame] | 5554 | |
Willy Tarreau | b59c7bf | 2014-04-22 14:29:58 +0200 | [diff] [blame] | 5555 | /* The previous analysers guarantee that the state is somewhere |
| 5556 | * between MSG_BODY and the first MSG_DATA. So msg->sol and |
| 5557 | * msg->next are always correct. |
| 5558 | */ |
| 5559 | if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) { |
| 5560 | if (msg->flags & HTTP_MSGF_TE_CHNK) |
| 5561 | msg->msg_state = HTTP_MSG_CHUNK_SIZE; |
| 5562 | else |
| 5563 | msg->msg_state = HTTP_MSG_DATA; |
| 5564 | } |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5565 | } |
| 5566 | |
Willy Tarreau | 7ba2354 | 2014-04-17 21:50:00 +0200 | [diff] [blame] | 5567 | /* Some post-connect processing might want us to refrain from starting to |
| 5568 | * forward data. Currently, the only reason for this is "balance url_param" |
| 5569 | * whichs need to parse/process the request after we've enabled forwarding. |
| 5570 | */ |
| 5571 | if (unlikely(msg->flags & HTTP_MSGF_WAIT_CONN)) { |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 5572 | if (!(s->res.flags & CF_READ_ATTACHED)) { |
Willy Tarreau | 7ba2354 | 2014-04-17 21:50:00 +0200 | [diff] [blame] | 5573 | channel_auto_connect(req); |
Willy Tarreau | 644c101 | 2014-04-30 18:11:11 +0200 | [diff] [blame] | 5574 | req->flags |= CF_WAKE_CONNECT; |
Willy Tarreau | 7ba2354 | 2014-04-17 21:50:00 +0200 | [diff] [blame] | 5575 | goto missing_data; |
| 5576 | } |
| 5577 | msg->flags &= ~HTTP_MSGF_WAIT_CONN; |
| 5578 | } |
| 5579 | |
Willy Tarreau | 80a92c0 | 2014-03-12 10:41:13 +0100 | [diff] [blame] | 5580 | /* in most states, we should abort in case of early close */ |
| 5581 | channel_auto_close(req); |
| 5582 | |
Willy Tarreau | efdf094 | 2014-04-24 20:08:57 +0200 | [diff] [blame] | 5583 | if (req->to_forward) { |
| 5584 | /* We can't process the buffer's contents yet */ |
| 5585 | req->flags |= CF_WAKE_WRITE; |
| 5586 | goto missing_data; |
| 5587 | } |
| 5588 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5589 | while (1) { |
Willy Tarreau | caabe41 | 2010-01-03 23:08:28 +0100 | [diff] [blame] | 5590 | if (msg->msg_state == HTTP_MSG_DATA) { |
| 5591 | /* must still forward */ |
Willy Tarreau | bed410e | 2014-04-22 08:19:34 +0200 | [diff] [blame] | 5592 | /* we may have some pending data starting at req->buf->p */ |
| 5593 | if (msg->chunk_len > req->buf->i - msg->next) { |
Willy Tarreau | 4afd70a | 2014-01-25 02:26:39 +0100 | [diff] [blame] | 5594 | req->flags |= CF_WAKE_WRITE; |
Willy Tarreau | f5c8bd6 | 2010-01-04 07:10:34 +0100 | [diff] [blame] | 5595 | goto missing_data; |
Willy Tarreau | 4afd70a | 2014-01-25 02:26:39 +0100 | [diff] [blame] | 5596 | } |
Willy Tarreau | bed410e | 2014-04-22 08:19:34 +0200 | [diff] [blame] | 5597 | msg->next += msg->chunk_len; |
| 5598 | msg->chunk_len = 0; |
Willy Tarreau | caabe41 | 2010-01-03 23:08:28 +0100 | [diff] [blame] | 5599 | |
| 5600 | /* nothing left to forward */ |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 5601 | if (msg->flags & HTTP_MSGF_TE_CHNK) |
Willy Tarreau | 54d23df | 2012-10-25 19:04:45 +0200 | [diff] [blame] | 5602 | msg->msg_state = HTTP_MSG_CHUNK_CRLF; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5603 | else |
Willy Tarreau | caabe41 | 2010-01-03 23:08:28 +0100 | [diff] [blame] | 5604 | msg->msg_state = HTTP_MSG_DONE; |
Willy Tarreau | caabe41 | 2010-01-03 23:08:28 +0100 | [diff] [blame] | 5605 | } |
| 5606 | else if (msg->msg_state == HTTP_MSG_CHUNK_SIZE) { |
Willy Tarreau | 124d991 | 2011-03-01 20:30:48 +0100 | [diff] [blame] | 5607 | /* read the chunk size and assign it to ->chunk_len, then |
Willy Tarreau | c24715e | 2014-04-17 15:21:20 +0200 | [diff] [blame] | 5608 | * set ->next to point to the body and switch to DATA or |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5609 | * TRAILERS state. |
| 5610 | */ |
Willy Tarreau | 4baf44b | 2012-03-09 14:10:20 +0100 | [diff] [blame] | 5611 | int ret = http_parse_chunk_size(msg); |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5612 | |
Willy Tarreau | 54d23df | 2012-10-25 19:04:45 +0200 | [diff] [blame] | 5613 | if (ret == 0) |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5614 | goto missing_data; |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 5615 | else if (ret < 0) { |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 5616 | stream_inc_http_err_ctr(s); |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 5617 | if (msg->err_pos >= 0) |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 5618 | http_capture_bad_message(&sess->fe->invalid_req, s, msg, HTTP_MSG_CHUNK_SIZE, s->be); |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5619 | goto return_bad_req; |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 5620 | } |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5621 | /* otherwise we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state */ |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5622 | } |
Willy Tarreau | 54d23df | 2012-10-25 19:04:45 +0200 | [diff] [blame] | 5623 | else if (msg->msg_state == HTTP_MSG_CHUNK_CRLF) { |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5624 | /* we want the CRLF after the data */ |
Willy Tarreau | 54d23df | 2012-10-25 19:04:45 +0200 | [diff] [blame] | 5625 | int ret = http_skip_chunk_crlf(msg); |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5626 | |
| 5627 | if (ret == 0) |
| 5628 | goto missing_data; |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 5629 | else if (ret < 0) { |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 5630 | stream_inc_http_err_ctr(s); |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 5631 | if (msg->err_pos >= 0) |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 5632 | http_capture_bad_message(&sess->fe->invalid_req, s, msg, HTTP_MSG_CHUNK_CRLF, s->be); |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5633 | goto return_bad_req; |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 5634 | } |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5635 | /* we're in MSG_CHUNK_SIZE now */ |
| 5636 | } |
| 5637 | else if (msg->msg_state == HTTP_MSG_TRAILERS) { |
Willy Tarreau | 4baf44b | 2012-03-09 14:10:20 +0100 | [diff] [blame] | 5638 | int ret = http_forward_trailers(msg); |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5639 | |
| 5640 | if (ret == 0) |
| 5641 | goto missing_data; |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 5642 | else if (ret < 0) { |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 5643 | stream_inc_http_err_ctr(s); |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 5644 | if (msg->err_pos >= 0) |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 5645 | http_capture_bad_message(&sess->fe->invalid_req, s, msg, HTTP_MSG_TRAILERS, s->be); |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5646 | goto return_bad_req; |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 5647 | } |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5648 | /* we're in HTTP_MSG_DONE now */ |
| 5649 | } |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5650 | else { |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 5651 | int old_state = msg->msg_state; |
| 5652 | |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5653 | /* other states, DONE...TUNNEL */ |
Willy Tarreau | bed410e | 2014-04-22 08:19:34 +0200 | [diff] [blame] | 5654 | |
| 5655 | /* we may have some pending data starting at req->buf->p |
| 5656 | * such as last chunk of data or trailers. |
| 5657 | */ |
| 5658 | b_adv(req->buf, msg->next); |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 5659 | if (unlikely(!(s->req.flags & CF_WROTE_DATA))) |
Willy Tarreau | bb2e669 | 2014-07-10 19:06:10 +0200 | [diff] [blame] | 5660 | msg->sov -= msg->next; |
Willy Tarreau | bed410e | 2014-04-22 08:19:34 +0200 | [diff] [blame] | 5661 | msg->next = 0; |
| 5662 | |
Willy Tarreau | bbfb6c4 | 2015-05-11 18:30:33 +0200 | [diff] [blame] | 5663 | /* we don't want to forward closes on DONE except in |
| 5664 | * tunnel mode. |
| 5665 | */ |
| 5666 | if ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 5667 | channel_dont_close(req); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5668 | if (http_resync_states(s)) { |
| 5669 | /* some state changes occurred, maybe the analyser |
| 5670 | * was disabled too. |
Willy Tarreau | face839 | 2010-01-03 11:37:54 +0100 | [diff] [blame] | 5671 | */ |
Willy Tarreau | 3fe693b | 2010-12-12 12:50:05 +0100 | [diff] [blame] | 5672 | if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) { |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 5673 | if (req->flags & CF_SHUTW) { |
Willy Tarreau | 3fe693b | 2010-12-12 12:50:05 +0100 | [diff] [blame] | 5674 | /* request errors are most likely due to |
| 5675 | * the server aborting the transfer. |
| 5676 | */ |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 5677 | goto aborted_xfer; |
Willy Tarreau | 3fe693b | 2010-12-12 12:50:05 +0100 | [diff] [blame] | 5678 | } |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 5679 | if (msg->err_pos >= 0) |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 5680 | http_capture_bad_message(&sess->fe->invalid_req, s, msg, old_state, s->be); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5681 | goto return_bad_req; |
Willy Tarreau | 3fe693b | 2010-12-12 12:50:05 +0100 | [diff] [blame] | 5682 | } |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5683 | return 1; |
Willy Tarreau | b608feb | 2010-01-02 22:47:18 +0100 | [diff] [blame] | 5684 | } |
Willy Tarreau | 5c54c71 | 2010-07-17 08:02:58 +0200 | [diff] [blame] | 5685 | |
| 5686 | /* If "option abortonclose" is set on the backend, we |
| 5687 | * want to monitor the client's connection and forward |
| 5688 | * any shutdown notification to the server, which will |
| 5689 | * decide whether to close or to go on processing the |
Willy Tarreau | bbfb6c4 | 2015-05-11 18:30:33 +0200 | [diff] [blame] | 5690 | * request. We only do that in tunnel mode, and not in |
| 5691 | * other modes since it can be abused to exhaust source |
| 5692 | * ports. |
Willy Tarreau | 5c54c71 | 2010-07-17 08:02:58 +0200 | [diff] [blame] | 5693 | */ |
| 5694 | if (s->be->options & PR_O_ABRT_CLOSE) { |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 5695 | channel_auto_read(req); |
Willy Tarreau | bbfb6c4 | 2015-05-11 18:30:33 +0200 | [diff] [blame] | 5696 | if ((req->flags & (CF_SHUTR|CF_READ_NULL)) && |
| 5697 | ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN)) |
| 5698 | s->si[1].flags |= SI_FL_NOLINGER; |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 5699 | channel_auto_close(req); |
Willy Tarreau | 5c54c71 | 2010-07-17 08:02:58 +0200 | [diff] [blame] | 5700 | } |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 5701 | else if (s->txn->meth == HTTP_METH_POST) { |
Willy Tarreau | 58bd8fd | 2010-09-28 14:16:41 +0200 | [diff] [blame] | 5702 | /* POST requests may require to read extra CRLF |
| 5703 | * sent by broken browsers and which could cause |
| 5704 | * an RST to be sent upon close on some systems |
| 5705 | * (eg: Linux). |
| 5706 | */ |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 5707 | channel_auto_read(req); |
Willy Tarreau | 58bd8fd | 2010-09-28 14:16:41 +0200 | [diff] [blame] | 5708 | } |
Willy Tarreau | 5c54c71 | 2010-07-17 08:02:58 +0200 | [diff] [blame] | 5709 | |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5710 | return 0; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5711 | } |
| 5712 | } |
| 5713 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5714 | missing_data: |
Willy Tarreau | bed410e | 2014-04-22 08:19:34 +0200 | [diff] [blame] | 5715 | /* we may have some pending data starting at req->buf->p */ |
| 5716 | b_adv(req->buf, msg->next); |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 5717 | if (unlikely(!(s->req.flags & CF_WROTE_DATA))) |
Willy Tarreau | bb2e669 | 2014-07-10 19:06:10 +0200 | [diff] [blame] | 5718 | msg->sov -= msg->next + MIN(msg->chunk_len, req->buf->i); |
| 5719 | |
Willy Tarreau | bed410e | 2014-04-22 08:19:34 +0200 | [diff] [blame] | 5720 | msg->next = 0; |
| 5721 | msg->chunk_len -= channel_forward(req, msg->chunk_len); |
| 5722 | |
Willy Tarreau | f5c8bd6 | 2010-01-04 07:10:34 +0100 | [diff] [blame] | 5723 | /* stop waiting for data if the input is closed before the end */ |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 5724 | if (req->flags & CF_SHUTR) { |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 5725 | if (!(s->flags & SF_ERR_MASK)) |
| 5726 | s->flags |= SF_ERR_CLICL; |
| 5727 | if (!(s->flags & SF_FINST_MASK)) { |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 5728 | if (txn->rsp.msg_state < HTTP_MSG_ERROR) |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 5729 | s->flags |= SF_FINST_H; |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 5730 | else |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 5731 | s->flags |= SF_FINST_D; |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 5732 | } |
| 5733 | |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 5734 | sess->fe->fe_counters.cli_aborts++; |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 5735 | s->be->be_counters.cli_aborts++; |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 5736 | if (objt_server(s->target)) |
| 5737 | objt_server(s->target)->counters.cli_aborts++; |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 5738 | |
| 5739 | goto return_bad_req_stats_ok; |
Willy Tarreau | 79ebac6 | 2010-06-07 13:47:49 +0200 | [diff] [blame] | 5740 | } |
Willy Tarreau | f5c8bd6 | 2010-01-04 07:10:34 +0100 | [diff] [blame] | 5741 | |
Willy Tarreau | f5c8bd6 | 2010-01-04 07:10:34 +0100 | [diff] [blame] | 5742 | /* waiting for the last bits to leave the buffer */ |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 5743 | if (req->flags & CF_SHUTW) |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 5744 | goto aborted_xfer; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5745 | |
Willy Tarreau | 92aa1fa | 2010-08-28 18:57:20 +0200 | [diff] [blame] | 5746 | /* 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] | 5747 | * 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] | 5748 | */ |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 5749 | if (msg->flags & HTTP_MSGF_TE_CHNK) |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 5750 | channel_dont_close(req); |
Willy Tarreau | 92aa1fa | 2010-08-28 18:57:20 +0200 | [diff] [blame] | 5751 | |
Willy Tarreau | 5c62092 | 2011-05-11 19:56:11 +0200 | [diff] [blame] | 5752 | /* 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] | 5753 | * 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] | 5754 | * 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] | 5755 | * modes are already handled by the stream sock layer. We must not do |
| 5756 | * this in content-length mode because it could present the MSG_MORE |
| 5757 | * flag with the last block of forwarded data, which would cause an |
| 5758 | * additional delay to be observed by the receiver. |
Willy Tarreau | 5c62092 | 2011-05-11 19:56:11 +0200 | [diff] [blame] | 5759 | */ |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 5760 | if (msg->flags & HTTP_MSGF_TE_CHNK) |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 5761 | req->flags |= CF_EXPECT_MORE; |
Willy Tarreau | 5c62092 | 2011-05-11 19:56:11 +0200 | [diff] [blame] | 5762 | |
Willy Tarreau | f5c8bd6 | 2010-01-04 07:10:34 +0100 | [diff] [blame] | 5763 | return 0; |
| 5764 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5765 | return_bad_req: /* let's centralize all bad requests */ |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 5766 | sess->fe->fe_counters.failed_req++; |
Willy Tarreau | fb0afa7 | 2015-04-03 14:46:27 +0200 | [diff] [blame] | 5767 | if (sess->listener->counters) |
| 5768 | sess->listener->counters->failed_req++; |
Willy Tarreau | bed410e | 2014-04-22 08:19:34 +0200 | [diff] [blame] | 5769 | |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 5770 | return_bad_req_stats_ok: |
Willy Tarreau | bed410e | 2014-04-22 08:19:34 +0200 | [diff] [blame] | 5771 | /* we may have some pending data starting at req->buf->p */ |
| 5772 | b_adv(req->buf, msg->next); |
| 5773 | msg->next = 0; |
| 5774 | |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 5775 | txn->req.msg_state = HTTP_MSG_ERROR; |
| 5776 | if (txn->status) { |
| 5777 | /* Note: we don't send any error if some data were already sent */ |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 5778 | stream_int_retnclose(&s->si[0], NULL); |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 5779 | } else { |
| 5780 | txn->status = 400; |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 5781 | stream_int_retnclose(&s->si[0], http_error_message(s, HTTP_ERR_400)); |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 5782 | } |
| 5783 | req->analysers = 0; |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 5784 | s->res.analysers = 0; /* we're in data phase, we want to abort both directions */ |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5785 | |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 5786 | if (!(s->flags & SF_ERR_MASK)) |
| 5787 | s->flags |= SF_ERR_PRXCOND; |
| 5788 | if (!(s->flags & SF_FINST_MASK)) { |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 5789 | if (txn->rsp.msg_state < HTTP_MSG_ERROR) |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 5790 | s->flags |= SF_FINST_H; |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 5791 | else |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 5792 | s->flags |= SF_FINST_D; |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 5793 | } |
| 5794 | return 0; |
| 5795 | |
| 5796 | aborted_xfer: |
| 5797 | txn->req.msg_state = HTTP_MSG_ERROR; |
| 5798 | if (txn->status) { |
| 5799 | /* Note: we don't send any error if some data were already sent */ |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 5800 | stream_int_retnclose(&s->si[0], NULL); |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 5801 | } else { |
| 5802 | txn->status = 502; |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 5803 | stream_int_retnclose(&s->si[0], http_error_message(s, HTTP_ERR_502)); |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 5804 | } |
| 5805 | req->analysers = 0; |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 5806 | s->res.analysers = 0; /* we're in data phase, we want to abort both directions */ |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 5807 | |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 5808 | sess->fe->fe_counters.srv_aborts++; |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 5809 | s->be->be_counters.srv_aborts++; |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 5810 | if (objt_server(s->target)) |
| 5811 | objt_server(s->target)->counters.srv_aborts++; |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 5812 | |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 5813 | if (!(s->flags & SF_ERR_MASK)) |
| 5814 | s->flags |= SF_ERR_SRVCL; |
| 5815 | if (!(s->flags & SF_FINST_MASK)) { |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 5816 | if (txn->rsp.msg_state < HTTP_MSG_ERROR) |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 5817 | s->flags |= SF_FINST_H; |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 5818 | else |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 5819 | s->flags |= SF_FINST_D; |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 5820 | } |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5821 | return 0; |
| 5822 | } |
| 5823 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5824 | /* This stream analyser waits for a complete HTTP response. It returns 1 if the |
| 5825 | * processing can continue on next analysers, or zero if it either needs more |
| 5826 | * data or wants to immediately abort the response (eg: timeout, error, ...). It |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 5827 | * 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] | 5828 | * when it has nothing left to do, and may remove any analyser when it wants to |
| 5829 | * abort. |
Willy Tarreau | c65a3ba | 2008-08-11 23:42:50 +0200 | [diff] [blame] | 5830 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 5831 | 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] | 5832 | { |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 5833 | struct session *sess = s->sess; |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 5834 | struct http_txn *txn = s->txn; |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5835 | struct http_msg *msg = &txn->rsp; |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 5836 | struct hdr_ctx ctx; |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 5837 | int use_close_only; |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5838 | int cur_idx; |
Krzysztof Piotr Oledzki | 5fb1882 | 2009-10-13 21:14:09 +0200 | [diff] [blame] | 5839 | int n; |
Willy Tarreau | adfb856 | 2008-08-11 15:24:42 +0200 | [diff] [blame] | 5840 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 5841 | 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] | 5842 | now_ms, __FUNCTION__, |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5843 | s, |
Willy Tarreau | 3a16b2c | 2008-08-28 08:54:27 +0200 | [diff] [blame] | 5844 | rep, |
| 5845 | rep->rex, rep->wex, |
| 5846 | rep->flags, |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 5847 | rep->buf->i, |
Willy Tarreau | 3a16b2c | 2008-08-28 08:54:27 +0200 | [diff] [blame] | 5848 | rep->analysers); |
Willy Tarreau | 67f0eea | 2008-08-10 22:55:22 +0200 | [diff] [blame] | 5849 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5850 | /* |
| 5851 | * Now parse the partial (or complete) lines. |
| 5852 | * We will check the response syntax, and also join multi-line |
| 5853 | * headers. An index of all the lines will be elaborated while |
| 5854 | * parsing. |
| 5855 | * |
| 5856 | * For the parsing, we use a 28 states FSM. |
| 5857 | * |
| 5858 | * Here is the information we currently have : |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 5859 | * rep->buf->p = beginning of response |
| 5860 | * rep->buf->p + msg->eoh = end of processed headers / start of current one |
| 5861 | * rep->buf->p + rep->buf->i = end of input data |
Willy Tarreau | 2692736 | 2012-05-18 23:22:52 +0200 | [diff] [blame] | 5862 | * msg->eol = end of current header or line (LF or CRLF) |
| 5863 | * msg->next = first non-visited byte |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5864 | */ |
| 5865 | |
Willy Tarreau | 628c40c | 2014-04-24 19:11:26 +0200 | [diff] [blame] | 5866 | next_one: |
Willy Tarreau | 83e3af0 | 2009-12-28 17:39:57 +0100 | [diff] [blame] | 5867 | /* There's a protected area at the end of the buffer for rewriting |
| 5868 | * purposes. We don't want to start to parse the request if the |
| 5869 | * protected area is affected, because we may have to move processed |
| 5870 | * data later, which is much more complicated. |
| 5871 | */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 5872 | if (buffer_not_empty(rep->buf) && msg->msg_state < HTTP_MSG_ERROR) { |
Willy Tarreau | ba0902e | 2015-01-13 14:39:16 +0100 | [diff] [blame] | 5873 | if (unlikely(!channel_is_rewritable(rep))) { |
Willy Tarreau | 379357a | 2013-06-08 12:55:46 +0200 | [diff] [blame] | 5874 | /* some data has still not left the buffer, wake us once that's done */ |
| 5875 | if (rep->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_WRITE_ERROR|CF_WRITE_TIMEOUT)) |
| 5876 | goto abort_response; |
| 5877 | channel_dont_close(rep); |
| 5878 | rep->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */ |
Willy Tarreau | d7ad9f5 | 2013-12-31 17:26:25 +0100 | [diff] [blame] | 5879 | rep->flags |= CF_WAKE_WRITE; |
Willy Tarreau | 379357a | 2013-06-08 12:55:46 +0200 | [diff] [blame] | 5880 | return 0; |
Willy Tarreau | 83e3af0 | 2009-12-28 17:39:57 +0100 | [diff] [blame] | 5881 | } |
| 5882 | |
Willy Tarreau | 379357a | 2013-06-08 12:55:46 +0200 | [diff] [blame] | 5883 | if (unlikely(bi_end(rep->buf) < b_ptr(rep->buf, msg->next) || |
| 5884 | bi_end(rep->buf) > rep->buf->data + rep->buf->size - global.tune.maxrewrite)) |
| 5885 | buffer_slow_realign(rep->buf); |
| 5886 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 5887 | if (likely(msg->next < rep->buf->i)) |
Willy Tarreau | a560c21 | 2012-03-09 13:50:57 +0100 | [diff] [blame] | 5888 | http_msg_analyzer(msg, &txn->hdr_idx); |
Willy Tarreau | 83e3af0 | 2009-12-28 17:39:57 +0100 | [diff] [blame] | 5889 | } |
| 5890 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5891 | /* 1: we might have to print this header in debug mode */ |
| 5892 | if (unlikely((global.mode & MODE_DEBUG) && |
| 5893 | (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) && |
Willy Tarreau | 7d59e90 | 2014-10-21 19:36:09 +0200 | [diff] [blame] | 5894 | msg->msg_state >= HTTP_MSG_BODY)) { |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5895 | char *eol, *sol; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5896 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 5897 | sol = rep->buf->p; |
| 5898 | 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] | 5899 | debug_hdr("srvrep", s, sol, eol); |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5900 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5901 | sol += hdr_idx_first_pos(&txn->hdr_idx); |
| 5902 | cur_idx = hdr_idx_first_idx(&txn->hdr_idx); |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5903 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5904 | while (cur_idx) { |
| 5905 | eol = sol + txn->hdr_idx.v[cur_idx].len; |
| 5906 | debug_hdr("srvhdr", s, sol, eol); |
| 5907 | sol = eol + txn->hdr_idx.v[cur_idx].cr + 1; |
| 5908 | cur_idx = txn->hdr_idx.v[cur_idx].next; |
| 5909 | } |
| 5910 | } |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5911 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5912 | /* |
| 5913 | * Now we quickly check if we have found a full valid response. |
| 5914 | * If not so, we check the FD and buffer states before leaving. |
| 5915 | * A full response is indicated by the fact that we have seen |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 5916 | * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5917 | * responses are checked first. |
| 5918 | * |
| 5919 | * Depending on whether the client is still there or not, we |
| 5920 | * may send an error response back or not. Note that normally |
| 5921 | * we should only check for HTTP status there, and check I/O |
| 5922 | * errors somewhere else. |
| 5923 | */ |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5924 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 5925 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) { |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5926 | /* Invalid response */ |
| 5927 | if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) { |
| 5928 | /* we detected a parsing error. We want to archive this response |
| 5929 | * in the dedicated proxy area for later troubleshooting. |
| 5930 | */ |
| 5931 | hdr_response_bad: |
| 5932 | if (msg->msg_state == HTTP_MSG_ERROR || msg->err_pos >= 0) |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 5933 | http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, sess->fe); |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5934 | |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 5935 | s->be->be_counters.failed_resp++; |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 5936 | if (objt_server(s->target)) { |
| 5937 | objt_server(s->target)->counters.failed_resp++; |
| 5938 | health_adjust(objt_server(s->target), HANA_STATUS_HTTP_HDRRSP); |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 5939 | } |
Willy Tarreau | 6464841 | 2010-03-05 10:41:54 +0100 | [diff] [blame] | 5940 | abort_response: |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 5941 | channel_auto_close(rep); |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5942 | rep->analysers = 0; |
| 5943 | txn->status = 502; |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 5944 | s->si[1].flags |= SI_FL_NOLINGER; |
Willy Tarreau | 319f745 | 2015-01-14 20:32:59 +0100 | [diff] [blame] | 5945 | channel_truncate(rep); |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 5946 | stream_int_retnclose(&s->si[0], http_error_message(s, HTTP_ERR_502)); |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5947 | |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 5948 | if (!(s->flags & SF_ERR_MASK)) |
| 5949 | s->flags |= SF_ERR_PRXCOND; |
| 5950 | if (!(s->flags & SF_FINST_MASK)) |
| 5951 | s->flags |= SF_FINST_H; |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5952 | |
| 5953 | return 0; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 5954 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 5955 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5956 | /* too large response does not fit in buffer. */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 5957 | else if (buffer_full(rep->buf, global.tune.maxrewrite)) { |
Willy Tarreau | fec4d89 | 2011-09-02 20:04:57 +0200 | [diff] [blame] | 5958 | if (msg->err_pos < 0) |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 5959 | msg->err_pos = rep->buf->i; |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5960 | goto hdr_response_bad; |
| 5961 | } |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5962 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5963 | /* read error */ |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 5964 | else if (rep->flags & CF_READ_ERROR) { |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5965 | if (msg->err_pos >= 0) |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 5966 | http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, sess->fe); |
Willy Tarreau | 6b726ad | 2013-12-15 19:31:37 +0100 | [diff] [blame] | 5967 | else if (txn->flags & TX_NOT_FIRST) |
| 5968 | goto abort_keep_alive; |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 5969 | |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 5970 | s->be->be_counters.failed_resp++; |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 5971 | if (objt_server(s->target)) { |
| 5972 | objt_server(s->target)->counters.failed_resp++; |
| 5973 | health_adjust(objt_server(s->target), HANA_STATUS_HTTP_READ_ERROR); |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 5974 | } |
Willy Tarreau | 461f662 | 2008-08-15 23:43:19 +0200 | [diff] [blame] | 5975 | |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 5976 | channel_auto_close(rep); |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5977 | rep->analysers = 0; |
| 5978 | txn->status = 502; |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 5979 | s->si[1].flags |= SI_FL_NOLINGER; |
Willy Tarreau | 319f745 | 2015-01-14 20:32:59 +0100 | [diff] [blame] | 5980 | channel_truncate(rep); |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 5981 | stream_int_retnclose(&s->si[0], http_error_message(s, HTTP_ERR_502)); |
Willy Tarreau | 816b979 | 2009-09-15 21:25:21 +0200 | [diff] [blame] | 5982 | |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 5983 | if (!(s->flags & SF_ERR_MASK)) |
| 5984 | s->flags |= SF_ERR_SRVCL; |
| 5985 | if (!(s->flags & SF_FINST_MASK)) |
| 5986 | s->flags |= SF_FINST_H; |
Willy Tarreau | cebf57e | 2008-08-15 18:16:37 +0200 | [diff] [blame] | 5987 | return 0; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5988 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 5989 | |
Willy Tarreau | 6f0a7ba | 2014-06-23 15:22:31 +0200 | [diff] [blame] | 5990 | /* read timeout : return a 504 to the client. */ |
| 5991 | else if (rep->flags & CF_READ_TIMEOUT) { |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5992 | if (msg->err_pos >= 0) |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 5993 | http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, sess->fe); |
Willy Tarreau | 6b726ad | 2013-12-15 19:31:37 +0100 | [diff] [blame] | 5994 | else if (txn->flags & TX_NOT_FIRST) |
| 5995 | goto abort_keep_alive; |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 5996 | |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 5997 | s->be->be_counters.failed_resp++; |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 5998 | if (objt_server(s->target)) { |
| 5999 | objt_server(s->target)->counters.failed_resp++; |
| 6000 | health_adjust(objt_server(s->target), HANA_STATUS_HTTP_READ_TIMEOUT); |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 6001 | } |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 6002 | |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 6003 | channel_auto_close(rep); |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 6004 | rep->analysers = 0; |
| 6005 | txn->status = 504; |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 6006 | s->si[1].flags |= SI_FL_NOLINGER; |
Willy Tarreau | 319f745 | 2015-01-14 20:32:59 +0100 | [diff] [blame] | 6007 | channel_truncate(rep); |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 6008 | stream_int_retnclose(&s->si[0], http_error_message(s, HTTP_ERR_504)); |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 6009 | |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 6010 | if (!(s->flags & SF_ERR_MASK)) |
| 6011 | s->flags |= SF_ERR_SRVTO; |
| 6012 | if (!(s->flags & SF_FINST_MASK)) |
| 6013 | s->flags |= SF_FINST_H; |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 6014 | return 0; |
| 6015 | } |
Willy Tarreau | a7c5276 | 2008-08-16 18:40:18 +0200 | [diff] [blame] | 6016 | |
Willy Tarreau | f003d37 | 2012-11-26 13:35:37 +0100 | [diff] [blame] | 6017 | /* client abort with an abortonclose */ |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 6018 | 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] | 6019 | sess->fe->fe_counters.cli_aborts++; |
Willy Tarreau | f003d37 | 2012-11-26 13:35:37 +0100 | [diff] [blame] | 6020 | s->be->be_counters.cli_aborts++; |
| 6021 | if (objt_server(s->target)) |
| 6022 | objt_server(s->target)->counters.cli_aborts++; |
| 6023 | |
| 6024 | rep->analysers = 0; |
| 6025 | channel_auto_close(rep); |
| 6026 | |
| 6027 | txn->status = 400; |
Willy Tarreau | 319f745 | 2015-01-14 20:32:59 +0100 | [diff] [blame] | 6028 | channel_truncate(rep); |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 6029 | stream_int_retnclose(&s->si[0], http_error_message(s, HTTP_ERR_400)); |
Willy Tarreau | f003d37 | 2012-11-26 13:35:37 +0100 | [diff] [blame] | 6030 | |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 6031 | if (!(s->flags & SF_ERR_MASK)) |
| 6032 | s->flags |= SF_ERR_CLICL; |
| 6033 | if (!(s->flags & SF_FINST_MASK)) |
| 6034 | s->flags |= SF_FINST_H; |
Willy Tarreau | f003d37 | 2012-11-26 13:35:37 +0100 | [diff] [blame] | 6035 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 6036 | /* process_stream() will take care of the error */ |
Willy Tarreau | f003d37 | 2012-11-26 13:35:37 +0100 | [diff] [blame] | 6037 | return 0; |
| 6038 | } |
| 6039 | |
Willy Tarreau | 3b8c08a | 2011-09-02 20:16:24 +0200 | [diff] [blame] | 6040 | /* 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] | 6041 | else if (rep->flags & CF_SHUTR) { |
Willy Tarreau | 3b8c08a | 2011-09-02 20:16:24 +0200 | [diff] [blame] | 6042 | if (msg->msg_state >= HTTP_MSG_RPVER || msg->err_pos >= 0) |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 6043 | http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, sess->fe); |
Willy Tarreau | 6b726ad | 2013-12-15 19:31:37 +0100 | [diff] [blame] | 6044 | else if (txn->flags & TX_NOT_FIRST) |
| 6045 | goto abort_keep_alive; |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 6046 | |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 6047 | s->be->be_counters.failed_resp++; |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 6048 | if (objt_server(s->target)) { |
| 6049 | objt_server(s->target)->counters.failed_resp++; |
| 6050 | health_adjust(objt_server(s->target), HANA_STATUS_HTTP_BROKEN_PIPE); |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 6051 | } |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 6052 | |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 6053 | channel_auto_close(rep); |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 6054 | rep->analysers = 0; |
| 6055 | txn->status = 502; |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 6056 | s->si[1].flags |= SI_FL_NOLINGER; |
Willy Tarreau | 319f745 | 2015-01-14 20:32:59 +0100 | [diff] [blame] | 6057 | channel_truncate(rep); |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 6058 | stream_int_retnclose(&s->si[0], http_error_message(s, HTTP_ERR_502)); |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 6059 | |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 6060 | if (!(s->flags & SF_ERR_MASK)) |
| 6061 | s->flags |= SF_ERR_SRVCL; |
| 6062 | if (!(s->flags & SF_FINST_MASK)) |
| 6063 | s->flags |= SF_FINST_H; |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 6064 | return 0; |
| 6065 | } |
Krzysztof Piotr Oledzki | 5fb1882 | 2009-10-13 21:14:09 +0200 | [diff] [blame] | 6066 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 6067 | /* write error to client (we don't send any message then) */ |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 6068 | else if (rep->flags & CF_WRITE_ERROR) { |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 6069 | if (msg->err_pos >= 0) |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 6070 | http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, sess->fe); |
Willy Tarreau | 6b726ad | 2013-12-15 19:31:37 +0100 | [diff] [blame] | 6071 | else if (txn->flags & TX_NOT_FIRST) |
| 6072 | goto abort_keep_alive; |
Krzysztof Piotr Oledzki | 5fb1882 | 2009-10-13 21:14:09 +0200 | [diff] [blame] | 6073 | |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 6074 | s->be->be_counters.failed_resp++; |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 6075 | rep->analysers = 0; |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 6076 | channel_auto_close(rep); |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 6077 | |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 6078 | if (!(s->flags & SF_ERR_MASK)) |
| 6079 | s->flags |= SF_ERR_CLICL; |
| 6080 | if (!(s->flags & SF_FINST_MASK)) |
| 6081 | s->flags |= SF_FINST_H; |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 6082 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 6083 | /* process_stream() will take care of the error */ |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 6084 | return 0; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 6085 | } |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 6086 | |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 6087 | channel_dont_close(rep); |
Willy Tarreau | 3f3997e | 2013-12-15 15:21:32 +0100 | [diff] [blame] | 6088 | rep->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */ |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 6089 | return 0; |
| 6090 | } |
| 6091 | |
| 6092 | /* More interesting part now : we know that we have a complete |
| 6093 | * response which at least looks like HTTP. We have an indicator |
| 6094 | * of each header's length, so we can parse them quickly. |
| 6095 | */ |
| 6096 | |
| 6097 | if (unlikely(msg->err_pos >= 0)) |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 6098 | http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, sess->fe); |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 6099 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 6100 | /* |
| 6101 | * 1: get the status code |
| 6102 | */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 6103 | n = rep->buf->p[msg->sl.st.c] - '0'; |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 6104 | if (n < 1 || n > 5) |
| 6105 | n = 0; |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 6106 | /* when the client triggers a 4xx from the server, it's most often due |
| 6107 | * to a missing object or permission. These events should be tracked |
| 6108 | * because if they happen often, it may indicate a brute force or a |
| 6109 | * vulnerability scan. |
| 6110 | */ |
| 6111 | if (n == 4) |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 6112 | stream_inc_http_err_ctr(s); |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 6113 | |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 6114 | if (objt_server(s->target)) |
| 6115 | objt_server(s->target)->counters.p.http.rsp[n]++; |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 6116 | |
Willy Tarreau | 91852eb | 2015-05-01 13:26:00 +0200 | [diff] [blame] | 6117 | /* RFC7230#2.6 has enforced the format of the HTTP version string to be |
| 6118 | * exactly one digit "." one digit. This check may be disabled using |
| 6119 | * option accept-invalid-http-response. |
| 6120 | */ |
| 6121 | if (!(s->be->options2 & PR_O2_RSPBUG_OK)) { |
| 6122 | if (msg->sl.st.v_l != 8) { |
| 6123 | msg->err_pos = 0; |
| 6124 | goto hdr_response_bad; |
| 6125 | } |
| 6126 | |
| 6127 | if (rep->buf->p[4] != '/' || |
| 6128 | !isdigit((unsigned char)rep->buf->p[5]) || |
| 6129 | rep->buf->p[6] != '.' || |
| 6130 | !isdigit((unsigned char)rep->buf->p[7])) { |
| 6131 | msg->err_pos = 4; |
| 6132 | goto hdr_response_bad; |
| 6133 | } |
| 6134 | } |
| 6135 | |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 6136 | /* check if the response is HTTP/1.1 or above */ |
| 6137 | if ((msg->sl.st.v_l == 8) && |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 6138 | ((rep->buf->p[5] > '1') || |
| 6139 | ((rep->buf->p[5] == '1') && (rep->buf->p[7] >= '1')))) |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 6140 | msg->flags |= HTTP_MSGF_VER_11; |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 6141 | |
| 6142 | /* "connection" has not been parsed yet */ |
Willy Tarreau | 50fc777 | 2012-11-11 22:19:57 +0100 | [diff] [blame] | 6143 | 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] | 6144 | |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 6145 | /* transfer length unknown*/ |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 6146 | msg->flags &= ~HTTP_MSGF_XFER_LEN; |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 6147 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 6148 | 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] | 6149 | |
Willy Tarreau | 3965040 | 2010-03-15 19:44:39 +0100 | [diff] [blame] | 6150 | /* Adjust server's health based on status code. Note: status codes 501 |
| 6151 | * and 505 are triggered on demand by client request, so we must not |
| 6152 | * count them as server failures. |
| 6153 | */ |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 6154 | if (objt_server(s->target)) { |
Willy Tarreau | d45b3d5 | 2010-05-20 11:49:03 +0200 | [diff] [blame] | 6155 | 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] | 6156 | health_adjust(objt_server(s->target), HANA_STATUS_HTTP_OK); |
Willy Tarreau | d45b3d5 | 2010-05-20 11:49:03 +0200 | [diff] [blame] | 6157 | else |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 6158 | health_adjust(objt_server(s->target), HANA_STATUS_HTTP_STS); |
Willy Tarreau | d45b3d5 | 2010-05-20 11:49:03 +0200 | [diff] [blame] | 6159 | } |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 6160 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 6161 | /* |
| 6162 | * 2: check for cacheability. |
| 6163 | */ |
| 6164 | |
| 6165 | switch (txn->status) { |
Willy Tarreau | 628c40c | 2014-04-24 19:11:26 +0200 | [diff] [blame] | 6166 | case 100: |
| 6167 | /* |
| 6168 | * We may be facing a 100-continue response, in which case this |
| 6169 | * is not the right response, and we're waiting for the next one. |
| 6170 | * Let's allow this response to go to the client and wait for the |
| 6171 | * next one. |
| 6172 | */ |
| 6173 | hdr_idx_init(&txn->hdr_idx); |
| 6174 | msg->next -= channel_forward(rep, msg->next); |
| 6175 | msg->msg_state = HTTP_MSG_RPBEFORE; |
| 6176 | txn->status = 0; |
| 6177 | s->logs.t_data = -1; /* was not a response yet */ |
| 6178 | goto next_one; |
| 6179 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 6180 | case 200: |
| 6181 | case 203: |
| 6182 | case 206: |
| 6183 | case 300: |
| 6184 | case 301: |
| 6185 | case 410: |
| 6186 | /* RFC2616 @13.4: |
| 6187 | * "A response received with a status code of |
| 6188 | * 200, 203, 206, 300, 301 or 410 MAY be stored |
| 6189 | * by a cache (...) unless a cache-control |
| 6190 | * directive prohibits caching." |
| 6191 | * |
| 6192 | * RFC2616 @9.5: POST method : |
| 6193 | * "Responses to this method are not cacheable, |
| 6194 | * unless the response includes appropriate |
| 6195 | * Cache-Control or Expires header fields." |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 6196 | */ |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 6197 | if (likely(txn->meth != HTTP_METH_POST) && |
Willy Tarreau | 6740213 | 2012-05-31 20:40:20 +0200 | [diff] [blame] | 6198 | ((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] | 6199 | txn->flags |= TX_CACHEABLE | TX_CACHE_COOK; |
| 6200 | break; |
| 6201 | default: |
| 6202 | break; |
| 6203 | } |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 6204 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 6205 | /* |
| 6206 | * 3: we may need to capture headers |
| 6207 | */ |
| 6208 | s->logs.logwait &= ~LW_RESP; |
Willy Tarreau | cb7dd01 | 2015-04-03 22:16:32 +0200 | [diff] [blame] | 6209 | if (unlikely((s->logs.logwait & LW_RSPHDR) && s->res_cap)) |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 6210 | capture_headers(rep->buf->p, &txn->hdr_idx, |
Willy Tarreau | cb7dd01 | 2015-04-03 22:16:32 +0200 | [diff] [blame] | 6211 | s->res_cap, sess->fe->rsp_cap); |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 6212 | |
Willy Tarreau | 557f199 | 2015-05-01 10:05:17 +0200 | [diff] [blame] | 6213 | /* 4: determine the transfer-length according to RFC2616 #4.4, updated |
| 6214 | * by RFC7230#3.3.3 : |
| 6215 | * |
| 6216 | * The length of a message body is determined by one of the following |
| 6217 | * (in order of precedence): |
| 6218 | * |
| 6219 | * 1. Any response to a HEAD request and any response with a 1xx |
| 6220 | * (Informational), 204 (No Content), or 304 (Not Modified) status |
| 6221 | * code is always terminated by the first empty line after the |
| 6222 | * header fields, regardless of the header fields present in the |
| 6223 | * message, and thus cannot contain a message body. |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 6224 | * |
Willy Tarreau | 557f199 | 2015-05-01 10:05:17 +0200 | [diff] [blame] | 6225 | * 2. Any 2xx (Successful) response to a CONNECT request implies that |
| 6226 | * the connection will become a tunnel immediately after the empty |
| 6227 | * line that concludes the header fields. A client MUST ignore any |
| 6228 | * Content-Length or Transfer-Encoding header fields received in |
| 6229 | * such a message. |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 6230 | * |
Willy Tarreau | 557f199 | 2015-05-01 10:05:17 +0200 | [diff] [blame] | 6231 | * 3. If a Transfer-Encoding header field is present and the chunked |
| 6232 | * transfer coding (Section 4.1) is the final encoding, the message |
| 6233 | * body length is determined by reading and decoding the chunked |
| 6234 | * data until the transfer coding indicates the data is complete. |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 6235 | * |
Willy Tarreau | 557f199 | 2015-05-01 10:05:17 +0200 | [diff] [blame] | 6236 | * If a Transfer-Encoding header field is present in a response and |
| 6237 | * the chunked transfer coding is not the final encoding, the |
| 6238 | * message body length is determined by reading the connection until |
| 6239 | * it is closed by the server. If a Transfer-Encoding header field |
| 6240 | * is present in a request and the chunked transfer coding is not |
| 6241 | * the final encoding, the message body length cannot be determined |
| 6242 | * reliably; the server MUST respond with the 400 (Bad Request) |
| 6243 | * status code and then close the connection. |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 6244 | * |
Willy Tarreau | 557f199 | 2015-05-01 10:05:17 +0200 | [diff] [blame] | 6245 | * If a message is received with both a Transfer-Encoding and a |
| 6246 | * Content-Length header field, the Transfer-Encoding overrides the |
| 6247 | * Content-Length. Such a message might indicate an attempt to |
| 6248 | * perform request smuggling (Section 9.5) or response splitting |
| 6249 | * (Section 9.4) and ought to be handled as an error. A sender MUST |
| 6250 | * remove the received Content-Length field prior to forwarding such |
| 6251 | * a message downstream. |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 6252 | * |
Willy Tarreau | 557f199 | 2015-05-01 10:05:17 +0200 | [diff] [blame] | 6253 | * 4. If a message is received without Transfer-Encoding and with |
| 6254 | * either multiple Content-Length header fields having differing |
| 6255 | * field-values or a single Content-Length header field having an |
| 6256 | * invalid value, then the message framing is invalid and the |
| 6257 | * recipient MUST treat it as an unrecoverable error. If this is a |
| 6258 | * request message, the server MUST respond with a 400 (Bad Request) |
| 6259 | * status code and then close the connection. If this is a response |
| 6260 | * message received by a proxy, the proxy MUST close the connection |
| 6261 | * to the server, discard the received response, and send a 502 (Bad |
| 6262 | * Gateway) response to the client. If this is a response message |
| 6263 | * received by a user agent, the user agent MUST close the |
| 6264 | * connection to the server and discard the received response. |
| 6265 | * |
| 6266 | * 5. If a valid Content-Length header field is present without |
| 6267 | * Transfer-Encoding, its decimal value defines the expected message |
| 6268 | * body length in octets. If the sender closes the connection or |
| 6269 | * the recipient times out before the indicated number of octets are |
| 6270 | * received, the recipient MUST consider the message to be |
| 6271 | * incomplete and close the connection. |
| 6272 | * |
| 6273 | * 6. If this is a request message and none of the above are true, then |
| 6274 | * the message body length is zero (no message body is present). |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 6275 | * |
Willy Tarreau | 557f199 | 2015-05-01 10:05:17 +0200 | [diff] [blame] | 6276 | * 7. Otherwise, this is a response message without a declared message |
| 6277 | * body length, so the message body length is determined by the |
| 6278 | * number of octets received prior to the server closing the |
| 6279 | * connection. |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 6280 | */ |
| 6281 | |
| 6282 | /* Skip parsing if no content length is possible. The response flags |
Willy Tarreau | 124d991 | 2011-03-01 20:30:48 +0100 | [diff] [blame] | 6283 | * 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] | 6284 | * 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] | 6285 | * FIXME: should we parse anyway and return an error on chunked encoding ? |
| 6286 | */ |
| 6287 | if (txn->meth == HTTP_METH_HEAD || |
| 6288 | (txn->status >= 100 && txn->status < 200) || |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 6289 | txn->status == 204 || txn->status == 304) { |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 6290 | msg->flags |= HTTP_MSGF_XFER_LEN; |
Willy Tarreau | 9101535 | 2012-11-27 07:31:33 +0100 | [diff] [blame] | 6291 | s->comp_algo = NULL; |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 6292 | goto skip_content_length; |
| 6293 | } |
| 6294 | |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 6295 | use_close_only = 0; |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 6296 | ctx.idx = 0; |
Willy Tarreau | 4979d5c | 2015-05-01 10:06:30 +0200 | [diff] [blame] | 6297 | 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] | 6298 | if (ctx.vlen == 7 && strncasecmp(ctx.line + ctx.val, "chunked", 7) == 0) |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 6299 | msg->flags |= (HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN); |
| 6300 | else if (msg->flags & HTTP_MSGF_TE_CHNK) { |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 6301 | /* bad transfer-encoding (chunked followed by something else) */ |
| 6302 | use_close_only = 1; |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 6303 | msg->flags &= ~(HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN); |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 6304 | break; |
| 6305 | } |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 6306 | } |
| 6307 | |
Willy Tarreau | 1c91391 | 2015-04-30 10:57:51 +0200 | [diff] [blame] | 6308 | /* Chunked responses must have their content-length removed */ |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 6309 | ctx.idx = 0; |
Willy Tarreau | b4d0c03 | 2015-05-01 10:25:45 +0200 | [diff] [blame] | 6310 | if (use_close_only || (msg->flags & HTTP_MSGF_TE_CHNK)) { |
Willy Tarreau | 1c91391 | 2015-04-30 10:57:51 +0200 | [diff] [blame] | 6311 | while (http_find_header2("Content-Length", 14, rep->buf->p, &txn->hdr_idx, &ctx)) |
| 6312 | http_remove_header2(msg, &txn->hdr_idx, &ctx); |
| 6313 | } |
Willy Tarreau | b4d0c03 | 2015-05-01 10:25:45 +0200 | [diff] [blame] | 6314 | 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] | 6315 | signed long long cl; |
| 6316 | |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 6317 | if (!ctx.vlen) { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 6318 | msg->err_pos = ctx.line + ctx.val - rep->buf->p; |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 6319 | goto hdr_response_bad; |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 6320 | } |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 6321 | |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 6322 | if (strl2llrc(ctx.line + ctx.val, ctx.vlen, &cl)) { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 6323 | msg->err_pos = ctx.line + ctx.val - rep->buf->p; |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 6324 | goto hdr_response_bad; /* parse failure */ |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 6325 | } |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 6326 | |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 6327 | if (cl < 0) { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 6328 | msg->err_pos = ctx.line + ctx.val - rep->buf->p; |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 6329 | goto hdr_response_bad; |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 6330 | } |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 6331 | |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 6332 | if ((msg->flags & HTTP_MSGF_CNT_LEN) && (msg->chunk_len != cl)) { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 6333 | msg->err_pos = ctx.line + ctx.val - rep->buf->p; |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 6334 | goto hdr_response_bad; /* already specified, was different */ |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 6335 | } |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 6336 | |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 6337 | msg->flags |= HTTP_MSGF_CNT_LEN | HTTP_MSGF_XFER_LEN; |
Willy Tarreau | 124d991 | 2011-03-01 20:30:48 +0100 | [diff] [blame] | 6338 | msg->body_len = msg->chunk_len = cl; |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 6339 | } |
| 6340 | |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 6341 | if (sess->fe->comp || s->be->comp) |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 6342 | select_compression_response_header(s, rep->buf); |
| 6343 | |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 6344 | skip_content_length: |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 6345 | /* Now we have to check if we need to modify the Connection header. |
| 6346 | * This is more difficult on the response than it is on the request, |
| 6347 | * because we can have two different HTTP versions and we don't know |
| 6348 | * how the client will interprete a response. For instance, let's say |
| 6349 | * that the client sends a keep-alive request in HTTP/1.0 and gets an |
| 6350 | * HTTP/1.1 response without any header. Maybe it will bound itself to |
| 6351 | * HTTP/1.0 because it only knows about it, and will consider the lack |
| 6352 | * of header as a close, or maybe it knows HTTP/1.1 and can consider |
| 6353 | * the lack of header as a keep-alive. Thus we will use two flags |
| 6354 | * indicating how a request MAY be understood by the client. In case |
| 6355 | * of multiple possibilities, we'll fix the header to be explicit. If |
| 6356 | * ambiguous cases such as both close and keepalive are seen, then we |
| 6357 | * will fall back to explicit close. Note that we won't take risks with |
| 6358 | * HTTP/1.0 clients which may not necessarily understand keep-alive. |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 6359 | * See doc/internals/connection-header.txt for the complete matrix. |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 6360 | */ |
| 6361 | |
Willy Tarreau | dc008c5 | 2010-02-01 16:20:08 +0100 | [diff] [blame] | 6362 | if (unlikely((txn->meth == HTTP_METH_CONNECT && txn->status == 200) || |
| 6363 | txn->status == 101)) { |
| 6364 | /* Either we've established an explicit tunnel, or we're |
| 6365 | * switching the protocol. In both cases, we're very unlikely |
Willy Tarreau | 5843d1a | 2010-02-01 15:13:32 +0100 | [diff] [blame] | 6366 | * to understand the next protocols. We have to switch to tunnel |
| 6367 | * mode, so that we transfer the request and responses then let |
| 6368 | * this protocol pass unmodified. When we later implement specific |
| 6369 | * parsers for such protocols, we'll want to check the Upgrade |
Willy Tarreau | dc008c5 | 2010-02-01 16:20:08 +0100 | [diff] [blame] | 6370 | * header which contains information about that protocol for |
| 6371 | * responses with status 101 (eg: see RFC2817 about TLS). |
Willy Tarreau | 5843d1a | 2010-02-01 15:13:32 +0100 | [diff] [blame] | 6372 | */ |
| 6373 | txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_TUN; |
| 6374 | } |
Willy Tarreau | dc008c5 | 2010-02-01 16:20:08 +0100 | [diff] [blame] | 6375 | else if ((txn->status >= 200) && !(txn->flags & TX_HDR_CONN_PRS) && |
| 6376 | ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN || |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 6377 | ((sess->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL || |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 6378 | (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL))) { |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 6379 | int to_del = 0; |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 6380 | |
Willy Tarreau | 70dffda | 2014-01-30 03:07:23 +0100 | [diff] [blame] | 6381 | /* this situation happens when combining pretend-keepalive with httpclose. */ |
| 6382 | if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL && |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 6383 | ((sess->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL || |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 6384 | (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL)) |
Willy Tarreau | 70dffda | 2014-01-30 03:07:23 +0100 | [diff] [blame] | 6385 | txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO; |
| 6386 | |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 6387 | /* on unknown transfer length, we must close */ |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 6388 | if (!(msg->flags & HTTP_MSGF_XFER_LEN) && |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 6389 | (txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) |
| 6390 | txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO; |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 6391 | |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 6392 | /* now adjust header transformations depending on current state */ |
| 6393 | if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN || |
| 6394 | (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) { |
| 6395 | to_del |= 2; /* remove "keep-alive" on any response */ |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 6396 | if (!(msg->flags & HTTP_MSGF_VER_11)) |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 6397 | to_del |= 1; /* remove "close" for HTTP/1.0 responses */ |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 6398 | } |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 6399 | else { /* SCL / KAL */ |
| 6400 | to_del |= 1; /* remove "close" on any response */ |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 6401 | if (txn->req.flags & msg->flags & HTTP_MSGF_VER_11) |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 6402 | to_del |= 2; /* remove "keep-alive" on pure 1.1 responses */ |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 6403 | } |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 6404 | |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 6405 | /* Parse and remove some headers from the connection header */ |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 6406 | http_parse_connection_header(txn, msg, to_del); |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 6407 | |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 6408 | /* Some keep-alive responses are converted to Server-close if |
| 6409 | * the server wants to close. |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 6410 | */ |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 6411 | if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL) { |
| 6412 | if ((txn->flags & TX_HDR_CONN_CLO) || |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 6413 | (!(txn->flags & TX_HDR_CONN_KAL) && !(msg->flags & HTTP_MSGF_VER_11))) |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 6414 | txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_SCL; |
Willy Tarreau | b608feb | 2010-01-02 22:47:18 +0100 | [diff] [blame] | 6415 | } |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 6416 | } |
| 6417 | |
Willy Tarreau | 7959a55 | 2013-09-23 16:44:27 +0200 | [diff] [blame] | 6418 | /* we want to have the response time before we start processing it */ |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 6419 | s->logs.t_data = tv_ms_elapsed(&s->logs.tv_accept, &now); |
Willy Tarreau | 7959a55 | 2013-09-23 16:44:27 +0200 | [diff] [blame] | 6420 | |
Willy Tarreau | f118d9f | 2014-04-24 18:26:08 +0200 | [diff] [blame] | 6421 | /* end of job, return OK */ |
| 6422 | rep->analysers &= ~an_bit; |
| 6423 | rep->analyse_exp = TICK_ETERNITY; |
| 6424 | channel_auto_close(rep); |
| 6425 | return 1; |
| 6426 | |
| 6427 | abort_keep_alive: |
| 6428 | /* A keep-alive request to the server failed on a network error. |
| 6429 | * The client is required to retry. We need to close without returning |
| 6430 | * any other information so that the client retries. |
| 6431 | */ |
| 6432 | txn->status = 0; |
| 6433 | rep->analysers = 0; |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 6434 | s->req.analysers = 0; |
Willy Tarreau | f118d9f | 2014-04-24 18:26:08 +0200 | [diff] [blame] | 6435 | channel_auto_close(rep); |
| 6436 | s->logs.logwait = 0; |
| 6437 | s->logs.level = 0; |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 6438 | s->res.flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */ |
Willy Tarreau | 319f745 | 2015-01-14 20:32:59 +0100 | [diff] [blame] | 6439 | channel_truncate(rep); |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 6440 | stream_int_retnclose(&s->si[0], NULL); |
Willy Tarreau | f118d9f | 2014-04-24 18:26:08 +0200 | [diff] [blame] | 6441 | return 0; |
| 6442 | } |
| 6443 | |
| 6444 | /* This function performs all the processing enabled for the current response. |
| 6445 | * 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] | 6446 | * 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] | 6447 | * other functions. It works like process_request (see indications above). |
| 6448 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 6449 | 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] | 6450 | { |
Willy Tarreau | fb0afa7 | 2015-04-03 14:46:27 +0200 | [diff] [blame] | 6451 | struct session *sess = s->sess; |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 6452 | struct http_txn *txn = s->txn; |
Willy Tarreau | f118d9f | 2014-04-24 18:26:08 +0200 | [diff] [blame] | 6453 | struct http_msg *msg = &txn->rsp; |
| 6454 | struct proxy *cur_proxy; |
| 6455 | struct cond_wordlist *wl; |
Thierry FOURNIER | 9e2ef99 | 2015-02-25 13:51:19 +0100 | [diff] [blame] | 6456 | enum rule_result ret = HTTP_RULE_RES_CONT; |
Willy Tarreau | f118d9f | 2014-04-24 18:26:08 +0200 | [diff] [blame] | 6457 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 6458 | 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] | 6459 | now_ms, __FUNCTION__, |
| 6460 | s, |
| 6461 | rep, |
| 6462 | rep->rex, rep->wex, |
| 6463 | rep->flags, |
| 6464 | rep->buf->i, |
| 6465 | rep->analysers); |
| 6466 | |
| 6467 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) /* we need more data */ |
| 6468 | return 0; |
| 6469 | |
Willy Tarreau | 70730dd | 2014-04-24 18:06:27 +0200 | [diff] [blame] | 6470 | /* The stats applet needs to adjust the Connection header but we don't |
| 6471 | * apply any filter there. |
| 6472 | */ |
Willy Tarreau | 612adb8 | 2015-03-10 15:25:54 +0100 | [diff] [blame] | 6473 | if (unlikely(objt_applet(s->target) == &http_stats_applet)) { |
| 6474 | rep->analysers &= ~an_bit; |
| 6475 | rep->analyse_exp = TICK_ETERNITY; |
Willy Tarreau | 70730dd | 2014-04-24 18:06:27 +0200 | [diff] [blame] | 6476 | goto skip_filters; |
Willy Tarreau | 612adb8 | 2015-03-10 15:25:54 +0100 | [diff] [blame] | 6477 | } |
Willy Tarreau | 70730dd | 2014-04-24 18:06:27 +0200 | [diff] [blame] | 6478 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6479 | /* |
| 6480 | * We will have to evaluate the filters. |
| 6481 | * As opposed to version 1.2, now they will be evaluated in the |
| 6482 | * filters order and not in the header order. This means that |
| 6483 | * each filter has to be validated among all headers. |
| 6484 | * |
| 6485 | * Filters are tried with ->be first, then with ->fe if it is |
| 6486 | * different from ->be. |
Thierry FOURNIER | bc4c1ac | 2015-02-25 13:36:14 +0100 | [diff] [blame] | 6487 | * |
| 6488 | * Maybe we are in resume condiion. In this case I choose the |
| 6489 | * "struct proxy" which contains the rule list matching the resume |
| 6490 | * pointer. If none of theses "struct proxy" match, I initialise |
| 6491 | * the process with the first one. |
| 6492 | * |
| 6493 | * In fact, I check only correspondance betwwen the current list |
| 6494 | * pointer and the ->fe rule list. If it doesn't match, I initialize |
| 6495 | * the loop with the ->be. |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6496 | */ |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 6497 | if (s->current_rule_list == &sess->fe->http_res_rules) |
| 6498 | cur_proxy = sess->fe; |
Thierry FOURNIER | bc4c1ac | 2015-02-25 13:36:14 +0100 | [diff] [blame] | 6499 | else |
| 6500 | cur_proxy = s->be; |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6501 | while (1) { |
| 6502 | struct proxy *rule_set = cur_proxy; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 6503 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6504 | /* evaluate http-response rules */ |
Willy Tarreau | 51d861a | 2015-05-22 17:30:48 +0200 | [diff] [blame] | 6505 | if (ret == HTTP_RULE_RES_CONT) { |
Willy Tarreau | 987e3fb | 2015-04-04 01:09:08 +0200 | [diff] [blame] | 6506 | 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] | 6507 | |
Willy Tarreau | 51d861a | 2015-05-22 17:30:48 +0200 | [diff] [blame] | 6508 | if (ret == HTTP_RULE_RES_BADREQ) |
| 6509 | goto return_srv_prx_502; |
| 6510 | |
| 6511 | if (ret == HTTP_RULE_RES_DONE) { |
| 6512 | rep->analysers &= ~an_bit; |
| 6513 | rep->analyse_exp = TICK_ETERNITY; |
| 6514 | return 1; |
| 6515 | } |
| 6516 | } |
| 6517 | |
Thierry FOURNIER | bc4c1ac | 2015-02-25 13:36:14 +0100 | [diff] [blame] | 6518 | /* we need to be called again. */ |
| 6519 | if (ret == HTTP_RULE_RES_YIELD) { |
| 6520 | channel_dont_close(rep); |
| 6521 | return 0; |
| 6522 | } |
| 6523 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6524 | /* try headers filters */ |
| 6525 | if (rule_set->rsp_exp != NULL) { |
| 6526 | if (apply_filters_to_response(s, rep, rule_set) < 0) { |
| 6527 | return_bad_resp: |
| 6528 | if (objt_server(s->target)) { |
| 6529 | objt_server(s->target)->counters.failed_resp++; |
| 6530 | health_adjust(objt_server(s->target), HANA_STATUS_HTTP_RSP); |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 6531 | } |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6532 | s->be->be_counters.failed_resp++; |
| 6533 | return_srv_prx_502: |
| 6534 | rep->analysers = 0; |
| 6535 | txn->status = 502; |
| 6536 | s->logs.t_data = -1; /* was not a valid response */ |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 6537 | s->si[1].flags |= SI_FL_NOLINGER; |
Willy Tarreau | 319f745 | 2015-01-14 20:32:59 +0100 | [diff] [blame] | 6538 | channel_truncate(rep); |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 6539 | stream_int_retnclose(&s->si[0], http_error_message(s, HTTP_ERR_502)); |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 6540 | if (!(s->flags & SF_ERR_MASK)) |
| 6541 | s->flags |= SF_ERR_PRXCOND; |
| 6542 | if (!(s->flags & SF_FINST_MASK)) |
| 6543 | s->flags |= SF_FINST_H; |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6544 | return 0; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 6545 | } |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6546 | } |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 6547 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6548 | /* has the response been denied ? */ |
| 6549 | if (txn->flags & TX_SVDENY) { |
| 6550 | if (objt_server(s->target)) |
| 6551 | objt_server(s->target)->counters.failed_secu++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 6552 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6553 | s->be->be_counters.denied_resp++; |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 6554 | sess->fe->fe_counters.denied_resp++; |
Willy Tarreau | fb0afa7 | 2015-04-03 14:46:27 +0200 | [diff] [blame] | 6555 | if (sess->listener->counters) |
| 6556 | sess->listener->counters->denied_resp++; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 6557 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6558 | goto return_srv_prx_502; |
| 6559 | } |
Willy Tarreau | 0bbc3cf | 2006-10-15 14:26:02 +0200 | [diff] [blame] | 6560 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6561 | /* add response headers from the rule sets in the same order */ |
| 6562 | list_for_each_entry(wl, &rule_set->rsp_add, list) { |
Willy Tarreau | ce730de | 2014-09-16 10:40:38 +0200 | [diff] [blame] | 6563 | if (txn->status < 200 && txn->status != 101) |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 6564 | break; |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6565 | if (wl->cond) { |
Willy Tarreau | 192252e | 2015-04-04 01:47:55 +0200 | [diff] [blame] | 6566 | 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] | 6567 | ret = acl_pass(ret); |
| 6568 | if (((struct acl_cond *)wl->cond)->pol == ACL_COND_UNLESS) |
| 6569 | ret = !ret; |
| 6570 | if (!ret) |
| 6571 | continue; |
| 6572 | } |
| 6573 | if (unlikely(http_header_add_tail(&txn->rsp, &txn->hdr_idx, wl->s) < 0)) |
| 6574 | goto return_bad_resp; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 6575 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 6576 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6577 | /* check whether we're already working on the frontend */ |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 6578 | if (cur_proxy == sess->fe) |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6579 | break; |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 6580 | cur_proxy = sess->fe; |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6581 | } |
Willy Tarreau | 63c9e5f | 2009-12-22 16:01:27 +0100 | [diff] [blame] | 6582 | |
Thierry FOURNIER | bc4c1ac | 2015-02-25 13:36:14 +0100 | [diff] [blame] | 6583 | /* After this point, this anayzer can't return yield, so we can |
| 6584 | * remove the bit corresponding to this analyzer from the list. |
| 6585 | * |
| 6586 | * Note that the intermediate returns and goto found previously |
| 6587 | * reset the analyzers. |
| 6588 | */ |
| 6589 | rep->analysers &= ~an_bit; |
| 6590 | rep->analyse_exp = TICK_ETERNITY; |
| 6591 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6592 | /* OK that's all we can do for 1xx responses */ |
Willy Tarreau | ce730de | 2014-09-16 10:40:38 +0200 | [diff] [blame] | 6593 | if (unlikely(txn->status < 200 && txn->status != 101)) |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6594 | goto skip_header_mangling; |
Willy Tarreau | 63c9e5f | 2009-12-22 16:01:27 +0100 | [diff] [blame] | 6595 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6596 | /* |
| 6597 | * Now check for a server cookie. |
| 6598 | */ |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 6599 | if (s->be->cookie_name || s->be->appsession_name || sess->fe->capture_name || |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6600 | (s->be->options & PR_O_CHK_CACHE)) |
| 6601 | manage_server_side_cookies(s, rep); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 6602 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6603 | /* |
| 6604 | * Check for cache-control or pragma headers if required. |
| 6605 | */ |
Willy Tarreau | ce730de | 2014-09-16 10:40:38 +0200 | [diff] [blame] | 6606 | 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] | 6607 | check_response_for_cacheability(s, rep); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 6608 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6609 | /* |
| 6610 | * Add server cookie in the response if needed |
| 6611 | */ |
| 6612 | if (objt_server(s->target) && (s->be->ck_opts & PR_CK_INS) && |
| 6613 | !((txn->flags & TX_SCK_FOUND) && (s->be->ck_opts & PR_CK_PSV)) && |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 6614 | (!(s->flags & SF_DIRECT) || |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6615 | ((s->be->cookie_maxidle || txn->cookie_last_date) && |
| 6616 | (!txn->cookie_last_date || (txn->cookie_last_date - date.tv_sec) < 0)) || |
| 6617 | (s->be->cookie_maxlife && !txn->cookie_first_date) || // set the first_date |
| 6618 | (!s->be->cookie_maxlife && txn->cookie_first_date)) && // remove the first_date |
| 6619 | (!(s->be->ck_opts & PR_CK_POST) || (txn->meth == HTTP_METH_POST)) && |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 6620 | !(s->flags & SF_IGNORE_PRST)) { |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6621 | /* the server is known, it's not the one the client requested, or the |
| 6622 | * cookie's last seen date needs to be refreshed. We have to |
| 6623 | * insert a set-cookie here, except if we want to insert only on POST |
| 6624 | * requests and this one isn't. Note that servers which don't have cookies |
| 6625 | * (eg: some backup servers) will return a full cookie removal request. |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6626 | */ |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6627 | if (!objt_server(s->target)->cookie) { |
| 6628 | chunk_printf(&trash, |
| 6629 | "Set-Cookie: %s=; Expires=Thu, 01-Jan-1970 00:00:01 GMT; path=/", |
| 6630 | s->be->cookie_name); |
| 6631 | } |
| 6632 | else { |
| 6633 | 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] | 6634 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6635 | if (s->be->cookie_maxidle || s->be->cookie_maxlife) { |
| 6636 | /* emit last_date, which is mandatory */ |
| 6637 | trash.str[trash.len++] = COOKIE_DELIM_DATE; |
| 6638 | s30tob64((date.tv_sec+3) >> 2, trash.str + trash.len); |
| 6639 | trash.len += 5; |
Willy Tarreau | ef4f391 | 2010-10-07 21:00:29 +0200 | [diff] [blame] | 6640 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6641 | if (s->be->cookie_maxlife) { |
| 6642 | /* emit first_date, which is either the original one or |
| 6643 | * the current date. |
| 6644 | */ |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 6645 | trash.str[trash.len++] = COOKIE_DELIM_DATE; |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6646 | s30tob64(txn->cookie_first_date ? |
| 6647 | txn->cookie_first_date >> 2 : |
| 6648 | (date.tv_sec+3) >> 2, trash.str + trash.len); |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 6649 | trash.len += 5; |
Willy Tarreau | ef4f391 | 2010-10-07 21:00:29 +0200 | [diff] [blame] | 6650 | } |
Willy Tarreau | ef4f391 | 2010-10-07 21:00:29 +0200 | [diff] [blame] | 6651 | } |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6652 | chunk_appendf(&trash, "; path=/"); |
| 6653 | } |
Willy Tarreau | 4992dd2 | 2012-05-31 21:02:17 +0200 | [diff] [blame] | 6654 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6655 | if (s->be->cookie_domain) |
| 6656 | chunk_appendf(&trash, "; domain=%s", s->be->cookie_domain); |
Willy Tarreau | ef4f391 | 2010-10-07 21:00:29 +0200 | [diff] [blame] | 6657 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6658 | if (s->be->ck_opts & PR_CK_HTTPONLY) |
| 6659 | chunk_appendf(&trash, "; HttpOnly"); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 6660 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6661 | if (s->be->ck_opts & PR_CK_SECURE) |
| 6662 | chunk_appendf(&trash, "; Secure"); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 6663 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6664 | if (unlikely(http_header_add_tail2(&txn->rsp, &txn->hdr_idx, trash.str, trash.len) < 0)) |
| 6665 | goto return_bad_resp; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 6666 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6667 | txn->flags &= ~TX_SCK_MASK; |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 6668 | if (objt_server(s->target)->cookie && (s->flags & SF_DIRECT)) |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6669 | /* the server did not change, only the date was updated */ |
| 6670 | txn->flags |= TX_SCK_UPDATED; |
| 6671 | else |
| 6672 | txn->flags |= TX_SCK_INSERTED; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 6673 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6674 | /* Here, we will tell an eventual cache on the client side that we don't |
| 6675 | * want it to cache this reply because HTTP/1.0 caches also cache cookies ! |
| 6676 | * Some caches understand the correct form: 'no-cache="set-cookie"', but |
| 6677 | * 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] | 6678 | */ |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6679 | if ((s->be->ck_opts & PR_CK_NOC) && (txn->flags & TX_CACHEABLE)) { |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 6680 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6681 | txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 6682 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6683 | if (unlikely(http_header_add_tail2(&txn->rsp, &txn->hdr_idx, |
| 6684 | "Cache-control: private", 22) < 0)) |
| 6685 | goto return_bad_resp; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 6686 | } |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6687 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6688 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6689 | /* |
| 6690 | * Check if result will be cacheable with a cookie. |
| 6691 | * We'll block the response if security checks have caught |
| 6692 | * nasty things such as a cacheable cookie. |
| 6693 | */ |
| 6694 | if (((txn->flags & (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) == |
| 6695 | (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) && |
| 6696 | (s->be->options & PR_O_CHK_CACHE)) { |
| 6697 | /* we're in presence of a cacheable response containing |
| 6698 | * a set-cookie header. We'll block it as requested by |
| 6699 | * the 'checkcache' option, and send an alert. |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6700 | */ |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6701 | if (objt_server(s->target)) |
| 6702 | objt_server(s->target)->counters.failed_secu++; |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 6703 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6704 | s->be->be_counters.denied_resp++; |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 6705 | sess->fe->fe_counters.denied_resp++; |
Willy Tarreau | fb0afa7 | 2015-04-03 14:46:27 +0200 | [diff] [blame] | 6706 | if (sess->listener->counters) |
| 6707 | sess->listener->counters->denied_resp++; |
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 | Alert("Blocking cacheable cookie in response from instance %s, server %s.\n", |
| 6710 | s->be->id, objt_server(s->target) ? objt_server(s->target)->id : "<dispatch>"); |
| 6711 | send_log(s->be, LOG_ALERT, |
| 6712 | "Blocking cacheable cookie in response from instance %s, server %s.\n", |
| 6713 | s->be->id, objt_server(s->target) ? objt_server(s->target)->id : "<dispatch>"); |
| 6714 | goto return_srv_prx_502; |
| 6715 | } |
Willy Tarreau | 0394594 | 2009-12-22 16:50:27 +0100 | [diff] [blame] | 6716 | |
Willy Tarreau | 70730dd | 2014-04-24 18:06:27 +0200 | [diff] [blame] | 6717 | skip_filters: |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6718 | /* |
| 6719 | * Adjust "Connection: close" or "Connection: keep-alive" if needed. |
| 6720 | * If an "Upgrade" token is found, the header is left untouched in order |
| 6721 | * 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] | 6722 | * if anything but "Upgrade" is present in the Connection header. We don't |
| 6723 | * want to touch any 101 response either since it's switching to another |
| 6724 | * protocol. |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6725 | */ |
Willy Tarreau | ce730de | 2014-09-16 10:40:38 +0200 | [diff] [blame] | 6726 | if ((txn->status != 101) && !(txn->flags & TX_HDR_CONN_UPG) && |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6727 | (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) || |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 6728 | ((sess->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL || |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6729 | (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL))) { |
| 6730 | unsigned int want_flags = 0; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 6731 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6732 | if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL || |
| 6733 | (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) { |
| 6734 | /* we want a keep-alive response here. Keep-alive header |
| 6735 | * required if either side is not 1.1. |
| 6736 | */ |
| 6737 | if (!(txn->req.flags & msg->flags & HTTP_MSGF_VER_11)) |
| 6738 | want_flags |= TX_CON_KAL_SET; |
| 6739 | } |
| 6740 | else { |
| 6741 | /* we want a close response here. Close header required if |
| 6742 | * the server is 1.1, regardless of the client. |
| 6743 | */ |
| 6744 | if (msg->flags & HTTP_MSGF_VER_11) |
| 6745 | want_flags |= TX_CON_CLO_SET; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 6746 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6747 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6748 | if (want_flags != (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET))) |
| 6749 | http_change_connection_header(txn, msg, want_flags); |
| 6750 | } |
| 6751 | |
| 6752 | skip_header_mangling: |
| 6753 | if ((msg->flags & HTTP_MSGF_XFER_LEN) || |
| 6754 | (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN) |
| 6755 | rep->analysers |= AN_RES_HTTP_XFER_BODY; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6756 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6757 | /* if the user wants to log as soon as possible, without counting |
| 6758 | * bytes from the server, then this is the right moment. We have |
| 6759 | * to temporarily assign bytes_out to log what we currently have. |
| 6760 | */ |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 6761 | if (!LIST_ISEMPTY(&sess->fe->logformat) && !(s->logs.logwait & LW_BYTES)) { |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6762 | s->logs.t_close = s->logs.t_data; /* to get a valid end date */ |
| 6763 | s->logs.bytes_out = txn->rsp.eoh; |
| 6764 | s->do_log(s); |
| 6765 | s->logs.bytes_out = 0; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 6766 | } |
Willy Tarreau | e3fa6e5 | 2010-01-04 22:57:43 +0100 | [diff] [blame] | 6767 | return 1; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 6768 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6769 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 6770 | /* This function is an analyser which forwards response body (including chunk |
| 6771 | * sizes if any). It is called as soon as we must forward, even if we forward |
| 6772 | * zero byte. The only situation where it must not be called is when we're in |
| 6773 | * tunnel mode and we want to forward till the close. It's used both to forward |
| 6774 | * remaining data and to resync after end of body. It expects the msg_state to |
| 6775 | * 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] | 6776 | * 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] | 6777 | * |
| 6778 | * It is capable of compressing response data both in content-length mode and |
| 6779 | * in chunked mode. The state machines follows different flows depending on |
| 6780 | * whether content-length and chunked modes are used, since there are no |
| 6781 | * trailers in content-length : |
| 6782 | * |
| 6783 | * chk-mode cl-mode |
| 6784 | * ,----- BODY -----. |
| 6785 | * / \ |
| 6786 | * V size > 0 V chk-mode |
| 6787 | * .--> SIZE -------------> DATA -------------> CRLF |
| 6788 | * | | size == 0 | last byte | |
| 6789 | * | v final crlf v inspected | |
| 6790 | * | TRAILERS -----------> DONE | |
| 6791 | * | | |
| 6792 | * `----------------------------------------------' |
| 6793 | * |
| 6794 | * Compression only happens in the DATA state, and must be flushed in final |
| 6795 | * states (TRAILERS/DONE) or when leaving on missing data. Normal forwarding |
| 6796 | * is performed at once on final states for all bytes parsed, or when leaving |
| 6797 | * on missing data. |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 6798 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 6799 | 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] | 6800 | { |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 6801 | struct session *sess = s->sess; |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 6802 | struct http_txn *txn = s->txn; |
| 6803 | struct http_msg *msg = &s->txn->rsp; |
Willy Tarreau | f2f7d6b | 2014-11-24 11:55:08 +0100 | [diff] [blame] | 6804 | static struct buffer *tmpbuf = &buf_empty; |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 6805 | int compressing = 0; |
Willy Tarreau | d655ffe | 2013-04-02 01:48:58 +0200 | [diff] [blame] | 6806 | int ret; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 6807 | |
Willy Tarreau | f5c8bd6 | 2010-01-04 07:10:34 +0100 | [diff] [blame] | 6808 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) |
| 6809 | return 0; |
| 6810 | |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 6811 | 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] | 6812 | ((res->flags & CF_SHUTW) && (res->to_forward || res->buf->o)) || |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 6813 | !s->req.analysers) { |
Willy Tarreau | 4fe4190 | 2010-06-07 22:27:41 +0200 | [diff] [blame] | 6814 | /* Output closed while we were sending data. We must abort and |
| 6815 | * wake the other side up. |
| 6816 | */ |
| 6817 | msg->msg_state = HTTP_MSG_ERROR; |
| 6818 | http_resync_states(s); |
Willy Tarreau | 082b01c | 2010-01-02 23:58:04 +0100 | [diff] [blame] | 6819 | return 1; |
| 6820 | } |
| 6821 | |
Willy Tarreau | 4fe4190 | 2010-06-07 22:27:41 +0200 | [diff] [blame] | 6822 | /* in most states, we should abort in case of early close */ |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 6823 | channel_auto_close(res); |
Willy Tarreau | b608feb | 2010-01-02 22:47:18 +0100 | [diff] [blame] | 6824 | |
Willy Tarreau | bb2e669 | 2014-07-10 19:06:10 +0200 | [diff] [blame] | 6825 | if (msg->sov > 0) { |
Willy Tarreau | b59c7bf | 2014-04-22 14:29:58 +0200 | [diff] [blame] | 6826 | /* we have msg->sov which points to the first byte of message |
| 6827 | * body, and res->buf.p still points to the beginning of the |
| 6828 | * message. We forward the headers now, as we don't need them |
| 6829 | * anymore, and we want to flush them. |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 6830 | */ |
Willy Tarreau | b59c7bf | 2014-04-22 14:29:58 +0200 | [diff] [blame] | 6831 | b_adv(res->buf, msg->sov); |
| 6832 | msg->next -= msg->sov; |
| 6833 | msg->sov = 0; |
Willy Tarreau | a458b67 | 2012-03-05 11:17:50 +0100 | [diff] [blame] | 6834 | |
Willy Tarreau | b59c7bf | 2014-04-22 14:29:58 +0200 | [diff] [blame] | 6835 | /* The previous analysers guarantee that the state is somewhere |
| 6836 | * between MSG_BODY and the first MSG_DATA. So msg->sol and |
| 6837 | * msg->next are always correct. |
| 6838 | */ |
| 6839 | if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) { |
| 6840 | if (msg->flags & HTTP_MSGF_TE_CHNK) |
| 6841 | msg->msg_state = HTTP_MSG_CHUNK_SIZE; |
| 6842 | else |
| 6843 | msg->msg_state = HTTP_MSG_DATA; |
| 6844 | } |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 6845 | } |
| 6846 | |
Willy Tarreau | efdf094 | 2014-04-24 20:08:57 +0200 | [diff] [blame] | 6847 | if (res->to_forward) { |
| 6848 | /* We can't process the buffer's contents yet */ |
| 6849 | res->flags |= CF_WAKE_WRITE; |
| 6850 | goto missing_data; |
| 6851 | } |
| 6852 | |
Willy Tarreau | 32b5ab2 | 2014-04-21 11:27:29 +0200 | [diff] [blame] | 6853 | if (unlikely(s->comp_algo != NULL) && msg->msg_state < HTTP_MSG_TRAILERS) { |
| 6854 | /* We need a compression buffer in the DATA state to put the |
| 6855 | * output of compressed data, and in CRLF state to let the |
| 6856 | * TRAILERS state finish the job of removing the trailing CRLF. |
| 6857 | */ |
Willy Tarreau | f2f7d6b | 2014-11-24 11:55:08 +0100 | [diff] [blame] | 6858 | if (unlikely(!tmpbuf->size)) { |
Willy Tarreau | 32b5ab2 | 2014-04-21 11:27:29 +0200 | [diff] [blame] | 6859 | /* this is the first time we need the compression buffer */ |
Willy Tarreau | e583ea5 | 2014-11-24 11:30:16 +0100 | [diff] [blame] | 6860 | if (b_alloc(&tmpbuf) == NULL) |
Willy Tarreau | 32b5ab2 | 2014-04-21 11:27:29 +0200 | [diff] [blame] | 6861 | goto aborted_xfer; /* no memory */ |
| 6862 | } |
| 6863 | |
| 6864 | ret = http_compression_buffer_init(s, res->buf, tmpbuf); |
Willy Tarreau | 4afd70a | 2014-01-25 02:26:39 +0100 | [diff] [blame] | 6865 | if (ret < 0) { |
| 6866 | res->flags |= CF_WAKE_WRITE; |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 6867 | goto missing_data; /* not enough spaces in buffers */ |
Willy Tarreau | 4afd70a | 2014-01-25 02:26:39 +0100 | [diff] [blame] | 6868 | } |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 6869 | compressing = 1; |
| 6870 | } |
| 6871 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 6872 | while (1) { |
Willy Tarreau | d655ffe | 2013-04-02 01:48:58 +0200 | [diff] [blame] | 6873 | switch (msg->msg_state - HTTP_MSG_DATA) { |
| 6874 | case HTTP_MSG_DATA - HTTP_MSG_DATA: /* must still forward */ |
Willy Tarreau | c623c17 | 2014-04-18 09:53:50 +0200 | [diff] [blame] | 6875 | /* we may have some pending data starting at res->buf->p */ |
| 6876 | if (unlikely(s->comp_algo)) { |
Willy Tarreau | 7f2f8d5 | 2014-04-18 00:20:14 +0200 | [diff] [blame] | 6877 | ret = http_compression_buffer_add_data(s, res->buf, tmpbuf); |
William Lallemand | bf3ae61 | 2012-11-19 12:35:37 +0100 | [diff] [blame] | 6878 | if (ret < 0) |
| 6879 | goto aborted_xfer; |
Willy Tarreau | c623c17 | 2014-04-18 09:53:50 +0200 | [diff] [blame] | 6880 | |
Willy Tarreau | d5a6783 | 2014-04-21 10:54:27 +0200 | [diff] [blame] | 6881 | if (msg->chunk_len) { |
| 6882 | /* input empty or output full */ |
| 6883 | if (res->buf->i > msg->next) |
| 6884 | res->flags |= CF_WAKE_WRITE; |
Willy Tarreau | c623c17 | 2014-04-18 09:53:50 +0200 | [diff] [blame] | 6885 | goto missing_data; |
| 6886 | } |
William Lallemand | bf3ae61 | 2012-11-19 12:35:37 +0100 | [diff] [blame] | 6887 | } |
Willy Tarreau | c623c17 | 2014-04-18 09:53:50 +0200 | [diff] [blame] | 6888 | else { |
Willy Tarreau | d5a6783 | 2014-04-21 10:54:27 +0200 | [diff] [blame] | 6889 | if (msg->chunk_len > res->buf->i - msg->next) { |
| 6890 | /* output full */ |
| 6891 | res->flags |= CF_WAKE_WRITE; |
| 6892 | goto missing_data; |
| 6893 | } |
Willy Tarreau | c623c17 | 2014-04-18 09:53:50 +0200 | [diff] [blame] | 6894 | msg->next += msg->chunk_len; |
| 6895 | msg->chunk_len = 0; |
| 6896 | } |
Willy Tarreau | caabe41 | 2010-01-03 23:08:28 +0100 | [diff] [blame] | 6897 | |
| 6898 | /* nothing left to forward */ |
William Lallemand | bf3ae61 | 2012-11-19 12:35:37 +0100 | [diff] [blame] | 6899 | if (msg->flags & HTTP_MSGF_TE_CHNK) { |
Willy Tarreau | 54d23df | 2012-10-25 19:04:45 +0200 | [diff] [blame] | 6900 | msg->msg_state = HTTP_MSG_CHUNK_CRLF; |
William Lallemand | bf3ae61 | 2012-11-19 12:35:37 +0100 | [diff] [blame] | 6901 | } else { |
Willy Tarreau | caabe41 | 2010-01-03 23:08:28 +0100 | [diff] [blame] | 6902 | msg->msg_state = HTTP_MSG_DONE; |
Willy Tarreau | d655ffe | 2013-04-02 01:48:58 +0200 | [diff] [blame] | 6903 | break; |
William Lallemand | bf3ae61 | 2012-11-19 12:35:37 +0100 | [diff] [blame] | 6904 | } |
Willy Tarreau | d655ffe | 2013-04-02 01:48:58 +0200 | [diff] [blame] | 6905 | /* fall through for HTTP_MSG_CHUNK_CRLF */ |
| 6906 | |
| 6907 | case HTTP_MSG_CHUNK_CRLF - HTTP_MSG_DATA: |
| 6908 | /* we want the CRLF after the data */ |
| 6909 | |
| 6910 | ret = http_skip_chunk_crlf(msg); |
| 6911 | if (ret == 0) |
| 6912 | goto missing_data; |
| 6913 | else if (ret < 0) { |
| 6914 | if (msg->err_pos >= 0) |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 6915 | http_capture_bad_message(&s->be->invalid_rep, s, msg, HTTP_MSG_CHUNK_CRLF, sess->fe); |
Willy Tarreau | d655ffe | 2013-04-02 01:48:58 +0200 | [diff] [blame] | 6916 | goto return_bad_res; |
| 6917 | } |
Willy Tarreau | d655ffe | 2013-04-02 01:48:58 +0200 | [diff] [blame] | 6918 | /* we're in MSG_CHUNK_SIZE now, fall through */ |
| 6919 | |
| 6920 | case HTTP_MSG_CHUNK_SIZE - HTTP_MSG_DATA: |
Willy Tarreau | 124d991 | 2011-03-01 20:30:48 +0100 | [diff] [blame] | 6921 | /* read the chunk size and assign it to ->chunk_len, then |
Willy Tarreau | c24715e | 2014-04-17 15:21:20 +0200 | [diff] [blame] | 6922 | * set ->next to point to the body and switch to DATA or |
Willy Tarreau | a458b67 | 2012-03-05 11:17:50 +0100 | [diff] [blame] | 6923 | * TRAILERS state. |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 6924 | */ |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 6925 | |
Willy Tarreau | d655ffe | 2013-04-02 01:48:58 +0200 | [diff] [blame] | 6926 | ret = http_parse_chunk_size(msg); |
Willy Tarreau | 54d23df | 2012-10-25 19:04:45 +0200 | [diff] [blame] | 6927 | if (ret == 0) |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 6928 | goto missing_data; |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 6929 | else if (ret < 0) { |
| 6930 | if (msg->err_pos >= 0) |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 6931 | http_capture_bad_message(&s->be->invalid_rep, s, msg, HTTP_MSG_CHUNK_SIZE, sess->fe); |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 6932 | goto return_bad_res; |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 6933 | } |
Willy Tarreau | 0161d62 | 2013-04-02 01:26:55 +0200 | [diff] [blame] | 6934 | /* otherwise we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state */ |
Willy Tarreau | d655ffe | 2013-04-02 01:48:58 +0200 | [diff] [blame] | 6935 | break; |
Willy Tarreau | 5523b32 | 2009-12-29 12:05:52 +0100 | [diff] [blame] | 6936 | |
Willy Tarreau | d655ffe | 2013-04-02 01:48:58 +0200 | [diff] [blame] | 6937 | case HTTP_MSG_TRAILERS - HTTP_MSG_DATA: |
Willy Tarreau | 168ebc5 | 2014-04-18 00:53:43 +0200 | [diff] [blame] | 6938 | if (unlikely(compressing)) { |
| 6939 | /* we need to flush output contents before syncing FSMs */ |
| 6940 | http_compression_buffer_end(s, &res->buf, &tmpbuf, 1); |
| 6941 | compressing = 0; |
| 6942 | } |
| 6943 | |
Willy Tarreau | d655ffe | 2013-04-02 01:48:58 +0200 | [diff] [blame] | 6944 | ret = http_forward_trailers(msg); |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 6945 | if (ret == 0) |
| 6946 | goto missing_data; |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 6947 | else if (ret < 0) { |
| 6948 | if (msg->err_pos >= 0) |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 6949 | http_capture_bad_message(&s->be->invalid_rep, s, msg, HTTP_MSG_TRAILERS, sess->fe); |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 6950 | goto return_bad_res; |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 6951 | } |
Willy Tarreau | 168ebc5 | 2014-04-18 00:53:43 +0200 | [diff] [blame] | 6952 | /* we're in HTTP_MSG_DONE now, fall through */ |
Willy Tarreau | d655ffe | 2013-04-02 01:48:58 +0200 | [diff] [blame] | 6953 | |
| 6954 | default: |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 6955 | /* other states, DONE...TUNNEL */ |
Willy Tarreau | 168ebc5 | 2014-04-18 00:53:43 +0200 | [diff] [blame] | 6956 | if (unlikely(compressing)) { |
| 6957 | /* we need to flush output contents before syncing FSMs */ |
| 6958 | http_compression_buffer_end(s, &res->buf, &tmpbuf, 1); |
| 6959 | compressing = 0; |
| 6960 | } |
Willy Tarreau | d655ffe | 2013-04-02 01:48:58 +0200 | [diff] [blame] | 6961 | |
Willy Tarreau | c623c17 | 2014-04-18 09:53:50 +0200 | [diff] [blame] | 6962 | /* we may have some pending data starting at res->buf->p |
| 6963 | * such as a last chunk of data or trailers. |
| 6964 | */ |
| 6965 | b_adv(res->buf, msg->next); |
| 6966 | msg->next = 0; |
| 6967 | |
Willy Tarreau | d655ffe | 2013-04-02 01:48:58 +0200 | [diff] [blame] | 6968 | ret = msg->msg_state; |
Willy Tarreau | 4fe4190 | 2010-06-07 22:27:41 +0200 | [diff] [blame] | 6969 | /* for keep-alive we don't want to forward closes on DONE */ |
Willy Tarreau | 92aa1fa | 2010-08-28 18:57:20 +0200 | [diff] [blame] | 6970 | if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL || |
| 6971 | (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 6972 | channel_dont_close(res); |
Willy Tarreau | 3ce10ff | 2014-04-22 08:24:38 +0200 | [diff] [blame] | 6973 | |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 6974 | if (http_resync_states(s)) { |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 6975 | /* some state changes occurred, maybe the analyser |
| 6976 | * was disabled too. |
Willy Tarreau | 5523b32 | 2009-12-29 12:05:52 +0100 | [diff] [blame] | 6977 | */ |
Willy Tarreau | 3fe693b | 2010-12-12 12:50:05 +0100 | [diff] [blame] | 6978 | if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) { |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 6979 | if (res->flags & CF_SHUTW) { |
Willy Tarreau | 3fe693b | 2010-12-12 12:50:05 +0100 | [diff] [blame] | 6980 | /* response errors are most likely due to |
| 6981 | * the client aborting the transfer. |
| 6982 | */ |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 6983 | goto aborted_xfer; |
Willy Tarreau | 3fe693b | 2010-12-12 12:50:05 +0100 | [diff] [blame] | 6984 | } |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 6985 | if (msg->err_pos >= 0) |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 6986 | http_capture_bad_message(&s->be->invalid_rep, s, msg, ret, sess->fe); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 6987 | goto return_bad_res; |
Willy Tarreau | 3fe693b | 2010-12-12 12:50:05 +0100 | [diff] [blame] | 6988 | } |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 6989 | return 1; |
Willy Tarreau | 5523b32 | 2009-12-29 12:05:52 +0100 | [diff] [blame] | 6990 | } |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 6991 | return 0; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 6992 | } |
| 6993 | } |
| 6994 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 6995 | missing_data: |
Willy Tarreau | c623c17 | 2014-04-18 09:53:50 +0200 | [diff] [blame] | 6996 | /* we may have some pending data starting at res->buf->p */ |
Willy Tarreau | 168ebc5 | 2014-04-18 00:53:43 +0200 | [diff] [blame] | 6997 | if (unlikely(compressing)) { |
| 6998 | http_compression_buffer_end(s, &res->buf, &tmpbuf, msg->msg_state >= HTTP_MSG_TRAILERS); |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 6999 | compressing = 0; |
| 7000 | } |
Willy Tarreau | f003d37 | 2012-11-26 13:35:37 +0100 | [diff] [blame] | 7001 | |
Willy Tarreau | c623c17 | 2014-04-18 09:53:50 +0200 | [diff] [blame] | 7002 | if ((s->comp_algo == NULL || msg->msg_state >= HTTP_MSG_TRAILERS)) { |
| 7003 | b_adv(res->buf, msg->next); |
| 7004 | msg->next = 0; |
| 7005 | msg->chunk_len -= channel_forward(res, msg->chunk_len); |
| 7006 | } |
| 7007 | |
Willy Tarreau | f003d37 | 2012-11-26 13:35:37 +0100 | [diff] [blame] | 7008 | if (res->flags & CF_SHUTW) |
| 7009 | goto aborted_xfer; |
| 7010 | |
| 7011 | /* stop waiting for data if the input is closed before the end. If the |
| 7012 | * client side was already closed, it means that the client has aborted, |
| 7013 | * so we don't want to count this as a server abort. Otherwise it's a |
| 7014 | * server abort. |
| 7015 | */ |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 7016 | if (res->flags & CF_SHUTR) { |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 7017 | if ((s->req.flags & (CF_SHUTR|CF_SHUTW)) == (CF_SHUTR|CF_SHUTW)) |
Willy Tarreau | f003d37 | 2012-11-26 13:35:37 +0100 | [diff] [blame] | 7018 | goto aborted_xfer; |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 7019 | if (!(s->flags & SF_ERR_MASK)) |
| 7020 | s->flags |= SF_ERR_SRVCL; |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 7021 | s->be->be_counters.srv_aborts++; |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 7022 | if (objt_server(s->target)) |
| 7023 | objt_server(s->target)->counters.srv_aborts++; |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 7024 | goto return_bad_res_stats_ok; |
Willy Tarreau | 40dba09 | 2010-03-04 18:14:51 +0100 | [diff] [blame] | 7025 | } |
Willy Tarreau | f5c8bd6 | 2010-01-04 07:10:34 +0100 | [diff] [blame] | 7026 | |
Willy Tarreau | 40dba09 | 2010-03-04 18:14:51 +0100 | [diff] [blame] | 7027 | /* 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] | 7028 | if (!s->req.analysers) |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 7029 | goto return_bad_res; |
| 7030 | |
Willy Tarreau | 92aa1fa | 2010-08-28 18:57:20 +0200 | [diff] [blame] | 7031 | /* 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] | 7032 | * chunks even if the server has closed, so we don't want to set CF_DONTCLOSE. |
Willy Tarreau | 92aa1fa | 2010-08-28 18:57:20 +0200 | [diff] [blame] | 7033 | * Similarly, with keep-alive on the client side, we don't want to forward a |
| 7034 | * close. |
| 7035 | */ |
Willy Tarreau | 08b4d79 | 2012-10-27 01:36:34 +0200 | [diff] [blame] | 7036 | if ((msg->flags & HTTP_MSGF_TE_CHNK) || s->comp_algo || |
Willy Tarreau | 92aa1fa | 2010-08-28 18:57:20 +0200 | [diff] [blame] | 7037 | (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL || |
| 7038 | (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 7039 | channel_dont_close(res); |
Willy Tarreau | 92aa1fa | 2010-08-28 18:57:20 +0200 | [diff] [blame] | 7040 | |
Willy Tarreau | 5c62092 | 2011-05-11 19:56:11 +0200 | [diff] [blame] | 7041 | /* 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] | 7042 | * 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] | 7043 | * 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] | 7044 | * modes are already handled by the stream sock layer. We must not do |
| 7045 | * this in content-length mode because it could present the MSG_MORE |
| 7046 | * flag with the last block of forwarded data, which would cause an |
| 7047 | * additional delay to be observed by the receiver. |
Willy Tarreau | 5c62092 | 2011-05-11 19:56:11 +0200 | [diff] [blame] | 7048 | */ |
Willy Tarreau | 08b4d79 | 2012-10-27 01:36:34 +0200 | [diff] [blame] | 7049 | if ((msg->flags & HTTP_MSGF_TE_CHNK) || s->comp_algo) |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 7050 | res->flags |= CF_EXPECT_MORE; |
Willy Tarreau | 5c62092 | 2011-05-11 19:56:11 +0200 | [diff] [blame] | 7051 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 7052 | /* the stream handler will take care of timeouts and errors */ |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 7053 | return 0; |
| 7054 | |
Willy Tarreau | 40dba09 | 2010-03-04 18:14:51 +0100 | [diff] [blame] | 7055 | return_bad_res: /* let's centralize all bad responses */ |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 7056 | s->be->be_counters.failed_resp++; |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 7057 | if (objt_server(s->target)) |
| 7058 | objt_server(s->target)->counters.failed_resp++; |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 7059 | |
| 7060 | return_bad_res_stats_ok: |
Willy Tarreau | d01f426 | 2014-04-21 11:00:13 +0200 | [diff] [blame] | 7061 | if (unlikely(compressing)) { |
Willy Tarreau | 168ebc5 | 2014-04-18 00:53:43 +0200 | [diff] [blame] | 7062 | http_compression_buffer_end(s, &res->buf, &tmpbuf, msg->msg_state >= HTTP_MSG_TRAILERS); |
Willy Tarreau | d01f426 | 2014-04-21 11:00:13 +0200 | [diff] [blame] | 7063 | compressing = 0; |
| 7064 | } |
| 7065 | |
Willy Tarreau | c623c17 | 2014-04-18 09:53:50 +0200 | [diff] [blame] | 7066 | /* we may have some pending data starting at res->buf->p */ |
| 7067 | if (s->comp_algo == NULL) { |
| 7068 | b_adv(res->buf, msg->next); |
| 7069 | msg->next = 0; |
| 7070 | } |
| 7071 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 7072 | txn->rsp.msg_state = HTTP_MSG_ERROR; |
Willy Tarreau | 148d099 | 2010-01-10 10:21:21 +0100 | [diff] [blame] | 7073 | /* don't send any error message as we're in the body */ |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 7074 | stream_int_retnclose(&s->si[0], NULL); |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 7075 | res->analysers = 0; |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 7076 | s->req.analysers = 0; /* we're in data phase, we want to abort both directions */ |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 7077 | if (objt_server(s->target)) |
| 7078 | health_adjust(objt_server(s->target), HANA_STATUS_HTTP_HDRRSP); |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 7079 | |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 7080 | if (!(s->flags & SF_ERR_MASK)) |
| 7081 | s->flags |= SF_ERR_PRXCOND; |
| 7082 | if (!(s->flags & SF_FINST_MASK)) |
| 7083 | s->flags |= SF_FINST_D; |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 7084 | return 0; |
| 7085 | |
| 7086 | aborted_xfer: |
Willy Tarreau | 6fef8ae | 2014-04-22 21:22:06 +0200 | [diff] [blame] | 7087 | if (unlikely(compressing)) { |
| 7088 | http_compression_buffer_end(s, &res->buf, &tmpbuf, msg->msg_state >= HTTP_MSG_TRAILERS); |
| 7089 | compressing = 0; |
| 7090 | } |
| 7091 | |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 7092 | txn->rsp.msg_state = HTTP_MSG_ERROR; |
| 7093 | /* don't send any error message as we're in the body */ |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 7094 | stream_int_retnclose(&s->si[0], NULL); |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 7095 | res->analysers = 0; |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 7096 | s->req.analysers = 0; /* we're in data phase, we want to abort both directions */ |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 7097 | |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 7098 | sess->fe->fe_counters.cli_aborts++; |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 7099 | s->be->be_counters.cli_aborts++; |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 7100 | if (objt_server(s->target)) |
| 7101 | objt_server(s->target)->counters.cli_aborts++; |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 7102 | |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 7103 | if (!(s->flags & SF_ERR_MASK)) |
| 7104 | s->flags |= SF_ERR_CLICL; |
| 7105 | if (!(s->flags & SF_FINST_MASK)) |
| 7106 | s->flags |= SF_FINST_D; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 7107 | return 0; |
| 7108 | } |
| 7109 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7110 | /* Iterate the same filter through all request headers. |
| 7111 | * Returns 1 if this filter can be stopped upon return, otherwise 0. |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7112 | * Since it can manage the switch to another backend, it updates the per-proxy |
| 7113 | * DENY stats. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7114 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 7115 | 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] | 7116 | { |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7117 | char *cur_ptr, *cur_end, *cur_next; |
| 7118 | int cur_idx, old_idx, last_hdr; |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 7119 | struct http_txn *txn = s->txn; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7120 | struct hdr_idx_elem *cur_hdr; |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 7121 | int delta; |
Willy Tarreau | 0f7562b | 2007-01-07 15:46:13 +0100 | [diff] [blame] | 7122 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7123 | last_hdr = 0; |
| 7124 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 7125 | cur_next = req->buf->p + hdr_idx_first_pos(&txn->hdr_idx); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7126 | old_idx = 0; |
| 7127 | |
| 7128 | while (!last_hdr) { |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 7129 | if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT))) |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7130 | return 1; |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 7131 | else if (unlikely(txn->flags & TX_CLALLOW) && |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7132 | (exp->action == ACT_ALLOW || |
| 7133 | exp->action == ACT_DENY || |
| 7134 | exp->action == ACT_TARPIT)) |
| 7135 | return 0; |
| 7136 | |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 7137 | cur_idx = txn->hdr_idx.v[old_idx].next; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7138 | if (!cur_idx) |
| 7139 | break; |
| 7140 | |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 7141 | cur_hdr = &txn->hdr_idx.v[cur_idx]; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7142 | cur_ptr = cur_next; |
| 7143 | cur_end = cur_ptr + cur_hdr->len; |
| 7144 | cur_next = cur_end + cur_hdr->cr + 1; |
| 7145 | |
| 7146 | /* Now we have one header between cur_ptr and cur_end, |
| 7147 | * and the next header starts at cur_next. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7148 | */ |
| 7149 | |
Willy Tarreau | 15a53a4 | 2015-01-21 13:39:42 +0100 | [diff] [blame] | 7150 | 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] | 7151 | switch (exp->action) { |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7152 | case ACT_ALLOW: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 7153 | txn->flags |= TX_CLALLOW; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7154 | last_hdr = 1; |
| 7155 | break; |
| 7156 | |
| 7157 | case ACT_DENY: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 7158 | txn->flags |= TX_CLDENY; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7159 | last_hdr = 1; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7160 | break; |
| 7161 | |
| 7162 | case ACT_TARPIT: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 7163 | txn->flags |= TX_CLTARPIT; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7164 | last_hdr = 1; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7165 | break; |
| 7166 | |
| 7167 | case ACT_REPLACE: |
Sasha Pachev | c600204 | 2014-05-26 12:33:48 -0600 | [diff] [blame] | 7168 | trash.len = exp_replace(trash.str, trash.size, cur_ptr, exp->replace, pmatch); |
| 7169 | if (trash.len < 0) |
| 7170 | return -1; |
| 7171 | |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 7172 | 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] | 7173 | /* FIXME: if the user adds a newline in the replacement, the |
| 7174 | * index will not be recalculated for now, and the new line |
| 7175 | * will not be counted as a new header. |
| 7176 | */ |
| 7177 | |
| 7178 | cur_end += delta; |
| 7179 | cur_next += delta; |
| 7180 | cur_hdr->len += delta; |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 7181 | http_msg_move_end(&txn->req, delta); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7182 | break; |
| 7183 | |
| 7184 | case ACT_REMOVE: |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 7185 | delta = buffer_replace2(req->buf, cur_ptr, cur_next, NULL, 0); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7186 | cur_next += delta; |
| 7187 | |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 7188 | http_msg_move_end(&txn->req, delta); |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 7189 | txn->hdr_idx.v[old_idx].next = cur_hdr->next; |
| 7190 | txn->hdr_idx.used--; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7191 | cur_hdr->len = 0; |
| 7192 | cur_end = NULL; /* null-term has been rewritten */ |
Willy Tarreau | 26db59e | 2010-11-28 06:57:24 +0100 | [diff] [blame] | 7193 | cur_idx = old_idx; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7194 | break; |
| 7195 | |
| 7196 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7197 | } |
| 7198 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7199 | /* keep the link from this header to next one in case of later |
| 7200 | * removal of next header. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7201 | */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7202 | old_idx = cur_idx; |
| 7203 | } |
| 7204 | return 0; |
| 7205 | } |
| 7206 | |
| 7207 | |
| 7208 | /* Apply the filter to the request line. |
| 7209 | * Returns 0 if nothing has been done, 1 if the filter has been applied, |
| 7210 | * or -1 if a replacement resulted in an invalid request line. |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7211 | * Since it can manage the switch to another backend, it updates the per-proxy |
| 7212 | * DENY stats. |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7213 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 7214 | 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] | 7215 | { |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7216 | char *cur_ptr, *cur_end; |
| 7217 | int done; |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 7218 | struct http_txn *txn = s->txn; |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 7219 | int delta; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7220 | |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 7221 | if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT))) |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7222 | return 1; |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 7223 | else if (unlikely(txn->flags & TX_CLALLOW) && |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7224 | (exp->action == ACT_ALLOW || |
| 7225 | exp->action == ACT_DENY || |
| 7226 | exp->action == ACT_TARPIT)) |
| 7227 | return 0; |
| 7228 | else if (exp->action == ACT_REMOVE) |
| 7229 | return 0; |
| 7230 | |
| 7231 | done = 0; |
| 7232 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 7233 | cur_ptr = req->buf->p; |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 7234 | cur_end = cur_ptr + txn->req.sl.rq.l; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7235 | |
| 7236 | /* Now we have the request line between cur_ptr and cur_end */ |
| 7237 | |
Willy Tarreau | 15a53a4 | 2015-01-21 13:39:42 +0100 | [diff] [blame] | 7238 | 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] | 7239 | switch (exp->action) { |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7240 | case ACT_ALLOW: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 7241 | txn->flags |= TX_CLALLOW; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7242 | done = 1; |
| 7243 | break; |
Willy Tarreau | a496b60 | 2006-12-17 23:15:24 +0100 | [diff] [blame] | 7244 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7245 | case ACT_DENY: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 7246 | txn->flags |= TX_CLDENY; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7247 | done = 1; |
| 7248 | break; |
Willy Tarreau | a496b60 | 2006-12-17 23:15:24 +0100 | [diff] [blame] | 7249 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7250 | case ACT_TARPIT: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 7251 | txn->flags |= TX_CLTARPIT; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7252 | done = 1; |
| 7253 | break; |
Willy Tarreau | a496b60 | 2006-12-17 23:15:24 +0100 | [diff] [blame] | 7254 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7255 | case ACT_REPLACE: |
Sasha Pachev | c600204 | 2014-05-26 12:33:48 -0600 | [diff] [blame] | 7256 | trash.len = exp_replace(trash.str, trash.size, cur_ptr, exp->replace, pmatch); |
| 7257 | if (trash.len < 0) |
| 7258 | return -1; |
| 7259 | |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 7260 | 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] | 7261 | /* FIXME: if the user adds a newline in the replacement, the |
| 7262 | * index will not be recalculated for now, and the new line |
| 7263 | * will not be counted as a new header. |
| 7264 | */ |
Willy Tarreau | a496b60 | 2006-12-17 23:15:24 +0100 | [diff] [blame] | 7265 | |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 7266 | http_msg_move_end(&txn->req, delta); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7267 | cur_end += delta; |
Willy Tarreau | 69d8c5d | 2012-05-08 09:44:41 +0200 | [diff] [blame] | 7268 | cur_end = (char *)http_parse_reqline(&txn->req, |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7269 | HTTP_MSG_RQMETH, |
| 7270 | cur_ptr, cur_end + 1, |
| 7271 | NULL, NULL); |
| 7272 | if (unlikely(!cur_end)) |
| 7273 | return -1; |
Willy Tarreau | a496b60 | 2006-12-17 23:15:24 +0100 | [diff] [blame] | 7274 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7275 | /* we have a full request and we know that we have either a CR |
| 7276 | * or an LF at <ptr>. |
| 7277 | */ |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 7278 | txn->meth = find_http_meth(cur_ptr, txn->req.sl.rq.m_l); |
| 7279 | 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] | 7280 | /* there is no point trying this regex on headers */ |
| 7281 | return 1; |
| 7282 | } |
| 7283 | } |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7284 | return done; |
| 7285 | } |
Willy Tarreau | 97de624 | 2006-12-27 17:18:38 +0100 | [diff] [blame] | 7286 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7287 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7288 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7289 | /* |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 7290 | * 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] | 7291 | * 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] | 7292 | * unparsable request. Since it can manage the switch to another backend, it |
| 7293 | * updates the per-proxy DENY stats. |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7294 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 7295 | 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] | 7296 | { |
Willy Tarreau | 192252e | 2015-04-04 01:47:55 +0200 | [diff] [blame] | 7297 | struct session *sess = s->sess; |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 7298 | struct http_txn *txn = s->txn; |
Willy Tarreau | 6c123b1 | 2010-01-28 20:22:06 +0100 | [diff] [blame] | 7299 | struct hdr_exp *exp; |
| 7300 | |
| 7301 | for (exp = px->req_exp; exp; exp = exp->next) { |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7302 | int ret; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7303 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7304 | /* |
| 7305 | * The interleaving of transformations and verdicts |
| 7306 | * makes it difficult to decide to continue or stop |
| 7307 | * the evaluation. |
| 7308 | */ |
| 7309 | |
Willy Tarreau | 6c123b1 | 2010-01-28 20:22:06 +0100 | [diff] [blame] | 7310 | if (txn->flags & (TX_CLDENY|TX_CLTARPIT)) |
| 7311 | break; |
| 7312 | |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 7313 | if ((txn->flags & TX_CLALLOW) && |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7314 | (exp->action == ACT_ALLOW || exp->action == ACT_DENY || |
Willy Tarreau | 6c123b1 | 2010-01-28 20:22:06 +0100 | [diff] [blame] | 7315 | exp->action == ACT_TARPIT || exp->action == ACT_PASS)) |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7316 | continue; |
Willy Tarreau | 6c123b1 | 2010-01-28 20:22:06 +0100 | [diff] [blame] | 7317 | |
| 7318 | /* if this filter had a condition, evaluate it now and skip to |
| 7319 | * next filter if the condition does not match. |
| 7320 | */ |
| 7321 | if (exp->cond) { |
Willy Tarreau | 192252e | 2015-04-04 01:47:55 +0200 | [diff] [blame] | 7322 | 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] | 7323 | ret = acl_pass(ret); |
| 7324 | if (((struct acl_cond *)exp->cond)->pol == ACL_COND_UNLESS) |
| 7325 | ret = !ret; |
| 7326 | |
| 7327 | if (!ret) |
| 7328 | continue; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7329 | } |
| 7330 | |
| 7331 | /* Apply the filter to the request line. */ |
Willy Tarreau | 6c123b1 | 2010-01-28 20:22:06 +0100 | [diff] [blame] | 7332 | ret = apply_filter_to_req_line(s, req, exp); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7333 | if (unlikely(ret < 0)) |
| 7334 | return -1; |
| 7335 | |
| 7336 | if (likely(ret == 0)) { |
| 7337 | /* The filter did not match the request, it can be |
| 7338 | * iterated through all headers. |
| 7339 | */ |
Willy Tarreau | 34d4c3c | 2015-01-30 20:58:58 +0100 | [diff] [blame] | 7340 | if (unlikely(apply_filter_to_req_headers(s, req, exp) < 0)) |
| 7341 | return -1; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7342 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7343 | } |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7344 | return 0; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7345 | } |
| 7346 | |
| 7347 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7348 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7349 | /* |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 7350 | * Try to retrieve the server associated to the appsession. |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 7351 | * If the server is found, it's assigned to the stream. |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 7352 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 7353 | void manage_client_side_appsession(struct stream *s, const char *buf, int len) { |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 7354 | struct http_txn *txn = s->txn; |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 7355 | appsess *asession = NULL; |
| 7356 | char *sessid_temp = NULL; |
| 7357 | |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 7358 | if (len > s->be->appsession_len) { |
| 7359 | len = s->be->appsession_len; |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 7360 | } |
| 7361 | |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 7362 | if (s->be->options2 & PR_O2_AS_REQL) { |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 7363 | /* request-learn option is enabled : store the sessid in the stream for future use */ |
Willy Tarreau | a3377ee | 2010-01-10 10:49:11 +0100 | [diff] [blame] | 7364 | if (txn->sessid != NULL) { |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 7365 | /* free previously allocated memory as we don't need the stream id found in the URL anymore */ |
Willy Tarreau | a3377ee | 2010-01-10 10:49:11 +0100 | [diff] [blame] | 7366 | pool_free2(apools.sessid, txn->sessid); |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 7367 | } |
| 7368 | |
Willy Tarreau | a3377ee | 2010-01-10 10:49:11 +0100 | [diff] [blame] | 7369 | if ((txn->sessid = pool_alloc2(apools.sessid)) == NULL) { |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 7370 | Alert("Not enough memory process_cli():asession->sessid:malloc().\n"); |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 7371 | send_log(s->be, LOG_ALERT, "Not enough memory process_cli():asession->sessid:malloc().\n"); |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 7372 | return; |
| 7373 | } |
| 7374 | |
Willy Tarreau | a3377ee | 2010-01-10 10:49:11 +0100 | [diff] [blame] | 7375 | memcpy(txn->sessid, buf, len); |
| 7376 | txn->sessid[len] = 0; |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 7377 | } |
| 7378 | |
| 7379 | if ((sessid_temp = pool_alloc2(apools.sessid)) == NULL) { |
| 7380 | Alert("Not enough memory process_cli():asession->sessid:malloc().\n"); |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 7381 | send_log(s->be, LOG_ALERT, "Not enough memory process_cli():asession->sessid:malloc().\n"); |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 7382 | return; |
| 7383 | } |
| 7384 | |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 7385 | memcpy(sessid_temp, buf, len); |
| 7386 | sessid_temp[len] = 0; |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 7387 | |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 7388 | asession = appsession_hash_lookup(&(s->be->htbl_proxy), sessid_temp); |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 7389 | /* free previously allocated memory */ |
| 7390 | pool_free2(apools.sessid, sessid_temp); |
| 7391 | |
| 7392 | if (asession != NULL) { |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 7393 | asession->expire = tick_add_ifset(now_ms, s->be->timeout.appsession); |
| 7394 | if (!(s->be->options2 & PR_O2_AS_REQL)) |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 7395 | asession->request_count++; |
| 7396 | |
| 7397 | if (asession->serverid != NULL) { |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 7398 | struct server *srv = s->be->srv; |
Cyril Bonté | 47fdd8e | 2010-04-25 00:00:51 +0200 | [diff] [blame] | 7399 | |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 7400 | while (srv) { |
| 7401 | if (strcmp(srv->id, asession->serverid) == 0) { |
Willy Tarreau | 892337c | 2014-05-13 23:41:20 +0200 | [diff] [blame] | 7402 | if ((srv->state != SRV_ST_STOPPED) || |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 7403 | (s->be->options & PR_O_PERSIST) || |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 7404 | (s->flags & SF_FORCE_PRST)) { |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 7405 | /* we found the server and it's usable */ |
| 7406 | txn->flags &= ~TX_CK_MASK; |
Willy Tarreau | 892337c | 2014-05-13 23:41:20 +0200 | [diff] [blame] | 7407 | 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] | 7408 | s->flags |= SF_DIRECT | SF_ASSIGNED; |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 7409 | s->target = &srv->obj_type; |
Willy Tarreau | 664beb8 | 2011-03-10 11:38:29 +0100 | [diff] [blame] | 7410 | |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 7411 | break; |
| 7412 | } else { |
| 7413 | txn->flags &= ~TX_CK_MASK; |
| 7414 | txn->flags |= TX_CK_DOWN; |
| 7415 | } |
| 7416 | } |
| 7417 | srv = srv->next; |
| 7418 | } |
| 7419 | } |
| 7420 | } |
| 7421 | } |
| 7422 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7423 | /* Find the end of a cookie value contained between <s> and <e>. It works the |
| 7424 | * same way as with headers above except that the semi-colon also ends a token. |
| 7425 | * See RFC2965 for more information. Note that it requires a valid header to |
| 7426 | * return a valid result. |
| 7427 | */ |
| 7428 | char *find_cookie_value_end(char *s, const char *e) |
| 7429 | { |
| 7430 | int quoted, qdpair; |
| 7431 | |
| 7432 | quoted = qdpair = 0; |
| 7433 | for (; s < e; s++) { |
| 7434 | if (qdpair) qdpair = 0; |
| 7435 | else if (quoted) { |
| 7436 | if (*s == '\\') qdpair = 1; |
| 7437 | else if (*s == '"') quoted = 0; |
| 7438 | } |
| 7439 | else if (*s == '"') quoted = 1; |
| 7440 | else if (*s == ',' || *s == ';') return s; |
| 7441 | } |
| 7442 | return s; |
| 7443 | } |
| 7444 | |
| 7445 | /* Delete a value in a header between delimiters <from> and <next> in buffer |
| 7446 | * <buf>. The number of characters displaced is returned, and the pointer to |
| 7447 | * the first delimiter is updated if required. The function tries as much as |
| 7448 | * possible to respect the following principles : |
| 7449 | * - replace <from> delimiter by the <next> one unless <from> points to a |
| 7450 | * colon, in which case <next> is simply removed |
| 7451 | * - set exactly one space character after the new first delimiter, unless |
| 7452 | * there are not enough characters in the block being moved to do so. |
| 7453 | * - remove unneeded spaces before the previous delimiter and after the new |
| 7454 | * one. |
| 7455 | * |
| 7456 | * It is the caller's responsibility to ensure that : |
| 7457 | * - <from> points to a valid delimiter or the colon ; |
| 7458 | * - <next> points to a valid delimiter or the final CR/LF ; |
| 7459 | * - there are non-space chars before <from> ; |
| 7460 | * - there is a CR/LF at or after <next>. |
| 7461 | */ |
Willy Tarreau | af81935 | 2012-08-27 22:08:00 +0200 | [diff] [blame] | 7462 | int del_hdr_value(struct buffer *buf, char **from, char *next) |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7463 | { |
| 7464 | char *prev = *from; |
| 7465 | |
| 7466 | if (*prev == ':') { |
| 7467 | /* We're removing the first value, preserve the colon and add a |
| 7468 | * space if possible. |
| 7469 | */ |
| 7470 | if (!http_is_crlf[(unsigned char)*next]) |
| 7471 | next++; |
| 7472 | prev++; |
| 7473 | if (prev < next) |
| 7474 | *prev++ = ' '; |
| 7475 | |
| 7476 | while (http_is_spht[(unsigned char)*next]) |
| 7477 | next++; |
| 7478 | } else { |
| 7479 | /* Remove useless spaces before the old delimiter. */ |
| 7480 | while (http_is_spht[(unsigned char)*(prev-1)]) |
| 7481 | prev--; |
| 7482 | *from = prev; |
| 7483 | |
| 7484 | /* copy the delimiter and if possible a space if we're |
| 7485 | * not at the end of the line. |
| 7486 | */ |
| 7487 | if (!http_is_crlf[(unsigned char)*next]) { |
| 7488 | *prev++ = *next++; |
| 7489 | if (prev + 1 < next) |
| 7490 | *prev++ = ' '; |
| 7491 | while (http_is_spht[(unsigned char)*next]) |
| 7492 | next++; |
| 7493 | } |
| 7494 | } |
| 7495 | return buffer_replace2(buf, prev, next, NULL, 0); |
| 7496 | } |
| 7497 | |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 7498 | /* |
Willy Tarreau | 396d2c6 | 2007-11-04 19:30:00 +0100 | [diff] [blame] | 7499 | * 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] | 7500 | * desirable to call it only when needed. This code is quite complex because |
| 7501 | * of the multiple very crappy and ambiguous syntaxes we have to support. it |
| 7502 | * highly recommended not to touch this part without a good reason ! |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7503 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 7504 | void manage_client_side_cookies(struct stream *s, struct channel *req) |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7505 | { |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 7506 | struct http_txn *txn = s->txn; |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 7507 | struct session *sess = s->sess; |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7508 | int preserve_hdr; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7509 | int cur_idx, old_idx; |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7510 | char *hdr_beg, *hdr_end, *hdr_next, *del_from; |
| 7511 | char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7512 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7513 | /* Iterate through the headers, we start with the start line. */ |
Willy Tarreau | 83969f4 | 2007-01-22 08:55:47 +0100 | [diff] [blame] | 7514 | old_idx = 0; |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 7515 | hdr_next = req->buf->p + hdr_idx_first_pos(&txn->hdr_idx); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7516 | |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 7517 | while ((cur_idx = txn->hdr_idx.v[old_idx].next)) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7518 | struct hdr_idx_elem *cur_hdr; |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 7519 | int val; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7520 | |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 7521 | cur_hdr = &txn->hdr_idx.v[cur_idx]; |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7522 | hdr_beg = hdr_next; |
| 7523 | hdr_end = hdr_beg + cur_hdr->len; |
| 7524 | hdr_next = hdr_end + cur_hdr->cr + 1; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7525 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7526 | /* We have one full header between hdr_beg and hdr_end, and the |
| 7527 | * next header starts at hdr_next. We're only interested in |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7528 | * "Cookie:" headers. |
| 7529 | */ |
| 7530 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7531 | val = http_header_match2(hdr_beg, hdr_end, "Cookie", 6); |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 7532 | if (!val) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7533 | old_idx = cur_idx; |
| 7534 | continue; |
| 7535 | } |
| 7536 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7537 | del_from = NULL; /* nothing to be deleted */ |
| 7538 | preserve_hdr = 0; /* assume we may kill the whole header */ |
| 7539 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7540 | /* Now look for cookies. Conforming to RFC2109, we have to support |
| 7541 | * attributes whose name begin with a '$', and associate them with |
| 7542 | * the right cookie, if we want to delete this cookie. |
| 7543 | * So there are 3 cases for each cookie read : |
| 7544 | * 1) it's a special attribute, beginning with a '$' : ignore it. |
| 7545 | * 2) it's a server id cookie that we *MAY* want to delete : save |
| 7546 | * some pointers on it (last semi-colon, beginning of cookie...) |
| 7547 | * 3) it's an application cookie : we *MAY* have to delete a previous |
| 7548 | * "special" cookie. |
| 7549 | * At the end of loop, if a "special" cookie remains, we may have to |
| 7550 | * remove it. If no application cookie persists in the header, we |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7551 | * *MUST* delete it. |
| 7552 | * |
| 7553 | * Note: RFC2965 is unclear about the processing of spaces around |
| 7554 | * the equal sign in the ATTR=VALUE form. A careful inspection of |
| 7555 | * the RFC explicitly allows spaces before it, and not within the |
| 7556 | * tokens (attrs or values). An inspection of RFC2109 allows that |
| 7557 | * too but section 10.1.3 lets one think that spaces may be allowed |
| 7558 | * after the equal sign too, resulting in some (rare) buggy |
| 7559 | * implementations trying to do that. So let's do what servers do. |
| 7560 | * Latest ietf draft forbids spaces all around. Also, earlier RFCs |
| 7561 | * allowed quoted strings in values, with any possible character |
| 7562 | * after a backslash, including control chars and delimitors, which |
| 7563 | * causes parsing to become ambiguous. Browsers also allow spaces |
| 7564 | * within values even without quotes. |
| 7565 | * |
| 7566 | * We have to keep multiple pointers in order to support cookie |
| 7567 | * removal at the beginning, middle or end of header without |
| 7568 | * corrupting the header. All of these headers are valid : |
| 7569 | * |
| 7570 | * Cookie:NAME1=VALUE1;NAME2=VALUE2;NAME3=VALUE3\r\n |
| 7571 | * Cookie:NAME1=VALUE1;NAME2_ONLY ;NAME3=VALUE3\r\n |
| 7572 | * Cookie: NAME1 = VALUE 1 ; NAME2 = VALUE2 ; NAME3 = VALUE3\r\n |
| 7573 | * | | | | | | | | | |
| 7574 | * | | | | | | | | hdr_end <--+ |
| 7575 | * | | | | | | | +--> next |
| 7576 | * | | | | | | +----> val_end |
| 7577 | * | | | | | +-----------> val_beg |
| 7578 | * | | | | +--------------> equal |
| 7579 | * | | | +----------------> att_end |
| 7580 | * | | +---------------------> att_beg |
| 7581 | * | +--------------------------> prev |
| 7582 | * +--------------------------------> hdr_beg |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7583 | */ |
| 7584 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7585 | for (prev = hdr_beg + 6; prev < hdr_end; prev = next) { |
| 7586 | /* Iterate through all cookies on this line */ |
| 7587 | |
| 7588 | /* find att_beg */ |
| 7589 | att_beg = prev + 1; |
| 7590 | while (att_beg < hdr_end && http_is_spht[(unsigned char)*att_beg]) |
| 7591 | att_beg++; |
| 7592 | |
| 7593 | /* find att_end : this is the first character after the last non |
| 7594 | * space before the equal. It may be equal to hdr_end. |
| 7595 | */ |
| 7596 | equal = att_end = att_beg; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7597 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7598 | while (equal < hdr_end) { |
| 7599 | if (*equal == '=' || *equal == ',' || *equal == ';') |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7600 | break; |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7601 | if (http_is_spht[(unsigned char)*equal++]) |
| 7602 | continue; |
| 7603 | att_end = equal; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7604 | } |
| 7605 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7606 | /* here, <equal> points to '=', a delimitor or the end. <att_end> |
| 7607 | * is between <att_beg> and <equal>, both may be identical. |
| 7608 | */ |
| 7609 | |
| 7610 | /* look for end of cookie if there is an equal sign */ |
| 7611 | if (equal < hdr_end && *equal == '=') { |
| 7612 | /* look for the beginning of the value */ |
| 7613 | val_beg = equal + 1; |
| 7614 | while (val_beg < hdr_end && http_is_spht[(unsigned char)*val_beg]) |
| 7615 | val_beg++; |
| 7616 | |
| 7617 | /* find the end of the value, respecting quotes */ |
| 7618 | next = find_cookie_value_end(val_beg, hdr_end); |
| 7619 | |
| 7620 | /* make val_end point to the first white space or delimitor after the value */ |
| 7621 | val_end = next; |
| 7622 | while (val_end > val_beg && http_is_spht[(unsigned char)*(val_end - 1)]) |
| 7623 | val_end--; |
| 7624 | } else { |
| 7625 | val_beg = val_end = next = equal; |
Willy Tarreau | 305ae85 | 2010-01-03 19:45:54 +0100 | [diff] [blame] | 7626 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7627 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7628 | /* We have nothing to do with attributes beginning with '$'. However, |
| 7629 | * they will automatically be removed if a header before them is removed, |
| 7630 | * since they're supposed to be linked together. |
| 7631 | */ |
| 7632 | if (*att_beg == '$') |
| 7633 | continue; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7634 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7635 | /* Ignore cookies with no equal sign */ |
| 7636 | if (equal == next) { |
| 7637 | /* This is not our cookie, so we must preserve it. But if we already |
| 7638 | * scheduled another cookie for removal, we cannot remove the |
| 7639 | * complete header, but we can remove the previous block itself. |
| 7640 | */ |
| 7641 | preserve_hdr = 1; |
| 7642 | if (del_from != NULL) { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 7643 | int delta = del_hdr_value(req->buf, &del_from, prev); |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7644 | val_end += delta; |
| 7645 | next += delta; |
| 7646 | hdr_end += delta; |
| 7647 | hdr_next += delta; |
| 7648 | cur_hdr->len += delta; |
| 7649 | http_msg_move_end(&txn->req, delta); |
| 7650 | prev = del_from; |
| 7651 | del_from = NULL; |
| 7652 | } |
| 7653 | continue; |
Willy Tarreau | 305ae85 | 2010-01-03 19:45:54 +0100 | [diff] [blame] | 7654 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7655 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7656 | /* if there are spaces around the equal sign, we need to |
| 7657 | * strip them otherwise we'll get trouble for cookie captures, |
| 7658 | * or even for rewrites. Since this happens extremely rarely, |
| 7659 | * it does not hurt performance. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7660 | */ |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7661 | if (unlikely(att_end != equal || val_beg > equal + 1)) { |
| 7662 | int stripped_before = 0; |
| 7663 | int stripped_after = 0; |
| 7664 | |
| 7665 | if (att_end != equal) { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 7666 | stripped_before = buffer_replace2(req->buf, att_end, equal, NULL, 0); |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7667 | equal += stripped_before; |
| 7668 | val_beg += stripped_before; |
| 7669 | } |
| 7670 | |
| 7671 | if (val_beg > equal + 1) { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 7672 | stripped_after = buffer_replace2(req->buf, equal + 1, val_beg, NULL, 0); |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7673 | val_beg += stripped_after; |
| 7674 | stripped_before += stripped_after; |
| 7675 | } |
| 7676 | |
| 7677 | val_end += stripped_before; |
| 7678 | next += stripped_before; |
| 7679 | hdr_end += stripped_before; |
| 7680 | hdr_next += stripped_before; |
| 7681 | cur_hdr->len += stripped_before; |
| 7682 | http_msg_move_end(&txn->req, stripped_before); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7683 | } |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7684 | /* now everything is as on the diagram above */ |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7685 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7686 | /* First, let's see if we want to capture this cookie. We check |
| 7687 | * that we don't already have a client side cookie, because we |
| 7688 | * can only capture one. Also as an optimisation, we ignore |
| 7689 | * cookies shorter than the declared name. |
| 7690 | */ |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 7691 | if (sess->fe->capture_name != NULL && txn->cli_cookie == NULL && |
| 7692 | (val_end - att_beg >= sess->fe->capture_namelen) && |
| 7693 | memcmp(att_beg, sess->fe->capture_name, sess->fe->capture_namelen) == 0) { |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7694 | int log_len = val_end - att_beg; |
| 7695 | |
| 7696 | if ((txn->cli_cookie = pool_alloc2(pool2_capture)) == NULL) { |
| 7697 | Alert("HTTP logging : out of memory.\n"); |
| 7698 | } else { |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 7699 | if (log_len > sess->fe->capture_len) |
| 7700 | log_len = sess->fe->capture_len; |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7701 | memcpy(txn->cli_cookie, att_beg, log_len); |
| 7702 | txn->cli_cookie[log_len] = 0; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7703 | } |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7704 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7705 | |
Willy Tarreau | bca9969 | 2010-10-06 19:25:55 +0200 | [diff] [blame] | 7706 | /* Persistence cookies in passive, rewrite or insert mode have the |
| 7707 | * following form : |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7708 | * |
Willy Tarreau | bca9969 | 2010-10-06 19:25:55 +0200 | [diff] [blame] | 7709 | * Cookie: NAME=SRV[|<lastseen>[|<firstseen>]] |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7710 | * |
Willy Tarreau | bca9969 | 2010-10-06 19:25:55 +0200 | [diff] [blame] | 7711 | * For cookies in prefix mode, the form is : |
| 7712 | * |
| 7713 | * Cookie: NAME=SRV~VALUE |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7714 | */ |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 7715 | if ((att_end - att_beg == s->be->cookie_len) && (s->be->cookie_name != NULL) && |
| 7716 | (memcmp(att_beg, s->be->cookie_name, att_end - att_beg) == 0)) { |
| 7717 | struct server *srv = s->be->srv; |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7718 | char *delim; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7719 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7720 | /* if we're in cookie prefix mode, we'll search the delimitor so that we |
| 7721 | * have the server ID between val_beg and delim, and the original cookie between |
| 7722 | * delim+1 and val_end. Otherwise, delim==val_end : |
| 7723 | * |
| 7724 | * Cookie: NAME=SRV; # in all but prefix modes |
| 7725 | * Cookie: NAME=SRV~OPAQUE ; # in prefix mode |
| 7726 | * | || || | |+-> next |
| 7727 | * | || || | +--> val_end |
| 7728 | * | || || +---------> delim |
| 7729 | * | || |+------------> val_beg |
| 7730 | * | || +-------------> att_end = equal |
| 7731 | * | |+-----------------> att_beg |
| 7732 | * | +------------------> prev |
| 7733 | * +-------------------------> hdr_beg |
| 7734 | */ |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7735 | |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 7736 | if (s->be->ck_opts & PR_CK_PFX) { |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7737 | for (delim = val_beg; delim < val_end; delim++) |
| 7738 | if (*delim == COOKIE_DELIM) |
| 7739 | break; |
Willy Tarreau | bca9969 | 2010-10-06 19:25:55 +0200 | [diff] [blame] | 7740 | } else { |
| 7741 | char *vbar1; |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7742 | delim = val_end; |
Willy Tarreau | bca9969 | 2010-10-06 19:25:55 +0200 | [diff] [blame] | 7743 | /* Now check if the cookie contains a date field, which would |
| 7744 | * appear after a vertical bar ('|') just after the server name |
| 7745 | * and before the delimiter. |
| 7746 | */ |
| 7747 | vbar1 = memchr(val_beg, COOKIE_DELIM_DATE, val_end - val_beg); |
| 7748 | if (vbar1) { |
| 7749 | /* OK, so left of the bar is the server's cookie and |
Willy Tarreau | f64d141 | 2010-10-07 20:06:11 +0200 | [diff] [blame] | 7750 | * right is the last seen date. It is a base64 encoded |
| 7751 | * 30-bit value representing the UNIX date since the |
| 7752 | * epoch in 4-second quantities. |
Willy Tarreau | bca9969 | 2010-10-06 19:25:55 +0200 | [diff] [blame] | 7753 | */ |
Willy Tarreau | f64d141 | 2010-10-07 20:06:11 +0200 | [diff] [blame] | 7754 | int val; |
Willy Tarreau | bca9969 | 2010-10-06 19:25:55 +0200 | [diff] [blame] | 7755 | delim = vbar1++; |
Willy Tarreau | f64d141 | 2010-10-07 20:06:11 +0200 | [diff] [blame] | 7756 | if (val_end - vbar1 >= 5) { |
| 7757 | val = b64tos30(vbar1); |
| 7758 | if (val > 0) |
| 7759 | txn->cookie_last_date = val << 2; |
| 7760 | } |
| 7761 | /* look for a second vertical bar */ |
| 7762 | vbar1 = memchr(vbar1, COOKIE_DELIM_DATE, val_end - vbar1); |
| 7763 | if (vbar1 && (val_end - vbar1 > 5)) { |
| 7764 | val = b64tos30(vbar1 + 1); |
| 7765 | if (val > 0) |
| 7766 | txn->cookie_first_date = val << 2; |
| 7767 | } |
Willy Tarreau | bca9969 | 2010-10-06 19:25:55 +0200 | [diff] [blame] | 7768 | } |
| 7769 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7770 | |
Willy Tarreau | f64d141 | 2010-10-07 20:06:11 +0200 | [diff] [blame] | 7771 | /* if the cookie has an expiration date and the proxy wants to check |
| 7772 | * it, then we do that now. We first check if the cookie is too old, |
| 7773 | * then only if it has expired. We detect strict overflow because the |
| 7774 | * time resolution here is not great (4 seconds). Cookies with dates |
| 7775 | * in the future are ignored if their offset is beyond one day. This |
| 7776 | * allows an admin to fix timezone issues without expiring everyone |
| 7777 | * and at the same time avoids keeping unwanted side effects for too |
| 7778 | * long. |
| 7779 | */ |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 7780 | if (txn->cookie_first_date && s->be->cookie_maxlife && |
| 7781 | (((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] | 7782 | ((signed)(txn->cookie_first_date - date.tv_sec) > 86400))) { |
Willy Tarreau | f64d141 | 2010-10-07 20:06:11 +0200 | [diff] [blame] | 7783 | txn->flags &= ~TX_CK_MASK; |
| 7784 | txn->flags |= TX_CK_OLD; |
| 7785 | delim = val_beg; // let's pretend we have not found the cookie |
| 7786 | txn->cookie_first_date = 0; |
| 7787 | txn->cookie_last_date = 0; |
| 7788 | } |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 7789 | else if (txn->cookie_last_date && s->be->cookie_maxidle && |
| 7790 | (((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] | 7791 | ((signed)(txn->cookie_last_date - date.tv_sec) > 86400))) { |
Willy Tarreau | f64d141 | 2010-10-07 20:06:11 +0200 | [diff] [blame] | 7792 | txn->flags &= ~TX_CK_MASK; |
| 7793 | txn->flags |= TX_CK_EXPIRED; |
| 7794 | delim = val_beg; // let's pretend we have not found the cookie |
| 7795 | txn->cookie_first_date = 0; |
| 7796 | txn->cookie_last_date = 0; |
| 7797 | } |
| 7798 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7799 | /* Here, we'll look for the first running server which supports the cookie. |
| 7800 | * This allows to share a same cookie between several servers, for example |
| 7801 | * to dedicate backup servers to specific servers only. |
| 7802 | * However, to prevent clients from sticking to cookie-less backup server |
| 7803 | * when they have incidentely learned an empty cookie, we simply ignore |
| 7804 | * empty cookies and mark them as invalid. |
| 7805 | * The same behaviour is applied when persistence must be ignored. |
| 7806 | */ |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 7807 | if ((delim == val_beg) || (s->flags & (SF_IGNORE_PRST | SF_ASSIGNED))) |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7808 | srv = NULL; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7809 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7810 | while (srv) { |
| 7811 | if (srv->cookie && (srv->cklen == delim - val_beg) && |
| 7812 | !memcmp(val_beg, srv->cookie, delim - val_beg)) { |
Willy Tarreau | 892337c | 2014-05-13 23:41:20 +0200 | [diff] [blame] | 7813 | if ((srv->state != SRV_ST_STOPPED) || |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 7814 | (s->be->options & PR_O_PERSIST) || |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 7815 | (s->flags & SF_FORCE_PRST)) { |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7816 | /* we found the server and we can use it */ |
| 7817 | txn->flags &= ~TX_CK_MASK; |
Willy Tarreau | 892337c | 2014-05-13 23:41:20 +0200 | [diff] [blame] | 7818 | 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] | 7819 | s->flags |= SF_DIRECT | SF_ASSIGNED; |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 7820 | s->target = &srv->obj_type; |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7821 | break; |
| 7822 | } else { |
| 7823 | /* we found a server, but it's down, |
| 7824 | * mark it as such and go on in case |
| 7825 | * another one is available. |
| 7826 | */ |
| 7827 | txn->flags &= ~TX_CK_MASK; |
| 7828 | txn->flags |= TX_CK_DOWN; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7829 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7830 | } |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7831 | srv = srv->next; |
| 7832 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7833 | |
Willy Tarreau | f64d141 | 2010-10-07 20:06:11 +0200 | [diff] [blame] | 7834 | 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] | 7835 | /* no server matched this cookie or we deliberately skipped it */ |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7836 | txn->flags &= ~TX_CK_MASK; |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 7837 | if ((s->flags & (SF_IGNORE_PRST | SF_ASSIGNED))) |
Willy Tarreau | c89ccb6 | 2012-04-05 21:18:22 +0200 | [diff] [blame] | 7838 | txn->flags |= TX_CK_UNUSED; |
| 7839 | else |
| 7840 | txn->flags |= TX_CK_INVALID; |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7841 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7842 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7843 | /* depending on the cookie mode, we may have to either : |
| 7844 | * - delete the complete cookie if we're in insert+indirect mode, so that |
| 7845 | * the server never sees it ; |
| 7846 | * - remove the server id from the cookie value, and tag the cookie as an |
| 7847 | * application cookie so that it does not get accidentely removed later, |
| 7848 | * if we're in cookie prefix mode |
| 7849 | */ |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 7850 | if ((s->be->ck_opts & PR_CK_PFX) && (delim != val_end)) { |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7851 | int delta; /* negative */ |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7852 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 7853 | delta = buffer_replace2(req->buf, val_beg, delim + 1, NULL, 0); |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7854 | val_end += delta; |
| 7855 | next += delta; |
| 7856 | hdr_end += delta; |
| 7857 | hdr_next += delta; |
| 7858 | cur_hdr->len += delta; |
| 7859 | http_msg_move_end(&txn->req, delta); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7860 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7861 | del_from = NULL; |
| 7862 | preserve_hdr = 1; /* we want to keep this cookie */ |
| 7863 | } |
| 7864 | else if (del_from == NULL && |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 7865 | (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] | 7866 | del_from = prev; |
| 7867 | } |
| 7868 | } else { |
| 7869 | /* This is not our cookie, so we must preserve it. But if we already |
| 7870 | * scheduled another cookie for removal, we cannot remove the |
| 7871 | * complete header, but we can remove the previous block itself. |
| 7872 | */ |
| 7873 | preserve_hdr = 1; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7874 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7875 | if (del_from != NULL) { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 7876 | int delta = del_hdr_value(req->buf, &del_from, prev); |
Willy Tarreau | b810554 | 2010-11-24 18:31:28 +0100 | [diff] [blame] | 7877 | if (att_beg >= del_from) |
| 7878 | att_beg += delta; |
| 7879 | if (att_end >= del_from) |
| 7880 | att_end += delta; |
| 7881 | val_beg += delta; |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7882 | val_end += delta; |
| 7883 | next += delta; |
| 7884 | hdr_end += delta; |
| 7885 | hdr_next += delta; |
| 7886 | cur_hdr->len += delta; |
| 7887 | http_msg_move_end(&txn->req, delta); |
| 7888 | prev = del_from; |
| 7889 | del_from = NULL; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7890 | } |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7891 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7892 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7893 | /* Look for the appsession cookie unless persistence must be ignored */ |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 7894 | if (!(s->flags & SF_IGNORE_PRST) && (s->be->appsession_name != NULL)) { |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7895 | int cmp_len, value_len; |
| 7896 | char *value_begin; |
Aleksandar Lazic | 697bbb0 | 2008-08-13 19:57:02 +0200 | [diff] [blame] | 7897 | |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 7898 | if (s->be->options2 & PR_O2_AS_PFX) { |
| 7899 | cmp_len = MIN(val_end - att_beg, s->be->appsession_name_len); |
| 7900 | value_begin = att_beg + s->be->appsession_name_len; |
| 7901 | value_len = val_end - att_beg - s->be->appsession_name_len; |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7902 | } else { |
| 7903 | cmp_len = att_end - att_beg; |
| 7904 | value_begin = val_beg; |
| 7905 | value_len = val_end - val_beg; |
| 7906 | } |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 7907 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7908 | /* let's see if the cookie is our appcookie */ |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 7909 | if (cmp_len == s->be->appsession_name_len && |
| 7910 | memcmp(att_beg, s->be->appsession_name, cmp_len) == 0) { |
| 7911 | manage_client_side_appsession(s, value_begin, value_len); |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7912 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7913 | } |
| 7914 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7915 | /* continue with next cookie on this header line */ |
| 7916 | att_beg = next; |
| 7917 | } /* for each cookie */ |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7918 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7919 | /* There are no more cookies on this line. |
| 7920 | * We may still have one (or several) marked for deletion at the |
| 7921 | * end of the line. We must do this now in two ways : |
| 7922 | * - if some cookies must be preserved, we only delete from the |
| 7923 | * mark to the end of line ; |
| 7924 | * - if nothing needs to be preserved, simply delete the whole header |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7925 | */ |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7926 | if (del_from) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7927 | int delta; |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7928 | if (preserve_hdr) { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 7929 | delta = del_hdr_value(req->buf, &del_from, hdr_end); |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7930 | hdr_end = del_from; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7931 | cur_hdr->len += delta; |
| 7932 | } else { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 7933 | delta = buffer_replace2(req->buf, hdr_beg, hdr_next, NULL, 0); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7934 | |
| 7935 | /* FIXME: this should be a separate function */ |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 7936 | txn->hdr_idx.v[old_idx].next = cur_hdr->next; |
| 7937 | txn->hdr_idx.used--; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7938 | cur_hdr->len = 0; |
Willy Tarreau | 26db59e | 2010-11-28 06:57:24 +0100 | [diff] [blame] | 7939 | cur_idx = old_idx; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7940 | } |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7941 | hdr_next += delta; |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 7942 | http_msg_move_end(&txn->req, delta); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7943 | } |
| 7944 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7945 | /* check next header */ |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7946 | old_idx = cur_idx; |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7947 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7948 | } |
| 7949 | |
| 7950 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7951 | /* Iterate the same filter through all response headers contained in <rtr>. |
| 7952 | * Returns 1 if this filter can be stopped upon return, otherwise 0. |
| 7953 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 7954 | 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] | 7955 | { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7956 | char *cur_ptr, *cur_end, *cur_next; |
| 7957 | int cur_idx, old_idx, last_hdr; |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 7958 | struct http_txn *txn = s->txn; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7959 | struct hdr_idx_elem *cur_hdr; |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 7960 | int delta; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7961 | |
| 7962 | last_hdr = 0; |
| 7963 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 7964 | cur_next = rtr->buf->p + hdr_idx_first_pos(&txn->hdr_idx); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7965 | old_idx = 0; |
| 7966 | |
| 7967 | while (!last_hdr) { |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 7968 | if (unlikely(txn->flags & TX_SVDENY)) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7969 | return 1; |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 7970 | else if (unlikely(txn->flags & TX_SVALLOW) && |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7971 | (exp->action == ACT_ALLOW || |
| 7972 | exp->action == ACT_DENY)) |
| 7973 | return 0; |
| 7974 | |
| 7975 | cur_idx = txn->hdr_idx.v[old_idx].next; |
| 7976 | if (!cur_idx) |
| 7977 | break; |
| 7978 | |
| 7979 | cur_hdr = &txn->hdr_idx.v[cur_idx]; |
| 7980 | cur_ptr = cur_next; |
| 7981 | cur_end = cur_ptr + cur_hdr->len; |
| 7982 | cur_next = cur_end + cur_hdr->cr + 1; |
| 7983 | |
| 7984 | /* Now we have one header between cur_ptr and cur_end, |
| 7985 | * and the next header starts at cur_next. |
| 7986 | */ |
| 7987 | |
Willy Tarreau | 15a53a4 | 2015-01-21 13:39:42 +0100 | [diff] [blame] | 7988 | 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] | 7989 | switch (exp->action) { |
| 7990 | case ACT_ALLOW: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 7991 | txn->flags |= TX_SVALLOW; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7992 | last_hdr = 1; |
| 7993 | break; |
| 7994 | |
| 7995 | case ACT_DENY: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 7996 | txn->flags |= TX_SVDENY; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7997 | last_hdr = 1; |
| 7998 | break; |
| 7999 | |
| 8000 | case ACT_REPLACE: |
Sasha Pachev | c600204 | 2014-05-26 12:33:48 -0600 | [diff] [blame] | 8001 | trash.len = exp_replace(trash.str, trash.size, cur_ptr, exp->replace, pmatch); |
| 8002 | if (trash.len < 0) |
| 8003 | return -1; |
| 8004 | |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 8005 | 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] | 8006 | /* FIXME: if the user adds a newline in the replacement, the |
| 8007 | * index will not be recalculated for now, and the new line |
| 8008 | * will not be counted as a new header. |
| 8009 | */ |
| 8010 | |
| 8011 | cur_end += delta; |
| 8012 | cur_next += delta; |
| 8013 | cur_hdr->len += delta; |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 8014 | http_msg_move_end(&txn->rsp, delta); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8015 | break; |
| 8016 | |
| 8017 | case ACT_REMOVE: |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 8018 | delta = buffer_replace2(rtr->buf, cur_ptr, cur_next, NULL, 0); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8019 | cur_next += delta; |
| 8020 | |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 8021 | http_msg_move_end(&txn->rsp, delta); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8022 | txn->hdr_idx.v[old_idx].next = cur_hdr->next; |
| 8023 | txn->hdr_idx.used--; |
| 8024 | cur_hdr->len = 0; |
| 8025 | cur_end = NULL; /* null-term has been rewritten */ |
Willy Tarreau | 26db59e | 2010-11-28 06:57:24 +0100 | [diff] [blame] | 8026 | cur_idx = old_idx; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8027 | break; |
| 8028 | |
| 8029 | } |
| 8030 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8031 | |
| 8032 | /* keep the link from this header to next one in case of later |
| 8033 | * removal of next header. |
| 8034 | */ |
| 8035 | old_idx = cur_idx; |
| 8036 | } |
| 8037 | return 0; |
| 8038 | } |
| 8039 | |
| 8040 | |
| 8041 | /* Apply the filter to the status line in the response buffer <rtr>. |
| 8042 | * Returns 0 if nothing has been done, 1 if the filter has been applied, |
| 8043 | * or -1 if a replacement resulted in an invalid status line. |
| 8044 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 8045 | 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] | 8046 | { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8047 | char *cur_ptr, *cur_end; |
| 8048 | int done; |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 8049 | struct http_txn *txn = s->txn; |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 8050 | int delta; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8051 | |
| 8052 | |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 8053 | if (unlikely(txn->flags & TX_SVDENY)) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8054 | return 1; |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 8055 | else if (unlikely(txn->flags & TX_SVALLOW) && |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8056 | (exp->action == ACT_ALLOW || |
| 8057 | exp->action == ACT_DENY)) |
| 8058 | return 0; |
| 8059 | else if (exp->action == ACT_REMOVE) |
| 8060 | return 0; |
| 8061 | |
| 8062 | done = 0; |
| 8063 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 8064 | cur_ptr = rtr->buf->p; |
Willy Tarreau | 1ba0e5f | 2010-06-07 13:57:32 +0200 | [diff] [blame] | 8065 | cur_end = cur_ptr + txn->rsp.sl.st.l; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8066 | |
| 8067 | /* Now we have the status line between cur_ptr and cur_end */ |
| 8068 | |
Willy Tarreau | 15a53a4 | 2015-01-21 13:39:42 +0100 | [diff] [blame] | 8069 | 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] | 8070 | switch (exp->action) { |
| 8071 | case ACT_ALLOW: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 8072 | txn->flags |= TX_SVALLOW; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8073 | done = 1; |
| 8074 | break; |
| 8075 | |
| 8076 | case ACT_DENY: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 8077 | txn->flags |= TX_SVDENY; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8078 | done = 1; |
| 8079 | break; |
| 8080 | |
| 8081 | case ACT_REPLACE: |
Sasha Pachev | c600204 | 2014-05-26 12:33:48 -0600 | [diff] [blame] | 8082 | trash.len = exp_replace(trash.str, trash.size, cur_ptr, exp->replace, pmatch); |
| 8083 | if (trash.len < 0) |
| 8084 | return -1; |
| 8085 | |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 8086 | 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] | 8087 | /* FIXME: if the user adds a newline in the replacement, the |
| 8088 | * index will not be recalculated for now, and the new line |
| 8089 | * will not be counted as a new header. |
| 8090 | */ |
| 8091 | |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 8092 | http_msg_move_end(&txn->rsp, delta); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8093 | cur_end += delta; |
Willy Tarreau | 69d8c5d | 2012-05-08 09:44:41 +0200 | [diff] [blame] | 8094 | cur_end = (char *)http_parse_stsline(&txn->rsp, |
Willy Tarreau | 0278576 | 2007-04-03 14:45:44 +0200 | [diff] [blame] | 8095 | HTTP_MSG_RPVER, |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8096 | cur_ptr, cur_end + 1, |
| 8097 | NULL, NULL); |
| 8098 | if (unlikely(!cur_end)) |
| 8099 | return -1; |
| 8100 | |
| 8101 | /* we have a full respnse and we know that we have either a CR |
| 8102 | * or an LF at <ptr>. |
| 8103 | */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 8104 | 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] | 8105 | 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] | 8106 | /* there is no point trying this regex on headers */ |
| 8107 | return 1; |
| 8108 | } |
| 8109 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8110 | return done; |
| 8111 | } |
| 8112 | |
| 8113 | |
| 8114 | |
| 8115 | /* |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 8116 | * 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] | 8117 | * Returns 0 if everything is alright, or -1 in case a replacement lead to an |
| 8118 | * unparsable response. |
| 8119 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 8120 | 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] | 8121 | { |
Willy Tarreau | 192252e | 2015-04-04 01:47:55 +0200 | [diff] [blame] | 8122 | struct session *sess = s->sess; |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 8123 | struct http_txn *txn = s->txn; |
Willy Tarreau | fdb563c | 2010-01-31 15:43:27 +0100 | [diff] [blame] | 8124 | struct hdr_exp *exp; |
| 8125 | |
| 8126 | for (exp = px->rsp_exp; exp; exp = exp->next) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8127 | int ret; |
| 8128 | |
| 8129 | /* |
| 8130 | * The interleaving of transformations and verdicts |
| 8131 | * makes it difficult to decide to continue or stop |
| 8132 | * the evaluation. |
| 8133 | */ |
| 8134 | |
Willy Tarreau | fdb563c | 2010-01-31 15:43:27 +0100 | [diff] [blame] | 8135 | if (txn->flags & TX_SVDENY) |
| 8136 | break; |
| 8137 | |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 8138 | if ((txn->flags & TX_SVALLOW) && |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8139 | (exp->action == ACT_ALLOW || exp->action == ACT_DENY || |
| 8140 | exp->action == ACT_PASS)) { |
| 8141 | exp = exp->next; |
| 8142 | continue; |
| 8143 | } |
| 8144 | |
Willy Tarreau | fdb563c | 2010-01-31 15:43:27 +0100 | [diff] [blame] | 8145 | /* if this filter had a condition, evaluate it now and skip to |
| 8146 | * next filter if the condition does not match. |
| 8147 | */ |
| 8148 | if (exp->cond) { |
Willy Tarreau | 192252e | 2015-04-04 01:47:55 +0200 | [diff] [blame] | 8149 | 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] | 8150 | ret = acl_pass(ret); |
| 8151 | if (((struct acl_cond *)exp->cond)->pol == ACL_COND_UNLESS) |
| 8152 | ret = !ret; |
| 8153 | if (!ret) |
| 8154 | continue; |
| 8155 | } |
| 8156 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8157 | /* Apply the filter to the status line. */ |
Willy Tarreau | fdb563c | 2010-01-31 15:43:27 +0100 | [diff] [blame] | 8158 | ret = apply_filter_to_sts_line(s, rtr, exp); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8159 | if (unlikely(ret < 0)) |
| 8160 | return -1; |
| 8161 | |
| 8162 | if (likely(ret == 0)) { |
| 8163 | /* The filter did not match the response, it can be |
| 8164 | * iterated through all headers. |
| 8165 | */ |
Sasha Pachev | c600204 | 2014-05-26 12:33:48 -0600 | [diff] [blame] | 8166 | if (unlikely(apply_filter_to_resp_headers(s, rtr, exp) < 0)) |
| 8167 | return -1; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8168 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8169 | } |
| 8170 | return 0; |
| 8171 | } |
| 8172 | |
| 8173 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8174 | /* |
Willy Tarreau | 396d2c6 | 2007-11-04 19:30:00 +0100 | [diff] [blame] | 8175 | * 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] | 8176 | * desirable to call it only when needed. This function is also used when we |
| 8177 | * 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] | 8178 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 8179 | void manage_server_side_cookies(struct stream *s, struct channel *res) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8180 | { |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 8181 | struct http_txn *txn = s->txn; |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 8182 | struct session *sess = s->sess; |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 8183 | struct server *srv; |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 8184 | int is_cookie2; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8185 | int cur_idx, old_idx, delta; |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 8186 | char *hdr_beg, *hdr_end, *hdr_next; |
| 8187 | char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8188 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8189 | /* Iterate through the headers. |
| 8190 | * we start with the start line. |
| 8191 | */ |
| 8192 | old_idx = 0; |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 8193 | hdr_next = res->buf->p + hdr_idx_first_pos(&txn->hdr_idx); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8194 | |
| 8195 | while ((cur_idx = txn->hdr_idx.v[old_idx].next)) { |
| 8196 | struct hdr_idx_elem *cur_hdr; |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 8197 | int val; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8198 | |
| 8199 | cur_hdr = &txn->hdr_idx.v[cur_idx]; |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 8200 | hdr_beg = hdr_next; |
| 8201 | hdr_end = hdr_beg + cur_hdr->len; |
| 8202 | hdr_next = hdr_end + cur_hdr->cr + 1; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8203 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 8204 | /* We have one full header between hdr_beg and hdr_end, and the |
| 8205 | * next header starts at hdr_next. We're only interested in |
| 8206 | * "Set-Cookie" and "Set-Cookie2" headers. |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8207 | */ |
| 8208 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 8209 | is_cookie2 = 0; |
| 8210 | prev = hdr_beg + 10; |
| 8211 | val = http_header_match2(hdr_beg, hdr_end, "Set-Cookie", 10); |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 8212 | if (!val) { |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 8213 | val = http_header_match2(hdr_beg, hdr_end, "Set-Cookie2", 11); |
| 8214 | if (!val) { |
| 8215 | old_idx = cur_idx; |
| 8216 | continue; |
| 8217 | } |
| 8218 | is_cookie2 = 1; |
| 8219 | prev = hdr_beg + 11; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8220 | } |
| 8221 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 8222 | /* OK, right now we know we have a Set-Cookie* at hdr_beg, and |
| 8223 | * <prev> points to the colon. |
| 8224 | */ |
Willy Tarreau | f134831 | 2010-10-07 15:54:11 +0200 | [diff] [blame] | 8225 | txn->flags |= TX_SCK_PRESENT; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8226 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 8227 | /* Maybe we only wanted to see if there was a Set-Cookie (eg: |
| 8228 | * check-cache is enabled) and we are not interested in checking |
| 8229 | * them. Warning, the cookie capture is declared in the frontend. |
Willy Tarreau | fd39dda | 2008-10-17 12:01:58 +0200 | [diff] [blame] | 8230 | */ |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 8231 | if (s->be->cookie_name == NULL && |
| 8232 | s->be->appsession_name == NULL && |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 8233 | sess->fe->capture_name == NULL) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8234 | return; |
| 8235 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 8236 | /* OK so now we know we have to process this response cookie. |
| 8237 | * The format of the Set-Cookie header is slightly different |
| 8238 | * from the format of the Cookie header in that it does not |
| 8239 | * support the comma as a cookie delimiter (thus the header |
| 8240 | * cannot be folded) because the Expires attribute described in |
| 8241 | * the original Netscape's spec may contain an unquoted date |
| 8242 | * with a comma inside. We have to live with this because |
| 8243 | * many browsers don't support Max-Age and some browsers don't |
| 8244 | * support quoted strings. However the Set-Cookie2 header is |
| 8245 | * clean. |
| 8246 | * |
| 8247 | * We have to keep multiple pointers in order to support cookie |
| 8248 | * removal at the beginning, middle or end of header without |
| 8249 | * corrupting the header (in case of set-cookie2). A special |
| 8250 | * pointer, <scav> points to the beginning of the set-cookie-av |
| 8251 | * fields after the first semi-colon. The <next> pointer points |
| 8252 | * either to the end of line (set-cookie) or next unquoted comma |
| 8253 | * (set-cookie2). All of these headers are valid : |
| 8254 | * |
| 8255 | * Set-Cookie: NAME1 = VALUE 1 ; Secure; Path="/"\r\n |
| 8256 | * Set-Cookie:NAME=VALUE; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT\r\n |
| 8257 | * Set-Cookie: NAME = VALUE ; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT\r\n |
| 8258 | * Set-Cookie2: NAME1 = VALUE 1 ; Max-Age=0, NAME2=VALUE2; Discard\r\n |
| 8259 | * | | | | | | | | | | |
| 8260 | * | | | | | | | | +-> next hdr_end <--+ |
| 8261 | * | | | | | | | +------------> scav |
| 8262 | * | | | | | | +--------------> val_end |
| 8263 | * | | | | | +--------------------> val_beg |
| 8264 | * | | | | +----------------------> equal |
| 8265 | * | | | +------------------------> att_end |
| 8266 | * | | +----------------------------> att_beg |
| 8267 | * | +------------------------------> prev |
| 8268 | * +-----------------------------------------> hdr_beg |
| 8269 | */ |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8270 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 8271 | for (; prev < hdr_end; prev = next) { |
| 8272 | /* Iterate through all cookies on this line */ |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8273 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 8274 | /* find att_beg */ |
| 8275 | att_beg = prev + 1; |
| 8276 | while (att_beg < hdr_end && http_is_spht[(unsigned char)*att_beg]) |
| 8277 | att_beg++; |
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 | /* find att_end : this is the first character after the last non |
| 8280 | * space before the equal. It may be equal to hdr_end. |
| 8281 | */ |
| 8282 | equal = att_end = att_beg; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8283 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 8284 | while (equal < hdr_end) { |
| 8285 | if (*equal == '=' || *equal == ';' || (is_cookie2 && *equal == ',')) |
| 8286 | break; |
| 8287 | if (http_is_spht[(unsigned char)*equal++]) |
| 8288 | continue; |
| 8289 | att_end = equal; |
| 8290 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8291 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 8292 | /* here, <equal> points to '=', a delimitor or the end. <att_end> |
| 8293 | * is between <att_beg> and <equal>, both may be identical. |
| 8294 | */ |
| 8295 | |
| 8296 | /* look for end of cookie if there is an equal sign */ |
| 8297 | if (equal < hdr_end && *equal == '=') { |
| 8298 | /* look for the beginning of the value */ |
| 8299 | val_beg = equal + 1; |
| 8300 | while (val_beg < hdr_end && http_is_spht[(unsigned char)*val_beg]) |
| 8301 | val_beg++; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8302 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 8303 | /* find the end of the value, respecting quotes */ |
| 8304 | next = find_cookie_value_end(val_beg, hdr_end); |
| 8305 | |
| 8306 | /* make val_end point to the first white space or delimitor after the value */ |
| 8307 | val_end = next; |
| 8308 | while (val_end > val_beg && http_is_spht[(unsigned char)*(val_end - 1)]) |
| 8309 | val_end--; |
| 8310 | } else { |
| 8311 | /* <equal> points to next comma, semi-colon or EOL */ |
| 8312 | val_beg = val_end = next = equal; |
| 8313 | } |
| 8314 | |
| 8315 | if (next < hdr_end) { |
| 8316 | /* Set-Cookie2 supports multiple cookies, and <next> points to |
| 8317 | * a colon or semi-colon before the end. So skip all attr-value |
| 8318 | * pairs and look for the next comma. For Set-Cookie, since |
| 8319 | * commas are permitted in values, skip to the end. |
| 8320 | */ |
| 8321 | if (is_cookie2) |
| 8322 | next = find_hdr_value_end(next, hdr_end); |
| 8323 | else |
| 8324 | next = hdr_end; |
| 8325 | } |
| 8326 | |
| 8327 | /* Now everything is as on the diagram above */ |
| 8328 | |
| 8329 | /* Ignore cookies with no equal sign */ |
| 8330 | if (equal == val_end) |
| 8331 | continue; |
| 8332 | |
| 8333 | /* If there are spaces around the equal sign, we need to |
| 8334 | * strip them otherwise we'll get trouble for cookie captures, |
| 8335 | * or even for rewrites. Since this happens extremely rarely, |
| 8336 | * it does not hurt performance. |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8337 | */ |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 8338 | if (unlikely(att_end != equal || val_beg > equal + 1)) { |
| 8339 | int stripped_before = 0; |
| 8340 | int stripped_after = 0; |
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 | if (att_end != equal) { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 8343 | stripped_before = buffer_replace2(res->buf, att_end, equal, NULL, 0); |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 8344 | equal += stripped_before; |
| 8345 | val_beg += stripped_before; |
| 8346 | } |
| 8347 | |
| 8348 | if (val_beg > equal + 1) { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 8349 | stripped_after = buffer_replace2(res->buf, equal + 1, val_beg, NULL, 0); |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 8350 | val_beg += stripped_after; |
| 8351 | stripped_before += stripped_after; |
| 8352 | } |
| 8353 | |
| 8354 | val_end += stripped_before; |
| 8355 | next += stripped_before; |
| 8356 | hdr_end += stripped_before; |
| 8357 | hdr_next += stripped_before; |
| 8358 | cur_hdr->len += stripped_before; |
Willy Tarreau | 1fc1f45 | 2011-04-07 22:35:37 +0200 | [diff] [blame] | 8359 | http_msg_move_end(&txn->rsp, stripped_before); |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 8360 | } |
| 8361 | |
| 8362 | /* First, let's see if we want to capture this cookie. We check |
| 8363 | * that we don't already have a server side cookie, because we |
| 8364 | * can only capture one. Also as an optimisation, we ignore |
| 8365 | * cookies shorter than the declared name. |
| 8366 | */ |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 8367 | if (sess->fe->capture_name != NULL && |
Willy Tarreau | 3bac9ff | 2007-03-18 17:31:28 +0100 | [diff] [blame] | 8368 | txn->srv_cookie == NULL && |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 8369 | (val_end - att_beg >= sess->fe->capture_namelen) && |
| 8370 | memcmp(att_beg, sess->fe->capture_name, sess->fe->capture_namelen) == 0) { |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 8371 | int log_len = val_end - att_beg; |
Willy Tarreau | 086b3b4 | 2007-05-13 21:45:51 +0200 | [diff] [blame] | 8372 | if ((txn->srv_cookie = pool_alloc2(pool2_capture)) == NULL) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8373 | Alert("HTTP logging : out of memory.\n"); |
| 8374 | } |
Willy Tarreau | f70fc75 | 2010-11-19 11:27:18 +0100 | [diff] [blame] | 8375 | else { |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 8376 | if (log_len > sess->fe->capture_len) |
| 8377 | log_len = sess->fe->capture_len; |
Willy Tarreau | f70fc75 | 2010-11-19 11:27:18 +0100 | [diff] [blame] | 8378 | memcpy(txn->srv_cookie, att_beg, log_len); |
| 8379 | txn->srv_cookie[log_len] = 0; |
| 8380 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8381 | } |
| 8382 | |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 8383 | srv = objt_server(s->target); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8384 | /* now check if we need to process it for persistence */ |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 8385 | if (!(s->flags & SF_IGNORE_PRST) && |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 8386 | (att_end - att_beg == s->be->cookie_len) && (s->be->cookie_name != NULL) && |
| 8387 | (memcmp(att_beg, s->be->cookie_name, att_end - att_beg) == 0)) { |
Willy Tarreau | f134831 | 2010-10-07 15:54:11 +0200 | [diff] [blame] | 8388 | /* assume passive cookie by default */ |
| 8389 | txn->flags &= ~TX_SCK_MASK; |
| 8390 | txn->flags |= TX_SCK_FOUND; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8391 | |
| 8392 | /* If the cookie is in insert mode on a known server, we'll delete |
| 8393 | * this occurrence because we'll insert another one later. |
| 8394 | * 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] | 8395 | * a direct access. |
| 8396 | */ |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 8397 | if (s->be->ck_opts & PR_CK_PSV) { |
Willy Tarreau | ba4c5be | 2010-10-23 12:46:42 +0200 | [diff] [blame] | 8398 | /* The "preserve" flag was set, we don't want to touch the |
| 8399 | * server's cookie. |
| 8400 | */ |
| 8401 | } |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 8402 | else if ((srv && (s->be->ck_opts & PR_CK_INS)) || |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 8403 | ((s->flags & SF_DIRECT) && (s->be->ck_opts & PR_CK_IND))) { |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 8404 | /* this cookie must be deleted */ |
| 8405 | if (*prev == ':' && next == hdr_end) { |
| 8406 | /* whole header */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 8407 | delta = buffer_replace2(res->buf, hdr_beg, hdr_next, NULL, 0); |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 8408 | txn->hdr_idx.v[old_idx].next = cur_hdr->next; |
| 8409 | txn->hdr_idx.used--; |
| 8410 | cur_hdr->len = 0; |
Willy Tarreau | 26db59e | 2010-11-28 06:57:24 +0100 | [diff] [blame] | 8411 | cur_idx = old_idx; |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 8412 | hdr_next += delta; |
| 8413 | http_msg_move_end(&txn->rsp, delta); |
| 8414 | /* note: while both invalid now, <next> and <hdr_end> |
| 8415 | * are still equal, so the for() will stop as expected. |
| 8416 | */ |
| 8417 | } else { |
| 8418 | /* just remove the value */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 8419 | int delta = del_hdr_value(res->buf, &prev, next); |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 8420 | next = prev; |
| 8421 | hdr_end += delta; |
| 8422 | hdr_next += delta; |
| 8423 | cur_hdr->len += delta; |
| 8424 | http_msg_move_end(&txn->rsp, delta); |
| 8425 | } |
Willy Tarreau | f134831 | 2010-10-07 15:54:11 +0200 | [diff] [blame] | 8426 | txn->flags &= ~TX_SCK_MASK; |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 8427 | txn->flags |= TX_SCK_DELETED; |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 8428 | /* and go on with next cookie */ |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8429 | } |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 8430 | else if (srv && srv->cookie && (s->be->ck_opts & PR_CK_RW)) { |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 8431 | /* replace bytes val_beg->val_end with the cookie name associated |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8432 | * with this server since we know it. |
| 8433 | */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 8434 | 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] | 8435 | next += delta; |
| 8436 | hdr_end += delta; |
| 8437 | hdr_next += delta; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8438 | cur_hdr->len += delta; |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 8439 | http_msg_move_end(&txn->rsp, delta); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8440 | |
Willy Tarreau | f134831 | 2010-10-07 15:54:11 +0200 | [diff] [blame] | 8441 | txn->flags &= ~TX_SCK_MASK; |
| 8442 | txn->flags |= TX_SCK_REPLACED; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8443 | } |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 8444 | else if (srv && srv->cookie && (s->be->ck_opts & PR_CK_PFX)) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8445 | /* insert the cookie name associated with this server |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 8446 | * before existing cookie, and insert a delimiter between them.. |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8447 | */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 8448 | 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] | 8449 | next += delta; |
| 8450 | hdr_end += delta; |
| 8451 | hdr_next += delta; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8452 | cur_hdr->len += delta; |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 8453 | http_msg_move_end(&txn->rsp, delta); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8454 | |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 8455 | val_beg[srv->cklen] = COOKIE_DELIM; |
Willy Tarreau | f134831 | 2010-10-07 15:54:11 +0200 | [diff] [blame] | 8456 | txn->flags &= ~TX_SCK_MASK; |
| 8457 | txn->flags |= TX_SCK_REPLACED; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8458 | } |
| 8459 | } |
Cyril Bonté | 47fdd8e | 2010-04-25 00:00:51 +0200 | [diff] [blame] | 8460 | /* next, let's see if the cookie is our appcookie, unless persistence must be ignored */ |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 8461 | else if (!(s->flags & SF_IGNORE_PRST) && (s->be->appsession_name != NULL)) { |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 8462 | int cmp_len, value_len; |
| 8463 | char *value_begin; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8464 | |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 8465 | if (s->be->options2 & PR_O2_AS_PFX) { |
| 8466 | cmp_len = MIN(val_end - att_beg, s->be->appsession_name_len); |
| 8467 | value_begin = att_beg + s->be->appsession_name_len; |
| 8468 | value_len = MIN(s->be->appsession_len, val_end - att_beg - s->be->appsession_name_len); |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 8469 | } else { |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 8470 | cmp_len = att_end - att_beg; |
| 8471 | value_begin = val_beg; |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 8472 | value_len = MIN(s->be->appsession_len, val_end - val_beg); |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 8473 | } |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 8474 | |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 8475 | if ((cmp_len == s->be->appsession_name_len) && |
| 8476 | (memcmp(att_beg, s->be->appsession_name, s->be->appsession_name_len) == 0)) { |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 8477 | /* free a possibly previously allocated memory */ |
| 8478 | pool_free2(apools.sessid, txn->sessid); |
| 8479 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 8480 | /* Store the sessid in the stream for future use */ |
Willy Tarreau | a3377ee | 2010-01-10 10:49:11 +0100 | [diff] [blame] | 8481 | if ((txn->sessid = pool_alloc2(apools.sessid)) == NULL) { |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 8482 | Alert("Not enough Memory process_srv():asession->sessid:malloc().\n"); |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 8483 | send_log(s->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n"); |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 8484 | return; |
| 8485 | } |
Willy Tarreau | a3377ee | 2010-01-10 10:49:11 +0100 | [diff] [blame] | 8486 | memcpy(txn->sessid, value_begin, value_len); |
| 8487 | txn->sessid[value_len] = 0; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8488 | } |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 8489 | } |
| 8490 | /* that's done for this cookie, check the next one on the same |
| 8491 | * line when next != hdr_end (only if is_cookie2). |
| 8492 | */ |
| 8493 | } |
| 8494 | /* check next header */ |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8495 | old_idx = cur_idx; |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 8496 | } |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 8497 | |
Willy Tarreau | a3377ee | 2010-01-10 10:49:11 +0100 | [diff] [blame] | 8498 | if (txn->sessid != NULL) { |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 8499 | appsess *asession = NULL; |
| 8500 | /* only do insert, if lookup fails */ |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 8501 | asession = appsession_hash_lookup(&(s->be->htbl_proxy), txn->sessid); |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 8502 | if (asession == NULL) { |
Willy Tarreau | 1fac753 | 2010-01-09 19:23:06 +0100 | [diff] [blame] | 8503 | size_t server_id_len; |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 8504 | if ((asession = pool_alloc2(pool2_appsess)) == NULL) { |
| 8505 | Alert("Not enough Memory process_srv():asession:calloc().\n"); |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 8506 | send_log(s->be, LOG_ALERT, "Not enough Memory process_srv():asession:calloc().\n"); |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 8507 | return; |
| 8508 | } |
Willy Tarreau | 77eb9b8 | 2010-11-19 11:29:06 +0100 | [diff] [blame] | 8509 | asession->serverid = NULL; /* to avoid a double free in case of allocation error */ |
| 8510 | |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 8511 | if ((asession->sessid = pool_alloc2(apools.sessid)) == NULL) { |
| 8512 | Alert("Not enough Memory process_srv():asession->sessid:malloc().\n"); |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 8513 | send_log(s->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n"); |
| 8514 | s->be->htbl_proxy.destroy(asession); |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 8515 | return; |
| 8516 | } |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 8517 | memcpy(asession->sessid, txn->sessid, s->be->appsession_len); |
| 8518 | asession->sessid[s->be->appsession_len] = 0; |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 8519 | |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 8520 | server_id_len = strlen(objt_server(s->target)->id) + 1; |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 8521 | if ((asession->serverid = pool_alloc2(apools.serverid)) == NULL) { |
Willy Tarreau | 77eb9b8 | 2010-11-19 11:29:06 +0100 | [diff] [blame] | 8522 | Alert("Not enough Memory process_srv():asession->serverid:malloc().\n"); |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 8523 | send_log(s->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n"); |
| 8524 | s->be->htbl_proxy.destroy(asession); |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 8525 | return; |
| 8526 | } |
| 8527 | asession->serverid[0] = '\0'; |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 8528 | memcpy(asession->serverid, objt_server(s->target)->id, server_id_len); |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 8529 | |
| 8530 | asession->request_count = 0; |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 8531 | appsession_hash_insert(&(s->be->htbl_proxy), asession); |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 8532 | } |
| 8533 | |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 8534 | asession->expire = tick_add_ifset(now_ms, s->be->timeout.appsession); |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 8535 | asession->request_count++; |
| 8536 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8537 | } |
| 8538 | |
| 8539 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8540 | /* |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 8541 | * Check if response is cacheable or not. Updates s->flags. |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8542 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 8543 | void check_response_for_cacheability(struct stream *s, struct channel *rtr) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8544 | { |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 8545 | struct http_txn *txn = s->txn; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8546 | char *p1, *p2; |
| 8547 | |
| 8548 | char *cur_ptr, *cur_end, *cur_next; |
| 8549 | int cur_idx; |
| 8550 | |
Willy Tarreau | 5df5187 | 2007-11-25 16:20:08 +0100 | [diff] [blame] | 8551 | if (!(txn->flags & TX_CACHEABLE)) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8552 | return; |
| 8553 | |
| 8554 | /* Iterate through the headers. |
| 8555 | * we start with the start line. |
| 8556 | */ |
| 8557 | cur_idx = 0; |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 8558 | cur_next = rtr->buf->p + hdr_idx_first_pos(&txn->hdr_idx); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8559 | |
| 8560 | while ((cur_idx = txn->hdr_idx.v[cur_idx].next)) { |
| 8561 | struct hdr_idx_elem *cur_hdr; |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 8562 | int val; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8563 | |
| 8564 | cur_hdr = &txn->hdr_idx.v[cur_idx]; |
| 8565 | cur_ptr = cur_next; |
| 8566 | cur_end = cur_ptr + cur_hdr->len; |
| 8567 | cur_next = cur_end + cur_hdr->cr + 1; |
| 8568 | |
| 8569 | /* We have one full header between cur_ptr and cur_end, and the |
| 8570 | * next header starts at cur_next. We're only interested in |
| 8571 | * "Cookie:" headers. |
| 8572 | */ |
| 8573 | |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 8574 | val = http_header_match2(cur_ptr, cur_end, "Pragma", 6); |
| 8575 | if (val) { |
| 8576 | if ((cur_end - (cur_ptr + val) >= 8) && |
| 8577 | strncasecmp(cur_ptr + val, "no-cache", 8) == 0) { |
| 8578 | txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK; |
| 8579 | return; |
| 8580 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8581 | } |
| 8582 | |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 8583 | val = http_header_match2(cur_ptr, cur_end, "Cache-control", 13); |
| 8584 | if (!val) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8585 | continue; |
| 8586 | |
| 8587 | /* OK, right now we know we have a cache-control header at cur_ptr */ |
| 8588 | |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 8589 | p1 = cur_ptr + val; /* first non-space char after 'cache-control:' */ |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8590 | |
| 8591 | if (p1 >= cur_end) /* no more info */ |
| 8592 | continue; |
| 8593 | |
| 8594 | /* p1 is at the beginning of the value */ |
| 8595 | p2 = p1; |
| 8596 | |
Willy Tarreau | 8f8e645 | 2007-06-17 21:51:38 +0200 | [diff] [blame] | 8597 | while (p2 < cur_end && *p2 != '=' && *p2 != ',' && !isspace((unsigned char)*p2)) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8598 | p2++; |
| 8599 | |
| 8600 | /* we have a complete value between p1 and p2 */ |
| 8601 | if (p2 < cur_end && *p2 == '=') { |
| 8602 | /* we have something of the form no-cache="set-cookie" */ |
| 8603 | if ((cur_end - p1 >= 21) && |
| 8604 | strncasecmp(p1, "no-cache=\"set-cookie", 20) == 0 |
| 8605 | && (p1[20] == '"' || p1[20] == ',')) |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 8606 | txn->flags &= ~TX_CACHE_COOK; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8607 | continue; |
| 8608 | } |
| 8609 | |
| 8610 | /* OK, so we know that either p2 points to the end of string or to a comma */ |
| 8611 | if (((p2 - p1 == 7) && strncasecmp(p1, "private", 7) == 0) || |
Willy Tarreau | 5b15f90 | 2013-07-04 12:46:56 +0200 | [diff] [blame] | 8612 | ((p2 - p1 == 8) && strncasecmp(p1, "no-cache", 8) == 0) || |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8613 | ((p2 - p1 == 8) && strncasecmp(p1, "no-store", 8) == 0) || |
| 8614 | ((p2 - p1 == 9) && strncasecmp(p1, "max-age=0", 9) == 0) || |
| 8615 | ((p2 - p1 == 10) && strncasecmp(p1, "s-maxage=0", 10) == 0)) { |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 8616 | txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8617 | return; |
| 8618 | } |
| 8619 | |
| 8620 | if ((p2 - p1 == 6) && strncasecmp(p1, "public", 6) == 0) { |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 8621 | txn->flags |= TX_CACHEABLE | TX_CACHE_COOK; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8622 | continue; |
| 8623 | } |
| 8624 | } |
| 8625 | } |
| 8626 | |
| 8627 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 8628 | /* |
| 8629 | * Try to retrieve a known appsession in the URI, then the associated server. |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 8630 | * If the server is found, it's assigned to the stream. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 8631 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 8632 | void get_srv_from_appsession(struct stream *s, const char *begin, int len) |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 8633 | { |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 8634 | char *end_params, *first_param, *cur_param, *next_param; |
| 8635 | char separator; |
| 8636 | int value_len; |
| 8637 | |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 8638 | int mode = s->be->options2 & PR_O2_AS_M_ANY; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 8639 | |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 8640 | if (s->be->appsession_name == NULL || |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 8641 | (s->txn->meth != HTTP_METH_GET && s->txn->meth != HTTP_METH_POST && s->txn->meth != HTTP_METH_HEAD)) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 8642 | return; |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 8643 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 8644 | |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 8645 | first_param = NULL; |
| 8646 | switch (mode) { |
| 8647 | case PR_O2_AS_M_PP: |
| 8648 | first_param = memchr(begin, ';', len); |
| 8649 | break; |
| 8650 | case PR_O2_AS_M_QS: |
| 8651 | first_param = memchr(begin, '?', len); |
| 8652 | break; |
| 8653 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 8654 | |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 8655 | if (first_param == NULL) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 8656 | return; |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 8657 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 8658 | |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 8659 | switch (mode) { |
| 8660 | case PR_O2_AS_M_PP: |
| 8661 | if ((end_params = memchr(first_param, '?', len - (begin - first_param))) == NULL) { |
| 8662 | end_params = (char *) begin + len; |
| 8663 | } |
| 8664 | separator = ';'; |
| 8665 | break; |
| 8666 | case PR_O2_AS_M_QS: |
| 8667 | end_params = (char *) begin + len; |
| 8668 | separator = '&'; |
| 8669 | break; |
| 8670 | default: |
| 8671 | /* unknown mode, shouldn't happen */ |
| 8672 | return; |
| 8673 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 8674 | |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 8675 | cur_param = next_param = end_params; |
| 8676 | while (cur_param > first_param) { |
| 8677 | cur_param--; |
| 8678 | if ((cur_param[0] == separator) || (cur_param == first_param)) { |
| 8679 | /* let's see if this is the appsession parameter */ |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 8680 | if ((cur_param + s->be->appsession_name_len + 1 < next_param) && |
| 8681 | ((s->be->options2 & PR_O2_AS_PFX) || cur_param[s->be->appsession_name_len + 1] == '=') && |
| 8682 | (strncasecmp(cur_param + 1, s->be->appsession_name, s->be->appsession_name_len) == 0)) { |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 8683 | /* Cool... it's the right one */ |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 8684 | cur_param += s->be->appsession_name_len + (s->be->options2 & PR_O2_AS_PFX ? 1 : 2); |
| 8685 | value_len = MIN(s->be->appsession_len, next_param - cur_param); |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 8686 | if (value_len > 0) { |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 8687 | manage_client_side_appsession(s, cur_param, value_len); |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 8688 | } |
| 8689 | break; |
| 8690 | } |
| 8691 | next_param = cur_param; |
| 8692 | } |
| 8693 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 8694 | #if defined(DEBUG_HASH) |
Aleksandar Lazic | 697bbb0 | 2008-08-13 19:57:02 +0200 | [diff] [blame] | 8695 | Alert("get_srv_from_appsession\n"); |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 8696 | appsession_hash_dump(&(s->be->htbl_proxy)); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 8697 | #endif |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 8698 | } |
| 8699 | |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 8700 | /* |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 8701 | * 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] | 8702 | * for the current backend. |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 8703 | * |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 8704 | * 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] | 8705 | * uri_auth field is valid. |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 8706 | * |
Krzysztof Piotr Oledzki | 8c8bd45 | 2010-01-29 19:29:32 +0100 | [diff] [blame] | 8707 | * Returns 1 if stats should be provided, otherwise 0. |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 8708 | */ |
Willy Tarreau | 295a837 | 2011-03-10 11:25:07 +0100 | [diff] [blame] | 8709 | 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] | 8710 | { |
| 8711 | struct uri_auth *uri_auth = backend->uri_auth; |
Willy Tarreau | 3a215be | 2012-03-09 21:39:51 +0100 | [diff] [blame] | 8712 | struct http_msg *msg = &txn->req; |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 8713 | const char *uri = msg->chn->buf->p+ msg->sl.rq.u; |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 8714 | |
Krzysztof Piotr Oledzki | 8c8bd45 | 2010-01-29 19:29:32 +0100 | [diff] [blame] | 8715 | if (!uri_auth) |
| 8716 | return 0; |
| 8717 | |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 8718 | 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] | 8719 | return 0; |
| 8720 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 8721 | /* check URI size */ |
Willy Tarreau | 3a215be | 2012-03-09 21:39:51 +0100 | [diff] [blame] | 8722 | if (uri_auth->uri_len > msg->sl.rq.u_l) |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 8723 | return 0; |
| 8724 | |
Willy Tarreau | 414e9bb | 2013-11-23 00:30:38 +0100 | [diff] [blame] | 8725 | if (memcmp(uri, uri_auth->uri_prefix, uri_auth->uri_len) != 0) |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 8726 | return 0; |
| 8727 | |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 8728 | return 1; |
| 8729 | } |
| 8730 | |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 8731 | /* |
| 8732 | * 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] | 8733 | * By default it tries to report the error position as msg->err_pos. However if |
| 8734 | * this one is not set, it will then report msg->next, which is the last known |
| 8735 | * 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] | 8736 | * displays buffers as a contiguous area starting at buf->p. |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 8737 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 8738 | void http_capture_bad_message(struct error_snapshot *es, struct stream *s, |
Willy Tarreau | 8a0cef2 | 2012-03-09 13:39:23 +0100 | [diff] [blame] | 8739 | struct http_msg *msg, |
Willy Tarreau | 3770f23 | 2013-12-07 00:01:53 +0100 | [diff] [blame] | 8740 | enum ht_state state, struct proxy *other_end) |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 8741 | { |
Willy Tarreau | 9ad7bd4 | 2015-04-03 19:19:59 +0200 | [diff] [blame] | 8742 | struct session *sess = strm_sess(s); |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 8743 | struct channel *chn = msg->chn; |
Willy Tarreau | 69d8c5d | 2012-05-08 09:44:41 +0200 | [diff] [blame] | 8744 | int len1, len2; |
Willy Tarreau | 8a0cef2 | 2012-03-09 13:39:23 +0100 | [diff] [blame] | 8745 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 8746 | es->len = MIN(chn->buf->i, sizeof(es->buf)); |
| 8747 | len1 = chn->buf->data + chn->buf->size - chn->buf->p; |
Willy Tarreau | 69d8c5d | 2012-05-08 09:44:41 +0200 | [diff] [blame] | 8748 | len1 = MIN(len1, es->len); |
| 8749 | len2 = es->len - len1; /* remaining data if buffer wraps */ |
| 8750 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 8751 | memcpy(es->buf, chn->buf->p, len1); |
Willy Tarreau | 69d8c5d | 2012-05-08 09:44:41 +0200 | [diff] [blame] | 8752 | if (len2) |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 8753 | memcpy(es->buf + len1, chn->buf->data, len2); |
Willy Tarreau | 81f2fb9 | 2010-12-12 13:09:08 +0100 | [diff] [blame] | 8754 | |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 8755 | if (msg->err_pos >= 0) |
Willy Tarreau | 69d8c5d | 2012-05-08 09:44:41 +0200 | [diff] [blame] | 8756 | es->pos = msg->err_pos; |
Willy Tarreau | 81f2fb9 | 2010-12-12 13:09:08 +0100 | [diff] [blame] | 8757 | else |
Willy Tarreau | 69d8c5d | 2012-05-08 09:44:41 +0200 | [diff] [blame] | 8758 | es->pos = msg->next; |
Willy Tarreau | 81f2fb9 | 2010-12-12 13:09:08 +0100 | [diff] [blame] | 8759 | |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 8760 | es->when = date; // user-visible date |
| 8761 | es->sid = s->uniq_id; |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 8762 | es->srv = objt_server(s->target); |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 8763 | es->oe = other_end; |
Willy Tarreau | 9ad7bd4 | 2015-04-03 19:19:59 +0200 | [diff] [blame] | 8764 | if (objt_conn(sess->origin)) |
| 8765 | es->src = __objt_conn(sess->origin)->addr.from; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 8766 | else |
| 8767 | memset(&es->src, 0, sizeof(es->src)); |
| 8768 | |
Willy Tarreau | 078272e | 2010-12-12 12:46:33 +0100 | [diff] [blame] | 8769 | es->state = state; |
Willy Tarreau | 10479e4 | 2010-12-12 14:00:34 +0100 | [diff] [blame] | 8770 | es->ev_id = error_snapshot_id++; |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 8771 | es->b_flags = chn->flags; |
Willy Tarreau | d04b1bc | 2012-05-08 11:03:10 +0200 | [diff] [blame] | 8772 | es->s_flags = s->flags; |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 8773 | es->t_flags = s->txn->flags; |
Willy Tarreau | d04b1bc | 2012-05-08 11:03:10 +0200 | [diff] [blame] | 8774 | es->m_flags = msg->flags; |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 8775 | es->b_out = chn->buf->o; |
| 8776 | es->b_wrap = chn->buf->data + chn->buf->size - chn->buf->p; |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 8777 | es->b_tot = chn->total; |
Willy Tarreau | d04b1bc | 2012-05-08 11:03:10 +0200 | [diff] [blame] | 8778 | es->m_clen = msg->chunk_len; |
| 8779 | es->m_blen = msg->body_len; |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 8780 | } |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 8781 | |
Willy Tarreau | 294c473 | 2011-12-16 21:35:50 +0100 | [diff] [blame] | 8782 | /* Return in <vptr> and <vlen> the pointer and length of occurrence <occ> of |
| 8783 | * header whose name is <hname> of length <hlen>. If <ctx> is null, lookup is |
| 8784 | * performed over the whole headers. Otherwise it must contain a valid header |
| 8785 | * context, initialised with ctx->idx=0 for the first lookup in a series. If |
| 8786 | * <occ> is positive or null, occurrence #occ from the beginning (or last ctx) |
| 8787 | * is returned. Occ #0 and #1 are equivalent. If <occ> is negative (and no less |
| 8788 | * 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] | 8789 | * -1. The value fetch stops at commas, so this function is suited for use with |
| 8790 | * list headers. |
Willy Tarreau | 294c473 | 2011-12-16 21:35:50 +0100 | [diff] [blame] | 8791 | * 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] | 8792 | */ |
Willy Tarreau | 185b5c4 | 2012-04-26 15:11:51 +0200 | [diff] [blame] | 8793 | 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] | 8794 | struct hdr_idx *idx, int occ, |
| 8795 | struct hdr_ctx *ctx, char **vptr, int *vlen) |
Willy Tarreau | bce7088 | 2009-09-07 11:51:47 +0200 | [diff] [blame] | 8796 | { |
Willy Tarreau | 294c473 | 2011-12-16 21:35:50 +0100 | [diff] [blame] | 8797 | struct hdr_ctx local_ctx; |
| 8798 | char *ptr_hist[MAX_HDR_HISTORY]; |
| 8799 | int len_hist[MAX_HDR_HISTORY]; |
Willy Tarreau | bce7088 | 2009-09-07 11:51:47 +0200 | [diff] [blame] | 8800 | unsigned int hist_ptr; |
Willy Tarreau | 294c473 | 2011-12-16 21:35:50 +0100 | [diff] [blame] | 8801 | int found; |
Willy Tarreau | bce7088 | 2009-09-07 11:51:47 +0200 | [diff] [blame] | 8802 | |
Willy Tarreau | 294c473 | 2011-12-16 21:35:50 +0100 | [diff] [blame] | 8803 | if (!ctx) { |
| 8804 | local_ctx.idx = 0; |
| 8805 | ctx = &local_ctx; |
| 8806 | } |
| 8807 | |
Willy Tarreau | bce7088 | 2009-09-07 11:51:47 +0200 | [diff] [blame] | 8808 | if (occ >= 0) { |
Willy Tarreau | 294c473 | 2011-12-16 21:35:50 +0100 | [diff] [blame] | 8809 | /* search from the beginning */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 8810 | while (http_find_header2(hname, hlen, msg->chn->buf->p, idx, ctx)) { |
Willy Tarreau | bce7088 | 2009-09-07 11:51:47 +0200 | [diff] [blame] | 8811 | occ--; |
| 8812 | if (occ <= 0) { |
Willy Tarreau | 294c473 | 2011-12-16 21:35:50 +0100 | [diff] [blame] | 8813 | *vptr = ctx->line + ctx->val; |
| 8814 | *vlen = ctx->vlen; |
| 8815 | return 1; |
Willy Tarreau | bce7088 | 2009-09-07 11:51:47 +0200 | [diff] [blame] | 8816 | } |
| 8817 | } |
Willy Tarreau | 294c473 | 2011-12-16 21:35:50 +0100 | [diff] [blame] | 8818 | return 0; |
Willy Tarreau | bce7088 | 2009-09-07 11:51:47 +0200 | [diff] [blame] | 8819 | } |
| 8820 | |
| 8821 | /* negative occurrence, we scan all the list then walk back */ |
| 8822 | if (-occ > MAX_HDR_HISTORY) |
| 8823 | return 0; |
| 8824 | |
Willy Tarreau | 294c473 | 2011-12-16 21:35:50 +0100 | [diff] [blame] | 8825 | found = hist_ptr = 0; |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 8826 | while (http_find_header2(hname, hlen, msg->chn->buf->p, idx, ctx)) { |
Willy Tarreau | 294c473 | 2011-12-16 21:35:50 +0100 | [diff] [blame] | 8827 | ptr_hist[hist_ptr] = ctx->line + ctx->val; |
| 8828 | len_hist[hist_ptr] = ctx->vlen; |
| 8829 | if (++hist_ptr >= MAX_HDR_HISTORY) |
Willy Tarreau | bce7088 | 2009-09-07 11:51:47 +0200 | [diff] [blame] | 8830 | hist_ptr = 0; |
| 8831 | found++; |
| 8832 | } |
| 8833 | if (-occ > found) |
| 8834 | return 0; |
| 8835 | /* 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] | 8836 | * find occurrence -occ. 0 <= hist_ptr < MAX_HDR_HISTORY, and we have |
| 8837 | * -10 <= occ <= -1. So we have to check [hist_ptr%MAX_HDR_HISTORY+occ] |
| 8838 | * to remain in the 0..9 range. |
Willy Tarreau | bce7088 | 2009-09-07 11:51:47 +0200 | [diff] [blame] | 8839 | */ |
Willy Tarreau | 67dad27 | 2013-06-12 22:27:44 +0200 | [diff] [blame] | 8840 | hist_ptr += occ + MAX_HDR_HISTORY; |
Willy Tarreau | bce7088 | 2009-09-07 11:51:47 +0200 | [diff] [blame] | 8841 | if (hist_ptr >= MAX_HDR_HISTORY) |
| 8842 | hist_ptr -= MAX_HDR_HISTORY; |
Willy Tarreau | 294c473 | 2011-12-16 21:35:50 +0100 | [diff] [blame] | 8843 | *vptr = ptr_hist[hist_ptr]; |
| 8844 | *vlen = len_hist[hist_ptr]; |
| 8845 | return 1; |
Willy Tarreau | bce7088 | 2009-09-07 11:51:47 +0200 | [diff] [blame] | 8846 | } |
| 8847 | |
Willy Tarreau | 04ff9f1 | 2013-06-10 18:39:42 +0200 | [diff] [blame] | 8848 | /* Return in <vptr> and <vlen> the pointer and length of occurrence <occ> of |
| 8849 | * header whose name is <hname> of length <hlen>. If <ctx> is null, lookup is |
| 8850 | * performed over the whole headers. Otherwise it must contain a valid header |
| 8851 | * context, initialised with ctx->idx=0 for the first lookup in a series. If |
| 8852 | * <occ> is positive or null, occurrence #occ from the beginning (or last ctx) |
| 8853 | * is returned. Occ #0 and #1 are equivalent. If <occ> is negative (and no less |
| 8854 | * than -MAX_HDR_HISTORY), the occurrence is counted from the last one which is |
| 8855 | * -1. This function differs from http_get_hdr() in that it only returns full |
| 8856 | * line header values and does not stop at commas. |
| 8857 | * The return value is 0 if nothing was found, or non-zero otherwise. |
| 8858 | */ |
| 8859 | unsigned int http_get_fhdr(const struct http_msg *msg, const char *hname, int hlen, |
| 8860 | struct hdr_idx *idx, int occ, |
| 8861 | struct hdr_ctx *ctx, char **vptr, int *vlen) |
| 8862 | { |
| 8863 | struct hdr_ctx local_ctx; |
| 8864 | char *ptr_hist[MAX_HDR_HISTORY]; |
| 8865 | int len_hist[MAX_HDR_HISTORY]; |
| 8866 | unsigned int hist_ptr; |
| 8867 | int found; |
| 8868 | |
| 8869 | if (!ctx) { |
| 8870 | local_ctx.idx = 0; |
| 8871 | ctx = &local_ctx; |
| 8872 | } |
| 8873 | |
| 8874 | if (occ >= 0) { |
| 8875 | /* search from the beginning */ |
| 8876 | while (http_find_full_header2(hname, hlen, msg->chn->buf->p, idx, ctx)) { |
| 8877 | occ--; |
| 8878 | if (occ <= 0) { |
| 8879 | *vptr = ctx->line + ctx->val; |
| 8880 | *vlen = ctx->vlen; |
| 8881 | return 1; |
| 8882 | } |
| 8883 | } |
| 8884 | return 0; |
| 8885 | } |
| 8886 | |
| 8887 | /* negative occurrence, we scan all the list then walk back */ |
| 8888 | if (-occ > MAX_HDR_HISTORY) |
| 8889 | return 0; |
| 8890 | |
| 8891 | found = hist_ptr = 0; |
| 8892 | while (http_find_full_header2(hname, hlen, msg->chn->buf->p, idx, ctx)) { |
| 8893 | ptr_hist[hist_ptr] = ctx->line + ctx->val; |
| 8894 | len_hist[hist_ptr] = ctx->vlen; |
| 8895 | if (++hist_ptr >= MAX_HDR_HISTORY) |
| 8896 | hist_ptr = 0; |
| 8897 | found++; |
| 8898 | } |
| 8899 | if (-occ > found) |
| 8900 | return 0; |
| 8901 | /* OK now we have the last occurrence in [hist_ptr-1], and we need to |
| 8902 | * find occurrence -occ, so we have to check [hist_ptr+occ]. |
| 8903 | */ |
| 8904 | hist_ptr += occ; |
| 8905 | if (hist_ptr >= MAX_HDR_HISTORY) |
| 8906 | hist_ptr -= MAX_HDR_HISTORY; |
| 8907 | *vptr = ptr_hist[hist_ptr]; |
| 8908 | *vlen = len_hist[hist_ptr]; |
| 8909 | return 1; |
| 8910 | } |
| 8911 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 8912 | /* |
Willy Tarreau | e92693a | 2012-09-24 21:13:39 +0200 | [diff] [blame] | 8913 | * Print a debug line with a header. Always stop at the first CR or LF char, |
| 8914 | * so it is safe to pass it a full buffer if needed. If <err> is not NULL, an |
| 8915 | * arrow is printed after the line which contains the pointer. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 8916 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 8917 | 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] | 8918 | { |
Willy Tarreau | 9ad7bd4 | 2015-04-03 19:19:59 +0200 | [diff] [blame] | 8919 | struct session *sess = strm_sess(s); |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 8920 | int max; |
Willy Tarreau | 9ad7bd4 | 2015-04-03 19:19:59 +0200 | [diff] [blame] | 8921 | |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 8922 | 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] | 8923 | dir, |
Willy Tarreau | 9ad7bd4 | 2015-04-03 19:19:59 +0200 | [diff] [blame] | 8924 | 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] | 8925 | 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] | 8926 | |
| 8927 | for (max = 0; start + max < end; max++) |
| 8928 | if (start[max] == '\r' || start[max] == '\n') |
| 8929 | break; |
| 8930 | |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 8931 | UBOUND(max, trash.size - trash.len - 3); |
| 8932 | trash.len += strlcpy2(trash.str + trash.len, start, max + 1); |
| 8933 | trash.str[trash.len++] = '\n'; |
Willy Tarreau | 89efaed | 2013-12-13 15:14:55 +0100 | [diff] [blame] | 8934 | shut_your_big_mouth_gcc(write(1, trash.str, trash.len)); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 8935 | } |
| 8936 | |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 8937 | |
| 8938 | /* Allocate a new HTTP transaction for stream <s> unless there is one already. |
| 8939 | * The hdr_idx is allocated as well. In case of allocation failure, everything |
| 8940 | * allocated is freed and NULL is returned. Otherwise the new transaction is |
| 8941 | * assigned to the stream and returned. |
| 8942 | */ |
| 8943 | struct http_txn *http_alloc_txn(struct stream *s) |
| 8944 | { |
| 8945 | struct http_txn *txn = s->txn; |
| 8946 | |
| 8947 | if (txn) |
| 8948 | return txn; |
| 8949 | |
| 8950 | txn = pool_alloc2(pool2_http_txn); |
| 8951 | if (!txn) |
| 8952 | return txn; |
| 8953 | |
| 8954 | txn->hdr_idx.size = global.tune.max_http_hdr; |
| 8955 | txn->hdr_idx.v = pool_alloc2(pool2_hdr_idx); |
| 8956 | if (!txn->hdr_idx.v) { |
| 8957 | pool_free2(pool2_http_txn, txn); |
| 8958 | return NULL; |
| 8959 | } |
| 8960 | |
| 8961 | s->txn = txn; |
| 8962 | return txn; |
| 8963 | } |
| 8964 | |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 8965 | /* |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 8966 | * 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] | 8967 | * the required fields are properly allocated and that we only need to (re)init |
| 8968 | * them. This should be used before processing any new request. |
| 8969 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 8970 | void http_init_txn(struct stream *s) |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 8971 | { |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 8972 | struct http_txn *txn = s->txn; |
Willy Tarreau | d0d8da9 | 2015-04-04 02:10:38 +0200 | [diff] [blame] | 8973 | struct proxy *fe = strm_fe(s); |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 8974 | |
| 8975 | txn->flags = 0; |
| 8976 | txn->status = -1; |
| 8977 | |
Willy Tarreau | f64d141 | 2010-10-07 20:06:11 +0200 | [diff] [blame] | 8978 | txn->cookie_first_date = 0; |
| 8979 | txn->cookie_last_date = 0; |
| 8980 | |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 8981 | txn->sessid = NULL; |
| 8982 | txn->srv_cookie = NULL; |
| 8983 | txn->cli_cookie = NULL; |
| 8984 | txn->uri = NULL; |
| 8985 | |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 8986 | txn->req.flags = 0; |
Willy Tarreau | 2692736 | 2012-05-18 23:22:52 +0200 | [diff] [blame] | 8987 | txn->req.sol = txn->req.eol = txn->req.eoh = 0; /* relative to the buffer */ |
Willy Tarreau | a458b67 | 2012-03-05 11:17:50 +0100 | [diff] [blame] | 8988 | txn->req.next = 0; |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 8989 | txn->rsp.flags = 0; |
Willy Tarreau | 2692736 | 2012-05-18 23:22:52 +0200 | [diff] [blame] | 8990 | txn->rsp.sol = txn->rsp.eol = txn->rsp.eoh = 0; /* relative to the buffer */ |
Willy Tarreau | a458b67 | 2012-03-05 11:17:50 +0100 | [diff] [blame] | 8991 | txn->rsp.next = 0; |
Willy Tarreau | 124d991 | 2011-03-01 20:30:48 +0100 | [diff] [blame] | 8992 | txn->req.chunk_len = 0LL; |
| 8993 | txn->req.body_len = 0LL; |
| 8994 | txn->rsp.chunk_len = 0LL; |
| 8995 | txn->rsp.body_len = 0LL; |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 8996 | txn->req.msg_state = HTTP_MSG_RQBEFORE; /* at the very beginning of the request */ |
| 8997 | txn->rsp.msg_state = HTTP_MSG_RPBEFORE; /* at the very beginning of the response */ |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 8998 | txn->req.chn = &s->req; |
| 8999 | txn->rsp.chn = &s->res; |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 9000 | |
| 9001 | txn->auth.method = HTTP_AUTH_UNKNOWN; |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 9002 | |
| 9003 | txn->req.err_pos = txn->rsp.err_pos = -2; /* block buggy requests/responses */ |
| 9004 | if (fe->options2 & PR_O2_REQBUG_OK) |
| 9005 | txn->req.err_pos = -1; /* let buggy requests pass */ |
| 9006 | |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 9007 | if (txn->hdr_idx.v) |
| 9008 | hdr_idx_init(&txn->hdr_idx); |
| 9009 | } |
| 9010 | |
| 9011 | /* to be used at the end of a transaction */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 9012 | void http_end_txn(struct stream *s) |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 9013 | { |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 9014 | struct http_txn *txn = s->txn; |
Willy Tarreau | d0d8da9 | 2015-04-04 02:10:38 +0200 | [diff] [blame] | 9015 | struct proxy *fe = strm_fe(s); |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 9016 | |
Willy Tarreau | 7519560 | 2014-03-11 15:48:55 +0100 | [diff] [blame] | 9017 | /* release any possible compression context */ |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 9018 | if (s->flags & SF_COMP_READY) |
Willy Tarreau | 7519560 | 2014-03-11 15:48:55 +0100 | [diff] [blame] | 9019 | s->comp_algo->end(&s->comp_ctx); |
| 9020 | s->comp_algo = NULL; |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 9021 | s->flags &= ~SF_COMP_READY; |
Willy Tarreau | 7519560 | 2014-03-11 15:48:55 +0100 | [diff] [blame] | 9022 | |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 9023 | /* these ones will have been dynamically allocated */ |
| 9024 | pool_free2(pool2_requri, txn->uri); |
| 9025 | pool_free2(pool2_capture, txn->cli_cookie); |
| 9026 | pool_free2(pool2_capture, txn->srv_cookie); |
Willy Tarreau | a3377ee | 2010-01-10 10:49:11 +0100 | [diff] [blame] | 9027 | pool_free2(apools.sessid, txn->sessid); |
William Lallemand | a73203e | 2012-03-12 12:48:57 +0100 | [diff] [blame] | 9028 | pool_free2(pool2_uniqueid, s->unique_id); |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 9029 | |
William Lallemand | a73203e | 2012-03-12 12:48:57 +0100 | [diff] [blame] | 9030 | s->unique_id = NULL; |
Willy Tarreau | a3377ee | 2010-01-10 10:49:11 +0100 | [diff] [blame] | 9031 | txn->sessid = NULL; |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 9032 | txn->uri = NULL; |
| 9033 | txn->srv_cookie = NULL; |
| 9034 | txn->cli_cookie = NULL; |
Willy Tarreau | 4602363 | 2010-01-07 22:51:47 +0100 | [diff] [blame] | 9035 | |
Willy Tarreau | cb7dd01 | 2015-04-03 22:16:32 +0200 | [diff] [blame] | 9036 | if (s->req_cap) { |
Willy Tarreau | 4602363 | 2010-01-07 22:51:47 +0100 | [diff] [blame] | 9037 | struct cap_hdr *h; |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 9038 | for (h = fe->req_cap; h; h = h->next) |
Willy Tarreau | cb7dd01 | 2015-04-03 22:16:32 +0200 | [diff] [blame] | 9039 | pool_free2(h->pool, s->req_cap[h->index]); |
| 9040 | memset(s->req_cap, 0, fe->nb_req_cap * sizeof(void *)); |
Willy Tarreau | 4602363 | 2010-01-07 22:51:47 +0100 | [diff] [blame] | 9041 | } |
| 9042 | |
Willy Tarreau | cb7dd01 | 2015-04-03 22:16:32 +0200 | [diff] [blame] | 9043 | if (s->res_cap) { |
Willy Tarreau | 4602363 | 2010-01-07 22:51:47 +0100 | [diff] [blame] | 9044 | struct cap_hdr *h; |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 9045 | for (h = fe->rsp_cap; h; h = h->next) |
Willy Tarreau | cb7dd01 | 2015-04-03 22:16:32 +0200 | [diff] [blame] | 9046 | pool_free2(h->pool, s->res_cap[h->index]); |
| 9047 | memset(s->res_cap, 0, fe->nb_rsp_cap * sizeof(void *)); |
Willy Tarreau | 4602363 | 2010-01-07 22:51:47 +0100 | [diff] [blame] | 9048 | } |
| 9049 | |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 9050 | } |
| 9051 | |
| 9052 | /* 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] | 9053 | void http_reset_txn(struct stream *s) |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 9054 | { |
| 9055 | http_end_txn(s); |
| 9056 | http_init_txn(s); |
| 9057 | |
Thierry FOURNIER | bc4c1ac | 2015-02-25 13:36:14 +0100 | [diff] [blame] | 9058 | /* reinitialise the current rule list pointer to NULL. We are sure that |
| 9059 | * any rulelist match the NULL pointer. |
| 9060 | */ |
| 9061 | s->current_rule_list = NULL; |
| 9062 | |
Willy Tarreau | d0d8da9 | 2015-04-04 02:10:38 +0200 | [diff] [blame] | 9063 | s->be = strm_fe(s); |
| 9064 | s->logs.logwait = strm_fe(s)->to_log; |
Willy Tarreau | abcd514 | 2013-06-11 17:18:02 +0200 | [diff] [blame] | 9065 | s->logs.level = 0; |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 9066 | stream_del_srv_conn(s); |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 9067 | s->target = NULL; |
Emeric Brun | b982a3d | 2010-01-04 15:45:53 +0100 | [diff] [blame] | 9068 | /* re-init store persistence */ |
| 9069 | s->store_count = 0; |
Willy Tarreau | 1f0da24 | 2014-01-25 11:01:50 +0100 | [diff] [blame] | 9070 | s->uniq_id = global.req_count++; |
Emeric Brun | b982a3d | 2010-01-04 15:45:53 +0100 | [diff] [blame] | 9071 | |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 9072 | s->pend_pos = NULL; |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 9073 | |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 9074 | s->req.flags |= CF_READ_DONTWAIT; /* one read is usually enough */ |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 9075 | |
Willy Tarreau | 739cfba | 2010-01-25 23:11:14 +0100 | [diff] [blame] | 9076 | /* We must trim any excess data from the response buffer, because we |
| 9077 | * may have blocked an invalid response from a server that we don't |
| 9078 | * want to accidentely forward once we disable the analysers, nor do |
| 9079 | * we want those data to come along with next response. A typical |
| 9080 | * example of such data would be from a buggy server responding to |
| 9081 | * a HEAD with some data, or sending more than the advertised |
| 9082 | * content-length. |
| 9083 | */ |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 9084 | if (unlikely(s->res.buf->i)) |
| 9085 | s->res.buf->i = 0; |
Willy Tarreau | 739cfba | 2010-01-25 23:11:14 +0100 | [diff] [blame] | 9086 | |
Willy Tarreau | d0d8da9 | 2015-04-04 02:10:38 +0200 | [diff] [blame] | 9087 | s->req.rto = strm_fe(s)->timeout.client; |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 9088 | s->req.wto = TICK_ETERNITY; |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 9089 | |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 9090 | s->res.rto = TICK_ETERNITY; |
Willy Tarreau | d0d8da9 | 2015-04-04 02:10:38 +0200 | [diff] [blame] | 9091 | s->res.wto = strm_fe(s)->timeout.client; |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 9092 | |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 9093 | s->req.rex = TICK_ETERNITY; |
| 9094 | s->req.wex = TICK_ETERNITY; |
| 9095 | s->req.analyse_exp = TICK_ETERNITY; |
| 9096 | s->res.rex = TICK_ETERNITY; |
| 9097 | s->res.wex = TICK_ETERNITY; |
| 9098 | s->res.analyse_exp = TICK_ETERNITY; |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 9099 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 9100 | |
Sasha Pachev | 218f064 | 2014-06-16 12:05:59 -0600 | [diff] [blame] | 9101 | void free_http_res_rules(struct list *r) |
| 9102 | { |
| 9103 | struct http_res_rule *tr, *pr; |
| 9104 | |
| 9105 | list_for_each_entry_safe(pr, tr, r, list) { |
| 9106 | LIST_DEL(&pr->list); |
Thierry FOURNIER | 09af0d6 | 2014-06-18 11:35:54 +0200 | [diff] [blame] | 9107 | regex_free(&pr->arg.hdr_add.re); |
Sasha Pachev | 218f064 | 2014-06-16 12:05:59 -0600 | [diff] [blame] | 9108 | free(pr); |
| 9109 | } |
| 9110 | } |
| 9111 | |
| 9112 | void free_http_req_rules(struct list *r) |
| 9113 | { |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 9114 | struct http_req_rule *tr, *pr; |
| 9115 | |
| 9116 | list_for_each_entry_safe(pr, tr, r, list) { |
| 9117 | LIST_DEL(&pr->list); |
Willy Tarreau | 20b0de5 | 2012-12-24 15:45:22 +0100 | [diff] [blame] | 9118 | if (pr->action == HTTP_REQ_ACT_AUTH) |
Willy Tarreau | 5c2e198 | 2012-12-24 12:00:25 +0100 | [diff] [blame] | 9119 | free(pr->arg.auth.realm); |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 9120 | |
Thierry FOURNIER | 09af0d6 | 2014-06-18 11:35:54 +0200 | [diff] [blame] | 9121 | regex_free(&pr->arg.hdr_add.re); |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 9122 | free(pr); |
| 9123 | } |
| 9124 | } |
| 9125 | |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 9126 | /* parse an "http-request" rule */ |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 9127 | struct http_req_rule *parse_http_req_cond(const char **args, const char *file, int linenum, struct proxy *proxy) |
| 9128 | { |
| 9129 | struct http_req_rule *rule; |
William Lallemand | 73025dd | 2014-04-24 14:38:37 +0200 | [diff] [blame] | 9130 | struct http_req_action_kw *custom = NULL; |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 9131 | int cur_arg; |
Thierry FOURNIER | 09af0d6 | 2014-06-18 11:35:54 +0200 | [diff] [blame] | 9132 | char *error; |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 9133 | |
| 9134 | rule = (struct http_req_rule*)calloc(1, sizeof(struct http_req_rule)); |
| 9135 | if (!rule) { |
| 9136 | Alert("parsing [%s:%d]: out of memory.\n", file, linenum); |
Willy Tarreau | 81499eb | 2012-12-27 12:19:02 +0100 | [diff] [blame] | 9137 | goto out_err; |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 9138 | } |
| 9139 | |
CJ Ess | 108b1dd | 2015-04-07 12:03:37 -0400 | [diff] [blame] | 9140 | rule->deny_status = HTTP_ERR_403; |
Willy Tarreau | 5c2e198 | 2012-12-24 12:00:25 +0100 | [diff] [blame] | 9141 | if (!strcmp(args[0], "allow")) { |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 9142 | rule->action = HTTP_REQ_ACT_ALLOW; |
| 9143 | cur_arg = 1; |
Willy Tarreau | 5bd6759 | 2014-04-28 22:00:46 +0200 | [diff] [blame] | 9144 | } else if (!strcmp(args[0], "deny") || !strcmp(args[0], "block")) { |
CJ Ess | 108b1dd | 2015-04-07 12:03:37 -0400 | [diff] [blame] | 9145 | int code; |
| 9146 | int hc; |
| 9147 | |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 9148 | rule->action = HTTP_REQ_ACT_DENY; |
| 9149 | cur_arg = 1; |
CJ Ess | 108b1dd | 2015-04-07 12:03:37 -0400 | [diff] [blame] | 9150 | if (strcmp(args[cur_arg], "deny_status") == 0) { |
| 9151 | cur_arg++; |
| 9152 | if (!args[cur_arg]) { |
| 9153 | Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : missing status code.\n", |
| 9154 | file, linenum, proxy_type_str(proxy), proxy->id, args[0]); |
| 9155 | goto out_err; |
| 9156 | } |
| 9157 | |
| 9158 | code = atol(args[cur_arg]); |
| 9159 | cur_arg++; |
| 9160 | for (hc = 0; hc < HTTP_ERR_SIZE; hc++) { |
| 9161 | if (http_err_codes[hc] == code) { |
| 9162 | rule->deny_status = hc; |
| 9163 | break; |
| 9164 | } |
| 9165 | } |
| 9166 | |
| 9167 | if (hc >= HTTP_ERR_SIZE) { |
| 9168 | Warning("parsing [%s:%d] : status code %d not handled, using default code 403.\n", |
| 9169 | file, linenum, code); |
| 9170 | } |
| 9171 | } |
Willy Tarreau | ccbcc37 | 2012-12-27 12:37:57 +0100 | [diff] [blame] | 9172 | } else if (!strcmp(args[0], "tarpit")) { |
| 9173 | rule->action = HTTP_REQ_ACT_TARPIT; |
| 9174 | cur_arg = 1; |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 9175 | } else if (!strcmp(args[0], "auth")) { |
Willy Tarreau | 20b0de5 | 2012-12-24 15:45:22 +0100 | [diff] [blame] | 9176 | rule->action = HTTP_REQ_ACT_AUTH; |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 9177 | cur_arg = 1; |
| 9178 | |
| 9179 | while(*args[cur_arg]) { |
| 9180 | if (!strcmp(args[cur_arg], "realm")) { |
Willy Tarreau | 5c2e198 | 2012-12-24 12:00:25 +0100 | [diff] [blame] | 9181 | rule->arg.auth.realm = strdup(args[cur_arg + 1]); |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 9182 | cur_arg+=2; |
| 9183 | continue; |
| 9184 | } else |
| 9185 | break; |
| 9186 | } |
Willy Tarreau | f4c43c1 | 2013-06-11 17:01:13 +0200 | [diff] [blame] | 9187 | } else if (!strcmp(args[0], "set-nice")) { |
| 9188 | rule->action = HTTP_REQ_ACT_SET_NICE; |
| 9189 | cur_arg = 1; |
| 9190 | |
| 9191 | if (!*args[cur_arg] || |
| 9192 | (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) { |
| 9193 | Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (integer value).\n", |
| 9194 | file, linenum, args[0]); |
| 9195 | goto out_err; |
| 9196 | } |
| 9197 | rule->arg.nice = atoi(args[cur_arg]); |
| 9198 | if (rule->arg.nice < -1024) |
| 9199 | rule->arg.nice = -1024; |
| 9200 | else if (rule->arg.nice > 1024) |
| 9201 | rule->arg.nice = 1024; |
| 9202 | cur_arg++; |
Willy Tarreau | 42cf39e | 2013-06-11 18:51:32 +0200 | [diff] [blame] | 9203 | } else if (!strcmp(args[0], "set-tos")) { |
| 9204 | #ifdef IP_TOS |
| 9205 | char *err; |
| 9206 | rule->action = HTTP_REQ_ACT_SET_TOS; |
| 9207 | cur_arg = 1; |
| 9208 | |
| 9209 | if (!*args[cur_arg] || |
| 9210 | (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) { |
| 9211 | Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (integer/hex value).\n", |
| 9212 | file, linenum, args[0]); |
| 9213 | goto out_err; |
| 9214 | } |
| 9215 | |
| 9216 | rule->arg.tos = strtol(args[cur_arg], &err, 0); |
| 9217 | if (err && *err != '\0') { |
| 9218 | Alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-request %s' (integer/hex value expected).\n", |
| 9219 | file, linenum, err, args[0]); |
| 9220 | goto out_err; |
| 9221 | } |
| 9222 | cur_arg++; |
| 9223 | #else |
| 9224 | Alert("parsing [%s:%d]: 'http-request %s' is not supported on this platform (IP_TOS undefined).\n", file, linenum, args[0]); |
| 9225 | goto out_err; |
| 9226 | #endif |
Willy Tarreau | 51347ed | 2013-06-11 19:34:13 +0200 | [diff] [blame] | 9227 | } else if (!strcmp(args[0], "set-mark")) { |
| 9228 | #ifdef SO_MARK |
| 9229 | char *err; |
| 9230 | rule->action = HTTP_REQ_ACT_SET_MARK; |
| 9231 | cur_arg = 1; |
| 9232 | |
| 9233 | if (!*args[cur_arg] || |
| 9234 | (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) { |
| 9235 | Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (integer/hex value).\n", |
| 9236 | file, linenum, args[0]); |
| 9237 | goto out_err; |
| 9238 | } |
| 9239 | |
| 9240 | rule->arg.mark = strtoul(args[cur_arg], &err, 0); |
| 9241 | if (err && *err != '\0') { |
| 9242 | Alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-request %s' (integer/hex value expected).\n", |
| 9243 | file, linenum, err, args[0]); |
| 9244 | goto out_err; |
| 9245 | } |
| 9246 | cur_arg++; |
| 9247 | global.last_checks |= LSTCHK_NETADM; |
| 9248 | #else |
| 9249 | Alert("parsing [%s:%d]: 'http-request %s' is not supported on this platform (SO_MARK undefined).\n", file, linenum, args[0]); |
| 9250 | goto out_err; |
| 9251 | #endif |
Willy Tarreau | 9a355ec | 2013-06-11 17:45:46 +0200 | [diff] [blame] | 9252 | } else if (!strcmp(args[0], "set-log-level")) { |
| 9253 | rule->action = HTTP_REQ_ACT_SET_LOGL; |
| 9254 | cur_arg = 1; |
| 9255 | |
| 9256 | if (!*args[cur_arg] || |
| 9257 | (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) { |
| 9258 | bad_log_level: |
| 9259 | Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (log level name or 'silent').\n", |
| 9260 | file, linenum, args[0]); |
| 9261 | goto out_err; |
| 9262 | } |
| 9263 | if (strcmp(args[cur_arg], "silent") == 0) |
| 9264 | rule->arg.loglevel = -1; |
| 9265 | else if ((rule->arg.loglevel = get_log_level(args[cur_arg]) + 1) == 0) |
| 9266 | goto bad_log_level; |
| 9267 | cur_arg++; |
Willy Tarreau | 20b0de5 | 2012-12-24 15:45:22 +0100 | [diff] [blame] | 9268 | } else if (strcmp(args[0], "add-header") == 0 || strcmp(args[0], "set-header") == 0) { |
| 9269 | rule->action = *args[0] == 'a' ? HTTP_REQ_ACT_ADD_HDR : HTTP_REQ_ACT_SET_HDR; |
| 9270 | cur_arg = 1; |
| 9271 | |
Willy Tarreau | 8d1c516 | 2013-04-03 14:13:58 +0200 | [diff] [blame] | 9272 | if (!*args[cur_arg] || !*args[cur_arg+1] || |
| 9273 | (*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] | 9274 | Alert("parsing [%s:%d]: 'http-request %s' expects exactly 2 arguments.\n", |
| 9275 | file, linenum, args[0]); |
Willy Tarreau | 81499eb | 2012-12-27 12:19:02 +0100 | [diff] [blame] | 9276 | goto out_err; |
Willy Tarreau | 20b0de5 | 2012-12-24 15:45:22 +0100 | [diff] [blame] | 9277 | } |
| 9278 | |
| 9279 | rule->arg.hdr_add.name = strdup(args[cur_arg]); |
| 9280 | rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name); |
| 9281 | LIST_INIT(&rule->arg.hdr_add.fmt); |
Willy Tarreau | a4312fa | 2013-04-02 16:34:32 +0200 | [diff] [blame] | 9282 | |
Thierry FOURNIER | 1c0054f | 2013-11-20 15:09:52 +0100 | [diff] [blame] | 9283 | proxy->conf.args.ctx = ARGC_HRQ; |
Thierry FOURNIER | d048d8b | 2014-03-13 16:46:18 +0100 | [diff] [blame] | 9284 | parse_logformat_string(args[cur_arg + 1], proxy, &rule->arg.hdr_add.fmt, LOG_OPT_HTTP, |
Thierry FOURNIER | eeaa951 | 2014-02-11 14:00:19 +0100 | [diff] [blame] | 9285 | (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR, |
| 9286 | file, linenum); |
Willy Tarreau | 59ad1a2 | 2014-01-29 14:39:58 +0100 | [diff] [blame] | 9287 | free(proxy->conf.lfs_file); |
| 9288 | proxy->conf.lfs_file = strdup(proxy->conf.args.file); |
| 9289 | proxy->conf.lfs_line = proxy->conf.args.line; |
Willy Tarreau | 20b0de5 | 2012-12-24 15:45:22 +0100 | [diff] [blame] | 9290 | cur_arg += 2; |
Willy Tarreau | b854392 | 2014-06-17 18:58:26 +0200 | [diff] [blame] | 9291 | } else if (strcmp(args[0], "replace-header") == 0 || strcmp(args[0], "replace-value") == 0) { |
| 9292 | rule->action = args[0][8] == 'h' ? HTTP_REQ_ACT_REPLACE_HDR : HTTP_REQ_ACT_REPLACE_VAL; |
Sasha Pachev | 218f064 | 2014-06-16 12:05:59 -0600 | [diff] [blame] | 9293 | cur_arg = 1; |
| 9294 | |
| 9295 | if (!*args[cur_arg] || !*args[cur_arg+1] || !*args[cur_arg+2] || |
Baptiste Assmann | 92df370 | 2014-06-24 11:10:00 +0200 | [diff] [blame] | 9296 | (*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] | 9297 | Alert("parsing [%s:%d]: 'http-request %s' expects exactly 3 arguments.\n", |
| 9298 | file, linenum, args[0]); |
| 9299 | goto out_err; |
| 9300 | } |
| 9301 | |
| 9302 | rule->arg.hdr_add.name = strdup(args[cur_arg]); |
| 9303 | rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name); |
| 9304 | LIST_INIT(&rule->arg.hdr_add.fmt); |
| 9305 | |
Thierry FOURNIER | 09af0d6 | 2014-06-18 11:35:54 +0200 | [diff] [blame] | 9306 | error = NULL; |
| 9307 | if (!regex_comp(args[cur_arg + 1], &rule->arg.hdr_add.re, 1, 1, &error)) { |
| 9308 | Alert("parsing [%s:%d] : '%s' : %s.\n", file, linenum, |
| 9309 | args[cur_arg + 1], error); |
| 9310 | free(error); |
Sasha Pachev | 218f064 | 2014-06-16 12:05:59 -0600 | [diff] [blame] | 9311 | goto out_err; |
| 9312 | } |
| 9313 | |
| 9314 | proxy->conf.args.ctx = ARGC_HRQ; |
| 9315 | parse_logformat_string(args[cur_arg + 2], proxy, &rule->arg.hdr_add.fmt, LOG_OPT_HTTP, |
| 9316 | (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR, |
| 9317 | file, linenum); |
| 9318 | |
| 9319 | free(proxy->conf.lfs_file); |
| 9320 | proxy->conf.lfs_file = strdup(proxy->conf.args.file); |
| 9321 | proxy->conf.lfs_line = proxy->conf.args.line; |
| 9322 | cur_arg += 3; |
Thierry FOURNIER | dad3d1d | 2014-04-22 18:07:25 +0200 | [diff] [blame] | 9323 | } else if (strcmp(args[0], "del-header") == 0) { |
| 9324 | rule->action = HTTP_REQ_ACT_DEL_HDR; |
| 9325 | cur_arg = 1; |
| 9326 | |
| 9327 | if (!*args[cur_arg] || |
| 9328 | (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) { |
| 9329 | Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument.\n", |
| 9330 | file, linenum, args[0]); |
| 9331 | goto out_err; |
| 9332 | } |
| 9333 | |
| 9334 | rule->arg.hdr_add.name = strdup(args[cur_arg]); |
| 9335 | rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name); |
| 9336 | |
| 9337 | proxy->conf.args.ctx = ARGC_HRQ; |
| 9338 | free(proxy->conf.lfs_file); |
| 9339 | proxy->conf.lfs_file = strdup(proxy->conf.args.file); |
| 9340 | proxy->conf.lfs_line = proxy->conf.args.line; |
| 9341 | cur_arg += 1; |
Willy Tarreau | 09448f7 | 2014-06-25 18:12:15 +0200 | [diff] [blame] | 9342 | } else if (strncmp(args[0], "track-sc", 8) == 0 && |
| 9343 | args[0][9] == '\0' && args[0][8] >= '0' && |
Willy Tarreau | e1cfc1f | 2014-10-17 11:53:05 +0200 | [diff] [blame] | 9344 | args[0][8] < '0' + MAX_SESS_STKCTR) { /* track-sc 0..9 */ |
Willy Tarreau | 09448f7 | 2014-06-25 18:12:15 +0200 | [diff] [blame] | 9345 | struct sample_expr *expr; |
| 9346 | unsigned int where; |
| 9347 | char *err = NULL; |
| 9348 | |
| 9349 | cur_arg = 1; |
| 9350 | proxy->conf.args.ctx = ARGC_TRK; |
| 9351 | |
| 9352 | expr = sample_parse_expr((char **)args, &cur_arg, file, linenum, &err, &proxy->conf.args); |
| 9353 | if (!expr) { |
| 9354 | Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : %s.\n", |
| 9355 | file, linenum, proxy_type_str(proxy), proxy->id, args[0], err); |
| 9356 | free(err); |
| 9357 | goto out_err; |
| 9358 | } |
| 9359 | |
| 9360 | where = 0; |
| 9361 | if (proxy->cap & PR_CAP_FE) |
| 9362 | where |= SMP_VAL_FE_HRQ_HDR; |
| 9363 | if (proxy->cap & PR_CAP_BE) |
| 9364 | where |= SMP_VAL_BE_HRQ_HDR; |
| 9365 | |
| 9366 | if (!(expr->fetch->val & where)) { |
| 9367 | Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule :" |
| 9368 | " fetch method '%s' extracts information from '%s', none of which is available here.\n", |
| 9369 | file, linenum, proxy_type_str(proxy), proxy->id, args[0], |
| 9370 | args[cur_arg-1], sample_src_names(expr->fetch->use)); |
| 9371 | free(expr); |
| 9372 | goto out_err; |
| 9373 | } |
| 9374 | |
| 9375 | if (strcmp(args[cur_arg], "table") == 0) { |
| 9376 | cur_arg++; |
| 9377 | if (!args[cur_arg]) { |
| 9378 | Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : missing table name.\n", |
| 9379 | file, linenum, proxy_type_str(proxy), proxy->id, args[0]); |
| 9380 | free(expr); |
| 9381 | goto out_err; |
| 9382 | } |
| 9383 | /* we copy the table name for now, it will be resolved later */ |
| 9384 | rule->act_prm.trk_ctr.table.n = strdup(args[cur_arg]); |
| 9385 | cur_arg++; |
| 9386 | } |
| 9387 | rule->act_prm.trk_ctr.expr = expr; |
| 9388 | rule->action = HTTP_REQ_ACT_TRK_SC0 + args[0][8] - '0'; |
Willy Tarreau | 81499eb | 2012-12-27 12:19:02 +0100 | [diff] [blame] | 9389 | } else if (strcmp(args[0], "redirect") == 0) { |
| 9390 | struct redirect_rule *redir; |
Willy Tarreau | 6d4890c | 2013-11-18 18:04:25 +0100 | [diff] [blame] | 9391 | char *errmsg = NULL; |
Willy Tarreau | 81499eb | 2012-12-27 12:19:02 +0100 | [diff] [blame] | 9392 | |
Willy Tarreau | be4653b | 2015-05-28 15:26:58 +0200 | [diff] [blame] | 9393 | 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] | 9394 | Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : %s.\n", |
| 9395 | file, linenum, proxy_type_str(proxy), proxy->id, args[0], errmsg); |
| 9396 | goto out_err; |
| 9397 | } |
| 9398 | |
| 9399 | /* this redirect rule might already contain a parsed condition which |
| 9400 | * we'll pass to the http-request rule. |
| 9401 | */ |
| 9402 | rule->action = HTTP_REQ_ACT_REDIR; |
| 9403 | rule->arg.redir = redir; |
| 9404 | rule->cond = redir->cond; |
| 9405 | redir->cond = NULL; |
| 9406 | cur_arg = 2; |
| 9407 | return rule; |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 9408 | } else if (strncmp(args[0], "add-acl", 7) == 0) { |
| 9409 | /* http-request add-acl(<reference (acl name)>) <key pattern> */ |
| 9410 | rule->action = HTTP_REQ_ACT_ADD_ACL; |
| 9411 | /* |
| 9412 | * '+ 8' for 'add-acl(' |
| 9413 | * '- 9' for 'add-acl(' + trailing ')' |
| 9414 | */ |
Willy Tarreau | 6c09c2c | 2014-04-25 21:38:08 +0200 | [diff] [blame] | 9415 | 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] | 9416 | |
| 9417 | cur_arg = 1; |
| 9418 | |
| 9419 | if (!*args[cur_arg] || |
| 9420 | (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) { |
| 9421 | Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument.\n", |
| 9422 | file, linenum, args[0]); |
| 9423 | goto out_err; |
| 9424 | } |
| 9425 | |
| 9426 | LIST_INIT(&rule->arg.map.key); |
| 9427 | proxy->conf.args.ctx = ARGC_HRQ; |
| 9428 | parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP, |
| 9429 | (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR, |
| 9430 | file, linenum); |
| 9431 | free(proxy->conf.lfs_file); |
| 9432 | proxy->conf.lfs_file = strdup(proxy->conf.args.file); |
| 9433 | proxy->conf.lfs_line = proxy->conf.args.line; |
| 9434 | cur_arg += 1; |
| 9435 | } else if (strncmp(args[0], "del-acl", 7) == 0) { |
| 9436 | /* http-request del-acl(<reference (acl name)>) <key pattern> */ |
| 9437 | rule->action = HTTP_REQ_ACT_DEL_ACL; |
| 9438 | /* |
| 9439 | * '+ 8' for 'del-acl(' |
| 9440 | * '- 9' for 'del-acl(' + trailing ')' |
| 9441 | */ |
Willy Tarreau | 6c09c2c | 2014-04-25 21:38:08 +0200 | [diff] [blame] | 9442 | 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] | 9443 | |
| 9444 | cur_arg = 1; |
| 9445 | |
| 9446 | if (!*args[cur_arg] || |
| 9447 | (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) { |
| 9448 | Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument.\n", |
| 9449 | file, linenum, args[0]); |
| 9450 | goto out_err; |
| 9451 | } |
| 9452 | |
| 9453 | LIST_INIT(&rule->arg.map.key); |
| 9454 | proxy->conf.args.ctx = ARGC_HRQ; |
| 9455 | parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP, |
| 9456 | (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR, |
| 9457 | file, linenum); |
| 9458 | free(proxy->conf.lfs_file); |
| 9459 | proxy->conf.lfs_file = strdup(proxy->conf.args.file); |
| 9460 | proxy->conf.lfs_line = proxy->conf.args.line; |
| 9461 | cur_arg += 1; |
| 9462 | } else if (strncmp(args[0], "del-map", 7) == 0) { |
| 9463 | /* http-request del-map(<reference (map name)>) <key pattern> */ |
| 9464 | rule->action = HTTP_REQ_ACT_DEL_MAP; |
| 9465 | /* |
| 9466 | * '+ 8' for 'del-map(' |
| 9467 | * '- 9' for 'del-map(' + trailing ')' |
| 9468 | */ |
Willy Tarreau | 6c09c2c | 2014-04-25 21:38:08 +0200 | [diff] [blame] | 9469 | 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] | 9470 | |
| 9471 | cur_arg = 1; |
| 9472 | |
| 9473 | if (!*args[cur_arg] || |
| 9474 | (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) { |
| 9475 | Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument.\n", |
| 9476 | file, linenum, args[0]); |
| 9477 | goto out_err; |
| 9478 | } |
| 9479 | |
| 9480 | LIST_INIT(&rule->arg.map.key); |
| 9481 | proxy->conf.args.ctx = ARGC_HRQ; |
| 9482 | parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP, |
| 9483 | (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR, |
| 9484 | file, linenum); |
| 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 | cur_arg += 1; |
| 9489 | } else if (strncmp(args[0], "set-map", 7) == 0) { |
| 9490 | /* http-request set-map(<reference (map name)>) <key pattern> <value pattern> */ |
| 9491 | rule->action = HTTP_REQ_ACT_SET_MAP; |
| 9492 | /* |
| 9493 | * '+ 8' for 'set-map(' |
| 9494 | * '- 9' for 'set-map(' + trailing ')' |
| 9495 | */ |
Willy Tarreau | 6c09c2c | 2014-04-25 21:38:08 +0200 | [diff] [blame] | 9496 | 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] | 9497 | |
| 9498 | cur_arg = 1; |
| 9499 | |
| 9500 | if (!*args[cur_arg] || !*args[cur_arg+1] || |
| 9501 | (*args[cur_arg+2] && strcmp(args[cur_arg+2], "if") != 0 && strcmp(args[cur_arg+2], "unless") != 0)) { |
| 9502 | Alert("parsing [%s:%d]: 'http-request %s' expects exactly 2 arguments.\n", |
| 9503 | file, linenum, args[0]); |
| 9504 | goto out_err; |
| 9505 | } |
| 9506 | |
| 9507 | LIST_INIT(&rule->arg.map.key); |
| 9508 | LIST_INIT(&rule->arg.map.value); |
| 9509 | proxy->conf.args.ctx = ARGC_HRQ; |
| 9510 | |
| 9511 | /* key pattern */ |
| 9512 | parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP, |
| 9513 | (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR, |
| 9514 | file, linenum); |
| 9515 | |
| 9516 | /* value pattern */ |
| 9517 | parse_logformat_string(args[cur_arg + 1], proxy, &rule->arg.map.value, LOG_OPT_HTTP, |
| 9518 | (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR, |
| 9519 | file, linenum); |
| 9520 | free(proxy->conf.lfs_file); |
| 9521 | proxy->conf.lfs_file = strdup(proxy->conf.args.file); |
| 9522 | proxy->conf.lfs_line = proxy->conf.args.line; |
| 9523 | |
| 9524 | cur_arg += 2; |
William Lallemand | 73025dd | 2014-04-24 14:38:37 +0200 | [diff] [blame] | 9525 | } else if (((custom = action_http_req_custom(args[0])) != NULL)) { |
| 9526 | char *errmsg = NULL; |
| 9527 | cur_arg = 1; |
| 9528 | /* try in the module list */ |
| 9529 | if (custom->parse(args, &cur_arg, proxy, rule, &errmsg) < 0) { |
| 9530 | Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : %s.\n", |
| 9531 | file, linenum, proxy_type_str(proxy), proxy->id, args[0], errmsg); |
| 9532 | free(errmsg); |
| 9533 | goto out_err; |
| 9534 | } |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 9535 | } else { |
Sasha Pachev | 218f064 | 2014-06-16 12:05:59 -0600 | [diff] [blame] | 9536 | Alert("parsing [%s:%d]: 'http-request' expects 'allow', 'deny', 'auth', 'redirect', 'tarpit', 'add-header', 'set-header', 'replace-header', 'replace-value', 'set-nice', 'set-tos', 'set-mark', 'set-log-level', 'add-acl', 'del-acl', 'del-map', 'set-map', but got '%s'%s.\n", |
Willy Tarreau | 5c2e198 | 2012-12-24 12:00:25 +0100 | [diff] [blame] | 9537 | file, linenum, args[0], *args[0] ? "" : " (missing argument)"); |
Willy Tarreau | 81499eb | 2012-12-27 12:19:02 +0100 | [diff] [blame] | 9538 | goto out_err; |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 9539 | } |
| 9540 | |
| 9541 | if (strcmp(args[cur_arg], "if") == 0 || strcmp(args[cur_arg], "unless") == 0) { |
| 9542 | struct acl_cond *cond; |
Willy Tarreau | b7451bb | 2012-04-27 12:38:15 +0200 | [diff] [blame] | 9543 | char *errmsg = NULL; |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 9544 | |
Willy Tarreau | b7451bb | 2012-04-27 12:38:15 +0200 | [diff] [blame] | 9545 | if ((cond = build_acl_cond(file, linenum, proxy, args+cur_arg, &errmsg)) == NULL) { |
| 9546 | Alert("parsing [%s:%d] : error detected while parsing an 'http-request %s' condition : %s.\n", |
| 9547 | file, linenum, args[0], errmsg); |
| 9548 | free(errmsg); |
Willy Tarreau | 81499eb | 2012-12-27 12:19:02 +0100 | [diff] [blame] | 9549 | goto out_err; |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 9550 | } |
| 9551 | rule->cond = cond; |
| 9552 | } |
| 9553 | else if (*args[cur_arg]) { |
| 9554 | Alert("parsing [%s:%d]: 'http-request %s' expects 'realm' for 'auth' or" |
| 9555 | " either 'if' or 'unless' followed by a condition but found '%s'.\n", |
| 9556 | file, linenum, args[0], args[cur_arg]); |
Willy Tarreau | 81499eb | 2012-12-27 12:19:02 +0100 | [diff] [blame] | 9557 | goto out_err; |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 9558 | } |
| 9559 | |
| 9560 | return rule; |
Willy Tarreau | 81499eb | 2012-12-27 12:19:02 +0100 | [diff] [blame] | 9561 | out_err: |
| 9562 | free(rule); |
| 9563 | return NULL; |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 9564 | } |
| 9565 | |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 9566 | /* parse an "http-respose" rule */ |
| 9567 | struct http_res_rule *parse_http_res_cond(const char **args, const char *file, int linenum, struct proxy *proxy) |
| 9568 | { |
| 9569 | struct http_res_rule *rule; |
William Lallemand | 73025dd | 2014-04-24 14:38:37 +0200 | [diff] [blame] | 9570 | struct http_res_action_kw *custom = NULL; |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 9571 | int cur_arg; |
Thierry FOURNIER | 09af0d6 | 2014-06-18 11:35:54 +0200 | [diff] [blame] | 9572 | char *error; |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 9573 | |
| 9574 | rule = calloc(1, sizeof(*rule)); |
| 9575 | if (!rule) { |
| 9576 | Alert("parsing [%s:%d]: out of memory.\n", file, linenum); |
| 9577 | goto out_err; |
| 9578 | } |
| 9579 | |
| 9580 | if (!strcmp(args[0], "allow")) { |
| 9581 | rule->action = HTTP_RES_ACT_ALLOW; |
| 9582 | cur_arg = 1; |
| 9583 | } else if (!strcmp(args[0], "deny")) { |
| 9584 | rule->action = HTTP_RES_ACT_DENY; |
| 9585 | cur_arg = 1; |
Willy Tarreau | f4c43c1 | 2013-06-11 17:01:13 +0200 | [diff] [blame] | 9586 | } else if (!strcmp(args[0], "set-nice")) { |
| 9587 | rule->action = HTTP_RES_ACT_SET_NICE; |
| 9588 | cur_arg = 1; |
| 9589 | |
| 9590 | if (!*args[cur_arg] || |
| 9591 | (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) { |
| 9592 | Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (integer value).\n", |
| 9593 | file, linenum, args[0]); |
| 9594 | goto out_err; |
| 9595 | } |
| 9596 | rule->arg.nice = atoi(args[cur_arg]); |
| 9597 | if (rule->arg.nice < -1024) |
| 9598 | rule->arg.nice = -1024; |
| 9599 | else if (rule->arg.nice > 1024) |
| 9600 | rule->arg.nice = 1024; |
| 9601 | cur_arg++; |
Willy Tarreau | 42cf39e | 2013-06-11 18:51:32 +0200 | [diff] [blame] | 9602 | } else if (!strcmp(args[0], "set-tos")) { |
| 9603 | #ifdef IP_TOS |
| 9604 | char *err; |
| 9605 | rule->action = HTTP_RES_ACT_SET_TOS; |
| 9606 | cur_arg = 1; |
| 9607 | |
| 9608 | if (!*args[cur_arg] || |
| 9609 | (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) { |
| 9610 | Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (integer/hex value).\n", |
| 9611 | file, linenum, args[0]); |
| 9612 | goto out_err; |
| 9613 | } |
| 9614 | |
| 9615 | rule->arg.tos = strtol(args[cur_arg], &err, 0); |
| 9616 | if (err && *err != '\0') { |
| 9617 | Alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-response %s' (integer/hex value expected).\n", |
| 9618 | file, linenum, err, args[0]); |
| 9619 | goto out_err; |
| 9620 | } |
| 9621 | cur_arg++; |
| 9622 | #else |
| 9623 | Alert("parsing [%s:%d]: 'http-response %s' is not supported on this platform (IP_TOS undefined).\n", file, linenum, args[0]); |
| 9624 | goto out_err; |
| 9625 | #endif |
Willy Tarreau | 51347ed | 2013-06-11 19:34:13 +0200 | [diff] [blame] | 9626 | } else if (!strcmp(args[0], "set-mark")) { |
| 9627 | #ifdef SO_MARK |
| 9628 | char *err; |
| 9629 | rule->action = HTTP_RES_ACT_SET_MARK; |
| 9630 | cur_arg = 1; |
| 9631 | |
| 9632 | if (!*args[cur_arg] || |
| 9633 | (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) { |
| 9634 | Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (integer/hex value).\n", |
| 9635 | file, linenum, args[0]); |
| 9636 | goto out_err; |
| 9637 | } |
| 9638 | |
| 9639 | rule->arg.mark = strtoul(args[cur_arg], &err, 0); |
| 9640 | if (err && *err != '\0') { |
| 9641 | Alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-response %s' (integer/hex value expected).\n", |
| 9642 | file, linenum, err, args[0]); |
| 9643 | goto out_err; |
| 9644 | } |
| 9645 | cur_arg++; |
| 9646 | global.last_checks |= LSTCHK_NETADM; |
| 9647 | #else |
| 9648 | Alert("parsing [%s:%d]: 'http-response %s' is not supported on this platform (SO_MARK undefined).\n", file, linenum, args[0]); |
| 9649 | goto out_err; |
| 9650 | #endif |
Willy Tarreau | 9a355ec | 2013-06-11 17:45:46 +0200 | [diff] [blame] | 9651 | } else if (!strcmp(args[0], "set-log-level")) { |
| 9652 | rule->action = HTTP_RES_ACT_SET_LOGL; |
| 9653 | cur_arg = 1; |
| 9654 | |
| 9655 | if (!*args[cur_arg] || |
| 9656 | (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) { |
| 9657 | bad_log_level: |
| 9658 | Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (log level name or 'silent').\n", |
| 9659 | file, linenum, args[0]); |
| 9660 | goto out_err; |
| 9661 | } |
| 9662 | if (strcmp(args[cur_arg], "silent") == 0) |
| 9663 | rule->arg.loglevel = -1; |
| 9664 | else if ((rule->arg.loglevel = get_log_level(args[cur_arg] + 1)) == 0) |
| 9665 | goto bad_log_level; |
| 9666 | cur_arg++; |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 9667 | } else if (strcmp(args[0], "add-header") == 0 || strcmp(args[0], "set-header") == 0) { |
| 9668 | rule->action = *args[0] == 'a' ? HTTP_RES_ACT_ADD_HDR : HTTP_RES_ACT_SET_HDR; |
| 9669 | cur_arg = 1; |
| 9670 | |
| 9671 | if (!*args[cur_arg] || !*args[cur_arg+1] || |
| 9672 | (*args[cur_arg+2] && strcmp(args[cur_arg+2], "if") != 0 && strcmp(args[cur_arg+2], "unless") != 0)) { |
| 9673 | Alert("parsing [%s:%d]: 'http-response %s' expects exactly 2 arguments.\n", |
| 9674 | file, linenum, args[0]); |
| 9675 | goto out_err; |
| 9676 | } |
| 9677 | |
| 9678 | rule->arg.hdr_add.name = strdup(args[cur_arg]); |
| 9679 | rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name); |
| 9680 | LIST_INIT(&rule->arg.hdr_add.fmt); |
| 9681 | |
Thierry FOURNIER | 1c0054f | 2013-11-20 15:09:52 +0100 | [diff] [blame] | 9682 | proxy->conf.args.ctx = ARGC_HRS; |
Thierry FOURNIER | d048d8b | 2014-03-13 16:46:18 +0100 | [diff] [blame] | 9683 | parse_logformat_string(args[cur_arg + 1], proxy, &rule->arg.hdr_add.fmt, LOG_OPT_HTTP, |
Thierry FOURNIER | eeaa951 | 2014-02-11 14:00:19 +0100 | [diff] [blame] | 9684 | (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR, |
| 9685 | file, linenum); |
Willy Tarreau | 59ad1a2 | 2014-01-29 14:39:58 +0100 | [diff] [blame] | 9686 | free(proxy->conf.lfs_file); |
| 9687 | proxy->conf.lfs_file = strdup(proxy->conf.args.file); |
| 9688 | proxy->conf.lfs_line = proxy->conf.args.line; |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 9689 | cur_arg += 2; |
Sasha Pachev | 218f064 | 2014-06-16 12:05:59 -0600 | [diff] [blame] | 9690 | } else if (strcmp(args[0], "replace-header") == 0 || strcmp(args[0], "replace-value") == 0) { |
Willy Tarreau | b854392 | 2014-06-17 18:58:26 +0200 | [diff] [blame] | 9691 | rule->action = args[0][8] == 'h' ? HTTP_RES_ACT_REPLACE_HDR : HTTP_RES_ACT_REPLACE_VAL; |
Sasha Pachev | 218f064 | 2014-06-16 12:05:59 -0600 | [diff] [blame] | 9692 | cur_arg = 1; |
| 9693 | |
| 9694 | if (!*args[cur_arg] || !*args[cur_arg+1] || !*args[cur_arg+2] || |
Baptiste Assmann | 12cb00b | 2014-08-08 17:29:06 +0200 | [diff] [blame] | 9695 | (*args[cur_arg+3] && strcmp(args[cur_arg+3], "if") != 0 && strcmp(args[cur_arg+3], "unless") != 0)) { |
| 9696 | Alert("parsing [%s:%d]: 'http-response %s' expects exactly 3 arguments.\n", |
Sasha Pachev | 218f064 | 2014-06-16 12:05:59 -0600 | [diff] [blame] | 9697 | file, linenum, args[0]); |
| 9698 | goto out_err; |
| 9699 | } |
| 9700 | |
| 9701 | rule->arg.hdr_add.name = strdup(args[cur_arg]); |
| 9702 | rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name); |
| 9703 | LIST_INIT(&rule->arg.hdr_add.fmt); |
| 9704 | |
Thierry FOURNIER | 09af0d6 | 2014-06-18 11:35:54 +0200 | [diff] [blame] | 9705 | error = NULL; |
| 9706 | if (!regex_comp(args[cur_arg + 1], &rule->arg.hdr_add.re, 1, 1, &error)) { |
| 9707 | Alert("parsing [%s:%d] : '%s' : %s.\n", file, linenum, |
| 9708 | args[cur_arg + 1], error); |
| 9709 | free(error); |
Sasha Pachev | 218f064 | 2014-06-16 12:05:59 -0600 | [diff] [blame] | 9710 | goto out_err; |
| 9711 | } |
| 9712 | |
| 9713 | proxy->conf.args.ctx = ARGC_HRQ; |
| 9714 | parse_logformat_string(args[cur_arg + 2], proxy, &rule->arg.hdr_add.fmt, LOG_OPT_HTTP, |
| 9715 | (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR, |
| 9716 | file, linenum); |
| 9717 | |
| 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 += 3; |
Thierry FOURNIER | dad3d1d | 2014-04-22 18:07:25 +0200 | [diff] [blame] | 9722 | } else if (strcmp(args[0], "del-header") == 0) { |
| 9723 | rule->action = HTTP_RES_ACT_DEL_HDR; |
| 9724 | cur_arg = 1; |
| 9725 | |
| 9726 | if (!*args[cur_arg] || |
| 9727 | (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) { |
| 9728 | Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument.\n", |
| 9729 | file, linenum, args[0]); |
| 9730 | goto out_err; |
| 9731 | } |
| 9732 | |
| 9733 | rule->arg.hdr_add.name = strdup(args[cur_arg]); |
| 9734 | rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name); |
| 9735 | |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 9736 | proxy->conf.args.ctx = ARGC_HRS; |
| 9737 | free(proxy->conf.lfs_file); |
| 9738 | proxy->conf.lfs_file = strdup(proxy->conf.args.file); |
| 9739 | proxy->conf.lfs_line = proxy->conf.args.line; |
| 9740 | cur_arg += 1; |
| 9741 | } else if (strncmp(args[0], "add-acl", 7) == 0) { |
| 9742 | /* http-request add-acl(<reference (acl name)>) <key pattern> */ |
| 9743 | rule->action = HTTP_RES_ACT_ADD_ACL; |
| 9744 | /* |
| 9745 | * '+ 8' for 'add-acl(' |
| 9746 | * '- 9' for 'add-acl(' + trailing ')' |
| 9747 | */ |
Willy Tarreau | 6c09c2c | 2014-04-25 21:38:08 +0200 | [diff] [blame] | 9748 | 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] | 9749 | |
| 9750 | cur_arg = 1; |
| 9751 | |
| 9752 | if (!*args[cur_arg] || |
| 9753 | (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) { |
| 9754 | Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument.\n", |
| 9755 | file, linenum, args[0]); |
| 9756 | goto out_err; |
| 9757 | } |
| 9758 | |
| 9759 | LIST_INIT(&rule->arg.map.key); |
Thierry FOURNIER | dad3d1d | 2014-04-22 18:07:25 +0200 | [diff] [blame] | 9760 | proxy->conf.args.ctx = ARGC_HRS; |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 9761 | parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP, |
| 9762 | (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR, |
| 9763 | file, linenum); |
Thierry FOURNIER | dad3d1d | 2014-04-22 18:07:25 +0200 | [diff] [blame] | 9764 | free(proxy->conf.lfs_file); |
| 9765 | proxy->conf.lfs_file = strdup(proxy->conf.args.file); |
| 9766 | proxy->conf.lfs_line = proxy->conf.args.line; |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 9767 | |
Thierry FOURNIER | dad3d1d | 2014-04-22 18:07:25 +0200 | [diff] [blame] | 9768 | cur_arg += 1; |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 9769 | } else if (strncmp(args[0], "del-acl", 7) == 0) { |
| 9770 | /* http-response del-acl(<reference (acl name)>) <key pattern> */ |
| 9771 | rule->action = HTTP_RES_ACT_DEL_ACL; |
| 9772 | /* |
| 9773 | * '+ 8' for 'del-acl(' |
| 9774 | * '- 9' for 'del-acl(' + trailing ')' |
| 9775 | */ |
Willy Tarreau | 6c09c2c | 2014-04-25 21:38:08 +0200 | [diff] [blame] | 9776 | 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] | 9777 | |
| 9778 | cur_arg = 1; |
| 9779 | |
| 9780 | if (!*args[cur_arg] || |
| 9781 | (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) { |
| 9782 | Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument.\n", |
| 9783 | file, linenum, args[0]); |
| 9784 | goto out_err; |
| 9785 | } |
| 9786 | |
| 9787 | LIST_INIT(&rule->arg.map.key); |
| 9788 | proxy->conf.args.ctx = ARGC_HRS; |
| 9789 | parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP, |
| 9790 | (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR, |
| 9791 | file, linenum); |
| 9792 | free(proxy->conf.lfs_file); |
| 9793 | proxy->conf.lfs_file = strdup(proxy->conf.args.file); |
| 9794 | proxy->conf.lfs_line = proxy->conf.args.line; |
| 9795 | cur_arg += 1; |
| 9796 | } else if (strncmp(args[0], "del-map", 7) == 0) { |
| 9797 | /* http-response del-map(<reference (map name)>) <key pattern> */ |
| 9798 | rule->action = HTTP_RES_ACT_DEL_MAP; |
| 9799 | /* |
| 9800 | * '+ 8' for 'del-map(' |
| 9801 | * '- 9' for 'del-map(' + trailing ')' |
| 9802 | */ |
Willy Tarreau | 6c09c2c | 2014-04-25 21:38:08 +0200 | [diff] [blame] | 9803 | 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] | 9804 | |
| 9805 | cur_arg = 1; |
| 9806 | |
| 9807 | if (!*args[cur_arg] || |
| 9808 | (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) { |
| 9809 | Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument.\n", |
| 9810 | file, linenum, args[0]); |
| 9811 | goto out_err; |
| 9812 | } |
| 9813 | |
| 9814 | LIST_INIT(&rule->arg.map.key); |
| 9815 | proxy->conf.args.ctx = ARGC_HRS; |
| 9816 | parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP, |
| 9817 | (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR, |
| 9818 | file, linenum); |
| 9819 | free(proxy->conf.lfs_file); |
| 9820 | proxy->conf.lfs_file = strdup(proxy->conf.args.file); |
| 9821 | proxy->conf.lfs_line = proxy->conf.args.line; |
| 9822 | cur_arg += 1; |
| 9823 | } else if (strncmp(args[0], "set-map", 7) == 0) { |
| 9824 | /* http-response set-map(<reference (map name)>) <key pattern> <value pattern> */ |
| 9825 | rule->action = HTTP_RES_ACT_SET_MAP; |
| 9826 | /* |
| 9827 | * '+ 8' for 'set-map(' |
| 9828 | * '- 9' for 'set-map(' + trailing ')' |
| 9829 | */ |
Willy Tarreau | 6c09c2c | 2014-04-25 21:38:08 +0200 | [diff] [blame] | 9830 | 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] | 9831 | |
| 9832 | cur_arg = 1; |
| 9833 | |
| 9834 | if (!*args[cur_arg] || !*args[cur_arg+1] || |
| 9835 | (*args[cur_arg+2] && strcmp(args[cur_arg+2], "if") != 0 && strcmp(args[cur_arg+2], "unless") != 0)) { |
| 9836 | Alert("parsing [%s:%d]: 'http-response %s' expects exactly 2 arguments.\n", |
| 9837 | file, linenum, args[0]); |
| 9838 | goto out_err; |
| 9839 | } |
| 9840 | |
| 9841 | LIST_INIT(&rule->arg.map.key); |
| 9842 | LIST_INIT(&rule->arg.map.value); |
| 9843 | |
| 9844 | proxy->conf.args.ctx = ARGC_HRS; |
| 9845 | |
| 9846 | /* key pattern */ |
| 9847 | parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP, |
| 9848 | (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR, |
| 9849 | file, linenum); |
| 9850 | |
| 9851 | /* value pattern */ |
| 9852 | parse_logformat_string(args[cur_arg + 1], proxy, &rule->arg.map.value, LOG_OPT_HTTP, |
| 9853 | (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR, |
| 9854 | file, linenum); |
| 9855 | |
| 9856 | free(proxy->conf.lfs_file); |
| 9857 | proxy->conf.lfs_file = strdup(proxy->conf.args.file); |
| 9858 | proxy->conf.lfs_line = proxy->conf.args.line; |
| 9859 | |
| 9860 | cur_arg += 2; |
Willy Tarreau | 51d861a | 2015-05-22 17:30:48 +0200 | [diff] [blame] | 9861 | } else if (strcmp(args[0], "redirect") == 0) { |
| 9862 | struct redirect_rule *redir; |
| 9863 | char *errmsg = NULL; |
| 9864 | |
| 9865 | if ((redir = http_parse_redirect_rule(file, linenum, proxy, (const char **)args + 1, &errmsg, 1, 1)) == NULL) { |
| 9866 | Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-response %s' rule : %s.\n", |
| 9867 | file, linenum, proxy_type_str(proxy), proxy->id, args[0], errmsg); |
| 9868 | goto out_err; |
| 9869 | } |
| 9870 | |
| 9871 | /* this redirect rule might already contain a parsed condition which |
| 9872 | * we'll pass to the http-request rule. |
| 9873 | */ |
| 9874 | rule->action = HTTP_RES_ACT_REDIR; |
| 9875 | rule->arg.redir = redir; |
| 9876 | rule->cond = redir->cond; |
| 9877 | redir->cond = NULL; |
| 9878 | cur_arg = 2; |
| 9879 | return rule; |
William Lallemand | 73025dd | 2014-04-24 14:38:37 +0200 | [diff] [blame] | 9880 | } else if (((custom = action_http_res_custom(args[0])) != NULL)) { |
| 9881 | char *errmsg = NULL; |
| 9882 | cur_arg = 1; |
| 9883 | /* try in the module list */ |
| 9884 | if (custom->parse(args, &cur_arg, proxy, rule, &errmsg) < 0) { |
| 9885 | Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-response %s' rule : %s.\n", |
| 9886 | file, linenum, proxy_type_str(proxy), proxy->id, args[0], errmsg); |
| 9887 | free(errmsg); |
| 9888 | goto out_err; |
| 9889 | } |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 9890 | } else { |
Sasha Pachev | 218f064 | 2014-06-16 12:05:59 -0600 | [diff] [blame] | 9891 | Alert("parsing [%s:%d]: 'http-response' expects 'allow', 'deny', 'redirect', 'add-header', 'del-header', 'set-header', 'replace-header', 'replace-value', 'set-nice', 'set-tos', 'set-mark', 'set-log-level', 'del-acl', 'add-acl', 'del-map', 'set-map', but got '%s'%s.\n", |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 9892 | file, linenum, args[0], *args[0] ? "" : " (missing argument)"); |
| 9893 | goto out_err; |
| 9894 | } |
| 9895 | |
| 9896 | if (strcmp(args[cur_arg], "if") == 0 || strcmp(args[cur_arg], "unless") == 0) { |
| 9897 | struct acl_cond *cond; |
| 9898 | char *errmsg = NULL; |
| 9899 | |
| 9900 | if ((cond = build_acl_cond(file, linenum, proxy, args+cur_arg, &errmsg)) == NULL) { |
| 9901 | Alert("parsing [%s:%d] : error detected while parsing an 'http-response %s' condition : %s.\n", |
| 9902 | file, linenum, args[0], errmsg); |
| 9903 | free(errmsg); |
| 9904 | goto out_err; |
| 9905 | } |
| 9906 | rule->cond = cond; |
| 9907 | } |
| 9908 | else if (*args[cur_arg]) { |
| 9909 | Alert("parsing [%s:%d]: 'http-response %s' expects" |
| 9910 | " either 'if' or 'unless' followed by a condition but found '%s'.\n", |
| 9911 | file, linenum, args[0], args[cur_arg]); |
| 9912 | goto out_err; |
| 9913 | } |
| 9914 | |
| 9915 | return rule; |
| 9916 | out_err: |
| 9917 | free(rule); |
| 9918 | return NULL; |
| 9919 | } |
| 9920 | |
Willy Tarreau | 4baae24 | 2012-12-27 12:00:31 +0100 | [diff] [blame] | 9921 | /* 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] | 9922 | * 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] | 9923 | * dynamic log-format rule instead of a static string. Parameter <dir> indicates |
| 9924 | * 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] | 9925 | */ |
| 9926 | 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] | 9927 | const char **args, char **errmsg, int use_fmt, int dir) |
Willy Tarreau | 4baae24 | 2012-12-27 12:00:31 +0100 | [diff] [blame] | 9928 | { |
| 9929 | struct redirect_rule *rule; |
| 9930 | int cur_arg; |
| 9931 | int type = REDIRECT_TYPE_NONE; |
| 9932 | int code = 302; |
| 9933 | const char *destination = NULL; |
| 9934 | const char *cookie = NULL; |
| 9935 | int cookie_set = 0; |
| 9936 | unsigned int flags = REDIRECT_FLAG_NONE; |
| 9937 | struct acl_cond *cond = NULL; |
| 9938 | |
| 9939 | cur_arg = 0; |
| 9940 | while (*(args[cur_arg])) { |
| 9941 | if (strcmp(args[cur_arg], "location") == 0) { |
| 9942 | if (!*args[cur_arg + 1]) |
| 9943 | goto missing_arg; |
| 9944 | |
| 9945 | type = REDIRECT_TYPE_LOCATION; |
| 9946 | cur_arg++; |
| 9947 | destination = args[cur_arg]; |
| 9948 | } |
| 9949 | else if (strcmp(args[cur_arg], "prefix") == 0) { |
| 9950 | if (!*args[cur_arg + 1]) |
| 9951 | goto missing_arg; |
Willy Tarreau | 4baae24 | 2012-12-27 12:00:31 +0100 | [diff] [blame] | 9952 | type = REDIRECT_TYPE_PREFIX; |
| 9953 | cur_arg++; |
| 9954 | destination = args[cur_arg]; |
| 9955 | } |
| 9956 | else if (strcmp(args[cur_arg], "scheme") == 0) { |
| 9957 | if (!*args[cur_arg + 1]) |
| 9958 | goto missing_arg; |
| 9959 | |
| 9960 | type = REDIRECT_TYPE_SCHEME; |
| 9961 | cur_arg++; |
| 9962 | destination = args[cur_arg]; |
| 9963 | } |
| 9964 | else if (strcmp(args[cur_arg], "set-cookie") == 0) { |
| 9965 | if (!*args[cur_arg + 1]) |
| 9966 | goto missing_arg; |
| 9967 | |
| 9968 | cur_arg++; |
| 9969 | cookie = args[cur_arg]; |
| 9970 | cookie_set = 1; |
| 9971 | } |
| 9972 | else if (strcmp(args[cur_arg], "clear-cookie") == 0) { |
| 9973 | if (!*args[cur_arg + 1]) |
| 9974 | goto missing_arg; |
| 9975 | |
| 9976 | cur_arg++; |
| 9977 | cookie = args[cur_arg]; |
| 9978 | cookie_set = 0; |
| 9979 | } |
| 9980 | else if (strcmp(args[cur_arg], "code") == 0) { |
| 9981 | if (!*args[cur_arg + 1]) |
| 9982 | goto missing_arg; |
| 9983 | |
| 9984 | cur_arg++; |
| 9985 | code = atol(args[cur_arg]); |
Yves Lafon | 3e8d1ae | 2013-03-11 11:06:05 -0400 | [diff] [blame] | 9986 | if (code < 301 || code > 308 || (code > 303 && code < 307)) { |
Willy Tarreau | 4baae24 | 2012-12-27 12:00:31 +0100 | [diff] [blame] | 9987 | memprintf(errmsg, |
Yves Lafon | 3e8d1ae | 2013-03-11 11:06:05 -0400 | [diff] [blame] | 9988 | "'%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] | 9989 | args[cur_arg - 1], args[cur_arg]); |
| 9990 | return NULL; |
| 9991 | } |
| 9992 | } |
| 9993 | else if (!strcmp(args[cur_arg],"drop-query")) { |
| 9994 | flags |= REDIRECT_FLAG_DROP_QS; |
| 9995 | } |
| 9996 | else if (!strcmp(args[cur_arg],"append-slash")) { |
| 9997 | flags |= REDIRECT_FLAG_APPEND_SLASH; |
| 9998 | } |
| 9999 | else if (strcmp(args[cur_arg], "if") == 0 || |
| 10000 | strcmp(args[cur_arg], "unless") == 0) { |
| 10001 | cond = build_acl_cond(file, linenum, curproxy, (const char **)args + cur_arg, errmsg); |
| 10002 | if (!cond) { |
| 10003 | memprintf(errmsg, "error in condition: %s", *errmsg); |
| 10004 | return NULL; |
| 10005 | } |
| 10006 | break; |
| 10007 | } |
| 10008 | else { |
| 10009 | memprintf(errmsg, |
| 10010 | "expects 'code', 'prefix', 'location', 'scheme', 'set-cookie', 'clear-cookie', 'drop-query' or 'append-slash' (was '%s')", |
| 10011 | args[cur_arg]); |
| 10012 | return NULL; |
| 10013 | } |
| 10014 | cur_arg++; |
| 10015 | } |
| 10016 | |
| 10017 | if (type == REDIRECT_TYPE_NONE) { |
| 10018 | memprintf(errmsg, "redirection type expected ('prefix', 'location', or 'scheme')"); |
| 10019 | return NULL; |
| 10020 | } |
| 10021 | |
Willy Tarreau | be4653b | 2015-05-28 15:26:58 +0200 | [diff] [blame] | 10022 | if (dir && type != REDIRECT_TYPE_LOCATION) { |
| 10023 | memprintf(errmsg, "response only supports redirect type 'location'"); |
| 10024 | return NULL; |
| 10025 | } |
| 10026 | |
Willy Tarreau | 4baae24 | 2012-12-27 12:00:31 +0100 | [diff] [blame] | 10027 | rule = (struct redirect_rule *)calloc(1, sizeof(*rule)); |
| 10028 | rule->cond = cond; |
Willy Tarreau | 60e0838 | 2013-12-03 00:48:45 +0100 | [diff] [blame] | 10029 | LIST_INIT(&rule->rdr_fmt); |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 10030 | |
| 10031 | if (!use_fmt) { |
| 10032 | /* old-style static redirect rule */ |
| 10033 | rule->rdr_str = strdup(destination); |
| 10034 | rule->rdr_len = strlen(destination); |
| 10035 | } |
| 10036 | else { |
| 10037 | /* log-format based redirect rule */ |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 10038 | |
| 10039 | /* Parse destination. Note that in the REDIRECT_TYPE_PREFIX case, |
| 10040 | * if prefix == "/", we don't want to add anything, otherwise it |
| 10041 | * makes it hard for the user to configure a self-redirection. |
| 10042 | */ |
Godbach | d972203 | 2014-12-18 15:44:58 +0800 | [diff] [blame] | 10043 | curproxy->conf.args.ctx = ARGC_RDR; |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 10044 | if (!(type == REDIRECT_TYPE_PREFIX && destination[0] == '/' && destination[1] == '\0')) { |
Thierry FOURNIER | d048d8b | 2014-03-13 16:46:18 +0100 | [diff] [blame] | 10045 | parse_logformat_string(destination, curproxy, &rule->rdr_fmt, LOG_OPT_HTTP, |
Willy Tarreau | be4653b | 2015-05-28 15:26:58 +0200 | [diff] [blame] | 10046 | dir ? (curproxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRS_HDR : SMP_VAL_BE_HRS_HDR |
| 10047 | : (curproxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR, |
Thierry FOURNIER | eeaa951 | 2014-02-11 14:00:19 +0100 | [diff] [blame] | 10048 | file, linenum); |
Willy Tarreau | 59ad1a2 | 2014-01-29 14:39:58 +0100 | [diff] [blame] | 10049 | free(curproxy->conf.lfs_file); |
| 10050 | curproxy->conf.lfs_file = strdup(curproxy->conf.args.file); |
| 10051 | curproxy->conf.lfs_line = curproxy->conf.args.line; |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 10052 | } |
| 10053 | } |
| 10054 | |
Willy Tarreau | 4baae24 | 2012-12-27 12:00:31 +0100 | [diff] [blame] | 10055 | if (cookie) { |
| 10056 | /* depending on cookie_set, either we want to set the cookie, or to clear it. |
| 10057 | * a clear consists in appending "; path=/; Max-Age=0;" at the end. |
| 10058 | */ |
| 10059 | rule->cookie_len = strlen(cookie); |
| 10060 | if (cookie_set) { |
| 10061 | rule->cookie_str = malloc(rule->cookie_len + 10); |
| 10062 | memcpy(rule->cookie_str, cookie, rule->cookie_len); |
| 10063 | memcpy(rule->cookie_str + rule->cookie_len, "; path=/;", 10); |
| 10064 | rule->cookie_len += 9; |
| 10065 | } else { |
| 10066 | rule->cookie_str = malloc(rule->cookie_len + 21); |
| 10067 | memcpy(rule->cookie_str, cookie, rule->cookie_len); |
| 10068 | memcpy(rule->cookie_str + rule->cookie_len, "; path=/; Max-Age=0;", 21); |
| 10069 | rule->cookie_len += 20; |
| 10070 | } |
| 10071 | } |
| 10072 | rule->type = type; |
| 10073 | rule->code = code; |
| 10074 | rule->flags = flags; |
| 10075 | LIST_INIT(&rule->list); |
| 10076 | return rule; |
| 10077 | |
| 10078 | missing_arg: |
| 10079 | memprintf(errmsg, "missing argument for '%s'", args[cur_arg]); |
| 10080 | return NULL; |
| 10081 | } |
| 10082 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 10083 | /************************************************************************/ |
| 10084 | /* The code below is dedicated to ACL parsing and matching */ |
| 10085 | /************************************************************************/ |
| 10086 | |
| 10087 | |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 10088 | /* This function ensures that the prerequisites for an L7 fetch are ready, |
| 10089 | * which means that a request or response is ready. If some data is missing, |
| 10090 | * 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] | 10091 | * to extract data from L7. If <req_vol> is non-null during a request prefetch, |
| 10092 | * another test is made to ensure the required information is not gone. |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 10093 | * |
| 10094 | * The function returns : |
Willy Tarreau | 506d050 | 2013-07-06 13:29:24 +0200 | [diff] [blame] | 10095 | * 0 with SMP_F_MAY_CHANGE in the sample flags if some data is missing to |
| 10096 | * decide whether or not an HTTP message is present ; |
| 10097 | * 0 if the requested data cannot be fetched or if it is certain that |
| 10098 | * we'll never have any HTTP message there ; |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 10099 | * 1 if an HTTP message is ready |
| 10100 | */ |
| 10101 | static int |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 10102 | smp_prefetch_http(struct proxy *px, struct stream *s, unsigned int opt, |
Willy Tarreau | 24e32d8 | 2012-04-23 23:55:44 +0200 | [diff] [blame] | 10103 | const struct arg *args, struct sample *smp, int req_vol) |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 10104 | { |
Willy Tarreau | 192252e | 2015-04-04 01:47:55 +0200 | [diff] [blame] | 10105 | struct http_txn *txn; |
| 10106 | struct http_msg *msg; |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 10107 | |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 10108 | /* Note: this function may only be used from places where |
| 10109 | * http_init_txn() has already been done, and implies that <s>, |
| 10110 | * <txn>, and <hdr_idx.v> are properly set. An extra check protects |
| 10111 | * against an eventual mistake in the fetch capability matrix. |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 10112 | */ |
| 10113 | |
Willy Tarreau | 192252e | 2015-04-04 01:47:55 +0200 | [diff] [blame] | 10114 | if (!s) |
| 10115 | return 0; |
| 10116 | txn = s->txn; |
| 10117 | |
| 10118 | if (!txn) |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 10119 | return 0; |
Willy Tarreau | 192252e | 2015-04-04 01:47:55 +0200 | [diff] [blame] | 10120 | msg = &txn->req; |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 10121 | |
| 10122 | /* Check for a dependency on a request */ |
Willy Tarreau | f853c46 | 2012-04-23 18:53:56 +0200 | [diff] [blame] | 10123 | smp->type = SMP_T_BOOL; |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 10124 | |
Willy Tarreau | 32a6f2e | 2012-04-25 10:13:36 +0200 | [diff] [blame] | 10125 | if ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) { |
Willy Tarreau | aae75e3 | 2013-03-29 12:31:49 +0100 | [diff] [blame] | 10126 | /* If the buffer does not leave enough free space at the end, |
| 10127 | * we must first realign it. |
| 10128 | */ |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 10129 | if (s->req.buf->p > s->req.buf->data && |
| 10130 | s->req.buf->i + s->req.buf->p > s->req.buf->data + s->req.buf->size - global.tune.maxrewrite) |
| 10131 | buffer_slow_realign(s->req.buf); |
Willy Tarreau | aae75e3 | 2013-03-29 12:31:49 +0100 | [diff] [blame] | 10132 | |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 10133 | if (unlikely(txn->req.msg_state < HTTP_MSG_BODY)) { |
Willy Tarreau | 472b1ee | 2013-10-14 22:41:30 +0200 | [diff] [blame] | 10134 | if (msg->msg_state == HTTP_MSG_ERROR) |
Willy Tarreau | 506d050 | 2013-07-06 13:29:24 +0200 | [diff] [blame] | 10135 | return 0; |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 10136 | |
| 10137 | /* Try to decode HTTP request */ |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 10138 | if (likely(msg->next < s->req.buf->i)) |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 10139 | http_msg_analyzer(msg, &txn->hdr_idx); |
| 10140 | |
| 10141 | /* Still no valid request ? */ |
| 10142 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) { |
Willy Tarreau | 3bf1b2b | 2012-08-27 20:46:07 +0200 | [diff] [blame] | 10143 | if ((msg->msg_state == HTTP_MSG_ERROR) || |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 10144 | buffer_full(s->req.buf, global.tune.maxrewrite)) { |
Willy Tarreau | 506d050 | 2013-07-06 13:29:24 +0200 | [diff] [blame] | 10145 | return 0; |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 10146 | } |
| 10147 | /* wait for final state */ |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 10148 | smp->flags |= SMP_F_MAY_CHANGE; |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 10149 | return 0; |
| 10150 | } |
| 10151 | |
| 10152 | /* OK we just got a valid HTTP request. We have some minor |
| 10153 | * preparation to perform so that further checks can rely |
| 10154 | * on HTTP tests. |
| 10155 | */ |
Willy Tarreau | aae75e3 | 2013-03-29 12:31:49 +0100 | [diff] [blame] | 10156 | |
| 10157 | /* If the request was parsed but was too large, we must absolutely |
| 10158 | * return an error so that it is not processed. At the moment this |
| 10159 | * cannot happen, but if the parsers are to change in the future, |
| 10160 | * we want this check to be maintained. |
| 10161 | */ |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 10162 | if (unlikely(s->req.buf->i + s->req.buf->p > |
| 10163 | s->req.buf->data + s->req.buf->size - global.tune.maxrewrite)) { |
Willy Tarreau | aae75e3 | 2013-03-29 12:31:49 +0100 | [diff] [blame] | 10164 | msg->msg_state = HTTP_MSG_ERROR; |
Willy Tarreau | 506d050 | 2013-07-06 13:29:24 +0200 | [diff] [blame] | 10165 | smp->data.uint = 1; |
Willy Tarreau | aae75e3 | 2013-03-29 12:31:49 +0100 | [diff] [blame] | 10166 | return 1; |
| 10167 | } |
| 10168 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 10169 | 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] | 10170 | if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD) |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 10171 | s->flags |= SF_REDIRECTABLE; |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 10172 | |
Willy Tarreau | 506d050 | 2013-07-06 13:29:24 +0200 | [diff] [blame] | 10173 | if (unlikely(msg->sl.rq.v_l == 0) && !http_upgrade_v09_to_v10(txn)) |
| 10174 | return 0; |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 10175 | } |
| 10176 | |
Willy Tarreau | 506d050 | 2013-07-06 13:29:24 +0200 | [diff] [blame] | 10177 | if (req_vol && txn->rsp.msg_state != HTTP_MSG_RPBEFORE) { |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 10178 | return 0; /* data might have moved and indexes changed */ |
Willy Tarreau | 506d050 | 2013-07-06 13:29:24 +0200 | [diff] [blame] | 10179 | } |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 10180 | |
| 10181 | /* otherwise everything's ready for the request */ |
| 10182 | } |
Willy Tarreau | 24e32d8 | 2012-04-23 23:55:44 +0200 | [diff] [blame] | 10183 | else { |
| 10184 | /* Check for a dependency on a response */ |
Willy Tarreau | 506d050 | 2013-07-06 13:29:24 +0200 | [diff] [blame] | 10185 | if (txn->rsp.msg_state < HTTP_MSG_BODY) { |
| 10186 | smp->flags |= SMP_F_MAY_CHANGE; |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 10187 | return 0; |
Willy Tarreau | 506d050 | 2013-07-06 13:29:24 +0200 | [diff] [blame] | 10188 | } |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 10189 | } |
| 10190 | |
| 10191 | /* everything's OK */ |
Willy Tarreau | 506d050 | 2013-07-06 13:29:24 +0200 | [diff] [blame] | 10192 | smp->data.uint = 1; |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 10193 | return 1; |
| 10194 | } |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 10195 | |
Willy Tarreau | 6c616e0 | 2014-06-25 16:56:41 +0200 | [diff] [blame] | 10196 | /* Note: these functinos *do* modify the sample. Even in case of success, at |
| 10197 | * least the type and uint value are modified. |
| 10198 | */ |
Willy Tarreau | c0239e0 | 2012-04-16 14:42:55 +0200 | [diff] [blame] | 10199 | #define CHECK_HTTP_MESSAGE_FIRST() \ |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10200 | do { int r = smp_prefetch_http(smp->px, smp->strm, smp->opt, args, smp, 1); if (r <= 0) return r; } while (0) |
Willy Tarreau | c0239e0 | 2012-04-16 14:42:55 +0200 | [diff] [blame] | 10201 | |
Willy Tarreau | 24e32d8 | 2012-04-23 23:55:44 +0200 | [diff] [blame] | 10202 | #define CHECK_HTTP_MESSAGE_FIRST_PERM() \ |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10203 | do { int r = smp_prefetch_http(smp->px, smp->strm, smp->opt, args, smp, 0); if (r <= 0) return r; } while (0) |
Willy Tarreau | 24e32d8 | 2012-04-23 23:55:44 +0200 | [diff] [blame] | 10204 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 10205 | |
| 10206 | /* 1. Check on METHOD |
| 10207 | * We use the pre-parsed method if it is known, and store its number as an |
| 10208 | * integer. If it is unknown, we use the pointer and the length. |
| 10209 | */ |
Thierry FOURNIER | e47e4e2 | 2014-04-28 11:18:57 +0200 | [diff] [blame] | 10210 | 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] | 10211 | { |
| 10212 | int len, meth; |
| 10213 | |
Thierry FOURNIER | 580c32c | 2014-01-24 10:58:12 +0100 | [diff] [blame] | 10214 | len = strlen(text); |
| 10215 | meth = find_http_meth(text, len); |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 10216 | |
| 10217 | pattern->val.i = meth; |
| 10218 | if (meth == HTTP_METH_OTHER) { |
Willy Tarreau | 912c119 | 2014-08-29 15:15:50 +0200 | [diff] [blame] | 10219 | pattern->ptr.str = (char *)text; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 10220 | pattern->len = len; |
| 10221 | } |
Thierry FOURNIER | d437314 | 2013-12-17 01:10:10 +0100 | [diff] [blame] | 10222 | else { |
| 10223 | pattern->ptr.str = NULL; |
| 10224 | pattern->len = 0; |
Thierry FOURNIER | d437314 | 2013-12-17 01:10:10 +0100 | [diff] [blame] | 10225 | } |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 10226 | return 1; |
| 10227 | } |
| 10228 | |
Willy Tarreau | 8e5e955 | 2011-12-16 15:38:49 +0100 | [diff] [blame] | 10229 | /* This function fetches the method of current HTTP request and stores |
| 10230 | * it in the global pattern struct as a chunk. There are two possibilities : |
| 10231 | * - if the method is known (not HTTP_METH_OTHER), its identifier is stored |
| 10232 | * in <len> and <ptr> is NULL ; |
| 10233 | * - if the method is unknown (HTTP_METH_OTHER), <ptr> points to the text and |
| 10234 | * <len> to its length. |
Thierry FOURNIER | a65b343 | 2013-11-28 18:22:00 +0100 | [diff] [blame] | 10235 | * This is intended to be used with pat_match_meth() only. |
Willy Tarreau | 8e5e955 | 2011-12-16 15:38:49 +0100 | [diff] [blame] | 10236 | */ |
Willy Tarreau | d41f8d8 | 2007-06-10 10:06:18 +0200 | [diff] [blame] | 10237 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10238 | 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] | 10239 | { |
| 10240 | int meth; |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 10241 | struct http_txn *txn = smp->strm->txn; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 10242 | |
Willy Tarreau | 24e32d8 | 2012-04-23 23:55:44 +0200 | [diff] [blame] | 10243 | CHECK_HTTP_MESSAGE_FIRST_PERM(); |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 10244 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 10245 | meth = txn->meth; |
Thierry FOURNIER | d437314 | 2013-12-17 01:10:10 +0100 | [diff] [blame] | 10246 | smp->type = SMP_T_METH; |
| 10247 | smp->data.meth.meth = meth; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 10248 | if (meth == HTTP_METH_OTHER) { |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 10249 | if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE) |
| 10250 | /* ensure the indexes are not affected */ |
| 10251 | return 0; |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 10252 | smp->flags |= SMP_F_CONST; |
Thierry FOURNIER | d437314 | 2013-12-17 01:10:10 +0100 | [diff] [blame] | 10253 | smp->data.meth.str.len = txn->req.sl.rq.m_l; |
| 10254 | smp->data.meth.str.str = txn->req.chn->buf->p; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 10255 | } |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 10256 | smp->flags |= SMP_F_VOL_1ST; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 10257 | return 1; |
| 10258 | } |
| 10259 | |
Willy Tarreau | 8e5e955 | 2011-12-16 15:38:49 +0100 | [diff] [blame] | 10260 | /* See above how the method is stored in the global pattern */ |
Thierry FOURNIER | 5338eea | 2013-12-16 14:22:13 +0100 | [diff] [blame] | 10261 | 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] | 10262 | { |
Willy Tarreau | c8d7c96 | 2007-06-17 08:20:33 +0200 | [diff] [blame] | 10263 | int icase; |
Thierry FOURNIER | 5338eea | 2013-12-16 14:22:13 +0100 | [diff] [blame] | 10264 | struct pattern_list *lst; |
| 10265 | struct pattern *pattern; |
Willy Tarreau | c8d7c96 | 2007-06-17 08:20:33 +0200 | [diff] [blame] | 10266 | |
Thierry FOURNIER | 5338eea | 2013-12-16 14:22:13 +0100 | [diff] [blame] | 10267 | list_for_each_entry(lst, &expr->patterns, list) { |
| 10268 | pattern = &lst->pat; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 10269 | |
Thierry FOURNIER | 5338eea | 2013-12-16 14:22:13 +0100 | [diff] [blame] | 10270 | /* well-known method */ |
| 10271 | if (pattern->val.i != HTTP_METH_OTHER) { |
| 10272 | if (smp->data.meth.meth == pattern->val.i) |
| 10273 | return pattern; |
| 10274 | else |
| 10275 | continue; |
| 10276 | } |
Willy Tarreau | c8d7c96 | 2007-06-17 08:20:33 +0200 | [diff] [blame] | 10277 | |
Thierry FOURNIER | 5338eea | 2013-12-16 14:22:13 +0100 | [diff] [blame] | 10278 | /* Other method, we must compare the strings */ |
| 10279 | if (pattern->len != smp->data.meth.str.len) |
| 10280 | continue; |
| 10281 | |
Thierry FOURNIER | e47e4e2 | 2014-04-28 11:18:57 +0200 | [diff] [blame] | 10282 | icase = expr->mflags & PAT_MF_IGNORE_CASE; |
Willy Tarreau | 4de2a94 | 2014-08-28 20:42:57 +0200 | [diff] [blame] | 10283 | if ((icase && strncasecmp(pattern->ptr.str, smp->data.meth.str.str, smp->data.meth.str.len) == 0) || |
| 10284 | (!icase && strncmp(pattern->ptr.str, smp->data.meth.str.str, smp->data.meth.str.len) == 0)) |
Thierry FOURNIER | 5338eea | 2013-12-16 14:22:13 +0100 | [diff] [blame] | 10285 | return pattern; |
| 10286 | } |
| 10287 | return NULL; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 10288 | } |
| 10289 | |
Willy Tarreau | d41f8d8 | 2007-06-10 10:06:18 +0200 | [diff] [blame] | 10290 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10291 | 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] | 10292 | { |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 10293 | struct http_txn *txn = smp->strm->txn; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 10294 | char *ptr; |
| 10295 | int len; |
| 10296 | |
Willy Tarreau | c0239e0 | 2012-04-16 14:42:55 +0200 | [diff] [blame] | 10297 | CHECK_HTTP_MESSAGE_FIRST(); |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 10298 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 10299 | len = txn->req.sl.rq.v_l; |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 10300 | ptr = txn->req.chn->buf->p + txn->req.sl.rq.v; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 10301 | |
| 10302 | while ((len-- > 0) && (*ptr++ != '/')); |
| 10303 | if (len <= 0) |
| 10304 | return 0; |
| 10305 | |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 10306 | smp->type = SMP_T_STR; |
Willy Tarreau | f853c46 | 2012-04-23 18:53:56 +0200 | [diff] [blame] | 10307 | smp->data.str.str = ptr; |
| 10308 | smp->data.str.len = len; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 10309 | |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 10310 | smp->flags = SMP_F_VOL_1ST | SMP_F_CONST; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 10311 | return 1; |
| 10312 | } |
| 10313 | |
Willy Tarreau | d41f8d8 | 2007-06-10 10:06:18 +0200 | [diff] [blame] | 10314 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10315 | 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] | 10316 | { |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 10317 | struct http_txn *txn; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 10318 | char *ptr; |
| 10319 | int len; |
| 10320 | |
Willy Tarreau | c0239e0 | 2012-04-16 14:42:55 +0200 | [diff] [blame] | 10321 | CHECK_HTTP_MESSAGE_FIRST(); |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 10322 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 10323 | txn = smp->strm->txn; |
Willy Tarreau | f26b252 | 2012-12-14 08:33:14 +0100 | [diff] [blame] | 10324 | if (txn->rsp.msg_state < HTTP_MSG_BODY) |
| 10325 | return 0; |
| 10326 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 10327 | len = txn->rsp.sl.st.v_l; |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 10328 | ptr = txn->rsp.chn->buf->p; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 10329 | |
| 10330 | while ((len-- > 0) && (*ptr++ != '/')); |
| 10331 | if (len <= 0) |
| 10332 | return 0; |
| 10333 | |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 10334 | smp->type = SMP_T_STR; |
Willy Tarreau | f853c46 | 2012-04-23 18:53:56 +0200 | [diff] [blame] | 10335 | smp->data.str.str = ptr; |
| 10336 | smp->data.str.len = len; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 10337 | |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 10338 | smp->flags = SMP_F_VOL_1ST | SMP_F_CONST; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 10339 | return 1; |
| 10340 | } |
| 10341 | |
| 10342 | /* 3. Check on Status Code. We manipulate integers here. */ |
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_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] | 10345 | { |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [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 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 10352 | txn = smp->strm->txn; |
Willy Tarreau | f26b252 | 2012-12-14 08:33:14 +0100 | [diff] [blame] | 10353 | if (txn->rsp.msg_state < HTTP_MSG_BODY) |
| 10354 | return 0; |
| 10355 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 10356 | len = txn->rsp.sl.st.c_l; |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 10357 | ptr = txn->rsp.chn->buf->p + txn->rsp.sl.st.c; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 10358 | |
Willy Tarreau | f853c46 | 2012-04-23 18:53:56 +0200 | [diff] [blame] | 10359 | smp->type = SMP_T_UINT; |
| 10360 | smp->data.uint = __strl2ui(ptr, len); |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 10361 | smp->flags = SMP_F_VOL_1ST; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 10362 | return 1; |
| 10363 | } |
| 10364 | |
Willy Tarreau | a5910cc | 2015-05-02 00:46:08 +0200 | [diff] [blame] | 10365 | /* returns the longest available part of the body. This requires that the body |
| 10366 | * has been waited for using http-buffer-request. |
| 10367 | */ |
| 10368 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10369 | 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] | 10370 | { |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 10371 | struct http_txn *txn = smp->strm->txn; |
Willy Tarreau | a5910cc | 2015-05-02 00:46:08 +0200 | [diff] [blame] | 10372 | struct http_msg *msg; |
| 10373 | unsigned long len; |
| 10374 | unsigned long block1; |
| 10375 | char *body; |
| 10376 | struct chunk *temp; |
| 10377 | |
| 10378 | CHECK_HTTP_MESSAGE_FIRST(); |
| 10379 | |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10380 | if ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) |
Willy Tarreau | a5910cc | 2015-05-02 00:46:08 +0200 | [diff] [blame] | 10381 | msg = &txn->req; |
| 10382 | else |
| 10383 | msg = &txn->rsp; |
| 10384 | |
| 10385 | len = http_body_bytes(msg); |
| 10386 | body = b_ptr(msg->chn->buf, -http_data_rewind(msg)); |
| 10387 | |
| 10388 | block1 = len; |
| 10389 | if (block1 > msg->chn->buf->data + msg->chn->buf->size - body) |
| 10390 | block1 = msg->chn->buf->data + msg->chn->buf->size - body; |
| 10391 | |
| 10392 | if (block1 == len) { |
| 10393 | /* buffer is not wrapped (or empty) */ |
| 10394 | smp->type = SMP_T_BIN; |
| 10395 | smp->data.str.str = body; |
| 10396 | smp->data.str.len = len; |
| 10397 | smp->flags = SMP_F_VOL_TEST | SMP_F_CONST; |
| 10398 | } |
| 10399 | else { |
| 10400 | /* buffer is wrapped, we need to defragment it */ |
| 10401 | temp = get_trash_chunk(); |
| 10402 | memcpy(temp->str, body, block1); |
| 10403 | memcpy(temp->str + block1, msg->chn->buf->data, len - block1); |
| 10404 | smp->type = SMP_T_BIN; |
| 10405 | smp->data.str.str = temp->str; |
| 10406 | smp->data.str.len = len; |
| 10407 | smp->flags = SMP_F_VOL_TEST; |
| 10408 | } |
| 10409 | return 1; |
| 10410 | } |
| 10411 | |
| 10412 | |
| 10413 | /* returns the available length of the body. This requires that the body |
| 10414 | * has been waited for using http-buffer-request. |
| 10415 | */ |
| 10416 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10417 | smp_fetch_body_len(const struct arg *args, struct sample *smp, const char *kw, void *private) |
Willy Tarreau | a5910cc | 2015-05-02 00:46:08 +0200 | [diff] [blame] | 10418 | { |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 10419 | struct http_txn *txn = smp->strm->txn; |
Willy Tarreau | a5910cc | 2015-05-02 00:46:08 +0200 | [diff] [blame] | 10420 | struct http_msg *msg; |
| 10421 | |
| 10422 | CHECK_HTTP_MESSAGE_FIRST(); |
| 10423 | |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10424 | if ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) |
Willy Tarreau | a5910cc | 2015-05-02 00:46:08 +0200 | [diff] [blame] | 10425 | msg = &txn->req; |
| 10426 | else |
| 10427 | msg = &txn->rsp; |
| 10428 | |
| 10429 | smp->type = SMP_T_UINT; |
| 10430 | smp->data.uint = http_body_bytes(msg); |
| 10431 | |
| 10432 | smp->flags = SMP_F_VOL_TEST; |
| 10433 | return 1; |
| 10434 | } |
| 10435 | |
| 10436 | |
| 10437 | /* returns the advertised length of the body, or the advertised size of the |
| 10438 | * chunks available in the buffer. This requires that the body has been waited |
| 10439 | * for using http-buffer-request. |
| 10440 | */ |
| 10441 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10442 | 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] | 10443 | { |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 10444 | struct http_txn *txn = smp->strm->txn; |
Willy Tarreau | a5910cc | 2015-05-02 00:46:08 +0200 | [diff] [blame] | 10445 | struct http_msg *msg; |
| 10446 | |
| 10447 | CHECK_HTTP_MESSAGE_FIRST(); |
| 10448 | |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10449 | if ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) |
Willy Tarreau | a5910cc | 2015-05-02 00:46:08 +0200 | [diff] [blame] | 10450 | msg = &txn->req; |
| 10451 | else |
| 10452 | msg = &txn->rsp; |
| 10453 | |
| 10454 | smp->type = SMP_T_UINT; |
| 10455 | smp->data.uint = msg->body_len; |
| 10456 | |
| 10457 | smp->flags = SMP_F_VOL_TEST; |
| 10458 | return 1; |
| 10459 | } |
| 10460 | |
| 10461 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 10462 | /* 4. Check on URL/URI. A pointer to the URI is stored. */ |
Willy Tarreau | d41f8d8 | 2007-06-10 10:06:18 +0200 | [diff] [blame] | 10463 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10464 | 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] | 10465 | { |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 10466 | struct http_txn *txn; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 10467 | |
Willy Tarreau | c0239e0 | 2012-04-16 14:42:55 +0200 | [diff] [blame] | 10468 | CHECK_HTTP_MESSAGE_FIRST(); |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 10469 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 10470 | txn = smp->strm->txn; |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 10471 | smp->type = SMP_T_STR; |
Willy Tarreau | f853c46 | 2012-04-23 18:53:56 +0200 | [diff] [blame] | 10472 | smp->data.str.len = txn->req.sl.rq.u_l; |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 10473 | smp->data.str.str = txn->req.chn->buf->p + txn->req.sl.rq.u; |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 10474 | smp->flags = SMP_F_VOL_1ST | SMP_F_CONST; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 10475 | return 1; |
| 10476 | } |
| 10477 | |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 10478 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10479 | 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] | 10480 | { |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 10481 | struct http_txn *txn; |
Willy Tarreau | 4c804ec | 2013-09-30 14:37:14 +0200 | [diff] [blame] | 10482 | struct sockaddr_storage addr; |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 10483 | |
Willy Tarreau | c0239e0 | 2012-04-16 14:42:55 +0200 | [diff] [blame] | 10484 | CHECK_HTTP_MESSAGE_FIRST(); |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 10485 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 10486 | txn = smp->strm->txn; |
Thierry FOURNIER | 9f95e40 | 2014-03-21 14:51:46 +0100 | [diff] [blame] | 10487 | 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] | 10488 | if (((struct sockaddr_in *)&addr)->sin_family != AF_INET) |
Willy Tarreau | f4362b3 | 2011-12-16 17:49:52 +0100 | [diff] [blame] | 10489 | return 0; |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 10490 | |
Willy Tarreau | 4c804ec | 2013-09-30 14:37:14 +0200 | [diff] [blame] | 10491 | smp->type = SMP_T_IPV4; |
| 10492 | smp->data.ipv4 = ((struct sockaddr_in *)&addr)->sin_addr; |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 10493 | smp->flags = 0; |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 10494 | return 1; |
| 10495 | } |
| 10496 | |
| 10497 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10498 | 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] | 10499 | { |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 10500 | struct http_txn *txn; |
Willy Tarreau | 4c804ec | 2013-09-30 14:37:14 +0200 | [diff] [blame] | 10501 | struct sockaddr_storage addr; |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 10502 | |
Willy Tarreau | c0239e0 | 2012-04-16 14:42:55 +0200 | [diff] [blame] | 10503 | CHECK_HTTP_MESSAGE_FIRST(); |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 10504 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 10505 | txn = smp->strm->txn; |
Thierry FOURNIER | 9f95e40 | 2014-03-21 14:51:46 +0100 | [diff] [blame] | 10506 | 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] | 10507 | if (((struct sockaddr_in *)&addr)->sin_family != AF_INET) |
| 10508 | return 0; |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 10509 | |
Willy Tarreau | 4c804ec | 2013-09-30 14:37:14 +0200 | [diff] [blame] | 10510 | smp->type = SMP_T_UINT; |
| 10511 | smp->data.uint = ntohs(((struct sockaddr_in *)&addr)->sin_port); |
Willy Tarreau | 21e5b0e | 2012-04-23 19:25:44 +0200 | [diff] [blame] | 10512 | smp->flags = 0; |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 10513 | return 1; |
| 10514 | } |
| 10515 | |
Willy Tarreau | 185b5c4 | 2012-04-26 15:11:51 +0200 | [diff] [blame] | 10516 | /* Fetch an HTTP header. A pointer to the beginning of the value is returned. |
| 10517 | * Accepts an optional argument of type string containing the header field name, |
| 10518 | * and an optional argument of type signed or unsigned integer to request an |
| 10519 | * 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] | 10520 | * headers are considered from the first one. It does not stop on commas and |
| 10521 | * returns full lines instead (useful for User-Agent or Date for example). |
| 10522 | */ |
| 10523 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10524 | 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] | 10525 | { |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 10526 | struct hdr_idx *idx; |
Willy Tarreau | 04ff9f1 | 2013-06-10 18:39:42 +0200 | [diff] [blame] | 10527 | struct hdr_ctx *ctx = smp->ctx.a[0]; |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 10528 | const struct http_msg *msg; |
Willy Tarreau | 04ff9f1 | 2013-06-10 18:39:42 +0200 | [diff] [blame] | 10529 | int occ = 0; |
| 10530 | const char *name_str = NULL; |
| 10531 | int name_len = 0; |
| 10532 | |
| 10533 | if (!ctx) { |
| 10534 | /* first call */ |
| 10535 | ctx = &static_hdr_ctx; |
| 10536 | ctx->idx = 0; |
| 10537 | smp->ctx.a[0] = ctx; |
| 10538 | } |
| 10539 | |
| 10540 | if (args) { |
| 10541 | if (args[0].type != ARGT_STR) |
| 10542 | return 0; |
| 10543 | name_str = args[0].data.str.str; |
| 10544 | name_len = args[0].data.str.len; |
| 10545 | |
| 10546 | if (args[1].type == ARGT_UINT || args[1].type == ARGT_SINT) |
| 10547 | occ = args[1].data.uint; |
| 10548 | } |
| 10549 | |
| 10550 | CHECK_HTTP_MESSAGE_FIRST(); |
| 10551 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 10552 | idx = &smp->strm->txn->hdr_idx; |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10553 | 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] | 10554 | |
Willy Tarreau | 04ff9f1 | 2013-06-10 18:39:42 +0200 | [diff] [blame] | 10555 | if (ctx && !(smp->flags & SMP_F_NOT_LAST)) |
| 10556 | /* search for header from the beginning */ |
| 10557 | ctx->idx = 0; |
| 10558 | |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10559 | if (!occ && !(smp->opt & SMP_OPT_ITERATE)) |
Willy Tarreau | 04ff9f1 | 2013-06-10 18:39:42 +0200 | [diff] [blame] | 10560 | /* no explicit occurrence and single fetch => last header by default */ |
| 10561 | occ = -1; |
| 10562 | |
| 10563 | if (!occ) |
| 10564 | /* prepare to report multiple occurrences for ACL fetches */ |
| 10565 | smp->flags |= SMP_F_NOT_LAST; |
| 10566 | |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 10567 | smp->type = SMP_T_STR; |
| 10568 | smp->flags |= SMP_F_VOL_HDR | SMP_F_CONST; |
Willy Tarreau | 04ff9f1 | 2013-06-10 18:39:42 +0200 | [diff] [blame] | 10569 | if (http_get_fhdr(msg, name_str, name_len, idx, occ, ctx, &smp->data.str.str, &smp->data.str.len)) |
| 10570 | return 1; |
| 10571 | |
| 10572 | smp->flags &= ~SMP_F_NOT_LAST; |
| 10573 | return 0; |
| 10574 | } |
| 10575 | |
| 10576 | /* 6. Check on HTTP header count. The number of occurrences is returned. |
| 10577 | * Accepts exactly 1 argument of type string. It does not stop on commas and |
| 10578 | * returns full lines instead (useful for User-Agent or Date for example). |
| 10579 | */ |
| 10580 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10581 | 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] | 10582 | { |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 10583 | struct hdr_idx *idx; |
Willy Tarreau | 04ff9f1 | 2013-06-10 18:39:42 +0200 | [diff] [blame] | 10584 | struct hdr_ctx ctx; |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 10585 | const struct http_msg *msg; |
Willy Tarreau | 04ff9f1 | 2013-06-10 18:39:42 +0200 | [diff] [blame] | 10586 | int cnt; |
Willy Tarreau | 601a4d1 | 2015-04-01 19:16:09 +0200 | [diff] [blame] | 10587 | const char *name = NULL; |
| 10588 | int len = 0; |
Willy Tarreau | 04ff9f1 | 2013-06-10 18:39:42 +0200 | [diff] [blame] | 10589 | |
Willy Tarreau | 601a4d1 | 2015-04-01 19:16:09 +0200 | [diff] [blame] | 10590 | if (args && args->type == ARGT_STR) { |
| 10591 | name = args->data.str.str; |
| 10592 | len = args->data.str.len; |
| 10593 | } |
Willy Tarreau | 04ff9f1 | 2013-06-10 18:39:42 +0200 | [diff] [blame] | 10594 | |
| 10595 | CHECK_HTTP_MESSAGE_FIRST(); |
| 10596 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 10597 | idx = &smp->strm->txn->hdr_idx; |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10598 | 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] | 10599 | |
Willy Tarreau | 04ff9f1 | 2013-06-10 18:39:42 +0200 | [diff] [blame] | 10600 | ctx.idx = 0; |
| 10601 | cnt = 0; |
Willy Tarreau | 601a4d1 | 2015-04-01 19:16:09 +0200 | [diff] [blame] | 10602 | 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] | 10603 | cnt++; |
| 10604 | |
| 10605 | smp->type = SMP_T_UINT; |
| 10606 | smp->data.uint = cnt; |
| 10607 | smp->flags = SMP_F_VOL_HDR; |
| 10608 | return 1; |
| 10609 | } |
| 10610 | |
Willy Tarreau | eb27ec7 | 2015-02-20 13:55:29 +0100 | [diff] [blame] | 10611 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10612 | 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] | 10613 | { |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 10614 | struct hdr_idx *idx; |
Willy Tarreau | eb27ec7 | 2015-02-20 13:55:29 +0100 | [diff] [blame] | 10615 | struct hdr_ctx ctx; |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 10616 | const struct http_msg *msg; |
Willy Tarreau | eb27ec7 | 2015-02-20 13:55:29 +0100 | [diff] [blame] | 10617 | struct chunk *temp; |
| 10618 | char del = ','; |
| 10619 | |
| 10620 | if (args && args->type == ARGT_STR) |
| 10621 | del = *args[0].data.str.str; |
| 10622 | |
| 10623 | CHECK_HTTP_MESSAGE_FIRST(); |
| 10624 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 10625 | idx = &smp->strm->txn->hdr_idx; |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10626 | 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] | 10627 | |
Willy Tarreau | eb27ec7 | 2015-02-20 13:55:29 +0100 | [diff] [blame] | 10628 | temp = get_trash_chunk(); |
| 10629 | |
| 10630 | ctx.idx = 0; |
| 10631 | while (http_find_next_header(msg->chn->buf->p, idx, &ctx)) { |
| 10632 | if (temp->len) |
| 10633 | temp->str[temp->len++] = del; |
| 10634 | memcpy(temp->str + temp->len, ctx.line, ctx.del); |
| 10635 | temp->len += ctx.del; |
| 10636 | } |
| 10637 | |
| 10638 | smp->type = SMP_T_STR; |
| 10639 | smp->data.str.str = temp->str; |
| 10640 | smp->data.str.len = temp->len; |
| 10641 | smp->flags = SMP_F_VOL_HDR; |
| 10642 | return 1; |
| 10643 | } |
| 10644 | |
Willy Tarreau | 04ff9f1 | 2013-06-10 18:39:42 +0200 | [diff] [blame] | 10645 | /* Fetch an HTTP header. A pointer to the beginning of the value is returned. |
| 10646 | * Accepts an optional argument of type string containing the header field name, |
| 10647 | * and an optional argument of type signed or unsigned integer to request an |
| 10648 | * 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] | 10649 | * headers are considered from the first one. |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 10650 | */ |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 10651 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10652 | 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] | 10653 | { |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 10654 | struct hdr_idx *idx; |
Willy Tarreau | a890d07 | 2013-04-02 12:01:06 +0200 | [diff] [blame] | 10655 | struct hdr_ctx *ctx = smp->ctx.a[0]; |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 10656 | const struct http_msg *msg; |
Willy Tarreau | 185b5c4 | 2012-04-26 15:11:51 +0200 | [diff] [blame] | 10657 | int occ = 0; |
| 10658 | const char *name_str = NULL; |
| 10659 | int name_len = 0; |
Willy Tarreau | e333ec9 | 2012-04-16 16:26:40 +0200 | [diff] [blame] | 10660 | |
Willy Tarreau | a890d07 | 2013-04-02 12:01:06 +0200 | [diff] [blame] | 10661 | if (!ctx) { |
| 10662 | /* first call */ |
| 10663 | ctx = &static_hdr_ctx; |
| 10664 | ctx->idx = 0; |
Willy Tarreau | ffb6f08 | 2013-04-02 23:16:53 +0200 | [diff] [blame] | 10665 | smp->ctx.a[0] = ctx; |
Willy Tarreau | a890d07 | 2013-04-02 12:01:06 +0200 | [diff] [blame] | 10666 | } |
| 10667 | |
Willy Tarreau | 185b5c4 | 2012-04-26 15:11:51 +0200 | [diff] [blame] | 10668 | if (args) { |
| 10669 | if (args[0].type != ARGT_STR) |
| 10670 | return 0; |
| 10671 | name_str = args[0].data.str.str; |
| 10672 | name_len = args[0].data.str.len; |
| 10673 | |
| 10674 | if (args[1].type == ARGT_UINT || args[1].type == ARGT_SINT) |
| 10675 | occ = args[1].data.uint; |
| 10676 | } |
Willy Tarreau | 34db108 | 2012-04-19 17:16:54 +0200 | [diff] [blame] | 10677 | |
Willy Tarreau | e333ec9 | 2012-04-16 16:26:40 +0200 | [diff] [blame] | 10678 | CHECK_HTTP_MESSAGE_FIRST(); |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 10679 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 10680 | idx = &smp->strm->txn->hdr_idx; |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10681 | 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] | 10682 | |
Willy Tarreau | 185b5c4 | 2012-04-26 15:11:51 +0200 | [diff] [blame] | 10683 | if (ctx && !(smp->flags & SMP_F_NOT_LAST)) |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 10684 | /* search for header from the beginning */ |
| 10685 | ctx->idx = 0; |
| 10686 | |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10687 | if (!occ && !(smp->opt & SMP_OPT_ITERATE)) |
Willy Tarreau | 185b5c4 | 2012-04-26 15:11:51 +0200 | [diff] [blame] | 10688 | /* no explicit occurrence and single fetch => last header by default */ |
| 10689 | occ = -1; |
| 10690 | |
| 10691 | if (!occ) |
| 10692 | /* prepare to report multiple occurrences for ACL fetches */ |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 10693 | smp->flags |= SMP_F_NOT_LAST; |
Willy Tarreau | 664092c | 2011-12-16 19:11:42 +0100 | [diff] [blame] | 10694 | |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 10695 | smp->type = SMP_T_STR; |
| 10696 | smp->flags |= SMP_F_VOL_HDR | SMP_F_CONST; |
Willy Tarreau | 185b5c4 | 2012-04-26 15:11:51 +0200 | [diff] [blame] | 10697 | if (http_get_hdr(msg, name_str, name_len, idx, occ, ctx, &smp->data.str.str, &smp->data.str.len)) |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 10698 | return 1; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 10699 | |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 10700 | smp->flags &= ~SMP_F_NOT_LAST; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 10701 | return 0; |
| 10702 | } |
| 10703 | |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 10704 | /* 6. Check on HTTP header count. The number of occurrences is returned. |
Willy Tarreau | 34db108 | 2012-04-19 17:16:54 +0200 | [diff] [blame] | 10705 | * Accepts exactly 1 argument of type string. |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 10706 | */ |
| 10707 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10708 | 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] | 10709 | { |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 10710 | struct hdr_idx *idx; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 10711 | struct hdr_ctx ctx; |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 10712 | const struct http_msg *msg; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 10713 | int cnt; |
Willy Tarreau | 601a4d1 | 2015-04-01 19:16:09 +0200 | [diff] [blame] | 10714 | const char *name = NULL; |
| 10715 | int len = 0; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 10716 | |
Willy Tarreau | 601a4d1 | 2015-04-01 19:16:09 +0200 | [diff] [blame] | 10717 | if (args && args->type == ARGT_STR) { |
| 10718 | name = args->data.str.str; |
| 10719 | len = args->data.str.len; |
| 10720 | } |
Willy Tarreau | 34db108 | 2012-04-19 17:16:54 +0200 | [diff] [blame] | 10721 | |
Willy Tarreau | e333ec9 | 2012-04-16 16:26:40 +0200 | [diff] [blame] | 10722 | CHECK_HTTP_MESSAGE_FIRST(); |
| 10723 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 10724 | idx = &smp->strm->txn->hdr_idx; |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10725 | 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] | 10726 | |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 10727 | ctx.idx = 0; |
| 10728 | cnt = 0; |
Willy Tarreau | 601a4d1 | 2015-04-01 19:16:09 +0200 | [diff] [blame] | 10729 | while (http_find_header2(name, len, msg->chn->buf->p, idx, &ctx)) |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 10730 | cnt++; |
| 10731 | |
Willy Tarreau | f853c46 | 2012-04-23 18:53:56 +0200 | [diff] [blame] | 10732 | smp->type = SMP_T_UINT; |
| 10733 | smp->data.uint = cnt; |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 10734 | smp->flags = SMP_F_VOL_HDR; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 10735 | return 1; |
| 10736 | } |
| 10737 | |
Willy Tarreau | 185b5c4 | 2012-04-26 15:11:51 +0200 | [diff] [blame] | 10738 | /* Fetch an HTTP header's integer value. The integer value is returned. It |
| 10739 | * takes a mandatory argument of type string and an optional one of type int |
| 10740 | * to designate a specific occurrence. It returns an unsigned integer, which |
| 10741 | * may or may not be appropriate for everything. |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 10742 | */ |
| 10743 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10744 | 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] | 10745 | { |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10746 | int ret = smp_fetch_hdr(args, smp, kw, private); |
Willy Tarreau | e333ec9 | 2012-04-16 16:26:40 +0200 | [diff] [blame] | 10747 | |
Willy Tarreau | f853c46 | 2012-04-23 18:53:56 +0200 | [diff] [blame] | 10748 | if (ret > 0) { |
| 10749 | smp->type = SMP_T_UINT; |
| 10750 | smp->data.uint = strl2ic(smp->data.str.str, smp->data.str.len); |
| 10751 | } |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 10752 | |
Willy Tarreau | d53e242 | 2012-04-16 17:21:11 +0200 | [diff] [blame] | 10753 | return ret; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 10754 | } |
| 10755 | |
Cyril Bonté | 69fa992 | 2012-10-25 00:01:06 +0200 | [diff] [blame] | 10756 | /* Fetch an HTTP header's IP value. takes a mandatory argument of type string |
| 10757 | * and an optional one of type int to designate a specific occurrence. |
| 10758 | * It returns an IPv4 or IPv6 address. |
Willy Tarreau | 106f979 | 2009-09-19 07:54:16 +0200 | [diff] [blame] | 10759 | */ |
| 10760 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10761 | 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] | 10762 | { |
Willy Tarreau | d53e242 | 2012-04-16 17:21:11 +0200 | [diff] [blame] | 10763 | int ret; |
Willy Tarreau | e333ec9 | 2012-04-16 16:26:40 +0200 | [diff] [blame] | 10764 | |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10765 | while ((ret = smp_fetch_hdr(args, smp, kw, private)) > 0) { |
Cyril Bonté | 69fa992 | 2012-10-25 00:01:06 +0200 | [diff] [blame] | 10766 | if (url2ipv4((char *)smp->data.str.str, &smp->data.ipv4)) { |
| 10767 | smp->type = SMP_T_IPV4; |
Willy Tarreau | d53e242 | 2012-04-16 17:21:11 +0200 | [diff] [blame] | 10768 | break; |
Cyril Bonté | 69fa992 | 2012-10-25 00:01:06 +0200 | [diff] [blame] | 10769 | } else { |
Willy Tarreau | 47ca545 | 2012-12-23 20:22:19 +0100 | [diff] [blame] | 10770 | struct chunk *temp = get_trash_chunk(); |
Cyril Bonté | 69fa992 | 2012-10-25 00:01:06 +0200 | [diff] [blame] | 10771 | if (smp->data.str.len < temp->size - 1) { |
| 10772 | memcpy(temp->str, smp->data.str.str, smp->data.str.len); |
| 10773 | temp->str[smp->data.str.len] = '\0'; |
| 10774 | if (inet_pton(AF_INET6, temp->str, &smp->data.ipv6)) { |
| 10775 | smp->type = SMP_T_IPV6; |
| 10776 | break; |
| 10777 | } |
| 10778 | } |
| 10779 | } |
| 10780 | |
Willy Tarreau | d53e242 | 2012-04-16 17:21:11 +0200 | [diff] [blame] | 10781 | /* if the header doesn't match an IP address, fetch next one */ |
Willy Tarreau | 185b5c4 | 2012-04-26 15:11:51 +0200 | [diff] [blame] | 10782 | if (!(smp->flags & SMP_F_NOT_LAST)) |
| 10783 | return 0; |
Willy Tarreau | 106f979 | 2009-09-19 07:54:16 +0200 | [diff] [blame] | 10784 | } |
Willy Tarreau | d53e242 | 2012-04-16 17:21:11 +0200 | [diff] [blame] | 10785 | return ret; |
Willy Tarreau | 106f979 | 2009-09-19 07:54:16 +0200 | [diff] [blame] | 10786 | } |
| 10787 | |
Willy Tarreau | 737b0c1 | 2007-06-10 21:28:46 +0200 | [diff] [blame] | 10788 | /* 8. Check on URI PATH. A pointer to the PATH is stored. The path starts at |
| 10789 | * the first '/' after the possible hostname, and ends before the possible '?'. |
| 10790 | */ |
| 10791 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10792 | 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] | 10793 | { |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 10794 | struct http_txn *txn; |
Willy Tarreau | 737b0c1 | 2007-06-10 21:28:46 +0200 | [diff] [blame] | 10795 | char *ptr, *end; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 10796 | |
Willy Tarreau | c0239e0 | 2012-04-16 14:42:55 +0200 | [diff] [blame] | 10797 | CHECK_HTTP_MESSAGE_FIRST(); |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 10798 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 10799 | txn = smp->strm->txn; |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 10800 | 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] | 10801 | ptr = http_get_path(txn); |
| 10802 | if (!ptr) |
Willy Tarreau | 737b0c1 | 2007-06-10 21:28:46 +0200 | [diff] [blame] | 10803 | return 0; |
| 10804 | |
| 10805 | /* OK, we got the '/' ! */ |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 10806 | smp->type = SMP_T_STR; |
Willy Tarreau | f853c46 | 2012-04-23 18:53:56 +0200 | [diff] [blame] | 10807 | smp->data.str.str = ptr; |
Willy Tarreau | 737b0c1 | 2007-06-10 21:28:46 +0200 | [diff] [blame] | 10808 | |
| 10809 | while (ptr < end && *ptr != '?') |
| 10810 | ptr++; |
| 10811 | |
Willy Tarreau | f853c46 | 2012-04-23 18:53:56 +0200 | [diff] [blame] | 10812 | smp->data.str.len = ptr - smp->data.str.str; |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 10813 | smp->flags = SMP_F_VOL_1ST | SMP_F_CONST; |
Willy Tarreau | 737b0c1 | 2007-06-10 21:28:46 +0200 | [diff] [blame] | 10814 | return 1; |
| 10815 | } |
| 10816 | |
Willy Tarreau | a7ad50c | 2012-04-29 15:39:40 +0200 | [diff] [blame] | 10817 | /* This produces a concatenation of the first occurrence of the Host header |
| 10818 | * followed by the path component if it begins with a slash ('/'). This means |
| 10819 | * that '*' will not be added, resulting in exactly the first Host entry. |
| 10820 | * If no Host header is found, then the path is returned as-is. The returned |
| 10821 | * 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] | 10822 | * The returned sample is of type string. |
Willy Tarreau | a7ad50c | 2012-04-29 15:39:40 +0200 | [diff] [blame] | 10823 | */ |
| 10824 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10825 | 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] | 10826 | { |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 10827 | struct http_txn *txn; |
Willy Tarreau | a7ad50c | 2012-04-29 15:39:40 +0200 | [diff] [blame] | 10828 | char *ptr, *end, *beg; |
| 10829 | struct hdr_ctx ctx; |
Willy Tarreau | 3caf2af | 2014-06-24 17:27:02 +0200 | [diff] [blame] | 10830 | struct chunk *temp; |
Willy Tarreau | a7ad50c | 2012-04-29 15:39:40 +0200 | [diff] [blame] | 10831 | |
| 10832 | CHECK_HTTP_MESSAGE_FIRST(); |
| 10833 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 10834 | txn = smp->strm->txn; |
Willy Tarreau | a7ad50c | 2012-04-29 15:39:40 +0200 | [diff] [blame] | 10835 | ctx.idx = 0; |
Willy Tarreau | 877e78d | 2013-04-07 18:48:08 +0200 | [diff] [blame] | 10836 | 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] | 10837 | return smp_fetch_path(args, smp, kw, private); |
Willy Tarreau | a7ad50c | 2012-04-29 15:39:40 +0200 | [diff] [blame] | 10838 | |
| 10839 | /* 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] | 10840 | temp = get_trash_chunk(); |
| 10841 | memcpy(temp->str, ctx.line + ctx.val, ctx.vlen); |
Willy Tarreau | a7ad50c | 2012-04-29 15:39:40 +0200 | [diff] [blame] | 10842 | smp->type = SMP_T_STR; |
Willy Tarreau | 3caf2af | 2014-06-24 17:27:02 +0200 | [diff] [blame] | 10843 | smp->data.str.str = temp->str; |
Willy Tarreau | a7ad50c | 2012-04-29 15:39:40 +0200 | [diff] [blame] | 10844 | smp->data.str.len = ctx.vlen; |
| 10845 | |
| 10846 | /* now retrieve the path */ |
Willy Tarreau | 877e78d | 2013-04-07 18:48:08 +0200 | [diff] [blame] | 10847 | 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] | 10848 | beg = http_get_path(txn); |
| 10849 | if (!beg) |
| 10850 | beg = end; |
| 10851 | |
| 10852 | for (ptr = beg; ptr < end && *ptr != '?'; ptr++); |
| 10853 | |
| 10854 | if (beg < ptr && *beg == '/') { |
| 10855 | memcpy(smp->data.str.str + smp->data.str.len, beg, ptr - beg); |
| 10856 | smp->data.str.len += ptr - beg; |
| 10857 | } |
| 10858 | |
| 10859 | smp->flags = SMP_F_VOL_1ST; |
| 10860 | return 1; |
| 10861 | } |
| 10862 | |
Willy Tarreau | ab1f7b7 | 2012-12-09 13:38:54 +0100 | [diff] [blame] | 10863 | /* This produces a 32-bit hash of the concatenation of the first occurrence of |
| 10864 | * the Host header followed by the path component if it begins with a slash ('/'). |
| 10865 | * This means that '*' will not be added, resulting in exactly the first Host |
| 10866 | * 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] | 10867 | * is hashed using the path hash followed by a full avalanche hash and provides a |
| 10868 | * 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] | 10869 | * high-traffic sites without having to store whole paths. |
| 10870 | */ |
Thierry FOURNIER | 055b9d5 | 2014-07-15 16:11:07 +0200 | [diff] [blame] | 10871 | int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10872 | 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] | 10873 | { |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 10874 | struct http_txn *txn; |
Willy Tarreau | ab1f7b7 | 2012-12-09 13:38:54 +0100 | [diff] [blame] | 10875 | struct hdr_ctx ctx; |
| 10876 | unsigned int hash = 0; |
| 10877 | char *ptr, *beg, *end; |
| 10878 | int len; |
| 10879 | |
| 10880 | CHECK_HTTP_MESSAGE_FIRST(); |
| 10881 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 10882 | txn = smp->strm->txn; |
Willy Tarreau | ab1f7b7 | 2012-12-09 13:38:54 +0100 | [diff] [blame] | 10883 | ctx.idx = 0; |
Willy Tarreau | 877e78d | 2013-04-07 18:48:08 +0200 | [diff] [blame] | 10884 | 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] | 10885 | /* OK we have the header value in ctx.line+ctx.val for ctx.vlen bytes */ |
| 10886 | ptr = ctx.line + ctx.val; |
| 10887 | len = ctx.vlen; |
| 10888 | while (len--) |
| 10889 | hash = *(ptr++) + (hash << 6) + (hash << 16) - hash; |
| 10890 | } |
| 10891 | |
| 10892 | /* now retrieve the path */ |
Willy Tarreau | 877e78d | 2013-04-07 18:48:08 +0200 | [diff] [blame] | 10893 | 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] | 10894 | beg = http_get_path(txn); |
| 10895 | if (!beg) |
| 10896 | beg = end; |
| 10897 | |
| 10898 | for (ptr = beg; ptr < end && *ptr != '?'; ptr++); |
| 10899 | |
| 10900 | if (beg < ptr && *beg == '/') { |
| 10901 | while (beg < ptr) |
| 10902 | hash = *(beg++) + (hash << 6) + (hash << 16) - hash; |
| 10903 | } |
| 10904 | hash = full_hash(hash); |
| 10905 | |
| 10906 | smp->type = SMP_T_UINT; |
| 10907 | smp->data.uint = hash; |
| 10908 | smp->flags = SMP_F_VOL_1ST; |
| 10909 | return 1; |
| 10910 | } |
| 10911 | |
Willy Tarreau | 4a55060 | 2012-12-09 14:53:32 +0100 | [diff] [blame] | 10912 | /* 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] | 10913 | * path as returned by smp_fetch_base32(). The idea is to have per-source and |
| 10914 | * per-path counters. The result is a binary block from 8 to 20 bytes depending |
| 10915 | * 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] | 10916 | * that in environments where IPv6 is insignificant, truncating the output to |
| 10917 | * 8 bytes would still work. |
| 10918 | */ |
| 10919 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10920 | 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] | 10921 | { |
Willy Tarreau | 47ca545 | 2012-12-23 20:22:19 +0100 | [diff] [blame] | 10922 | struct chunk *temp; |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 10923 | struct connection *cli_conn = objt_conn(smp->sess->origin); |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 10924 | |
| 10925 | if (!cli_conn) |
| 10926 | return 0; |
Willy Tarreau | 4a55060 | 2012-12-09 14:53:32 +0100 | [diff] [blame] | 10927 | |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10928 | if (!smp_fetch_base32(args, smp, kw, private)) |
Willy Tarreau | 4a55060 | 2012-12-09 14:53:32 +0100 | [diff] [blame] | 10929 | return 0; |
| 10930 | |
Willy Tarreau | 47ca545 | 2012-12-23 20:22:19 +0100 | [diff] [blame] | 10931 | temp = get_trash_chunk(); |
Willy Tarreau | 5ad6e1d | 2014-07-15 21:34:06 +0200 | [diff] [blame] | 10932 | *(unsigned int *)temp->str = htonl(smp->data.uint); |
| 10933 | temp->len += sizeof(unsigned int); |
Willy Tarreau | 4a55060 | 2012-12-09 14:53:32 +0100 | [diff] [blame] | 10934 | |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 10935 | switch (cli_conn->addr.from.ss_family) { |
Willy Tarreau | 4a55060 | 2012-12-09 14:53:32 +0100 | [diff] [blame] | 10936 | case AF_INET: |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 10937 | 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] | 10938 | temp->len += 4; |
| 10939 | break; |
| 10940 | case AF_INET6: |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 10941 | 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] | 10942 | temp->len += 16; |
| 10943 | break; |
| 10944 | default: |
| 10945 | return 0; |
| 10946 | } |
| 10947 | |
| 10948 | smp->data.str = *temp; |
| 10949 | smp->type = SMP_T_BIN; |
| 10950 | return 1; |
| 10951 | } |
| 10952 | |
Willy Tarreau | 49ad95c | 2015-01-19 15:06:26 +0100 | [diff] [blame] | 10953 | /* Extracts the query string, which comes after the question mark '?'. If no |
| 10954 | * question mark is found, nothing is returned. Otherwise it returns a sample |
| 10955 | * of type string carrying the whole query string. |
| 10956 | */ |
| 10957 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10958 | 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] | 10959 | { |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 10960 | struct http_txn *txn; |
Willy Tarreau | 49ad95c | 2015-01-19 15:06:26 +0100 | [diff] [blame] | 10961 | char *ptr, *end; |
| 10962 | |
| 10963 | CHECK_HTTP_MESSAGE_FIRST(); |
| 10964 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 10965 | txn = smp->strm->txn; |
Willy Tarreau | 49ad95c | 2015-01-19 15:06:26 +0100 | [diff] [blame] | 10966 | ptr = txn->req.chn->buf->p + txn->req.sl.rq.u; |
| 10967 | end = ptr + txn->req.sl.rq.u_l; |
| 10968 | |
| 10969 | /* look up the '?' */ |
| 10970 | do { |
| 10971 | if (ptr == end) |
| 10972 | return 0; |
| 10973 | } while (*ptr++ != '?'); |
| 10974 | |
| 10975 | smp->type = SMP_T_STR; |
| 10976 | smp->data.str.str = ptr; |
| 10977 | smp->data.str.len = end - ptr; |
| 10978 | smp->flags = SMP_F_VOL_1ST | SMP_F_CONST; |
| 10979 | return 1; |
| 10980 | } |
| 10981 | |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 10982 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10983 | 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] | 10984 | { |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 10985 | /* Note: hdr_idx.v cannot be NULL in this ACL because the ACL is tagged |
| 10986 | * as a layer7 ACL, which involves automatic allocation of hdr_idx. |
| 10987 | */ |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 10988 | |
Willy Tarreau | 24e32d8 | 2012-04-23 23:55:44 +0200 | [diff] [blame] | 10989 | CHECK_HTTP_MESSAGE_FIRST_PERM(); |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 10990 | |
Willy Tarreau | f853c46 | 2012-04-23 18:53:56 +0200 | [diff] [blame] | 10991 | smp->type = SMP_T_BOOL; |
Willy Tarreau | 197e10a | 2012-04-23 19:18:42 +0200 | [diff] [blame] | 10992 | smp->data.uint = 1; |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 10993 | return 1; |
| 10994 | } |
| 10995 | |
Willy Tarreau | 7f18e52 | 2010-10-22 20:04:13 +0200 | [diff] [blame] | 10996 | /* return a valid test if the current request is the first one on the connection */ |
| 10997 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10998 | 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] | 10999 | { |
Willy Tarreau | f853c46 | 2012-04-23 18:53:56 +0200 | [diff] [blame] | 11000 | smp->type = SMP_T_BOOL; |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 11001 | smp->data.uint = !(smp->strm->txn->flags & TX_NOT_FIRST); |
Willy Tarreau | 7f18e52 | 2010-10-22 20:04:13 +0200 | [diff] [blame] | 11002 | return 1; |
| 11003 | } |
| 11004 | |
Willy Tarreau | 34db108 | 2012-04-19 17:16:54 +0200 | [diff] [blame] | 11005 | /* Accepts exactly 1 argument of type userlist */ |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 11006 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 11007 | 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] | 11008 | { |
| 11009 | |
Willy Tarreau | 24e32d8 | 2012-04-23 23:55:44 +0200 | [diff] [blame] | 11010 | if (!args || args->type != ARGT_USR) |
Willy Tarreau | 34db108 | 2012-04-19 17:16:54 +0200 | [diff] [blame] | 11011 | return 0; |
| 11012 | |
Willy Tarreau | c0239e0 | 2012-04-16 14:42:55 +0200 | [diff] [blame] | 11013 | CHECK_HTTP_MESSAGE_FIRST(); |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 11014 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 11015 | if (!get_http_auth(smp->strm)) |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 11016 | return 0; |
| 11017 | |
Willy Tarreau | f853c46 | 2012-04-23 18:53:56 +0200 | [diff] [blame] | 11018 | smp->type = SMP_T_BOOL; |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 11019 | smp->data.uint = check_user(args->data.usr, smp->strm->txn->auth.user, |
| 11020 | smp->strm->txn->auth.pass); |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 11021 | return 1; |
| 11022 | } |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 11023 | |
Willy Tarreau | 4a3fd4c | 2012-05-10 23:18:26 +0200 | [diff] [blame] | 11024 | /* Accepts exactly 1 argument of type userlist */ |
| 11025 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 11026 | 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] | 11027 | { |
Willy Tarreau | 4a3fd4c | 2012-05-10 23:18:26 +0200 | [diff] [blame] | 11028 | if (!args || args->type != ARGT_USR) |
| 11029 | return 0; |
| 11030 | |
| 11031 | CHECK_HTTP_MESSAGE_FIRST(); |
| 11032 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 11033 | if (!get_http_auth(smp->strm)) |
Willy Tarreau | 4a3fd4c | 2012-05-10 23:18:26 +0200 | [diff] [blame] | 11034 | return 0; |
| 11035 | |
Willy Tarreau | 4a3fd4c | 2012-05-10 23:18:26 +0200 | [diff] [blame] | 11036 | /* if the user does not belong to the userlist or has a wrong password, |
| 11037 | * report that it unconditionally does not match. Otherwise we return |
Thierry FOURNIER | 9eec0a6 | 2014-01-22 18:38:02 +0100 | [diff] [blame] | 11038 | * a string containing the username. |
Willy Tarreau | 4a3fd4c | 2012-05-10 23:18:26 +0200 | [diff] [blame] | 11039 | */ |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 11040 | if (!check_user(args->data.usr, smp->strm->txn->auth.user, |
| 11041 | smp->strm->txn->auth.pass)) |
Thierry FOURNIER | 9eec0a6 | 2014-01-22 18:38:02 +0100 | [diff] [blame] | 11042 | return 0; |
| 11043 | |
| 11044 | /* pat_match_auth() will need the user list */ |
| 11045 | smp->ctx.a[0] = args->data.usr; |
| 11046 | |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 11047 | smp->type = SMP_T_STR; |
| 11048 | smp->flags = SMP_F_CONST; |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 11049 | smp->data.str.str = smp->strm->txn->auth.user; |
| 11050 | smp->data.str.len = strlen(smp->strm->txn->auth.user); |
Willy Tarreau | 4a3fd4c | 2012-05-10 23:18:26 +0200 | [diff] [blame] | 11051 | |
| 11052 | return 1; |
| 11053 | } |
| 11054 | |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 11055 | /* Try to find the next occurrence of a cookie name in a cookie header value. |
| 11056 | * The lookup begins at <hdr>. The pointer and size of the next occurrence of |
| 11057 | * the cookie value is returned into *value and *value_l, and the function |
| 11058 | * returns a pointer to the next pointer to search from if the value was found. |
| 11059 | * Otherwise if the cookie was not found, NULL is returned and neither value |
| 11060 | * nor value_l are touched. The input <hdr> string should first point to the |
| 11061 | * header's value, and the <hdr_end> pointer must point to the first character |
| 11062 | * not part of the value. <list> must be non-zero if value may represent a list |
| 11063 | * of values (cookie headers). This makes it faster to abort parsing when no |
| 11064 | * list is expected. |
| 11065 | */ |
| 11066 | static char * |
| 11067 | extract_cookie_value(char *hdr, const char *hdr_end, |
| 11068 | char *cookie_name, size_t cookie_name_l, int list, |
Willy Tarreau | 3fb818c | 2012-04-11 17:21:08 +0200 | [diff] [blame] | 11069 | char **value, int *value_l) |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 11070 | { |
| 11071 | char *equal, *att_end, *att_beg, *val_beg, *val_end; |
| 11072 | char *next; |
| 11073 | |
| 11074 | /* we search at least a cookie name followed by an equal, and more |
| 11075 | * generally something like this : |
| 11076 | * Cookie: NAME1 = VALUE 1 ; NAME2 = VALUE2 ; NAME3 = VALUE3\r\n |
| 11077 | */ |
| 11078 | for (att_beg = hdr; att_beg + cookie_name_l + 1 < hdr_end; att_beg = next + 1) { |
| 11079 | /* Iterate through all cookies on this line */ |
| 11080 | |
| 11081 | while (att_beg < hdr_end && http_is_spht[(unsigned char)*att_beg]) |
| 11082 | att_beg++; |
| 11083 | |
| 11084 | /* find att_end : this is the first character after the last non |
| 11085 | * space before the equal. It may be equal to hdr_end. |
| 11086 | */ |
| 11087 | equal = att_end = att_beg; |
| 11088 | |
| 11089 | while (equal < hdr_end) { |
| 11090 | if (*equal == '=' || *equal == ';' || (list && *equal == ',')) |
| 11091 | break; |
| 11092 | if (http_is_spht[(unsigned char)*equal++]) |
| 11093 | continue; |
| 11094 | att_end = equal; |
| 11095 | } |
| 11096 | |
| 11097 | /* here, <equal> points to '=', a delimitor or the end. <att_end> |
| 11098 | * is between <att_beg> and <equal>, both may be identical. |
| 11099 | */ |
| 11100 | |
| 11101 | /* look for end of cookie if there is an equal sign */ |
| 11102 | if (equal < hdr_end && *equal == '=') { |
| 11103 | /* look for the beginning of the value */ |
| 11104 | val_beg = equal + 1; |
| 11105 | while (val_beg < hdr_end && http_is_spht[(unsigned char)*val_beg]) |
| 11106 | val_beg++; |
| 11107 | |
| 11108 | /* find the end of the value, respecting quotes */ |
| 11109 | next = find_cookie_value_end(val_beg, hdr_end); |
| 11110 | |
| 11111 | /* make val_end point to the first white space or delimitor after the value */ |
| 11112 | val_end = next; |
| 11113 | while (val_end > val_beg && http_is_spht[(unsigned char)*(val_end - 1)]) |
| 11114 | val_end--; |
| 11115 | } else { |
| 11116 | val_beg = val_end = next = equal; |
| 11117 | } |
| 11118 | |
| 11119 | /* We have nothing to do with attributes beginning with '$'. However, |
| 11120 | * they will automatically be removed if a header before them is removed, |
| 11121 | * since they're supposed to be linked together. |
| 11122 | */ |
| 11123 | if (*att_beg == '$') |
| 11124 | continue; |
| 11125 | |
| 11126 | /* Ignore cookies with no equal sign */ |
| 11127 | if (equal == next) |
| 11128 | continue; |
| 11129 | |
| 11130 | /* Now we have the cookie name between att_beg and att_end, and |
| 11131 | * its value between val_beg and val_end. |
| 11132 | */ |
| 11133 | |
| 11134 | if (att_end - att_beg == cookie_name_l && |
| 11135 | memcmp(att_beg, cookie_name, cookie_name_l) == 0) { |
| 11136 | /* let's return this value and indicate where to go on from */ |
| 11137 | *value = val_beg; |
| 11138 | *value_l = val_end - val_beg; |
| 11139 | return next + 1; |
| 11140 | } |
| 11141 | |
| 11142 | /* Set-Cookie headers only have the name in the first attr=value part */ |
| 11143 | if (!list) |
| 11144 | break; |
| 11145 | } |
| 11146 | |
| 11147 | return NULL; |
| 11148 | } |
| 11149 | |
William Lallemand | a43ba4e | 2014-01-28 18:14:25 +0100 | [diff] [blame] | 11150 | /* Fetch a captured HTTP request header. The index is the position of |
| 11151 | * the "capture" option in the configuration file |
| 11152 | */ |
| 11153 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 11154 | 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] | 11155 | { |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 11156 | struct proxy *fe = strm_fe(smp->strm); |
William Lallemand | a43ba4e | 2014-01-28 18:14:25 +0100 | [diff] [blame] | 11157 | int idx; |
| 11158 | |
| 11159 | if (!args || args->type != ARGT_UINT) |
| 11160 | return 0; |
| 11161 | |
| 11162 | idx = args->data.uint; |
| 11163 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 11164 | 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] | 11165 | return 0; |
| 11166 | |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 11167 | smp->type = SMP_T_STR; |
| 11168 | smp->flags |= SMP_F_CONST; |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 11169 | smp->data.str.str = smp->strm->req_cap[idx]; |
| 11170 | smp->data.str.len = strlen(smp->strm->req_cap[idx]); |
William Lallemand | a43ba4e | 2014-01-28 18:14:25 +0100 | [diff] [blame] | 11171 | |
| 11172 | return 1; |
| 11173 | } |
| 11174 | |
| 11175 | /* Fetch a captured HTTP response header. The index is the position of |
| 11176 | * the "capture" option in the configuration file |
| 11177 | */ |
| 11178 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 11179 | 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] | 11180 | { |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 11181 | struct proxy *fe = strm_fe(smp->strm); |
William Lallemand | a43ba4e | 2014-01-28 18:14:25 +0100 | [diff] [blame] | 11182 | int idx; |
| 11183 | |
| 11184 | if (!args || args->type != ARGT_UINT) |
| 11185 | return 0; |
| 11186 | |
| 11187 | idx = args->data.uint; |
| 11188 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 11189 | 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] | 11190 | return 0; |
| 11191 | |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 11192 | smp->type = SMP_T_STR; |
| 11193 | smp->flags |= SMP_F_CONST; |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 11194 | smp->data.str.str = smp->strm->res_cap[idx]; |
| 11195 | smp->data.str.len = strlen(smp->strm->res_cap[idx]); |
William Lallemand | a43ba4e | 2014-01-28 18:14:25 +0100 | [diff] [blame] | 11196 | |
| 11197 | return 1; |
| 11198 | } |
| 11199 | |
William Lallemand | 65ad6e1 | 2014-01-31 15:08:02 +0100 | [diff] [blame] | 11200 | /* Extracts the METHOD in the HTTP request, the txn->uri should be filled before the call */ |
| 11201 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 11202 | 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] | 11203 | { |
| 11204 | struct chunk *temp; |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 11205 | struct http_txn *txn = smp->strm->txn; |
William Lallemand | 96a7785 | 2014-02-05 00:30:02 +0100 | [diff] [blame] | 11206 | char *ptr; |
William Lallemand | 65ad6e1 | 2014-01-31 15:08:02 +0100 | [diff] [blame] | 11207 | |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 11208 | if (!txn || !txn->uri) |
William Lallemand | 65ad6e1 | 2014-01-31 15:08:02 +0100 | [diff] [blame] | 11209 | return 0; |
| 11210 | |
William Lallemand | 96a7785 | 2014-02-05 00:30:02 +0100 | [diff] [blame] | 11211 | ptr = txn->uri; |
William Lallemand | 65ad6e1 | 2014-01-31 15:08:02 +0100 | [diff] [blame] | 11212 | |
William Lallemand | 96a7785 | 2014-02-05 00:30:02 +0100 | [diff] [blame] | 11213 | while (*ptr != ' ' && *ptr != '\0') /* find first space */ |
| 11214 | ptr++; |
William Lallemand | 65ad6e1 | 2014-01-31 15:08:02 +0100 | [diff] [blame] | 11215 | |
William Lallemand | 96a7785 | 2014-02-05 00:30:02 +0100 | [diff] [blame] | 11216 | temp = get_trash_chunk(); |
| 11217 | temp->str = txn->uri; |
| 11218 | temp->len = ptr - txn->uri; |
William Lallemand | 65ad6e1 | 2014-01-31 15:08:02 +0100 | [diff] [blame] | 11219 | smp->data.str = *temp; |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 11220 | smp->type = SMP_T_STR; |
| 11221 | smp->flags = SMP_F_CONST; |
William Lallemand | 65ad6e1 | 2014-01-31 15:08:02 +0100 | [diff] [blame] | 11222 | |
| 11223 | return 1; |
| 11224 | |
| 11225 | } |
| 11226 | |
| 11227 | /* Extracts the path in the HTTP request, the txn->uri should be filled before the call */ |
| 11228 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 11229 | 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] | 11230 | { |
| 11231 | struct chunk *temp; |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 11232 | struct http_txn *txn = smp->strm->txn; |
William Lallemand | 65ad6e1 | 2014-01-31 15:08:02 +0100 | [diff] [blame] | 11233 | char *ptr; |
William Lallemand | 65ad6e1 | 2014-01-31 15:08:02 +0100 | [diff] [blame] | 11234 | |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 11235 | if (!txn || !txn->uri) |
William Lallemand | 65ad6e1 | 2014-01-31 15:08:02 +0100 | [diff] [blame] | 11236 | return 0; |
William Lallemand | 96a7785 | 2014-02-05 00:30:02 +0100 | [diff] [blame] | 11237 | |
William Lallemand | 65ad6e1 | 2014-01-31 15:08:02 +0100 | [diff] [blame] | 11238 | ptr = txn->uri; |
| 11239 | |
| 11240 | while (*ptr != ' ' && *ptr != '\0') /* find first space */ |
| 11241 | ptr++; |
William Lallemand | 96a7785 | 2014-02-05 00:30:02 +0100 | [diff] [blame] | 11242 | |
William Lallemand | 65ad6e1 | 2014-01-31 15:08:02 +0100 | [diff] [blame] | 11243 | if (!*ptr) |
| 11244 | return 0; |
| 11245 | |
| 11246 | ptr++; /* skip the space */ |
| 11247 | |
| 11248 | temp = get_trash_chunk(); |
William Lallemand | 96a7785 | 2014-02-05 00:30:02 +0100 | [diff] [blame] | 11249 | ptr = temp->str = http_get_path_from_string(ptr); |
William Lallemand | 65ad6e1 | 2014-01-31 15:08:02 +0100 | [diff] [blame] | 11250 | if (!ptr) |
| 11251 | return 0; |
| 11252 | while (*ptr != ' ' && *ptr != '\0') /* find space after URI */ |
| 11253 | ptr++; |
William Lallemand | 65ad6e1 | 2014-01-31 15:08:02 +0100 | [diff] [blame] | 11254 | |
| 11255 | smp->data.str = *temp; |
William Lallemand | 96a7785 | 2014-02-05 00:30:02 +0100 | [diff] [blame] | 11256 | smp->data.str.len = ptr - temp->str; |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 11257 | smp->type = SMP_T_STR; |
| 11258 | smp->flags = SMP_F_CONST; |
William Lallemand | 65ad6e1 | 2014-01-31 15:08:02 +0100 | [diff] [blame] | 11259 | |
| 11260 | return 1; |
| 11261 | } |
| 11262 | |
Willy Tarreau | 3c1b5ec | 2014-04-24 23:41:57 +0200 | [diff] [blame] | 11263 | /* Retrieves the HTTP version from the request (either 1.0 or 1.1) and emits it |
| 11264 | * as a string (either "HTTP/1.0" or "HTTP/1.1"). |
| 11265 | */ |
| 11266 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 11267 | 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] | 11268 | { |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 11269 | struct http_txn *txn = smp->strm->txn; |
Willy Tarreau | 3c1b5ec | 2014-04-24 23:41:57 +0200 | [diff] [blame] | 11270 | |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 11271 | if (!txn || txn->req.msg_state < HTTP_MSG_HDR_FIRST) |
Willy Tarreau | 3c1b5ec | 2014-04-24 23:41:57 +0200 | [diff] [blame] | 11272 | return 0; |
| 11273 | |
| 11274 | if (txn->req.flags & HTTP_MSGF_VER_11) |
| 11275 | smp->data.str.str = "HTTP/1.1"; |
| 11276 | else |
| 11277 | smp->data.str.str = "HTTP/1.0"; |
| 11278 | |
| 11279 | smp->data.str.len = 8; |
| 11280 | smp->type = SMP_T_STR; |
| 11281 | smp->flags = SMP_F_CONST; |
| 11282 | return 1; |
| 11283 | |
| 11284 | } |
| 11285 | |
| 11286 | /* Retrieves the HTTP version from the response (either 1.0 or 1.1) and emits it |
| 11287 | * as a string (either "HTTP/1.0" or "HTTP/1.1"). |
| 11288 | */ |
| 11289 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 11290 | 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] | 11291 | { |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 11292 | struct http_txn *txn = smp->strm->txn; |
Willy Tarreau | 3c1b5ec | 2014-04-24 23:41:57 +0200 | [diff] [blame] | 11293 | |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 11294 | if (!txn || txn->rsp.msg_state < HTTP_MSG_HDR_FIRST) |
Willy Tarreau | 3c1b5ec | 2014-04-24 23:41:57 +0200 | [diff] [blame] | 11295 | return 0; |
| 11296 | |
| 11297 | if (txn->rsp.flags & HTTP_MSGF_VER_11) |
| 11298 | smp->data.str.str = "HTTP/1.1"; |
| 11299 | else |
| 11300 | smp->data.str.str = "HTTP/1.0"; |
| 11301 | |
| 11302 | smp->data.str.len = 8; |
| 11303 | smp->type = SMP_T_STR; |
| 11304 | smp->flags = SMP_F_CONST; |
| 11305 | return 1; |
| 11306 | |
| 11307 | } |
| 11308 | |
William Lallemand | 65ad6e1 | 2014-01-31 15:08:02 +0100 | [diff] [blame] | 11309 | |
Willy Tarreau | e333ec9 | 2012-04-16 16:26:40 +0200 | [diff] [blame] | 11310 | /* 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] | 11311 | * 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] | 11312 | * 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] | 11313 | * 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] | 11314 | * 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] | 11315 | * Accepts exactly 1 argument of type string. If the input options indicate |
| 11316 | * 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] | 11317 | * The returned sample is of type CSTR. Can be used to parse cookies in other |
| 11318 | * files. |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 11319 | */ |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 11320 | 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] | 11321 | { |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 11322 | struct http_txn *txn; |
| 11323 | struct hdr_idx *idx; |
Willy Tarreau | a890d07 | 2013-04-02 12:01:06 +0200 | [diff] [blame] | 11324 | struct hdr_ctx *ctx = smp->ctx.a[2]; |
Willy Tarreau | e333ec9 | 2012-04-16 16:26:40 +0200 | [diff] [blame] | 11325 | const struct http_msg *msg; |
| 11326 | const char *hdr_name; |
| 11327 | int hdr_name_len; |
| 11328 | char *sol; |
Willy Tarreau | 28376d6 | 2012-04-26 21:26:10 +0200 | [diff] [blame] | 11329 | int occ = 0; |
| 11330 | int found = 0; |
Willy Tarreau | e333ec9 | 2012-04-16 16:26:40 +0200 | [diff] [blame] | 11331 | |
Willy Tarreau | 24e32d8 | 2012-04-23 23:55:44 +0200 | [diff] [blame] | 11332 | if (!args || args->type != ARGT_STR) |
Willy Tarreau | 34db108 | 2012-04-19 17:16:54 +0200 | [diff] [blame] | 11333 | return 0; |
| 11334 | |
Willy Tarreau | a890d07 | 2013-04-02 12:01:06 +0200 | [diff] [blame] | 11335 | if (!ctx) { |
| 11336 | /* first call */ |
| 11337 | ctx = &static_hdr_ctx; |
| 11338 | ctx->idx = 0; |
| 11339 | smp->ctx.a[2] = ctx; |
| 11340 | } |
| 11341 | |
Willy Tarreau | e333ec9 | 2012-04-16 16:26:40 +0200 | [diff] [blame] | 11342 | CHECK_HTTP_MESSAGE_FIRST(); |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 11343 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 11344 | txn = smp->strm->txn; |
| 11345 | idx = &smp->strm->txn->hdr_idx; |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 11346 | |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 11347 | if ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) { |
Willy Tarreau | e333ec9 | 2012-04-16 16:26:40 +0200 | [diff] [blame] | 11348 | msg = &txn->req; |
| 11349 | hdr_name = "Cookie"; |
| 11350 | hdr_name_len = 6; |
| 11351 | } else { |
| 11352 | msg = &txn->rsp; |
| 11353 | hdr_name = "Set-Cookie"; |
| 11354 | hdr_name_len = 10; |
| 11355 | } |
| 11356 | |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 11357 | if (!occ && !(smp->opt & SMP_OPT_ITERATE)) |
Willy Tarreau | 28376d6 | 2012-04-26 21:26:10 +0200 | [diff] [blame] | 11358 | /* no explicit occurrence and single fetch => last cookie by default */ |
| 11359 | occ = -1; |
| 11360 | |
| 11361 | /* OK so basically here, either we want only one value and it's the |
| 11362 | * last one, or we want to iterate over all of them and we fetch the |
| 11363 | * next one. |
| 11364 | */ |
| 11365 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 11366 | sol = msg->chn->buf->p; |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 11367 | if (!(smp->flags & SMP_F_NOT_LAST)) { |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 11368 | /* search for the header from the beginning, we must first initialize |
| 11369 | * the search parameters. |
| 11370 | */ |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 11371 | smp->ctx.a[0] = NULL; |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 11372 | ctx->idx = 0; |
| 11373 | } |
| 11374 | |
Willy Tarreau | 28376d6 | 2012-04-26 21:26:10 +0200 | [diff] [blame] | 11375 | smp->flags |= SMP_F_VOL_HDR; |
| 11376 | |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 11377 | while (1) { |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 11378 | /* Note: smp->ctx.a[0] == NULL every time we need to fetch a new header */ |
| 11379 | if (!smp->ctx.a[0]) { |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 11380 | if (!http_find_header2(hdr_name, hdr_name_len, sol, idx, ctx)) |
| 11381 | goto out; |
| 11382 | |
Willy Tarreau | 24e32d8 | 2012-04-23 23:55:44 +0200 | [diff] [blame] | 11383 | if (ctx->vlen < args->data.str.len + 1) |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 11384 | continue; |
| 11385 | |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 11386 | smp->ctx.a[0] = ctx->line + ctx->val; |
| 11387 | smp->ctx.a[1] = smp->ctx.a[0] + ctx->vlen; |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 11388 | } |
| 11389 | |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 11390 | smp->type = SMP_T_STR; |
| 11391 | smp->flags |= SMP_F_CONST; |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 11392 | 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] | 11393 | args->data.str.str, args->data.str.len, |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 11394 | (smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ, |
Willy Tarreau | f853c46 | 2012-04-23 18:53:56 +0200 | [diff] [blame] | 11395 | &smp->data.str.str, |
| 11396 | &smp->data.str.len); |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 11397 | if (smp->ctx.a[0]) { |
Willy Tarreau | 28376d6 | 2012-04-26 21:26:10 +0200 | [diff] [blame] | 11398 | found = 1; |
| 11399 | if (occ >= 0) { |
| 11400 | /* one value was returned into smp->data.str.{str,len} */ |
| 11401 | smp->flags |= SMP_F_NOT_LAST; |
| 11402 | return 1; |
| 11403 | } |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 11404 | } |
Willy Tarreau | 28376d6 | 2012-04-26 21:26:10 +0200 | [diff] [blame] | 11405 | /* if we're looking for last occurrence, let's loop */ |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 11406 | } |
Willy Tarreau | 28376d6 | 2012-04-26 21:26:10 +0200 | [diff] [blame] | 11407 | /* all cookie headers and values were scanned. If we're looking for the |
| 11408 | * last occurrence, we may return it now. |
| 11409 | */ |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 11410 | out: |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 11411 | smp->flags &= ~SMP_F_NOT_LAST; |
Willy Tarreau | 28376d6 | 2012-04-26 21:26:10 +0200 | [diff] [blame] | 11412 | return found; |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 11413 | } |
| 11414 | |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 11415 | /* 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] | 11416 | * 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] | 11417 | * multiple cookies may be parsed on the same line. The returned sample is of |
| 11418 | * type UINT. Accepts exactly 1 argument of type string. |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 11419 | */ |
| 11420 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 11421 | 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] | 11422 | { |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 11423 | struct http_txn *txn; |
| 11424 | struct hdr_idx *idx; |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 11425 | struct hdr_ctx ctx; |
Willy Tarreau | e333ec9 | 2012-04-16 16:26:40 +0200 | [diff] [blame] | 11426 | const struct http_msg *msg; |
| 11427 | const char *hdr_name; |
| 11428 | int hdr_name_len; |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 11429 | int cnt; |
| 11430 | char *val_beg, *val_end; |
Willy Tarreau | e333ec9 | 2012-04-16 16:26:40 +0200 | [diff] [blame] | 11431 | char *sol; |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 11432 | |
Willy Tarreau | 24e32d8 | 2012-04-23 23:55:44 +0200 | [diff] [blame] | 11433 | if (!args || args->type != ARGT_STR) |
Willy Tarreau | 34db108 | 2012-04-19 17:16:54 +0200 | [diff] [blame] | 11434 | return 0; |
| 11435 | |
Willy Tarreau | e333ec9 | 2012-04-16 16:26:40 +0200 | [diff] [blame] | 11436 | CHECK_HTTP_MESSAGE_FIRST(); |
| 11437 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 11438 | txn = smp->strm->txn; |
| 11439 | idx = &smp->strm->txn->hdr_idx; |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 11440 | |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 11441 | if ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) { |
Willy Tarreau | e333ec9 | 2012-04-16 16:26:40 +0200 | [diff] [blame] | 11442 | msg = &txn->req; |
| 11443 | hdr_name = "Cookie"; |
| 11444 | hdr_name_len = 6; |
| 11445 | } else { |
| 11446 | msg = &txn->rsp; |
| 11447 | hdr_name = "Set-Cookie"; |
| 11448 | hdr_name_len = 10; |
| 11449 | } |
| 11450 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 11451 | sol = msg->chn->buf->p; |
Willy Tarreau | 46787ed | 2012-04-11 17:28:40 +0200 | [diff] [blame] | 11452 | val_end = val_beg = NULL; |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 11453 | ctx.idx = 0; |
| 11454 | cnt = 0; |
| 11455 | |
| 11456 | while (1) { |
| 11457 | /* Note: val_beg == NULL every time we need to fetch a new header */ |
| 11458 | if (!val_beg) { |
| 11459 | if (!http_find_header2(hdr_name, hdr_name_len, sol, idx, &ctx)) |
| 11460 | break; |
| 11461 | |
Willy Tarreau | 24e32d8 | 2012-04-23 23:55:44 +0200 | [diff] [blame] | 11462 | if (ctx.vlen < args->data.str.len + 1) |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 11463 | continue; |
| 11464 | |
| 11465 | val_beg = ctx.line + ctx.val; |
| 11466 | val_end = val_beg + ctx.vlen; |
| 11467 | } |
| 11468 | |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 11469 | smp->type = SMP_T_STR; |
| 11470 | smp->flags |= SMP_F_CONST; |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 11471 | while ((val_beg = extract_cookie_value(val_beg, val_end, |
Willy Tarreau | 24e32d8 | 2012-04-23 23:55:44 +0200 | [diff] [blame] | 11472 | args->data.str.str, args->data.str.len, |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 11473 | (smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ, |
Willy Tarreau | f853c46 | 2012-04-23 18:53:56 +0200 | [diff] [blame] | 11474 | &smp->data.str.str, |
| 11475 | &smp->data.str.len))) { |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 11476 | cnt++; |
| 11477 | } |
| 11478 | } |
| 11479 | |
Willy Tarreau | b169eba | 2013-12-16 15:14:43 +0100 | [diff] [blame] | 11480 | smp->type = SMP_T_UINT; |
Willy Tarreau | f853c46 | 2012-04-23 18:53:56 +0200 | [diff] [blame] | 11481 | smp->data.uint = cnt; |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 11482 | smp->flags |= SMP_F_VOL_HDR; |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 11483 | return 1; |
| 11484 | } |
| 11485 | |
Willy Tarreau | 5153936 | 2012-05-08 12:46:28 +0200 | [diff] [blame] | 11486 | /* Fetch an cookie's integer value. The integer value is returned. It |
| 11487 | * takes a mandatory argument of type string. It relies on smp_fetch_cookie(). |
| 11488 | */ |
| 11489 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 11490 | 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] | 11491 | { |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 11492 | int ret = smp_fetch_cookie(args, smp, kw, private); |
Willy Tarreau | 5153936 | 2012-05-08 12:46:28 +0200 | [diff] [blame] | 11493 | |
| 11494 | if (ret > 0) { |
| 11495 | smp->type = SMP_T_UINT; |
| 11496 | smp->data.uint = strl2ic(smp->data.str.str, smp->data.str.len); |
| 11497 | } |
| 11498 | |
| 11499 | return ret; |
| 11500 | } |
| 11501 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 11502 | /************************************************************************/ |
Willy Tarreau | 1278578 | 2012-04-27 21:37:17 +0200 | [diff] [blame] | 11503 | /* The code below is dedicated to sample fetches */ |
Willy Tarreau | 4a56897 | 2010-05-12 08:08:50 +0200 | [diff] [blame] | 11504 | /************************************************************************/ |
| 11505 | |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 11506 | /* |
| 11507 | * Given a path string and its length, find the position of beginning of the |
| 11508 | * query string. Returns NULL if no query string is found in the path. |
| 11509 | * |
| 11510 | * Example: if path = "/foo/bar/fubar?yo=mama;ye=daddy", and n = 22: |
| 11511 | * |
Willy Tarreau | 1ede1da | 2015-05-07 16:06:18 +0200 | [diff] [blame] | 11512 | * find_query_string(path, n, '?') points to "yo=mama;ye=daddy" string. |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 11513 | */ |
bedis | 4c75cca | 2012-10-05 08:38:24 +0200 | [diff] [blame] | 11514 | 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] | 11515 | { |
| 11516 | char *p; |
Emeric Brun | 485479d | 2010-09-23 18:02:19 +0200 | [diff] [blame] | 11517 | |
bedis | 4c75cca | 2012-10-05 08:38:24 +0200 | [diff] [blame] | 11518 | p = memchr(path, delim, path_l); |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 11519 | return p ? p + 1 : NULL; |
| 11520 | } |
| 11521 | |
bedis | 4c75cca | 2012-10-05 08:38:24 +0200 | [diff] [blame] | 11522 | static inline int is_param_delimiter(char c, char delim) |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 11523 | { |
bedis | 4c75cca | 2012-10-05 08:38:24 +0200 | [diff] [blame] | 11524 | return c == '&' || c == ';' || c == delim; |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 11525 | } |
| 11526 | |
Thierry FOURNIER | 8be451c | 2015-05-20 15:28:12 +0200 | [diff] [blame] | 11527 | /* after increasing a pointer value, it can exceed the first buffer |
| 11528 | * size. This function transform the value of <ptr> according with |
| 11529 | * the expected position. <chunks> is an array of the one or two |
| 11530 | * avalaible chunks. The first value is the start of the first chunk, |
| 11531 | * the second value if the end+1 of the first chunks. The third value |
| 11532 | * is NULL or the start of the second chunk and the fourth value is |
| 11533 | * the end+1 of the second chunk. The function returns 1 if does a |
| 11534 | * wrap, else returns 0. |
| 11535 | */ |
| 11536 | static inline int fix_pointer_if_wrap(const char **chunks, const char **ptr) |
| 11537 | { |
| 11538 | if (*ptr < chunks[1]) |
| 11539 | return 0; |
| 11540 | if (!chunks[2]) |
| 11541 | return 0; |
| 11542 | *ptr = chunks[2] + ( *ptr - chunks[1] ); |
| 11543 | return 1; |
| 11544 | } |
| 11545 | |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 11546 | /* |
| 11547 | * Given a url parameter, find the starting position of the first occurence, |
| 11548 | * or NULL if the parameter is not found. |
| 11549 | * |
| 11550 | * Example: if query_string is "yo=mama;ye=daddy" and url_param_name is "ye", |
| 11551 | * the function will return query_string+8. |
Thierry FOURNIER | 8be451c | 2015-05-20 15:28:12 +0200 | [diff] [blame] | 11552 | * |
| 11553 | * Warning:this function returns a pointer that can be point to the first chunk |
| 11554 | * or the second chunk. The caller must be check the position before using the |
| 11555 | * result. |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 11556 | */ |
Thierry FOURNIER | 8be451c | 2015-05-20 15:28:12 +0200 | [diff] [blame] | 11557 | static const char * |
| 11558 | find_url_param_pos(const char **chunks, |
Willy Tarreau | 1ede1da | 2015-05-07 16:06:18 +0200 | [diff] [blame] | 11559 | const char* url_param_name, size_t url_param_name_l, |
bedis | 4c75cca | 2012-10-05 08:38:24 +0200 | [diff] [blame] | 11560 | char delim) |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 11561 | { |
Thierry FOURNIER | 8be451c | 2015-05-20 15:28:12 +0200 | [diff] [blame] | 11562 | const char *pos, *last, *equal; |
| 11563 | const char **bufs = chunks; |
| 11564 | int l1, l2; |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 11565 | |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 11566 | |
Thierry FOURNIER | 8be451c | 2015-05-20 15:28:12 +0200 | [diff] [blame] | 11567 | pos = bufs[0]; |
| 11568 | last = bufs[1]; |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 11569 | while (pos <= last) { |
Thierry FOURNIER | 8be451c | 2015-05-20 15:28:12 +0200 | [diff] [blame] | 11570 | /* Check the equal. */ |
| 11571 | equal = pos + url_param_name_l; |
| 11572 | if (fix_pointer_if_wrap(chunks, &equal)) { |
| 11573 | if (equal >= chunks[3]) |
| 11574 | return NULL; |
| 11575 | } else { |
| 11576 | if (equal >= chunks[1]) |
| 11577 | return NULL; |
| 11578 | } |
| 11579 | if (*equal == '=') { |
| 11580 | if (pos + url_param_name_l > last) { |
| 11581 | /* process wrap case, we detect a wrap. In this case, the |
| 11582 | * comparison is performed in two parts. |
| 11583 | */ |
| 11584 | |
| 11585 | /* This is the end, we dont have any other chunk. */ |
| 11586 | if (bufs != chunks || !bufs[2]) |
| 11587 | return NULL; |
| 11588 | |
| 11589 | /* Compute the length of each part of the comparison. */ |
| 11590 | l1 = last - pos; |
| 11591 | l2 = url_param_name_l - l1; |
| 11592 | |
| 11593 | /* The second buffer is too short to contain the compared string. */ |
| 11594 | if (bufs[2] + l2 > bufs[3]) |
| 11595 | return NULL; |
| 11596 | |
| 11597 | if (memcmp(pos, url_param_name, l1) == 0 && |
| 11598 | memcmp(bufs[2], url_param_name+l1, l2) == 0) |
| 11599 | return pos; |
| 11600 | |
| 11601 | /* Perform wrapping and jump the string who fail the comparison. */ |
| 11602 | bufs += 2; |
| 11603 | pos = bufs[0] + l2; |
| 11604 | last = bufs[1]; |
| 11605 | |
| 11606 | } else { |
| 11607 | /* process a simple comparison. */ |
| 11608 | if (memcmp(pos, url_param_name, url_param_name_l) == 0) { |
| 11609 | return pos; } |
| 11610 | pos += url_param_name_l + 1; |
| 11611 | if (fix_pointer_if_wrap(chunks, &pos)) |
| 11612 | last = bufs[2]; |
| 11613 | } |
| 11614 | } |
| 11615 | |
| 11616 | while (1) { |
| 11617 | /* Look for the next delimiter. */ |
| 11618 | while (pos <= last && !is_param_delimiter(*pos, delim)) |
| 11619 | pos++; |
| 11620 | if (pos < last) |
| 11621 | break; |
| 11622 | /* process buffer wrapping. */ |
| 11623 | if (bufs != chunks || !bufs[2]) |
| 11624 | return NULL; |
| 11625 | bufs += 2; |
| 11626 | pos = bufs[0]; |
| 11627 | last = bufs[1]; |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 11628 | } |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 11629 | pos++; |
| 11630 | } |
| 11631 | return NULL; |
| 11632 | } |
| 11633 | |
| 11634 | /* |
Willy Tarreau | 1ede1da | 2015-05-07 16:06:18 +0200 | [diff] [blame] | 11635 | * Given a url parameter name and a query string, returns its value and size |
| 11636 | * into *value and *value_l respectively, and returns non-zero. An empty |
| 11637 | * url_param_name matches the first available parameter. If the parameter is |
| 11638 | * not found, zero is returned and value/value_l are not touched. |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 11639 | */ |
| 11640 | static int |
Thierry FOURNIER | 8be451c | 2015-05-20 15:28:12 +0200 | [diff] [blame] | 11641 | find_next_url_param(const char **chunks, |
Willy Tarreau | 1ede1da | 2015-05-07 16:06:18 +0200 | [diff] [blame] | 11642 | const char* url_param_name, size_t url_param_name_l, |
Thierry FOURNIER | 8be451c | 2015-05-20 15:28:12 +0200 | [diff] [blame] | 11643 | const char **vstart, const char **vend, char delim) |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 11644 | { |
Thierry FOURNIER | 8be451c | 2015-05-20 15:28:12 +0200 | [diff] [blame] | 11645 | const char *arg_start, *qs_end; |
| 11646 | const char *value_start, *value_end; |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 11647 | |
Thierry FOURNIER | 8be451c | 2015-05-20 15:28:12 +0200 | [diff] [blame] | 11648 | arg_start = chunks[0]; |
| 11649 | qs_end = chunks[1]; |
Willy Tarreau | 1ede1da | 2015-05-07 16:06:18 +0200 | [diff] [blame] | 11650 | if (url_param_name_l) { |
Thierry FOURNIER | 8be451c | 2015-05-20 15:28:12 +0200 | [diff] [blame] | 11651 | /* Looks for an argument name. */ |
| 11652 | arg_start = find_url_param_pos(chunks, |
Willy Tarreau | 1ede1da | 2015-05-07 16:06:18 +0200 | [diff] [blame] | 11653 | url_param_name, url_param_name_l, |
| 11654 | delim); |
Thierry FOURNIER | 8be451c | 2015-05-20 15:28:12 +0200 | [diff] [blame] | 11655 | /* Check for wrapping. */ |
| 11656 | if (arg_start > qs_end) |
| 11657 | qs_end = chunks[3]; |
Willy Tarreau | 1ede1da | 2015-05-07 16:06:18 +0200 | [diff] [blame] | 11658 | } |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 11659 | if (!arg_start) |
| 11660 | return 0; |
| 11661 | |
Willy Tarreau | 1ede1da | 2015-05-07 16:06:18 +0200 | [diff] [blame] | 11662 | if (!url_param_name_l) { |
Thierry FOURNIER | 8be451c | 2015-05-20 15:28:12 +0200 | [diff] [blame] | 11663 | while (1) { |
| 11664 | /* looks for the first argument. */ |
| 11665 | value_start = memchr(arg_start, '=', qs_end - arg_start); |
| 11666 | if (!value_start) { |
| 11667 | |
| 11668 | /* Check for wrapping. */ |
| 11669 | if (arg_start >= chunks[0] && |
| 11670 | arg_start <= chunks[1] && |
| 11671 | chunks[2]) { |
| 11672 | arg_start = chunks[2]; |
| 11673 | qs_end = chunks[3]; |
| 11674 | continue; |
| 11675 | } |
| 11676 | return 0; |
| 11677 | } |
| 11678 | break; |
| 11679 | } |
Willy Tarreau | 1ede1da | 2015-05-07 16:06:18 +0200 | [diff] [blame] | 11680 | value_start++; |
| 11681 | } |
Thierry FOURNIER | 8be451c | 2015-05-20 15:28:12 +0200 | [diff] [blame] | 11682 | else { |
| 11683 | /* Jump the argument length. */ |
Willy Tarreau | 1ede1da | 2015-05-07 16:06:18 +0200 | [diff] [blame] | 11684 | value_start = arg_start + url_param_name_l + 1; |
| 11685 | |
Thierry FOURNIER | 8be451c | 2015-05-20 15:28:12 +0200 | [diff] [blame] | 11686 | /* Check for pointer wrapping. */ |
| 11687 | if (fix_pointer_if_wrap(chunks, &value_start)) { |
| 11688 | /* Update the end pointer. */ |
| 11689 | qs_end = chunks[3]; |
| 11690 | |
| 11691 | /* Check for overflow. */ |
| 11692 | if (value_start > qs_end) |
| 11693 | return 0; |
| 11694 | } |
| 11695 | } |
| 11696 | |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 11697 | value_end = value_start; |
| 11698 | |
Thierry FOURNIER | 8be451c | 2015-05-20 15:28:12 +0200 | [diff] [blame] | 11699 | while (1) { |
| 11700 | while ((value_end < qs_end) && !is_param_delimiter(*value_end, delim)) |
| 11701 | value_end++; |
| 11702 | if (value_end < qs_end) |
| 11703 | break; |
| 11704 | /* process buffer wrapping. */ |
| 11705 | if (value_end >= chunks[0] && |
| 11706 | value_end <= chunks[1] && |
| 11707 | chunks[2]) { |
| 11708 | value_end = chunks[2]; |
| 11709 | qs_end = chunks[3]; |
| 11710 | continue; |
| 11711 | } |
| 11712 | break; |
| 11713 | } |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 11714 | |
Thierry FOURNIER | 8be451c | 2015-05-20 15:28:12 +0200 | [diff] [blame] | 11715 | *vstart = value_start; |
| 11716 | *vend = value_end; |
Willy Tarreau | 0013433 | 2011-01-04 14:57:34 +0100 | [diff] [blame] | 11717 | return value_end != value_start; |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 11718 | } |
| 11719 | |
Thierry FOURNIER | 8be451c | 2015-05-20 15:28:12 +0200 | [diff] [blame] | 11720 | /* This scans a URL-encoded query string. It takes an optionally wrapping |
| 11721 | * string whose first contigous chunk has its beginning in ctx->a[0] and end |
| 11722 | * in ctx->a[1], and the optional second part in (ctx->a[2]..ctx->a[3]). The |
| 11723 | * pointers are updated for next iteration before leaving. |
Thierry FOURNIER | 4fdc74c | 2015-05-19 14:46:23 +0200 | [diff] [blame] | 11724 | */ |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 11725 | static int |
Thierry FOURNIER | 4fdc74c | 2015-05-19 14:46:23 +0200 | [diff] [blame] | 11726 | 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] | 11727 | { |
Thierry FOURNIER | 8be451c | 2015-05-20 15:28:12 +0200 | [diff] [blame] | 11728 | const char *vstart, *vend; |
| 11729 | struct chunk *temp; |
| 11730 | const char **chunks = (const char **)smp->ctx.a; |
bedis | 4c75cca | 2012-10-05 08:38:24 +0200 | [diff] [blame] | 11731 | |
Thierry FOURNIER | 8be451c | 2015-05-20 15:28:12 +0200 | [diff] [blame] | 11732 | if (!find_next_url_param(chunks, |
Thierry FOURNIER | 0948d41 | 2015-05-20 15:22:37 +0200 | [diff] [blame] | 11733 | name, name_len, |
Thierry FOURNIER | 8be451c | 2015-05-20 15:28:12 +0200 | [diff] [blame] | 11734 | &vstart, &vend, |
Thierry FOURNIER | 0948d41 | 2015-05-20 15:22:37 +0200 | [diff] [blame] | 11735 | delim)) |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 11736 | return 0; |
| 11737 | |
Thierry FOURNIER | 8be451c | 2015-05-20 15:28:12 +0200 | [diff] [blame] | 11738 | /* Create sample. If the value is contiguous, return the pointer as CONST, |
| 11739 | * if the value is wrapped, copy-it in a buffer. |
| 11740 | */ |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 11741 | smp->type = SMP_T_STR; |
Thierry FOURNIER | 8be451c | 2015-05-20 15:28:12 +0200 | [diff] [blame] | 11742 | if (chunks[2] && |
| 11743 | vstart >= chunks[0] && vstart <= chunks[1] && |
| 11744 | vend >= chunks[2] && vend <= chunks[3]) { |
| 11745 | /* Wrapped case. */ |
| 11746 | temp = get_trash_chunk(); |
| 11747 | memcpy(temp->str, vstart, chunks[1] - vstart); |
| 11748 | memcpy(temp->str + ( chunks[1] - vstart ), chunks[2], vend - chunks[2]); |
| 11749 | smp->data.str.str = temp->str; |
| 11750 | smp->data.str.len = ( chunks[1] - vstart ) + ( vend - chunks[2] ); |
| 11751 | } else { |
| 11752 | /* Contiguous case. */ |
| 11753 | smp->data.str.str = (char *)vstart; |
| 11754 | smp->data.str.len = vend - vstart; |
| 11755 | smp->flags = SMP_F_VOL_1ST | SMP_F_CONST; |
| 11756 | } |
| 11757 | |
| 11758 | /* Update context, check wrapping. */ |
| 11759 | chunks[0] = vend; |
| 11760 | if (chunks[2] && vend >= chunks[2] && vend <= chunks[3]) { |
| 11761 | chunks[1] = chunks[3]; |
| 11762 | chunks[2] = NULL; |
| 11763 | } |
Willy Tarreau | 1ede1da | 2015-05-07 16:06:18 +0200 | [diff] [blame] | 11764 | |
Thierry FOURNIER | 8be451c | 2015-05-20 15:28:12 +0200 | [diff] [blame] | 11765 | if (chunks[0] < chunks[1]) |
Willy Tarreau | 1ede1da | 2015-05-07 16:06:18 +0200 | [diff] [blame] | 11766 | smp->flags |= SMP_F_NOT_LAST; |
| 11767 | |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 11768 | return 1; |
| 11769 | } |
| 11770 | |
Thierry FOURNIER | 4fdc74c | 2015-05-19 14:46:23 +0200 | [diff] [blame] | 11771 | /* This function iterates over each parameter of the query string. It uses |
| 11772 | * 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] | 11773 | * parameter. Since it uses smp_fetch_param(), ctx->a[2..3] are both NULL. |
| 11774 | * An optional parameter name is passed in args[0], otherwise any parameter is |
| 11775 | * considered. It supports an optional delimiter argument for the beginning of |
| 11776 | * the string in args[1], which defaults to "?". |
Thierry FOURNIER | 4fdc74c | 2015-05-19 14:46:23 +0200 | [diff] [blame] | 11777 | */ |
| 11778 | static int |
| 11779 | smp_fetch_url_param(const struct arg *args, struct sample *smp, const char *kw, void *private) |
| 11780 | { |
| 11781 | struct http_msg *msg; |
| 11782 | char delim = '?'; |
| 11783 | const char *name; |
| 11784 | int name_len; |
| 11785 | |
Dragan Dosen | 26f77e5 | 2015-05-25 10:02:11 +0200 | [diff] [blame] | 11786 | if (!args || |
| 11787 | (args[0].type && args[0].type != ARGT_STR) || |
| 11788 | (args[1].type && args[1].type != ARGT_STR)) |
| 11789 | return 0; |
Thierry FOURNIER | 4fdc74c | 2015-05-19 14:46:23 +0200 | [diff] [blame] | 11790 | |
Dragan Dosen | 26f77e5 | 2015-05-25 10:02:11 +0200 | [diff] [blame] | 11791 | name = ""; |
| 11792 | name_len = 0; |
| 11793 | if (args->type == ARGT_STR) { |
| 11794 | name = args->data.str.str; |
| 11795 | name_len = args->data.str.len; |
| 11796 | } |
Thierry FOURNIER | 4fdc74c | 2015-05-19 14:46:23 +0200 | [diff] [blame] | 11797 | |
Dragan Dosen | 26f77e5 | 2015-05-25 10:02:11 +0200 | [diff] [blame] | 11798 | if (args[1].type) |
| 11799 | delim = *args[1].data.str.str; |
Thierry FOURNIER | 4fdc74c | 2015-05-19 14:46:23 +0200 | [diff] [blame] | 11800 | |
Dragan Dosen | 26f77e5 | 2015-05-25 10:02:11 +0200 | [diff] [blame] | 11801 | if (!smp->ctx.a[0]) { // first call, find the query string |
Thierry FOURNIER | 4fdc74c | 2015-05-19 14:46:23 +0200 | [diff] [blame] | 11802 | CHECK_HTTP_MESSAGE_FIRST(); |
| 11803 | |
| 11804 | msg = &smp->strm->txn->req; |
| 11805 | |
| 11806 | smp->ctx.a[0] = find_param_list(msg->chn->buf->p + msg->sl.rq.u, |
| 11807 | msg->sl.rq.u_l, delim); |
| 11808 | if (!smp->ctx.a[0]) |
| 11809 | return 0; |
| 11810 | |
| 11811 | 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] | 11812 | |
| 11813 | /* Assume that the context is filled with NULL pointer |
| 11814 | * before the first call. |
| 11815 | * smp->ctx.a[2] = NULL; |
| 11816 | * smp->ctx.a[3] = NULL; |
| 11817 | */ |
Thierry FOURNIER | 4fdc74c | 2015-05-19 14:46:23 +0200 | [diff] [blame] | 11818 | } |
| 11819 | |
| 11820 | return smp_fetch_param(delim, name, name_len, args, smp, kw, private); |
| 11821 | } |
| 11822 | |
Thierry FOURNIER | e28c499 | 2015-05-19 14:45:09 +0200 | [diff] [blame] | 11823 | /* This function iterates over each parameter of the body. This requires |
| 11824 | * 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] | 11825 | * ctx->a[0] and ctx->a[1] to store the beginning and end of the first |
| 11826 | * contigous part of the body, and optionally ctx->a[2..3] to reference the |
| 11827 | * optional second part if the body wraps at the end of the buffer. An optional |
| 11828 | * parameter name is passed in args[0], otherwise any parameter is considered. |
Thierry FOURNIER | e28c499 | 2015-05-19 14:45:09 +0200 | [diff] [blame] | 11829 | */ |
| 11830 | static int |
| 11831 | smp_fetch_body_param(const struct arg *args, struct sample *smp, const char *kw, void *private) |
| 11832 | { |
| 11833 | struct http_txn *txn = smp->strm->txn; |
| 11834 | struct http_msg *msg; |
| 11835 | unsigned long len; |
| 11836 | unsigned long block1; |
| 11837 | char *body; |
| 11838 | const char *name; |
| 11839 | int name_len; |
| 11840 | |
| 11841 | if (!args || (args[0].type && args[0].type != ARGT_STR)) |
| 11842 | return 0; |
| 11843 | |
| 11844 | name = ""; |
| 11845 | name_len = 0; |
| 11846 | if (args[0].type == ARGT_STR) { |
| 11847 | name = args[0].data.str.str; |
| 11848 | name_len = args[0].data.str.len; |
| 11849 | } |
| 11850 | |
| 11851 | if (!smp->ctx.a[0]) { // first call, find the query string |
| 11852 | CHECK_HTTP_MESSAGE_FIRST(); |
| 11853 | |
| 11854 | if ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) |
| 11855 | msg = &txn->req; |
| 11856 | else |
| 11857 | msg = &txn->rsp; |
| 11858 | |
| 11859 | len = http_body_bytes(msg); |
| 11860 | body = b_ptr(msg->chn->buf, -http_data_rewind(msg)); |
| 11861 | |
| 11862 | block1 = len; |
| 11863 | if (block1 > msg->chn->buf->data + msg->chn->buf->size - body) |
| 11864 | block1 = msg->chn->buf->data + msg->chn->buf->size - body; |
| 11865 | |
| 11866 | if (block1 == len) { |
| 11867 | /* buffer is not wrapped (or empty) */ |
| 11868 | smp->ctx.a[0] = body; |
| 11869 | smp->ctx.a[1] = body + len; |
Thierry FOURNIER | 8be451c | 2015-05-20 15:28:12 +0200 | [diff] [blame] | 11870 | |
| 11871 | /* Assume that the context is filled with NULL pointer |
| 11872 | * before the first call. |
| 11873 | * smp->ctx.a[2] = NULL; |
| 11874 | * smp->ctx.a[3] = NULL; |
| 11875 | */ |
Thierry FOURNIER | e28c499 | 2015-05-19 14:45:09 +0200 | [diff] [blame] | 11876 | } |
| 11877 | else { |
| 11878 | /* buffer is wrapped, we need to defragment it */ |
| 11879 | smp->ctx.a[0] = body; |
| 11880 | smp->ctx.a[1] = body + block1; |
Thierry FOURNIER | 8be451c | 2015-05-20 15:28:12 +0200 | [diff] [blame] | 11881 | smp->ctx.a[2] = msg->chn->buf->data; |
| 11882 | smp->ctx.a[3] = msg->chn->buf->data + ( len - block1 ); |
Thierry FOURNIER | e28c499 | 2015-05-19 14:45:09 +0200 | [diff] [blame] | 11883 | } |
| 11884 | } |
| 11885 | return smp_fetch_param('&', name, name_len, args, smp, kw, private); |
| 11886 | } |
| 11887 | |
Willy Tarreau | a9fddca | 2012-07-31 07:51:48 +0200 | [diff] [blame] | 11888 | /* Return the signed integer value for the specified url parameter (see url_param |
| 11889 | * above). |
| 11890 | */ |
| 11891 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 11892 | 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] | 11893 | { |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 11894 | int ret = smp_fetch_url_param(args, smp, kw, private); |
Willy Tarreau | a9fddca | 2012-07-31 07:51:48 +0200 | [diff] [blame] | 11895 | |
| 11896 | if (ret > 0) { |
| 11897 | smp->type = SMP_T_UINT; |
| 11898 | smp->data.uint = strl2ic(smp->data.str.str, smp->data.str.len); |
| 11899 | } |
| 11900 | |
| 11901 | return ret; |
| 11902 | } |
| 11903 | |
Neil - HAProxy List | 39c63c5 | 2013-11-04 13:48:42 +0000 | [diff] [blame] | 11904 | /* This produces a 32-bit hash of the concatenation of the first occurrence of |
| 11905 | * the Host header followed by the path component if it begins with a slash ('/'). |
| 11906 | * This means that '*' will not be added, resulting in exactly the first Host |
| 11907 | * entry. If no Host header is found, then the path is used. The resulting value |
| 11908 | * is hashed using the url hash followed by a full avalanche hash and provides a |
| 11909 | * 32-bit integer value. This fetch is useful for tracking per-URL activity on |
| 11910 | * high-traffic sites without having to store whole paths. |
| 11911 | * this differs from the base32 functions in that it includes the url parameters |
| 11912 | * as well as the path |
| 11913 | */ |
| 11914 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 11915 | 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] | 11916 | { |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 11917 | struct http_txn *txn; |
Neil - HAProxy List | 39c63c5 | 2013-11-04 13:48:42 +0000 | [diff] [blame] | 11918 | struct hdr_ctx ctx; |
| 11919 | unsigned int hash = 0; |
| 11920 | char *ptr, *beg, *end; |
| 11921 | int len; |
| 11922 | |
| 11923 | CHECK_HTTP_MESSAGE_FIRST(); |
| 11924 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 11925 | txn = smp->strm->txn; |
Neil - HAProxy List | 39c63c5 | 2013-11-04 13:48:42 +0000 | [diff] [blame] | 11926 | ctx.idx = 0; |
Willy Tarreau | 877e78d | 2013-04-07 18:48:08 +0200 | [diff] [blame] | 11927 | 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] | 11928 | /* OK we have the header value in ctx.line+ctx.val for ctx.vlen bytes */ |
| 11929 | ptr = ctx.line + ctx.val; |
| 11930 | len = ctx.vlen; |
| 11931 | while (len--) |
| 11932 | hash = *(ptr++) + (hash << 6) + (hash << 16) - hash; |
| 11933 | } |
| 11934 | |
| 11935 | /* now retrieve the path */ |
Willy Tarreau | 877e78d | 2013-04-07 18:48:08 +0200 | [diff] [blame] | 11936 | 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] | 11937 | beg = http_get_path(txn); |
| 11938 | if (!beg) |
| 11939 | beg = end; |
| 11940 | |
| 11941 | for (ptr = beg; ptr < end ; ptr++); |
| 11942 | |
| 11943 | if (beg < ptr && *beg == '/') { |
| 11944 | while (beg < ptr) |
| 11945 | hash = *(beg++) + (hash << 6) + (hash << 16) - hash; |
| 11946 | } |
| 11947 | hash = full_hash(hash); |
| 11948 | |
| 11949 | smp->type = SMP_T_UINT; |
| 11950 | smp->data.uint = hash; |
| 11951 | smp->flags = SMP_F_VOL_1ST; |
| 11952 | return 1; |
| 11953 | } |
| 11954 | |
| 11955 | /* This concatenates the source address with the 32-bit hash of the Host and |
| 11956 | * URL as returned by smp_fetch_base32(). The idea is to have per-source and |
| 11957 | * per-url counters. The result is a binary block from 8 to 20 bytes depending |
| 11958 | * on the source address length. The URL hash is stored before the address so |
| 11959 | * that in environments where IPv6 is insignificant, truncating the output to |
| 11960 | * 8 bytes would still work. |
| 11961 | */ |
| 11962 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 11963 | 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] | 11964 | { |
| 11965 | struct chunk *temp; |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 11966 | struct connection *cli_conn = objt_conn(smp->sess->origin); |
Neil - HAProxy List | 39c63c5 | 2013-11-04 13:48:42 +0000 | [diff] [blame] | 11967 | |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 11968 | if (!smp_fetch_url32(args, smp, kw, private)) |
Neil - HAProxy List | 39c63c5 | 2013-11-04 13:48:42 +0000 | [diff] [blame] | 11969 | return 0; |
| 11970 | |
| 11971 | temp = get_trash_chunk(); |
| 11972 | memcpy(temp->str + temp->len, &smp->data.uint, sizeof(smp->data.uint)); |
| 11973 | temp->len += sizeof(smp->data.uint); |
| 11974 | |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 11975 | switch (cli_conn->addr.from.ss_family) { |
Neil - HAProxy List | 39c63c5 | 2013-11-04 13:48:42 +0000 | [diff] [blame] | 11976 | case AF_INET: |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 11977 | memcpy(temp->str + temp->len, &((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr, 4); |
Neil - HAProxy List | 39c63c5 | 2013-11-04 13:48:42 +0000 | [diff] [blame] | 11978 | temp->len += 4; |
| 11979 | break; |
| 11980 | case AF_INET6: |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 11981 | memcpy(temp->str + temp->len, &((struct sockaddr_in6 *)&cli_conn->addr.from)->sin6_addr, 16); |
Neil - HAProxy List | 39c63c5 | 2013-11-04 13:48:42 +0000 | [diff] [blame] | 11982 | temp->len += 16; |
| 11983 | break; |
| 11984 | default: |
| 11985 | return 0; |
| 11986 | } |
| 11987 | |
| 11988 | smp->data.str = *temp; |
| 11989 | smp->type = SMP_T_BIN; |
| 11990 | return 1; |
| 11991 | } |
| 11992 | |
Willy Tarreau | 185b5c4 | 2012-04-26 15:11:51 +0200 | [diff] [blame] | 11993 | /* This function is used to validate the arguments passed to any "hdr" fetch |
| 11994 | * keyword. These keywords support an optional positive or negative occurrence |
| 11995 | * number. We must ensure that the number is greater than -MAX_HDR_HISTORY. It |
| 11996 | * is assumed that the types are already the correct ones. Returns 0 on error, |
| 11997 | * non-zero if OK. If <err> is not NULL, it will be filled with a pointer to an |
| 11998 | * error message in case of error, that the caller is responsible for freeing. |
| 11999 | * The initial location must either be freeable or NULL. |
| 12000 | */ |
Thierry FOURNIER | 49f45af | 2014-12-08 19:50:43 +0100 | [diff] [blame] | 12001 | int val_hdr(struct arg *arg, char **err_msg) |
Willy Tarreau | 185b5c4 | 2012-04-26 15:11:51 +0200 | [diff] [blame] | 12002 | { |
| 12003 | if (arg && arg[1].type == ARGT_SINT && arg[1].data.sint < -MAX_HDR_HISTORY) { |
Willy Tarreau | eb6cead | 2012-09-20 19:43:14 +0200 | [diff] [blame] | 12004 | memprintf(err_msg, "header occurrence must be >= %d", -MAX_HDR_HISTORY); |
Willy Tarreau | 185b5c4 | 2012-04-26 15:11:51 +0200 | [diff] [blame] | 12005 | return 0; |
| 12006 | } |
| 12007 | return 1; |
| 12008 | } |
| 12009 | |
Willy Tarreau | 276fae9 | 2013-07-25 14:36:01 +0200 | [diff] [blame] | 12010 | /* takes an UINT value on input supposed to represent the time since EPOCH, |
| 12011 | * adds an optional offset found in args[0] and emits a string representing |
| 12012 | * the date in RFC-1123/5322 format. |
| 12013 | */ |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 12014 | static int sample_conv_http_date(const struct arg *args, struct sample *smp, void *private) |
Willy Tarreau | 276fae9 | 2013-07-25 14:36:01 +0200 | [diff] [blame] | 12015 | { |
| 12016 | const char day[7][4] = { "Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun" }; |
| 12017 | const char mon[12][4] = { "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" }; |
| 12018 | struct chunk *temp; |
| 12019 | struct tm *tm; |
| 12020 | time_t curr_date = smp->data.uint; |
| 12021 | |
| 12022 | /* add offset */ |
| 12023 | if (args && (args[0].type == ARGT_SINT || args[0].type == ARGT_UINT)) |
| 12024 | curr_date += args[0].data.sint; |
| 12025 | |
| 12026 | tm = gmtime(&curr_date); |
| 12027 | |
| 12028 | temp = get_trash_chunk(); |
| 12029 | temp->len = snprintf(temp->str, temp->size - temp->len, |
| 12030 | "%s, %02d %s %04d %02d:%02d:%02d GMT", |
| 12031 | day[tm->tm_wday], tm->tm_mday, mon[tm->tm_mon], 1900+tm->tm_year, |
| 12032 | tm->tm_hour, tm->tm_min, tm->tm_sec); |
| 12033 | |
| 12034 | smp->data.str = *temp; |
| 12035 | smp->type = SMP_T_STR; |
| 12036 | return 1; |
| 12037 | } |
| 12038 | |
Thierry FOURNIER | ad90351 | 2014-04-11 17:51:01 +0200 | [diff] [blame] | 12039 | /* Match language range with language tag. RFC2616 14.4: |
| 12040 | * |
| 12041 | * A language-range matches a language-tag if it exactly equals |
| 12042 | * the tag, or if it exactly equals a prefix of the tag such |
| 12043 | * that the first tag character following the prefix is "-". |
| 12044 | * |
| 12045 | * Return 1 if the strings match, else return 0. |
| 12046 | */ |
| 12047 | static inline int language_range_match(const char *range, int range_len, |
| 12048 | const char *tag, int tag_len) |
| 12049 | { |
| 12050 | const char *end = range + range_len; |
| 12051 | const char *tend = tag + tag_len; |
| 12052 | while (range < end) { |
| 12053 | if (*range == '-' && tag == tend) |
| 12054 | return 1; |
| 12055 | if (*range != *tag || tag == tend) |
| 12056 | return 0; |
| 12057 | range++; |
| 12058 | tag++; |
| 12059 | } |
| 12060 | /* Return true only if the last char of the tag is matched. */ |
| 12061 | return tag == tend; |
| 12062 | } |
| 12063 | |
| 12064 | /* 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] | 12065 | 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] | 12066 | { |
| 12067 | const char *al = smp->data.str.str; |
| 12068 | const char *end = al + smp->data.str.len; |
| 12069 | const char *token; |
| 12070 | int toklen; |
| 12071 | int qvalue; |
| 12072 | const char *str; |
| 12073 | const char *w; |
| 12074 | int best_q = 0; |
| 12075 | |
| 12076 | /* Set the constant to the sample, because the output of the |
| 12077 | * function will be peek in the constant configuration string. |
| 12078 | */ |
| 12079 | smp->flags |= SMP_F_CONST; |
| 12080 | smp->data.str.size = 0; |
| 12081 | smp->data.str.str = ""; |
| 12082 | smp->data.str.len = 0; |
| 12083 | |
| 12084 | /* Parse the accept language */ |
| 12085 | while (1) { |
| 12086 | |
| 12087 | /* Jump spaces, quit if the end is detected. */ |
Willy Tarreau | 506c69a | 2014-07-08 00:59:48 +0200 | [diff] [blame] | 12088 | while (al < end && isspace((unsigned char)*al)) |
Thierry FOURNIER | ad90351 | 2014-04-11 17:51:01 +0200 | [diff] [blame] | 12089 | al++; |
| 12090 | if (al >= end) |
| 12091 | break; |
| 12092 | |
| 12093 | /* Start of the fisrt word. */ |
| 12094 | token = al; |
| 12095 | |
| 12096 | /* Look for separator: isspace(), ',' or ';'. Next value if 0 length word. */ |
Willy Tarreau | 506c69a | 2014-07-08 00:59:48 +0200 | [diff] [blame] | 12097 | while (al < end && *al != ';' && *al != ',' && !isspace((unsigned char)*al)) |
Thierry FOURNIER | ad90351 | 2014-04-11 17:51:01 +0200 | [diff] [blame] | 12098 | al++; |
| 12099 | if (al == token) |
| 12100 | goto expect_comma; |
| 12101 | |
| 12102 | /* Length of the token. */ |
| 12103 | toklen = al - token; |
| 12104 | qvalue = 1000; |
| 12105 | |
| 12106 | /* Check if the token exists in the list. If the token not exists, |
| 12107 | * jump to the next token. |
| 12108 | */ |
| 12109 | str = args[0].data.str.str; |
| 12110 | w = str; |
| 12111 | while (1) { |
| 12112 | if (*str == ';' || *str == '\0') { |
| 12113 | if (language_range_match(token, toklen, w, str-w)) |
| 12114 | goto look_for_q; |
| 12115 | if (*str == '\0') |
| 12116 | goto expect_comma; |
| 12117 | w = str + 1; |
| 12118 | } |
| 12119 | str++; |
| 12120 | } |
| 12121 | goto expect_comma; |
| 12122 | |
| 12123 | look_for_q: |
| 12124 | |
| 12125 | /* Jump spaces, quit if the end is detected. */ |
Willy Tarreau | 506c69a | 2014-07-08 00:59:48 +0200 | [diff] [blame] | 12126 | while (al < end && isspace((unsigned char)*al)) |
Thierry FOURNIER | ad90351 | 2014-04-11 17:51:01 +0200 | [diff] [blame] | 12127 | al++; |
| 12128 | if (al >= end) |
| 12129 | goto process_value; |
| 12130 | |
| 12131 | /* If ',' is found, process the result */ |
| 12132 | if (*al == ',') |
| 12133 | goto process_value; |
| 12134 | |
| 12135 | /* If the character is different from ';', look |
| 12136 | * for the end of the header part in best effort. |
| 12137 | */ |
| 12138 | if (*al != ';') |
| 12139 | goto expect_comma; |
| 12140 | |
| 12141 | /* Assumes that the char is ';', now expect "q=". */ |
| 12142 | al++; |
| 12143 | |
| 12144 | /* Jump spaces, process value if the end is detected. */ |
Willy Tarreau | 506c69a | 2014-07-08 00:59:48 +0200 | [diff] [blame] | 12145 | while (al < end && isspace((unsigned char)*al)) |
Thierry FOURNIER | ad90351 | 2014-04-11 17:51:01 +0200 | [diff] [blame] | 12146 | al++; |
| 12147 | if (al >= end) |
| 12148 | goto process_value; |
| 12149 | |
| 12150 | /* Expect 'q'. If no 'q', continue in best effort */ |
| 12151 | if (*al != 'q') |
| 12152 | goto process_value; |
| 12153 | al++; |
| 12154 | |
| 12155 | /* Jump spaces, process value if the end is detected. */ |
Willy Tarreau | 506c69a | 2014-07-08 00:59:48 +0200 | [diff] [blame] | 12156 | while (al < end && isspace((unsigned char)*al)) |
Thierry FOURNIER | ad90351 | 2014-04-11 17:51:01 +0200 | [diff] [blame] | 12157 | al++; |
| 12158 | if (al >= end) |
| 12159 | goto process_value; |
| 12160 | |
| 12161 | /* Expect '='. If no '=', continue in best effort */ |
| 12162 | if (*al != '=') |
| 12163 | goto process_value; |
| 12164 | al++; |
| 12165 | |
| 12166 | /* Jump spaces, process value if the end is detected. */ |
Willy Tarreau | 506c69a | 2014-07-08 00:59:48 +0200 | [diff] [blame] | 12167 | while (al < end && isspace((unsigned char)*al)) |
Thierry FOURNIER | ad90351 | 2014-04-11 17:51:01 +0200 | [diff] [blame] | 12168 | al++; |
| 12169 | if (al >= end) |
| 12170 | goto process_value; |
| 12171 | |
| 12172 | /* Parse the q value. */ |
| 12173 | qvalue = parse_qvalue(al, &al); |
| 12174 | |
| 12175 | process_value: |
| 12176 | |
| 12177 | /* If the new q value is the best q value, then store the associated |
| 12178 | * language in the response. If qvalue is the biggest value (1000), |
| 12179 | * break the process. |
| 12180 | */ |
| 12181 | if (qvalue > best_q) { |
| 12182 | smp->data.str.str = (char *)w; |
| 12183 | smp->data.str.len = str - w; |
| 12184 | if (qvalue >= 1000) |
| 12185 | break; |
| 12186 | best_q = qvalue; |
| 12187 | } |
| 12188 | |
| 12189 | expect_comma: |
| 12190 | |
| 12191 | /* Expect comma or end. If the end is detected, quit the loop. */ |
| 12192 | while (al < end && *al != ',') |
| 12193 | al++; |
| 12194 | if (al >= end) |
| 12195 | break; |
| 12196 | |
| 12197 | /* Comma is found, jump it and restart the analyzer. */ |
| 12198 | al++; |
| 12199 | } |
| 12200 | |
| 12201 | /* Set default value if required. */ |
| 12202 | if (smp->data.str.len == 0 && args[1].type == ARGT_STR) { |
| 12203 | smp->data.str.str = args[1].data.str.str; |
| 12204 | smp->data.str.len = args[1].data.str.len; |
| 12205 | } |
| 12206 | |
| 12207 | /* Return true only if a matching language was found. */ |
| 12208 | return smp->data.str.len != 0; |
| 12209 | } |
| 12210 | |
Thierry FOURNIER | 82ff3c9 | 2015-05-07 15:46:20 +0200 | [diff] [blame] | 12211 | /* This fetch url-decode any input string. */ |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 12212 | 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] | 12213 | { |
| 12214 | /* If the constant flag is set or if not size is avalaible at |
| 12215 | * the end of the buffer, copy the string in other buffer |
| 12216 | * before decoding. |
| 12217 | */ |
| 12218 | if (smp->flags & SMP_F_CONST || smp->data.str.size <= smp->data.str.len) { |
| 12219 | struct chunk *str = get_trash_chunk(); |
| 12220 | memcpy(str->str, smp->data.str.str, smp->data.str.len); |
| 12221 | smp->data.str.str = str->str; |
| 12222 | smp->data.str.size = str->size; |
| 12223 | smp->flags &= ~SMP_F_CONST; |
| 12224 | } |
| 12225 | |
| 12226 | /* Add final \0 required by url_decode(), and convert the input string. */ |
| 12227 | smp->data.str.str[smp->data.str.len] = '\0'; |
| 12228 | smp->data.str.len = url_decode(smp->data.str.str); |
| 12229 | return 1; |
| 12230 | } |
| 12231 | |
Thierry FOURNIER | 35ab275 | 2015-05-28 13:22:03 +0200 | [diff] [blame] | 12232 | static int smp_conv_req_capture(const struct arg *args, struct sample *smp, void *private) |
| 12233 | { |
| 12234 | struct proxy *fe = strm_fe(smp->strm); |
| 12235 | int idx, i; |
| 12236 | struct cap_hdr *hdr; |
| 12237 | int len; |
| 12238 | |
| 12239 | if (!args || args->type != ARGT_UINT) |
| 12240 | return 0; |
| 12241 | |
| 12242 | idx = args->data.uint; |
| 12243 | |
| 12244 | /* Check the availibity of the capture id. */ |
| 12245 | if (idx > fe->nb_req_cap - 1) |
| 12246 | return 0; |
| 12247 | |
| 12248 | /* Look for the original configuration. */ |
| 12249 | for (hdr = fe->req_cap, i = fe->nb_req_cap - 1; |
| 12250 | hdr != NULL && i != idx ; |
| 12251 | i--, hdr = hdr->next); |
| 12252 | if (!hdr) |
| 12253 | return 0; |
| 12254 | |
| 12255 | /* check for the memory allocation */ |
| 12256 | if (smp->strm->req_cap[hdr->index] == NULL) |
| 12257 | smp->strm->req_cap[hdr->index] = pool_alloc2(hdr->pool); |
| 12258 | if (smp->strm->req_cap[hdr->index] == NULL) |
| 12259 | return 0; |
| 12260 | |
| 12261 | /* Check length. */ |
| 12262 | len = smp->data.str.len; |
| 12263 | if (len > hdr->len) |
| 12264 | len = hdr->len; |
| 12265 | |
| 12266 | /* Capture input data. */ |
| 12267 | memcpy(smp->strm->req_cap[idx], smp->data.str.str, len); |
| 12268 | smp->strm->req_cap[idx][len] = '\0'; |
| 12269 | |
| 12270 | return 1; |
| 12271 | } |
| 12272 | |
| 12273 | static int smp_conv_res_capture(const struct arg *args, struct sample *smp, void *private) |
| 12274 | { |
| 12275 | struct proxy *fe = strm_fe(smp->strm); |
| 12276 | int idx, i; |
| 12277 | struct cap_hdr *hdr; |
| 12278 | int len; |
| 12279 | |
| 12280 | if (!args || args->type != ARGT_UINT) |
| 12281 | return 0; |
| 12282 | |
| 12283 | idx = args->data.uint; |
| 12284 | |
| 12285 | /* Check the availibity of the capture id. */ |
| 12286 | if (idx > fe->nb_rsp_cap - 1) |
| 12287 | return 0; |
| 12288 | |
| 12289 | /* Look for the original configuration. */ |
| 12290 | for (hdr = fe->rsp_cap, i = fe->nb_rsp_cap - 1; |
| 12291 | hdr != NULL && i != idx ; |
| 12292 | i--, hdr = hdr->next); |
| 12293 | if (!hdr) |
| 12294 | return 0; |
| 12295 | |
| 12296 | /* check for the memory allocation */ |
| 12297 | if (smp->strm->res_cap[hdr->index] == NULL) |
| 12298 | smp->strm->res_cap[hdr->index] = pool_alloc2(hdr->pool); |
| 12299 | if (smp->strm->res_cap[hdr->index] == NULL) |
| 12300 | return 0; |
| 12301 | |
| 12302 | /* Check length. */ |
| 12303 | len = smp->data.str.len; |
| 12304 | if (len > hdr->len) |
| 12305 | len = hdr->len; |
| 12306 | |
| 12307 | /* Capture input data. */ |
| 12308 | memcpy(smp->strm->res_cap[idx], smp->data.str.str, len); |
| 12309 | smp->strm->res_cap[idx][len] = '\0'; |
| 12310 | |
| 12311 | return 1; |
| 12312 | } |
| 12313 | |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 12314 | /* 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] | 12315 | * takes the string from the variable 'replace' with length 'len', then modifies |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 12316 | * the relevant part of the request line accordingly. Then it updates various |
| 12317 | * pointers to the next elements which were moved, and the total buffer length. |
| 12318 | * 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] | 12319 | * parse_set_req_line(). It returns 0 in case of success, -1 in case of internal |
| 12320 | * error, though this can be revisited when this code is finally exploited. |
| 12321 | * |
| 12322 | * 'action' can be '0' to replace method, '1' to replace path, '2' to replace |
| 12323 | * query string and 3 to replace uri. |
| 12324 | * |
| 12325 | * In query string case, the mark question '?' must be set at the start of the |
| 12326 | * string by the caller, event if the replacement query string is empty. |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 12327 | */ |
Thierry FOURNIER | b77aece | 2015-03-14 13:55:46 +0100 | [diff] [blame] | 12328 | int http_replace_req_line(int action, const char *replace, int len, |
Willy Tarreau | 987e3fb | 2015-04-04 01:09:08 +0200 | [diff] [blame] | 12329 | struct proxy *px, struct stream *s) |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 12330 | { |
Willy Tarreau | 987e3fb | 2015-04-04 01:09:08 +0200 | [diff] [blame] | 12331 | struct http_txn *txn = s->txn; |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 12332 | char *cur_ptr, *cur_end; |
Thierry FOURNIER | b77aece | 2015-03-14 13:55:46 +0100 | [diff] [blame] | 12333 | int offset = 0; |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 12334 | int delta; |
| 12335 | |
Thierry FOURNIER | b77aece | 2015-03-14 13:55:46 +0100 | [diff] [blame] | 12336 | switch (action) { |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 12337 | case 0: // method |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 12338 | cur_ptr = s->req.buf->p; |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 12339 | cur_end = cur_ptr + txn->req.sl.rq.m_l; |
| 12340 | |
| 12341 | /* adjust req line offsets and lengths */ |
Thierry FOURNIER | b77aece | 2015-03-14 13:55:46 +0100 | [diff] [blame] | 12342 | delta = len - offset - (cur_end - cur_ptr); |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 12343 | txn->req.sl.rq.m_l += delta; |
| 12344 | txn->req.sl.rq.u += delta; |
| 12345 | txn->req.sl.rq.v += delta; |
| 12346 | break; |
| 12347 | |
| 12348 | case 1: // path |
| 12349 | cur_ptr = http_get_path(txn); |
| 12350 | if (!cur_ptr) |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 12351 | cur_ptr = s->req.buf->p + txn->req.sl.rq.u; |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 12352 | |
| 12353 | cur_end = cur_ptr; |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 12354 | 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] | 12355 | cur_end++; |
| 12356 | |
| 12357 | /* adjust req line offsets and lengths */ |
Thierry FOURNIER | b77aece | 2015-03-14 13:55:46 +0100 | [diff] [blame] | 12358 | delta = len - offset - (cur_end - cur_ptr); |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 12359 | txn->req.sl.rq.u_l += delta; |
| 12360 | txn->req.sl.rq.v += delta; |
| 12361 | break; |
| 12362 | |
| 12363 | case 2: // query |
Thierry FOURNIER | b77aece | 2015-03-14 13:55:46 +0100 | [diff] [blame] | 12364 | offset = 1; |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 12365 | cur_ptr = s->req.buf->p + txn->req.sl.rq.u; |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 12366 | cur_end = cur_ptr + txn->req.sl.rq.u_l; |
| 12367 | while (cur_ptr < cur_end && *cur_ptr != '?') |
| 12368 | cur_ptr++; |
| 12369 | |
| 12370 | /* skip the question mark or indicate that we must insert it |
| 12371 | * (but only if the format string is not empty then). |
| 12372 | */ |
| 12373 | if (cur_ptr < cur_end) |
| 12374 | cur_ptr++; |
Thierry FOURNIER | b77aece | 2015-03-14 13:55:46 +0100 | [diff] [blame] | 12375 | else if (len > 1) |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 12376 | offset = 0; |
| 12377 | |
| 12378 | /* adjust req line offsets and lengths */ |
Thierry FOURNIER | b77aece | 2015-03-14 13:55:46 +0100 | [diff] [blame] | 12379 | delta = len - offset - (cur_end - cur_ptr); |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 12380 | txn->req.sl.rq.u_l += delta; |
| 12381 | txn->req.sl.rq.v += delta; |
| 12382 | break; |
| 12383 | |
| 12384 | case 3: // uri |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 12385 | cur_ptr = s->req.buf->p + txn->req.sl.rq.u; |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 12386 | cur_end = cur_ptr + txn->req.sl.rq.u_l; |
| 12387 | |
| 12388 | /* adjust req line offsets and lengths */ |
Thierry FOURNIER | b77aece | 2015-03-14 13:55:46 +0100 | [diff] [blame] | 12389 | delta = len - offset - (cur_end - cur_ptr); |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 12390 | txn->req.sl.rq.u_l += delta; |
| 12391 | txn->req.sl.rq.v += delta; |
| 12392 | break; |
| 12393 | |
| 12394 | default: |
Thierry FOURNIER | b77aece | 2015-03-14 13:55:46 +0100 | [diff] [blame] | 12395 | return -1; |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 12396 | } |
| 12397 | |
| 12398 | /* commit changes and adjust end of message */ |
Thierry FOURNIER | b77aece | 2015-03-14 13:55:46 +0100 | [diff] [blame] | 12399 | 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] | 12400 | txn->req.sl.rq.l += delta; |
| 12401 | txn->hdr_idx.v[0].len += delta; |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 12402 | http_msg_move_end(&txn->req, delta); |
Thierry FOURNIER | b77aece | 2015-03-14 13:55:46 +0100 | [diff] [blame] | 12403 | return 0; |
| 12404 | } |
| 12405 | |
| 12406 | /* This function executes one of the set-{method,path,query,uri} actions. It |
| 12407 | * builds a string in the trash from the specified format string. It finds |
| 12408 | * the action to be performed in p[2], previously filled by function |
| 12409 | * parse_set_req_line(). The replacement action is excuted by the function |
| 12410 | * http_action_set_req_line_exec(). It always returns 1. If an error occurs |
| 12411 | * the action is canceled, but the rule processing continue. |
| 12412 | */ |
Willy Tarreau | 987e3fb | 2015-04-04 01:09:08 +0200 | [diff] [blame] | 12413 | int http_action_set_req_line(struct http_req_rule *rule, struct proxy *px, struct stream *s) |
Thierry FOURNIER | b77aece | 2015-03-14 13:55:46 +0100 | [diff] [blame] | 12414 | { |
| 12415 | chunk_reset(&trash); |
| 12416 | |
| 12417 | /* If we have to create a query string, prepare a '?'. */ |
| 12418 | if (*(int *)&rule->arg.act.p[2] == 2) |
| 12419 | trash.str[trash.len++] = '?'; |
| 12420 | trash.len += build_logline(s, trash.str + trash.len, trash.size - trash.len, (struct list *)&rule->arg.act.p[0]); |
| 12421 | |
Willy Tarreau | 987e3fb | 2015-04-04 01:09:08 +0200 | [diff] [blame] | 12422 | http_replace_req_line(*(int *)&rule->arg.act.p[2], trash.str, trash.len, px, s); |
Thierry FOURNIER | 01c3012 | 2015-03-14 14:14:47 +0100 | [diff] [blame] | 12423 | return 1; |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 12424 | } |
| 12425 | |
| 12426 | /* parse an http-request action among : |
| 12427 | * set-method |
| 12428 | * set-path |
| 12429 | * set-query |
| 12430 | * set-uri |
| 12431 | * |
| 12432 | * All of them accept a single argument of type string representing a log-format. |
| 12433 | * The resulting rule makes use of arg->act.p[0..1] to store the log-format list |
| 12434 | * head, and p[2] to store the action as an int (0=method, 1=path, 2=query, 3=uri). |
| 12435 | * It returns 0 on success, < 0 on error. |
| 12436 | */ |
| 12437 | int parse_set_req_line(const char **args, int *orig_arg, struct proxy *px, struct http_req_rule *rule, char **err) |
| 12438 | { |
| 12439 | int cur_arg = *orig_arg; |
| 12440 | |
| 12441 | rule->action = HTTP_REQ_ACT_CUSTOM_CONT; |
| 12442 | |
| 12443 | switch (args[0][4]) { |
| 12444 | case 'm' : |
| 12445 | *(int *)&rule->arg.act.p[2] = 0; |
| 12446 | rule->action_ptr = http_action_set_req_line; |
| 12447 | break; |
| 12448 | case 'p' : |
| 12449 | *(int *)&rule->arg.act.p[2] = 1; |
| 12450 | rule->action_ptr = http_action_set_req_line; |
| 12451 | break; |
| 12452 | case 'q' : |
| 12453 | *(int *)&rule->arg.act.p[2] = 2; |
| 12454 | rule->action_ptr = http_action_set_req_line; |
| 12455 | break; |
| 12456 | case 'u' : |
| 12457 | *(int *)&rule->arg.act.p[2] = 3; |
| 12458 | rule->action_ptr = http_action_set_req_line; |
| 12459 | break; |
| 12460 | default: |
| 12461 | memprintf(err, "internal error: unhandled action '%s'", args[0]); |
| 12462 | return -1; |
| 12463 | } |
| 12464 | |
| 12465 | if (!*args[cur_arg] || |
| 12466 | (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) { |
| 12467 | memprintf(err, "expects exactly 1 argument <format>"); |
| 12468 | return -1; |
| 12469 | } |
| 12470 | |
| 12471 | LIST_INIT((struct list *)&rule->arg.act.p[0]); |
| 12472 | proxy->conf.args.ctx = ARGC_HRQ; |
| 12473 | parse_logformat_string(args[cur_arg], proxy, (struct list *)&rule->arg.act.p[0], LOG_OPT_HTTP, |
| 12474 | (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR, |
| 12475 | proxy->conf.args.file, proxy->conf.args.line); |
| 12476 | |
| 12477 | (*orig_arg)++; |
| 12478 | return 0; |
| 12479 | } |
| 12480 | |
Willy Tarreau | a9083d0 | 2015-05-08 15:27:59 +0200 | [diff] [blame] | 12481 | /* This function executes the "capture" action. It executes a fetch expression, |
| 12482 | * turns the result into a string and puts it in a capture slot. It always |
| 12483 | * returns 1. If an error occurs the action is cancelled, but the rule |
| 12484 | * processing continues. |
| 12485 | */ |
| 12486 | int http_action_req_capture(struct http_req_rule *rule, struct proxy *px, struct stream *s) |
| 12487 | { |
| 12488 | struct session *sess = s->sess; |
| 12489 | struct sample *key; |
| 12490 | struct sample_expr *expr = rule->arg.act.p[0]; |
| 12491 | struct cap_hdr *h = rule->arg.act.p[1]; |
| 12492 | char **cap = s->req_cap; |
| 12493 | int len; |
| 12494 | |
| 12495 | key = sample_fetch_string(s->be, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL, expr); |
| 12496 | if (!key) |
| 12497 | return 1; |
| 12498 | |
| 12499 | if (cap[h->index] == NULL) |
| 12500 | cap[h->index] = pool_alloc2(h->pool); |
| 12501 | |
| 12502 | if (cap[h->index] == NULL) /* no more capture memory */ |
| 12503 | return 1; |
| 12504 | |
| 12505 | len = key->data.str.len; |
| 12506 | if (len > h->len) |
| 12507 | len = h->len; |
| 12508 | |
| 12509 | memcpy(cap[h->index], key->data.str.str, len); |
| 12510 | cap[h->index][len] = 0; |
| 12511 | return 1; |
| 12512 | } |
| 12513 | |
Thierry FOURNIER | 82bf70d | 2015-05-26 17:58:29 +0200 | [diff] [blame] | 12514 | /* This function executes the "capture" action and store the result in a |
| 12515 | * capture slot if exists. It executes a fetch expression, turns the result |
| 12516 | * into a string and puts it in a capture slot. It always returns 1. If an |
| 12517 | * error occurs the action is cancelled, but the rule processing continues. |
| 12518 | */ |
| 12519 | int http_action_req_capture_by_id(struct http_req_rule *rule, struct proxy *px, struct stream *s) |
| 12520 | { |
| 12521 | struct session *sess = s->sess; |
| 12522 | struct sample *key; |
| 12523 | struct sample_expr *expr = rule->arg.act.p[0]; |
| 12524 | struct cap_hdr *h; |
| 12525 | int idx = (long)rule->arg.act.p[1]; |
| 12526 | char **cap = s->req_cap; |
| 12527 | struct proxy *fe = strm_fe(s); |
| 12528 | int len; |
| 12529 | int i; |
| 12530 | |
| 12531 | /* Look for the original configuration. */ |
| 12532 | for (h = fe->req_cap, i = fe->nb_req_cap - 1; |
| 12533 | h != NULL && i != idx ; |
| 12534 | i--, h = h->next); |
| 12535 | if (!h) |
| 12536 | return 1; |
| 12537 | |
| 12538 | key = sample_fetch_string(s->be, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL, expr); |
| 12539 | if (!key) |
| 12540 | return 1; |
| 12541 | |
| 12542 | if (cap[h->index] == NULL) |
| 12543 | cap[h->index] = pool_alloc2(h->pool); |
| 12544 | |
| 12545 | if (cap[h->index] == NULL) /* no more capture memory */ |
| 12546 | return 1; |
| 12547 | |
| 12548 | len = key->data.str.len; |
| 12549 | if (len > h->len) |
| 12550 | len = h->len; |
| 12551 | |
| 12552 | memcpy(cap[h->index], key->data.str.str, len); |
| 12553 | cap[h->index][len] = 0; |
| 12554 | return 1; |
| 12555 | } |
| 12556 | |
Willy Tarreau | a9083d0 | 2015-05-08 15:27:59 +0200 | [diff] [blame] | 12557 | /* parse an "http-request capture" action. It takes a single argument which is |
| 12558 | * 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] | 12559 | * the allocated hdr_cap struct or the preallocated "id" into arg->act.p[1]. |
Willy Tarreau | a9083d0 | 2015-05-08 15:27:59 +0200 | [diff] [blame] | 12560 | * It returns 0 on success, < 0 on error. |
| 12561 | */ |
| 12562 | int parse_http_req_capture(const char **args, int *orig_arg, struct proxy *px, struct http_req_rule *rule, char **err) |
| 12563 | { |
| 12564 | struct sample_expr *expr; |
| 12565 | struct cap_hdr *hdr; |
| 12566 | int cur_arg; |
Willy Tarreau | 3986ac1 | 2015-05-08 16:13:42 +0200 | [diff] [blame] | 12567 | int len = 0; |
Willy Tarreau | a9083d0 | 2015-05-08 15:27:59 +0200 | [diff] [blame] | 12568 | |
| 12569 | for (cur_arg = *orig_arg; cur_arg < *orig_arg + 3 && *args[cur_arg]; cur_arg++) |
| 12570 | if (strcmp(args[cur_arg], "if") == 0 || |
| 12571 | strcmp(args[cur_arg], "unless") == 0) |
| 12572 | break; |
| 12573 | |
| 12574 | if (cur_arg < *orig_arg + 3) { |
Thierry FOURNIER | 82bf70d | 2015-05-26 17:58:29 +0200 | [diff] [blame] | 12575 | memprintf(err, "expects <expression> [ 'len' <length> | id <idx> ]"); |
Willy Tarreau | a9083d0 | 2015-05-08 15:27:59 +0200 | [diff] [blame] | 12576 | return -1; |
| 12577 | } |
| 12578 | |
Willy Tarreau | a9083d0 | 2015-05-08 15:27:59 +0200 | [diff] [blame] | 12579 | cur_arg = *orig_arg; |
| 12580 | expr = sample_parse_expr((char **)args, &cur_arg, px->conf.args.file, px->conf.args.line, err, &px->conf.args); |
| 12581 | if (!expr) |
| 12582 | return -1; |
| 12583 | |
| 12584 | if (!(expr->fetch->val & SMP_VAL_FE_HRQ_HDR)) { |
| 12585 | memprintf(err, |
| 12586 | "fetch method '%s' extracts information from '%s', none of which is available here", |
| 12587 | args[cur_arg-1], sample_src_names(expr->fetch->use)); |
| 12588 | free(expr); |
| 12589 | return -1; |
| 12590 | } |
| 12591 | |
Thierry FOURNIER | 82bf70d | 2015-05-26 17:58:29 +0200 | [diff] [blame] | 12592 | if (!args[cur_arg] || !*args[cur_arg]) { |
| 12593 | memprintf(err, "expects 'len or 'id'"); |
| 12594 | free(expr); |
| 12595 | return -1; |
| 12596 | } |
| 12597 | |
Willy Tarreau | a9083d0 | 2015-05-08 15:27:59 +0200 | [diff] [blame] | 12598 | if (strcmp(args[cur_arg], "len") == 0) { |
| 12599 | cur_arg++; |
Thierry FOURNIER | 82bf70d | 2015-05-26 17:58:29 +0200 | [diff] [blame] | 12600 | |
| 12601 | if (!(px->cap & PR_CAP_FE)) { |
| 12602 | memprintf(err, "proxy '%s' has no frontend capability", px->id); |
| 12603 | return -1; |
| 12604 | } |
| 12605 | |
| 12606 | proxy->conf.args.ctx = ARGC_CAP; |
| 12607 | |
Willy Tarreau | a9083d0 | 2015-05-08 15:27:59 +0200 | [diff] [blame] | 12608 | if (!args[cur_arg]) { |
| 12609 | memprintf(err, "missing length value"); |
| 12610 | free(expr); |
| 12611 | return -1; |
| 12612 | } |
| 12613 | /* we copy the table name for now, it will be resolved later */ |
| 12614 | len = atoi(args[cur_arg]); |
| 12615 | if (len <= 0) { |
| 12616 | memprintf(err, "length must be > 0"); |
| 12617 | free(expr); |
| 12618 | return -1; |
| 12619 | } |
| 12620 | cur_arg++; |
Willy Tarreau | a9083d0 | 2015-05-08 15:27:59 +0200 | [diff] [blame] | 12621 | |
Thierry FOURNIER | 82bf70d | 2015-05-26 17:58:29 +0200 | [diff] [blame] | 12622 | if (!len) { |
| 12623 | memprintf(err, "a positive 'len' argument is mandatory"); |
| 12624 | free(expr); |
| 12625 | return -1; |
| 12626 | } |
| 12627 | |
| 12628 | hdr = calloc(sizeof(struct cap_hdr), 1); |
| 12629 | hdr->next = px->req_cap; |
| 12630 | hdr->name = NULL; /* not a header capture */ |
| 12631 | hdr->namelen = 0; |
| 12632 | hdr->len = len; |
| 12633 | hdr->pool = create_pool("caphdr", hdr->len + 1, MEM_F_SHARED); |
| 12634 | hdr->index = px->nb_req_cap++; |
| 12635 | |
| 12636 | px->req_cap = hdr; |
| 12637 | px->to_log |= LW_REQHDR; |
| 12638 | |
| 12639 | rule->action = HTTP_REQ_ACT_CUSTOM_CONT; |
| 12640 | rule->action_ptr = http_action_req_capture; |
| 12641 | rule->arg.act.p[0] = expr; |
| 12642 | rule->arg.act.p[1] = hdr; |
Willy Tarreau | a9083d0 | 2015-05-08 15:27:59 +0200 | [diff] [blame] | 12643 | } |
| 12644 | |
Thierry FOURNIER | 82bf70d | 2015-05-26 17:58:29 +0200 | [diff] [blame] | 12645 | else if (strcmp(args[cur_arg], "id") == 0) { |
| 12646 | int id; |
| 12647 | char *error; |
Willy Tarreau | a9083d0 | 2015-05-08 15:27:59 +0200 | [diff] [blame] | 12648 | |
Thierry FOURNIER | 82bf70d | 2015-05-26 17:58:29 +0200 | [diff] [blame] | 12649 | cur_arg++; |
Willy Tarreau | a9083d0 | 2015-05-08 15:27:59 +0200 | [diff] [blame] | 12650 | |
Thierry FOURNIER | 82bf70d | 2015-05-26 17:58:29 +0200 | [diff] [blame] | 12651 | if (!args[cur_arg]) { |
| 12652 | memprintf(err, "missing id value"); |
| 12653 | free(expr); |
| 12654 | return -1; |
| 12655 | } |
Willy Tarreau | a9083d0 | 2015-05-08 15:27:59 +0200 | [diff] [blame] | 12656 | |
Thierry FOURNIER | 82bf70d | 2015-05-26 17:58:29 +0200 | [diff] [blame] | 12657 | id = strtol(args[cur_arg], &error, 10); |
| 12658 | if (*error != '\0') { |
| 12659 | memprintf(err, "cannot parse id '%s'", args[cur_arg]); |
| 12660 | free(expr); |
| 12661 | return -1; |
| 12662 | } |
| 12663 | cur_arg++; |
| 12664 | |
| 12665 | proxy->conf.args.ctx = ARGC_CAP; |
| 12666 | |
| 12667 | rule->action = HTTP_REQ_ACT_CUSTOM_CONT; |
| 12668 | rule->action_ptr = http_action_req_capture_by_id; |
| 12669 | rule->arg.act.p[0] = expr; |
| 12670 | rule->arg.act.p[1] = (void *)(long)id; |
| 12671 | } |
| 12672 | |
| 12673 | else { |
| 12674 | memprintf(err, "expects 'len' or 'id', found '%s'", args[cur_arg]); |
| 12675 | free(expr); |
| 12676 | return -1; |
| 12677 | } |
Willy Tarreau | a9083d0 | 2015-05-08 15:27:59 +0200 | [diff] [blame] | 12678 | |
| 12679 | *orig_arg = cur_arg; |
| 12680 | return 0; |
| 12681 | } |
| 12682 | |
Thierry FOURNIER | e80fada | 2015-05-26 18:06:31 +0200 | [diff] [blame] | 12683 | /* This function executes the "capture" action and store the result in a |
| 12684 | * capture slot if exists. It executes a fetch expression, turns the result |
| 12685 | * into a string and puts it in a capture slot. It always returns 1. If an |
| 12686 | * error occurs the action is cancelled, but the rule processing continues. |
| 12687 | */ |
| 12688 | int http_action_res_capture_by_id(struct http_res_rule *rule, struct proxy *px, struct stream *s) |
| 12689 | { |
| 12690 | struct session *sess = s->sess; |
| 12691 | struct sample *key; |
| 12692 | struct sample_expr *expr = rule->arg.act.p[0]; |
| 12693 | struct cap_hdr *h; |
| 12694 | int idx = (long)rule->arg.act.p[1]; |
| 12695 | char **cap = s->res_cap; |
| 12696 | struct proxy *fe = strm_fe(s); |
| 12697 | int len; |
| 12698 | int i; |
| 12699 | |
| 12700 | /* Look for the original configuration. */ |
| 12701 | for (h = fe->rsp_cap, i = fe->nb_rsp_cap - 1; |
| 12702 | h != NULL && i != idx ; |
| 12703 | i--, h = h->next); |
| 12704 | if (!h) |
| 12705 | return 1; |
| 12706 | |
| 12707 | key = sample_fetch_string(s->be, sess, s, SMP_OPT_DIR_RES|SMP_OPT_FINAL, expr); |
| 12708 | if (!key) |
| 12709 | return 1; |
| 12710 | |
| 12711 | if (cap[h->index] == NULL) |
| 12712 | cap[h->index] = pool_alloc2(h->pool); |
| 12713 | |
| 12714 | if (cap[h->index] == NULL) /* no more capture memory */ |
| 12715 | return 1; |
| 12716 | |
| 12717 | len = key->data.str.len; |
| 12718 | if (len > h->len) |
| 12719 | len = h->len; |
| 12720 | |
| 12721 | memcpy(cap[h->index], key->data.str.str, len); |
| 12722 | cap[h->index][len] = 0; |
| 12723 | return 1; |
| 12724 | } |
| 12725 | |
| 12726 | /* parse an "http-response capture" action. It takes a single argument which is |
| 12727 | * a sample fetch expression. It stores the expression into arg->act.p[0] and |
| 12728 | * the allocated hdr_cap struct od the preallocated id into arg->act.p[1]. |
| 12729 | * It returns 0 on success, < 0 on error. |
| 12730 | */ |
| 12731 | int parse_http_res_capture(const char **args, int *orig_arg, struct proxy *px, struct http_res_rule *rule, char **err) |
| 12732 | { |
| 12733 | struct sample_expr *expr; |
| 12734 | int cur_arg; |
| 12735 | int id; |
| 12736 | char *error; |
| 12737 | |
| 12738 | for (cur_arg = *orig_arg; cur_arg < *orig_arg + 3 && *args[cur_arg]; cur_arg++) |
| 12739 | if (strcmp(args[cur_arg], "if") == 0 || |
| 12740 | strcmp(args[cur_arg], "unless") == 0) |
| 12741 | break; |
| 12742 | |
| 12743 | if (cur_arg < *orig_arg + 3) { |
| 12744 | memprintf(err, "expects <expression> [ 'len' <length> | id <idx> ]"); |
| 12745 | return -1; |
| 12746 | } |
| 12747 | |
| 12748 | cur_arg = *orig_arg; |
| 12749 | expr = sample_parse_expr((char **)args, &cur_arg, px->conf.args.file, px->conf.args.line, err, &px->conf.args); |
| 12750 | if (!expr) |
| 12751 | return -1; |
| 12752 | |
| 12753 | if (!(expr->fetch->val & SMP_VAL_FE_HRS_HDR)) { |
| 12754 | memprintf(err, |
| 12755 | "fetch method '%s' extracts information from '%s', none of which is available here", |
| 12756 | args[cur_arg-1], sample_src_names(expr->fetch->use)); |
| 12757 | free(expr); |
| 12758 | return -1; |
| 12759 | } |
| 12760 | |
| 12761 | if (!args[cur_arg] || !*args[cur_arg]) { |
| 12762 | memprintf(err, "expects 'len or 'id'"); |
| 12763 | free(expr); |
| 12764 | return -1; |
| 12765 | } |
| 12766 | |
| 12767 | if (strcmp(args[cur_arg], "id") != 0) { |
| 12768 | memprintf(err, "expects 'id', found '%s'", args[cur_arg]); |
| 12769 | free(expr); |
| 12770 | return -1; |
| 12771 | } |
| 12772 | |
| 12773 | cur_arg++; |
| 12774 | |
| 12775 | if (!args[cur_arg]) { |
| 12776 | memprintf(err, "missing id value"); |
| 12777 | free(expr); |
| 12778 | return -1; |
| 12779 | } |
| 12780 | |
| 12781 | id = strtol(args[cur_arg], &error, 10); |
| 12782 | if (*error != '\0') { |
| 12783 | memprintf(err, "cannot parse id '%s'", args[cur_arg]); |
| 12784 | free(expr); |
| 12785 | return -1; |
| 12786 | } |
| 12787 | cur_arg++; |
| 12788 | |
| 12789 | LIST_INIT((struct list *)&rule->arg.act.p[0]); |
| 12790 | proxy->conf.args.ctx = ARGC_CAP; |
| 12791 | |
| 12792 | rule->action = HTTP_RES_ACT_CUSTOM_CONT; |
| 12793 | rule->action_ptr = http_action_res_capture_by_id; |
| 12794 | rule->arg.act.p[0] = expr; |
| 12795 | rule->arg.act.p[1] = (void *)(long)id; |
| 12796 | |
| 12797 | *orig_arg = cur_arg; |
| 12798 | return 0; |
| 12799 | } |
| 12800 | |
William Lallemand | 73025dd | 2014-04-24 14:38:37 +0200 | [diff] [blame] | 12801 | /* |
| 12802 | * Return the struct http_req_action_kw associated to a keyword. |
| 12803 | */ |
| 12804 | struct http_req_action_kw *action_http_req_custom(const char *kw) |
| 12805 | { |
| 12806 | if (!LIST_ISEMPTY(&http_req_keywords.list)) { |
| 12807 | struct http_req_action_kw_list *kw_list; |
| 12808 | int i; |
| 12809 | |
| 12810 | list_for_each_entry(kw_list, &http_req_keywords.list, list) { |
| 12811 | for (i = 0; kw_list->kw[i].kw != NULL; i++) { |
| 12812 | if (!strcmp(kw, kw_list->kw[i].kw)) |
| 12813 | return &kw_list->kw[i]; |
| 12814 | } |
| 12815 | } |
| 12816 | } |
| 12817 | return NULL; |
| 12818 | } |
| 12819 | |
| 12820 | /* |
| 12821 | * Return the struct http_res_action_kw associated to a keyword. |
| 12822 | */ |
| 12823 | struct http_res_action_kw *action_http_res_custom(const char *kw) |
| 12824 | { |
| 12825 | if (!LIST_ISEMPTY(&http_res_keywords.list)) { |
| 12826 | struct http_res_action_kw_list *kw_list; |
| 12827 | int i; |
| 12828 | |
| 12829 | list_for_each_entry(kw_list, &http_res_keywords.list, list) { |
| 12830 | for (i = 0; kw_list->kw[i].kw != NULL; i++) { |
| 12831 | if (!strcmp(kw, kw_list->kw[i].kw)) |
| 12832 | return &kw_list->kw[i]; |
| 12833 | } |
| 12834 | } |
| 12835 | } |
| 12836 | return NULL; |
| 12837 | } |
| 12838 | |
Willy Tarreau | 4a56897 | 2010-05-12 08:08:50 +0200 | [diff] [blame] | 12839 | /************************************************************************/ |
Willy Tarreau | 25c1ebc | 2012-04-25 16:21:44 +0200 | [diff] [blame] | 12840 | /* All supported ACL keywords must be declared here. */ |
| 12841 | /************************************************************************/ |
| 12842 | |
| 12843 | /* Note: must not be declared <const> as its list will be overwritten. |
| 12844 | * Please take care of keeping this list alphabetically sorted. |
| 12845 | */ |
Willy Tarreau | dc13c11 | 2013-06-21 23:16:39 +0200 | [diff] [blame] | 12846 | static struct acl_kw_list acl_kws = {ILH, { |
Thierry FOURNIER | c5a4e98 | 2014-03-05 16:07:08 +0100 | [diff] [blame] | 12847 | { "base", "base", PAT_MATCH_STR }, |
| 12848 | { "base_beg", "base", PAT_MATCH_BEG }, |
| 12849 | { "base_dir", "base", PAT_MATCH_DIR }, |
| 12850 | { "base_dom", "base", PAT_MATCH_DOM }, |
| 12851 | { "base_end", "base", PAT_MATCH_END }, |
| 12852 | { "base_len", "base", PAT_MATCH_LEN }, |
| 12853 | { "base_reg", "base", PAT_MATCH_REG }, |
| 12854 | { "base_sub", "base", PAT_MATCH_SUB }, |
Willy Tarreau | a7ad50c | 2012-04-29 15:39:40 +0200 | [diff] [blame] | 12855 | |
Thierry FOURNIER | c5a4e98 | 2014-03-05 16:07:08 +0100 | [diff] [blame] | 12856 | { "cook", "req.cook", PAT_MATCH_STR }, |
| 12857 | { "cook_beg", "req.cook", PAT_MATCH_BEG }, |
| 12858 | { "cook_dir", "req.cook", PAT_MATCH_DIR }, |
| 12859 | { "cook_dom", "req.cook", PAT_MATCH_DOM }, |
| 12860 | { "cook_end", "req.cook", PAT_MATCH_END }, |
| 12861 | { "cook_len", "req.cook", PAT_MATCH_LEN }, |
| 12862 | { "cook_reg", "req.cook", PAT_MATCH_REG }, |
| 12863 | { "cook_sub", "req.cook", PAT_MATCH_SUB }, |
Willy Tarreau | 25c1ebc | 2012-04-25 16:21:44 +0200 | [diff] [blame] | 12864 | |
Thierry FOURNIER | c5a4e98 | 2014-03-05 16:07:08 +0100 | [diff] [blame] | 12865 | { "hdr", "req.hdr", PAT_MATCH_STR }, |
| 12866 | { "hdr_beg", "req.hdr", PAT_MATCH_BEG }, |
| 12867 | { "hdr_dir", "req.hdr", PAT_MATCH_DIR }, |
| 12868 | { "hdr_dom", "req.hdr", PAT_MATCH_DOM }, |
| 12869 | { "hdr_end", "req.hdr", PAT_MATCH_END }, |
| 12870 | { "hdr_len", "req.hdr", PAT_MATCH_LEN }, |
| 12871 | { "hdr_reg", "req.hdr", PAT_MATCH_REG }, |
| 12872 | { "hdr_sub", "req.hdr", PAT_MATCH_SUB }, |
Willy Tarreau | 25c1ebc | 2012-04-25 16:21:44 +0200 | [diff] [blame] | 12873 | |
Thierry FOURNIER | c5a4e98 | 2014-03-05 16:07:08 +0100 | [diff] [blame] | 12874 | /* these two declarations uses strings with list storage (in place |
| 12875 | * of tree storage). The basic match is PAT_MATCH_STR, but the indexation |
| 12876 | * and delete functions are relative to the list management. The parse |
| 12877 | * and match method are related to the corresponding fetch methods. This |
| 12878 | * is very particular ACL declaration mode. |
| 12879 | */ |
| 12880 | { "http_auth_group", NULL, PAT_MATCH_STR, NULL, pat_idx_list_str, pat_del_list_ptr, NULL, pat_match_auth }, |
| 12881 | { "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] | 12882 | |
Thierry FOURNIER | c5a4e98 | 2014-03-05 16:07:08 +0100 | [diff] [blame] | 12883 | { "path", "path", PAT_MATCH_STR }, |
| 12884 | { "path_beg", "path", PAT_MATCH_BEG }, |
| 12885 | { "path_dir", "path", PAT_MATCH_DIR }, |
| 12886 | { "path_dom", "path", PAT_MATCH_DOM }, |
| 12887 | { "path_end", "path", PAT_MATCH_END }, |
| 12888 | { "path_len", "path", PAT_MATCH_LEN }, |
| 12889 | { "path_reg", "path", PAT_MATCH_REG }, |
| 12890 | { "path_sub", "path", PAT_MATCH_SUB }, |
Willy Tarreau | 25c1ebc | 2012-04-25 16:21:44 +0200 | [diff] [blame] | 12891 | |
Thierry FOURNIER | c5a4e98 | 2014-03-05 16:07:08 +0100 | [diff] [blame] | 12892 | { "req_ver", "req.ver", PAT_MATCH_STR }, |
| 12893 | { "resp_ver", "res.ver", PAT_MATCH_STR }, |
Willy Tarreau | 25c1ebc | 2012-04-25 16:21:44 +0200 | [diff] [blame] | 12894 | |
Thierry FOURNIER | c5a4e98 | 2014-03-05 16:07:08 +0100 | [diff] [blame] | 12895 | { "scook", "res.cook", PAT_MATCH_STR }, |
| 12896 | { "scook_beg", "res.cook", PAT_MATCH_BEG }, |
| 12897 | { "scook_dir", "res.cook", PAT_MATCH_DIR }, |
| 12898 | { "scook_dom", "res.cook", PAT_MATCH_DOM }, |
| 12899 | { "scook_end", "res.cook", PAT_MATCH_END }, |
| 12900 | { "scook_len", "res.cook", PAT_MATCH_LEN }, |
| 12901 | { "scook_reg", "res.cook", PAT_MATCH_REG }, |
| 12902 | { "scook_sub", "res.cook", PAT_MATCH_SUB }, |
Willy Tarreau | 25c1ebc | 2012-04-25 16:21:44 +0200 | [diff] [blame] | 12903 | |
Thierry FOURNIER | c5a4e98 | 2014-03-05 16:07:08 +0100 | [diff] [blame] | 12904 | { "shdr", "res.hdr", PAT_MATCH_STR }, |
| 12905 | { "shdr_beg", "res.hdr", PAT_MATCH_BEG }, |
| 12906 | { "shdr_dir", "res.hdr", PAT_MATCH_DIR }, |
| 12907 | { "shdr_dom", "res.hdr", PAT_MATCH_DOM }, |
| 12908 | { "shdr_end", "res.hdr", PAT_MATCH_END }, |
| 12909 | { "shdr_len", "res.hdr", PAT_MATCH_LEN }, |
| 12910 | { "shdr_reg", "res.hdr", PAT_MATCH_REG }, |
| 12911 | { "shdr_sub", "res.hdr", PAT_MATCH_SUB }, |
Willy Tarreau | 25c1ebc | 2012-04-25 16:21:44 +0200 | [diff] [blame] | 12912 | |
Thierry FOURNIER | c5a4e98 | 2014-03-05 16:07:08 +0100 | [diff] [blame] | 12913 | { "url", "url", PAT_MATCH_STR }, |
| 12914 | { "url_beg", "url", PAT_MATCH_BEG }, |
| 12915 | { "url_dir", "url", PAT_MATCH_DIR }, |
| 12916 | { "url_dom", "url", PAT_MATCH_DOM }, |
| 12917 | { "url_end", "url", PAT_MATCH_END }, |
| 12918 | { "url_len", "url", PAT_MATCH_LEN }, |
| 12919 | { "url_reg", "url", PAT_MATCH_REG }, |
| 12920 | { "url_sub", "url", PAT_MATCH_SUB }, |
Willy Tarreau | 25c1ebc | 2012-04-25 16:21:44 +0200 | [diff] [blame] | 12921 | |
Thierry FOURNIER | c5a4e98 | 2014-03-05 16:07:08 +0100 | [diff] [blame] | 12922 | { "urlp", "urlp", PAT_MATCH_STR }, |
| 12923 | { "urlp_beg", "urlp", PAT_MATCH_BEG }, |
| 12924 | { "urlp_dir", "urlp", PAT_MATCH_DIR }, |
| 12925 | { "urlp_dom", "urlp", PAT_MATCH_DOM }, |
| 12926 | { "urlp_end", "urlp", PAT_MATCH_END }, |
| 12927 | { "urlp_len", "urlp", PAT_MATCH_LEN }, |
| 12928 | { "urlp_reg", "urlp", PAT_MATCH_REG }, |
| 12929 | { "urlp_sub", "urlp", PAT_MATCH_SUB }, |
Willy Tarreau | 25c1ebc | 2012-04-25 16:21:44 +0200 | [diff] [blame] | 12930 | |
Willy Tarreau | 8ed669b | 2013-01-11 15:49:37 +0100 | [diff] [blame] | 12931 | { /* END */ }, |
Willy Tarreau | 25c1ebc | 2012-04-25 16:21:44 +0200 | [diff] [blame] | 12932 | }}; |
| 12933 | |
| 12934 | /************************************************************************/ |
| 12935 | /* All supported pattern keywords must be declared here. */ |
Willy Tarreau | 4a56897 | 2010-05-12 08:08:50 +0200 | [diff] [blame] | 12936 | /************************************************************************/ |
| 12937 | /* Note: must not be declared <const> as its list will be overwritten */ |
Willy Tarreau | dc13c11 | 2013-06-21 23:16:39 +0200 | [diff] [blame] | 12938 | static struct sample_fetch_kw_list sample_fetch_keywords = {ILH, { |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 12939 | { "base", smp_fetch_base, 0, NULL, SMP_T_STR, SMP_USE_HRQHV }, |
Willy Tarreau | 409bcde | 2013-01-08 00:31:00 +0100 | [diff] [blame] | 12940 | { "base32", smp_fetch_base32, 0, NULL, SMP_T_UINT, SMP_USE_HRQHV }, |
| 12941 | { "base32+src", smp_fetch_base32_src, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV }, |
| 12942 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 12943 | /* capture are allocated and are permanent in the stream */ |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 12944 | { "capture.req.hdr", smp_fetch_capture_header_req, ARG1(1, UINT), NULL, SMP_T_STR, SMP_USE_HRQHP }, |
Willy Tarreau | 3c1b5ec | 2014-04-24 23:41:57 +0200 | [diff] [blame] | 12945 | |
| 12946 | /* retrieve these captures from the HTTP logs */ |
| 12947 | { "capture.req.method", smp_fetch_capture_req_method, 0, NULL, SMP_T_STR, SMP_USE_HRQHP }, |
| 12948 | { "capture.req.uri", smp_fetch_capture_req_uri, 0, NULL, SMP_T_STR, SMP_USE_HRQHP }, |
| 12949 | { "capture.req.ver", smp_fetch_capture_req_ver, 0, NULL, SMP_T_STR, SMP_USE_HRQHP }, |
| 12950 | |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 12951 | { "capture.res.hdr", smp_fetch_capture_header_res, ARG1(1, UINT), NULL, SMP_T_STR, SMP_USE_HRSHP }, |
Willy Tarreau | 3c1b5ec | 2014-04-24 23:41:57 +0200 | [diff] [blame] | 12952 | { "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] | 12953 | |
Willy Tarreau | 409bcde | 2013-01-08 00:31:00 +0100 | [diff] [blame] | 12954 | /* cookie is valid in both directions (eg: for "stick ...") but cook* |
| 12955 | * are only here to match the ACL's name, are request-only and are used |
| 12956 | * for ACL compatibility only. |
| 12957 | */ |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 12958 | { "cook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRQHV }, |
| 12959 | { "cookie", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRQHV|SMP_USE_HRSHV }, |
Willy Tarreau | 409bcde | 2013-01-08 00:31:00 +0100 | [diff] [blame] | 12960 | { "cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV }, |
| 12961 | { "cook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV }, |
| 12962 | |
| 12963 | /* hdr is valid in both directions (eg: for "stick ...") but hdr_* are |
| 12964 | * only here to match the ACL's name, are request-only and are used for |
| 12965 | * ACL compatibility only. |
| 12966 | */ |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 12967 | { "hdr", smp_fetch_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRQHV|SMP_USE_HRSHV }, |
Willy Tarreau | 409bcde | 2013-01-08 00:31:00 +0100 | [diff] [blame] | 12968 | { "hdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV }, |
| 12969 | { "hdr_ip", smp_fetch_hdr_ip, ARG2(0,STR,SINT), val_hdr, SMP_T_IPV4, SMP_USE_HRQHV }, |
| 12970 | { "hdr_val", smp_fetch_hdr_val, ARG2(0,STR,SINT), val_hdr, SMP_T_UINT, SMP_USE_HRQHV }, |
| 12971 | |
Willy Tarreau | 0a0daec | 2013-04-02 22:44:58 +0200 | [diff] [blame] | 12972 | { "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] | 12973 | { "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] | 12974 | { "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] | 12975 | { "method", smp_fetch_meth, 0, NULL, SMP_T_METH, SMP_USE_HRQHP }, |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 12976 | { "path", smp_fetch_path, 0, NULL, SMP_T_STR, SMP_USE_HRQHV }, |
Willy Tarreau | 49ad95c | 2015-01-19 15:06:26 +0100 | [diff] [blame] | 12977 | { "query", smp_fetch_query, 0, NULL, SMP_T_STR, SMP_USE_HRQHV }, |
Willy Tarreau | 18ed256 | 2013-01-14 15:56:36 +0100 | [diff] [blame] | 12978 | |
| 12979 | /* HTTP protocol on the request path */ |
| 12980 | { "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] | 12981 | { "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] | 12982 | |
| 12983 | /* HTTP version on the request path */ |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 12984 | { "req.ver", smp_fetch_rqver, 0, NULL, SMP_T_STR, SMP_USE_HRQHV }, |
| 12985 | { "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] | 12986 | |
Willy Tarreau | a5910cc | 2015-05-02 00:46:08 +0200 | [diff] [blame] | 12987 | { "req.body", smp_fetch_body, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV }, |
| 12988 | { "req.body_len", smp_fetch_body_len, 0, NULL, SMP_T_UINT, SMP_USE_HRQHV }, |
| 12989 | { "req.body_size", smp_fetch_body_size, 0, NULL, SMP_T_UINT, SMP_USE_HRQHV }, |
Thierry FOURNIER | e28c499 | 2015-05-19 14:45:09 +0200 | [diff] [blame] | 12990 | { "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] | 12991 | |
Willy Tarreau | 18ed256 | 2013-01-14 15:56:36 +0100 | [diff] [blame] | 12992 | /* HTTP version on the response path */ |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 12993 | { "res.ver", smp_fetch_stver, 0, NULL, SMP_T_STR, SMP_USE_HRSHV }, |
| 12994 | { "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] | 12995 | |
Willy Tarreau | 18ed256 | 2013-01-14 15:56:36 +0100 | [diff] [blame] | 12996 | /* 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] | 12997 | { "req.cook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRQHV }, |
Willy Tarreau | 18ed256 | 2013-01-14 15:56:36 +0100 | [diff] [blame] | 12998 | { "req.cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV }, |
| 12999 | { "req.cook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV }, |
| 13000 | |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 13001 | { "req.fhdr", smp_fetch_fhdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRQHV }, |
Willy Tarreau | 04ff9f1 | 2013-06-10 18:39:42 +0200 | [diff] [blame] | 13002 | { "req.fhdr_cnt", smp_fetch_fhdr_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV }, |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 13003 | { "req.hdr", smp_fetch_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRQHV }, |
Willy Tarreau | 18ed256 | 2013-01-14 15:56:36 +0100 | [diff] [blame] | 13004 | { "req.hdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV }, |
| 13005 | { "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] | 13006 | { "req.hdr_names", smp_fetch_hdr_names, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRQHV }, |
Willy Tarreau | 18ed256 | 2013-01-14 15:56:36 +0100 | [diff] [blame] | 13007 | { "req.hdr_val", smp_fetch_hdr_val, ARG2(0,STR,SINT), val_hdr, SMP_T_UINT, SMP_USE_HRQHV }, |
| 13008 | |
| 13009 | /* 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] | 13010 | { "res.cook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRSHV }, |
Willy Tarreau | 18ed256 | 2013-01-14 15:56:36 +0100 | [diff] [blame] | 13011 | { "res.cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRSHV }, |
| 13012 | { "res.cook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRSHV }, |
| 13013 | |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 13014 | { "res.fhdr", smp_fetch_fhdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRSHV }, |
Willy Tarreau | 04ff9f1 | 2013-06-10 18:39:42 +0200 | [diff] [blame] | 13015 | { "res.fhdr_cnt", smp_fetch_fhdr_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRSHV }, |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 13016 | { "res.hdr", smp_fetch_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRSHV }, |
Willy Tarreau | 18ed256 | 2013-01-14 15:56:36 +0100 | [diff] [blame] | 13017 | { "res.hdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRSHV }, |
| 13018 | { "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] | 13019 | { "res.hdr_names", smp_fetch_hdr_names, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRSHV }, |
Willy Tarreau | 18ed256 | 2013-01-14 15:56:36 +0100 | [diff] [blame] | 13020 | { "res.hdr_val", smp_fetch_hdr_val, ARG2(0,STR,SINT), val_hdr, SMP_T_UINT, SMP_USE_HRSHV }, |
| 13021 | |
Willy Tarreau | 409bcde | 2013-01-08 00:31:00 +0100 | [diff] [blame] | 13022 | /* 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] | 13023 | { "scook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRSHV }, |
Willy Tarreau | 409bcde | 2013-01-08 00:31:00 +0100 | [diff] [blame] | 13024 | { "scook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRSHV }, |
| 13025 | { "scook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRSHV }, |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 13026 | { "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] | 13027 | |
| 13028 | /* 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] | 13029 | { "shdr", smp_fetch_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRSHV }, |
Willy Tarreau | 409bcde | 2013-01-08 00:31:00 +0100 | [diff] [blame] | 13030 | { "shdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRSHV }, |
| 13031 | { "shdr_ip", smp_fetch_hdr_ip, ARG2(0,STR,SINT), val_hdr, SMP_T_IPV4, SMP_USE_HRSHV }, |
| 13032 | { "shdr_val", smp_fetch_hdr_val, ARG2(0,STR,SINT), val_hdr, SMP_T_UINT, SMP_USE_HRSHV }, |
| 13033 | |
| 13034 | { "status", smp_fetch_stcode, 0, NULL, SMP_T_UINT, SMP_USE_HRSHP }, |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 13035 | { "url", smp_fetch_url, 0, NULL, SMP_T_STR, SMP_USE_HRQHV }, |
Neil - HAProxy List | 39c63c5 | 2013-11-04 13:48:42 +0000 | [diff] [blame] | 13036 | { "url32", smp_fetch_url32, 0, NULL, SMP_T_UINT, SMP_USE_HRQHV }, |
| 13037 | { "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] | 13038 | { "url_ip", smp_fetch_url_ip, 0, NULL, SMP_T_IPV4, SMP_USE_HRQHV }, |
| 13039 | { "url_port", smp_fetch_url_port, 0, NULL, SMP_T_UINT, SMP_USE_HRQHV }, |
Willy Tarreau | 1ede1da | 2015-05-07 16:06:18 +0200 | [diff] [blame] | 13040 | { "url_param", smp_fetch_url_param, ARG2(0,STR,STR), NULL, SMP_T_STR, SMP_USE_HRQHV }, |
| 13041 | { "urlp" , smp_fetch_url_param, ARG2(0,STR,STR), NULL, SMP_T_STR, SMP_USE_HRQHV }, |
| 13042 | { "urlp_val", smp_fetch_url_param_val, ARG2(0,STR,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV }, |
Willy Tarreau | 409bcde | 2013-01-08 00:31:00 +0100 | [diff] [blame] | 13043 | { /* END */ }, |
Willy Tarreau | 4a56897 | 2010-05-12 08:08:50 +0200 | [diff] [blame] | 13044 | }}; |
| 13045 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 13046 | |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 13047 | /************************************************************************/ |
| 13048 | /* All supported converter keywords must be declared here. */ |
| 13049 | /************************************************************************/ |
Willy Tarreau | 276fae9 | 2013-07-25 14:36:01 +0200 | [diff] [blame] | 13050 | /* Note: must not be declared <const> as its list will be overwritten */ |
| 13051 | static struct sample_conv_kw_list sample_conv_kws = {ILH, { |
Thierry FOURNIER | ad90351 | 2014-04-11 17:51:01 +0200 | [diff] [blame] | 13052 | { "http_date", sample_conv_http_date, ARG1(0,SINT), NULL, SMP_T_UINT, SMP_T_STR}, |
| 13053 | { "language", sample_conv_q_prefered, ARG2(1,STR,STR), NULL, SMP_T_STR, SMP_T_STR}, |
Thierry FOURNIER | 35ab275 | 2015-05-28 13:22:03 +0200 | [diff] [blame] | 13054 | { "capture-req", smp_conv_req_capture, ARG1(1,UINT), NULL, SMP_T_STR, SMP_T_STR}, |
| 13055 | { "capture-res", smp_conv_res_capture, ARG1(1,UINT), NULL, SMP_T_STR, SMP_T_STR}, |
Thierry FOURNIER | 82ff3c9 | 2015-05-07 15:46:20 +0200 | [diff] [blame] | 13056 | { "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] | 13057 | { NULL, NULL, 0, 0, 0 }, |
| 13058 | }}; |
| 13059 | |
Thierry FOURNIER | 35ab275 | 2015-05-28 13:22:03 +0200 | [diff] [blame] | 13060 | |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 13061 | /************************************************************************/ |
| 13062 | /* All supported http-request action keywords must be declared here. */ |
| 13063 | /************************************************************************/ |
| 13064 | struct http_req_action_kw_list http_req_actions = { |
| 13065 | .scope = "http", |
| 13066 | .kw = { |
Willy Tarreau | a9083d0 | 2015-05-08 15:27:59 +0200 | [diff] [blame] | 13067 | { "capture", parse_http_req_capture }, |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 13068 | { "set-method", parse_set_req_line }, |
| 13069 | { "set-path", parse_set_req_line }, |
| 13070 | { "set-query", parse_set_req_line }, |
| 13071 | { "set-uri", parse_set_req_line }, |
Willy Tarreau | cb703b0 | 2015-04-03 09:52:01 +0200 | [diff] [blame] | 13072 | { NULL, NULL } |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 13073 | } |
| 13074 | }; |
| 13075 | |
Thierry FOURNIER | e80fada | 2015-05-26 18:06:31 +0200 | [diff] [blame] | 13076 | struct http_res_action_kw_list http_res_actions = { |
| 13077 | .scope = "http", |
| 13078 | .kw = { |
| 13079 | { "capture", parse_http_res_capture }, |
| 13080 | { NULL, NULL } |
| 13081 | } |
| 13082 | }; |
| 13083 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 13084 | __attribute__((constructor)) |
| 13085 | static void __http_protocol_init(void) |
| 13086 | { |
| 13087 | acl_register_keywords(&acl_kws); |
Willy Tarreau | 1278578 | 2012-04-27 21:37:17 +0200 | [diff] [blame] | 13088 | sample_register_fetches(&sample_fetch_keywords); |
Willy Tarreau | 276fae9 | 2013-07-25 14:36:01 +0200 | [diff] [blame] | 13089 | sample_register_convs(&sample_conv_kws); |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 13090 | http_req_keywords_register(&http_req_actions); |
Thierry FOURNIER | e80fada | 2015-05-26 18:06:31 +0200 | [diff] [blame] | 13091 | http_res_keywords_register(&http_res_actions); |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 13092 | } |
| 13093 | |
| 13094 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 13095 | /* |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 13096 | * Local variables: |
| 13097 | * c-indent-level: 8 |
| 13098 | * c-basic-offset: 8 |
| 13099 | * End: |
| 13100 | */ |