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 | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 64 | #include <proto/session.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> |
| 67 | |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 68 | const char HTTP_100[] = |
| 69 | "HTTP/1.1 100 Continue\r\n\r\n"; |
| 70 | |
| 71 | const struct chunk http_100_chunk = { |
| 72 | .str = (char *)&HTTP_100, |
| 73 | .len = sizeof(HTTP_100)-1 |
| 74 | }; |
| 75 | |
Willy Tarreau | a9679ac | 2010-01-03 17:32:57 +0100 | [diff] [blame] | 76 | /* Warning: no "connection" header is provided with the 3xx messages below */ |
Willy Tarreau | b463dfb | 2008-06-07 23:08:56 +0200 | [diff] [blame] | 77 | const char *HTTP_301 = |
Willy Tarreau | bc5aa19 | 2010-01-03 15:09:36 +0100 | [diff] [blame] | 78 | "HTTP/1.1 301 Moved Permanently\r\n" |
Willy Tarreau | bc5aa19 | 2010-01-03 15:09:36 +0100 | [diff] [blame] | 79 | "Content-length: 0\r\n" |
Willy Tarreau | b463dfb | 2008-06-07 23:08:56 +0200 | [diff] [blame] | 80 | "Location: "; /* not terminated since it will be concatenated with the URL */ |
| 81 | |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 82 | const char *HTTP_302 = |
Willy Tarreau | bc5aa19 | 2010-01-03 15:09:36 +0100 | [diff] [blame] | 83 | "HTTP/1.1 302 Found\r\n" |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 84 | "Cache-Control: no-cache\r\n" |
Willy Tarreau | bc5aa19 | 2010-01-03 15:09:36 +0100 | [diff] [blame] | 85 | "Content-length: 0\r\n" |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 86 | "Location: "; /* not terminated since it will be concatenated with the URL */ |
| 87 | |
| 88 | /* same as 302 except that the browser MUST retry with the GET method */ |
| 89 | const char *HTTP_303 = |
Willy Tarreau | bc5aa19 | 2010-01-03 15:09:36 +0100 | [diff] [blame] | 90 | "HTTP/1.1 303 See Other\r\n" |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 91 | "Cache-Control: no-cache\r\n" |
Willy Tarreau | bc5aa19 | 2010-01-03 15:09:36 +0100 | [diff] [blame] | 92 | "Content-length: 0\r\n" |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 93 | "Location: "; /* not terminated since it will be concatenated with the URL */ |
| 94 | |
Yves Lafon | 3e8d1ae | 2013-03-11 11:06:05 -0400 | [diff] [blame] | 95 | |
| 96 | /* same as 302 except that the browser MUST retry with the same method */ |
| 97 | const char *HTTP_307 = |
| 98 | "HTTP/1.1 307 Temporary Redirect\r\n" |
| 99 | "Cache-Control: no-cache\r\n" |
| 100 | "Content-length: 0\r\n" |
| 101 | "Location: "; /* not terminated since it will be concatenated with the URL */ |
| 102 | |
| 103 | /* same as 301 except that the browser MUST retry with the same method */ |
| 104 | const char *HTTP_308 = |
| 105 | "HTTP/1.1 308 Permanent Redirect\r\n" |
| 106 | "Content-length: 0\r\n" |
| 107 | "Location: "; /* not terminated since it will be concatenated with the URL */ |
| 108 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 109 | /* Warning: this one is an sprintf() fmt string, with <realm> as its only argument */ |
| 110 | const char *HTTP_401_fmt = |
| 111 | "HTTP/1.0 401 Unauthorized\r\n" |
| 112 | "Cache-Control: no-cache\r\n" |
| 113 | "Connection: close\r\n" |
Willy Tarreau | 791d66d | 2006-07-08 16:53:38 +0200 | [diff] [blame] | 114 | "Content-Type: text/html\r\n" |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 115 | "WWW-Authenticate: Basic realm=\"%s\"\r\n" |
| 116 | "\r\n" |
| 117 | "<html><body><h1>401 Unauthorized</h1>\nYou need a valid user and password to access this content.\n</body></html>\n"; |
| 118 | |
Willy Tarreau | 844a7e7 | 2010-01-31 21:46:18 +0100 | [diff] [blame] | 119 | const char *HTTP_407_fmt = |
| 120 | "HTTP/1.0 407 Unauthorized\r\n" |
| 121 | "Cache-Control: no-cache\r\n" |
| 122 | "Connection: close\r\n" |
| 123 | "Content-Type: text/html\r\n" |
| 124 | "Proxy-Authenticate: Basic realm=\"%s\"\r\n" |
| 125 | "\r\n" |
| 126 | "<html><body><h1>401 Unauthorized</h1>\nYou need a valid user and password to access this content.\n</body></html>\n"; |
| 127 | |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 128 | |
| 129 | const int http_err_codes[HTTP_ERR_SIZE] = { |
Willy Tarreau | ae94d4d | 2011-05-11 16:28:49 +0200 | [diff] [blame] | 130 | [HTTP_ERR_200] = 200, /* used by "monitor-uri" */ |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 131 | [HTTP_ERR_400] = 400, |
| 132 | [HTTP_ERR_403] = 403, |
| 133 | [HTTP_ERR_408] = 408, |
| 134 | [HTTP_ERR_500] = 500, |
| 135 | [HTTP_ERR_502] = 502, |
| 136 | [HTTP_ERR_503] = 503, |
| 137 | [HTTP_ERR_504] = 504, |
| 138 | }; |
| 139 | |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 140 | static const char *http_err_msgs[HTTP_ERR_SIZE] = { |
Willy Tarreau | ae94d4d | 2011-05-11 16:28:49 +0200 | [diff] [blame] | 141 | [HTTP_ERR_200] = |
| 142 | "HTTP/1.0 200 OK\r\n" |
| 143 | "Cache-Control: no-cache\r\n" |
| 144 | "Connection: close\r\n" |
| 145 | "Content-Type: text/html\r\n" |
| 146 | "\r\n" |
| 147 | "<html><body><h1>200 OK</h1>\nService ready.\n</body></html>\n", |
| 148 | |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 149 | [HTTP_ERR_400] = |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 150 | "HTTP/1.0 400 Bad request\r\n" |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 151 | "Cache-Control: no-cache\r\n" |
| 152 | "Connection: close\r\n" |
| 153 | "Content-Type: text/html\r\n" |
| 154 | "\r\n" |
| 155 | "<html><body><h1>400 Bad request</h1>\nYour browser sent an invalid request.\n</body></html>\n", |
| 156 | |
| 157 | [HTTP_ERR_403] = |
| 158 | "HTTP/1.0 403 Forbidden\r\n" |
| 159 | "Cache-Control: no-cache\r\n" |
| 160 | "Connection: close\r\n" |
| 161 | "Content-Type: text/html\r\n" |
| 162 | "\r\n" |
| 163 | "<html><body><h1>403 Forbidden</h1>\nRequest forbidden by administrative rules.\n</body></html>\n", |
| 164 | |
| 165 | [HTTP_ERR_408] = |
| 166 | "HTTP/1.0 408 Request Time-out\r\n" |
| 167 | "Cache-Control: no-cache\r\n" |
| 168 | "Connection: close\r\n" |
| 169 | "Content-Type: text/html\r\n" |
| 170 | "\r\n" |
| 171 | "<html><body><h1>408 Request Time-out</h1>\nYour browser didn't send a complete request in time.\n</body></html>\n", |
| 172 | |
| 173 | [HTTP_ERR_500] = |
| 174 | "HTTP/1.0 500 Server Error\r\n" |
| 175 | "Cache-Control: no-cache\r\n" |
| 176 | "Connection: close\r\n" |
| 177 | "Content-Type: text/html\r\n" |
| 178 | "\r\n" |
| 179 | "<html><body><h1>500 Server Error</h1>\nAn internal server error occured.\n</body></html>\n", |
| 180 | |
| 181 | [HTTP_ERR_502] = |
| 182 | "HTTP/1.0 502 Bad Gateway\r\n" |
| 183 | "Cache-Control: no-cache\r\n" |
| 184 | "Connection: close\r\n" |
| 185 | "Content-Type: text/html\r\n" |
| 186 | "\r\n" |
| 187 | "<html><body><h1>502 Bad Gateway</h1>\nThe server returned an invalid or incomplete response.\n</body></html>\n", |
| 188 | |
| 189 | [HTTP_ERR_503] = |
| 190 | "HTTP/1.0 503 Service Unavailable\r\n" |
| 191 | "Cache-Control: no-cache\r\n" |
| 192 | "Connection: close\r\n" |
| 193 | "Content-Type: text/html\r\n" |
| 194 | "\r\n" |
| 195 | "<html><body><h1>503 Service Unavailable</h1>\nNo server is available to handle this request.\n</body></html>\n", |
| 196 | |
| 197 | [HTTP_ERR_504] = |
| 198 | "HTTP/1.0 504 Gateway Time-out\r\n" |
| 199 | "Cache-Control: no-cache\r\n" |
| 200 | "Connection: close\r\n" |
| 201 | "Content-Type: text/html\r\n" |
| 202 | "\r\n" |
| 203 | "<html><body><h1>504 Gateway Time-out</h1>\nThe server didn't respond in time.\n</body></html>\n", |
| 204 | |
| 205 | }; |
| 206 | |
Cyril Bonté | 19979e1 | 2012-04-04 12:57:21 +0200 | [diff] [blame] | 207 | /* status codes available for the stats admin page (strictly 4 chars length) */ |
| 208 | const char *stat_status_codes[STAT_STATUS_SIZE] = { |
| 209 | [STAT_STATUS_DENY] = "DENY", |
| 210 | [STAT_STATUS_DONE] = "DONE", |
| 211 | [STAT_STATUS_ERRP] = "ERRP", |
| 212 | [STAT_STATUS_EXCD] = "EXCD", |
| 213 | [STAT_STATUS_NONE] = "NONE", |
| 214 | [STAT_STATUS_PART] = "PART", |
| 215 | [STAT_STATUS_UNKN] = "UNKN", |
| 216 | }; |
| 217 | |
| 218 | |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 219 | /* We must put the messages here since GCC cannot initialize consts depending |
| 220 | * on strlen(). |
| 221 | */ |
| 222 | struct chunk http_err_chunks[HTTP_ERR_SIZE]; |
| 223 | |
Willy Tarreau | a890d07 | 2013-04-02 12:01:06 +0200 | [diff] [blame] | 224 | /* this struct is used between calls to smp_fetch_hdr() or smp_fetch_cookie() */ |
| 225 | static struct hdr_ctx static_hdr_ctx; |
| 226 | |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 227 | #define FD_SETS_ARE_BITFIELDS |
| 228 | #ifdef FD_SETS_ARE_BITFIELDS |
| 229 | /* |
| 230 | * This map is used with all the FD_* macros to check whether a particular bit |
| 231 | * is set or not. Each bit represents an ACSII code. FD_SET() sets those bytes |
| 232 | * which should be encoded. When FD_ISSET() returns non-zero, it means that the |
| 233 | * byte should be encoded. Be careful to always pass bytes from 0 to 255 |
| 234 | * exclusively to the macros. |
| 235 | */ |
| 236 | fd_set hdr_encode_map[(sizeof(fd_set) > (256/8)) ? 1 : ((256/8) / sizeof(fd_set))]; |
| 237 | fd_set url_encode_map[(sizeof(fd_set) > (256/8)) ? 1 : ((256/8) / sizeof(fd_set))]; |
| 238 | |
| 239 | #else |
| 240 | #error "Check if your OS uses bitfields for fd_sets" |
| 241 | #endif |
| 242 | |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 243 | void init_proto_http() |
| 244 | { |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 245 | int i; |
| 246 | char *tmp; |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 247 | int msg; |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 248 | |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 249 | for (msg = 0; msg < HTTP_ERR_SIZE; msg++) { |
| 250 | if (!http_err_msgs[msg]) { |
| 251 | Alert("Internal error: no message defined for HTTP return code %d. Aborting.\n", msg); |
| 252 | abort(); |
| 253 | } |
| 254 | |
| 255 | http_err_chunks[msg].str = (char *)http_err_msgs[msg]; |
| 256 | http_err_chunks[msg].len = strlen(http_err_msgs[msg]); |
| 257 | } |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 258 | |
| 259 | /* initialize the log header encoding map : '{|}"#' should be encoded with |
| 260 | * '#' as prefix, as well as non-printable characters ( <32 or >= 127 ). |
| 261 | * URL encoding only requires '"', '#' to be encoded as well as non- |
| 262 | * printable characters above. |
| 263 | */ |
| 264 | memset(hdr_encode_map, 0, sizeof(hdr_encode_map)); |
| 265 | memset(url_encode_map, 0, sizeof(url_encode_map)); |
| 266 | for (i = 0; i < 32; i++) { |
| 267 | FD_SET(i, hdr_encode_map); |
| 268 | FD_SET(i, url_encode_map); |
| 269 | } |
| 270 | for (i = 127; i < 256; i++) { |
| 271 | FD_SET(i, hdr_encode_map); |
| 272 | FD_SET(i, url_encode_map); |
| 273 | } |
| 274 | |
| 275 | tmp = "\"#{|}"; |
| 276 | while (*tmp) { |
| 277 | FD_SET(*tmp, hdr_encode_map); |
| 278 | tmp++; |
| 279 | } |
| 280 | |
| 281 | tmp = "\"#"; |
| 282 | while (*tmp) { |
| 283 | FD_SET(*tmp, url_encode_map); |
| 284 | tmp++; |
| 285 | } |
Willy Tarreau | 332f8bf | 2007-05-13 21:36:56 +0200 | [diff] [blame] | 286 | |
| 287 | /* memory allocations */ |
| 288 | pool2_requri = create_pool("requri", REQURI_LEN, MEM_F_SHARED); |
William Lallemand | a73203e | 2012-03-12 12:48:57 +0100 | [diff] [blame] | 289 | pool2_uniqueid = create_pool("uniqueid", UNIQUEID_LEN, MEM_F_SHARED); |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 290 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 291 | |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 292 | /* |
| 293 | * We have 26 list of methods (1 per first letter), each of which can have |
| 294 | * up to 3 entries (2 valid, 1 null). |
| 295 | */ |
| 296 | struct http_method_desc { |
Willy Tarreau | c8987b3 | 2013-12-06 23:43:17 +0100 | [diff] [blame] | 297 | enum http_meth_t meth; |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 298 | int len; |
| 299 | const char text[8]; |
| 300 | }; |
| 301 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 302 | const struct http_method_desc http_methods[26][3] = { |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 303 | ['C' - 'A'] = { |
| 304 | [0] = { .meth = HTTP_METH_CONNECT , .len=7, .text="CONNECT" }, |
| 305 | }, |
| 306 | ['D' - 'A'] = { |
| 307 | [0] = { .meth = HTTP_METH_DELETE , .len=6, .text="DELETE" }, |
| 308 | }, |
| 309 | ['G' - 'A'] = { |
| 310 | [0] = { .meth = HTTP_METH_GET , .len=3, .text="GET" }, |
| 311 | }, |
| 312 | ['H' - 'A'] = { |
| 313 | [0] = { .meth = HTTP_METH_HEAD , .len=4, .text="HEAD" }, |
| 314 | }, |
| 315 | ['P' - 'A'] = { |
| 316 | [0] = { .meth = HTTP_METH_POST , .len=4, .text="POST" }, |
| 317 | [1] = { .meth = HTTP_METH_PUT , .len=3, .text="PUT" }, |
| 318 | }, |
| 319 | ['T' - 'A'] = { |
| 320 | [0] = { .meth = HTTP_METH_TRACE , .len=5, .text="TRACE" }, |
| 321 | }, |
| 322 | /* rest is empty like this : |
| 323 | * [1] = { .meth = HTTP_METH_NONE , .len=0, .text="" }, |
| 324 | */ |
| 325 | }; |
| 326 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 327 | /* 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] | 328 | * 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] | 329 | * RFC2616 for those chars. |
| 330 | */ |
| 331 | |
| 332 | const char http_is_spht[256] = { |
| 333 | [' '] = 1, ['\t'] = 1, |
| 334 | }; |
| 335 | |
| 336 | const char http_is_crlf[256] = { |
| 337 | ['\r'] = 1, ['\n'] = 1, |
| 338 | }; |
| 339 | |
| 340 | const char http_is_lws[256] = { |
| 341 | [' '] = 1, ['\t'] = 1, |
| 342 | ['\r'] = 1, ['\n'] = 1, |
| 343 | }; |
| 344 | |
| 345 | const char http_is_sep[256] = { |
| 346 | ['('] = 1, [')'] = 1, ['<'] = 1, ['>'] = 1, |
| 347 | ['@'] = 1, [','] = 1, [';'] = 1, [':'] = 1, |
| 348 | ['"'] = 1, ['/'] = 1, ['['] = 1, [']'] = 1, |
| 349 | ['{'] = 1, ['}'] = 1, ['?'] = 1, ['='] = 1, |
| 350 | [' '] = 1, ['\t'] = 1, ['\\'] = 1, |
| 351 | }; |
| 352 | |
| 353 | const char http_is_ctl[256] = { |
| 354 | [0 ... 31] = 1, |
| 355 | [127] = 1, |
| 356 | }; |
| 357 | |
| 358 | /* |
| 359 | * A token is any ASCII char that is neither a separator nor a CTL char. |
| 360 | * Do not overwrite values in assignment since gcc-2.95 will not handle |
| 361 | * them correctly. Instead, define every non-CTL char's status. |
| 362 | */ |
| 363 | const char http_is_token[256] = { |
| 364 | [' '] = 0, ['!'] = 1, ['"'] = 0, ['#'] = 1, |
| 365 | ['$'] = 1, ['%'] = 1, ['&'] = 1, ['\''] = 1, |
| 366 | ['('] = 0, [')'] = 0, ['*'] = 1, ['+'] = 1, |
| 367 | [','] = 0, ['-'] = 1, ['.'] = 1, ['/'] = 0, |
| 368 | ['0'] = 1, ['1'] = 1, ['2'] = 1, ['3'] = 1, |
| 369 | ['4'] = 1, ['5'] = 1, ['6'] = 1, ['7'] = 1, |
| 370 | ['8'] = 1, ['9'] = 1, [':'] = 0, [';'] = 0, |
| 371 | ['<'] = 0, ['='] = 0, ['>'] = 0, ['?'] = 0, |
| 372 | ['@'] = 0, ['A'] = 1, ['B'] = 1, ['C'] = 1, |
| 373 | ['D'] = 1, ['E'] = 1, ['F'] = 1, ['G'] = 1, |
| 374 | ['H'] = 1, ['I'] = 1, ['J'] = 1, ['K'] = 1, |
| 375 | ['L'] = 1, ['M'] = 1, ['N'] = 1, ['O'] = 1, |
| 376 | ['P'] = 1, ['Q'] = 1, ['R'] = 1, ['S'] = 1, |
| 377 | ['T'] = 1, ['U'] = 1, ['V'] = 1, ['W'] = 1, |
| 378 | ['X'] = 1, ['Y'] = 1, ['Z'] = 1, ['['] = 0, |
| 379 | ['\\'] = 0, [']'] = 0, ['^'] = 1, ['_'] = 1, |
| 380 | ['`'] = 1, ['a'] = 1, ['b'] = 1, ['c'] = 1, |
| 381 | ['d'] = 1, ['e'] = 1, ['f'] = 1, ['g'] = 1, |
| 382 | ['h'] = 1, ['i'] = 1, ['j'] = 1, ['k'] = 1, |
| 383 | ['l'] = 1, ['m'] = 1, ['n'] = 1, ['o'] = 1, |
| 384 | ['p'] = 1, ['q'] = 1, ['r'] = 1, ['s'] = 1, |
| 385 | ['t'] = 1, ['u'] = 1, ['v'] = 1, ['w'] = 1, |
| 386 | ['x'] = 1, ['y'] = 1, ['z'] = 1, ['{'] = 0, |
| 387 | ['|'] = 1, ['}'] = 0, ['~'] = 1, |
| 388 | }; |
| 389 | |
| 390 | |
Willy Tarreau | 4b89ad4 | 2007-03-04 18:13:58 +0100 | [diff] [blame] | 391 | /* |
| 392 | * An http ver_token is any ASCII which can be found in an HTTP version, |
| 393 | * which includes 'H', 'T', 'P', '/', '.' and any digit. |
| 394 | */ |
| 395 | const char http_is_ver_token[256] = { |
| 396 | ['.'] = 1, ['/'] = 1, |
| 397 | ['0'] = 1, ['1'] = 1, ['2'] = 1, ['3'] = 1, ['4'] = 1, |
| 398 | ['5'] = 1, ['6'] = 1, ['7'] = 1, ['8'] = 1, ['9'] = 1, |
| 399 | ['H'] = 1, ['P'] = 1, ['T'] = 1, |
| 400 | }; |
| 401 | |
| 402 | |
Willy Tarreau | 4af6f3a | 2007-03-18 22:36:26 +0100 | [diff] [blame] | 403 | /* |
Willy Tarreau | e988a79 | 2010-01-04 21:13:14 +0100 | [diff] [blame] | 404 | * Silent debug that outputs only in strace, using fd #-1. Trash is modified. |
| 405 | */ |
| 406 | #if defined(DEBUG_FSM) |
| 407 | static void http_silent_debug(int line, struct session *s) |
| 408 | { |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 409 | chunk_printf(&trash, |
| 410 | "[%04d] req: p=%d(%d) s=%d bf=%08x an=%08x data=%p size=%d l=%d w=%p r=%p o=%p sm=%d fw=%ld tf=%08x\n", |
| 411 | line, |
| 412 | s->si[0].state, s->si[0].fd, s->txn.req.msg_state, s->req->flags, s->req->analysers, |
| 413 | s->req->buf->data, s->req->buf->size, s->req->l, s->req->w, s->req->r, s->req->buf->p, s->req->buf->o, s->req->to_forward, s->txn.flags); |
| 414 | write(-1, trash.str, trash.len); |
Willy Tarreau | e988a79 | 2010-01-04 21:13:14 +0100 | [diff] [blame] | 415 | |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 416 | chunk_printf(&trash, |
| 417 | " %04d rep: p=%d(%d) s=%d bf=%08x an=%08x data=%p size=%d l=%d w=%p r=%p o=%p sm=%d fw=%ld\n", |
| 418 | line, |
| 419 | s->si[1].state, s->si[1].fd, s->txn.rsp.msg_state, s->rep->flags, s->rep->analysers, |
| 420 | s->rep->buf->data, s->rep->buf->size, s->rep->l, s->rep->w, s->rep->r, s->rep->buf->p, s->rep->buf->o, s->rep->to_forward); |
| 421 | write(-1, trash.str, trash.len); |
Willy Tarreau | e988a79 | 2010-01-04 21:13:14 +0100 | [diff] [blame] | 422 | } |
| 423 | #else |
| 424 | #define http_silent_debug(l,s) do { } while (0) |
| 425 | #endif |
| 426 | |
| 427 | /* |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 428 | * Adds a header and its CRLF at the tail of the message's buffer, just before |
| 429 | * 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] | 430 | * The header is also automatically added to the index <hdr_idx>, and the end |
| 431 | * of headers is automatically adjusted. The number of bytes added is returned |
| 432 | * on success, otherwise <0 is returned indicating an error. |
| 433 | */ |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 434 | 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] | 435 | { |
| 436 | int bytes, len; |
| 437 | |
| 438 | len = strlen(text); |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 439 | 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] | 440 | if (!bytes) |
| 441 | return -1; |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 442 | http_msg_move_end(msg, bytes); |
Willy Tarreau | 4af6f3a | 2007-03-18 22:36:26 +0100 | [diff] [blame] | 443 | return hdr_idx_add(len, 1, hdr_idx, hdr_idx->tail); |
| 444 | } |
| 445 | |
| 446 | /* |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 447 | * Adds a header and its CRLF at the tail of the message's buffer, just before |
| 448 | * 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] | 449 | * the buffer is only opened and the space reserved, but nothing is copied. |
| 450 | * The header is also automatically added to the index <hdr_idx>, and the end |
| 451 | * of headers is automatically adjusted. The number of bytes added is returned |
| 452 | * on success, otherwise <0 is returned indicating an error. |
| 453 | */ |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 454 | int http_header_add_tail2(struct http_msg *msg, |
| 455 | struct hdr_idx *hdr_idx, const char *text, int len) |
Willy Tarreau | 4af6f3a | 2007-03-18 22:36:26 +0100 | [diff] [blame] | 456 | { |
| 457 | int bytes; |
| 458 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 459 | 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] | 460 | if (!bytes) |
| 461 | return -1; |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 462 | http_msg_move_end(msg, bytes); |
Willy Tarreau | 4af6f3a | 2007-03-18 22:36:26 +0100 | [diff] [blame] | 463 | return hdr_idx_add(len, 1, hdr_idx, hdr_idx->tail); |
| 464 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 465 | |
| 466 | /* |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 467 | * Checks if <hdr> is exactly <name> for <len> chars, and ends with a colon. |
| 468 | * If so, returns the position of the first non-space character relative to |
| 469 | * <hdr>, or <end>-<hdr> if not found before. If no value is found, it tries |
| 470 | * to return a pointer to the place after the first space. Returns 0 if the |
| 471 | * header name does not match. Checks are case-insensitive. |
| 472 | */ |
| 473 | int http_header_match2(const char *hdr, const char *end, |
| 474 | const char *name, int len) |
| 475 | { |
| 476 | const char *val; |
| 477 | |
| 478 | if (hdr + len >= end) |
| 479 | return 0; |
| 480 | if (hdr[len] != ':') |
| 481 | return 0; |
| 482 | if (strncasecmp(hdr, name, len) != 0) |
| 483 | return 0; |
| 484 | val = hdr + len + 1; |
| 485 | while (val < end && HTTP_IS_SPHT(*val)) |
| 486 | val++; |
| 487 | if ((val >= end) && (len + 2 <= end - hdr)) |
| 488 | return len + 2; /* we may replace starting from second space */ |
| 489 | return val - hdr; |
| 490 | } |
| 491 | |
Willy Tarreau | 04ff9f1 | 2013-06-10 18:39:42 +0200 | [diff] [blame] | 492 | /* Find the first or next occurrence of header <name> in message buffer <sol> |
| 493 | * using headers index <idx>, and return it in the <ctx> structure. This |
| 494 | * structure holds everything necessary to use the header and find next |
| 495 | * occurrence. If its <idx> member is 0, the header is searched from the |
| 496 | * beginning. Otherwise, the next occurrence is returned. The function returns |
| 497 | * 1 when it finds a value, and 0 when there is no more. It is very similar to |
| 498 | * http_find_header2() except that it is designed to work with full-line headers |
| 499 | * whose comma is not a delimiter but is part of the syntax. As a special case, |
| 500 | * if ctx->val is NULL when searching for a new values of a header, the current |
| 501 | * header is rescanned. This allows rescanning after a header deletion. |
| 502 | */ |
| 503 | int http_find_full_header2(const char *name, int len, |
| 504 | char *sol, struct hdr_idx *idx, |
| 505 | struct hdr_ctx *ctx) |
| 506 | { |
| 507 | char *eol, *sov; |
| 508 | int cur_idx, old_idx; |
| 509 | |
| 510 | cur_idx = ctx->idx; |
| 511 | if (cur_idx) { |
| 512 | /* We have previously returned a header, let's search another one */ |
| 513 | sol = ctx->line; |
| 514 | eol = sol + idx->v[cur_idx].len; |
| 515 | goto next_hdr; |
| 516 | } |
| 517 | |
| 518 | /* first request for this header */ |
| 519 | sol += hdr_idx_first_pos(idx); |
| 520 | old_idx = 0; |
| 521 | cur_idx = hdr_idx_first_idx(idx); |
| 522 | while (cur_idx) { |
| 523 | eol = sol + idx->v[cur_idx].len; |
| 524 | |
| 525 | if (len == 0) { |
| 526 | /* No argument was passed, we want any header. |
| 527 | * To achieve this, we simply build a fake request. */ |
| 528 | while (sol + len < eol && sol[len] != ':') |
| 529 | len++; |
| 530 | name = sol; |
| 531 | } |
| 532 | |
| 533 | if ((len < eol - sol) && |
| 534 | (sol[len] == ':') && |
| 535 | (strncasecmp(sol, name, len) == 0)) { |
| 536 | ctx->del = len; |
| 537 | sov = sol + len + 1; |
| 538 | while (sov < eol && http_is_lws[(unsigned char)*sov]) |
| 539 | sov++; |
| 540 | |
| 541 | ctx->line = sol; |
| 542 | ctx->prev = old_idx; |
| 543 | ctx->idx = cur_idx; |
| 544 | ctx->val = sov - sol; |
| 545 | ctx->tws = 0; |
| 546 | while (eol > sov && http_is_lws[(unsigned char)*(eol - 1)]) { |
| 547 | eol--; |
| 548 | ctx->tws++; |
| 549 | } |
| 550 | ctx->vlen = eol - sov; |
| 551 | return 1; |
| 552 | } |
| 553 | next_hdr: |
| 554 | sol = eol + idx->v[cur_idx].cr + 1; |
| 555 | old_idx = cur_idx; |
| 556 | cur_idx = idx->v[cur_idx].next; |
| 557 | } |
| 558 | return 0; |
| 559 | } |
| 560 | |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 561 | /* Find the end of the header value contained between <s> and <e>. See RFC2616, |
| 562 | * par 2.2 for more information. Note that it requires a valid header to return |
| 563 | * a valid result. This works for headers defined as comma-separated lists. |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 564 | */ |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 565 | char *find_hdr_value_end(char *s, const char *e) |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 566 | { |
| 567 | int quoted, qdpair; |
| 568 | |
| 569 | quoted = qdpair = 0; |
| 570 | for (; s < e; s++) { |
| 571 | if (qdpair) qdpair = 0; |
Willy Tarreau | 0f7f51f | 2010-08-30 11:06:34 +0200 | [diff] [blame] | 572 | else if (quoted) { |
| 573 | if (*s == '\\') qdpair = 1; |
| 574 | else if (*s == '"') quoted = 0; |
| 575 | } |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 576 | else if (*s == '"') quoted = 1; |
| 577 | else if (*s == ',') return s; |
| 578 | } |
| 579 | return s; |
| 580 | } |
| 581 | |
| 582 | /* Find the first or next occurrence of header <name> in message buffer <sol> |
| 583 | * using headers index <idx>, and return it in the <ctx> structure. This |
| 584 | * structure holds everything necessary to use the header and find next |
| 585 | * occurrence. If its <idx> member is 0, the header is searched from the |
| 586 | * beginning. Otherwise, the next occurrence is returned. The function returns |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 587 | * 1 when it finds a value, and 0 when there is no more. It is designed to work |
| 588 | * with headers defined as comma-separated lists. As a special case, if ctx->val |
| 589 | * is NULL when searching for a new values of a header, the current header is |
| 590 | * rescanned. This allows rescanning after a header deletion. |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 591 | */ |
| 592 | int http_find_header2(const char *name, int len, |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 593 | char *sol, struct hdr_idx *idx, |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 594 | struct hdr_ctx *ctx) |
| 595 | { |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 596 | char *eol, *sov; |
| 597 | int cur_idx, old_idx; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 598 | |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 599 | cur_idx = ctx->idx; |
| 600 | if (cur_idx) { |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 601 | /* We have previously returned a value, let's search |
| 602 | * another one on the same line. |
| 603 | */ |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 604 | sol = ctx->line; |
Willy Tarreau | 588bd4f | 2011-09-01 22:22:28 +0200 | [diff] [blame] | 605 | ctx->del = ctx->val + ctx->vlen + ctx->tws; |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 606 | sov = sol + ctx->del; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 607 | eol = sol + idx->v[cur_idx].len; |
| 608 | |
| 609 | if (sov >= eol) |
| 610 | /* no more values in this header */ |
| 611 | goto next_hdr; |
| 612 | |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 613 | /* values remaining for this header, skip the comma but save it |
| 614 | * for later use (eg: for header deletion). |
| 615 | */ |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 616 | sov++; |
| 617 | while (sov < eol && http_is_lws[(unsigned char)*sov]) |
| 618 | sov++; |
| 619 | |
| 620 | goto return_hdr; |
| 621 | } |
| 622 | |
| 623 | /* first request for this header */ |
| 624 | sol += hdr_idx_first_pos(idx); |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 625 | old_idx = 0; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 626 | cur_idx = hdr_idx_first_idx(idx); |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 627 | while (cur_idx) { |
| 628 | eol = sol + idx->v[cur_idx].len; |
| 629 | |
Willy Tarreau | 1ad7c6d | 2007-06-10 21:42:55 +0200 | [diff] [blame] | 630 | if (len == 0) { |
| 631 | /* No argument was passed, we want any header. |
| 632 | * To achieve this, we simply build a fake request. */ |
| 633 | while (sol + len < eol && sol[len] != ':') |
| 634 | len++; |
| 635 | name = sol; |
| 636 | } |
| 637 | |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 638 | if ((len < eol - sol) && |
| 639 | (sol[len] == ':') && |
| 640 | (strncasecmp(sol, name, len) == 0)) { |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 641 | ctx->del = len; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 642 | sov = sol + len + 1; |
| 643 | while (sov < eol && http_is_lws[(unsigned char)*sov]) |
| 644 | sov++; |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 645 | |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 646 | ctx->line = sol; |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 647 | ctx->prev = old_idx; |
| 648 | return_hdr: |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 649 | ctx->idx = cur_idx; |
| 650 | ctx->val = sov - sol; |
| 651 | |
| 652 | eol = find_hdr_value_end(sov, eol); |
Willy Tarreau | 588bd4f | 2011-09-01 22:22:28 +0200 | [diff] [blame] | 653 | ctx->tws = 0; |
Willy Tarreau | 275600b | 2011-09-16 08:11:26 +0200 | [diff] [blame] | 654 | while (eol > sov && http_is_lws[(unsigned char)*(eol - 1)]) { |
Willy Tarreau | 588bd4f | 2011-09-01 22:22:28 +0200 | [diff] [blame] | 655 | eol--; |
| 656 | ctx->tws++; |
| 657 | } |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 658 | ctx->vlen = eol - sov; |
| 659 | return 1; |
| 660 | } |
| 661 | next_hdr: |
| 662 | sol = eol + idx->v[cur_idx].cr + 1; |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 663 | old_idx = cur_idx; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 664 | cur_idx = idx->v[cur_idx].next; |
| 665 | } |
| 666 | return 0; |
| 667 | } |
| 668 | |
| 669 | int http_find_header(const char *name, |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 670 | char *sol, struct hdr_idx *idx, |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 671 | struct hdr_ctx *ctx) |
| 672 | { |
| 673 | return http_find_header2(name, strlen(name), sol, idx, ctx); |
| 674 | } |
| 675 | |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 676 | /* Remove one value of a header. This only works on a <ctx> returned by one of |
| 677 | * the http_find_header functions. The value is removed, as well as surrounding |
| 678 | * 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] | 679 | * 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] | 680 | * message <msg>. The new index is returned. If it is zero, it means there is |
| 681 | * no more header, so any processing may stop. The ctx is always left in a form |
| 682 | * that can be handled by http_find_header2() to find next occurrence. |
| 683 | */ |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 684 | 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] | 685 | { |
| 686 | int cur_idx = ctx->idx; |
| 687 | char *sol = ctx->line; |
| 688 | struct hdr_idx_elem *hdr; |
| 689 | int delta, skip_comma; |
| 690 | |
| 691 | if (!cur_idx) |
| 692 | return 0; |
| 693 | |
| 694 | hdr = &idx->v[cur_idx]; |
Willy Tarreau | 588bd4f | 2011-09-01 22:22:28 +0200 | [diff] [blame] | 695 | if (sol[ctx->del] == ':' && ctx->val + ctx->vlen + ctx->tws == hdr->len) { |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 696 | /* 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] | 697 | 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] | 698 | http_msg_move_end(msg, delta); |
| 699 | idx->used--; |
| 700 | hdr->len = 0; /* unused entry */ |
| 701 | idx->v[ctx->prev].next = idx->v[ctx->idx].next; |
Willy Tarreau | 5c4784f | 2011-02-12 13:07:35 +0100 | [diff] [blame] | 702 | if (idx->tail == ctx->idx) |
| 703 | idx->tail = ctx->prev; |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 704 | ctx->idx = ctx->prev; /* walk back to the end of previous header */ |
| 705 | ctx->line -= idx->v[ctx->idx].len + idx->v[cur_idx].cr + 1; |
| 706 | 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] | 707 | ctx->tws = ctx->vlen = 0; |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 708 | return ctx->idx; |
| 709 | } |
| 710 | |
| 711 | /* 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] | 712 | * ctx->del+1 and ctx->val+ctx->vlen+ctx->tws+1 included. If it is the |
| 713 | * last entry of the list, we remove the last separator. |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 714 | */ |
| 715 | |
Willy Tarreau | 588bd4f | 2011-09-01 22:22:28 +0200 | [diff] [blame] | 716 | skip_comma = (ctx->val + ctx->vlen + ctx->tws == hdr->len) ? 0 : 1; |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 717 | delta = buffer_replace2(msg->chn->buf, sol + ctx->del + skip_comma, |
Willy Tarreau | 588bd4f | 2011-09-01 22:22:28 +0200 | [diff] [blame] | 718 | sol + ctx->val + ctx->vlen + ctx->tws + skip_comma, |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 719 | NULL, 0); |
| 720 | hdr->len += delta; |
| 721 | http_msg_move_end(msg, delta); |
| 722 | ctx->val = ctx->del; |
Willy Tarreau | 588bd4f | 2011-09-01 22:22:28 +0200 | [diff] [blame] | 723 | ctx->tws = ctx->vlen = 0; |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 724 | return ctx->idx; |
| 725 | } |
| 726 | |
Willy Tarreau | 2d3d94c | 2008-11-30 20:20:08 +0100 | [diff] [blame] | 727 | /* This function handles a server error at the stream interface level. The |
| 728 | * stream interface is assumed to be already in a closed state. An optional |
| 729 | * message is copied into the input buffer, and an HTTP status code stored. |
| 730 | * 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] | 731 | * in this buffer will be lost. |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 732 | */ |
Willy Tarreau | 2d3d94c | 2008-11-30 20:20:08 +0100 | [diff] [blame] | 733 | static void http_server_error(struct session *t, struct stream_interface *si, |
| 734 | int err, int finst, int status, const struct chunk *msg) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 735 | { |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 736 | channel_auto_read(si->ob); |
| 737 | channel_abort(si->ob); |
| 738 | channel_auto_close(si->ob); |
| 739 | channel_erase(si->ob); |
| 740 | channel_auto_close(si->ib); |
| 741 | channel_auto_read(si->ib); |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 742 | if (status > 0 && msg) { |
Willy Tarreau | 3bac9ff | 2007-03-18 17:31:28 +0100 | [diff] [blame] | 743 | t->txn.status = status; |
Willy Tarreau | 9dab5fc | 2012-05-07 11:56:55 +0200 | [diff] [blame] | 744 | bo_inject(si->ib, msg->str, msg->len); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 745 | } |
| 746 | if (!(t->flags & SN_ERR_MASK)) |
| 747 | t->flags |= err; |
| 748 | if (!(t->flags & SN_FINST_MASK)) |
| 749 | t->flags |= finst; |
| 750 | } |
| 751 | |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 752 | /* This function returns the appropriate error location for the given session |
| 753 | * and message. |
| 754 | */ |
| 755 | |
Willy Tarreau | 783f258 | 2012-09-04 12:19:04 +0200 | [diff] [blame] | 756 | struct chunk *http_error_message(struct session *s, int msgnum) |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 757 | { |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 758 | if (s->be->errmsg[msgnum].str) |
| 759 | return &s->be->errmsg[msgnum]; |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 760 | else if (s->fe->errmsg[msgnum].str) |
| 761 | return &s->fe->errmsg[msgnum]; |
| 762 | else |
| 763 | return &http_err_chunks[msgnum]; |
| 764 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 765 | |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 766 | /* |
| 767 | * returns HTTP_METH_NONE if there is nothing valid to read (empty or non-text |
| 768 | * string), HTTP_METH_OTHER for unknown methods, or the identified method. |
| 769 | */ |
Willy Tarreau | c8987b3 | 2013-12-06 23:43:17 +0100 | [diff] [blame] | 770 | static 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] | 771 | { |
| 772 | unsigned char m; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 773 | const struct http_method_desc *h; |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 774 | |
| 775 | m = ((unsigned)*str - 'A'); |
| 776 | |
| 777 | if (m < 26) { |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 778 | for (h = http_methods[m]; h->len > 0; h++) { |
| 779 | if (unlikely(h->len != len)) |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 780 | continue; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 781 | if (likely(memcmp(str, h->text, h->len) == 0)) |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 782 | return h->meth; |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 783 | }; |
| 784 | return HTTP_METH_OTHER; |
| 785 | } |
| 786 | return HTTP_METH_NONE; |
| 787 | |
| 788 | } |
| 789 | |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 790 | /* Parse the URI from the given transaction (which is assumed to be in request |
| 791 | * phase) and look for the "/" beginning the PATH. If not found, return NULL. |
| 792 | * It is returned otherwise. |
| 793 | */ |
| 794 | static char * |
| 795 | http_get_path(struct http_txn *txn) |
| 796 | { |
| 797 | char *ptr, *end; |
| 798 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 799 | ptr = txn->req.chn->buf->p + txn->req.sl.rq.u; |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 800 | end = ptr + txn->req.sl.rq.u_l; |
| 801 | |
| 802 | if (ptr >= end) |
| 803 | return NULL; |
| 804 | |
| 805 | /* RFC2616, par. 5.1.2 : |
| 806 | * Request-URI = "*" | absuri | abspath | authority |
| 807 | */ |
| 808 | |
| 809 | if (*ptr == '*') |
| 810 | return NULL; |
| 811 | |
| 812 | if (isalpha((unsigned char)*ptr)) { |
| 813 | /* this is a scheme as described by RFC3986, par. 3.1 */ |
| 814 | ptr++; |
| 815 | while (ptr < end && |
| 816 | (isalnum((unsigned char)*ptr) || *ptr == '+' || *ptr == '-' || *ptr == '.')) |
| 817 | ptr++; |
| 818 | /* skip '://' */ |
| 819 | if (ptr == end || *ptr++ != ':') |
| 820 | return NULL; |
| 821 | if (ptr == end || *ptr++ != '/') |
| 822 | return NULL; |
| 823 | if (ptr == end || *ptr++ != '/') |
| 824 | return NULL; |
| 825 | } |
| 826 | /* skip [user[:passwd]@]host[:[port]] */ |
| 827 | |
| 828 | while (ptr < end && *ptr != '/') |
| 829 | ptr++; |
| 830 | |
| 831 | if (ptr == end) |
| 832 | return NULL; |
| 833 | |
| 834 | /* OK, we got the '/' ! */ |
| 835 | return ptr; |
| 836 | } |
| 837 | |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 838 | /* Returns a 302 for a redirectable request that reaches a server working in |
| 839 | * in redirect mode. This may only be called just after the stream interface |
| 840 | * has moved to SI_ST_ASS. Unprocessable requests are left unchanged and will |
| 841 | * follow normal proxy processing. NOTE: this function is designed to support |
| 842 | * being called once data are scheduled for forwarding. |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 843 | */ |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 844 | void http_perform_server_redirect(struct session *s, struct stream_interface *si) |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 845 | { |
| 846 | struct http_txn *txn; |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 847 | struct server *srv; |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 848 | char *path; |
Willy Tarreau | cde18fc | 2012-05-30 07:59:54 +0200 | [diff] [blame] | 849 | int len, rewind; |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 850 | |
| 851 | /* 1: create the response header */ |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 852 | trash.len = strlen(HTTP_302); |
| 853 | memcpy(trash.str, HTTP_302, trash.len); |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 854 | |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 855 | srv = objt_server(s->target); |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 856 | |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 857 | /* 2: add the server's prefix */ |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 858 | if (trash.len + srv->rdr_len > trash.size) |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 859 | return; |
| 860 | |
Willy Tarreau | dcb75c4 | 2010-01-10 00:24:22 +0100 | [diff] [blame] | 861 | /* special prefix "/" means don't change URL */ |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 862 | if (srv->rdr_len != 1 || *srv->rdr_pfx != '/') { |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 863 | memcpy(trash.str + trash.len, srv->rdr_pfx, srv->rdr_len); |
| 864 | trash.len += srv->rdr_len; |
Willy Tarreau | dcb75c4 | 2010-01-10 00:24:22 +0100 | [diff] [blame] | 865 | } |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 866 | |
Willy Tarreau | cde18fc | 2012-05-30 07:59:54 +0200 | [diff] [blame] | 867 | /* 3: add the request URI. Since it was already forwarded, we need |
| 868 | * to temporarily rewind the buffer. |
| 869 | */ |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 870 | txn = &s->txn; |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 871 | b_rew(s->req->buf, rewind = s->req->buf->o); |
Willy Tarreau | cde18fc | 2012-05-30 07:59:54 +0200 | [diff] [blame] | 872 | |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 873 | path = http_get_path(txn); |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 874 | 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] | 875 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 876 | b_adv(s->req->buf, rewind); |
Willy Tarreau | cde18fc | 2012-05-30 07:59:54 +0200 | [diff] [blame] | 877 | |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 878 | if (!path) |
| 879 | return; |
| 880 | |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 881 | if (trash.len + len > trash.size - 4) /* 4 for CRLF-CRLF */ |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 882 | return; |
| 883 | |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 884 | memcpy(trash.str + trash.len, path, len); |
| 885 | trash.len += len; |
Willy Tarreau | 88d349d | 2010-01-25 12:15:43 +0100 | [diff] [blame] | 886 | |
| 887 | if (unlikely(txn->flags & TX_USE_PX_CONN)) { |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 888 | memcpy(trash.str + trash.len, "\r\nProxy-Connection: close\r\n\r\n", 29); |
| 889 | trash.len += 29; |
Willy Tarreau | 88d349d | 2010-01-25 12:15:43 +0100 | [diff] [blame] | 890 | } else { |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 891 | memcpy(trash.str + trash.len, "\r\nConnection: close\r\n\r\n", 23); |
| 892 | trash.len += 23; |
Willy Tarreau | 88d349d | 2010-01-25 12:15:43 +0100 | [diff] [blame] | 893 | } |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 894 | |
| 895 | /* prepare to return without error. */ |
Willy Tarreau | 73b013b | 2012-05-21 16:31:45 +0200 | [diff] [blame] | 896 | si_shutr(si); |
| 897 | si_shutw(si); |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 898 | si->err_type = SI_ET_NONE; |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 899 | si->state = SI_ST_CLO; |
| 900 | |
| 901 | /* send the message */ |
Willy Tarreau | 570f221 | 2013-06-10 16:42:09 +0200 | [diff] [blame] | 902 | http_server_error(s, si, SN_ERR_LOCAL, SN_FINST_C, 302, &trash); |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 903 | |
| 904 | /* 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] | 905 | srv_inc_sess_ctr(srv); |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 906 | } |
| 907 | |
Willy Tarreau | 0cac36f | 2008-11-30 20:44:17 +0100 | [diff] [blame] | 908 | /* Return the error message corresponding to si->err_type. It is assumed |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 909 | * that the server side is closed. Note that err_type is actually a |
| 910 | * bitmask, where almost only aborts may be cumulated with other |
| 911 | * values. We consider that aborted operations are more important |
| 912 | * than timeouts or errors due to the fact that nobody else in the |
| 913 | * logs might explain incomplete retries. All others should avoid |
| 914 | * being cumulated. It should normally not be possible to have multiple |
| 915 | * 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] | 916 | * Note that connection errors appearing on the second request of a keep-alive |
| 917 | * connection are not reported since this allows the client to retry. |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 918 | */ |
Willy Tarreau | 0cac36f | 2008-11-30 20:44:17 +0100 | [diff] [blame] | 919 | void http_return_srv_error(struct session *s, struct stream_interface *si) |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 920 | { |
Willy Tarreau | 0cac36f | 2008-11-30 20:44:17 +0100 | [diff] [blame] | 921 | int err_type = si->err_type; |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 922 | |
| 923 | if (err_type & SI_ET_QUEUE_ABRT) |
Willy Tarreau | 2d3d94c | 2008-11-30 20:20:08 +0100 | [diff] [blame] | 924 | http_server_error(s, si, SN_ERR_CLICL, SN_FINST_Q, |
Willy Tarreau | 783f258 | 2012-09-04 12:19:04 +0200 | [diff] [blame] | 925 | 503, http_error_message(s, HTTP_ERR_503)); |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 926 | else if (err_type & SI_ET_CONN_ABRT) |
Willy Tarreau | 2d3d94c | 2008-11-30 20:20:08 +0100 | [diff] [blame] | 927 | http_server_error(s, si, SN_ERR_CLICL, SN_FINST_C, |
Willy Tarreau | 6b726ad | 2013-12-15 19:31:37 +0100 | [diff] [blame] | 928 | 503, (s->txn.flags & TX_NOT_FIRST) ? NULL : |
| 929 | http_error_message(s, HTTP_ERR_503)); |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 930 | else if (err_type & SI_ET_QUEUE_TO) |
Willy Tarreau | 2d3d94c | 2008-11-30 20:20:08 +0100 | [diff] [blame] | 931 | http_server_error(s, si, SN_ERR_SRVTO, SN_FINST_Q, |
Willy Tarreau | 783f258 | 2012-09-04 12:19:04 +0200 | [diff] [blame] | 932 | 503, http_error_message(s, HTTP_ERR_503)); |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 933 | else if (err_type & SI_ET_QUEUE_ERR) |
Willy Tarreau | 2d3d94c | 2008-11-30 20:20:08 +0100 | [diff] [blame] | 934 | http_server_error(s, si, SN_ERR_SRVCL, SN_FINST_Q, |
Willy Tarreau | 783f258 | 2012-09-04 12:19:04 +0200 | [diff] [blame] | 935 | 503, http_error_message(s, HTTP_ERR_503)); |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 936 | else if (err_type & SI_ET_CONN_TO) |
Willy Tarreau | 2d3d94c | 2008-11-30 20:20:08 +0100 | [diff] [blame] | 937 | http_server_error(s, si, SN_ERR_SRVTO, SN_FINST_C, |
Willy Tarreau | 6b726ad | 2013-12-15 19:31:37 +0100 | [diff] [blame] | 938 | 503, (s->txn.flags & TX_NOT_FIRST) ? NULL : |
| 939 | http_error_message(s, HTTP_ERR_503)); |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 940 | else if (err_type & SI_ET_CONN_ERR) |
Willy Tarreau | 2d3d94c | 2008-11-30 20:20:08 +0100 | [diff] [blame] | 941 | http_server_error(s, si, SN_ERR_SRVCL, SN_FINST_C, |
Willy Tarreau | 6b726ad | 2013-12-15 19:31:37 +0100 | [diff] [blame] | 942 | 503, (s->txn.flags & TX_NOT_FIRST) ? NULL : |
| 943 | http_error_message(s, HTTP_ERR_503)); |
Willy Tarreau | 2d400bb | 2012-05-14 12:11:47 +0200 | [diff] [blame] | 944 | else if (err_type & SI_ET_CONN_RES) |
| 945 | http_server_error(s, si, SN_ERR_RESOURCE, SN_FINST_C, |
Willy Tarreau | 6b726ad | 2013-12-15 19:31:37 +0100 | [diff] [blame] | 946 | 503, (s->txn.flags & TX_NOT_FIRST) ? NULL : |
| 947 | http_error_message(s, HTTP_ERR_503)); |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 948 | else /* SI_ET_CONN_OTHER and others */ |
Willy Tarreau | 2d3d94c | 2008-11-30 20:20:08 +0100 | [diff] [blame] | 949 | http_server_error(s, si, SN_ERR_INTERNAL, SN_FINST_C, |
Willy Tarreau | 783f258 | 2012-09-04 12:19:04 +0200 | [diff] [blame] | 950 | 500, http_error_message(s, HTTP_ERR_500)); |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 951 | } |
| 952 | |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 953 | extern const char sess_term_cond[8]; |
| 954 | extern const char sess_fin_state[8]; |
| 955 | extern const char *monthname[12]; |
Willy Tarreau | 332f8bf | 2007-05-13 21:36:56 +0200 | [diff] [blame] | 956 | struct pool_head *pool2_requri; |
Willy Tarreau | 193b8c6 | 2012-11-22 00:17:38 +0100 | [diff] [blame] | 957 | struct pool_head *pool2_capture = NULL; |
William Lallemand | a73203e | 2012-03-12 12:48:57 +0100 | [diff] [blame] | 958 | struct pool_head *pool2_uniqueid; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 959 | |
Willy Tarreau | 117f59e | 2007-03-04 18:17:17 +0100 | [diff] [blame] | 960 | /* |
| 961 | * Capture headers from message starting at <som> according to header list |
| 962 | * <cap_hdr>, and fill the <idx> structure appropriately. |
| 963 | */ |
| 964 | void capture_headers(char *som, struct hdr_idx *idx, |
| 965 | char **cap, struct cap_hdr *cap_hdr) |
| 966 | { |
| 967 | char *eol, *sol, *col, *sov; |
| 968 | int cur_idx; |
| 969 | struct cap_hdr *h; |
| 970 | int len; |
| 971 | |
| 972 | sol = som + hdr_idx_first_pos(idx); |
| 973 | cur_idx = hdr_idx_first_idx(idx); |
| 974 | |
| 975 | while (cur_idx) { |
| 976 | eol = sol + idx->v[cur_idx].len; |
| 977 | |
| 978 | col = sol; |
| 979 | while (col < eol && *col != ':') |
| 980 | col++; |
| 981 | |
| 982 | sov = col + 1; |
| 983 | while (sov < eol && http_is_lws[(unsigned char)*sov]) |
| 984 | sov++; |
| 985 | |
| 986 | for (h = cap_hdr; h; h = h->next) { |
| 987 | if ((h->namelen == col - sol) && |
| 988 | (strncasecmp(sol, h->name, h->namelen) == 0)) { |
| 989 | if (cap[h->index] == NULL) |
| 990 | cap[h->index] = |
Willy Tarreau | cf7f320 | 2007-05-13 22:46:04 +0200 | [diff] [blame] | 991 | pool_alloc2(h->pool); |
Willy Tarreau | 117f59e | 2007-03-04 18:17:17 +0100 | [diff] [blame] | 992 | |
| 993 | if (cap[h->index] == NULL) { |
| 994 | Alert("HTTP capture : out of memory.\n"); |
| 995 | continue; |
| 996 | } |
| 997 | |
| 998 | len = eol - sov; |
| 999 | if (len > h->len) |
| 1000 | len = h->len; |
| 1001 | |
| 1002 | memcpy(cap[h->index], sov, len); |
| 1003 | cap[h->index][len]=0; |
| 1004 | } |
| 1005 | } |
| 1006 | sol = eol + idx->v[cur_idx].cr + 1; |
| 1007 | cur_idx = idx->v[cur_idx].next; |
| 1008 | } |
| 1009 | } |
| 1010 | |
| 1011 | |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 1012 | /* either we find an LF at <ptr> or we jump to <bad>. |
| 1013 | */ |
| 1014 | #define EXPECT_LF_HERE(ptr, bad) do { if (unlikely(*(ptr) != '\n')) goto bad; } while (0) |
| 1015 | |
| 1016 | /* plays with variables <ptr>, <end> and <state>. Jumps to <good> if OK, |
| 1017 | * otherwise to <http_msg_ood> with <state> set to <st>. |
| 1018 | */ |
| 1019 | #define EAT_AND_JUMP_OR_RETURN(good, st) do { \ |
| 1020 | ptr++; \ |
| 1021 | if (likely(ptr < end)) \ |
| 1022 | goto good; \ |
| 1023 | else { \ |
| 1024 | state = (st); \ |
| 1025 | goto http_msg_ood; \ |
| 1026 | } \ |
| 1027 | } while (0) |
| 1028 | |
| 1029 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1030 | /* |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 1031 | * This function parses a status line between <ptr> and <end>, starting with |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1032 | * parser state <state>. Only states HTTP_MSG_RPVER, HTTP_MSG_RPVER_SP, |
| 1033 | * HTTP_MSG_RPCODE, HTTP_MSG_RPCODE_SP and HTTP_MSG_RPREASON are handled. Others |
| 1034 | * will give undefined results. |
| 1035 | * Note that it is upon the caller's responsibility to ensure that ptr < end, |
| 1036 | * and that msg->sol points to the beginning of the response. |
| 1037 | * If a complete line is found (which implies that at least one CR or LF is |
| 1038 | * found before <end>, the updated <ptr> is returned, otherwise NULL is |
| 1039 | * returned indicating an incomplete line (which does not mean that parts have |
| 1040 | * not been updated). In the incomplete case, if <ret_ptr> or <ret_state> are |
| 1041 | * non-NULL, they are fed with the new <ptr> and <state> values to be passed |
| 1042 | * upon next call. |
| 1043 | * |
Willy Tarreau | 9cdde23 | 2007-05-02 20:58:19 +0200 | [diff] [blame] | 1044 | * This function was intentionally designed to be called from |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1045 | * http_msg_analyzer() with the lowest overhead. It should integrate perfectly |
| 1046 | * 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] | 1047 | * labels and variable names. Note that msg->sol is left unchanged. |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1048 | */ |
Willy Tarreau | 69d8c5d | 2012-05-08 09:44:41 +0200 | [diff] [blame] | 1049 | const char *http_parse_stsline(struct http_msg *msg, |
Willy Tarreau | 3770f23 | 2013-12-07 00:01:53 +0100 | [diff] [blame] | 1050 | enum ht_state state, const char *ptr, const char *end, |
| 1051 | unsigned int *ret_ptr, enum ht_state *ret_state) |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1052 | { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 1053 | const char *msg_start = msg->chn->buf->p; |
Willy Tarreau | 62f791e | 2012-03-09 11:32:30 +0100 | [diff] [blame] | 1054 | |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1055 | switch (state) { |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1056 | case HTTP_MSG_RPVER: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1057 | http_msg_rpver: |
Willy Tarreau | 4b89ad4 | 2007-03-04 18:13:58 +0100 | [diff] [blame] | 1058 | if (likely(HTTP_IS_VER_TOKEN(*ptr))) |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1059 | EAT_AND_JUMP_OR_RETURN(http_msg_rpver, HTTP_MSG_RPVER); |
| 1060 | |
| 1061 | if (likely(HTTP_IS_SPHT(*ptr))) { |
Willy Tarreau | ea1175a | 2012-03-05 15:52:30 +0100 | [diff] [blame] | 1062 | msg->sl.st.v_l = ptr - msg_start; |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1063 | EAT_AND_JUMP_OR_RETURN(http_msg_rpver_sp, HTTP_MSG_RPVER_SP); |
| 1064 | } |
Willy Tarreau | 7552c03 | 2009-03-01 11:10:40 +0100 | [diff] [blame] | 1065 | state = HTTP_MSG_ERROR; |
| 1066 | break; |
| 1067 | |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1068 | case HTTP_MSG_RPVER_SP: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1069 | http_msg_rpver_sp: |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1070 | if (likely(!HTTP_IS_LWS(*ptr))) { |
Willy Tarreau | ea1175a | 2012-03-05 15:52:30 +0100 | [diff] [blame] | 1071 | msg->sl.st.c = ptr - msg_start; |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1072 | goto http_msg_rpcode; |
| 1073 | } |
| 1074 | if (likely(HTTP_IS_SPHT(*ptr))) |
| 1075 | EAT_AND_JUMP_OR_RETURN(http_msg_rpver_sp, HTTP_MSG_RPVER_SP); |
| 1076 | /* so it's a CR/LF, this is invalid */ |
Willy Tarreau | 7552c03 | 2009-03-01 11:10:40 +0100 | [diff] [blame] | 1077 | state = HTTP_MSG_ERROR; |
| 1078 | break; |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1079 | |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1080 | case HTTP_MSG_RPCODE: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1081 | http_msg_rpcode: |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1082 | if (likely(!HTTP_IS_LWS(*ptr))) |
| 1083 | EAT_AND_JUMP_OR_RETURN(http_msg_rpcode, HTTP_MSG_RPCODE); |
| 1084 | |
| 1085 | if (likely(HTTP_IS_SPHT(*ptr))) { |
Willy Tarreau | ea1175a | 2012-03-05 15:52:30 +0100 | [diff] [blame] | 1086 | msg->sl.st.c_l = ptr - msg_start - msg->sl.st.c; |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1087 | EAT_AND_JUMP_OR_RETURN(http_msg_rpcode_sp, HTTP_MSG_RPCODE_SP); |
| 1088 | } |
| 1089 | |
| 1090 | /* so it's a CR/LF, so there is no reason phrase */ |
Willy Tarreau | ea1175a | 2012-03-05 15:52:30 +0100 | [diff] [blame] | 1091 | msg->sl.st.c_l = ptr - msg_start - msg->sl.st.c; |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1092 | http_msg_rsp_reason: |
| 1093 | /* FIXME: should we support HTTP responses without any reason phrase ? */ |
Willy Tarreau | ea1175a | 2012-03-05 15:52:30 +0100 | [diff] [blame] | 1094 | msg->sl.st.r = ptr - msg_start; |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1095 | msg->sl.st.r_l = 0; |
| 1096 | goto http_msg_rpline_eol; |
| 1097 | |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1098 | case HTTP_MSG_RPCODE_SP: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1099 | http_msg_rpcode_sp: |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1100 | if (likely(!HTTP_IS_LWS(*ptr))) { |
Willy Tarreau | ea1175a | 2012-03-05 15:52:30 +0100 | [diff] [blame] | 1101 | msg->sl.st.r = ptr - msg_start; |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1102 | goto http_msg_rpreason; |
| 1103 | } |
| 1104 | if (likely(HTTP_IS_SPHT(*ptr))) |
| 1105 | EAT_AND_JUMP_OR_RETURN(http_msg_rpcode_sp, HTTP_MSG_RPCODE_SP); |
| 1106 | /* so it's a CR/LF, so there is no reason phrase */ |
| 1107 | goto http_msg_rsp_reason; |
| 1108 | |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1109 | case HTTP_MSG_RPREASON: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1110 | http_msg_rpreason: |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1111 | if (likely(!HTTP_IS_CRLF(*ptr))) |
| 1112 | EAT_AND_JUMP_OR_RETURN(http_msg_rpreason, HTTP_MSG_RPREASON); |
Willy Tarreau | ea1175a | 2012-03-05 15:52:30 +0100 | [diff] [blame] | 1113 | msg->sl.st.r_l = ptr - msg_start - msg->sl.st.r; |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1114 | http_msg_rpline_eol: |
| 1115 | /* We have seen the end of line. Note that we do not |
| 1116 | * necessarily have the \n yet, but at least we know that we |
| 1117 | * have EITHER \r OR \n, otherwise the response would not be |
| 1118 | * complete. We can then record the response length and return |
| 1119 | * to the caller which will be able to register it. |
| 1120 | */ |
Willy Tarreau | 3a215be | 2012-03-09 21:39:51 +0100 | [diff] [blame] | 1121 | msg->sl.st.l = ptr - msg_start - msg->sol; |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1122 | return ptr; |
| 1123 | |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1124 | default: |
Willy Tarreau | 3770f23 | 2013-12-07 00:01:53 +0100 | [diff] [blame] | 1125 | #ifdef DEBUG_FULL |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1126 | fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state); |
| 1127 | exit(1); |
| 1128 | #endif |
Willy Tarreau | 3770f23 | 2013-12-07 00:01:53 +0100 | [diff] [blame] | 1129 | ; |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1130 | } |
| 1131 | |
| 1132 | http_msg_ood: |
Willy Tarreau | 7552c03 | 2009-03-01 11:10:40 +0100 | [diff] [blame] | 1133 | /* out of valid data */ |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1134 | if (ret_state) |
| 1135 | *ret_state = state; |
| 1136 | if (ret_ptr) |
Willy Tarreau | a458b67 | 2012-03-05 11:17:50 +0100 | [diff] [blame] | 1137 | *ret_ptr = ptr - msg_start; |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1138 | return NULL; |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1139 | } |
| 1140 | |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1141 | /* |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1142 | * This function parses a request line between <ptr> and <end>, starting with |
| 1143 | * parser state <state>. Only states HTTP_MSG_RQMETH, HTTP_MSG_RQMETH_SP, |
| 1144 | * HTTP_MSG_RQURI, HTTP_MSG_RQURI_SP and HTTP_MSG_RQVER are handled. Others |
| 1145 | * will give undefined results. |
| 1146 | * Note that it is upon the caller's responsibility to ensure that ptr < end, |
| 1147 | * and that msg->sol points to the beginning of the request. |
| 1148 | * If a complete line is found (which implies that at least one CR or LF is |
| 1149 | * found before <end>, the updated <ptr> is returned, otherwise NULL is |
| 1150 | * returned indicating an incomplete line (which does not mean that parts have |
| 1151 | * not been updated). In the incomplete case, if <ret_ptr> or <ret_state> are |
| 1152 | * non-NULL, they are fed with the new <ptr> and <state> values to be passed |
| 1153 | * upon next call. |
| 1154 | * |
Willy Tarreau | 9cdde23 | 2007-05-02 20:58:19 +0200 | [diff] [blame] | 1155 | * This function was intentionally designed to be called from |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1156 | * http_msg_analyzer() with the lowest overhead. It should integrate perfectly |
| 1157 | * 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] | 1158 | * labels and variable names. Note that msg->sol is left unchanged. |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1159 | */ |
Willy Tarreau | 69d8c5d | 2012-05-08 09:44:41 +0200 | [diff] [blame] | 1160 | const char *http_parse_reqline(struct http_msg *msg, |
Willy Tarreau | 3770f23 | 2013-12-07 00:01:53 +0100 | [diff] [blame] | 1161 | enum ht_state state, const char *ptr, const char *end, |
| 1162 | unsigned int *ret_ptr, enum ht_state *ret_state) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1163 | { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 1164 | const char *msg_start = msg->chn->buf->p; |
Willy Tarreau | 62f791e | 2012-03-09 11:32:30 +0100 | [diff] [blame] | 1165 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1166 | switch (state) { |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1167 | case HTTP_MSG_RQMETH: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1168 | http_msg_rqmeth: |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1169 | if (likely(HTTP_IS_TOKEN(*ptr))) |
| 1170 | EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth, HTTP_MSG_RQMETH); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1171 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1172 | if (likely(HTTP_IS_SPHT(*ptr))) { |
Willy Tarreau | ea1175a | 2012-03-05 15:52:30 +0100 | [diff] [blame] | 1173 | msg->sl.rq.m_l = ptr - msg_start; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1174 | EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth_sp, HTTP_MSG_RQMETH_SP); |
| 1175 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1176 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1177 | if (likely(HTTP_IS_CRLF(*ptr))) { |
| 1178 | /* HTTP 0.9 request */ |
Willy Tarreau | ea1175a | 2012-03-05 15:52:30 +0100 | [diff] [blame] | 1179 | msg->sl.rq.m_l = ptr - msg_start; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1180 | http_msg_req09_uri: |
Willy Tarreau | ea1175a | 2012-03-05 15:52:30 +0100 | [diff] [blame] | 1181 | msg->sl.rq.u = ptr - msg_start; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1182 | http_msg_req09_uri_e: |
Willy Tarreau | ea1175a | 2012-03-05 15:52:30 +0100 | [diff] [blame] | 1183 | msg->sl.rq.u_l = ptr - msg_start - msg->sl.rq.u; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1184 | http_msg_req09_ver: |
Willy Tarreau | ea1175a | 2012-03-05 15:52:30 +0100 | [diff] [blame] | 1185 | msg->sl.rq.v = ptr - msg_start; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1186 | msg->sl.rq.v_l = 0; |
| 1187 | goto http_msg_rqline_eol; |
| 1188 | } |
Willy Tarreau | 7552c03 | 2009-03-01 11:10:40 +0100 | [diff] [blame] | 1189 | state = HTTP_MSG_ERROR; |
| 1190 | break; |
| 1191 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1192 | case HTTP_MSG_RQMETH_SP: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1193 | http_msg_rqmeth_sp: |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1194 | if (likely(!HTTP_IS_LWS(*ptr))) { |
Willy Tarreau | ea1175a | 2012-03-05 15:52:30 +0100 | [diff] [blame] | 1195 | msg->sl.rq.u = ptr - msg_start; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1196 | goto http_msg_rquri; |
| 1197 | } |
| 1198 | if (likely(HTTP_IS_SPHT(*ptr))) |
| 1199 | EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth_sp, HTTP_MSG_RQMETH_SP); |
| 1200 | /* so it's a CR/LF, meaning an HTTP 0.9 request */ |
| 1201 | goto http_msg_req09_uri; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1202 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1203 | case HTTP_MSG_RQURI: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1204 | http_msg_rquri: |
Willy Tarreau | 2e9506d | 2012-01-07 23:22:31 +0100 | [diff] [blame] | 1205 | if (likely((unsigned char)(*ptr - 33) <= 93)) /* 33 to 126 included */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1206 | EAT_AND_JUMP_OR_RETURN(http_msg_rquri, HTTP_MSG_RQURI); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1207 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1208 | if (likely(HTTP_IS_SPHT(*ptr))) { |
Willy Tarreau | ea1175a | 2012-03-05 15:52:30 +0100 | [diff] [blame] | 1209 | msg->sl.rq.u_l = ptr - msg_start - msg->sl.rq.u; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1210 | EAT_AND_JUMP_OR_RETURN(http_msg_rquri_sp, HTTP_MSG_RQURI_SP); |
| 1211 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1212 | |
Willy Tarreau | 2e9506d | 2012-01-07 23:22:31 +0100 | [diff] [blame] | 1213 | if (likely((unsigned char)*ptr >= 128)) { |
Willy Tarreau | 422246e | 2012-01-07 23:54:13 +0100 | [diff] [blame] | 1214 | /* non-ASCII chars are forbidden unless option |
| 1215 | * accept-invalid-http-request is enabled in the frontend. |
| 1216 | * In any case, we capture the faulty char. |
Willy Tarreau | 2e9506d | 2012-01-07 23:22:31 +0100 | [diff] [blame] | 1217 | */ |
Willy Tarreau | 422246e | 2012-01-07 23:54:13 +0100 | [diff] [blame] | 1218 | if (msg->err_pos < -1) |
| 1219 | goto invalid_char; |
| 1220 | if (msg->err_pos == -1) |
Willy Tarreau | 69d8c5d | 2012-05-08 09:44:41 +0200 | [diff] [blame] | 1221 | msg->err_pos = ptr - msg_start; |
Willy Tarreau | 2e9506d | 2012-01-07 23:22:31 +0100 | [diff] [blame] | 1222 | EAT_AND_JUMP_OR_RETURN(http_msg_rquri, HTTP_MSG_RQURI); |
| 1223 | } |
| 1224 | |
| 1225 | if (likely(HTTP_IS_CRLF(*ptr))) { |
| 1226 | /* so it's a CR/LF, meaning an HTTP 0.9 request */ |
| 1227 | goto http_msg_req09_uri_e; |
| 1228 | } |
| 1229 | |
| 1230 | /* OK forbidden chars, 0..31 or 127 */ |
Willy Tarreau | 422246e | 2012-01-07 23:54:13 +0100 | [diff] [blame] | 1231 | invalid_char: |
Willy Tarreau | 69d8c5d | 2012-05-08 09:44:41 +0200 | [diff] [blame] | 1232 | msg->err_pos = ptr - msg_start; |
Willy Tarreau | 2e9506d | 2012-01-07 23:22:31 +0100 | [diff] [blame] | 1233 | state = HTTP_MSG_ERROR; |
| 1234 | break; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1235 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1236 | case HTTP_MSG_RQURI_SP: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1237 | http_msg_rquri_sp: |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1238 | if (likely(!HTTP_IS_LWS(*ptr))) { |
Willy Tarreau | ea1175a | 2012-03-05 15:52:30 +0100 | [diff] [blame] | 1239 | msg->sl.rq.v = ptr - msg_start; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1240 | goto http_msg_rqver; |
| 1241 | } |
| 1242 | if (likely(HTTP_IS_SPHT(*ptr))) |
| 1243 | EAT_AND_JUMP_OR_RETURN(http_msg_rquri_sp, HTTP_MSG_RQURI_SP); |
| 1244 | /* so it's a CR/LF, meaning an HTTP 0.9 request */ |
| 1245 | goto http_msg_req09_ver; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1246 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1247 | case HTTP_MSG_RQVER: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1248 | http_msg_rqver: |
Willy Tarreau | 4b89ad4 | 2007-03-04 18:13:58 +0100 | [diff] [blame] | 1249 | if (likely(HTTP_IS_VER_TOKEN(*ptr))) |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1250 | EAT_AND_JUMP_OR_RETURN(http_msg_rqver, HTTP_MSG_RQVER); |
Willy Tarreau | 4b89ad4 | 2007-03-04 18:13:58 +0100 | [diff] [blame] | 1251 | |
| 1252 | if (likely(HTTP_IS_CRLF(*ptr))) { |
Willy Tarreau | ea1175a | 2012-03-05 15:52:30 +0100 | [diff] [blame] | 1253 | msg->sl.rq.v_l = ptr - msg_start - msg->sl.rq.v; |
Willy Tarreau | 4b89ad4 | 2007-03-04 18:13:58 +0100 | [diff] [blame] | 1254 | http_msg_rqline_eol: |
| 1255 | /* We have seen the end of line. Note that we do not |
| 1256 | * necessarily have the \n yet, but at least we know that we |
| 1257 | * have EITHER \r OR \n, otherwise the request would not be |
| 1258 | * complete. We can then record the request length and return |
| 1259 | * to the caller which will be able to register it. |
| 1260 | */ |
Willy Tarreau | 3a215be | 2012-03-09 21:39:51 +0100 | [diff] [blame] | 1261 | msg->sl.rq.l = ptr - msg_start - msg->sol; |
Willy Tarreau | 4b89ad4 | 2007-03-04 18:13:58 +0100 | [diff] [blame] | 1262 | return ptr; |
| 1263 | } |
| 1264 | |
| 1265 | /* neither an HTTP_VER token nor a CRLF */ |
Willy Tarreau | 7552c03 | 2009-03-01 11:10:40 +0100 | [diff] [blame] | 1266 | state = HTTP_MSG_ERROR; |
| 1267 | break; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1268 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1269 | default: |
Willy Tarreau | 3770f23 | 2013-12-07 00:01:53 +0100 | [diff] [blame] | 1270 | #ifdef DEBUG_FULL |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1271 | fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state); |
| 1272 | exit(1); |
| 1273 | #endif |
Willy Tarreau | 3770f23 | 2013-12-07 00:01:53 +0100 | [diff] [blame] | 1274 | ; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1275 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1276 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1277 | http_msg_ood: |
Willy Tarreau | 7552c03 | 2009-03-01 11:10:40 +0100 | [diff] [blame] | 1278 | /* out of valid data */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1279 | if (ret_state) |
| 1280 | *ret_state = state; |
| 1281 | if (ret_ptr) |
Willy Tarreau | a458b67 | 2012-03-05 11:17:50 +0100 | [diff] [blame] | 1282 | *ret_ptr = ptr - msg_start; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1283 | return NULL; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1284 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1285 | |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 1286 | /* |
| 1287 | * Returns the data from Authorization header. Function may be called more |
| 1288 | * than once so data is stored in txn->auth_data. When no header is found |
| 1289 | * or auth method is unknown auth_method is set to HTTP_AUTH_WRONG to avoid |
| 1290 | * searching again for something we are unable to find anyway. |
| 1291 | */ |
| 1292 | |
Willy Tarreau | 7e2c647 | 2012-10-29 20:44:36 +0100 | [diff] [blame] | 1293 | char *get_http_auth_buff; |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 1294 | |
| 1295 | int |
| 1296 | get_http_auth(struct session *s) |
| 1297 | { |
| 1298 | |
| 1299 | struct http_txn *txn = &s->txn; |
| 1300 | struct chunk auth_method; |
| 1301 | struct hdr_ctx ctx; |
| 1302 | char *h, *p; |
| 1303 | int len; |
| 1304 | |
| 1305 | #ifdef DEBUG_AUTH |
| 1306 | printf("Auth for session %p: %d\n", s, txn->auth.method); |
| 1307 | #endif |
| 1308 | |
| 1309 | if (txn->auth.method == HTTP_AUTH_WRONG) |
| 1310 | return 0; |
| 1311 | |
| 1312 | if (txn->auth.method) |
| 1313 | return 1; |
| 1314 | |
| 1315 | txn->auth.method = HTTP_AUTH_WRONG; |
| 1316 | |
| 1317 | ctx.idx = 0; |
Willy Tarreau | 844a7e7 | 2010-01-31 21:46:18 +0100 | [diff] [blame] | 1318 | |
| 1319 | if (txn->flags & TX_USE_PX_CONN) { |
| 1320 | h = "Proxy-Authorization"; |
| 1321 | len = strlen(h); |
| 1322 | } else { |
| 1323 | h = "Authorization"; |
| 1324 | len = strlen(h); |
| 1325 | } |
| 1326 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 1327 | 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] | 1328 | return 0; |
| 1329 | |
| 1330 | h = ctx.line + ctx.val; |
| 1331 | |
| 1332 | p = memchr(h, ' ', ctx.vlen); |
| 1333 | if (!p || p == h) |
| 1334 | return 0; |
| 1335 | |
| 1336 | chunk_initlen(&auth_method, h, 0, p-h); |
| 1337 | chunk_initlen(&txn->auth.method_data, p+1, 0, ctx.vlen-(p-h)-1); |
| 1338 | |
| 1339 | if (!strncasecmp("Basic", auth_method.str, auth_method.len)) { |
| 1340 | |
| 1341 | len = base64dec(txn->auth.method_data.str, txn->auth.method_data.len, |
Willy Tarreau | 7e2c647 | 2012-10-29 20:44:36 +0100 | [diff] [blame] | 1342 | get_http_auth_buff, global.tune.bufsize - 1); |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 1343 | |
| 1344 | if (len < 0) |
| 1345 | return 0; |
| 1346 | |
| 1347 | |
| 1348 | get_http_auth_buff[len] = '\0'; |
| 1349 | |
| 1350 | p = strchr(get_http_auth_buff, ':'); |
| 1351 | |
| 1352 | if (!p) |
| 1353 | return 0; |
| 1354 | |
| 1355 | txn->auth.user = get_http_auth_buff; |
| 1356 | *p = '\0'; |
| 1357 | txn->auth.pass = p+1; |
| 1358 | |
| 1359 | txn->auth.method = HTTP_AUTH_BASIC; |
| 1360 | return 1; |
| 1361 | } |
| 1362 | |
| 1363 | return 0; |
| 1364 | } |
| 1365 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1366 | |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1367 | /* |
| 1368 | * This function parses an HTTP message, either a request or a response, |
Willy Tarreau | 8b1323e | 2012-03-09 14:46:19 +0100 | [diff] [blame] | 1369 | * depending on the initial msg->msg_state. The caller is responsible for |
| 1370 | * ensuring that the message does not wrap. The function can be preempted |
| 1371 | * everywhere when data are missing and recalled at the exact same location |
| 1372 | * with no information loss. The message may even be realigned between two |
| 1373 | * calls. The header index is re-initialized when switching from |
Willy Tarreau | 9cdde23 | 2007-05-02 20:58:19 +0200 | [diff] [blame] | 1374 | * 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] | 1375 | * fields. Note that msg->sol will be initialized after completing the first |
| 1376 | * state, so that none of the msg pointers has to be initialized prior to the |
| 1377 | * first call. |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1378 | */ |
Willy Tarreau | a560c21 | 2012-03-09 13:50:57 +0100 | [diff] [blame] | 1379 | void http_msg_analyzer(struct http_msg *msg, struct hdr_idx *idx) |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1380 | { |
Willy Tarreau | 3770f23 | 2013-12-07 00:01:53 +0100 | [diff] [blame] | 1381 | enum ht_state state; /* updated only when leaving the FSM */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1382 | register char *ptr, *end; /* request pointers, to avoid dereferences */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1383 | struct buffer *buf; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1384 | |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1385 | state = msg->msg_state; |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 1386 | buf = msg->chn->buf; |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1387 | ptr = buf->p + msg->next; |
| 1388 | end = buf->p + buf->i; |
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 | if (unlikely(ptr >= end)) |
| 1391 | goto http_msg_ood; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1392 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1393 | switch (state) { |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1394 | /* |
| 1395 | * First, states that are specific to the response only. |
| 1396 | * We check them first so that request and headers are |
| 1397 | * closer to each other (accessed more often). |
| 1398 | */ |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1399 | case HTTP_MSG_RPBEFORE: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1400 | http_msg_rpbefore: |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1401 | if (likely(HTTP_IS_TOKEN(*ptr))) { |
Willy Tarreau | 15de77e | 2010-01-02 21:59:16 +0100 | [diff] [blame] | 1402 | /* we have a start of message, but we have to check |
| 1403 | * first if we need to remove some CRLF. We can only |
Willy Tarreau | 2e046c6 | 2012-03-01 16:08:30 +0100 | [diff] [blame] | 1404 | * do this when o=0. |
Willy Tarreau | 15de77e | 2010-01-02 21:59:16 +0100 | [diff] [blame] | 1405 | */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1406 | if (unlikely(ptr != buf->p)) { |
| 1407 | if (buf->o) |
Willy Tarreau | 15de77e | 2010-01-02 21:59:16 +0100 | [diff] [blame] | 1408 | goto http_msg_ood; |
Willy Tarreau | 1d3bcce | 2009-12-27 15:50:06 +0100 | [diff] [blame] | 1409 | /* Remove empty leading lines, as recommended by RFC2616. */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1410 | bi_fast_delete(buf, ptr - buf->p); |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1411 | } |
Willy Tarreau | 2692736 | 2012-05-18 23:22:52 +0200 | [diff] [blame] | 1412 | msg->sol = 0; |
Willy Tarreau | e92693a | 2012-09-24 21:13:39 +0200 | [diff] [blame] | 1413 | msg->sl.st.l = 0; /* used in debug mode */ |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1414 | hdr_idx_init(idx); |
| 1415 | state = HTTP_MSG_RPVER; |
| 1416 | goto http_msg_rpver; |
| 1417 | } |
| 1418 | |
| 1419 | if (unlikely(!HTTP_IS_CRLF(*ptr))) |
| 1420 | goto http_msg_invalid; |
| 1421 | |
| 1422 | if (unlikely(*ptr == '\n')) |
| 1423 | EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore, HTTP_MSG_RPBEFORE); |
| 1424 | EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore_cr, HTTP_MSG_RPBEFORE_CR); |
| 1425 | /* stop here */ |
| 1426 | |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1427 | case HTTP_MSG_RPBEFORE_CR: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1428 | http_msg_rpbefore_cr: |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1429 | EXPECT_LF_HERE(ptr, http_msg_invalid); |
| 1430 | EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore, HTTP_MSG_RPBEFORE); |
| 1431 | /* stop here */ |
| 1432 | |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1433 | case HTTP_MSG_RPVER: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1434 | http_msg_rpver: |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1435 | case HTTP_MSG_RPVER_SP: |
| 1436 | case HTTP_MSG_RPCODE: |
| 1437 | case HTTP_MSG_RPCODE_SP: |
| 1438 | case HTTP_MSG_RPREASON: |
Willy Tarreau | 69d8c5d | 2012-05-08 09:44:41 +0200 | [diff] [blame] | 1439 | ptr = (char *)http_parse_stsline(msg, |
Willy Tarreau | a458b67 | 2012-03-05 11:17:50 +0100 | [diff] [blame] | 1440 | state, ptr, end, |
| 1441 | &msg->next, &msg->msg_state); |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1442 | if (unlikely(!ptr)) |
| 1443 | return; |
| 1444 | |
| 1445 | /* we have a full response and we know that we have either a CR |
| 1446 | * or an LF at <ptr>. |
| 1447 | */ |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1448 | hdr_idx_set_start(idx, msg->sl.st.l, *ptr == '\r'); |
| 1449 | |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1450 | msg->sol = ptr - buf->p; |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1451 | if (likely(*ptr == '\r')) |
| 1452 | EAT_AND_JUMP_OR_RETURN(http_msg_rpline_end, HTTP_MSG_RPLINE_END); |
| 1453 | goto http_msg_rpline_end; |
| 1454 | |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1455 | case HTTP_MSG_RPLINE_END: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1456 | http_msg_rpline_end: |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1457 | /* msg->sol must point to the first of CR or LF. */ |
| 1458 | EXPECT_LF_HERE(ptr, http_msg_invalid); |
| 1459 | EAT_AND_JUMP_OR_RETURN(http_msg_hdr_first, HTTP_MSG_HDR_FIRST); |
| 1460 | /* stop here */ |
| 1461 | |
| 1462 | /* |
| 1463 | * Second, states that are specific to the request only |
| 1464 | */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1465 | case HTTP_MSG_RQBEFORE: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1466 | http_msg_rqbefore: |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1467 | if (likely(HTTP_IS_TOKEN(*ptr))) { |
Willy Tarreau | 15de77e | 2010-01-02 21:59:16 +0100 | [diff] [blame] | 1468 | /* we have a start of message, but we have to check |
| 1469 | * first if we need to remove some CRLF. We can only |
Willy Tarreau | 2e046c6 | 2012-03-01 16:08:30 +0100 | [diff] [blame] | 1470 | * do this when o=0. |
Willy Tarreau | 15de77e | 2010-01-02 21:59:16 +0100 | [diff] [blame] | 1471 | */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1472 | if (likely(ptr != buf->p)) { |
| 1473 | if (buf->o) |
Willy Tarreau | 15de77e | 2010-01-02 21:59:16 +0100 | [diff] [blame] | 1474 | goto http_msg_ood; |
Willy Tarreau | 1d3bcce | 2009-12-27 15:50:06 +0100 | [diff] [blame] | 1475 | /* Remove empty leading lines, as recommended by RFC2616. */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1476 | bi_fast_delete(buf, ptr - buf->p); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1477 | } |
Willy Tarreau | 2692736 | 2012-05-18 23:22:52 +0200 | [diff] [blame] | 1478 | msg->sol = 0; |
Willy Tarreau | e92693a | 2012-09-24 21:13:39 +0200 | [diff] [blame] | 1479 | msg->sl.rq.l = 0; /* used in debug mode */ |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1480 | state = HTTP_MSG_RQMETH; |
| 1481 | goto http_msg_rqmeth; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1482 | } |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1483 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1484 | if (unlikely(!HTTP_IS_CRLF(*ptr))) |
| 1485 | goto http_msg_invalid; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1486 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1487 | if (unlikely(*ptr == '\n')) |
| 1488 | EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore, HTTP_MSG_RQBEFORE); |
| 1489 | 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] | 1490 | /* stop here */ |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1491 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1492 | case HTTP_MSG_RQBEFORE_CR: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1493 | http_msg_rqbefore_cr: |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1494 | EXPECT_LF_HERE(ptr, http_msg_invalid); |
| 1495 | EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore, HTTP_MSG_RQBEFORE); |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1496 | /* stop here */ |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1497 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1498 | case HTTP_MSG_RQMETH: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1499 | http_msg_rqmeth: |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1500 | case HTTP_MSG_RQMETH_SP: |
| 1501 | case HTTP_MSG_RQURI: |
| 1502 | case HTTP_MSG_RQURI_SP: |
| 1503 | case HTTP_MSG_RQVER: |
Willy Tarreau | 69d8c5d | 2012-05-08 09:44:41 +0200 | [diff] [blame] | 1504 | ptr = (char *)http_parse_reqline(msg, |
Willy Tarreau | a458b67 | 2012-03-05 11:17:50 +0100 | [diff] [blame] | 1505 | state, ptr, end, |
| 1506 | &msg->next, &msg->msg_state); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1507 | if (unlikely(!ptr)) |
| 1508 | return; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1509 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1510 | /* we have a full request and we know that we have either a CR |
| 1511 | * or an LF at <ptr>. |
| 1512 | */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1513 | hdr_idx_set_start(idx, msg->sl.rq.l, *ptr == '\r'); |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1514 | |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1515 | msg->sol = ptr - buf->p; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1516 | if (likely(*ptr == '\r')) |
| 1517 | 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] | 1518 | goto http_msg_rqline_end; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1519 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1520 | case HTTP_MSG_RQLINE_END: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1521 | http_msg_rqline_end: |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1522 | /* check for HTTP/0.9 request : no version information available. |
| 1523 | * msg->sol must point to the first of CR or LF. |
| 1524 | */ |
| 1525 | if (unlikely(msg->sl.rq.v_l == 0)) |
| 1526 | goto http_msg_last_lf; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1527 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1528 | EXPECT_LF_HERE(ptr, http_msg_invalid); |
| 1529 | 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] | 1530 | /* stop here */ |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1531 | |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1532 | /* |
| 1533 | * Common states below |
| 1534 | */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1535 | case HTTP_MSG_HDR_FIRST: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1536 | http_msg_hdr_first: |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1537 | msg->sol = ptr - buf->p; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1538 | if (likely(!HTTP_IS_CRLF(*ptr))) { |
| 1539 | goto http_msg_hdr_name; |
| 1540 | } |
| 1541 | |
| 1542 | if (likely(*ptr == '\r')) |
| 1543 | EAT_AND_JUMP_OR_RETURN(http_msg_last_lf, HTTP_MSG_LAST_LF); |
| 1544 | goto http_msg_last_lf; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1545 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1546 | case HTTP_MSG_HDR_NAME: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1547 | http_msg_hdr_name: |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1548 | /* assumes msg->sol points to the first char */ |
| 1549 | if (likely(HTTP_IS_TOKEN(*ptr))) |
| 1550 | 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] | 1551 | |
Willy Tarreau | fa4a03c | 2012-03-09 21:28:54 +0100 | [diff] [blame] | 1552 | if (likely(*ptr == ':')) |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1553 | 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] | 1554 | |
Willy Tarreau | 32a4ec0 | 2009-04-02 11:35:18 +0200 | [diff] [blame] | 1555 | if (likely(msg->err_pos < -1) || *ptr == '\n') |
| 1556 | goto http_msg_invalid; |
| 1557 | |
| 1558 | if (msg->err_pos == -1) /* capture error pointer */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1559 | msg->err_pos = ptr - buf->p; /* >= 0 now */ |
Willy Tarreau | 32a4ec0 | 2009-04-02 11:35:18 +0200 | [diff] [blame] | 1560 | |
| 1561 | /* and we still accept this non-token character */ |
| 1562 | 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] | 1563 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1564 | case HTTP_MSG_HDR_L1_SP: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1565 | http_msg_hdr_l1_sp: |
Willy Tarreau | fa4a03c | 2012-03-09 21:28:54 +0100 | [diff] [blame] | 1566 | /* assumes msg->sol points to the first char */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1567 | if (likely(HTTP_IS_SPHT(*ptr))) |
| 1568 | 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] | 1569 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1570 | /* header value can be basically anything except CR/LF */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1571 | msg->sov = ptr - buf->p; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1572 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1573 | if (likely(!HTTP_IS_CRLF(*ptr))) { |
| 1574 | goto http_msg_hdr_val; |
| 1575 | } |
| 1576 | |
| 1577 | if (likely(*ptr == '\r')) |
| 1578 | EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_lf, HTTP_MSG_HDR_L1_LF); |
| 1579 | goto http_msg_hdr_l1_lf; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1580 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1581 | case HTTP_MSG_HDR_L1_LF: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1582 | http_msg_hdr_l1_lf: |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1583 | EXPECT_LF_HERE(ptr, http_msg_invalid); |
| 1584 | 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] | 1585 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1586 | case HTTP_MSG_HDR_L1_LWS: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1587 | http_msg_hdr_l1_lws: |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1588 | if (likely(HTTP_IS_SPHT(*ptr))) { |
| 1589 | /* replace HT,CR,LF with spaces */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1590 | for (; buf->p + msg->sov < ptr; msg->sov++) |
| 1591 | buf->p[msg->sov] = ' '; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1592 | goto http_msg_hdr_l1_sp; |
| 1593 | } |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 1594 | /* we had a header consisting only in spaces ! */ |
Willy Tarreau | 12e48b3 | 2012-03-05 16:57:34 +0100 | [diff] [blame] | 1595 | msg->eol = msg->sov; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1596 | goto http_msg_complete_header; |
| 1597 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1598 | case HTTP_MSG_HDR_VAL: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1599 | http_msg_hdr_val: |
Willy Tarreau | fa4a03c | 2012-03-09 21:28:54 +0100 | [diff] [blame] | 1600 | /* assumes msg->sol points to the first char, and msg->sov |
| 1601 | * points to the first character of the value. |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1602 | */ |
| 1603 | if (likely(!HTTP_IS_CRLF(*ptr))) |
| 1604 | 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] | 1605 | |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1606 | msg->eol = ptr - buf->p; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1607 | /* Note: we could also copy eol into ->eoh so that we have the |
| 1608 | * real header end in case it ends with lots of LWS, but is this |
| 1609 | * really needed ? |
| 1610 | */ |
| 1611 | if (likely(*ptr == '\r')) |
| 1612 | EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l2_lf, HTTP_MSG_HDR_L2_LF); |
| 1613 | goto http_msg_hdr_l2_lf; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1614 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1615 | case HTTP_MSG_HDR_L2_LF: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1616 | http_msg_hdr_l2_lf: |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1617 | EXPECT_LF_HERE(ptr, http_msg_invalid); |
| 1618 | 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] | 1619 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1620 | case HTTP_MSG_HDR_L2_LWS: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1621 | http_msg_hdr_l2_lws: |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1622 | if (unlikely(HTTP_IS_SPHT(*ptr))) { |
| 1623 | /* LWS: replace HT,CR,LF with spaces */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1624 | for (; buf->p + msg->eol < ptr; msg->eol++) |
| 1625 | buf->p[msg->eol] = ' '; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1626 | goto http_msg_hdr_val; |
| 1627 | } |
| 1628 | http_msg_complete_header: |
| 1629 | /* |
| 1630 | * It was a new header, so the last one is finished. |
Willy Tarreau | fa4a03c | 2012-03-09 21:28:54 +0100 | [diff] [blame] | 1631 | * Assumes msg->sol points to the first char, msg->sov points |
| 1632 | * to the first character of the value and msg->eol to the |
| 1633 | * first CR or LF so we know how the line ends. We insert last |
| 1634 | * header into the index. |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1635 | */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1636 | 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] | 1637 | idx, idx->tail) < 0)) |
| 1638 | goto http_msg_invalid; |
Willy Tarreau | 230fd0b | 2006-12-17 12:05:00 +0100 | [diff] [blame] | 1639 | |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1640 | msg->sol = ptr - buf->p; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1641 | if (likely(!HTTP_IS_CRLF(*ptr))) { |
| 1642 | goto http_msg_hdr_name; |
| 1643 | } |
| 1644 | |
| 1645 | if (likely(*ptr == '\r')) |
| 1646 | EAT_AND_JUMP_OR_RETURN(http_msg_last_lf, HTTP_MSG_LAST_LF); |
| 1647 | goto http_msg_last_lf; |
Willy Tarreau | 230fd0b | 2006-12-17 12:05:00 +0100 | [diff] [blame] | 1648 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1649 | case HTTP_MSG_LAST_LF: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1650 | http_msg_last_lf: |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1651 | /* Assumes msg->sol points to the first of either CR or LF */ |
| 1652 | EXPECT_LF_HERE(ptr, http_msg_invalid); |
| 1653 | ptr++; |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1654 | msg->sov = msg->next = ptr - buf->p; |
Willy Tarreau | 3a215be | 2012-03-09 21:39:51 +0100 | [diff] [blame] | 1655 | msg->eoh = msg->sol; |
| 1656 | msg->sol = 0; |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1657 | msg->msg_state = HTTP_MSG_BODY; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1658 | return; |
Willy Tarreau | b56928a | 2012-04-16 14:51:55 +0200 | [diff] [blame] | 1659 | |
| 1660 | case HTTP_MSG_ERROR: |
| 1661 | /* this may only happen if we call http_msg_analyser() twice with an error */ |
| 1662 | break; |
| 1663 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1664 | default: |
Willy Tarreau | 3770f23 | 2013-12-07 00:01:53 +0100 | [diff] [blame] | 1665 | #ifdef DEBUG_FULL |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1666 | fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state); |
| 1667 | exit(1); |
Willy Tarreau | 230fd0b | 2006-12-17 12:05:00 +0100 | [diff] [blame] | 1668 | #endif |
Willy Tarreau | 3770f23 | 2013-12-07 00:01:53 +0100 | [diff] [blame] | 1669 | ; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1670 | } |
| 1671 | http_msg_ood: |
| 1672 | /* out of data */ |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1673 | msg->msg_state = state; |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1674 | msg->next = ptr - buf->p; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1675 | return; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1676 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1677 | http_msg_invalid: |
| 1678 | /* invalid message */ |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1679 | msg->msg_state = HTTP_MSG_ERROR; |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1680 | msg->next = ptr - buf->p; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1681 | return; |
| 1682 | } |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 1683 | |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 1684 | /* convert an HTTP/0.9 request into an HTTP/1.0 request. Returns 1 if the |
| 1685 | * conversion succeeded, 0 in case of error. If the request was already 1.X, |
| 1686 | * nothing is done and 1 is returned. |
| 1687 | */ |
Willy Tarreau | 418bfcc | 2012-03-09 13:56:20 +0100 | [diff] [blame] | 1688 | static int http_upgrade_v09_to_v10(struct http_txn *txn) |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 1689 | { |
| 1690 | int delta; |
| 1691 | char *cur_end; |
Willy Tarreau | 418bfcc | 2012-03-09 13:56:20 +0100 | [diff] [blame] | 1692 | struct http_msg *msg = &txn->req; |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 1693 | |
| 1694 | if (msg->sl.rq.v_l != 0) |
| 1695 | return 1; |
| 1696 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 1697 | cur_end = msg->chn->buf->p + msg->sl.rq.l; |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 1698 | delta = 0; |
| 1699 | |
| 1700 | if (msg->sl.rq.u_l == 0) { |
| 1701 | /* if no URI was set, add "/" */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 1702 | delta = buffer_replace2(msg->chn->buf, cur_end, cur_end, " /", 2); |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 1703 | cur_end += delta; |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 1704 | http_msg_move_end(msg, delta); |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 1705 | } |
| 1706 | /* add HTTP version */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 1707 | 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] | 1708 | http_msg_move_end(msg, delta); |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 1709 | cur_end += delta; |
Willy Tarreau | 69d8c5d | 2012-05-08 09:44:41 +0200 | [diff] [blame] | 1710 | cur_end = (char *)http_parse_reqline(msg, |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 1711 | HTTP_MSG_RQMETH, |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 1712 | msg->chn->buf->p, cur_end + 1, |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 1713 | NULL, NULL); |
| 1714 | if (unlikely(!cur_end)) |
| 1715 | return 0; |
| 1716 | |
| 1717 | /* we have a full HTTP/1.0 request now and we know that |
| 1718 | * we have either a CR or an LF at <ptr>. |
| 1719 | */ |
| 1720 | hdr_idx_set_start(&txn->hdr_idx, msg->sl.rq.l, *cur_end == '\r'); |
| 1721 | return 1; |
| 1722 | } |
| 1723 | |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 1724 | /* Parse the Connection: header of an HTTP request, looking for both "close" |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 1725 | * and "keep-alive" values. If we already know that some headers may safely |
| 1726 | * 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] | 1727 | * - bit 0 means remove "close" headers (in HTTP/1.0 requests/responses) |
| 1728 | * - 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] | 1729 | * Presence of the "Upgrade" token is also checked and reported. |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 1730 | * The TX_HDR_CONN_* flags are adjusted in txn->flags depending on what was |
| 1731 | * found, and TX_CON_*_SET is adjusted depending on what is left so only |
| 1732 | * harmless combinations may be removed. Do not call that after changes have |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 1733 | * been processed. |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 1734 | */ |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 1735 | 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] | 1736 | { |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 1737 | struct hdr_ctx ctx; |
Willy Tarreau | 88d349d | 2010-01-25 12:15:43 +0100 | [diff] [blame] | 1738 | const char *hdr_val = "Connection"; |
| 1739 | int hdr_len = 10; |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 1740 | |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 1741 | if (txn->flags & TX_HDR_CONN_PRS) |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 1742 | return; |
| 1743 | |
Willy Tarreau | 88d349d | 2010-01-25 12:15:43 +0100 | [diff] [blame] | 1744 | if (unlikely(txn->flags & TX_USE_PX_CONN)) { |
| 1745 | hdr_val = "Proxy-Connection"; |
| 1746 | hdr_len = 16; |
| 1747 | } |
| 1748 | |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 1749 | ctx.idx = 0; |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 1750 | txn->flags &= ~(TX_CON_KAL_SET|TX_CON_CLO_SET); |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 1751 | 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] | 1752 | if (ctx.vlen >= 10 && word_match(ctx.line + ctx.val, ctx.vlen, "keep-alive", 10)) { |
| 1753 | txn->flags |= TX_HDR_CONN_KAL; |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 1754 | if (to_del & 2) |
| 1755 | http_remove_header2(msg, &txn->hdr_idx, &ctx); |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 1756 | else |
| 1757 | txn->flags |= TX_CON_KAL_SET; |
| 1758 | } |
| 1759 | else if (ctx.vlen >= 5 && word_match(ctx.line + ctx.val, ctx.vlen, "close", 5)) { |
| 1760 | txn->flags |= TX_HDR_CONN_CLO; |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 1761 | if (to_del & 1) |
| 1762 | http_remove_header2(msg, &txn->hdr_idx, &ctx); |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 1763 | else |
| 1764 | txn->flags |= TX_CON_CLO_SET; |
| 1765 | } |
Willy Tarreau | 50fc777 | 2012-11-11 22:19:57 +0100 | [diff] [blame] | 1766 | else if (ctx.vlen >= 7 && word_match(ctx.line + ctx.val, ctx.vlen, "upgrade", 7)) { |
| 1767 | txn->flags |= TX_HDR_CONN_UPG; |
| 1768 | } |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 1769 | } |
| 1770 | |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 1771 | txn->flags |= TX_HDR_CONN_PRS; |
| 1772 | return; |
| 1773 | } |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 1774 | |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 1775 | /* Apply desired changes on the Connection: header. Values may be removed and/or |
| 1776 | * added depending on the <wanted> flags, which are exclusively composed of |
| 1777 | * TX_CON_CLO_SET and TX_CON_KAL_SET, depending on what flags are desired. The |
| 1778 | * TX_CON_*_SET flags are adjusted in txn->flags depending on what is left. |
| 1779 | */ |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 1780 | 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] | 1781 | { |
| 1782 | struct hdr_ctx ctx; |
Willy Tarreau | 88d349d | 2010-01-25 12:15:43 +0100 | [diff] [blame] | 1783 | const char *hdr_val = "Connection"; |
| 1784 | int hdr_len = 10; |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 1785 | |
| 1786 | ctx.idx = 0; |
| 1787 | |
Willy Tarreau | 88d349d | 2010-01-25 12:15:43 +0100 | [diff] [blame] | 1788 | |
| 1789 | if (unlikely(txn->flags & TX_USE_PX_CONN)) { |
| 1790 | hdr_val = "Proxy-Connection"; |
| 1791 | hdr_len = 16; |
| 1792 | } |
| 1793 | |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 1794 | txn->flags &= ~(TX_CON_CLO_SET | TX_CON_KAL_SET); |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 1795 | 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] | 1796 | if (ctx.vlen >= 10 && word_match(ctx.line + ctx.val, ctx.vlen, "keep-alive", 10)) { |
| 1797 | if (wanted & TX_CON_KAL_SET) |
| 1798 | txn->flags |= TX_CON_KAL_SET; |
| 1799 | else |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 1800 | http_remove_header2(msg, &txn->hdr_idx, &ctx); |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 1801 | } |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 1802 | else if (ctx.vlen >= 5 && word_match(ctx.line + ctx.val, ctx.vlen, "close", 5)) { |
| 1803 | if (wanted & TX_CON_CLO_SET) |
| 1804 | txn->flags |= TX_CON_CLO_SET; |
| 1805 | else |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 1806 | http_remove_header2(msg, &txn->hdr_idx, &ctx); |
Willy Tarreau | 0dfdf19 | 2010-01-05 11:33:11 +0100 | [diff] [blame] | 1807 | } |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 1808 | } |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 1809 | |
| 1810 | if (wanted == (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET))) |
| 1811 | return; |
| 1812 | |
| 1813 | if ((wanted & TX_CON_CLO_SET) && !(txn->flags & TX_CON_CLO_SET)) { |
| 1814 | txn->flags |= TX_CON_CLO_SET; |
Willy Tarreau | 88d349d | 2010-01-25 12:15:43 +0100 | [diff] [blame] | 1815 | hdr_val = "Connection: close"; |
| 1816 | hdr_len = 17; |
| 1817 | if (unlikely(txn->flags & TX_USE_PX_CONN)) { |
| 1818 | hdr_val = "Proxy-Connection: close"; |
| 1819 | hdr_len = 23; |
| 1820 | } |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 1821 | http_header_add_tail2(msg, &txn->hdr_idx, hdr_val, hdr_len); |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 1822 | } |
| 1823 | |
| 1824 | if ((wanted & TX_CON_KAL_SET) && !(txn->flags & TX_CON_KAL_SET)) { |
| 1825 | txn->flags |= TX_CON_KAL_SET; |
Willy Tarreau | 88d349d | 2010-01-25 12:15:43 +0100 | [diff] [blame] | 1826 | hdr_val = "Connection: keep-alive"; |
| 1827 | hdr_len = 22; |
| 1828 | if (unlikely(txn->flags & TX_USE_PX_CONN)) { |
| 1829 | hdr_val = "Proxy-Connection: keep-alive"; |
| 1830 | hdr_len = 28; |
| 1831 | } |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 1832 | http_header_add_tail2(msg, &txn->hdr_idx, hdr_val, hdr_len); |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 1833 | } |
| 1834 | return; |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 1835 | } |
| 1836 | |
Willy Tarreau | a458b67 | 2012-03-05 11:17:50 +0100 | [diff] [blame] | 1837 | /* Parse the chunk size at msg->next. Once done, it adjusts ->next to point to the |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 1838 | * first byte of body, and increments msg->sov by the number of bytes parsed, |
Willy Tarreau | 2692736 | 2012-05-18 23:22:52 +0200 | [diff] [blame] | 1839 | * so that we know we can forward between ->sol and ->sov. |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 1840 | * 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] | 1841 | * 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] | 1842 | */ |
Willy Tarreau | 24e6d97 | 2012-10-26 00:49:52 +0200 | [diff] [blame] | 1843 | static inline int http_parse_chunk_size(struct http_msg *msg) |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 1844 | { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 1845 | const struct buffer *buf = msg->chn->buf; |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1846 | const char *ptr = b_ptr(buf, msg->next); |
Willy Tarreau | 4baf44b | 2012-03-09 14:10:20 +0100 | [diff] [blame] | 1847 | const char *ptr_old = ptr; |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1848 | const char *end = buf->data + buf->size; |
| 1849 | const char *stop = bi_end(buf); |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 1850 | unsigned int chunk = 0; |
| 1851 | |
| 1852 | /* The chunk size is in the following form, though we are only |
| 1853 | * interested in the size and CRLF : |
| 1854 | * 1*HEXDIGIT *WSP *[ ';' extensions ] CRLF |
| 1855 | */ |
| 1856 | while (1) { |
| 1857 | int c; |
Willy Tarreau | 363a5bb | 2012-03-02 20:14:45 +0100 | [diff] [blame] | 1858 | if (ptr == stop) |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 1859 | return 0; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 1860 | c = hex2i(*ptr); |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 1861 | if (c < 0) /* not a hex digit anymore */ |
| 1862 | break; |
Willy Tarreau | 0161d62 | 2013-04-02 01:26:55 +0200 | [diff] [blame] | 1863 | if (unlikely(++ptr >= end)) |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1864 | ptr = buf->data; |
Willy Tarreau | 431946e | 2012-02-24 19:20:12 +0100 | [diff] [blame] | 1865 | if (chunk & 0xF8000000) /* integer overflow will occur if result >= 2GB */ |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 1866 | goto error; |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 1867 | chunk = (chunk << 4) + c; |
| 1868 | } |
| 1869 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 1870 | /* empty size not allowed */ |
Willy Tarreau | 0161d62 | 2013-04-02 01:26:55 +0200 | [diff] [blame] | 1871 | if (unlikely(ptr == ptr_old)) |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 1872 | goto error; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 1873 | |
| 1874 | while (http_is_spht[(unsigned char)*ptr]) { |
| 1875 | if (++ptr >= end) |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1876 | ptr = buf->data; |
Willy Tarreau | 0161d62 | 2013-04-02 01:26:55 +0200 | [diff] [blame] | 1877 | if (unlikely(ptr == stop)) |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 1878 | return 0; |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 1879 | } |
| 1880 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 1881 | /* Up to there, we know that at least one byte is present at *ptr. Check |
| 1882 | * for the end of chunk size. |
| 1883 | */ |
| 1884 | while (1) { |
| 1885 | if (likely(HTTP_IS_CRLF(*ptr))) { |
| 1886 | /* we now have a CR or an LF at ptr */ |
| 1887 | if (likely(*ptr == '\r')) { |
| 1888 | if (++ptr >= end) |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1889 | ptr = buf->data; |
Willy Tarreau | 363a5bb | 2012-03-02 20:14:45 +0100 | [diff] [blame] | 1890 | if (ptr == stop) |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 1891 | return 0; |
| 1892 | } |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 1893 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 1894 | if (*ptr != '\n') |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 1895 | goto error; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 1896 | if (++ptr >= end) |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1897 | ptr = buf->data; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 1898 | /* done */ |
| 1899 | break; |
| 1900 | } |
| 1901 | else if (*ptr == ';') { |
| 1902 | /* chunk extension, ends at next CRLF */ |
| 1903 | if (++ptr >= end) |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1904 | ptr = buf->data; |
Willy Tarreau | 363a5bb | 2012-03-02 20:14:45 +0100 | [diff] [blame] | 1905 | if (ptr == stop) |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 1906 | return 0; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 1907 | |
| 1908 | while (!HTTP_IS_CRLF(*ptr)) { |
| 1909 | if (++ptr >= end) |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1910 | ptr = buf->data; |
Willy Tarreau | 363a5bb | 2012-03-02 20:14:45 +0100 | [diff] [blame] | 1911 | if (ptr == stop) |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 1912 | return 0; |
| 1913 | } |
| 1914 | /* we have a CRLF now, loop above */ |
| 1915 | continue; |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 1916 | } |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 1917 | else |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 1918 | goto error; |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 1919 | } |
| 1920 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 1921 | /* OK we found our CRLF and now <ptr> points to the next byte, |
Willy Tarreau | a458b67 | 2012-03-05 11:17:50 +0100 | [diff] [blame] | 1922 | * which may or may not be present. We save that into ->next and |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 1923 | * ->sov. |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 1924 | */ |
Willy Tarreau | 0161d62 | 2013-04-02 01:26:55 +0200 | [diff] [blame] | 1925 | if (unlikely(ptr < ptr_old)) |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1926 | msg->sov += buf->size; |
Willy Tarreau | a458b67 | 2012-03-05 11:17:50 +0100 | [diff] [blame] | 1927 | msg->sov += ptr - ptr_old; |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1928 | msg->next = buffer_count(buf, buf->p, ptr); |
Willy Tarreau | 124d991 | 2011-03-01 20:30:48 +0100 | [diff] [blame] | 1929 | msg->chunk_len = chunk; |
| 1930 | msg->body_len += chunk; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 1931 | msg->msg_state = chunk ? HTTP_MSG_DATA : HTTP_MSG_TRAILERS; |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 1932 | return 1; |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 1933 | error: |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1934 | msg->err_pos = buffer_count(buf, buf->p, ptr); |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 1935 | return -1; |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 1936 | } |
| 1937 | |
Willy Tarreau | 4baf44b | 2012-03-09 14:10:20 +0100 | [diff] [blame] | 1938 | /* This function skips trailers in the buffer associated with HTTP |
Willy Tarreau | a458b67 | 2012-03-05 11:17:50 +0100 | [diff] [blame] | 1939 | * 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] | 1940 | * the trailers is found, it is automatically scheduled to be forwarded, |
| 1941 | * msg->msg_state switches to HTTP_MSG_DONE, and the function returns >0. |
| 1942 | * If not enough data are available, the function does not change anything |
Willy Tarreau | a458b67 | 2012-03-05 11:17:50 +0100 | [diff] [blame] | 1943 | * except maybe msg->next and msg->sov if it could parse some lines, and returns |
Willy Tarreau | 638cd02 | 2010-01-03 07:42:04 +0100 | [diff] [blame] | 1944 | * zero. If a parse error is encountered, the function returns < 0 and does not |
Willy Tarreau | a458b67 | 2012-03-05 11:17:50 +0100 | [diff] [blame] | 1945 | * change anything except maybe msg->next and msg->sov. Note that the message |
Willy Tarreau | 638cd02 | 2010-01-03 07:42:04 +0100 | [diff] [blame] | 1946 | * must already be in HTTP_MSG_TRAILERS state before calling this function, |
| 1947 | * which implies that all non-trailers data have already been scheduled for |
Willy Tarreau | 2692736 | 2012-05-18 23:22:52 +0200 | [diff] [blame] | 1948 | * forwarding, and that the difference between msg->sol and msg->sov exactly |
Willy Tarreau | 638cd02 | 2010-01-03 07:42:04 +0100 | [diff] [blame] | 1949 | * matches the length of trailers already parsed and not forwarded. It is also |
| 1950 | * important to note that this function is designed to be able to parse wrapped |
| 1951 | * headers at end of buffer. |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 1952 | */ |
Willy Tarreau | 24e6d97 | 2012-10-26 00:49:52 +0200 | [diff] [blame] | 1953 | static int http_forward_trailers(struct http_msg *msg) |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 1954 | { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 1955 | const struct buffer *buf = msg->chn->buf; |
Willy Tarreau | 4baf44b | 2012-03-09 14:10:20 +0100 | [diff] [blame] | 1956 | |
Willy Tarreau | a458b67 | 2012-03-05 11:17:50 +0100 | [diff] [blame] | 1957 | /* we have msg->next which points to next line. Look for CRLF. */ |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 1958 | while (1) { |
Willy Tarreau | 4baf44b | 2012-03-09 14:10:20 +0100 | [diff] [blame] | 1959 | const char *p1 = NULL, *p2 = NULL; |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1960 | const char *ptr = b_ptr(buf, msg->next); |
| 1961 | const char *stop = bi_end(buf); |
Willy Tarreau | 638cd02 | 2010-01-03 07:42:04 +0100 | [diff] [blame] | 1962 | int bytes; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 1963 | |
| 1964 | /* scan current line and stop at LF or CRLF */ |
| 1965 | while (1) { |
Willy Tarreau | 363a5bb | 2012-03-02 20:14:45 +0100 | [diff] [blame] | 1966 | if (ptr == stop) |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 1967 | return 0; |
| 1968 | |
| 1969 | if (*ptr == '\n') { |
| 1970 | if (!p1) |
| 1971 | p1 = ptr; |
| 1972 | p2 = ptr; |
| 1973 | break; |
| 1974 | } |
| 1975 | |
| 1976 | if (*ptr == '\r') { |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 1977 | if (p1) { |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1978 | msg->err_pos = buffer_count(buf, buf->p, ptr); |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 1979 | return -1; |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 1980 | } |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 1981 | p1 = ptr; |
| 1982 | } |
| 1983 | |
| 1984 | ptr++; |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1985 | if (ptr >= buf->data + buf->size) |
| 1986 | ptr = buf->data; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 1987 | } |
| 1988 | |
| 1989 | /* after LF; point to beginning of next line */ |
| 1990 | p2++; |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1991 | if (p2 >= buf->data + buf->size) |
| 1992 | p2 = buf->data; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 1993 | |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1994 | bytes = p2 - b_ptr(buf, msg->next); |
Willy Tarreau | 638cd02 | 2010-01-03 07:42:04 +0100 | [diff] [blame] | 1995 | if (bytes < 0) |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1996 | bytes += buf->size; |
Willy Tarreau | 638cd02 | 2010-01-03 07:42:04 +0100 | [diff] [blame] | 1997 | |
| 1998 | /* schedule this line for forwarding */ |
| 1999 | msg->sov += bytes; |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 2000 | if (msg->sov >= buf->size) |
| 2001 | msg->sov -= buf->size; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2002 | |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 2003 | if (p1 == b_ptr(buf, msg->next)) { |
Willy Tarreau | 638cd02 | 2010-01-03 07:42:04 +0100 | [diff] [blame] | 2004 | /* LF/CRLF at beginning of line => end of trailers at p2. |
| 2005 | * Everything was scheduled for forwarding, there's nothing |
| 2006 | * left from this message. |
Willy Tarreau | 5523b32 | 2009-12-29 12:05:52 +0100 | [diff] [blame] | 2007 | */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 2008 | msg->next = buffer_count(buf, buf->p, p2); |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2009 | msg->msg_state = HTTP_MSG_DONE; |
| 2010 | return 1; |
| 2011 | } |
| 2012 | /* OK, next line then */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 2013 | msg->next = buffer_count(buf, buf->p, p2); |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2014 | } |
| 2015 | } |
| 2016 | |
Willy Tarreau | 54d23df | 2012-10-25 19:04:45 +0200 | [diff] [blame] | 2017 | /* This function may be called only in HTTP_MSG_CHUNK_CRLF. It reads the CRLF or |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2018 | * a possible LF alone at the end of a chunk. It automatically adjusts msg->sov, |
Willy Tarreau | 2692736 | 2012-05-18 23:22:52 +0200 | [diff] [blame] | 2019 | * ->sol, ->next in order to include this part into the next forwarding phase. |
Willy Tarreau | a458b67 | 2012-03-05 11:17:50 +0100 | [diff] [blame] | 2020 | * 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] | 2021 | * It also sets msg_state to HTTP_MSG_CHUNK_SIZE and returns >0 on success. If |
| 2022 | * not enough data are available, the function does not change anything and |
| 2023 | * returns zero. If a parse error is encountered, the function returns < 0 and |
| 2024 | * does not change anything. Note: this function is designed to parse wrapped |
| 2025 | * CRLF at the end of the buffer. |
| 2026 | */ |
Willy Tarreau | 24e6d97 | 2012-10-26 00:49:52 +0200 | [diff] [blame] | 2027 | static inline int http_skip_chunk_crlf(struct http_msg *msg) |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2028 | { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2029 | const struct buffer *buf = msg->chn->buf; |
Willy Tarreau | 4baf44b | 2012-03-09 14:10:20 +0100 | [diff] [blame] | 2030 | const char *ptr; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2031 | int bytes; |
| 2032 | |
| 2033 | /* NB: we'll check data availabilty at the end. It's not a |
| 2034 | * problem because whatever we match first will be checked |
| 2035 | * against the correct length. |
| 2036 | */ |
| 2037 | bytes = 1; |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 2038 | ptr = buf->p; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2039 | if (*ptr == '\r') { |
| 2040 | bytes++; |
| 2041 | ptr++; |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 2042 | if (ptr >= buf->data + buf->size) |
| 2043 | ptr = buf->data; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2044 | } |
| 2045 | |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 2046 | if (bytes > buf->i) |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2047 | return 0; |
| 2048 | |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 2049 | if (*ptr != '\n') { |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 2050 | msg->err_pos = buffer_count(buf, buf->p, ptr); |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2051 | return -1; |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 2052 | } |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2053 | |
| 2054 | ptr++; |
Willy Tarreau | 0161d62 | 2013-04-02 01:26:55 +0200 | [diff] [blame] | 2055 | if (unlikely(ptr >= buf->data + buf->size)) |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 2056 | ptr = buf->data; |
Willy Tarreau | 2692736 | 2012-05-18 23:22:52 +0200 | [diff] [blame] | 2057 | /* prepare the CRLF to be forwarded (between ->sol and ->sov) */ |
| 2058 | msg->sol = 0; |
Willy Tarreau | ea1175a | 2012-03-05 15:52:30 +0100 | [diff] [blame] | 2059 | msg->sov = msg->next = bytes; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2060 | msg->msg_state = HTTP_MSG_CHUNK_SIZE; |
| 2061 | return 1; |
| 2062 | } |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 2063 | |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 2064 | |
| 2065 | /* |
| 2066 | * Selects a compression algorithm depending on the client request. |
Willy Tarreau | 05d8460 | 2012-10-26 02:11:25 +0200 | [diff] [blame] | 2067 | */ |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 2068 | int select_compression_request_header(struct session *s, struct buffer *req) |
| 2069 | { |
| 2070 | struct http_txn *txn = &s->txn; |
Willy Tarreau | 70737d1 | 2012-10-27 00:34:28 +0200 | [diff] [blame] | 2071 | struct http_msg *msg = &txn->req; |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 2072 | struct hdr_ctx ctx; |
| 2073 | struct comp_algo *comp_algo = NULL; |
Willy Tarreau | 3c7b97b | 2012-10-26 14:50:26 +0200 | [diff] [blame] | 2074 | struct comp_algo *comp_algo_back = NULL; |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 2075 | |
Finn Arne Gangstad | cbb9a4b | 2012-10-29 21:43:01 +0100 | [diff] [blame] | 2076 | /* Disable compression for older user agents announcing themselves as "Mozilla/4" |
| 2077 | * 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] | 2078 | * See http://zoompf.com/2012/02/lose-the-wait-http-compression for more details. |
| 2079 | */ |
| 2080 | ctx.idx = 0; |
| 2081 | if (http_find_header2("User-Agent", 10, req->p, &txn->hdr_idx, &ctx) && |
| 2082 | ctx.vlen >= 9 && |
Finn Arne Gangstad | cbb9a4b | 2012-10-29 21:43:01 +0100 | [diff] [blame] | 2083 | memcmp(ctx.line + ctx.val, "Mozilla/4", 9) == 0 && |
| 2084 | (ctx.vlen < 31 || |
| 2085 | memcmp(ctx.line + ctx.val + 25, "MSIE ", 5) != 0 || |
| 2086 | ctx.line[ctx.val + 30] < '6' || |
| 2087 | (ctx.line[ctx.val + 30] == '6' && |
| 2088 | (ctx.vlen < 54 || memcmp(ctx.line + 51, "SV1", 3) != 0)))) { |
| 2089 | s->comp_algo = NULL; |
| 2090 | return 0; |
Willy Tarreau | 05d8460 | 2012-10-26 02:11:25 +0200 | [diff] [blame] | 2091 | } |
| 2092 | |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 2093 | /* search for the algo in the backend in priority or the frontend */ |
Willy Tarreau | 3c7b97b | 2012-10-26 14:50:26 +0200 | [diff] [blame] | 2094 | if ((s->be->comp && (comp_algo_back = s->be->comp->algos)) || (s->fe->comp && (comp_algo_back = s->fe->comp->algos))) { |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 2095 | ctx.idx = 0; |
| 2096 | while (http_find_header2("Accept-Encoding", 15, req->p, &txn->hdr_idx, &ctx)) { |
Willy Tarreau | 3c7b97b | 2012-10-26 14:50:26 +0200 | [diff] [blame] | 2097 | for (comp_algo = comp_algo_back; comp_algo; comp_algo = comp_algo->next) { |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 2098 | if (word_match(ctx.line + ctx.val, ctx.vlen, comp_algo->name, comp_algo->name_len)) { |
| 2099 | s->comp_algo = comp_algo; |
Willy Tarreau | 70737d1 | 2012-10-27 00:34:28 +0200 | [diff] [blame] | 2100 | |
| 2101 | /* remove all occurrences of the header when "compression offload" is set */ |
| 2102 | |
| 2103 | if ((s->be->comp && s->be->comp->offload) || |
| 2104 | (s->fe->comp && s->fe->comp->offload)) { |
| 2105 | http_remove_header2(msg, &txn->hdr_idx, &ctx); |
| 2106 | ctx.idx = 0; |
| 2107 | while (http_find_header2("Accept-Encoding", 15, req->p, &txn->hdr_idx, &ctx)) { |
| 2108 | http_remove_header2(msg, &txn->hdr_idx, &ctx); |
| 2109 | } |
| 2110 | } |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 2111 | return 1; |
| 2112 | } |
| 2113 | } |
| 2114 | } |
| 2115 | } |
| 2116 | |
| 2117 | /* identity is implicit does not require headers */ |
Willy Tarreau | 3c7b97b | 2012-10-26 14:50:26 +0200 | [diff] [blame] | 2118 | if ((s->be->comp && (comp_algo_back = s->be->comp->algos)) || (s->fe->comp && (comp_algo_back = s->fe->comp->algos))) { |
| 2119 | for (comp_algo = comp_algo_back; comp_algo; comp_algo = comp_algo->next) { |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 2120 | if (comp_algo->add_data == identity_add_data) { |
| 2121 | s->comp_algo = comp_algo; |
| 2122 | return 1; |
| 2123 | } |
| 2124 | } |
| 2125 | } |
| 2126 | |
| 2127 | s->comp_algo = NULL; |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 2128 | return 0; |
| 2129 | } |
| 2130 | |
| 2131 | /* |
| 2132 | * Selects a comression algorithm depending of the server response. |
| 2133 | */ |
| 2134 | int select_compression_response_header(struct session *s, struct buffer *res) |
| 2135 | { |
| 2136 | struct http_txn *txn = &s->txn; |
| 2137 | struct http_msg *msg = &txn->rsp; |
| 2138 | struct hdr_ctx ctx; |
| 2139 | struct comp_type *comp_type; |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 2140 | |
| 2141 | /* no common compression algorithm was found in request header */ |
| 2142 | if (s->comp_algo == NULL) |
| 2143 | goto fail; |
| 2144 | |
| 2145 | /* HTTP < 1.1 should not be compressed */ |
Willy Tarreau | 7257550 | 2013-12-24 14:41:35 +0100 | [diff] [blame] | 2146 | 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] | 2147 | goto fail; |
| 2148 | |
William Lallemand | d300261 | 2012-11-26 14:34:47 +0100 | [diff] [blame] | 2149 | /* 200 only */ |
| 2150 | if (txn->status != 200) |
| 2151 | goto fail; |
| 2152 | |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 2153 | /* Content-Length is null */ |
| 2154 | if (!(msg->flags & HTTP_MSGF_TE_CHNK) && msg->body_len == 0) |
| 2155 | goto fail; |
| 2156 | |
Willy Tarreau | 667c2a3 | 2013-04-09 08:13:58 +0200 | [diff] [blame] | 2157 | /* TEMPORARY WORKAROUND: do not compress if response is chunked !!!!!! */ |
| 2158 | if (msg->flags & HTTP_MSGF_TE_CHNK) |
| 2159 | goto fail; |
| 2160 | |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 2161 | /* content is already compressed */ |
Willy Tarreau | 0a80a8d | 2012-11-26 16:33:37 +0100 | [diff] [blame] | 2162 | ctx.idx = 0; |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 2163 | if (http_find_header2("Content-Encoding", 16, res->p, &txn->hdr_idx, &ctx)) |
| 2164 | goto fail; |
| 2165 | |
Willy Tarreau | 56e9ffa | 2013-01-05 16:20:35 +0100 | [diff] [blame] | 2166 | /* no compression when Cache-Control: no-transform is present in the message */ |
| 2167 | ctx.idx = 0; |
| 2168 | while (http_find_header2("Cache-Control", 13, res->p, &txn->hdr_idx, &ctx)) { |
| 2169 | if (word_match(ctx.line + ctx.val, ctx.vlen, "no-transform", 12)) |
| 2170 | goto fail; |
| 2171 | } |
| 2172 | |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 2173 | comp_type = NULL; |
| 2174 | |
Willy Tarreau | 0a80a8d | 2012-11-26 16:33:37 +0100 | [diff] [blame] | 2175 | /* we don't want to compress multipart content-types, nor content-types that are |
| 2176 | * not listed in the "compression type" directive if any. If no content-type was |
| 2177 | * found but configuration requires one, we don't compress either. Backend has |
| 2178 | * the priority. |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 2179 | */ |
Willy Tarreau | 0a80a8d | 2012-11-26 16:33:37 +0100 | [diff] [blame] | 2180 | ctx.idx = 0; |
| 2181 | if (http_find_header2("Content-Type", 12, res->p, &txn->hdr_idx, &ctx)) { |
| 2182 | if (ctx.vlen >= 9 && strncasecmp("multipart", ctx.line+ctx.val, 9) == 0) |
| 2183 | goto fail; |
| 2184 | |
| 2185 | if ((s->be->comp && (comp_type = s->be->comp->types)) || |
| 2186 | (s->fe->comp && (comp_type = s->fe->comp->types))) { |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 2187 | for (; comp_type; comp_type = comp_type->next) { |
Willy Tarreau | 0a80a8d | 2012-11-26 16:33:37 +0100 | [diff] [blame] | 2188 | if (ctx.vlen >= comp_type->name_len && |
| 2189 | 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] | 2190 | /* this Content-Type should be compressed */ |
| 2191 | break; |
| 2192 | } |
Willy Tarreau | 0a80a8d | 2012-11-26 16:33:37 +0100 | [diff] [blame] | 2193 | /* this Content-Type should not be compressed */ |
| 2194 | if (comp_type == NULL) |
| 2195 | goto fail; |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 2196 | } |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 2197 | } |
Willy Tarreau | 0a80a8d | 2012-11-26 16:33:37 +0100 | [diff] [blame] | 2198 | else { /* no content-type header */ |
| 2199 | if ((s->be->comp && s->be->comp->types) || (s->fe->comp && s->fe->comp->types)) |
| 2200 | goto fail; /* a content-type was required */ |
William Lallemand | d300261 | 2012-11-26 14:34:47 +0100 | [diff] [blame] | 2201 | } |
| 2202 | |
William Lallemand | d85f917 | 2012-11-09 17:05:39 +0100 | [diff] [blame] | 2203 | /* limit compression rate */ |
| 2204 | if (global.comp_rate_lim > 0) |
| 2205 | if (read_freq_ctr(&global.comp_bps_in) > global.comp_rate_lim) |
| 2206 | goto fail; |
| 2207 | |
William Lallemand | 072a2bf | 2012-11-20 17:01:01 +0100 | [diff] [blame] | 2208 | /* limit cpu usage */ |
| 2209 | if (idle_pct < compress_min_idle) |
| 2210 | goto fail; |
| 2211 | |
William Lallemand | 4c49fae | 2012-11-07 15:00:23 +0100 | [diff] [blame] | 2212 | /* initialize compression */ |
William Lallemand | f374783 | 2012-11-09 12:33:10 +0100 | [diff] [blame] | 2213 | 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] | 2214 | goto fail; |
| 2215 | |
William Lallemand | ec3e389 | 2012-11-12 17:02:18 +0100 | [diff] [blame] | 2216 | s->flags |= SN_COMP_READY; |
| 2217 | |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 2218 | /* remove Content-Length header */ |
Willy Tarreau | 0a80a8d | 2012-11-26 16:33:37 +0100 | [diff] [blame] | 2219 | ctx.idx = 0; |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 2220 | if ((msg->flags & HTTP_MSGF_CNT_LEN) && http_find_header2("Content-Length", 14, res->p, &txn->hdr_idx, &ctx)) |
| 2221 | http_remove_header2(msg, &txn->hdr_idx, &ctx); |
| 2222 | |
| 2223 | /* add Transfer-Encoding header */ |
| 2224 | if (!(msg->flags & HTTP_MSGF_TE_CHNK)) |
| 2225 | http_header_add_tail2(&txn->rsp, &txn->hdr_idx, "Transfer-Encoding: chunked", 26); |
| 2226 | |
| 2227 | /* |
| 2228 | * Add Content-Encoding header when it's not identity encoding. |
| 2229 | * RFC 2616 : Identity encoding: This content-coding is used only in the |
| 2230 | * Accept-Encoding header, and SHOULD NOT be used in the Content-Encoding |
| 2231 | * header. |
| 2232 | */ |
| 2233 | if (s->comp_algo->add_data != identity_add_data) { |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 2234 | trash.len = 18; |
| 2235 | memcpy(trash.str, "Content-Encoding: ", trash.len); |
| 2236 | memcpy(trash.str + trash.len, s->comp_algo->name, s->comp_algo->name_len); |
| 2237 | trash.len += s->comp_algo->name_len; |
| 2238 | trash.str[trash.len] = '\0'; |
| 2239 | 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] | 2240 | } |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 2241 | return 1; |
| 2242 | |
| 2243 | fail: |
Willy Tarreau | b97b619 | 2012-11-19 14:55:02 +0100 | [diff] [blame] | 2244 | s->comp_algo = NULL; |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 2245 | return 0; |
| 2246 | } |
| 2247 | |
| 2248 | |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 2249 | /* This stream analyser waits for a complete HTTP request. It returns 1 if the |
| 2250 | * processing can continue on next analysers, or zero if it either needs more |
| 2251 | * data or wants to immediately abort the request (eg: timeout, error, ...). It |
| 2252 | * is tied to AN_REQ_WAIT_HTTP and may may remove itself from s->req->analysers |
| 2253 | * when it has nothing left to do, and may remove any analyser when it wants to |
| 2254 | * abort. |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2255 | */ |
Willy Tarreau | 7421efb | 2012-07-02 15:11:27 +0200 | [diff] [blame] | 2256 | int http_wait_for_request(struct session *s, struct channel *req, int an_bit) |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2257 | { |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2258 | /* |
| 2259 | * We will parse the partial (or complete) lines. |
| 2260 | * We will check the request syntax, and also join multi-line |
| 2261 | * headers. An index of all the lines will be elaborated while |
| 2262 | * parsing. |
| 2263 | * |
| 2264 | * For the parsing, we use a 28 states FSM. |
| 2265 | * |
| 2266 | * Here is the information we currently have : |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2267 | * req->buf->p = beginning of request |
| 2268 | * req->buf->p + msg->eoh = end of processed headers / start of current one |
| 2269 | * req->buf->p + req->buf->i = end of input data |
Willy Tarreau | 2692736 | 2012-05-18 23:22:52 +0200 | [diff] [blame] | 2270 | * msg->eol = end of current header or line (LF or CRLF) |
| 2271 | * msg->next = first non-visited byte |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 2272 | * |
| 2273 | * At end of parsing, we may perform a capture of the error (if any), and |
| 2274 | * we will set a few fields (msg->sol, txn->meth, sn->flags/SN_REDIRECTABLE). |
| 2275 | * We also check for monitor-uri, logging, HTTP/0.9 to 1.0 conversion, and |
| 2276 | * finally headers capture. |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2277 | */ |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 2278 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2279 | int cur_idx; |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 2280 | int use_close_only; |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2281 | struct http_txn *txn = &s->txn; |
| 2282 | struct http_msg *msg = &txn->req; |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 2283 | struct hdr_ctx ctx; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 2284 | |
Willy Tarreau | 02d6cfc | 2012-03-01 18:19:58 +0100 | [diff] [blame] | 2285 | DPRINTF(stderr,"[%u] %s: session=%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] | 2286 | now_ms, __FUNCTION__, |
| 2287 | s, |
| 2288 | req, |
| 2289 | req->rex, req->wex, |
| 2290 | req->flags, |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2291 | req->buf->i, |
Willy Tarreau | 6bf1736 | 2009-02-24 10:48:35 +0100 | [diff] [blame] | 2292 | req->analysers); |
| 2293 | |
Willy Tarreau | 52a0c60 | 2009-08-16 22:45:38 +0200 | [diff] [blame] | 2294 | /* we're speaking HTTP here, so let's speak HTTP to the client */ |
| 2295 | s->srv_error = http_return_srv_error; |
| 2296 | |
Willy Tarreau | 83e3af0 | 2009-12-28 17:39:57 +0100 | [diff] [blame] | 2297 | /* There's a protected area at the end of the buffer for rewriting |
| 2298 | * purposes. We don't want to start to parse the request if the |
| 2299 | * protected area is affected, because we may have to move processed |
| 2300 | * data later, which is much more complicated. |
| 2301 | */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2302 | if (buffer_not_empty(req->buf) && msg->msg_state < HTTP_MSG_ERROR) { |
Willy Tarreau | 379357a | 2013-06-08 12:55:46 +0200 | [diff] [blame] | 2303 | if (txn->flags & TX_NOT_FIRST) { |
| 2304 | if (unlikely(!channel_reserved(req))) { |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 2305 | 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] | 2306 | goto failed_keep_alive; |
Willy Tarreau | 2ab6eb1 | 2010-01-02 22:04:45 +0100 | [diff] [blame] | 2307 | /* 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] | 2308 | channel_dont_connect(req); |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 2309 | req->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */ |
Willy Tarreau | d7ad9f5 | 2013-12-31 17:26:25 +0100 | [diff] [blame] | 2310 | req->flags |= CF_WAKE_WRITE; |
Willy Tarreau | 2ab6eb1 | 2010-01-02 22:04:45 +0100 | [diff] [blame] | 2311 | return 0; |
| 2312 | } |
Willy Tarreau | 379357a | 2013-06-08 12:55:46 +0200 | [diff] [blame] | 2313 | if (unlikely(bi_end(req->buf) < b_ptr(req->buf, msg->next) || |
| 2314 | bi_end(req->buf) > req->buf->data + req->buf->size - global.tune.maxrewrite)) |
| 2315 | buffer_slow_realign(req->buf); |
Willy Tarreau | 83e3af0 | 2009-12-28 17:39:57 +0100 | [diff] [blame] | 2316 | } |
| 2317 | |
Willy Tarreau | 065e833 | 2010-01-08 00:30:20 +0100 | [diff] [blame] | 2318 | /* Note that we have the same problem with the response ; we |
| 2319 | * may want to send a redirect, error or anything which requires |
| 2320 | * some spare space. So we'll ensure that we have at least |
| 2321 | * maxrewrite bytes available in the response buffer before |
| 2322 | * processing that one. This will only affect pipelined |
| 2323 | * keep-alive requests. |
| 2324 | */ |
| 2325 | if ((txn->flags & TX_NOT_FIRST) && |
Willy Tarreau | 379357a | 2013-06-08 12:55:46 +0200 | [diff] [blame] | 2326 | unlikely(!channel_reserved(s->rep) || |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2327 | bi_end(s->rep->buf) < b_ptr(s->rep->buf, txn->rsp.next) || |
| 2328 | bi_end(s->rep->buf) > s->rep->buf->data + s->rep->buf->size - global.tune.maxrewrite)) { |
| 2329 | if (s->rep->buf->o) { |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 2330 | if (s->rep->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_WRITE_ERROR|CF_WRITE_TIMEOUT)) |
Willy Tarreau | 6464841 | 2010-03-05 10:41:54 +0100 | [diff] [blame] | 2331 | goto failed_keep_alive; |
Willy Tarreau | 065e833 | 2010-01-08 00:30:20 +0100 | [diff] [blame] | 2332 | /* don't let a connection request be initiated */ |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 2333 | channel_dont_connect(req); |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 2334 | s->rep->flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */ |
Willy Tarreau | d7ad9f5 | 2013-12-31 17:26:25 +0100 | [diff] [blame] | 2335 | s->rep->flags |= CF_WAKE_WRITE; |
Willy Tarreau | 0499e35 | 2010-12-17 07:13:42 +0100 | [diff] [blame] | 2336 | s->rep->analysers |= an_bit; /* wake us up once it changes */ |
Willy Tarreau | 065e833 | 2010-01-08 00:30:20 +0100 | [diff] [blame] | 2337 | return 0; |
| 2338 | } |
| 2339 | } |
| 2340 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2341 | if (likely(msg->next < req->buf->i)) /* some unparsed data are available */ |
Willy Tarreau | a560c21 | 2012-03-09 13:50:57 +0100 | [diff] [blame] | 2342 | http_msg_analyzer(msg, &txn->hdr_idx); |
Willy Tarreau | 83e3af0 | 2009-12-28 17:39:57 +0100 | [diff] [blame] | 2343 | } |
| 2344 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2345 | /* 1: we might have to print this header in debug mode */ |
| 2346 | if (unlikely((global.mode & MODE_DEBUG) && |
| 2347 | (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) && |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 2348 | (msg->msg_state >= HTTP_MSG_BODY || msg->msg_state == HTTP_MSG_ERROR))) { |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2349 | char *eol, *sol; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2350 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2351 | sol = req->buf->p; |
Willy Tarreau | e92693a | 2012-09-24 21:13:39 +0200 | [diff] [blame] | 2352 | /* this is a bit complex : in case of error on the request line, |
| 2353 | * we know that rq.l is still zero, so we display only the part |
| 2354 | * up to the end of the line (truncated by debug_hdr). |
| 2355 | */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2356 | 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] | 2357 | debug_hdr("clireq", s, sol, eol); |
Willy Tarreau | 45e73e3 | 2006-12-17 00:05:15 +0100 | [diff] [blame] | 2358 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2359 | sol += hdr_idx_first_pos(&txn->hdr_idx); |
| 2360 | cur_idx = hdr_idx_first_idx(&txn->hdr_idx); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2361 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2362 | while (cur_idx) { |
| 2363 | eol = sol + txn->hdr_idx.v[cur_idx].len; |
| 2364 | debug_hdr("clihdr", s, sol, eol); |
| 2365 | sol = eol + txn->hdr_idx.v[cur_idx].cr + 1; |
| 2366 | cur_idx = txn->hdr_idx.v[cur_idx].next; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2367 | } |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2368 | } |
| 2369 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2370 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2371 | /* |
| 2372 | * Now we quickly check if we have found a full valid request. |
| 2373 | * If not so, we check the FD and buffer states before leaving. |
| 2374 | * A full request is indicated by the fact that we have seen |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 2375 | * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid |
Willy Tarreau | d3c343f | 2010-01-16 10:26:19 +0100 | [diff] [blame] | 2376 | * requests are checked first. When waiting for a second request |
| 2377 | * on a keep-alive session, if we encounter and error, close, t/o, |
| 2378 | * we note the error in the session flags but don't set any state. |
| 2379 | * Since the error will be noted there, it will not be counted by |
| 2380 | * process_session() as a frontend error. |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 2381 | * Last, we may increase some tracked counters' http request errors on |
| 2382 | * the cases that are deliberately the client's fault. For instance, |
| 2383 | * a timeout or connection reset is not counted as an error. However |
| 2384 | * a bad request is. |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2385 | */ |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2386 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 2387 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2388 | /* |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2389 | * First, let's catch bad requests. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2390 | */ |
Willy Tarreau | 3e1b6d1 | 2010-03-04 23:02:38 +0100 | [diff] [blame] | 2391 | if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) { |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 2392 | session_inc_http_req_ctr(s); |
| 2393 | session_inc_http_err_ctr(s); |
Willy Tarreau | 3e1b6d1 | 2010-03-04 23:02:38 +0100 | [diff] [blame] | 2394 | proxy_inc_fe_req_ctr(s->fe); |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2395 | goto return_bad_req; |
Willy Tarreau | 3e1b6d1 | 2010-03-04 23:02:38 +0100 | [diff] [blame] | 2396 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2397 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2398 | /* 1: Since we are in header mode, if there's no space |
| 2399 | * left for headers, we won't be able to free more |
| 2400 | * later, so the session will never terminate. We |
| 2401 | * must terminate it now. |
| 2402 | */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2403 | if (unlikely(buffer_full(req->buf, global.tune.maxrewrite))) { |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2404 | /* FIXME: check if URI is set and return Status |
| 2405 | * 414 Request URI too long instead. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2406 | */ |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 2407 | session_inc_http_req_ctr(s); |
| 2408 | session_inc_http_err_ctr(s); |
Willy Tarreau | 3e1b6d1 | 2010-03-04 23:02:38 +0100 | [diff] [blame] | 2409 | proxy_inc_fe_req_ctr(s->fe); |
Willy Tarreau | fec4d89 | 2011-09-02 20:04:57 +0200 | [diff] [blame] | 2410 | if (msg->err_pos < 0) |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2411 | msg->err_pos = req->buf->i; |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2412 | goto return_bad_req; |
| 2413 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2414 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2415 | /* 2: have we encountered a read error ? */ |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 2416 | else if (req->flags & CF_READ_ERROR) { |
Willy Tarreau | d3c343f | 2010-01-16 10:26:19 +0100 | [diff] [blame] | 2417 | if (!(s->flags & SN_ERR_MASK)) |
| 2418 | s->flags |= SN_ERR_CLICL; |
| 2419 | |
Willy Tarreau | fcffa69 | 2010-01-10 14:21:19 +0100 | [diff] [blame] | 2420 | if (txn->flags & TX_WAIT_NEXT_RQ) |
Willy Tarreau | b608feb | 2010-01-02 22:47:18 +0100 | [diff] [blame] | 2421 | goto failed_keep_alive; |
| 2422 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2423 | /* we cannot return any message on error */ |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 2424 | if (msg->err_pos >= 0) { |
Willy Tarreau | 8a0cef2 | 2012-03-09 13:39:23 +0100 | [diff] [blame] | 2425 | http_capture_bad_message(&s->fe->invalid_req, s, msg, msg->msg_state, s->fe); |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 2426 | session_inc_http_err_ctr(s); |
| 2427 | } |
| 2428 | |
Willy Tarreau | dc979f2 | 2012-12-04 10:39:01 +0100 | [diff] [blame] | 2429 | txn->status = 400; |
| 2430 | stream_int_retnclose(req->prod, NULL); |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2431 | msg->msg_state = HTTP_MSG_ERROR; |
| 2432 | req->analysers = 0; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 2433 | |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 2434 | session_inc_http_req_ctr(s); |
Willy Tarreau | 3e1b6d1 | 2010-03-04 23:02:38 +0100 | [diff] [blame] | 2435 | proxy_inc_fe_req_ctr(s->fe); |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 2436 | s->fe->fe_counters.failed_req++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 2437 | if (s->listener->counters) |
| 2438 | s->listener->counters->failed_req++; |
| 2439 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2440 | if (!(s->flags & SN_FINST_MASK)) |
| 2441 | s->flags |= SN_FINST_R; |
| 2442 | return 0; |
| 2443 | } |
Willy Tarreau | f9839bd | 2008-08-27 23:57:16 +0200 | [diff] [blame] | 2444 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2445 | /* 3: has the read timeout expired ? */ |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 2446 | else if (req->flags & CF_READ_TIMEOUT || tick_is_expired(req->analyse_exp, now_ms)) { |
Willy Tarreau | d3c343f | 2010-01-16 10:26:19 +0100 | [diff] [blame] | 2447 | if (!(s->flags & SN_ERR_MASK)) |
| 2448 | s->flags |= SN_ERR_CLITO; |
| 2449 | |
Willy Tarreau | fcffa69 | 2010-01-10 14:21:19 +0100 | [diff] [blame] | 2450 | if (txn->flags & TX_WAIT_NEXT_RQ) |
Willy Tarreau | b608feb | 2010-01-02 22:47:18 +0100 | [diff] [blame] | 2451 | goto failed_keep_alive; |
| 2452 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2453 | /* read timeout : give up with an error message. */ |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 2454 | if (msg->err_pos >= 0) { |
Willy Tarreau | 8a0cef2 | 2012-03-09 13:39:23 +0100 | [diff] [blame] | 2455 | http_capture_bad_message(&s->fe->invalid_req, s, msg, msg->msg_state, s->fe); |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 2456 | session_inc_http_err_ctr(s); |
| 2457 | } |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2458 | txn->status = 408; |
Willy Tarreau | 783f258 | 2012-09-04 12:19:04 +0200 | [diff] [blame] | 2459 | stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_408)); |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2460 | msg->msg_state = HTTP_MSG_ERROR; |
| 2461 | req->analysers = 0; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 2462 | |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 2463 | session_inc_http_req_ctr(s); |
Willy Tarreau | 3e1b6d1 | 2010-03-04 23:02:38 +0100 | [diff] [blame] | 2464 | proxy_inc_fe_req_ctr(s->fe); |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 2465 | s->fe->fe_counters.failed_req++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 2466 | if (s->listener->counters) |
| 2467 | s->listener->counters->failed_req++; |
| 2468 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2469 | if (!(s->flags & SN_FINST_MASK)) |
| 2470 | s->flags |= SN_FINST_R; |
| 2471 | return 0; |
| 2472 | } |
Willy Tarreau | c65a3ba | 2008-08-11 23:42:50 +0200 | [diff] [blame] | 2473 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2474 | /* 4: have we encountered a close ? */ |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 2475 | else if (req->flags & CF_SHUTR) { |
Willy Tarreau | d3c343f | 2010-01-16 10:26:19 +0100 | [diff] [blame] | 2476 | if (!(s->flags & SN_ERR_MASK)) |
| 2477 | s->flags |= SN_ERR_CLICL; |
| 2478 | |
Willy Tarreau | fcffa69 | 2010-01-10 14:21:19 +0100 | [diff] [blame] | 2479 | if (txn->flags & TX_WAIT_NEXT_RQ) |
Willy Tarreau | b608feb | 2010-01-02 22:47:18 +0100 | [diff] [blame] | 2480 | goto failed_keep_alive; |
| 2481 | |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 2482 | if (msg->err_pos >= 0) |
Willy Tarreau | 8a0cef2 | 2012-03-09 13:39:23 +0100 | [diff] [blame] | 2483 | http_capture_bad_message(&s->fe->invalid_req, s, msg, msg->msg_state, s->fe); |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2484 | txn->status = 400; |
Willy Tarreau | 783f258 | 2012-09-04 12:19:04 +0200 | [diff] [blame] | 2485 | stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_400)); |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2486 | msg->msg_state = HTTP_MSG_ERROR; |
| 2487 | req->analysers = 0; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 2488 | |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 2489 | session_inc_http_err_ctr(s); |
| 2490 | session_inc_http_req_ctr(s); |
Willy Tarreau | 3e1b6d1 | 2010-03-04 23:02:38 +0100 | [diff] [blame] | 2491 | proxy_inc_fe_req_ctr(s->fe); |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 2492 | s->fe->fe_counters.failed_req++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 2493 | if (s->listener->counters) |
| 2494 | s->listener->counters->failed_req++; |
| 2495 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2496 | if (!(s->flags & SN_FINST_MASK)) |
| 2497 | s->flags |= SN_FINST_R; |
Willy Tarreau | dafde43 | 2008-08-17 01:00:46 +0200 | [diff] [blame] | 2498 | return 0; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2499 | } |
| 2500 | |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 2501 | channel_dont_connect(req); |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 2502 | req->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */ |
| 2503 | s->rep->flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */ |
Willy Tarreau | 5e20552 | 2011-12-17 16:34:27 +0100 | [diff] [blame] | 2504 | #ifdef TCP_QUICKACK |
Willy Tarreau | f79c817 | 2013-10-21 16:30:56 +0200 | [diff] [blame] | 2505 | if (s->listener->options & LI_O_NOQUICKACK && req->buf->i && objt_conn(s->req->prod->end) && (__objt_conn(s->req->prod->end)->flags & CO_FL_CTRL_READY)) { |
Willy Tarreau | 5e20552 | 2011-12-17 16:34:27 +0100 | [diff] [blame] | 2506 | /* We need more data, we have to re-enable quick-ack in case we |
| 2507 | * previously disabled it, otherwise we might cause the client |
| 2508 | * to delay next data. |
| 2509 | */ |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 2510 | setsockopt(__objt_conn(s->req->prod->end)->t.sock.fd, IPPROTO_TCP, TCP_QUICKACK, &one, sizeof(one)); |
Willy Tarreau | 5e20552 | 2011-12-17 16:34:27 +0100 | [diff] [blame] | 2511 | } |
| 2512 | #endif |
Willy Tarreau | 1b194fe | 2009-03-21 21:10:04 +0100 | [diff] [blame] | 2513 | |
Willy Tarreau | fcffa69 | 2010-01-10 14:21:19 +0100 | [diff] [blame] | 2514 | if ((msg->msg_state != HTTP_MSG_RQBEFORE) && (txn->flags & TX_WAIT_NEXT_RQ)) { |
| 2515 | /* If the client starts to talk, let's fall back to |
| 2516 | * request timeout processing. |
| 2517 | */ |
| 2518 | txn->flags &= ~TX_WAIT_NEXT_RQ; |
Willy Tarreau | b16a574 | 2010-01-10 14:46:16 +0100 | [diff] [blame] | 2519 | req->analyse_exp = TICK_ETERNITY; |
Willy Tarreau | fcffa69 | 2010-01-10 14:21:19 +0100 | [diff] [blame] | 2520 | } |
| 2521 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2522 | /* just set the request timeout once at the beginning of the request */ |
Willy Tarreau | b16a574 | 2010-01-10 14:46:16 +0100 | [diff] [blame] | 2523 | if (!tick_isset(req->analyse_exp)) { |
| 2524 | if ((msg->msg_state == HTTP_MSG_RQBEFORE) && |
| 2525 | (txn->flags & TX_WAIT_NEXT_RQ) && |
| 2526 | tick_isset(s->be->timeout.httpka)) |
| 2527 | req->analyse_exp = tick_add(now_ms, s->be->timeout.httpka); |
| 2528 | else |
| 2529 | req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.httpreq); |
| 2530 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2531 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2532 | /* we're not ready yet */ |
| 2533 | return 0; |
Willy Tarreau | b608feb | 2010-01-02 22:47:18 +0100 | [diff] [blame] | 2534 | |
| 2535 | failed_keep_alive: |
| 2536 | /* Here we process low-level errors for keep-alive requests. In |
| 2537 | * short, if the request is not the first one and it experiences |
| 2538 | * a timeout, read error or shutdown, we just silently close so |
| 2539 | * that the client can try again. |
| 2540 | */ |
| 2541 | txn->status = 0; |
| 2542 | msg->msg_state = HTTP_MSG_RQBEFORE; |
| 2543 | req->analysers = 0; |
| 2544 | s->logs.logwait = 0; |
Willy Tarreau | abcd514 | 2013-06-11 17:18:02 +0200 | [diff] [blame] | 2545 | s->logs.level = 0; |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 2546 | s->rep->flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */ |
Willy Tarreau | 148d099 | 2010-01-10 10:21:21 +0100 | [diff] [blame] | 2547 | stream_int_retnclose(req->prod, NULL); |
Willy Tarreau | b608feb | 2010-01-02 22:47:18 +0100 | [diff] [blame] | 2548 | return 0; |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2549 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2550 | |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 2551 | /* OK now we have a complete HTTP request with indexed headers. Let's |
| 2552 | * complete the request parsing by setting a few fields we will need |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2553 | * 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] | 2554 | * 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] | 2555 | * 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] | 2556 | * byte after the last LF. msg->sov points to the first byte of data. |
| 2557 | * msg->eol cannot be trusted because it may have been left uninitialized |
| 2558 | * (for instance in the absence of headers). |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 2559 | */ |
Willy Tarreau | 9cdde23 | 2007-05-02 20:58:19 +0200 | [diff] [blame] | 2560 | |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 2561 | session_inc_http_req_ctr(s); |
Willy Tarreau | d9b587f | 2010-02-26 10:05:55 +0100 | [diff] [blame] | 2562 | proxy_inc_fe_req_ctr(s->fe); /* one more valid request for this FE */ |
| 2563 | |
Willy Tarreau | b16a574 | 2010-01-10 14:46:16 +0100 | [diff] [blame] | 2564 | if (txn->flags & TX_WAIT_NEXT_RQ) { |
| 2565 | /* kill the pending keep-alive timeout */ |
| 2566 | txn->flags &= ~TX_WAIT_NEXT_RQ; |
| 2567 | req->analyse_exp = TICK_ETERNITY; |
| 2568 | } |
| 2569 | |
| 2570 | |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 2571 | /* Maybe we found in invalid header name while we were configured not |
| 2572 | * to block on that, so we have to capture it now. |
| 2573 | */ |
| 2574 | if (unlikely(msg->err_pos >= 0)) |
Willy Tarreau | 8a0cef2 | 2012-03-09 13:39:23 +0100 | [diff] [blame] | 2575 | http_capture_bad_message(&s->fe->invalid_req, s, msg, msg->msg_state, s->fe); |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 2576 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2577 | /* |
| 2578 | * 1: identify the method |
| 2579 | */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2580 | 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] | 2581 | |
| 2582 | /* we can make use of server redirect on GET and HEAD */ |
| 2583 | if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD) |
| 2584 | s->flags |= SN_REDIRECTABLE; |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 2585 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2586 | /* |
| 2587 | * 2: check if the URI matches the monitor_uri. |
| 2588 | * We have to do this for every request which gets in, because |
| 2589 | * the monitor-uri is defined by the frontend. |
| 2590 | */ |
| 2591 | if (unlikely((s->fe->monitor_uri_len != 0) && |
| 2592 | (s->fe->monitor_uri_len == msg->sl.rq.u_l) && |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2593 | !memcmp(req->buf->p + msg->sl.rq.u, |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2594 | s->fe->monitor_uri, |
| 2595 | s->fe->monitor_uri_len))) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2596 | /* |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2597 | * We have found the monitor URI |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2598 | */ |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2599 | struct acl_cond *cond; |
Willy Tarreau | b80c230 | 2007-11-30 20:51:32 +0100 | [diff] [blame] | 2600 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2601 | s->flags |= SN_MONITOR; |
Willy Tarreau | eabea07 | 2011-09-10 23:29:44 +0200 | [diff] [blame] | 2602 | s->fe->fe_counters.intercepted_req++; |
Willy Tarreau | b80c230 | 2007-11-30 20:51:32 +0100 | [diff] [blame] | 2603 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2604 | /* Check if we want to fail this monitor request or not */ |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 2605 | list_for_each_entry(cond, &s->fe->mon_fail_cond, list) { |
Willy Tarreau | 32a6f2e | 2012-04-25 10:13:36 +0200 | [diff] [blame] | 2606 | int ret = acl_exec_cond(cond, s->fe, s, txn, SMP_OPT_DIR_REQ|SMP_OPT_FINAL); |
Willy Tarreau | 1138281 | 2008-07-09 16:18:21 +0200 | [diff] [blame] | 2607 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2608 | ret = acl_pass(ret); |
| 2609 | if (cond->pol == ACL_COND_UNLESS) |
| 2610 | ret = !ret; |
Willy Tarreau | b80c230 | 2007-11-30 20:51:32 +0100 | [diff] [blame] | 2611 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2612 | if (ret) { |
| 2613 | /* we fail this request, let's return 503 service unavail */ |
| 2614 | txn->status = 503; |
Willy Tarreau | 783f258 | 2012-09-04 12:19:04 +0200 | [diff] [blame] | 2615 | stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_503)); |
Willy Tarreau | 570f221 | 2013-06-10 16:42:09 +0200 | [diff] [blame] | 2616 | if (!(s->flags & SN_ERR_MASK)) |
| 2617 | s->flags |= SN_ERR_LOCAL; /* we don't want a real error here */ |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2618 | goto return_prx_cond; |
Willy Tarreau | b80c230 | 2007-11-30 20:51:32 +0100 | [diff] [blame] | 2619 | } |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2620 | } |
Willy Tarreau | a5555ec | 2008-11-30 19:02:32 +0100 | [diff] [blame] | 2621 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2622 | /* nothing to fail, let's reply normaly */ |
| 2623 | txn->status = 200; |
Willy Tarreau | 783f258 | 2012-09-04 12:19:04 +0200 | [diff] [blame] | 2624 | stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_200)); |
Willy Tarreau | 570f221 | 2013-06-10 16:42:09 +0200 | [diff] [blame] | 2625 | if (!(s->flags & SN_ERR_MASK)) |
| 2626 | s->flags |= SN_ERR_LOCAL; /* we don't want a real error here */ |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2627 | goto return_prx_cond; |
| 2628 | } |
| 2629 | |
| 2630 | /* |
| 2631 | * 3: Maybe we have to copy the original REQURI for the logs ? |
| 2632 | * Note: we cannot log anymore if the request has been |
| 2633 | * classified as invalid. |
| 2634 | */ |
| 2635 | if (unlikely(s->logs.logwait & LW_REQ)) { |
| 2636 | /* we have a complete HTTP request that we must log */ |
| 2637 | if ((txn->uri = pool_alloc2(pool2_requri)) != NULL) { |
| 2638 | int urilen = msg->sl.rq.l; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2639 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2640 | if (urilen >= REQURI_LEN) |
| 2641 | urilen = REQURI_LEN - 1; |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2642 | memcpy(txn->uri, req->buf->p, urilen); |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2643 | txn->uri[urilen] = 0; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2644 | |
Willy Tarreau | d79a3b2 | 2012-12-28 09:40:16 +0100 | [diff] [blame] | 2645 | if (!(s->logs.logwait &= ~(LW_REQ|LW_INIT))) |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2646 | s->do_log(s); |
| 2647 | } else { |
| 2648 | Alert("HTTP logging : out of memory.\n"); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2649 | } |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2650 | } |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 2651 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2652 | /* 4. We may have to convert HTTP/0.9 requests to HTTP/1.0 */ |
Willy Tarreau | 418bfcc | 2012-03-09 13:56:20 +0100 | [diff] [blame] | 2653 | if (unlikely(msg->sl.rq.v_l == 0) && !http_upgrade_v09_to_v10(txn)) |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 2654 | goto return_bad_req; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2655 | |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 2656 | /* ... and check if the request is HTTP/1.1 or above */ |
| 2657 | if ((msg->sl.rq.v_l == 8) && |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2658 | ((req->buf->p[msg->sl.rq.v + 5] > '1') || |
| 2659 | ((req->buf->p[msg->sl.rq.v + 5] == '1') && |
| 2660 | (req->buf->p[msg->sl.rq.v + 7] >= '1')))) |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 2661 | msg->flags |= HTTP_MSGF_VER_11; |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 2662 | |
| 2663 | /* "connection" has not been parsed yet */ |
Willy Tarreau | 50fc777 | 2012-11-11 22:19:57 +0100 | [diff] [blame] | 2664 | 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] | 2665 | |
Willy Tarreau | 88d349d | 2010-01-25 12:15:43 +0100 | [diff] [blame] | 2666 | /* if the frontend has "option http-use-proxy-header", we'll check if |
| 2667 | * we have what looks like a proxied connection instead of a connection, |
| 2668 | * and in this case set the TX_USE_PX_CONN flag to use Proxy-connection. |
| 2669 | * Note that this is *not* RFC-compliant, however browsers and proxies |
| 2670 | * happen to do that despite being non-standard :-( |
| 2671 | * We consider that a request not beginning with either '/' or '*' is |
| 2672 | * a proxied connection, which covers both "scheme://location" and |
| 2673 | * CONNECT ip:port. |
| 2674 | */ |
| 2675 | if ((s->fe->options2 & PR_O2_USE_PXHDR) && |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2676 | 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] | 2677 | txn->flags |= TX_USE_PX_CONN; |
| 2678 | |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 2679 | /* transfer length unknown*/ |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 2680 | msg->flags &= ~HTTP_MSGF_XFER_LEN; |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 2681 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2682 | /* 5: we may need to capture headers */ |
Willy Tarreau | 42f7d89 | 2012-03-24 08:28:09 +0100 | [diff] [blame] | 2683 | if (unlikely((s->logs.logwait & LW_REQHDR) && txn->req.cap)) |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2684 | capture_headers(req->buf->p, &txn->hdr_idx, |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2685 | txn->req.cap, s->fe->req_cap); |
Willy Tarreau | 1138281 | 2008-07-09 16:18:21 +0200 | [diff] [blame] | 2686 | |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 2687 | /* 6: determine the transfer-length. |
| 2688 | * According to RFC2616 #4.4, amended by the HTTPbis working group, |
| 2689 | * the presence of a message-body in a REQUEST and its transfer length |
| 2690 | * must be determined that way (in order of precedence) : |
| 2691 | * 1. The presence of a message-body in a request is signaled by the |
| 2692 | * inclusion of a Content-Length or Transfer-Encoding header field |
| 2693 | * in the request's header fields. When a request message contains |
| 2694 | * both a message-body of non-zero length and a method that does |
| 2695 | * not define any semantics for that request message-body, then an |
| 2696 | * origin server SHOULD either ignore the message-body or respond |
| 2697 | * with an appropriate error message (e.g., 413). A proxy or |
| 2698 | * gateway, when presented the same request, SHOULD either forward |
| 2699 | * the request inbound with the message- body or ignore the |
| 2700 | * message-body when determining a response. |
| 2701 | * |
| 2702 | * 2. If a Transfer-Encoding header field (Section 9.7) is present |
| 2703 | * and the "chunked" transfer-coding (Section 6.2) is used, the |
| 2704 | * transfer-length is defined by the use of this transfer-coding. |
| 2705 | * If a Transfer-Encoding header field is present and the "chunked" |
| 2706 | * transfer-coding is not present, the transfer-length is defined |
| 2707 | * by the sender closing the connection. |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 2708 | * |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 2709 | * 3. If a Content-Length header field is present, its decimal value in |
| 2710 | * OCTETs represents both the entity-length and the transfer-length. |
| 2711 | * If a message is received with both a Transfer-Encoding header |
| 2712 | * field and a Content-Length header field, the latter MUST be ignored. |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 2713 | * |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 2714 | * 4. By the server closing the connection. (Closing the connection |
| 2715 | * cannot be used to indicate the end of a request body, since that |
| 2716 | * would leave no possibility for the server to send back a response.) |
| 2717 | * |
| 2718 | * Whenever a transfer-coding is applied to a message-body, the set of |
| 2719 | * transfer-codings MUST include "chunked", unless the message indicates |
| 2720 | * it is terminated by closing the connection. When the "chunked" |
| 2721 | * transfer-coding is used, it MUST be the last transfer-coding applied |
| 2722 | * to the message-body. |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 2723 | */ |
| 2724 | |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 2725 | use_close_only = 0; |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 2726 | ctx.idx = 0; |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 2727 | /* set TE_CHNK and XFER_LEN only if "chunked" is seen last */ |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 2728 | while ((msg->flags & HTTP_MSGF_VER_11) && |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2729 | 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] | 2730 | if (ctx.vlen == 7 && strncasecmp(ctx.line + ctx.val, "chunked", 7) == 0) |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 2731 | msg->flags |= (HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN); |
| 2732 | else if (msg->flags & HTTP_MSGF_TE_CHNK) { |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 2733 | /* bad transfer-encoding (chunked followed by something else) */ |
| 2734 | use_close_only = 1; |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 2735 | msg->flags &= ~(HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN); |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 2736 | break; |
| 2737 | } |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 2738 | } |
| 2739 | |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 2740 | ctx.idx = 0; |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 2741 | while (!(msg->flags & HTTP_MSGF_TE_CHNK) && !use_close_only && |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2742 | 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] | 2743 | signed long long cl; |
| 2744 | |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 2745 | if (!ctx.vlen) { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2746 | msg->err_pos = ctx.line + ctx.val - req->buf->p; |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 2747 | goto return_bad_req; |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 2748 | } |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 2749 | |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 2750 | if (strl2llrc(ctx.line + ctx.val, ctx.vlen, &cl)) { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2751 | msg->err_pos = ctx.line + ctx.val - req->buf->p; |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 2752 | goto return_bad_req; /* parse failure */ |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 2753 | } |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 2754 | |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 2755 | if (cl < 0) { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2756 | msg->err_pos = ctx.line + ctx.val - req->buf->p; |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 2757 | goto return_bad_req; |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 2758 | } |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 2759 | |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 2760 | if ((msg->flags & HTTP_MSGF_CNT_LEN) && (msg->chunk_len != cl)) { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2761 | msg->err_pos = ctx.line + ctx.val - req->buf->p; |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 2762 | goto return_bad_req; /* already specified, was different */ |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 2763 | } |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 2764 | |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 2765 | msg->flags |= HTTP_MSGF_CNT_LEN | HTTP_MSGF_XFER_LEN; |
Willy Tarreau | 124d991 | 2011-03-01 20:30:48 +0100 | [diff] [blame] | 2766 | msg->body_len = msg->chunk_len = cl; |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 2767 | } |
| 2768 | |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 2769 | /* bodyless requests have a known length */ |
| 2770 | if (!use_close_only) |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 2771 | msg->flags |= HTTP_MSGF_XFER_LEN; |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 2772 | |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 2773 | /* end of job, return OK */ |
Willy Tarreau | 3a81629 | 2009-07-07 10:55:49 +0200 | [diff] [blame] | 2774 | req->analysers &= ~an_bit; |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 2775 | req->analyse_exp = TICK_ETERNITY; |
| 2776 | return 1; |
| 2777 | |
| 2778 | return_bad_req: |
| 2779 | /* We centralize bad requests processing here */ |
| 2780 | if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) { |
| 2781 | /* we detected a parsing error. We want to archive this request |
| 2782 | * in the dedicated proxy area for later troubleshooting. |
| 2783 | */ |
Willy Tarreau | 8a0cef2 | 2012-03-09 13:39:23 +0100 | [diff] [blame] | 2784 | http_capture_bad_message(&s->fe->invalid_req, s, msg, msg->msg_state, s->fe); |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 2785 | } |
| 2786 | |
| 2787 | txn->req.msg_state = HTTP_MSG_ERROR; |
| 2788 | txn->status = 400; |
Willy Tarreau | 783f258 | 2012-09-04 12:19:04 +0200 | [diff] [blame] | 2789 | stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_400)); |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 2790 | |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 2791 | s->fe->fe_counters.failed_req++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 2792 | if (s->listener->counters) |
| 2793 | s->listener->counters->failed_req++; |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 2794 | |
| 2795 | return_prx_cond: |
| 2796 | if (!(s->flags & SN_ERR_MASK)) |
| 2797 | s->flags |= SN_ERR_PRXCOND; |
| 2798 | if (!(s->flags & SN_FINST_MASK)) |
| 2799 | s->flags |= SN_FINST_R; |
| 2800 | |
| 2801 | req->analysers = 0; |
| 2802 | req->analyse_exp = TICK_ETERNITY; |
| 2803 | return 0; |
| 2804 | } |
| 2805 | |
Willy Tarreau | 4f8a83c | 2012-06-04 00:26:23 +0200 | [diff] [blame] | 2806 | |
Willy Tarreau | 347a35d | 2013-11-22 17:51:09 +0100 | [diff] [blame] | 2807 | /* This function prepares an applet to handle the stats. It can deal with the |
| 2808 | * "100-continue" expectation, check that admin rules are met for POST requests, |
| 2809 | * and program a response message if something was unexpected. It cannot fail |
| 2810 | * 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] | 2811 | * 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] | 2812 | * s->target which is supposed to already point to the stats applet. The caller |
| 2813 | * is expected to have already assigned an appctx to the session. |
Willy Tarreau | 1facd6d | 2012-12-22 22:03:39 +0100 | [diff] [blame] | 2814 | */ |
| 2815 | int http_handle_stats(struct session *s, struct channel *req) |
| 2816 | { |
| 2817 | struct stats_admin_rule *stats_admin_rule; |
| 2818 | struct stream_interface *si = s->rep->prod; |
| 2819 | struct http_txn *txn = &s->txn; |
| 2820 | struct http_msg *msg = &txn->req; |
Willy Tarreau | 414e9bb | 2013-11-23 00:30:38 +0100 | [diff] [blame] | 2821 | struct uri_auth *uri_auth = s->be->uri_auth; |
| 2822 | const char *uri, *h, *lookup; |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 2823 | struct appctx *appctx; |
Willy Tarreau | 414e9bb | 2013-11-23 00:30:38 +0100 | [diff] [blame] | 2824 | |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 2825 | appctx = si_appctx(si); |
| 2826 | memset(&appctx->ctx.stats, 0, sizeof(appctx->ctx.stats)); |
| 2827 | appctx->st1 = appctx->st2 = 0; |
| 2828 | appctx->ctx.stats.st_code = STAT_STATUS_INIT; |
| 2829 | appctx->ctx.stats.flags |= STAT_FMT_HTML; /* assume HTML mode by default */ |
Willy Tarreau | 414e9bb | 2013-11-23 00:30:38 +0100 | [diff] [blame] | 2830 | |
| 2831 | uri = msg->chn->buf->p + msg->sl.rq.u; |
| 2832 | lookup = uri + uri_auth->uri_len; |
| 2833 | |
| 2834 | for (h = lookup; h <= uri + msg->sl.rq.u_l - 3; h++) { |
| 2835 | if (memcmp(h, ";up", 3) == 0) { |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 2836 | appctx->ctx.stats.flags |= STAT_HIDE_DOWN; |
Willy Tarreau | 414e9bb | 2013-11-23 00:30:38 +0100 | [diff] [blame] | 2837 | break; |
| 2838 | } |
| 2839 | } |
| 2840 | |
| 2841 | if (uri_auth->refresh) { |
| 2842 | for (h = lookup; h <= uri + msg->sl.rq.u_l - 10; h++) { |
| 2843 | if (memcmp(h, ";norefresh", 10) == 0) { |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 2844 | appctx->ctx.stats.flags |= STAT_NO_REFRESH; |
Willy Tarreau | 414e9bb | 2013-11-23 00:30:38 +0100 | [diff] [blame] | 2845 | break; |
| 2846 | } |
| 2847 | } |
| 2848 | } |
| 2849 | |
| 2850 | for (h = lookup; h <= uri + msg->sl.rq.u_l - 4; h++) { |
| 2851 | if (memcmp(h, ";csv", 4) == 0) { |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 2852 | appctx->ctx.stats.flags &= ~STAT_FMT_HTML; |
Willy Tarreau | 414e9bb | 2013-11-23 00:30:38 +0100 | [diff] [blame] | 2853 | break; |
| 2854 | } |
| 2855 | } |
| 2856 | |
| 2857 | for (h = lookup; h <= uri + msg->sl.rq.u_l - 8; h++) { |
| 2858 | if (memcmp(h, ";st=", 4) == 0) { |
| 2859 | int i; |
| 2860 | h += 4; |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 2861 | appctx->ctx.stats.st_code = STAT_STATUS_UNKN; |
Willy Tarreau | 414e9bb | 2013-11-23 00:30:38 +0100 | [diff] [blame] | 2862 | for (i = STAT_STATUS_INIT + 1; i < STAT_STATUS_SIZE; i++) { |
| 2863 | if (strncmp(stat_status_codes[i], h, 4) == 0) { |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 2864 | appctx->ctx.stats.st_code = i; |
Willy Tarreau | 414e9bb | 2013-11-23 00:30:38 +0100 | [diff] [blame] | 2865 | break; |
| 2866 | } |
| 2867 | } |
| 2868 | break; |
| 2869 | } |
| 2870 | } |
| 2871 | |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 2872 | appctx->ctx.stats.scope_str = 0; |
| 2873 | appctx->ctx.stats.scope_len = 0; |
Willy Tarreau | 414e9bb | 2013-11-23 00:30:38 +0100 | [diff] [blame] | 2874 | for (h = lookup; h <= uri + msg->sl.rq.u_l - 8; h++) { |
| 2875 | if (memcmp(h, STAT_SCOPE_INPUT_NAME "=", strlen(STAT_SCOPE_INPUT_NAME) + 1) == 0) { |
| 2876 | int itx = 0; |
| 2877 | const char *h2; |
| 2878 | char scope_txt[STAT_SCOPE_TXT_MAXLEN + 1]; |
| 2879 | const char *err; |
| 2880 | |
| 2881 | h += strlen(STAT_SCOPE_INPUT_NAME) + 1; |
| 2882 | h2 = h; |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 2883 | appctx->ctx.stats.scope_str = h2 - msg->chn->buf->p; |
Willy Tarreau | 414e9bb | 2013-11-23 00:30:38 +0100 | [diff] [blame] | 2884 | while (*h != ';' && *h != '\0' && *h != '&' && *h != ' ' && *h != '\n') { |
| 2885 | itx++; |
| 2886 | h++; |
| 2887 | } |
| 2888 | |
| 2889 | if (itx > STAT_SCOPE_TXT_MAXLEN) |
| 2890 | itx = STAT_SCOPE_TXT_MAXLEN; |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 2891 | appctx->ctx.stats.scope_len = itx; |
Willy Tarreau | 414e9bb | 2013-11-23 00:30:38 +0100 | [diff] [blame] | 2892 | |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 2893 | /* 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] | 2894 | memcpy(scope_txt, h2, itx); |
| 2895 | scope_txt[itx] = '\0'; |
| 2896 | err = invalid_char(scope_txt); |
| 2897 | if (err) { |
| 2898 | /* bad char in search text => clear scope */ |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 2899 | appctx->ctx.stats.scope_str = 0; |
| 2900 | appctx->ctx.stats.scope_len = 0; |
Willy Tarreau | 414e9bb | 2013-11-23 00:30:38 +0100 | [diff] [blame] | 2901 | } |
| 2902 | break; |
| 2903 | } |
| 2904 | } |
Willy Tarreau | 1facd6d | 2012-12-22 22:03:39 +0100 | [diff] [blame] | 2905 | |
| 2906 | /* now check whether we have some admin rules for this request */ |
Willy Tarreau | 414e9bb | 2013-11-23 00:30:38 +0100 | [diff] [blame] | 2907 | list_for_each_entry(stats_admin_rule, &uri_auth->admin_rules, list) { |
Willy Tarreau | 1facd6d | 2012-12-22 22:03:39 +0100 | [diff] [blame] | 2908 | int ret = 1; |
| 2909 | |
| 2910 | if (stats_admin_rule->cond) { |
| 2911 | ret = acl_exec_cond(stats_admin_rule->cond, s->be, s, &s->txn, SMP_OPT_DIR_REQ|SMP_OPT_FINAL); |
| 2912 | ret = acl_pass(ret); |
| 2913 | if (stats_admin_rule->cond->pol == ACL_COND_UNLESS) |
| 2914 | ret = !ret; |
| 2915 | } |
| 2916 | |
| 2917 | if (ret) { |
| 2918 | /* no rule, or the rule matches */ |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 2919 | appctx->ctx.stats.flags |= STAT_ADMIN; |
Willy Tarreau | 1facd6d | 2012-12-22 22:03:39 +0100 | [diff] [blame] | 2920 | break; |
| 2921 | } |
| 2922 | } |
| 2923 | |
| 2924 | /* Was the status page requested with a POST ? */ |
Willy Tarreau | 347a35d | 2013-11-22 17:51:09 +0100 | [diff] [blame] | 2925 | if (unlikely(txn->meth == HTTP_METH_POST && txn->req.body_len > 0)) { |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 2926 | if (appctx->ctx.stats.flags & STAT_ADMIN) { |
Willy Tarreau | 1facd6d | 2012-12-22 22:03:39 +0100 | [diff] [blame] | 2927 | if (msg->msg_state < HTTP_MSG_100_SENT) { |
| 2928 | /* If we have HTTP/1.1 and Expect: 100-continue, then we must |
| 2929 | * send an HTTP/1.1 100 Continue intermediate response. |
| 2930 | */ |
| 2931 | if (msg->flags & HTTP_MSGF_VER_11) { |
| 2932 | struct hdr_ctx ctx; |
| 2933 | ctx.idx = 0; |
| 2934 | /* Expect is allowed in 1.1, look for it */ |
| 2935 | if (http_find_header2("Expect", 6, req->buf->p, &txn->hdr_idx, &ctx) && |
| 2936 | unlikely(ctx.vlen == 12 && strncasecmp(ctx.line+ctx.val, "100-continue", 12) == 0)) { |
| 2937 | bo_inject(s->rep, http_100_chunk.str, http_100_chunk.len); |
| 2938 | } |
| 2939 | } |
| 2940 | msg->msg_state = HTTP_MSG_100_SENT; |
| 2941 | s->logs.tv_request = now; /* update the request timer to reflect full request */ |
| 2942 | } |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 2943 | appctx->st0 = STAT_HTTP_POST; |
Willy Tarreau | 1facd6d | 2012-12-22 22:03:39 +0100 | [diff] [blame] | 2944 | } |
Willy Tarreau | 347a35d | 2013-11-22 17:51:09 +0100 | [diff] [blame] | 2945 | else { |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 2946 | appctx->ctx.stats.st_code = STAT_STATUS_DENY; |
| 2947 | appctx->st0 = STAT_HTTP_LAST; |
de Lafond Guillaume | 88c278f | 2013-04-15 19:27:10 +0200 | [diff] [blame] | 2948 | } |
Willy Tarreau | 1facd6d | 2012-12-22 22:03:39 +0100 | [diff] [blame] | 2949 | } |
Willy Tarreau | 347a35d | 2013-11-22 17:51:09 +0100 | [diff] [blame] | 2950 | else { |
| 2951 | /* So it was another method (GET/HEAD) */ |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 2952 | appctx->st0 = STAT_HTTP_HEAD; |
Willy Tarreau | 1facd6d | 2012-12-22 22:03:39 +0100 | [diff] [blame] | 2953 | } |
| 2954 | |
Willy Tarreau | 1facd6d | 2012-12-22 22:03:39 +0100 | [diff] [blame] | 2955 | s->task->nice = -32; /* small boost for HTTP statistics */ |
Willy Tarreau | 1facd6d | 2012-12-22 22:03:39 +0100 | [diff] [blame] | 2956 | return 1; |
| 2957 | } |
| 2958 | |
Lukas Tribus | 67db8df | 2013-06-23 17:37:13 +0200 | [diff] [blame] | 2959 | /* Sets the TOS header in IPv4 and the traffic class header in IPv6 packets |
| 2960 | * (as per RFC3260 #4 and BCP37 #4.2 and #5.2). |
| 2961 | */ |
| 2962 | static inline void inet_set_tos(int fd, struct sockaddr_storage from, int tos) |
| 2963 | { |
| 2964 | #ifdef IP_TOS |
| 2965 | if (from.ss_family == AF_INET) |
| 2966 | setsockopt(fd, IPPROTO_IP, IP_TOS, &tos, sizeof(tos)); |
| 2967 | #endif |
| 2968 | #ifdef IPV6_TCLASS |
| 2969 | if (from.ss_family == AF_INET6) { |
| 2970 | if (IN6_IS_ADDR_V4MAPPED(&((struct sockaddr_in6 *)&from)->sin6_addr)) |
| 2971 | /* v4-mapped addresses need IP_TOS */ |
| 2972 | setsockopt(fd, IPPROTO_IP, IP_TOS, &tos, sizeof(tos)); |
| 2973 | else |
| 2974 | setsockopt(fd, IPPROTO_IPV6, IPV6_TCLASS, &tos, sizeof(tos)); |
| 2975 | } |
| 2976 | #endif |
| 2977 | } |
| 2978 | |
Willy Tarreau | 20b0de5 | 2012-12-24 15:45:22 +0100 | [diff] [blame] | 2979 | /* Executes the http-request rules <rules> for session <s>, proxy <px> and |
Willy Tarreau | 96257ec | 2012-12-27 10:46:37 +0100 | [diff] [blame] | 2980 | * transaction <txn>. Returns the first rule that prevents further processing |
| 2981 | * of the request (auth, deny, ...) or NULL if it executed all rules or stopped |
| 2982 | * on an allow. It may set the TX_CLDENY on txn->flags if it encounters a deny |
| 2983 | * rule. |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 2984 | */ |
Willy Tarreau | 20b0de5 | 2012-12-24 15:45:22 +0100 | [diff] [blame] | 2985 | static struct http_req_rule * |
Willy Tarreau | 96257ec | 2012-12-27 10:46:37 +0100 | [diff] [blame] | 2986 | http_req_get_intercept_rule(struct proxy *px, struct list *rules, struct session *s, struct http_txn *txn) |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 2987 | { |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 2988 | struct connection *cli_conn; |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 2989 | struct http_req_rule *rule; |
Willy Tarreau | 20b0de5 | 2012-12-24 15:45:22 +0100 | [diff] [blame] | 2990 | struct hdr_ctx ctx; |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 2991 | |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 2992 | list_for_each_entry(rule, rules, list) { |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 2993 | if (rule->action >= HTTP_REQ_ACT_MAX) |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 2994 | continue; |
| 2995 | |
Willy Tarreau | 96257ec | 2012-12-27 10:46:37 +0100 | [diff] [blame] | 2996 | /* check optional condition */ |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 2997 | if (rule->cond) { |
Willy Tarreau | 96257ec | 2012-12-27 10:46:37 +0100 | [diff] [blame] | 2998 | int ret; |
| 2999 | |
Willy Tarreau | 32a6f2e | 2012-04-25 10:13:36 +0200 | [diff] [blame] | 3000 | ret = acl_exec_cond(rule->cond, px, s, txn, SMP_OPT_DIR_REQ|SMP_OPT_FINAL); |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 3001 | ret = acl_pass(ret); |
| 3002 | |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 3003 | if (rule->cond->pol == ACL_COND_UNLESS) |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 3004 | ret = !ret; |
Willy Tarreau | 96257ec | 2012-12-27 10:46:37 +0100 | [diff] [blame] | 3005 | |
| 3006 | if (!ret) /* condition not matched */ |
| 3007 | continue; |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 3008 | } |
| 3009 | |
Willy Tarreau | 20b0de5 | 2012-12-24 15:45:22 +0100 | [diff] [blame] | 3010 | |
Willy Tarreau | 96257ec | 2012-12-27 10:46:37 +0100 | [diff] [blame] | 3011 | switch (rule->action) { |
| 3012 | case HTTP_REQ_ACT_ALLOW: |
| 3013 | return NULL; /* "allow" rules are OK */ |
| 3014 | |
| 3015 | case HTTP_REQ_ACT_DENY: |
| 3016 | txn->flags |= TX_CLDENY; |
| 3017 | return rule; |
| 3018 | |
Willy Tarreau | ccbcc37 | 2012-12-27 12:37:57 +0100 | [diff] [blame] | 3019 | case HTTP_REQ_ACT_TARPIT: |
| 3020 | txn->flags |= TX_CLTARPIT; |
| 3021 | return rule; |
| 3022 | |
Willy Tarreau | 96257ec | 2012-12-27 10:46:37 +0100 | [diff] [blame] | 3023 | case HTTP_REQ_ACT_AUTH: |
| 3024 | return rule; |
| 3025 | |
Willy Tarreau | 81499eb | 2012-12-27 12:19:02 +0100 | [diff] [blame] | 3026 | case HTTP_REQ_ACT_REDIR: |
| 3027 | return rule; |
| 3028 | |
Willy Tarreau | f4c43c1 | 2013-06-11 17:01:13 +0200 | [diff] [blame] | 3029 | case HTTP_REQ_ACT_SET_NICE: |
| 3030 | s->task->nice = rule->arg.nice; |
| 3031 | break; |
| 3032 | |
Willy Tarreau | 42cf39e | 2013-06-11 18:51:32 +0200 | [diff] [blame] | 3033 | case HTTP_REQ_ACT_SET_TOS: |
Willy Tarreau | f79c817 | 2013-10-21 16:30:56 +0200 | [diff] [blame] | 3034 | if ((cli_conn = objt_conn(s->req->prod->end)) && (cli_conn->flags & CO_FL_CTRL_READY)) |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 3035 | 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] | 3036 | break; |
| 3037 | |
Willy Tarreau | 51347ed | 2013-06-11 19:34:13 +0200 | [diff] [blame] | 3038 | case HTTP_REQ_ACT_SET_MARK: |
| 3039 | #ifdef SO_MARK |
Willy Tarreau | f79c817 | 2013-10-21 16:30:56 +0200 | [diff] [blame] | 3040 | if ((cli_conn = objt_conn(s->req->prod->end)) && (cli_conn->flags & CO_FL_CTRL_READY)) |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 3041 | 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] | 3042 | #endif |
| 3043 | break; |
| 3044 | |
Willy Tarreau | 9a355ec | 2013-06-11 17:45:46 +0200 | [diff] [blame] | 3045 | case HTTP_REQ_ACT_SET_LOGL: |
| 3046 | s->logs.level = rule->arg.loglevel; |
| 3047 | break; |
| 3048 | |
Willy Tarreau | 96257ec | 2012-12-27 10:46:37 +0100 | [diff] [blame] | 3049 | case HTTP_REQ_ACT_SET_HDR: |
| 3050 | ctx.idx = 0; |
| 3051 | /* remove all occurrences of the header */ |
| 3052 | while (http_find_header2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len, |
| 3053 | txn->req.chn->buf->p, &txn->hdr_idx, &ctx)) { |
| 3054 | http_remove_header2(&txn->req, &txn->hdr_idx, &ctx); |
Willy Tarreau | 20b0de5 | 2012-12-24 15:45:22 +0100 | [diff] [blame] | 3055 | } |
Willy Tarreau | 96257ec | 2012-12-27 10:46:37 +0100 | [diff] [blame] | 3056 | /* now fall through to header addition */ |
| 3057 | |
| 3058 | case HTTP_REQ_ACT_ADD_HDR: |
| 3059 | chunk_printf(&trash, "%s: ", rule->arg.hdr_add.name); |
| 3060 | memcpy(trash.str, rule->arg.hdr_add.name, rule->arg.hdr_add.name_len); |
| 3061 | trash.len = rule->arg.hdr_add.name_len; |
| 3062 | trash.str[trash.len++] = ':'; |
| 3063 | trash.str[trash.len++] = ' '; |
| 3064 | trash.len += build_logline(s, trash.str + trash.len, trash.size - trash.len, &rule->arg.hdr_add.fmt); |
| 3065 | http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, trash.len); |
| 3066 | break; |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 3067 | } |
| 3068 | } |
Willy Tarreau | 96257ec | 2012-12-27 10:46:37 +0100 | [diff] [blame] | 3069 | |
| 3070 | /* we reached the end of the rules, nothing to report */ |
Willy Tarreau | 418c1a0 | 2012-12-25 20:52:58 +0100 | [diff] [blame] | 3071 | return NULL; |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 3072 | } |
| 3073 | |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 3074 | |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 3075 | /* Executes the http-response rules <rules> for session <s>, proxy <px> and |
| 3076 | * transaction <txn>. Returns the first rule that prevents further processing |
| 3077 | * of the response (deny, ...) or NULL if it executed all rules or stopped |
| 3078 | * on an allow. It may set the TX_SVDENY on txn->flags if it encounters a deny |
| 3079 | * rule. |
| 3080 | */ |
| 3081 | static struct http_res_rule * |
| 3082 | http_res_get_intercept_rule(struct proxy *px, struct list *rules, struct session *s, struct http_txn *txn) |
| 3083 | { |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 3084 | struct connection *cli_conn; |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 3085 | struct http_res_rule *rule; |
| 3086 | struct hdr_ctx ctx; |
| 3087 | |
| 3088 | list_for_each_entry(rule, rules, list) { |
| 3089 | if (rule->action >= HTTP_RES_ACT_MAX) |
| 3090 | continue; |
| 3091 | |
| 3092 | /* check optional condition */ |
| 3093 | if (rule->cond) { |
| 3094 | int ret; |
| 3095 | |
| 3096 | ret = acl_exec_cond(rule->cond, px, s, txn, SMP_OPT_DIR_RES|SMP_OPT_FINAL); |
| 3097 | ret = acl_pass(ret); |
| 3098 | |
| 3099 | if (rule->cond->pol == ACL_COND_UNLESS) |
| 3100 | ret = !ret; |
| 3101 | |
| 3102 | if (!ret) /* condition not matched */ |
| 3103 | continue; |
| 3104 | } |
| 3105 | |
| 3106 | |
| 3107 | switch (rule->action) { |
| 3108 | case HTTP_RES_ACT_ALLOW: |
| 3109 | return NULL; /* "allow" rules are OK */ |
| 3110 | |
| 3111 | case HTTP_RES_ACT_DENY: |
| 3112 | txn->flags |= TX_SVDENY; |
| 3113 | return rule; |
| 3114 | |
Willy Tarreau | f4c43c1 | 2013-06-11 17:01:13 +0200 | [diff] [blame] | 3115 | case HTTP_RES_ACT_SET_NICE: |
| 3116 | s->task->nice = rule->arg.nice; |
| 3117 | break; |
| 3118 | |
Willy Tarreau | 42cf39e | 2013-06-11 18:51:32 +0200 | [diff] [blame] | 3119 | case HTTP_RES_ACT_SET_TOS: |
Willy Tarreau | f79c817 | 2013-10-21 16:30:56 +0200 | [diff] [blame] | 3120 | if ((cli_conn = objt_conn(s->req->prod->end)) && (cli_conn->flags & CO_FL_CTRL_READY)) |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 3121 | 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] | 3122 | break; |
| 3123 | |
Willy Tarreau | 51347ed | 2013-06-11 19:34:13 +0200 | [diff] [blame] | 3124 | case HTTP_RES_ACT_SET_MARK: |
| 3125 | #ifdef SO_MARK |
Willy Tarreau | f79c817 | 2013-10-21 16:30:56 +0200 | [diff] [blame] | 3126 | if ((cli_conn = objt_conn(s->req->prod->end)) && (cli_conn->flags & CO_FL_CTRL_READY)) |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 3127 | 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] | 3128 | #endif |
| 3129 | break; |
| 3130 | |
Willy Tarreau | 9a355ec | 2013-06-11 17:45:46 +0200 | [diff] [blame] | 3131 | case HTTP_RES_ACT_SET_LOGL: |
| 3132 | s->logs.level = rule->arg.loglevel; |
| 3133 | break; |
| 3134 | |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 3135 | case HTTP_RES_ACT_SET_HDR: |
| 3136 | ctx.idx = 0; |
| 3137 | /* remove all occurrences of the header */ |
| 3138 | while (http_find_header2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len, |
| 3139 | txn->rsp.chn->buf->p, &txn->hdr_idx, &ctx)) { |
| 3140 | http_remove_header2(&txn->rsp, &txn->hdr_idx, &ctx); |
| 3141 | } |
| 3142 | /* now fall through to header addition */ |
| 3143 | |
| 3144 | case HTTP_RES_ACT_ADD_HDR: |
| 3145 | chunk_printf(&trash, "%s: ", rule->arg.hdr_add.name); |
| 3146 | memcpy(trash.str, rule->arg.hdr_add.name, rule->arg.hdr_add.name_len); |
| 3147 | trash.len = rule->arg.hdr_add.name_len; |
| 3148 | trash.str[trash.len++] = ':'; |
| 3149 | trash.str[trash.len++] = ' '; |
| 3150 | trash.len += build_logline(s, trash.str + trash.len, trash.size - trash.len, &rule->arg.hdr_add.fmt); |
| 3151 | http_header_add_tail2(&txn->rsp, &txn->hdr_idx, trash.str, trash.len); |
| 3152 | break; |
| 3153 | } |
| 3154 | } |
| 3155 | |
| 3156 | /* we reached the end of the rules, nothing to report */ |
| 3157 | return NULL; |
| 3158 | } |
| 3159 | |
| 3160 | |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 3161 | /* Perform an HTTP redirect based on the information in <rule>. The function |
| 3162 | * returns non-zero on success, or zero in case of a, irrecoverable error such |
| 3163 | * as too large a request to build a valid response. |
| 3164 | */ |
| 3165 | static int http_apply_redirect_rule(struct redirect_rule *rule, struct session *s, struct http_txn *txn) |
| 3166 | { |
| 3167 | struct http_msg *msg = &txn->req; |
| 3168 | const char *msg_fmt; |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 3169 | const char *location; |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 3170 | |
| 3171 | /* build redirect message */ |
| 3172 | switch(rule->code) { |
Yves Lafon | 3e8d1ae | 2013-03-11 11:06:05 -0400 | [diff] [blame] | 3173 | case 308: |
| 3174 | msg_fmt = HTTP_308; |
| 3175 | break; |
| 3176 | case 307: |
| 3177 | msg_fmt = HTTP_307; |
| 3178 | break; |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 3179 | case 303: |
| 3180 | msg_fmt = HTTP_303; |
| 3181 | break; |
| 3182 | case 301: |
| 3183 | msg_fmt = HTTP_301; |
| 3184 | break; |
| 3185 | case 302: |
| 3186 | default: |
| 3187 | msg_fmt = HTTP_302; |
| 3188 | break; |
| 3189 | } |
| 3190 | |
| 3191 | if (unlikely(!chunk_strcpy(&trash, msg_fmt))) |
| 3192 | return 0; |
| 3193 | |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 3194 | location = trash.str + trash.len; |
| 3195 | |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 3196 | switch(rule->type) { |
| 3197 | case REDIRECT_TYPE_SCHEME: { |
| 3198 | const char *path; |
| 3199 | const char *host; |
| 3200 | struct hdr_ctx ctx; |
| 3201 | int pathlen; |
| 3202 | int hostlen; |
| 3203 | |
| 3204 | host = ""; |
| 3205 | hostlen = 0; |
| 3206 | ctx.idx = 0; |
| 3207 | if (http_find_header2("Host", 4, txn->req.chn->buf->p + txn->req.sol, &txn->hdr_idx, &ctx)) { |
| 3208 | host = ctx.line + ctx.val; |
| 3209 | hostlen = ctx.vlen; |
| 3210 | } |
| 3211 | |
| 3212 | path = http_get_path(txn); |
| 3213 | /* build message using path */ |
| 3214 | if (path) { |
| 3215 | pathlen = txn->req.sl.rq.u_l + (txn->req.chn->buf->p + txn->req.sl.rq.u) - path; |
| 3216 | if (rule->flags & REDIRECT_FLAG_DROP_QS) { |
| 3217 | int qs = 0; |
| 3218 | while (qs < pathlen) { |
| 3219 | if (path[qs] == '?') { |
| 3220 | pathlen = qs; |
| 3221 | break; |
| 3222 | } |
| 3223 | qs++; |
| 3224 | } |
| 3225 | } |
| 3226 | } else { |
| 3227 | path = "/"; |
| 3228 | pathlen = 1; |
| 3229 | } |
| 3230 | |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 3231 | if (rule->rdr_str) { /* this is an old "redirect" rule */ |
| 3232 | /* check if we can add scheme + "://" + host + path */ |
| 3233 | if (trash.len + rule->rdr_len + 3 + hostlen + pathlen > trash.size - 4) |
| 3234 | return 0; |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 3235 | |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 3236 | /* add scheme */ |
| 3237 | memcpy(trash.str + trash.len, rule->rdr_str, rule->rdr_len); |
| 3238 | trash.len += rule->rdr_len; |
| 3239 | } |
| 3240 | else { |
| 3241 | /* add scheme with executing log format */ |
| 3242 | 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] | 3243 | |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 3244 | /* check if we can add scheme + "://" + host + path */ |
| 3245 | if (trash.len + 3 + hostlen + pathlen > trash.size - 4) |
| 3246 | return 0; |
| 3247 | } |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 3248 | /* add "://" */ |
| 3249 | memcpy(trash.str + trash.len, "://", 3); |
| 3250 | trash.len += 3; |
| 3251 | |
| 3252 | /* add host */ |
| 3253 | memcpy(trash.str + trash.len, host, hostlen); |
| 3254 | trash.len += hostlen; |
| 3255 | |
| 3256 | /* add path */ |
| 3257 | memcpy(trash.str + trash.len, path, pathlen); |
| 3258 | trash.len += pathlen; |
| 3259 | |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 3260 | /* 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] | 3261 | if (trash.len && trash.str[trash.len - 1] != '/' && |
| 3262 | (rule->flags & REDIRECT_FLAG_APPEND_SLASH)) { |
| 3263 | if (trash.len > trash.size - 5) |
| 3264 | return 0; |
| 3265 | trash.str[trash.len] = '/'; |
| 3266 | trash.len++; |
| 3267 | } |
| 3268 | |
| 3269 | break; |
| 3270 | } |
| 3271 | case REDIRECT_TYPE_PREFIX: { |
| 3272 | const char *path; |
| 3273 | int pathlen; |
| 3274 | |
| 3275 | path = http_get_path(txn); |
| 3276 | /* build message using path */ |
| 3277 | if (path) { |
| 3278 | pathlen = txn->req.sl.rq.u_l + (txn->req.chn->buf->p + txn->req.sl.rq.u) - path; |
| 3279 | if (rule->flags & REDIRECT_FLAG_DROP_QS) { |
| 3280 | int qs = 0; |
| 3281 | while (qs < pathlen) { |
| 3282 | if (path[qs] == '?') { |
| 3283 | pathlen = qs; |
| 3284 | break; |
| 3285 | } |
| 3286 | qs++; |
| 3287 | } |
| 3288 | } |
| 3289 | } else { |
| 3290 | path = "/"; |
| 3291 | pathlen = 1; |
| 3292 | } |
| 3293 | |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 3294 | if (rule->rdr_str) { /* this is an old "redirect" rule */ |
| 3295 | if (trash.len + rule->rdr_len + pathlen > trash.size - 4) |
| 3296 | return 0; |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 3297 | |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 3298 | /* add prefix. Note that if prefix == "/", we don't want to |
| 3299 | * add anything, otherwise it makes it hard for the user to |
| 3300 | * configure a self-redirection. |
| 3301 | */ |
| 3302 | if (rule->rdr_len != 1 || *rule->rdr_str != '/') { |
| 3303 | memcpy(trash.str + trash.len, rule->rdr_str, rule->rdr_len); |
| 3304 | trash.len += rule->rdr_len; |
| 3305 | } |
| 3306 | } |
| 3307 | else { |
| 3308 | /* add prefix with executing log format */ |
| 3309 | trash.len += build_logline(s, trash.str + trash.len, trash.size - trash.len, &rule->rdr_fmt); |
| 3310 | |
| 3311 | /* Check length */ |
| 3312 | if (trash.len + pathlen > trash.size - 4) |
| 3313 | return 0; |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 3314 | } |
| 3315 | |
| 3316 | /* add path */ |
| 3317 | memcpy(trash.str + trash.len, path, pathlen); |
| 3318 | trash.len += pathlen; |
| 3319 | |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 3320 | /* 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] | 3321 | if (trash.len && trash.str[trash.len - 1] != '/' && |
| 3322 | (rule->flags & REDIRECT_FLAG_APPEND_SLASH)) { |
| 3323 | if (trash.len > trash.size - 5) |
| 3324 | return 0; |
| 3325 | trash.str[trash.len] = '/'; |
| 3326 | trash.len++; |
| 3327 | } |
| 3328 | |
| 3329 | break; |
| 3330 | } |
| 3331 | case REDIRECT_TYPE_LOCATION: |
| 3332 | default: |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 3333 | if (rule->rdr_str) { /* this is an old "redirect" rule */ |
| 3334 | if (trash.len + rule->rdr_len > trash.size - 4) |
| 3335 | return 0; |
| 3336 | |
| 3337 | /* add location */ |
| 3338 | memcpy(trash.str + trash.len, rule->rdr_str, rule->rdr_len); |
| 3339 | trash.len += rule->rdr_len; |
| 3340 | } |
| 3341 | else { |
| 3342 | /* add location with executing log format */ |
| 3343 | 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] | 3344 | |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 3345 | /* Check left length */ |
| 3346 | if (trash.len > trash.size - 4) |
| 3347 | return 0; |
| 3348 | } |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 3349 | break; |
| 3350 | } |
| 3351 | |
| 3352 | if (rule->cookie_len) { |
| 3353 | memcpy(trash.str + trash.len, "\r\nSet-Cookie: ", 14); |
| 3354 | trash.len += 14; |
| 3355 | memcpy(trash.str + trash.len, rule->cookie_str, rule->cookie_len); |
| 3356 | trash.len += rule->cookie_len; |
| 3357 | memcpy(trash.str + trash.len, "\r\n", 2); |
| 3358 | trash.len += 2; |
| 3359 | } |
| 3360 | |
| 3361 | /* add end of headers and the keep-alive/close status. |
| 3362 | * We may choose to set keep-alive if the Location begins |
| 3363 | * with a slash, because the client will come back to the |
| 3364 | * same server. |
| 3365 | */ |
| 3366 | txn->status = rule->code; |
| 3367 | /* let's log the request time */ |
| 3368 | s->logs.tv_request = now; |
| 3369 | |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 3370 | if (*location == '/' && |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 3371 | (msg->flags & HTTP_MSGF_XFER_LEN) && |
| 3372 | !(msg->flags & HTTP_MSGF_TE_CHNK) && !txn->req.body_len && |
| 3373 | ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL || |
| 3374 | (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL)) { |
| 3375 | /* keep-alive possible */ |
| 3376 | if (!(msg->flags & HTTP_MSGF_VER_11)) { |
| 3377 | if (unlikely(txn->flags & TX_USE_PX_CONN)) { |
| 3378 | memcpy(trash.str + trash.len, "\r\nProxy-Connection: keep-alive", 30); |
| 3379 | trash.len += 30; |
| 3380 | } else { |
| 3381 | memcpy(trash.str + trash.len, "\r\nConnection: keep-alive", 24); |
| 3382 | trash.len += 24; |
| 3383 | } |
| 3384 | } |
| 3385 | memcpy(trash.str + trash.len, "\r\n\r\n", 4); |
| 3386 | trash.len += 4; |
| 3387 | bo_inject(txn->rsp.chn, trash.str, trash.len); |
| 3388 | /* "eat" the request */ |
| 3389 | bi_fast_delete(txn->req.chn->buf, msg->sov); |
| 3390 | msg->sov = 0; |
| 3391 | txn->req.chn->analysers = AN_REQ_HTTP_XFER_BODY; |
| 3392 | s->rep->analysers = AN_RES_HTTP_XFER_BODY; |
| 3393 | txn->req.msg_state = HTTP_MSG_CLOSED; |
| 3394 | txn->rsp.msg_state = HTTP_MSG_DONE; |
| 3395 | } else { |
| 3396 | /* keep-alive not possible */ |
| 3397 | if (unlikely(txn->flags & TX_USE_PX_CONN)) { |
| 3398 | memcpy(trash.str + trash.len, "\r\nProxy-Connection: close\r\n\r\n", 29); |
| 3399 | trash.len += 29; |
| 3400 | } else { |
| 3401 | memcpy(trash.str + trash.len, "\r\nConnection: close\r\n\r\n", 23); |
| 3402 | trash.len += 23; |
| 3403 | } |
| 3404 | stream_int_retnclose(txn->req.chn->prod, &trash); |
| 3405 | txn->req.chn->analysers = 0; |
| 3406 | } |
| 3407 | |
| 3408 | if (!(s->flags & SN_ERR_MASK)) |
Willy Tarreau | 570f221 | 2013-06-10 16:42:09 +0200 | [diff] [blame] | 3409 | s->flags |= SN_ERR_LOCAL; |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 3410 | if (!(s->flags & SN_FINST_MASK)) |
| 3411 | s->flags |= SN_FINST_R; |
| 3412 | |
| 3413 | return 1; |
| 3414 | } |
| 3415 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3416 | /* This stream analyser runs all HTTP request processing which is common to |
| 3417 | * frontends and backends, which means blocking ACLs, filters, connection-close, |
| 3418 | * reqadd, stats and redirects. This is performed for the designated proxy. |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 3419 | * 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] | 3420 | * either needs more data or wants to immediately abort the request (eg: deny, |
| 3421 | * error, ...). |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 3422 | */ |
Willy Tarreau | 7421efb | 2012-07-02 15:11:27 +0200 | [diff] [blame] | 3423 | int http_process_req_common(struct session *s, struct channel *req, int an_bit, struct proxy *px) |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 3424 | { |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 3425 | struct http_txn *txn = &s->txn; |
| 3426 | struct http_msg *msg = &txn->req; |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3427 | struct acl_cond *cond; |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 3428 | struct http_req_rule *http_req_last_rule = NULL; |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3429 | struct redirect_rule *rule; |
Willy Tarreau | f4f0412 | 2010-01-28 18:10:50 +0100 | [diff] [blame] | 3430 | struct cond_wordlist *wl; |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 3431 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 3432 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) { |
Willy Tarreau | 51aecc7 | 2009-07-12 09:47:04 +0200 | [diff] [blame] | 3433 | /* we need more data */ |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 3434 | channel_dont_connect(req); |
Willy Tarreau | 51aecc7 | 2009-07-12 09:47:04 +0200 | [diff] [blame] | 3435 | return 0; |
| 3436 | } |
| 3437 | |
Willy Tarreau | 3a81629 | 2009-07-07 10:55:49 +0200 | [diff] [blame] | 3438 | req->analysers &= ~an_bit; |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 3439 | req->analyse_exp = TICK_ETERNITY; |
| 3440 | |
Willy Tarreau | 02d6cfc | 2012-03-01 18:19:58 +0100 | [diff] [blame] | 3441 | DPRINTF(stderr,"[%u] %s: session=%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] | 3442 | now_ms, __FUNCTION__, |
| 3443 | s, |
| 3444 | req, |
| 3445 | req->rex, req->wex, |
| 3446 | req->flags, |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 3447 | req->buf->i, |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 3448 | req->analysers); |
| 3449 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3450 | /* first check whether we have some ACLs set to block this request */ |
| 3451 | list_for_each_entry(cond, &px->block_cond, list) { |
Willy Tarreau | 32a6f2e | 2012-04-25 10:13:36 +0200 | [diff] [blame] | 3452 | int ret = acl_exec_cond(cond, px, s, txn, SMP_OPT_DIR_REQ|SMP_OPT_FINAL); |
Willy Tarreau | b463dfb | 2008-06-07 23:08:56 +0200 | [diff] [blame] | 3453 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3454 | ret = acl_pass(ret); |
| 3455 | if (cond->pol == ACL_COND_UNLESS) |
| 3456 | ret = !ret; |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 3457 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3458 | if (ret) { |
| 3459 | txn->status = 403; |
| 3460 | /* let's log the request time */ |
| 3461 | s->logs.tv_request = now; |
Willy Tarreau | 783f258 | 2012-09-04 12:19:04 +0200 | [diff] [blame] | 3462 | stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_403)); |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 3463 | session_inc_http_err_ctr(s); |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3464 | goto return_prx_cond; |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3465 | } |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3466 | } |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3467 | |
Willy Tarreau | 5d5b5d8 | 2012-12-09 12:00:04 +0100 | [diff] [blame] | 3468 | /* just in case we have some per-backend tracking */ |
| 3469 | session_inc_be_http_req_ctr(s); |
| 3470 | |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 3471 | /* evaluate http-request rules */ |
Willy Tarreau | 96257ec | 2012-12-27 10:46:37 +0100 | [diff] [blame] | 3472 | http_req_last_rule = http_req_get_intercept_rule(px, &px->http_req_rules, s, txn); |
Willy Tarreau | 5142594 | 2010-02-01 10:40:19 +0100 | [diff] [blame] | 3473 | |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 3474 | /* evaluate stats http-request rules only if http-request is OK */ |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 3475 | if (!http_req_last_rule) { |
Willy Tarreau | 414e9bb | 2013-11-23 00:30:38 +0100 | [diff] [blame] | 3476 | if (stats_check_uri(s->rep->prod, txn, px)) { |
| 3477 | s->target = &http_stats_applet.obj_type; |
Willy Tarreau | 1fbe1c9 | 2013-12-01 09:35:41 +0100 | [diff] [blame] | 3478 | if (unlikely(!stream_int_register_handler(s->rep->prod, objt_applet(s->target)))) { |
| 3479 | txn->status = 500; |
| 3480 | s->logs.tv_request = now; |
| 3481 | stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_500)); |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 3482 | |
Willy Tarreau | 1fbe1c9 | 2013-12-01 09:35:41 +0100 | [diff] [blame] | 3483 | if (!(s->flags & SN_ERR_MASK)) |
| 3484 | s->flags |= SN_ERR_RESOURCE; |
| 3485 | goto return_prx_cond; |
| 3486 | } |
Willy Tarreau | 414e9bb | 2013-11-23 00:30:38 +0100 | [diff] [blame] | 3487 | /* parse the whole stats request and extract the relevant information */ |
| 3488 | http_handle_stats(s, req); |
Willy Tarreau | 96257ec | 2012-12-27 10:46:37 +0100 | [diff] [blame] | 3489 | http_req_last_rule = http_req_get_intercept_rule(px, &px->uri_auth->http_req_rules, s, txn); |
Willy Tarreau | 414e9bb | 2013-11-23 00:30:38 +0100 | [diff] [blame] | 3490 | } |
Krzysztof Piotr Oledzki | 59bb218 | 2010-01-29 17:58:21 +0100 | [diff] [blame] | 3491 | } |
| 3492 | |
Willy Tarreau | 3b44e72 | 2013-11-16 10:28:23 +0100 | [diff] [blame] | 3493 | /* only apply req{,i}{rep/deny/tarpit} if the request was not yet |
| 3494 | * blocked by an http-request rule. |
| 3495 | */ |
| 3496 | if (!(txn->flags & (TX_CLDENY|TX_CLTARPIT)) && (px->req_exp != NULL)) { |
Willy Tarreau | 6c123b1 | 2010-01-28 20:22:06 +0100 | [diff] [blame] | 3497 | if (apply_filters_to_request(s, req, px) < 0) |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3498 | goto return_bad_req; |
Willy Tarreau | 3b44e72 | 2013-11-16 10:28:23 +0100 | [diff] [blame] | 3499 | } |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 3500 | |
Willy Tarreau | 3b44e72 | 2013-11-16 10:28:23 +0100 | [diff] [blame] | 3501 | /* return a 403 if either rule has blocked */ |
| 3502 | if (txn->flags & (TX_CLDENY|TX_CLTARPIT)) { |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3503 | if (txn->flags & TX_CLDENY) { |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3504 | txn->status = 403; |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3505 | s->logs.tv_request = now; |
Willy Tarreau | 783f258 | 2012-09-04 12:19:04 +0200 | [diff] [blame] | 3506 | stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_403)); |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 3507 | session_inc_http_err_ctr(s); |
Willy Tarreau | 687ba13 | 2013-11-16 10:13:35 +0100 | [diff] [blame] | 3508 | s->fe->fe_counters.denied_req++; |
| 3509 | if (s->fe != s->be) |
| 3510 | s->be->be_counters.denied_req++; |
| 3511 | if (s->listener->counters) |
| 3512 | s->listener->counters->denied_req++; |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3513 | goto return_prx_cond; |
| 3514 | } |
Willy Tarreau | c465fd7 | 2009-08-31 00:17:18 +0200 | [diff] [blame] | 3515 | |
| 3516 | /* When a connection is tarpitted, we use the tarpit timeout, |
| 3517 | * which may be the same as the connect timeout if unspecified. |
| 3518 | * If unset, then set it to zero because we really want it to |
| 3519 | * eventually expire. We build the tarpit as an analyser. |
| 3520 | */ |
| 3521 | if (txn->flags & TX_CLTARPIT) { |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 3522 | channel_erase(s->req); |
Willy Tarreau | c465fd7 | 2009-08-31 00:17:18 +0200 | [diff] [blame] | 3523 | /* wipe the request out so that we can drop the connection early |
| 3524 | * if the client closes first. |
| 3525 | */ |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 3526 | channel_dont_connect(req); |
Willy Tarreau | c465fd7 | 2009-08-31 00:17:18 +0200 | [diff] [blame] | 3527 | req->analysers = 0; /* remove switching rules etc... */ |
| 3528 | req->analysers |= AN_REQ_HTTP_TARPIT; |
| 3529 | req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.tarpit); |
| 3530 | if (!req->analyse_exp) |
| 3531 | req->analyse_exp = tick_add(now_ms, 0); |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 3532 | session_inc_http_err_ctr(s); |
Willy Tarreau | 687ba13 | 2013-11-16 10:13:35 +0100 | [diff] [blame] | 3533 | s->fe->fe_counters.denied_req++; |
| 3534 | if (s->fe != s->be) |
| 3535 | s->be->be_counters.denied_req++; |
| 3536 | if (s->listener->counters) |
| 3537 | s->listener->counters->denied_req++; |
Willy Tarreau | c465fd7 | 2009-08-31 00:17:18 +0200 | [diff] [blame] | 3538 | return 1; |
| 3539 | } |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3540 | } |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 3541 | |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 3542 | /* Until set to anything else, the connection mode is set as TUNNEL. It will |
| 3543 | * only change if both the request and the config reference something else. |
Willy Tarreau | 0dfdf19 | 2010-01-05 11:33:11 +0100 | [diff] [blame] | 3544 | * Option httpclose by itself does not set a mode, it remains a tunnel mode |
| 3545 | * in which headers are mangled. However, if another mode is set, it will |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 3546 | * affect it (eg: server-close/keep-alive + httpclose = close). Note that we |
| 3547 | * avoid to redo the same work if FE and BE have the same settings (common). |
| 3548 | * The method consists in checking if options changed between the two calls |
| 3549 | * (implying that either one is non-null, or one of them is non-null and we |
| 3550 | * are there for the first time. |
Willy Tarreau | 4273664 | 2009-10-18 21:04:35 +0200 | [diff] [blame] | 3551 | */ |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 3552 | |
Willy Tarreau | dc008c5 | 2010-02-01 16:20:08 +0100 | [diff] [blame] | 3553 | if ((!(txn->flags & TX_HDR_CONN_PRS) && |
| 3554 | (s->fe->options & (PR_O_KEEPALIVE|PR_O_SERVER_CLO|PR_O_HTTP_CLOSE|PR_O_FORCE_CLO))) || |
| 3555 | ((s->fe->options & (PR_O_KEEPALIVE|PR_O_SERVER_CLO|PR_O_HTTP_CLOSE|PR_O_FORCE_CLO)) != |
| 3556 | (s->be->options & (PR_O_KEEPALIVE|PR_O_SERVER_CLO|PR_O_HTTP_CLOSE|PR_O_FORCE_CLO)))) { |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 3557 | int tmp = TX_CON_WANT_TUN; |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 3558 | |
Cyril Bonté | 9ea2b9a | 2010-12-29 09:36:56 +0100 | [diff] [blame] | 3559 | if ((s->fe->options|s->be->options) & PR_O_KEEPALIVE || |
| 3560 | ((s->fe->options2|s->be->options2) & PR_O2_FAKE_KA)) |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 3561 | tmp = TX_CON_WANT_KAL; |
Willy Tarreau | b608feb | 2010-01-02 22:47:18 +0100 | [diff] [blame] | 3562 | if ((s->fe->options|s->be->options) & PR_O_SERVER_CLO) |
| 3563 | tmp = TX_CON_WANT_SCL; |
Willy Tarreau | 0dfdf19 | 2010-01-05 11:33:11 +0100 | [diff] [blame] | 3564 | if ((s->fe->options|s->be->options) & PR_O_FORCE_CLO) |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 3565 | tmp = TX_CON_WANT_CLO; |
| 3566 | |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 3567 | if ((txn->flags & TX_CON_WANT_MSK) < tmp) |
| 3568 | txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | tmp; |
Willy Tarreau | 0dfdf19 | 2010-01-05 11:33:11 +0100 | [diff] [blame] | 3569 | |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 3570 | if (!(txn->flags & TX_HDR_CONN_PRS)) { |
| 3571 | /* parse the Connection header and possibly clean it */ |
| 3572 | int to_del = 0; |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 3573 | if ((msg->flags & HTTP_MSGF_VER_11) || |
Willy Tarreau | 8a8e1d9 | 2010-04-05 16:15:16 +0200 | [diff] [blame] | 3574 | ((txn->flags & TX_CON_WANT_MSK) >= TX_CON_WANT_SCL && |
| 3575 | !((s->fe->options2|s->be->options2) & PR_O2_FAKE_KA))) |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 3576 | to_del |= 2; /* remove "keep-alive" */ |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 3577 | if (!(msg->flags & HTTP_MSGF_VER_11)) |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 3578 | to_del |= 1; /* remove "close" */ |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 3579 | http_parse_connection_header(txn, msg, to_del); |
Willy Tarreau | 0dfdf19 | 2010-01-05 11:33:11 +0100 | [diff] [blame] | 3580 | } |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 3581 | |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 3582 | /* check if client or config asks for explicit close in KAL/SCL */ |
| 3583 | if (((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL || |
| 3584 | (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) && |
| 3585 | ((txn->flags & TX_HDR_CONN_CLO) || /* "connection: close" */ |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 3586 | (!(msg->flags & HTTP_MSGF_VER_11) && !(txn->flags & TX_HDR_CONN_KAL)) || /* no "connection: k-a" in 1.0 */ |
Cyril Bonté | 9ea2b9a | 2010-12-29 09:36:56 +0100 | [diff] [blame] | 3587 | ((s->fe->options|s->be->options) & PR_O_HTTP_CLOSE) || /* httpclose+any = forceclose */ |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 3588 | !(msg->flags & HTTP_MSGF_XFER_LEN) || /* no length known => close */ |
Willy Tarreau | c3e8b25 | 2010-01-28 15:01:20 +0100 | [diff] [blame] | 3589 | s->fe->state == PR_STSTOPPED)) /* frontend is stopping */ |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 3590 | txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO; |
| 3591 | } |
Willy Tarreau | 7859991 | 2009-10-17 20:12:21 +0200 | [diff] [blame] | 3592 | |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 3593 | /* we can be blocked here because the request needs to be authenticated, |
| 3594 | * either to pass or to access stats. |
| 3595 | */ |
Willy Tarreau | 20b0de5 | 2012-12-24 15:45:22 +0100 | [diff] [blame] | 3596 | if (http_req_last_rule && http_req_last_rule->action == HTTP_REQ_ACT_AUTH) { |
Willy Tarreau | 5c2e198 | 2012-12-24 12:00:25 +0100 | [diff] [blame] | 3597 | char *realm = http_req_last_rule->arg.auth.realm; |
Krzysztof Piotr Oledzki | 59bb218 | 2010-01-29 17:58:21 +0100 | [diff] [blame] | 3598 | |
Krzysztof Piotr Oledzki | 8c8bd45 | 2010-01-29 19:29:32 +0100 | [diff] [blame] | 3599 | if (!realm) |
Willy Tarreau | 414e9bb | 2013-11-23 00:30:38 +0100 | [diff] [blame] | 3600 | realm = (objt_applet(s->target) == &http_stats_applet) ? STATS_DEFAULT_REALM : px->id; |
Krzysztof Piotr Oledzki | 8c8bd45 | 2010-01-29 19:29:32 +0100 | [diff] [blame] | 3601 | |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 3602 | chunk_printf(&trash, (txn->flags & TX_USE_PX_CONN) ? HTTP_407_fmt : HTTP_401_fmt, realm); |
Krzysztof Piotr Oledzki | 59bb218 | 2010-01-29 17:58:21 +0100 | [diff] [blame] | 3603 | txn->status = 401; |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 3604 | stream_int_retnclose(req->prod, &trash); |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 3605 | /* on 401 we still count one error, because normal browsing |
| 3606 | * won't significantly increase the counter but brute force |
| 3607 | * attempts will. |
| 3608 | */ |
| 3609 | session_inc_http_err_ctr(s); |
Krzysztof Piotr Oledzki | 59bb218 | 2010-01-29 17:58:21 +0100 | [diff] [blame] | 3610 | goto return_prx_cond; |
| 3611 | } |
| 3612 | |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 3613 | /* add request headers from the rule sets in the same order */ |
| 3614 | list_for_each_entry(wl, &px->req_add, list) { |
| 3615 | if (wl->cond) { |
Willy Tarreau | 32a6f2e | 2012-04-25 10:13:36 +0200 | [diff] [blame] | 3616 | int ret = acl_exec_cond(wl->cond, px, s, txn, SMP_OPT_DIR_REQ|SMP_OPT_FINAL); |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 3617 | ret = acl_pass(ret); |
| 3618 | if (((struct acl_cond *)wl->cond)->pol == ACL_COND_UNLESS) |
| 3619 | ret = !ret; |
| 3620 | if (!ret) |
| 3621 | continue; |
| 3622 | } |
| 3623 | |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 3624 | 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] | 3625 | goto return_bad_req; |
Willy Tarreau | 81499eb | 2012-12-27 12:19:02 +0100 | [diff] [blame] | 3626 | } |
| 3627 | |
| 3628 | if (http_req_last_rule && http_req_last_rule->action == HTTP_REQ_ACT_REDIR) { |
| 3629 | if (!http_apply_redirect_rule(http_req_last_rule->arg.redir, s, txn)) |
| 3630 | goto return_bad_req; |
| 3631 | req->analyse_exp = TICK_ETERNITY; |
| 3632 | return 1; |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 3633 | } |
| 3634 | |
Willy Tarreau | 414e9bb | 2013-11-23 00:30:38 +0100 | [diff] [blame] | 3635 | if (unlikely(objt_applet(s->target) == &http_stats_applet)) { |
Willy Tarreau | 1facd6d | 2012-12-22 22:03:39 +0100 | [diff] [blame] | 3636 | /* process the stats request now */ |
Willy Tarreau | 347a35d | 2013-11-22 17:51:09 +0100 | [diff] [blame] | 3637 | if (s->fe == s->be) /* report it if the request was intercepted by the frontend */ |
| 3638 | s->fe->fe_counters.intercepted_req++; |
| 3639 | |
| 3640 | if (!(s->flags & SN_ERR_MASK)) // this is not really an error but it is |
| 3641 | s->flags |= SN_ERR_LOCAL; // to mark that it comes from the proxy |
| 3642 | if (!(s->flags & SN_FINST_MASK)) |
| 3643 | s->flags |= SN_FINST_R; |
| 3644 | |
| 3645 | req->analyse_exp = TICK_ETERNITY; |
Willy Tarreau | 51437d2 | 2013-12-29 00:43:40 +0100 | [diff] [blame] | 3646 | req->analysers = 0; |
Willy Tarreau | 1facd6d | 2012-12-22 22:03:39 +0100 | [diff] [blame] | 3647 | return 1; |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3648 | } |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 3649 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3650 | /* check whether we have some ACLs set to redirect this request */ |
| 3651 | list_for_each_entry(rule, &px->redirect_rules, list) { |
Willy Tarreau | f285f54 | 2010-01-03 20:03:03 +0100 | [diff] [blame] | 3652 | if (rule->cond) { |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 3653 | int ret; |
| 3654 | |
Willy Tarreau | 32a6f2e | 2012-04-25 10:13:36 +0200 | [diff] [blame] | 3655 | ret = acl_exec_cond(rule->cond, px, s, txn, SMP_OPT_DIR_REQ|SMP_OPT_FINAL); |
Willy Tarreau | f285f54 | 2010-01-03 20:03:03 +0100 | [diff] [blame] | 3656 | ret = acl_pass(ret); |
| 3657 | if (rule->cond->pol == ACL_COND_UNLESS) |
| 3658 | ret = !ret; |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 3659 | if (!ret) |
| 3660 | continue; |
Willy Tarreau | f285f54 | 2010-01-03 20:03:03 +0100 | [diff] [blame] | 3661 | } |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 3662 | if (!http_apply_redirect_rule(rule, s, txn)) |
| 3663 | goto return_bad_req; |
Willy Tarreau | a9679ac | 2010-01-03 17:32:57 +0100 | [diff] [blame] | 3664 | |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 3665 | req->analyse_exp = TICK_ETERNITY; |
| 3666 | return 1; |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3667 | } |
Willy Tarreau | 55ea757 | 2007-06-17 19:56:27 +0200 | [diff] [blame] | 3668 | |
Willy Tarreau | 2be3939 | 2010-01-03 17:24:51 +0100 | [diff] [blame] | 3669 | /* POST requests may be accompanied with an "Expect: 100-Continue" header. |
| 3670 | * If this happens, then the data will not come immediately, so we must |
| 3671 | * send all what we have without waiting. Note that due to the small gain |
| 3672 | * 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] | 3673 | * 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] | 3674 | * itself once used. |
| 3675 | */ |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 3676 | req->flags |= CF_SEND_DONTWAIT; |
Willy Tarreau | 2be3939 | 2010-01-03 17:24:51 +0100 | [diff] [blame] | 3677 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3678 | /* that's OK for us now, let's move on to next analysers */ |
| 3679 | return 1; |
Willy Tarreau | 1138281 | 2008-07-09 16:18:21 +0200 | [diff] [blame] | 3680 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3681 | return_bad_req: |
| 3682 | /* We centralize bad requests processing here */ |
| 3683 | if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) { |
| 3684 | /* we detected a parsing error. We want to archive this request |
| 3685 | * in the dedicated proxy area for later troubleshooting. |
| 3686 | */ |
Willy Tarreau | 8a0cef2 | 2012-03-09 13:39:23 +0100 | [diff] [blame] | 3687 | http_capture_bad_message(&s->fe->invalid_req, s, msg, msg->msg_state, s->fe); |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3688 | } |
Willy Tarreau | 55ea757 | 2007-06-17 19:56:27 +0200 | [diff] [blame] | 3689 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3690 | txn->req.msg_state = HTTP_MSG_ERROR; |
| 3691 | txn->status = 400; |
Willy Tarreau | 783f258 | 2012-09-04 12:19:04 +0200 | [diff] [blame] | 3692 | stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_400)); |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 3693 | |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 3694 | s->fe->fe_counters.failed_req++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 3695 | if (s->listener->counters) |
| 3696 | s->listener->counters->failed_req++; |
Willy Tarreau | 6e4261e | 2007-09-18 18:36:05 +0200 | [diff] [blame] | 3697 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3698 | return_prx_cond: |
| 3699 | if (!(s->flags & SN_ERR_MASK)) |
| 3700 | s->flags |= SN_ERR_PRXCOND; |
| 3701 | if (!(s->flags & SN_FINST_MASK)) |
| 3702 | s->flags |= SN_FINST_R; |
Willy Tarreau | f1221aa | 2006-12-17 22:14:12 +0100 | [diff] [blame] | 3703 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3704 | req->analysers = 0; |
| 3705 | req->analyse_exp = TICK_ETERNITY; |
| 3706 | return 0; |
| 3707 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3708 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3709 | /* This function performs all the processing enabled for the current request. |
| 3710 | * It returns 1 if the processing can continue on next analysers, or zero if it |
| 3711 | * needs more data, encounters an error, or wants to immediately abort the |
| 3712 | * request. It relies on buffers flags, and updates s->req->analysers. |
| 3713 | */ |
Willy Tarreau | 7421efb | 2012-07-02 15:11:27 +0200 | [diff] [blame] | 3714 | int http_process_request(struct session *s, struct channel *req, int an_bit) |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3715 | { |
| 3716 | struct http_txn *txn = &s->txn; |
| 3717 | struct http_msg *msg = &txn->req; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 3718 | struct connection *cli_conn = objt_conn(req->prod->end); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3719 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 3720 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) { |
Willy Tarreau | 51aecc7 | 2009-07-12 09:47:04 +0200 | [diff] [blame] | 3721 | /* we need more data */ |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 3722 | channel_dont_connect(req); |
Willy Tarreau | 51aecc7 | 2009-07-12 09:47:04 +0200 | [diff] [blame] | 3723 | return 0; |
| 3724 | } |
| 3725 | |
Willy Tarreau | 02d6cfc | 2012-03-01 18:19:58 +0100 | [diff] [blame] | 3726 | DPRINTF(stderr,"[%u] %s: session=%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] | 3727 | now_ms, __FUNCTION__, |
| 3728 | s, |
| 3729 | req, |
| 3730 | req->rex, req->wex, |
| 3731 | req->flags, |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 3732 | req->buf->i, |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3733 | req->analysers); |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 3734 | |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 3735 | if (s->fe->comp || s->be->comp) |
| 3736 | select_compression_request_header(s, req->buf); |
| 3737 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3738 | /* |
| 3739 | * Right now, we know that we have processed the entire headers |
| 3740 | * and that unwanted requests have been filtered out. We can do |
| 3741 | * whatever we want with the remaining request. Also, now we |
| 3742 | * may have separate values for ->fe, ->be. |
| 3743 | */ |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 3744 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3745 | /* |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 3746 | * If HTTP PROXY is set we simply get remote server address parsing |
| 3747 | * incoming request. Note that this requires that a connection is |
| 3748 | * allocated on the server side. |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3749 | */ |
| 3750 | if ((s->be->options & PR_O_HTTP_PROXY) && !(s->flags & SN_ADDR_SET)) { |
Willy Tarreau | 32e3c6a | 2013-10-11 19:34:20 +0200 | [diff] [blame] | 3751 | struct connection *conn; |
Willy Tarreau | e8df1e1 | 2013-12-16 14:30:55 +0100 | [diff] [blame] | 3752 | char *path; |
Willy Tarreau | 32e3c6a | 2013-10-11 19:34:20 +0200 | [diff] [blame] | 3753 | |
Willy Tarreau | 9471b8c | 2013-12-15 13:31:35 +0100 | [diff] [blame] | 3754 | /* Note that for now we don't reuse existing proxy connections */ |
| 3755 | if (unlikely((conn = si_alloc_conn(req->cons, 0)) == NULL)) { |
Willy Tarreau | 32e3c6a | 2013-10-11 19:34:20 +0200 | [diff] [blame] | 3756 | txn->req.msg_state = HTTP_MSG_ERROR; |
| 3757 | txn->status = 500; |
| 3758 | req->analysers = 0; |
| 3759 | stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_500)); |
| 3760 | |
| 3761 | if (!(s->flags & SN_ERR_MASK)) |
| 3762 | s->flags |= SN_ERR_RESOURCE; |
| 3763 | if (!(s->flags & SN_FINST_MASK)) |
| 3764 | s->flags |= SN_FINST_R; |
| 3765 | |
| 3766 | return 0; |
| 3767 | } |
Willy Tarreau | e8df1e1 | 2013-12-16 14:30:55 +0100 | [diff] [blame] | 3768 | |
| 3769 | path = http_get_path(txn); |
| 3770 | url2sa(req->buf->p + msg->sl.rq.u, |
| 3771 | path ? path - (req->buf->p + msg->sl.rq.u) : msg->sl.rq.u_l, |
| 3772 | &conn->addr.to); |
| 3773 | /* if the path was found, we have to remove everything between |
| 3774 | * req->buf->p + msg->sl.rq.u and path (excluded). If it was not |
| 3775 | * found, we need to replace from req->buf->p + msg->sl.rq.u for |
| 3776 | * u_l characters by a single "/". |
| 3777 | */ |
| 3778 | if (path) { |
| 3779 | char *cur_ptr = req->buf->p; |
| 3780 | char *cur_end = cur_ptr + txn->req.sl.rq.l; |
| 3781 | int delta; |
| 3782 | |
| 3783 | delta = buffer_replace2(req->buf, req->buf->p + msg->sl.rq.u, path, NULL, 0); |
| 3784 | http_msg_move_end(&txn->req, delta); |
| 3785 | cur_end += delta; |
| 3786 | if (http_parse_reqline(&txn->req, HTTP_MSG_RQMETH, cur_ptr, cur_end + 1, NULL, NULL) == NULL) |
| 3787 | goto return_bad_req; |
| 3788 | } |
| 3789 | else { |
| 3790 | char *cur_ptr = req->buf->p; |
| 3791 | char *cur_end = cur_ptr + txn->req.sl.rq.l; |
| 3792 | int delta; |
| 3793 | |
| 3794 | delta = buffer_replace2(req->buf, req->buf->p + msg->sl.rq.u, |
| 3795 | req->buf->p + msg->sl.rq.u + msg->sl.rq.u_l, "/", 1); |
| 3796 | http_msg_move_end(&txn->req, delta); |
| 3797 | cur_end += delta; |
| 3798 | if (http_parse_reqline(&txn->req, HTTP_MSG_RQMETH, cur_ptr, cur_end + 1, NULL, NULL) == NULL) |
| 3799 | goto return_bad_req; |
| 3800 | } |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3801 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3802 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3803 | /* |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 3804 | * 7: Now we can work with the cookies. |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3805 | * Note that doing so might move headers in the request, but |
| 3806 | * the fields will stay coherent and the URI will not move. |
| 3807 | * This should only be performed in the backend. |
| 3808 | */ |
Willy Tarreau | fd39dda | 2008-10-17 12:01:58 +0200 | [diff] [blame] | 3809 | if ((s->be->cookie_name || s->be->appsession_name || s->fe->capture_name) |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3810 | && !(txn->flags & (TX_CLDENY|TX_CLTARPIT))) |
| 3811 | manage_client_side_cookies(s, req); |
Willy Tarreau | 7ac51f6 | 2007-03-25 16:00:04 +0200 | [diff] [blame] | 3812 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3813 | /* |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 3814 | * 8: the appsession cookie was looked up very early in 1.2, |
| 3815 | * so let's do the same now. |
| 3816 | */ |
| 3817 | |
Cyril Bonté | 47fdd8e | 2010-04-25 00:00:51 +0200 | [diff] [blame] | 3818 | /* It needs to look into the URI unless persistence must be ignored */ |
| 3819 | if ((txn->sessid == NULL) && s->be->appsession_name && !(s->flags & SN_IGNORE_PRST)) { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 3820 | 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] | 3821 | } |
| 3822 | |
William Lallemand | a73203e | 2012-03-12 12:48:57 +0100 | [diff] [blame] | 3823 | /* add unique-id if "header-unique-id" is specified */ |
| 3824 | |
William Lallemand | 5b7ea3a | 2013-08-28 15:44:19 +0200 | [diff] [blame] | 3825 | if (!LIST_ISEMPTY(&s->fe->format_unique_id)) { |
| 3826 | if ((s->unique_id = pool_alloc2(pool2_uniqueid)) == NULL) |
| 3827 | goto return_bad_req; |
| 3828 | s->unique_id[0] = '\0'; |
William Lallemand | a73203e | 2012-03-12 12:48:57 +0100 | [diff] [blame] | 3829 | build_logline(s, s->unique_id, UNIQUEID_LEN, &s->fe->format_unique_id); |
William Lallemand | 5b7ea3a | 2013-08-28 15:44:19 +0200 | [diff] [blame] | 3830 | } |
William Lallemand | a73203e | 2012-03-12 12:48:57 +0100 | [diff] [blame] | 3831 | |
| 3832 | if (s->fe->header_unique_id && s->unique_id) { |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 3833 | chunk_printf(&trash, "%s: %s", s->fe->header_unique_id, s->unique_id); |
| 3834 | if (trash.len < 0) |
William Lallemand | a73203e | 2012-03-12 12:48:57 +0100 | [diff] [blame] | 3835 | goto return_bad_req; |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 3836 | 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] | 3837 | goto return_bad_req; |
| 3838 | } |
| 3839 | |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 3840 | /* |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3841 | * 9: add X-Forwarded-For if either the frontend or the backend |
| 3842 | * asks for it. |
| 3843 | */ |
| 3844 | if ((s->fe->options | s->be->options) & PR_O_FWDFOR) { |
Willy Tarreau | 87cf514 | 2011-08-19 22:57:24 +0200 | [diff] [blame] | 3845 | struct hdr_ctx ctx = { .idx = 0 }; |
Willy Tarreau | 87cf514 | 2011-08-19 22:57:24 +0200 | [diff] [blame] | 3846 | if (!((s->fe->options | s->be->options) & PR_O_FF_ALWAYS) && |
Cyril Bonté | a32d275 | 2012-05-29 23:27:41 +0200 | [diff] [blame] | 3847 | http_find_header2(s->be->fwdfor_hdr_len ? s->be->fwdfor_hdr_name : s->fe->fwdfor_hdr_name, |
| 3848 | s->be->fwdfor_hdr_len ? s->be->fwdfor_hdr_len : s->fe->fwdfor_hdr_len, |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 3849 | req->buf->p, &txn->hdr_idx, &ctx)) { |
Willy Tarreau | 87cf514 | 2011-08-19 22:57:24 +0200 | [diff] [blame] | 3850 | /* The header is set to be added only if none is present |
| 3851 | * and we found it, so don't do anything. |
| 3852 | */ |
| 3853 | } |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 3854 | else if (cli_conn && cli_conn->addr.from.ss_family == AF_INET) { |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3855 | /* Add an X-Forwarded-For header unless the source IP is |
| 3856 | * in the 'except' network range. |
| 3857 | */ |
| 3858 | if ((!s->fe->except_mask.s_addr || |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 3859 | (((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr.s_addr & s->fe->except_mask.s_addr) |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3860 | != s->fe->except_net.s_addr) && |
| 3861 | (!s->be->except_mask.s_addr || |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 3862 | (((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] | 3863 | != s->be->except_net.s_addr)) { |
Willy Tarreau | 2a32428 | 2006-12-05 00:05:46 +0100 | [diff] [blame] | 3864 | int len; |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3865 | unsigned char *pn; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 3866 | pn = (unsigned char *)&((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr; |
Ross West | af72a1d | 2008-08-03 10:51:45 +0200 | [diff] [blame] | 3867 | |
| 3868 | /* Note: we rely on the backend to get the header name to be used for |
| 3869 | * x-forwarded-for, because the header is really meant for the backends. |
| 3870 | * However, if the backend did not specify any option, we have to rely |
| 3871 | * on the frontend's header name. |
| 3872 | */ |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3873 | if (s->be->fwdfor_hdr_len) { |
| 3874 | len = s->be->fwdfor_hdr_len; |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 3875 | memcpy(trash.str, s->be->fwdfor_hdr_name, len); |
Ross West | af72a1d | 2008-08-03 10:51:45 +0200 | [diff] [blame] | 3876 | } else { |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3877 | len = s->fe->fwdfor_hdr_len; |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 3878 | memcpy(trash.str, s->fe->fwdfor_hdr_name, len); |
Willy Tarreau | b86db34 | 2009-11-30 11:50:16 +0100 | [diff] [blame] | 3879 | } |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 3880 | len += sprintf(trash.str + 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] | 3881 | |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 3882 | 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] | 3883 | goto return_bad_req; |
Willy Tarreau | 2a32428 | 2006-12-05 00:05:46 +0100 | [diff] [blame] | 3884 | } |
| 3885 | } |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 3886 | else if (cli_conn && cli_conn->addr.from.ss_family == AF_INET6) { |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3887 | /* FIXME: for the sake of completeness, we should also support |
| 3888 | * 'except' here, although it is mostly useless in this case. |
matt.farnsworth@nokia.com | 1c2ab96 | 2008-04-14 20:47:37 +0200 | [diff] [blame] | 3889 | */ |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3890 | int len; |
| 3891 | char pn[INET6_ADDRSTRLEN]; |
| 3892 | inet_ntop(AF_INET6, |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 3893 | (const void *)&((struct sockaddr_in6 *)(&cli_conn->addr.from))->sin6_addr, |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3894 | pn, sizeof(pn)); |
matt.farnsworth@nokia.com | 1c2ab96 | 2008-04-14 20:47:37 +0200 | [diff] [blame] | 3895 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3896 | /* Note: we rely on the backend to get the header name to be used for |
| 3897 | * x-forwarded-for, because the header is really meant for the backends. |
| 3898 | * However, if the backend did not specify any option, we have to rely |
| 3899 | * on the frontend's header name. |
matt.farnsworth@nokia.com | 1c2ab96 | 2008-04-14 20:47:37 +0200 | [diff] [blame] | 3900 | */ |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3901 | if (s->be->fwdfor_hdr_len) { |
| 3902 | len = s->be->fwdfor_hdr_len; |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 3903 | memcpy(trash.str, s->be->fwdfor_hdr_name, len); |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3904 | } else { |
| 3905 | len = s->fe->fwdfor_hdr_len; |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 3906 | memcpy(trash.str, s->fe->fwdfor_hdr_name, len); |
matt.farnsworth@nokia.com | 1c2ab96 | 2008-04-14 20:47:37 +0200 | [diff] [blame] | 3907 | } |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 3908 | len += sprintf(trash.str + len, ": %s", pn); |
Willy Tarreau | adfb856 | 2008-08-11 15:24:42 +0200 | [diff] [blame] | 3909 | |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 3910 | 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] | 3911 | goto return_bad_req; |
| 3912 | } |
| 3913 | } |
| 3914 | |
| 3915 | /* |
Maik Broemme | 2850cb4 | 2009-04-17 18:53:21 +0200 | [diff] [blame] | 3916 | * 10: add X-Original-To if either the frontend or the backend |
| 3917 | * asks for it. |
| 3918 | */ |
| 3919 | if ((s->fe->options | s->be->options) & PR_O_ORGTO) { |
| 3920 | |
| 3921 | /* FIXME: don't know if IPv6 can handle that case too. */ |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 3922 | if (cli_conn && cli_conn->addr.from.ss_family == AF_INET) { |
Maik Broemme | 2850cb4 | 2009-04-17 18:53:21 +0200 | [diff] [blame] | 3923 | /* Add an X-Original-To header unless the destination IP is |
| 3924 | * in the 'except' network range. |
| 3925 | */ |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 3926 | conn_get_to_addr(cli_conn); |
Maik Broemme | 2850cb4 | 2009-04-17 18:53:21 +0200 | [diff] [blame] | 3927 | |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 3928 | if (cli_conn->addr.to.ss_family == AF_INET && |
Emeric Brun | 5bd86a8 | 2010-10-22 17:23:04 +0200 | [diff] [blame] | 3929 | ((!s->fe->except_mask_to.s_addr || |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 3930 | (((struct sockaddr_in *)&cli_conn->addr.to)->sin_addr.s_addr & s->fe->except_mask_to.s_addr) |
Emeric Brun | 5bd86a8 | 2010-10-22 17:23:04 +0200 | [diff] [blame] | 3931 | != s->fe->except_to.s_addr) && |
| 3932 | (!s->be->except_mask_to.s_addr || |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 3933 | (((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] | 3934 | != s->be->except_to.s_addr))) { |
Maik Broemme | 2850cb4 | 2009-04-17 18:53:21 +0200 | [diff] [blame] | 3935 | int len; |
| 3936 | unsigned char *pn; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 3937 | pn = (unsigned char *)&((struct sockaddr_in *)&cli_conn->addr.to)->sin_addr; |
Maik Broemme | 2850cb4 | 2009-04-17 18:53:21 +0200 | [diff] [blame] | 3938 | |
| 3939 | /* Note: we rely on the backend to get the header name to be used for |
| 3940 | * x-original-to, because the header is really meant for the backends. |
| 3941 | * However, if the backend did not specify any option, we have to rely |
| 3942 | * on the frontend's header name. |
| 3943 | */ |
| 3944 | if (s->be->orgto_hdr_len) { |
| 3945 | len = s->be->orgto_hdr_len; |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 3946 | memcpy(trash.str, s->be->orgto_hdr_name, len); |
Maik Broemme | 2850cb4 | 2009-04-17 18:53:21 +0200 | [diff] [blame] | 3947 | } else { |
| 3948 | len = s->fe->orgto_hdr_len; |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 3949 | memcpy(trash.str, s->fe->orgto_hdr_name, len); |
Willy Tarreau | b86db34 | 2009-11-30 11:50:16 +0100 | [diff] [blame] | 3950 | } |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 3951 | len += sprintf(trash.str + 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] | 3952 | |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 3953 | 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] | 3954 | goto return_bad_req; |
| 3955 | } |
| 3956 | } |
| 3957 | } |
| 3958 | |
Willy Tarreau | 50fc777 | 2012-11-11 22:19:57 +0100 | [diff] [blame] | 3959 | /* 11: add "Connection: close" or "Connection: keep-alive" if needed and not yet set. |
| 3960 | * If an "Upgrade" token is found, the header is left untouched in order not to have |
| 3961 | * to deal with some servers bugs : some of them fail an Upgrade if anything but |
| 3962 | * "Upgrade" is present in the Connection header. |
| 3963 | */ |
| 3964 | if (!(txn->flags & TX_HDR_CONN_UPG) && |
| 3965 | (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) || |
| 3966 | ((s->fe->options|s->be->options) & PR_O_HTTP_CLOSE))) { |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 3967 | unsigned int want_flags = 0; |
| 3968 | |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 3969 | if (msg->flags & HTTP_MSGF_VER_11) { |
Willy Tarreau | 22a9534 | 2010-09-29 14:31:41 +0200 | [diff] [blame] | 3970 | if (((txn->flags & TX_CON_WANT_MSK) >= TX_CON_WANT_SCL || |
| 3971 | ((s->fe->options|s->be->options) & PR_O_HTTP_CLOSE)) && |
| 3972 | !((s->fe->options2|s->be->options2) & PR_O2_FAKE_KA)) |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 3973 | want_flags |= TX_CON_CLO_SET; |
| 3974 | } else { |
Willy Tarreau | 22a9534 | 2010-09-29 14:31:41 +0200 | [diff] [blame] | 3975 | if (((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL && |
| 3976 | !((s->fe->options|s->be->options) & PR_O_HTTP_CLOSE)) || |
| 3977 | ((s->fe->options2|s->be->options2) & PR_O2_FAKE_KA)) |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 3978 | want_flags |= TX_CON_KAL_SET; |
| 3979 | } |
| 3980 | |
| 3981 | 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] | 3982 | http_change_connection_header(txn, msg, want_flags); |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3983 | } |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 3984 | |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 3985 | |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 3986 | /* If we have no server assigned yet and we're balancing on url_param |
| 3987 | * with a POST request, we may be interested in checking the body for |
| 3988 | * that parameter. This will be done in another analyser. |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3989 | */ |
| 3990 | if (!(s->flags & (SN_ASSIGNED|SN_DIRECT)) && |
| 3991 | s->txn.meth == HTTP_METH_POST && s->be->url_param_name != NULL && |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 3992 | s->be->url_param_post_limit != 0 && |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 3993 | (msg->flags & (HTTP_MSGF_CNT_LEN|HTTP_MSGF_TE_CHNK))) { |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 3994 | channel_dont_connect(req); |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 3995 | req->analysers |= AN_REQ_HTTP_BODY; |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3996 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3997 | |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 3998 | if (msg->flags & HTTP_MSGF_XFER_LEN) { |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 3999 | req->analysers |= AN_REQ_HTTP_XFER_BODY; |
Willy Tarreau | 5e20552 | 2011-12-17 16:34:27 +0100 | [diff] [blame] | 4000 | #ifdef TCP_QUICKACK |
| 4001 | /* We expect some data from the client. Unless we know for sure |
| 4002 | * we already have a full request, we have to re-enable quick-ack |
| 4003 | * in case we previously disabled it, otherwise we might cause |
| 4004 | * the client to delay further data. |
| 4005 | */ |
| 4006 | if ((s->listener->options & LI_O_NOQUICKACK) && |
Willy Tarreau | f79c817 | 2013-10-21 16:30:56 +0200 | [diff] [blame] | 4007 | cli_conn && (cli_conn->flags & CO_FL_CTRL_READY) && |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 4008 | ((msg->flags & HTTP_MSGF_TE_CHNK) || |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 4009 | (msg->body_len > req->buf->i - txn->req.eoh - 2))) |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 4010 | 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] | 4011 | #endif |
| 4012 | } |
Willy Tarreau | 0394594 | 2009-12-22 16:50:27 +0100 | [diff] [blame] | 4013 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4014 | /************************************************************* |
| 4015 | * OK, that's finished for the headers. We have done what we * |
| 4016 | * could. Let's switch to the DATA state. * |
| 4017 | ************************************************************/ |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 4018 | req->analyse_exp = TICK_ETERNITY; |
| 4019 | req->analysers &= ~an_bit; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 4020 | |
Willy Tarreau | 7bb68ab | 2012-05-13 14:48:59 +0200 | [diff] [blame] | 4021 | /* if the server closes the connection, we want to immediately react |
| 4022 | * and close the socket to save packets and syscalls. |
| 4023 | */ |
Willy Tarreau | 40f151a | 2012-12-20 12:10:09 +0100 | [diff] [blame] | 4024 | if (!(req->analysers & AN_REQ_HTTP_XFER_BODY)) |
| 4025 | req->cons->flags |= SI_FL_NOHALF; |
Willy Tarreau | 7bb68ab | 2012-05-13 14:48:59 +0200 | [diff] [blame] | 4026 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4027 | s->logs.tv_request = now; |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4028 | /* OK let's go on with the BODY now */ |
| 4029 | return 1; |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 4030 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4031 | return_bad_req: /* let's centralize all bad requests */ |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 4032 | if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) { |
Willy Tarreau | f073a83 | 2009-03-01 23:21:47 +0100 | [diff] [blame] | 4033 | /* we detected a parsing error. We want to archive this request |
| 4034 | * in the dedicated proxy area for later troubleshooting. |
| 4035 | */ |
Willy Tarreau | 8a0cef2 | 2012-03-09 13:39:23 +0100 | [diff] [blame] | 4036 | http_capture_bad_message(&s->fe->invalid_req, s, msg, msg->msg_state, s->fe); |
Willy Tarreau | f073a83 | 2009-03-01 23:21:47 +0100 | [diff] [blame] | 4037 | } |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 4038 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4039 | txn->req.msg_state = HTTP_MSG_ERROR; |
| 4040 | txn->status = 400; |
| 4041 | req->analysers = 0; |
Willy Tarreau | 783f258 | 2012-09-04 12:19:04 +0200 | [diff] [blame] | 4042 | stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_400)); |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 4043 | |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 4044 | s->fe->fe_counters.failed_req++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 4045 | if (s->listener->counters) |
| 4046 | s->listener->counters->failed_req++; |
Willy Tarreau | adfb856 | 2008-08-11 15:24:42 +0200 | [diff] [blame] | 4047 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4048 | if (!(s->flags & SN_ERR_MASK)) |
| 4049 | s->flags |= SN_ERR_PRXCOND; |
| 4050 | if (!(s->flags & SN_FINST_MASK)) |
| 4051 | s->flags |= SN_FINST_R; |
Willy Tarreau | dafde43 | 2008-08-17 01:00:46 +0200 | [diff] [blame] | 4052 | return 0; |
Willy Tarreau | c65a3ba | 2008-08-11 23:42:50 +0200 | [diff] [blame] | 4053 | } |
Willy Tarreau | adfb856 | 2008-08-11 15:24:42 +0200 | [diff] [blame] | 4054 | |
Willy Tarreau | 60b85b0 | 2008-11-30 23:28:40 +0100 | [diff] [blame] | 4055 | /* This function is an analyser which processes the HTTP tarpit. It always |
| 4056 | * returns zero, at the beginning because it prevents any other processing |
| 4057 | * from occurring, and at the end because it terminates the request. |
| 4058 | */ |
Willy Tarreau | 7421efb | 2012-07-02 15:11:27 +0200 | [diff] [blame] | 4059 | int http_process_tarpit(struct session *s, struct channel *req, int an_bit) |
Willy Tarreau | 60b85b0 | 2008-11-30 23:28:40 +0100 | [diff] [blame] | 4060 | { |
| 4061 | struct http_txn *txn = &s->txn; |
| 4062 | |
| 4063 | /* This connection is being tarpitted. The CLIENT side has |
| 4064 | * already set the connect expiration date to the right |
| 4065 | * timeout. We just have to check that the client is still |
| 4066 | * there and that the timeout has not expired. |
| 4067 | */ |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 4068 | channel_dont_connect(req); |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 4069 | if ((req->flags & (CF_SHUTR|CF_READ_ERROR)) == 0 && |
Willy Tarreau | 60b85b0 | 2008-11-30 23:28:40 +0100 | [diff] [blame] | 4070 | !tick_is_expired(req->analyse_exp, now_ms)) |
| 4071 | return 0; |
| 4072 | |
| 4073 | /* We will set the queue timer to the time spent, just for |
| 4074 | * logging purposes. We fake a 500 server error, so that the |
| 4075 | * attacker will not suspect his connection has been tarpitted. |
| 4076 | * It will not cause trouble to the logs because we can exclude |
| 4077 | * the tarpitted connections by filtering on the 'PT' status flags. |
| 4078 | */ |
Willy Tarreau | 60b85b0 | 2008-11-30 23:28:40 +0100 | [diff] [blame] | 4079 | s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now); |
| 4080 | |
| 4081 | txn->status = 500; |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 4082 | if (!(req->flags & CF_READ_ERROR)) |
Willy Tarreau | 783f258 | 2012-09-04 12:19:04 +0200 | [diff] [blame] | 4083 | stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_500)); |
Willy Tarreau | 60b85b0 | 2008-11-30 23:28:40 +0100 | [diff] [blame] | 4084 | |
| 4085 | req->analysers = 0; |
| 4086 | req->analyse_exp = TICK_ETERNITY; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 4087 | |
Willy Tarreau | 60b85b0 | 2008-11-30 23:28:40 +0100 | [diff] [blame] | 4088 | if (!(s->flags & SN_ERR_MASK)) |
| 4089 | s->flags |= SN_ERR_PRXCOND; |
| 4090 | if (!(s->flags & SN_FINST_MASK)) |
| 4091 | s->flags |= SN_FINST_T; |
| 4092 | return 0; |
| 4093 | } |
| 4094 | |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 4095 | /* This function is an analyser which processes the HTTP request body. It looks |
| 4096 | * for parameters to be used for the load balancing algorithm (url_param). It |
| 4097 | * must only be called after the standard HTTP request processing has occurred, |
| 4098 | * because it expects the request to be parsed. It returns zero if it needs to |
| 4099 | * read more data, or 1 once it has completed its analysis. |
| 4100 | */ |
Willy Tarreau | 7421efb | 2012-07-02 15:11:27 +0200 | [diff] [blame] | 4101 | int http_process_request_body(struct session *s, struct channel *req, int an_bit) |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 4102 | { |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 4103 | struct http_txn *txn = &s->txn; |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 4104 | struct http_msg *msg = &s->txn.req; |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 4105 | long long limit = s->be->url_param_post_limit; |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 4106 | |
| 4107 | /* We have to parse the HTTP request body to find any required data. |
| 4108 | * "balance url_param check_post" should have been the only way to get |
| 4109 | * into this. We were brought here after HTTP header analysis, so all |
| 4110 | * related structures are ready. |
| 4111 | */ |
| 4112 | |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 4113 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) |
| 4114 | goto missing_data; |
| 4115 | |
| 4116 | if (msg->msg_state < HTTP_MSG_100_SENT) { |
| 4117 | /* If we have HTTP/1.1 and Expect: 100-continue, then we must |
| 4118 | * send an HTTP/1.1 100 Continue intermediate response. |
| 4119 | */ |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 4120 | if (msg->flags & HTTP_MSGF_VER_11) { |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 4121 | struct hdr_ctx ctx; |
| 4122 | ctx.idx = 0; |
| 4123 | /* Expect is allowed in 1.1, look for it */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 4124 | if (http_find_header2("Expect", 6, req->buf->p, &txn->hdr_idx, &ctx) && |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 4125 | unlikely(ctx.vlen == 12 && strncasecmp(ctx.line+ctx.val, "100-continue", 12) == 0)) { |
Willy Tarreau | 9dab5fc | 2012-05-07 11:56:55 +0200 | [diff] [blame] | 4126 | bo_inject(s->rep, http_100_chunk.str, http_100_chunk.len); |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 4127 | } |
| 4128 | } |
| 4129 | msg->msg_state = HTTP_MSG_100_SENT; |
| 4130 | } |
| 4131 | |
| 4132 | if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) { |
Willy Tarreau | fa4a03c | 2012-03-09 21:28:54 +0100 | [diff] [blame] | 4133 | /* we have msg->sov which points to the first byte of message body. |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 4134 | * req->buf->p still points to the beginning of the message and msg->sol |
Willy Tarreau | 2692736 | 2012-05-18 23:22:52 +0200 | [diff] [blame] | 4135 | * is still null. We must save the body in msg->next because it |
| 4136 | * survives buffer re-alignments. |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4137 | */ |
Willy Tarreau | ea1175a | 2012-03-05 15:52:30 +0100 | [diff] [blame] | 4138 | msg->next = msg->sov; |
Willy Tarreau | a458b67 | 2012-03-05 11:17:50 +0100 | [diff] [blame] | 4139 | |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 4140 | if (msg->flags & HTTP_MSGF_TE_CHNK) |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 4141 | msg->msg_state = HTTP_MSG_CHUNK_SIZE; |
| 4142 | else |
| 4143 | msg->msg_state = HTTP_MSG_DATA; |
| 4144 | } |
| 4145 | |
| 4146 | if (msg->msg_state == HTTP_MSG_CHUNK_SIZE) { |
Willy Tarreau | 124d991 | 2011-03-01 20:30:48 +0100 | [diff] [blame] | 4147 | /* read the chunk size and assign it to ->chunk_len, then |
Willy Tarreau | a458b67 | 2012-03-05 11:17:50 +0100 | [diff] [blame] | 4148 | * 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] | 4149 | * TRAILERS state. |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 4150 | */ |
Willy Tarreau | 4baf44b | 2012-03-09 14:10:20 +0100 | [diff] [blame] | 4151 | int ret = http_parse_chunk_size(msg); |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 4152 | |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 4153 | if (!ret) |
| 4154 | goto missing_data; |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 4155 | else if (ret < 0) { |
| 4156 | session_inc_http_err_ctr(s); |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 4157 | goto return_bad_req; |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 4158 | } |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 4159 | } |
| 4160 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4161 | /* Now we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state. |
Willy Tarreau | fa4a03c | 2012-03-09 21:28:54 +0100 | [diff] [blame] | 4162 | * We have the first data byte is in msg->sov. We're waiting for at |
| 4163 | * least <url_param_post_limit> bytes after msg->sov. |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 4164 | */ |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 4165 | |
Willy Tarreau | 124d991 | 2011-03-01 20:30:48 +0100 | [diff] [blame] | 4166 | if (msg->body_len < limit) |
| 4167 | limit = msg->body_len; |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 4168 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 4169 | if (req->buf->i - msg->sov >= limit) /* we have enough bytes now */ |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 4170 | goto http_end; |
| 4171 | |
| 4172 | missing_data: |
| 4173 | /* we get here if we need to wait for more data */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 4174 | if (buffer_full(req->buf, global.tune.maxrewrite)) { |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 4175 | session_inc_http_err_ctr(s); |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 4176 | goto return_bad_req; |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 4177 | } |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 4178 | |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 4179 | 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] | 4180 | txn->status = 408; |
Willy Tarreau | 783f258 | 2012-09-04 12:19:04 +0200 | [diff] [blame] | 4181 | stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_408)); |
Willy Tarreau | 79ebac6 | 2010-06-07 13:47:49 +0200 | [diff] [blame] | 4182 | |
| 4183 | if (!(s->flags & SN_ERR_MASK)) |
| 4184 | s->flags |= SN_ERR_CLITO; |
| 4185 | if (!(s->flags & SN_FINST_MASK)) |
| 4186 | s->flags |= SN_FINST_D; |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 4187 | goto return_err_msg; |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 4188 | } |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 4189 | |
| 4190 | /* we get here if we need to wait for more data */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 4191 | if (!(req->flags & (CF_SHUTR | CF_READ_ERROR)) && !buffer_full(req->buf, global.tune.maxrewrite)) { |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 4192 | /* Not enough data. We'll re-use the http-request |
| 4193 | * timeout here. Ideally, we should set the timeout |
| 4194 | * relative to the accept() date. We just set the |
| 4195 | * request timeout once at the beginning of the |
| 4196 | * request. |
| 4197 | */ |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 4198 | channel_dont_connect(req); |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 4199 | if (!tick_isset(req->analyse_exp)) |
Willy Tarreau | cd7afc0 | 2009-07-12 10:03:17 +0200 | [diff] [blame] | 4200 | req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.httpreq); |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 4201 | return 0; |
| 4202 | } |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 4203 | |
| 4204 | http_end: |
| 4205 | /* The situation will not evolve, so let's give up on the analysis. */ |
| 4206 | s->logs.tv_request = now; /* update the request timer to reflect full request */ |
| 4207 | req->analysers &= ~an_bit; |
| 4208 | req->analyse_exp = TICK_ETERNITY; |
| 4209 | return 1; |
| 4210 | |
| 4211 | return_bad_req: /* let's centralize all bad requests */ |
| 4212 | txn->req.msg_state = HTTP_MSG_ERROR; |
| 4213 | txn->status = 400; |
Willy Tarreau | 783f258 | 2012-09-04 12:19:04 +0200 | [diff] [blame] | 4214 | stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_400)); |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 4215 | |
Willy Tarreau | 79ebac6 | 2010-06-07 13:47:49 +0200 | [diff] [blame] | 4216 | if (!(s->flags & SN_ERR_MASK)) |
| 4217 | s->flags |= SN_ERR_PRXCOND; |
| 4218 | if (!(s->flags & SN_FINST_MASK)) |
| 4219 | s->flags |= SN_FINST_R; |
| 4220 | |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 4221 | return_err_msg: |
| 4222 | req->analysers = 0; |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 4223 | s->fe->fe_counters.failed_req++; |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 4224 | if (s->listener->counters) |
| 4225 | s->listener->counters->failed_req++; |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 4226 | return 0; |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 4227 | } |
| 4228 | |
Willy Tarreau | d1de8af | 2012-05-18 22:12:14 +0200 | [diff] [blame] | 4229 | /* send a server's name with an outgoing request over an established connection. |
| 4230 | * Note: this function is designed to be called once the request has been scheduled |
| 4231 | * for being forwarded. This is the reason why it rewinds the buffer before |
| 4232 | * proceeding. |
| 4233 | */ |
Willy Tarreau | 45c0d98 | 2012-03-09 12:11:57 +0100 | [diff] [blame] | 4234 | 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] | 4235 | |
| 4236 | struct hdr_ctx ctx; |
| 4237 | |
Mark Lamourine | c2247f0 | 2012-01-04 13:02:01 -0500 | [diff] [blame] | 4238 | char *hdr_name = be->server_id_hdr_name; |
| 4239 | int hdr_name_len = be->server_id_hdr_len; |
Willy Tarreau | 394db37 | 2012-10-12 22:40:39 +0200 | [diff] [blame] | 4240 | struct channel *chn = txn->req.chn; |
Mark Lamourine | c2247f0 | 2012-01-04 13:02:01 -0500 | [diff] [blame] | 4241 | char *hdr_val; |
Willy Tarreau | d1de8af | 2012-05-18 22:12:14 +0200 | [diff] [blame] | 4242 | unsigned int old_o, old_i; |
Mark Lamourine | c2247f0 | 2012-01-04 13:02:01 -0500 | [diff] [blame] | 4243 | |
William Lallemand | d9e9066 | 2012-01-30 17:27:17 +0100 | [diff] [blame] | 4244 | ctx.idx = 0; |
| 4245 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 4246 | old_o = chn->buf->o; |
Willy Tarreau | d1de8af | 2012-05-18 22:12:14 +0200 | [diff] [blame] | 4247 | if (old_o) { |
| 4248 | /* The request was already skipped, let's restore it */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 4249 | b_rew(chn->buf, old_o); |
Willy Tarreau | d1de8af | 2012-05-18 22:12:14 +0200 | [diff] [blame] | 4250 | } |
| 4251 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 4252 | old_i = chn->buf->i; |
| 4253 | 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] | 4254 | /* remove any existing values from the header */ |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 4255 | http_remove_header2(&txn->req, &txn->hdr_idx, &ctx); |
Mark Lamourine | c2247f0 | 2012-01-04 13:02:01 -0500 | [diff] [blame] | 4256 | } |
| 4257 | |
| 4258 | /* Add the new header requested with the server value */ |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 4259 | hdr_val = trash.str; |
Mark Lamourine | c2247f0 | 2012-01-04 13:02:01 -0500 | [diff] [blame] | 4260 | memcpy(hdr_val, hdr_name, hdr_name_len); |
| 4261 | hdr_val += hdr_name_len; |
| 4262 | *hdr_val++ = ':'; |
| 4263 | *hdr_val++ = ' '; |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 4264 | hdr_val += strlcpy2(hdr_val, srv_name, trash.str + trash.size - hdr_val); |
| 4265 | 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] | 4266 | |
Willy Tarreau | d1de8af | 2012-05-18 22:12:14 +0200 | [diff] [blame] | 4267 | if (old_o) { |
| 4268 | /* If this was a forwarded request, we must readjust the amount of |
| 4269 | * data to be forwarded in order to take into account the size |
Willy Tarreau | 2fef9b1 | 2013-03-26 01:08:21 +0100 | [diff] [blame] | 4270 | * variations. Note that if the request was already scheduled for |
| 4271 | * forwarding, it had its req->sol pointing to the body, which |
| 4272 | * must then be updated too. |
Willy Tarreau | d1de8af | 2012-05-18 22:12:14 +0200 | [diff] [blame] | 4273 | */ |
Willy Tarreau | 2fef9b1 | 2013-03-26 01:08:21 +0100 | [diff] [blame] | 4274 | txn->req.sol += chn->buf->i - old_i; |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 4275 | b_adv(chn->buf, old_o + chn->buf->i - old_i); |
Willy Tarreau | d1de8af | 2012-05-18 22:12:14 +0200 | [diff] [blame] | 4276 | } |
| 4277 | |
Mark Lamourine | c2247f0 | 2012-01-04 13:02:01 -0500 | [diff] [blame] | 4278 | return 0; |
| 4279 | } |
| 4280 | |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4281 | /* Terminate current transaction and prepare a new one. This is very tricky |
| 4282 | * right now but it works. |
| 4283 | */ |
| 4284 | void http_end_txn_clean_session(struct session *s) |
| 4285 | { |
Willy Tarreau | 068621e | 2013-12-23 15:11:25 +0100 | [diff] [blame] | 4286 | int prev_status = s->txn.status; |
| 4287 | |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4288 | /* FIXME: We need a more portable way of releasing a backend's and a |
| 4289 | * server's connections. We need a safer way to reinitialize buffer |
| 4290 | * flags. We also need a more accurate method for computing per-request |
| 4291 | * data. |
| 4292 | */ |
| 4293 | http_silent_debug(__LINE__, s); |
| 4294 | |
Willy Tarreau | 4213a11 | 2013-12-15 10:25:42 +0100 | [diff] [blame] | 4295 | /* unless we're doing keep-alive, we want to quickly close the connection |
| 4296 | * to the server. |
| 4297 | */ |
| 4298 | if (((s->txn.flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) || |
| 4299 | !si_conn_ready(s->req->cons)) { |
| 4300 | s->req->cons->flags |= SI_FL_NOLINGER | SI_FL_NOHALF; |
| 4301 | si_shutr(s->req->cons); |
| 4302 | si_shutw(s->req->cons); |
| 4303 | } |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4304 | |
| 4305 | http_silent_debug(__LINE__, s); |
| 4306 | |
Willy Tarreau | 2d5cd47 | 2012-03-01 23:34:37 +0100 | [diff] [blame] | 4307 | if (s->flags & SN_BE_ASSIGNED) { |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4308 | s->be->beconn--; |
Willy Tarreau | 2d5cd47 | 2012-03-01 23:34:37 +0100 | [diff] [blame] | 4309 | if (unlikely(s->srv_conn)) |
| 4310 | sess_change_server(s, NULL); |
| 4311 | } |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4312 | |
| 4313 | s->logs.t_close = tv_ms_elapsed(&s->logs.tv_accept, &now); |
| 4314 | session_process_counters(s); |
Willy Tarreau | f059a0f | 2010-08-03 16:29:52 +0200 | [diff] [blame] | 4315 | session_stop_backend_counters(s); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4316 | |
| 4317 | if (s->txn.status) { |
| 4318 | int n; |
| 4319 | |
| 4320 | n = s->txn.status / 100; |
| 4321 | if (n < 1 || n > 5) |
| 4322 | n = 0; |
| 4323 | |
Willy Tarreau | 5e16cbc | 2012-11-24 14:54:13 +0100 | [diff] [blame] | 4324 | if (s->fe->mode == PR_MODE_HTTP) { |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 4325 | s->fe->fe_counters.p.http.rsp[n]++; |
Willy Tarreau | 8139b99 | 2012-11-27 07:35:31 +0100 | [diff] [blame] | 4326 | if (s->comp_algo && (s->flags & SN_COMP_READY)) |
Willy Tarreau | 5e16cbc | 2012-11-24 14:54:13 +0100 | [diff] [blame] | 4327 | s->fe->fe_counters.p.http.comp_rsp++; |
| 4328 | } |
Willy Tarreau | 2465779 | 2010-02-26 10:30:28 +0100 | [diff] [blame] | 4329 | if ((s->flags & SN_BE_ASSIGNED) && |
Willy Tarreau | 5e16cbc | 2012-11-24 14:54:13 +0100 | [diff] [blame] | 4330 | (s->be->mode == PR_MODE_HTTP)) { |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 4331 | s->be->be_counters.p.http.rsp[n]++; |
Willy Tarreau | 5e16cbc | 2012-11-24 14:54:13 +0100 | [diff] [blame] | 4332 | s->be->be_counters.p.http.cum_req++; |
Willy Tarreau | 8139b99 | 2012-11-27 07:35:31 +0100 | [diff] [blame] | 4333 | if (s->comp_algo && (s->flags & SN_COMP_READY)) |
Willy Tarreau | 5e16cbc | 2012-11-24 14:54:13 +0100 | [diff] [blame] | 4334 | s->be->be_counters.p.http.comp_rsp++; |
| 4335 | } |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4336 | } |
| 4337 | |
| 4338 | /* don't count other requests' data */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 4339 | s->logs.bytes_in -= s->req->buf->i; |
| 4340 | s->logs.bytes_out -= s->rep->buf->i; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4341 | |
| 4342 | /* let's do a final log if we need it */ |
Willy Tarreau | d79a3b2 | 2012-12-28 09:40:16 +0100 | [diff] [blame] | 4343 | if (!LIST_ISEMPTY(&s->fe->logformat) && s->logs.logwait && |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4344 | !(s->flags & SN_MONITOR) && |
| 4345 | (!(s->fe->options & PR_O_NULLNOLOG) || s->req->total)) { |
| 4346 | s->do_log(s); |
| 4347 | } |
| 4348 | |
| 4349 | s->logs.accept_date = date; /* user-visible date for logging */ |
| 4350 | s->logs.tv_accept = now; /* corrected date for internal use */ |
| 4351 | tv_zero(&s->logs.tv_request); |
| 4352 | s->logs.t_queue = -1; |
| 4353 | s->logs.t_connect = -1; |
| 4354 | s->logs.t_data = -1; |
| 4355 | s->logs.t_close = 0; |
| 4356 | s->logs.prx_queue_size = 0; /* we get the number of pending conns before us */ |
| 4357 | s->logs.srv_queue_size = 0; /* we will get this number soon */ |
| 4358 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 4359 | s->logs.bytes_in = s->req->total = s->req->buf->i; |
| 4360 | s->logs.bytes_out = s->rep->total = s->rep->buf->i; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4361 | |
| 4362 | if (s->pend_pos) |
| 4363 | pendconn_free(s->pend_pos); |
| 4364 | |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 4365 | if (objt_server(s->target)) { |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4366 | if (s->flags & SN_CURR_SESS) { |
| 4367 | s->flags &= ~SN_CURR_SESS; |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 4368 | objt_server(s->target)->cur_sess--; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4369 | } |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 4370 | if (may_dequeue_tasks(objt_server(s->target), s->be)) |
| 4371 | process_srv_queue(objt_server(s->target)); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4372 | } |
| 4373 | |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 4374 | s->target = NULL; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4375 | |
Willy Tarreau | 4213a11 | 2013-12-15 10:25:42 +0100 | [diff] [blame] | 4376 | /* only release our endpoint if we don't intend to reuse the |
| 4377 | * connection. |
| 4378 | */ |
| 4379 | if (((s->txn.flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) || |
| 4380 | !si_conn_ready(s->req->cons)) { |
| 4381 | si_release_endpoint(s->req->cons); |
| 4382 | } |
| 4383 | |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4384 | s->req->cons->state = s->req->cons->prev_state = SI_ST_INI; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4385 | s->req->cons->err_type = SI_ET_NONE; |
Willy Tarreau | 0b3a411 | 2011-03-27 19:16:56 +0200 | [diff] [blame] | 4386 | s->req->cons->conn_retries = 0; /* used for logging too */ |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4387 | s->req->cons->exp = TICK_ETERNITY; |
Willy Tarreau | c920096 | 2013-12-31 23:03:09 +0100 | [diff] [blame] | 4388 | s->req->cons->flags &= SI_FL_DONT_WAKE; /* we're in the context of process_session */ |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 4389 | s->req->flags &= ~(CF_SHUTW|CF_SHUTW_NOW|CF_AUTO_CONNECT|CF_WRITE_ERROR|CF_STREAMER|CF_STREAMER_FAST|CF_NEVER_WAIT); |
| 4390 | s->rep->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); |
Cyril Bonté | 47fdd8e | 2010-04-25 00:00:51 +0200 | [diff] [blame] | 4391 | s->flags &= ~(SN_DIRECT|SN_ASSIGNED|SN_ADDR_SET|SN_BE_ASSIGNED|SN_FORCE_PRST|SN_IGNORE_PRST); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4392 | s->flags &= ~(SN_CURR_SESS|SN_REDIRECTABLE); |
Willy Tarreau | 543db62 | 2012-11-15 16:41:22 +0100 | [diff] [blame] | 4393 | |
| 4394 | if (s->flags & SN_COMP_READY) |
| 4395 | s->comp_algo->end(&s->comp_ctx); |
| 4396 | s->comp_algo = NULL; |
| 4397 | s->flags &= ~SN_COMP_READY; |
| 4398 | |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4399 | s->txn.meth = 0; |
| 4400 | http_reset_txn(s); |
Willy Tarreau | fcffa69 | 2010-01-10 14:21:19 +0100 | [diff] [blame] | 4401 | s->txn.flags |= TX_NOT_FIRST | TX_WAIT_NEXT_RQ; |
Willy Tarreau | 068621e | 2013-12-23 15:11:25 +0100 | [diff] [blame] | 4402 | |
| 4403 | if (prev_status == 401 || prev_status == 407) { |
| 4404 | /* In HTTP keep-alive mode, if we receive a 401, we still have |
| 4405 | * a chance of being able to send the visitor again to the same |
| 4406 | * server over the same connection. This is required by some |
| 4407 | * broken protocols such as NTLM, and anyway whenever there is |
| 4408 | * an opportunity for sending the challenge to the proper place, |
| 4409 | * it's better to do it (at least it helps with debugging). |
| 4410 | */ |
| 4411 | s->txn.flags |= TX_PREFER_LAST; |
| 4412 | } |
| 4413 | |
Willy Tarreau | ee55dc0 | 2010-06-01 10:56:34 +0200 | [diff] [blame] | 4414 | if (s->fe->options2 & PR_O2_INDEPSTR) |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4415 | s->req->cons->flags |= SI_FL_INDEP_STR; |
| 4416 | |
Willy Tarreau | 96e3121 | 2011-05-30 18:10:30 +0200 | [diff] [blame] | 4417 | if (s->fe->options2 & PR_O2_NODELAY) { |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 4418 | s->req->flags |= CF_NEVER_WAIT; |
| 4419 | s->rep->flags |= CF_NEVER_WAIT; |
Willy Tarreau | 96e3121 | 2011-05-30 18:10:30 +0200 | [diff] [blame] | 4420 | } |
| 4421 | |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4422 | /* if the request buffer is not empty, it means we're |
| 4423 | * about to process another request, so send pending |
| 4424 | * data with MSG_MORE to merge TCP packets when possible. |
Willy Tarreau | 065e833 | 2010-01-08 00:30:20 +0100 | [diff] [blame] | 4425 | * Just don't do this if the buffer is close to be full, |
| 4426 | * because the request will wait for it to flush a little |
| 4427 | * bit before proceeding. |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4428 | */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 4429 | if (s->req->buf->i) { |
| 4430 | if (s->rep->buf->o && |
| 4431 | !buffer_full(s->rep->buf, global.tune.maxrewrite) && |
| 4432 | bi_end(s->rep->buf) <= s->rep->buf->data + s->rep->buf->size - global.tune.maxrewrite) |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 4433 | s->rep->flags |= CF_EXPECT_MORE; |
Willy Tarreau | 065e833 | 2010-01-08 00:30:20 +0100 | [diff] [blame] | 4434 | } |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 4435 | |
| 4436 | /* we're removing the analysers, we MUST re-enable events detection */ |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 4437 | channel_auto_read(s->req); |
| 4438 | channel_auto_close(s->req); |
| 4439 | channel_auto_read(s->rep); |
| 4440 | channel_auto_close(s->rep); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4441 | |
Willy Tarreau | 2737562 | 2013-12-17 00:00:28 +0100 | [diff] [blame] | 4442 | /* we're in keep-alive with an idle connection, monitor it */ |
| 4443 | si_idle_conn(s->req->cons); |
| 4444 | |
Willy Tarreau | 342b11c | 2010-11-24 16:22:09 +0100 | [diff] [blame] | 4445 | s->req->analysers = s->listener->analysers; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4446 | s->rep->analysers = 0; |
| 4447 | |
| 4448 | http_silent_debug(__LINE__, s); |
| 4449 | } |
| 4450 | |
| 4451 | |
| 4452 | /* This function updates the request state machine according to the response |
| 4453 | * state machine and buffer flags. It returns 1 if it changes anything (flag |
| 4454 | * or state), otherwise zero. It ignores any state before HTTP_MSG_DONE, as |
| 4455 | * it is only used to find when a request/response couple is complete. Both |
| 4456 | * this function and its equivalent should loop until both return zero. It |
| 4457 | * can set its own state to DONE, CLOSING, CLOSED, TUNNEL, ERROR. |
| 4458 | */ |
| 4459 | int http_sync_req_state(struct session *s) |
| 4460 | { |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 4461 | struct channel *chn = s->req; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4462 | struct http_txn *txn = &s->txn; |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 4463 | unsigned int old_flags = chn->flags; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4464 | unsigned int old_state = txn->req.msg_state; |
| 4465 | |
| 4466 | http_silent_debug(__LINE__, s); |
| 4467 | if (unlikely(txn->req.msg_state < HTTP_MSG_BODY)) |
| 4468 | return 0; |
| 4469 | |
| 4470 | if (txn->req.msg_state == HTTP_MSG_DONE) { |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 4471 | /* No need to read anymore, the request was completely parsed. |
Willy Tarreau | 58bd8fd | 2010-09-28 14:16:41 +0200 | [diff] [blame] | 4472 | * We can shut the read side unless we want to abort_on_close, |
| 4473 | * or we have a POST request. The issue with POST requests is |
| 4474 | * that some browsers still send a CRLF after the request, and |
| 4475 | * this CRLF must be read so that it does not remain in the kernel |
| 4476 | * buffers, otherwise a close could cause an RST on some systems |
| 4477 | * (eg: Linux). |
Willy Tarreau | 3988d93 | 2013-12-27 23:03:08 +0100 | [diff] [blame] | 4478 | * Note that if we're using keep-alive on the client side, we'd |
| 4479 | * rather poll now and keep the polling enabled for the whole |
| 4480 | * session's life than enabling/disabling it between each |
| 4481 | * response and next request. |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 4482 | */ |
Willy Tarreau | 3988d93 | 2013-12-27 23:03:08 +0100 | [diff] [blame] | 4483 | if (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_SCL) && |
| 4484 | ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) && |
| 4485 | !(s->be->options & PR_O_ABRT_CLOSE) && |
| 4486 | txn->meth != HTTP_METH_POST) |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 4487 | channel_dont_read(chn); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4488 | |
Willy Tarreau | 40f151a | 2012-12-20 12:10:09 +0100 | [diff] [blame] | 4489 | /* if the server closes the connection, we want to immediately react |
| 4490 | * and close the socket to save packets and syscalls. |
| 4491 | */ |
| 4492 | chn->cons->flags |= SI_FL_NOHALF; |
| 4493 | |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4494 | if (txn->rsp.msg_state == HTTP_MSG_ERROR) |
| 4495 | goto wait_other_side; |
| 4496 | |
| 4497 | if (txn->rsp.msg_state < HTTP_MSG_DONE) { |
| 4498 | /* The server has not finished to respond, so we |
| 4499 | * don't want to move in order not to upset it. |
| 4500 | */ |
| 4501 | goto wait_other_side; |
| 4502 | } |
| 4503 | |
| 4504 | if (txn->rsp.msg_state == HTTP_MSG_TUNNEL) { |
| 4505 | /* if any side switches to tunnel mode, the other one does too */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 4506 | channel_auto_read(chn); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4507 | txn->req.msg_state = HTTP_MSG_TUNNEL; |
Willy Tarreau | fc47f91 | 2012-10-20 10:38:09 +0200 | [diff] [blame] | 4508 | chn->flags |= CF_NEVER_WAIT; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4509 | goto wait_other_side; |
| 4510 | } |
| 4511 | |
| 4512 | /* When we get here, it means that both the request and the |
| 4513 | * response have finished receiving. Depending on the connection |
| 4514 | * mode, we'll have to wait for the last bytes to leave in either |
| 4515 | * direction, and sometimes for a close to be effective. |
| 4516 | */ |
| 4517 | |
Willy Tarreau | cce7fa4 | 2010-01-16 23:19:39 +0100 | [diff] [blame] | 4518 | if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) { |
| 4519 | /* Server-close mode : queue a connection close to the server */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 4520 | if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW))) |
| 4521 | channel_shutw_now(chn); |
Willy Tarreau | cce7fa4 | 2010-01-16 23:19:39 +0100 | [diff] [blame] | 4522 | } |
| 4523 | else if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) { |
| 4524 | /* Option forceclose is set, or either side wants to close, |
| 4525 | * let's enforce it now that we're not expecting any new |
| 4526 | * data to come. The caller knows the session is complete |
| 4527 | * once both states are CLOSED. |
| 4528 | */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 4529 | if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW))) { |
| 4530 | channel_shutr_now(chn); |
| 4531 | channel_shutw_now(chn); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4532 | } |
Willy Tarreau | cce7fa4 | 2010-01-16 23:19:39 +0100 | [diff] [blame] | 4533 | } |
| 4534 | else { |
Willy Tarreau | 4213a11 | 2013-12-15 10:25:42 +0100 | [diff] [blame] | 4535 | /* The last possible modes are keep-alive and tunnel. Tunnel mode |
| 4536 | * will not have any analyser so it needs to poll for reads. |
Willy Tarreau | cce7fa4 | 2010-01-16 23:19:39 +0100 | [diff] [blame] | 4537 | */ |
Willy Tarreau | 4213a11 | 2013-12-15 10:25:42 +0100 | [diff] [blame] | 4538 | if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN) { |
| 4539 | channel_auto_read(chn); |
| 4540 | txn->req.msg_state = HTTP_MSG_TUNNEL; |
| 4541 | chn->flags |= CF_NEVER_WAIT; |
| 4542 | } |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4543 | } |
| 4544 | |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 4545 | if (chn->flags & (CF_SHUTW|CF_SHUTW_NOW)) { |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4546 | /* if we've just closed an output, let's switch */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 4547 | chn->cons->flags |= SI_FL_NOLINGER; /* we want to close ASAP */ |
Willy Tarreau | cce7fa4 | 2010-01-16 23:19:39 +0100 | [diff] [blame] | 4548 | |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 4549 | if (!channel_is_empty(chn)) { |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4550 | txn->req.msg_state = HTTP_MSG_CLOSING; |
| 4551 | goto http_msg_closing; |
| 4552 | } |
| 4553 | else { |
| 4554 | txn->req.msg_state = HTTP_MSG_CLOSED; |
| 4555 | goto http_msg_closed; |
| 4556 | } |
| 4557 | } |
Willy Tarreau | cce7fa4 | 2010-01-16 23:19:39 +0100 | [diff] [blame] | 4558 | goto wait_other_side; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4559 | } |
| 4560 | |
| 4561 | if (txn->req.msg_state == HTTP_MSG_CLOSING) { |
| 4562 | http_msg_closing: |
| 4563 | /* nothing else to forward, just waiting for the output buffer |
| 4564 | * to be empty and for the shutw_now to take effect. |
| 4565 | */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 4566 | if (channel_is_empty(chn)) { |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4567 | txn->req.msg_state = HTTP_MSG_CLOSED; |
| 4568 | goto http_msg_closed; |
| 4569 | } |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 4570 | else if (chn->flags & CF_SHUTW) { |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4571 | txn->req.msg_state = HTTP_MSG_ERROR; |
| 4572 | goto wait_other_side; |
| 4573 | } |
| 4574 | } |
| 4575 | |
| 4576 | if (txn->req.msg_state == HTTP_MSG_CLOSED) { |
| 4577 | http_msg_closed: |
Willy Tarreau | 3988d93 | 2013-12-27 23:03:08 +0100 | [diff] [blame] | 4578 | /* see above in MSG_DONE why we only do this in these states */ |
| 4579 | if (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_SCL) && |
| 4580 | ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) && |
| 4581 | !(s->be->options & PR_O_ABRT_CLOSE)) |
Willy Tarreau | 2e7a165 | 2013-12-15 15:32:10 +0100 | [diff] [blame] | 4582 | channel_dont_read(chn); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4583 | goto wait_other_side; |
| 4584 | } |
| 4585 | |
| 4586 | wait_other_side: |
| 4587 | http_silent_debug(__LINE__, s); |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 4588 | return txn->req.msg_state != old_state || chn->flags != old_flags; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4589 | } |
| 4590 | |
| 4591 | |
| 4592 | /* This function updates the response state machine according to the request |
| 4593 | * state machine and buffer flags. It returns 1 if it changes anything (flag |
| 4594 | * or state), otherwise zero. It ignores any state before HTTP_MSG_DONE, as |
| 4595 | * it is only used to find when a request/response couple is complete. Both |
| 4596 | * this function and its equivalent should loop until both return zero. It |
| 4597 | * can set its own state to DONE, CLOSING, CLOSED, TUNNEL, ERROR. |
| 4598 | */ |
| 4599 | int http_sync_res_state(struct session *s) |
| 4600 | { |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 4601 | struct channel *chn = s->rep; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4602 | struct http_txn *txn = &s->txn; |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 4603 | unsigned int old_flags = chn->flags; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4604 | unsigned int old_state = txn->rsp.msg_state; |
| 4605 | |
| 4606 | http_silent_debug(__LINE__, s); |
| 4607 | if (unlikely(txn->rsp.msg_state < HTTP_MSG_BODY)) |
| 4608 | return 0; |
| 4609 | |
| 4610 | if (txn->rsp.msg_state == HTTP_MSG_DONE) { |
| 4611 | /* In theory, we don't need to read anymore, but we must |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 4612 | * still monitor the server connection for a possible close |
| 4613 | * while the request is being uploaded, so we don't disable |
| 4614 | * reading. |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4615 | */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 4616 | /* channel_dont_read(chn); */ |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4617 | |
| 4618 | if (txn->req.msg_state == HTTP_MSG_ERROR) |
| 4619 | goto wait_other_side; |
| 4620 | |
| 4621 | if (txn->req.msg_state < HTTP_MSG_DONE) { |
| 4622 | /* The client seems to still be sending data, probably |
| 4623 | * because we got an error response during an upload. |
| 4624 | * We have the choice of either breaking the connection |
| 4625 | * or letting it pass through. Let's do the later. |
| 4626 | */ |
| 4627 | goto wait_other_side; |
| 4628 | } |
| 4629 | |
| 4630 | if (txn->req.msg_state == HTTP_MSG_TUNNEL) { |
| 4631 | /* if any side switches to tunnel mode, the other one does too */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 4632 | channel_auto_read(chn); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4633 | txn->rsp.msg_state = HTTP_MSG_TUNNEL; |
Willy Tarreau | fc47f91 | 2012-10-20 10:38:09 +0200 | [diff] [blame] | 4634 | chn->flags |= CF_NEVER_WAIT; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4635 | goto wait_other_side; |
| 4636 | } |
| 4637 | |
| 4638 | /* When we get here, it means that both the request and the |
| 4639 | * response have finished receiving. Depending on the connection |
| 4640 | * mode, we'll have to wait for the last bytes to leave in either |
| 4641 | * direction, and sometimes for a close to be effective. |
| 4642 | */ |
| 4643 | |
| 4644 | if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) { |
| 4645 | /* Server-close mode : shut read and wait for the request |
| 4646 | * side to close its output buffer. The caller will detect |
| 4647 | * when we're in DONE and the other is in CLOSED and will |
| 4648 | * catch that for the final cleanup. |
| 4649 | */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 4650 | if (!(chn->flags & (CF_SHUTR|CF_SHUTR_NOW))) |
| 4651 | channel_shutr_now(chn); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4652 | } |
Willy Tarreau | cce7fa4 | 2010-01-16 23:19:39 +0100 | [diff] [blame] | 4653 | else if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) { |
| 4654 | /* Option forceclose is set, or either side wants to close, |
| 4655 | * let's enforce it now that we're not expecting any new |
| 4656 | * data to come. The caller knows the session is complete |
| 4657 | * once both states are CLOSED. |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4658 | */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 4659 | if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW))) { |
| 4660 | channel_shutr_now(chn); |
| 4661 | channel_shutw_now(chn); |
Willy Tarreau | cce7fa4 | 2010-01-16 23:19:39 +0100 | [diff] [blame] | 4662 | } |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4663 | } |
| 4664 | else { |
Willy Tarreau | 4213a11 | 2013-12-15 10:25:42 +0100 | [diff] [blame] | 4665 | /* The last possible modes are keep-alive and tunnel. Tunnel will |
| 4666 | * need to forward remaining data. Keep-alive will need to monitor |
| 4667 | * for connection closing. |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4668 | */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 4669 | channel_auto_read(chn); |
Willy Tarreau | fc47f91 | 2012-10-20 10:38:09 +0200 | [diff] [blame] | 4670 | chn->flags |= CF_NEVER_WAIT; |
Willy Tarreau | 4213a11 | 2013-12-15 10:25:42 +0100 | [diff] [blame] | 4671 | if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN) |
| 4672 | txn->rsp.msg_state = HTTP_MSG_TUNNEL; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4673 | } |
| 4674 | |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 4675 | if (chn->flags & (CF_SHUTW|CF_SHUTW_NOW)) { |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4676 | /* if we've just closed an output, let's switch */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 4677 | if (!channel_is_empty(chn)) { |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4678 | txn->rsp.msg_state = HTTP_MSG_CLOSING; |
| 4679 | goto http_msg_closing; |
| 4680 | } |
| 4681 | else { |
| 4682 | txn->rsp.msg_state = HTTP_MSG_CLOSED; |
| 4683 | goto http_msg_closed; |
| 4684 | } |
| 4685 | } |
| 4686 | goto wait_other_side; |
| 4687 | } |
| 4688 | |
| 4689 | if (txn->rsp.msg_state == HTTP_MSG_CLOSING) { |
| 4690 | http_msg_closing: |
| 4691 | /* nothing else to forward, just waiting for the output buffer |
| 4692 | * to be empty and for the shutw_now to take effect. |
| 4693 | */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 4694 | if (channel_is_empty(chn)) { |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4695 | txn->rsp.msg_state = HTTP_MSG_CLOSED; |
| 4696 | goto http_msg_closed; |
| 4697 | } |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 4698 | else if (chn->flags & CF_SHUTW) { |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4699 | txn->rsp.msg_state = HTTP_MSG_ERROR; |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 4700 | s->be->be_counters.cli_aborts++; |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 4701 | if (objt_server(s->target)) |
| 4702 | objt_server(s->target)->counters.cli_aborts++; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4703 | goto wait_other_side; |
| 4704 | } |
| 4705 | } |
| 4706 | |
| 4707 | if (txn->rsp.msg_state == HTTP_MSG_CLOSED) { |
| 4708 | http_msg_closed: |
| 4709 | /* drop any pending data */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 4710 | bi_erase(chn); |
| 4711 | channel_auto_close(chn); |
| 4712 | channel_auto_read(chn); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4713 | goto wait_other_side; |
| 4714 | } |
| 4715 | |
| 4716 | wait_other_side: |
| 4717 | http_silent_debug(__LINE__, s); |
Willy Tarreau | fc47f91 | 2012-10-20 10:38:09 +0200 | [diff] [blame] | 4718 | /* We force the response to leave immediately if we're waiting for the |
| 4719 | * other side, since there is no pending shutdown to push it out. |
| 4720 | */ |
| 4721 | if (!channel_is_empty(chn)) |
| 4722 | chn->flags |= CF_SEND_DONTWAIT; |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 4723 | return txn->rsp.msg_state != old_state || chn->flags != old_flags; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4724 | } |
| 4725 | |
| 4726 | |
| 4727 | /* Resync the request and response state machines. Return 1 if either state |
| 4728 | * changes. |
| 4729 | */ |
| 4730 | int http_resync_states(struct session *s) |
| 4731 | { |
| 4732 | struct http_txn *txn = &s->txn; |
| 4733 | int old_req_state = txn->req.msg_state; |
| 4734 | int old_res_state = txn->rsp.msg_state; |
| 4735 | |
| 4736 | http_silent_debug(__LINE__, s); |
| 4737 | http_sync_req_state(s); |
| 4738 | while (1) { |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 4739 | http_silent_debug(__LINE__, s); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4740 | if (!http_sync_res_state(s)) |
| 4741 | break; |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 4742 | http_silent_debug(__LINE__, s); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4743 | if (!http_sync_req_state(s)) |
| 4744 | break; |
| 4745 | } |
| 4746 | http_silent_debug(__LINE__, s); |
| 4747 | /* OK, both state machines agree on a compatible state. |
| 4748 | * There are a few cases we're interested in : |
| 4749 | * - HTTP_MSG_TUNNEL on either means we have to disable both analysers |
| 4750 | * - HTTP_MSG_CLOSED on both sides means we've reached the end in both |
| 4751 | * directions, so let's simply disable both analysers. |
| 4752 | * - HTTP_MSG_CLOSED on the response only means we must abort the |
| 4753 | * request. |
| 4754 | * - HTTP_MSG_CLOSED on the request and HTTP_MSG_DONE on the response |
| 4755 | * with server-close mode means we've completed one request and we |
| 4756 | * must re-initialize the server connection. |
| 4757 | */ |
| 4758 | |
| 4759 | if (txn->req.msg_state == HTTP_MSG_TUNNEL || |
| 4760 | txn->rsp.msg_state == HTTP_MSG_TUNNEL || |
| 4761 | (txn->req.msg_state == HTTP_MSG_CLOSED && |
| 4762 | txn->rsp.msg_state == HTTP_MSG_CLOSED)) { |
| 4763 | s->req->analysers = 0; |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 4764 | channel_auto_close(s->req); |
| 4765 | channel_auto_read(s->req); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4766 | s->rep->analysers = 0; |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 4767 | channel_auto_close(s->rep); |
| 4768 | channel_auto_read(s->rep); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4769 | } |
Willy Tarreau | 40f151a | 2012-12-20 12:10:09 +0100 | [diff] [blame] | 4770 | else if ((txn->req.msg_state >= HTTP_MSG_DONE && |
| 4771 | (txn->rsp.msg_state == HTTP_MSG_CLOSED || (s->rep->flags & CF_SHUTW))) || |
Willy Tarreau | 2fa144c | 2010-01-04 23:13:26 +0100 | [diff] [blame] | 4772 | txn->rsp.msg_state == HTTP_MSG_ERROR || |
Willy Tarreau | 40f151a | 2012-12-20 12:10:09 +0100 | [diff] [blame] | 4773 | txn->req.msg_state == HTTP_MSG_ERROR) { |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 4774 | s->rep->analysers = 0; |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 4775 | channel_auto_close(s->rep); |
| 4776 | channel_auto_read(s->rep); |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 4777 | s->req->analysers = 0; |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 4778 | channel_abort(s->req); |
| 4779 | channel_auto_close(s->req); |
| 4780 | channel_auto_read(s->req); |
Willy Tarreau | 9dab5fc | 2012-05-07 11:56:55 +0200 | [diff] [blame] | 4781 | bi_erase(s->req); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4782 | } |
Willy Tarreau | 4213a11 | 2013-12-15 10:25:42 +0100 | [diff] [blame] | 4783 | else if ((txn->req.msg_state == HTTP_MSG_DONE || |
| 4784 | txn->req.msg_state == HTTP_MSG_CLOSED) && |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4785 | txn->rsp.msg_state == HTTP_MSG_DONE && |
Willy Tarreau | 4213a11 | 2013-12-15 10:25:42 +0100 | [diff] [blame] | 4786 | ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL || |
| 4787 | (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL)) { |
| 4788 | /* server-close/keep-alive: terminate this transaction, |
| 4789 | * possibly killing the server connection and reinitialize |
| 4790 | * a fresh-new transaction. |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4791 | */ |
| 4792 | http_end_txn_clean_session(s); |
| 4793 | } |
| 4794 | |
| 4795 | http_silent_debug(__LINE__, s); |
| 4796 | return txn->req.msg_state != old_req_state || |
| 4797 | txn->rsp.msg_state != old_res_state; |
| 4798 | } |
| 4799 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4800 | /* This function is an analyser which forwards request body (including chunk |
| 4801 | * sizes if any). It is called as soon as we must forward, even if we forward |
| 4802 | * zero byte. The only situation where it must not be called is when we're in |
| 4803 | * tunnel mode and we want to forward till the close. It's used both to forward |
| 4804 | * remaining data and to resync after end of body. It expects the msg_state to |
| 4805 | * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to |
| 4806 | * read more data, or 1 once we can go on with next request or end the session. |
Willy Tarreau | 124d991 | 2011-03-01 20:30:48 +0100 | [diff] [blame] | 4807 | * When in MSG_DATA or MSG_TRAILERS, it will automatically forward chunk_len |
Willy Tarreau | 2692736 | 2012-05-18 23:22:52 +0200 | [diff] [blame] | 4808 | * bytes of pending data + the headers if not already done (between sol and sov). |
| 4809 | * It eventually adjusts sol to match sov after the data in between have been sent. |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4810 | */ |
Willy Tarreau | 7421efb | 2012-07-02 15:11:27 +0200 | [diff] [blame] | 4811 | int http_request_forward_body(struct session *s, struct channel *req, int an_bit) |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4812 | { |
| 4813 | struct http_txn *txn = &s->txn; |
| 4814 | struct http_msg *msg = &s->txn.req; |
| 4815 | |
Willy Tarreau | f5c8bd6 | 2010-01-04 07:10:34 +0100 | [diff] [blame] | 4816 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) |
| 4817 | return 0; |
| 4818 | |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 4819 | 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] | 4820 | ((req->flags & CF_SHUTW) && (req->to_forward || req->buf->o))) { |
Willy Tarreau | 4fe4190 | 2010-06-07 22:27:41 +0200 | [diff] [blame] | 4821 | /* Output closed while we were sending data. We must abort and |
| 4822 | * wake the other side up. |
| 4823 | */ |
| 4824 | msg->msg_state = HTTP_MSG_ERROR; |
| 4825 | http_resync_states(s); |
Willy Tarreau | 082b01c | 2010-01-02 23:58:04 +0100 | [diff] [blame] | 4826 | return 1; |
| 4827 | } |
| 4828 | |
Willy Tarreau | 4fe4190 | 2010-06-07 22:27:41 +0200 | [diff] [blame] | 4829 | /* in most states, we should abort in case of early close */ |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 4830 | channel_auto_close(req); |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4831 | |
| 4832 | /* Note that we don't have to send 100-continue back because we don't |
| 4833 | * need the data to complete our job, and it's up to the server to |
| 4834 | * decide whether to return 100, 417 or anything else in return of |
| 4835 | * an "Expect: 100-continue" header. |
| 4836 | */ |
| 4837 | |
| 4838 | if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) { |
Willy Tarreau | fa4a03c | 2012-03-09 21:28:54 +0100 | [diff] [blame] | 4839 | /* we have msg->sov which points to the first byte of message body. |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 4840 | * req->buf->p still points to the beginning of the message and msg->sol |
Willy Tarreau | 2692736 | 2012-05-18 23:22:52 +0200 | [diff] [blame] | 4841 | * is still null. We must save the body in msg->next because it |
| 4842 | * survives buffer re-alignments. |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4843 | */ |
Willy Tarreau | ea1175a | 2012-03-05 15:52:30 +0100 | [diff] [blame] | 4844 | msg->next = msg->sov; |
Willy Tarreau | a458b67 | 2012-03-05 11:17:50 +0100 | [diff] [blame] | 4845 | |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 4846 | if (msg->flags & HTTP_MSGF_TE_CHNK) |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4847 | msg->msg_state = HTTP_MSG_CHUNK_SIZE; |
Willy Tarreau | 54d23df | 2012-10-25 19:04:45 +0200 | [diff] [blame] | 4848 | else |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4849 | msg->msg_state = HTTP_MSG_DATA; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4850 | } |
| 4851 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4852 | while (1) { |
Willy Tarreau | ea95316 | 2012-05-18 23:41:28 +0200 | [diff] [blame] | 4853 | unsigned int bytes; |
Willy Tarreau | d8ee85a | 2011-03-28 16:06:28 +0200 | [diff] [blame] | 4854 | |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4855 | http_silent_debug(__LINE__, s); |
Willy Tarreau | ea95316 | 2012-05-18 23:41:28 +0200 | [diff] [blame] | 4856 | /* we may have some data pending between sol and sov */ |
Willy Tarreau | 2692736 | 2012-05-18 23:22:52 +0200 | [diff] [blame] | 4857 | bytes = msg->sov - msg->sol; |
Willy Tarreau | d8ee85a | 2011-03-28 16:06:28 +0200 | [diff] [blame] | 4858 | if (msg->chunk_len || bytes) { |
Willy Tarreau | 2692736 | 2012-05-18 23:22:52 +0200 | [diff] [blame] | 4859 | msg->sol = msg->sov; |
Willy Tarreau | a458b67 | 2012-03-05 11:17:50 +0100 | [diff] [blame] | 4860 | msg->next -= bytes; /* will be forwarded */ |
Willy Tarreau | ea95316 | 2012-05-18 23:41:28 +0200 | [diff] [blame] | 4861 | msg->chunk_len += bytes; |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 4862 | msg->chunk_len -= channel_forward(req, msg->chunk_len); |
Willy Tarreau | 638cd02 | 2010-01-03 07:42:04 +0100 | [diff] [blame] | 4863 | } |
Willy Tarreau | 5523b32 | 2009-12-29 12:05:52 +0100 | [diff] [blame] | 4864 | |
Willy Tarreau | caabe41 | 2010-01-03 23:08:28 +0100 | [diff] [blame] | 4865 | if (msg->msg_state == HTTP_MSG_DATA) { |
| 4866 | /* must still forward */ |
| 4867 | if (req->to_forward) |
Willy Tarreau | f5c8bd6 | 2010-01-04 07:10:34 +0100 | [diff] [blame] | 4868 | goto missing_data; |
Willy Tarreau | caabe41 | 2010-01-03 23:08:28 +0100 | [diff] [blame] | 4869 | |
| 4870 | /* nothing left to forward */ |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 4871 | if (msg->flags & HTTP_MSGF_TE_CHNK) |
Willy Tarreau | 54d23df | 2012-10-25 19:04:45 +0200 | [diff] [blame] | 4872 | msg->msg_state = HTTP_MSG_CHUNK_CRLF; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4873 | else |
Willy Tarreau | caabe41 | 2010-01-03 23:08:28 +0100 | [diff] [blame] | 4874 | msg->msg_state = HTTP_MSG_DONE; |
Willy Tarreau | caabe41 | 2010-01-03 23:08:28 +0100 | [diff] [blame] | 4875 | } |
| 4876 | else if (msg->msg_state == HTTP_MSG_CHUNK_SIZE) { |
Willy Tarreau | 124d991 | 2011-03-01 20:30:48 +0100 | [diff] [blame] | 4877 | /* read the chunk size and assign it to ->chunk_len, then |
Willy Tarreau | a458b67 | 2012-03-05 11:17:50 +0100 | [diff] [blame] | 4878 | * 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] | 4879 | * TRAILERS state. |
| 4880 | */ |
Willy Tarreau | 4baf44b | 2012-03-09 14:10:20 +0100 | [diff] [blame] | 4881 | int ret = http_parse_chunk_size(msg); |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4882 | |
Willy Tarreau | 54d23df | 2012-10-25 19:04:45 +0200 | [diff] [blame] | 4883 | if (ret == 0) |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4884 | goto missing_data; |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 4885 | else if (ret < 0) { |
| 4886 | session_inc_http_err_ctr(s); |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 4887 | if (msg->err_pos >= 0) |
Willy Tarreau | 8a0cef2 | 2012-03-09 13:39:23 +0100 | [diff] [blame] | 4888 | http_capture_bad_message(&s->fe->invalid_req, s, msg, HTTP_MSG_CHUNK_SIZE, s->be); |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4889 | goto return_bad_req; |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 4890 | } |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4891 | /* otherwise we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state */ |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4892 | } |
Willy Tarreau | 54d23df | 2012-10-25 19:04:45 +0200 | [diff] [blame] | 4893 | else if (msg->msg_state == HTTP_MSG_CHUNK_CRLF) { |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4894 | /* we want the CRLF after the data */ |
Willy Tarreau | 54d23df | 2012-10-25 19:04:45 +0200 | [diff] [blame] | 4895 | int ret = http_skip_chunk_crlf(msg); |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4896 | |
| 4897 | if (ret == 0) |
| 4898 | goto missing_data; |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 4899 | else if (ret < 0) { |
| 4900 | session_inc_http_err_ctr(s); |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 4901 | if (msg->err_pos >= 0) |
Willy Tarreau | 54d23df | 2012-10-25 19:04:45 +0200 | [diff] [blame] | 4902 | http_capture_bad_message(&s->fe->invalid_req, s, msg, HTTP_MSG_CHUNK_CRLF, s->be); |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4903 | goto return_bad_req; |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 4904 | } |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4905 | /* we're in MSG_CHUNK_SIZE now */ |
| 4906 | } |
| 4907 | else if (msg->msg_state == HTTP_MSG_TRAILERS) { |
Willy Tarreau | 4baf44b | 2012-03-09 14:10:20 +0100 | [diff] [blame] | 4908 | int ret = http_forward_trailers(msg); |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4909 | |
| 4910 | if (ret == 0) |
| 4911 | goto missing_data; |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 4912 | else if (ret < 0) { |
| 4913 | session_inc_http_err_ctr(s); |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 4914 | if (msg->err_pos >= 0) |
Willy Tarreau | 8a0cef2 | 2012-03-09 13:39:23 +0100 | [diff] [blame] | 4915 | http_capture_bad_message(&s->fe->invalid_req, s, msg, HTTP_MSG_TRAILERS, s->be); |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4916 | goto return_bad_req; |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 4917 | } |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4918 | /* we're in HTTP_MSG_DONE now */ |
| 4919 | } |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4920 | else { |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 4921 | int old_state = msg->msg_state; |
| 4922 | |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4923 | /* other states, DONE...TUNNEL */ |
Willy Tarreau | 4fe4190 | 2010-06-07 22:27:41 +0200 | [diff] [blame] | 4924 | /* for keep-alive we don't want to forward closes on DONE */ |
Willy Tarreau | 92aa1fa | 2010-08-28 18:57:20 +0200 | [diff] [blame] | 4925 | if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL || |
| 4926 | (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 4927 | channel_dont_close(req); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4928 | if (http_resync_states(s)) { |
| 4929 | /* some state changes occurred, maybe the analyser |
| 4930 | * was disabled too. |
Willy Tarreau | face839 | 2010-01-03 11:37:54 +0100 | [diff] [blame] | 4931 | */ |
Willy Tarreau | 3fe693b | 2010-12-12 12:50:05 +0100 | [diff] [blame] | 4932 | if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) { |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 4933 | if (req->flags & CF_SHUTW) { |
Willy Tarreau | 3fe693b | 2010-12-12 12:50:05 +0100 | [diff] [blame] | 4934 | /* request errors are most likely due to |
| 4935 | * the server aborting the transfer. |
| 4936 | */ |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 4937 | goto aborted_xfer; |
Willy Tarreau | 3fe693b | 2010-12-12 12:50:05 +0100 | [diff] [blame] | 4938 | } |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 4939 | if (msg->err_pos >= 0) |
Willy Tarreau | 8a0cef2 | 2012-03-09 13:39:23 +0100 | [diff] [blame] | 4940 | http_capture_bad_message(&s->fe->invalid_req, s, msg, old_state, s->be); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4941 | goto return_bad_req; |
Willy Tarreau | 3fe693b | 2010-12-12 12:50:05 +0100 | [diff] [blame] | 4942 | } |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4943 | return 1; |
Willy Tarreau | b608feb | 2010-01-02 22:47:18 +0100 | [diff] [blame] | 4944 | } |
Willy Tarreau | 5c54c71 | 2010-07-17 08:02:58 +0200 | [diff] [blame] | 4945 | |
| 4946 | /* If "option abortonclose" is set on the backend, we |
| 4947 | * want to monitor the client's connection and forward |
| 4948 | * any shutdown notification to the server, which will |
| 4949 | * decide whether to close or to go on processing the |
| 4950 | * request. |
| 4951 | */ |
| 4952 | if (s->be->options & PR_O_ABRT_CLOSE) { |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 4953 | channel_auto_read(req); |
| 4954 | channel_auto_close(req); |
Willy Tarreau | 5c54c71 | 2010-07-17 08:02:58 +0200 | [diff] [blame] | 4955 | } |
Willy Tarreau | 58bd8fd | 2010-09-28 14:16:41 +0200 | [diff] [blame] | 4956 | else if (s->txn.meth == HTTP_METH_POST) { |
| 4957 | /* POST requests may require to read extra CRLF |
| 4958 | * sent by broken browsers and which could cause |
| 4959 | * an RST to be sent upon close on some systems |
| 4960 | * (eg: Linux). |
| 4961 | */ |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 4962 | channel_auto_read(req); |
Willy Tarreau | 58bd8fd | 2010-09-28 14:16:41 +0200 | [diff] [blame] | 4963 | } |
Willy Tarreau | 5c54c71 | 2010-07-17 08:02:58 +0200 | [diff] [blame] | 4964 | |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4965 | return 0; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4966 | } |
| 4967 | } |
| 4968 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4969 | missing_data: |
Willy Tarreau | f5c8bd6 | 2010-01-04 07:10:34 +0100 | [diff] [blame] | 4970 | /* stop waiting for data if the input is closed before the end */ |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 4971 | if (req->flags & CF_SHUTR) { |
Willy Tarreau | 79ebac6 | 2010-06-07 13:47:49 +0200 | [diff] [blame] | 4972 | if (!(s->flags & SN_ERR_MASK)) |
| 4973 | s->flags |= SN_ERR_CLICL; |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 4974 | if (!(s->flags & SN_FINST_MASK)) { |
| 4975 | if (txn->rsp.msg_state < HTTP_MSG_ERROR) |
| 4976 | s->flags |= SN_FINST_H; |
| 4977 | else |
| 4978 | s->flags |= SN_FINST_D; |
| 4979 | } |
| 4980 | |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 4981 | s->fe->fe_counters.cli_aborts++; |
| 4982 | s->be->be_counters.cli_aborts++; |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 4983 | if (objt_server(s->target)) |
| 4984 | objt_server(s->target)->counters.cli_aborts++; |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 4985 | |
| 4986 | goto return_bad_req_stats_ok; |
Willy Tarreau | 79ebac6 | 2010-06-07 13:47:49 +0200 | [diff] [blame] | 4987 | } |
Willy Tarreau | f5c8bd6 | 2010-01-04 07:10:34 +0100 | [diff] [blame] | 4988 | |
Willy Tarreau | f5c8bd6 | 2010-01-04 07:10:34 +0100 | [diff] [blame] | 4989 | /* waiting for the last bits to leave the buffer */ |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 4990 | if (req->flags & CF_SHUTW) |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 4991 | goto aborted_xfer; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4992 | |
Willy Tarreau | 92aa1fa | 2010-08-28 18:57:20 +0200 | [diff] [blame] | 4993 | /* 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] | 4994 | * 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] | 4995 | */ |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 4996 | if (msg->flags & HTTP_MSGF_TE_CHNK) |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 4997 | channel_dont_close(req); |
Willy Tarreau | 92aa1fa | 2010-08-28 18:57:20 +0200 | [diff] [blame] | 4998 | |
Willy Tarreau | 5c62092 | 2011-05-11 19:56:11 +0200 | [diff] [blame] | 4999 | /* 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] | 5000 | * 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] | 5001 | * 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] | 5002 | * modes are already handled by the stream sock layer. We must not do |
| 5003 | * this in content-length mode because it could present the MSG_MORE |
| 5004 | * flag with the last block of forwarded data, which would cause an |
| 5005 | * additional delay to be observed by the receiver. |
Willy Tarreau | 5c62092 | 2011-05-11 19:56:11 +0200 | [diff] [blame] | 5006 | */ |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 5007 | if (msg->flags & HTTP_MSGF_TE_CHNK) |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 5008 | req->flags |= CF_EXPECT_MORE; |
Willy Tarreau | 5c62092 | 2011-05-11 19:56:11 +0200 | [diff] [blame] | 5009 | |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5010 | http_silent_debug(__LINE__, s); |
Willy Tarreau | f5c8bd6 | 2010-01-04 07:10:34 +0100 | [diff] [blame] | 5011 | return 0; |
| 5012 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5013 | return_bad_req: /* let's centralize all bad requests */ |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 5014 | s->fe->fe_counters.failed_req++; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5015 | if (s->listener->counters) |
| 5016 | s->listener->counters->failed_req++; |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 5017 | return_bad_req_stats_ok: |
| 5018 | txn->req.msg_state = HTTP_MSG_ERROR; |
| 5019 | if (txn->status) { |
| 5020 | /* Note: we don't send any error if some data were already sent */ |
| 5021 | stream_int_retnclose(req->prod, NULL); |
| 5022 | } else { |
| 5023 | txn->status = 400; |
Willy Tarreau | 783f258 | 2012-09-04 12:19:04 +0200 | [diff] [blame] | 5024 | stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_400)); |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 5025 | } |
| 5026 | req->analysers = 0; |
| 5027 | s->rep->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] | 5028 | |
| 5029 | if (!(s->flags & SN_ERR_MASK)) |
| 5030 | s->flags |= SN_ERR_PRXCOND; |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 5031 | if (!(s->flags & SN_FINST_MASK)) { |
| 5032 | if (txn->rsp.msg_state < HTTP_MSG_ERROR) |
| 5033 | s->flags |= SN_FINST_H; |
| 5034 | else |
| 5035 | s->flags |= SN_FINST_D; |
| 5036 | } |
| 5037 | return 0; |
| 5038 | |
| 5039 | aborted_xfer: |
| 5040 | txn->req.msg_state = HTTP_MSG_ERROR; |
| 5041 | if (txn->status) { |
| 5042 | /* Note: we don't send any error if some data were already sent */ |
| 5043 | stream_int_retnclose(req->prod, NULL); |
| 5044 | } else { |
| 5045 | txn->status = 502; |
Willy Tarreau | 783f258 | 2012-09-04 12:19:04 +0200 | [diff] [blame] | 5046 | stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_502)); |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 5047 | } |
| 5048 | req->analysers = 0; |
| 5049 | s->rep->analysers = 0; /* we're in data phase, we want to abort both directions */ |
| 5050 | |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 5051 | s->fe->fe_counters.srv_aborts++; |
| 5052 | s->be->be_counters.srv_aborts++; |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 5053 | if (objt_server(s->target)) |
| 5054 | objt_server(s->target)->counters.srv_aborts++; |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 5055 | |
| 5056 | if (!(s->flags & SN_ERR_MASK)) |
| 5057 | s->flags |= SN_ERR_SRVCL; |
| 5058 | if (!(s->flags & SN_FINST_MASK)) { |
| 5059 | if (txn->rsp.msg_state < HTTP_MSG_ERROR) |
| 5060 | s->flags |= SN_FINST_H; |
| 5061 | else |
| 5062 | s->flags |= SN_FINST_D; |
| 5063 | } |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5064 | return 0; |
| 5065 | } |
| 5066 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5067 | /* This stream analyser waits for a complete HTTP response. It returns 1 if the |
| 5068 | * processing can continue on next analysers, or zero if it either needs more |
| 5069 | * data or wants to immediately abort the response (eg: timeout, error, ...). It |
| 5070 | * is tied to AN_RES_WAIT_HTTP and may may remove itself from s->rep->analysers |
| 5071 | * when it has nothing left to do, and may remove any analyser when it wants to |
| 5072 | * abort. |
Willy Tarreau | c65a3ba | 2008-08-11 23:42:50 +0200 | [diff] [blame] | 5073 | */ |
Willy Tarreau | 7421efb | 2012-07-02 15:11:27 +0200 | [diff] [blame] | 5074 | int http_wait_for_response(struct session *s, struct channel *rep, int an_bit) |
Willy Tarreau | c65a3ba | 2008-08-11 23:42:50 +0200 | [diff] [blame] | 5075 | { |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5076 | struct http_txn *txn = &s->txn; |
| 5077 | struct http_msg *msg = &txn->rsp; |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 5078 | struct hdr_ctx ctx; |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 5079 | int use_close_only; |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5080 | int cur_idx; |
Krzysztof Piotr Oledzki | 5fb1882 | 2009-10-13 21:14:09 +0200 | [diff] [blame] | 5081 | int n; |
Willy Tarreau | adfb856 | 2008-08-11 15:24:42 +0200 | [diff] [blame] | 5082 | |
Willy Tarreau | 02d6cfc | 2012-03-01 18:19:58 +0100 | [diff] [blame] | 5083 | DPRINTF(stderr,"[%u] %s: session=%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] | 5084 | now_ms, __FUNCTION__, |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5085 | s, |
Willy Tarreau | 3a16b2c | 2008-08-28 08:54:27 +0200 | [diff] [blame] | 5086 | rep, |
| 5087 | rep->rex, rep->wex, |
| 5088 | rep->flags, |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 5089 | rep->buf->i, |
Willy Tarreau | 3a16b2c | 2008-08-28 08:54:27 +0200 | [diff] [blame] | 5090 | rep->analysers); |
Willy Tarreau | 67f0eea | 2008-08-10 22:55:22 +0200 | [diff] [blame] | 5091 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5092 | /* |
| 5093 | * Now parse the partial (or complete) lines. |
| 5094 | * We will check the response syntax, and also join multi-line |
| 5095 | * headers. An index of all the lines will be elaborated while |
| 5096 | * parsing. |
| 5097 | * |
| 5098 | * For the parsing, we use a 28 states FSM. |
| 5099 | * |
| 5100 | * Here is the information we currently have : |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 5101 | * rep->buf->p = beginning of response |
| 5102 | * rep->buf->p + msg->eoh = end of processed headers / start of current one |
| 5103 | * rep->buf->p + rep->buf->i = end of input data |
Willy Tarreau | 2692736 | 2012-05-18 23:22:52 +0200 | [diff] [blame] | 5104 | * msg->eol = end of current header or line (LF or CRLF) |
| 5105 | * msg->next = first non-visited byte |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5106 | */ |
| 5107 | |
Willy Tarreau | 83e3af0 | 2009-12-28 17:39:57 +0100 | [diff] [blame] | 5108 | /* There's a protected area at the end of the buffer for rewriting |
| 5109 | * purposes. We don't want to start to parse the request if the |
| 5110 | * protected area is affected, because we may have to move processed |
| 5111 | * data later, which is much more complicated. |
| 5112 | */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 5113 | if (buffer_not_empty(rep->buf) && msg->msg_state < HTTP_MSG_ERROR) { |
Willy Tarreau | 379357a | 2013-06-08 12:55:46 +0200 | [diff] [blame] | 5114 | if (unlikely(!channel_reserved(rep))) { |
| 5115 | /* some data has still not left the buffer, wake us once that's done */ |
| 5116 | if (rep->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_WRITE_ERROR|CF_WRITE_TIMEOUT)) |
| 5117 | goto abort_response; |
| 5118 | channel_dont_close(rep); |
| 5119 | rep->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */ |
Willy Tarreau | d7ad9f5 | 2013-12-31 17:26:25 +0100 | [diff] [blame] | 5120 | rep->flags |= CF_WAKE_WRITE; |
Willy Tarreau | 379357a | 2013-06-08 12:55:46 +0200 | [diff] [blame] | 5121 | return 0; |
Willy Tarreau | 83e3af0 | 2009-12-28 17:39:57 +0100 | [diff] [blame] | 5122 | } |
| 5123 | |
Willy Tarreau | 379357a | 2013-06-08 12:55:46 +0200 | [diff] [blame] | 5124 | if (unlikely(bi_end(rep->buf) < b_ptr(rep->buf, msg->next) || |
| 5125 | bi_end(rep->buf) > rep->buf->data + rep->buf->size - global.tune.maxrewrite)) |
| 5126 | buffer_slow_realign(rep->buf); |
| 5127 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 5128 | if (likely(msg->next < rep->buf->i)) |
Willy Tarreau | a560c21 | 2012-03-09 13:50:57 +0100 | [diff] [blame] | 5129 | http_msg_analyzer(msg, &txn->hdr_idx); |
Willy Tarreau | 83e3af0 | 2009-12-28 17:39:57 +0100 | [diff] [blame] | 5130 | } |
| 5131 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5132 | /* 1: we might have to print this header in debug mode */ |
| 5133 | if (unlikely((global.mode & MODE_DEBUG) && |
| 5134 | (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) && |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 5135 | (msg->msg_state >= HTTP_MSG_BODY || msg->msg_state == HTTP_MSG_ERROR))) { |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5136 | char *eol, *sol; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5137 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 5138 | sol = rep->buf->p; |
| 5139 | 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] | 5140 | debug_hdr("srvrep", s, sol, eol); |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5141 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5142 | sol += hdr_idx_first_pos(&txn->hdr_idx); |
| 5143 | cur_idx = hdr_idx_first_idx(&txn->hdr_idx); |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5144 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5145 | while (cur_idx) { |
| 5146 | eol = sol + txn->hdr_idx.v[cur_idx].len; |
| 5147 | debug_hdr("srvhdr", s, sol, eol); |
| 5148 | sol = eol + txn->hdr_idx.v[cur_idx].cr + 1; |
| 5149 | cur_idx = txn->hdr_idx.v[cur_idx].next; |
| 5150 | } |
| 5151 | } |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5152 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5153 | /* |
| 5154 | * Now we quickly check if we have found a full valid response. |
| 5155 | * If not so, we check the FD and buffer states before leaving. |
| 5156 | * A full response is indicated by the fact that we have seen |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 5157 | * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5158 | * responses are checked first. |
| 5159 | * |
| 5160 | * Depending on whether the client is still there or not, we |
| 5161 | * may send an error response back or not. Note that normally |
| 5162 | * we should only check for HTTP status there, and check I/O |
| 5163 | * errors somewhere else. |
| 5164 | */ |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5165 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 5166 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) { |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5167 | /* Invalid response */ |
| 5168 | if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) { |
| 5169 | /* we detected a parsing error. We want to archive this response |
| 5170 | * in the dedicated proxy area for later troubleshooting. |
| 5171 | */ |
| 5172 | hdr_response_bad: |
| 5173 | if (msg->msg_state == HTTP_MSG_ERROR || msg->err_pos >= 0) |
Willy Tarreau | 8a0cef2 | 2012-03-09 13:39:23 +0100 | [diff] [blame] | 5174 | http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, s->fe); |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5175 | |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 5176 | s->be->be_counters.failed_resp++; |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 5177 | if (objt_server(s->target)) { |
| 5178 | objt_server(s->target)->counters.failed_resp++; |
| 5179 | health_adjust(objt_server(s->target), HANA_STATUS_HTTP_HDRRSP); |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 5180 | } |
Willy Tarreau | 6464841 | 2010-03-05 10:41:54 +0100 | [diff] [blame] | 5181 | abort_response: |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 5182 | channel_auto_close(rep); |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5183 | rep->analysers = 0; |
| 5184 | txn->status = 502; |
Willy Tarreau | c88ea68 | 2009-12-29 14:56:36 +0100 | [diff] [blame] | 5185 | rep->prod->flags |= SI_FL_NOLINGER; |
Willy Tarreau | 9dab5fc | 2012-05-07 11:56:55 +0200 | [diff] [blame] | 5186 | bi_erase(rep); |
Willy Tarreau | 783f258 | 2012-09-04 12:19:04 +0200 | [diff] [blame] | 5187 | stream_int_retnclose(rep->cons, http_error_message(s, HTTP_ERR_502)); |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5188 | |
| 5189 | if (!(s->flags & SN_ERR_MASK)) |
| 5190 | s->flags |= SN_ERR_PRXCOND; |
| 5191 | if (!(s->flags & SN_FINST_MASK)) |
| 5192 | s->flags |= SN_FINST_H; |
| 5193 | |
| 5194 | return 0; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 5195 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 5196 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5197 | /* too large response does not fit in buffer. */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 5198 | else if (buffer_full(rep->buf, global.tune.maxrewrite)) { |
Willy Tarreau | fec4d89 | 2011-09-02 20:04:57 +0200 | [diff] [blame] | 5199 | if (msg->err_pos < 0) |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 5200 | msg->err_pos = rep->buf->i; |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5201 | goto hdr_response_bad; |
| 5202 | } |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5203 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5204 | /* read error */ |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 5205 | else if (rep->flags & CF_READ_ERROR) { |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5206 | if (msg->err_pos >= 0) |
Willy Tarreau | 8a0cef2 | 2012-03-09 13:39:23 +0100 | [diff] [blame] | 5207 | http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, s->fe); |
Willy Tarreau | 6b726ad | 2013-12-15 19:31:37 +0100 | [diff] [blame] | 5208 | else if (txn->flags & TX_NOT_FIRST) |
| 5209 | goto abort_keep_alive; |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 5210 | |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 5211 | s->be->be_counters.failed_resp++; |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 5212 | if (objt_server(s->target)) { |
| 5213 | objt_server(s->target)->counters.failed_resp++; |
| 5214 | health_adjust(objt_server(s->target), HANA_STATUS_HTTP_READ_ERROR); |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 5215 | } |
Willy Tarreau | 461f662 | 2008-08-15 23:43:19 +0200 | [diff] [blame] | 5216 | |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 5217 | channel_auto_close(rep); |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5218 | rep->analysers = 0; |
| 5219 | txn->status = 502; |
Willy Tarreau | c88ea68 | 2009-12-29 14:56:36 +0100 | [diff] [blame] | 5220 | rep->prod->flags |= SI_FL_NOLINGER; |
Willy Tarreau | 9dab5fc | 2012-05-07 11:56:55 +0200 | [diff] [blame] | 5221 | bi_erase(rep); |
Willy Tarreau | 783f258 | 2012-09-04 12:19:04 +0200 | [diff] [blame] | 5222 | stream_int_retnclose(rep->cons, http_error_message(s, HTTP_ERR_502)); |
Willy Tarreau | 816b979 | 2009-09-15 21:25:21 +0200 | [diff] [blame] | 5223 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5224 | if (!(s->flags & SN_ERR_MASK)) |
| 5225 | s->flags |= SN_ERR_SRVCL; |
| 5226 | if (!(s->flags & SN_FINST_MASK)) |
| 5227 | s->flags |= SN_FINST_H; |
Willy Tarreau | cebf57e | 2008-08-15 18:16:37 +0200 | [diff] [blame] | 5228 | return 0; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5229 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 5230 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5231 | /* read timeout : return a 504 to the client. */ |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 5232 | else if (rep->flags & CF_READ_TIMEOUT) { |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5233 | if (msg->err_pos >= 0) |
Willy Tarreau | 8a0cef2 | 2012-03-09 13:39:23 +0100 | [diff] [blame] | 5234 | http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, s->fe); |
Willy Tarreau | 6b726ad | 2013-12-15 19:31:37 +0100 | [diff] [blame] | 5235 | else if (txn->flags & TX_NOT_FIRST) |
| 5236 | goto abort_keep_alive; |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 5237 | |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 5238 | s->be->be_counters.failed_resp++; |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 5239 | if (objt_server(s->target)) { |
| 5240 | objt_server(s->target)->counters.failed_resp++; |
| 5241 | health_adjust(objt_server(s->target), HANA_STATUS_HTTP_READ_TIMEOUT); |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 5242 | } |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 5243 | |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 5244 | channel_auto_close(rep); |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5245 | rep->analysers = 0; |
| 5246 | txn->status = 504; |
Willy Tarreau | c88ea68 | 2009-12-29 14:56:36 +0100 | [diff] [blame] | 5247 | rep->prod->flags |= SI_FL_NOLINGER; |
Willy Tarreau | 9dab5fc | 2012-05-07 11:56:55 +0200 | [diff] [blame] | 5248 | bi_erase(rep); |
Willy Tarreau | 783f258 | 2012-09-04 12:19:04 +0200 | [diff] [blame] | 5249 | stream_int_retnclose(rep->cons, http_error_message(s, HTTP_ERR_504)); |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 5250 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5251 | if (!(s->flags & SN_ERR_MASK)) |
| 5252 | s->flags |= SN_ERR_SRVTO; |
| 5253 | if (!(s->flags & SN_FINST_MASK)) |
| 5254 | s->flags |= SN_FINST_H; |
| 5255 | return 0; |
| 5256 | } |
Willy Tarreau | a7c5276 | 2008-08-16 18:40:18 +0200 | [diff] [blame] | 5257 | |
Willy Tarreau | f003d37 | 2012-11-26 13:35:37 +0100 | [diff] [blame] | 5258 | /* client abort with an abortonclose */ |
| 5259 | else if ((rep->flags & CF_SHUTR) && ((s->req->flags & (CF_SHUTR|CF_SHUTW)) == (CF_SHUTR|CF_SHUTW))) { |
| 5260 | s->fe->fe_counters.cli_aborts++; |
| 5261 | s->be->be_counters.cli_aborts++; |
| 5262 | if (objt_server(s->target)) |
| 5263 | objt_server(s->target)->counters.cli_aborts++; |
| 5264 | |
| 5265 | rep->analysers = 0; |
| 5266 | channel_auto_close(rep); |
| 5267 | |
| 5268 | txn->status = 400; |
| 5269 | bi_erase(rep); |
| 5270 | stream_int_retnclose(rep->cons, http_error_message(s, HTTP_ERR_400)); |
| 5271 | |
| 5272 | if (!(s->flags & SN_ERR_MASK)) |
| 5273 | s->flags |= SN_ERR_CLICL; |
| 5274 | if (!(s->flags & SN_FINST_MASK)) |
| 5275 | s->flags |= SN_FINST_H; |
| 5276 | |
| 5277 | /* process_session() will take care of the error */ |
| 5278 | return 0; |
| 5279 | } |
| 5280 | |
Willy Tarreau | 3b8c08a | 2011-09-02 20:16:24 +0200 | [diff] [blame] | 5281 | /* 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] | 5282 | else if (rep->flags & CF_SHUTR) { |
Willy Tarreau | 3b8c08a | 2011-09-02 20:16:24 +0200 | [diff] [blame] | 5283 | if (msg->msg_state >= HTTP_MSG_RPVER || msg->err_pos >= 0) |
Willy Tarreau | 8a0cef2 | 2012-03-09 13:39:23 +0100 | [diff] [blame] | 5284 | http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, s->fe); |
Willy Tarreau | 6b726ad | 2013-12-15 19:31:37 +0100 | [diff] [blame] | 5285 | else if (txn->flags & TX_NOT_FIRST) |
| 5286 | goto abort_keep_alive; |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 5287 | |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 5288 | s->be->be_counters.failed_resp++; |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 5289 | if (objt_server(s->target)) { |
| 5290 | objt_server(s->target)->counters.failed_resp++; |
| 5291 | health_adjust(objt_server(s->target), HANA_STATUS_HTTP_BROKEN_PIPE); |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 5292 | } |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 5293 | |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 5294 | channel_auto_close(rep); |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5295 | rep->analysers = 0; |
| 5296 | txn->status = 502; |
Willy Tarreau | c88ea68 | 2009-12-29 14:56:36 +0100 | [diff] [blame] | 5297 | rep->prod->flags |= SI_FL_NOLINGER; |
Willy Tarreau | 9dab5fc | 2012-05-07 11:56:55 +0200 | [diff] [blame] | 5298 | bi_erase(rep); |
Willy Tarreau | 783f258 | 2012-09-04 12:19:04 +0200 | [diff] [blame] | 5299 | stream_int_retnclose(rep->cons, http_error_message(s, HTTP_ERR_502)); |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 5300 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5301 | if (!(s->flags & SN_ERR_MASK)) |
| 5302 | s->flags |= SN_ERR_SRVCL; |
| 5303 | if (!(s->flags & SN_FINST_MASK)) |
| 5304 | s->flags |= SN_FINST_H; |
| 5305 | return 0; |
| 5306 | } |
Krzysztof Piotr Oledzki | 5fb1882 | 2009-10-13 21:14:09 +0200 | [diff] [blame] | 5307 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5308 | /* write error to client (we don't send any message then) */ |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 5309 | else if (rep->flags & CF_WRITE_ERROR) { |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5310 | if (msg->err_pos >= 0) |
Willy Tarreau | 8a0cef2 | 2012-03-09 13:39:23 +0100 | [diff] [blame] | 5311 | http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, s->fe); |
Willy Tarreau | 6b726ad | 2013-12-15 19:31:37 +0100 | [diff] [blame] | 5312 | else if (txn->flags & TX_NOT_FIRST) |
| 5313 | goto abort_keep_alive; |
Krzysztof Piotr Oledzki | 5fb1882 | 2009-10-13 21:14:09 +0200 | [diff] [blame] | 5314 | |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 5315 | s->be->be_counters.failed_resp++; |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5316 | rep->analysers = 0; |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 5317 | channel_auto_close(rep); |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5318 | |
| 5319 | if (!(s->flags & SN_ERR_MASK)) |
| 5320 | s->flags |= SN_ERR_CLICL; |
| 5321 | if (!(s->flags & SN_FINST_MASK)) |
| 5322 | s->flags |= SN_FINST_H; |
| 5323 | |
| 5324 | /* process_session() will take care of the error */ |
| 5325 | return 0; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5326 | } |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 5327 | |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 5328 | channel_dont_close(rep); |
Willy Tarreau | 3f3997e | 2013-12-15 15:21:32 +0100 | [diff] [blame] | 5329 | rep->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */ |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5330 | return 0; |
| 5331 | } |
| 5332 | |
| 5333 | /* More interesting part now : we know that we have a complete |
| 5334 | * response which at least looks like HTTP. We have an indicator |
| 5335 | * of each header's length, so we can parse them quickly. |
| 5336 | */ |
| 5337 | |
| 5338 | if (unlikely(msg->err_pos >= 0)) |
Willy Tarreau | 8a0cef2 | 2012-03-09 13:39:23 +0100 | [diff] [blame] | 5339 | http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, s->fe); |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5340 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5341 | /* |
| 5342 | * 1: get the status code |
| 5343 | */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 5344 | n = rep->buf->p[msg->sl.st.c] - '0'; |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5345 | if (n < 1 || n > 5) |
| 5346 | n = 0; |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 5347 | /* when the client triggers a 4xx from the server, it's most often due |
| 5348 | * to a missing object or permission. These events should be tracked |
| 5349 | * because if they happen often, it may indicate a brute force or a |
| 5350 | * vulnerability scan. |
| 5351 | */ |
| 5352 | if (n == 4) |
| 5353 | session_inc_http_err_ctr(s); |
| 5354 | |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 5355 | if (objt_server(s->target)) |
| 5356 | objt_server(s->target)->counters.p.http.rsp[n]++; |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5357 | |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 5358 | /* check if the response is HTTP/1.1 or above */ |
| 5359 | if ((msg->sl.st.v_l == 8) && |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 5360 | ((rep->buf->p[5] > '1') || |
| 5361 | ((rep->buf->p[5] == '1') && (rep->buf->p[7] >= '1')))) |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 5362 | msg->flags |= HTTP_MSGF_VER_11; |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 5363 | |
| 5364 | /* "connection" has not been parsed yet */ |
Willy Tarreau | 50fc777 | 2012-11-11 22:19:57 +0100 | [diff] [blame] | 5365 | 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] | 5366 | |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 5367 | /* transfer length unknown*/ |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 5368 | msg->flags &= ~HTTP_MSGF_XFER_LEN; |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 5369 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 5370 | 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] | 5371 | |
Willy Tarreau | 3965040 | 2010-03-15 19:44:39 +0100 | [diff] [blame] | 5372 | /* Adjust server's health based on status code. Note: status codes 501 |
| 5373 | * and 505 are triggered on demand by client request, so we must not |
| 5374 | * count them as server failures. |
| 5375 | */ |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 5376 | if (objt_server(s->target)) { |
Willy Tarreau | d45b3d5 | 2010-05-20 11:49:03 +0200 | [diff] [blame] | 5377 | 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] | 5378 | health_adjust(objt_server(s->target), HANA_STATUS_HTTP_OK); |
Willy Tarreau | d45b3d5 | 2010-05-20 11:49:03 +0200 | [diff] [blame] | 5379 | else |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 5380 | health_adjust(objt_server(s->target), HANA_STATUS_HTTP_STS); |
Willy Tarreau | d45b3d5 | 2010-05-20 11:49:03 +0200 | [diff] [blame] | 5381 | } |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 5382 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5383 | /* |
| 5384 | * 2: check for cacheability. |
| 5385 | */ |
| 5386 | |
| 5387 | switch (txn->status) { |
| 5388 | case 200: |
| 5389 | case 203: |
| 5390 | case 206: |
| 5391 | case 300: |
| 5392 | case 301: |
| 5393 | case 410: |
| 5394 | /* RFC2616 @13.4: |
| 5395 | * "A response received with a status code of |
| 5396 | * 200, 203, 206, 300, 301 or 410 MAY be stored |
| 5397 | * by a cache (...) unless a cache-control |
| 5398 | * directive prohibits caching." |
| 5399 | * |
| 5400 | * RFC2616 @9.5: POST method : |
| 5401 | * "Responses to this method are not cacheable, |
| 5402 | * unless the response includes appropriate |
| 5403 | * Cache-Control or Expires header fields." |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5404 | */ |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5405 | if (likely(txn->meth != HTTP_METH_POST) && |
Willy Tarreau | 6740213 | 2012-05-31 20:40:20 +0200 | [diff] [blame] | 5406 | ((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] | 5407 | txn->flags |= TX_CACHEABLE | TX_CACHE_COOK; |
| 5408 | break; |
| 5409 | default: |
| 5410 | break; |
| 5411 | } |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5412 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5413 | /* |
| 5414 | * 3: we may need to capture headers |
| 5415 | */ |
| 5416 | s->logs.logwait &= ~LW_RESP; |
Willy Tarreau | 42f7d89 | 2012-03-24 08:28:09 +0100 | [diff] [blame] | 5417 | if (unlikely((s->logs.logwait & LW_RSPHDR) && txn->rsp.cap)) |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 5418 | capture_headers(rep->buf->p, &txn->hdr_idx, |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5419 | txn->rsp.cap, s->fe->rsp_cap); |
| 5420 | |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 5421 | /* 4: determine the transfer-length. |
| 5422 | * According to RFC2616 #4.4, amended by the HTTPbis working group, |
| 5423 | * the presence of a message-body in a RESPONSE and its transfer length |
| 5424 | * must be determined that way : |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 5425 | * |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 5426 | * All responses to the HEAD request method MUST NOT include a |
| 5427 | * message-body, even though the presence of entity-header fields |
| 5428 | * might lead one to believe they do. All 1xx (informational), 204 |
| 5429 | * (No Content), and 304 (Not Modified) responses MUST NOT include a |
| 5430 | * message-body. All other responses do include a message-body, |
| 5431 | * although it MAY be of zero length. |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 5432 | * |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 5433 | * 1. Any response which "MUST NOT" include a message-body (such as the |
| 5434 | * 1xx, 204 and 304 responses and any response to a HEAD request) is |
| 5435 | * always terminated by the first empty line after the header fields, |
| 5436 | * regardless of the entity-header fields present in the message. |
| 5437 | * |
| 5438 | * 2. If a Transfer-Encoding header field (Section 9.7) is present and |
| 5439 | * the "chunked" transfer-coding (Section 6.2) is used, the |
| 5440 | * transfer-length is defined by the use of this transfer-coding. |
| 5441 | * If a Transfer-Encoding header field is present and the "chunked" |
| 5442 | * transfer-coding is not present, the transfer-length is defined by |
| 5443 | * the sender closing the connection. |
| 5444 | * |
| 5445 | * 3. If a Content-Length header field is present, its decimal value in |
| 5446 | * OCTETs represents both the entity-length and the transfer-length. |
| 5447 | * If a message is received with both a Transfer-Encoding header |
| 5448 | * field and a Content-Length header field, the latter MUST be ignored. |
| 5449 | * |
| 5450 | * 4. If the message uses the media type "multipart/byteranges", and |
| 5451 | * the transfer-length is not otherwise specified, then this self- |
| 5452 | * delimiting media type defines the transfer-length. This media |
| 5453 | * type MUST NOT be used unless the sender knows that the recipient |
| 5454 | * can parse it; the presence in a request of a Range header with |
| 5455 | * multiple byte-range specifiers from a 1.1 client implies that the |
| 5456 | * client can parse multipart/byteranges responses. |
| 5457 | * |
| 5458 | * 5. By the server closing the connection. |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 5459 | */ |
| 5460 | |
| 5461 | /* Skip parsing if no content length is possible. The response flags |
Willy Tarreau | 124d991 | 2011-03-01 20:30:48 +0100 | [diff] [blame] | 5462 | * 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] | 5463 | * 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] | 5464 | * FIXME: should we parse anyway and return an error on chunked encoding ? |
| 5465 | */ |
| 5466 | if (txn->meth == HTTP_METH_HEAD || |
| 5467 | (txn->status >= 100 && txn->status < 200) || |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 5468 | txn->status == 204 || txn->status == 304) { |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 5469 | msg->flags |= HTTP_MSGF_XFER_LEN; |
Willy Tarreau | 9101535 | 2012-11-27 07:31:33 +0100 | [diff] [blame] | 5470 | s->comp_algo = NULL; |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 5471 | goto skip_content_length; |
| 5472 | } |
| 5473 | |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 5474 | use_close_only = 0; |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 5475 | ctx.idx = 0; |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 5476 | while ((msg->flags & HTTP_MSGF_VER_11) && |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 5477 | 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] | 5478 | if (ctx.vlen == 7 && strncasecmp(ctx.line + ctx.val, "chunked", 7) == 0) |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 5479 | msg->flags |= (HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN); |
| 5480 | else if (msg->flags & HTTP_MSGF_TE_CHNK) { |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 5481 | /* bad transfer-encoding (chunked followed by something else) */ |
| 5482 | use_close_only = 1; |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 5483 | msg->flags &= ~(HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN); |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 5484 | break; |
| 5485 | } |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 5486 | } |
| 5487 | |
| 5488 | /* FIXME: below we should remove the content-length header(s) in case of chunked encoding */ |
| 5489 | ctx.idx = 0; |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 5490 | while (!(msg->flags & HTTP_MSGF_TE_CHNK) && !use_close_only && |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 5491 | 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] | 5492 | signed long long cl; |
| 5493 | |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 5494 | if (!ctx.vlen) { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 5495 | msg->err_pos = ctx.line + ctx.val - rep->buf->p; |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 5496 | goto hdr_response_bad; |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 5497 | } |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 5498 | |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 5499 | if (strl2llrc(ctx.line + ctx.val, ctx.vlen, &cl)) { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 5500 | msg->err_pos = ctx.line + ctx.val - rep->buf->p; |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 5501 | goto hdr_response_bad; /* parse failure */ |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 5502 | } |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 5503 | |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 5504 | if (cl < 0) { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 5505 | msg->err_pos = ctx.line + ctx.val - rep->buf->p; |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 5506 | goto hdr_response_bad; |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 5507 | } |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 5508 | |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 5509 | if ((msg->flags & HTTP_MSGF_CNT_LEN) && (msg->chunk_len != cl)) { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 5510 | msg->err_pos = ctx.line + ctx.val - rep->buf->p; |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 5511 | goto hdr_response_bad; /* already specified, was different */ |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 5512 | } |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 5513 | |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 5514 | msg->flags |= HTTP_MSGF_CNT_LEN | HTTP_MSGF_XFER_LEN; |
Willy Tarreau | 124d991 | 2011-03-01 20:30:48 +0100 | [diff] [blame] | 5515 | msg->body_len = msg->chunk_len = cl; |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 5516 | } |
| 5517 | |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 5518 | if (s->fe->comp || s->be->comp) |
| 5519 | select_compression_response_header(s, rep->buf); |
| 5520 | |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 5521 | /* FIXME: we should also implement the multipart/byterange method. |
| 5522 | * For now on, we resort to close mode in this case (unknown length). |
| 5523 | */ |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 5524 | skip_content_length: |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 5525 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5526 | /* end of job, return OK */ |
| 5527 | rep->analysers &= ~an_bit; |
| 5528 | rep->analyse_exp = TICK_ETERNITY; |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 5529 | channel_auto_close(rep); |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5530 | return 1; |
Willy Tarreau | 6b726ad | 2013-12-15 19:31:37 +0100 | [diff] [blame] | 5531 | |
| 5532 | abort_keep_alive: |
| 5533 | /* A keep-alive request to the server failed on a network error. |
| 5534 | * The client is required to retry. We need to close without returning |
| 5535 | * any other information so that the client retries. |
| 5536 | */ |
| 5537 | txn->status = 0; |
| 5538 | rep->analysers = 0; |
| 5539 | s->req->analysers = 0; |
| 5540 | channel_auto_close(rep); |
| 5541 | s->logs.logwait = 0; |
| 5542 | s->logs.level = 0; |
| 5543 | s->rep->flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */ |
| 5544 | bi_erase(rep); |
| 5545 | stream_int_retnclose(rep->cons, NULL); |
| 5546 | return 0; |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5547 | } |
| 5548 | |
| 5549 | /* This function performs all the processing enabled for the current response. |
Willy Tarreau | e3fa6e5 | 2010-01-04 22:57:43 +0100 | [diff] [blame] | 5550 | * It normally returns 1 unless it wants to break. It relies on buffers flags, |
| 5551 | * and updates t->rep->analysers. It might make sense to explode it into several |
| 5552 | * other functions. It works like process_request (see indications above). |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5553 | */ |
Willy Tarreau | 7421efb | 2012-07-02 15:11:27 +0200 | [diff] [blame] | 5554 | int http_process_res_common(struct session *t, struct channel *rep, int an_bit, struct proxy *px) |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5555 | { |
| 5556 | struct http_txn *txn = &t->txn; |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5557 | struct http_msg *msg = &txn->rsp; |
| 5558 | struct proxy *cur_proxy; |
Willy Tarreau | f4f0412 | 2010-01-28 18:10:50 +0100 | [diff] [blame] | 5559 | struct cond_wordlist *wl; |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 5560 | struct http_res_rule *http_res_last_rule = NULL; |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5561 | |
Willy Tarreau | 02d6cfc | 2012-03-01 18:19:58 +0100 | [diff] [blame] | 5562 | DPRINTF(stderr,"[%u] %s: session=%p b=%p, exp(r,w)=%u,%u bf=%08x bh=%d analysers=%02x\n", |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5563 | now_ms, __FUNCTION__, |
| 5564 | t, |
| 5565 | rep, |
| 5566 | rep->rex, rep->wex, |
| 5567 | rep->flags, |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 5568 | rep->buf->i, |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5569 | rep->analysers); |
| 5570 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 5571 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) /* we need more data */ |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5572 | return 0; |
| 5573 | |
| 5574 | rep->analysers &= ~an_bit; |
| 5575 | rep->analyse_exp = TICK_ETERNITY; |
| 5576 | |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 5577 | /* Now we have to check if we need to modify the Connection header. |
| 5578 | * This is more difficult on the response than it is on the request, |
| 5579 | * because we can have two different HTTP versions and we don't know |
| 5580 | * how the client will interprete a response. For instance, let's say |
| 5581 | * that the client sends a keep-alive request in HTTP/1.0 and gets an |
| 5582 | * HTTP/1.1 response without any header. Maybe it will bound itself to |
| 5583 | * HTTP/1.0 because it only knows about it, and will consider the lack |
| 5584 | * of header as a close, or maybe it knows HTTP/1.1 and can consider |
| 5585 | * the lack of header as a keep-alive. Thus we will use two flags |
| 5586 | * indicating how a request MAY be understood by the client. In case |
| 5587 | * of multiple possibilities, we'll fix the header to be explicit. If |
| 5588 | * ambiguous cases such as both close and keepalive are seen, then we |
| 5589 | * will fall back to explicit close. Note that we won't take risks with |
| 5590 | * HTTP/1.0 clients which may not necessarily understand keep-alive. |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 5591 | * See doc/internals/connection-header.txt for the complete matrix. |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 5592 | */ |
| 5593 | |
Willy Tarreau | dc008c5 | 2010-02-01 16:20:08 +0100 | [diff] [blame] | 5594 | if (unlikely((txn->meth == HTTP_METH_CONNECT && txn->status == 200) || |
| 5595 | txn->status == 101)) { |
| 5596 | /* Either we've established an explicit tunnel, or we're |
| 5597 | * switching the protocol. In both cases, we're very unlikely |
Willy Tarreau | 5843d1a | 2010-02-01 15:13:32 +0100 | [diff] [blame] | 5598 | * to understand the next protocols. We have to switch to tunnel |
| 5599 | * mode, so that we transfer the request and responses then let |
| 5600 | * this protocol pass unmodified. When we later implement specific |
| 5601 | * parsers for such protocols, we'll want to check the Upgrade |
Willy Tarreau | dc008c5 | 2010-02-01 16:20:08 +0100 | [diff] [blame] | 5602 | * header which contains information about that protocol for |
| 5603 | * responses with status 101 (eg: see RFC2817 about TLS). |
Willy Tarreau | 5843d1a | 2010-02-01 15:13:32 +0100 | [diff] [blame] | 5604 | */ |
| 5605 | txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_TUN; |
| 5606 | } |
Willy Tarreau | dc008c5 | 2010-02-01 16:20:08 +0100 | [diff] [blame] | 5607 | else if ((txn->status >= 200) && !(txn->flags & TX_HDR_CONN_PRS) && |
| 5608 | ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN || |
| 5609 | ((t->fe->options|t->be->options) & PR_O_HTTP_CLOSE))) { |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 5610 | int to_del = 0; |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 5611 | |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 5612 | /* on unknown transfer length, we must close */ |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 5613 | if (!(msg->flags & HTTP_MSGF_XFER_LEN) && |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 5614 | (txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) |
| 5615 | txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO; |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 5616 | |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 5617 | /* now adjust header transformations depending on current state */ |
| 5618 | if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN || |
| 5619 | (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) { |
| 5620 | to_del |= 2; /* remove "keep-alive" on any response */ |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 5621 | if (!(msg->flags & HTTP_MSGF_VER_11)) |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 5622 | to_del |= 1; /* remove "close" for HTTP/1.0 responses */ |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 5623 | } |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 5624 | else { /* SCL / KAL */ |
| 5625 | to_del |= 1; /* remove "close" on any response */ |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 5626 | if (txn->req.flags & msg->flags & HTTP_MSGF_VER_11) |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 5627 | to_del |= 2; /* remove "keep-alive" on pure 1.1 responses */ |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 5628 | } |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 5629 | |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 5630 | /* Parse and remove some headers from the connection header */ |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 5631 | http_parse_connection_header(txn, msg, to_del); |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 5632 | |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 5633 | /* Some keep-alive responses are converted to Server-close if |
| 5634 | * the server wants to close. |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 5635 | */ |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 5636 | if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL) { |
| 5637 | if ((txn->flags & TX_HDR_CONN_CLO) || |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 5638 | (!(txn->flags & TX_HDR_CONN_KAL) && !(msg->flags & HTTP_MSGF_VER_11))) |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 5639 | txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_SCL; |
Willy Tarreau | b608feb | 2010-01-02 22:47:18 +0100 | [diff] [blame] | 5640 | } |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 5641 | } |
| 5642 | |
Willy Tarreau | 7959a55 | 2013-09-23 16:44:27 +0200 | [diff] [blame] | 5643 | /* we want to have the response time before we start processing it */ |
| 5644 | t->logs.t_data = tv_ms_elapsed(&t->logs.tv_accept, &now); |
| 5645 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5646 | if (1) { |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5647 | /* |
| 5648 | * 3: we will have to evaluate the filters. |
| 5649 | * As opposed to version 1.2, now they will be evaluated in the |
| 5650 | * filters order and not in the header order. This means that |
| 5651 | * each filter has to be validated among all headers. |
| 5652 | * |
| 5653 | * Filters are tried with ->be first, then with ->fe if it is |
| 5654 | * different from ->be. |
| 5655 | */ |
| 5656 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5657 | cur_proxy = t->be; |
| 5658 | while (1) { |
| 5659 | struct proxy *rule_set = cur_proxy; |
| 5660 | |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 5661 | /* evaluate http-response rules */ |
| 5662 | if (!http_res_last_rule) |
| 5663 | http_res_last_rule = http_res_get_intercept_rule(cur_proxy, &cur_proxy->http_res_rules, t, txn); |
| 5664 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5665 | /* try headers filters */ |
| 5666 | if (rule_set->rsp_exp != NULL) { |
Willy Tarreau | fdb563c | 2010-01-31 15:43:27 +0100 | [diff] [blame] | 5667 | if (apply_filters_to_response(t, rep, rule_set) < 0) { |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5668 | return_bad_resp: |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 5669 | if (objt_server(t->target)) { |
| 5670 | objt_server(t->target)->counters.failed_resp++; |
| 5671 | health_adjust(objt_server(t->target), HANA_STATUS_HTTP_RSP); |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 5672 | } |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 5673 | t->be->be_counters.failed_resp++; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5674 | return_srv_prx_502: |
Willy Tarreau | 2df28e8 | 2008-08-17 15:20:19 +0200 | [diff] [blame] | 5675 | rep->analysers = 0; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5676 | txn->status = 502; |
Willy Tarreau | 7959a55 | 2013-09-23 16:44:27 +0200 | [diff] [blame] | 5677 | t->logs.t_data = -1; /* was not a valid response */ |
Willy Tarreau | c88ea68 | 2009-12-29 14:56:36 +0100 | [diff] [blame] | 5678 | rep->prod->flags |= SI_FL_NOLINGER; |
Willy Tarreau | 9dab5fc | 2012-05-07 11:56:55 +0200 | [diff] [blame] | 5679 | bi_erase(rep); |
Willy Tarreau | 783f258 | 2012-09-04 12:19:04 +0200 | [diff] [blame] | 5680 | stream_int_retnclose(rep->cons, http_error_message(t, HTTP_ERR_502)); |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5681 | if (!(t->flags & SN_ERR_MASK)) |
| 5682 | t->flags |= SN_ERR_PRXCOND; |
| 5683 | if (!(t->flags & SN_FINST_MASK)) |
| 5684 | t->flags |= SN_FINST_H; |
Willy Tarreau | dafde43 | 2008-08-17 01:00:46 +0200 | [diff] [blame] | 5685 | return 0; |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 5686 | } |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5687 | } |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 5688 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5689 | /* has the response been denied ? */ |
| 5690 | if (txn->flags & TX_SVDENY) { |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 5691 | if (objt_server(t->target)) |
| 5692 | objt_server(t->target)->counters.failed_secu++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 5693 | |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 5694 | t->be->be_counters.denied_resp++; |
| 5695 | t->fe->fe_counters.denied_resp++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 5696 | if (t->listener->counters) |
| 5697 | t->listener->counters->denied_resp++; |
| 5698 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5699 | goto return_srv_prx_502; |
Willy Tarreau | 5140623 | 2008-03-10 22:04:20 +0100 | [diff] [blame] | 5700 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 5701 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5702 | /* add response headers from the rule sets in the same order */ |
Willy Tarreau | deb9ed8 | 2010-01-03 21:03:22 +0100 | [diff] [blame] | 5703 | list_for_each_entry(wl, &rule_set->rsp_add, list) { |
Willy Tarreau | 816b979 | 2009-09-15 21:25:21 +0200 | [diff] [blame] | 5704 | if (txn->status < 200) |
| 5705 | break; |
Willy Tarreau | fdb563c | 2010-01-31 15:43:27 +0100 | [diff] [blame] | 5706 | if (wl->cond) { |
Willy Tarreau | 32a6f2e | 2012-04-25 10:13:36 +0200 | [diff] [blame] | 5707 | int ret = acl_exec_cond(wl->cond, px, t, txn, SMP_OPT_DIR_RES|SMP_OPT_FINAL); |
Willy Tarreau | fdb563c | 2010-01-31 15:43:27 +0100 | [diff] [blame] | 5708 | ret = acl_pass(ret); |
| 5709 | if (((struct acl_cond *)wl->cond)->pol == ACL_COND_UNLESS) |
| 5710 | ret = !ret; |
| 5711 | if (!ret) |
| 5712 | continue; |
| 5713 | } |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 5714 | if (unlikely(http_header_add_tail(&txn->rsp, &txn->hdr_idx, wl->s) < 0)) |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5715 | goto return_bad_resp; |
Willy Tarreau | 0bbc3cf | 2006-10-15 14:26:02 +0200 | [diff] [blame] | 5716 | } |
| 5717 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5718 | /* check whether we're already working on the frontend */ |
| 5719 | if (cur_proxy == t->fe) |
| 5720 | break; |
| 5721 | cur_proxy = t->fe; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 5722 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 5723 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5724 | /* |
Willy Tarreau | 5843d1a | 2010-02-01 15:13:32 +0100 | [diff] [blame] | 5725 | * We may be facing a 100-continue response, in which case this |
| 5726 | * is not the right response, and we're waiting for the next one. |
| 5727 | * Let's allow this response to go to the client and wait for the |
Willy Tarreau | 63c9e5f | 2009-12-22 16:01:27 +0100 | [diff] [blame] | 5728 | * next one. |
| 5729 | */ |
Willy Tarreau | 5843d1a | 2010-02-01 15:13:32 +0100 | [diff] [blame] | 5730 | if (unlikely(txn->status == 100)) { |
Willy Tarreau | 63c9e5f | 2009-12-22 16:01:27 +0100 | [diff] [blame] | 5731 | hdr_idx_init(&txn->hdr_idx); |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 5732 | msg->next -= channel_forward(rep, msg->next); |
Willy Tarreau | 63c9e5f | 2009-12-22 16:01:27 +0100 | [diff] [blame] | 5733 | msg->msg_state = HTTP_MSG_RPBEFORE; |
| 5734 | txn->status = 0; |
Willy Tarreau | 7959a55 | 2013-09-23 16:44:27 +0200 | [diff] [blame] | 5735 | t->logs.t_data = -1; /* was not a response yet */ |
Willy Tarreau | 63c9e5f | 2009-12-22 16:01:27 +0100 | [diff] [blame] | 5736 | rep->analysers |= AN_RES_WAIT_HTTP | an_bit; |
| 5737 | return 1; |
| 5738 | } |
Willy Tarreau | 5843d1a | 2010-02-01 15:13:32 +0100 | [diff] [blame] | 5739 | else if (unlikely(txn->status < 200)) |
| 5740 | goto skip_header_mangling; |
Willy Tarreau | 63c9e5f | 2009-12-22 16:01:27 +0100 | [diff] [blame] | 5741 | |
| 5742 | /* we don't have any 1xx status code now */ |
| 5743 | |
| 5744 | /* |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5745 | * 4: check for server cookie. |
| 5746 | */ |
Willy Tarreau | 63c9e5f | 2009-12-22 16:01:27 +0100 | [diff] [blame] | 5747 | if (t->be->cookie_name || t->be->appsession_name || t->fe->capture_name || |
| 5748 | (t->be->options & PR_O_CHK_CACHE)) |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5749 | manage_server_side_cookies(t, rep); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 5750 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 5751 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5752 | /* |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5753 | * 5: check for cache-control or pragma headers if required. |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5754 | */ |
Willy Tarreau | 6740213 | 2012-05-31 20:40:20 +0200 | [diff] [blame] | 5755 | if ((t->be->options & PR_O_CHK_CACHE) || (t->be->ck_opts & PR_CK_NOC)) |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5756 | check_response_for_cacheability(t, rep); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5757 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5758 | /* |
| 5759 | * 6: add server cookie in the response if needed |
| 5760 | */ |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 5761 | if (objt_server(t->target) && (t->be->ck_opts & PR_CK_INS) && |
Willy Tarreau | 6740213 | 2012-05-31 20:40:20 +0200 | [diff] [blame] | 5762 | !((txn->flags & TX_SCK_FOUND) && (t->be->ck_opts & PR_CK_PSV)) && |
Willy Tarreau | ef4f391 | 2010-10-07 21:00:29 +0200 | [diff] [blame] | 5763 | (!(t->flags & SN_DIRECT) || |
| 5764 | ((t->be->cookie_maxidle || txn->cookie_last_date) && |
| 5765 | (!txn->cookie_last_date || (txn->cookie_last_date - date.tv_sec) < 0)) || |
| 5766 | (t->be->cookie_maxlife && !txn->cookie_first_date) || // set the first_date |
| 5767 | (!t->be->cookie_maxlife && txn->cookie_first_date)) && // remove the first_date |
Willy Tarreau | 6740213 | 2012-05-31 20:40:20 +0200 | [diff] [blame] | 5768 | (!(t->be->ck_opts & PR_CK_POST) || (txn->meth == HTTP_METH_POST)) && |
Cyril Bonté | 47fdd8e | 2010-04-25 00:00:51 +0200 | [diff] [blame] | 5769 | !(t->flags & SN_IGNORE_PRST)) { |
Willy Tarreau | ef4f391 | 2010-10-07 21:00:29 +0200 | [diff] [blame] | 5770 | /* the server is known, it's not the one the client requested, or the |
| 5771 | * cookie's last seen date needs to be refreshed. We have to |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5772 | * insert a set-cookie here, except if we want to insert only on POST |
| 5773 | * requests and this one isn't. Note that servers which don't have cookies |
| 5774 | * (eg: some backup servers) will return a full cookie removal request. |
| 5775 | */ |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 5776 | if (!objt_server(t->target)->cookie) { |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 5777 | chunk_printf(&trash, |
Willy Tarreau | ef4f391 | 2010-10-07 21:00:29 +0200 | [diff] [blame] | 5778 | "Set-Cookie: %s=; Expires=Thu, 01-Jan-1970 00:00:01 GMT; path=/", |
| 5779 | t->be->cookie_name); |
| 5780 | } |
| 5781 | else { |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 5782 | chunk_printf(&trash, "Set-Cookie: %s=%s", t->be->cookie_name, objt_server(t->target)->cookie); |
Willy Tarreau | ef4f391 | 2010-10-07 21:00:29 +0200 | [diff] [blame] | 5783 | |
| 5784 | if (t->be->cookie_maxidle || t->be->cookie_maxlife) { |
| 5785 | /* emit last_date, which is mandatory */ |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 5786 | trash.str[trash.len++] = COOKIE_DELIM_DATE; |
| 5787 | s30tob64((date.tv_sec+3) >> 2, trash.str + trash.len); |
| 5788 | trash.len += 5; |
| 5789 | |
Willy Tarreau | ef4f391 | 2010-10-07 21:00:29 +0200 | [diff] [blame] | 5790 | if (t->be->cookie_maxlife) { |
| 5791 | /* emit first_date, which is either the original one or |
| 5792 | * the current date. |
| 5793 | */ |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 5794 | trash.str[trash.len++] = COOKIE_DELIM_DATE; |
Willy Tarreau | ef4f391 | 2010-10-07 21:00:29 +0200 | [diff] [blame] | 5795 | s30tob64(txn->cookie_first_date ? |
| 5796 | txn->cookie_first_date >> 2 : |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 5797 | (date.tv_sec+3) >> 2, trash.str + trash.len); |
| 5798 | trash.len += 5; |
Willy Tarreau | ef4f391 | 2010-10-07 21:00:29 +0200 | [diff] [blame] | 5799 | } |
| 5800 | } |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 5801 | chunk_appendf(&trash, "; path=/"); |
Willy Tarreau | ef4f391 | 2010-10-07 21:00:29 +0200 | [diff] [blame] | 5802 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 5803 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5804 | if (t->be->cookie_domain) |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 5805 | chunk_appendf(&trash, "; domain=%s", t->be->cookie_domain); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 5806 | |
Willy Tarreau | 4992dd2 | 2012-05-31 21:02:17 +0200 | [diff] [blame] | 5807 | if (t->be->ck_opts & PR_CK_HTTPONLY) |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 5808 | chunk_appendf(&trash, "; HttpOnly"); |
Willy Tarreau | 4992dd2 | 2012-05-31 21:02:17 +0200 | [diff] [blame] | 5809 | |
| 5810 | if (t->be->ck_opts & PR_CK_SECURE) |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 5811 | chunk_appendf(&trash, "; Secure"); |
Willy Tarreau | 4992dd2 | 2012-05-31 21:02:17 +0200 | [diff] [blame] | 5812 | |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 5813 | if (unlikely(http_header_add_tail2(&txn->rsp, &txn->hdr_idx, trash.str, trash.len) < 0)) |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5814 | goto return_bad_resp; |
Willy Tarreau | ef4f391 | 2010-10-07 21:00:29 +0200 | [diff] [blame] | 5815 | |
Willy Tarreau | f134831 | 2010-10-07 15:54:11 +0200 | [diff] [blame] | 5816 | txn->flags &= ~TX_SCK_MASK; |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 5817 | if (objt_server(t->target)->cookie && (t->flags & SN_DIRECT)) |
Willy Tarreau | ef4f391 | 2010-10-07 21:00:29 +0200 | [diff] [blame] | 5818 | /* the server did not change, only the date was updated */ |
| 5819 | txn->flags |= TX_SCK_UPDATED; |
| 5820 | else |
| 5821 | txn->flags |= TX_SCK_INSERTED; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 5822 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5823 | /* Here, we will tell an eventual cache on the client side that we don't |
| 5824 | * want it to cache this reply because HTTP/1.0 caches also cache cookies ! |
| 5825 | * Some caches understand the correct form: 'no-cache="set-cookie"', but |
| 5826 | * others don't (eg: apache <= 1.3.26). So we use 'private' instead. |
| 5827 | */ |
Willy Tarreau | 6740213 | 2012-05-31 20:40:20 +0200 | [diff] [blame] | 5828 | if ((t->be->ck_opts & PR_CK_NOC) && (txn->flags & TX_CACHEABLE)) { |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 5829 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5830 | txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK; |
| 5831 | |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 5832 | if (unlikely(http_header_add_tail2(&txn->rsp, &txn->hdr_idx, |
Willy Tarreau | 58cc872 | 2009-12-28 06:57:33 +0100 | [diff] [blame] | 5833 | "Cache-control: private", 22) < 0)) |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5834 | goto return_bad_resp; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5835 | } |
| 5836 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 5837 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5838 | /* |
| 5839 | * 7: check if result will be cacheable with a cookie. |
| 5840 | * We'll block the response if security checks have caught |
| 5841 | * nasty things such as a cacheable cookie. |
| 5842 | */ |
Willy Tarreau | f134831 | 2010-10-07 15:54:11 +0200 | [diff] [blame] | 5843 | if (((txn->flags & (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) == |
| 5844 | (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) && |
Willy Tarreau | 63c9e5f | 2009-12-22 16:01:27 +0100 | [diff] [blame] | 5845 | (t->be->options & PR_O_CHK_CACHE)) { |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5846 | |
| 5847 | /* we're in presence of a cacheable response containing |
| 5848 | * a set-cookie header. We'll block it as requested by |
| 5849 | * the 'checkcache' option, and send an alert. |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5850 | */ |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 5851 | if (objt_server(t->target)) |
| 5852 | objt_server(t->target)->counters.failed_secu++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 5853 | |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 5854 | t->be->be_counters.denied_resp++; |
| 5855 | t->fe->fe_counters.denied_resp++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 5856 | if (t->listener->counters) |
| 5857 | t->listener->counters->denied_resp++; |
| 5858 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5859 | Alert("Blocking cacheable cookie in response from instance %s, server %s.\n", |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 5860 | t->be->id, objt_server(t->target) ? objt_server(t->target)->id : "<dispatch>"); |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5861 | send_log(t->be, LOG_ALERT, |
| 5862 | "Blocking cacheable cookie in response from instance %s, server %s.\n", |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 5863 | t->be->id, objt_server(t->target) ? objt_server(t->target)->id : "<dispatch>"); |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5864 | goto return_srv_prx_502; |
| 5865 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5866 | |
| 5867 | /* |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 5868 | * 8: adjust "Connection: close" or "Connection: keep-alive" if needed. |
Willy Tarreau | 50fc777 | 2012-11-11 22:19:57 +0100 | [diff] [blame] | 5869 | * If an "Upgrade" token is found, the header is left untouched in order |
| 5870 | * not to have to deal with some client bugs : some of them fail an upgrade |
| 5871 | * if anything but "Upgrade" is present in the Connection header. |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5872 | */ |
Willy Tarreau | 50fc777 | 2012-11-11 22:19:57 +0100 | [diff] [blame] | 5873 | if (!(txn->flags & TX_HDR_CONN_UPG) && |
| 5874 | (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) || |
| 5875 | ((t->fe->options|t->be->options) & PR_O_HTTP_CLOSE))) { |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 5876 | unsigned int want_flags = 0; |
| 5877 | |
| 5878 | if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL || |
| 5879 | (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) { |
| 5880 | /* we want a keep-alive response here. Keep-alive header |
| 5881 | * required if either side is not 1.1. |
| 5882 | */ |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 5883 | if (!(txn->req.flags & msg->flags & HTTP_MSGF_VER_11)) |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 5884 | want_flags |= TX_CON_KAL_SET; |
| 5885 | } |
| 5886 | else { |
| 5887 | /* we want a close response here. Close header required if |
| 5888 | * the server is 1.1, regardless of the client. |
| 5889 | */ |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 5890 | if (msg->flags & HTTP_MSGF_VER_11) |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 5891 | want_flags |= TX_CON_CLO_SET; |
| 5892 | } |
| 5893 | |
| 5894 | 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] | 5895 | http_change_connection_header(txn, msg, want_flags); |
Willy Tarreau | b608feb | 2010-01-02 22:47:18 +0100 | [diff] [blame] | 5896 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5897 | |
Willy Tarreau | 5843d1a | 2010-02-01 15:13:32 +0100 | [diff] [blame] | 5898 | skip_header_mangling: |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 5899 | if ((msg->flags & HTTP_MSGF_XFER_LEN) || |
Willy Tarreau | dc008c5 | 2010-02-01 16:20:08 +0100 | [diff] [blame] | 5900 | (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN) |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5901 | rep->analysers |= AN_RES_HTTP_XFER_BODY; |
Willy Tarreau | 0394594 | 2009-12-22 16:50:27 +0100 | [diff] [blame] | 5902 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5903 | /************************************************************* |
| 5904 | * OK, that's finished for the headers. We have done what we * |
| 5905 | * could. Let's switch to the DATA state. * |
| 5906 | ************************************************************/ |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 5907 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5908 | /* if the user wants to log as soon as possible, without counting |
| 5909 | * bytes from the server, then this is the right moment. We have |
| 5910 | * to temporarily assign bytes_out to log what we currently have. |
| 5911 | */ |
Willy Tarreau | d79a3b2 | 2012-12-28 09:40:16 +0100 | [diff] [blame] | 5912 | if (!LIST_ISEMPTY(&t->fe->logformat) && !(t->logs.logwait & LW_BYTES)) { |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5913 | t->logs.t_close = t->logs.t_data; /* to get a valid end date */ |
| 5914 | t->logs.bytes_out = txn->rsp.eoh; |
Willy Tarreau | a5555ec | 2008-11-30 19:02:32 +0100 | [diff] [blame] | 5915 | t->do_log(t); |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5916 | t->logs.bytes_out = 0; |
| 5917 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5918 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5919 | /* Note: we must not try to cheat by jumping directly to DATA, |
| 5920 | * otherwise we would not let the client side wake up. |
| 5921 | */ |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5922 | |
Willy Tarreau | e3fa6e5 | 2010-01-04 22:57:43 +0100 | [diff] [blame] | 5923 | return 1; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5924 | } |
Willy Tarreau | e3fa6e5 | 2010-01-04 22:57:43 +0100 | [diff] [blame] | 5925 | return 1; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5926 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5927 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5928 | /* This function is an analyser which forwards response body (including chunk |
| 5929 | * sizes if any). It is called as soon as we must forward, even if we forward |
| 5930 | * zero byte. The only situation where it must not be called is when we're in |
| 5931 | * tunnel mode and we want to forward till the close. It's used both to forward |
| 5932 | * remaining data and to resync after end of body. It expects the msg_state to |
| 5933 | * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to |
| 5934 | * read more data, or 1 once we can go on with next request or end the session. |
Willy Tarreau | 124d991 | 2011-03-01 20:30:48 +0100 | [diff] [blame] | 5935 | * When in MSG_DATA or MSG_TRAILERS, it will automatically forward chunk_len |
Willy Tarreau | 2692736 | 2012-05-18 23:22:52 +0200 | [diff] [blame] | 5936 | * bytes of pending data + the headers if not already done (between sol and sov). |
| 5937 | * It eventually adjusts sol to match sov after the data in between have been sent. |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5938 | */ |
Willy Tarreau | 7421efb | 2012-07-02 15:11:27 +0200 | [diff] [blame] | 5939 | int http_response_forward_body(struct session *s, struct channel *res, int an_bit) |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5940 | { |
| 5941 | struct http_txn *txn = &s->txn; |
| 5942 | struct http_msg *msg = &s->txn.rsp; |
Willy Tarreau | ea95316 | 2012-05-18 23:41:28 +0200 | [diff] [blame] | 5943 | unsigned int bytes; |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 5944 | static struct buffer *tmpbuf = NULL; |
| 5945 | int compressing = 0; |
William Lallemand | bf3ae61 | 2012-11-19 12:35:37 +0100 | [diff] [blame] | 5946 | int consumed_data = 0; |
Willy Tarreau | d655ffe | 2013-04-02 01:48:58 +0200 | [diff] [blame] | 5947 | int ret; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5948 | |
Willy Tarreau | f5c8bd6 | 2010-01-04 07:10:34 +0100 | [diff] [blame] | 5949 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) |
| 5950 | return 0; |
| 5951 | |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 5952 | 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] | 5953 | ((res->flags & CF_SHUTW) && (res->to_forward || res->buf->o)) || |
Willy Tarreau | 6c2cbe1 | 2010-01-03 17:07:49 +0100 | [diff] [blame] | 5954 | !s->req->analysers) { |
Willy Tarreau | 4fe4190 | 2010-06-07 22:27:41 +0200 | [diff] [blame] | 5955 | /* Output closed while we were sending data. We must abort and |
| 5956 | * wake the other side up. |
| 5957 | */ |
| 5958 | msg->msg_state = HTTP_MSG_ERROR; |
| 5959 | http_resync_states(s); |
Willy Tarreau | 082b01c | 2010-01-02 23:58:04 +0100 | [diff] [blame] | 5960 | return 1; |
| 5961 | } |
| 5962 | |
Willy Tarreau | 4fe4190 | 2010-06-07 22:27:41 +0200 | [diff] [blame] | 5963 | /* in most states, we should abort in case of early close */ |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 5964 | channel_auto_close(res); |
Willy Tarreau | b608feb | 2010-01-02 22:47:18 +0100 | [diff] [blame] | 5965 | |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 5966 | /* this is the first time we need the compression buffer */ |
| 5967 | if (s->comp_algo != NULL && tmpbuf == NULL) { |
| 5968 | if ((tmpbuf = pool_alloc2(pool2_buffer)) == NULL) |
| 5969 | goto aborted_xfer; /* no memory */ |
| 5970 | } |
| 5971 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5972 | if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) { |
Willy Tarreau | fa4a03c | 2012-03-09 21:28:54 +0100 | [diff] [blame] | 5973 | /* we have msg->sov which points to the first byte of message body. |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 5974 | * rep->buf.p still points to the beginning of the message and msg->sol |
| 5975 | * is still null. We forward the headers, we don't need them. |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5976 | */ |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 5977 | channel_forward(res, msg->sov); |
| 5978 | msg->next = 0; |
| 5979 | msg->sov = 0; |
Willy Tarreau | a458b67 | 2012-03-05 11:17:50 +0100 | [diff] [blame] | 5980 | |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 5981 | if (msg->flags & HTTP_MSGF_TE_CHNK) |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5982 | msg->msg_state = HTTP_MSG_CHUNK_SIZE; |
Willy Tarreau | 54d23df | 2012-10-25 19:04:45 +0200 | [diff] [blame] | 5983 | else |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5984 | msg->msg_state = HTTP_MSG_DATA; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5985 | } |
| 5986 | |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 5987 | if (s->comp_algo != NULL) { |
Willy Tarreau | d655ffe | 2013-04-02 01:48:58 +0200 | [diff] [blame] | 5988 | ret = http_compression_buffer_init(s, res->buf, tmpbuf); /* init a buffer with headers */ |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 5989 | if (ret < 0) |
| 5990 | goto missing_data; /* not enough spaces in buffers */ |
| 5991 | compressing = 1; |
| 5992 | } |
| 5993 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5994 | while (1) { |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5995 | http_silent_debug(__LINE__, s); |
Willy Tarreau | ea95316 | 2012-05-18 23:41:28 +0200 | [diff] [blame] | 5996 | /* we may have some data pending between sol and sov */ |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 5997 | if (s->comp_algo == NULL) { |
| 5998 | bytes = msg->sov - msg->sol; |
| 5999 | if (msg->chunk_len || bytes) { |
| 6000 | msg->sol = msg->sov; |
| 6001 | msg->next -= bytes; /* will be forwarded */ |
| 6002 | msg->chunk_len += bytes; |
| 6003 | msg->chunk_len -= channel_forward(res, msg->chunk_len); |
| 6004 | } |
Willy Tarreau | 638cd02 | 2010-01-03 07:42:04 +0100 | [diff] [blame] | 6005 | } |
| 6006 | |
Willy Tarreau | d655ffe | 2013-04-02 01:48:58 +0200 | [diff] [blame] | 6007 | switch (msg->msg_state - HTTP_MSG_DATA) { |
| 6008 | case HTTP_MSG_DATA - HTTP_MSG_DATA: /* must still forward */ |
William Lallemand | bf3ae61 | 2012-11-19 12:35:37 +0100 | [diff] [blame] | 6009 | if (compressing) { |
| 6010 | consumed_data += ret = http_compression_buffer_add_data(s, res->buf, tmpbuf); |
| 6011 | if (ret < 0) |
| 6012 | goto aborted_xfer; |
| 6013 | } |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 6014 | |
| 6015 | if (res->to_forward || msg->chunk_len) |
Willy Tarreau | f5c8bd6 | 2010-01-04 07:10:34 +0100 | [diff] [blame] | 6016 | goto missing_data; |
Willy Tarreau | caabe41 | 2010-01-03 23:08:28 +0100 | [diff] [blame] | 6017 | |
| 6018 | /* nothing left to forward */ |
William Lallemand | bf3ae61 | 2012-11-19 12:35:37 +0100 | [diff] [blame] | 6019 | if (msg->flags & HTTP_MSGF_TE_CHNK) { |
Willy Tarreau | 54d23df | 2012-10-25 19:04:45 +0200 | [diff] [blame] | 6020 | msg->msg_state = HTTP_MSG_CHUNK_CRLF; |
William Lallemand | bf3ae61 | 2012-11-19 12:35:37 +0100 | [diff] [blame] | 6021 | } else { |
Willy Tarreau | caabe41 | 2010-01-03 23:08:28 +0100 | [diff] [blame] | 6022 | msg->msg_state = HTTP_MSG_DONE; |
William Lallemand | bf3ae61 | 2012-11-19 12:35:37 +0100 | [diff] [blame] | 6023 | if (compressing && consumed_data) { |
| 6024 | http_compression_buffer_end(s, &res->buf, &tmpbuf, 1); |
| 6025 | compressing = 0; |
| 6026 | } |
Willy Tarreau | d655ffe | 2013-04-02 01:48:58 +0200 | [diff] [blame] | 6027 | break; |
William Lallemand | bf3ae61 | 2012-11-19 12:35:37 +0100 | [diff] [blame] | 6028 | } |
Willy Tarreau | d655ffe | 2013-04-02 01:48:58 +0200 | [diff] [blame] | 6029 | /* fall through for HTTP_MSG_CHUNK_CRLF */ |
| 6030 | |
| 6031 | case HTTP_MSG_CHUNK_CRLF - HTTP_MSG_DATA: |
| 6032 | /* we want the CRLF after the data */ |
| 6033 | |
| 6034 | ret = http_skip_chunk_crlf(msg); |
| 6035 | if (ret == 0) |
| 6036 | goto missing_data; |
| 6037 | else if (ret < 0) { |
| 6038 | if (msg->err_pos >= 0) |
| 6039 | http_capture_bad_message(&s->be->invalid_rep, s, msg, HTTP_MSG_CHUNK_CRLF, s->fe); |
| 6040 | goto return_bad_res; |
| 6041 | } |
| 6042 | /* skipping data in buffer for compression */ |
| 6043 | if (compressing) { |
| 6044 | b_adv(res->buf, msg->next); |
| 6045 | msg->next = 0; |
| 6046 | msg->sov = 0; |
| 6047 | msg->sol = 0; |
| 6048 | } |
| 6049 | /* we're in MSG_CHUNK_SIZE now, fall through */ |
| 6050 | |
| 6051 | case HTTP_MSG_CHUNK_SIZE - HTTP_MSG_DATA: |
Willy Tarreau | 124d991 | 2011-03-01 20:30:48 +0100 | [diff] [blame] | 6052 | /* read the chunk size and assign it to ->chunk_len, then |
Willy Tarreau | a458b67 | 2012-03-05 11:17:50 +0100 | [diff] [blame] | 6053 | * set ->sov and ->next to point to the body and switch to DATA or |
| 6054 | * TRAILERS state. |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 6055 | */ |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 6056 | |
Willy Tarreau | d655ffe | 2013-04-02 01:48:58 +0200 | [diff] [blame] | 6057 | ret = http_parse_chunk_size(msg); |
Willy Tarreau | 54d23df | 2012-10-25 19:04:45 +0200 | [diff] [blame] | 6058 | if (ret == 0) |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 6059 | goto missing_data; |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 6060 | else if (ret < 0) { |
| 6061 | if (msg->err_pos >= 0) |
Willy Tarreau | 8a0cef2 | 2012-03-09 13:39:23 +0100 | [diff] [blame] | 6062 | http_capture_bad_message(&s->be->invalid_rep, s, msg, HTTP_MSG_CHUNK_SIZE, s->fe); |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 6063 | goto return_bad_res; |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 6064 | } |
William Lallemand | bf3ae61 | 2012-11-19 12:35:37 +0100 | [diff] [blame] | 6065 | if (compressing) { |
| 6066 | if (likely(msg->chunk_len > 0)) { |
| 6067 | /* skipping data if we are in compression mode */ |
| 6068 | b_adv(res->buf, msg->next); |
| 6069 | msg->next = 0; |
| 6070 | msg->sov = 0; |
| 6071 | msg->sol = 0; |
| 6072 | } else { |
| 6073 | if (consumed_data) { |
| 6074 | http_compression_buffer_end(s, &res->buf, &tmpbuf, 1); |
| 6075 | compressing = 0; |
| 6076 | } |
| 6077 | } |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 6078 | } |
Willy Tarreau | 0161d62 | 2013-04-02 01:26:55 +0200 | [diff] [blame] | 6079 | /* otherwise we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state */ |
Willy Tarreau | d655ffe | 2013-04-02 01:48:58 +0200 | [diff] [blame] | 6080 | break; |
Willy Tarreau | 5523b32 | 2009-12-29 12:05:52 +0100 | [diff] [blame] | 6081 | |
Willy Tarreau | d655ffe | 2013-04-02 01:48:58 +0200 | [diff] [blame] | 6082 | case HTTP_MSG_TRAILERS - HTTP_MSG_DATA: |
| 6083 | ret = http_forward_trailers(msg); |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 6084 | if (ret == 0) |
| 6085 | goto missing_data; |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 6086 | else if (ret < 0) { |
| 6087 | if (msg->err_pos >= 0) |
Willy Tarreau | 8a0cef2 | 2012-03-09 13:39:23 +0100 | [diff] [blame] | 6088 | http_capture_bad_message(&s->be->invalid_rep, s, msg, HTTP_MSG_TRAILERS, s->fe); |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 6089 | goto return_bad_res; |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 6090 | } |
William Lallemand | 00bf1de | 2012-11-22 17:55:14 +0100 | [diff] [blame] | 6091 | if (s->comp_algo != NULL) { |
| 6092 | /* forwarding trailers */ |
| 6093 | channel_forward(res, msg->next); |
| 6094 | msg->next = 0; |
| 6095 | } |
Willy Tarreau | 2d43e18 | 2013-04-03 00:22:25 +0200 | [diff] [blame] | 6096 | /* we're in HTTP_MSG_DONE now, but we might still have |
| 6097 | * some data pending, so let's loop over once. |
| 6098 | */ |
| 6099 | break; |
Willy Tarreau | d655ffe | 2013-04-02 01:48:58 +0200 | [diff] [blame] | 6100 | |
| 6101 | default: |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 6102 | /* other states, DONE...TUNNEL */ |
Willy Tarreau | d655ffe | 2013-04-02 01:48:58 +0200 | [diff] [blame] | 6103 | |
| 6104 | ret = msg->msg_state; |
Willy Tarreau | 4fe4190 | 2010-06-07 22:27:41 +0200 | [diff] [blame] | 6105 | /* for keep-alive we don't want to forward closes on DONE */ |
Willy Tarreau | 92aa1fa | 2010-08-28 18:57:20 +0200 | [diff] [blame] | 6106 | if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL || |
| 6107 | (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 6108 | channel_dont_close(res); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 6109 | if (http_resync_states(s)) { |
| 6110 | http_silent_debug(__LINE__, s); |
| 6111 | /* some state changes occurred, maybe the analyser |
| 6112 | * was disabled too. |
Willy Tarreau | 5523b32 | 2009-12-29 12:05:52 +0100 | [diff] [blame] | 6113 | */ |
Willy Tarreau | 3fe693b | 2010-12-12 12:50:05 +0100 | [diff] [blame] | 6114 | if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) { |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 6115 | if (res->flags & CF_SHUTW) { |
Willy Tarreau | 3fe693b | 2010-12-12 12:50:05 +0100 | [diff] [blame] | 6116 | /* response errors are most likely due to |
| 6117 | * the client aborting the transfer. |
| 6118 | */ |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 6119 | goto aborted_xfer; |
Willy Tarreau | 3fe693b | 2010-12-12 12:50:05 +0100 | [diff] [blame] | 6120 | } |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 6121 | if (msg->err_pos >= 0) |
Willy Tarreau | d655ffe | 2013-04-02 01:48:58 +0200 | [diff] [blame] | 6122 | http_capture_bad_message(&s->be->invalid_rep, s, msg, ret, s->fe); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 6123 | goto return_bad_res; |
Willy Tarreau | 3fe693b | 2010-12-12 12:50:05 +0100 | [diff] [blame] | 6124 | } |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 6125 | return 1; |
Willy Tarreau | 5523b32 | 2009-12-29 12:05:52 +0100 | [diff] [blame] | 6126 | } |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 6127 | return 0; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 6128 | } |
| 6129 | } |
| 6130 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 6131 | missing_data: |
William Lallemand | bf3ae61 | 2012-11-19 12:35:37 +0100 | [diff] [blame] | 6132 | if (compressing && consumed_data) { |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 6133 | http_compression_buffer_end(s, &res->buf, &tmpbuf, 0); |
| 6134 | compressing = 0; |
| 6135 | } |
Willy Tarreau | f003d37 | 2012-11-26 13:35:37 +0100 | [diff] [blame] | 6136 | |
| 6137 | if (res->flags & CF_SHUTW) |
| 6138 | goto aborted_xfer; |
| 6139 | |
| 6140 | /* stop waiting for data if the input is closed before the end. If the |
| 6141 | * client side was already closed, it means that the client has aborted, |
| 6142 | * so we don't want to count this as a server abort. Otherwise it's a |
| 6143 | * server abort. |
| 6144 | */ |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 6145 | if (res->flags & CF_SHUTR) { |
Willy Tarreau | f003d37 | 2012-11-26 13:35:37 +0100 | [diff] [blame] | 6146 | if ((res->flags & CF_SHUTW_NOW) || (s->req->flags & CF_SHUTR)) |
| 6147 | goto aborted_xfer; |
Willy Tarreau | 40dba09 | 2010-03-04 18:14:51 +0100 | [diff] [blame] | 6148 | if (!(s->flags & SN_ERR_MASK)) |
| 6149 | s->flags |= SN_ERR_SRVCL; |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 6150 | s->be->be_counters.srv_aborts++; |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 6151 | if (objt_server(s->target)) |
| 6152 | objt_server(s->target)->counters.srv_aborts++; |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 6153 | goto return_bad_res_stats_ok; |
Willy Tarreau | 40dba09 | 2010-03-04 18:14:51 +0100 | [diff] [blame] | 6154 | } |
Willy Tarreau | f5c8bd6 | 2010-01-04 07:10:34 +0100 | [diff] [blame] | 6155 | |
Willy Tarreau | 40dba09 | 2010-03-04 18:14:51 +0100 | [diff] [blame] | 6156 | /* we need to obey the req analyser, so if it leaves, we must too */ |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 6157 | if (!s->req->analysers) |
| 6158 | goto return_bad_res; |
| 6159 | |
Willy Tarreau | ea95316 | 2012-05-18 23:41:28 +0200 | [diff] [blame] | 6160 | /* forward any data pending between sol and sov */ |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 6161 | if (s->comp_algo == NULL) { |
| 6162 | bytes = msg->sov - msg->sol; |
| 6163 | if (msg->chunk_len || bytes) { |
| 6164 | msg->sol = msg->sov; |
| 6165 | msg->next -= bytes; /* will be forwarded */ |
| 6166 | msg->chunk_len += bytes; |
| 6167 | msg->chunk_len -= channel_forward(res, msg->chunk_len); |
| 6168 | } |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 6169 | } |
| 6170 | |
Willy Tarreau | 92aa1fa | 2010-08-28 18:57:20 +0200 | [diff] [blame] | 6171 | /* 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] | 6172 | * 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] | 6173 | * Similarly, with keep-alive on the client side, we don't want to forward a |
| 6174 | * close. |
| 6175 | */ |
Willy Tarreau | 08b4d79 | 2012-10-27 01:36:34 +0200 | [diff] [blame] | 6176 | if ((msg->flags & HTTP_MSGF_TE_CHNK) || s->comp_algo || |
Willy Tarreau | 92aa1fa | 2010-08-28 18:57:20 +0200 | [diff] [blame] | 6177 | (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL || |
| 6178 | (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 6179 | channel_dont_close(res); |
Willy Tarreau | 92aa1fa | 2010-08-28 18:57:20 +0200 | [diff] [blame] | 6180 | |
Willy Tarreau | 5c62092 | 2011-05-11 19:56:11 +0200 | [diff] [blame] | 6181 | /* 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] | 6182 | * 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] | 6183 | * 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] | 6184 | * modes are already handled by the stream sock layer. We must not do |
| 6185 | * this in content-length mode because it could present the MSG_MORE |
| 6186 | * flag with the last block of forwarded data, which would cause an |
| 6187 | * additional delay to be observed by the receiver. |
Willy Tarreau | 5c62092 | 2011-05-11 19:56:11 +0200 | [diff] [blame] | 6188 | */ |
Willy Tarreau | 08b4d79 | 2012-10-27 01:36:34 +0200 | [diff] [blame] | 6189 | if ((msg->flags & HTTP_MSGF_TE_CHNK) || s->comp_algo) |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 6190 | res->flags |= CF_EXPECT_MORE; |
Willy Tarreau | 5c62092 | 2011-05-11 19:56:11 +0200 | [diff] [blame] | 6191 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 6192 | /* the session handler will take care of timeouts and errors */ |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 6193 | http_silent_debug(__LINE__, s); |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 6194 | return 0; |
| 6195 | |
Willy Tarreau | 40dba09 | 2010-03-04 18:14:51 +0100 | [diff] [blame] | 6196 | return_bad_res: /* let's centralize all bad responses */ |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 6197 | s->be->be_counters.failed_resp++; |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 6198 | if (objt_server(s->target)) |
| 6199 | objt_server(s->target)->counters.failed_resp++; |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 6200 | |
| 6201 | return_bad_res_stats_ok: |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 6202 | txn->rsp.msg_state = HTTP_MSG_ERROR; |
Willy Tarreau | 148d099 | 2010-01-10 10:21:21 +0100 | [diff] [blame] | 6203 | /* don't send any error message as we're in the body */ |
| 6204 | stream_int_retnclose(res->cons, NULL); |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 6205 | res->analysers = 0; |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 6206 | 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] | 6207 | if (objt_server(s->target)) |
| 6208 | health_adjust(objt_server(s->target), HANA_STATUS_HTTP_HDRRSP); |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 6209 | |
| 6210 | if (!(s->flags & SN_ERR_MASK)) |
| 6211 | s->flags |= SN_ERR_PRXCOND; |
| 6212 | if (!(s->flags & SN_FINST_MASK)) |
Willy Tarreau | 40dba09 | 2010-03-04 18:14:51 +0100 | [diff] [blame] | 6213 | s->flags |= SN_FINST_D; |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 6214 | return 0; |
| 6215 | |
| 6216 | aborted_xfer: |
| 6217 | txn->rsp.msg_state = HTTP_MSG_ERROR; |
| 6218 | /* don't send any error message as we're in the body */ |
| 6219 | stream_int_retnclose(res->cons, NULL); |
| 6220 | res->analysers = 0; |
| 6221 | s->req->analysers = 0; /* we're in data phase, we want to abort both directions */ |
| 6222 | |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 6223 | s->fe->fe_counters.cli_aborts++; |
| 6224 | s->be->be_counters.cli_aborts++; |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 6225 | if (objt_server(s->target)) |
| 6226 | objt_server(s->target)->counters.cli_aborts++; |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 6227 | |
| 6228 | if (!(s->flags & SN_ERR_MASK)) |
| 6229 | s->flags |= SN_ERR_CLICL; |
| 6230 | if (!(s->flags & SN_FINST_MASK)) |
| 6231 | s->flags |= SN_FINST_D; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 6232 | return 0; |
| 6233 | } |
| 6234 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6235 | /* Iterate the same filter through all request headers. |
| 6236 | * Returns 1 if this filter can be stopped upon return, otherwise 0. |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6237 | * Since it can manage the switch to another backend, it updates the per-proxy |
| 6238 | * DENY stats. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6239 | */ |
Willy Tarreau | 7421efb | 2012-07-02 15:11:27 +0200 | [diff] [blame] | 6240 | int apply_filter_to_req_headers(struct session *t, struct channel *req, struct hdr_exp *exp) |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6241 | { |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6242 | char term; |
| 6243 | char *cur_ptr, *cur_end, *cur_next; |
| 6244 | int cur_idx, old_idx, last_hdr; |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 6245 | struct http_txn *txn = &t->txn; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6246 | struct hdr_idx_elem *cur_hdr; |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 6247 | int delta; |
Willy Tarreau | 0f7562b | 2007-01-07 15:46:13 +0100 | [diff] [blame] | 6248 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6249 | last_hdr = 0; |
| 6250 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 6251 | cur_next = req->buf->p + hdr_idx_first_pos(&txn->hdr_idx); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6252 | old_idx = 0; |
| 6253 | |
| 6254 | while (!last_hdr) { |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 6255 | if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT))) |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6256 | return 1; |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 6257 | else if (unlikely(txn->flags & TX_CLALLOW) && |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6258 | (exp->action == ACT_ALLOW || |
| 6259 | exp->action == ACT_DENY || |
| 6260 | exp->action == ACT_TARPIT)) |
| 6261 | return 0; |
| 6262 | |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 6263 | cur_idx = txn->hdr_idx.v[old_idx].next; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6264 | if (!cur_idx) |
| 6265 | break; |
| 6266 | |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 6267 | cur_hdr = &txn->hdr_idx.v[cur_idx]; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6268 | cur_ptr = cur_next; |
| 6269 | cur_end = cur_ptr + cur_hdr->len; |
| 6270 | cur_next = cur_end + cur_hdr->cr + 1; |
| 6271 | |
| 6272 | /* Now we have one header between cur_ptr and cur_end, |
| 6273 | * and the next header starts at cur_next. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6274 | */ |
| 6275 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6276 | /* The annoying part is that pattern matching needs |
| 6277 | * that we modify the contents to null-terminate all |
| 6278 | * strings before testing them. |
| 6279 | */ |
| 6280 | |
| 6281 | term = *cur_end; |
| 6282 | *cur_end = '\0'; |
| 6283 | |
| 6284 | if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) { |
| 6285 | switch (exp->action) { |
| 6286 | case ACT_SETBE: |
| 6287 | /* It is not possible to jump a second time. |
| 6288 | * FIXME: should we return an HTTP/500 here so that |
| 6289 | * the admin knows there's a problem ? |
| 6290 | */ |
| 6291 | if (t->be != t->fe) |
| 6292 | break; |
| 6293 | |
| 6294 | /* Swithing Proxy */ |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 6295 | session_set_backend(t, (struct proxy *)exp->replace); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6296 | last_hdr = 1; |
| 6297 | break; |
| 6298 | |
| 6299 | case ACT_ALLOW: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 6300 | txn->flags |= TX_CLALLOW; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6301 | last_hdr = 1; |
| 6302 | break; |
| 6303 | |
| 6304 | case ACT_DENY: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 6305 | txn->flags |= TX_CLDENY; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6306 | last_hdr = 1; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6307 | break; |
| 6308 | |
| 6309 | case ACT_TARPIT: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 6310 | txn->flags |= TX_CLTARPIT; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6311 | last_hdr = 1; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6312 | break; |
| 6313 | |
| 6314 | case ACT_REPLACE: |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 6315 | trash.len = exp_replace(trash.str, cur_ptr, exp->replace, pmatch); |
| 6316 | 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] | 6317 | /* FIXME: if the user adds a newline in the replacement, the |
| 6318 | * index will not be recalculated for now, and the new line |
| 6319 | * will not be counted as a new header. |
| 6320 | */ |
| 6321 | |
| 6322 | cur_end += delta; |
| 6323 | cur_next += delta; |
| 6324 | cur_hdr->len += delta; |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 6325 | http_msg_move_end(&txn->req, delta); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6326 | break; |
| 6327 | |
| 6328 | case ACT_REMOVE: |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 6329 | delta = buffer_replace2(req->buf, cur_ptr, cur_next, NULL, 0); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6330 | cur_next += delta; |
| 6331 | |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 6332 | http_msg_move_end(&txn->req, delta); |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 6333 | txn->hdr_idx.v[old_idx].next = cur_hdr->next; |
| 6334 | txn->hdr_idx.used--; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6335 | cur_hdr->len = 0; |
| 6336 | cur_end = NULL; /* null-term has been rewritten */ |
Willy Tarreau | 26db59e | 2010-11-28 06:57:24 +0100 | [diff] [blame] | 6337 | cur_idx = old_idx; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6338 | break; |
| 6339 | |
| 6340 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6341 | } |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6342 | if (cur_end) |
| 6343 | *cur_end = term; /* restore the string terminator */ |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6344 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6345 | /* keep the link from this header to next one in case of later |
| 6346 | * removal of next header. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6347 | */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6348 | old_idx = cur_idx; |
| 6349 | } |
| 6350 | return 0; |
| 6351 | } |
| 6352 | |
| 6353 | |
| 6354 | /* Apply the filter to the request line. |
| 6355 | * Returns 0 if nothing has been done, 1 if the filter has been applied, |
| 6356 | * or -1 if a replacement resulted in an invalid request line. |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6357 | * Since it can manage the switch to another backend, it updates the per-proxy |
| 6358 | * DENY stats. |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6359 | */ |
Willy Tarreau | 7421efb | 2012-07-02 15:11:27 +0200 | [diff] [blame] | 6360 | int apply_filter_to_req_line(struct session *t, struct channel *req, struct hdr_exp *exp) |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6361 | { |
| 6362 | char term; |
| 6363 | char *cur_ptr, *cur_end; |
| 6364 | int done; |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 6365 | struct http_txn *txn = &t->txn; |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 6366 | int delta; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6367 | |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 6368 | if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT))) |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6369 | return 1; |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 6370 | else if (unlikely(txn->flags & TX_CLALLOW) && |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6371 | (exp->action == ACT_ALLOW || |
| 6372 | exp->action == ACT_DENY || |
| 6373 | exp->action == ACT_TARPIT)) |
| 6374 | return 0; |
| 6375 | else if (exp->action == ACT_REMOVE) |
| 6376 | return 0; |
| 6377 | |
| 6378 | done = 0; |
| 6379 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 6380 | cur_ptr = req->buf->p; |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 6381 | cur_end = cur_ptr + txn->req.sl.rq.l; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6382 | |
| 6383 | /* Now we have the request line between cur_ptr and cur_end */ |
| 6384 | |
| 6385 | /* The annoying part is that pattern matching needs |
| 6386 | * that we modify the contents to null-terminate all |
| 6387 | * strings before testing them. |
| 6388 | */ |
| 6389 | |
| 6390 | term = *cur_end; |
| 6391 | *cur_end = '\0'; |
| 6392 | |
| 6393 | if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) { |
| 6394 | switch (exp->action) { |
| 6395 | case ACT_SETBE: |
| 6396 | /* It is not possible to jump a second time. |
| 6397 | * FIXME: should we return an HTTP/500 here so that |
| 6398 | * the admin knows there's a problem ? |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6399 | */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6400 | if (t->be != t->fe) |
| 6401 | break; |
| 6402 | |
| 6403 | /* Swithing Proxy */ |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 6404 | session_set_backend(t, (struct proxy *)exp->replace); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6405 | done = 1; |
| 6406 | break; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6407 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6408 | case ACT_ALLOW: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 6409 | txn->flags |= TX_CLALLOW; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6410 | done = 1; |
| 6411 | break; |
Willy Tarreau | a496b60 | 2006-12-17 23:15:24 +0100 | [diff] [blame] | 6412 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6413 | case ACT_DENY: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 6414 | txn->flags |= TX_CLDENY; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6415 | done = 1; |
| 6416 | break; |
Willy Tarreau | a496b60 | 2006-12-17 23:15:24 +0100 | [diff] [blame] | 6417 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6418 | case ACT_TARPIT: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 6419 | txn->flags |= TX_CLTARPIT; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6420 | done = 1; |
| 6421 | break; |
Willy Tarreau | a496b60 | 2006-12-17 23:15:24 +0100 | [diff] [blame] | 6422 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6423 | case ACT_REPLACE: |
| 6424 | *cur_end = term; /* restore the string terminator */ |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 6425 | trash.len = exp_replace(trash.str, cur_ptr, exp->replace, pmatch); |
| 6426 | 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] | 6427 | /* FIXME: if the user adds a newline in the replacement, the |
| 6428 | * index will not be recalculated for now, and the new line |
| 6429 | * will not be counted as a new header. |
| 6430 | */ |
Willy Tarreau | a496b60 | 2006-12-17 23:15:24 +0100 | [diff] [blame] | 6431 | |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 6432 | http_msg_move_end(&txn->req, delta); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6433 | cur_end += delta; |
Willy Tarreau | 69d8c5d | 2012-05-08 09:44:41 +0200 | [diff] [blame] | 6434 | cur_end = (char *)http_parse_reqline(&txn->req, |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6435 | HTTP_MSG_RQMETH, |
| 6436 | cur_ptr, cur_end + 1, |
| 6437 | NULL, NULL); |
| 6438 | if (unlikely(!cur_end)) |
| 6439 | return -1; |
Willy Tarreau | a496b60 | 2006-12-17 23:15:24 +0100 | [diff] [blame] | 6440 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6441 | /* we have a full request and we know that we have either a CR |
| 6442 | * or an LF at <ptr>. |
| 6443 | */ |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 6444 | txn->meth = find_http_meth(cur_ptr, txn->req.sl.rq.m_l); |
| 6445 | 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] | 6446 | /* there is no point trying this regex on headers */ |
| 6447 | return 1; |
| 6448 | } |
| 6449 | } |
| 6450 | *cur_end = term; /* restore the string terminator */ |
| 6451 | return done; |
| 6452 | } |
Willy Tarreau | 97de624 | 2006-12-27 17:18:38 +0100 | [diff] [blame] | 6453 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6454 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6455 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6456 | /* |
Willy Tarreau | 6c123b1 | 2010-01-28 20:22:06 +0100 | [diff] [blame] | 6457 | * Apply all the req filters of proxy <px> to all headers in buffer <req> of session <s>. |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6458 | * 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] | 6459 | * unparsable request. Since it can manage the switch to another backend, it |
| 6460 | * updates the per-proxy DENY stats. |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6461 | */ |
Willy Tarreau | 7421efb | 2012-07-02 15:11:27 +0200 | [diff] [blame] | 6462 | int apply_filters_to_request(struct session *s, struct channel *req, struct proxy *px) |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6463 | { |
Willy Tarreau | 6c123b1 | 2010-01-28 20:22:06 +0100 | [diff] [blame] | 6464 | struct http_txn *txn = &s->txn; |
| 6465 | struct hdr_exp *exp; |
| 6466 | |
| 6467 | for (exp = px->req_exp; exp; exp = exp->next) { |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6468 | int ret; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6469 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6470 | /* |
| 6471 | * The interleaving of transformations and verdicts |
| 6472 | * makes it difficult to decide to continue or stop |
| 6473 | * the evaluation. |
| 6474 | */ |
| 6475 | |
Willy Tarreau | 6c123b1 | 2010-01-28 20:22:06 +0100 | [diff] [blame] | 6476 | if (txn->flags & (TX_CLDENY|TX_CLTARPIT)) |
| 6477 | break; |
| 6478 | |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 6479 | if ((txn->flags & TX_CLALLOW) && |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6480 | (exp->action == ACT_ALLOW || exp->action == ACT_DENY || |
Willy Tarreau | 6c123b1 | 2010-01-28 20:22:06 +0100 | [diff] [blame] | 6481 | exp->action == ACT_TARPIT || exp->action == ACT_PASS)) |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6482 | continue; |
Willy Tarreau | 6c123b1 | 2010-01-28 20:22:06 +0100 | [diff] [blame] | 6483 | |
| 6484 | /* if this filter had a condition, evaluate it now and skip to |
| 6485 | * next filter if the condition does not match. |
| 6486 | */ |
| 6487 | if (exp->cond) { |
Willy Tarreau | 32a6f2e | 2012-04-25 10:13:36 +0200 | [diff] [blame] | 6488 | ret = acl_exec_cond(exp->cond, px, s, txn, SMP_OPT_DIR_REQ|SMP_OPT_FINAL); |
Willy Tarreau | 6c123b1 | 2010-01-28 20:22:06 +0100 | [diff] [blame] | 6489 | ret = acl_pass(ret); |
| 6490 | if (((struct acl_cond *)exp->cond)->pol == ACL_COND_UNLESS) |
| 6491 | ret = !ret; |
| 6492 | |
| 6493 | if (!ret) |
| 6494 | continue; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6495 | } |
| 6496 | |
| 6497 | /* Apply the filter to the request line. */ |
Willy Tarreau | 6c123b1 | 2010-01-28 20:22:06 +0100 | [diff] [blame] | 6498 | ret = apply_filter_to_req_line(s, req, exp); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6499 | if (unlikely(ret < 0)) |
| 6500 | return -1; |
| 6501 | |
| 6502 | if (likely(ret == 0)) { |
| 6503 | /* The filter did not match the request, it can be |
| 6504 | * iterated through all headers. |
| 6505 | */ |
Willy Tarreau | 6c123b1 | 2010-01-28 20:22:06 +0100 | [diff] [blame] | 6506 | apply_filter_to_req_headers(s, req, exp); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6507 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6508 | } |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6509 | return 0; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6510 | } |
| 6511 | |
| 6512 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6513 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6514 | /* |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 6515 | * Try to retrieve the server associated to the appsession. |
| 6516 | * If the server is found, it's assigned to the session. |
| 6517 | */ |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 6518 | void manage_client_side_appsession(struct session *t, const char *buf, int len) { |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 6519 | struct http_txn *txn = &t->txn; |
| 6520 | appsess *asession = NULL; |
| 6521 | char *sessid_temp = NULL; |
| 6522 | |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 6523 | if (len > t->be->appsession_len) { |
| 6524 | len = t->be->appsession_len; |
| 6525 | } |
| 6526 | |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 6527 | if (t->be->options2 & PR_O2_AS_REQL) { |
| 6528 | /* request-learn option is enabled : store the sessid in the session for future use */ |
Willy Tarreau | a3377ee | 2010-01-10 10:49:11 +0100 | [diff] [blame] | 6529 | if (txn->sessid != NULL) { |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 6530 | /* free previously allocated memory as we don't need the session id found in the URL anymore */ |
Willy Tarreau | a3377ee | 2010-01-10 10:49:11 +0100 | [diff] [blame] | 6531 | pool_free2(apools.sessid, txn->sessid); |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 6532 | } |
| 6533 | |
Willy Tarreau | a3377ee | 2010-01-10 10:49:11 +0100 | [diff] [blame] | 6534 | if ((txn->sessid = pool_alloc2(apools.sessid)) == NULL) { |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 6535 | Alert("Not enough memory process_cli():asession->sessid:malloc().\n"); |
| 6536 | send_log(t->be, LOG_ALERT, "Not enough memory process_cli():asession->sessid:malloc().\n"); |
| 6537 | return; |
| 6538 | } |
| 6539 | |
Willy Tarreau | a3377ee | 2010-01-10 10:49:11 +0100 | [diff] [blame] | 6540 | memcpy(txn->sessid, buf, len); |
| 6541 | txn->sessid[len] = 0; |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 6542 | } |
| 6543 | |
| 6544 | if ((sessid_temp = pool_alloc2(apools.sessid)) == NULL) { |
| 6545 | Alert("Not enough memory process_cli():asession->sessid:malloc().\n"); |
| 6546 | send_log(t->be, LOG_ALERT, "Not enough memory process_cli():asession->sessid:malloc().\n"); |
| 6547 | return; |
| 6548 | } |
| 6549 | |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 6550 | memcpy(sessid_temp, buf, len); |
| 6551 | sessid_temp[len] = 0; |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 6552 | |
| 6553 | asession = appsession_hash_lookup(&(t->be->htbl_proxy), sessid_temp); |
| 6554 | /* free previously allocated memory */ |
| 6555 | pool_free2(apools.sessid, sessid_temp); |
| 6556 | |
| 6557 | if (asession != NULL) { |
| 6558 | asession->expire = tick_add_ifset(now_ms, t->be->timeout.appsession); |
| 6559 | if (!(t->be->options2 & PR_O2_AS_REQL)) |
| 6560 | asession->request_count++; |
| 6561 | |
| 6562 | if (asession->serverid != NULL) { |
| 6563 | struct server *srv = t->be->srv; |
Cyril Bonté | 47fdd8e | 2010-04-25 00:00:51 +0200 | [diff] [blame] | 6564 | |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 6565 | while (srv) { |
| 6566 | if (strcmp(srv->id, asession->serverid) == 0) { |
Willy Tarreau | 4de9149 | 2010-01-22 19:10:05 +0100 | [diff] [blame] | 6567 | if ((srv->state & SRV_RUNNING) || |
| 6568 | (t->be->options & PR_O_PERSIST) || |
| 6569 | (t->flags & SN_FORCE_PRST)) { |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 6570 | /* we found the server and it's usable */ |
| 6571 | txn->flags &= ~TX_CK_MASK; |
Willy Tarreau | 2a6d88d | 2010-01-24 13:10:43 +0100 | [diff] [blame] | 6572 | txn->flags |= (srv->state & SRV_RUNNING) ? TX_CK_VALID : TX_CK_DOWN; |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 6573 | t->flags |= SN_DIRECT | SN_ASSIGNED; |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 6574 | t->target = &srv->obj_type; |
Willy Tarreau | 664beb8 | 2011-03-10 11:38:29 +0100 | [diff] [blame] | 6575 | |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 6576 | break; |
| 6577 | } else { |
| 6578 | txn->flags &= ~TX_CK_MASK; |
| 6579 | txn->flags |= TX_CK_DOWN; |
| 6580 | } |
| 6581 | } |
| 6582 | srv = srv->next; |
| 6583 | } |
| 6584 | } |
| 6585 | } |
| 6586 | } |
| 6587 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6588 | /* Find the end of a cookie value contained between <s> and <e>. It works the |
| 6589 | * same way as with headers above except that the semi-colon also ends a token. |
| 6590 | * See RFC2965 for more information. Note that it requires a valid header to |
| 6591 | * return a valid result. |
| 6592 | */ |
| 6593 | char *find_cookie_value_end(char *s, const char *e) |
| 6594 | { |
| 6595 | int quoted, qdpair; |
| 6596 | |
| 6597 | quoted = qdpair = 0; |
| 6598 | for (; s < e; s++) { |
| 6599 | if (qdpair) qdpair = 0; |
| 6600 | else if (quoted) { |
| 6601 | if (*s == '\\') qdpair = 1; |
| 6602 | else if (*s == '"') quoted = 0; |
| 6603 | } |
| 6604 | else if (*s == '"') quoted = 1; |
| 6605 | else if (*s == ',' || *s == ';') return s; |
| 6606 | } |
| 6607 | return s; |
| 6608 | } |
| 6609 | |
| 6610 | /* Delete a value in a header between delimiters <from> and <next> in buffer |
| 6611 | * <buf>. The number of characters displaced is returned, and the pointer to |
| 6612 | * the first delimiter is updated if required. The function tries as much as |
| 6613 | * possible to respect the following principles : |
| 6614 | * - replace <from> delimiter by the <next> one unless <from> points to a |
| 6615 | * colon, in which case <next> is simply removed |
| 6616 | * - set exactly one space character after the new first delimiter, unless |
| 6617 | * there are not enough characters in the block being moved to do so. |
| 6618 | * - remove unneeded spaces before the previous delimiter and after the new |
| 6619 | * one. |
| 6620 | * |
| 6621 | * It is the caller's responsibility to ensure that : |
| 6622 | * - <from> points to a valid delimiter or the colon ; |
| 6623 | * - <next> points to a valid delimiter or the final CR/LF ; |
| 6624 | * - there are non-space chars before <from> ; |
| 6625 | * - there is a CR/LF at or after <next>. |
| 6626 | */ |
Willy Tarreau | af81935 | 2012-08-27 22:08:00 +0200 | [diff] [blame] | 6627 | int del_hdr_value(struct buffer *buf, char **from, char *next) |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6628 | { |
| 6629 | char *prev = *from; |
| 6630 | |
| 6631 | if (*prev == ':') { |
| 6632 | /* We're removing the first value, preserve the colon and add a |
| 6633 | * space if possible. |
| 6634 | */ |
| 6635 | if (!http_is_crlf[(unsigned char)*next]) |
| 6636 | next++; |
| 6637 | prev++; |
| 6638 | if (prev < next) |
| 6639 | *prev++ = ' '; |
| 6640 | |
| 6641 | while (http_is_spht[(unsigned char)*next]) |
| 6642 | next++; |
| 6643 | } else { |
| 6644 | /* Remove useless spaces before the old delimiter. */ |
| 6645 | while (http_is_spht[(unsigned char)*(prev-1)]) |
| 6646 | prev--; |
| 6647 | *from = prev; |
| 6648 | |
| 6649 | /* copy the delimiter and if possible a space if we're |
| 6650 | * not at the end of the line. |
| 6651 | */ |
| 6652 | if (!http_is_crlf[(unsigned char)*next]) { |
| 6653 | *prev++ = *next++; |
| 6654 | if (prev + 1 < next) |
| 6655 | *prev++ = ' '; |
| 6656 | while (http_is_spht[(unsigned char)*next]) |
| 6657 | next++; |
| 6658 | } |
| 6659 | } |
| 6660 | return buffer_replace2(buf, prev, next, NULL, 0); |
| 6661 | } |
| 6662 | |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 6663 | /* |
Willy Tarreau | 396d2c6 | 2007-11-04 19:30:00 +0100 | [diff] [blame] | 6664 | * 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] | 6665 | * desirable to call it only when needed. This code is quite complex because |
| 6666 | * of the multiple very crappy and ambiguous syntaxes we have to support. it |
| 6667 | * highly recommended not to touch this part without a good reason ! |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6668 | */ |
Willy Tarreau | 7421efb | 2012-07-02 15:11:27 +0200 | [diff] [blame] | 6669 | void manage_client_side_cookies(struct session *t, struct channel *req) |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6670 | { |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 6671 | struct http_txn *txn = &t->txn; |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6672 | int preserve_hdr; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6673 | int cur_idx, old_idx; |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6674 | char *hdr_beg, *hdr_end, *hdr_next, *del_from; |
| 6675 | char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6676 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6677 | /* Iterate through the headers, we start with the start line. */ |
Willy Tarreau | 83969f4 | 2007-01-22 08:55:47 +0100 | [diff] [blame] | 6678 | old_idx = 0; |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 6679 | hdr_next = req->buf->p + hdr_idx_first_pos(&txn->hdr_idx); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6680 | |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 6681 | while ((cur_idx = txn->hdr_idx.v[old_idx].next)) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6682 | struct hdr_idx_elem *cur_hdr; |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 6683 | int val; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6684 | |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 6685 | cur_hdr = &txn->hdr_idx.v[cur_idx]; |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6686 | hdr_beg = hdr_next; |
| 6687 | hdr_end = hdr_beg + cur_hdr->len; |
| 6688 | hdr_next = hdr_end + cur_hdr->cr + 1; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6689 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6690 | /* We have one full header between hdr_beg and hdr_end, and the |
| 6691 | * next header starts at hdr_next. We're only interested in |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6692 | * "Cookie:" headers. |
| 6693 | */ |
| 6694 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6695 | val = http_header_match2(hdr_beg, hdr_end, "Cookie", 6); |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 6696 | if (!val) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6697 | old_idx = cur_idx; |
| 6698 | continue; |
| 6699 | } |
| 6700 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6701 | del_from = NULL; /* nothing to be deleted */ |
| 6702 | preserve_hdr = 0; /* assume we may kill the whole header */ |
| 6703 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6704 | /* Now look for cookies. Conforming to RFC2109, we have to support |
| 6705 | * attributes whose name begin with a '$', and associate them with |
| 6706 | * the right cookie, if we want to delete this cookie. |
| 6707 | * So there are 3 cases for each cookie read : |
| 6708 | * 1) it's a special attribute, beginning with a '$' : ignore it. |
| 6709 | * 2) it's a server id cookie that we *MAY* want to delete : save |
| 6710 | * some pointers on it (last semi-colon, beginning of cookie...) |
| 6711 | * 3) it's an application cookie : we *MAY* have to delete a previous |
| 6712 | * "special" cookie. |
| 6713 | * At the end of loop, if a "special" cookie remains, we may have to |
| 6714 | * remove it. If no application cookie persists in the header, we |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6715 | * *MUST* delete it. |
| 6716 | * |
| 6717 | * Note: RFC2965 is unclear about the processing of spaces around |
| 6718 | * the equal sign in the ATTR=VALUE form. A careful inspection of |
| 6719 | * the RFC explicitly allows spaces before it, and not within the |
| 6720 | * tokens (attrs or values). An inspection of RFC2109 allows that |
| 6721 | * too but section 10.1.3 lets one think that spaces may be allowed |
| 6722 | * after the equal sign too, resulting in some (rare) buggy |
| 6723 | * implementations trying to do that. So let's do what servers do. |
| 6724 | * Latest ietf draft forbids spaces all around. Also, earlier RFCs |
| 6725 | * allowed quoted strings in values, with any possible character |
| 6726 | * after a backslash, including control chars and delimitors, which |
| 6727 | * causes parsing to become ambiguous. Browsers also allow spaces |
| 6728 | * within values even without quotes. |
| 6729 | * |
| 6730 | * We have to keep multiple pointers in order to support cookie |
| 6731 | * removal at the beginning, middle or end of header without |
| 6732 | * corrupting the header. All of these headers are valid : |
| 6733 | * |
| 6734 | * Cookie:NAME1=VALUE1;NAME2=VALUE2;NAME3=VALUE3\r\n |
| 6735 | * Cookie:NAME1=VALUE1;NAME2_ONLY ;NAME3=VALUE3\r\n |
| 6736 | * Cookie: NAME1 = VALUE 1 ; NAME2 = VALUE2 ; NAME3 = VALUE3\r\n |
| 6737 | * | | | | | | | | | |
| 6738 | * | | | | | | | | hdr_end <--+ |
| 6739 | * | | | | | | | +--> next |
| 6740 | * | | | | | | +----> val_end |
| 6741 | * | | | | | +-----------> val_beg |
| 6742 | * | | | | +--------------> equal |
| 6743 | * | | | +----------------> att_end |
| 6744 | * | | +---------------------> att_beg |
| 6745 | * | +--------------------------> prev |
| 6746 | * +--------------------------------> hdr_beg |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6747 | */ |
| 6748 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6749 | for (prev = hdr_beg + 6; prev < hdr_end; prev = next) { |
| 6750 | /* Iterate through all cookies on this line */ |
| 6751 | |
| 6752 | /* find att_beg */ |
| 6753 | att_beg = prev + 1; |
| 6754 | while (att_beg < hdr_end && http_is_spht[(unsigned char)*att_beg]) |
| 6755 | att_beg++; |
| 6756 | |
| 6757 | /* find att_end : this is the first character after the last non |
| 6758 | * space before the equal. It may be equal to hdr_end. |
| 6759 | */ |
| 6760 | equal = att_end = att_beg; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6761 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6762 | while (equal < hdr_end) { |
| 6763 | if (*equal == '=' || *equal == ',' || *equal == ';') |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6764 | break; |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6765 | if (http_is_spht[(unsigned char)*equal++]) |
| 6766 | continue; |
| 6767 | att_end = equal; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6768 | } |
| 6769 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6770 | /* here, <equal> points to '=', a delimitor or the end. <att_end> |
| 6771 | * is between <att_beg> and <equal>, both may be identical. |
| 6772 | */ |
| 6773 | |
| 6774 | /* look for end of cookie if there is an equal sign */ |
| 6775 | if (equal < hdr_end && *equal == '=') { |
| 6776 | /* look for the beginning of the value */ |
| 6777 | val_beg = equal + 1; |
| 6778 | while (val_beg < hdr_end && http_is_spht[(unsigned char)*val_beg]) |
| 6779 | val_beg++; |
| 6780 | |
| 6781 | /* find the end of the value, respecting quotes */ |
| 6782 | next = find_cookie_value_end(val_beg, hdr_end); |
| 6783 | |
| 6784 | /* make val_end point to the first white space or delimitor after the value */ |
| 6785 | val_end = next; |
| 6786 | while (val_end > val_beg && http_is_spht[(unsigned char)*(val_end - 1)]) |
| 6787 | val_end--; |
| 6788 | } else { |
| 6789 | val_beg = val_end = next = equal; |
Willy Tarreau | 305ae85 | 2010-01-03 19:45:54 +0100 | [diff] [blame] | 6790 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6791 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6792 | /* We have nothing to do with attributes beginning with '$'. However, |
| 6793 | * they will automatically be removed if a header before them is removed, |
| 6794 | * since they're supposed to be linked together. |
| 6795 | */ |
| 6796 | if (*att_beg == '$') |
| 6797 | continue; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6798 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6799 | /* Ignore cookies with no equal sign */ |
| 6800 | if (equal == next) { |
| 6801 | /* This is not our cookie, so we must preserve it. But if we already |
| 6802 | * scheduled another cookie for removal, we cannot remove the |
| 6803 | * complete header, but we can remove the previous block itself. |
| 6804 | */ |
| 6805 | preserve_hdr = 1; |
| 6806 | if (del_from != NULL) { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 6807 | int delta = del_hdr_value(req->buf, &del_from, prev); |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6808 | val_end += delta; |
| 6809 | next += delta; |
| 6810 | hdr_end += delta; |
| 6811 | hdr_next += delta; |
| 6812 | cur_hdr->len += delta; |
| 6813 | http_msg_move_end(&txn->req, delta); |
| 6814 | prev = del_from; |
| 6815 | del_from = NULL; |
| 6816 | } |
| 6817 | continue; |
Willy Tarreau | 305ae85 | 2010-01-03 19:45:54 +0100 | [diff] [blame] | 6818 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6819 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6820 | /* if there are spaces around the equal sign, we need to |
| 6821 | * strip them otherwise we'll get trouble for cookie captures, |
| 6822 | * or even for rewrites. Since this happens extremely rarely, |
| 6823 | * it does not hurt performance. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6824 | */ |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6825 | if (unlikely(att_end != equal || val_beg > equal + 1)) { |
| 6826 | int stripped_before = 0; |
| 6827 | int stripped_after = 0; |
| 6828 | |
| 6829 | if (att_end != equal) { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 6830 | stripped_before = buffer_replace2(req->buf, att_end, equal, NULL, 0); |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6831 | equal += stripped_before; |
| 6832 | val_beg += stripped_before; |
| 6833 | } |
| 6834 | |
| 6835 | if (val_beg > equal + 1) { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 6836 | stripped_after = buffer_replace2(req->buf, equal + 1, val_beg, NULL, 0); |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6837 | val_beg += stripped_after; |
| 6838 | stripped_before += stripped_after; |
| 6839 | } |
| 6840 | |
| 6841 | val_end += stripped_before; |
| 6842 | next += stripped_before; |
| 6843 | hdr_end += stripped_before; |
| 6844 | hdr_next += stripped_before; |
| 6845 | cur_hdr->len += stripped_before; |
| 6846 | http_msg_move_end(&txn->req, stripped_before); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6847 | } |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6848 | /* now everything is as on the diagram above */ |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6849 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6850 | /* First, let's see if we want to capture this cookie. We check |
| 6851 | * that we don't already have a client side cookie, because we |
| 6852 | * can only capture one. Also as an optimisation, we ignore |
| 6853 | * cookies shorter than the declared name. |
| 6854 | */ |
| 6855 | if (t->fe->capture_name != NULL && txn->cli_cookie == NULL && |
| 6856 | (val_end - att_beg >= t->fe->capture_namelen) && |
| 6857 | memcmp(att_beg, t->fe->capture_name, t->fe->capture_namelen) == 0) { |
| 6858 | int log_len = val_end - att_beg; |
| 6859 | |
| 6860 | if ((txn->cli_cookie = pool_alloc2(pool2_capture)) == NULL) { |
| 6861 | Alert("HTTP logging : out of memory.\n"); |
| 6862 | } else { |
| 6863 | if (log_len > t->fe->capture_len) |
| 6864 | log_len = t->fe->capture_len; |
| 6865 | memcpy(txn->cli_cookie, att_beg, log_len); |
| 6866 | txn->cli_cookie[log_len] = 0; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6867 | } |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6868 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6869 | |
Willy Tarreau | bca9969 | 2010-10-06 19:25:55 +0200 | [diff] [blame] | 6870 | /* Persistence cookies in passive, rewrite or insert mode have the |
| 6871 | * following form : |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6872 | * |
Willy Tarreau | bca9969 | 2010-10-06 19:25:55 +0200 | [diff] [blame] | 6873 | * Cookie: NAME=SRV[|<lastseen>[|<firstseen>]] |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6874 | * |
Willy Tarreau | bca9969 | 2010-10-06 19:25:55 +0200 | [diff] [blame] | 6875 | * For cookies in prefix mode, the form is : |
| 6876 | * |
| 6877 | * Cookie: NAME=SRV~VALUE |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6878 | */ |
| 6879 | if ((att_end - att_beg == t->be->cookie_len) && (t->be->cookie_name != NULL) && |
| 6880 | (memcmp(att_beg, t->be->cookie_name, att_end - att_beg) == 0)) { |
| 6881 | struct server *srv = t->be->srv; |
| 6882 | char *delim; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6883 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6884 | /* if we're in cookie prefix mode, we'll search the delimitor so that we |
| 6885 | * have the server ID between val_beg and delim, and the original cookie between |
| 6886 | * delim+1 and val_end. Otherwise, delim==val_end : |
| 6887 | * |
| 6888 | * Cookie: NAME=SRV; # in all but prefix modes |
| 6889 | * Cookie: NAME=SRV~OPAQUE ; # in prefix mode |
| 6890 | * | || || | |+-> next |
| 6891 | * | || || | +--> val_end |
| 6892 | * | || || +---------> delim |
| 6893 | * | || |+------------> val_beg |
| 6894 | * | || +-------------> att_end = equal |
| 6895 | * | |+-----------------> att_beg |
| 6896 | * | +------------------> prev |
| 6897 | * +-------------------------> hdr_beg |
| 6898 | */ |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6899 | |
Willy Tarreau | 6740213 | 2012-05-31 20:40:20 +0200 | [diff] [blame] | 6900 | if (t->be->ck_opts & PR_CK_PFX) { |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6901 | for (delim = val_beg; delim < val_end; delim++) |
| 6902 | if (*delim == COOKIE_DELIM) |
| 6903 | break; |
Willy Tarreau | bca9969 | 2010-10-06 19:25:55 +0200 | [diff] [blame] | 6904 | } else { |
| 6905 | char *vbar1; |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6906 | delim = val_end; |
Willy Tarreau | bca9969 | 2010-10-06 19:25:55 +0200 | [diff] [blame] | 6907 | /* Now check if the cookie contains a date field, which would |
| 6908 | * appear after a vertical bar ('|') just after the server name |
| 6909 | * and before the delimiter. |
| 6910 | */ |
| 6911 | vbar1 = memchr(val_beg, COOKIE_DELIM_DATE, val_end - val_beg); |
| 6912 | if (vbar1) { |
| 6913 | /* OK, so left of the bar is the server's cookie and |
Willy Tarreau | f64d141 | 2010-10-07 20:06:11 +0200 | [diff] [blame] | 6914 | * right is the last seen date. It is a base64 encoded |
| 6915 | * 30-bit value representing the UNIX date since the |
| 6916 | * epoch in 4-second quantities. |
Willy Tarreau | bca9969 | 2010-10-06 19:25:55 +0200 | [diff] [blame] | 6917 | */ |
Willy Tarreau | f64d141 | 2010-10-07 20:06:11 +0200 | [diff] [blame] | 6918 | int val; |
Willy Tarreau | bca9969 | 2010-10-06 19:25:55 +0200 | [diff] [blame] | 6919 | delim = vbar1++; |
Willy Tarreau | f64d141 | 2010-10-07 20:06:11 +0200 | [diff] [blame] | 6920 | if (val_end - vbar1 >= 5) { |
| 6921 | val = b64tos30(vbar1); |
| 6922 | if (val > 0) |
| 6923 | txn->cookie_last_date = val << 2; |
| 6924 | } |
| 6925 | /* look for a second vertical bar */ |
| 6926 | vbar1 = memchr(vbar1, COOKIE_DELIM_DATE, val_end - vbar1); |
| 6927 | if (vbar1 && (val_end - vbar1 > 5)) { |
| 6928 | val = b64tos30(vbar1 + 1); |
| 6929 | if (val > 0) |
| 6930 | txn->cookie_first_date = val << 2; |
| 6931 | } |
Willy Tarreau | bca9969 | 2010-10-06 19:25:55 +0200 | [diff] [blame] | 6932 | } |
| 6933 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6934 | |
Willy Tarreau | f64d141 | 2010-10-07 20:06:11 +0200 | [diff] [blame] | 6935 | /* if the cookie has an expiration date and the proxy wants to check |
| 6936 | * it, then we do that now. We first check if the cookie is too old, |
| 6937 | * then only if it has expired. We detect strict overflow because the |
| 6938 | * time resolution here is not great (4 seconds). Cookies with dates |
| 6939 | * in the future are ignored if their offset is beyond one day. This |
| 6940 | * allows an admin to fix timezone issues without expiring everyone |
| 6941 | * and at the same time avoids keeping unwanted side effects for too |
| 6942 | * long. |
| 6943 | */ |
| 6944 | if (txn->cookie_first_date && t->be->cookie_maxlife && |
Willy Tarreau | ef4f391 | 2010-10-07 21:00:29 +0200 | [diff] [blame] | 6945 | (((signed)(date.tv_sec - txn->cookie_first_date) > (signed)t->be->cookie_maxlife) || |
| 6946 | ((signed)(txn->cookie_first_date - date.tv_sec) > 86400))) { |
Willy Tarreau | f64d141 | 2010-10-07 20:06:11 +0200 | [diff] [blame] | 6947 | txn->flags &= ~TX_CK_MASK; |
| 6948 | txn->flags |= TX_CK_OLD; |
| 6949 | delim = val_beg; // let's pretend we have not found the cookie |
| 6950 | txn->cookie_first_date = 0; |
| 6951 | txn->cookie_last_date = 0; |
| 6952 | } |
| 6953 | else if (txn->cookie_last_date && t->be->cookie_maxidle && |
Willy Tarreau | ef4f391 | 2010-10-07 21:00:29 +0200 | [diff] [blame] | 6954 | (((signed)(date.tv_sec - txn->cookie_last_date) > (signed)t->be->cookie_maxidle) || |
| 6955 | ((signed)(txn->cookie_last_date - date.tv_sec) > 86400))) { |
Willy Tarreau | f64d141 | 2010-10-07 20:06:11 +0200 | [diff] [blame] | 6956 | txn->flags &= ~TX_CK_MASK; |
| 6957 | txn->flags |= TX_CK_EXPIRED; |
| 6958 | delim = val_beg; // let's pretend we have not found the cookie |
| 6959 | txn->cookie_first_date = 0; |
| 6960 | txn->cookie_last_date = 0; |
| 6961 | } |
| 6962 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6963 | /* Here, we'll look for the first running server which supports the cookie. |
| 6964 | * This allows to share a same cookie between several servers, for example |
| 6965 | * to dedicate backup servers to specific servers only. |
| 6966 | * However, to prevent clients from sticking to cookie-less backup server |
| 6967 | * when they have incidentely learned an empty cookie, we simply ignore |
| 6968 | * empty cookies and mark them as invalid. |
| 6969 | * The same behaviour is applied when persistence must be ignored. |
| 6970 | */ |
Willy Tarreau | 4a5cade | 2012-04-05 21:09:48 +0200 | [diff] [blame] | 6971 | if ((delim == val_beg) || (t->flags & (SN_IGNORE_PRST | SN_ASSIGNED))) |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6972 | srv = NULL; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6973 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6974 | while (srv) { |
| 6975 | if (srv->cookie && (srv->cklen == delim - val_beg) && |
| 6976 | !memcmp(val_beg, srv->cookie, delim - val_beg)) { |
| 6977 | if ((srv->state & SRV_RUNNING) || |
| 6978 | (t->be->options & PR_O_PERSIST) || |
| 6979 | (t->flags & SN_FORCE_PRST)) { |
| 6980 | /* we found the server and we can use it */ |
| 6981 | txn->flags &= ~TX_CK_MASK; |
| 6982 | txn->flags |= (srv->state & SRV_RUNNING) ? TX_CK_VALID : TX_CK_DOWN; |
| 6983 | t->flags |= SN_DIRECT | SN_ASSIGNED; |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 6984 | t->target = &srv->obj_type; |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6985 | break; |
| 6986 | } else { |
| 6987 | /* we found a server, but it's down, |
| 6988 | * mark it as such and go on in case |
| 6989 | * another one is available. |
| 6990 | */ |
| 6991 | txn->flags &= ~TX_CK_MASK; |
| 6992 | txn->flags |= TX_CK_DOWN; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6993 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6994 | } |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6995 | srv = srv->next; |
| 6996 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6997 | |
Willy Tarreau | f64d141 | 2010-10-07 20:06:11 +0200 | [diff] [blame] | 6998 | 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] | 6999 | /* no server matched this cookie or we deliberately skipped it */ |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7000 | txn->flags &= ~TX_CK_MASK; |
Willy Tarreau | c89ccb6 | 2012-04-05 21:18:22 +0200 | [diff] [blame] | 7001 | if ((t->flags & (SN_IGNORE_PRST | SN_ASSIGNED))) |
| 7002 | txn->flags |= TX_CK_UNUSED; |
| 7003 | else |
| 7004 | txn->flags |= TX_CK_INVALID; |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7005 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7006 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7007 | /* depending on the cookie mode, we may have to either : |
| 7008 | * - delete the complete cookie if we're in insert+indirect mode, so that |
| 7009 | * the server never sees it ; |
| 7010 | * - remove the server id from the cookie value, and tag the cookie as an |
| 7011 | * application cookie so that it does not get accidentely removed later, |
| 7012 | * if we're in cookie prefix mode |
| 7013 | */ |
Willy Tarreau | 6740213 | 2012-05-31 20:40:20 +0200 | [diff] [blame] | 7014 | if ((t->be->ck_opts & PR_CK_PFX) && (delim != val_end)) { |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7015 | int delta; /* negative */ |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7016 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 7017 | delta = buffer_replace2(req->buf, val_beg, delim + 1, NULL, 0); |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7018 | val_end += delta; |
| 7019 | next += delta; |
| 7020 | hdr_end += delta; |
| 7021 | hdr_next += delta; |
| 7022 | cur_hdr->len += delta; |
| 7023 | http_msg_move_end(&txn->req, delta); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7024 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7025 | del_from = NULL; |
| 7026 | preserve_hdr = 1; /* we want to keep this cookie */ |
| 7027 | } |
| 7028 | else if (del_from == NULL && |
Willy Tarreau | 6740213 | 2012-05-31 20:40:20 +0200 | [diff] [blame] | 7029 | (t->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] | 7030 | del_from = prev; |
| 7031 | } |
| 7032 | } else { |
| 7033 | /* This is not our cookie, so we must preserve it. But if we already |
| 7034 | * scheduled another cookie for removal, we cannot remove the |
| 7035 | * complete header, but we can remove the previous block itself. |
| 7036 | */ |
| 7037 | preserve_hdr = 1; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7038 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7039 | if (del_from != NULL) { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 7040 | int delta = del_hdr_value(req->buf, &del_from, prev); |
Willy Tarreau | b810554 | 2010-11-24 18:31:28 +0100 | [diff] [blame] | 7041 | if (att_beg >= del_from) |
| 7042 | att_beg += delta; |
| 7043 | if (att_end >= del_from) |
| 7044 | att_end += delta; |
| 7045 | val_beg += delta; |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7046 | val_end += delta; |
| 7047 | next += delta; |
| 7048 | hdr_end += delta; |
| 7049 | hdr_next += delta; |
| 7050 | cur_hdr->len += delta; |
| 7051 | http_msg_move_end(&txn->req, delta); |
| 7052 | prev = del_from; |
| 7053 | del_from = NULL; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7054 | } |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7055 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7056 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7057 | /* Look for the appsession cookie unless persistence must be ignored */ |
| 7058 | if (!(t->flags & SN_IGNORE_PRST) && (t->be->appsession_name != NULL)) { |
| 7059 | int cmp_len, value_len; |
| 7060 | char *value_begin; |
Aleksandar Lazic | 697bbb0 | 2008-08-13 19:57:02 +0200 | [diff] [blame] | 7061 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7062 | if (t->be->options2 & PR_O2_AS_PFX) { |
| 7063 | cmp_len = MIN(val_end - att_beg, t->be->appsession_name_len); |
| 7064 | value_begin = att_beg + t->be->appsession_name_len; |
| 7065 | value_len = val_end - att_beg - t->be->appsession_name_len; |
| 7066 | } else { |
| 7067 | cmp_len = att_end - att_beg; |
| 7068 | value_begin = val_beg; |
| 7069 | value_len = val_end - val_beg; |
| 7070 | } |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 7071 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7072 | /* let's see if the cookie is our appcookie */ |
| 7073 | if (cmp_len == t->be->appsession_name_len && |
| 7074 | memcmp(att_beg, t->be->appsession_name, cmp_len) == 0) { |
| 7075 | manage_client_side_appsession(t, value_begin, value_len); |
| 7076 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7077 | } |
| 7078 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7079 | /* continue with next cookie on this header line */ |
| 7080 | att_beg = next; |
| 7081 | } /* for each cookie */ |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7082 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7083 | /* There are no more cookies on this line. |
| 7084 | * We may still have one (or several) marked for deletion at the |
| 7085 | * end of the line. We must do this now in two ways : |
| 7086 | * - if some cookies must be preserved, we only delete from the |
| 7087 | * mark to the end of line ; |
| 7088 | * - if nothing needs to be preserved, simply delete the whole header |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7089 | */ |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7090 | if (del_from) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7091 | int delta; |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7092 | if (preserve_hdr) { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 7093 | delta = del_hdr_value(req->buf, &del_from, hdr_end); |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7094 | hdr_end = del_from; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7095 | cur_hdr->len += delta; |
| 7096 | } else { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 7097 | delta = buffer_replace2(req->buf, hdr_beg, hdr_next, NULL, 0); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7098 | |
| 7099 | /* FIXME: this should be a separate function */ |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 7100 | txn->hdr_idx.v[old_idx].next = cur_hdr->next; |
| 7101 | txn->hdr_idx.used--; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7102 | cur_hdr->len = 0; |
Willy Tarreau | 26db59e | 2010-11-28 06:57:24 +0100 | [diff] [blame] | 7103 | cur_idx = old_idx; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7104 | } |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7105 | hdr_next += delta; |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 7106 | http_msg_move_end(&txn->req, delta); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7107 | } |
| 7108 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7109 | /* check next header */ |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7110 | old_idx = cur_idx; |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7111 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7112 | } |
| 7113 | |
| 7114 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7115 | /* Iterate the same filter through all response headers contained in <rtr>. |
| 7116 | * Returns 1 if this filter can be stopped upon return, otherwise 0. |
| 7117 | */ |
Willy Tarreau | 7421efb | 2012-07-02 15:11:27 +0200 | [diff] [blame] | 7118 | int apply_filter_to_resp_headers(struct session *t, struct channel *rtr, struct hdr_exp *exp) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7119 | { |
| 7120 | char term; |
| 7121 | char *cur_ptr, *cur_end, *cur_next; |
| 7122 | int cur_idx, old_idx, last_hdr; |
| 7123 | struct http_txn *txn = &t->txn; |
| 7124 | struct hdr_idx_elem *cur_hdr; |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 7125 | int delta; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7126 | |
| 7127 | last_hdr = 0; |
| 7128 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 7129 | cur_next = rtr->buf->p + hdr_idx_first_pos(&txn->hdr_idx); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7130 | old_idx = 0; |
| 7131 | |
| 7132 | while (!last_hdr) { |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 7133 | if (unlikely(txn->flags & TX_SVDENY)) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7134 | return 1; |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 7135 | else if (unlikely(txn->flags & TX_SVALLOW) && |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7136 | (exp->action == ACT_ALLOW || |
| 7137 | exp->action == ACT_DENY)) |
| 7138 | return 0; |
| 7139 | |
| 7140 | cur_idx = txn->hdr_idx.v[old_idx].next; |
| 7141 | if (!cur_idx) |
| 7142 | break; |
| 7143 | |
| 7144 | cur_hdr = &txn->hdr_idx.v[cur_idx]; |
| 7145 | cur_ptr = cur_next; |
| 7146 | cur_end = cur_ptr + cur_hdr->len; |
| 7147 | cur_next = cur_end + cur_hdr->cr + 1; |
| 7148 | |
| 7149 | /* Now we have one header between cur_ptr and cur_end, |
| 7150 | * and the next header starts at cur_next. |
| 7151 | */ |
| 7152 | |
| 7153 | /* The annoying part is that pattern matching needs |
| 7154 | * that we modify the contents to null-terminate all |
| 7155 | * strings before testing them. |
| 7156 | */ |
| 7157 | |
| 7158 | term = *cur_end; |
| 7159 | *cur_end = '\0'; |
| 7160 | |
| 7161 | if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) { |
| 7162 | switch (exp->action) { |
| 7163 | case ACT_ALLOW: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 7164 | txn->flags |= TX_SVALLOW; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7165 | last_hdr = 1; |
| 7166 | break; |
| 7167 | |
| 7168 | case ACT_DENY: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 7169 | txn->flags |= TX_SVDENY; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7170 | last_hdr = 1; |
| 7171 | break; |
| 7172 | |
| 7173 | case ACT_REPLACE: |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 7174 | trash.len = exp_replace(trash.str, cur_ptr, exp->replace, pmatch); |
| 7175 | 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] | 7176 | /* FIXME: if the user adds a newline in the replacement, the |
| 7177 | * index will not be recalculated for now, and the new line |
| 7178 | * will not be counted as a new header. |
| 7179 | */ |
| 7180 | |
| 7181 | cur_end += delta; |
| 7182 | cur_next += delta; |
| 7183 | cur_hdr->len += delta; |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 7184 | http_msg_move_end(&txn->rsp, delta); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7185 | break; |
| 7186 | |
| 7187 | case ACT_REMOVE: |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 7188 | delta = buffer_replace2(rtr->buf, cur_ptr, cur_next, NULL, 0); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7189 | cur_next += delta; |
| 7190 | |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 7191 | http_msg_move_end(&txn->rsp, delta); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7192 | txn->hdr_idx.v[old_idx].next = cur_hdr->next; |
| 7193 | txn->hdr_idx.used--; |
| 7194 | cur_hdr->len = 0; |
| 7195 | cur_end = NULL; /* null-term has been rewritten */ |
Willy Tarreau | 26db59e | 2010-11-28 06:57:24 +0100 | [diff] [blame] | 7196 | cur_idx = old_idx; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7197 | break; |
| 7198 | |
| 7199 | } |
| 7200 | } |
| 7201 | if (cur_end) |
| 7202 | *cur_end = term; /* restore the string terminator */ |
| 7203 | |
| 7204 | /* keep the link from this header to next one in case of later |
| 7205 | * removal of next header. |
| 7206 | */ |
| 7207 | old_idx = cur_idx; |
| 7208 | } |
| 7209 | return 0; |
| 7210 | } |
| 7211 | |
| 7212 | |
| 7213 | /* Apply the filter to the status line in the response buffer <rtr>. |
| 7214 | * Returns 0 if nothing has been done, 1 if the filter has been applied, |
| 7215 | * or -1 if a replacement resulted in an invalid status line. |
| 7216 | */ |
Willy Tarreau | 7421efb | 2012-07-02 15:11:27 +0200 | [diff] [blame] | 7217 | int apply_filter_to_sts_line(struct session *t, struct channel *rtr, struct hdr_exp *exp) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7218 | { |
| 7219 | char term; |
| 7220 | char *cur_ptr, *cur_end; |
| 7221 | int done; |
| 7222 | struct http_txn *txn = &t->txn; |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 7223 | int delta; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7224 | |
| 7225 | |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 7226 | if (unlikely(txn->flags & TX_SVDENY)) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7227 | return 1; |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 7228 | else if (unlikely(txn->flags & TX_SVALLOW) && |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7229 | (exp->action == ACT_ALLOW || |
| 7230 | exp->action == ACT_DENY)) |
| 7231 | return 0; |
| 7232 | else if (exp->action == ACT_REMOVE) |
| 7233 | return 0; |
| 7234 | |
| 7235 | done = 0; |
| 7236 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 7237 | cur_ptr = rtr->buf->p; |
Willy Tarreau | 1ba0e5f | 2010-06-07 13:57:32 +0200 | [diff] [blame] | 7238 | cur_end = cur_ptr + txn->rsp.sl.st.l; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7239 | |
| 7240 | /* Now we have the status line between cur_ptr and cur_end */ |
| 7241 | |
| 7242 | /* The annoying part is that pattern matching needs |
| 7243 | * that we modify the contents to null-terminate all |
| 7244 | * strings before testing them. |
| 7245 | */ |
| 7246 | |
| 7247 | term = *cur_end; |
| 7248 | *cur_end = '\0'; |
| 7249 | |
| 7250 | if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) { |
| 7251 | switch (exp->action) { |
| 7252 | case ACT_ALLOW: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 7253 | txn->flags |= TX_SVALLOW; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7254 | done = 1; |
| 7255 | break; |
| 7256 | |
| 7257 | case ACT_DENY: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 7258 | txn->flags |= TX_SVDENY; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7259 | done = 1; |
| 7260 | break; |
| 7261 | |
| 7262 | case ACT_REPLACE: |
| 7263 | *cur_end = term; /* restore the string terminator */ |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 7264 | trash.len = exp_replace(trash.str, cur_ptr, exp->replace, pmatch); |
| 7265 | 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] | 7266 | /* FIXME: if the user adds a newline in the replacement, the |
| 7267 | * index will not be recalculated for now, and the new line |
| 7268 | * will not be counted as a new header. |
| 7269 | */ |
| 7270 | |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 7271 | http_msg_move_end(&txn->rsp, delta); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7272 | cur_end += delta; |
Willy Tarreau | 69d8c5d | 2012-05-08 09:44:41 +0200 | [diff] [blame] | 7273 | cur_end = (char *)http_parse_stsline(&txn->rsp, |
Willy Tarreau | 0278576 | 2007-04-03 14:45:44 +0200 | [diff] [blame] | 7274 | HTTP_MSG_RPVER, |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7275 | cur_ptr, cur_end + 1, |
| 7276 | NULL, NULL); |
| 7277 | if (unlikely(!cur_end)) |
| 7278 | return -1; |
| 7279 | |
| 7280 | /* we have a full respnse and we know that we have either a CR |
| 7281 | * or an LF at <ptr>. |
| 7282 | */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 7283 | 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] | 7284 | 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] | 7285 | /* there is no point trying this regex on headers */ |
| 7286 | return 1; |
| 7287 | } |
| 7288 | } |
| 7289 | *cur_end = term; /* restore the string terminator */ |
| 7290 | return done; |
| 7291 | } |
| 7292 | |
| 7293 | |
| 7294 | |
| 7295 | /* |
Willy Tarreau | fdb563c | 2010-01-31 15:43:27 +0100 | [diff] [blame] | 7296 | * Apply all the resp filters of proxy <px> to all headers in buffer <rtr> of session <s>. |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7297 | * Returns 0 if everything is alright, or -1 in case a replacement lead to an |
| 7298 | * unparsable response. |
| 7299 | */ |
Willy Tarreau | 7421efb | 2012-07-02 15:11:27 +0200 | [diff] [blame] | 7300 | int apply_filters_to_response(struct session *s, struct channel *rtr, struct proxy *px) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7301 | { |
Willy Tarreau | fdb563c | 2010-01-31 15:43:27 +0100 | [diff] [blame] | 7302 | struct http_txn *txn = &s->txn; |
| 7303 | struct hdr_exp *exp; |
| 7304 | |
| 7305 | for (exp = px->rsp_exp; exp; exp = exp->next) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7306 | int ret; |
| 7307 | |
| 7308 | /* |
| 7309 | * The interleaving of transformations and verdicts |
| 7310 | * makes it difficult to decide to continue or stop |
| 7311 | * the evaluation. |
| 7312 | */ |
| 7313 | |
Willy Tarreau | fdb563c | 2010-01-31 15:43:27 +0100 | [diff] [blame] | 7314 | if (txn->flags & TX_SVDENY) |
| 7315 | break; |
| 7316 | |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 7317 | if ((txn->flags & TX_SVALLOW) && |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7318 | (exp->action == ACT_ALLOW || exp->action == ACT_DENY || |
| 7319 | exp->action == ACT_PASS)) { |
| 7320 | exp = exp->next; |
| 7321 | continue; |
| 7322 | } |
| 7323 | |
Willy Tarreau | fdb563c | 2010-01-31 15:43:27 +0100 | [diff] [blame] | 7324 | /* if this filter had a condition, evaluate it now and skip to |
| 7325 | * next filter if the condition does not match. |
| 7326 | */ |
| 7327 | if (exp->cond) { |
Willy Tarreau | 32a6f2e | 2012-04-25 10:13:36 +0200 | [diff] [blame] | 7328 | ret = acl_exec_cond(exp->cond, px, s, txn, SMP_OPT_DIR_RES|SMP_OPT_FINAL); |
Willy Tarreau | fdb563c | 2010-01-31 15:43:27 +0100 | [diff] [blame] | 7329 | ret = acl_pass(ret); |
| 7330 | if (((struct acl_cond *)exp->cond)->pol == ACL_COND_UNLESS) |
| 7331 | ret = !ret; |
| 7332 | if (!ret) |
| 7333 | continue; |
| 7334 | } |
| 7335 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7336 | /* Apply the filter to the status line. */ |
Willy Tarreau | fdb563c | 2010-01-31 15:43:27 +0100 | [diff] [blame] | 7337 | ret = apply_filter_to_sts_line(s, rtr, exp); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7338 | if (unlikely(ret < 0)) |
| 7339 | return -1; |
| 7340 | |
| 7341 | if (likely(ret == 0)) { |
| 7342 | /* The filter did not match the response, it can be |
| 7343 | * iterated through all headers. |
| 7344 | */ |
Willy Tarreau | fdb563c | 2010-01-31 15:43:27 +0100 | [diff] [blame] | 7345 | apply_filter_to_resp_headers(s, rtr, exp); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7346 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7347 | } |
| 7348 | return 0; |
| 7349 | } |
| 7350 | |
| 7351 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7352 | /* |
Willy Tarreau | 396d2c6 | 2007-11-04 19:30:00 +0100 | [diff] [blame] | 7353 | * 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] | 7354 | * desirable to call it only when needed. This function is also used when we |
| 7355 | * 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] | 7356 | */ |
Willy Tarreau | 7421efb | 2012-07-02 15:11:27 +0200 | [diff] [blame] | 7357 | void manage_server_side_cookies(struct session *t, struct channel *res) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7358 | { |
| 7359 | struct http_txn *txn = &t->txn; |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 7360 | struct server *srv; |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7361 | int is_cookie2; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7362 | int cur_idx, old_idx, delta; |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7363 | char *hdr_beg, *hdr_end, *hdr_next; |
| 7364 | char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7365 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7366 | /* Iterate through the headers. |
| 7367 | * we start with the start line. |
| 7368 | */ |
| 7369 | old_idx = 0; |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 7370 | hdr_next = res->buf->p + hdr_idx_first_pos(&txn->hdr_idx); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7371 | |
| 7372 | while ((cur_idx = txn->hdr_idx.v[old_idx].next)) { |
| 7373 | struct hdr_idx_elem *cur_hdr; |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 7374 | int val; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7375 | |
| 7376 | cur_hdr = &txn->hdr_idx.v[cur_idx]; |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7377 | hdr_beg = hdr_next; |
| 7378 | hdr_end = hdr_beg + cur_hdr->len; |
| 7379 | hdr_next = hdr_end + cur_hdr->cr + 1; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7380 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7381 | /* We have one full header between hdr_beg and hdr_end, and the |
| 7382 | * next header starts at hdr_next. We're only interested in |
| 7383 | * "Set-Cookie" and "Set-Cookie2" headers. |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7384 | */ |
| 7385 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7386 | is_cookie2 = 0; |
| 7387 | prev = hdr_beg + 10; |
| 7388 | val = http_header_match2(hdr_beg, hdr_end, "Set-Cookie", 10); |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 7389 | if (!val) { |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7390 | val = http_header_match2(hdr_beg, hdr_end, "Set-Cookie2", 11); |
| 7391 | if (!val) { |
| 7392 | old_idx = cur_idx; |
| 7393 | continue; |
| 7394 | } |
| 7395 | is_cookie2 = 1; |
| 7396 | prev = hdr_beg + 11; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7397 | } |
| 7398 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7399 | /* OK, right now we know we have a Set-Cookie* at hdr_beg, and |
| 7400 | * <prev> points to the colon. |
| 7401 | */ |
Willy Tarreau | f134831 | 2010-10-07 15:54:11 +0200 | [diff] [blame] | 7402 | txn->flags |= TX_SCK_PRESENT; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7403 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7404 | /* Maybe we only wanted to see if there was a Set-Cookie (eg: |
| 7405 | * check-cache is enabled) and we are not interested in checking |
| 7406 | * them. Warning, the cookie capture is declared in the frontend. |
Willy Tarreau | fd39dda | 2008-10-17 12:01:58 +0200 | [diff] [blame] | 7407 | */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 7408 | if (t->be->cookie_name == NULL && |
| 7409 | t->be->appsession_name == NULL && |
Willy Tarreau | fd39dda | 2008-10-17 12:01:58 +0200 | [diff] [blame] | 7410 | t->fe->capture_name == NULL) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7411 | return; |
| 7412 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7413 | /* OK so now we know we have to process this response cookie. |
| 7414 | * The format of the Set-Cookie header is slightly different |
| 7415 | * from the format of the Cookie header in that it does not |
| 7416 | * support the comma as a cookie delimiter (thus the header |
| 7417 | * cannot be folded) because the Expires attribute described in |
| 7418 | * the original Netscape's spec may contain an unquoted date |
| 7419 | * with a comma inside. We have to live with this because |
| 7420 | * many browsers don't support Max-Age and some browsers don't |
| 7421 | * support quoted strings. However the Set-Cookie2 header is |
| 7422 | * clean. |
| 7423 | * |
| 7424 | * We have to keep multiple pointers in order to support cookie |
| 7425 | * removal at the beginning, middle or end of header without |
| 7426 | * corrupting the header (in case of set-cookie2). A special |
| 7427 | * pointer, <scav> points to the beginning of the set-cookie-av |
| 7428 | * fields after the first semi-colon. The <next> pointer points |
| 7429 | * either to the end of line (set-cookie) or next unquoted comma |
| 7430 | * (set-cookie2). All of these headers are valid : |
| 7431 | * |
| 7432 | * Set-Cookie: NAME1 = VALUE 1 ; Secure; Path="/"\r\n |
| 7433 | * Set-Cookie:NAME=VALUE; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT\r\n |
| 7434 | * Set-Cookie: NAME = VALUE ; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT\r\n |
| 7435 | * Set-Cookie2: NAME1 = VALUE 1 ; Max-Age=0, NAME2=VALUE2; Discard\r\n |
| 7436 | * | | | | | | | | | | |
| 7437 | * | | | | | | | | +-> next hdr_end <--+ |
| 7438 | * | | | | | | | +------------> scav |
| 7439 | * | | | | | | +--------------> val_end |
| 7440 | * | | | | | +--------------------> val_beg |
| 7441 | * | | | | +----------------------> equal |
| 7442 | * | | | +------------------------> att_end |
| 7443 | * | | +----------------------------> att_beg |
| 7444 | * | +------------------------------> prev |
| 7445 | * +-----------------------------------------> hdr_beg |
| 7446 | */ |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7447 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7448 | for (; prev < hdr_end; prev = next) { |
| 7449 | /* Iterate through all cookies on this line */ |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7450 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7451 | /* find att_beg */ |
| 7452 | att_beg = prev + 1; |
| 7453 | while (att_beg < hdr_end && http_is_spht[(unsigned char)*att_beg]) |
| 7454 | att_beg++; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7455 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7456 | /* find att_end : this is the first character after the last non |
| 7457 | * space before the equal. It may be equal to hdr_end. |
| 7458 | */ |
| 7459 | equal = att_end = att_beg; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7460 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7461 | while (equal < hdr_end) { |
| 7462 | if (*equal == '=' || *equal == ';' || (is_cookie2 && *equal == ',')) |
| 7463 | break; |
| 7464 | if (http_is_spht[(unsigned char)*equal++]) |
| 7465 | continue; |
| 7466 | att_end = equal; |
| 7467 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7468 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7469 | /* here, <equal> points to '=', a delimitor or the end. <att_end> |
| 7470 | * is between <att_beg> and <equal>, both may be identical. |
| 7471 | */ |
| 7472 | |
| 7473 | /* look for end of cookie if there is an equal sign */ |
| 7474 | if (equal < hdr_end && *equal == '=') { |
| 7475 | /* look for the beginning of the value */ |
| 7476 | val_beg = equal + 1; |
| 7477 | while (val_beg < hdr_end && http_is_spht[(unsigned char)*val_beg]) |
| 7478 | val_beg++; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7479 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7480 | /* find the end of the value, respecting quotes */ |
| 7481 | next = find_cookie_value_end(val_beg, hdr_end); |
| 7482 | |
| 7483 | /* make val_end point to the first white space or delimitor after the value */ |
| 7484 | val_end = next; |
| 7485 | while (val_end > val_beg && http_is_spht[(unsigned char)*(val_end - 1)]) |
| 7486 | val_end--; |
| 7487 | } else { |
| 7488 | /* <equal> points to next comma, semi-colon or EOL */ |
| 7489 | val_beg = val_end = next = equal; |
| 7490 | } |
| 7491 | |
| 7492 | if (next < hdr_end) { |
| 7493 | /* Set-Cookie2 supports multiple cookies, and <next> points to |
| 7494 | * a colon or semi-colon before the end. So skip all attr-value |
| 7495 | * pairs and look for the next comma. For Set-Cookie, since |
| 7496 | * commas are permitted in values, skip to the end. |
| 7497 | */ |
| 7498 | if (is_cookie2) |
| 7499 | next = find_hdr_value_end(next, hdr_end); |
| 7500 | else |
| 7501 | next = hdr_end; |
| 7502 | } |
| 7503 | |
| 7504 | /* Now everything is as on the diagram above */ |
| 7505 | |
| 7506 | /* Ignore cookies with no equal sign */ |
| 7507 | if (equal == val_end) |
| 7508 | continue; |
| 7509 | |
| 7510 | /* If there are spaces around the equal sign, we need to |
| 7511 | * strip them otherwise we'll get trouble for cookie captures, |
| 7512 | * or even for rewrites. Since this happens extremely rarely, |
| 7513 | * it does not hurt performance. |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7514 | */ |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7515 | if (unlikely(att_end != equal || val_beg > equal + 1)) { |
| 7516 | int stripped_before = 0; |
| 7517 | int stripped_after = 0; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7518 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7519 | if (att_end != equal) { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 7520 | stripped_before = buffer_replace2(res->buf, att_end, equal, NULL, 0); |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7521 | equal += stripped_before; |
| 7522 | val_beg += stripped_before; |
| 7523 | } |
| 7524 | |
| 7525 | if (val_beg > equal + 1) { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 7526 | stripped_after = buffer_replace2(res->buf, equal + 1, val_beg, NULL, 0); |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7527 | val_beg += stripped_after; |
| 7528 | stripped_before += stripped_after; |
| 7529 | } |
| 7530 | |
| 7531 | val_end += stripped_before; |
| 7532 | next += stripped_before; |
| 7533 | hdr_end += stripped_before; |
| 7534 | hdr_next += stripped_before; |
| 7535 | cur_hdr->len += stripped_before; |
Willy Tarreau | 1fc1f45 | 2011-04-07 22:35:37 +0200 | [diff] [blame] | 7536 | http_msg_move_end(&txn->rsp, stripped_before); |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7537 | } |
| 7538 | |
| 7539 | /* First, let's see if we want to capture this cookie. We check |
| 7540 | * that we don't already have a server side cookie, because we |
| 7541 | * can only capture one. Also as an optimisation, we ignore |
| 7542 | * cookies shorter than the declared name. |
| 7543 | */ |
Willy Tarreau | fd39dda | 2008-10-17 12:01:58 +0200 | [diff] [blame] | 7544 | if (t->fe->capture_name != NULL && |
Willy Tarreau | 3bac9ff | 2007-03-18 17:31:28 +0100 | [diff] [blame] | 7545 | txn->srv_cookie == NULL && |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7546 | (val_end - att_beg >= t->fe->capture_namelen) && |
| 7547 | memcmp(att_beg, t->fe->capture_name, t->fe->capture_namelen) == 0) { |
| 7548 | int log_len = val_end - att_beg; |
Willy Tarreau | 086b3b4 | 2007-05-13 21:45:51 +0200 | [diff] [blame] | 7549 | if ((txn->srv_cookie = pool_alloc2(pool2_capture)) == NULL) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7550 | Alert("HTTP logging : out of memory.\n"); |
| 7551 | } |
Willy Tarreau | f70fc75 | 2010-11-19 11:27:18 +0100 | [diff] [blame] | 7552 | else { |
| 7553 | if (log_len > t->fe->capture_len) |
| 7554 | log_len = t->fe->capture_len; |
| 7555 | memcpy(txn->srv_cookie, att_beg, log_len); |
| 7556 | txn->srv_cookie[log_len] = 0; |
| 7557 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7558 | } |
| 7559 | |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 7560 | srv = objt_server(t->target); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7561 | /* now check if we need to process it for persistence */ |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7562 | if (!(t->flags & SN_IGNORE_PRST) && |
| 7563 | (att_end - att_beg == t->be->cookie_len) && (t->be->cookie_name != NULL) && |
| 7564 | (memcmp(att_beg, t->be->cookie_name, att_end - att_beg) == 0)) { |
Willy Tarreau | f134831 | 2010-10-07 15:54:11 +0200 | [diff] [blame] | 7565 | /* assume passive cookie by default */ |
| 7566 | txn->flags &= ~TX_SCK_MASK; |
| 7567 | txn->flags |= TX_SCK_FOUND; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7568 | |
| 7569 | /* If the cookie is in insert mode on a known server, we'll delete |
| 7570 | * this occurrence because we'll insert another one later. |
| 7571 | * 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] | 7572 | * a direct access. |
| 7573 | */ |
Willy Tarreau | 6740213 | 2012-05-31 20:40:20 +0200 | [diff] [blame] | 7574 | if (t->be->ck_opts & PR_CK_PSV) { |
Willy Tarreau | ba4c5be | 2010-10-23 12:46:42 +0200 | [diff] [blame] | 7575 | /* The "preserve" flag was set, we don't want to touch the |
| 7576 | * server's cookie. |
| 7577 | */ |
| 7578 | } |
Willy Tarreau | 6740213 | 2012-05-31 20:40:20 +0200 | [diff] [blame] | 7579 | else if ((srv && (t->be->ck_opts & PR_CK_INS)) || |
| 7580 | ((t->flags & SN_DIRECT) && (t->be->ck_opts & PR_CK_IND))) { |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7581 | /* this cookie must be deleted */ |
| 7582 | if (*prev == ':' && next == hdr_end) { |
| 7583 | /* whole header */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 7584 | delta = buffer_replace2(res->buf, hdr_beg, hdr_next, NULL, 0); |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7585 | txn->hdr_idx.v[old_idx].next = cur_hdr->next; |
| 7586 | txn->hdr_idx.used--; |
| 7587 | cur_hdr->len = 0; |
Willy Tarreau | 26db59e | 2010-11-28 06:57:24 +0100 | [diff] [blame] | 7588 | cur_idx = old_idx; |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7589 | hdr_next += delta; |
| 7590 | http_msg_move_end(&txn->rsp, delta); |
| 7591 | /* note: while both invalid now, <next> and <hdr_end> |
| 7592 | * are still equal, so the for() will stop as expected. |
| 7593 | */ |
| 7594 | } else { |
| 7595 | /* just remove the value */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 7596 | int delta = del_hdr_value(res->buf, &prev, next); |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7597 | next = prev; |
| 7598 | hdr_end += delta; |
| 7599 | hdr_next += delta; |
| 7600 | cur_hdr->len += delta; |
| 7601 | http_msg_move_end(&txn->rsp, delta); |
| 7602 | } |
Willy Tarreau | f134831 | 2010-10-07 15:54:11 +0200 | [diff] [blame] | 7603 | txn->flags &= ~TX_SCK_MASK; |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 7604 | txn->flags |= TX_SCK_DELETED; |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7605 | /* and go on with next cookie */ |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7606 | } |
Willy Tarreau | 6740213 | 2012-05-31 20:40:20 +0200 | [diff] [blame] | 7607 | else if (srv && srv->cookie && (t->be->ck_opts & PR_CK_RW)) { |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7608 | /* replace bytes val_beg->val_end with the cookie name associated |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7609 | * with this server since we know it. |
| 7610 | */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 7611 | 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] | 7612 | next += delta; |
| 7613 | hdr_end += delta; |
| 7614 | hdr_next += delta; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7615 | cur_hdr->len += delta; |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 7616 | http_msg_move_end(&txn->rsp, delta); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7617 | |
Willy Tarreau | f134831 | 2010-10-07 15:54:11 +0200 | [diff] [blame] | 7618 | txn->flags &= ~TX_SCK_MASK; |
| 7619 | txn->flags |= TX_SCK_REPLACED; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7620 | } |
Willy Tarreau | a059031 | 2012-06-06 16:07:00 +0200 | [diff] [blame] | 7621 | else if (srv && srv->cookie && (t->be->ck_opts & PR_CK_PFX)) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7622 | /* insert the cookie name associated with this server |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7623 | * before existing cookie, and insert a delimiter between them.. |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7624 | */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 7625 | 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] | 7626 | next += delta; |
| 7627 | hdr_end += delta; |
| 7628 | hdr_next += delta; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7629 | cur_hdr->len += delta; |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 7630 | http_msg_move_end(&txn->rsp, delta); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7631 | |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 7632 | val_beg[srv->cklen] = COOKIE_DELIM; |
Willy Tarreau | f134831 | 2010-10-07 15:54:11 +0200 | [diff] [blame] | 7633 | txn->flags &= ~TX_SCK_MASK; |
| 7634 | txn->flags |= TX_SCK_REPLACED; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7635 | } |
| 7636 | } |
Cyril Bonté | 47fdd8e | 2010-04-25 00:00:51 +0200 | [diff] [blame] | 7637 | /* next, let's see if the cookie is our appcookie, unless persistence must be ignored */ |
| 7638 | else if (!(t->flags & SN_IGNORE_PRST) && (t->be->appsession_name != NULL)) { |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 7639 | int cmp_len, value_len; |
| 7640 | char *value_begin; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7641 | |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 7642 | if (t->be->options2 & PR_O2_AS_PFX) { |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7643 | cmp_len = MIN(val_end - att_beg, t->be->appsession_name_len); |
| 7644 | value_begin = att_beg + t->be->appsession_name_len; |
| 7645 | value_len = MIN(t->be->appsession_len, val_end - att_beg - t->be->appsession_name_len); |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 7646 | } else { |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7647 | cmp_len = att_end - att_beg; |
| 7648 | value_begin = val_beg; |
| 7649 | value_len = MIN(t->be->appsession_len, val_end - val_beg); |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 7650 | } |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 7651 | |
Cyril Bonté | 17530c3 | 2010-04-06 21:11:10 +0200 | [diff] [blame] | 7652 | if ((cmp_len == t->be->appsession_name_len) && |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7653 | (memcmp(att_beg, t->be->appsession_name, t->be->appsession_name_len) == 0)) { |
| 7654 | /* free a possibly previously allocated memory */ |
| 7655 | pool_free2(apools.sessid, txn->sessid); |
| 7656 | |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 7657 | /* Store the sessid in the session for future use */ |
Willy Tarreau | a3377ee | 2010-01-10 10:49:11 +0100 | [diff] [blame] | 7658 | if ((txn->sessid = pool_alloc2(apools.sessid)) == NULL) { |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 7659 | Alert("Not enough Memory process_srv():asession->sessid:malloc().\n"); |
| 7660 | send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n"); |
| 7661 | return; |
| 7662 | } |
Willy Tarreau | a3377ee | 2010-01-10 10:49:11 +0100 | [diff] [blame] | 7663 | memcpy(txn->sessid, value_begin, value_len); |
| 7664 | txn->sessid[value_len] = 0; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7665 | } |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7666 | } |
| 7667 | /* that's done for this cookie, check the next one on the same |
| 7668 | * line when next != hdr_end (only if is_cookie2). |
| 7669 | */ |
| 7670 | } |
| 7671 | /* check next header */ |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7672 | old_idx = cur_idx; |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7673 | } |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 7674 | |
Willy Tarreau | a3377ee | 2010-01-10 10:49:11 +0100 | [diff] [blame] | 7675 | if (txn->sessid != NULL) { |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 7676 | appsess *asession = NULL; |
| 7677 | /* only do insert, if lookup fails */ |
Willy Tarreau | a3377ee | 2010-01-10 10:49:11 +0100 | [diff] [blame] | 7678 | asession = appsession_hash_lookup(&(t->be->htbl_proxy), txn->sessid); |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 7679 | if (asession == NULL) { |
Willy Tarreau | 1fac753 | 2010-01-09 19:23:06 +0100 | [diff] [blame] | 7680 | size_t server_id_len; |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 7681 | if ((asession = pool_alloc2(pool2_appsess)) == NULL) { |
| 7682 | Alert("Not enough Memory process_srv():asession:calloc().\n"); |
| 7683 | send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession:calloc().\n"); |
| 7684 | return; |
| 7685 | } |
Willy Tarreau | 77eb9b8 | 2010-11-19 11:29:06 +0100 | [diff] [blame] | 7686 | asession->serverid = NULL; /* to avoid a double free in case of allocation error */ |
| 7687 | |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 7688 | if ((asession->sessid = pool_alloc2(apools.sessid)) == NULL) { |
| 7689 | Alert("Not enough Memory process_srv():asession->sessid:malloc().\n"); |
| 7690 | send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n"); |
Cyril Bonté | 41689c2 | 2010-01-10 00:30:14 +0100 | [diff] [blame] | 7691 | t->be->htbl_proxy.destroy(asession); |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 7692 | return; |
| 7693 | } |
Willy Tarreau | a3377ee | 2010-01-10 10:49:11 +0100 | [diff] [blame] | 7694 | memcpy(asession->sessid, txn->sessid, t->be->appsession_len); |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 7695 | asession->sessid[t->be->appsession_len] = 0; |
| 7696 | |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 7697 | server_id_len = strlen(objt_server(t->target)->id) + 1; |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 7698 | if ((asession->serverid = pool_alloc2(apools.serverid)) == NULL) { |
Willy Tarreau | 77eb9b8 | 2010-11-19 11:29:06 +0100 | [diff] [blame] | 7699 | Alert("Not enough Memory process_srv():asession->serverid:malloc().\n"); |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 7700 | send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n"); |
Cyril Bonté | 41689c2 | 2010-01-10 00:30:14 +0100 | [diff] [blame] | 7701 | t->be->htbl_proxy.destroy(asession); |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 7702 | return; |
| 7703 | } |
| 7704 | asession->serverid[0] = '\0'; |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 7705 | memcpy(asession->serverid, objt_server(t->target)->id, server_id_len); |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 7706 | |
| 7707 | asession->request_count = 0; |
| 7708 | appsession_hash_insert(&(t->be->htbl_proxy), asession); |
| 7709 | } |
| 7710 | |
| 7711 | asession->expire = tick_add_ifset(now_ms, t->be->timeout.appsession); |
| 7712 | asession->request_count++; |
| 7713 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7714 | } |
| 7715 | |
| 7716 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7717 | /* |
| 7718 | * Check if response is cacheable or not. Updates t->flags. |
| 7719 | */ |
Willy Tarreau | 7421efb | 2012-07-02 15:11:27 +0200 | [diff] [blame] | 7720 | void check_response_for_cacheability(struct session *t, struct channel *rtr) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7721 | { |
| 7722 | struct http_txn *txn = &t->txn; |
| 7723 | char *p1, *p2; |
| 7724 | |
| 7725 | char *cur_ptr, *cur_end, *cur_next; |
| 7726 | int cur_idx; |
| 7727 | |
Willy Tarreau | 5df5187 | 2007-11-25 16:20:08 +0100 | [diff] [blame] | 7728 | if (!(txn->flags & TX_CACHEABLE)) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7729 | return; |
| 7730 | |
| 7731 | /* Iterate through the headers. |
| 7732 | * we start with the start line. |
| 7733 | */ |
| 7734 | cur_idx = 0; |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 7735 | cur_next = rtr->buf->p + hdr_idx_first_pos(&txn->hdr_idx); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7736 | |
| 7737 | while ((cur_idx = txn->hdr_idx.v[cur_idx].next)) { |
| 7738 | struct hdr_idx_elem *cur_hdr; |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 7739 | int val; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7740 | |
| 7741 | cur_hdr = &txn->hdr_idx.v[cur_idx]; |
| 7742 | cur_ptr = cur_next; |
| 7743 | cur_end = cur_ptr + cur_hdr->len; |
| 7744 | cur_next = cur_end + cur_hdr->cr + 1; |
| 7745 | |
| 7746 | /* We have one full header between cur_ptr and cur_end, and the |
| 7747 | * next header starts at cur_next. We're only interested in |
| 7748 | * "Cookie:" headers. |
| 7749 | */ |
| 7750 | |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 7751 | val = http_header_match2(cur_ptr, cur_end, "Pragma", 6); |
| 7752 | if (val) { |
| 7753 | if ((cur_end - (cur_ptr + val) >= 8) && |
| 7754 | strncasecmp(cur_ptr + val, "no-cache", 8) == 0) { |
| 7755 | txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK; |
| 7756 | return; |
| 7757 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7758 | } |
| 7759 | |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 7760 | val = http_header_match2(cur_ptr, cur_end, "Cache-control", 13); |
| 7761 | if (!val) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7762 | continue; |
| 7763 | |
| 7764 | /* OK, right now we know we have a cache-control header at cur_ptr */ |
| 7765 | |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 7766 | p1 = cur_ptr + val; /* first non-space char after 'cache-control:' */ |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7767 | |
| 7768 | if (p1 >= cur_end) /* no more info */ |
| 7769 | continue; |
| 7770 | |
| 7771 | /* p1 is at the beginning of the value */ |
| 7772 | p2 = p1; |
| 7773 | |
Willy Tarreau | 8f8e645 | 2007-06-17 21:51:38 +0200 | [diff] [blame] | 7774 | while (p2 < cur_end && *p2 != '=' && *p2 != ',' && !isspace((unsigned char)*p2)) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7775 | p2++; |
| 7776 | |
| 7777 | /* we have a complete value between p1 and p2 */ |
| 7778 | if (p2 < cur_end && *p2 == '=') { |
| 7779 | /* we have something of the form no-cache="set-cookie" */ |
| 7780 | if ((cur_end - p1 >= 21) && |
| 7781 | strncasecmp(p1, "no-cache=\"set-cookie", 20) == 0 |
| 7782 | && (p1[20] == '"' || p1[20] == ',')) |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 7783 | txn->flags &= ~TX_CACHE_COOK; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7784 | continue; |
| 7785 | } |
| 7786 | |
| 7787 | /* OK, so we know that either p2 points to the end of string or to a comma */ |
| 7788 | if (((p2 - p1 == 7) && strncasecmp(p1, "private", 7) == 0) || |
Willy Tarreau | 5b15f90 | 2013-07-04 12:46:56 +0200 | [diff] [blame] | 7789 | ((p2 - p1 == 8) && strncasecmp(p1, "no-cache", 8) == 0) || |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7790 | ((p2 - p1 == 8) && strncasecmp(p1, "no-store", 8) == 0) || |
| 7791 | ((p2 - p1 == 9) && strncasecmp(p1, "max-age=0", 9) == 0) || |
| 7792 | ((p2 - p1 == 10) && strncasecmp(p1, "s-maxage=0", 10) == 0)) { |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 7793 | txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7794 | return; |
| 7795 | } |
| 7796 | |
| 7797 | if ((p2 - p1 == 6) && strncasecmp(p1, "public", 6) == 0) { |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 7798 | txn->flags |= TX_CACHEABLE | TX_CACHE_COOK; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7799 | continue; |
| 7800 | } |
| 7801 | } |
| 7802 | } |
| 7803 | |
| 7804 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7805 | /* |
| 7806 | * Try to retrieve a known appsession in the URI, then the associated server. |
| 7807 | * If the server is found, it's assigned to the session. |
| 7808 | */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7809 | void get_srv_from_appsession(struct session *t, const char *begin, int len) |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7810 | { |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 7811 | char *end_params, *first_param, *cur_param, *next_param; |
| 7812 | char separator; |
| 7813 | int value_len; |
| 7814 | |
| 7815 | int mode = t->be->options2 & PR_O2_AS_M_ANY; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7816 | |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 7817 | if (t->be->appsession_name == NULL || |
Cyril Bonté | 17530c3 | 2010-04-06 21:11:10 +0200 | [diff] [blame] | 7818 | (t->txn.meth != HTTP_METH_GET && t->txn.meth != HTTP_METH_POST && t->txn.meth != HTTP_METH_HEAD)) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7819 | return; |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 7820 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7821 | |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 7822 | first_param = NULL; |
| 7823 | switch (mode) { |
| 7824 | case PR_O2_AS_M_PP: |
| 7825 | first_param = memchr(begin, ';', len); |
| 7826 | break; |
| 7827 | case PR_O2_AS_M_QS: |
| 7828 | first_param = memchr(begin, '?', len); |
| 7829 | break; |
| 7830 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7831 | |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 7832 | if (first_param == NULL) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7833 | return; |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 7834 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7835 | |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 7836 | switch (mode) { |
| 7837 | case PR_O2_AS_M_PP: |
| 7838 | if ((end_params = memchr(first_param, '?', len - (begin - first_param))) == NULL) { |
| 7839 | end_params = (char *) begin + len; |
| 7840 | } |
| 7841 | separator = ';'; |
| 7842 | break; |
| 7843 | case PR_O2_AS_M_QS: |
| 7844 | end_params = (char *) begin + len; |
| 7845 | separator = '&'; |
| 7846 | break; |
| 7847 | default: |
| 7848 | /* unknown mode, shouldn't happen */ |
| 7849 | return; |
| 7850 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7851 | |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 7852 | cur_param = next_param = end_params; |
| 7853 | while (cur_param > first_param) { |
| 7854 | cur_param--; |
| 7855 | if ((cur_param[0] == separator) || (cur_param == first_param)) { |
| 7856 | /* let's see if this is the appsession parameter */ |
| 7857 | if ((cur_param + t->be->appsession_name_len + 1 < next_param) && |
| 7858 | ((t->be->options2 & PR_O2_AS_PFX) || cur_param[t->be->appsession_name_len + 1] == '=') && |
| 7859 | (strncasecmp(cur_param + 1, t->be->appsession_name, t->be->appsession_name_len) == 0)) { |
| 7860 | /* Cool... it's the right one */ |
| 7861 | cur_param += t->be->appsession_name_len + (t->be->options2 & PR_O2_AS_PFX ? 1 : 2); |
| 7862 | value_len = MIN(t->be->appsession_len, next_param - cur_param); |
| 7863 | if (value_len > 0) { |
| 7864 | manage_client_side_appsession(t, cur_param, value_len); |
| 7865 | } |
| 7866 | break; |
| 7867 | } |
| 7868 | next_param = cur_param; |
| 7869 | } |
| 7870 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7871 | #if defined(DEBUG_HASH) |
Aleksandar Lazic | 697bbb0 | 2008-08-13 19:57:02 +0200 | [diff] [blame] | 7872 | Alert("get_srv_from_appsession\n"); |
Willy Tarreau | 51041c7 | 2007-09-09 21:56:53 +0200 | [diff] [blame] | 7873 | appsession_hash_dump(&(t->be->htbl_proxy)); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7874 | #endif |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7875 | } |
| 7876 | |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 7877 | /* |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 7878 | * 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] | 7879 | * for the current backend. |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 7880 | * |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 7881 | * 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] | 7882 | * uri_auth field is valid. |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 7883 | * |
Krzysztof Piotr Oledzki | 8c8bd45 | 2010-01-29 19:29:32 +0100 | [diff] [blame] | 7884 | * Returns 1 if stats should be provided, otherwise 0. |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 7885 | */ |
Willy Tarreau | 295a837 | 2011-03-10 11:25:07 +0100 | [diff] [blame] | 7886 | 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] | 7887 | { |
| 7888 | struct uri_auth *uri_auth = backend->uri_auth; |
Willy Tarreau | 3a215be | 2012-03-09 21:39:51 +0100 | [diff] [blame] | 7889 | struct http_msg *msg = &txn->req; |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 7890 | const char *uri = msg->chn->buf->p+ msg->sl.rq.u; |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 7891 | |
Krzysztof Piotr Oledzki | 8c8bd45 | 2010-01-29 19:29:32 +0100 | [diff] [blame] | 7892 | if (!uri_auth) |
| 7893 | return 0; |
| 7894 | |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 7895 | 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] | 7896 | return 0; |
| 7897 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7898 | /* check URI size */ |
Willy Tarreau | 3a215be | 2012-03-09 21:39:51 +0100 | [diff] [blame] | 7899 | if (uri_auth->uri_len > msg->sl.rq.u_l) |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 7900 | return 0; |
| 7901 | |
Willy Tarreau | 414e9bb | 2013-11-23 00:30:38 +0100 | [diff] [blame] | 7902 | if (memcmp(uri, uri_auth->uri_prefix, uri_auth->uri_len) != 0) |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 7903 | return 0; |
| 7904 | |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 7905 | return 1; |
| 7906 | } |
| 7907 | |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 7908 | /* |
| 7909 | * 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] | 7910 | * By default it tries to report the error position as msg->err_pos. However if |
| 7911 | * this one is not set, it will then report msg->next, which is the last known |
| 7912 | * 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] | 7913 | * displays buffers as a contiguous area starting at buf->p. |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 7914 | */ |
| 7915 | void http_capture_bad_message(struct error_snapshot *es, struct session *s, |
Willy Tarreau | 8a0cef2 | 2012-03-09 13:39:23 +0100 | [diff] [blame] | 7916 | struct http_msg *msg, |
Willy Tarreau | 3770f23 | 2013-12-07 00:01:53 +0100 | [diff] [blame] | 7917 | enum ht_state state, struct proxy *other_end) |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 7918 | { |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 7919 | struct channel *chn = msg->chn; |
Willy Tarreau | 69d8c5d | 2012-05-08 09:44:41 +0200 | [diff] [blame] | 7920 | int len1, len2; |
Willy Tarreau | 8a0cef2 | 2012-03-09 13:39:23 +0100 | [diff] [blame] | 7921 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 7922 | es->len = MIN(chn->buf->i, sizeof(es->buf)); |
| 7923 | len1 = chn->buf->data + chn->buf->size - chn->buf->p; |
Willy Tarreau | 69d8c5d | 2012-05-08 09:44:41 +0200 | [diff] [blame] | 7924 | len1 = MIN(len1, es->len); |
| 7925 | len2 = es->len - len1; /* remaining data if buffer wraps */ |
| 7926 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 7927 | memcpy(es->buf, chn->buf->p, len1); |
Willy Tarreau | 69d8c5d | 2012-05-08 09:44:41 +0200 | [diff] [blame] | 7928 | if (len2) |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 7929 | memcpy(es->buf + len1, chn->buf->data, len2); |
Willy Tarreau | 81f2fb9 | 2010-12-12 13:09:08 +0100 | [diff] [blame] | 7930 | |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 7931 | if (msg->err_pos >= 0) |
Willy Tarreau | 69d8c5d | 2012-05-08 09:44:41 +0200 | [diff] [blame] | 7932 | es->pos = msg->err_pos; |
Willy Tarreau | 81f2fb9 | 2010-12-12 13:09:08 +0100 | [diff] [blame] | 7933 | else |
Willy Tarreau | 69d8c5d | 2012-05-08 09:44:41 +0200 | [diff] [blame] | 7934 | es->pos = msg->next; |
Willy Tarreau | 81f2fb9 | 2010-12-12 13:09:08 +0100 | [diff] [blame] | 7935 | |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 7936 | es->when = date; // user-visible date |
| 7937 | es->sid = s->uniq_id; |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 7938 | es->srv = objt_server(s->target); |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 7939 | es->oe = other_end; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 7940 | if (objt_conn(s->req->prod->end)) |
| 7941 | es->src = __objt_conn(s->req->prod->end)->addr.from; |
| 7942 | else |
| 7943 | memset(&es->src, 0, sizeof(es->src)); |
| 7944 | |
Willy Tarreau | 078272e | 2010-12-12 12:46:33 +0100 | [diff] [blame] | 7945 | es->state = state; |
Willy Tarreau | 10479e4 | 2010-12-12 14:00:34 +0100 | [diff] [blame] | 7946 | es->ev_id = error_snapshot_id++; |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 7947 | es->b_flags = chn->flags; |
Willy Tarreau | d04b1bc | 2012-05-08 11:03:10 +0200 | [diff] [blame] | 7948 | es->s_flags = s->flags; |
| 7949 | es->t_flags = s->txn.flags; |
| 7950 | es->m_flags = msg->flags; |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 7951 | es->b_out = chn->buf->o; |
| 7952 | es->b_wrap = chn->buf->data + chn->buf->size - chn->buf->p; |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 7953 | es->b_tot = chn->total; |
Willy Tarreau | d04b1bc | 2012-05-08 11:03:10 +0200 | [diff] [blame] | 7954 | es->m_clen = msg->chunk_len; |
| 7955 | es->m_blen = msg->body_len; |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 7956 | } |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 7957 | |
Willy Tarreau | 294c473 | 2011-12-16 21:35:50 +0100 | [diff] [blame] | 7958 | /* Return in <vptr> and <vlen> the pointer and length of occurrence <occ> of |
| 7959 | * header whose name is <hname> of length <hlen>. If <ctx> is null, lookup is |
| 7960 | * performed over the whole headers. Otherwise it must contain a valid header |
| 7961 | * context, initialised with ctx->idx=0 for the first lookup in a series. If |
| 7962 | * <occ> is positive or null, occurrence #occ from the beginning (or last ctx) |
| 7963 | * is returned. Occ #0 and #1 are equivalent. If <occ> is negative (and no less |
| 7964 | * 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] | 7965 | * -1. The value fetch stops at commas, so this function is suited for use with |
| 7966 | * list headers. |
Willy Tarreau | 294c473 | 2011-12-16 21:35:50 +0100 | [diff] [blame] | 7967 | * 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] | 7968 | */ |
Willy Tarreau | 185b5c4 | 2012-04-26 15:11:51 +0200 | [diff] [blame] | 7969 | 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] | 7970 | struct hdr_idx *idx, int occ, |
| 7971 | struct hdr_ctx *ctx, char **vptr, int *vlen) |
Willy Tarreau | bce7088 | 2009-09-07 11:51:47 +0200 | [diff] [blame] | 7972 | { |
Willy Tarreau | 294c473 | 2011-12-16 21:35:50 +0100 | [diff] [blame] | 7973 | struct hdr_ctx local_ctx; |
| 7974 | char *ptr_hist[MAX_HDR_HISTORY]; |
| 7975 | int len_hist[MAX_HDR_HISTORY]; |
Willy Tarreau | bce7088 | 2009-09-07 11:51:47 +0200 | [diff] [blame] | 7976 | unsigned int hist_ptr; |
Willy Tarreau | 294c473 | 2011-12-16 21:35:50 +0100 | [diff] [blame] | 7977 | int found; |
Willy Tarreau | bce7088 | 2009-09-07 11:51:47 +0200 | [diff] [blame] | 7978 | |
Willy Tarreau | 294c473 | 2011-12-16 21:35:50 +0100 | [diff] [blame] | 7979 | if (!ctx) { |
| 7980 | local_ctx.idx = 0; |
| 7981 | ctx = &local_ctx; |
| 7982 | } |
| 7983 | |
Willy Tarreau | bce7088 | 2009-09-07 11:51:47 +0200 | [diff] [blame] | 7984 | if (occ >= 0) { |
Willy Tarreau | 294c473 | 2011-12-16 21:35:50 +0100 | [diff] [blame] | 7985 | /* search from the beginning */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 7986 | while (http_find_header2(hname, hlen, msg->chn->buf->p, idx, ctx)) { |
Willy Tarreau | bce7088 | 2009-09-07 11:51:47 +0200 | [diff] [blame] | 7987 | occ--; |
| 7988 | if (occ <= 0) { |
Willy Tarreau | 294c473 | 2011-12-16 21:35:50 +0100 | [diff] [blame] | 7989 | *vptr = ctx->line + ctx->val; |
| 7990 | *vlen = ctx->vlen; |
| 7991 | return 1; |
Willy Tarreau | bce7088 | 2009-09-07 11:51:47 +0200 | [diff] [blame] | 7992 | } |
| 7993 | } |
Willy Tarreau | 294c473 | 2011-12-16 21:35:50 +0100 | [diff] [blame] | 7994 | return 0; |
Willy Tarreau | bce7088 | 2009-09-07 11:51:47 +0200 | [diff] [blame] | 7995 | } |
| 7996 | |
| 7997 | /* negative occurrence, we scan all the list then walk back */ |
| 7998 | if (-occ > MAX_HDR_HISTORY) |
| 7999 | return 0; |
| 8000 | |
Willy Tarreau | 294c473 | 2011-12-16 21:35:50 +0100 | [diff] [blame] | 8001 | found = hist_ptr = 0; |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 8002 | while (http_find_header2(hname, hlen, msg->chn->buf->p, idx, ctx)) { |
Willy Tarreau | 294c473 | 2011-12-16 21:35:50 +0100 | [diff] [blame] | 8003 | ptr_hist[hist_ptr] = ctx->line + ctx->val; |
| 8004 | len_hist[hist_ptr] = ctx->vlen; |
| 8005 | if (++hist_ptr >= MAX_HDR_HISTORY) |
Willy Tarreau | bce7088 | 2009-09-07 11:51:47 +0200 | [diff] [blame] | 8006 | hist_ptr = 0; |
| 8007 | found++; |
| 8008 | } |
| 8009 | if (-occ > found) |
| 8010 | return 0; |
| 8011 | /* 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] | 8012 | * find occurrence -occ. 0 <= hist_ptr < MAX_HDR_HISTORY, and we have |
| 8013 | * -10 <= occ <= -1. So we have to check [hist_ptr%MAX_HDR_HISTORY+occ] |
| 8014 | * to remain in the 0..9 range. |
Willy Tarreau | bce7088 | 2009-09-07 11:51:47 +0200 | [diff] [blame] | 8015 | */ |
Willy Tarreau | 67dad27 | 2013-06-12 22:27:44 +0200 | [diff] [blame] | 8016 | hist_ptr += occ + MAX_HDR_HISTORY; |
Willy Tarreau | bce7088 | 2009-09-07 11:51:47 +0200 | [diff] [blame] | 8017 | if (hist_ptr >= MAX_HDR_HISTORY) |
| 8018 | hist_ptr -= MAX_HDR_HISTORY; |
Willy Tarreau | 294c473 | 2011-12-16 21:35:50 +0100 | [diff] [blame] | 8019 | *vptr = ptr_hist[hist_ptr]; |
| 8020 | *vlen = len_hist[hist_ptr]; |
| 8021 | return 1; |
Willy Tarreau | bce7088 | 2009-09-07 11:51:47 +0200 | [diff] [blame] | 8022 | } |
| 8023 | |
Willy Tarreau | 04ff9f1 | 2013-06-10 18:39:42 +0200 | [diff] [blame] | 8024 | /* Return in <vptr> and <vlen> the pointer and length of occurrence <occ> of |
| 8025 | * header whose name is <hname> of length <hlen>. If <ctx> is null, lookup is |
| 8026 | * performed over the whole headers. Otherwise it must contain a valid header |
| 8027 | * context, initialised with ctx->idx=0 for the first lookup in a series. If |
| 8028 | * <occ> is positive or null, occurrence #occ from the beginning (or last ctx) |
| 8029 | * is returned. Occ #0 and #1 are equivalent. If <occ> is negative (and no less |
| 8030 | * than -MAX_HDR_HISTORY), the occurrence is counted from the last one which is |
| 8031 | * -1. This function differs from http_get_hdr() in that it only returns full |
| 8032 | * line header values and does not stop at commas. |
| 8033 | * The return value is 0 if nothing was found, or non-zero otherwise. |
| 8034 | */ |
| 8035 | unsigned int http_get_fhdr(const struct http_msg *msg, const char *hname, int hlen, |
| 8036 | struct hdr_idx *idx, int occ, |
| 8037 | struct hdr_ctx *ctx, char **vptr, int *vlen) |
| 8038 | { |
| 8039 | struct hdr_ctx local_ctx; |
| 8040 | char *ptr_hist[MAX_HDR_HISTORY]; |
| 8041 | int len_hist[MAX_HDR_HISTORY]; |
| 8042 | unsigned int hist_ptr; |
| 8043 | int found; |
| 8044 | |
| 8045 | if (!ctx) { |
| 8046 | local_ctx.idx = 0; |
| 8047 | ctx = &local_ctx; |
| 8048 | } |
| 8049 | |
| 8050 | if (occ >= 0) { |
| 8051 | /* search from the beginning */ |
| 8052 | while (http_find_full_header2(hname, hlen, msg->chn->buf->p, idx, ctx)) { |
| 8053 | occ--; |
| 8054 | if (occ <= 0) { |
| 8055 | *vptr = ctx->line + ctx->val; |
| 8056 | *vlen = ctx->vlen; |
| 8057 | return 1; |
| 8058 | } |
| 8059 | } |
| 8060 | return 0; |
| 8061 | } |
| 8062 | |
| 8063 | /* negative occurrence, we scan all the list then walk back */ |
| 8064 | if (-occ > MAX_HDR_HISTORY) |
| 8065 | return 0; |
| 8066 | |
| 8067 | found = hist_ptr = 0; |
| 8068 | while (http_find_full_header2(hname, hlen, msg->chn->buf->p, idx, ctx)) { |
| 8069 | ptr_hist[hist_ptr] = ctx->line + ctx->val; |
| 8070 | len_hist[hist_ptr] = ctx->vlen; |
| 8071 | if (++hist_ptr >= MAX_HDR_HISTORY) |
| 8072 | hist_ptr = 0; |
| 8073 | found++; |
| 8074 | } |
| 8075 | if (-occ > found) |
| 8076 | return 0; |
| 8077 | /* OK now we have the last occurrence in [hist_ptr-1], and we need to |
| 8078 | * find occurrence -occ, so we have to check [hist_ptr+occ]. |
| 8079 | */ |
| 8080 | hist_ptr += occ; |
| 8081 | if (hist_ptr >= MAX_HDR_HISTORY) |
| 8082 | hist_ptr -= MAX_HDR_HISTORY; |
| 8083 | *vptr = ptr_hist[hist_ptr]; |
| 8084 | *vlen = len_hist[hist_ptr]; |
| 8085 | return 1; |
| 8086 | } |
| 8087 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 8088 | /* |
Willy Tarreau | e92693a | 2012-09-24 21:13:39 +0200 | [diff] [blame] | 8089 | * Print a debug line with a header. Always stop at the first CR or LF char, |
| 8090 | * so it is safe to pass it a full buffer if needed. If <err> is not NULL, an |
| 8091 | * arrow is printed after the line which contains the pointer. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 8092 | */ |
| 8093 | void debug_hdr(const char *dir, struct session *t, const char *start, const char *end) |
| 8094 | { |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 8095 | int max; |
| 8096 | chunk_printf(&trash, "%08x:%s.%s[%04x:%04x]: ", t->uniq_id, t->be->id, |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 8097 | dir, |
| 8098 | objt_conn(t->req->prod->end) ? (unsigned short)objt_conn(t->req->prod->end)->t.sock.fd : -1, |
| 8099 | objt_conn(t->req->cons->end) ? (unsigned short)objt_conn(t->req->cons->end)->t.sock.fd : -1); |
Willy Tarreau | e92693a | 2012-09-24 21:13:39 +0200 | [diff] [blame] | 8100 | |
| 8101 | for (max = 0; start + max < end; max++) |
| 8102 | if (start[max] == '\r' || start[max] == '\n') |
| 8103 | break; |
| 8104 | |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 8105 | UBOUND(max, trash.size - trash.len - 3); |
| 8106 | trash.len += strlcpy2(trash.str + trash.len, start, max + 1); |
| 8107 | trash.str[trash.len++] = '\n'; |
Willy Tarreau | 89efaed | 2013-12-13 15:14:55 +0100 | [diff] [blame] | 8108 | shut_your_big_mouth_gcc(write(1, trash.str, trash.len)); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 8109 | } |
| 8110 | |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 8111 | /* |
| 8112 | * Initialize a new HTTP transaction for session <s>. It is assumed that all |
| 8113 | * the required fields are properly allocated and that we only need to (re)init |
| 8114 | * them. This should be used before processing any new request. |
| 8115 | */ |
| 8116 | void http_init_txn(struct session *s) |
| 8117 | { |
| 8118 | struct http_txn *txn = &s->txn; |
| 8119 | struct proxy *fe = s->fe; |
| 8120 | |
| 8121 | txn->flags = 0; |
| 8122 | txn->status = -1; |
| 8123 | |
Willy Tarreau | f64d141 | 2010-10-07 20:06:11 +0200 | [diff] [blame] | 8124 | txn->cookie_first_date = 0; |
| 8125 | txn->cookie_last_date = 0; |
| 8126 | |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 8127 | txn->req.flags = 0; |
Willy Tarreau | 2692736 | 2012-05-18 23:22:52 +0200 | [diff] [blame] | 8128 | 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] | 8129 | txn->req.next = 0; |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 8130 | txn->rsp.flags = 0; |
Willy Tarreau | 2692736 | 2012-05-18 23:22:52 +0200 | [diff] [blame] | 8131 | 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] | 8132 | txn->rsp.next = 0; |
Willy Tarreau | 124d991 | 2011-03-01 20:30:48 +0100 | [diff] [blame] | 8133 | txn->req.chunk_len = 0LL; |
| 8134 | txn->req.body_len = 0LL; |
| 8135 | txn->rsp.chunk_len = 0LL; |
| 8136 | txn->rsp.body_len = 0LL; |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 8137 | txn->req.msg_state = HTTP_MSG_RQBEFORE; /* at the very beginning of the request */ |
| 8138 | txn->rsp.msg_state = HTTP_MSG_RPBEFORE; /* at the very beginning of the response */ |
Willy Tarreau | 394db37 | 2012-10-12 22:40:39 +0200 | [diff] [blame] | 8139 | txn->req.chn = s->req; |
| 8140 | txn->rsp.chn = s->rep; |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 8141 | |
| 8142 | txn->auth.method = HTTP_AUTH_UNKNOWN; |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 8143 | |
| 8144 | txn->req.err_pos = txn->rsp.err_pos = -2; /* block buggy requests/responses */ |
| 8145 | if (fe->options2 & PR_O2_REQBUG_OK) |
| 8146 | txn->req.err_pos = -1; /* let buggy requests pass */ |
| 8147 | |
Willy Tarreau | 4602363 | 2010-01-07 22:51:47 +0100 | [diff] [blame] | 8148 | if (txn->req.cap) |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 8149 | memset(txn->req.cap, 0, fe->nb_req_cap * sizeof(void *)); |
| 8150 | |
Willy Tarreau | 4602363 | 2010-01-07 22:51:47 +0100 | [diff] [blame] | 8151 | if (txn->rsp.cap) |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 8152 | memset(txn->rsp.cap, 0, fe->nb_rsp_cap * sizeof(void *)); |
| 8153 | |
| 8154 | if (txn->hdr_idx.v) |
| 8155 | hdr_idx_init(&txn->hdr_idx); |
| 8156 | } |
| 8157 | |
| 8158 | /* to be used at the end of a transaction */ |
| 8159 | void http_end_txn(struct session *s) |
| 8160 | { |
| 8161 | struct http_txn *txn = &s->txn; |
| 8162 | |
| 8163 | /* these ones will have been dynamically allocated */ |
| 8164 | pool_free2(pool2_requri, txn->uri); |
| 8165 | pool_free2(pool2_capture, txn->cli_cookie); |
| 8166 | pool_free2(pool2_capture, txn->srv_cookie); |
Willy Tarreau | a3377ee | 2010-01-10 10:49:11 +0100 | [diff] [blame] | 8167 | pool_free2(apools.sessid, txn->sessid); |
William Lallemand | a73203e | 2012-03-12 12:48:57 +0100 | [diff] [blame] | 8168 | pool_free2(pool2_uniqueid, s->unique_id); |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 8169 | |
William Lallemand | a73203e | 2012-03-12 12:48:57 +0100 | [diff] [blame] | 8170 | s->unique_id = NULL; |
Willy Tarreau | a3377ee | 2010-01-10 10:49:11 +0100 | [diff] [blame] | 8171 | txn->sessid = NULL; |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 8172 | txn->uri = NULL; |
| 8173 | txn->srv_cookie = NULL; |
| 8174 | txn->cli_cookie = NULL; |
Willy Tarreau | 4602363 | 2010-01-07 22:51:47 +0100 | [diff] [blame] | 8175 | |
| 8176 | if (txn->req.cap) { |
| 8177 | struct cap_hdr *h; |
| 8178 | for (h = s->fe->req_cap; h; h = h->next) |
| 8179 | pool_free2(h->pool, txn->req.cap[h->index]); |
| 8180 | memset(txn->req.cap, 0, s->fe->nb_req_cap * sizeof(void *)); |
| 8181 | } |
| 8182 | |
| 8183 | if (txn->rsp.cap) { |
| 8184 | struct cap_hdr *h; |
| 8185 | for (h = s->fe->rsp_cap; h; h = h->next) |
| 8186 | pool_free2(h->pool, txn->rsp.cap[h->index]); |
| 8187 | memset(txn->rsp.cap, 0, s->fe->nb_rsp_cap * sizeof(void *)); |
| 8188 | } |
| 8189 | |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 8190 | } |
| 8191 | |
| 8192 | /* to be used at the end of a transaction to prepare a new one */ |
| 8193 | void http_reset_txn(struct session *s) |
| 8194 | { |
| 8195 | http_end_txn(s); |
| 8196 | http_init_txn(s); |
| 8197 | |
| 8198 | s->be = s->fe; |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 8199 | s->logs.logwait = s->fe->to_log; |
Willy Tarreau | abcd514 | 2013-06-11 17:18:02 +0200 | [diff] [blame] | 8200 | s->logs.level = 0; |
Simon Horman | af51495 | 2011-06-21 14:34:57 +0900 | [diff] [blame] | 8201 | session_del_srv_conn(s); |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 8202 | s->target = NULL; |
Emeric Brun | b982a3d | 2010-01-04 15:45:53 +0100 | [diff] [blame] | 8203 | /* re-init store persistence */ |
| 8204 | s->store_count = 0; |
| 8205 | |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 8206 | s->pend_pos = NULL; |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 8207 | |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 8208 | s->req->flags |= CF_READ_DONTWAIT; /* one read is usually enough */ |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 8209 | |
Willy Tarreau | 739cfba | 2010-01-25 23:11:14 +0100 | [diff] [blame] | 8210 | /* We must trim any excess data from the response buffer, because we |
| 8211 | * may have blocked an invalid response from a server that we don't |
| 8212 | * want to accidentely forward once we disable the analysers, nor do |
| 8213 | * we want those data to come along with next response. A typical |
| 8214 | * example of such data would be from a buggy server responding to |
| 8215 | * a HEAD with some data, or sending more than the advertised |
| 8216 | * content-length. |
| 8217 | */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 8218 | if (unlikely(s->rep->buf->i)) |
| 8219 | s->rep->buf->i = 0; |
Willy Tarreau | 739cfba | 2010-01-25 23:11:14 +0100 | [diff] [blame] | 8220 | |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 8221 | s->req->rto = s->fe->timeout.client; |
Willy Tarreau | d04e858 | 2010-05-31 12:31:35 +0200 | [diff] [blame] | 8222 | s->req->wto = TICK_ETERNITY; |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 8223 | |
Willy Tarreau | d04e858 | 2010-05-31 12:31:35 +0200 | [diff] [blame] | 8224 | s->rep->rto = TICK_ETERNITY; |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 8225 | s->rep->wto = s->fe->timeout.client; |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 8226 | |
| 8227 | s->req->rex = TICK_ETERNITY; |
| 8228 | s->req->wex = TICK_ETERNITY; |
| 8229 | s->req->analyse_exp = TICK_ETERNITY; |
| 8230 | s->rep->rex = TICK_ETERNITY; |
| 8231 | s->rep->wex = TICK_ETERNITY; |
| 8232 | s->rep->analyse_exp = TICK_ETERNITY; |
| 8233 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 8234 | |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 8235 | void free_http_req_rules(struct list *r) { |
| 8236 | struct http_req_rule *tr, *pr; |
| 8237 | |
| 8238 | list_for_each_entry_safe(pr, tr, r, list) { |
| 8239 | LIST_DEL(&pr->list); |
Willy Tarreau | 20b0de5 | 2012-12-24 15:45:22 +0100 | [diff] [blame] | 8240 | if (pr->action == HTTP_REQ_ACT_AUTH) |
Willy Tarreau | 5c2e198 | 2012-12-24 12:00:25 +0100 | [diff] [blame] | 8241 | free(pr->arg.auth.realm); |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 8242 | |
| 8243 | free(pr); |
| 8244 | } |
| 8245 | } |
| 8246 | |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 8247 | /* parse an "http-request" rule */ |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 8248 | struct http_req_rule *parse_http_req_cond(const char **args, const char *file, int linenum, struct proxy *proxy) |
| 8249 | { |
| 8250 | struct http_req_rule *rule; |
| 8251 | int cur_arg; |
| 8252 | |
| 8253 | rule = (struct http_req_rule*)calloc(1, sizeof(struct http_req_rule)); |
| 8254 | if (!rule) { |
| 8255 | Alert("parsing [%s:%d]: out of memory.\n", file, linenum); |
Willy Tarreau | 81499eb | 2012-12-27 12:19:02 +0100 | [diff] [blame] | 8256 | goto out_err; |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 8257 | } |
| 8258 | |
Willy Tarreau | 5c2e198 | 2012-12-24 12:00:25 +0100 | [diff] [blame] | 8259 | if (!strcmp(args[0], "allow")) { |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 8260 | rule->action = HTTP_REQ_ACT_ALLOW; |
| 8261 | cur_arg = 1; |
| 8262 | } else if (!strcmp(args[0], "deny")) { |
| 8263 | rule->action = HTTP_REQ_ACT_DENY; |
| 8264 | cur_arg = 1; |
Willy Tarreau | ccbcc37 | 2012-12-27 12:37:57 +0100 | [diff] [blame] | 8265 | } else if (!strcmp(args[0], "tarpit")) { |
| 8266 | rule->action = HTTP_REQ_ACT_TARPIT; |
| 8267 | cur_arg = 1; |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 8268 | } else if (!strcmp(args[0], "auth")) { |
Willy Tarreau | 20b0de5 | 2012-12-24 15:45:22 +0100 | [diff] [blame] | 8269 | rule->action = HTTP_REQ_ACT_AUTH; |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 8270 | cur_arg = 1; |
| 8271 | |
| 8272 | while(*args[cur_arg]) { |
| 8273 | if (!strcmp(args[cur_arg], "realm")) { |
Willy Tarreau | 5c2e198 | 2012-12-24 12:00:25 +0100 | [diff] [blame] | 8274 | rule->arg.auth.realm = strdup(args[cur_arg + 1]); |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 8275 | cur_arg+=2; |
| 8276 | continue; |
| 8277 | } else |
| 8278 | break; |
| 8279 | } |
Willy Tarreau | f4c43c1 | 2013-06-11 17:01:13 +0200 | [diff] [blame] | 8280 | } else if (!strcmp(args[0], "set-nice")) { |
| 8281 | rule->action = HTTP_REQ_ACT_SET_NICE; |
| 8282 | cur_arg = 1; |
| 8283 | |
| 8284 | if (!*args[cur_arg] || |
| 8285 | (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) { |
| 8286 | Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (integer value).\n", |
| 8287 | file, linenum, args[0]); |
| 8288 | goto out_err; |
| 8289 | } |
| 8290 | rule->arg.nice = atoi(args[cur_arg]); |
| 8291 | if (rule->arg.nice < -1024) |
| 8292 | rule->arg.nice = -1024; |
| 8293 | else if (rule->arg.nice > 1024) |
| 8294 | rule->arg.nice = 1024; |
| 8295 | cur_arg++; |
Willy Tarreau | 42cf39e | 2013-06-11 18:51:32 +0200 | [diff] [blame] | 8296 | } else if (!strcmp(args[0], "set-tos")) { |
| 8297 | #ifdef IP_TOS |
| 8298 | char *err; |
| 8299 | rule->action = HTTP_REQ_ACT_SET_TOS; |
| 8300 | cur_arg = 1; |
| 8301 | |
| 8302 | if (!*args[cur_arg] || |
| 8303 | (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) { |
| 8304 | Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (integer/hex value).\n", |
| 8305 | file, linenum, args[0]); |
| 8306 | goto out_err; |
| 8307 | } |
| 8308 | |
| 8309 | rule->arg.tos = strtol(args[cur_arg], &err, 0); |
| 8310 | if (err && *err != '\0') { |
| 8311 | Alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-request %s' (integer/hex value expected).\n", |
| 8312 | file, linenum, err, args[0]); |
| 8313 | goto out_err; |
| 8314 | } |
| 8315 | cur_arg++; |
| 8316 | #else |
| 8317 | Alert("parsing [%s:%d]: 'http-request %s' is not supported on this platform (IP_TOS undefined).\n", file, linenum, args[0]); |
| 8318 | goto out_err; |
| 8319 | #endif |
Willy Tarreau | 51347ed | 2013-06-11 19:34:13 +0200 | [diff] [blame] | 8320 | } else if (!strcmp(args[0], "set-mark")) { |
| 8321 | #ifdef SO_MARK |
| 8322 | char *err; |
| 8323 | rule->action = HTTP_REQ_ACT_SET_MARK; |
| 8324 | cur_arg = 1; |
| 8325 | |
| 8326 | if (!*args[cur_arg] || |
| 8327 | (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) { |
| 8328 | Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (integer/hex value).\n", |
| 8329 | file, linenum, args[0]); |
| 8330 | goto out_err; |
| 8331 | } |
| 8332 | |
| 8333 | rule->arg.mark = strtoul(args[cur_arg], &err, 0); |
| 8334 | if (err && *err != '\0') { |
| 8335 | Alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-request %s' (integer/hex value expected).\n", |
| 8336 | file, linenum, err, args[0]); |
| 8337 | goto out_err; |
| 8338 | } |
| 8339 | cur_arg++; |
| 8340 | global.last_checks |= LSTCHK_NETADM; |
| 8341 | #else |
| 8342 | Alert("parsing [%s:%d]: 'http-request %s' is not supported on this platform (SO_MARK undefined).\n", file, linenum, args[0]); |
| 8343 | goto out_err; |
| 8344 | #endif |
Willy Tarreau | 9a355ec | 2013-06-11 17:45:46 +0200 | [diff] [blame] | 8345 | } else if (!strcmp(args[0], "set-log-level")) { |
| 8346 | rule->action = HTTP_REQ_ACT_SET_LOGL; |
| 8347 | cur_arg = 1; |
| 8348 | |
| 8349 | if (!*args[cur_arg] || |
| 8350 | (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) { |
| 8351 | bad_log_level: |
| 8352 | Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (log level name or 'silent').\n", |
| 8353 | file, linenum, args[0]); |
| 8354 | goto out_err; |
| 8355 | } |
| 8356 | if (strcmp(args[cur_arg], "silent") == 0) |
| 8357 | rule->arg.loglevel = -1; |
| 8358 | else if ((rule->arg.loglevel = get_log_level(args[cur_arg]) + 1) == 0) |
| 8359 | goto bad_log_level; |
| 8360 | cur_arg++; |
Willy Tarreau | 20b0de5 | 2012-12-24 15:45:22 +0100 | [diff] [blame] | 8361 | } else if (strcmp(args[0], "add-header") == 0 || strcmp(args[0], "set-header") == 0) { |
| 8362 | rule->action = *args[0] == 'a' ? HTTP_REQ_ACT_ADD_HDR : HTTP_REQ_ACT_SET_HDR; |
| 8363 | cur_arg = 1; |
| 8364 | |
Willy Tarreau | 8d1c516 | 2013-04-03 14:13:58 +0200 | [diff] [blame] | 8365 | if (!*args[cur_arg] || !*args[cur_arg+1] || |
| 8366 | (*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] | 8367 | Alert("parsing [%s:%d]: 'http-request %s' expects exactly 2 arguments.\n", |
| 8368 | file, linenum, args[0]); |
Willy Tarreau | 81499eb | 2012-12-27 12:19:02 +0100 | [diff] [blame] | 8369 | goto out_err; |
Willy Tarreau | 20b0de5 | 2012-12-24 15:45:22 +0100 | [diff] [blame] | 8370 | } |
| 8371 | |
| 8372 | rule->arg.hdr_add.name = strdup(args[cur_arg]); |
| 8373 | rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name); |
| 8374 | LIST_INIT(&rule->arg.hdr_add.fmt); |
Willy Tarreau | a4312fa | 2013-04-02 16:34:32 +0200 | [diff] [blame] | 8375 | |
Thierry FOURNIER | 1c0054f | 2013-11-20 15:09:52 +0100 | [diff] [blame] | 8376 | proxy->conf.args.ctx = ARGC_HRQ; |
Willy Tarreau | 434c57c | 2013-01-08 01:10:24 +0100 | [diff] [blame] | 8377 | parse_logformat_string(args[cur_arg + 1], proxy, &rule->arg.hdr_add.fmt, 0, |
| 8378 | (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR); |
Willy Tarreau | 20b0de5 | 2012-12-24 15:45:22 +0100 | [diff] [blame] | 8379 | cur_arg += 2; |
Willy Tarreau | 81499eb | 2012-12-27 12:19:02 +0100 | [diff] [blame] | 8380 | } else if (strcmp(args[0], "redirect") == 0) { |
| 8381 | struct redirect_rule *redir; |
Willy Tarreau | 6d4890c | 2013-11-18 18:04:25 +0100 | [diff] [blame] | 8382 | char *errmsg = NULL; |
Willy Tarreau | 81499eb | 2012-12-27 12:19:02 +0100 | [diff] [blame] | 8383 | |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 8384 | if ((redir = http_parse_redirect_rule(file, linenum, proxy, (const char **)args + 1, &errmsg, 1)) == NULL) { |
Willy Tarreau | 81499eb | 2012-12-27 12:19:02 +0100 | [diff] [blame] | 8385 | Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : %s.\n", |
| 8386 | file, linenum, proxy_type_str(proxy), proxy->id, args[0], errmsg); |
| 8387 | goto out_err; |
| 8388 | } |
| 8389 | |
| 8390 | /* this redirect rule might already contain a parsed condition which |
| 8391 | * we'll pass to the http-request rule. |
| 8392 | */ |
| 8393 | rule->action = HTTP_REQ_ACT_REDIR; |
| 8394 | rule->arg.redir = redir; |
| 8395 | rule->cond = redir->cond; |
| 8396 | redir->cond = NULL; |
| 8397 | cur_arg = 2; |
| 8398 | return rule; |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 8399 | } else { |
Willy Tarreau | 51347ed | 2013-06-11 19:34:13 +0200 | [diff] [blame] | 8400 | Alert("parsing [%s:%d]: 'http-request' expects 'allow', 'deny', 'auth', 'redirect', 'tarpit', 'add-header', 'set-header', 'set-nice', 'set-tos', 'set-mark', 'set-log-level', but got '%s'%s.\n", |
Willy Tarreau | 5c2e198 | 2012-12-24 12:00:25 +0100 | [diff] [blame] | 8401 | file, linenum, args[0], *args[0] ? "" : " (missing argument)"); |
Willy Tarreau | 81499eb | 2012-12-27 12:19:02 +0100 | [diff] [blame] | 8402 | goto out_err; |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 8403 | } |
| 8404 | |
| 8405 | if (strcmp(args[cur_arg], "if") == 0 || strcmp(args[cur_arg], "unless") == 0) { |
| 8406 | struct acl_cond *cond; |
Willy Tarreau | b7451bb | 2012-04-27 12:38:15 +0200 | [diff] [blame] | 8407 | char *errmsg = NULL; |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 8408 | |
Willy Tarreau | b7451bb | 2012-04-27 12:38:15 +0200 | [diff] [blame] | 8409 | if ((cond = build_acl_cond(file, linenum, proxy, args+cur_arg, &errmsg)) == NULL) { |
| 8410 | Alert("parsing [%s:%d] : error detected while parsing an 'http-request %s' condition : %s.\n", |
| 8411 | file, linenum, args[0], errmsg); |
| 8412 | free(errmsg); |
Willy Tarreau | 81499eb | 2012-12-27 12:19:02 +0100 | [diff] [blame] | 8413 | goto out_err; |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 8414 | } |
| 8415 | rule->cond = cond; |
| 8416 | } |
| 8417 | else if (*args[cur_arg]) { |
| 8418 | Alert("parsing [%s:%d]: 'http-request %s' expects 'realm' for 'auth' or" |
| 8419 | " either 'if' or 'unless' followed by a condition but found '%s'.\n", |
| 8420 | file, linenum, args[0], args[cur_arg]); |
Willy Tarreau | 81499eb | 2012-12-27 12:19:02 +0100 | [diff] [blame] | 8421 | goto out_err; |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 8422 | } |
| 8423 | |
| 8424 | return rule; |
Willy Tarreau | 81499eb | 2012-12-27 12:19:02 +0100 | [diff] [blame] | 8425 | out_err: |
| 8426 | free(rule); |
| 8427 | return NULL; |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 8428 | } |
| 8429 | |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 8430 | /* parse an "http-respose" rule */ |
| 8431 | struct http_res_rule *parse_http_res_cond(const char **args, const char *file, int linenum, struct proxy *proxy) |
| 8432 | { |
| 8433 | struct http_res_rule *rule; |
| 8434 | int cur_arg; |
| 8435 | |
| 8436 | rule = calloc(1, sizeof(*rule)); |
| 8437 | if (!rule) { |
| 8438 | Alert("parsing [%s:%d]: out of memory.\n", file, linenum); |
| 8439 | goto out_err; |
| 8440 | } |
| 8441 | |
| 8442 | if (!strcmp(args[0], "allow")) { |
| 8443 | rule->action = HTTP_RES_ACT_ALLOW; |
| 8444 | cur_arg = 1; |
| 8445 | } else if (!strcmp(args[0], "deny")) { |
| 8446 | rule->action = HTTP_RES_ACT_DENY; |
| 8447 | cur_arg = 1; |
Willy Tarreau | f4c43c1 | 2013-06-11 17:01:13 +0200 | [diff] [blame] | 8448 | } else if (!strcmp(args[0], "set-nice")) { |
| 8449 | rule->action = HTTP_RES_ACT_SET_NICE; |
| 8450 | cur_arg = 1; |
| 8451 | |
| 8452 | if (!*args[cur_arg] || |
| 8453 | (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) { |
| 8454 | Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (integer value).\n", |
| 8455 | file, linenum, args[0]); |
| 8456 | goto out_err; |
| 8457 | } |
| 8458 | rule->arg.nice = atoi(args[cur_arg]); |
| 8459 | if (rule->arg.nice < -1024) |
| 8460 | rule->arg.nice = -1024; |
| 8461 | else if (rule->arg.nice > 1024) |
| 8462 | rule->arg.nice = 1024; |
| 8463 | cur_arg++; |
Willy Tarreau | 42cf39e | 2013-06-11 18:51:32 +0200 | [diff] [blame] | 8464 | } else if (!strcmp(args[0], "set-tos")) { |
| 8465 | #ifdef IP_TOS |
| 8466 | char *err; |
| 8467 | rule->action = HTTP_RES_ACT_SET_TOS; |
| 8468 | cur_arg = 1; |
| 8469 | |
| 8470 | if (!*args[cur_arg] || |
| 8471 | (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) { |
| 8472 | Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (integer/hex value).\n", |
| 8473 | file, linenum, args[0]); |
| 8474 | goto out_err; |
| 8475 | } |
| 8476 | |
| 8477 | rule->arg.tos = strtol(args[cur_arg], &err, 0); |
| 8478 | if (err && *err != '\0') { |
| 8479 | Alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-response %s' (integer/hex value expected).\n", |
| 8480 | file, linenum, err, args[0]); |
| 8481 | goto out_err; |
| 8482 | } |
| 8483 | cur_arg++; |
| 8484 | #else |
| 8485 | Alert("parsing [%s:%d]: 'http-response %s' is not supported on this platform (IP_TOS undefined).\n", file, linenum, args[0]); |
| 8486 | goto out_err; |
| 8487 | #endif |
Willy Tarreau | 51347ed | 2013-06-11 19:34:13 +0200 | [diff] [blame] | 8488 | } else if (!strcmp(args[0], "set-mark")) { |
| 8489 | #ifdef SO_MARK |
| 8490 | char *err; |
| 8491 | rule->action = HTTP_RES_ACT_SET_MARK; |
| 8492 | cur_arg = 1; |
| 8493 | |
| 8494 | if (!*args[cur_arg] || |
| 8495 | (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) { |
| 8496 | Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (integer/hex value).\n", |
| 8497 | file, linenum, args[0]); |
| 8498 | goto out_err; |
| 8499 | } |
| 8500 | |
| 8501 | rule->arg.mark = strtoul(args[cur_arg], &err, 0); |
| 8502 | if (err && *err != '\0') { |
| 8503 | Alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-response %s' (integer/hex value expected).\n", |
| 8504 | file, linenum, err, args[0]); |
| 8505 | goto out_err; |
| 8506 | } |
| 8507 | cur_arg++; |
| 8508 | global.last_checks |= LSTCHK_NETADM; |
| 8509 | #else |
| 8510 | Alert("parsing [%s:%d]: 'http-response %s' is not supported on this platform (SO_MARK undefined).\n", file, linenum, args[0]); |
| 8511 | goto out_err; |
| 8512 | #endif |
Willy Tarreau | 9a355ec | 2013-06-11 17:45:46 +0200 | [diff] [blame] | 8513 | } else if (!strcmp(args[0], "set-log-level")) { |
| 8514 | rule->action = HTTP_RES_ACT_SET_LOGL; |
| 8515 | cur_arg = 1; |
| 8516 | |
| 8517 | if (!*args[cur_arg] || |
| 8518 | (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) { |
| 8519 | bad_log_level: |
| 8520 | Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (log level name or 'silent').\n", |
| 8521 | file, linenum, args[0]); |
| 8522 | goto out_err; |
| 8523 | } |
| 8524 | if (strcmp(args[cur_arg], "silent") == 0) |
| 8525 | rule->arg.loglevel = -1; |
| 8526 | else if ((rule->arg.loglevel = get_log_level(args[cur_arg] + 1)) == 0) |
| 8527 | goto bad_log_level; |
| 8528 | cur_arg++; |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 8529 | } else if (strcmp(args[0], "add-header") == 0 || strcmp(args[0], "set-header") == 0) { |
| 8530 | rule->action = *args[0] == 'a' ? HTTP_RES_ACT_ADD_HDR : HTTP_RES_ACT_SET_HDR; |
| 8531 | cur_arg = 1; |
| 8532 | |
| 8533 | if (!*args[cur_arg] || !*args[cur_arg+1] || |
| 8534 | (*args[cur_arg+2] && strcmp(args[cur_arg+2], "if") != 0 && strcmp(args[cur_arg+2], "unless") != 0)) { |
| 8535 | Alert("parsing [%s:%d]: 'http-response %s' expects exactly 2 arguments.\n", |
| 8536 | file, linenum, args[0]); |
| 8537 | goto out_err; |
| 8538 | } |
| 8539 | |
| 8540 | rule->arg.hdr_add.name = strdup(args[cur_arg]); |
| 8541 | rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name); |
| 8542 | LIST_INIT(&rule->arg.hdr_add.fmt); |
| 8543 | |
Thierry FOURNIER | 1c0054f | 2013-11-20 15:09:52 +0100 | [diff] [blame] | 8544 | proxy->conf.args.ctx = ARGC_HRS; |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 8545 | parse_logformat_string(args[cur_arg + 1], proxy, &rule->arg.hdr_add.fmt, 0, |
| 8546 | (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR); |
| 8547 | cur_arg += 2; |
| 8548 | } else { |
Willy Tarreau | 51347ed | 2013-06-11 19:34:13 +0200 | [diff] [blame] | 8549 | Alert("parsing [%s:%d]: 'http-response' expects 'allow', 'deny', 'redirect', 'add-header', 'set-header', 'set-nice', 'set-tos', 'set-mark', 'set-log-level', but got '%s'%s.\n", |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 8550 | file, linenum, args[0], *args[0] ? "" : " (missing argument)"); |
| 8551 | goto out_err; |
| 8552 | } |
| 8553 | |
| 8554 | if (strcmp(args[cur_arg], "if") == 0 || strcmp(args[cur_arg], "unless") == 0) { |
| 8555 | struct acl_cond *cond; |
| 8556 | char *errmsg = NULL; |
| 8557 | |
| 8558 | if ((cond = build_acl_cond(file, linenum, proxy, args+cur_arg, &errmsg)) == NULL) { |
| 8559 | Alert("parsing [%s:%d] : error detected while parsing an 'http-response %s' condition : %s.\n", |
| 8560 | file, linenum, args[0], errmsg); |
| 8561 | free(errmsg); |
| 8562 | goto out_err; |
| 8563 | } |
| 8564 | rule->cond = cond; |
| 8565 | } |
| 8566 | else if (*args[cur_arg]) { |
| 8567 | Alert("parsing [%s:%d]: 'http-response %s' expects" |
| 8568 | " either 'if' or 'unless' followed by a condition but found '%s'.\n", |
| 8569 | file, linenum, args[0], args[cur_arg]); |
| 8570 | goto out_err; |
| 8571 | } |
| 8572 | |
| 8573 | return rule; |
| 8574 | out_err: |
| 8575 | free(rule); |
| 8576 | return NULL; |
| 8577 | } |
| 8578 | |
Willy Tarreau | 4baae24 | 2012-12-27 12:00:31 +0100 | [diff] [blame] | 8579 | /* 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] | 8580 | * with <err> filled with the error message. If <use_fmt> is not null, builds a |
| 8581 | * dynamic log-format rule instead of a static string. |
Willy Tarreau | 4baae24 | 2012-12-27 12:00:31 +0100 | [diff] [blame] | 8582 | */ |
| 8583 | struct redirect_rule *http_parse_redirect_rule(const char *file, int linenum, struct proxy *curproxy, |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 8584 | const char **args, char **errmsg, int use_fmt) |
Willy Tarreau | 4baae24 | 2012-12-27 12:00:31 +0100 | [diff] [blame] | 8585 | { |
| 8586 | struct redirect_rule *rule; |
| 8587 | int cur_arg; |
| 8588 | int type = REDIRECT_TYPE_NONE; |
| 8589 | int code = 302; |
| 8590 | const char *destination = NULL; |
| 8591 | const char *cookie = NULL; |
| 8592 | int cookie_set = 0; |
| 8593 | unsigned int flags = REDIRECT_FLAG_NONE; |
| 8594 | struct acl_cond *cond = NULL; |
| 8595 | |
| 8596 | cur_arg = 0; |
| 8597 | while (*(args[cur_arg])) { |
| 8598 | if (strcmp(args[cur_arg], "location") == 0) { |
| 8599 | if (!*args[cur_arg + 1]) |
| 8600 | goto missing_arg; |
| 8601 | |
| 8602 | type = REDIRECT_TYPE_LOCATION; |
| 8603 | cur_arg++; |
| 8604 | destination = args[cur_arg]; |
| 8605 | } |
| 8606 | else if (strcmp(args[cur_arg], "prefix") == 0) { |
| 8607 | if (!*args[cur_arg + 1]) |
| 8608 | goto missing_arg; |
| 8609 | |
| 8610 | type = REDIRECT_TYPE_PREFIX; |
| 8611 | cur_arg++; |
| 8612 | destination = args[cur_arg]; |
| 8613 | } |
| 8614 | else if (strcmp(args[cur_arg], "scheme") == 0) { |
| 8615 | if (!*args[cur_arg + 1]) |
| 8616 | goto missing_arg; |
| 8617 | |
| 8618 | type = REDIRECT_TYPE_SCHEME; |
| 8619 | cur_arg++; |
| 8620 | destination = args[cur_arg]; |
| 8621 | } |
| 8622 | else if (strcmp(args[cur_arg], "set-cookie") == 0) { |
| 8623 | if (!*args[cur_arg + 1]) |
| 8624 | goto missing_arg; |
| 8625 | |
| 8626 | cur_arg++; |
| 8627 | cookie = args[cur_arg]; |
| 8628 | cookie_set = 1; |
| 8629 | } |
| 8630 | else if (strcmp(args[cur_arg], "clear-cookie") == 0) { |
| 8631 | if (!*args[cur_arg + 1]) |
| 8632 | goto missing_arg; |
| 8633 | |
| 8634 | cur_arg++; |
| 8635 | cookie = args[cur_arg]; |
| 8636 | cookie_set = 0; |
| 8637 | } |
| 8638 | else if (strcmp(args[cur_arg], "code") == 0) { |
| 8639 | if (!*args[cur_arg + 1]) |
| 8640 | goto missing_arg; |
| 8641 | |
| 8642 | cur_arg++; |
| 8643 | code = atol(args[cur_arg]); |
Yves Lafon | 3e8d1ae | 2013-03-11 11:06:05 -0400 | [diff] [blame] | 8644 | if (code < 301 || code > 308 || (code > 303 && code < 307)) { |
Willy Tarreau | 4baae24 | 2012-12-27 12:00:31 +0100 | [diff] [blame] | 8645 | memprintf(errmsg, |
Yves Lafon | 3e8d1ae | 2013-03-11 11:06:05 -0400 | [diff] [blame] | 8646 | "'%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] | 8647 | args[cur_arg - 1], args[cur_arg]); |
| 8648 | return NULL; |
| 8649 | } |
| 8650 | } |
| 8651 | else if (!strcmp(args[cur_arg],"drop-query")) { |
| 8652 | flags |= REDIRECT_FLAG_DROP_QS; |
| 8653 | } |
| 8654 | else if (!strcmp(args[cur_arg],"append-slash")) { |
| 8655 | flags |= REDIRECT_FLAG_APPEND_SLASH; |
| 8656 | } |
| 8657 | else if (strcmp(args[cur_arg], "if") == 0 || |
| 8658 | strcmp(args[cur_arg], "unless") == 0) { |
| 8659 | cond = build_acl_cond(file, linenum, curproxy, (const char **)args + cur_arg, errmsg); |
| 8660 | if (!cond) { |
| 8661 | memprintf(errmsg, "error in condition: %s", *errmsg); |
| 8662 | return NULL; |
| 8663 | } |
| 8664 | break; |
| 8665 | } |
| 8666 | else { |
| 8667 | memprintf(errmsg, |
| 8668 | "expects 'code', 'prefix', 'location', 'scheme', 'set-cookie', 'clear-cookie', 'drop-query' or 'append-slash' (was '%s')", |
| 8669 | args[cur_arg]); |
| 8670 | return NULL; |
| 8671 | } |
| 8672 | cur_arg++; |
| 8673 | } |
| 8674 | |
| 8675 | if (type == REDIRECT_TYPE_NONE) { |
| 8676 | memprintf(errmsg, "redirection type expected ('prefix', 'location', or 'scheme')"); |
| 8677 | return NULL; |
| 8678 | } |
| 8679 | |
| 8680 | rule = (struct redirect_rule *)calloc(1, sizeof(*rule)); |
| 8681 | rule->cond = cond; |
Willy Tarreau | 60e0838 | 2013-12-03 00:48:45 +0100 | [diff] [blame] | 8682 | LIST_INIT(&rule->rdr_fmt); |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 8683 | |
| 8684 | if (!use_fmt) { |
| 8685 | /* old-style static redirect rule */ |
| 8686 | rule->rdr_str = strdup(destination); |
| 8687 | rule->rdr_len = strlen(destination); |
| 8688 | } |
| 8689 | else { |
| 8690 | /* log-format based redirect rule */ |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 8691 | |
| 8692 | /* Parse destination. Note that in the REDIRECT_TYPE_PREFIX case, |
| 8693 | * if prefix == "/", we don't want to add anything, otherwise it |
| 8694 | * makes it hard for the user to configure a self-redirection. |
| 8695 | */ |
| 8696 | proxy->conf.args.ctx = ARGC_RDR; |
| 8697 | if (!(type == REDIRECT_TYPE_PREFIX && destination[0] == '/' && destination[1] == '\0')) { |
| 8698 | parse_logformat_string(destination, curproxy, &rule->rdr_fmt, 0, |
| 8699 | (curproxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR); |
| 8700 | } |
| 8701 | } |
| 8702 | |
Willy Tarreau | 4baae24 | 2012-12-27 12:00:31 +0100 | [diff] [blame] | 8703 | if (cookie) { |
| 8704 | /* depending on cookie_set, either we want to set the cookie, or to clear it. |
| 8705 | * a clear consists in appending "; path=/; Max-Age=0;" at the end. |
| 8706 | */ |
| 8707 | rule->cookie_len = strlen(cookie); |
| 8708 | if (cookie_set) { |
| 8709 | rule->cookie_str = malloc(rule->cookie_len + 10); |
| 8710 | memcpy(rule->cookie_str, cookie, rule->cookie_len); |
| 8711 | memcpy(rule->cookie_str + rule->cookie_len, "; path=/;", 10); |
| 8712 | rule->cookie_len += 9; |
| 8713 | } else { |
| 8714 | rule->cookie_str = malloc(rule->cookie_len + 21); |
| 8715 | memcpy(rule->cookie_str, cookie, rule->cookie_len); |
| 8716 | memcpy(rule->cookie_str + rule->cookie_len, "; path=/; Max-Age=0;", 21); |
| 8717 | rule->cookie_len += 20; |
| 8718 | } |
| 8719 | } |
| 8720 | rule->type = type; |
| 8721 | rule->code = code; |
| 8722 | rule->flags = flags; |
| 8723 | LIST_INIT(&rule->list); |
| 8724 | return rule; |
| 8725 | |
| 8726 | missing_arg: |
| 8727 | memprintf(errmsg, "missing argument for '%s'", args[cur_arg]); |
| 8728 | return NULL; |
| 8729 | } |
| 8730 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 8731 | /************************************************************************/ |
| 8732 | /* The code below is dedicated to ACL parsing and matching */ |
| 8733 | /************************************************************************/ |
| 8734 | |
| 8735 | |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 8736 | /* This function ensures that the prerequisites for an L7 fetch are ready, |
| 8737 | * which means that a request or response is ready. If some data is missing, |
| 8738 | * 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] | 8739 | * to extract data from L7. If <req_vol> is non-null during a request prefetch, |
| 8740 | * another test is made to ensure the required information is not gone. |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 8741 | * |
| 8742 | * The function returns : |
Willy Tarreau | 506d050 | 2013-07-06 13:29:24 +0200 | [diff] [blame] | 8743 | * 0 with SMP_F_MAY_CHANGE in the sample flags if some data is missing to |
| 8744 | * decide whether or not an HTTP message is present ; |
| 8745 | * 0 if the requested data cannot be fetched or if it is certain that |
| 8746 | * we'll never have any HTTP message there ; |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 8747 | * 1 if an HTTP message is ready |
| 8748 | */ |
| 8749 | static int |
Willy Tarreau | 506d050 | 2013-07-06 13:29:24 +0200 | [diff] [blame] | 8750 | smp_prefetch_http(struct proxy *px, struct session *s, void *l7, unsigned int opt, |
Willy Tarreau | 24e32d8 | 2012-04-23 23:55:44 +0200 | [diff] [blame] | 8751 | const struct arg *args, struct sample *smp, int req_vol) |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 8752 | { |
| 8753 | struct http_txn *txn = l7; |
| 8754 | struct http_msg *msg = &txn->req; |
| 8755 | |
| 8756 | /* Note: hdr_idx.v cannot be NULL in this ACL because the ACL is tagged |
| 8757 | * as a layer7 ACL, which involves automatic allocation of hdr_idx. |
| 8758 | */ |
| 8759 | |
| 8760 | if (unlikely(!s || !txn)) |
| 8761 | return 0; |
| 8762 | |
| 8763 | /* Check for a dependency on a request */ |
Willy Tarreau | f853c46 | 2012-04-23 18:53:56 +0200 | [diff] [blame] | 8764 | smp->type = SMP_T_BOOL; |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 8765 | |
Willy Tarreau | 32a6f2e | 2012-04-25 10:13:36 +0200 | [diff] [blame] | 8766 | if ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) { |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 8767 | if (unlikely(!s->req)) |
| 8768 | return 0; |
| 8769 | |
Willy Tarreau | aae75e3 | 2013-03-29 12:31:49 +0100 | [diff] [blame] | 8770 | /* If the buffer does not leave enough free space at the end, |
| 8771 | * we must first realign it. |
| 8772 | */ |
| 8773 | if (s->req->buf->p > s->req->buf->data && |
| 8774 | s->req->buf->i + s->req->buf->p > s->req->buf->data + s->req->buf->size - global.tune.maxrewrite) |
| 8775 | buffer_slow_realign(s->req->buf); |
| 8776 | |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 8777 | if (unlikely(txn->req.msg_state < HTTP_MSG_BODY)) { |
Willy Tarreau | 472b1ee | 2013-10-14 22:41:30 +0200 | [diff] [blame] | 8778 | if (msg->msg_state == HTTP_MSG_ERROR) |
Willy Tarreau | 506d050 | 2013-07-06 13:29:24 +0200 | [diff] [blame] | 8779 | return 0; |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 8780 | |
| 8781 | /* Try to decode HTTP request */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 8782 | if (likely(msg->next < s->req->buf->i)) |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 8783 | http_msg_analyzer(msg, &txn->hdr_idx); |
| 8784 | |
| 8785 | /* Still no valid request ? */ |
| 8786 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) { |
Willy Tarreau | 3bf1b2b | 2012-08-27 20:46:07 +0200 | [diff] [blame] | 8787 | if ((msg->msg_state == HTTP_MSG_ERROR) || |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 8788 | buffer_full(s->req->buf, global.tune.maxrewrite)) { |
Willy Tarreau | 506d050 | 2013-07-06 13:29:24 +0200 | [diff] [blame] | 8789 | return 0; |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 8790 | } |
| 8791 | /* wait for final state */ |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 8792 | smp->flags |= SMP_F_MAY_CHANGE; |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 8793 | return 0; |
| 8794 | } |
| 8795 | |
| 8796 | /* OK we just got a valid HTTP request. We have some minor |
| 8797 | * preparation to perform so that further checks can rely |
| 8798 | * on HTTP tests. |
| 8799 | */ |
Willy Tarreau | aae75e3 | 2013-03-29 12:31:49 +0100 | [diff] [blame] | 8800 | |
| 8801 | /* If the request was parsed but was too large, we must absolutely |
| 8802 | * return an error so that it is not processed. At the moment this |
| 8803 | * cannot happen, but if the parsers are to change in the future, |
| 8804 | * we want this check to be maintained. |
| 8805 | */ |
| 8806 | if (unlikely(s->req->buf->i + s->req->buf->p > |
| 8807 | s->req->buf->data + s->req->buf->size - global.tune.maxrewrite)) { |
| 8808 | msg->msg_state = HTTP_MSG_ERROR; |
Willy Tarreau | 506d050 | 2013-07-06 13:29:24 +0200 | [diff] [blame] | 8809 | smp->data.uint = 1; |
Willy Tarreau | aae75e3 | 2013-03-29 12:31:49 +0100 | [diff] [blame] | 8810 | return 1; |
| 8811 | } |
| 8812 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 8813 | 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] | 8814 | if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD) |
| 8815 | s->flags |= SN_REDIRECTABLE; |
| 8816 | |
Willy Tarreau | 506d050 | 2013-07-06 13:29:24 +0200 | [diff] [blame] | 8817 | if (unlikely(msg->sl.rq.v_l == 0) && !http_upgrade_v09_to_v10(txn)) |
| 8818 | return 0; |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 8819 | } |
| 8820 | |
Willy Tarreau | 506d050 | 2013-07-06 13:29:24 +0200 | [diff] [blame] | 8821 | if (req_vol && txn->rsp.msg_state != HTTP_MSG_RPBEFORE) { |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 8822 | return 0; /* data might have moved and indexes changed */ |
Willy Tarreau | 506d050 | 2013-07-06 13:29:24 +0200 | [diff] [blame] | 8823 | } |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 8824 | |
| 8825 | /* otherwise everything's ready for the request */ |
| 8826 | } |
Willy Tarreau | 24e32d8 | 2012-04-23 23:55:44 +0200 | [diff] [blame] | 8827 | else { |
| 8828 | /* Check for a dependency on a response */ |
Willy Tarreau | 506d050 | 2013-07-06 13:29:24 +0200 | [diff] [blame] | 8829 | if (txn->rsp.msg_state < HTTP_MSG_BODY) { |
| 8830 | smp->flags |= SMP_F_MAY_CHANGE; |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 8831 | return 0; |
Willy Tarreau | 506d050 | 2013-07-06 13:29:24 +0200 | [diff] [blame] | 8832 | } |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 8833 | } |
| 8834 | |
| 8835 | /* everything's OK */ |
Willy Tarreau | 506d050 | 2013-07-06 13:29:24 +0200 | [diff] [blame] | 8836 | smp->data.uint = 1; |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 8837 | return 1; |
| 8838 | } |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 8839 | |
Willy Tarreau | c0239e0 | 2012-04-16 14:42:55 +0200 | [diff] [blame] | 8840 | #define CHECK_HTTP_MESSAGE_FIRST() \ |
Willy Tarreau | 506d050 | 2013-07-06 13:29:24 +0200 | [diff] [blame] | 8841 | do { int r = smp_prefetch_http(px, l4, l7, opt, args, smp, 1); if (r <= 0) return r; } while (0) |
Willy Tarreau | c0239e0 | 2012-04-16 14:42:55 +0200 | [diff] [blame] | 8842 | |
Willy Tarreau | 24e32d8 | 2012-04-23 23:55:44 +0200 | [diff] [blame] | 8843 | #define CHECK_HTTP_MESSAGE_FIRST_PERM() \ |
Willy Tarreau | 506d050 | 2013-07-06 13:29:24 +0200 | [diff] [blame] | 8844 | do { int r = smp_prefetch_http(px, l4, l7, opt, args, smp, 0); if (r <= 0) return r; } while (0) |
Willy Tarreau | 24e32d8 | 2012-04-23 23:55:44 +0200 | [diff] [blame] | 8845 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 8846 | |
| 8847 | /* 1. Check on METHOD |
| 8848 | * We use the pre-parsed method if it is known, and store its number as an |
| 8849 | * integer. If it is unknown, we use the pointer and the length. |
| 8850 | */ |
Thierry FOURNIER | 0b2fe4a | 2013-12-06 20:33:50 +0100 | [diff] [blame] | 8851 | static int pat_parse_meth(const char **text, struct pattern *pattern, enum pat_usage usage, int *opaque, char **err) |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 8852 | { |
| 8853 | int len, meth; |
Thierry FOURNIER | 0b2fe4a | 2013-12-06 20:33:50 +0100 | [diff] [blame] | 8854 | struct chunk *trash; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 8855 | |
Willy Tarreau | ae8b796 | 2007-06-09 23:10:04 +0200 | [diff] [blame] | 8856 | len = strlen(*text); |
| 8857 | meth = find_http_meth(*text, len); |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 8858 | |
| 8859 | pattern->val.i = meth; |
| 8860 | if (meth == HTTP_METH_OTHER) { |
Thierry FOURNIER | 0b2fe4a | 2013-12-06 20:33:50 +0100 | [diff] [blame] | 8861 | if (usage == PAT_U_COMPILE) { |
| 8862 | pattern->ptr.str = strdup(*text); |
| 8863 | if (!pattern->ptr.str) { |
| 8864 | memprintf(err, "out of memory while loading pattern"); |
| 8865 | return 0; |
| 8866 | } |
Willy Tarreau | 7dcb648 | 2012-04-27 17:52:25 +0200 | [diff] [blame] | 8867 | } |
Thierry FOURNIER | 0b2fe4a | 2013-12-06 20:33:50 +0100 | [diff] [blame] | 8868 | else { |
| 8869 | trash = get_trash_chunk(); |
| 8870 | if (trash->size < len) { |
| 8871 | memprintf(err, "no space avalaible in the buffer. expect %d, provides %d", |
| 8872 | len, trash->size); |
| 8873 | return 0; |
| 8874 | } |
| 8875 | pattern->ptr.str = trash->str; |
| 8876 | } |
| 8877 | pattern->expect_type = SMP_T_CSTR; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 8878 | pattern->len = len; |
| 8879 | } |
Thierry FOURNIER | cc0e0b3 | 2013-12-06 16:56:40 +0100 | [diff] [blame] | 8880 | else |
| 8881 | pattern->expect_type = SMP_T_UINT; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 8882 | return 1; |
| 8883 | } |
| 8884 | |
Willy Tarreau | 8e5e955 | 2011-12-16 15:38:49 +0100 | [diff] [blame] | 8885 | /* This function fetches the method of current HTTP request and stores |
| 8886 | * it in the global pattern struct as a chunk. There are two possibilities : |
| 8887 | * - if the method is known (not HTTP_METH_OTHER), its identifier is stored |
| 8888 | * in <len> and <ptr> is NULL ; |
| 8889 | * - if the method is unknown (HTTP_METH_OTHER), <ptr> points to the text and |
| 8890 | * <len> to its length. |
Thierry FOURNIER | a65b343 | 2013-11-28 18:22:00 +0100 | [diff] [blame] | 8891 | * This is intended to be used with pat_match_meth() only. |
Willy Tarreau | 8e5e955 | 2011-12-16 15:38:49 +0100 | [diff] [blame] | 8892 | */ |
Willy Tarreau | d41f8d8 | 2007-06-10 10:06:18 +0200 | [diff] [blame] | 8893 | static int |
Willy Tarreau | 409bcde | 2013-01-08 00:31:00 +0100 | [diff] [blame] | 8894 | smp_fetch_meth(struct proxy *px, struct session *l4, void *l7, unsigned int opt, |
Willy Tarreau | ef38c39 | 2013-07-22 16:29:32 +0200 | [diff] [blame] | 8895 | const struct arg *args, struct sample *smp, const char *kw) |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 8896 | { |
| 8897 | int meth; |
| 8898 | struct http_txn *txn = l7; |
| 8899 | |
Willy Tarreau | 24e32d8 | 2012-04-23 23:55:44 +0200 | [diff] [blame] | 8900 | CHECK_HTTP_MESSAGE_FIRST_PERM(); |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 8901 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 8902 | meth = txn->meth; |
Willy Tarreau | f853c46 | 2012-04-23 18:53:56 +0200 | [diff] [blame] | 8903 | smp->type = SMP_T_UINT; |
| 8904 | smp->data.uint = meth; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 8905 | if (meth == HTTP_METH_OTHER) { |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 8906 | if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE) |
| 8907 | /* ensure the indexes are not affected */ |
| 8908 | return 0; |
Willy Tarreau | f853c46 | 2012-04-23 18:53:56 +0200 | [diff] [blame] | 8909 | smp->type = SMP_T_CSTR; |
| 8910 | smp->data.str.len = txn->req.sl.rq.m_l; |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 8911 | smp->data.str.str = txn->req.chn->buf->p; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 8912 | } |
Willy Tarreau | 21e5b0e | 2012-04-23 19:25:44 +0200 | [diff] [blame] | 8913 | smp->flags = SMP_F_VOL_1ST; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 8914 | return 1; |
| 8915 | } |
| 8916 | |
Willy Tarreau | 8e5e955 | 2011-12-16 15:38:49 +0100 | [diff] [blame] | 8917 | /* See above how the method is stored in the global pattern */ |
Willy Tarreau | 0cba607 | 2013-11-28 22:21:02 +0100 | [diff] [blame] | 8918 | static enum pat_match_res pat_match_meth(struct sample *smp, struct pattern *pattern) |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 8919 | { |
Willy Tarreau | c8d7c96 | 2007-06-17 08:20:33 +0200 | [diff] [blame] | 8920 | int icase; |
| 8921 | |
Willy Tarreau | 8e5e955 | 2011-12-16 15:38:49 +0100 | [diff] [blame] | 8922 | |
Willy Tarreau | f853c46 | 2012-04-23 18:53:56 +0200 | [diff] [blame] | 8923 | if (smp->type == SMP_T_UINT) { |
Willy Tarreau | 8e5e955 | 2011-12-16 15:38:49 +0100 | [diff] [blame] | 8924 | /* well-known method */ |
Willy Tarreau | f853c46 | 2012-04-23 18:53:56 +0200 | [diff] [blame] | 8925 | if (smp->data.uint == pattern->val.i) |
Thierry FOURNIER | a65b343 | 2013-11-28 18:22:00 +0100 | [diff] [blame] | 8926 | return PAT_MATCH; |
| 8927 | return PAT_NOMATCH; |
Willy Tarreau | 8e5e955 | 2011-12-16 15:38:49 +0100 | [diff] [blame] | 8928 | } |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 8929 | |
Willy Tarreau | 8e5e955 | 2011-12-16 15:38:49 +0100 | [diff] [blame] | 8930 | /* Uncommon method, only HTTP_METH_OTHER is accepted now */ |
| 8931 | if (pattern->val.i != HTTP_METH_OTHER) |
Thierry FOURNIER | a65b343 | 2013-11-28 18:22:00 +0100 | [diff] [blame] | 8932 | return PAT_NOMATCH; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 8933 | |
| 8934 | /* Other method, we must compare the strings */ |
Willy Tarreau | f853c46 | 2012-04-23 18:53:56 +0200 | [diff] [blame] | 8935 | if (pattern->len != smp->data.str.len) |
Thierry FOURNIER | a65b343 | 2013-11-28 18:22:00 +0100 | [diff] [blame] | 8936 | return PAT_NOMATCH; |
Willy Tarreau | c8d7c96 | 2007-06-17 08:20:33 +0200 | [diff] [blame] | 8937 | |
Thierry FOURNIER | a65b343 | 2013-11-28 18:22:00 +0100 | [diff] [blame] | 8938 | icase = pattern->flags & PAT_F_IGNORE_CASE; |
Willy Tarreau | f853c46 | 2012-04-23 18:53:56 +0200 | [diff] [blame] | 8939 | if ((icase && strncasecmp(pattern->ptr.str, smp->data.str.str, smp->data.str.len) != 0) || |
| 8940 | (!icase && strncmp(pattern->ptr.str, smp->data.str.str, smp->data.str.len) != 0)) |
Thierry FOURNIER | a65b343 | 2013-11-28 18:22:00 +0100 | [diff] [blame] | 8941 | return PAT_NOMATCH; |
| 8942 | return PAT_MATCH; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 8943 | } |
| 8944 | |
Willy Tarreau | d41f8d8 | 2007-06-10 10:06:18 +0200 | [diff] [blame] | 8945 | static int |
Willy Tarreau | 409bcde | 2013-01-08 00:31:00 +0100 | [diff] [blame] | 8946 | smp_fetch_rqver(struct proxy *px, struct session *l4, void *l7, unsigned int opt, |
Willy Tarreau | ef38c39 | 2013-07-22 16:29:32 +0200 | [diff] [blame] | 8947 | const struct arg *args, struct sample *smp, const char *kw) |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 8948 | { |
| 8949 | struct http_txn *txn = l7; |
| 8950 | char *ptr; |
| 8951 | int len; |
| 8952 | |
Willy Tarreau | c0239e0 | 2012-04-16 14:42:55 +0200 | [diff] [blame] | 8953 | CHECK_HTTP_MESSAGE_FIRST(); |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 8954 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 8955 | len = txn->req.sl.rq.v_l; |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 8956 | ptr = txn->req.chn->buf->p + txn->req.sl.rq.v; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 8957 | |
| 8958 | while ((len-- > 0) && (*ptr++ != '/')); |
| 8959 | if (len <= 0) |
| 8960 | return 0; |
| 8961 | |
Willy Tarreau | f853c46 | 2012-04-23 18:53:56 +0200 | [diff] [blame] | 8962 | smp->type = SMP_T_CSTR; |
| 8963 | smp->data.str.str = ptr; |
| 8964 | smp->data.str.len = len; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 8965 | |
Willy Tarreau | 21e5b0e | 2012-04-23 19:25:44 +0200 | [diff] [blame] | 8966 | smp->flags = SMP_F_VOL_1ST; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 8967 | return 1; |
| 8968 | } |
| 8969 | |
Willy Tarreau | d41f8d8 | 2007-06-10 10:06:18 +0200 | [diff] [blame] | 8970 | static int |
Willy Tarreau | 409bcde | 2013-01-08 00:31:00 +0100 | [diff] [blame] | 8971 | smp_fetch_stver(struct proxy *px, struct session *l4, void *l7, unsigned int opt, |
Willy Tarreau | ef38c39 | 2013-07-22 16:29:32 +0200 | [diff] [blame] | 8972 | const struct arg *args, struct sample *smp, const char *kw) |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 8973 | { |
| 8974 | struct http_txn *txn = l7; |
| 8975 | char *ptr; |
| 8976 | int len; |
| 8977 | |
Willy Tarreau | c0239e0 | 2012-04-16 14:42:55 +0200 | [diff] [blame] | 8978 | CHECK_HTTP_MESSAGE_FIRST(); |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 8979 | |
Willy Tarreau | f26b252 | 2012-12-14 08:33:14 +0100 | [diff] [blame] | 8980 | if (txn->rsp.msg_state < HTTP_MSG_BODY) |
| 8981 | return 0; |
| 8982 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 8983 | len = txn->rsp.sl.st.v_l; |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 8984 | ptr = txn->rsp.chn->buf->p; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 8985 | |
| 8986 | while ((len-- > 0) && (*ptr++ != '/')); |
| 8987 | if (len <= 0) |
| 8988 | return 0; |
| 8989 | |
Willy Tarreau | f853c46 | 2012-04-23 18:53:56 +0200 | [diff] [blame] | 8990 | smp->type = SMP_T_CSTR; |
| 8991 | smp->data.str.str = ptr; |
| 8992 | smp->data.str.len = len; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 8993 | |
Willy Tarreau | 21e5b0e | 2012-04-23 19:25:44 +0200 | [diff] [blame] | 8994 | smp->flags = SMP_F_VOL_1ST; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 8995 | return 1; |
| 8996 | } |
| 8997 | |
| 8998 | /* 3. Check on Status Code. We manipulate integers here. */ |
Willy Tarreau | d41f8d8 | 2007-06-10 10:06:18 +0200 | [diff] [blame] | 8999 | static int |
Willy Tarreau | 409bcde | 2013-01-08 00:31:00 +0100 | [diff] [blame] | 9000 | smp_fetch_stcode(struct proxy *px, struct session *l4, void *l7, unsigned int opt, |
Willy Tarreau | ef38c39 | 2013-07-22 16:29:32 +0200 | [diff] [blame] | 9001 | const struct arg *args, struct sample *smp, const char *kw) |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 9002 | { |
| 9003 | struct http_txn *txn = l7; |
| 9004 | char *ptr; |
| 9005 | int len; |
| 9006 | |
Willy Tarreau | c0239e0 | 2012-04-16 14:42:55 +0200 | [diff] [blame] | 9007 | CHECK_HTTP_MESSAGE_FIRST(); |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 9008 | |
Willy Tarreau | f26b252 | 2012-12-14 08:33:14 +0100 | [diff] [blame] | 9009 | if (txn->rsp.msg_state < HTTP_MSG_BODY) |
| 9010 | return 0; |
| 9011 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 9012 | len = txn->rsp.sl.st.c_l; |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 9013 | ptr = txn->rsp.chn->buf->p + txn->rsp.sl.st.c; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 9014 | |
Willy Tarreau | f853c46 | 2012-04-23 18:53:56 +0200 | [diff] [blame] | 9015 | smp->type = SMP_T_UINT; |
| 9016 | smp->data.uint = __strl2ui(ptr, len); |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 9017 | smp->flags = SMP_F_VOL_1ST; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 9018 | return 1; |
| 9019 | } |
| 9020 | |
| 9021 | /* 4. Check on URL/URI. A pointer to the URI is stored. */ |
Willy Tarreau | d41f8d8 | 2007-06-10 10:06:18 +0200 | [diff] [blame] | 9022 | static int |
Willy Tarreau | 6812bcf | 2012-04-29 09:28:50 +0200 | [diff] [blame] | 9023 | smp_fetch_url(struct proxy *px, struct session *l4, void *l7, unsigned int opt, |
Willy Tarreau | ef38c39 | 2013-07-22 16:29:32 +0200 | [diff] [blame] | 9024 | const struct arg *args, struct sample *smp, const char *kw) |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 9025 | { |
| 9026 | struct http_txn *txn = l7; |
| 9027 | |
Willy Tarreau | c0239e0 | 2012-04-16 14:42:55 +0200 | [diff] [blame] | 9028 | CHECK_HTTP_MESSAGE_FIRST(); |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 9029 | |
Willy Tarreau | f853c46 | 2012-04-23 18:53:56 +0200 | [diff] [blame] | 9030 | smp->type = SMP_T_CSTR; |
| 9031 | smp->data.str.len = txn->req.sl.rq.u_l; |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 9032 | smp->data.str.str = txn->req.chn->buf->p + txn->req.sl.rq.u; |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 9033 | smp->flags = SMP_F_VOL_1ST; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 9034 | return 1; |
| 9035 | } |
| 9036 | |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 9037 | static int |
Willy Tarreau | 6812bcf | 2012-04-29 09:28:50 +0200 | [diff] [blame] | 9038 | smp_fetch_url_ip(struct proxy *px, struct session *l4, void *l7, unsigned int opt, |
Willy Tarreau | ef38c39 | 2013-07-22 16:29:32 +0200 | [diff] [blame] | 9039 | const struct arg *args, struct sample *smp, const char *kw) |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 9040 | { |
| 9041 | struct http_txn *txn = l7; |
Willy Tarreau | 4c804ec | 2013-09-30 14:37:14 +0200 | [diff] [blame] | 9042 | struct sockaddr_storage addr; |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 9043 | |
Willy Tarreau | c0239e0 | 2012-04-16 14:42:55 +0200 | [diff] [blame] | 9044 | CHECK_HTTP_MESSAGE_FIRST(); |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 9045 | |
Willy Tarreau | 4c804ec | 2013-09-30 14:37:14 +0200 | [diff] [blame] | 9046 | url2sa(txn->req.chn->buf->p + txn->req.sl.rq.u, txn->req.sl.rq.u_l, &addr); |
| 9047 | if (((struct sockaddr_in *)&addr)->sin_family != AF_INET) |
Willy Tarreau | f4362b3 | 2011-12-16 17:49:52 +0100 | [diff] [blame] | 9048 | return 0; |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 9049 | |
Willy Tarreau | 4c804ec | 2013-09-30 14:37:14 +0200 | [diff] [blame] | 9050 | smp->type = SMP_T_IPV4; |
| 9051 | smp->data.ipv4 = ((struct sockaddr_in *)&addr)->sin_addr; |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 9052 | smp->flags = 0; |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 9053 | return 1; |
| 9054 | } |
| 9055 | |
| 9056 | static int |
Willy Tarreau | 6812bcf | 2012-04-29 09:28:50 +0200 | [diff] [blame] | 9057 | smp_fetch_url_port(struct proxy *px, struct session *l4, void *l7, unsigned int opt, |
Willy Tarreau | ef38c39 | 2013-07-22 16:29:32 +0200 | [diff] [blame] | 9058 | const struct arg *args, struct sample *smp, const char *kw) |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 9059 | { |
| 9060 | struct http_txn *txn = l7; |
Willy Tarreau | 4c804ec | 2013-09-30 14:37:14 +0200 | [diff] [blame] | 9061 | struct sockaddr_storage addr; |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 9062 | |
Willy Tarreau | c0239e0 | 2012-04-16 14:42:55 +0200 | [diff] [blame] | 9063 | CHECK_HTTP_MESSAGE_FIRST(); |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 9064 | |
Willy Tarreau | 4c804ec | 2013-09-30 14:37:14 +0200 | [diff] [blame] | 9065 | url2sa(txn->req.chn->buf->p + txn->req.sl.rq.u, txn->req.sl.rq.u_l, &addr); |
| 9066 | if (((struct sockaddr_in *)&addr)->sin_family != AF_INET) |
| 9067 | return 0; |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 9068 | |
Willy Tarreau | 4c804ec | 2013-09-30 14:37:14 +0200 | [diff] [blame] | 9069 | smp->type = SMP_T_UINT; |
| 9070 | smp->data.uint = ntohs(((struct sockaddr_in *)&addr)->sin_port); |
Willy Tarreau | 21e5b0e | 2012-04-23 19:25:44 +0200 | [diff] [blame] | 9071 | smp->flags = 0; |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 9072 | return 1; |
| 9073 | } |
| 9074 | |
Willy Tarreau | 185b5c4 | 2012-04-26 15:11:51 +0200 | [diff] [blame] | 9075 | /* Fetch an HTTP header. A pointer to the beginning of the value is returned. |
| 9076 | * Accepts an optional argument of type string containing the header field name, |
| 9077 | * and an optional argument of type signed or unsigned integer to request an |
| 9078 | * 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] | 9079 | * headers are considered from the first one. It does not stop on commas and |
| 9080 | * returns full lines instead (useful for User-Agent or Date for example). |
| 9081 | */ |
| 9082 | static int |
| 9083 | smp_fetch_fhdr(struct proxy *px, struct session *l4, void *l7, unsigned int opt, |
Willy Tarreau | ef38c39 | 2013-07-22 16:29:32 +0200 | [diff] [blame] | 9084 | const struct arg *args, struct sample *smp, const char *kw) |
Willy Tarreau | 04ff9f1 | 2013-06-10 18:39:42 +0200 | [diff] [blame] | 9085 | { |
| 9086 | struct http_txn *txn = l7; |
| 9087 | struct hdr_idx *idx = &txn->hdr_idx; |
| 9088 | struct hdr_ctx *ctx = smp->ctx.a[0]; |
| 9089 | const struct http_msg *msg = ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) ? &txn->req : &txn->rsp; |
| 9090 | int occ = 0; |
| 9091 | const char *name_str = NULL; |
| 9092 | int name_len = 0; |
| 9093 | |
| 9094 | if (!ctx) { |
| 9095 | /* first call */ |
| 9096 | ctx = &static_hdr_ctx; |
| 9097 | ctx->idx = 0; |
| 9098 | smp->ctx.a[0] = ctx; |
| 9099 | } |
| 9100 | |
| 9101 | if (args) { |
| 9102 | if (args[0].type != ARGT_STR) |
| 9103 | return 0; |
| 9104 | name_str = args[0].data.str.str; |
| 9105 | name_len = args[0].data.str.len; |
| 9106 | |
| 9107 | if (args[1].type == ARGT_UINT || args[1].type == ARGT_SINT) |
| 9108 | occ = args[1].data.uint; |
| 9109 | } |
| 9110 | |
| 9111 | CHECK_HTTP_MESSAGE_FIRST(); |
| 9112 | |
| 9113 | if (ctx && !(smp->flags & SMP_F_NOT_LAST)) |
| 9114 | /* search for header from the beginning */ |
| 9115 | ctx->idx = 0; |
| 9116 | |
| 9117 | if (!occ && !(opt & SMP_OPT_ITERATE)) |
| 9118 | /* no explicit occurrence and single fetch => last header by default */ |
| 9119 | occ = -1; |
| 9120 | |
| 9121 | if (!occ) |
| 9122 | /* prepare to report multiple occurrences for ACL fetches */ |
| 9123 | smp->flags |= SMP_F_NOT_LAST; |
| 9124 | |
| 9125 | smp->type = SMP_T_CSTR; |
| 9126 | smp->flags |= SMP_F_VOL_HDR; |
| 9127 | if (http_get_fhdr(msg, name_str, name_len, idx, occ, ctx, &smp->data.str.str, &smp->data.str.len)) |
| 9128 | return 1; |
| 9129 | |
| 9130 | smp->flags &= ~SMP_F_NOT_LAST; |
| 9131 | return 0; |
| 9132 | } |
| 9133 | |
| 9134 | /* 6. Check on HTTP header count. The number of occurrences is returned. |
| 9135 | * Accepts exactly 1 argument of type string. It does not stop on commas and |
| 9136 | * returns full lines instead (useful for User-Agent or Date for example). |
| 9137 | */ |
| 9138 | static int |
| 9139 | smp_fetch_fhdr_cnt(struct proxy *px, struct session *l4, void *l7, unsigned int opt, |
Willy Tarreau | ef38c39 | 2013-07-22 16:29:32 +0200 | [diff] [blame] | 9140 | const struct arg *args, struct sample *smp, const char *kw) |
Willy Tarreau | 04ff9f1 | 2013-06-10 18:39:42 +0200 | [diff] [blame] | 9141 | { |
| 9142 | struct http_txn *txn = l7; |
| 9143 | struct hdr_idx *idx = &txn->hdr_idx; |
| 9144 | struct hdr_ctx ctx; |
| 9145 | const struct http_msg *msg = ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) ? &txn->req : &txn->rsp; |
| 9146 | int cnt; |
| 9147 | |
| 9148 | if (!args || args->type != ARGT_STR) |
| 9149 | return 0; |
| 9150 | |
| 9151 | CHECK_HTTP_MESSAGE_FIRST(); |
| 9152 | |
| 9153 | ctx.idx = 0; |
| 9154 | cnt = 0; |
| 9155 | while (http_find_full_header2(args->data.str.str, args->data.str.len, msg->chn->buf->p, idx, &ctx)) |
| 9156 | cnt++; |
| 9157 | |
| 9158 | smp->type = SMP_T_UINT; |
| 9159 | smp->data.uint = cnt; |
| 9160 | smp->flags = SMP_F_VOL_HDR; |
| 9161 | return 1; |
| 9162 | } |
| 9163 | |
| 9164 | /* Fetch an HTTP header. A pointer to the beginning of the value is returned. |
| 9165 | * Accepts an optional argument of type string containing the header field name, |
| 9166 | * and an optional argument of type signed or unsigned integer to request an |
| 9167 | * 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] | 9168 | * headers are considered from the first one. |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 9169 | */ |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 9170 | static int |
Willy Tarreau | 185b5c4 | 2012-04-26 15:11:51 +0200 | [diff] [blame] | 9171 | smp_fetch_hdr(struct proxy *px, struct session *l4, void *l7, unsigned int opt, |
Willy Tarreau | ef38c39 | 2013-07-22 16:29:32 +0200 | [diff] [blame] | 9172 | const struct arg *args, struct sample *smp, const char *kw) |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 9173 | { |
| 9174 | struct http_txn *txn = l7; |
| 9175 | struct hdr_idx *idx = &txn->hdr_idx; |
Willy Tarreau | a890d07 | 2013-04-02 12:01:06 +0200 | [diff] [blame] | 9176 | struct hdr_ctx *ctx = smp->ctx.a[0]; |
Willy Tarreau | 32a6f2e | 2012-04-25 10:13:36 +0200 | [diff] [blame] | 9177 | const struct http_msg *msg = ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) ? &txn->req : &txn->rsp; |
Willy Tarreau | 185b5c4 | 2012-04-26 15:11:51 +0200 | [diff] [blame] | 9178 | int occ = 0; |
| 9179 | const char *name_str = NULL; |
| 9180 | int name_len = 0; |
Willy Tarreau | e333ec9 | 2012-04-16 16:26:40 +0200 | [diff] [blame] | 9181 | |
Willy Tarreau | a890d07 | 2013-04-02 12:01:06 +0200 | [diff] [blame] | 9182 | if (!ctx) { |
| 9183 | /* first call */ |
| 9184 | ctx = &static_hdr_ctx; |
| 9185 | ctx->idx = 0; |
Willy Tarreau | ffb6f08 | 2013-04-02 23:16:53 +0200 | [diff] [blame] | 9186 | smp->ctx.a[0] = ctx; |
Willy Tarreau | a890d07 | 2013-04-02 12:01:06 +0200 | [diff] [blame] | 9187 | } |
| 9188 | |
Willy Tarreau | 185b5c4 | 2012-04-26 15:11:51 +0200 | [diff] [blame] | 9189 | if (args) { |
| 9190 | if (args[0].type != ARGT_STR) |
| 9191 | return 0; |
| 9192 | name_str = args[0].data.str.str; |
| 9193 | name_len = args[0].data.str.len; |
| 9194 | |
| 9195 | if (args[1].type == ARGT_UINT || args[1].type == ARGT_SINT) |
| 9196 | occ = args[1].data.uint; |
| 9197 | } |
Willy Tarreau | 34db108 | 2012-04-19 17:16:54 +0200 | [diff] [blame] | 9198 | |
Willy Tarreau | e333ec9 | 2012-04-16 16:26:40 +0200 | [diff] [blame] | 9199 | CHECK_HTTP_MESSAGE_FIRST(); |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 9200 | |
Willy Tarreau | 185b5c4 | 2012-04-26 15:11:51 +0200 | [diff] [blame] | 9201 | if (ctx && !(smp->flags & SMP_F_NOT_LAST)) |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 9202 | /* search for header from the beginning */ |
| 9203 | ctx->idx = 0; |
| 9204 | |
Willy Tarreau | 185b5c4 | 2012-04-26 15:11:51 +0200 | [diff] [blame] | 9205 | if (!occ && !(opt & SMP_OPT_ITERATE)) |
| 9206 | /* no explicit occurrence and single fetch => last header by default */ |
| 9207 | occ = -1; |
| 9208 | |
| 9209 | if (!occ) |
| 9210 | /* prepare to report multiple occurrences for ACL fetches */ |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 9211 | smp->flags |= SMP_F_NOT_LAST; |
Willy Tarreau | 664092c | 2011-12-16 19:11:42 +0100 | [diff] [blame] | 9212 | |
Willy Tarreau | 185b5c4 | 2012-04-26 15:11:51 +0200 | [diff] [blame] | 9213 | smp->type = SMP_T_CSTR; |
| 9214 | smp->flags |= SMP_F_VOL_HDR; |
| 9215 | 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] | 9216 | return 1; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 9217 | |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 9218 | smp->flags &= ~SMP_F_NOT_LAST; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 9219 | return 0; |
| 9220 | } |
| 9221 | |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 9222 | /* 6. Check on HTTP header count. The number of occurrences is returned. |
Willy Tarreau | 34db108 | 2012-04-19 17:16:54 +0200 | [diff] [blame] | 9223 | * Accepts exactly 1 argument of type string. |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 9224 | */ |
| 9225 | static int |
Willy Tarreau | 185b5c4 | 2012-04-26 15:11:51 +0200 | [diff] [blame] | 9226 | smp_fetch_hdr_cnt(struct proxy *px, struct session *l4, void *l7, unsigned int opt, |
Willy Tarreau | ef38c39 | 2013-07-22 16:29:32 +0200 | [diff] [blame] | 9227 | const struct arg *args, struct sample *smp, const char *kw) |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 9228 | { |
| 9229 | struct http_txn *txn = l7; |
| 9230 | struct hdr_idx *idx = &txn->hdr_idx; |
| 9231 | struct hdr_ctx ctx; |
Willy Tarreau | 32a6f2e | 2012-04-25 10:13:36 +0200 | [diff] [blame] | 9232 | const struct http_msg *msg = ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) ? &txn->req : &txn->rsp; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 9233 | int cnt; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 9234 | |
Willy Tarreau | 24e32d8 | 2012-04-23 23:55:44 +0200 | [diff] [blame] | 9235 | if (!args || args->type != ARGT_STR) |
Willy Tarreau | 34db108 | 2012-04-19 17:16:54 +0200 | [diff] [blame] | 9236 | return 0; |
| 9237 | |
Willy Tarreau | e333ec9 | 2012-04-16 16:26:40 +0200 | [diff] [blame] | 9238 | CHECK_HTTP_MESSAGE_FIRST(); |
| 9239 | |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 9240 | ctx.idx = 0; |
| 9241 | cnt = 0; |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 9242 | while (http_find_header2(args->data.str.str, args->data.str.len, msg->chn->buf->p, idx, &ctx)) |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 9243 | cnt++; |
| 9244 | |
Willy Tarreau | f853c46 | 2012-04-23 18:53:56 +0200 | [diff] [blame] | 9245 | smp->type = SMP_T_UINT; |
| 9246 | smp->data.uint = cnt; |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 9247 | smp->flags = SMP_F_VOL_HDR; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 9248 | return 1; |
| 9249 | } |
| 9250 | |
Willy Tarreau | 185b5c4 | 2012-04-26 15:11:51 +0200 | [diff] [blame] | 9251 | /* Fetch an HTTP header's integer value. The integer value is returned. It |
| 9252 | * takes a mandatory argument of type string and an optional one of type int |
| 9253 | * to designate a specific occurrence. It returns an unsigned integer, which |
| 9254 | * may or may not be appropriate for everything. |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 9255 | */ |
| 9256 | static int |
Willy Tarreau | 185b5c4 | 2012-04-26 15:11:51 +0200 | [diff] [blame] | 9257 | smp_fetch_hdr_val(struct proxy *px, struct session *l4, void *l7, unsigned int opt, |
Willy Tarreau | ef38c39 | 2013-07-22 16:29:32 +0200 | [diff] [blame] | 9258 | const struct arg *args, struct sample *smp, const char *kw) |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 9259 | { |
Willy Tarreau | ef38c39 | 2013-07-22 16:29:32 +0200 | [diff] [blame] | 9260 | int ret = smp_fetch_hdr(px, l4, l7, opt, args, smp, kw); |
Willy Tarreau | e333ec9 | 2012-04-16 16:26:40 +0200 | [diff] [blame] | 9261 | |
Willy Tarreau | f853c46 | 2012-04-23 18:53:56 +0200 | [diff] [blame] | 9262 | if (ret > 0) { |
| 9263 | smp->type = SMP_T_UINT; |
| 9264 | smp->data.uint = strl2ic(smp->data.str.str, smp->data.str.len); |
| 9265 | } |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 9266 | |
Willy Tarreau | d53e242 | 2012-04-16 17:21:11 +0200 | [diff] [blame] | 9267 | return ret; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 9268 | } |
| 9269 | |
Cyril Bonté | 69fa992 | 2012-10-25 00:01:06 +0200 | [diff] [blame] | 9270 | /* Fetch an HTTP header's IP value. takes a mandatory argument of type string |
| 9271 | * and an optional one of type int to designate a specific occurrence. |
| 9272 | * It returns an IPv4 or IPv6 address. |
Willy Tarreau | 106f979 | 2009-09-19 07:54:16 +0200 | [diff] [blame] | 9273 | */ |
| 9274 | static int |
Willy Tarreau | 185b5c4 | 2012-04-26 15:11:51 +0200 | [diff] [blame] | 9275 | smp_fetch_hdr_ip(struct proxy *px, struct session *l4, void *l7, unsigned int opt, |
Willy Tarreau | ef38c39 | 2013-07-22 16:29:32 +0200 | [diff] [blame] | 9276 | const struct arg *args, struct sample *smp, const char *kw) |
Willy Tarreau | 106f979 | 2009-09-19 07:54:16 +0200 | [diff] [blame] | 9277 | { |
Willy Tarreau | d53e242 | 2012-04-16 17:21:11 +0200 | [diff] [blame] | 9278 | int ret; |
Willy Tarreau | e333ec9 | 2012-04-16 16:26:40 +0200 | [diff] [blame] | 9279 | |
Willy Tarreau | ef38c39 | 2013-07-22 16:29:32 +0200 | [diff] [blame] | 9280 | while ((ret = smp_fetch_hdr(px, l4, l7, opt, args, smp, kw)) > 0) { |
Cyril Bonté | 69fa992 | 2012-10-25 00:01:06 +0200 | [diff] [blame] | 9281 | if (url2ipv4((char *)smp->data.str.str, &smp->data.ipv4)) { |
| 9282 | smp->type = SMP_T_IPV4; |
Willy Tarreau | d53e242 | 2012-04-16 17:21:11 +0200 | [diff] [blame] | 9283 | break; |
Cyril Bonté | 69fa992 | 2012-10-25 00:01:06 +0200 | [diff] [blame] | 9284 | } else { |
Willy Tarreau | 47ca545 | 2012-12-23 20:22:19 +0100 | [diff] [blame] | 9285 | struct chunk *temp = get_trash_chunk(); |
Cyril Bonté | 69fa992 | 2012-10-25 00:01:06 +0200 | [diff] [blame] | 9286 | if (smp->data.str.len < temp->size - 1) { |
| 9287 | memcpy(temp->str, smp->data.str.str, smp->data.str.len); |
| 9288 | temp->str[smp->data.str.len] = '\0'; |
| 9289 | if (inet_pton(AF_INET6, temp->str, &smp->data.ipv6)) { |
| 9290 | smp->type = SMP_T_IPV6; |
| 9291 | break; |
| 9292 | } |
| 9293 | } |
| 9294 | } |
| 9295 | |
Willy Tarreau | d53e242 | 2012-04-16 17:21:11 +0200 | [diff] [blame] | 9296 | /* if the header doesn't match an IP address, fetch next one */ |
Willy Tarreau | 185b5c4 | 2012-04-26 15:11:51 +0200 | [diff] [blame] | 9297 | if (!(smp->flags & SMP_F_NOT_LAST)) |
| 9298 | return 0; |
Willy Tarreau | 106f979 | 2009-09-19 07:54:16 +0200 | [diff] [blame] | 9299 | } |
Willy Tarreau | d53e242 | 2012-04-16 17:21:11 +0200 | [diff] [blame] | 9300 | return ret; |
Willy Tarreau | 106f979 | 2009-09-19 07:54:16 +0200 | [diff] [blame] | 9301 | } |
| 9302 | |
Willy Tarreau | 737b0c1 | 2007-06-10 21:28:46 +0200 | [diff] [blame] | 9303 | /* 8. Check on URI PATH. A pointer to the PATH is stored. The path starts at |
| 9304 | * the first '/' after the possible hostname, and ends before the possible '?'. |
| 9305 | */ |
| 9306 | static int |
Willy Tarreau | 6812bcf | 2012-04-29 09:28:50 +0200 | [diff] [blame] | 9307 | smp_fetch_path(struct proxy *px, struct session *l4, void *l7, unsigned int opt, |
Willy Tarreau | ef38c39 | 2013-07-22 16:29:32 +0200 | [diff] [blame] | 9308 | const struct arg *args, struct sample *smp, const char *kw) |
Willy Tarreau | 737b0c1 | 2007-06-10 21:28:46 +0200 | [diff] [blame] | 9309 | { |
| 9310 | struct http_txn *txn = l7; |
| 9311 | char *ptr, *end; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 9312 | |
Willy Tarreau | c0239e0 | 2012-04-16 14:42:55 +0200 | [diff] [blame] | 9313 | CHECK_HTTP_MESSAGE_FIRST(); |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 9314 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 9315 | 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] | 9316 | ptr = http_get_path(txn); |
| 9317 | if (!ptr) |
Willy Tarreau | 737b0c1 | 2007-06-10 21:28:46 +0200 | [diff] [blame] | 9318 | return 0; |
| 9319 | |
| 9320 | /* OK, we got the '/' ! */ |
Willy Tarreau | f853c46 | 2012-04-23 18:53:56 +0200 | [diff] [blame] | 9321 | smp->type = SMP_T_CSTR; |
| 9322 | smp->data.str.str = ptr; |
Willy Tarreau | 737b0c1 | 2007-06-10 21:28:46 +0200 | [diff] [blame] | 9323 | |
| 9324 | while (ptr < end && *ptr != '?') |
| 9325 | ptr++; |
| 9326 | |
Willy Tarreau | f853c46 | 2012-04-23 18:53:56 +0200 | [diff] [blame] | 9327 | smp->data.str.len = ptr - smp->data.str.str; |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 9328 | smp->flags = SMP_F_VOL_1ST; |
Willy Tarreau | 737b0c1 | 2007-06-10 21:28:46 +0200 | [diff] [blame] | 9329 | return 1; |
| 9330 | } |
| 9331 | |
Willy Tarreau | a7ad50c | 2012-04-29 15:39:40 +0200 | [diff] [blame] | 9332 | /* This produces a concatenation of the first occurrence of the Host header |
| 9333 | * followed by the path component if it begins with a slash ('/'). This means |
| 9334 | * that '*' will not be added, resulting in exactly the first Host entry. |
| 9335 | * If no Host header is found, then the path is returned as-is. The returned |
| 9336 | * 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] | 9337 | * The returned sample is of type string. |
Willy Tarreau | a7ad50c | 2012-04-29 15:39:40 +0200 | [diff] [blame] | 9338 | */ |
| 9339 | static int |
| 9340 | smp_fetch_base(struct proxy *px, struct session *l4, void *l7, unsigned int opt, |
Willy Tarreau | ef38c39 | 2013-07-22 16:29:32 +0200 | [diff] [blame] | 9341 | const struct arg *args, struct sample *smp, const char *kw) |
Willy Tarreau | a7ad50c | 2012-04-29 15:39:40 +0200 | [diff] [blame] | 9342 | { |
| 9343 | struct http_txn *txn = l7; |
| 9344 | char *ptr, *end, *beg; |
| 9345 | struct hdr_ctx ctx; |
| 9346 | |
| 9347 | CHECK_HTTP_MESSAGE_FIRST(); |
| 9348 | |
| 9349 | ctx.idx = 0; |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 9350 | if (!http_find_header2("Host", 4, txn->req.chn->buf->p + txn->req.sol, &txn->hdr_idx, &ctx) || |
Willy Tarreau | a7ad50c | 2012-04-29 15:39:40 +0200 | [diff] [blame] | 9351 | !ctx.vlen) |
Willy Tarreau | ef38c39 | 2013-07-22 16:29:32 +0200 | [diff] [blame] | 9352 | return smp_fetch_path(px, l4, l7, opt, args, smp, kw); |
Willy Tarreau | a7ad50c | 2012-04-29 15:39:40 +0200 | [diff] [blame] | 9353 | |
| 9354 | /* OK we have the header value in ctx.line+ctx.val for ctx.vlen bytes */ |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 9355 | memcpy(trash.str, ctx.line + ctx.val, ctx.vlen); |
Willy Tarreau | a7ad50c | 2012-04-29 15:39:40 +0200 | [diff] [blame] | 9356 | smp->type = SMP_T_STR; |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 9357 | smp->data.str.str = trash.str; |
Willy Tarreau | a7ad50c | 2012-04-29 15:39:40 +0200 | [diff] [blame] | 9358 | smp->data.str.len = ctx.vlen; |
| 9359 | |
| 9360 | /* now retrieve the path */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 9361 | end = txn->req.chn->buf->p + txn->req.sol + txn->req.sl.rq.u + txn->req.sl.rq.u_l; |
Willy Tarreau | a7ad50c | 2012-04-29 15:39:40 +0200 | [diff] [blame] | 9362 | beg = http_get_path(txn); |
| 9363 | if (!beg) |
| 9364 | beg = end; |
| 9365 | |
| 9366 | for (ptr = beg; ptr < end && *ptr != '?'; ptr++); |
| 9367 | |
| 9368 | if (beg < ptr && *beg == '/') { |
| 9369 | memcpy(smp->data.str.str + smp->data.str.len, beg, ptr - beg); |
| 9370 | smp->data.str.len += ptr - beg; |
| 9371 | } |
| 9372 | |
| 9373 | smp->flags = SMP_F_VOL_1ST; |
| 9374 | return 1; |
| 9375 | } |
| 9376 | |
Willy Tarreau | ab1f7b7 | 2012-12-09 13:38:54 +0100 | [diff] [blame] | 9377 | /* This produces a 32-bit hash of the concatenation of the first occurrence of |
| 9378 | * the Host header followed by the path component if it begins with a slash ('/'). |
| 9379 | * This means that '*' will not be added, resulting in exactly the first Host |
| 9380 | * 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] | 9381 | * is hashed using the path hash followed by a full avalanche hash and provides a |
| 9382 | * 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] | 9383 | * high-traffic sites without having to store whole paths. |
| 9384 | */ |
| 9385 | static int |
| 9386 | smp_fetch_base32(struct proxy *px, struct session *l4, void *l7, unsigned int opt, |
Willy Tarreau | ef38c39 | 2013-07-22 16:29:32 +0200 | [diff] [blame] | 9387 | const struct arg *args, struct sample *smp, const char *kw) |
Willy Tarreau | ab1f7b7 | 2012-12-09 13:38:54 +0100 | [diff] [blame] | 9388 | { |
| 9389 | struct http_txn *txn = l7; |
| 9390 | struct hdr_ctx ctx; |
| 9391 | unsigned int hash = 0; |
| 9392 | char *ptr, *beg, *end; |
| 9393 | int len; |
| 9394 | |
| 9395 | CHECK_HTTP_MESSAGE_FIRST(); |
| 9396 | |
| 9397 | ctx.idx = 0; |
| 9398 | if (http_find_header2("Host", 4, txn->req.chn->buf->p + txn->req.sol, &txn->hdr_idx, &ctx)) { |
| 9399 | /* OK we have the header value in ctx.line+ctx.val for ctx.vlen bytes */ |
| 9400 | ptr = ctx.line + ctx.val; |
| 9401 | len = ctx.vlen; |
| 9402 | while (len--) |
| 9403 | hash = *(ptr++) + (hash << 6) + (hash << 16) - hash; |
| 9404 | } |
| 9405 | |
| 9406 | /* now retrieve the path */ |
| 9407 | end = txn->req.chn->buf->p + txn->req.sol + txn->req.sl.rq.u + txn->req.sl.rq.u_l; |
| 9408 | beg = http_get_path(txn); |
| 9409 | if (!beg) |
| 9410 | beg = end; |
| 9411 | |
| 9412 | for (ptr = beg; ptr < end && *ptr != '?'; ptr++); |
| 9413 | |
| 9414 | if (beg < ptr && *beg == '/') { |
| 9415 | while (beg < ptr) |
| 9416 | hash = *(beg++) + (hash << 6) + (hash << 16) - hash; |
| 9417 | } |
| 9418 | hash = full_hash(hash); |
| 9419 | |
| 9420 | smp->type = SMP_T_UINT; |
| 9421 | smp->data.uint = hash; |
| 9422 | smp->flags = SMP_F_VOL_1ST; |
| 9423 | return 1; |
| 9424 | } |
| 9425 | |
Willy Tarreau | 4a55060 | 2012-12-09 14:53:32 +0100 | [diff] [blame] | 9426 | /* 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] | 9427 | * path as returned by smp_fetch_base32(). The idea is to have per-source and |
| 9428 | * per-path counters. The result is a binary block from 8 to 20 bytes depending |
| 9429 | * 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] | 9430 | * that in environments where IPv6 is insignificant, truncating the output to |
| 9431 | * 8 bytes would still work. |
| 9432 | */ |
| 9433 | static int |
| 9434 | smp_fetch_base32_src(struct proxy *px, struct session *l4, void *l7, unsigned int opt, |
Willy Tarreau | ef38c39 | 2013-07-22 16:29:32 +0200 | [diff] [blame] | 9435 | const struct arg *args, struct sample *smp, const char *kw) |
Willy Tarreau | 4a55060 | 2012-12-09 14:53:32 +0100 | [diff] [blame] | 9436 | { |
Willy Tarreau | 47ca545 | 2012-12-23 20:22:19 +0100 | [diff] [blame] | 9437 | struct chunk *temp; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 9438 | struct connection *cli_conn = objt_conn(l4->si[0].end); |
| 9439 | |
| 9440 | if (!cli_conn) |
| 9441 | return 0; |
Willy Tarreau | 4a55060 | 2012-12-09 14:53:32 +0100 | [diff] [blame] | 9442 | |
Willy Tarreau | ef38c39 | 2013-07-22 16:29:32 +0200 | [diff] [blame] | 9443 | if (!smp_fetch_base32(px, l4, l7, opt, args, smp, kw)) |
Willy Tarreau | 4a55060 | 2012-12-09 14:53:32 +0100 | [diff] [blame] | 9444 | return 0; |
| 9445 | |
Willy Tarreau | 47ca545 | 2012-12-23 20:22:19 +0100 | [diff] [blame] | 9446 | temp = get_trash_chunk(); |
Willy Tarreau | 4a55060 | 2012-12-09 14:53:32 +0100 | [diff] [blame] | 9447 | memcpy(temp->str + temp->len, &smp->data.uint, sizeof(smp->data.uint)); |
| 9448 | temp->len += sizeof(smp->data.uint); |
| 9449 | |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 9450 | switch (cli_conn->addr.from.ss_family) { |
Willy Tarreau | 4a55060 | 2012-12-09 14:53:32 +0100 | [diff] [blame] | 9451 | case AF_INET: |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 9452 | 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] | 9453 | temp->len += 4; |
| 9454 | break; |
| 9455 | case AF_INET6: |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 9456 | 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] | 9457 | temp->len += 16; |
| 9458 | break; |
| 9459 | default: |
| 9460 | return 0; |
| 9461 | } |
| 9462 | |
| 9463 | smp->data.str = *temp; |
| 9464 | smp->type = SMP_T_BIN; |
| 9465 | return 1; |
| 9466 | } |
| 9467 | |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 9468 | static int |
Willy Tarreau | 409bcde | 2013-01-08 00:31:00 +0100 | [diff] [blame] | 9469 | smp_fetch_proto_http(struct proxy *px, struct session *l4, void *l7, unsigned int opt, |
Willy Tarreau | ef38c39 | 2013-07-22 16:29:32 +0200 | [diff] [blame] | 9470 | const struct arg *args, struct sample *smp, const char *kw) |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 9471 | { |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 9472 | /* Note: hdr_idx.v cannot be NULL in this ACL because the ACL is tagged |
| 9473 | * as a layer7 ACL, which involves automatic allocation of hdr_idx. |
| 9474 | */ |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 9475 | |
Willy Tarreau | 24e32d8 | 2012-04-23 23:55:44 +0200 | [diff] [blame] | 9476 | CHECK_HTTP_MESSAGE_FIRST_PERM(); |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 9477 | |
Willy Tarreau | f853c46 | 2012-04-23 18:53:56 +0200 | [diff] [blame] | 9478 | smp->type = SMP_T_BOOL; |
Willy Tarreau | 197e10a | 2012-04-23 19:18:42 +0200 | [diff] [blame] | 9479 | smp->data.uint = 1; |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 9480 | return 1; |
| 9481 | } |
| 9482 | |
Willy Tarreau | 7f18e52 | 2010-10-22 20:04:13 +0200 | [diff] [blame] | 9483 | /* return a valid test if the current request is the first one on the connection */ |
| 9484 | static int |
Willy Tarreau | 409bcde | 2013-01-08 00:31:00 +0100 | [diff] [blame] | 9485 | smp_fetch_http_first_req(struct proxy *px, struct session *s, void *l7, unsigned int opt, |
Willy Tarreau | ef38c39 | 2013-07-22 16:29:32 +0200 | [diff] [blame] | 9486 | const struct arg *args, struct sample *smp, const char *kw) |
Willy Tarreau | 7f18e52 | 2010-10-22 20:04:13 +0200 | [diff] [blame] | 9487 | { |
| 9488 | if (!s) |
| 9489 | return 0; |
| 9490 | |
Willy Tarreau | f853c46 | 2012-04-23 18:53:56 +0200 | [diff] [blame] | 9491 | smp->type = SMP_T_BOOL; |
Willy Tarreau | 197e10a | 2012-04-23 19:18:42 +0200 | [diff] [blame] | 9492 | smp->data.uint = !(s->txn.flags & TX_NOT_FIRST); |
Willy Tarreau | 7f18e52 | 2010-10-22 20:04:13 +0200 | [diff] [blame] | 9493 | return 1; |
| 9494 | } |
| 9495 | |
Willy Tarreau | 34db108 | 2012-04-19 17:16:54 +0200 | [diff] [blame] | 9496 | /* Accepts exactly 1 argument of type userlist */ |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 9497 | static int |
Willy Tarreau | 409bcde | 2013-01-08 00:31:00 +0100 | [diff] [blame] | 9498 | smp_fetch_http_auth(struct proxy *px, struct session *l4, void *l7, unsigned int opt, |
Willy Tarreau | ef38c39 | 2013-07-22 16:29:32 +0200 | [diff] [blame] | 9499 | const struct arg *args, struct sample *smp, const char *kw) |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 9500 | { |
| 9501 | |
Willy Tarreau | 24e32d8 | 2012-04-23 23:55:44 +0200 | [diff] [blame] | 9502 | if (!args || args->type != ARGT_USR) |
Willy Tarreau | 34db108 | 2012-04-19 17:16:54 +0200 | [diff] [blame] | 9503 | return 0; |
| 9504 | |
Willy Tarreau | c0239e0 | 2012-04-16 14:42:55 +0200 | [diff] [blame] | 9505 | CHECK_HTTP_MESSAGE_FIRST(); |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 9506 | |
Willy Tarreau | c0239e0 | 2012-04-16 14:42:55 +0200 | [diff] [blame] | 9507 | if (!get_http_auth(l4)) |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 9508 | return 0; |
| 9509 | |
Willy Tarreau | f853c46 | 2012-04-23 18:53:56 +0200 | [diff] [blame] | 9510 | smp->type = SMP_T_BOOL; |
Willy Tarreau | 24e32d8 | 2012-04-23 23:55:44 +0200 | [diff] [blame] | 9511 | smp->data.uint = check_user(args->data.usr, 0, l4->txn.auth.user, l4->txn.auth.pass); |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 9512 | return 1; |
| 9513 | } |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 9514 | |
Willy Tarreau | 4a3fd4c | 2012-05-10 23:18:26 +0200 | [diff] [blame] | 9515 | /* Accepts exactly 1 argument of type userlist */ |
| 9516 | static int |
Willy Tarreau | 409bcde | 2013-01-08 00:31:00 +0100 | [diff] [blame] | 9517 | smp_fetch_http_auth_grp(struct proxy *px, struct session *l4, void *l7, unsigned int opt, |
Willy Tarreau | ef38c39 | 2013-07-22 16:29:32 +0200 | [diff] [blame] | 9518 | const struct arg *args, struct sample *smp, const char *kw) |
Willy Tarreau | 4a3fd4c | 2012-05-10 23:18:26 +0200 | [diff] [blame] | 9519 | { |
| 9520 | |
| 9521 | if (!args || args->type != ARGT_USR) |
| 9522 | return 0; |
| 9523 | |
| 9524 | CHECK_HTTP_MESSAGE_FIRST(); |
| 9525 | |
| 9526 | if (!get_http_auth(l4)) |
| 9527 | return 0; |
| 9528 | |
Thierry FOURNIER | a65b343 | 2013-11-28 18:22:00 +0100 | [diff] [blame] | 9529 | /* pat_match_auth() will need several information at once */ |
Willy Tarreau | 4a3fd4c | 2012-05-10 23:18:26 +0200 | [diff] [blame] | 9530 | smp->ctx.a[0] = args->data.usr; /* user list */ |
| 9531 | smp->ctx.a[1] = l4->txn.auth.user; /* user name */ |
| 9532 | smp->ctx.a[2] = l4->txn.auth.pass; /* password */ |
| 9533 | |
| 9534 | /* if the user does not belong to the userlist or has a wrong password, |
| 9535 | * report that it unconditionally does not match. Otherwise we return |
| 9536 | * a non-zero integer which will be ignored anyway since all the params |
Thierry FOURNIER | a65b343 | 2013-11-28 18:22:00 +0100 | [diff] [blame] | 9537 | * that pat_match_auth() will use are in test->ctx.a[0,1,2]. |
Willy Tarreau | 4a3fd4c | 2012-05-10 23:18:26 +0200 | [diff] [blame] | 9538 | */ |
| 9539 | smp->type = SMP_T_BOOL; |
| 9540 | smp->data.uint = check_user(args->data.usr, 0, l4->txn.auth.user, l4->txn.auth.pass); |
| 9541 | if (smp->data.uint) |
| 9542 | smp->type = SMP_T_UINT; |
| 9543 | |
| 9544 | return 1; |
| 9545 | } |
| 9546 | |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 9547 | /* Try to find the next occurrence of a cookie name in a cookie header value. |
| 9548 | * The lookup begins at <hdr>. The pointer and size of the next occurrence of |
| 9549 | * the cookie value is returned into *value and *value_l, and the function |
| 9550 | * returns a pointer to the next pointer to search from if the value was found. |
| 9551 | * Otherwise if the cookie was not found, NULL is returned and neither value |
| 9552 | * nor value_l are touched. The input <hdr> string should first point to the |
| 9553 | * header's value, and the <hdr_end> pointer must point to the first character |
| 9554 | * not part of the value. <list> must be non-zero if value may represent a list |
| 9555 | * of values (cookie headers). This makes it faster to abort parsing when no |
| 9556 | * list is expected. |
| 9557 | */ |
| 9558 | static char * |
| 9559 | extract_cookie_value(char *hdr, const char *hdr_end, |
| 9560 | char *cookie_name, size_t cookie_name_l, int list, |
Willy Tarreau | 3fb818c | 2012-04-11 17:21:08 +0200 | [diff] [blame] | 9561 | char **value, int *value_l) |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 9562 | { |
| 9563 | char *equal, *att_end, *att_beg, *val_beg, *val_end; |
| 9564 | char *next; |
| 9565 | |
| 9566 | /* we search at least a cookie name followed by an equal, and more |
| 9567 | * generally something like this : |
| 9568 | * Cookie: NAME1 = VALUE 1 ; NAME2 = VALUE2 ; NAME3 = VALUE3\r\n |
| 9569 | */ |
| 9570 | for (att_beg = hdr; att_beg + cookie_name_l + 1 < hdr_end; att_beg = next + 1) { |
| 9571 | /* Iterate through all cookies on this line */ |
| 9572 | |
| 9573 | while (att_beg < hdr_end && http_is_spht[(unsigned char)*att_beg]) |
| 9574 | att_beg++; |
| 9575 | |
| 9576 | /* find att_end : this is the first character after the last non |
| 9577 | * space before the equal. It may be equal to hdr_end. |
| 9578 | */ |
| 9579 | equal = att_end = att_beg; |
| 9580 | |
| 9581 | while (equal < hdr_end) { |
| 9582 | if (*equal == '=' || *equal == ';' || (list && *equal == ',')) |
| 9583 | break; |
| 9584 | if (http_is_spht[(unsigned char)*equal++]) |
| 9585 | continue; |
| 9586 | att_end = equal; |
| 9587 | } |
| 9588 | |
| 9589 | /* here, <equal> points to '=', a delimitor or the end. <att_end> |
| 9590 | * is between <att_beg> and <equal>, both may be identical. |
| 9591 | */ |
| 9592 | |
| 9593 | /* look for end of cookie if there is an equal sign */ |
| 9594 | if (equal < hdr_end && *equal == '=') { |
| 9595 | /* look for the beginning of the value */ |
| 9596 | val_beg = equal + 1; |
| 9597 | while (val_beg < hdr_end && http_is_spht[(unsigned char)*val_beg]) |
| 9598 | val_beg++; |
| 9599 | |
| 9600 | /* find the end of the value, respecting quotes */ |
| 9601 | next = find_cookie_value_end(val_beg, hdr_end); |
| 9602 | |
| 9603 | /* make val_end point to the first white space or delimitor after the value */ |
| 9604 | val_end = next; |
| 9605 | while (val_end > val_beg && http_is_spht[(unsigned char)*(val_end - 1)]) |
| 9606 | val_end--; |
| 9607 | } else { |
| 9608 | val_beg = val_end = next = equal; |
| 9609 | } |
| 9610 | |
| 9611 | /* We have nothing to do with attributes beginning with '$'. However, |
| 9612 | * they will automatically be removed if a header before them is removed, |
| 9613 | * since they're supposed to be linked together. |
| 9614 | */ |
| 9615 | if (*att_beg == '$') |
| 9616 | continue; |
| 9617 | |
| 9618 | /* Ignore cookies with no equal sign */ |
| 9619 | if (equal == next) |
| 9620 | continue; |
| 9621 | |
| 9622 | /* Now we have the cookie name between att_beg and att_end, and |
| 9623 | * its value between val_beg and val_end. |
| 9624 | */ |
| 9625 | |
| 9626 | if (att_end - att_beg == cookie_name_l && |
| 9627 | memcmp(att_beg, cookie_name, cookie_name_l) == 0) { |
| 9628 | /* let's return this value and indicate where to go on from */ |
| 9629 | *value = val_beg; |
| 9630 | *value_l = val_end - val_beg; |
| 9631 | return next + 1; |
| 9632 | } |
| 9633 | |
| 9634 | /* Set-Cookie headers only have the name in the first attr=value part */ |
| 9635 | if (!list) |
| 9636 | break; |
| 9637 | } |
| 9638 | |
| 9639 | return NULL; |
| 9640 | } |
| 9641 | |
Willy Tarreau | e333ec9 | 2012-04-16 16:26:40 +0200 | [diff] [blame] | 9642 | /* 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] | 9643 | * 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] | 9644 | * 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] | 9645 | * 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] | 9646 | * 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] | 9647 | * Accepts exactly 1 argument of type string. If the input options indicate |
| 9648 | * that no iterating is desired, then only last value is fetched if any. |
Willy Tarreau | b169eba | 2013-12-16 15:14:43 +0100 | [diff] [blame] | 9649 | * The returned sample is of type CSTR. |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 9650 | */ |
| 9651 | static int |
Willy Tarreau | 5153936 | 2012-05-08 12:46:28 +0200 | [diff] [blame] | 9652 | smp_fetch_cookie(struct proxy *px, struct session *l4, void *l7, unsigned int opt, |
Willy Tarreau | ef38c39 | 2013-07-22 16:29:32 +0200 | [diff] [blame] | 9653 | const struct arg *args, struct sample *smp, const char *kw) |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 9654 | { |
| 9655 | struct http_txn *txn = l7; |
| 9656 | struct hdr_idx *idx = &txn->hdr_idx; |
Willy Tarreau | a890d07 | 2013-04-02 12:01:06 +0200 | [diff] [blame] | 9657 | struct hdr_ctx *ctx = smp->ctx.a[2]; |
Willy Tarreau | e333ec9 | 2012-04-16 16:26:40 +0200 | [diff] [blame] | 9658 | const struct http_msg *msg; |
| 9659 | const char *hdr_name; |
| 9660 | int hdr_name_len; |
| 9661 | char *sol; |
Willy Tarreau | 28376d6 | 2012-04-26 21:26:10 +0200 | [diff] [blame] | 9662 | int occ = 0; |
| 9663 | int found = 0; |
Willy Tarreau | e333ec9 | 2012-04-16 16:26:40 +0200 | [diff] [blame] | 9664 | |
Willy Tarreau | 24e32d8 | 2012-04-23 23:55:44 +0200 | [diff] [blame] | 9665 | if (!args || args->type != ARGT_STR) |
Willy Tarreau | 34db108 | 2012-04-19 17:16:54 +0200 | [diff] [blame] | 9666 | return 0; |
| 9667 | |
Willy Tarreau | a890d07 | 2013-04-02 12:01:06 +0200 | [diff] [blame] | 9668 | if (!ctx) { |
| 9669 | /* first call */ |
| 9670 | ctx = &static_hdr_ctx; |
| 9671 | ctx->idx = 0; |
| 9672 | smp->ctx.a[2] = ctx; |
| 9673 | } |
| 9674 | |
Willy Tarreau | e333ec9 | 2012-04-16 16:26:40 +0200 | [diff] [blame] | 9675 | CHECK_HTTP_MESSAGE_FIRST(); |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 9676 | |
Willy Tarreau | 32a6f2e | 2012-04-25 10:13:36 +0200 | [diff] [blame] | 9677 | if ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) { |
Willy Tarreau | e333ec9 | 2012-04-16 16:26:40 +0200 | [diff] [blame] | 9678 | msg = &txn->req; |
| 9679 | hdr_name = "Cookie"; |
| 9680 | hdr_name_len = 6; |
| 9681 | } else { |
| 9682 | msg = &txn->rsp; |
| 9683 | hdr_name = "Set-Cookie"; |
| 9684 | hdr_name_len = 10; |
| 9685 | } |
| 9686 | |
Willy Tarreau | 28376d6 | 2012-04-26 21:26:10 +0200 | [diff] [blame] | 9687 | if (!occ && !(opt & SMP_OPT_ITERATE)) |
| 9688 | /* no explicit occurrence and single fetch => last cookie by default */ |
| 9689 | occ = -1; |
| 9690 | |
| 9691 | /* OK so basically here, either we want only one value and it's the |
| 9692 | * last one, or we want to iterate over all of them and we fetch the |
| 9693 | * next one. |
| 9694 | */ |
| 9695 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 9696 | sol = msg->chn->buf->p; |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 9697 | if (!(smp->flags & SMP_F_NOT_LAST)) { |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 9698 | /* search for the header from the beginning, we must first initialize |
| 9699 | * the search parameters. |
| 9700 | */ |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 9701 | smp->ctx.a[0] = NULL; |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 9702 | ctx->idx = 0; |
| 9703 | } |
| 9704 | |
Willy Tarreau | 28376d6 | 2012-04-26 21:26:10 +0200 | [diff] [blame] | 9705 | smp->flags |= SMP_F_VOL_HDR; |
| 9706 | |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 9707 | while (1) { |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 9708 | /* Note: smp->ctx.a[0] == NULL every time we need to fetch a new header */ |
| 9709 | if (!smp->ctx.a[0]) { |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 9710 | if (!http_find_header2(hdr_name, hdr_name_len, sol, idx, ctx)) |
| 9711 | goto out; |
| 9712 | |
Willy Tarreau | 24e32d8 | 2012-04-23 23:55:44 +0200 | [diff] [blame] | 9713 | if (ctx->vlen < args->data.str.len + 1) |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 9714 | continue; |
| 9715 | |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 9716 | smp->ctx.a[0] = ctx->line + ctx->val; |
| 9717 | smp->ctx.a[1] = smp->ctx.a[0] + ctx->vlen; |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 9718 | } |
| 9719 | |
Willy Tarreau | f853c46 | 2012-04-23 18:53:56 +0200 | [diff] [blame] | 9720 | smp->type = SMP_T_CSTR; |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 9721 | 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] | 9722 | args->data.str.str, args->data.str.len, |
Willy Tarreau | 32a6f2e | 2012-04-25 10:13:36 +0200 | [diff] [blame] | 9723 | (opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ, |
Willy Tarreau | f853c46 | 2012-04-23 18:53:56 +0200 | [diff] [blame] | 9724 | &smp->data.str.str, |
| 9725 | &smp->data.str.len); |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 9726 | if (smp->ctx.a[0]) { |
Willy Tarreau | 28376d6 | 2012-04-26 21:26:10 +0200 | [diff] [blame] | 9727 | found = 1; |
| 9728 | if (occ >= 0) { |
| 9729 | /* one value was returned into smp->data.str.{str,len} */ |
| 9730 | smp->flags |= SMP_F_NOT_LAST; |
| 9731 | return 1; |
| 9732 | } |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 9733 | } |
Willy Tarreau | 28376d6 | 2012-04-26 21:26:10 +0200 | [diff] [blame] | 9734 | /* if we're looking for last occurrence, let's loop */ |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 9735 | } |
Willy Tarreau | 28376d6 | 2012-04-26 21:26:10 +0200 | [diff] [blame] | 9736 | /* all cookie headers and values were scanned. If we're looking for the |
| 9737 | * last occurrence, we may return it now. |
| 9738 | */ |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 9739 | out: |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 9740 | smp->flags &= ~SMP_F_NOT_LAST; |
Willy Tarreau | 28376d6 | 2012-04-26 21:26:10 +0200 | [diff] [blame] | 9741 | return found; |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 9742 | } |
| 9743 | |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 9744 | /* 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] | 9745 | * 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] | 9746 | * multiple cookies may be parsed on the same line. The returned sample is of |
| 9747 | * type UINT. Accepts exactly 1 argument of type string. |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 9748 | */ |
| 9749 | static int |
Willy Tarreau | 409bcde | 2013-01-08 00:31:00 +0100 | [diff] [blame] | 9750 | smp_fetch_cookie_cnt(struct proxy *px, struct session *l4, void *l7, unsigned int opt, |
Willy Tarreau | ef38c39 | 2013-07-22 16:29:32 +0200 | [diff] [blame] | 9751 | const struct arg *args, struct sample *smp, const char *kw) |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 9752 | { |
| 9753 | struct http_txn *txn = l7; |
| 9754 | struct hdr_idx *idx = &txn->hdr_idx; |
| 9755 | struct hdr_ctx ctx; |
Willy Tarreau | e333ec9 | 2012-04-16 16:26:40 +0200 | [diff] [blame] | 9756 | const struct http_msg *msg; |
| 9757 | const char *hdr_name; |
| 9758 | int hdr_name_len; |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 9759 | int cnt; |
| 9760 | char *val_beg, *val_end; |
Willy Tarreau | e333ec9 | 2012-04-16 16:26:40 +0200 | [diff] [blame] | 9761 | char *sol; |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 9762 | |
Willy Tarreau | 24e32d8 | 2012-04-23 23:55:44 +0200 | [diff] [blame] | 9763 | if (!args || args->type != ARGT_STR) |
Willy Tarreau | 34db108 | 2012-04-19 17:16:54 +0200 | [diff] [blame] | 9764 | return 0; |
| 9765 | |
Willy Tarreau | e333ec9 | 2012-04-16 16:26:40 +0200 | [diff] [blame] | 9766 | CHECK_HTTP_MESSAGE_FIRST(); |
| 9767 | |
Willy Tarreau | 32a6f2e | 2012-04-25 10:13:36 +0200 | [diff] [blame] | 9768 | if ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) { |
Willy Tarreau | e333ec9 | 2012-04-16 16:26:40 +0200 | [diff] [blame] | 9769 | msg = &txn->req; |
| 9770 | hdr_name = "Cookie"; |
| 9771 | hdr_name_len = 6; |
| 9772 | } else { |
| 9773 | msg = &txn->rsp; |
| 9774 | hdr_name = "Set-Cookie"; |
| 9775 | hdr_name_len = 10; |
| 9776 | } |
| 9777 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 9778 | sol = msg->chn->buf->p; |
Willy Tarreau | 46787ed | 2012-04-11 17:28:40 +0200 | [diff] [blame] | 9779 | val_end = val_beg = NULL; |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 9780 | ctx.idx = 0; |
| 9781 | cnt = 0; |
| 9782 | |
| 9783 | while (1) { |
| 9784 | /* Note: val_beg == NULL every time we need to fetch a new header */ |
| 9785 | if (!val_beg) { |
| 9786 | if (!http_find_header2(hdr_name, hdr_name_len, sol, idx, &ctx)) |
| 9787 | break; |
| 9788 | |
Willy Tarreau | 24e32d8 | 2012-04-23 23:55:44 +0200 | [diff] [blame] | 9789 | if (ctx.vlen < args->data.str.len + 1) |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 9790 | continue; |
| 9791 | |
| 9792 | val_beg = ctx.line + ctx.val; |
| 9793 | val_end = val_beg + ctx.vlen; |
| 9794 | } |
| 9795 | |
Willy Tarreau | f853c46 | 2012-04-23 18:53:56 +0200 | [diff] [blame] | 9796 | smp->type = SMP_T_CSTR; |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 9797 | while ((val_beg = extract_cookie_value(val_beg, val_end, |
Willy Tarreau | 24e32d8 | 2012-04-23 23:55:44 +0200 | [diff] [blame] | 9798 | args->data.str.str, args->data.str.len, |
Willy Tarreau | 32a6f2e | 2012-04-25 10:13:36 +0200 | [diff] [blame] | 9799 | (opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ, |
Willy Tarreau | f853c46 | 2012-04-23 18:53:56 +0200 | [diff] [blame] | 9800 | &smp->data.str.str, |
| 9801 | &smp->data.str.len))) { |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 9802 | cnt++; |
| 9803 | } |
| 9804 | } |
| 9805 | |
Willy Tarreau | b169eba | 2013-12-16 15:14:43 +0100 | [diff] [blame] | 9806 | smp->type = SMP_T_UINT; |
Willy Tarreau | f853c46 | 2012-04-23 18:53:56 +0200 | [diff] [blame] | 9807 | smp->data.uint = cnt; |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 9808 | smp->flags |= SMP_F_VOL_HDR; |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 9809 | return 1; |
| 9810 | } |
| 9811 | |
Willy Tarreau | 5153936 | 2012-05-08 12:46:28 +0200 | [diff] [blame] | 9812 | /* Fetch an cookie's integer value. The integer value is returned. It |
| 9813 | * takes a mandatory argument of type string. It relies on smp_fetch_cookie(). |
| 9814 | */ |
| 9815 | static int |
| 9816 | smp_fetch_cookie_val(struct proxy *px, struct session *l4, void *l7, unsigned int opt, |
Willy Tarreau | ef38c39 | 2013-07-22 16:29:32 +0200 | [diff] [blame] | 9817 | const struct arg *args, struct sample *smp, const char *kw) |
Willy Tarreau | 5153936 | 2012-05-08 12:46:28 +0200 | [diff] [blame] | 9818 | { |
Willy Tarreau | ef38c39 | 2013-07-22 16:29:32 +0200 | [diff] [blame] | 9819 | int ret = smp_fetch_cookie(px, l4, l7, opt, args, smp, kw); |
Willy Tarreau | 5153936 | 2012-05-08 12:46:28 +0200 | [diff] [blame] | 9820 | |
| 9821 | if (ret > 0) { |
| 9822 | smp->type = SMP_T_UINT; |
| 9823 | smp->data.uint = strl2ic(smp->data.str.str, smp->data.str.len); |
| 9824 | } |
| 9825 | |
| 9826 | return ret; |
| 9827 | } |
| 9828 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 9829 | /************************************************************************/ |
Willy Tarreau | 1278578 | 2012-04-27 21:37:17 +0200 | [diff] [blame] | 9830 | /* The code below is dedicated to sample fetches */ |
Willy Tarreau | 4a56897 | 2010-05-12 08:08:50 +0200 | [diff] [blame] | 9831 | /************************************************************************/ |
| 9832 | |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 9833 | /* |
| 9834 | * Given a path string and its length, find the position of beginning of the |
| 9835 | * query string. Returns NULL if no query string is found in the path. |
| 9836 | * |
| 9837 | * Example: if path = "/foo/bar/fubar?yo=mama;ye=daddy", and n = 22: |
| 9838 | * |
| 9839 | * find_query_string(path, n) points to "yo=mama;ye=daddy" string. |
| 9840 | */ |
bedis | 4c75cca | 2012-10-05 08:38:24 +0200 | [diff] [blame] | 9841 | 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] | 9842 | { |
| 9843 | char *p; |
Emeric Brun | 485479d | 2010-09-23 18:02:19 +0200 | [diff] [blame] | 9844 | |
bedis | 4c75cca | 2012-10-05 08:38:24 +0200 | [diff] [blame] | 9845 | p = memchr(path, delim, path_l); |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 9846 | return p ? p + 1 : NULL; |
| 9847 | } |
| 9848 | |
bedis | 4c75cca | 2012-10-05 08:38:24 +0200 | [diff] [blame] | 9849 | static inline int is_param_delimiter(char c, char delim) |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 9850 | { |
bedis | 4c75cca | 2012-10-05 08:38:24 +0200 | [diff] [blame] | 9851 | return c == '&' || c == ';' || c == delim; |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 9852 | } |
| 9853 | |
| 9854 | /* |
| 9855 | * Given a url parameter, find the starting position of the first occurence, |
| 9856 | * or NULL if the parameter is not found. |
| 9857 | * |
| 9858 | * Example: if query_string is "yo=mama;ye=daddy" and url_param_name is "ye", |
| 9859 | * the function will return query_string+8. |
| 9860 | */ |
| 9861 | static char* |
| 9862 | find_url_param_pos(char* query_string, size_t query_string_l, |
bedis | 4c75cca | 2012-10-05 08:38:24 +0200 | [diff] [blame] | 9863 | char* url_param_name, size_t url_param_name_l, |
| 9864 | char delim) |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 9865 | { |
| 9866 | char *pos, *last; |
| 9867 | |
| 9868 | pos = query_string; |
| 9869 | last = query_string + query_string_l - url_param_name_l - 1; |
| 9870 | |
| 9871 | while (pos <= last) { |
| 9872 | if (pos[url_param_name_l] == '=') { |
| 9873 | if (memcmp(pos, url_param_name, url_param_name_l) == 0) |
| 9874 | return pos; |
| 9875 | pos += url_param_name_l + 1; |
| 9876 | } |
bedis | 4c75cca | 2012-10-05 08:38:24 +0200 | [diff] [blame] | 9877 | while (pos <= last && !is_param_delimiter(*pos, delim)) |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 9878 | pos++; |
| 9879 | pos++; |
| 9880 | } |
| 9881 | return NULL; |
| 9882 | } |
| 9883 | |
| 9884 | /* |
| 9885 | * Given a url parameter name, returns its value and size into *value and |
| 9886 | * *value_l respectively, and returns non-zero. If the parameter is not found, |
| 9887 | * zero is returned and value/value_l are not touched. |
| 9888 | */ |
| 9889 | static int |
| 9890 | find_url_param_value(char* path, size_t path_l, |
| 9891 | char* url_param_name, size_t url_param_name_l, |
bedis | 4c75cca | 2012-10-05 08:38:24 +0200 | [diff] [blame] | 9892 | char** value, int* value_l, char delim) |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 9893 | { |
| 9894 | char *query_string, *qs_end; |
| 9895 | char *arg_start; |
| 9896 | char *value_start, *value_end; |
| 9897 | |
bedis | 4c75cca | 2012-10-05 08:38:24 +0200 | [diff] [blame] | 9898 | query_string = find_param_list(path, path_l, delim); |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 9899 | if (!query_string) |
| 9900 | return 0; |
| 9901 | |
| 9902 | qs_end = path + path_l; |
| 9903 | arg_start = find_url_param_pos(query_string, qs_end - query_string, |
bedis | 4c75cca | 2012-10-05 08:38:24 +0200 | [diff] [blame] | 9904 | url_param_name, url_param_name_l, |
| 9905 | delim); |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 9906 | if (!arg_start) |
| 9907 | return 0; |
| 9908 | |
| 9909 | value_start = arg_start + url_param_name_l + 1; |
| 9910 | value_end = value_start; |
| 9911 | |
bedis | 4c75cca | 2012-10-05 08:38:24 +0200 | [diff] [blame] | 9912 | while ((value_end < qs_end) && !is_param_delimiter(*value_end, delim)) |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 9913 | value_end++; |
| 9914 | |
| 9915 | *value = value_start; |
| 9916 | *value_l = value_end - value_start; |
Willy Tarreau | 0013433 | 2011-01-04 14:57:34 +0100 | [diff] [blame] | 9917 | return value_end != value_start; |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 9918 | } |
| 9919 | |
| 9920 | static int |
Willy Tarreau | 25c1ebc | 2012-04-25 16:21:44 +0200 | [diff] [blame] | 9921 | smp_fetch_url_param(struct proxy *px, struct session *l4, void *l7, unsigned int opt, |
Willy Tarreau | ef38c39 | 2013-07-22 16:29:32 +0200 | [diff] [blame] | 9922 | const struct arg *args, struct sample *smp, const char *kw) |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 9923 | { |
bedis | 4c75cca | 2012-10-05 08:38:24 +0200 | [diff] [blame] | 9924 | char delim = '?'; |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 9925 | struct http_txn *txn = l7; |
| 9926 | struct http_msg *msg = &txn->req; |
Willy Tarreau | 25c1ebc | 2012-04-25 16:21:44 +0200 | [diff] [blame] | 9927 | |
bedis | 4c75cca | 2012-10-05 08:38:24 +0200 | [diff] [blame] | 9928 | if (!args || args[0].type != ARGT_STR || |
| 9929 | (args[1].type && args[1].type != ARGT_STR)) |
Willy Tarreau | 25c1ebc | 2012-04-25 16:21:44 +0200 | [diff] [blame] | 9930 | return 0; |
| 9931 | |
| 9932 | CHECK_HTTP_MESSAGE_FIRST(); |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 9933 | |
bedis | 4c75cca | 2012-10-05 08:38:24 +0200 | [diff] [blame] | 9934 | if (args[1].type) |
| 9935 | delim = *args[1].data.str.str; |
| 9936 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 9937 | if (!find_url_param_value(msg->chn->buf->p + msg->sl.rq.u, msg->sl.rq.u_l, |
bedis | 4c75cca | 2012-10-05 08:38:24 +0200 | [diff] [blame] | 9938 | args->data.str.str, args->data.str.len, |
| 9939 | &smp->data.str.str, &smp->data.str.len, |
| 9940 | delim)) |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 9941 | return 0; |
| 9942 | |
Willy Tarreau | b8c8f1f | 2012-04-23 22:38:26 +0200 | [diff] [blame] | 9943 | smp->type = SMP_T_CSTR; |
Willy Tarreau | 25c1ebc | 2012-04-25 16:21:44 +0200 | [diff] [blame] | 9944 | smp->flags = SMP_F_VOL_1ST; |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 9945 | return 1; |
| 9946 | } |
| 9947 | |
Willy Tarreau | a9fddca | 2012-07-31 07:51:48 +0200 | [diff] [blame] | 9948 | /* Return the signed integer value for the specified url parameter (see url_param |
| 9949 | * above). |
| 9950 | */ |
| 9951 | static int |
| 9952 | smp_fetch_url_param_val(struct proxy *px, struct session *l4, void *l7, unsigned int opt, |
Willy Tarreau | ef38c39 | 2013-07-22 16:29:32 +0200 | [diff] [blame] | 9953 | const struct arg *args, struct sample *smp, const char *kw) |
Willy Tarreau | a9fddca | 2012-07-31 07:51:48 +0200 | [diff] [blame] | 9954 | { |
Willy Tarreau | ef38c39 | 2013-07-22 16:29:32 +0200 | [diff] [blame] | 9955 | int ret = smp_fetch_url_param(px, l4, l7, opt, args, smp, kw); |
Willy Tarreau | a9fddca | 2012-07-31 07:51:48 +0200 | [diff] [blame] | 9956 | |
| 9957 | if (ret > 0) { |
| 9958 | smp->type = SMP_T_UINT; |
| 9959 | smp->data.uint = strl2ic(smp->data.str.str, smp->data.str.len); |
| 9960 | } |
| 9961 | |
| 9962 | return ret; |
| 9963 | } |
| 9964 | |
Neil - HAProxy List | 39c63c5 | 2013-11-04 13:48:42 +0000 | [diff] [blame] | 9965 | /* This produces a 32-bit hash of the concatenation of the first occurrence of |
| 9966 | * the Host header followed by the path component if it begins with a slash ('/'). |
| 9967 | * This means that '*' will not be added, resulting in exactly the first Host |
| 9968 | * entry. If no Host header is found, then the path is used. The resulting value |
| 9969 | * is hashed using the url hash followed by a full avalanche hash and provides a |
| 9970 | * 32-bit integer value. This fetch is useful for tracking per-URL activity on |
| 9971 | * high-traffic sites without having to store whole paths. |
| 9972 | * this differs from the base32 functions in that it includes the url parameters |
| 9973 | * as well as the path |
| 9974 | */ |
| 9975 | static int |
| 9976 | smp_fetch_url32(struct proxy *px, struct session *l4, void *l7, unsigned int opt, |
Willy Tarreau | e155ec2 | 2013-11-18 18:33:22 +0100 | [diff] [blame] | 9977 | const struct arg *args, struct sample *smp, const char *kw) |
Neil - HAProxy List | 39c63c5 | 2013-11-04 13:48:42 +0000 | [diff] [blame] | 9978 | { |
| 9979 | struct http_txn *txn = l7; |
| 9980 | struct hdr_ctx ctx; |
| 9981 | unsigned int hash = 0; |
| 9982 | char *ptr, *beg, *end; |
| 9983 | int len; |
| 9984 | |
| 9985 | CHECK_HTTP_MESSAGE_FIRST(); |
| 9986 | |
| 9987 | ctx.idx = 0; |
| 9988 | if (http_find_header2("Host", 4, txn->req.chn->buf->p + txn->req.sol, &txn->hdr_idx, &ctx)) { |
| 9989 | /* OK we have the header value in ctx.line+ctx.val for ctx.vlen bytes */ |
| 9990 | ptr = ctx.line + ctx.val; |
| 9991 | len = ctx.vlen; |
| 9992 | while (len--) |
| 9993 | hash = *(ptr++) + (hash << 6) + (hash << 16) - hash; |
| 9994 | } |
| 9995 | |
| 9996 | /* now retrieve the path */ |
| 9997 | end = txn->req.chn->buf->p + txn->req.sol + txn->req.sl.rq.u + txn->req.sl.rq.u_l; |
| 9998 | beg = http_get_path(txn); |
| 9999 | if (!beg) |
| 10000 | beg = end; |
| 10001 | |
| 10002 | for (ptr = beg; ptr < end ; ptr++); |
| 10003 | |
| 10004 | if (beg < ptr && *beg == '/') { |
| 10005 | while (beg < ptr) |
| 10006 | hash = *(beg++) + (hash << 6) + (hash << 16) - hash; |
| 10007 | } |
| 10008 | hash = full_hash(hash); |
| 10009 | |
| 10010 | smp->type = SMP_T_UINT; |
| 10011 | smp->data.uint = hash; |
| 10012 | smp->flags = SMP_F_VOL_1ST; |
| 10013 | return 1; |
| 10014 | } |
| 10015 | |
| 10016 | /* This concatenates the source address with the 32-bit hash of the Host and |
| 10017 | * URL as returned by smp_fetch_base32(). The idea is to have per-source and |
| 10018 | * per-url counters. The result is a binary block from 8 to 20 bytes depending |
| 10019 | * on the source address length. The URL hash is stored before the address so |
| 10020 | * that in environments where IPv6 is insignificant, truncating the output to |
| 10021 | * 8 bytes would still work. |
| 10022 | */ |
| 10023 | static int |
| 10024 | smp_fetch_url32_src(struct proxy *px, struct session *l4, void *l7, unsigned int opt, |
Willy Tarreau | e155ec2 | 2013-11-18 18:33:22 +0100 | [diff] [blame] | 10025 | const struct arg *args, struct sample *smp, const char *kw) |
Neil - HAProxy List | 39c63c5 | 2013-11-04 13:48:42 +0000 | [diff] [blame] | 10026 | { |
| 10027 | struct chunk *temp; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 10028 | struct connection *cli_conn = objt_conn(l4->si[0].end); |
Neil - HAProxy List | 39c63c5 | 2013-11-04 13:48:42 +0000 | [diff] [blame] | 10029 | |
Willy Tarreau | e155ec2 | 2013-11-18 18:33:22 +0100 | [diff] [blame] | 10030 | if (!smp_fetch_url32(px, l4, l7, opt, args, smp, kw)) |
Neil - HAProxy List | 39c63c5 | 2013-11-04 13:48:42 +0000 | [diff] [blame] | 10031 | return 0; |
| 10032 | |
| 10033 | temp = get_trash_chunk(); |
| 10034 | memcpy(temp->str + temp->len, &smp->data.uint, sizeof(smp->data.uint)); |
| 10035 | temp->len += sizeof(smp->data.uint); |
| 10036 | |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 10037 | switch (cli_conn->addr.from.ss_family) { |
Neil - HAProxy List | 39c63c5 | 2013-11-04 13:48:42 +0000 | [diff] [blame] | 10038 | case AF_INET: |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 10039 | 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] | 10040 | temp->len += 4; |
| 10041 | break; |
| 10042 | case AF_INET6: |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 10043 | 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] | 10044 | temp->len += 16; |
| 10045 | break; |
| 10046 | default: |
| 10047 | return 0; |
| 10048 | } |
| 10049 | |
| 10050 | smp->data.str = *temp; |
| 10051 | smp->type = SMP_T_BIN; |
| 10052 | return 1; |
| 10053 | } |
| 10054 | |
Willy Tarreau | 185b5c4 | 2012-04-26 15:11:51 +0200 | [diff] [blame] | 10055 | /* This function is used to validate the arguments passed to any "hdr" fetch |
| 10056 | * keyword. These keywords support an optional positive or negative occurrence |
| 10057 | * number. We must ensure that the number is greater than -MAX_HDR_HISTORY. It |
| 10058 | * is assumed that the types are already the correct ones. Returns 0 on error, |
| 10059 | * non-zero if OK. If <err> is not NULL, it will be filled with a pointer to an |
| 10060 | * error message in case of error, that the caller is responsible for freeing. |
| 10061 | * The initial location must either be freeable or NULL. |
| 10062 | */ |
| 10063 | static int val_hdr(struct arg *arg, char **err_msg) |
| 10064 | { |
| 10065 | 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] | 10066 | memprintf(err_msg, "header occurrence must be >= %d", -MAX_HDR_HISTORY); |
Willy Tarreau | 185b5c4 | 2012-04-26 15:11:51 +0200 | [diff] [blame] | 10067 | return 0; |
| 10068 | } |
| 10069 | return 1; |
| 10070 | } |
| 10071 | |
Willy Tarreau | 276fae9 | 2013-07-25 14:36:01 +0200 | [diff] [blame] | 10072 | /* takes an UINT value on input supposed to represent the time since EPOCH, |
| 10073 | * adds an optional offset found in args[0] and emits a string representing |
| 10074 | * the date in RFC-1123/5322 format. |
| 10075 | */ |
| 10076 | static int sample_conv_http_date(const struct arg *args, struct sample *smp) |
| 10077 | { |
| 10078 | const char day[7][4] = { "Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun" }; |
| 10079 | const char mon[12][4] = { "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" }; |
| 10080 | struct chunk *temp; |
| 10081 | struct tm *tm; |
| 10082 | time_t curr_date = smp->data.uint; |
| 10083 | |
| 10084 | /* add offset */ |
| 10085 | if (args && (args[0].type == ARGT_SINT || args[0].type == ARGT_UINT)) |
| 10086 | curr_date += args[0].data.sint; |
| 10087 | |
| 10088 | tm = gmtime(&curr_date); |
| 10089 | |
| 10090 | temp = get_trash_chunk(); |
| 10091 | temp->len = snprintf(temp->str, temp->size - temp->len, |
| 10092 | "%s, %02d %s %04d %02d:%02d:%02d GMT", |
| 10093 | day[tm->tm_wday], tm->tm_mday, mon[tm->tm_mon], 1900+tm->tm_year, |
| 10094 | tm->tm_hour, tm->tm_min, tm->tm_sec); |
| 10095 | |
| 10096 | smp->data.str = *temp; |
| 10097 | smp->type = SMP_T_STR; |
| 10098 | return 1; |
| 10099 | } |
| 10100 | |
Willy Tarreau | 4a56897 | 2010-05-12 08:08:50 +0200 | [diff] [blame] | 10101 | /************************************************************************/ |
Willy Tarreau | 25c1ebc | 2012-04-25 16:21:44 +0200 | [diff] [blame] | 10102 | /* All supported ACL keywords must be declared here. */ |
| 10103 | /************************************************************************/ |
| 10104 | |
| 10105 | /* Note: must not be declared <const> as its list will be overwritten. |
| 10106 | * Please take care of keeping this list alphabetically sorted. |
| 10107 | */ |
Willy Tarreau | dc13c11 | 2013-06-21 23:16:39 +0200 | [diff] [blame] | 10108 | static struct acl_kw_list acl_kws = {ILH, { |
Thierry FOURNIER | a65b343 | 2013-11-28 18:22:00 +0100 | [diff] [blame] | 10109 | { "base", "base", pat_parse_str, pat_match_str }, |
| 10110 | { "base_beg", "base", pat_parse_str, pat_match_beg }, |
| 10111 | { "base_dir", "base", pat_parse_str, pat_match_dir }, |
| 10112 | { "base_dom", "base", pat_parse_str, pat_match_dom }, |
| 10113 | { "base_end", "base", pat_parse_str, pat_match_end }, |
| 10114 | { "base_len", "base", pat_parse_int, pat_match_len }, |
| 10115 | { "base_reg", "base", pat_parse_reg, pat_match_reg }, |
| 10116 | { "base_sub", "base", pat_parse_str, pat_match_sub }, |
Willy Tarreau | a7ad50c | 2012-04-29 15:39:40 +0200 | [diff] [blame] | 10117 | |
Thierry FOURNIER | a65b343 | 2013-11-28 18:22:00 +0100 | [diff] [blame] | 10118 | { "cook", "req.cook", pat_parse_str, pat_match_str }, |
| 10119 | { "cook_beg", "req.cook", pat_parse_str, pat_match_beg }, |
| 10120 | { "cook_dir", "req.cook", pat_parse_str, pat_match_dir }, |
| 10121 | { "cook_dom", "req.cook", pat_parse_str, pat_match_dom }, |
| 10122 | { "cook_end", "req.cook", pat_parse_str, pat_match_end }, |
| 10123 | { "cook_len", "req.cook", pat_parse_int, pat_match_len }, |
| 10124 | { "cook_reg", "req.cook", pat_parse_reg, pat_match_reg }, |
| 10125 | { "cook_sub", "req.cook", pat_parse_str, pat_match_sub }, |
Willy Tarreau | 25c1ebc | 2012-04-25 16:21:44 +0200 | [diff] [blame] | 10126 | |
Thierry FOURNIER | a65b343 | 2013-11-28 18:22:00 +0100 | [diff] [blame] | 10127 | { "hdr", "req.hdr", pat_parse_str, pat_match_str }, |
| 10128 | { "hdr_beg", "req.hdr", pat_parse_str, pat_match_beg }, |
| 10129 | { "hdr_dir", "req.hdr", pat_parse_str, pat_match_dir }, |
| 10130 | { "hdr_dom", "req.hdr", pat_parse_str, pat_match_dom }, |
| 10131 | { "hdr_end", "req.hdr", pat_parse_str, pat_match_end }, |
| 10132 | { "hdr_len", "req.hdr", pat_parse_int, pat_match_len }, |
| 10133 | { "hdr_reg", "req.hdr", pat_parse_reg, pat_match_reg }, |
| 10134 | { "hdr_sub", "req.hdr", pat_parse_str, pat_match_sub }, |
Willy Tarreau | 25c1ebc | 2012-04-25 16:21:44 +0200 | [diff] [blame] | 10135 | |
Thierry FOURNIER | a65b343 | 2013-11-28 18:22:00 +0100 | [diff] [blame] | 10136 | { "http_auth_group", NULL, pat_parse_strcat, pat_match_auth }, |
Willy Tarreau | 25c1ebc | 2012-04-25 16:21:44 +0200 | [diff] [blame] | 10137 | |
Thierry FOURNIER | a65b343 | 2013-11-28 18:22:00 +0100 | [diff] [blame] | 10138 | { "method", NULL, pat_parse_meth, pat_match_meth }, |
Willy Tarreau | 25c1ebc | 2012-04-25 16:21:44 +0200 | [diff] [blame] | 10139 | |
Thierry FOURNIER | a65b343 | 2013-11-28 18:22:00 +0100 | [diff] [blame] | 10140 | { "path", "path", pat_parse_str, pat_match_str }, |
| 10141 | { "path_beg", "path", pat_parse_str, pat_match_beg }, |
| 10142 | { "path_dir", "path", pat_parse_str, pat_match_dir }, |
| 10143 | { "path_dom", "path", pat_parse_str, pat_match_dom }, |
| 10144 | { "path_end", "path", pat_parse_str, pat_match_end }, |
| 10145 | { "path_len", "path", pat_parse_int, pat_match_len }, |
| 10146 | { "path_reg", "path", pat_parse_reg, pat_match_reg }, |
| 10147 | { "path_sub", "path", pat_parse_str, pat_match_sub }, |
Willy Tarreau | 25c1ebc | 2012-04-25 16:21:44 +0200 | [diff] [blame] | 10148 | |
Thierry FOURNIER | a65b343 | 2013-11-28 18:22:00 +0100 | [diff] [blame] | 10149 | { "req_ver", "req.ver", pat_parse_str, pat_match_str }, |
| 10150 | { "resp_ver", "res.ver", pat_parse_str, pat_match_str }, |
Willy Tarreau | 25c1ebc | 2012-04-25 16:21:44 +0200 | [diff] [blame] | 10151 | |
Thierry FOURNIER | a65b343 | 2013-11-28 18:22:00 +0100 | [diff] [blame] | 10152 | { "scook", "res.cook", pat_parse_str, pat_match_str }, |
| 10153 | { "scook_beg", "res.cook", pat_parse_str, pat_match_beg }, |
| 10154 | { "scook_dir", "res.cook", pat_parse_str, pat_match_dir }, |
| 10155 | { "scook_dom", "res.cook", pat_parse_str, pat_match_dom }, |
| 10156 | { "scook_end", "res.cook", pat_parse_str, pat_match_end }, |
| 10157 | { "scook_len", "res.cook", pat_parse_int, pat_match_len }, |
| 10158 | { "scook_reg", "res.cook", pat_parse_reg, pat_match_reg }, |
| 10159 | { "scook_sub", "res.cook", pat_parse_str, pat_match_sub }, |
Willy Tarreau | 25c1ebc | 2012-04-25 16:21:44 +0200 | [diff] [blame] | 10160 | |
Thierry FOURNIER | a65b343 | 2013-11-28 18:22:00 +0100 | [diff] [blame] | 10161 | { "shdr", "res.hdr", pat_parse_str, pat_match_str }, |
| 10162 | { "shdr_beg", "res.hdr", pat_parse_str, pat_match_beg }, |
| 10163 | { "shdr_dir", "res.hdr", pat_parse_str, pat_match_dir }, |
| 10164 | { "shdr_dom", "res.hdr", pat_parse_str, pat_match_dom }, |
| 10165 | { "shdr_end", "res.hdr", pat_parse_str, pat_match_end }, |
| 10166 | { "shdr_len", "res.hdr", pat_parse_int, pat_match_len }, |
| 10167 | { "shdr_reg", "res.hdr", pat_parse_reg, pat_match_reg }, |
| 10168 | { "shdr_sub", "res.hdr", pat_parse_str, pat_match_sub }, |
Willy Tarreau | 25c1ebc | 2012-04-25 16:21:44 +0200 | [diff] [blame] | 10169 | |
Thierry FOURNIER | a65b343 | 2013-11-28 18:22:00 +0100 | [diff] [blame] | 10170 | { "url", "url", pat_parse_str, pat_match_str }, |
| 10171 | { "url_beg", "url", pat_parse_str, pat_match_beg }, |
| 10172 | { "url_dir", "url", pat_parse_str, pat_match_dir }, |
| 10173 | { "url_dom", "url", pat_parse_str, pat_match_dom }, |
| 10174 | { "url_end", "url", pat_parse_str, pat_match_end }, |
| 10175 | { "url_len", "url", pat_parse_int, pat_match_len }, |
| 10176 | { "url_reg", "url", pat_parse_reg, pat_match_reg }, |
| 10177 | { "url_sub", "url", pat_parse_str, pat_match_sub }, |
Willy Tarreau | 25c1ebc | 2012-04-25 16:21:44 +0200 | [diff] [blame] | 10178 | |
Thierry FOURNIER | a65b343 | 2013-11-28 18:22:00 +0100 | [diff] [blame] | 10179 | { "urlp", "urlp", pat_parse_str, pat_match_str }, |
| 10180 | { "urlp_beg", "urlp", pat_parse_str, pat_match_beg }, |
| 10181 | { "urlp_dir", "urlp", pat_parse_str, pat_match_dir }, |
| 10182 | { "urlp_dom", "urlp", pat_parse_str, pat_match_dom }, |
| 10183 | { "urlp_end", "urlp", pat_parse_str, pat_match_end }, |
| 10184 | { "urlp_len", "urlp", pat_parse_int, pat_match_len }, |
| 10185 | { "urlp_reg", "urlp", pat_parse_reg, pat_match_reg }, |
| 10186 | { "urlp_sub", "urlp", pat_parse_str, pat_match_sub }, |
Willy Tarreau | 25c1ebc | 2012-04-25 16:21:44 +0200 | [diff] [blame] | 10187 | |
Willy Tarreau | 8ed669b | 2013-01-11 15:49:37 +0100 | [diff] [blame] | 10188 | { /* END */ }, |
Willy Tarreau | 25c1ebc | 2012-04-25 16:21:44 +0200 | [diff] [blame] | 10189 | }}; |
| 10190 | |
| 10191 | /************************************************************************/ |
| 10192 | /* All supported pattern keywords must be declared here. */ |
Willy Tarreau | 4a56897 | 2010-05-12 08:08:50 +0200 | [diff] [blame] | 10193 | /************************************************************************/ |
| 10194 | /* Note: must not be declared <const> as its list will be overwritten */ |
Willy Tarreau | dc13c11 | 2013-06-21 23:16:39 +0200 | [diff] [blame] | 10195 | static struct sample_fetch_kw_list sample_fetch_keywords = {ILH, { |
Willy Tarreau | 409bcde | 2013-01-08 00:31:00 +0100 | [diff] [blame] | 10196 | { "base", smp_fetch_base, 0, NULL, SMP_T_CSTR, SMP_USE_HRQHV }, |
| 10197 | { "base32", smp_fetch_base32, 0, NULL, SMP_T_UINT, SMP_USE_HRQHV }, |
| 10198 | { "base32+src", smp_fetch_base32_src, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV }, |
| 10199 | |
| 10200 | /* cookie is valid in both directions (eg: for "stick ...") but cook* |
| 10201 | * are only here to match the ACL's name, are request-only and are used |
| 10202 | * for ACL compatibility only. |
| 10203 | */ |
| 10204 | { "cook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_CSTR, SMP_USE_HRQHV }, |
| 10205 | { "cookie", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_CSTR, SMP_USE_HRQHV|SMP_USE_HRSHV }, |
| 10206 | { "cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV }, |
| 10207 | { "cook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV }, |
| 10208 | |
| 10209 | /* hdr is valid in both directions (eg: for "stick ...") but hdr_* are |
| 10210 | * only here to match the ACL's name, are request-only and are used for |
| 10211 | * ACL compatibility only. |
| 10212 | */ |
| 10213 | { "hdr", smp_fetch_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_CSTR, SMP_USE_HRQHV|SMP_USE_HRSHV }, |
| 10214 | { "hdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV }, |
| 10215 | { "hdr_ip", smp_fetch_hdr_ip, ARG2(0,STR,SINT), val_hdr, SMP_T_IPV4, SMP_USE_HRQHV }, |
| 10216 | { "hdr_val", smp_fetch_hdr_val, ARG2(0,STR,SINT), val_hdr, SMP_T_UINT, SMP_USE_HRQHV }, |
| 10217 | |
Willy Tarreau | 0a0daec | 2013-04-02 22:44:58 +0200 | [diff] [blame] | 10218 | { "http_auth", smp_fetch_http_auth, ARG1(1,USR), NULL, SMP_T_BOOL, SMP_USE_HRQHV }, |
| 10219 | { "http_auth_group", smp_fetch_http_auth_grp, ARG1(1,USR), NULL, SMP_T_BOOL, SMP_USE_HRQHV }, |
Willy Tarreau | 409bcde | 2013-01-08 00:31:00 +0100 | [diff] [blame] | 10220 | { "http_first_req", smp_fetch_http_first_req, 0, NULL, SMP_T_BOOL, SMP_USE_HRQHP }, |
| 10221 | { "method", smp_fetch_meth, 0, NULL, SMP_T_UINT, SMP_USE_HRQHP }, |
| 10222 | { "path", smp_fetch_path, 0, NULL, SMP_T_CSTR, SMP_USE_HRQHV }, |
Willy Tarreau | 18ed256 | 2013-01-14 15:56:36 +0100 | [diff] [blame] | 10223 | |
| 10224 | /* HTTP protocol on the request path */ |
| 10225 | { "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] | 10226 | { "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] | 10227 | |
| 10228 | /* HTTP version on the request path */ |
| 10229 | { "req.ver", smp_fetch_rqver, 0, NULL, SMP_T_CSTR, SMP_USE_HRQHV }, |
Willy Tarreau | 409bcde | 2013-01-08 00:31:00 +0100 | [diff] [blame] | 10230 | { "req_ver", smp_fetch_rqver, 0, NULL, SMP_T_CSTR, SMP_USE_HRQHV }, |
Willy Tarreau | 18ed256 | 2013-01-14 15:56:36 +0100 | [diff] [blame] | 10231 | |
| 10232 | /* HTTP version on the response path */ |
| 10233 | { "res.ver", smp_fetch_stver, 0, NULL, SMP_T_CSTR, SMP_USE_HRSHV }, |
Willy Tarreau | 409bcde | 2013-01-08 00:31:00 +0100 | [diff] [blame] | 10234 | { "resp_ver", smp_fetch_stver, 0, NULL, SMP_T_CSTR, SMP_USE_HRSHV }, |
| 10235 | |
Willy Tarreau | 18ed256 | 2013-01-14 15:56:36 +0100 | [diff] [blame] | 10236 | /* explicit req.{cook,hdr} are used to force the fetch direction to be request-only */ |
| 10237 | { "req.cook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_CSTR, SMP_USE_HRQHV }, |
| 10238 | { "req.cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV }, |
| 10239 | { "req.cook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV }, |
| 10240 | |
Willy Tarreau | 04ff9f1 | 2013-06-10 18:39:42 +0200 | [diff] [blame] | 10241 | { "req.fhdr", smp_fetch_fhdr, ARG2(0,STR,SINT), val_hdr, SMP_T_CSTR, SMP_USE_HRQHV }, |
| 10242 | { "req.fhdr_cnt", smp_fetch_fhdr_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV }, |
Willy Tarreau | 18ed256 | 2013-01-14 15:56:36 +0100 | [diff] [blame] | 10243 | { "req.hdr", smp_fetch_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_CSTR, SMP_USE_HRQHV }, |
| 10244 | { "req.hdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV }, |
| 10245 | { "req.hdr_ip", smp_fetch_hdr_ip, ARG2(0,STR,SINT), val_hdr, SMP_T_IPV4, SMP_USE_HRQHV }, |
| 10246 | { "req.hdr_val", smp_fetch_hdr_val, ARG2(0,STR,SINT), val_hdr, SMP_T_UINT, SMP_USE_HRQHV }, |
| 10247 | |
| 10248 | /* explicit req.{cook,hdr} are used to force the fetch direction to be response-only */ |
| 10249 | { "res.cook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_CSTR, SMP_USE_HRSHV }, |
| 10250 | { "res.cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRSHV }, |
| 10251 | { "res.cook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRSHV }, |
| 10252 | |
Willy Tarreau | 04ff9f1 | 2013-06-10 18:39:42 +0200 | [diff] [blame] | 10253 | { "res.fhdr", smp_fetch_fhdr, ARG2(0,STR,SINT), val_hdr, SMP_T_CSTR, SMP_USE_HRSHV }, |
| 10254 | { "res.fhdr_cnt", smp_fetch_fhdr_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRSHV }, |
Willy Tarreau | 18ed256 | 2013-01-14 15:56:36 +0100 | [diff] [blame] | 10255 | { "res.hdr", smp_fetch_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_CSTR, SMP_USE_HRSHV }, |
| 10256 | { "res.hdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRSHV }, |
| 10257 | { "res.hdr_ip", smp_fetch_hdr_ip, ARG2(0,STR,SINT), val_hdr, SMP_T_IPV4, SMP_USE_HRSHV }, |
| 10258 | { "res.hdr_val", smp_fetch_hdr_val, ARG2(0,STR,SINT), val_hdr, SMP_T_UINT, SMP_USE_HRSHV }, |
| 10259 | |
Willy Tarreau | 409bcde | 2013-01-08 00:31:00 +0100 | [diff] [blame] | 10260 | /* scook is valid only on the response and is used for ACL compatibility */ |
| 10261 | { "scook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_CSTR, SMP_USE_HRSHV }, |
| 10262 | { "scook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRSHV }, |
| 10263 | { "scook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRSHV }, |
| 10264 | { "set-cookie", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_CSTR, SMP_USE_HRSHV }, /* deprecated */ |
| 10265 | |
| 10266 | /* shdr is valid only on the response and is used for ACL compatibility */ |
| 10267 | { "shdr", smp_fetch_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_CSTR, SMP_USE_HRSHV }, |
| 10268 | { "shdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRSHV }, |
| 10269 | { "shdr_ip", smp_fetch_hdr_ip, ARG2(0,STR,SINT), val_hdr, SMP_T_IPV4, SMP_USE_HRSHV }, |
| 10270 | { "shdr_val", smp_fetch_hdr_val, ARG2(0,STR,SINT), val_hdr, SMP_T_UINT, SMP_USE_HRSHV }, |
| 10271 | |
| 10272 | { "status", smp_fetch_stcode, 0, NULL, SMP_T_UINT, SMP_USE_HRSHP }, |
| 10273 | { "url", smp_fetch_url, 0, NULL, SMP_T_CSTR, SMP_USE_HRQHV }, |
Neil - HAProxy List | 39c63c5 | 2013-11-04 13:48:42 +0000 | [diff] [blame] | 10274 | { "url32", smp_fetch_url32, 0, NULL, SMP_T_UINT, SMP_USE_HRQHV }, |
| 10275 | { "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] | 10276 | { "url_ip", smp_fetch_url_ip, 0, NULL, SMP_T_IPV4, SMP_USE_HRQHV }, |
| 10277 | { "url_port", smp_fetch_url_port, 0, NULL, SMP_T_UINT, SMP_USE_HRQHV }, |
| 10278 | { "url_param", smp_fetch_url_param, ARG2(1,STR,STR), NULL, SMP_T_CSTR, SMP_USE_HRQHV }, |
| 10279 | { "urlp" , smp_fetch_url_param, ARG2(1,STR,STR), NULL, SMP_T_CSTR, SMP_USE_HRQHV }, |
| 10280 | { "urlp_val", smp_fetch_url_param_val, ARG2(1,STR,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV }, |
| 10281 | { /* END */ }, |
Willy Tarreau | 4a56897 | 2010-05-12 08:08:50 +0200 | [diff] [blame] | 10282 | }}; |
| 10283 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 10284 | |
Willy Tarreau | 276fae9 | 2013-07-25 14:36:01 +0200 | [diff] [blame] | 10285 | /* Note: must not be declared <const> as its list will be overwritten */ |
| 10286 | static struct sample_conv_kw_list sample_conv_kws = {ILH, { |
| 10287 | { "http_date", sample_conv_http_date, ARG1(0,SINT), NULL, SMP_T_UINT, SMP_T_STR }, |
| 10288 | { NULL, NULL, 0, 0, 0 }, |
| 10289 | }}; |
| 10290 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 10291 | __attribute__((constructor)) |
| 10292 | static void __http_protocol_init(void) |
| 10293 | { |
| 10294 | acl_register_keywords(&acl_kws); |
Willy Tarreau | 1278578 | 2012-04-27 21:37:17 +0200 | [diff] [blame] | 10295 | sample_register_fetches(&sample_fetch_keywords); |
Willy Tarreau | 276fae9 | 2013-07-25 14:36:01 +0200 | [diff] [blame] | 10296 | sample_register_convs(&sample_conv_kws); |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 10297 | } |
| 10298 | |
| 10299 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 10300 | /* |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 10301 | * Local variables: |
| 10302 | * c-indent-level: 8 |
| 10303 | * c-basic-offset: 8 |
| 10304 | * End: |
| 10305 | */ |