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))]; |
Thierry FOURNIER | d048d8b | 2014-03-13 16:46:18 +0100 | [diff] [blame] | 238 | fd_set http_encode_map[(sizeof(fd_set) > (256/8)) ? 1 : ((256/8) / sizeof(fd_set))]; |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 239 | |
| 240 | #else |
| 241 | #error "Check if your OS uses bitfields for fd_sets" |
| 242 | #endif |
| 243 | |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 244 | void init_proto_http() |
| 245 | { |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 246 | int i; |
| 247 | char *tmp; |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 248 | int msg; |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 249 | |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 250 | for (msg = 0; msg < HTTP_ERR_SIZE; msg++) { |
| 251 | if (!http_err_msgs[msg]) { |
| 252 | Alert("Internal error: no message defined for HTTP return code %d. Aborting.\n", msg); |
| 253 | abort(); |
| 254 | } |
| 255 | |
| 256 | http_err_chunks[msg].str = (char *)http_err_msgs[msg]; |
| 257 | http_err_chunks[msg].len = strlen(http_err_msgs[msg]); |
| 258 | } |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 259 | |
| 260 | /* initialize the log header encoding map : '{|}"#' should be encoded with |
| 261 | * '#' as prefix, as well as non-printable characters ( <32 or >= 127 ). |
| 262 | * URL encoding only requires '"', '#' to be encoded as well as non- |
| 263 | * printable characters above. |
| 264 | */ |
| 265 | memset(hdr_encode_map, 0, sizeof(hdr_encode_map)); |
| 266 | memset(url_encode_map, 0, sizeof(url_encode_map)); |
Thierry FOURNIER | d048d8b | 2014-03-13 16:46:18 +0100 | [diff] [blame] | 267 | memset(http_encode_map, 0, sizeof(url_encode_map)); |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 268 | for (i = 0; i < 32; i++) { |
| 269 | FD_SET(i, hdr_encode_map); |
| 270 | FD_SET(i, url_encode_map); |
| 271 | } |
| 272 | for (i = 127; i < 256; i++) { |
| 273 | FD_SET(i, hdr_encode_map); |
| 274 | FD_SET(i, url_encode_map); |
| 275 | } |
| 276 | |
| 277 | tmp = "\"#{|}"; |
| 278 | while (*tmp) { |
| 279 | FD_SET(*tmp, hdr_encode_map); |
| 280 | tmp++; |
| 281 | } |
| 282 | |
| 283 | tmp = "\"#"; |
| 284 | while (*tmp) { |
| 285 | FD_SET(*tmp, url_encode_map); |
| 286 | tmp++; |
| 287 | } |
Willy Tarreau | 332f8bf | 2007-05-13 21:36:56 +0200 | [diff] [blame] | 288 | |
Thierry FOURNIER | d048d8b | 2014-03-13 16:46:18 +0100 | [diff] [blame] | 289 | /* initialize the http header encoding map. The draft httpbis define the |
| 290 | * header content as: |
| 291 | * |
| 292 | * HTTP-message = start-line |
| 293 | * *( header-field CRLF ) |
| 294 | * CRLF |
| 295 | * [ message-body ] |
| 296 | * header-field = field-name ":" OWS field-value OWS |
| 297 | * field-value = *( field-content / obs-fold ) |
| 298 | * field-content = field-vchar [ 1*( SP / HTAB ) field-vchar ] |
| 299 | * obs-fold = CRLF 1*( SP / HTAB ) |
| 300 | * field-vchar = VCHAR / obs-text |
| 301 | * VCHAR = %x21-7E |
| 302 | * obs-text = %x80-FF |
| 303 | * |
| 304 | * All the chars are encoded except "VCHAR", "obs-text", SP and HTAB. |
| 305 | * The encoded chars are form 0x00 to 0x08, 0x0a to 0x1f and 0x7f. The |
| 306 | * "obs-fold" is volontary forgotten because haproxy remove this. |
| 307 | */ |
| 308 | memset(http_encode_map, 0, sizeof(http_encode_map)); |
| 309 | for (i = 0x00; i <= 0x08; i++) |
| 310 | FD_SET(i, http_encode_map); |
| 311 | for (i = 0x0a; i <= 0x1f; i++) |
| 312 | FD_SET(i, http_encode_map); |
| 313 | FD_SET(0x7f, http_encode_map); |
| 314 | |
Willy Tarreau | 332f8bf | 2007-05-13 21:36:56 +0200 | [diff] [blame] | 315 | /* memory allocations */ |
| 316 | pool2_requri = create_pool("requri", REQURI_LEN, MEM_F_SHARED); |
William Lallemand | a73203e | 2012-03-12 12:48:57 +0100 | [diff] [blame] | 317 | pool2_uniqueid = create_pool("uniqueid", UNIQUEID_LEN, MEM_F_SHARED); |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 318 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 319 | |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 320 | /* |
| 321 | * We have 26 list of methods (1 per first letter), each of which can have |
| 322 | * up to 3 entries (2 valid, 1 null). |
| 323 | */ |
| 324 | struct http_method_desc { |
Willy Tarreau | c8987b3 | 2013-12-06 23:43:17 +0100 | [diff] [blame] | 325 | enum http_meth_t meth; |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 326 | int len; |
| 327 | const char text[8]; |
| 328 | }; |
| 329 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 330 | const struct http_method_desc http_methods[26][3] = { |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 331 | ['C' - 'A'] = { |
| 332 | [0] = { .meth = HTTP_METH_CONNECT , .len=7, .text="CONNECT" }, |
| 333 | }, |
| 334 | ['D' - 'A'] = { |
| 335 | [0] = { .meth = HTTP_METH_DELETE , .len=6, .text="DELETE" }, |
| 336 | }, |
| 337 | ['G' - 'A'] = { |
| 338 | [0] = { .meth = HTTP_METH_GET , .len=3, .text="GET" }, |
| 339 | }, |
| 340 | ['H' - 'A'] = { |
| 341 | [0] = { .meth = HTTP_METH_HEAD , .len=4, .text="HEAD" }, |
| 342 | }, |
| 343 | ['P' - 'A'] = { |
| 344 | [0] = { .meth = HTTP_METH_POST , .len=4, .text="POST" }, |
| 345 | [1] = { .meth = HTTP_METH_PUT , .len=3, .text="PUT" }, |
| 346 | }, |
| 347 | ['T' - 'A'] = { |
| 348 | [0] = { .meth = HTTP_METH_TRACE , .len=5, .text="TRACE" }, |
| 349 | }, |
| 350 | /* rest is empty like this : |
| 351 | * [1] = { .meth = HTTP_METH_NONE , .len=0, .text="" }, |
| 352 | */ |
| 353 | }; |
| 354 | |
Thierry FOURNIER | d437314 | 2013-12-17 01:10:10 +0100 | [diff] [blame] | 355 | const struct http_method_name http_known_methods[HTTP_METH_OTHER] = { |
| 356 | [HTTP_METH_NONE] = { "", 0 }, |
| 357 | [HTTP_METH_OPTIONS] = { "OPTIONS", 7 }, |
| 358 | [HTTP_METH_GET] = { "GET", 3 }, |
| 359 | [HTTP_METH_HEAD] = { "HEAD", 4 }, |
| 360 | [HTTP_METH_POST] = { "POST", 4 }, |
| 361 | [HTTP_METH_PUT] = { "PUT", 3 }, |
| 362 | [HTTP_METH_DELETE] = { "DELETE", 6 }, |
| 363 | [HTTP_METH_TRACE] = { "TRACE", 5 }, |
| 364 | [HTTP_METH_CONNECT] = { "CONNECT", 7 }, |
| 365 | }; |
| 366 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 367 | /* 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] | 368 | * 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] | 369 | * RFC2616 for those chars. |
| 370 | */ |
| 371 | |
| 372 | const char http_is_spht[256] = { |
| 373 | [' '] = 1, ['\t'] = 1, |
| 374 | }; |
| 375 | |
| 376 | const char http_is_crlf[256] = { |
| 377 | ['\r'] = 1, ['\n'] = 1, |
| 378 | }; |
| 379 | |
| 380 | const char http_is_lws[256] = { |
| 381 | [' '] = 1, ['\t'] = 1, |
| 382 | ['\r'] = 1, ['\n'] = 1, |
| 383 | }; |
| 384 | |
| 385 | const char http_is_sep[256] = { |
| 386 | ['('] = 1, [')'] = 1, ['<'] = 1, ['>'] = 1, |
| 387 | ['@'] = 1, [','] = 1, [';'] = 1, [':'] = 1, |
| 388 | ['"'] = 1, ['/'] = 1, ['['] = 1, [']'] = 1, |
| 389 | ['{'] = 1, ['}'] = 1, ['?'] = 1, ['='] = 1, |
| 390 | [' '] = 1, ['\t'] = 1, ['\\'] = 1, |
| 391 | }; |
| 392 | |
| 393 | const char http_is_ctl[256] = { |
| 394 | [0 ... 31] = 1, |
| 395 | [127] = 1, |
| 396 | }; |
| 397 | |
| 398 | /* |
| 399 | * A token is any ASCII char that is neither a separator nor a CTL char. |
| 400 | * Do not overwrite values in assignment since gcc-2.95 will not handle |
| 401 | * them correctly. Instead, define every non-CTL char's status. |
| 402 | */ |
| 403 | const char http_is_token[256] = { |
| 404 | [' '] = 0, ['!'] = 1, ['"'] = 0, ['#'] = 1, |
| 405 | ['$'] = 1, ['%'] = 1, ['&'] = 1, ['\''] = 1, |
| 406 | ['('] = 0, [')'] = 0, ['*'] = 1, ['+'] = 1, |
| 407 | [','] = 0, ['-'] = 1, ['.'] = 1, ['/'] = 0, |
| 408 | ['0'] = 1, ['1'] = 1, ['2'] = 1, ['3'] = 1, |
| 409 | ['4'] = 1, ['5'] = 1, ['6'] = 1, ['7'] = 1, |
| 410 | ['8'] = 1, ['9'] = 1, [':'] = 0, [';'] = 0, |
| 411 | ['<'] = 0, ['='] = 0, ['>'] = 0, ['?'] = 0, |
| 412 | ['@'] = 0, ['A'] = 1, ['B'] = 1, ['C'] = 1, |
| 413 | ['D'] = 1, ['E'] = 1, ['F'] = 1, ['G'] = 1, |
| 414 | ['H'] = 1, ['I'] = 1, ['J'] = 1, ['K'] = 1, |
| 415 | ['L'] = 1, ['M'] = 1, ['N'] = 1, ['O'] = 1, |
| 416 | ['P'] = 1, ['Q'] = 1, ['R'] = 1, ['S'] = 1, |
| 417 | ['T'] = 1, ['U'] = 1, ['V'] = 1, ['W'] = 1, |
| 418 | ['X'] = 1, ['Y'] = 1, ['Z'] = 1, ['['] = 0, |
| 419 | ['\\'] = 0, [']'] = 0, ['^'] = 1, ['_'] = 1, |
| 420 | ['`'] = 1, ['a'] = 1, ['b'] = 1, ['c'] = 1, |
| 421 | ['d'] = 1, ['e'] = 1, ['f'] = 1, ['g'] = 1, |
| 422 | ['h'] = 1, ['i'] = 1, ['j'] = 1, ['k'] = 1, |
| 423 | ['l'] = 1, ['m'] = 1, ['n'] = 1, ['o'] = 1, |
| 424 | ['p'] = 1, ['q'] = 1, ['r'] = 1, ['s'] = 1, |
| 425 | ['t'] = 1, ['u'] = 1, ['v'] = 1, ['w'] = 1, |
| 426 | ['x'] = 1, ['y'] = 1, ['z'] = 1, ['{'] = 0, |
| 427 | ['|'] = 1, ['}'] = 0, ['~'] = 1, |
| 428 | }; |
| 429 | |
| 430 | |
Willy Tarreau | 4b89ad4 | 2007-03-04 18:13:58 +0100 | [diff] [blame] | 431 | /* |
| 432 | * An http ver_token is any ASCII which can be found in an HTTP version, |
| 433 | * which includes 'H', 'T', 'P', '/', '.' and any digit. |
| 434 | */ |
| 435 | const char http_is_ver_token[256] = { |
| 436 | ['.'] = 1, ['/'] = 1, |
| 437 | ['0'] = 1, ['1'] = 1, ['2'] = 1, ['3'] = 1, ['4'] = 1, |
| 438 | ['5'] = 1, ['6'] = 1, ['7'] = 1, ['8'] = 1, ['9'] = 1, |
| 439 | ['H'] = 1, ['P'] = 1, ['T'] = 1, |
| 440 | }; |
| 441 | |
| 442 | |
Willy Tarreau | 4af6f3a | 2007-03-18 22:36:26 +0100 | [diff] [blame] | 443 | /* |
Willy Tarreau | e988a79 | 2010-01-04 21:13:14 +0100 | [diff] [blame] | 444 | * Silent debug that outputs only in strace, using fd #-1. Trash is modified. |
| 445 | */ |
| 446 | #if defined(DEBUG_FSM) |
| 447 | static void http_silent_debug(int line, struct session *s) |
| 448 | { |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 449 | chunk_printf(&trash, |
| 450 | "[%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", |
| 451 | line, |
| 452 | s->si[0].state, s->si[0].fd, s->txn.req.msg_state, s->req->flags, s->req->analysers, |
| 453 | 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); |
| 454 | write(-1, trash.str, trash.len); |
Willy Tarreau | e988a79 | 2010-01-04 21:13:14 +0100 | [diff] [blame] | 455 | |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 456 | chunk_printf(&trash, |
| 457 | " %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", |
| 458 | line, |
| 459 | s->si[1].state, s->si[1].fd, s->txn.rsp.msg_state, s->rep->flags, s->rep->analysers, |
| 460 | 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); |
| 461 | write(-1, trash.str, trash.len); |
Willy Tarreau | e988a79 | 2010-01-04 21:13:14 +0100 | [diff] [blame] | 462 | } |
| 463 | #else |
| 464 | #define http_silent_debug(l,s) do { } while (0) |
| 465 | #endif |
| 466 | |
| 467 | /* |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 468 | * Adds a header and its CRLF at the tail of the message's buffer, just before |
| 469 | * 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] | 470 | * The header is also automatically added to the index <hdr_idx>, and the end |
| 471 | * of headers is automatically adjusted. The number of bytes added is returned |
| 472 | * on success, otherwise <0 is returned indicating an error. |
| 473 | */ |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 474 | 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] | 475 | { |
| 476 | int bytes, len; |
| 477 | |
| 478 | len = strlen(text); |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 479 | 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] | 480 | if (!bytes) |
| 481 | return -1; |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 482 | http_msg_move_end(msg, bytes); |
Willy Tarreau | 4af6f3a | 2007-03-18 22:36:26 +0100 | [diff] [blame] | 483 | return hdr_idx_add(len, 1, hdr_idx, hdr_idx->tail); |
| 484 | } |
| 485 | |
| 486 | /* |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 487 | * Adds a header and its CRLF at the tail of the message's buffer, just before |
| 488 | * 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] | 489 | * the buffer is only opened and the space reserved, but nothing is copied. |
| 490 | * The header is also automatically added to the index <hdr_idx>, and the end |
| 491 | * of headers is automatically adjusted. The number of bytes added is returned |
| 492 | * on success, otherwise <0 is returned indicating an error. |
| 493 | */ |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 494 | int http_header_add_tail2(struct http_msg *msg, |
| 495 | struct hdr_idx *hdr_idx, const char *text, int len) |
Willy Tarreau | 4af6f3a | 2007-03-18 22:36:26 +0100 | [diff] [blame] | 496 | { |
| 497 | int bytes; |
| 498 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 499 | 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] | 500 | if (!bytes) |
| 501 | return -1; |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 502 | http_msg_move_end(msg, bytes); |
Willy Tarreau | 4af6f3a | 2007-03-18 22:36:26 +0100 | [diff] [blame] | 503 | return hdr_idx_add(len, 1, hdr_idx, hdr_idx->tail); |
| 504 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 505 | |
| 506 | /* |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 507 | * Checks if <hdr> is exactly <name> for <len> chars, and ends with a colon. |
| 508 | * If so, returns the position of the first non-space character relative to |
| 509 | * <hdr>, or <end>-<hdr> if not found before. If no value is found, it tries |
| 510 | * to return a pointer to the place after the first space. Returns 0 if the |
| 511 | * header name does not match. Checks are case-insensitive. |
| 512 | */ |
| 513 | int http_header_match2(const char *hdr, const char *end, |
| 514 | const char *name, int len) |
| 515 | { |
| 516 | const char *val; |
| 517 | |
| 518 | if (hdr + len >= end) |
| 519 | return 0; |
| 520 | if (hdr[len] != ':') |
| 521 | return 0; |
| 522 | if (strncasecmp(hdr, name, len) != 0) |
| 523 | return 0; |
| 524 | val = hdr + len + 1; |
| 525 | while (val < end && HTTP_IS_SPHT(*val)) |
| 526 | val++; |
| 527 | if ((val >= end) && (len + 2 <= end - hdr)) |
| 528 | return len + 2; /* we may replace starting from second space */ |
| 529 | return val - hdr; |
| 530 | } |
| 531 | |
Willy Tarreau | 04ff9f1 | 2013-06-10 18:39:42 +0200 | [diff] [blame] | 532 | /* Find the first or next occurrence of header <name> in message buffer <sol> |
| 533 | * using headers index <idx>, and return it in the <ctx> structure. This |
| 534 | * structure holds everything necessary to use the header and find next |
| 535 | * occurrence. If its <idx> member is 0, the header is searched from the |
| 536 | * beginning. Otherwise, the next occurrence is returned. The function returns |
| 537 | * 1 when it finds a value, and 0 when there is no more. It is very similar to |
| 538 | * http_find_header2() except that it is designed to work with full-line headers |
| 539 | * whose comma is not a delimiter but is part of the syntax. As a special case, |
| 540 | * if ctx->val is NULL when searching for a new values of a header, the current |
| 541 | * header is rescanned. This allows rescanning after a header deletion. |
| 542 | */ |
| 543 | int http_find_full_header2(const char *name, int len, |
| 544 | char *sol, struct hdr_idx *idx, |
| 545 | struct hdr_ctx *ctx) |
| 546 | { |
| 547 | char *eol, *sov; |
| 548 | int cur_idx, old_idx; |
| 549 | |
| 550 | cur_idx = ctx->idx; |
| 551 | if (cur_idx) { |
| 552 | /* We have previously returned a header, let's search another one */ |
| 553 | sol = ctx->line; |
| 554 | eol = sol + idx->v[cur_idx].len; |
| 555 | goto next_hdr; |
| 556 | } |
| 557 | |
| 558 | /* first request for this header */ |
| 559 | sol += hdr_idx_first_pos(idx); |
| 560 | old_idx = 0; |
| 561 | cur_idx = hdr_idx_first_idx(idx); |
| 562 | while (cur_idx) { |
| 563 | eol = sol + idx->v[cur_idx].len; |
| 564 | |
| 565 | if (len == 0) { |
| 566 | /* No argument was passed, we want any header. |
| 567 | * To achieve this, we simply build a fake request. */ |
| 568 | while (sol + len < eol && sol[len] != ':') |
| 569 | len++; |
| 570 | name = sol; |
| 571 | } |
| 572 | |
| 573 | if ((len < eol - sol) && |
| 574 | (sol[len] == ':') && |
| 575 | (strncasecmp(sol, name, len) == 0)) { |
| 576 | ctx->del = len; |
| 577 | sov = sol + len + 1; |
| 578 | while (sov < eol && http_is_lws[(unsigned char)*sov]) |
| 579 | sov++; |
| 580 | |
| 581 | ctx->line = sol; |
| 582 | ctx->prev = old_idx; |
| 583 | ctx->idx = cur_idx; |
| 584 | ctx->val = sov - sol; |
| 585 | ctx->tws = 0; |
| 586 | while (eol > sov && http_is_lws[(unsigned char)*(eol - 1)]) { |
| 587 | eol--; |
| 588 | ctx->tws++; |
| 589 | } |
| 590 | ctx->vlen = eol - sov; |
| 591 | return 1; |
| 592 | } |
| 593 | next_hdr: |
| 594 | sol = eol + idx->v[cur_idx].cr + 1; |
| 595 | old_idx = cur_idx; |
| 596 | cur_idx = idx->v[cur_idx].next; |
| 597 | } |
| 598 | return 0; |
| 599 | } |
| 600 | |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 601 | /* Find the end of the header value contained between <s> and <e>. See RFC2616, |
| 602 | * par 2.2 for more information. Note that it requires a valid header to return |
| 603 | * a valid result. This works for headers defined as comma-separated lists. |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 604 | */ |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 605 | char *find_hdr_value_end(char *s, const char *e) |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 606 | { |
| 607 | int quoted, qdpair; |
| 608 | |
| 609 | quoted = qdpair = 0; |
| 610 | for (; s < e; s++) { |
| 611 | if (qdpair) qdpair = 0; |
Willy Tarreau | 0f7f51f | 2010-08-30 11:06:34 +0200 | [diff] [blame] | 612 | else if (quoted) { |
| 613 | if (*s == '\\') qdpair = 1; |
| 614 | else if (*s == '"') quoted = 0; |
| 615 | } |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 616 | else if (*s == '"') quoted = 1; |
| 617 | else if (*s == ',') return s; |
| 618 | } |
| 619 | return s; |
| 620 | } |
| 621 | |
| 622 | /* Find the first or next occurrence of header <name> in message buffer <sol> |
| 623 | * using headers index <idx>, and return it in the <ctx> structure. This |
| 624 | * structure holds everything necessary to use the header and find next |
| 625 | * occurrence. If its <idx> member is 0, the header is searched from the |
| 626 | * beginning. Otherwise, the next occurrence is returned. The function returns |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 627 | * 1 when it finds a value, and 0 when there is no more. It is designed to work |
| 628 | * with headers defined as comma-separated lists. As a special case, if ctx->val |
| 629 | * is NULL when searching for a new values of a header, the current header is |
| 630 | * rescanned. This allows rescanning after a header deletion. |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 631 | */ |
| 632 | int http_find_header2(const char *name, int len, |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 633 | char *sol, struct hdr_idx *idx, |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 634 | struct hdr_ctx *ctx) |
| 635 | { |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 636 | char *eol, *sov; |
| 637 | int cur_idx, old_idx; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 638 | |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 639 | cur_idx = ctx->idx; |
| 640 | if (cur_idx) { |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 641 | /* We have previously returned a value, let's search |
| 642 | * another one on the same line. |
| 643 | */ |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 644 | sol = ctx->line; |
Willy Tarreau | 588bd4f | 2011-09-01 22:22:28 +0200 | [diff] [blame] | 645 | ctx->del = ctx->val + ctx->vlen + ctx->tws; |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 646 | sov = sol + ctx->del; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 647 | eol = sol + idx->v[cur_idx].len; |
| 648 | |
| 649 | if (sov >= eol) |
| 650 | /* no more values in this header */ |
| 651 | goto next_hdr; |
| 652 | |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 653 | /* values remaining for this header, skip the comma but save it |
| 654 | * for later use (eg: for header deletion). |
| 655 | */ |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 656 | sov++; |
| 657 | while (sov < eol && http_is_lws[(unsigned char)*sov]) |
| 658 | sov++; |
| 659 | |
| 660 | goto return_hdr; |
| 661 | } |
| 662 | |
| 663 | /* first request for this header */ |
| 664 | sol += hdr_idx_first_pos(idx); |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 665 | old_idx = 0; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 666 | cur_idx = hdr_idx_first_idx(idx); |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 667 | while (cur_idx) { |
| 668 | eol = sol + idx->v[cur_idx].len; |
| 669 | |
Willy Tarreau | 1ad7c6d | 2007-06-10 21:42:55 +0200 | [diff] [blame] | 670 | if (len == 0) { |
| 671 | /* No argument was passed, we want any header. |
| 672 | * To achieve this, we simply build a fake request. */ |
| 673 | while (sol + len < eol && sol[len] != ':') |
| 674 | len++; |
| 675 | name = sol; |
| 676 | } |
| 677 | |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 678 | if ((len < eol - sol) && |
| 679 | (sol[len] == ':') && |
| 680 | (strncasecmp(sol, name, len) == 0)) { |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 681 | ctx->del = len; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 682 | sov = sol + len + 1; |
| 683 | while (sov < eol && http_is_lws[(unsigned char)*sov]) |
| 684 | sov++; |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 685 | |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 686 | ctx->line = sol; |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 687 | ctx->prev = old_idx; |
| 688 | return_hdr: |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 689 | ctx->idx = cur_idx; |
| 690 | ctx->val = sov - sol; |
| 691 | |
| 692 | eol = find_hdr_value_end(sov, eol); |
Willy Tarreau | 588bd4f | 2011-09-01 22:22:28 +0200 | [diff] [blame] | 693 | ctx->tws = 0; |
Willy Tarreau | 275600b | 2011-09-16 08:11:26 +0200 | [diff] [blame] | 694 | while (eol > sov && http_is_lws[(unsigned char)*(eol - 1)]) { |
Willy Tarreau | 588bd4f | 2011-09-01 22:22:28 +0200 | [diff] [blame] | 695 | eol--; |
| 696 | ctx->tws++; |
| 697 | } |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 698 | ctx->vlen = eol - sov; |
| 699 | return 1; |
| 700 | } |
| 701 | next_hdr: |
| 702 | sol = eol + idx->v[cur_idx].cr + 1; |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 703 | old_idx = cur_idx; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 704 | cur_idx = idx->v[cur_idx].next; |
| 705 | } |
| 706 | return 0; |
| 707 | } |
| 708 | |
| 709 | int http_find_header(const char *name, |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 710 | char *sol, struct hdr_idx *idx, |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 711 | struct hdr_ctx *ctx) |
| 712 | { |
| 713 | return http_find_header2(name, strlen(name), sol, idx, ctx); |
| 714 | } |
| 715 | |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 716 | /* Remove one value of a header. This only works on a <ctx> returned by one of |
| 717 | * the http_find_header functions. The value is removed, as well as surrounding |
| 718 | * 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] | 719 | * 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] | 720 | * message <msg>. The new index is returned. If it is zero, it means there is |
| 721 | * no more header, so any processing may stop. The ctx is always left in a form |
| 722 | * that can be handled by http_find_header2() to find next occurrence. |
| 723 | */ |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 724 | 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] | 725 | { |
| 726 | int cur_idx = ctx->idx; |
| 727 | char *sol = ctx->line; |
| 728 | struct hdr_idx_elem *hdr; |
| 729 | int delta, skip_comma; |
| 730 | |
| 731 | if (!cur_idx) |
| 732 | return 0; |
| 733 | |
| 734 | hdr = &idx->v[cur_idx]; |
Willy Tarreau | 588bd4f | 2011-09-01 22:22:28 +0200 | [diff] [blame] | 735 | if (sol[ctx->del] == ':' && ctx->val + ctx->vlen + ctx->tws == hdr->len) { |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 736 | /* 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] | 737 | 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] | 738 | http_msg_move_end(msg, delta); |
| 739 | idx->used--; |
| 740 | hdr->len = 0; /* unused entry */ |
| 741 | idx->v[ctx->prev].next = idx->v[ctx->idx].next; |
Willy Tarreau | 5c4784f | 2011-02-12 13:07:35 +0100 | [diff] [blame] | 742 | if (idx->tail == ctx->idx) |
| 743 | idx->tail = ctx->prev; |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 744 | ctx->idx = ctx->prev; /* walk back to the end of previous header */ |
| 745 | ctx->line -= idx->v[ctx->idx].len + idx->v[cur_idx].cr + 1; |
| 746 | 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] | 747 | ctx->tws = ctx->vlen = 0; |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 748 | return ctx->idx; |
| 749 | } |
| 750 | |
| 751 | /* 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] | 752 | * ctx->del+1 and ctx->val+ctx->vlen+ctx->tws+1 included. If it is the |
| 753 | * last entry of the list, we remove the last separator. |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 754 | */ |
| 755 | |
Willy Tarreau | 588bd4f | 2011-09-01 22:22:28 +0200 | [diff] [blame] | 756 | skip_comma = (ctx->val + ctx->vlen + ctx->tws == hdr->len) ? 0 : 1; |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 757 | delta = buffer_replace2(msg->chn->buf, sol + ctx->del + skip_comma, |
Willy Tarreau | 588bd4f | 2011-09-01 22:22:28 +0200 | [diff] [blame] | 758 | sol + ctx->val + ctx->vlen + ctx->tws + skip_comma, |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 759 | NULL, 0); |
| 760 | hdr->len += delta; |
| 761 | http_msg_move_end(msg, delta); |
| 762 | ctx->val = ctx->del; |
Willy Tarreau | 588bd4f | 2011-09-01 22:22:28 +0200 | [diff] [blame] | 763 | ctx->tws = ctx->vlen = 0; |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 764 | return ctx->idx; |
| 765 | } |
| 766 | |
Willy Tarreau | 2d3d94c | 2008-11-30 20:20:08 +0100 | [diff] [blame] | 767 | /* This function handles a server error at the stream interface level. The |
| 768 | * stream interface is assumed to be already in a closed state. An optional |
| 769 | * message is copied into the input buffer, and an HTTP status code stored. |
| 770 | * 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] | 771 | * in this buffer will be lost. |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 772 | */ |
Willy Tarreau | 2d3d94c | 2008-11-30 20:20:08 +0100 | [diff] [blame] | 773 | static void http_server_error(struct session *t, struct stream_interface *si, |
| 774 | int err, int finst, int status, const struct chunk *msg) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 775 | { |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 776 | channel_auto_read(si->ob); |
| 777 | channel_abort(si->ob); |
| 778 | channel_auto_close(si->ob); |
| 779 | channel_erase(si->ob); |
| 780 | channel_auto_close(si->ib); |
| 781 | channel_auto_read(si->ib); |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 782 | if (status > 0 && msg) { |
Willy Tarreau | 3bac9ff | 2007-03-18 17:31:28 +0100 | [diff] [blame] | 783 | t->txn.status = status; |
Willy Tarreau | 9dab5fc | 2012-05-07 11:56:55 +0200 | [diff] [blame] | 784 | bo_inject(si->ib, msg->str, msg->len); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 785 | } |
| 786 | if (!(t->flags & SN_ERR_MASK)) |
| 787 | t->flags |= err; |
| 788 | if (!(t->flags & SN_FINST_MASK)) |
| 789 | t->flags |= finst; |
| 790 | } |
| 791 | |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 792 | /* This function returns the appropriate error location for the given session |
| 793 | * and message. |
| 794 | */ |
| 795 | |
Willy Tarreau | 783f258 | 2012-09-04 12:19:04 +0200 | [diff] [blame] | 796 | struct chunk *http_error_message(struct session *s, int msgnum) |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 797 | { |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 798 | if (s->be->errmsg[msgnum].str) |
| 799 | return &s->be->errmsg[msgnum]; |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 800 | else if (s->fe->errmsg[msgnum].str) |
| 801 | return &s->fe->errmsg[msgnum]; |
| 802 | else |
| 803 | return &http_err_chunks[msgnum]; |
| 804 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 805 | |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 806 | /* |
| 807 | * returns HTTP_METH_NONE if there is nothing valid to read (empty or non-text |
| 808 | * string), HTTP_METH_OTHER for unknown methods, or the identified method. |
| 809 | */ |
Thierry FOURNIER | d437314 | 2013-12-17 01:10:10 +0100 | [diff] [blame] | 810 | 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] | 811 | { |
| 812 | unsigned char m; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 813 | const struct http_method_desc *h; |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 814 | |
| 815 | m = ((unsigned)*str - 'A'); |
| 816 | |
| 817 | if (m < 26) { |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 818 | for (h = http_methods[m]; h->len > 0; h++) { |
| 819 | if (unlikely(h->len != len)) |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 820 | continue; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 821 | if (likely(memcmp(str, h->text, h->len) == 0)) |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 822 | return h->meth; |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 823 | }; |
| 824 | return HTTP_METH_OTHER; |
| 825 | } |
| 826 | return HTTP_METH_NONE; |
| 827 | |
| 828 | } |
| 829 | |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 830 | /* Parse the URI from the given transaction (which is assumed to be in request |
| 831 | * phase) and look for the "/" beginning the PATH. If not found, return NULL. |
| 832 | * It is returned otherwise. |
| 833 | */ |
| 834 | static char * |
| 835 | http_get_path(struct http_txn *txn) |
| 836 | { |
| 837 | char *ptr, *end; |
| 838 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 839 | ptr = txn->req.chn->buf->p + txn->req.sl.rq.u; |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 840 | end = ptr + txn->req.sl.rq.u_l; |
| 841 | |
| 842 | if (ptr >= end) |
| 843 | return NULL; |
| 844 | |
| 845 | /* RFC2616, par. 5.1.2 : |
| 846 | * Request-URI = "*" | absuri | abspath | authority |
| 847 | */ |
| 848 | |
| 849 | if (*ptr == '*') |
| 850 | return NULL; |
| 851 | |
| 852 | if (isalpha((unsigned char)*ptr)) { |
| 853 | /* this is a scheme as described by RFC3986, par. 3.1 */ |
| 854 | ptr++; |
| 855 | while (ptr < end && |
| 856 | (isalnum((unsigned char)*ptr) || *ptr == '+' || *ptr == '-' || *ptr == '.')) |
| 857 | ptr++; |
| 858 | /* skip '://' */ |
| 859 | if (ptr == end || *ptr++ != ':') |
| 860 | return NULL; |
| 861 | if (ptr == end || *ptr++ != '/') |
| 862 | return NULL; |
| 863 | if (ptr == end || *ptr++ != '/') |
| 864 | return NULL; |
| 865 | } |
| 866 | /* skip [user[:passwd]@]host[:[port]] */ |
| 867 | |
| 868 | while (ptr < end && *ptr != '/') |
| 869 | ptr++; |
| 870 | |
| 871 | if (ptr == end) |
| 872 | return NULL; |
| 873 | |
| 874 | /* OK, we got the '/' ! */ |
| 875 | return ptr; |
| 876 | } |
| 877 | |
William Lallemand | 65ad6e1 | 2014-01-31 15:08:02 +0100 | [diff] [blame] | 878 | /* Parse the URI from the given string and look for the "/" beginning the PATH. |
| 879 | * If not found, return NULL. It is returned otherwise. |
| 880 | */ |
| 881 | static char * |
| 882 | http_get_path_from_string(char *str) |
| 883 | { |
| 884 | char *ptr = str; |
| 885 | |
| 886 | /* RFC2616, par. 5.1.2 : |
| 887 | * Request-URI = "*" | absuri | abspath | authority |
| 888 | */ |
| 889 | |
| 890 | if (*ptr == '*') |
| 891 | return NULL; |
| 892 | |
| 893 | if (isalpha((unsigned char)*ptr)) { |
| 894 | /* this is a scheme as described by RFC3986, par. 3.1 */ |
| 895 | ptr++; |
| 896 | while (isalnum((unsigned char)*ptr) || *ptr == '+' || *ptr == '-' || *ptr == '.') |
| 897 | ptr++; |
| 898 | /* skip '://' */ |
| 899 | if (*ptr == '\0' || *ptr++ != ':') |
| 900 | return NULL; |
| 901 | if (*ptr == '\0' || *ptr++ != '/') |
| 902 | return NULL; |
| 903 | if (*ptr == '\0' || *ptr++ != '/') |
| 904 | return NULL; |
| 905 | } |
| 906 | /* skip [user[:passwd]@]host[:[port]] */ |
| 907 | |
| 908 | while (*ptr != '\0' && *ptr != ' ' && *ptr != '/') |
| 909 | ptr++; |
| 910 | |
| 911 | if (*ptr == '\0' || *ptr == ' ') |
| 912 | return NULL; |
| 913 | |
| 914 | /* OK, we got the '/' ! */ |
| 915 | return ptr; |
| 916 | } |
| 917 | |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 918 | /* Returns a 302 for a redirectable request that reaches a server working in |
| 919 | * in redirect mode. This may only be called just after the stream interface |
| 920 | * has moved to SI_ST_ASS. Unprocessable requests are left unchanged and will |
| 921 | * follow normal proxy processing. NOTE: this function is designed to support |
| 922 | * being called once data are scheduled for forwarding. |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 923 | */ |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 924 | void http_perform_server_redirect(struct session *s, struct stream_interface *si) |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 925 | { |
| 926 | struct http_txn *txn; |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 927 | struct server *srv; |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 928 | char *path; |
Willy Tarreau | cde18fc | 2012-05-30 07:59:54 +0200 | [diff] [blame] | 929 | int len, rewind; |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 930 | |
| 931 | /* 1: create the response header */ |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 932 | trash.len = strlen(HTTP_302); |
| 933 | memcpy(trash.str, HTTP_302, trash.len); |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 934 | |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 935 | srv = objt_server(s->target); |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 936 | |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 937 | /* 2: add the server's prefix */ |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 938 | if (trash.len + srv->rdr_len > trash.size) |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 939 | return; |
| 940 | |
Willy Tarreau | dcb75c4 | 2010-01-10 00:24:22 +0100 | [diff] [blame] | 941 | /* special prefix "/" means don't change URL */ |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 942 | if (srv->rdr_len != 1 || *srv->rdr_pfx != '/') { |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 943 | memcpy(trash.str + trash.len, srv->rdr_pfx, srv->rdr_len); |
| 944 | trash.len += srv->rdr_len; |
Willy Tarreau | dcb75c4 | 2010-01-10 00:24:22 +0100 | [diff] [blame] | 945 | } |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 946 | |
Willy Tarreau | cde18fc | 2012-05-30 07:59:54 +0200 | [diff] [blame] | 947 | /* 3: add the request URI. Since it was already forwarded, we need |
| 948 | * to temporarily rewind the buffer. |
| 949 | */ |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 950 | txn = &s->txn; |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 951 | b_rew(s->req->buf, rewind = s->req->buf->o); |
Willy Tarreau | cde18fc | 2012-05-30 07:59:54 +0200 | [diff] [blame] | 952 | |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 953 | path = http_get_path(txn); |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 954 | 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] | 955 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 956 | b_adv(s->req->buf, rewind); |
Willy Tarreau | cde18fc | 2012-05-30 07:59:54 +0200 | [diff] [blame] | 957 | |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 958 | if (!path) |
| 959 | return; |
| 960 | |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 961 | if (trash.len + len > trash.size - 4) /* 4 for CRLF-CRLF */ |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 962 | return; |
| 963 | |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 964 | memcpy(trash.str + trash.len, path, len); |
| 965 | trash.len += len; |
Willy Tarreau | 88d349d | 2010-01-25 12:15:43 +0100 | [diff] [blame] | 966 | |
| 967 | if (unlikely(txn->flags & TX_USE_PX_CONN)) { |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 968 | memcpy(trash.str + trash.len, "\r\nProxy-Connection: close\r\n\r\n", 29); |
| 969 | trash.len += 29; |
Willy Tarreau | 88d349d | 2010-01-25 12:15:43 +0100 | [diff] [blame] | 970 | } else { |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 971 | memcpy(trash.str + trash.len, "\r\nConnection: close\r\n\r\n", 23); |
| 972 | trash.len += 23; |
Willy Tarreau | 88d349d | 2010-01-25 12:15:43 +0100 | [diff] [blame] | 973 | } |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 974 | |
| 975 | /* prepare to return without error. */ |
Willy Tarreau | 73b013b | 2012-05-21 16:31:45 +0200 | [diff] [blame] | 976 | si_shutr(si); |
| 977 | si_shutw(si); |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 978 | si->err_type = SI_ET_NONE; |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 979 | si->state = SI_ST_CLO; |
| 980 | |
| 981 | /* send the message */ |
Willy Tarreau | 570f221 | 2013-06-10 16:42:09 +0200 | [diff] [blame] | 982 | 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] | 983 | |
| 984 | /* 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] | 985 | srv_inc_sess_ctr(srv); |
Bhaskar Maddala | a20cb85 | 2014-02-03 16:26:46 -0500 | [diff] [blame] | 986 | srv_set_sess_last(srv); |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 987 | } |
| 988 | |
Willy Tarreau | 0cac36f | 2008-11-30 20:44:17 +0100 | [diff] [blame] | 989 | /* Return the error message corresponding to si->err_type. It is assumed |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 990 | * that the server side is closed. Note that err_type is actually a |
| 991 | * bitmask, where almost only aborts may be cumulated with other |
| 992 | * values. We consider that aborted operations are more important |
| 993 | * than timeouts or errors due to the fact that nobody else in the |
| 994 | * logs might explain incomplete retries. All others should avoid |
| 995 | * being cumulated. It should normally not be possible to have multiple |
| 996 | * 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] | 997 | * Note that connection errors appearing on the second request of a keep-alive |
| 998 | * connection are not reported since this allows the client to retry. |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 999 | */ |
Willy Tarreau | 0cac36f | 2008-11-30 20:44:17 +0100 | [diff] [blame] | 1000 | void http_return_srv_error(struct session *s, struct stream_interface *si) |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 1001 | { |
Willy Tarreau | 0cac36f | 2008-11-30 20:44:17 +0100 | [diff] [blame] | 1002 | int err_type = si->err_type; |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 1003 | |
| 1004 | if (err_type & SI_ET_QUEUE_ABRT) |
Willy Tarreau | 2d3d94c | 2008-11-30 20:20:08 +0100 | [diff] [blame] | 1005 | http_server_error(s, si, SN_ERR_CLICL, SN_FINST_Q, |
Willy Tarreau | 783f258 | 2012-09-04 12:19:04 +0200 | [diff] [blame] | 1006 | 503, http_error_message(s, HTTP_ERR_503)); |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 1007 | else if (err_type & SI_ET_CONN_ABRT) |
Willy Tarreau | 2d3d94c | 2008-11-30 20:20:08 +0100 | [diff] [blame] | 1008 | http_server_error(s, si, SN_ERR_CLICL, SN_FINST_C, |
Willy Tarreau | 6b726ad | 2013-12-15 19:31:37 +0100 | [diff] [blame] | 1009 | 503, (s->txn.flags & TX_NOT_FIRST) ? NULL : |
| 1010 | http_error_message(s, HTTP_ERR_503)); |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 1011 | else if (err_type & SI_ET_QUEUE_TO) |
Willy Tarreau | 2d3d94c | 2008-11-30 20:20:08 +0100 | [diff] [blame] | 1012 | http_server_error(s, si, SN_ERR_SRVTO, SN_FINST_Q, |
Willy Tarreau | 783f258 | 2012-09-04 12:19:04 +0200 | [diff] [blame] | 1013 | 503, http_error_message(s, HTTP_ERR_503)); |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 1014 | else if (err_type & SI_ET_QUEUE_ERR) |
Willy Tarreau | 2d3d94c | 2008-11-30 20:20:08 +0100 | [diff] [blame] | 1015 | http_server_error(s, si, SN_ERR_SRVCL, SN_FINST_Q, |
Willy Tarreau | 783f258 | 2012-09-04 12:19:04 +0200 | [diff] [blame] | 1016 | 503, http_error_message(s, HTTP_ERR_503)); |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 1017 | else if (err_type & SI_ET_CONN_TO) |
Willy Tarreau | 2d3d94c | 2008-11-30 20:20:08 +0100 | [diff] [blame] | 1018 | http_server_error(s, si, SN_ERR_SRVTO, SN_FINST_C, |
Willy Tarreau | 6b726ad | 2013-12-15 19:31:37 +0100 | [diff] [blame] | 1019 | 503, (s->txn.flags & TX_NOT_FIRST) ? NULL : |
| 1020 | http_error_message(s, HTTP_ERR_503)); |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 1021 | else if (err_type & SI_ET_CONN_ERR) |
Willy Tarreau | 2d3d94c | 2008-11-30 20:20:08 +0100 | [diff] [blame] | 1022 | http_server_error(s, si, SN_ERR_SRVCL, SN_FINST_C, |
Willy Tarreau | 3634624 | 2014-02-24 18:26:30 +0100 | [diff] [blame] | 1023 | 503, (s->flags & SN_SRV_REUSED) ? NULL : |
Willy Tarreau | 6b726ad | 2013-12-15 19:31:37 +0100 | [diff] [blame] | 1024 | http_error_message(s, HTTP_ERR_503)); |
Willy Tarreau | 2d400bb | 2012-05-14 12:11:47 +0200 | [diff] [blame] | 1025 | else if (err_type & SI_ET_CONN_RES) |
| 1026 | http_server_error(s, si, SN_ERR_RESOURCE, SN_FINST_C, |
Willy Tarreau | 6b726ad | 2013-12-15 19:31:37 +0100 | [diff] [blame] | 1027 | 503, (s->txn.flags & TX_NOT_FIRST) ? NULL : |
| 1028 | http_error_message(s, HTTP_ERR_503)); |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 1029 | else /* SI_ET_CONN_OTHER and others */ |
Willy Tarreau | 2d3d94c | 2008-11-30 20:20:08 +0100 | [diff] [blame] | 1030 | http_server_error(s, si, SN_ERR_INTERNAL, SN_FINST_C, |
Willy Tarreau | 783f258 | 2012-09-04 12:19:04 +0200 | [diff] [blame] | 1031 | 500, http_error_message(s, HTTP_ERR_500)); |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 1032 | } |
| 1033 | |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 1034 | extern const char sess_term_cond[8]; |
| 1035 | extern const char sess_fin_state[8]; |
| 1036 | extern const char *monthname[12]; |
Willy Tarreau | 332f8bf | 2007-05-13 21:36:56 +0200 | [diff] [blame] | 1037 | struct pool_head *pool2_requri; |
Willy Tarreau | 193b8c6 | 2012-11-22 00:17:38 +0100 | [diff] [blame] | 1038 | struct pool_head *pool2_capture = NULL; |
William Lallemand | a73203e | 2012-03-12 12:48:57 +0100 | [diff] [blame] | 1039 | struct pool_head *pool2_uniqueid; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1040 | |
Willy Tarreau | 117f59e | 2007-03-04 18:17:17 +0100 | [diff] [blame] | 1041 | /* |
| 1042 | * Capture headers from message starting at <som> according to header list |
| 1043 | * <cap_hdr>, and fill the <idx> structure appropriately. |
| 1044 | */ |
| 1045 | void capture_headers(char *som, struct hdr_idx *idx, |
| 1046 | char **cap, struct cap_hdr *cap_hdr) |
| 1047 | { |
| 1048 | char *eol, *sol, *col, *sov; |
| 1049 | int cur_idx; |
| 1050 | struct cap_hdr *h; |
| 1051 | int len; |
| 1052 | |
| 1053 | sol = som + hdr_idx_first_pos(idx); |
| 1054 | cur_idx = hdr_idx_first_idx(idx); |
| 1055 | |
| 1056 | while (cur_idx) { |
| 1057 | eol = sol + idx->v[cur_idx].len; |
| 1058 | |
| 1059 | col = sol; |
| 1060 | while (col < eol && *col != ':') |
| 1061 | col++; |
| 1062 | |
| 1063 | sov = col + 1; |
| 1064 | while (sov < eol && http_is_lws[(unsigned char)*sov]) |
| 1065 | sov++; |
| 1066 | |
| 1067 | for (h = cap_hdr; h; h = h->next) { |
| 1068 | if ((h->namelen == col - sol) && |
| 1069 | (strncasecmp(sol, h->name, h->namelen) == 0)) { |
| 1070 | if (cap[h->index] == NULL) |
| 1071 | cap[h->index] = |
Willy Tarreau | cf7f320 | 2007-05-13 22:46:04 +0200 | [diff] [blame] | 1072 | pool_alloc2(h->pool); |
Willy Tarreau | 117f59e | 2007-03-04 18:17:17 +0100 | [diff] [blame] | 1073 | |
| 1074 | if (cap[h->index] == NULL) { |
| 1075 | Alert("HTTP capture : out of memory.\n"); |
| 1076 | continue; |
| 1077 | } |
| 1078 | |
| 1079 | len = eol - sov; |
| 1080 | if (len > h->len) |
| 1081 | len = h->len; |
| 1082 | |
| 1083 | memcpy(cap[h->index], sov, len); |
| 1084 | cap[h->index][len]=0; |
| 1085 | } |
| 1086 | } |
| 1087 | sol = eol + idx->v[cur_idx].cr + 1; |
| 1088 | cur_idx = idx->v[cur_idx].next; |
| 1089 | } |
| 1090 | } |
| 1091 | |
| 1092 | |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 1093 | /* either we find an LF at <ptr> or we jump to <bad>. |
| 1094 | */ |
| 1095 | #define EXPECT_LF_HERE(ptr, bad) do { if (unlikely(*(ptr) != '\n')) goto bad; } while (0) |
| 1096 | |
| 1097 | /* plays with variables <ptr>, <end> and <state>. Jumps to <good> if OK, |
| 1098 | * otherwise to <http_msg_ood> with <state> set to <st>. |
| 1099 | */ |
| 1100 | #define EAT_AND_JUMP_OR_RETURN(good, st) do { \ |
| 1101 | ptr++; \ |
| 1102 | if (likely(ptr < end)) \ |
| 1103 | goto good; \ |
| 1104 | else { \ |
| 1105 | state = (st); \ |
| 1106 | goto http_msg_ood; \ |
| 1107 | } \ |
| 1108 | } while (0) |
| 1109 | |
| 1110 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1111 | /* |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 1112 | * This function parses a status line between <ptr> and <end>, starting with |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1113 | * parser state <state>. Only states HTTP_MSG_RPVER, HTTP_MSG_RPVER_SP, |
| 1114 | * HTTP_MSG_RPCODE, HTTP_MSG_RPCODE_SP and HTTP_MSG_RPREASON are handled. Others |
| 1115 | * will give undefined results. |
| 1116 | * Note that it is upon the caller's responsibility to ensure that ptr < end, |
| 1117 | * and that msg->sol points to the beginning of the response. |
| 1118 | * If a complete line is found (which implies that at least one CR or LF is |
| 1119 | * found before <end>, the updated <ptr> is returned, otherwise NULL is |
| 1120 | * returned indicating an incomplete line (which does not mean that parts have |
| 1121 | * not been updated). In the incomplete case, if <ret_ptr> or <ret_state> are |
| 1122 | * non-NULL, they are fed with the new <ptr> and <state> values to be passed |
| 1123 | * upon next call. |
| 1124 | * |
Willy Tarreau | 9cdde23 | 2007-05-02 20:58:19 +0200 | [diff] [blame] | 1125 | * This function was intentionally designed to be called from |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1126 | * http_msg_analyzer() with the lowest overhead. It should integrate perfectly |
| 1127 | * 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] | 1128 | * labels and variable names. Note that msg->sol is left unchanged. |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1129 | */ |
Willy Tarreau | 69d8c5d | 2012-05-08 09:44:41 +0200 | [diff] [blame] | 1130 | const char *http_parse_stsline(struct http_msg *msg, |
Willy Tarreau | 3770f23 | 2013-12-07 00:01:53 +0100 | [diff] [blame] | 1131 | enum ht_state state, const char *ptr, const char *end, |
| 1132 | unsigned int *ret_ptr, enum ht_state *ret_state) |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1133 | { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 1134 | const char *msg_start = msg->chn->buf->p; |
Willy Tarreau | 62f791e | 2012-03-09 11:32:30 +0100 | [diff] [blame] | 1135 | |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1136 | switch (state) { |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1137 | case HTTP_MSG_RPVER: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1138 | http_msg_rpver: |
Willy Tarreau | 4b89ad4 | 2007-03-04 18:13:58 +0100 | [diff] [blame] | 1139 | if (likely(HTTP_IS_VER_TOKEN(*ptr))) |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1140 | EAT_AND_JUMP_OR_RETURN(http_msg_rpver, HTTP_MSG_RPVER); |
| 1141 | |
| 1142 | if (likely(HTTP_IS_SPHT(*ptr))) { |
Willy Tarreau | ea1175a | 2012-03-05 15:52:30 +0100 | [diff] [blame] | 1143 | msg->sl.st.v_l = ptr - msg_start; |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1144 | EAT_AND_JUMP_OR_RETURN(http_msg_rpver_sp, HTTP_MSG_RPVER_SP); |
| 1145 | } |
Willy Tarreau | 7552c03 | 2009-03-01 11:10:40 +0100 | [diff] [blame] | 1146 | state = HTTP_MSG_ERROR; |
| 1147 | break; |
| 1148 | |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1149 | case HTTP_MSG_RPVER_SP: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1150 | http_msg_rpver_sp: |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1151 | if (likely(!HTTP_IS_LWS(*ptr))) { |
Willy Tarreau | ea1175a | 2012-03-05 15:52:30 +0100 | [diff] [blame] | 1152 | msg->sl.st.c = ptr - msg_start; |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1153 | goto http_msg_rpcode; |
| 1154 | } |
| 1155 | if (likely(HTTP_IS_SPHT(*ptr))) |
| 1156 | EAT_AND_JUMP_OR_RETURN(http_msg_rpver_sp, HTTP_MSG_RPVER_SP); |
| 1157 | /* so it's a CR/LF, this is invalid */ |
Willy Tarreau | 7552c03 | 2009-03-01 11:10:40 +0100 | [diff] [blame] | 1158 | state = HTTP_MSG_ERROR; |
| 1159 | break; |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1160 | |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1161 | case HTTP_MSG_RPCODE: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1162 | http_msg_rpcode: |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1163 | if (likely(!HTTP_IS_LWS(*ptr))) |
| 1164 | EAT_AND_JUMP_OR_RETURN(http_msg_rpcode, HTTP_MSG_RPCODE); |
| 1165 | |
| 1166 | if (likely(HTTP_IS_SPHT(*ptr))) { |
Willy Tarreau | ea1175a | 2012-03-05 15:52:30 +0100 | [diff] [blame] | 1167 | msg->sl.st.c_l = ptr - msg_start - msg->sl.st.c; |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1168 | EAT_AND_JUMP_OR_RETURN(http_msg_rpcode_sp, HTTP_MSG_RPCODE_SP); |
| 1169 | } |
| 1170 | |
| 1171 | /* so it's a CR/LF, so there is no reason phrase */ |
Willy Tarreau | ea1175a | 2012-03-05 15:52:30 +0100 | [diff] [blame] | 1172 | msg->sl.st.c_l = ptr - msg_start - msg->sl.st.c; |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1173 | http_msg_rsp_reason: |
| 1174 | /* FIXME: should we support HTTP responses without any reason phrase ? */ |
Willy Tarreau | ea1175a | 2012-03-05 15:52:30 +0100 | [diff] [blame] | 1175 | msg->sl.st.r = ptr - msg_start; |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1176 | msg->sl.st.r_l = 0; |
| 1177 | goto http_msg_rpline_eol; |
| 1178 | |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1179 | case HTTP_MSG_RPCODE_SP: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1180 | http_msg_rpcode_sp: |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1181 | if (likely(!HTTP_IS_LWS(*ptr))) { |
Willy Tarreau | ea1175a | 2012-03-05 15:52:30 +0100 | [diff] [blame] | 1182 | msg->sl.st.r = ptr - msg_start; |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1183 | goto http_msg_rpreason; |
| 1184 | } |
| 1185 | if (likely(HTTP_IS_SPHT(*ptr))) |
| 1186 | EAT_AND_JUMP_OR_RETURN(http_msg_rpcode_sp, HTTP_MSG_RPCODE_SP); |
| 1187 | /* so it's a CR/LF, so there is no reason phrase */ |
| 1188 | goto http_msg_rsp_reason; |
| 1189 | |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1190 | case HTTP_MSG_RPREASON: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1191 | http_msg_rpreason: |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1192 | if (likely(!HTTP_IS_CRLF(*ptr))) |
| 1193 | EAT_AND_JUMP_OR_RETURN(http_msg_rpreason, HTTP_MSG_RPREASON); |
Willy Tarreau | ea1175a | 2012-03-05 15:52:30 +0100 | [diff] [blame] | 1194 | msg->sl.st.r_l = ptr - msg_start - msg->sl.st.r; |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1195 | http_msg_rpline_eol: |
| 1196 | /* We have seen the end of line. Note that we do not |
| 1197 | * necessarily have the \n yet, but at least we know that we |
| 1198 | * have EITHER \r OR \n, otherwise the response would not be |
| 1199 | * complete. We can then record the response length and return |
| 1200 | * to the caller which will be able to register it. |
| 1201 | */ |
Willy Tarreau | 3a215be | 2012-03-09 21:39:51 +0100 | [diff] [blame] | 1202 | msg->sl.st.l = ptr - msg_start - msg->sol; |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1203 | return ptr; |
| 1204 | |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1205 | default: |
Willy Tarreau | 3770f23 | 2013-12-07 00:01:53 +0100 | [diff] [blame] | 1206 | #ifdef DEBUG_FULL |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1207 | fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state); |
| 1208 | exit(1); |
| 1209 | #endif |
Willy Tarreau | 3770f23 | 2013-12-07 00:01:53 +0100 | [diff] [blame] | 1210 | ; |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1211 | } |
| 1212 | |
| 1213 | http_msg_ood: |
Willy Tarreau | 7552c03 | 2009-03-01 11:10:40 +0100 | [diff] [blame] | 1214 | /* out of valid data */ |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1215 | if (ret_state) |
| 1216 | *ret_state = state; |
| 1217 | if (ret_ptr) |
Willy Tarreau | a458b67 | 2012-03-05 11:17:50 +0100 | [diff] [blame] | 1218 | *ret_ptr = ptr - msg_start; |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1219 | return NULL; |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1220 | } |
| 1221 | |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1222 | /* |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1223 | * This function parses a request line between <ptr> and <end>, starting with |
| 1224 | * parser state <state>. Only states HTTP_MSG_RQMETH, HTTP_MSG_RQMETH_SP, |
| 1225 | * HTTP_MSG_RQURI, HTTP_MSG_RQURI_SP and HTTP_MSG_RQVER are handled. Others |
| 1226 | * will give undefined results. |
| 1227 | * Note that it is upon the caller's responsibility to ensure that ptr < end, |
| 1228 | * and that msg->sol points to the beginning of the request. |
| 1229 | * If a complete line is found (which implies that at least one CR or LF is |
| 1230 | * found before <end>, the updated <ptr> is returned, otherwise NULL is |
| 1231 | * returned indicating an incomplete line (which does not mean that parts have |
| 1232 | * not been updated). In the incomplete case, if <ret_ptr> or <ret_state> are |
| 1233 | * non-NULL, they are fed with the new <ptr> and <state> values to be passed |
| 1234 | * upon next call. |
| 1235 | * |
Willy Tarreau | 9cdde23 | 2007-05-02 20:58:19 +0200 | [diff] [blame] | 1236 | * This function was intentionally designed to be called from |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1237 | * http_msg_analyzer() with the lowest overhead. It should integrate perfectly |
| 1238 | * 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] | 1239 | * labels and variable names. Note that msg->sol is left unchanged. |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1240 | */ |
Willy Tarreau | 69d8c5d | 2012-05-08 09:44:41 +0200 | [diff] [blame] | 1241 | const char *http_parse_reqline(struct http_msg *msg, |
Willy Tarreau | 3770f23 | 2013-12-07 00:01:53 +0100 | [diff] [blame] | 1242 | enum ht_state state, const char *ptr, const char *end, |
| 1243 | unsigned int *ret_ptr, enum ht_state *ret_state) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1244 | { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 1245 | const char *msg_start = msg->chn->buf->p; |
Willy Tarreau | 62f791e | 2012-03-09 11:32:30 +0100 | [diff] [blame] | 1246 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1247 | switch (state) { |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1248 | case HTTP_MSG_RQMETH: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1249 | http_msg_rqmeth: |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1250 | if (likely(HTTP_IS_TOKEN(*ptr))) |
| 1251 | EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth, HTTP_MSG_RQMETH); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1252 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1253 | if (likely(HTTP_IS_SPHT(*ptr))) { |
Willy Tarreau | ea1175a | 2012-03-05 15:52:30 +0100 | [diff] [blame] | 1254 | msg->sl.rq.m_l = ptr - msg_start; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1255 | EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth_sp, HTTP_MSG_RQMETH_SP); |
| 1256 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1257 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1258 | if (likely(HTTP_IS_CRLF(*ptr))) { |
| 1259 | /* HTTP 0.9 request */ |
Willy Tarreau | ea1175a | 2012-03-05 15:52:30 +0100 | [diff] [blame] | 1260 | msg->sl.rq.m_l = ptr - msg_start; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1261 | http_msg_req09_uri: |
Willy Tarreau | ea1175a | 2012-03-05 15:52:30 +0100 | [diff] [blame] | 1262 | msg->sl.rq.u = ptr - msg_start; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1263 | http_msg_req09_uri_e: |
Willy Tarreau | ea1175a | 2012-03-05 15:52:30 +0100 | [diff] [blame] | 1264 | msg->sl.rq.u_l = ptr - msg_start - msg->sl.rq.u; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1265 | http_msg_req09_ver: |
Willy Tarreau | ea1175a | 2012-03-05 15:52:30 +0100 | [diff] [blame] | 1266 | msg->sl.rq.v = ptr - msg_start; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1267 | msg->sl.rq.v_l = 0; |
| 1268 | goto http_msg_rqline_eol; |
| 1269 | } |
Willy Tarreau | 7552c03 | 2009-03-01 11:10:40 +0100 | [diff] [blame] | 1270 | state = HTTP_MSG_ERROR; |
| 1271 | break; |
| 1272 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1273 | case HTTP_MSG_RQMETH_SP: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1274 | http_msg_rqmeth_sp: |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1275 | if (likely(!HTTP_IS_LWS(*ptr))) { |
Willy Tarreau | ea1175a | 2012-03-05 15:52:30 +0100 | [diff] [blame] | 1276 | msg->sl.rq.u = ptr - msg_start; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1277 | goto http_msg_rquri; |
| 1278 | } |
| 1279 | if (likely(HTTP_IS_SPHT(*ptr))) |
| 1280 | EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth_sp, HTTP_MSG_RQMETH_SP); |
| 1281 | /* so it's a CR/LF, meaning an HTTP 0.9 request */ |
| 1282 | goto http_msg_req09_uri; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1283 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1284 | case HTTP_MSG_RQURI: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1285 | http_msg_rquri: |
Willy Tarreau | 2e9506d | 2012-01-07 23:22:31 +0100 | [diff] [blame] | 1286 | if (likely((unsigned char)(*ptr - 33) <= 93)) /* 33 to 126 included */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1287 | EAT_AND_JUMP_OR_RETURN(http_msg_rquri, HTTP_MSG_RQURI); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1288 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1289 | if (likely(HTTP_IS_SPHT(*ptr))) { |
Willy Tarreau | ea1175a | 2012-03-05 15:52:30 +0100 | [diff] [blame] | 1290 | msg->sl.rq.u_l = ptr - msg_start - msg->sl.rq.u; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1291 | EAT_AND_JUMP_OR_RETURN(http_msg_rquri_sp, HTTP_MSG_RQURI_SP); |
| 1292 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1293 | |
Willy Tarreau | 2e9506d | 2012-01-07 23:22:31 +0100 | [diff] [blame] | 1294 | if (likely((unsigned char)*ptr >= 128)) { |
Willy Tarreau | 422246e | 2012-01-07 23:54:13 +0100 | [diff] [blame] | 1295 | /* non-ASCII chars are forbidden unless option |
| 1296 | * accept-invalid-http-request is enabled in the frontend. |
| 1297 | * In any case, we capture the faulty char. |
Willy Tarreau | 2e9506d | 2012-01-07 23:22:31 +0100 | [diff] [blame] | 1298 | */ |
Willy Tarreau | 422246e | 2012-01-07 23:54:13 +0100 | [diff] [blame] | 1299 | if (msg->err_pos < -1) |
| 1300 | goto invalid_char; |
| 1301 | if (msg->err_pos == -1) |
Willy Tarreau | 69d8c5d | 2012-05-08 09:44:41 +0200 | [diff] [blame] | 1302 | msg->err_pos = ptr - msg_start; |
Willy Tarreau | 2e9506d | 2012-01-07 23:22:31 +0100 | [diff] [blame] | 1303 | EAT_AND_JUMP_OR_RETURN(http_msg_rquri, HTTP_MSG_RQURI); |
| 1304 | } |
| 1305 | |
| 1306 | if (likely(HTTP_IS_CRLF(*ptr))) { |
| 1307 | /* so it's a CR/LF, meaning an HTTP 0.9 request */ |
| 1308 | goto http_msg_req09_uri_e; |
| 1309 | } |
| 1310 | |
| 1311 | /* OK forbidden chars, 0..31 or 127 */ |
Willy Tarreau | 422246e | 2012-01-07 23:54:13 +0100 | [diff] [blame] | 1312 | invalid_char: |
Willy Tarreau | 69d8c5d | 2012-05-08 09:44:41 +0200 | [diff] [blame] | 1313 | msg->err_pos = ptr - msg_start; |
Willy Tarreau | 2e9506d | 2012-01-07 23:22:31 +0100 | [diff] [blame] | 1314 | state = HTTP_MSG_ERROR; |
| 1315 | break; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1316 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1317 | case HTTP_MSG_RQURI_SP: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1318 | http_msg_rquri_sp: |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1319 | if (likely(!HTTP_IS_LWS(*ptr))) { |
Willy Tarreau | ea1175a | 2012-03-05 15:52:30 +0100 | [diff] [blame] | 1320 | msg->sl.rq.v = ptr - msg_start; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1321 | goto http_msg_rqver; |
| 1322 | } |
| 1323 | if (likely(HTTP_IS_SPHT(*ptr))) |
| 1324 | EAT_AND_JUMP_OR_RETURN(http_msg_rquri_sp, HTTP_MSG_RQURI_SP); |
| 1325 | /* so it's a CR/LF, meaning an HTTP 0.9 request */ |
| 1326 | goto http_msg_req09_ver; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1327 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1328 | case HTTP_MSG_RQVER: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1329 | http_msg_rqver: |
Willy Tarreau | 4b89ad4 | 2007-03-04 18:13:58 +0100 | [diff] [blame] | 1330 | if (likely(HTTP_IS_VER_TOKEN(*ptr))) |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1331 | EAT_AND_JUMP_OR_RETURN(http_msg_rqver, HTTP_MSG_RQVER); |
Willy Tarreau | 4b89ad4 | 2007-03-04 18:13:58 +0100 | [diff] [blame] | 1332 | |
| 1333 | if (likely(HTTP_IS_CRLF(*ptr))) { |
Willy Tarreau | ea1175a | 2012-03-05 15:52:30 +0100 | [diff] [blame] | 1334 | msg->sl.rq.v_l = ptr - msg_start - msg->sl.rq.v; |
Willy Tarreau | 4b89ad4 | 2007-03-04 18:13:58 +0100 | [diff] [blame] | 1335 | http_msg_rqline_eol: |
| 1336 | /* We have seen the end of line. Note that we do not |
| 1337 | * necessarily have the \n yet, but at least we know that we |
| 1338 | * have EITHER \r OR \n, otherwise the request would not be |
| 1339 | * complete. We can then record the request length and return |
| 1340 | * to the caller which will be able to register it. |
| 1341 | */ |
Willy Tarreau | 3a215be | 2012-03-09 21:39:51 +0100 | [diff] [blame] | 1342 | msg->sl.rq.l = ptr - msg_start - msg->sol; |
Willy Tarreau | 4b89ad4 | 2007-03-04 18:13:58 +0100 | [diff] [blame] | 1343 | return ptr; |
| 1344 | } |
| 1345 | |
| 1346 | /* neither an HTTP_VER token nor a CRLF */ |
Willy Tarreau | 7552c03 | 2009-03-01 11:10:40 +0100 | [diff] [blame] | 1347 | state = HTTP_MSG_ERROR; |
| 1348 | break; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1349 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1350 | default: |
Willy Tarreau | 3770f23 | 2013-12-07 00:01:53 +0100 | [diff] [blame] | 1351 | #ifdef DEBUG_FULL |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1352 | fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state); |
| 1353 | exit(1); |
| 1354 | #endif |
Willy Tarreau | 3770f23 | 2013-12-07 00:01:53 +0100 | [diff] [blame] | 1355 | ; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1356 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1357 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1358 | http_msg_ood: |
Willy Tarreau | 7552c03 | 2009-03-01 11:10:40 +0100 | [diff] [blame] | 1359 | /* out of valid data */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1360 | if (ret_state) |
| 1361 | *ret_state = state; |
| 1362 | if (ret_ptr) |
Willy Tarreau | a458b67 | 2012-03-05 11:17:50 +0100 | [diff] [blame] | 1363 | *ret_ptr = ptr - msg_start; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1364 | return NULL; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1365 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1366 | |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 1367 | /* |
| 1368 | * Returns the data from Authorization header. Function may be called more |
| 1369 | * than once so data is stored in txn->auth_data. When no header is found |
| 1370 | * or auth method is unknown auth_method is set to HTTP_AUTH_WRONG to avoid |
Thierry FOURNIER | 98d9695 | 2014-01-23 12:13:02 +0100 | [diff] [blame] | 1371 | * searching again for something we are unable to find anyway. However, if |
| 1372 | * the result if valid, the cache is not reused because we would risk to |
| 1373 | * have the credentials overwritten by another session in parallel. |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 1374 | */ |
| 1375 | |
Thierry FOURNIER | 9eec0a6 | 2014-01-22 18:38:02 +0100 | [diff] [blame] | 1376 | /* This bufffer is initialized in the file 'src/haproxy.c'. This length is |
| 1377 | * set according to global.tune.bufsize. |
| 1378 | */ |
Willy Tarreau | 7e2c647 | 2012-10-29 20:44:36 +0100 | [diff] [blame] | 1379 | char *get_http_auth_buff; |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 1380 | |
| 1381 | int |
| 1382 | get_http_auth(struct session *s) |
| 1383 | { |
| 1384 | |
| 1385 | struct http_txn *txn = &s->txn; |
| 1386 | struct chunk auth_method; |
| 1387 | struct hdr_ctx ctx; |
| 1388 | char *h, *p; |
| 1389 | int len; |
| 1390 | |
| 1391 | #ifdef DEBUG_AUTH |
| 1392 | printf("Auth for session %p: %d\n", s, txn->auth.method); |
| 1393 | #endif |
| 1394 | |
| 1395 | if (txn->auth.method == HTTP_AUTH_WRONG) |
| 1396 | return 0; |
| 1397 | |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 1398 | txn->auth.method = HTTP_AUTH_WRONG; |
| 1399 | |
| 1400 | ctx.idx = 0; |
Willy Tarreau | 844a7e7 | 2010-01-31 21:46:18 +0100 | [diff] [blame] | 1401 | |
| 1402 | if (txn->flags & TX_USE_PX_CONN) { |
| 1403 | h = "Proxy-Authorization"; |
| 1404 | len = strlen(h); |
| 1405 | } else { |
| 1406 | h = "Authorization"; |
| 1407 | len = strlen(h); |
| 1408 | } |
| 1409 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 1410 | 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] | 1411 | return 0; |
| 1412 | |
| 1413 | h = ctx.line + ctx.val; |
| 1414 | |
| 1415 | p = memchr(h, ' ', ctx.vlen); |
| 1416 | if (!p || p == h) |
| 1417 | return 0; |
| 1418 | |
| 1419 | chunk_initlen(&auth_method, h, 0, p-h); |
| 1420 | chunk_initlen(&txn->auth.method_data, p+1, 0, ctx.vlen-(p-h)-1); |
| 1421 | |
| 1422 | if (!strncasecmp("Basic", auth_method.str, auth_method.len)) { |
| 1423 | |
| 1424 | len = base64dec(txn->auth.method_data.str, txn->auth.method_data.len, |
Willy Tarreau | 7e2c647 | 2012-10-29 20:44:36 +0100 | [diff] [blame] | 1425 | get_http_auth_buff, global.tune.bufsize - 1); |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 1426 | |
| 1427 | if (len < 0) |
| 1428 | return 0; |
| 1429 | |
| 1430 | |
| 1431 | get_http_auth_buff[len] = '\0'; |
| 1432 | |
| 1433 | p = strchr(get_http_auth_buff, ':'); |
| 1434 | |
| 1435 | if (!p) |
| 1436 | return 0; |
| 1437 | |
| 1438 | txn->auth.user = get_http_auth_buff; |
| 1439 | *p = '\0'; |
| 1440 | txn->auth.pass = p+1; |
| 1441 | |
| 1442 | txn->auth.method = HTTP_AUTH_BASIC; |
| 1443 | return 1; |
| 1444 | } |
| 1445 | |
| 1446 | return 0; |
| 1447 | } |
| 1448 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1449 | |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1450 | /* |
| 1451 | * This function parses an HTTP message, either a request or a response, |
Willy Tarreau | 8b1323e | 2012-03-09 14:46:19 +0100 | [diff] [blame] | 1452 | * depending on the initial msg->msg_state. The caller is responsible for |
| 1453 | * ensuring that the message does not wrap. The function can be preempted |
| 1454 | * everywhere when data are missing and recalled at the exact same location |
| 1455 | * with no information loss. The message may even be realigned between two |
| 1456 | * calls. The header index is re-initialized when switching from |
Willy Tarreau | 9cdde23 | 2007-05-02 20:58:19 +0200 | [diff] [blame] | 1457 | * 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] | 1458 | * fields. Note that msg->sol will be initialized after completing the first |
| 1459 | * state, so that none of the msg pointers has to be initialized prior to the |
| 1460 | * first call. |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1461 | */ |
Willy Tarreau | a560c21 | 2012-03-09 13:50:57 +0100 | [diff] [blame] | 1462 | void http_msg_analyzer(struct http_msg *msg, struct hdr_idx *idx) |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1463 | { |
Willy Tarreau | 3770f23 | 2013-12-07 00:01:53 +0100 | [diff] [blame] | 1464 | enum ht_state state; /* updated only when leaving the FSM */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1465 | register char *ptr, *end; /* request pointers, to avoid dereferences */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1466 | struct buffer *buf; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1467 | |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1468 | state = msg->msg_state; |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 1469 | buf = msg->chn->buf; |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1470 | ptr = buf->p + msg->next; |
| 1471 | end = buf->p + buf->i; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1472 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1473 | if (unlikely(ptr >= end)) |
| 1474 | goto http_msg_ood; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1475 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1476 | switch (state) { |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1477 | /* |
| 1478 | * First, states that are specific to the response only. |
| 1479 | * We check them first so that request and headers are |
| 1480 | * closer to each other (accessed more often). |
| 1481 | */ |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1482 | case HTTP_MSG_RPBEFORE: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1483 | http_msg_rpbefore: |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1484 | if (likely(HTTP_IS_TOKEN(*ptr))) { |
Willy Tarreau | 15de77e | 2010-01-02 21:59:16 +0100 | [diff] [blame] | 1485 | /* we have a start of message, but we have to check |
| 1486 | * first if we need to remove some CRLF. We can only |
Willy Tarreau | 2e046c6 | 2012-03-01 16:08:30 +0100 | [diff] [blame] | 1487 | * do this when o=0. |
Willy Tarreau | 15de77e | 2010-01-02 21:59:16 +0100 | [diff] [blame] | 1488 | */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1489 | if (unlikely(ptr != buf->p)) { |
| 1490 | if (buf->o) |
Willy Tarreau | 15de77e | 2010-01-02 21:59:16 +0100 | [diff] [blame] | 1491 | goto http_msg_ood; |
Willy Tarreau | 1d3bcce | 2009-12-27 15:50:06 +0100 | [diff] [blame] | 1492 | /* Remove empty leading lines, as recommended by RFC2616. */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1493 | bi_fast_delete(buf, ptr - buf->p); |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1494 | } |
Willy Tarreau | 2692736 | 2012-05-18 23:22:52 +0200 | [diff] [blame] | 1495 | msg->sol = 0; |
Willy Tarreau | e92693a | 2012-09-24 21:13:39 +0200 | [diff] [blame] | 1496 | msg->sl.st.l = 0; /* used in debug mode */ |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1497 | hdr_idx_init(idx); |
| 1498 | state = HTTP_MSG_RPVER; |
| 1499 | goto http_msg_rpver; |
| 1500 | } |
| 1501 | |
| 1502 | if (unlikely(!HTTP_IS_CRLF(*ptr))) |
| 1503 | goto http_msg_invalid; |
| 1504 | |
| 1505 | if (unlikely(*ptr == '\n')) |
| 1506 | EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore, HTTP_MSG_RPBEFORE); |
| 1507 | EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore_cr, HTTP_MSG_RPBEFORE_CR); |
| 1508 | /* stop here */ |
| 1509 | |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1510 | case HTTP_MSG_RPBEFORE_CR: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1511 | http_msg_rpbefore_cr: |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1512 | EXPECT_LF_HERE(ptr, http_msg_invalid); |
| 1513 | EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore, HTTP_MSG_RPBEFORE); |
| 1514 | /* stop here */ |
| 1515 | |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1516 | case HTTP_MSG_RPVER: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1517 | http_msg_rpver: |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1518 | case HTTP_MSG_RPVER_SP: |
| 1519 | case HTTP_MSG_RPCODE: |
| 1520 | case HTTP_MSG_RPCODE_SP: |
| 1521 | case HTTP_MSG_RPREASON: |
Willy Tarreau | 69d8c5d | 2012-05-08 09:44:41 +0200 | [diff] [blame] | 1522 | ptr = (char *)http_parse_stsline(msg, |
Willy Tarreau | a458b67 | 2012-03-05 11:17:50 +0100 | [diff] [blame] | 1523 | state, ptr, end, |
| 1524 | &msg->next, &msg->msg_state); |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1525 | if (unlikely(!ptr)) |
| 1526 | return; |
| 1527 | |
| 1528 | /* we have a full response and we know that we have either a CR |
| 1529 | * or an LF at <ptr>. |
| 1530 | */ |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1531 | hdr_idx_set_start(idx, msg->sl.st.l, *ptr == '\r'); |
| 1532 | |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1533 | msg->sol = ptr - buf->p; |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1534 | if (likely(*ptr == '\r')) |
| 1535 | EAT_AND_JUMP_OR_RETURN(http_msg_rpline_end, HTTP_MSG_RPLINE_END); |
| 1536 | goto http_msg_rpline_end; |
| 1537 | |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1538 | case HTTP_MSG_RPLINE_END: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1539 | http_msg_rpline_end: |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1540 | /* msg->sol must point to the first of CR or LF. */ |
| 1541 | EXPECT_LF_HERE(ptr, http_msg_invalid); |
| 1542 | EAT_AND_JUMP_OR_RETURN(http_msg_hdr_first, HTTP_MSG_HDR_FIRST); |
| 1543 | /* stop here */ |
| 1544 | |
| 1545 | /* |
| 1546 | * Second, states that are specific to the request only |
| 1547 | */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1548 | case HTTP_MSG_RQBEFORE: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1549 | http_msg_rqbefore: |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1550 | if (likely(HTTP_IS_TOKEN(*ptr))) { |
Willy Tarreau | 15de77e | 2010-01-02 21:59:16 +0100 | [diff] [blame] | 1551 | /* we have a start of message, but we have to check |
| 1552 | * first if we need to remove some CRLF. We can only |
Willy Tarreau | 2e046c6 | 2012-03-01 16:08:30 +0100 | [diff] [blame] | 1553 | * do this when o=0. |
Willy Tarreau | 15de77e | 2010-01-02 21:59:16 +0100 | [diff] [blame] | 1554 | */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1555 | if (likely(ptr != buf->p)) { |
| 1556 | if (buf->o) |
Willy Tarreau | 15de77e | 2010-01-02 21:59:16 +0100 | [diff] [blame] | 1557 | goto http_msg_ood; |
Willy Tarreau | 1d3bcce | 2009-12-27 15:50:06 +0100 | [diff] [blame] | 1558 | /* Remove empty leading lines, as recommended by RFC2616. */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1559 | bi_fast_delete(buf, ptr - buf->p); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1560 | } |
Willy Tarreau | 2692736 | 2012-05-18 23:22:52 +0200 | [diff] [blame] | 1561 | msg->sol = 0; |
Willy Tarreau | e92693a | 2012-09-24 21:13:39 +0200 | [diff] [blame] | 1562 | msg->sl.rq.l = 0; /* used in debug mode */ |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1563 | state = HTTP_MSG_RQMETH; |
| 1564 | goto http_msg_rqmeth; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1565 | } |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1566 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1567 | if (unlikely(!HTTP_IS_CRLF(*ptr))) |
| 1568 | goto http_msg_invalid; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1569 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1570 | if (unlikely(*ptr == '\n')) |
| 1571 | EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore, HTTP_MSG_RQBEFORE); |
| 1572 | 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] | 1573 | /* stop here */ |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1574 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1575 | case HTTP_MSG_RQBEFORE_CR: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1576 | http_msg_rqbefore_cr: |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1577 | EXPECT_LF_HERE(ptr, http_msg_invalid); |
| 1578 | EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore, HTTP_MSG_RQBEFORE); |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1579 | /* stop here */ |
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_RQMETH: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1582 | http_msg_rqmeth: |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1583 | case HTTP_MSG_RQMETH_SP: |
| 1584 | case HTTP_MSG_RQURI: |
| 1585 | case HTTP_MSG_RQURI_SP: |
| 1586 | case HTTP_MSG_RQVER: |
Willy Tarreau | 69d8c5d | 2012-05-08 09:44:41 +0200 | [diff] [blame] | 1587 | ptr = (char *)http_parse_reqline(msg, |
Willy Tarreau | a458b67 | 2012-03-05 11:17:50 +0100 | [diff] [blame] | 1588 | state, ptr, end, |
| 1589 | &msg->next, &msg->msg_state); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1590 | if (unlikely(!ptr)) |
| 1591 | return; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1592 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1593 | /* we have a full request and we know that we have either a CR |
| 1594 | * or an LF at <ptr>. |
| 1595 | */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1596 | hdr_idx_set_start(idx, msg->sl.rq.l, *ptr == '\r'); |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1597 | |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1598 | msg->sol = ptr - buf->p; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1599 | if (likely(*ptr == '\r')) |
| 1600 | 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] | 1601 | goto http_msg_rqline_end; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1602 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1603 | case HTTP_MSG_RQLINE_END: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1604 | http_msg_rqline_end: |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1605 | /* check for HTTP/0.9 request : no version information available. |
| 1606 | * msg->sol must point to the first of CR or LF. |
| 1607 | */ |
| 1608 | if (unlikely(msg->sl.rq.v_l == 0)) |
| 1609 | goto http_msg_last_lf; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1610 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1611 | EXPECT_LF_HERE(ptr, http_msg_invalid); |
| 1612 | 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] | 1613 | /* stop here */ |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1614 | |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1615 | /* |
| 1616 | * Common states below |
| 1617 | */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1618 | case HTTP_MSG_HDR_FIRST: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1619 | http_msg_hdr_first: |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1620 | msg->sol = ptr - buf->p; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1621 | if (likely(!HTTP_IS_CRLF(*ptr))) { |
| 1622 | goto http_msg_hdr_name; |
| 1623 | } |
| 1624 | |
| 1625 | if (likely(*ptr == '\r')) |
| 1626 | EAT_AND_JUMP_OR_RETURN(http_msg_last_lf, HTTP_MSG_LAST_LF); |
| 1627 | goto http_msg_last_lf; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1628 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1629 | case HTTP_MSG_HDR_NAME: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1630 | http_msg_hdr_name: |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1631 | /* assumes msg->sol points to the first char */ |
| 1632 | if (likely(HTTP_IS_TOKEN(*ptr))) |
| 1633 | 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] | 1634 | |
Willy Tarreau | fa4a03c | 2012-03-09 21:28:54 +0100 | [diff] [blame] | 1635 | if (likely(*ptr == ':')) |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1636 | 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] | 1637 | |
Willy Tarreau | 32a4ec0 | 2009-04-02 11:35:18 +0200 | [diff] [blame] | 1638 | if (likely(msg->err_pos < -1) || *ptr == '\n') |
| 1639 | goto http_msg_invalid; |
| 1640 | |
| 1641 | if (msg->err_pos == -1) /* capture error pointer */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1642 | msg->err_pos = ptr - buf->p; /* >= 0 now */ |
Willy Tarreau | 32a4ec0 | 2009-04-02 11:35:18 +0200 | [diff] [blame] | 1643 | |
| 1644 | /* and we still accept this non-token character */ |
| 1645 | 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] | 1646 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1647 | case HTTP_MSG_HDR_L1_SP: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1648 | http_msg_hdr_l1_sp: |
Willy Tarreau | fa4a03c | 2012-03-09 21:28:54 +0100 | [diff] [blame] | 1649 | /* assumes msg->sol points to the first char */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1650 | if (likely(HTTP_IS_SPHT(*ptr))) |
| 1651 | 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] | 1652 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1653 | /* header value can be basically anything except CR/LF */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1654 | msg->sov = ptr - buf->p; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1655 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1656 | if (likely(!HTTP_IS_CRLF(*ptr))) { |
| 1657 | goto http_msg_hdr_val; |
| 1658 | } |
| 1659 | |
| 1660 | if (likely(*ptr == '\r')) |
| 1661 | EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_lf, HTTP_MSG_HDR_L1_LF); |
| 1662 | goto http_msg_hdr_l1_lf; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1663 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1664 | case HTTP_MSG_HDR_L1_LF: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1665 | http_msg_hdr_l1_lf: |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1666 | EXPECT_LF_HERE(ptr, http_msg_invalid); |
| 1667 | 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] | 1668 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1669 | case HTTP_MSG_HDR_L1_LWS: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1670 | http_msg_hdr_l1_lws: |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1671 | if (likely(HTTP_IS_SPHT(*ptr))) { |
| 1672 | /* replace HT,CR,LF with spaces */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1673 | for (; buf->p + msg->sov < ptr; msg->sov++) |
| 1674 | buf->p[msg->sov] = ' '; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1675 | goto http_msg_hdr_l1_sp; |
| 1676 | } |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 1677 | /* we had a header consisting only in spaces ! */ |
Willy Tarreau | 12e48b3 | 2012-03-05 16:57:34 +0100 | [diff] [blame] | 1678 | msg->eol = msg->sov; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1679 | goto http_msg_complete_header; |
| 1680 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1681 | case HTTP_MSG_HDR_VAL: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1682 | http_msg_hdr_val: |
Willy Tarreau | fa4a03c | 2012-03-09 21:28:54 +0100 | [diff] [blame] | 1683 | /* assumes msg->sol points to the first char, and msg->sov |
| 1684 | * points to the first character of the value. |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1685 | */ |
| 1686 | if (likely(!HTTP_IS_CRLF(*ptr))) |
| 1687 | 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] | 1688 | |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1689 | msg->eol = ptr - buf->p; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1690 | /* Note: we could also copy eol into ->eoh so that we have the |
| 1691 | * real header end in case it ends with lots of LWS, but is this |
| 1692 | * really needed ? |
| 1693 | */ |
| 1694 | if (likely(*ptr == '\r')) |
| 1695 | EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l2_lf, HTTP_MSG_HDR_L2_LF); |
| 1696 | goto http_msg_hdr_l2_lf; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1697 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1698 | case HTTP_MSG_HDR_L2_LF: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1699 | http_msg_hdr_l2_lf: |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1700 | EXPECT_LF_HERE(ptr, http_msg_invalid); |
| 1701 | 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] | 1702 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1703 | case HTTP_MSG_HDR_L2_LWS: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1704 | http_msg_hdr_l2_lws: |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1705 | if (unlikely(HTTP_IS_SPHT(*ptr))) { |
| 1706 | /* LWS: replace HT,CR,LF with spaces */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1707 | for (; buf->p + msg->eol < ptr; msg->eol++) |
| 1708 | buf->p[msg->eol] = ' '; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1709 | goto http_msg_hdr_val; |
| 1710 | } |
| 1711 | http_msg_complete_header: |
| 1712 | /* |
| 1713 | * It was a new header, so the last one is finished. |
Willy Tarreau | fa4a03c | 2012-03-09 21:28:54 +0100 | [diff] [blame] | 1714 | * Assumes msg->sol points to the first char, msg->sov points |
| 1715 | * to the first character of the value and msg->eol to the |
| 1716 | * first CR or LF so we know how the line ends. We insert last |
| 1717 | * header into the index. |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1718 | */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1719 | 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] | 1720 | idx, idx->tail) < 0)) |
| 1721 | goto http_msg_invalid; |
Willy Tarreau | 230fd0b | 2006-12-17 12:05:00 +0100 | [diff] [blame] | 1722 | |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1723 | msg->sol = ptr - buf->p; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1724 | if (likely(!HTTP_IS_CRLF(*ptr))) { |
| 1725 | goto http_msg_hdr_name; |
| 1726 | } |
| 1727 | |
| 1728 | if (likely(*ptr == '\r')) |
| 1729 | EAT_AND_JUMP_OR_RETURN(http_msg_last_lf, HTTP_MSG_LAST_LF); |
| 1730 | goto http_msg_last_lf; |
Willy Tarreau | 230fd0b | 2006-12-17 12:05:00 +0100 | [diff] [blame] | 1731 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1732 | case HTTP_MSG_LAST_LF: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1733 | http_msg_last_lf: |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1734 | /* Assumes msg->sol points to the first of either CR or LF */ |
| 1735 | EXPECT_LF_HERE(ptr, http_msg_invalid); |
| 1736 | ptr++; |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1737 | msg->sov = msg->next = ptr - buf->p; |
Willy Tarreau | 3a215be | 2012-03-09 21:39:51 +0100 | [diff] [blame] | 1738 | msg->eoh = msg->sol; |
| 1739 | msg->sol = 0; |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1740 | msg->msg_state = HTTP_MSG_BODY; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1741 | return; |
Willy Tarreau | b56928a | 2012-04-16 14:51:55 +0200 | [diff] [blame] | 1742 | |
| 1743 | case HTTP_MSG_ERROR: |
| 1744 | /* this may only happen if we call http_msg_analyser() twice with an error */ |
| 1745 | break; |
| 1746 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1747 | default: |
Willy Tarreau | 3770f23 | 2013-12-07 00:01:53 +0100 | [diff] [blame] | 1748 | #ifdef DEBUG_FULL |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1749 | fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state); |
| 1750 | exit(1); |
Willy Tarreau | 230fd0b | 2006-12-17 12:05:00 +0100 | [diff] [blame] | 1751 | #endif |
Willy Tarreau | 3770f23 | 2013-12-07 00:01:53 +0100 | [diff] [blame] | 1752 | ; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1753 | } |
| 1754 | http_msg_ood: |
| 1755 | /* out of data */ |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1756 | msg->msg_state = state; |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1757 | msg->next = ptr - buf->p; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1758 | return; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1759 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1760 | http_msg_invalid: |
| 1761 | /* invalid message */ |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1762 | msg->msg_state = HTTP_MSG_ERROR; |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1763 | msg->next = ptr - buf->p; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1764 | return; |
| 1765 | } |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 1766 | |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 1767 | /* convert an HTTP/0.9 request into an HTTP/1.0 request. Returns 1 if the |
| 1768 | * conversion succeeded, 0 in case of error. If the request was already 1.X, |
| 1769 | * nothing is done and 1 is returned. |
| 1770 | */ |
Willy Tarreau | 418bfcc | 2012-03-09 13:56:20 +0100 | [diff] [blame] | 1771 | static int http_upgrade_v09_to_v10(struct http_txn *txn) |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 1772 | { |
| 1773 | int delta; |
| 1774 | char *cur_end; |
Willy Tarreau | 418bfcc | 2012-03-09 13:56:20 +0100 | [diff] [blame] | 1775 | struct http_msg *msg = &txn->req; |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 1776 | |
| 1777 | if (msg->sl.rq.v_l != 0) |
| 1778 | return 1; |
| 1779 | |
Apollon Oikonomopoulos | 25a1522 | 2014-04-06 02:46:00 +0300 | [diff] [blame] | 1780 | /* RFC 1945 allows only GET for HTTP/0.9 requests */ |
| 1781 | if (txn->meth != HTTP_METH_GET) |
| 1782 | return 0; |
| 1783 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 1784 | cur_end = msg->chn->buf->p + msg->sl.rq.l; |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 1785 | delta = 0; |
| 1786 | |
| 1787 | if (msg->sl.rq.u_l == 0) { |
Apollon Oikonomopoulos | 25a1522 | 2014-04-06 02:46:00 +0300 | [diff] [blame] | 1788 | /* HTTP/0.9 requests *must* have a request URI, per RFC 1945 */ |
| 1789 | return 0; |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 1790 | } |
| 1791 | /* add HTTP version */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 1792 | 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] | 1793 | http_msg_move_end(msg, delta); |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 1794 | cur_end += delta; |
Willy Tarreau | 69d8c5d | 2012-05-08 09:44:41 +0200 | [diff] [blame] | 1795 | cur_end = (char *)http_parse_reqline(msg, |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 1796 | HTTP_MSG_RQMETH, |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 1797 | msg->chn->buf->p, cur_end + 1, |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 1798 | NULL, NULL); |
| 1799 | if (unlikely(!cur_end)) |
| 1800 | return 0; |
| 1801 | |
| 1802 | /* we have a full HTTP/1.0 request now and we know that |
| 1803 | * we have either a CR or an LF at <ptr>. |
| 1804 | */ |
| 1805 | hdr_idx_set_start(&txn->hdr_idx, msg->sl.rq.l, *cur_end == '\r'); |
| 1806 | return 1; |
| 1807 | } |
| 1808 | |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 1809 | /* Parse the Connection: header of an HTTP request, looking for both "close" |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 1810 | * and "keep-alive" values. If we already know that some headers may safely |
| 1811 | * 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] | 1812 | * - bit 0 means remove "close" headers (in HTTP/1.0 requests/responses) |
| 1813 | * - 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] | 1814 | * Presence of the "Upgrade" token is also checked and reported. |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 1815 | * The TX_HDR_CONN_* flags are adjusted in txn->flags depending on what was |
| 1816 | * found, and TX_CON_*_SET is adjusted depending on what is left so only |
| 1817 | * harmless combinations may be removed. Do not call that after changes have |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 1818 | * been processed. |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 1819 | */ |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 1820 | 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] | 1821 | { |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 1822 | struct hdr_ctx ctx; |
Willy Tarreau | 88d349d | 2010-01-25 12:15:43 +0100 | [diff] [blame] | 1823 | const char *hdr_val = "Connection"; |
| 1824 | int hdr_len = 10; |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 1825 | |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 1826 | if (txn->flags & TX_HDR_CONN_PRS) |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 1827 | return; |
| 1828 | |
Willy Tarreau | 88d349d | 2010-01-25 12:15:43 +0100 | [diff] [blame] | 1829 | if (unlikely(txn->flags & TX_USE_PX_CONN)) { |
| 1830 | hdr_val = "Proxy-Connection"; |
| 1831 | hdr_len = 16; |
| 1832 | } |
| 1833 | |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 1834 | ctx.idx = 0; |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 1835 | txn->flags &= ~(TX_CON_KAL_SET|TX_CON_CLO_SET); |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 1836 | 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] | 1837 | if (ctx.vlen >= 10 && word_match(ctx.line + ctx.val, ctx.vlen, "keep-alive", 10)) { |
| 1838 | txn->flags |= TX_HDR_CONN_KAL; |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 1839 | if (to_del & 2) |
| 1840 | http_remove_header2(msg, &txn->hdr_idx, &ctx); |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 1841 | else |
| 1842 | txn->flags |= TX_CON_KAL_SET; |
| 1843 | } |
| 1844 | else if (ctx.vlen >= 5 && word_match(ctx.line + ctx.val, ctx.vlen, "close", 5)) { |
| 1845 | txn->flags |= TX_HDR_CONN_CLO; |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 1846 | if (to_del & 1) |
| 1847 | http_remove_header2(msg, &txn->hdr_idx, &ctx); |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 1848 | else |
| 1849 | txn->flags |= TX_CON_CLO_SET; |
| 1850 | } |
Willy Tarreau | 50fc777 | 2012-11-11 22:19:57 +0100 | [diff] [blame] | 1851 | else if (ctx.vlen >= 7 && word_match(ctx.line + ctx.val, ctx.vlen, "upgrade", 7)) { |
| 1852 | txn->flags |= TX_HDR_CONN_UPG; |
| 1853 | } |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 1854 | } |
| 1855 | |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 1856 | txn->flags |= TX_HDR_CONN_PRS; |
| 1857 | return; |
| 1858 | } |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 1859 | |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 1860 | /* Apply desired changes on the Connection: header. Values may be removed and/or |
| 1861 | * added depending on the <wanted> flags, which are exclusively composed of |
| 1862 | * TX_CON_CLO_SET and TX_CON_KAL_SET, depending on what flags are desired. The |
| 1863 | * TX_CON_*_SET flags are adjusted in txn->flags depending on what is left. |
| 1864 | */ |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 1865 | 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] | 1866 | { |
| 1867 | struct hdr_ctx ctx; |
Willy Tarreau | 88d349d | 2010-01-25 12:15:43 +0100 | [diff] [blame] | 1868 | const char *hdr_val = "Connection"; |
| 1869 | int hdr_len = 10; |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 1870 | |
| 1871 | ctx.idx = 0; |
| 1872 | |
Willy Tarreau | 88d349d | 2010-01-25 12:15:43 +0100 | [diff] [blame] | 1873 | |
| 1874 | if (unlikely(txn->flags & TX_USE_PX_CONN)) { |
| 1875 | hdr_val = "Proxy-Connection"; |
| 1876 | hdr_len = 16; |
| 1877 | } |
| 1878 | |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 1879 | txn->flags &= ~(TX_CON_CLO_SET | TX_CON_KAL_SET); |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 1880 | 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] | 1881 | if (ctx.vlen >= 10 && word_match(ctx.line + ctx.val, ctx.vlen, "keep-alive", 10)) { |
| 1882 | if (wanted & TX_CON_KAL_SET) |
| 1883 | txn->flags |= TX_CON_KAL_SET; |
| 1884 | else |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 1885 | http_remove_header2(msg, &txn->hdr_idx, &ctx); |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 1886 | } |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 1887 | else if (ctx.vlen >= 5 && word_match(ctx.line + ctx.val, ctx.vlen, "close", 5)) { |
| 1888 | if (wanted & TX_CON_CLO_SET) |
| 1889 | txn->flags |= TX_CON_CLO_SET; |
| 1890 | else |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 1891 | http_remove_header2(msg, &txn->hdr_idx, &ctx); |
Willy Tarreau | 0dfdf19 | 2010-01-05 11:33:11 +0100 | [diff] [blame] | 1892 | } |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 1893 | } |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 1894 | |
| 1895 | if (wanted == (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET))) |
| 1896 | return; |
| 1897 | |
| 1898 | if ((wanted & TX_CON_CLO_SET) && !(txn->flags & TX_CON_CLO_SET)) { |
| 1899 | txn->flags |= TX_CON_CLO_SET; |
Willy Tarreau | 88d349d | 2010-01-25 12:15:43 +0100 | [diff] [blame] | 1900 | hdr_val = "Connection: close"; |
| 1901 | hdr_len = 17; |
| 1902 | if (unlikely(txn->flags & TX_USE_PX_CONN)) { |
| 1903 | hdr_val = "Proxy-Connection: close"; |
| 1904 | hdr_len = 23; |
| 1905 | } |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 1906 | http_header_add_tail2(msg, &txn->hdr_idx, hdr_val, hdr_len); |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 1907 | } |
| 1908 | |
| 1909 | if ((wanted & TX_CON_KAL_SET) && !(txn->flags & TX_CON_KAL_SET)) { |
| 1910 | txn->flags |= TX_CON_KAL_SET; |
Willy Tarreau | 88d349d | 2010-01-25 12:15:43 +0100 | [diff] [blame] | 1911 | hdr_val = "Connection: keep-alive"; |
| 1912 | hdr_len = 22; |
| 1913 | if (unlikely(txn->flags & TX_USE_PX_CONN)) { |
| 1914 | hdr_val = "Proxy-Connection: keep-alive"; |
| 1915 | hdr_len = 28; |
| 1916 | } |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 1917 | http_header_add_tail2(msg, &txn->hdr_idx, hdr_val, hdr_len); |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 1918 | } |
| 1919 | return; |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 1920 | } |
| 1921 | |
Willy Tarreau | a458b67 | 2012-03-05 11:17:50 +0100 | [diff] [blame] | 1922 | /* 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] | 1923 | * 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] | 1924 | * so that we know we can forward between ->sol and ->sov. |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 1925 | * 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] | 1926 | * 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] | 1927 | */ |
Willy Tarreau | 24e6d97 | 2012-10-26 00:49:52 +0200 | [diff] [blame] | 1928 | static inline int http_parse_chunk_size(struct http_msg *msg) |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 1929 | { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 1930 | const struct buffer *buf = msg->chn->buf; |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1931 | const char *ptr = b_ptr(buf, msg->next); |
Willy Tarreau | 4baf44b | 2012-03-09 14:10:20 +0100 | [diff] [blame] | 1932 | const char *ptr_old = ptr; |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1933 | const char *end = buf->data + buf->size; |
| 1934 | const char *stop = bi_end(buf); |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 1935 | unsigned int chunk = 0; |
| 1936 | |
| 1937 | /* The chunk size is in the following form, though we are only |
| 1938 | * interested in the size and CRLF : |
| 1939 | * 1*HEXDIGIT *WSP *[ ';' extensions ] CRLF |
| 1940 | */ |
| 1941 | while (1) { |
| 1942 | int c; |
Willy Tarreau | 363a5bb | 2012-03-02 20:14:45 +0100 | [diff] [blame] | 1943 | if (ptr == stop) |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 1944 | return 0; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 1945 | c = hex2i(*ptr); |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 1946 | if (c < 0) /* not a hex digit anymore */ |
| 1947 | break; |
Willy Tarreau | 0161d62 | 2013-04-02 01:26:55 +0200 | [diff] [blame] | 1948 | if (unlikely(++ptr >= end)) |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1949 | ptr = buf->data; |
Willy Tarreau | 431946e | 2012-02-24 19:20:12 +0100 | [diff] [blame] | 1950 | if (chunk & 0xF8000000) /* integer overflow will occur if result >= 2GB */ |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 1951 | goto error; |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 1952 | chunk = (chunk << 4) + c; |
| 1953 | } |
| 1954 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 1955 | /* empty size not allowed */ |
Willy Tarreau | 0161d62 | 2013-04-02 01:26:55 +0200 | [diff] [blame] | 1956 | if (unlikely(ptr == ptr_old)) |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 1957 | goto error; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 1958 | |
| 1959 | while (http_is_spht[(unsigned char)*ptr]) { |
| 1960 | if (++ptr >= end) |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1961 | ptr = buf->data; |
Willy Tarreau | 0161d62 | 2013-04-02 01:26:55 +0200 | [diff] [blame] | 1962 | if (unlikely(ptr == stop)) |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 1963 | return 0; |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 1964 | } |
| 1965 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 1966 | /* Up to there, we know that at least one byte is present at *ptr. Check |
| 1967 | * for the end of chunk size. |
| 1968 | */ |
| 1969 | while (1) { |
| 1970 | if (likely(HTTP_IS_CRLF(*ptr))) { |
| 1971 | /* we now have a CR or an LF at ptr */ |
| 1972 | if (likely(*ptr == '\r')) { |
| 1973 | if (++ptr >= end) |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1974 | ptr = buf->data; |
Willy Tarreau | 363a5bb | 2012-03-02 20:14:45 +0100 | [diff] [blame] | 1975 | if (ptr == stop) |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 1976 | return 0; |
| 1977 | } |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 1978 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 1979 | if (*ptr != '\n') |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 1980 | goto error; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 1981 | if (++ptr >= end) |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1982 | ptr = buf->data; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 1983 | /* done */ |
| 1984 | break; |
| 1985 | } |
| 1986 | else if (*ptr == ';') { |
| 1987 | /* chunk extension, ends at next CRLF */ |
| 1988 | if (++ptr >= end) |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1989 | ptr = buf->data; |
Willy Tarreau | 363a5bb | 2012-03-02 20:14:45 +0100 | [diff] [blame] | 1990 | if (ptr == stop) |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 1991 | return 0; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 1992 | |
| 1993 | while (!HTTP_IS_CRLF(*ptr)) { |
| 1994 | if (++ptr >= end) |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1995 | ptr = buf->data; |
Willy Tarreau | 363a5bb | 2012-03-02 20:14:45 +0100 | [diff] [blame] | 1996 | if (ptr == stop) |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 1997 | return 0; |
| 1998 | } |
| 1999 | /* we have a CRLF now, loop above */ |
| 2000 | continue; |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 2001 | } |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2002 | else |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 2003 | goto error; |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 2004 | } |
| 2005 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2006 | /* 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] | 2007 | * 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] | 2008 | * ->sov. |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 2009 | */ |
Willy Tarreau | 0161d62 | 2013-04-02 01:26:55 +0200 | [diff] [blame] | 2010 | if (unlikely(ptr < ptr_old)) |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 2011 | msg->sov += buf->size; |
Willy Tarreau | a458b67 | 2012-03-05 11:17:50 +0100 | [diff] [blame] | 2012 | msg->sov += ptr - ptr_old; |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 2013 | msg->next = buffer_count(buf, buf->p, ptr); |
Willy Tarreau | 124d991 | 2011-03-01 20:30:48 +0100 | [diff] [blame] | 2014 | msg->chunk_len = chunk; |
| 2015 | msg->body_len += chunk; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2016 | msg->msg_state = chunk ? HTTP_MSG_DATA : HTTP_MSG_TRAILERS; |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 2017 | return 1; |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 2018 | error: |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 2019 | msg->err_pos = buffer_count(buf, buf->p, ptr); |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 2020 | return -1; |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 2021 | } |
| 2022 | |
Willy Tarreau | 4baf44b | 2012-03-09 14:10:20 +0100 | [diff] [blame] | 2023 | /* This function skips trailers in the buffer associated with HTTP |
Willy Tarreau | a458b67 | 2012-03-05 11:17:50 +0100 | [diff] [blame] | 2024 | * 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] | 2025 | * the trailers is found, it is automatically scheduled to be forwarded, |
| 2026 | * msg->msg_state switches to HTTP_MSG_DONE, and the function returns >0. |
| 2027 | * If not enough data are available, the function does not change anything |
Willy Tarreau | a458b67 | 2012-03-05 11:17:50 +0100 | [diff] [blame] | 2028 | * 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] | 2029 | * 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] | 2030 | * 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] | 2031 | * must already be in HTTP_MSG_TRAILERS state before calling this function, |
| 2032 | * which implies that all non-trailers data have already been scheduled for |
Willy Tarreau | 2692736 | 2012-05-18 23:22:52 +0200 | [diff] [blame] | 2033 | * forwarding, and that the difference between msg->sol and msg->sov exactly |
Willy Tarreau | 638cd02 | 2010-01-03 07:42:04 +0100 | [diff] [blame] | 2034 | * matches the length of trailers already parsed and not forwarded. It is also |
| 2035 | * important to note that this function is designed to be able to parse wrapped |
| 2036 | * headers at end of buffer. |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2037 | */ |
Willy Tarreau | 24e6d97 | 2012-10-26 00:49:52 +0200 | [diff] [blame] | 2038 | static int http_forward_trailers(struct http_msg *msg) |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2039 | { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2040 | const struct buffer *buf = msg->chn->buf; |
Willy Tarreau | 4baf44b | 2012-03-09 14:10:20 +0100 | [diff] [blame] | 2041 | |
Willy Tarreau | a458b67 | 2012-03-05 11:17:50 +0100 | [diff] [blame] | 2042 | /* we have msg->next which points to next line. Look for CRLF. */ |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2043 | while (1) { |
Willy Tarreau | 4baf44b | 2012-03-09 14:10:20 +0100 | [diff] [blame] | 2044 | const char *p1 = NULL, *p2 = NULL; |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 2045 | const char *ptr = b_ptr(buf, msg->next); |
| 2046 | const char *stop = bi_end(buf); |
Willy Tarreau | 638cd02 | 2010-01-03 07:42:04 +0100 | [diff] [blame] | 2047 | int bytes; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2048 | |
| 2049 | /* scan current line and stop at LF or CRLF */ |
| 2050 | while (1) { |
Willy Tarreau | 363a5bb | 2012-03-02 20:14:45 +0100 | [diff] [blame] | 2051 | if (ptr == stop) |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2052 | return 0; |
| 2053 | |
| 2054 | if (*ptr == '\n') { |
| 2055 | if (!p1) |
| 2056 | p1 = ptr; |
| 2057 | p2 = ptr; |
| 2058 | break; |
| 2059 | } |
| 2060 | |
| 2061 | if (*ptr == '\r') { |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 2062 | if (p1) { |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 2063 | msg->err_pos = buffer_count(buf, buf->p, ptr); |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2064 | return -1; |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 2065 | } |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2066 | p1 = ptr; |
| 2067 | } |
| 2068 | |
| 2069 | ptr++; |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 2070 | if (ptr >= buf->data + buf->size) |
| 2071 | ptr = buf->data; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2072 | } |
| 2073 | |
| 2074 | /* after LF; point to beginning of next line */ |
| 2075 | p2++; |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 2076 | if (p2 >= buf->data + buf->size) |
| 2077 | p2 = buf->data; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2078 | |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 2079 | bytes = p2 - b_ptr(buf, msg->next); |
Willy Tarreau | 638cd02 | 2010-01-03 07:42:04 +0100 | [diff] [blame] | 2080 | if (bytes < 0) |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 2081 | bytes += buf->size; |
Willy Tarreau | 638cd02 | 2010-01-03 07:42:04 +0100 | [diff] [blame] | 2082 | |
| 2083 | /* schedule this line for forwarding */ |
| 2084 | msg->sov += bytes; |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 2085 | if (msg->sov >= buf->size) |
| 2086 | msg->sov -= buf->size; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2087 | |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 2088 | if (p1 == b_ptr(buf, msg->next)) { |
Willy Tarreau | 638cd02 | 2010-01-03 07:42:04 +0100 | [diff] [blame] | 2089 | /* LF/CRLF at beginning of line => end of trailers at p2. |
| 2090 | * Everything was scheduled for forwarding, there's nothing |
| 2091 | * left from this message. |
Willy Tarreau | 5523b32 | 2009-12-29 12:05:52 +0100 | [diff] [blame] | 2092 | */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 2093 | msg->next = buffer_count(buf, buf->p, p2); |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2094 | msg->msg_state = HTTP_MSG_DONE; |
| 2095 | return 1; |
| 2096 | } |
| 2097 | /* OK, next line then */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 2098 | msg->next = buffer_count(buf, buf->p, p2); |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2099 | } |
| 2100 | } |
| 2101 | |
Willy Tarreau | 54d23df | 2012-10-25 19:04:45 +0200 | [diff] [blame] | 2102 | /* 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] | 2103 | * 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] | 2104 | * ->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] | 2105 | * 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] | 2106 | * It also sets msg_state to HTTP_MSG_CHUNK_SIZE and returns >0 on success. If |
| 2107 | * not enough data are available, the function does not change anything and |
| 2108 | * returns zero. If a parse error is encountered, the function returns < 0 and |
| 2109 | * does not change anything. Note: this function is designed to parse wrapped |
| 2110 | * CRLF at the end of the buffer. |
| 2111 | */ |
Willy Tarreau | 24e6d97 | 2012-10-26 00:49:52 +0200 | [diff] [blame] | 2112 | static inline int http_skip_chunk_crlf(struct http_msg *msg) |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2113 | { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2114 | const struct buffer *buf = msg->chn->buf; |
Willy Tarreau | 4baf44b | 2012-03-09 14:10:20 +0100 | [diff] [blame] | 2115 | const char *ptr; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2116 | int bytes; |
| 2117 | |
| 2118 | /* NB: we'll check data availabilty at the end. It's not a |
| 2119 | * problem because whatever we match first will be checked |
| 2120 | * against the correct length. |
| 2121 | */ |
| 2122 | bytes = 1; |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 2123 | ptr = buf->p; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2124 | if (*ptr == '\r') { |
| 2125 | bytes++; |
| 2126 | ptr++; |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 2127 | if (ptr >= buf->data + buf->size) |
| 2128 | ptr = buf->data; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2129 | } |
| 2130 | |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 2131 | if (bytes > buf->i) |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2132 | return 0; |
| 2133 | |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 2134 | if (*ptr != '\n') { |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 2135 | msg->err_pos = buffer_count(buf, buf->p, ptr); |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2136 | return -1; |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 2137 | } |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2138 | |
| 2139 | ptr++; |
Willy Tarreau | 0161d62 | 2013-04-02 01:26:55 +0200 | [diff] [blame] | 2140 | if (unlikely(ptr >= buf->data + buf->size)) |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 2141 | ptr = buf->data; |
Willy Tarreau | 2692736 | 2012-05-18 23:22:52 +0200 | [diff] [blame] | 2142 | /* prepare the CRLF to be forwarded (between ->sol and ->sov) */ |
| 2143 | msg->sol = 0; |
Willy Tarreau | ea1175a | 2012-03-05 15:52:30 +0100 | [diff] [blame] | 2144 | msg->sov = msg->next = bytes; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2145 | msg->msg_state = HTTP_MSG_CHUNK_SIZE; |
| 2146 | return 1; |
| 2147 | } |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 2148 | |
Willy Tarreau | 0e9b1b4 | 2014-03-19 12:07:52 +0100 | [diff] [blame] | 2149 | /* Parses a qvalue and returns it multipled by 1000, from 0 to 1000. If the |
| 2150 | * value is larger than 1000, it is bound to 1000. The parser consumes up to |
| 2151 | * 1 digit, one dot and 3 digits and stops on the first invalid character. |
| 2152 | * Unparsable qvalues return 1000 as "q=1.000". |
| 2153 | */ |
Thierry FOURNIER | ad90351 | 2014-04-11 17:51:01 +0200 | [diff] [blame] | 2154 | int parse_qvalue(const char *qvalue, const char **end) |
Willy Tarreau | 0e9b1b4 | 2014-03-19 12:07:52 +0100 | [diff] [blame] | 2155 | { |
| 2156 | int q = 1000; |
| 2157 | |
| 2158 | if (!isdigit(*qvalue)) |
| 2159 | goto out; |
| 2160 | q = (*qvalue++ - '0') * 1000; |
| 2161 | |
| 2162 | if (*qvalue++ != '.') |
| 2163 | goto out; |
| 2164 | |
| 2165 | if (!isdigit(*qvalue)) |
| 2166 | goto out; |
| 2167 | q += (*qvalue++ - '0') * 100; |
| 2168 | |
| 2169 | if (!isdigit(*qvalue)) |
| 2170 | goto out; |
| 2171 | q += (*qvalue++ - '0') * 10; |
| 2172 | |
| 2173 | if (!isdigit(*qvalue)) |
| 2174 | goto out; |
| 2175 | q += (*qvalue++ - '0') * 1; |
| 2176 | out: |
| 2177 | if (q > 1000) |
| 2178 | q = 1000; |
Thierry FOURNIER | ad90351 | 2014-04-11 17:51:01 +0200 | [diff] [blame] | 2179 | if (*end) |
| 2180 | *end = qvalue; |
Willy Tarreau | 0e9b1b4 | 2014-03-19 12:07:52 +0100 | [diff] [blame] | 2181 | return q; |
| 2182 | } |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 2183 | |
| 2184 | /* |
| 2185 | * Selects a compression algorithm depending on the client request. |
Willy Tarreau | 05d8460 | 2012-10-26 02:11:25 +0200 | [diff] [blame] | 2186 | */ |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 2187 | int select_compression_request_header(struct session *s, struct buffer *req) |
| 2188 | { |
| 2189 | struct http_txn *txn = &s->txn; |
Willy Tarreau | 70737d1 | 2012-10-27 00:34:28 +0200 | [diff] [blame] | 2190 | struct http_msg *msg = &txn->req; |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 2191 | struct hdr_ctx ctx; |
| 2192 | struct comp_algo *comp_algo = NULL; |
Willy Tarreau | 3c7b97b | 2012-10-26 14:50:26 +0200 | [diff] [blame] | 2193 | struct comp_algo *comp_algo_back = NULL; |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 2194 | |
Finn Arne Gangstad | cbb9a4b | 2012-10-29 21:43:01 +0100 | [diff] [blame] | 2195 | /* Disable compression for older user agents announcing themselves as "Mozilla/4" |
| 2196 | * 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] | 2197 | * See http://zoompf.com/2012/02/lose-the-wait-http-compression for more details. |
| 2198 | */ |
| 2199 | ctx.idx = 0; |
| 2200 | if (http_find_header2("User-Agent", 10, req->p, &txn->hdr_idx, &ctx) && |
| 2201 | ctx.vlen >= 9 && |
Finn Arne Gangstad | cbb9a4b | 2012-10-29 21:43:01 +0100 | [diff] [blame] | 2202 | memcmp(ctx.line + ctx.val, "Mozilla/4", 9) == 0 && |
| 2203 | (ctx.vlen < 31 || |
| 2204 | memcmp(ctx.line + ctx.val + 25, "MSIE ", 5) != 0 || |
| 2205 | ctx.line[ctx.val + 30] < '6' || |
| 2206 | (ctx.line[ctx.val + 30] == '6' && |
| 2207 | (ctx.vlen < 54 || memcmp(ctx.line + 51, "SV1", 3) != 0)))) { |
| 2208 | s->comp_algo = NULL; |
| 2209 | return 0; |
Willy Tarreau | 05d8460 | 2012-10-26 02:11:25 +0200 | [diff] [blame] | 2210 | } |
| 2211 | |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 2212 | /* search for the algo in the backend in priority or the frontend */ |
Willy Tarreau | 3c7b97b | 2012-10-26 14:50:26 +0200 | [diff] [blame] | 2213 | if ((s->be->comp && (comp_algo_back = s->be->comp->algos)) || (s->fe->comp && (comp_algo_back = s->fe->comp->algos))) { |
Willy Tarreau | 0e9b1b4 | 2014-03-19 12:07:52 +0100 | [diff] [blame] | 2214 | int best_q = 0; |
| 2215 | |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 2216 | ctx.idx = 0; |
| 2217 | while (http_find_header2("Accept-Encoding", 15, req->p, &txn->hdr_idx, &ctx)) { |
Willy Tarreau | 0e9b1b4 | 2014-03-19 12:07:52 +0100 | [diff] [blame] | 2218 | const char *qval; |
| 2219 | int q; |
| 2220 | int toklen; |
| 2221 | |
| 2222 | /* try to isolate the token from the optional q-value */ |
| 2223 | toklen = 0; |
| 2224 | while (toklen < ctx.vlen && http_is_token[(unsigned char)*(ctx.line + ctx.val + toklen)]) |
| 2225 | toklen++; |
| 2226 | |
| 2227 | qval = ctx.line + ctx.val + toklen; |
| 2228 | while (1) { |
| 2229 | while (qval < ctx.line + ctx.val + ctx.vlen && http_is_lws[(unsigned char)*qval]) |
| 2230 | qval++; |
| 2231 | |
| 2232 | if (qval >= ctx.line + ctx.val + ctx.vlen || *qval != ';') { |
| 2233 | qval = NULL; |
| 2234 | break; |
| 2235 | } |
| 2236 | qval++; |
Willy Tarreau | 70737d1 | 2012-10-27 00:34:28 +0200 | [diff] [blame] | 2237 | |
Willy Tarreau | 0e9b1b4 | 2014-03-19 12:07:52 +0100 | [diff] [blame] | 2238 | while (qval < ctx.line + ctx.val + ctx.vlen && http_is_lws[(unsigned char)*qval]) |
| 2239 | qval++; |
Willy Tarreau | 70737d1 | 2012-10-27 00:34:28 +0200 | [diff] [blame] | 2240 | |
Willy Tarreau | 0e9b1b4 | 2014-03-19 12:07:52 +0100 | [diff] [blame] | 2241 | if (qval >= ctx.line + ctx.val + ctx.vlen) { |
| 2242 | qval = NULL; |
| 2243 | break; |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 2244 | } |
Willy Tarreau | 0e9b1b4 | 2014-03-19 12:07:52 +0100 | [diff] [blame] | 2245 | if (strncmp(qval, "q=", MIN(ctx.line + ctx.val + ctx.vlen - qval, 2)) == 0) |
| 2246 | break; |
| 2247 | |
| 2248 | while (qval < ctx.line + ctx.val + ctx.vlen && *qval != ';') |
| 2249 | qval++; |
| 2250 | } |
| 2251 | |
| 2252 | /* here we have qval pointing to the first "q=" attribute or NULL if not found */ |
Thierry FOURNIER | ad90351 | 2014-04-11 17:51:01 +0200 | [diff] [blame] | 2253 | q = qval ? parse_qvalue(qval + 2, NULL) : 1000; |
Willy Tarreau | 0e9b1b4 | 2014-03-19 12:07:52 +0100 | [diff] [blame] | 2254 | |
| 2255 | if (q <= best_q) |
| 2256 | continue; |
| 2257 | |
| 2258 | for (comp_algo = comp_algo_back; comp_algo; comp_algo = comp_algo->next) { |
| 2259 | if (*(ctx.line + ctx.val) == '*' || |
| 2260 | word_match(ctx.line + ctx.val, toklen, comp_algo->name, comp_algo->name_len)) { |
| 2261 | s->comp_algo = comp_algo; |
| 2262 | best_q = q; |
| 2263 | break; |
| 2264 | } |
| 2265 | } |
| 2266 | } |
| 2267 | } |
| 2268 | |
| 2269 | /* remove all occurrences of the header when "compression offload" is set */ |
| 2270 | if (s->comp_algo) { |
| 2271 | if ((s->be->comp && s->be->comp->offload) || (s->fe->comp && s->fe->comp->offload)) { |
| 2272 | http_remove_header2(msg, &txn->hdr_idx, &ctx); |
| 2273 | ctx.idx = 0; |
| 2274 | while (http_find_header2("Accept-Encoding", 15, req->p, &txn->hdr_idx, &ctx)) { |
| 2275 | http_remove_header2(msg, &txn->hdr_idx, &ctx); |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 2276 | } |
| 2277 | } |
Willy Tarreau | 0e9b1b4 | 2014-03-19 12:07:52 +0100 | [diff] [blame] | 2278 | return 1; |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 2279 | } |
| 2280 | |
| 2281 | /* identity is implicit does not require headers */ |
Willy Tarreau | 3c7b97b | 2012-10-26 14:50:26 +0200 | [diff] [blame] | 2282 | if ((s->be->comp && (comp_algo_back = s->be->comp->algos)) || (s->fe->comp && (comp_algo_back = s->fe->comp->algos))) { |
| 2283 | 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] | 2284 | if (comp_algo->add_data == identity_add_data) { |
| 2285 | s->comp_algo = comp_algo; |
| 2286 | return 1; |
| 2287 | } |
| 2288 | } |
| 2289 | } |
| 2290 | |
| 2291 | s->comp_algo = NULL; |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 2292 | return 0; |
| 2293 | } |
| 2294 | |
| 2295 | /* |
| 2296 | * Selects a comression algorithm depending of the server response. |
| 2297 | */ |
| 2298 | int select_compression_response_header(struct session *s, struct buffer *res) |
| 2299 | { |
| 2300 | struct http_txn *txn = &s->txn; |
| 2301 | struct http_msg *msg = &txn->rsp; |
| 2302 | struct hdr_ctx ctx; |
| 2303 | struct comp_type *comp_type; |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 2304 | |
| 2305 | /* no common compression algorithm was found in request header */ |
| 2306 | if (s->comp_algo == NULL) |
| 2307 | goto fail; |
| 2308 | |
| 2309 | /* HTTP < 1.1 should not be compressed */ |
Willy Tarreau | 7257550 | 2013-12-24 14:41:35 +0100 | [diff] [blame] | 2310 | 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] | 2311 | goto fail; |
| 2312 | |
William Lallemand | d300261 | 2012-11-26 14:34:47 +0100 | [diff] [blame] | 2313 | /* 200 only */ |
| 2314 | if (txn->status != 200) |
| 2315 | goto fail; |
| 2316 | |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 2317 | /* Content-Length is null */ |
| 2318 | if (!(msg->flags & HTTP_MSGF_TE_CHNK) && msg->body_len == 0) |
| 2319 | goto fail; |
| 2320 | |
Willy Tarreau | 667c2a3 | 2013-04-09 08:13:58 +0200 | [diff] [blame] | 2321 | /* TEMPORARY WORKAROUND: do not compress if response is chunked !!!!!! */ |
| 2322 | if (msg->flags & HTTP_MSGF_TE_CHNK) |
| 2323 | goto fail; |
| 2324 | |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 2325 | /* content is already compressed */ |
Willy Tarreau | 0a80a8d | 2012-11-26 16:33:37 +0100 | [diff] [blame] | 2326 | ctx.idx = 0; |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 2327 | if (http_find_header2("Content-Encoding", 16, res->p, &txn->hdr_idx, &ctx)) |
| 2328 | goto fail; |
| 2329 | |
Willy Tarreau | 56e9ffa | 2013-01-05 16:20:35 +0100 | [diff] [blame] | 2330 | /* no compression when Cache-Control: no-transform is present in the message */ |
| 2331 | ctx.idx = 0; |
| 2332 | while (http_find_header2("Cache-Control", 13, res->p, &txn->hdr_idx, &ctx)) { |
| 2333 | if (word_match(ctx.line + ctx.val, ctx.vlen, "no-transform", 12)) |
| 2334 | goto fail; |
| 2335 | } |
| 2336 | |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 2337 | comp_type = NULL; |
| 2338 | |
Willy Tarreau | 0a80a8d | 2012-11-26 16:33:37 +0100 | [diff] [blame] | 2339 | /* we don't want to compress multipart content-types, nor content-types that are |
| 2340 | * not listed in the "compression type" directive if any. If no content-type was |
| 2341 | * found but configuration requires one, we don't compress either. Backend has |
| 2342 | * the priority. |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 2343 | */ |
Willy Tarreau | 0a80a8d | 2012-11-26 16:33:37 +0100 | [diff] [blame] | 2344 | ctx.idx = 0; |
| 2345 | if (http_find_header2("Content-Type", 12, res->p, &txn->hdr_idx, &ctx)) { |
| 2346 | if (ctx.vlen >= 9 && strncasecmp("multipart", ctx.line+ctx.val, 9) == 0) |
| 2347 | goto fail; |
| 2348 | |
| 2349 | if ((s->be->comp && (comp_type = s->be->comp->types)) || |
| 2350 | (s->fe->comp && (comp_type = s->fe->comp->types))) { |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 2351 | for (; comp_type; comp_type = comp_type->next) { |
Willy Tarreau | 0a80a8d | 2012-11-26 16:33:37 +0100 | [diff] [blame] | 2352 | if (ctx.vlen >= comp_type->name_len && |
| 2353 | 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] | 2354 | /* this Content-Type should be compressed */ |
| 2355 | break; |
| 2356 | } |
Willy Tarreau | 0a80a8d | 2012-11-26 16:33:37 +0100 | [diff] [blame] | 2357 | /* this Content-Type should not be compressed */ |
| 2358 | if (comp_type == NULL) |
| 2359 | goto fail; |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 2360 | } |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 2361 | } |
Willy Tarreau | 0a80a8d | 2012-11-26 16:33:37 +0100 | [diff] [blame] | 2362 | else { /* no content-type header */ |
| 2363 | if ((s->be->comp && s->be->comp->types) || (s->fe->comp && s->fe->comp->types)) |
| 2364 | goto fail; /* a content-type was required */ |
William Lallemand | d300261 | 2012-11-26 14:34:47 +0100 | [diff] [blame] | 2365 | } |
| 2366 | |
William Lallemand | d85f917 | 2012-11-09 17:05:39 +0100 | [diff] [blame] | 2367 | /* limit compression rate */ |
| 2368 | if (global.comp_rate_lim > 0) |
| 2369 | if (read_freq_ctr(&global.comp_bps_in) > global.comp_rate_lim) |
| 2370 | goto fail; |
| 2371 | |
William Lallemand | 072a2bf | 2012-11-20 17:01:01 +0100 | [diff] [blame] | 2372 | /* limit cpu usage */ |
| 2373 | if (idle_pct < compress_min_idle) |
| 2374 | goto fail; |
| 2375 | |
William Lallemand | 4c49fae | 2012-11-07 15:00:23 +0100 | [diff] [blame] | 2376 | /* initialize compression */ |
William Lallemand | f374783 | 2012-11-09 12:33:10 +0100 | [diff] [blame] | 2377 | 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] | 2378 | goto fail; |
| 2379 | |
William Lallemand | ec3e389 | 2012-11-12 17:02:18 +0100 | [diff] [blame] | 2380 | s->flags |= SN_COMP_READY; |
| 2381 | |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 2382 | /* remove Content-Length header */ |
Willy Tarreau | 0a80a8d | 2012-11-26 16:33:37 +0100 | [diff] [blame] | 2383 | ctx.idx = 0; |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 2384 | if ((msg->flags & HTTP_MSGF_CNT_LEN) && http_find_header2("Content-Length", 14, res->p, &txn->hdr_idx, &ctx)) |
| 2385 | http_remove_header2(msg, &txn->hdr_idx, &ctx); |
| 2386 | |
| 2387 | /* add Transfer-Encoding header */ |
| 2388 | if (!(msg->flags & HTTP_MSGF_TE_CHNK)) |
| 2389 | http_header_add_tail2(&txn->rsp, &txn->hdr_idx, "Transfer-Encoding: chunked", 26); |
| 2390 | |
| 2391 | /* |
| 2392 | * Add Content-Encoding header when it's not identity encoding. |
| 2393 | * RFC 2616 : Identity encoding: This content-coding is used only in the |
| 2394 | * Accept-Encoding header, and SHOULD NOT be used in the Content-Encoding |
| 2395 | * header. |
| 2396 | */ |
| 2397 | if (s->comp_algo->add_data != identity_add_data) { |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 2398 | trash.len = 18; |
| 2399 | memcpy(trash.str, "Content-Encoding: ", trash.len); |
| 2400 | memcpy(trash.str + trash.len, s->comp_algo->name, s->comp_algo->name_len); |
| 2401 | trash.len += s->comp_algo->name_len; |
| 2402 | trash.str[trash.len] = '\0'; |
| 2403 | 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] | 2404 | } |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 2405 | return 1; |
| 2406 | |
| 2407 | fail: |
Willy Tarreau | b97b619 | 2012-11-19 14:55:02 +0100 | [diff] [blame] | 2408 | s->comp_algo = NULL; |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 2409 | return 0; |
| 2410 | } |
| 2411 | |
| 2412 | |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 2413 | /* This stream analyser waits for a complete HTTP request. It returns 1 if the |
| 2414 | * processing can continue on next analysers, or zero if it either needs more |
| 2415 | * data or wants to immediately abort the request (eg: timeout, error, ...). It |
| 2416 | * is tied to AN_REQ_WAIT_HTTP and may may remove itself from s->req->analysers |
| 2417 | * when it has nothing left to do, and may remove any analyser when it wants to |
| 2418 | * abort. |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2419 | */ |
Willy Tarreau | 7421efb | 2012-07-02 15:11:27 +0200 | [diff] [blame] | 2420 | 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] | 2421 | { |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2422 | /* |
| 2423 | * We will parse the partial (or complete) lines. |
| 2424 | * We will check the request syntax, and also join multi-line |
| 2425 | * headers. An index of all the lines will be elaborated while |
| 2426 | * parsing. |
| 2427 | * |
| 2428 | * For the parsing, we use a 28 states FSM. |
| 2429 | * |
| 2430 | * Here is the information we currently have : |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2431 | * req->buf->p = beginning of request |
| 2432 | * req->buf->p + msg->eoh = end of processed headers / start of current one |
| 2433 | * req->buf->p + req->buf->i = end of input data |
Willy Tarreau | 2692736 | 2012-05-18 23:22:52 +0200 | [diff] [blame] | 2434 | * msg->eol = end of current header or line (LF or CRLF) |
| 2435 | * msg->next = first non-visited byte |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 2436 | * |
| 2437 | * At end of parsing, we may perform a capture of the error (if any), and |
| 2438 | * we will set a few fields (msg->sol, txn->meth, sn->flags/SN_REDIRECTABLE). |
| 2439 | * We also check for monitor-uri, logging, HTTP/0.9 to 1.0 conversion, and |
| 2440 | * finally headers capture. |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2441 | */ |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 2442 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2443 | int cur_idx; |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 2444 | int use_close_only; |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2445 | struct http_txn *txn = &s->txn; |
| 2446 | struct http_msg *msg = &txn->req; |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 2447 | struct hdr_ctx ctx; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 2448 | |
Willy Tarreau | 02d6cfc | 2012-03-01 18:19:58 +0100 | [diff] [blame] | 2449 | 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] | 2450 | now_ms, __FUNCTION__, |
| 2451 | s, |
| 2452 | req, |
| 2453 | req->rex, req->wex, |
| 2454 | req->flags, |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2455 | req->buf->i, |
Willy Tarreau | 6bf1736 | 2009-02-24 10:48:35 +0100 | [diff] [blame] | 2456 | req->analysers); |
| 2457 | |
Willy Tarreau | 52a0c60 | 2009-08-16 22:45:38 +0200 | [diff] [blame] | 2458 | /* we're speaking HTTP here, so let's speak HTTP to the client */ |
| 2459 | s->srv_error = http_return_srv_error; |
| 2460 | |
Willy Tarreau | 83e3af0 | 2009-12-28 17:39:57 +0100 | [diff] [blame] | 2461 | /* There's a protected area at the end of the buffer for rewriting |
| 2462 | * purposes. We don't want to start to parse the request if the |
| 2463 | * protected area is affected, because we may have to move processed |
| 2464 | * data later, which is much more complicated. |
| 2465 | */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2466 | if (buffer_not_empty(req->buf) && msg->msg_state < HTTP_MSG_ERROR) { |
Willy Tarreau | 379357a | 2013-06-08 12:55:46 +0200 | [diff] [blame] | 2467 | if (txn->flags & TX_NOT_FIRST) { |
| 2468 | if (unlikely(!channel_reserved(req))) { |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 2469 | 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] | 2470 | goto failed_keep_alive; |
Willy Tarreau | 2ab6eb1 | 2010-01-02 22:04:45 +0100 | [diff] [blame] | 2471 | /* 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] | 2472 | channel_dont_connect(req); |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 2473 | req->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */ |
Willy Tarreau | d7ad9f5 | 2013-12-31 17:26:25 +0100 | [diff] [blame] | 2474 | req->flags |= CF_WAKE_WRITE; |
Willy Tarreau | 2ab6eb1 | 2010-01-02 22:04:45 +0100 | [diff] [blame] | 2475 | return 0; |
| 2476 | } |
Willy Tarreau | 379357a | 2013-06-08 12:55:46 +0200 | [diff] [blame] | 2477 | if (unlikely(bi_end(req->buf) < b_ptr(req->buf, msg->next) || |
| 2478 | bi_end(req->buf) > req->buf->data + req->buf->size - global.tune.maxrewrite)) |
| 2479 | buffer_slow_realign(req->buf); |
Willy Tarreau | 83e3af0 | 2009-12-28 17:39:57 +0100 | [diff] [blame] | 2480 | } |
| 2481 | |
Willy Tarreau | 065e833 | 2010-01-08 00:30:20 +0100 | [diff] [blame] | 2482 | /* Note that we have the same problem with the response ; we |
| 2483 | * may want to send a redirect, error or anything which requires |
| 2484 | * some spare space. So we'll ensure that we have at least |
| 2485 | * maxrewrite bytes available in the response buffer before |
| 2486 | * processing that one. This will only affect pipelined |
| 2487 | * keep-alive requests. |
| 2488 | */ |
| 2489 | if ((txn->flags & TX_NOT_FIRST) && |
Willy Tarreau | 379357a | 2013-06-08 12:55:46 +0200 | [diff] [blame] | 2490 | unlikely(!channel_reserved(s->rep) || |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2491 | bi_end(s->rep->buf) < b_ptr(s->rep->buf, txn->rsp.next) || |
| 2492 | bi_end(s->rep->buf) > s->rep->buf->data + s->rep->buf->size - global.tune.maxrewrite)) { |
| 2493 | if (s->rep->buf->o) { |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 2494 | 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] | 2495 | goto failed_keep_alive; |
Willy Tarreau | 065e833 | 2010-01-08 00:30:20 +0100 | [diff] [blame] | 2496 | /* don't let a connection request be initiated */ |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 2497 | channel_dont_connect(req); |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 2498 | s->rep->flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */ |
Willy Tarreau | d7ad9f5 | 2013-12-31 17:26:25 +0100 | [diff] [blame] | 2499 | s->rep->flags |= CF_WAKE_WRITE; |
Willy Tarreau | 0499e35 | 2010-12-17 07:13:42 +0100 | [diff] [blame] | 2500 | s->rep->analysers |= an_bit; /* wake us up once it changes */ |
Willy Tarreau | 065e833 | 2010-01-08 00:30:20 +0100 | [diff] [blame] | 2501 | return 0; |
| 2502 | } |
| 2503 | } |
| 2504 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2505 | if (likely(msg->next < req->buf->i)) /* some unparsed data are available */ |
Willy Tarreau | a560c21 | 2012-03-09 13:50:57 +0100 | [diff] [blame] | 2506 | http_msg_analyzer(msg, &txn->hdr_idx); |
Willy Tarreau | 83e3af0 | 2009-12-28 17:39:57 +0100 | [diff] [blame] | 2507 | } |
| 2508 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2509 | /* 1: we might have to print this header in debug mode */ |
| 2510 | if (unlikely((global.mode & MODE_DEBUG) && |
| 2511 | (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) && |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 2512 | (msg->msg_state >= HTTP_MSG_BODY || msg->msg_state == HTTP_MSG_ERROR))) { |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2513 | char *eol, *sol; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2514 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2515 | sol = req->buf->p; |
Willy Tarreau | e92693a | 2012-09-24 21:13:39 +0200 | [diff] [blame] | 2516 | /* this is a bit complex : in case of error on the request line, |
| 2517 | * we know that rq.l is still zero, so we display only the part |
| 2518 | * up to the end of the line (truncated by debug_hdr). |
| 2519 | */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2520 | 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] | 2521 | debug_hdr("clireq", s, sol, eol); |
Willy Tarreau | 45e73e3 | 2006-12-17 00:05:15 +0100 | [diff] [blame] | 2522 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2523 | sol += hdr_idx_first_pos(&txn->hdr_idx); |
| 2524 | cur_idx = hdr_idx_first_idx(&txn->hdr_idx); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2525 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2526 | while (cur_idx) { |
| 2527 | eol = sol + txn->hdr_idx.v[cur_idx].len; |
| 2528 | debug_hdr("clihdr", s, sol, eol); |
| 2529 | sol = eol + txn->hdr_idx.v[cur_idx].cr + 1; |
| 2530 | cur_idx = txn->hdr_idx.v[cur_idx].next; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2531 | } |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2532 | } |
| 2533 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2534 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2535 | /* |
| 2536 | * Now we quickly check if we have found a full valid request. |
| 2537 | * If not so, we check the FD and buffer states before leaving. |
| 2538 | * A full request is indicated by the fact that we have seen |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 2539 | * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid |
Willy Tarreau | d3c343f | 2010-01-16 10:26:19 +0100 | [diff] [blame] | 2540 | * requests are checked first. When waiting for a second request |
| 2541 | * on a keep-alive session, if we encounter and error, close, t/o, |
| 2542 | * we note the error in the session flags but don't set any state. |
| 2543 | * Since the error will be noted there, it will not be counted by |
| 2544 | * process_session() as a frontend error. |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 2545 | * Last, we may increase some tracked counters' http request errors on |
| 2546 | * the cases that are deliberately the client's fault. For instance, |
| 2547 | * a timeout or connection reset is not counted as an error. However |
| 2548 | * a bad request is. |
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 | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 2551 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2552 | /* |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2553 | * First, let's catch bad requests. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2554 | */ |
Willy Tarreau | 3e1b6d1 | 2010-03-04 23:02:38 +0100 | [diff] [blame] | 2555 | if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) { |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 2556 | session_inc_http_req_ctr(s); |
| 2557 | session_inc_http_err_ctr(s); |
Willy Tarreau | 3e1b6d1 | 2010-03-04 23:02:38 +0100 | [diff] [blame] | 2558 | proxy_inc_fe_req_ctr(s->fe); |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2559 | goto return_bad_req; |
Willy Tarreau | 3e1b6d1 | 2010-03-04 23:02:38 +0100 | [diff] [blame] | 2560 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2561 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2562 | /* 1: Since we are in header mode, if there's no space |
| 2563 | * left for headers, we won't be able to free more |
| 2564 | * later, so the session will never terminate. We |
| 2565 | * must terminate it now. |
| 2566 | */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2567 | if (unlikely(buffer_full(req->buf, global.tune.maxrewrite))) { |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2568 | /* FIXME: check if URI is set and return Status |
| 2569 | * 414 Request URI too long instead. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2570 | */ |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 2571 | session_inc_http_req_ctr(s); |
| 2572 | session_inc_http_err_ctr(s); |
Willy Tarreau | 3e1b6d1 | 2010-03-04 23:02:38 +0100 | [diff] [blame] | 2573 | proxy_inc_fe_req_ctr(s->fe); |
Willy Tarreau | fec4d89 | 2011-09-02 20:04:57 +0200 | [diff] [blame] | 2574 | if (msg->err_pos < 0) |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2575 | msg->err_pos = req->buf->i; |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2576 | goto return_bad_req; |
| 2577 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2578 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2579 | /* 2: have we encountered a read error ? */ |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 2580 | else if (req->flags & CF_READ_ERROR) { |
Willy Tarreau | d3c343f | 2010-01-16 10:26:19 +0100 | [diff] [blame] | 2581 | if (!(s->flags & SN_ERR_MASK)) |
| 2582 | s->flags |= SN_ERR_CLICL; |
| 2583 | |
Willy Tarreau | fcffa69 | 2010-01-10 14:21:19 +0100 | [diff] [blame] | 2584 | if (txn->flags & TX_WAIT_NEXT_RQ) |
Willy Tarreau | b608feb | 2010-01-02 22:47:18 +0100 | [diff] [blame] | 2585 | goto failed_keep_alive; |
| 2586 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2587 | /* we cannot return any message on error */ |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 2588 | if (msg->err_pos >= 0) { |
Willy Tarreau | 8a0cef2 | 2012-03-09 13:39:23 +0100 | [diff] [blame] | 2589 | 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] | 2590 | session_inc_http_err_ctr(s); |
| 2591 | } |
| 2592 | |
Willy Tarreau | dc979f2 | 2012-12-04 10:39:01 +0100 | [diff] [blame] | 2593 | txn->status = 400; |
| 2594 | stream_int_retnclose(req->prod, NULL); |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2595 | msg->msg_state = HTTP_MSG_ERROR; |
| 2596 | req->analysers = 0; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 2597 | |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 2598 | session_inc_http_req_ctr(s); |
Willy Tarreau | 3e1b6d1 | 2010-03-04 23:02:38 +0100 | [diff] [blame] | 2599 | proxy_inc_fe_req_ctr(s->fe); |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 2600 | s->fe->fe_counters.failed_req++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 2601 | if (s->listener->counters) |
| 2602 | s->listener->counters->failed_req++; |
| 2603 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2604 | if (!(s->flags & SN_FINST_MASK)) |
| 2605 | s->flags |= SN_FINST_R; |
| 2606 | return 0; |
| 2607 | } |
Willy Tarreau | f9839bd | 2008-08-27 23:57:16 +0200 | [diff] [blame] | 2608 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2609 | /* 3: has the read timeout expired ? */ |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 2610 | 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] | 2611 | if (!(s->flags & SN_ERR_MASK)) |
| 2612 | s->flags |= SN_ERR_CLITO; |
| 2613 | |
Willy Tarreau | fcffa69 | 2010-01-10 14:21:19 +0100 | [diff] [blame] | 2614 | if (txn->flags & TX_WAIT_NEXT_RQ) |
Willy Tarreau | b608feb | 2010-01-02 22:47:18 +0100 | [diff] [blame] | 2615 | goto failed_keep_alive; |
| 2616 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2617 | /* read timeout : give up with an error message. */ |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 2618 | if (msg->err_pos >= 0) { |
Willy Tarreau | 8a0cef2 | 2012-03-09 13:39:23 +0100 | [diff] [blame] | 2619 | 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] | 2620 | session_inc_http_err_ctr(s); |
| 2621 | } |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2622 | txn->status = 408; |
Willy Tarreau | 783f258 | 2012-09-04 12:19:04 +0200 | [diff] [blame] | 2623 | stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_408)); |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2624 | msg->msg_state = HTTP_MSG_ERROR; |
| 2625 | req->analysers = 0; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 2626 | |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 2627 | session_inc_http_req_ctr(s); |
Willy Tarreau | 3e1b6d1 | 2010-03-04 23:02:38 +0100 | [diff] [blame] | 2628 | proxy_inc_fe_req_ctr(s->fe); |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 2629 | s->fe->fe_counters.failed_req++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 2630 | if (s->listener->counters) |
| 2631 | s->listener->counters->failed_req++; |
| 2632 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2633 | if (!(s->flags & SN_FINST_MASK)) |
| 2634 | s->flags |= SN_FINST_R; |
| 2635 | return 0; |
| 2636 | } |
Willy Tarreau | c65a3ba | 2008-08-11 23:42:50 +0200 | [diff] [blame] | 2637 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2638 | /* 4: have we encountered a close ? */ |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 2639 | else if (req->flags & CF_SHUTR) { |
Willy Tarreau | d3c343f | 2010-01-16 10:26:19 +0100 | [diff] [blame] | 2640 | if (!(s->flags & SN_ERR_MASK)) |
| 2641 | s->flags |= SN_ERR_CLICL; |
| 2642 | |
Willy Tarreau | fcffa69 | 2010-01-10 14:21:19 +0100 | [diff] [blame] | 2643 | if (txn->flags & TX_WAIT_NEXT_RQ) |
Willy Tarreau | b608feb | 2010-01-02 22:47:18 +0100 | [diff] [blame] | 2644 | goto failed_keep_alive; |
| 2645 | |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 2646 | if (msg->err_pos >= 0) |
Willy Tarreau | 8a0cef2 | 2012-03-09 13:39:23 +0100 | [diff] [blame] | 2647 | 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] | 2648 | txn->status = 400; |
Willy Tarreau | 783f258 | 2012-09-04 12:19:04 +0200 | [diff] [blame] | 2649 | stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_400)); |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2650 | msg->msg_state = HTTP_MSG_ERROR; |
| 2651 | req->analysers = 0; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 2652 | |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 2653 | session_inc_http_err_ctr(s); |
| 2654 | session_inc_http_req_ctr(s); |
Willy Tarreau | 3e1b6d1 | 2010-03-04 23:02:38 +0100 | [diff] [blame] | 2655 | proxy_inc_fe_req_ctr(s->fe); |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 2656 | s->fe->fe_counters.failed_req++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 2657 | if (s->listener->counters) |
| 2658 | s->listener->counters->failed_req++; |
| 2659 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2660 | if (!(s->flags & SN_FINST_MASK)) |
| 2661 | s->flags |= SN_FINST_R; |
Willy Tarreau | dafde43 | 2008-08-17 01:00:46 +0200 | [diff] [blame] | 2662 | return 0; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2663 | } |
| 2664 | |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 2665 | channel_dont_connect(req); |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 2666 | req->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */ |
| 2667 | s->rep->flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */ |
Willy Tarreau | 5e20552 | 2011-12-17 16:34:27 +0100 | [diff] [blame] | 2668 | #ifdef TCP_QUICKACK |
Willy Tarreau | 3c72872 | 2014-01-23 13:50:42 +0100 | [diff] [blame] | 2669 | if (s->listener->options & LI_O_NOQUICKACK && req->buf->i && objt_conn(s->req->prod->end) && conn_ctrl_ready(__objt_conn(s->req->prod->end))) { |
Willy Tarreau | 5e20552 | 2011-12-17 16:34:27 +0100 | [diff] [blame] | 2670 | /* We need more data, we have to re-enable quick-ack in case we |
| 2671 | * previously disabled it, otherwise we might cause the client |
| 2672 | * to delay next data. |
| 2673 | */ |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 2674 | 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] | 2675 | } |
| 2676 | #endif |
Willy Tarreau | 1b194fe | 2009-03-21 21:10:04 +0100 | [diff] [blame] | 2677 | |
Willy Tarreau | fcffa69 | 2010-01-10 14:21:19 +0100 | [diff] [blame] | 2678 | if ((msg->msg_state != HTTP_MSG_RQBEFORE) && (txn->flags & TX_WAIT_NEXT_RQ)) { |
| 2679 | /* If the client starts to talk, let's fall back to |
| 2680 | * request timeout processing. |
| 2681 | */ |
| 2682 | txn->flags &= ~TX_WAIT_NEXT_RQ; |
Willy Tarreau | b16a574 | 2010-01-10 14:46:16 +0100 | [diff] [blame] | 2683 | req->analyse_exp = TICK_ETERNITY; |
Willy Tarreau | fcffa69 | 2010-01-10 14:21:19 +0100 | [diff] [blame] | 2684 | } |
| 2685 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2686 | /* just set the request timeout once at the beginning of the request */ |
Willy Tarreau | b16a574 | 2010-01-10 14:46:16 +0100 | [diff] [blame] | 2687 | if (!tick_isset(req->analyse_exp)) { |
| 2688 | if ((msg->msg_state == HTTP_MSG_RQBEFORE) && |
| 2689 | (txn->flags & TX_WAIT_NEXT_RQ) && |
| 2690 | tick_isset(s->be->timeout.httpka)) |
| 2691 | req->analyse_exp = tick_add(now_ms, s->be->timeout.httpka); |
| 2692 | else |
| 2693 | req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.httpreq); |
| 2694 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2695 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2696 | /* we're not ready yet */ |
| 2697 | return 0; |
Willy Tarreau | b608feb | 2010-01-02 22:47:18 +0100 | [diff] [blame] | 2698 | |
| 2699 | failed_keep_alive: |
| 2700 | /* Here we process low-level errors for keep-alive requests. In |
| 2701 | * short, if the request is not the first one and it experiences |
| 2702 | * a timeout, read error or shutdown, we just silently close so |
| 2703 | * that the client can try again. |
| 2704 | */ |
| 2705 | txn->status = 0; |
| 2706 | msg->msg_state = HTTP_MSG_RQBEFORE; |
| 2707 | req->analysers = 0; |
| 2708 | s->logs.logwait = 0; |
Willy Tarreau | abcd514 | 2013-06-11 17:18:02 +0200 | [diff] [blame] | 2709 | s->logs.level = 0; |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 2710 | s->rep->flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */ |
Willy Tarreau | 148d099 | 2010-01-10 10:21:21 +0100 | [diff] [blame] | 2711 | stream_int_retnclose(req->prod, NULL); |
Willy Tarreau | b608feb | 2010-01-02 22:47:18 +0100 | [diff] [blame] | 2712 | return 0; |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2713 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2714 | |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 2715 | /* OK now we have a complete HTTP request with indexed headers. Let's |
| 2716 | * complete the request parsing by setting a few fields we will need |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2717 | * 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] | 2718 | * 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] | 2719 | * 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] | 2720 | * byte after the last LF. msg->sov points to the first byte of data. |
| 2721 | * msg->eol cannot be trusted because it may have been left uninitialized |
| 2722 | * (for instance in the absence of headers). |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 2723 | */ |
Willy Tarreau | 9cdde23 | 2007-05-02 20:58:19 +0200 | [diff] [blame] | 2724 | |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 2725 | session_inc_http_req_ctr(s); |
Willy Tarreau | d9b587f | 2010-02-26 10:05:55 +0100 | [diff] [blame] | 2726 | proxy_inc_fe_req_ctr(s->fe); /* one more valid request for this FE */ |
| 2727 | |
Willy Tarreau | b16a574 | 2010-01-10 14:46:16 +0100 | [diff] [blame] | 2728 | if (txn->flags & TX_WAIT_NEXT_RQ) { |
| 2729 | /* kill the pending keep-alive timeout */ |
| 2730 | txn->flags &= ~TX_WAIT_NEXT_RQ; |
| 2731 | req->analyse_exp = TICK_ETERNITY; |
| 2732 | } |
| 2733 | |
| 2734 | |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 2735 | /* Maybe we found in invalid header name while we were configured not |
| 2736 | * to block on that, so we have to capture it now. |
| 2737 | */ |
| 2738 | if (unlikely(msg->err_pos >= 0)) |
Willy Tarreau | 8a0cef2 | 2012-03-09 13:39:23 +0100 | [diff] [blame] | 2739 | 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] | 2740 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2741 | /* |
| 2742 | * 1: identify the method |
| 2743 | */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2744 | 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] | 2745 | |
| 2746 | /* we can make use of server redirect on GET and HEAD */ |
| 2747 | if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD) |
| 2748 | s->flags |= SN_REDIRECTABLE; |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 2749 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2750 | /* |
| 2751 | * 2: check if the URI matches the monitor_uri. |
| 2752 | * We have to do this for every request which gets in, because |
| 2753 | * the monitor-uri is defined by the frontend. |
| 2754 | */ |
| 2755 | if (unlikely((s->fe->monitor_uri_len != 0) && |
| 2756 | (s->fe->monitor_uri_len == msg->sl.rq.u_l) && |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2757 | !memcmp(req->buf->p + msg->sl.rq.u, |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2758 | s->fe->monitor_uri, |
| 2759 | s->fe->monitor_uri_len))) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2760 | /* |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2761 | * We have found the monitor URI |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2762 | */ |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2763 | struct acl_cond *cond; |
Willy Tarreau | b80c230 | 2007-11-30 20:51:32 +0100 | [diff] [blame] | 2764 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2765 | s->flags |= SN_MONITOR; |
Willy Tarreau | eabea07 | 2011-09-10 23:29:44 +0200 | [diff] [blame] | 2766 | s->fe->fe_counters.intercepted_req++; |
Willy Tarreau | b80c230 | 2007-11-30 20:51:32 +0100 | [diff] [blame] | 2767 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2768 | /* Check if we want to fail this monitor request or not */ |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 2769 | list_for_each_entry(cond, &s->fe->mon_fail_cond, list) { |
Willy Tarreau | 32a6f2e | 2012-04-25 10:13:36 +0200 | [diff] [blame] | 2770 | 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] | 2771 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2772 | ret = acl_pass(ret); |
| 2773 | if (cond->pol == ACL_COND_UNLESS) |
| 2774 | ret = !ret; |
Willy Tarreau | b80c230 | 2007-11-30 20:51:32 +0100 | [diff] [blame] | 2775 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2776 | if (ret) { |
| 2777 | /* we fail this request, let's return 503 service unavail */ |
| 2778 | txn->status = 503; |
Willy Tarreau | 783f258 | 2012-09-04 12:19:04 +0200 | [diff] [blame] | 2779 | stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_503)); |
Willy Tarreau | 570f221 | 2013-06-10 16:42:09 +0200 | [diff] [blame] | 2780 | if (!(s->flags & SN_ERR_MASK)) |
| 2781 | 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] | 2782 | goto return_prx_cond; |
Willy Tarreau | b80c230 | 2007-11-30 20:51:32 +0100 | [diff] [blame] | 2783 | } |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2784 | } |
Willy Tarreau | a5555ec | 2008-11-30 19:02:32 +0100 | [diff] [blame] | 2785 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2786 | /* nothing to fail, let's reply normaly */ |
| 2787 | txn->status = 200; |
Willy Tarreau | 783f258 | 2012-09-04 12:19:04 +0200 | [diff] [blame] | 2788 | stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_200)); |
Willy Tarreau | 570f221 | 2013-06-10 16:42:09 +0200 | [diff] [blame] | 2789 | if (!(s->flags & SN_ERR_MASK)) |
| 2790 | 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] | 2791 | goto return_prx_cond; |
| 2792 | } |
| 2793 | |
| 2794 | /* |
| 2795 | * 3: Maybe we have to copy the original REQURI for the logs ? |
| 2796 | * Note: we cannot log anymore if the request has been |
| 2797 | * classified as invalid. |
| 2798 | */ |
| 2799 | if (unlikely(s->logs.logwait & LW_REQ)) { |
| 2800 | /* we have a complete HTTP request that we must log */ |
| 2801 | if ((txn->uri = pool_alloc2(pool2_requri)) != NULL) { |
| 2802 | int urilen = msg->sl.rq.l; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2803 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2804 | if (urilen >= REQURI_LEN) |
| 2805 | urilen = REQURI_LEN - 1; |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2806 | memcpy(txn->uri, req->buf->p, urilen); |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2807 | txn->uri[urilen] = 0; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2808 | |
Willy Tarreau | d79a3b2 | 2012-12-28 09:40:16 +0100 | [diff] [blame] | 2809 | if (!(s->logs.logwait &= ~(LW_REQ|LW_INIT))) |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2810 | s->do_log(s); |
| 2811 | } else { |
| 2812 | Alert("HTTP logging : out of memory.\n"); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2813 | } |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2814 | } |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 2815 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2816 | /* 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] | 2817 | 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] | 2818 | goto return_bad_req; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2819 | |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 2820 | /* ... and check if the request is HTTP/1.1 or above */ |
| 2821 | if ((msg->sl.rq.v_l == 8) && |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2822 | ((req->buf->p[msg->sl.rq.v + 5] > '1') || |
| 2823 | ((req->buf->p[msg->sl.rq.v + 5] == '1') && |
| 2824 | (req->buf->p[msg->sl.rq.v + 7] >= '1')))) |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 2825 | msg->flags |= HTTP_MSGF_VER_11; |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 2826 | |
| 2827 | /* "connection" has not been parsed yet */ |
Willy Tarreau | 50fc777 | 2012-11-11 22:19:57 +0100 | [diff] [blame] | 2828 | 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] | 2829 | |
Willy Tarreau | 88d349d | 2010-01-25 12:15:43 +0100 | [diff] [blame] | 2830 | /* if the frontend has "option http-use-proxy-header", we'll check if |
| 2831 | * we have what looks like a proxied connection instead of a connection, |
| 2832 | * and in this case set the TX_USE_PX_CONN flag to use Proxy-connection. |
| 2833 | * Note that this is *not* RFC-compliant, however browsers and proxies |
| 2834 | * happen to do that despite being non-standard :-( |
| 2835 | * We consider that a request not beginning with either '/' or '*' is |
| 2836 | * a proxied connection, which covers both "scheme://location" and |
| 2837 | * CONNECT ip:port. |
| 2838 | */ |
| 2839 | if ((s->fe->options2 & PR_O2_USE_PXHDR) && |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2840 | 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] | 2841 | txn->flags |= TX_USE_PX_CONN; |
| 2842 | |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 2843 | /* transfer length unknown*/ |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 2844 | msg->flags &= ~HTTP_MSGF_XFER_LEN; |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 2845 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2846 | /* 5: we may need to capture headers */ |
Willy Tarreau | 42f7d89 | 2012-03-24 08:28:09 +0100 | [diff] [blame] | 2847 | if (unlikely((s->logs.logwait & LW_REQHDR) && txn->req.cap)) |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2848 | capture_headers(req->buf->p, &txn->hdr_idx, |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2849 | txn->req.cap, s->fe->req_cap); |
Willy Tarreau | 1138281 | 2008-07-09 16:18:21 +0200 | [diff] [blame] | 2850 | |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 2851 | /* 6: determine the transfer-length. |
| 2852 | * According to RFC2616 #4.4, amended by the HTTPbis working group, |
| 2853 | * the presence of a message-body in a REQUEST and its transfer length |
| 2854 | * must be determined that way (in order of precedence) : |
| 2855 | * 1. The presence of a message-body in a request is signaled by the |
| 2856 | * inclusion of a Content-Length or Transfer-Encoding header field |
| 2857 | * in the request's header fields. When a request message contains |
| 2858 | * both a message-body of non-zero length and a method that does |
| 2859 | * not define any semantics for that request message-body, then an |
| 2860 | * origin server SHOULD either ignore the message-body or respond |
| 2861 | * with an appropriate error message (e.g., 413). A proxy or |
| 2862 | * gateway, when presented the same request, SHOULD either forward |
| 2863 | * the request inbound with the message- body or ignore the |
| 2864 | * message-body when determining a response. |
| 2865 | * |
| 2866 | * 2. If a Transfer-Encoding header field (Section 9.7) is present |
| 2867 | * and the "chunked" transfer-coding (Section 6.2) is used, the |
| 2868 | * transfer-length is defined by the use of this transfer-coding. |
| 2869 | * If a Transfer-Encoding header field is present and the "chunked" |
| 2870 | * transfer-coding is not present, the transfer-length is defined |
| 2871 | * by the sender closing the connection. |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 2872 | * |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 2873 | * 3. If a Content-Length header field is present, its decimal value in |
| 2874 | * OCTETs represents both the entity-length and the transfer-length. |
| 2875 | * If a message is received with both a Transfer-Encoding header |
| 2876 | * field and a Content-Length header field, the latter MUST be ignored. |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 2877 | * |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 2878 | * 4. By the server closing the connection. (Closing the connection |
| 2879 | * cannot be used to indicate the end of a request body, since that |
| 2880 | * would leave no possibility for the server to send back a response.) |
| 2881 | * |
| 2882 | * Whenever a transfer-coding is applied to a message-body, the set of |
| 2883 | * transfer-codings MUST include "chunked", unless the message indicates |
| 2884 | * it is terminated by closing the connection. When the "chunked" |
| 2885 | * transfer-coding is used, it MUST be the last transfer-coding applied |
| 2886 | * to the message-body. |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 2887 | */ |
| 2888 | |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 2889 | use_close_only = 0; |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 2890 | ctx.idx = 0; |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 2891 | /* set TE_CHNK and XFER_LEN only if "chunked" is seen last */ |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 2892 | while ((msg->flags & HTTP_MSGF_VER_11) && |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2893 | 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] | 2894 | if (ctx.vlen == 7 && strncasecmp(ctx.line + ctx.val, "chunked", 7) == 0) |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 2895 | msg->flags |= (HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN); |
| 2896 | else if (msg->flags & HTTP_MSGF_TE_CHNK) { |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 2897 | /* bad transfer-encoding (chunked followed by something else) */ |
| 2898 | use_close_only = 1; |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 2899 | msg->flags &= ~(HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN); |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 2900 | break; |
| 2901 | } |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 2902 | } |
| 2903 | |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 2904 | ctx.idx = 0; |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 2905 | while (!(msg->flags & HTTP_MSGF_TE_CHNK) && !use_close_only && |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2906 | 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] | 2907 | signed long long cl; |
| 2908 | |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 2909 | if (!ctx.vlen) { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2910 | msg->err_pos = ctx.line + ctx.val - req->buf->p; |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 2911 | goto return_bad_req; |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 2912 | } |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 2913 | |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 2914 | if (strl2llrc(ctx.line + ctx.val, ctx.vlen, &cl)) { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2915 | msg->err_pos = ctx.line + ctx.val - req->buf->p; |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 2916 | goto return_bad_req; /* parse failure */ |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 2917 | } |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 2918 | |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 2919 | if (cl < 0) { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2920 | msg->err_pos = ctx.line + ctx.val - req->buf->p; |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 2921 | goto return_bad_req; |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 2922 | } |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 2923 | |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 2924 | if ((msg->flags & HTTP_MSGF_CNT_LEN) && (msg->chunk_len != cl)) { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2925 | msg->err_pos = ctx.line + ctx.val - req->buf->p; |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 2926 | goto return_bad_req; /* already specified, was different */ |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 2927 | } |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 2928 | |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 2929 | msg->flags |= HTTP_MSGF_CNT_LEN | HTTP_MSGF_XFER_LEN; |
Willy Tarreau | 124d991 | 2011-03-01 20:30:48 +0100 | [diff] [blame] | 2930 | msg->body_len = msg->chunk_len = cl; |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 2931 | } |
| 2932 | |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 2933 | /* bodyless requests have a known length */ |
| 2934 | if (!use_close_only) |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 2935 | msg->flags |= HTTP_MSGF_XFER_LEN; |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 2936 | |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 2937 | /* end of job, return OK */ |
Willy Tarreau | 3a81629 | 2009-07-07 10:55:49 +0200 | [diff] [blame] | 2938 | req->analysers &= ~an_bit; |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 2939 | req->analyse_exp = TICK_ETERNITY; |
| 2940 | return 1; |
| 2941 | |
| 2942 | return_bad_req: |
| 2943 | /* We centralize bad requests processing here */ |
| 2944 | if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) { |
| 2945 | /* we detected a parsing error. We want to archive this request |
| 2946 | * in the dedicated proxy area for later troubleshooting. |
| 2947 | */ |
Willy Tarreau | 8a0cef2 | 2012-03-09 13:39:23 +0100 | [diff] [blame] | 2948 | 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] | 2949 | } |
| 2950 | |
| 2951 | txn->req.msg_state = HTTP_MSG_ERROR; |
| 2952 | txn->status = 400; |
Willy Tarreau | 783f258 | 2012-09-04 12:19:04 +0200 | [diff] [blame] | 2953 | 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] | 2954 | |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 2955 | s->fe->fe_counters.failed_req++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 2956 | if (s->listener->counters) |
| 2957 | s->listener->counters->failed_req++; |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 2958 | |
| 2959 | return_prx_cond: |
| 2960 | if (!(s->flags & SN_ERR_MASK)) |
| 2961 | s->flags |= SN_ERR_PRXCOND; |
| 2962 | if (!(s->flags & SN_FINST_MASK)) |
| 2963 | s->flags |= SN_FINST_R; |
| 2964 | |
| 2965 | req->analysers = 0; |
| 2966 | req->analyse_exp = TICK_ETERNITY; |
| 2967 | return 0; |
| 2968 | } |
| 2969 | |
Willy Tarreau | 4f8a83c | 2012-06-04 00:26:23 +0200 | [diff] [blame] | 2970 | |
Willy Tarreau | 347a35d | 2013-11-22 17:51:09 +0100 | [diff] [blame] | 2971 | /* This function prepares an applet to handle the stats. It can deal with the |
| 2972 | * "100-continue" expectation, check that admin rules are met for POST requests, |
| 2973 | * and program a response message if something was unexpected. It cannot fail |
| 2974 | * 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] | 2975 | * 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] | 2976 | * s->target which is supposed to already point to the stats applet. The caller |
| 2977 | * is expected to have already assigned an appctx to the session. |
Willy Tarreau | 1facd6d | 2012-12-22 22:03:39 +0100 | [diff] [blame] | 2978 | */ |
| 2979 | int http_handle_stats(struct session *s, struct channel *req) |
| 2980 | { |
| 2981 | struct stats_admin_rule *stats_admin_rule; |
| 2982 | struct stream_interface *si = s->rep->prod; |
| 2983 | struct http_txn *txn = &s->txn; |
| 2984 | struct http_msg *msg = &txn->req; |
Willy Tarreau | 414e9bb | 2013-11-23 00:30:38 +0100 | [diff] [blame] | 2985 | struct uri_auth *uri_auth = s->be->uri_auth; |
| 2986 | const char *uri, *h, *lookup; |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 2987 | struct appctx *appctx; |
Willy Tarreau | 414e9bb | 2013-11-23 00:30:38 +0100 | [diff] [blame] | 2988 | |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 2989 | appctx = si_appctx(si); |
| 2990 | memset(&appctx->ctx.stats, 0, sizeof(appctx->ctx.stats)); |
| 2991 | appctx->st1 = appctx->st2 = 0; |
| 2992 | appctx->ctx.stats.st_code = STAT_STATUS_INIT; |
| 2993 | appctx->ctx.stats.flags |= STAT_FMT_HTML; /* assume HTML mode by default */ |
Willy Tarreau | 414e9bb | 2013-11-23 00:30:38 +0100 | [diff] [blame] | 2994 | |
| 2995 | uri = msg->chn->buf->p + msg->sl.rq.u; |
| 2996 | lookup = uri + uri_auth->uri_len; |
| 2997 | |
| 2998 | for (h = lookup; h <= uri + msg->sl.rq.u_l - 3; h++) { |
| 2999 | if (memcmp(h, ";up", 3) == 0) { |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 3000 | appctx->ctx.stats.flags |= STAT_HIDE_DOWN; |
Willy Tarreau | 414e9bb | 2013-11-23 00:30:38 +0100 | [diff] [blame] | 3001 | break; |
| 3002 | } |
| 3003 | } |
| 3004 | |
| 3005 | if (uri_auth->refresh) { |
| 3006 | for (h = lookup; h <= uri + msg->sl.rq.u_l - 10; h++) { |
| 3007 | if (memcmp(h, ";norefresh", 10) == 0) { |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 3008 | appctx->ctx.stats.flags |= STAT_NO_REFRESH; |
Willy Tarreau | 414e9bb | 2013-11-23 00:30:38 +0100 | [diff] [blame] | 3009 | break; |
| 3010 | } |
| 3011 | } |
| 3012 | } |
| 3013 | |
| 3014 | for (h = lookup; h <= uri + msg->sl.rq.u_l - 4; h++) { |
| 3015 | if (memcmp(h, ";csv", 4) == 0) { |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 3016 | appctx->ctx.stats.flags &= ~STAT_FMT_HTML; |
Willy Tarreau | 414e9bb | 2013-11-23 00:30:38 +0100 | [diff] [blame] | 3017 | break; |
| 3018 | } |
| 3019 | } |
| 3020 | |
| 3021 | for (h = lookup; h <= uri + msg->sl.rq.u_l - 8; h++) { |
| 3022 | if (memcmp(h, ";st=", 4) == 0) { |
| 3023 | int i; |
| 3024 | h += 4; |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 3025 | appctx->ctx.stats.st_code = STAT_STATUS_UNKN; |
Willy Tarreau | 414e9bb | 2013-11-23 00:30:38 +0100 | [diff] [blame] | 3026 | for (i = STAT_STATUS_INIT + 1; i < STAT_STATUS_SIZE; i++) { |
| 3027 | if (strncmp(stat_status_codes[i], h, 4) == 0) { |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 3028 | appctx->ctx.stats.st_code = i; |
Willy Tarreau | 414e9bb | 2013-11-23 00:30:38 +0100 | [diff] [blame] | 3029 | break; |
| 3030 | } |
| 3031 | } |
| 3032 | break; |
| 3033 | } |
| 3034 | } |
| 3035 | |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 3036 | appctx->ctx.stats.scope_str = 0; |
| 3037 | appctx->ctx.stats.scope_len = 0; |
Willy Tarreau | 414e9bb | 2013-11-23 00:30:38 +0100 | [diff] [blame] | 3038 | for (h = lookup; h <= uri + msg->sl.rq.u_l - 8; h++) { |
| 3039 | if (memcmp(h, STAT_SCOPE_INPUT_NAME "=", strlen(STAT_SCOPE_INPUT_NAME) + 1) == 0) { |
| 3040 | int itx = 0; |
| 3041 | const char *h2; |
| 3042 | char scope_txt[STAT_SCOPE_TXT_MAXLEN + 1]; |
| 3043 | const char *err; |
| 3044 | |
| 3045 | h += strlen(STAT_SCOPE_INPUT_NAME) + 1; |
| 3046 | h2 = h; |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 3047 | appctx->ctx.stats.scope_str = h2 - msg->chn->buf->p; |
Willy Tarreau | 414e9bb | 2013-11-23 00:30:38 +0100 | [diff] [blame] | 3048 | while (*h != ';' && *h != '\0' && *h != '&' && *h != ' ' && *h != '\n') { |
| 3049 | itx++; |
| 3050 | h++; |
| 3051 | } |
| 3052 | |
| 3053 | if (itx > STAT_SCOPE_TXT_MAXLEN) |
| 3054 | itx = STAT_SCOPE_TXT_MAXLEN; |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 3055 | appctx->ctx.stats.scope_len = itx; |
Willy Tarreau | 414e9bb | 2013-11-23 00:30:38 +0100 | [diff] [blame] | 3056 | |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 3057 | /* 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] | 3058 | memcpy(scope_txt, h2, itx); |
| 3059 | scope_txt[itx] = '\0'; |
| 3060 | err = invalid_char(scope_txt); |
| 3061 | if (err) { |
| 3062 | /* bad char in search text => clear scope */ |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 3063 | appctx->ctx.stats.scope_str = 0; |
| 3064 | appctx->ctx.stats.scope_len = 0; |
Willy Tarreau | 414e9bb | 2013-11-23 00:30:38 +0100 | [diff] [blame] | 3065 | } |
| 3066 | break; |
| 3067 | } |
| 3068 | } |
Willy Tarreau | 1facd6d | 2012-12-22 22:03:39 +0100 | [diff] [blame] | 3069 | |
| 3070 | /* now check whether we have some admin rules for this request */ |
Willy Tarreau | 414e9bb | 2013-11-23 00:30:38 +0100 | [diff] [blame] | 3071 | list_for_each_entry(stats_admin_rule, &uri_auth->admin_rules, list) { |
Willy Tarreau | 1facd6d | 2012-12-22 22:03:39 +0100 | [diff] [blame] | 3072 | int ret = 1; |
| 3073 | |
| 3074 | if (stats_admin_rule->cond) { |
| 3075 | ret = acl_exec_cond(stats_admin_rule->cond, s->be, s, &s->txn, SMP_OPT_DIR_REQ|SMP_OPT_FINAL); |
| 3076 | ret = acl_pass(ret); |
| 3077 | if (stats_admin_rule->cond->pol == ACL_COND_UNLESS) |
| 3078 | ret = !ret; |
| 3079 | } |
| 3080 | |
| 3081 | if (ret) { |
| 3082 | /* no rule, or the rule matches */ |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 3083 | appctx->ctx.stats.flags |= STAT_ADMIN; |
Willy Tarreau | 1facd6d | 2012-12-22 22:03:39 +0100 | [diff] [blame] | 3084 | break; |
| 3085 | } |
| 3086 | } |
| 3087 | |
| 3088 | /* Was the status page requested with a POST ? */ |
Willy Tarreau | 347a35d | 2013-11-22 17:51:09 +0100 | [diff] [blame] | 3089 | if (unlikely(txn->meth == HTTP_METH_POST && txn->req.body_len > 0)) { |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 3090 | if (appctx->ctx.stats.flags & STAT_ADMIN) { |
Willy Tarreau | 1facd6d | 2012-12-22 22:03:39 +0100 | [diff] [blame] | 3091 | if (msg->msg_state < HTTP_MSG_100_SENT) { |
| 3092 | /* If we have HTTP/1.1 and Expect: 100-continue, then we must |
| 3093 | * send an HTTP/1.1 100 Continue intermediate response. |
| 3094 | */ |
| 3095 | if (msg->flags & HTTP_MSGF_VER_11) { |
| 3096 | struct hdr_ctx ctx; |
| 3097 | ctx.idx = 0; |
| 3098 | /* Expect is allowed in 1.1, look for it */ |
| 3099 | if (http_find_header2("Expect", 6, req->buf->p, &txn->hdr_idx, &ctx) && |
| 3100 | unlikely(ctx.vlen == 12 && strncasecmp(ctx.line+ctx.val, "100-continue", 12) == 0)) { |
| 3101 | bo_inject(s->rep, http_100_chunk.str, http_100_chunk.len); |
| 3102 | } |
| 3103 | } |
| 3104 | msg->msg_state = HTTP_MSG_100_SENT; |
| 3105 | s->logs.tv_request = now; /* update the request timer to reflect full request */ |
| 3106 | } |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 3107 | appctx->st0 = STAT_HTTP_POST; |
Willy Tarreau | 1facd6d | 2012-12-22 22:03:39 +0100 | [diff] [blame] | 3108 | } |
Willy Tarreau | 347a35d | 2013-11-22 17:51:09 +0100 | [diff] [blame] | 3109 | else { |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 3110 | appctx->ctx.stats.st_code = STAT_STATUS_DENY; |
| 3111 | appctx->st0 = STAT_HTTP_LAST; |
de Lafond Guillaume | 88c278f | 2013-04-15 19:27:10 +0200 | [diff] [blame] | 3112 | } |
Willy Tarreau | 1facd6d | 2012-12-22 22:03:39 +0100 | [diff] [blame] | 3113 | } |
Willy Tarreau | 347a35d | 2013-11-22 17:51:09 +0100 | [diff] [blame] | 3114 | else { |
| 3115 | /* So it was another method (GET/HEAD) */ |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 3116 | appctx->st0 = STAT_HTTP_HEAD; |
Willy Tarreau | 1facd6d | 2012-12-22 22:03:39 +0100 | [diff] [blame] | 3117 | } |
| 3118 | |
Willy Tarreau | 1facd6d | 2012-12-22 22:03:39 +0100 | [diff] [blame] | 3119 | s->task->nice = -32; /* small boost for HTTP statistics */ |
Willy Tarreau | 1facd6d | 2012-12-22 22:03:39 +0100 | [diff] [blame] | 3120 | return 1; |
| 3121 | } |
| 3122 | |
Lukas Tribus | 67db8df | 2013-06-23 17:37:13 +0200 | [diff] [blame] | 3123 | /* Sets the TOS header in IPv4 and the traffic class header in IPv6 packets |
| 3124 | * (as per RFC3260 #4 and BCP37 #4.2 and #5.2). |
| 3125 | */ |
| 3126 | static inline void inet_set_tos(int fd, struct sockaddr_storage from, int tos) |
| 3127 | { |
| 3128 | #ifdef IP_TOS |
| 3129 | if (from.ss_family == AF_INET) |
| 3130 | setsockopt(fd, IPPROTO_IP, IP_TOS, &tos, sizeof(tos)); |
| 3131 | #endif |
| 3132 | #ifdef IPV6_TCLASS |
| 3133 | if (from.ss_family == AF_INET6) { |
| 3134 | if (IN6_IS_ADDR_V4MAPPED(&((struct sockaddr_in6 *)&from)->sin6_addr)) |
| 3135 | /* v4-mapped addresses need IP_TOS */ |
| 3136 | setsockopt(fd, IPPROTO_IP, IP_TOS, &tos, sizeof(tos)); |
| 3137 | else |
| 3138 | setsockopt(fd, IPPROTO_IPV6, IPV6_TCLASS, &tos, sizeof(tos)); |
| 3139 | } |
| 3140 | #endif |
| 3141 | } |
| 3142 | |
Willy Tarreau | 20b0de5 | 2012-12-24 15:45:22 +0100 | [diff] [blame] | 3143 | /* Executes the http-request rules <rules> for session <s>, proxy <px> and |
Willy Tarreau | 96257ec | 2012-12-27 10:46:37 +0100 | [diff] [blame] | 3144 | * transaction <txn>. Returns the first rule that prevents further processing |
| 3145 | * of the request (auth, deny, ...) or NULL if it executed all rules or stopped |
| 3146 | * on an allow. It may set the TX_CLDENY on txn->flags if it encounters a deny |
| 3147 | * rule. |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 3148 | */ |
Willy Tarreau | 20b0de5 | 2012-12-24 15:45:22 +0100 | [diff] [blame] | 3149 | static struct http_req_rule * |
Willy Tarreau | 96257ec | 2012-12-27 10:46:37 +0100 | [diff] [blame] | 3150 | 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] | 3151 | { |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 3152 | struct connection *cli_conn; |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 3153 | struct http_req_rule *rule; |
Willy Tarreau | 20b0de5 | 2012-12-24 15:45:22 +0100 | [diff] [blame] | 3154 | struct hdr_ctx ctx; |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 3155 | |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 3156 | list_for_each_entry(rule, rules, list) { |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 3157 | if (rule->action >= HTTP_REQ_ACT_MAX) |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 3158 | continue; |
| 3159 | |
Willy Tarreau | 96257ec | 2012-12-27 10:46:37 +0100 | [diff] [blame] | 3160 | /* check optional condition */ |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 3161 | if (rule->cond) { |
Willy Tarreau | 96257ec | 2012-12-27 10:46:37 +0100 | [diff] [blame] | 3162 | int ret; |
| 3163 | |
Willy Tarreau | 32a6f2e | 2012-04-25 10:13:36 +0200 | [diff] [blame] | 3164 | 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] | 3165 | ret = acl_pass(ret); |
| 3166 | |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 3167 | if (rule->cond->pol == ACL_COND_UNLESS) |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 3168 | ret = !ret; |
Willy Tarreau | 96257ec | 2012-12-27 10:46:37 +0100 | [diff] [blame] | 3169 | |
| 3170 | if (!ret) /* condition not matched */ |
| 3171 | continue; |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 3172 | } |
| 3173 | |
Willy Tarreau | 20b0de5 | 2012-12-24 15:45:22 +0100 | [diff] [blame] | 3174 | |
Willy Tarreau | 96257ec | 2012-12-27 10:46:37 +0100 | [diff] [blame] | 3175 | switch (rule->action) { |
| 3176 | case HTTP_REQ_ACT_ALLOW: |
| 3177 | return NULL; /* "allow" rules are OK */ |
| 3178 | |
| 3179 | case HTTP_REQ_ACT_DENY: |
| 3180 | txn->flags |= TX_CLDENY; |
| 3181 | return rule; |
| 3182 | |
Willy Tarreau | ccbcc37 | 2012-12-27 12:37:57 +0100 | [diff] [blame] | 3183 | case HTTP_REQ_ACT_TARPIT: |
| 3184 | txn->flags |= TX_CLTARPIT; |
| 3185 | return rule; |
| 3186 | |
Willy Tarreau | 96257ec | 2012-12-27 10:46:37 +0100 | [diff] [blame] | 3187 | case HTTP_REQ_ACT_AUTH: |
| 3188 | return rule; |
| 3189 | |
Willy Tarreau | 81499eb | 2012-12-27 12:19:02 +0100 | [diff] [blame] | 3190 | case HTTP_REQ_ACT_REDIR: |
| 3191 | return rule; |
| 3192 | |
Willy Tarreau | f4c43c1 | 2013-06-11 17:01:13 +0200 | [diff] [blame] | 3193 | case HTTP_REQ_ACT_SET_NICE: |
| 3194 | s->task->nice = rule->arg.nice; |
| 3195 | break; |
| 3196 | |
Willy Tarreau | 42cf39e | 2013-06-11 18:51:32 +0200 | [diff] [blame] | 3197 | case HTTP_REQ_ACT_SET_TOS: |
Willy Tarreau | 3c72872 | 2014-01-23 13:50:42 +0100 | [diff] [blame] | 3198 | if ((cli_conn = objt_conn(s->req->prod->end)) && conn_ctrl_ready(cli_conn)) |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 3199 | 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] | 3200 | break; |
| 3201 | |
Willy Tarreau | 51347ed | 2013-06-11 19:34:13 +0200 | [diff] [blame] | 3202 | case HTTP_REQ_ACT_SET_MARK: |
| 3203 | #ifdef SO_MARK |
Willy Tarreau | 3c72872 | 2014-01-23 13:50:42 +0100 | [diff] [blame] | 3204 | if ((cli_conn = objt_conn(s->req->prod->end)) && conn_ctrl_ready(cli_conn)) |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 3205 | 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] | 3206 | #endif |
| 3207 | break; |
| 3208 | |
Willy Tarreau | 9a355ec | 2013-06-11 17:45:46 +0200 | [diff] [blame] | 3209 | case HTTP_REQ_ACT_SET_LOGL: |
| 3210 | s->logs.level = rule->arg.loglevel; |
| 3211 | break; |
| 3212 | |
Thierry FOURNIER | dad3d1d | 2014-04-22 18:07:25 +0200 | [diff] [blame] | 3213 | case HTTP_REQ_ACT_DEL_HDR: |
Willy Tarreau | 96257ec | 2012-12-27 10:46:37 +0100 | [diff] [blame] | 3214 | case HTTP_REQ_ACT_SET_HDR: |
| 3215 | ctx.idx = 0; |
| 3216 | /* remove all occurrences of the header */ |
| 3217 | while (http_find_header2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len, |
| 3218 | txn->req.chn->buf->p, &txn->hdr_idx, &ctx)) { |
| 3219 | http_remove_header2(&txn->req, &txn->hdr_idx, &ctx); |
Willy Tarreau | 20b0de5 | 2012-12-24 15:45:22 +0100 | [diff] [blame] | 3220 | } |
Thierry FOURNIER | dad3d1d | 2014-04-22 18:07:25 +0200 | [diff] [blame] | 3221 | if (rule->action == HTTP_REQ_ACT_DEL_HDR) |
| 3222 | break; |
Willy Tarreau | 96257ec | 2012-12-27 10:46:37 +0100 | [diff] [blame] | 3223 | /* now fall through to header addition */ |
| 3224 | |
| 3225 | case HTTP_REQ_ACT_ADD_HDR: |
| 3226 | chunk_printf(&trash, "%s: ", rule->arg.hdr_add.name); |
| 3227 | memcpy(trash.str, rule->arg.hdr_add.name, rule->arg.hdr_add.name_len); |
| 3228 | trash.len = rule->arg.hdr_add.name_len; |
| 3229 | trash.str[trash.len++] = ':'; |
| 3230 | trash.str[trash.len++] = ' '; |
| 3231 | trash.len += build_logline(s, trash.str + trash.len, trash.size - trash.len, &rule->arg.hdr_add.fmt); |
| 3232 | http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, trash.len); |
| 3233 | break; |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 3234 | } |
| 3235 | } |
Willy Tarreau | 96257ec | 2012-12-27 10:46:37 +0100 | [diff] [blame] | 3236 | |
| 3237 | /* we reached the end of the rules, nothing to report */ |
Willy Tarreau | 418c1a0 | 2012-12-25 20:52:58 +0100 | [diff] [blame] | 3238 | return NULL; |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 3239 | } |
| 3240 | |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 3241 | |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 3242 | /* Executes the http-response rules <rules> for session <s>, proxy <px> and |
| 3243 | * transaction <txn>. Returns the first rule that prevents further processing |
| 3244 | * of the response (deny, ...) or NULL if it executed all rules or stopped |
| 3245 | * on an allow. It may set the TX_SVDENY on txn->flags if it encounters a deny |
| 3246 | * rule. |
| 3247 | */ |
| 3248 | static struct http_res_rule * |
| 3249 | http_res_get_intercept_rule(struct proxy *px, struct list *rules, struct session *s, struct http_txn *txn) |
| 3250 | { |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 3251 | struct connection *cli_conn; |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 3252 | struct http_res_rule *rule; |
| 3253 | struct hdr_ctx ctx; |
| 3254 | |
| 3255 | list_for_each_entry(rule, rules, list) { |
| 3256 | if (rule->action >= HTTP_RES_ACT_MAX) |
| 3257 | continue; |
| 3258 | |
| 3259 | /* check optional condition */ |
| 3260 | if (rule->cond) { |
| 3261 | int ret; |
| 3262 | |
| 3263 | ret = acl_exec_cond(rule->cond, px, s, txn, SMP_OPT_DIR_RES|SMP_OPT_FINAL); |
| 3264 | ret = acl_pass(ret); |
| 3265 | |
| 3266 | if (rule->cond->pol == ACL_COND_UNLESS) |
| 3267 | ret = !ret; |
| 3268 | |
| 3269 | if (!ret) /* condition not matched */ |
| 3270 | continue; |
| 3271 | } |
| 3272 | |
| 3273 | |
| 3274 | switch (rule->action) { |
| 3275 | case HTTP_RES_ACT_ALLOW: |
| 3276 | return NULL; /* "allow" rules are OK */ |
| 3277 | |
| 3278 | case HTTP_RES_ACT_DENY: |
| 3279 | txn->flags |= TX_SVDENY; |
| 3280 | return rule; |
| 3281 | |
Willy Tarreau | f4c43c1 | 2013-06-11 17:01:13 +0200 | [diff] [blame] | 3282 | case HTTP_RES_ACT_SET_NICE: |
| 3283 | s->task->nice = rule->arg.nice; |
| 3284 | break; |
| 3285 | |
Willy Tarreau | 42cf39e | 2013-06-11 18:51:32 +0200 | [diff] [blame] | 3286 | case HTTP_RES_ACT_SET_TOS: |
Willy Tarreau | 3c72872 | 2014-01-23 13:50:42 +0100 | [diff] [blame] | 3287 | if ((cli_conn = objt_conn(s->req->prod->end)) && conn_ctrl_ready(cli_conn)) |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 3288 | 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] | 3289 | break; |
| 3290 | |
Willy Tarreau | 51347ed | 2013-06-11 19:34:13 +0200 | [diff] [blame] | 3291 | case HTTP_RES_ACT_SET_MARK: |
| 3292 | #ifdef SO_MARK |
Willy Tarreau | 3c72872 | 2014-01-23 13:50:42 +0100 | [diff] [blame] | 3293 | if ((cli_conn = objt_conn(s->req->prod->end)) && conn_ctrl_ready(cli_conn)) |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 3294 | 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] | 3295 | #endif |
| 3296 | break; |
| 3297 | |
Willy Tarreau | 9a355ec | 2013-06-11 17:45:46 +0200 | [diff] [blame] | 3298 | case HTTP_RES_ACT_SET_LOGL: |
| 3299 | s->logs.level = rule->arg.loglevel; |
| 3300 | break; |
| 3301 | |
Thierry FOURNIER | dad3d1d | 2014-04-22 18:07:25 +0200 | [diff] [blame] | 3302 | case HTTP_RES_ACT_DEL_HDR: |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 3303 | case HTTP_RES_ACT_SET_HDR: |
| 3304 | ctx.idx = 0; |
| 3305 | /* remove all occurrences of the header */ |
| 3306 | while (http_find_header2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len, |
| 3307 | txn->rsp.chn->buf->p, &txn->hdr_idx, &ctx)) { |
| 3308 | http_remove_header2(&txn->rsp, &txn->hdr_idx, &ctx); |
| 3309 | } |
Thierry FOURNIER | dad3d1d | 2014-04-22 18:07:25 +0200 | [diff] [blame] | 3310 | if (rule->action == HTTP_RES_ACT_DEL_HDR) |
| 3311 | break; |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 3312 | /* now fall through to header addition */ |
| 3313 | |
| 3314 | case HTTP_RES_ACT_ADD_HDR: |
| 3315 | chunk_printf(&trash, "%s: ", rule->arg.hdr_add.name); |
| 3316 | memcpy(trash.str, rule->arg.hdr_add.name, rule->arg.hdr_add.name_len); |
| 3317 | trash.len = rule->arg.hdr_add.name_len; |
| 3318 | trash.str[trash.len++] = ':'; |
| 3319 | trash.str[trash.len++] = ' '; |
| 3320 | trash.len += build_logline(s, trash.str + trash.len, trash.size - trash.len, &rule->arg.hdr_add.fmt); |
| 3321 | http_header_add_tail2(&txn->rsp, &txn->hdr_idx, trash.str, trash.len); |
| 3322 | break; |
| 3323 | } |
| 3324 | } |
| 3325 | |
| 3326 | /* we reached the end of the rules, nothing to report */ |
| 3327 | return NULL; |
| 3328 | } |
| 3329 | |
| 3330 | |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 3331 | /* Perform an HTTP redirect based on the information in <rule>. The function |
| 3332 | * returns non-zero on success, or zero in case of a, irrecoverable error such |
| 3333 | * as too large a request to build a valid response. |
| 3334 | */ |
| 3335 | static int http_apply_redirect_rule(struct redirect_rule *rule, struct session *s, struct http_txn *txn) |
| 3336 | { |
| 3337 | struct http_msg *msg = &txn->req; |
| 3338 | const char *msg_fmt; |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 3339 | const char *location; |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 3340 | |
| 3341 | /* build redirect message */ |
| 3342 | switch(rule->code) { |
Yves Lafon | 3e8d1ae | 2013-03-11 11:06:05 -0400 | [diff] [blame] | 3343 | case 308: |
| 3344 | msg_fmt = HTTP_308; |
| 3345 | break; |
| 3346 | case 307: |
| 3347 | msg_fmt = HTTP_307; |
| 3348 | break; |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 3349 | case 303: |
| 3350 | msg_fmt = HTTP_303; |
| 3351 | break; |
| 3352 | case 301: |
| 3353 | msg_fmt = HTTP_301; |
| 3354 | break; |
| 3355 | case 302: |
| 3356 | default: |
| 3357 | msg_fmt = HTTP_302; |
| 3358 | break; |
| 3359 | } |
| 3360 | |
| 3361 | if (unlikely(!chunk_strcpy(&trash, msg_fmt))) |
| 3362 | return 0; |
| 3363 | |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 3364 | location = trash.str + trash.len; |
| 3365 | |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 3366 | switch(rule->type) { |
| 3367 | case REDIRECT_TYPE_SCHEME: { |
| 3368 | const char *path; |
| 3369 | const char *host; |
| 3370 | struct hdr_ctx ctx; |
| 3371 | int pathlen; |
| 3372 | int hostlen; |
| 3373 | |
| 3374 | host = ""; |
| 3375 | hostlen = 0; |
| 3376 | ctx.idx = 0; |
| 3377 | if (http_find_header2("Host", 4, txn->req.chn->buf->p + txn->req.sol, &txn->hdr_idx, &ctx)) { |
| 3378 | host = ctx.line + ctx.val; |
| 3379 | hostlen = ctx.vlen; |
| 3380 | } |
| 3381 | |
| 3382 | path = http_get_path(txn); |
| 3383 | /* build message using path */ |
| 3384 | if (path) { |
| 3385 | pathlen = txn->req.sl.rq.u_l + (txn->req.chn->buf->p + txn->req.sl.rq.u) - path; |
| 3386 | if (rule->flags & REDIRECT_FLAG_DROP_QS) { |
| 3387 | int qs = 0; |
| 3388 | while (qs < pathlen) { |
| 3389 | if (path[qs] == '?') { |
| 3390 | pathlen = qs; |
| 3391 | break; |
| 3392 | } |
| 3393 | qs++; |
| 3394 | } |
| 3395 | } |
| 3396 | } else { |
| 3397 | path = "/"; |
| 3398 | pathlen = 1; |
| 3399 | } |
| 3400 | |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 3401 | if (rule->rdr_str) { /* this is an old "redirect" rule */ |
| 3402 | /* check if we can add scheme + "://" + host + path */ |
| 3403 | if (trash.len + rule->rdr_len + 3 + hostlen + pathlen > trash.size - 4) |
| 3404 | return 0; |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 3405 | |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 3406 | /* add scheme */ |
| 3407 | memcpy(trash.str + trash.len, rule->rdr_str, rule->rdr_len); |
| 3408 | trash.len += rule->rdr_len; |
| 3409 | } |
| 3410 | else { |
| 3411 | /* add scheme with executing log format */ |
| 3412 | 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] | 3413 | |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 3414 | /* check if we can add scheme + "://" + host + path */ |
| 3415 | if (trash.len + 3 + hostlen + pathlen > trash.size - 4) |
| 3416 | return 0; |
| 3417 | } |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 3418 | /* add "://" */ |
| 3419 | memcpy(trash.str + trash.len, "://", 3); |
| 3420 | trash.len += 3; |
| 3421 | |
| 3422 | /* add host */ |
| 3423 | memcpy(trash.str + trash.len, host, hostlen); |
| 3424 | trash.len += hostlen; |
| 3425 | |
| 3426 | /* add path */ |
| 3427 | memcpy(trash.str + trash.len, path, pathlen); |
| 3428 | trash.len += pathlen; |
| 3429 | |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 3430 | /* 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] | 3431 | if (trash.len && trash.str[trash.len - 1] != '/' && |
| 3432 | (rule->flags & REDIRECT_FLAG_APPEND_SLASH)) { |
| 3433 | if (trash.len > trash.size - 5) |
| 3434 | return 0; |
| 3435 | trash.str[trash.len] = '/'; |
| 3436 | trash.len++; |
| 3437 | } |
| 3438 | |
| 3439 | break; |
| 3440 | } |
| 3441 | case REDIRECT_TYPE_PREFIX: { |
| 3442 | const char *path; |
| 3443 | int pathlen; |
| 3444 | |
| 3445 | path = http_get_path(txn); |
| 3446 | /* build message using path */ |
| 3447 | if (path) { |
| 3448 | pathlen = txn->req.sl.rq.u_l + (txn->req.chn->buf->p + txn->req.sl.rq.u) - path; |
| 3449 | if (rule->flags & REDIRECT_FLAG_DROP_QS) { |
| 3450 | int qs = 0; |
| 3451 | while (qs < pathlen) { |
| 3452 | if (path[qs] == '?') { |
| 3453 | pathlen = qs; |
| 3454 | break; |
| 3455 | } |
| 3456 | qs++; |
| 3457 | } |
| 3458 | } |
| 3459 | } else { |
| 3460 | path = "/"; |
| 3461 | pathlen = 1; |
| 3462 | } |
| 3463 | |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 3464 | if (rule->rdr_str) { /* this is an old "redirect" rule */ |
| 3465 | if (trash.len + rule->rdr_len + pathlen > trash.size - 4) |
| 3466 | return 0; |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 3467 | |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 3468 | /* add prefix. Note that if prefix == "/", we don't want to |
| 3469 | * add anything, otherwise it makes it hard for the user to |
| 3470 | * configure a self-redirection. |
| 3471 | */ |
| 3472 | if (rule->rdr_len != 1 || *rule->rdr_str != '/') { |
| 3473 | memcpy(trash.str + trash.len, rule->rdr_str, rule->rdr_len); |
| 3474 | trash.len += rule->rdr_len; |
| 3475 | } |
| 3476 | } |
| 3477 | else { |
| 3478 | /* add prefix with executing log format */ |
| 3479 | trash.len += build_logline(s, trash.str + trash.len, trash.size - trash.len, &rule->rdr_fmt); |
| 3480 | |
| 3481 | /* Check length */ |
| 3482 | if (trash.len + pathlen > trash.size - 4) |
| 3483 | return 0; |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 3484 | } |
| 3485 | |
| 3486 | /* add path */ |
| 3487 | memcpy(trash.str + trash.len, path, pathlen); |
| 3488 | trash.len += pathlen; |
| 3489 | |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 3490 | /* 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] | 3491 | if (trash.len && trash.str[trash.len - 1] != '/' && |
| 3492 | (rule->flags & REDIRECT_FLAG_APPEND_SLASH)) { |
| 3493 | if (trash.len > trash.size - 5) |
| 3494 | return 0; |
| 3495 | trash.str[trash.len] = '/'; |
| 3496 | trash.len++; |
| 3497 | } |
| 3498 | |
| 3499 | break; |
| 3500 | } |
| 3501 | case REDIRECT_TYPE_LOCATION: |
| 3502 | default: |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 3503 | if (rule->rdr_str) { /* this is an old "redirect" rule */ |
| 3504 | if (trash.len + rule->rdr_len > trash.size - 4) |
| 3505 | return 0; |
| 3506 | |
| 3507 | /* add location */ |
| 3508 | memcpy(trash.str + trash.len, rule->rdr_str, rule->rdr_len); |
| 3509 | trash.len += rule->rdr_len; |
| 3510 | } |
| 3511 | else { |
| 3512 | /* add location with executing log format */ |
| 3513 | 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] | 3514 | |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 3515 | /* Check left length */ |
| 3516 | if (trash.len > trash.size - 4) |
| 3517 | return 0; |
| 3518 | } |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 3519 | break; |
| 3520 | } |
| 3521 | |
| 3522 | if (rule->cookie_len) { |
| 3523 | memcpy(trash.str + trash.len, "\r\nSet-Cookie: ", 14); |
| 3524 | trash.len += 14; |
| 3525 | memcpy(trash.str + trash.len, rule->cookie_str, rule->cookie_len); |
| 3526 | trash.len += rule->cookie_len; |
| 3527 | memcpy(trash.str + trash.len, "\r\n", 2); |
| 3528 | trash.len += 2; |
| 3529 | } |
| 3530 | |
| 3531 | /* add end of headers and the keep-alive/close status. |
| 3532 | * We may choose to set keep-alive if the Location begins |
| 3533 | * with a slash, because the client will come back to the |
| 3534 | * same server. |
| 3535 | */ |
| 3536 | txn->status = rule->code; |
| 3537 | /* let's log the request time */ |
| 3538 | s->logs.tv_request = now; |
| 3539 | |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 3540 | if (*location == '/' && |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 3541 | (msg->flags & HTTP_MSGF_XFER_LEN) && |
| 3542 | !(msg->flags & HTTP_MSGF_TE_CHNK) && !txn->req.body_len && |
| 3543 | ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL || |
| 3544 | (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL)) { |
| 3545 | /* keep-alive possible */ |
| 3546 | if (!(msg->flags & HTTP_MSGF_VER_11)) { |
| 3547 | if (unlikely(txn->flags & TX_USE_PX_CONN)) { |
| 3548 | memcpy(trash.str + trash.len, "\r\nProxy-Connection: keep-alive", 30); |
| 3549 | trash.len += 30; |
| 3550 | } else { |
| 3551 | memcpy(trash.str + trash.len, "\r\nConnection: keep-alive", 24); |
| 3552 | trash.len += 24; |
| 3553 | } |
| 3554 | } |
| 3555 | memcpy(trash.str + trash.len, "\r\n\r\n", 4); |
| 3556 | trash.len += 4; |
| 3557 | bo_inject(txn->rsp.chn, trash.str, trash.len); |
| 3558 | /* "eat" the request */ |
| 3559 | bi_fast_delete(txn->req.chn->buf, msg->sov); |
| 3560 | msg->sov = 0; |
| 3561 | txn->req.chn->analysers = AN_REQ_HTTP_XFER_BODY; |
| 3562 | s->rep->analysers = AN_RES_HTTP_XFER_BODY; |
| 3563 | txn->req.msg_state = HTTP_MSG_CLOSED; |
| 3564 | txn->rsp.msg_state = HTTP_MSG_DONE; |
| 3565 | } else { |
| 3566 | /* keep-alive not possible */ |
| 3567 | if (unlikely(txn->flags & TX_USE_PX_CONN)) { |
| 3568 | memcpy(trash.str + trash.len, "\r\nProxy-Connection: close\r\n\r\n", 29); |
| 3569 | trash.len += 29; |
| 3570 | } else { |
| 3571 | memcpy(trash.str + trash.len, "\r\nConnection: close\r\n\r\n", 23); |
| 3572 | trash.len += 23; |
| 3573 | } |
| 3574 | stream_int_retnclose(txn->req.chn->prod, &trash); |
| 3575 | txn->req.chn->analysers = 0; |
| 3576 | } |
| 3577 | |
| 3578 | if (!(s->flags & SN_ERR_MASK)) |
Willy Tarreau | 570f221 | 2013-06-10 16:42:09 +0200 | [diff] [blame] | 3579 | s->flags |= SN_ERR_LOCAL; |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 3580 | if (!(s->flags & SN_FINST_MASK)) |
| 3581 | s->flags |= SN_FINST_R; |
| 3582 | |
| 3583 | return 1; |
| 3584 | } |
| 3585 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3586 | /* This stream analyser runs all HTTP request processing which is common to |
| 3587 | * frontends and backends, which means blocking ACLs, filters, connection-close, |
| 3588 | * reqadd, stats and redirects. This is performed for the designated proxy. |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 3589 | * 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] | 3590 | * either needs more data or wants to immediately abort the request (eg: deny, |
| 3591 | * error, ...). |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 3592 | */ |
Willy Tarreau | 7421efb | 2012-07-02 15:11:27 +0200 | [diff] [blame] | 3593 | 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] | 3594 | { |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 3595 | struct http_txn *txn = &s->txn; |
| 3596 | struct http_msg *msg = &txn->req; |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3597 | struct acl_cond *cond; |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 3598 | struct http_req_rule *http_req_last_rule = NULL; |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3599 | struct redirect_rule *rule; |
Willy Tarreau | f4f0412 | 2010-01-28 18:10:50 +0100 | [diff] [blame] | 3600 | struct cond_wordlist *wl; |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 3601 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 3602 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) { |
Willy Tarreau | 51aecc7 | 2009-07-12 09:47:04 +0200 | [diff] [blame] | 3603 | /* we need more data */ |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 3604 | channel_dont_connect(req); |
Willy Tarreau | 51aecc7 | 2009-07-12 09:47:04 +0200 | [diff] [blame] | 3605 | return 0; |
| 3606 | } |
| 3607 | |
Willy Tarreau | 3a81629 | 2009-07-07 10:55:49 +0200 | [diff] [blame] | 3608 | req->analysers &= ~an_bit; |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 3609 | req->analyse_exp = TICK_ETERNITY; |
| 3610 | |
Willy Tarreau | 02d6cfc | 2012-03-01 18:19:58 +0100 | [diff] [blame] | 3611 | 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] | 3612 | now_ms, __FUNCTION__, |
| 3613 | s, |
| 3614 | req, |
| 3615 | req->rex, req->wex, |
| 3616 | req->flags, |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 3617 | req->buf->i, |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 3618 | req->analysers); |
| 3619 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3620 | /* first check whether we have some ACLs set to block this request */ |
| 3621 | list_for_each_entry(cond, &px->block_cond, list) { |
Willy Tarreau | 32a6f2e | 2012-04-25 10:13:36 +0200 | [diff] [blame] | 3622 | 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] | 3623 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3624 | ret = acl_pass(ret); |
| 3625 | if (cond->pol == ACL_COND_UNLESS) |
| 3626 | ret = !ret; |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 3627 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3628 | if (ret) { |
| 3629 | txn->status = 403; |
| 3630 | /* let's log the request time */ |
| 3631 | s->logs.tv_request = now; |
Willy Tarreau | 783f258 | 2012-09-04 12:19:04 +0200 | [diff] [blame] | 3632 | stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_403)); |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 3633 | session_inc_http_err_ctr(s); |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3634 | goto return_prx_cond; |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3635 | } |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3636 | } |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3637 | |
Willy Tarreau | 5d5b5d8 | 2012-12-09 12:00:04 +0100 | [diff] [blame] | 3638 | /* just in case we have some per-backend tracking */ |
| 3639 | session_inc_be_http_req_ctr(s); |
| 3640 | |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 3641 | /* evaluate http-request rules */ |
Willy Tarreau | 96257ec | 2012-12-27 10:46:37 +0100 | [diff] [blame] | 3642 | 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] | 3643 | |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 3644 | /* evaluate stats http-request rules only if http-request is OK */ |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 3645 | if (!http_req_last_rule) { |
Willy Tarreau | 414e9bb | 2013-11-23 00:30:38 +0100 | [diff] [blame] | 3646 | if (stats_check_uri(s->rep->prod, txn, px)) { |
| 3647 | s->target = &http_stats_applet.obj_type; |
Willy Tarreau | 1fbe1c9 | 2013-12-01 09:35:41 +0100 | [diff] [blame] | 3648 | if (unlikely(!stream_int_register_handler(s->rep->prod, objt_applet(s->target)))) { |
| 3649 | txn->status = 500; |
| 3650 | s->logs.tv_request = now; |
| 3651 | stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_500)); |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 3652 | |
Willy Tarreau | 1fbe1c9 | 2013-12-01 09:35:41 +0100 | [diff] [blame] | 3653 | if (!(s->flags & SN_ERR_MASK)) |
| 3654 | s->flags |= SN_ERR_RESOURCE; |
| 3655 | goto return_prx_cond; |
| 3656 | } |
Willy Tarreau | 414e9bb | 2013-11-23 00:30:38 +0100 | [diff] [blame] | 3657 | /* parse the whole stats request and extract the relevant information */ |
| 3658 | http_handle_stats(s, req); |
Willy Tarreau | 96257ec | 2012-12-27 10:46:37 +0100 | [diff] [blame] | 3659 | 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] | 3660 | } |
Krzysztof Piotr Oledzki | 59bb218 | 2010-01-29 17:58:21 +0100 | [diff] [blame] | 3661 | } |
| 3662 | |
Willy Tarreau | 3b44e72 | 2013-11-16 10:28:23 +0100 | [diff] [blame] | 3663 | /* only apply req{,i}{rep/deny/tarpit} if the request was not yet |
| 3664 | * blocked by an http-request rule. |
| 3665 | */ |
| 3666 | if (!(txn->flags & (TX_CLDENY|TX_CLTARPIT)) && (px->req_exp != NULL)) { |
Willy Tarreau | 6c123b1 | 2010-01-28 20:22:06 +0100 | [diff] [blame] | 3667 | if (apply_filters_to_request(s, req, px) < 0) |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3668 | goto return_bad_req; |
Willy Tarreau | 3b44e72 | 2013-11-16 10:28:23 +0100 | [diff] [blame] | 3669 | } |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 3670 | |
Willy Tarreau | 3b44e72 | 2013-11-16 10:28:23 +0100 | [diff] [blame] | 3671 | /* return a 403 if either rule has blocked */ |
| 3672 | if (txn->flags & (TX_CLDENY|TX_CLTARPIT)) { |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3673 | if (txn->flags & TX_CLDENY) { |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3674 | txn->status = 403; |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3675 | s->logs.tv_request = now; |
Willy Tarreau | 783f258 | 2012-09-04 12:19:04 +0200 | [diff] [blame] | 3676 | stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_403)); |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 3677 | session_inc_http_err_ctr(s); |
Willy Tarreau | 687ba13 | 2013-11-16 10:13:35 +0100 | [diff] [blame] | 3678 | s->fe->fe_counters.denied_req++; |
| 3679 | if (s->fe != s->be) |
| 3680 | s->be->be_counters.denied_req++; |
| 3681 | if (s->listener->counters) |
| 3682 | s->listener->counters->denied_req++; |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3683 | goto return_prx_cond; |
| 3684 | } |
Willy Tarreau | c465fd7 | 2009-08-31 00:17:18 +0200 | [diff] [blame] | 3685 | |
| 3686 | /* When a connection is tarpitted, we use the tarpit timeout, |
| 3687 | * which may be the same as the connect timeout if unspecified. |
| 3688 | * If unset, then set it to zero because we really want it to |
| 3689 | * eventually expire. We build the tarpit as an analyser. |
| 3690 | */ |
| 3691 | if (txn->flags & TX_CLTARPIT) { |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 3692 | channel_erase(s->req); |
Willy Tarreau | c465fd7 | 2009-08-31 00:17:18 +0200 | [diff] [blame] | 3693 | /* wipe the request out so that we can drop the connection early |
| 3694 | * if the client closes first. |
| 3695 | */ |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 3696 | channel_dont_connect(req); |
Willy Tarreau | c465fd7 | 2009-08-31 00:17:18 +0200 | [diff] [blame] | 3697 | req->analysers = 0; /* remove switching rules etc... */ |
| 3698 | req->analysers |= AN_REQ_HTTP_TARPIT; |
| 3699 | req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.tarpit); |
| 3700 | if (!req->analyse_exp) |
| 3701 | req->analyse_exp = tick_add(now_ms, 0); |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 3702 | session_inc_http_err_ctr(s); |
Willy Tarreau | 687ba13 | 2013-11-16 10:13:35 +0100 | [diff] [blame] | 3703 | s->fe->fe_counters.denied_req++; |
| 3704 | if (s->fe != s->be) |
| 3705 | s->be->be_counters.denied_req++; |
| 3706 | if (s->listener->counters) |
| 3707 | s->listener->counters->denied_req++; |
Willy Tarreau | c465fd7 | 2009-08-31 00:17:18 +0200 | [diff] [blame] | 3708 | return 1; |
| 3709 | } |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3710 | } |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 3711 | |
Willy Tarreau | 70dffda | 2014-01-30 03:07:23 +0100 | [diff] [blame] | 3712 | /* Until set to anything else, the connection mode is set as Keep-Alive. It will |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 3713 | * only change if both the request and the config reference something else. |
Willy Tarreau | 70dffda | 2014-01-30 03:07:23 +0100 | [diff] [blame] | 3714 | * Option httpclose by itself sets tunnel mode where headers are mangled. |
| 3715 | * However, if another mode is set, it will affect it (eg: server-close/ |
| 3716 | * keep-alive + httpclose = close). Note that we avoid to redo the same work |
| 3717 | * if FE and BE have the same settings (common). The method consists in |
| 3718 | * checking if options changed between the two calls (implying that either |
| 3719 | * one is non-null, or one of them is non-null and we are there for the first |
| 3720 | * time. |
Willy Tarreau | 4273664 | 2009-10-18 21:04:35 +0200 | [diff] [blame] | 3721 | */ |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 3722 | |
Willy Tarreau | 416ce61 | 2014-01-31 15:45:34 +0100 | [diff] [blame] | 3723 | if (!(txn->flags & TX_HDR_CONN_PRS) || |
Willy Tarreau | 02bce8b | 2014-01-30 00:15:28 +0100 | [diff] [blame] | 3724 | ((s->fe->options & PR_O_HTTP_MODE) != (s->be->options & PR_O_HTTP_MODE))) { |
Willy Tarreau | 70dffda | 2014-01-30 03:07:23 +0100 | [diff] [blame] | 3725 | int tmp = TX_CON_WANT_KAL; |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 3726 | |
Willy Tarreau | 70dffda | 2014-01-30 03:07:23 +0100 | [diff] [blame] | 3727 | if (!((s->fe->options2|s->be->options2) & PR_O2_FAKE_KA)) { |
| 3728 | if ((s->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_TUN || |
| 3729 | (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_TUN) |
| 3730 | tmp = TX_CON_WANT_TUN; |
| 3731 | |
| 3732 | if ((s->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL || |
| 3733 | (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL) |
| 3734 | tmp = TX_CON_WANT_TUN; |
| 3735 | } |
Willy Tarreau | 02bce8b | 2014-01-30 00:15:28 +0100 | [diff] [blame] | 3736 | |
| 3737 | if ((s->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_SCL || |
Willy Tarreau | 70dffda | 2014-01-30 03:07:23 +0100 | [diff] [blame] | 3738 | (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_SCL) { |
| 3739 | /* option httpclose + server_close => forceclose */ |
| 3740 | if ((s->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL || |
| 3741 | (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL) |
| 3742 | tmp = TX_CON_WANT_CLO; |
| 3743 | else |
| 3744 | tmp = TX_CON_WANT_SCL; |
| 3745 | } |
Willy Tarreau | 02bce8b | 2014-01-30 00:15:28 +0100 | [diff] [blame] | 3746 | |
| 3747 | if ((s->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_FCL || |
| 3748 | (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_FCL) |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 3749 | tmp = TX_CON_WANT_CLO; |
| 3750 | |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 3751 | if ((txn->flags & TX_CON_WANT_MSK) < tmp) |
| 3752 | txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | tmp; |
Willy Tarreau | 0dfdf19 | 2010-01-05 11:33:11 +0100 | [diff] [blame] | 3753 | |
Willy Tarreau | 416ce61 | 2014-01-31 15:45:34 +0100 | [diff] [blame] | 3754 | if (!(txn->flags & TX_HDR_CONN_PRS) && |
| 3755 | (txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) { |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 3756 | /* parse the Connection header and possibly clean it */ |
| 3757 | int to_del = 0; |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 3758 | if ((msg->flags & HTTP_MSGF_VER_11) || |
Willy Tarreau | 8a8e1d9 | 2010-04-05 16:15:16 +0200 | [diff] [blame] | 3759 | ((txn->flags & TX_CON_WANT_MSK) >= TX_CON_WANT_SCL && |
| 3760 | !((s->fe->options2|s->be->options2) & PR_O2_FAKE_KA))) |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 3761 | to_del |= 2; /* remove "keep-alive" */ |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 3762 | if (!(msg->flags & HTTP_MSGF_VER_11)) |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 3763 | to_del |= 1; /* remove "close" */ |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 3764 | http_parse_connection_header(txn, msg, to_del); |
Willy Tarreau | 0dfdf19 | 2010-01-05 11:33:11 +0100 | [diff] [blame] | 3765 | } |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 3766 | |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 3767 | /* check if client or config asks for explicit close in KAL/SCL */ |
| 3768 | if (((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL || |
| 3769 | (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) && |
| 3770 | ((txn->flags & TX_HDR_CONN_CLO) || /* "connection: close" */ |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 3771 | (!(msg->flags & HTTP_MSGF_VER_11) && !(txn->flags & TX_HDR_CONN_KAL)) || /* no "connection: k-a" in 1.0 */ |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 3772 | !(msg->flags & HTTP_MSGF_XFER_LEN) || /* no length known => close */ |
Willy Tarreau | c3e8b25 | 2010-01-28 15:01:20 +0100 | [diff] [blame] | 3773 | s->fe->state == PR_STSTOPPED)) /* frontend is stopping */ |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 3774 | txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO; |
| 3775 | } |
Willy Tarreau | 7859991 | 2009-10-17 20:12:21 +0200 | [diff] [blame] | 3776 | |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 3777 | /* we can be blocked here because the request needs to be authenticated, |
| 3778 | * either to pass or to access stats. |
| 3779 | */ |
Willy Tarreau | 20b0de5 | 2012-12-24 15:45:22 +0100 | [diff] [blame] | 3780 | 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] | 3781 | char *realm = http_req_last_rule->arg.auth.realm; |
Krzysztof Piotr Oledzki | 59bb218 | 2010-01-29 17:58:21 +0100 | [diff] [blame] | 3782 | |
Krzysztof Piotr Oledzki | 8c8bd45 | 2010-01-29 19:29:32 +0100 | [diff] [blame] | 3783 | if (!realm) |
Willy Tarreau | 414e9bb | 2013-11-23 00:30:38 +0100 | [diff] [blame] | 3784 | 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] | 3785 | |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 3786 | 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] | 3787 | txn->status = 401; |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 3788 | stream_int_retnclose(req->prod, &trash); |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 3789 | /* on 401 we still count one error, because normal browsing |
| 3790 | * won't significantly increase the counter but brute force |
| 3791 | * attempts will. |
| 3792 | */ |
| 3793 | session_inc_http_err_ctr(s); |
Krzysztof Piotr Oledzki | 59bb218 | 2010-01-29 17:58:21 +0100 | [diff] [blame] | 3794 | goto return_prx_cond; |
| 3795 | } |
| 3796 | |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 3797 | /* add request headers from the rule sets in the same order */ |
| 3798 | list_for_each_entry(wl, &px->req_add, list) { |
| 3799 | if (wl->cond) { |
Willy Tarreau | 32a6f2e | 2012-04-25 10:13:36 +0200 | [diff] [blame] | 3800 | 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] | 3801 | ret = acl_pass(ret); |
| 3802 | if (((struct acl_cond *)wl->cond)->pol == ACL_COND_UNLESS) |
| 3803 | ret = !ret; |
| 3804 | if (!ret) |
| 3805 | continue; |
| 3806 | } |
| 3807 | |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 3808 | 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] | 3809 | goto return_bad_req; |
Willy Tarreau | 81499eb | 2012-12-27 12:19:02 +0100 | [diff] [blame] | 3810 | } |
| 3811 | |
| 3812 | if (http_req_last_rule && http_req_last_rule->action == HTTP_REQ_ACT_REDIR) { |
| 3813 | if (!http_apply_redirect_rule(http_req_last_rule->arg.redir, s, txn)) |
| 3814 | goto return_bad_req; |
| 3815 | req->analyse_exp = TICK_ETERNITY; |
| 3816 | return 1; |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 3817 | } |
| 3818 | |
Willy Tarreau | 414e9bb | 2013-11-23 00:30:38 +0100 | [diff] [blame] | 3819 | if (unlikely(objt_applet(s->target) == &http_stats_applet)) { |
Willy Tarreau | 1facd6d | 2012-12-22 22:03:39 +0100 | [diff] [blame] | 3820 | /* process the stats request now */ |
Willy Tarreau | 347a35d | 2013-11-22 17:51:09 +0100 | [diff] [blame] | 3821 | if (s->fe == s->be) /* report it if the request was intercepted by the frontend */ |
| 3822 | s->fe->fe_counters.intercepted_req++; |
| 3823 | |
| 3824 | if (!(s->flags & SN_ERR_MASK)) // this is not really an error but it is |
| 3825 | s->flags |= SN_ERR_LOCAL; // to mark that it comes from the proxy |
| 3826 | if (!(s->flags & SN_FINST_MASK)) |
| 3827 | s->flags |= SN_FINST_R; |
| 3828 | |
| 3829 | req->analyse_exp = TICK_ETERNITY; |
Willy Tarreau | 51437d2 | 2013-12-29 00:43:40 +0100 | [diff] [blame] | 3830 | req->analysers = 0; |
Willy Tarreau | 1facd6d | 2012-12-22 22:03:39 +0100 | [diff] [blame] | 3831 | return 1; |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3832 | } |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 3833 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3834 | /* check whether we have some ACLs set to redirect this request */ |
| 3835 | list_for_each_entry(rule, &px->redirect_rules, list) { |
Willy Tarreau | f285f54 | 2010-01-03 20:03:03 +0100 | [diff] [blame] | 3836 | if (rule->cond) { |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 3837 | int ret; |
| 3838 | |
Willy Tarreau | 32a6f2e | 2012-04-25 10:13:36 +0200 | [diff] [blame] | 3839 | 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] | 3840 | ret = acl_pass(ret); |
| 3841 | if (rule->cond->pol == ACL_COND_UNLESS) |
| 3842 | ret = !ret; |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 3843 | if (!ret) |
| 3844 | continue; |
Willy Tarreau | f285f54 | 2010-01-03 20:03:03 +0100 | [diff] [blame] | 3845 | } |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 3846 | if (!http_apply_redirect_rule(rule, s, txn)) |
| 3847 | goto return_bad_req; |
Willy Tarreau | a9679ac | 2010-01-03 17:32:57 +0100 | [diff] [blame] | 3848 | |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 3849 | req->analyse_exp = TICK_ETERNITY; |
| 3850 | return 1; |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3851 | } |
Willy Tarreau | 55ea757 | 2007-06-17 19:56:27 +0200 | [diff] [blame] | 3852 | |
Willy Tarreau | 2be3939 | 2010-01-03 17:24:51 +0100 | [diff] [blame] | 3853 | /* POST requests may be accompanied with an "Expect: 100-Continue" header. |
| 3854 | * If this happens, then the data will not come immediately, so we must |
| 3855 | * send all what we have without waiting. Note that due to the small gain |
| 3856 | * 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] | 3857 | * 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] | 3858 | * itself once used. |
| 3859 | */ |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 3860 | req->flags |= CF_SEND_DONTWAIT; |
Willy Tarreau | 2be3939 | 2010-01-03 17:24:51 +0100 | [diff] [blame] | 3861 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3862 | /* that's OK for us now, let's move on to next analysers */ |
| 3863 | return 1; |
Willy Tarreau | 1138281 | 2008-07-09 16:18:21 +0200 | [diff] [blame] | 3864 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3865 | return_bad_req: |
| 3866 | /* We centralize bad requests processing here */ |
| 3867 | if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) { |
| 3868 | /* we detected a parsing error. We want to archive this request |
| 3869 | * in the dedicated proxy area for later troubleshooting. |
| 3870 | */ |
Willy Tarreau | 8a0cef2 | 2012-03-09 13:39:23 +0100 | [diff] [blame] | 3871 | 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] | 3872 | } |
Willy Tarreau | 55ea757 | 2007-06-17 19:56:27 +0200 | [diff] [blame] | 3873 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3874 | txn->req.msg_state = HTTP_MSG_ERROR; |
| 3875 | txn->status = 400; |
Willy Tarreau | 783f258 | 2012-09-04 12:19:04 +0200 | [diff] [blame] | 3876 | 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] | 3877 | |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 3878 | s->fe->fe_counters.failed_req++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 3879 | if (s->listener->counters) |
| 3880 | s->listener->counters->failed_req++; |
Willy Tarreau | 6e4261e | 2007-09-18 18:36:05 +0200 | [diff] [blame] | 3881 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3882 | return_prx_cond: |
| 3883 | if (!(s->flags & SN_ERR_MASK)) |
| 3884 | s->flags |= SN_ERR_PRXCOND; |
| 3885 | if (!(s->flags & SN_FINST_MASK)) |
| 3886 | s->flags |= SN_FINST_R; |
Willy Tarreau | f1221aa | 2006-12-17 22:14:12 +0100 | [diff] [blame] | 3887 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3888 | req->analysers = 0; |
| 3889 | req->analyse_exp = TICK_ETERNITY; |
| 3890 | return 0; |
| 3891 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3892 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3893 | /* This function performs all the processing enabled for the current request. |
| 3894 | * It returns 1 if the processing can continue on next analysers, or zero if it |
| 3895 | * needs more data, encounters an error, or wants to immediately abort the |
| 3896 | * request. It relies on buffers flags, and updates s->req->analysers. |
| 3897 | */ |
Willy Tarreau | 7421efb | 2012-07-02 15:11:27 +0200 | [diff] [blame] | 3898 | 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] | 3899 | { |
| 3900 | struct http_txn *txn = &s->txn; |
| 3901 | struct http_msg *msg = &txn->req; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 3902 | struct connection *cli_conn = objt_conn(req->prod->end); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3903 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 3904 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) { |
Willy Tarreau | 51aecc7 | 2009-07-12 09:47:04 +0200 | [diff] [blame] | 3905 | /* we need more data */ |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 3906 | channel_dont_connect(req); |
Willy Tarreau | 51aecc7 | 2009-07-12 09:47:04 +0200 | [diff] [blame] | 3907 | return 0; |
| 3908 | } |
| 3909 | |
Willy Tarreau | 02d6cfc | 2012-03-01 18:19:58 +0100 | [diff] [blame] | 3910 | 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] | 3911 | now_ms, __FUNCTION__, |
| 3912 | s, |
| 3913 | req, |
| 3914 | req->rex, req->wex, |
| 3915 | req->flags, |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 3916 | req->buf->i, |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3917 | req->analysers); |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 3918 | |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 3919 | if (s->fe->comp || s->be->comp) |
| 3920 | select_compression_request_header(s, req->buf); |
| 3921 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3922 | /* |
| 3923 | * Right now, we know that we have processed the entire headers |
| 3924 | * and that unwanted requests have been filtered out. We can do |
| 3925 | * whatever we want with the remaining request. Also, now we |
| 3926 | * may have separate values for ->fe, ->be. |
| 3927 | */ |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 3928 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3929 | /* |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 3930 | * If HTTP PROXY is set we simply get remote server address parsing |
| 3931 | * incoming request. Note that this requires that a connection is |
| 3932 | * allocated on the server side. |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3933 | */ |
| 3934 | 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] | 3935 | struct connection *conn; |
Willy Tarreau | e8df1e1 | 2013-12-16 14:30:55 +0100 | [diff] [blame] | 3936 | char *path; |
Willy Tarreau | 32e3c6a | 2013-10-11 19:34:20 +0200 | [diff] [blame] | 3937 | |
Willy Tarreau | 9471b8c | 2013-12-15 13:31:35 +0100 | [diff] [blame] | 3938 | /* Note that for now we don't reuse existing proxy connections */ |
| 3939 | if (unlikely((conn = si_alloc_conn(req->cons, 0)) == NULL)) { |
Willy Tarreau | 32e3c6a | 2013-10-11 19:34:20 +0200 | [diff] [blame] | 3940 | txn->req.msg_state = HTTP_MSG_ERROR; |
| 3941 | txn->status = 500; |
| 3942 | req->analysers = 0; |
| 3943 | stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_500)); |
| 3944 | |
| 3945 | if (!(s->flags & SN_ERR_MASK)) |
| 3946 | s->flags |= SN_ERR_RESOURCE; |
| 3947 | if (!(s->flags & SN_FINST_MASK)) |
| 3948 | s->flags |= SN_FINST_R; |
| 3949 | |
| 3950 | return 0; |
| 3951 | } |
Willy Tarreau | e8df1e1 | 2013-12-16 14:30:55 +0100 | [diff] [blame] | 3952 | |
| 3953 | path = http_get_path(txn); |
| 3954 | url2sa(req->buf->p + msg->sl.rq.u, |
| 3955 | path ? path - (req->buf->p + msg->sl.rq.u) : msg->sl.rq.u_l, |
Thierry FOURNIER | 9f95e40 | 2014-03-21 14:51:46 +0100 | [diff] [blame] | 3956 | &conn->addr.to, NULL); |
Willy Tarreau | e8df1e1 | 2013-12-16 14:30:55 +0100 | [diff] [blame] | 3957 | /* if the path was found, we have to remove everything between |
| 3958 | * req->buf->p + msg->sl.rq.u and path (excluded). If it was not |
| 3959 | * found, we need to replace from req->buf->p + msg->sl.rq.u for |
| 3960 | * u_l characters by a single "/". |
| 3961 | */ |
| 3962 | if (path) { |
| 3963 | char *cur_ptr = req->buf->p; |
| 3964 | char *cur_end = cur_ptr + txn->req.sl.rq.l; |
| 3965 | int delta; |
| 3966 | |
| 3967 | delta = buffer_replace2(req->buf, req->buf->p + msg->sl.rq.u, path, NULL, 0); |
| 3968 | http_msg_move_end(&txn->req, delta); |
| 3969 | cur_end += delta; |
| 3970 | if (http_parse_reqline(&txn->req, HTTP_MSG_RQMETH, cur_ptr, cur_end + 1, NULL, NULL) == NULL) |
| 3971 | goto return_bad_req; |
| 3972 | } |
| 3973 | else { |
| 3974 | char *cur_ptr = req->buf->p; |
| 3975 | char *cur_end = cur_ptr + txn->req.sl.rq.l; |
| 3976 | int delta; |
| 3977 | |
| 3978 | delta = buffer_replace2(req->buf, req->buf->p + msg->sl.rq.u, |
| 3979 | req->buf->p + msg->sl.rq.u + msg->sl.rq.u_l, "/", 1); |
| 3980 | http_msg_move_end(&txn->req, delta); |
| 3981 | cur_end += delta; |
| 3982 | if (http_parse_reqline(&txn->req, HTTP_MSG_RQMETH, cur_ptr, cur_end + 1, NULL, NULL) == NULL) |
| 3983 | goto return_bad_req; |
| 3984 | } |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3985 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3986 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3987 | /* |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 3988 | * 7: Now we can work with the cookies. |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3989 | * Note that doing so might move headers in the request, but |
| 3990 | * the fields will stay coherent and the URI will not move. |
| 3991 | * This should only be performed in the backend. |
| 3992 | */ |
Willy Tarreau | fd39dda | 2008-10-17 12:01:58 +0200 | [diff] [blame] | 3993 | 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] | 3994 | && !(txn->flags & (TX_CLDENY|TX_CLTARPIT))) |
| 3995 | manage_client_side_cookies(s, req); |
Willy Tarreau | 7ac51f6 | 2007-03-25 16:00:04 +0200 | [diff] [blame] | 3996 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3997 | /* |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 3998 | * 8: the appsession cookie was looked up very early in 1.2, |
| 3999 | * so let's do the same now. |
| 4000 | */ |
| 4001 | |
Cyril Bonté | 47fdd8e | 2010-04-25 00:00:51 +0200 | [diff] [blame] | 4002 | /* It needs to look into the URI unless persistence must be ignored */ |
| 4003 | 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] | 4004 | 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] | 4005 | } |
| 4006 | |
William Lallemand | a73203e | 2012-03-12 12:48:57 +0100 | [diff] [blame] | 4007 | /* add unique-id if "header-unique-id" is specified */ |
| 4008 | |
William Lallemand | 5b7ea3a | 2013-08-28 15:44:19 +0200 | [diff] [blame] | 4009 | if (!LIST_ISEMPTY(&s->fe->format_unique_id)) { |
| 4010 | if ((s->unique_id = pool_alloc2(pool2_uniqueid)) == NULL) |
| 4011 | goto return_bad_req; |
| 4012 | s->unique_id[0] = '\0'; |
William Lallemand | a73203e | 2012-03-12 12:48:57 +0100 | [diff] [blame] | 4013 | 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] | 4014 | } |
William Lallemand | a73203e | 2012-03-12 12:48:57 +0100 | [diff] [blame] | 4015 | |
| 4016 | if (s->fe->header_unique_id && s->unique_id) { |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 4017 | chunk_printf(&trash, "%s: %s", s->fe->header_unique_id, s->unique_id); |
| 4018 | if (trash.len < 0) |
William Lallemand | a73203e | 2012-03-12 12:48:57 +0100 | [diff] [blame] | 4019 | goto return_bad_req; |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 4020 | 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] | 4021 | goto return_bad_req; |
| 4022 | } |
| 4023 | |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 4024 | /* |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4025 | * 9: add X-Forwarded-For if either the frontend or the backend |
| 4026 | * asks for it. |
| 4027 | */ |
| 4028 | if ((s->fe->options | s->be->options) & PR_O_FWDFOR) { |
Willy Tarreau | 87cf514 | 2011-08-19 22:57:24 +0200 | [diff] [blame] | 4029 | struct hdr_ctx ctx = { .idx = 0 }; |
Willy Tarreau | 87cf514 | 2011-08-19 22:57:24 +0200 | [diff] [blame] | 4030 | if (!((s->fe->options | s->be->options) & PR_O_FF_ALWAYS) && |
Cyril Bonté | a32d275 | 2012-05-29 23:27:41 +0200 | [diff] [blame] | 4031 | http_find_header2(s->be->fwdfor_hdr_len ? s->be->fwdfor_hdr_name : s->fe->fwdfor_hdr_name, |
| 4032 | 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] | 4033 | req->buf->p, &txn->hdr_idx, &ctx)) { |
Willy Tarreau | 87cf514 | 2011-08-19 22:57:24 +0200 | [diff] [blame] | 4034 | /* The header is set to be added only if none is present |
| 4035 | * and we found it, so don't do anything. |
| 4036 | */ |
| 4037 | } |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 4038 | else if (cli_conn && cli_conn->addr.from.ss_family == AF_INET) { |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4039 | /* Add an X-Forwarded-For header unless the source IP is |
| 4040 | * in the 'except' network range. |
| 4041 | */ |
| 4042 | if ((!s->fe->except_mask.s_addr || |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 4043 | (((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] | 4044 | != s->fe->except_net.s_addr) && |
| 4045 | (!s->be->except_mask.s_addr || |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 4046 | (((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] | 4047 | != s->be->except_net.s_addr)) { |
Willy Tarreau | 2a32428 | 2006-12-05 00:05:46 +0100 | [diff] [blame] | 4048 | int len; |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4049 | unsigned char *pn; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 4050 | pn = (unsigned char *)&((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr; |
Ross West | af72a1d | 2008-08-03 10:51:45 +0200 | [diff] [blame] | 4051 | |
| 4052 | /* Note: we rely on the backend to get the header name to be used for |
| 4053 | * x-forwarded-for, because the header is really meant for the backends. |
| 4054 | * However, if the backend did not specify any option, we have to rely |
| 4055 | * on the frontend's header name. |
| 4056 | */ |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4057 | if (s->be->fwdfor_hdr_len) { |
| 4058 | len = s->be->fwdfor_hdr_len; |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 4059 | memcpy(trash.str, s->be->fwdfor_hdr_name, len); |
Ross West | af72a1d | 2008-08-03 10:51:45 +0200 | [diff] [blame] | 4060 | } else { |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4061 | len = s->fe->fwdfor_hdr_len; |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 4062 | memcpy(trash.str, s->fe->fwdfor_hdr_name, len); |
Willy Tarreau | b86db34 | 2009-11-30 11:50:16 +0100 | [diff] [blame] | 4063 | } |
Willy Tarreau | e9187f8 | 2014-04-14 15:27:14 +0200 | [diff] [blame] | 4064 | len += snprintf(trash.str + len, trash.size - len, ": %d.%d.%d.%d", pn[0], pn[1], pn[2], pn[3]); |
Willy Tarreau | edcf668 | 2008-11-30 23:15:34 +0100 | [diff] [blame] | 4065 | |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 4066 | 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] | 4067 | goto return_bad_req; |
Willy Tarreau | 2a32428 | 2006-12-05 00:05:46 +0100 | [diff] [blame] | 4068 | } |
| 4069 | } |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 4070 | else if (cli_conn && cli_conn->addr.from.ss_family == AF_INET6) { |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4071 | /* FIXME: for the sake of completeness, we should also support |
| 4072 | * 'except' here, although it is mostly useless in this case. |
matt.farnsworth@nokia.com | 1c2ab96 | 2008-04-14 20:47:37 +0200 | [diff] [blame] | 4073 | */ |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4074 | int len; |
| 4075 | char pn[INET6_ADDRSTRLEN]; |
| 4076 | inet_ntop(AF_INET6, |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 4077 | (const void *)&((struct sockaddr_in6 *)(&cli_conn->addr.from))->sin6_addr, |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4078 | pn, sizeof(pn)); |
matt.farnsworth@nokia.com | 1c2ab96 | 2008-04-14 20:47:37 +0200 | [diff] [blame] | 4079 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4080 | /* Note: we rely on the backend to get the header name to be used for |
| 4081 | * x-forwarded-for, because the header is really meant for the backends. |
| 4082 | * However, if the backend did not specify any option, we have to rely |
| 4083 | * on the frontend's header name. |
matt.farnsworth@nokia.com | 1c2ab96 | 2008-04-14 20:47:37 +0200 | [diff] [blame] | 4084 | */ |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4085 | if (s->be->fwdfor_hdr_len) { |
| 4086 | len = s->be->fwdfor_hdr_len; |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 4087 | memcpy(trash.str, s->be->fwdfor_hdr_name, len); |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4088 | } else { |
| 4089 | len = s->fe->fwdfor_hdr_len; |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 4090 | memcpy(trash.str, s->fe->fwdfor_hdr_name, len); |
matt.farnsworth@nokia.com | 1c2ab96 | 2008-04-14 20:47:37 +0200 | [diff] [blame] | 4091 | } |
Willy Tarreau | e9187f8 | 2014-04-14 15:27:14 +0200 | [diff] [blame] | 4092 | len += snprintf(trash.str + len, trash.size - len, ": %s", pn); |
Willy Tarreau | adfb856 | 2008-08-11 15:24:42 +0200 | [diff] [blame] | 4093 | |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 4094 | 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] | 4095 | goto return_bad_req; |
| 4096 | } |
| 4097 | } |
| 4098 | |
| 4099 | /* |
Maik Broemme | 2850cb4 | 2009-04-17 18:53:21 +0200 | [diff] [blame] | 4100 | * 10: add X-Original-To if either the frontend or the backend |
| 4101 | * asks for it. |
| 4102 | */ |
| 4103 | if ((s->fe->options | s->be->options) & PR_O_ORGTO) { |
| 4104 | |
| 4105 | /* FIXME: don't know if IPv6 can handle that case too. */ |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 4106 | if (cli_conn && cli_conn->addr.from.ss_family == AF_INET) { |
Maik Broemme | 2850cb4 | 2009-04-17 18:53:21 +0200 | [diff] [blame] | 4107 | /* Add an X-Original-To header unless the destination IP is |
| 4108 | * in the 'except' network range. |
| 4109 | */ |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 4110 | conn_get_to_addr(cli_conn); |
Maik Broemme | 2850cb4 | 2009-04-17 18:53:21 +0200 | [diff] [blame] | 4111 | |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 4112 | if (cli_conn->addr.to.ss_family == AF_INET && |
Emeric Brun | 5bd86a8 | 2010-10-22 17:23:04 +0200 | [diff] [blame] | 4113 | ((!s->fe->except_mask_to.s_addr || |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 4114 | (((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] | 4115 | != s->fe->except_to.s_addr) && |
| 4116 | (!s->be->except_mask_to.s_addr || |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 4117 | (((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] | 4118 | != s->be->except_to.s_addr))) { |
Maik Broemme | 2850cb4 | 2009-04-17 18:53:21 +0200 | [diff] [blame] | 4119 | int len; |
| 4120 | unsigned char *pn; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 4121 | pn = (unsigned char *)&((struct sockaddr_in *)&cli_conn->addr.to)->sin_addr; |
Maik Broemme | 2850cb4 | 2009-04-17 18:53:21 +0200 | [diff] [blame] | 4122 | |
| 4123 | /* Note: we rely on the backend to get the header name to be used for |
| 4124 | * x-original-to, because the header is really meant for the backends. |
| 4125 | * However, if the backend did not specify any option, we have to rely |
| 4126 | * on the frontend's header name. |
| 4127 | */ |
| 4128 | if (s->be->orgto_hdr_len) { |
| 4129 | len = s->be->orgto_hdr_len; |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 4130 | memcpy(trash.str, s->be->orgto_hdr_name, len); |
Maik Broemme | 2850cb4 | 2009-04-17 18:53:21 +0200 | [diff] [blame] | 4131 | } else { |
| 4132 | len = s->fe->orgto_hdr_len; |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 4133 | memcpy(trash.str, s->fe->orgto_hdr_name, len); |
Willy Tarreau | b86db34 | 2009-11-30 11:50:16 +0100 | [diff] [blame] | 4134 | } |
Willy Tarreau | e9187f8 | 2014-04-14 15:27:14 +0200 | [diff] [blame] | 4135 | len += snprintf(trash.str + len, trash.size - len, ": %d.%d.%d.%d", pn[0], pn[1], pn[2], pn[3]); |
Maik Broemme | 2850cb4 | 2009-04-17 18:53:21 +0200 | [diff] [blame] | 4136 | |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 4137 | 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] | 4138 | goto return_bad_req; |
| 4139 | } |
| 4140 | } |
| 4141 | } |
| 4142 | |
Willy Tarreau | 50fc777 | 2012-11-11 22:19:57 +0100 | [diff] [blame] | 4143 | /* 11: add "Connection: close" or "Connection: keep-alive" if needed and not yet set. |
| 4144 | * If an "Upgrade" token is found, the header is left untouched in order not to have |
| 4145 | * to deal with some servers bugs : some of them fail an Upgrade if anything but |
| 4146 | * "Upgrade" is present in the Connection header. |
| 4147 | */ |
| 4148 | if (!(txn->flags & TX_HDR_CONN_UPG) && |
| 4149 | (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) || |
Willy Tarreau | 02bce8b | 2014-01-30 00:15:28 +0100 | [diff] [blame] | 4150 | ((s->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL || |
| 4151 | (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL))) { |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 4152 | unsigned int want_flags = 0; |
| 4153 | |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 4154 | if (msg->flags & HTTP_MSGF_VER_11) { |
Willy Tarreau | 22a9534 | 2010-09-29 14:31:41 +0200 | [diff] [blame] | 4155 | if (((txn->flags & TX_CON_WANT_MSK) >= TX_CON_WANT_SCL || |
Willy Tarreau | 02bce8b | 2014-01-30 00:15:28 +0100 | [diff] [blame] | 4156 | ((s->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL || |
| 4157 | (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL)) && |
Willy Tarreau | 22a9534 | 2010-09-29 14:31:41 +0200 | [diff] [blame] | 4158 | !((s->fe->options2|s->be->options2) & PR_O2_FAKE_KA)) |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 4159 | want_flags |= TX_CON_CLO_SET; |
| 4160 | } else { |
Willy Tarreau | 22a9534 | 2010-09-29 14:31:41 +0200 | [diff] [blame] | 4161 | if (((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL && |
Willy Tarreau | 02bce8b | 2014-01-30 00:15:28 +0100 | [diff] [blame] | 4162 | ((s->fe->options & PR_O_HTTP_MODE) != PR_O_HTTP_PCL && |
| 4163 | (s->be->options & PR_O_HTTP_MODE) != PR_O_HTTP_PCL)) || |
Willy Tarreau | 22a9534 | 2010-09-29 14:31:41 +0200 | [diff] [blame] | 4164 | ((s->fe->options2|s->be->options2) & PR_O2_FAKE_KA)) |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 4165 | want_flags |= TX_CON_KAL_SET; |
| 4166 | } |
| 4167 | |
| 4168 | 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] | 4169 | http_change_connection_header(txn, msg, want_flags); |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4170 | } |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 4171 | |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 4172 | |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 4173 | /* If we have no server assigned yet and we're balancing on url_param |
| 4174 | * with a POST request, we may be interested in checking the body for |
| 4175 | * that parameter. This will be done in another analyser. |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4176 | */ |
| 4177 | if (!(s->flags & (SN_ASSIGNED|SN_DIRECT)) && |
| 4178 | s->txn.meth == HTTP_METH_POST && s->be->url_param_name != NULL && |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 4179 | (msg->flags & (HTTP_MSGF_CNT_LEN|HTTP_MSGF_TE_CHNK))) { |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 4180 | channel_dont_connect(req); |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 4181 | req->analysers |= AN_REQ_HTTP_BODY; |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4182 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 4183 | |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 4184 | if (msg->flags & HTTP_MSGF_XFER_LEN) { |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4185 | req->analysers |= AN_REQ_HTTP_XFER_BODY; |
Willy Tarreau | 5e20552 | 2011-12-17 16:34:27 +0100 | [diff] [blame] | 4186 | #ifdef TCP_QUICKACK |
| 4187 | /* We expect some data from the client. Unless we know for sure |
| 4188 | * we already have a full request, we have to re-enable quick-ack |
| 4189 | * in case we previously disabled it, otherwise we might cause |
| 4190 | * the client to delay further data. |
| 4191 | */ |
| 4192 | if ((s->listener->options & LI_O_NOQUICKACK) && |
Willy Tarreau | 3c72872 | 2014-01-23 13:50:42 +0100 | [diff] [blame] | 4193 | cli_conn && conn_ctrl_ready(cli_conn) && |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 4194 | ((msg->flags & HTTP_MSGF_TE_CHNK) || |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 4195 | (msg->body_len > req->buf->i - txn->req.eoh - 2))) |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 4196 | 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] | 4197 | #endif |
| 4198 | } |
Willy Tarreau | 0394594 | 2009-12-22 16:50:27 +0100 | [diff] [blame] | 4199 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4200 | /************************************************************* |
| 4201 | * OK, that's finished for the headers. We have done what we * |
| 4202 | * could. Let's switch to the DATA state. * |
| 4203 | ************************************************************/ |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 4204 | req->analyse_exp = TICK_ETERNITY; |
| 4205 | req->analysers &= ~an_bit; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 4206 | |
Willy Tarreau | 7bb68ab | 2012-05-13 14:48:59 +0200 | [diff] [blame] | 4207 | /* if the server closes the connection, we want to immediately react |
| 4208 | * and close the socket to save packets and syscalls. |
| 4209 | */ |
Willy Tarreau | 40f151a | 2012-12-20 12:10:09 +0100 | [diff] [blame] | 4210 | if (!(req->analysers & AN_REQ_HTTP_XFER_BODY)) |
| 4211 | req->cons->flags |= SI_FL_NOHALF; |
Willy Tarreau | 7bb68ab | 2012-05-13 14:48:59 +0200 | [diff] [blame] | 4212 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4213 | s->logs.tv_request = now; |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4214 | /* OK let's go on with the BODY now */ |
| 4215 | return 1; |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 4216 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4217 | return_bad_req: /* let's centralize all bad requests */ |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 4218 | if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) { |
Willy Tarreau | f073a83 | 2009-03-01 23:21:47 +0100 | [diff] [blame] | 4219 | /* we detected a parsing error. We want to archive this request |
| 4220 | * in the dedicated proxy area for later troubleshooting. |
| 4221 | */ |
Willy Tarreau | 8a0cef2 | 2012-03-09 13:39:23 +0100 | [diff] [blame] | 4222 | 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] | 4223 | } |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 4224 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4225 | txn->req.msg_state = HTTP_MSG_ERROR; |
| 4226 | txn->status = 400; |
| 4227 | req->analysers = 0; |
Willy Tarreau | 783f258 | 2012-09-04 12:19:04 +0200 | [diff] [blame] | 4228 | 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] | 4229 | |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 4230 | s->fe->fe_counters.failed_req++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 4231 | if (s->listener->counters) |
| 4232 | s->listener->counters->failed_req++; |
Willy Tarreau | adfb856 | 2008-08-11 15:24:42 +0200 | [diff] [blame] | 4233 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4234 | if (!(s->flags & SN_ERR_MASK)) |
| 4235 | s->flags |= SN_ERR_PRXCOND; |
| 4236 | if (!(s->flags & SN_FINST_MASK)) |
| 4237 | s->flags |= SN_FINST_R; |
Willy Tarreau | dafde43 | 2008-08-17 01:00:46 +0200 | [diff] [blame] | 4238 | return 0; |
Willy Tarreau | c65a3ba | 2008-08-11 23:42:50 +0200 | [diff] [blame] | 4239 | } |
Willy Tarreau | adfb856 | 2008-08-11 15:24:42 +0200 | [diff] [blame] | 4240 | |
Willy Tarreau | 60b85b0 | 2008-11-30 23:28:40 +0100 | [diff] [blame] | 4241 | /* This function is an analyser which processes the HTTP tarpit. It always |
| 4242 | * returns zero, at the beginning because it prevents any other processing |
| 4243 | * from occurring, and at the end because it terminates the request. |
| 4244 | */ |
Willy Tarreau | 7421efb | 2012-07-02 15:11:27 +0200 | [diff] [blame] | 4245 | 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] | 4246 | { |
| 4247 | struct http_txn *txn = &s->txn; |
| 4248 | |
| 4249 | /* This connection is being tarpitted. The CLIENT side has |
| 4250 | * already set the connect expiration date to the right |
| 4251 | * timeout. We just have to check that the client is still |
| 4252 | * there and that the timeout has not expired. |
| 4253 | */ |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 4254 | channel_dont_connect(req); |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 4255 | if ((req->flags & (CF_SHUTR|CF_READ_ERROR)) == 0 && |
Willy Tarreau | 60b85b0 | 2008-11-30 23:28:40 +0100 | [diff] [blame] | 4256 | !tick_is_expired(req->analyse_exp, now_ms)) |
| 4257 | return 0; |
| 4258 | |
| 4259 | /* We will set the queue timer to the time spent, just for |
| 4260 | * logging purposes. We fake a 500 server error, so that the |
| 4261 | * attacker will not suspect his connection has been tarpitted. |
| 4262 | * It will not cause trouble to the logs because we can exclude |
| 4263 | * the tarpitted connections by filtering on the 'PT' status flags. |
| 4264 | */ |
Willy Tarreau | 60b85b0 | 2008-11-30 23:28:40 +0100 | [diff] [blame] | 4265 | s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now); |
| 4266 | |
| 4267 | txn->status = 500; |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 4268 | if (!(req->flags & CF_READ_ERROR)) |
Willy Tarreau | 783f258 | 2012-09-04 12:19:04 +0200 | [diff] [blame] | 4269 | stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_500)); |
Willy Tarreau | 60b85b0 | 2008-11-30 23:28:40 +0100 | [diff] [blame] | 4270 | |
| 4271 | req->analysers = 0; |
| 4272 | req->analyse_exp = TICK_ETERNITY; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 4273 | |
Willy Tarreau | 60b85b0 | 2008-11-30 23:28:40 +0100 | [diff] [blame] | 4274 | if (!(s->flags & SN_ERR_MASK)) |
| 4275 | s->flags |= SN_ERR_PRXCOND; |
| 4276 | if (!(s->flags & SN_FINST_MASK)) |
| 4277 | s->flags |= SN_FINST_T; |
| 4278 | return 0; |
| 4279 | } |
| 4280 | |
Willy Tarreau | 5a8f947 | 2014-04-10 11:16:06 +0200 | [diff] [blame^] | 4281 | /* This function is an analyser which waits for the HTTP request body. It waits |
| 4282 | * for either the buffer to be full, or the full advertised contents to have |
| 4283 | * reached the buffer. It must only be called after the standard HTTP request |
| 4284 | * processing has occurred, because it expects the request to be parsed and will |
| 4285 | * look for the Expect header. It may send a 100-Continue interim response. It |
| 4286 | * takes in input any state starting from HTTP_MSG_BODY and leaves with one of |
| 4287 | * HTTP_MSG_CHK_SIZE, HTTP_MSG_DATA or HTTP_MSG_TRAILERS. It returns zero if it |
| 4288 | * needs to read more data, or 1 once it has completed its analysis. |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 4289 | */ |
Willy Tarreau | 5a8f947 | 2014-04-10 11:16:06 +0200 | [diff] [blame^] | 4290 | int http_wait_for_request_body(struct session *s, struct channel *req, int an_bit) |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 4291 | { |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 4292 | struct http_txn *txn = &s->txn; |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 4293 | struct http_msg *msg = &s->txn.req; |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 4294 | |
| 4295 | /* We have to parse the HTTP request body to find any required data. |
| 4296 | * "balance url_param check_post" should have been the only way to get |
| 4297 | * into this. We were brought here after HTTP header analysis, so all |
| 4298 | * related structures are ready. |
| 4299 | */ |
| 4300 | |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 4301 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) |
| 4302 | goto missing_data; |
| 4303 | |
| 4304 | if (msg->msg_state < HTTP_MSG_100_SENT) { |
| 4305 | /* If we have HTTP/1.1 and Expect: 100-continue, then we must |
| 4306 | * send an HTTP/1.1 100 Continue intermediate response. |
| 4307 | */ |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 4308 | if (msg->flags & HTTP_MSGF_VER_11) { |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 4309 | struct hdr_ctx ctx; |
| 4310 | ctx.idx = 0; |
| 4311 | /* Expect is allowed in 1.1, look for it */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 4312 | 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] | 4313 | 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] | 4314 | bo_inject(s->rep, http_100_chunk.str, http_100_chunk.len); |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 4315 | } |
| 4316 | } |
| 4317 | msg->msg_state = HTTP_MSG_100_SENT; |
| 4318 | } |
| 4319 | |
| 4320 | if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) { |
Willy Tarreau | fa4a03c | 2012-03-09 21:28:54 +0100 | [diff] [blame] | 4321 | /* 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] | 4322 | * 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] | 4323 | * is still null. We must save the body in msg->next because it |
| 4324 | * survives buffer re-alignments. |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4325 | */ |
Willy Tarreau | ea1175a | 2012-03-05 15:52:30 +0100 | [diff] [blame] | 4326 | msg->next = msg->sov; |
Willy Tarreau | a458b67 | 2012-03-05 11:17:50 +0100 | [diff] [blame] | 4327 | |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 4328 | if (msg->flags & HTTP_MSGF_TE_CHNK) |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 4329 | msg->msg_state = HTTP_MSG_CHUNK_SIZE; |
| 4330 | else |
| 4331 | msg->msg_state = HTTP_MSG_DATA; |
| 4332 | } |
| 4333 | |
| 4334 | if (msg->msg_state == HTTP_MSG_CHUNK_SIZE) { |
Willy Tarreau | 124d991 | 2011-03-01 20:30:48 +0100 | [diff] [blame] | 4335 | /* read the chunk size and assign it to ->chunk_len, then |
Willy Tarreau | a458b67 | 2012-03-05 11:17:50 +0100 | [diff] [blame] | 4336 | * 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] | 4337 | * TRAILERS state. |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 4338 | */ |
Willy Tarreau | 4baf44b | 2012-03-09 14:10:20 +0100 | [diff] [blame] | 4339 | int ret = http_parse_chunk_size(msg); |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 4340 | |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 4341 | if (!ret) |
| 4342 | goto missing_data; |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 4343 | else if (ret < 0) { |
| 4344 | session_inc_http_err_ctr(s); |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 4345 | goto return_bad_req; |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 4346 | } |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 4347 | } |
| 4348 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4349 | /* Now we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state. |
Willy Tarreau | fa4a03c | 2012-03-09 21:28:54 +0100 | [diff] [blame] | 4350 | * We have the first data byte is in msg->sov. We're waiting for at |
Willy Tarreau | 226071e | 2014-04-10 11:55:45 +0200 | [diff] [blame] | 4351 | * least a whole chunk or the whole content length bytes after msg->sov. |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 4352 | */ |
Willy Tarreau | 226071e | 2014-04-10 11:55:45 +0200 | [diff] [blame] | 4353 | if (req->buf->i - msg->sov >= msg->body_len) /* we have enough bytes now */ |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 4354 | goto http_end; |
| 4355 | |
| 4356 | missing_data: |
Willy Tarreau | 31a1995 | 2014-04-10 11:50:37 +0200 | [diff] [blame] | 4357 | /* we get here if we need to wait for more data. If the buffer is full, |
| 4358 | * we have the maximum we can expect. |
| 4359 | */ |
| 4360 | if (buffer_full(req->buf, global.tune.maxrewrite)) |
| 4361 | goto http_end; |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 4362 | |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 4363 | 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] | 4364 | txn->status = 408; |
Willy Tarreau | 783f258 | 2012-09-04 12:19:04 +0200 | [diff] [blame] | 4365 | stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_408)); |
Willy Tarreau | 79ebac6 | 2010-06-07 13:47:49 +0200 | [diff] [blame] | 4366 | |
| 4367 | if (!(s->flags & SN_ERR_MASK)) |
| 4368 | s->flags |= SN_ERR_CLITO; |
| 4369 | if (!(s->flags & SN_FINST_MASK)) |
| 4370 | s->flags |= SN_FINST_D; |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 4371 | goto return_err_msg; |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 4372 | } |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 4373 | |
| 4374 | /* we get here if we need to wait for more data */ |
Willy Tarreau | 31a1995 | 2014-04-10 11:50:37 +0200 | [diff] [blame] | 4375 | if (!(req->flags & (CF_SHUTR | CF_READ_ERROR))) { |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 4376 | /* Not enough data. We'll re-use the http-request |
| 4377 | * timeout here. Ideally, we should set the timeout |
| 4378 | * relative to the accept() date. We just set the |
| 4379 | * request timeout once at the beginning of the |
| 4380 | * request. |
| 4381 | */ |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 4382 | channel_dont_connect(req); |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 4383 | if (!tick_isset(req->analyse_exp)) |
Willy Tarreau | cd7afc0 | 2009-07-12 10:03:17 +0200 | [diff] [blame] | 4384 | req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.httpreq); |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 4385 | return 0; |
| 4386 | } |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 4387 | |
| 4388 | http_end: |
| 4389 | /* The situation will not evolve, so let's give up on the analysis. */ |
| 4390 | s->logs.tv_request = now; /* update the request timer to reflect full request */ |
| 4391 | req->analysers &= ~an_bit; |
| 4392 | req->analyse_exp = TICK_ETERNITY; |
| 4393 | return 1; |
| 4394 | |
| 4395 | return_bad_req: /* let's centralize all bad requests */ |
| 4396 | txn->req.msg_state = HTTP_MSG_ERROR; |
| 4397 | txn->status = 400; |
Willy Tarreau | 783f258 | 2012-09-04 12:19:04 +0200 | [diff] [blame] | 4398 | stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_400)); |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 4399 | |
Willy Tarreau | 79ebac6 | 2010-06-07 13:47:49 +0200 | [diff] [blame] | 4400 | if (!(s->flags & SN_ERR_MASK)) |
| 4401 | s->flags |= SN_ERR_PRXCOND; |
| 4402 | if (!(s->flags & SN_FINST_MASK)) |
| 4403 | s->flags |= SN_FINST_R; |
| 4404 | |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 4405 | return_err_msg: |
| 4406 | req->analysers = 0; |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 4407 | s->fe->fe_counters.failed_req++; |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 4408 | if (s->listener->counters) |
| 4409 | s->listener->counters->failed_req++; |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 4410 | return 0; |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 4411 | } |
| 4412 | |
Willy Tarreau | d1de8af | 2012-05-18 22:12:14 +0200 | [diff] [blame] | 4413 | /* send a server's name with an outgoing request over an established connection. |
| 4414 | * Note: this function is designed to be called once the request has been scheduled |
| 4415 | * for being forwarded. This is the reason why it rewinds the buffer before |
| 4416 | * proceeding. |
| 4417 | */ |
Willy Tarreau | 45c0d98 | 2012-03-09 12:11:57 +0100 | [diff] [blame] | 4418 | 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] | 4419 | |
| 4420 | struct hdr_ctx ctx; |
| 4421 | |
Mark Lamourine | c2247f0 | 2012-01-04 13:02:01 -0500 | [diff] [blame] | 4422 | char *hdr_name = be->server_id_hdr_name; |
| 4423 | int hdr_name_len = be->server_id_hdr_len; |
Willy Tarreau | 394db37 | 2012-10-12 22:40:39 +0200 | [diff] [blame] | 4424 | struct channel *chn = txn->req.chn; |
Mark Lamourine | c2247f0 | 2012-01-04 13:02:01 -0500 | [diff] [blame] | 4425 | char *hdr_val; |
Willy Tarreau | d1de8af | 2012-05-18 22:12:14 +0200 | [diff] [blame] | 4426 | unsigned int old_o, old_i; |
Mark Lamourine | c2247f0 | 2012-01-04 13:02:01 -0500 | [diff] [blame] | 4427 | |
William Lallemand | d9e9066 | 2012-01-30 17:27:17 +0100 | [diff] [blame] | 4428 | ctx.idx = 0; |
| 4429 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 4430 | old_o = chn->buf->o; |
Willy Tarreau | d1de8af | 2012-05-18 22:12:14 +0200 | [diff] [blame] | 4431 | if (old_o) { |
| 4432 | /* The request was already skipped, let's restore it */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 4433 | b_rew(chn->buf, old_o); |
Willy Tarreau | d1de8af | 2012-05-18 22:12:14 +0200 | [diff] [blame] | 4434 | } |
| 4435 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 4436 | old_i = chn->buf->i; |
| 4437 | 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] | 4438 | /* remove any existing values from the header */ |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 4439 | http_remove_header2(&txn->req, &txn->hdr_idx, &ctx); |
Mark Lamourine | c2247f0 | 2012-01-04 13:02:01 -0500 | [diff] [blame] | 4440 | } |
| 4441 | |
| 4442 | /* Add the new header requested with the server value */ |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 4443 | hdr_val = trash.str; |
Mark Lamourine | c2247f0 | 2012-01-04 13:02:01 -0500 | [diff] [blame] | 4444 | memcpy(hdr_val, hdr_name, hdr_name_len); |
| 4445 | hdr_val += hdr_name_len; |
| 4446 | *hdr_val++ = ':'; |
| 4447 | *hdr_val++ = ' '; |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 4448 | hdr_val += strlcpy2(hdr_val, srv_name, trash.str + trash.size - hdr_val); |
| 4449 | 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] | 4450 | |
Willy Tarreau | d1de8af | 2012-05-18 22:12:14 +0200 | [diff] [blame] | 4451 | if (old_o) { |
| 4452 | /* If this was a forwarded request, we must readjust the amount of |
| 4453 | * data to be forwarded in order to take into account the size |
Willy Tarreau | 2fef9b1 | 2013-03-26 01:08:21 +0100 | [diff] [blame] | 4454 | * variations. Note that if the request was already scheduled for |
| 4455 | * forwarding, it had its req->sol pointing to the body, which |
| 4456 | * must then be updated too. |
Willy Tarreau | d1de8af | 2012-05-18 22:12:14 +0200 | [diff] [blame] | 4457 | */ |
Willy Tarreau | 2fef9b1 | 2013-03-26 01:08:21 +0100 | [diff] [blame] | 4458 | txn->req.sol += chn->buf->i - old_i; |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 4459 | b_adv(chn->buf, old_o + chn->buf->i - old_i); |
Willy Tarreau | d1de8af | 2012-05-18 22:12:14 +0200 | [diff] [blame] | 4460 | } |
| 4461 | |
Mark Lamourine | c2247f0 | 2012-01-04 13:02:01 -0500 | [diff] [blame] | 4462 | return 0; |
| 4463 | } |
| 4464 | |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4465 | /* Terminate current transaction and prepare a new one. This is very tricky |
| 4466 | * right now but it works. |
| 4467 | */ |
| 4468 | void http_end_txn_clean_session(struct session *s) |
| 4469 | { |
Willy Tarreau | 068621e | 2013-12-23 15:11:25 +0100 | [diff] [blame] | 4470 | int prev_status = s->txn.status; |
| 4471 | |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4472 | /* FIXME: We need a more portable way of releasing a backend's and a |
| 4473 | * server's connections. We need a safer way to reinitialize buffer |
| 4474 | * flags. We also need a more accurate method for computing per-request |
| 4475 | * data. |
| 4476 | */ |
| 4477 | http_silent_debug(__LINE__, s); |
| 4478 | |
Willy Tarreau | 4213a11 | 2013-12-15 10:25:42 +0100 | [diff] [blame] | 4479 | /* unless we're doing keep-alive, we want to quickly close the connection |
| 4480 | * to the server. |
| 4481 | */ |
| 4482 | if (((s->txn.flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) || |
| 4483 | !si_conn_ready(s->req->cons)) { |
| 4484 | s->req->cons->flags |= SI_FL_NOLINGER | SI_FL_NOHALF; |
| 4485 | si_shutr(s->req->cons); |
| 4486 | si_shutw(s->req->cons); |
| 4487 | } |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4488 | |
| 4489 | http_silent_debug(__LINE__, s); |
| 4490 | |
Willy Tarreau | 2d5cd47 | 2012-03-01 23:34:37 +0100 | [diff] [blame] | 4491 | if (s->flags & SN_BE_ASSIGNED) { |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4492 | s->be->beconn--; |
Willy Tarreau | 2d5cd47 | 2012-03-01 23:34:37 +0100 | [diff] [blame] | 4493 | if (unlikely(s->srv_conn)) |
| 4494 | sess_change_server(s, NULL); |
| 4495 | } |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4496 | |
| 4497 | s->logs.t_close = tv_ms_elapsed(&s->logs.tv_accept, &now); |
| 4498 | session_process_counters(s); |
Willy Tarreau | f333834 | 2014-01-28 21:40:28 +0100 | [diff] [blame] | 4499 | session_stop_content_counters(s); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4500 | |
| 4501 | if (s->txn.status) { |
| 4502 | int n; |
| 4503 | |
| 4504 | n = s->txn.status / 100; |
| 4505 | if (n < 1 || n > 5) |
| 4506 | n = 0; |
| 4507 | |
Willy Tarreau | 5e16cbc | 2012-11-24 14:54:13 +0100 | [diff] [blame] | 4508 | if (s->fe->mode == PR_MODE_HTTP) { |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 4509 | s->fe->fe_counters.p.http.rsp[n]++; |
Willy Tarreau | 8139b99 | 2012-11-27 07:35:31 +0100 | [diff] [blame] | 4510 | if (s->comp_algo && (s->flags & SN_COMP_READY)) |
Willy Tarreau | 5e16cbc | 2012-11-24 14:54:13 +0100 | [diff] [blame] | 4511 | s->fe->fe_counters.p.http.comp_rsp++; |
| 4512 | } |
Willy Tarreau | 2465779 | 2010-02-26 10:30:28 +0100 | [diff] [blame] | 4513 | if ((s->flags & SN_BE_ASSIGNED) && |
Willy Tarreau | 5e16cbc | 2012-11-24 14:54:13 +0100 | [diff] [blame] | 4514 | (s->be->mode == PR_MODE_HTTP)) { |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 4515 | s->be->be_counters.p.http.rsp[n]++; |
Willy Tarreau | 5e16cbc | 2012-11-24 14:54:13 +0100 | [diff] [blame] | 4516 | s->be->be_counters.p.http.cum_req++; |
Willy Tarreau | 8139b99 | 2012-11-27 07:35:31 +0100 | [diff] [blame] | 4517 | if (s->comp_algo && (s->flags & SN_COMP_READY)) |
Willy Tarreau | 5e16cbc | 2012-11-24 14:54:13 +0100 | [diff] [blame] | 4518 | s->be->be_counters.p.http.comp_rsp++; |
| 4519 | } |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4520 | } |
| 4521 | |
| 4522 | /* don't count other requests' data */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 4523 | s->logs.bytes_in -= s->req->buf->i; |
| 4524 | s->logs.bytes_out -= s->rep->buf->i; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4525 | |
| 4526 | /* let's do a final log if we need it */ |
Willy Tarreau | d79a3b2 | 2012-12-28 09:40:16 +0100 | [diff] [blame] | 4527 | if (!LIST_ISEMPTY(&s->fe->logformat) && s->logs.logwait && |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4528 | !(s->flags & SN_MONITOR) && |
| 4529 | (!(s->fe->options & PR_O_NULLNOLOG) || s->req->total)) { |
| 4530 | s->do_log(s); |
| 4531 | } |
| 4532 | |
| 4533 | s->logs.accept_date = date; /* user-visible date for logging */ |
| 4534 | s->logs.tv_accept = now; /* corrected date for internal use */ |
| 4535 | tv_zero(&s->logs.tv_request); |
| 4536 | s->logs.t_queue = -1; |
| 4537 | s->logs.t_connect = -1; |
| 4538 | s->logs.t_data = -1; |
| 4539 | s->logs.t_close = 0; |
| 4540 | s->logs.prx_queue_size = 0; /* we get the number of pending conns before us */ |
| 4541 | s->logs.srv_queue_size = 0; /* we will get this number soon */ |
| 4542 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 4543 | s->logs.bytes_in = s->req->total = s->req->buf->i; |
| 4544 | s->logs.bytes_out = s->rep->total = s->rep->buf->i; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4545 | |
| 4546 | if (s->pend_pos) |
| 4547 | pendconn_free(s->pend_pos); |
| 4548 | |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 4549 | if (objt_server(s->target)) { |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4550 | if (s->flags & SN_CURR_SESS) { |
| 4551 | s->flags &= ~SN_CURR_SESS; |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 4552 | objt_server(s->target)->cur_sess--; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4553 | } |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 4554 | if (may_dequeue_tasks(objt_server(s->target), s->be)) |
| 4555 | process_srv_queue(objt_server(s->target)); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4556 | } |
| 4557 | |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 4558 | s->target = NULL; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4559 | |
Willy Tarreau | 4213a11 | 2013-12-15 10:25:42 +0100 | [diff] [blame] | 4560 | /* only release our endpoint if we don't intend to reuse the |
| 4561 | * connection. |
| 4562 | */ |
| 4563 | if (((s->txn.flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) || |
| 4564 | !si_conn_ready(s->req->cons)) { |
| 4565 | si_release_endpoint(s->req->cons); |
| 4566 | } |
| 4567 | |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4568 | s->req->cons->state = s->req->cons->prev_state = SI_ST_INI; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4569 | s->req->cons->err_type = SI_ET_NONE; |
Willy Tarreau | 0b3a411 | 2011-03-27 19:16:56 +0200 | [diff] [blame] | 4570 | s->req->cons->conn_retries = 0; /* used for logging too */ |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4571 | s->req->cons->exp = TICK_ETERNITY; |
Willy Tarreau | c920096 | 2013-12-31 23:03:09 +0100 | [diff] [blame] | 4572 | 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] | 4573 | s->req->flags &= ~(CF_SHUTW|CF_SHUTW_NOW|CF_AUTO_CONNECT|CF_WRITE_ERROR|CF_STREAMER|CF_STREAMER_FAST|CF_NEVER_WAIT); |
| 4574 | 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] | 4575 | s->flags &= ~(SN_DIRECT|SN_ASSIGNED|SN_ADDR_SET|SN_BE_ASSIGNED|SN_FORCE_PRST|SN_IGNORE_PRST); |
Willy Tarreau | 3634624 | 2014-02-24 18:26:30 +0100 | [diff] [blame] | 4576 | s->flags &= ~(SN_CURR_SESS|SN_REDIRECTABLE|SN_SRV_REUSED); |
Willy Tarreau | 543db62 | 2012-11-15 16:41:22 +0100 | [diff] [blame] | 4577 | |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4578 | s->txn.meth = 0; |
| 4579 | http_reset_txn(s); |
Willy Tarreau | fcffa69 | 2010-01-10 14:21:19 +0100 | [diff] [blame] | 4580 | s->txn.flags |= TX_NOT_FIRST | TX_WAIT_NEXT_RQ; |
Willy Tarreau | 068621e | 2013-12-23 15:11:25 +0100 | [diff] [blame] | 4581 | |
| 4582 | if (prev_status == 401 || prev_status == 407) { |
| 4583 | /* In HTTP keep-alive mode, if we receive a 401, we still have |
| 4584 | * a chance of being able to send the visitor again to the same |
| 4585 | * server over the same connection. This is required by some |
| 4586 | * broken protocols such as NTLM, and anyway whenever there is |
| 4587 | * an opportunity for sending the challenge to the proper place, |
| 4588 | * it's better to do it (at least it helps with debugging). |
| 4589 | */ |
| 4590 | s->txn.flags |= TX_PREFER_LAST; |
| 4591 | } |
| 4592 | |
Willy Tarreau | ee55dc0 | 2010-06-01 10:56:34 +0200 | [diff] [blame] | 4593 | if (s->fe->options2 & PR_O2_INDEPSTR) |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4594 | s->req->cons->flags |= SI_FL_INDEP_STR; |
| 4595 | |
Willy Tarreau | 96e3121 | 2011-05-30 18:10:30 +0200 | [diff] [blame] | 4596 | if (s->fe->options2 & PR_O2_NODELAY) { |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 4597 | s->req->flags |= CF_NEVER_WAIT; |
| 4598 | s->rep->flags |= CF_NEVER_WAIT; |
Willy Tarreau | 96e3121 | 2011-05-30 18:10:30 +0200 | [diff] [blame] | 4599 | } |
| 4600 | |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4601 | /* if the request buffer is not empty, it means we're |
| 4602 | * about to process another request, so send pending |
| 4603 | * data with MSG_MORE to merge TCP packets when possible. |
Willy Tarreau | 065e833 | 2010-01-08 00:30:20 +0100 | [diff] [blame] | 4604 | * Just don't do this if the buffer is close to be full, |
| 4605 | * because the request will wait for it to flush a little |
| 4606 | * bit before proceeding. |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4607 | */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 4608 | if (s->req->buf->i) { |
| 4609 | if (s->rep->buf->o && |
| 4610 | !buffer_full(s->rep->buf, global.tune.maxrewrite) && |
| 4611 | 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] | 4612 | s->rep->flags |= CF_EXPECT_MORE; |
Willy Tarreau | 065e833 | 2010-01-08 00:30:20 +0100 | [diff] [blame] | 4613 | } |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 4614 | |
| 4615 | /* we're removing the analysers, we MUST re-enable events detection */ |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 4616 | channel_auto_read(s->req); |
| 4617 | channel_auto_close(s->req); |
| 4618 | channel_auto_read(s->rep); |
| 4619 | channel_auto_close(s->rep); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4620 | |
Willy Tarreau | 2737562 | 2013-12-17 00:00:28 +0100 | [diff] [blame] | 4621 | /* we're in keep-alive with an idle connection, monitor it */ |
| 4622 | si_idle_conn(s->req->cons); |
| 4623 | |
Willy Tarreau | 342b11c | 2010-11-24 16:22:09 +0100 | [diff] [blame] | 4624 | s->req->analysers = s->listener->analysers; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4625 | s->rep->analysers = 0; |
| 4626 | |
| 4627 | http_silent_debug(__LINE__, s); |
| 4628 | } |
| 4629 | |
| 4630 | |
| 4631 | /* This function updates the request state machine according to the response |
| 4632 | * state machine and buffer flags. It returns 1 if it changes anything (flag |
| 4633 | * or state), otherwise zero. It ignores any state before HTTP_MSG_DONE, as |
| 4634 | * it is only used to find when a request/response couple is complete. Both |
| 4635 | * this function and its equivalent should loop until both return zero. It |
| 4636 | * can set its own state to DONE, CLOSING, CLOSED, TUNNEL, ERROR. |
| 4637 | */ |
| 4638 | int http_sync_req_state(struct session *s) |
| 4639 | { |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 4640 | struct channel *chn = s->req; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4641 | struct http_txn *txn = &s->txn; |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 4642 | unsigned int old_flags = chn->flags; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4643 | unsigned int old_state = txn->req.msg_state; |
| 4644 | |
| 4645 | http_silent_debug(__LINE__, s); |
| 4646 | if (unlikely(txn->req.msg_state < HTTP_MSG_BODY)) |
| 4647 | return 0; |
| 4648 | |
| 4649 | if (txn->req.msg_state == HTTP_MSG_DONE) { |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 4650 | /* No need to read anymore, the request was completely parsed. |
Willy Tarreau | 58bd8fd | 2010-09-28 14:16:41 +0200 | [diff] [blame] | 4651 | * We can shut the read side unless we want to abort_on_close, |
| 4652 | * or we have a POST request. The issue with POST requests is |
| 4653 | * that some browsers still send a CRLF after the request, and |
| 4654 | * this CRLF must be read so that it does not remain in the kernel |
| 4655 | * buffers, otherwise a close could cause an RST on some systems |
| 4656 | * (eg: Linux). |
Willy Tarreau | 3988d93 | 2013-12-27 23:03:08 +0100 | [diff] [blame] | 4657 | * Note that if we're using keep-alive on the client side, we'd |
| 4658 | * rather poll now and keep the polling enabled for the whole |
| 4659 | * session's life than enabling/disabling it between each |
| 4660 | * response and next request. |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 4661 | */ |
Willy Tarreau | 3988d93 | 2013-12-27 23:03:08 +0100 | [diff] [blame] | 4662 | if (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_SCL) && |
| 4663 | ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) && |
| 4664 | !(s->be->options & PR_O_ABRT_CLOSE) && |
| 4665 | txn->meth != HTTP_METH_POST) |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 4666 | channel_dont_read(chn); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4667 | |
Willy Tarreau | 40f151a | 2012-12-20 12:10:09 +0100 | [diff] [blame] | 4668 | /* if the server closes the connection, we want to immediately react |
| 4669 | * and close the socket to save packets and syscalls. |
| 4670 | */ |
| 4671 | chn->cons->flags |= SI_FL_NOHALF; |
| 4672 | |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4673 | if (txn->rsp.msg_state == HTTP_MSG_ERROR) |
| 4674 | goto wait_other_side; |
| 4675 | |
| 4676 | if (txn->rsp.msg_state < HTTP_MSG_DONE) { |
| 4677 | /* The server has not finished to respond, so we |
| 4678 | * don't want to move in order not to upset it. |
| 4679 | */ |
| 4680 | goto wait_other_side; |
| 4681 | } |
| 4682 | |
| 4683 | if (txn->rsp.msg_state == HTTP_MSG_TUNNEL) { |
| 4684 | /* if any side switches to tunnel mode, the other one does too */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 4685 | channel_auto_read(chn); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4686 | txn->req.msg_state = HTTP_MSG_TUNNEL; |
Willy Tarreau | fc47f91 | 2012-10-20 10:38:09 +0200 | [diff] [blame] | 4687 | chn->flags |= CF_NEVER_WAIT; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4688 | goto wait_other_side; |
| 4689 | } |
| 4690 | |
| 4691 | /* When we get here, it means that both the request and the |
| 4692 | * response have finished receiving. Depending on the connection |
| 4693 | * mode, we'll have to wait for the last bytes to leave in either |
| 4694 | * direction, and sometimes for a close to be effective. |
| 4695 | */ |
| 4696 | |
Willy Tarreau | cce7fa4 | 2010-01-16 23:19:39 +0100 | [diff] [blame] | 4697 | if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) { |
| 4698 | /* Server-close mode : queue a connection close to the server */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 4699 | if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW))) |
| 4700 | channel_shutw_now(chn); |
Willy Tarreau | cce7fa4 | 2010-01-16 23:19:39 +0100 | [diff] [blame] | 4701 | } |
| 4702 | else if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) { |
| 4703 | /* Option forceclose is set, or either side wants to close, |
| 4704 | * let's enforce it now that we're not expecting any new |
| 4705 | * data to come. The caller knows the session is complete |
| 4706 | * once both states are CLOSED. |
| 4707 | */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 4708 | if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW))) { |
| 4709 | channel_shutr_now(chn); |
| 4710 | channel_shutw_now(chn); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4711 | } |
Willy Tarreau | cce7fa4 | 2010-01-16 23:19:39 +0100 | [diff] [blame] | 4712 | } |
| 4713 | else { |
Willy Tarreau | 4213a11 | 2013-12-15 10:25:42 +0100 | [diff] [blame] | 4714 | /* The last possible modes are keep-alive and tunnel. Tunnel mode |
| 4715 | * will not have any analyser so it needs to poll for reads. |
Willy Tarreau | cce7fa4 | 2010-01-16 23:19:39 +0100 | [diff] [blame] | 4716 | */ |
Willy Tarreau | 4213a11 | 2013-12-15 10:25:42 +0100 | [diff] [blame] | 4717 | if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN) { |
| 4718 | channel_auto_read(chn); |
| 4719 | txn->req.msg_state = HTTP_MSG_TUNNEL; |
| 4720 | chn->flags |= CF_NEVER_WAIT; |
| 4721 | } |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4722 | } |
| 4723 | |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 4724 | if (chn->flags & (CF_SHUTW|CF_SHUTW_NOW)) { |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4725 | /* if we've just closed an output, let's switch */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 4726 | chn->cons->flags |= SI_FL_NOLINGER; /* we want to close ASAP */ |
Willy Tarreau | cce7fa4 | 2010-01-16 23:19:39 +0100 | [diff] [blame] | 4727 | |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 4728 | if (!channel_is_empty(chn)) { |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4729 | txn->req.msg_state = HTTP_MSG_CLOSING; |
| 4730 | goto http_msg_closing; |
| 4731 | } |
| 4732 | else { |
| 4733 | txn->req.msg_state = HTTP_MSG_CLOSED; |
| 4734 | goto http_msg_closed; |
| 4735 | } |
| 4736 | } |
Willy Tarreau | cce7fa4 | 2010-01-16 23:19:39 +0100 | [diff] [blame] | 4737 | goto wait_other_side; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4738 | } |
| 4739 | |
| 4740 | if (txn->req.msg_state == HTTP_MSG_CLOSING) { |
| 4741 | http_msg_closing: |
| 4742 | /* nothing else to forward, just waiting for the output buffer |
| 4743 | * to be empty and for the shutw_now to take effect. |
| 4744 | */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 4745 | if (channel_is_empty(chn)) { |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4746 | txn->req.msg_state = HTTP_MSG_CLOSED; |
| 4747 | goto http_msg_closed; |
| 4748 | } |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 4749 | else if (chn->flags & CF_SHUTW) { |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4750 | txn->req.msg_state = HTTP_MSG_ERROR; |
| 4751 | goto wait_other_side; |
| 4752 | } |
| 4753 | } |
| 4754 | |
| 4755 | if (txn->req.msg_state == HTTP_MSG_CLOSED) { |
| 4756 | http_msg_closed: |
Willy Tarreau | 3988d93 | 2013-12-27 23:03:08 +0100 | [diff] [blame] | 4757 | /* see above in MSG_DONE why we only do this in these states */ |
| 4758 | if (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_SCL) && |
| 4759 | ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) && |
| 4760 | !(s->be->options & PR_O_ABRT_CLOSE)) |
Willy Tarreau | 2e7a165 | 2013-12-15 15:32:10 +0100 | [diff] [blame] | 4761 | channel_dont_read(chn); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4762 | goto wait_other_side; |
| 4763 | } |
| 4764 | |
| 4765 | wait_other_side: |
| 4766 | http_silent_debug(__LINE__, s); |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 4767 | return txn->req.msg_state != old_state || chn->flags != old_flags; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4768 | } |
| 4769 | |
| 4770 | |
| 4771 | /* This function updates the response state machine according to the request |
| 4772 | * state machine and buffer flags. It returns 1 if it changes anything (flag |
| 4773 | * or state), otherwise zero. It ignores any state before HTTP_MSG_DONE, as |
| 4774 | * it is only used to find when a request/response couple is complete. Both |
| 4775 | * this function and its equivalent should loop until both return zero. It |
| 4776 | * can set its own state to DONE, CLOSING, CLOSED, TUNNEL, ERROR. |
| 4777 | */ |
| 4778 | int http_sync_res_state(struct session *s) |
| 4779 | { |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 4780 | struct channel *chn = s->rep; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4781 | struct http_txn *txn = &s->txn; |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 4782 | unsigned int old_flags = chn->flags; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4783 | unsigned int old_state = txn->rsp.msg_state; |
| 4784 | |
| 4785 | http_silent_debug(__LINE__, s); |
| 4786 | if (unlikely(txn->rsp.msg_state < HTTP_MSG_BODY)) |
| 4787 | return 0; |
| 4788 | |
| 4789 | if (txn->rsp.msg_state == HTTP_MSG_DONE) { |
| 4790 | /* In theory, we don't need to read anymore, but we must |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 4791 | * still monitor the server connection for a possible close |
| 4792 | * while the request is being uploaded, so we don't disable |
| 4793 | * reading. |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4794 | */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 4795 | /* channel_dont_read(chn); */ |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4796 | |
| 4797 | if (txn->req.msg_state == HTTP_MSG_ERROR) |
| 4798 | goto wait_other_side; |
| 4799 | |
| 4800 | if (txn->req.msg_state < HTTP_MSG_DONE) { |
| 4801 | /* The client seems to still be sending data, probably |
| 4802 | * because we got an error response during an upload. |
| 4803 | * We have the choice of either breaking the connection |
| 4804 | * or letting it pass through. Let's do the later. |
| 4805 | */ |
| 4806 | goto wait_other_side; |
| 4807 | } |
| 4808 | |
| 4809 | if (txn->req.msg_state == HTTP_MSG_TUNNEL) { |
| 4810 | /* if any side switches to tunnel mode, the other one does too */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 4811 | channel_auto_read(chn); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4812 | txn->rsp.msg_state = HTTP_MSG_TUNNEL; |
Willy Tarreau | fc47f91 | 2012-10-20 10:38:09 +0200 | [diff] [blame] | 4813 | chn->flags |= CF_NEVER_WAIT; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4814 | goto wait_other_side; |
| 4815 | } |
| 4816 | |
| 4817 | /* When we get here, it means that both the request and the |
| 4818 | * response have finished receiving. Depending on the connection |
| 4819 | * mode, we'll have to wait for the last bytes to leave in either |
| 4820 | * direction, and sometimes for a close to be effective. |
| 4821 | */ |
| 4822 | |
| 4823 | if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) { |
| 4824 | /* Server-close mode : shut read and wait for the request |
| 4825 | * side to close its output buffer. The caller will detect |
| 4826 | * when we're in DONE and the other is in CLOSED and will |
| 4827 | * catch that for the final cleanup. |
| 4828 | */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 4829 | if (!(chn->flags & (CF_SHUTR|CF_SHUTR_NOW))) |
| 4830 | channel_shutr_now(chn); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4831 | } |
Willy Tarreau | cce7fa4 | 2010-01-16 23:19:39 +0100 | [diff] [blame] | 4832 | else if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) { |
| 4833 | /* Option forceclose is set, or either side wants to close, |
| 4834 | * let's enforce it now that we're not expecting any new |
| 4835 | * data to come. The caller knows the session is complete |
| 4836 | * once both states are CLOSED. |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4837 | */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 4838 | if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW))) { |
| 4839 | channel_shutr_now(chn); |
| 4840 | channel_shutw_now(chn); |
Willy Tarreau | cce7fa4 | 2010-01-16 23:19:39 +0100 | [diff] [blame] | 4841 | } |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4842 | } |
| 4843 | else { |
Willy Tarreau | 4213a11 | 2013-12-15 10:25:42 +0100 | [diff] [blame] | 4844 | /* The last possible modes are keep-alive and tunnel. Tunnel will |
| 4845 | * need to forward remaining data. Keep-alive will need to monitor |
| 4846 | * for connection closing. |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4847 | */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 4848 | channel_auto_read(chn); |
Willy Tarreau | fc47f91 | 2012-10-20 10:38:09 +0200 | [diff] [blame] | 4849 | chn->flags |= CF_NEVER_WAIT; |
Willy Tarreau | 4213a11 | 2013-12-15 10:25:42 +0100 | [diff] [blame] | 4850 | if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN) |
| 4851 | txn->rsp.msg_state = HTTP_MSG_TUNNEL; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4852 | } |
| 4853 | |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 4854 | if (chn->flags & (CF_SHUTW|CF_SHUTW_NOW)) { |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4855 | /* if we've just closed an output, let's switch */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 4856 | if (!channel_is_empty(chn)) { |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4857 | txn->rsp.msg_state = HTTP_MSG_CLOSING; |
| 4858 | goto http_msg_closing; |
| 4859 | } |
| 4860 | else { |
| 4861 | txn->rsp.msg_state = HTTP_MSG_CLOSED; |
| 4862 | goto http_msg_closed; |
| 4863 | } |
| 4864 | } |
| 4865 | goto wait_other_side; |
| 4866 | } |
| 4867 | |
| 4868 | if (txn->rsp.msg_state == HTTP_MSG_CLOSING) { |
| 4869 | http_msg_closing: |
| 4870 | /* nothing else to forward, just waiting for the output buffer |
| 4871 | * to be empty and for the shutw_now to take effect. |
| 4872 | */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 4873 | if (channel_is_empty(chn)) { |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4874 | txn->rsp.msg_state = HTTP_MSG_CLOSED; |
| 4875 | goto http_msg_closed; |
| 4876 | } |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 4877 | else if (chn->flags & CF_SHUTW) { |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4878 | txn->rsp.msg_state = HTTP_MSG_ERROR; |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 4879 | s->be->be_counters.cli_aborts++; |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 4880 | if (objt_server(s->target)) |
| 4881 | objt_server(s->target)->counters.cli_aborts++; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4882 | goto wait_other_side; |
| 4883 | } |
| 4884 | } |
| 4885 | |
| 4886 | if (txn->rsp.msg_state == HTTP_MSG_CLOSED) { |
| 4887 | http_msg_closed: |
| 4888 | /* drop any pending data */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 4889 | bi_erase(chn); |
| 4890 | channel_auto_close(chn); |
| 4891 | channel_auto_read(chn); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4892 | goto wait_other_side; |
| 4893 | } |
| 4894 | |
| 4895 | wait_other_side: |
| 4896 | http_silent_debug(__LINE__, s); |
Willy Tarreau | fc47f91 | 2012-10-20 10:38:09 +0200 | [diff] [blame] | 4897 | /* We force the response to leave immediately if we're waiting for the |
| 4898 | * other side, since there is no pending shutdown to push it out. |
| 4899 | */ |
| 4900 | if (!channel_is_empty(chn)) |
| 4901 | chn->flags |= CF_SEND_DONTWAIT; |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 4902 | return txn->rsp.msg_state != old_state || chn->flags != old_flags; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4903 | } |
| 4904 | |
| 4905 | |
| 4906 | /* Resync the request and response state machines. Return 1 if either state |
| 4907 | * changes. |
| 4908 | */ |
| 4909 | int http_resync_states(struct session *s) |
| 4910 | { |
| 4911 | struct http_txn *txn = &s->txn; |
| 4912 | int old_req_state = txn->req.msg_state; |
| 4913 | int old_res_state = txn->rsp.msg_state; |
| 4914 | |
| 4915 | http_silent_debug(__LINE__, s); |
| 4916 | http_sync_req_state(s); |
| 4917 | while (1) { |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 4918 | http_silent_debug(__LINE__, s); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4919 | if (!http_sync_res_state(s)) |
| 4920 | break; |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 4921 | http_silent_debug(__LINE__, s); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4922 | if (!http_sync_req_state(s)) |
| 4923 | break; |
| 4924 | } |
| 4925 | http_silent_debug(__LINE__, s); |
| 4926 | /* OK, both state machines agree on a compatible state. |
| 4927 | * There are a few cases we're interested in : |
| 4928 | * - HTTP_MSG_TUNNEL on either means we have to disable both analysers |
| 4929 | * - HTTP_MSG_CLOSED on both sides means we've reached the end in both |
| 4930 | * directions, so let's simply disable both analysers. |
| 4931 | * - HTTP_MSG_CLOSED on the response only means we must abort the |
| 4932 | * request. |
| 4933 | * - HTTP_MSG_CLOSED on the request and HTTP_MSG_DONE on the response |
| 4934 | * with server-close mode means we've completed one request and we |
| 4935 | * must re-initialize the server connection. |
| 4936 | */ |
| 4937 | |
| 4938 | if (txn->req.msg_state == HTTP_MSG_TUNNEL || |
| 4939 | txn->rsp.msg_state == HTTP_MSG_TUNNEL || |
| 4940 | (txn->req.msg_state == HTTP_MSG_CLOSED && |
| 4941 | txn->rsp.msg_state == HTTP_MSG_CLOSED)) { |
| 4942 | s->req->analysers = 0; |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 4943 | channel_auto_close(s->req); |
| 4944 | channel_auto_read(s->req); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4945 | s->rep->analysers = 0; |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 4946 | channel_auto_close(s->rep); |
| 4947 | channel_auto_read(s->rep); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4948 | } |
Willy Tarreau | 40f151a | 2012-12-20 12:10:09 +0100 | [diff] [blame] | 4949 | else if ((txn->req.msg_state >= HTTP_MSG_DONE && |
| 4950 | (txn->rsp.msg_state == HTTP_MSG_CLOSED || (s->rep->flags & CF_SHUTW))) || |
Willy Tarreau | 2fa144c | 2010-01-04 23:13:26 +0100 | [diff] [blame] | 4951 | txn->rsp.msg_state == HTTP_MSG_ERROR || |
Willy Tarreau | 40f151a | 2012-12-20 12:10:09 +0100 | [diff] [blame] | 4952 | txn->req.msg_state == HTTP_MSG_ERROR) { |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 4953 | s->rep->analysers = 0; |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 4954 | channel_auto_close(s->rep); |
| 4955 | channel_auto_read(s->rep); |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 4956 | s->req->analysers = 0; |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 4957 | channel_abort(s->req); |
| 4958 | channel_auto_close(s->req); |
| 4959 | channel_auto_read(s->req); |
Willy Tarreau | 9dab5fc | 2012-05-07 11:56:55 +0200 | [diff] [blame] | 4960 | bi_erase(s->req); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4961 | } |
Willy Tarreau | 4213a11 | 2013-12-15 10:25:42 +0100 | [diff] [blame] | 4962 | else if ((txn->req.msg_state == HTTP_MSG_DONE || |
| 4963 | txn->req.msg_state == HTTP_MSG_CLOSED) && |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4964 | txn->rsp.msg_state == HTTP_MSG_DONE && |
Willy Tarreau | 4213a11 | 2013-12-15 10:25:42 +0100 | [diff] [blame] | 4965 | ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL || |
| 4966 | (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL)) { |
| 4967 | /* server-close/keep-alive: terminate this transaction, |
| 4968 | * possibly killing the server connection and reinitialize |
| 4969 | * a fresh-new transaction. |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4970 | */ |
| 4971 | http_end_txn_clean_session(s); |
| 4972 | } |
| 4973 | |
| 4974 | http_silent_debug(__LINE__, s); |
| 4975 | return txn->req.msg_state != old_req_state || |
| 4976 | txn->rsp.msg_state != old_res_state; |
| 4977 | } |
| 4978 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4979 | /* This function is an analyser which forwards request body (including chunk |
| 4980 | * sizes if any). It is called as soon as we must forward, even if we forward |
| 4981 | * zero byte. The only situation where it must not be called is when we're in |
| 4982 | * tunnel mode and we want to forward till the close. It's used both to forward |
| 4983 | * remaining data and to resync after end of body. It expects the msg_state to |
| 4984 | * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to |
| 4985 | * 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] | 4986 | * 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] | 4987 | * bytes of pending data + the headers if not already done (between sol and sov). |
| 4988 | * 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] | 4989 | */ |
Willy Tarreau | 7421efb | 2012-07-02 15:11:27 +0200 | [diff] [blame] | 4990 | 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] | 4991 | { |
| 4992 | struct http_txn *txn = &s->txn; |
| 4993 | struct http_msg *msg = &s->txn.req; |
| 4994 | |
Willy Tarreau | f5c8bd6 | 2010-01-04 07:10:34 +0100 | [diff] [blame] | 4995 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) |
| 4996 | return 0; |
| 4997 | |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 4998 | 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] | 4999 | ((req->flags & CF_SHUTW) && (req->to_forward || req->buf->o))) { |
Willy Tarreau | 4fe4190 | 2010-06-07 22:27:41 +0200 | [diff] [blame] | 5000 | /* Output closed while we were sending data. We must abort and |
| 5001 | * wake the other side up. |
| 5002 | */ |
| 5003 | msg->msg_state = HTTP_MSG_ERROR; |
| 5004 | http_resync_states(s); |
Willy Tarreau | 082b01c | 2010-01-02 23:58:04 +0100 | [diff] [blame] | 5005 | return 1; |
| 5006 | } |
| 5007 | |
Willy Tarreau | 80a92c0 | 2014-03-12 10:41:13 +0100 | [diff] [blame] | 5008 | /* Some post-connect processing might want us to refrain from starting to |
| 5009 | * forward data. Currently, the only reason for this is "balance url_param" |
| 5010 | * whichs need to parse/process the request after we've enabled forwarding. |
| 5011 | */ |
| 5012 | if (unlikely(msg->flags & HTTP_MSGF_WAIT_CONN)) { |
| 5013 | if (!(s->rep->flags & CF_READ_ATTACHED)) { |
| 5014 | channel_auto_connect(req); |
| 5015 | goto missing_data; |
| 5016 | } |
| 5017 | msg->flags &= ~HTTP_MSGF_WAIT_CONN; |
| 5018 | } |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5019 | |
| 5020 | /* Note that we don't have to send 100-continue back because we don't |
| 5021 | * need the data to complete our job, and it's up to the server to |
| 5022 | * decide whether to return 100, 417 or anything else in return of |
| 5023 | * an "Expect: 100-continue" header. |
| 5024 | */ |
| 5025 | |
| 5026 | if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) { |
Willy Tarreau | fa4a03c | 2012-03-09 21:28:54 +0100 | [diff] [blame] | 5027 | /* 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] | 5028 | * 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] | 5029 | * is still null. We must save the body in msg->next because it |
| 5030 | * survives buffer re-alignments. |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5031 | */ |
Willy Tarreau | ea1175a | 2012-03-05 15:52:30 +0100 | [diff] [blame] | 5032 | msg->next = msg->sov; |
Willy Tarreau | a458b67 | 2012-03-05 11:17:50 +0100 | [diff] [blame] | 5033 | |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 5034 | if (msg->flags & HTTP_MSGF_TE_CHNK) |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5035 | msg->msg_state = HTTP_MSG_CHUNK_SIZE; |
Willy Tarreau | 54d23df | 2012-10-25 19:04:45 +0200 | [diff] [blame] | 5036 | else |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5037 | msg->msg_state = HTTP_MSG_DATA; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5038 | } |
| 5039 | |
Willy Tarreau | 80a92c0 | 2014-03-12 10:41:13 +0100 | [diff] [blame] | 5040 | /* in most states, we should abort in case of early close */ |
| 5041 | channel_auto_close(req); |
| 5042 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5043 | while (1) { |
Willy Tarreau | ea95316 | 2012-05-18 23:41:28 +0200 | [diff] [blame] | 5044 | unsigned int bytes; |
Willy Tarreau | d8ee85a | 2011-03-28 16:06:28 +0200 | [diff] [blame] | 5045 | |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5046 | http_silent_debug(__LINE__, s); |
Willy Tarreau | ea95316 | 2012-05-18 23:41:28 +0200 | [diff] [blame] | 5047 | /* we may have some data pending between sol and sov */ |
Willy Tarreau | 2692736 | 2012-05-18 23:22:52 +0200 | [diff] [blame] | 5048 | bytes = msg->sov - msg->sol; |
Willy Tarreau | d8ee85a | 2011-03-28 16:06:28 +0200 | [diff] [blame] | 5049 | if (msg->chunk_len || bytes) { |
Willy Tarreau | 2692736 | 2012-05-18 23:22:52 +0200 | [diff] [blame] | 5050 | msg->sol = msg->sov; |
Willy Tarreau | a458b67 | 2012-03-05 11:17:50 +0100 | [diff] [blame] | 5051 | msg->next -= bytes; /* will be forwarded */ |
Willy Tarreau | ea95316 | 2012-05-18 23:41:28 +0200 | [diff] [blame] | 5052 | msg->chunk_len += bytes; |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 5053 | msg->chunk_len -= channel_forward(req, msg->chunk_len); |
Willy Tarreau | 638cd02 | 2010-01-03 07:42:04 +0100 | [diff] [blame] | 5054 | } |
Willy Tarreau | 5523b32 | 2009-12-29 12:05:52 +0100 | [diff] [blame] | 5055 | |
Willy Tarreau | caabe41 | 2010-01-03 23:08:28 +0100 | [diff] [blame] | 5056 | if (msg->msg_state == HTTP_MSG_DATA) { |
| 5057 | /* must still forward */ |
Willy Tarreau | 4afd70a | 2014-01-25 02:26:39 +0100 | [diff] [blame] | 5058 | if (req->to_forward) { |
| 5059 | req->flags |= CF_WAKE_WRITE; |
Willy Tarreau | f5c8bd6 | 2010-01-04 07:10:34 +0100 | [diff] [blame] | 5060 | goto missing_data; |
Willy Tarreau | 4afd70a | 2014-01-25 02:26:39 +0100 | [diff] [blame] | 5061 | } |
Willy Tarreau | caabe41 | 2010-01-03 23:08:28 +0100 | [diff] [blame] | 5062 | |
| 5063 | /* nothing left to forward */ |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 5064 | if (msg->flags & HTTP_MSGF_TE_CHNK) |
Willy Tarreau | 54d23df | 2012-10-25 19:04:45 +0200 | [diff] [blame] | 5065 | msg->msg_state = HTTP_MSG_CHUNK_CRLF; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5066 | else |
Willy Tarreau | caabe41 | 2010-01-03 23:08:28 +0100 | [diff] [blame] | 5067 | msg->msg_state = HTTP_MSG_DONE; |
Willy Tarreau | caabe41 | 2010-01-03 23:08:28 +0100 | [diff] [blame] | 5068 | } |
| 5069 | else if (msg->msg_state == HTTP_MSG_CHUNK_SIZE) { |
Willy Tarreau | 124d991 | 2011-03-01 20:30:48 +0100 | [diff] [blame] | 5070 | /* read the chunk size and assign it to ->chunk_len, then |
Willy Tarreau | a458b67 | 2012-03-05 11:17:50 +0100 | [diff] [blame] | 5071 | * 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] | 5072 | * TRAILERS state. |
| 5073 | */ |
Willy Tarreau | 4baf44b | 2012-03-09 14:10:20 +0100 | [diff] [blame] | 5074 | int ret = http_parse_chunk_size(msg); |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5075 | |
Willy Tarreau | 54d23df | 2012-10-25 19:04:45 +0200 | [diff] [blame] | 5076 | if (ret == 0) |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5077 | goto missing_data; |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 5078 | else if (ret < 0) { |
| 5079 | session_inc_http_err_ctr(s); |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 5080 | if (msg->err_pos >= 0) |
Willy Tarreau | 8a0cef2 | 2012-03-09 13:39:23 +0100 | [diff] [blame] | 5081 | 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] | 5082 | goto return_bad_req; |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 5083 | } |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5084 | /* otherwise we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state */ |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5085 | } |
Willy Tarreau | 54d23df | 2012-10-25 19:04:45 +0200 | [diff] [blame] | 5086 | else if (msg->msg_state == HTTP_MSG_CHUNK_CRLF) { |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5087 | /* we want the CRLF after the data */ |
Willy Tarreau | 54d23df | 2012-10-25 19:04:45 +0200 | [diff] [blame] | 5088 | int ret = http_skip_chunk_crlf(msg); |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5089 | |
| 5090 | if (ret == 0) |
| 5091 | goto missing_data; |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 5092 | else if (ret < 0) { |
| 5093 | session_inc_http_err_ctr(s); |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 5094 | if (msg->err_pos >= 0) |
Willy Tarreau | 54d23df | 2012-10-25 19:04:45 +0200 | [diff] [blame] | 5095 | 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] | 5096 | goto return_bad_req; |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 5097 | } |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5098 | /* we're in MSG_CHUNK_SIZE now */ |
| 5099 | } |
| 5100 | else if (msg->msg_state == HTTP_MSG_TRAILERS) { |
Willy Tarreau | 4baf44b | 2012-03-09 14:10:20 +0100 | [diff] [blame] | 5101 | int ret = http_forward_trailers(msg); |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5102 | |
| 5103 | if (ret == 0) |
| 5104 | goto missing_data; |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 5105 | else if (ret < 0) { |
| 5106 | session_inc_http_err_ctr(s); |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 5107 | if (msg->err_pos >= 0) |
Willy Tarreau | 8a0cef2 | 2012-03-09 13:39:23 +0100 | [diff] [blame] | 5108 | 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] | 5109 | goto return_bad_req; |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 5110 | } |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5111 | /* we're in HTTP_MSG_DONE now */ |
| 5112 | } |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5113 | else { |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 5114 | int old_state = msg->msg_state; |
| 5115 | |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5116 | /* other states, DONE...TUNNEL */ |
Willy Tarreau | 4fe4190 | 2010-06-07 22:27:41 +0200 | [diff] [blame] | 5117 | /* for keep-alive we don't want to forward closes on DONE */ |
Willy Tarreau | 92aa1fa | 2010-08-28 18:57:20 +0200 | [diff] [blame] | 5118 | if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL || |
| 5119 | (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 5120 | channel_dont_close(req); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5121 | if (http_resync_states(s)) { |
| 5122 | /* some state changes occurred, maybe the analyser |
| 5123 | * was disabled too. |
Willy Tarreau | face839 | 2010-01-03 11:37:54 +0100 | [diff] [blame] | 5124 | */ |
Willy Tarreau | 3fe693b | 2010-12-12 12:50:05 +0100 | [diff] [blame] | 5125 | if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) { |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 5126 | if (req->flags & CF_SHUTW) { |
Willy Tarreau | 3fe693b | 2010-12-12 12:50:05 +0100 | [diff] [blame] | 5127 | /* request errors are most likely due to |
| 5128 | * the server aborting the transfer. |
| 5129 | */ |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 5130 | goto aborted_xfer; |
Willy Tarreau | 3fe693b | 2010-12-12 12:50:05 +0100 | [diff] [blame] | 5131 | } |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 5132 | if (msg->err_pos >= 0) |
Willy Tarreau | 8a0cef2 | 2012-03-09 13:39:23 +0100 | [diff] [blame] | 5133 | 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] | 5134 | goto return_bad_req; |
Willy Tarreau | 3fe693b | 2010-12-12 12:50:05 +0100 | [diff] [blame] | 5135 | } |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5136 | return 1; |
Willy Tarreau | b608feb | 2010-01-02 22:47:18 +0100 | [diff] [blame] | 5137 | } |
Willy Tarreau | 5c54c71 | 2010-07-17 08:02:58 +0200 | [diff] [blame] | 5138 | |
| 5139 | /* If "option abortonclose" is set on the backend, we |
| 5140 | * want to monitor the client's connection and forward |
| 5141 | * any shutdown notification to the server, which will |
| 5142 | * decide whether to close or to go on processing the |
| 5143 | * request. |
| 5144 | */ |
| 5145 | if (s->be->options & PR_O_ABRT_CLOSE) { |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 5146 | channel_auto_read(req); |
| 5147 | channel_auto_close(req); |
Willy Tarreau | 5c54c71 | 2010-07-17 08:02:58 +0200 | [diff] [blame] | 5148 | } |
Willy Tarreau | 58bd8fd | 2010-09-28 14:16:41 +0200 | [diff] [blame] | 5149 | else if (s->txn.meth == HTTP_METH_POST) { |
| 5150 | /* POST requests may require to read extra CRLF |
| 5151 | * sent by broken browsers and which could cause |
| 5152 | * an RST to be sent upon close on some systems |
| 5153 | * (eg: Linux). |
| 5154 | */ |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 5155 | channel_auto_read(req); |
Willy Tarreau | 58bd8fd | 2010-09-28 14:16:41 +0200 | [diff] [blame] | 5156 | } |
Willy Tarreau | 5c54c71 | 2010-07-17 08:02:58 +0200 | [diff] [blame] | 5157 | |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5158 | return 0; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5159 | } |
| 5160 | } |
| 5161 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5162 | missing_data: |
Willy Tarreau | f5c8bd6 | 2010-01-04 07:10:34 +0100 | [diff] [blame] | 5163 | /* stop waiting for data if the input is closed before the end */ |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 5164 | if (req->flags & CF_SHUTR) { |
Willy Tarreau | 79ebac6 | 2010-06-07 13:47:49 +0200 | [diff] [blame] | 5165 | if (!(s->flags & SN_ERR_MASK)) |
| 5166 | s->flags |= SN_ERR_CLICL; |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 5167 | if (!(s->flags & SN_FINST_MASK)) { |
| 5168 | if (txn->rsp.msg_state < HTTP_MSG_ERROR) |
| 5169 | s->flags |= SN_FINST_H; |
| 5170 | else |
| 5171 | s->flags |= SN_FINST_D; |
| 5172 | } |
| 5173 | |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 5174 | s->fe->fe_counters.cli_aborts++; |
| 5175 | s->be->be_counters.cli_aborts++; |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 5176 | if (objt_server(s->target)) |
| 5177 | objt_server(s->target)->counters.cli_aborts++; |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 5178 | |
| 5179 | goto return_bad_req_stats_ok; |
Willy Tarreau | 79ebac6 | 2010-06-07 13:47:49 +0200 | [diff] [blame] | 5180 | } |
Willy Tarreau | f5c8bd6 | 2010-01-04 07:10:34 +0100 | [diff] [blame] | 5181 | |
Willy Tarreau | f5c8bd6 | 2010-01-04 07:10:34 +0100 | [diff] [blame] | 5182 | /* waiting for the last bits to leave the buffer */ |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 5183 | if (req->flags & CF_SHUTW) |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 5184 | goto aborted_xfer; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5185 | |
Willy Tarreau | 92aa1fa | 2010-08-28 18:57:20 +0200 | [diff] [blame] | 5186 | /* 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] | 5187 | * 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] | 5188 | */ |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 5189 | if (msg->flags & HTTP_MSGF_TE_CHNK) |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 5190 | channel_dont_close(req); |
Willy Tarreau | 92aa1fa | 2010-08-28 18:57:20 +0200 | [diff] [blame] | 5191 | |
Willy Tarreau | 5c62092 | 2011-05-11 19:56:11 +0200 | [diff] [blame] | 5192 | /* 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] | 5193 | * 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] | 5194 | * 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] | 5195 | * modes are already handled by the stream sock layer. We must not do |
| 5196 | * this in content-length mode because it could present the MSG_MORE |
| 5197 | * flag with the last block of forwarded data, which would cause an |
| 5198 | * additional delay to be observed by the receiver. |
Willy Tarreau | 5c62092 | 2011-05-11 19:56:11 +0200 | [diff] [blame] | 5199 | */ |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 5200 | if (msg->flags & HTTP_MSGF_TE_CHNK) |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 5201 | req->flags |= CF_EXPECT_MORE; |
Willy Tarreau | 5c62092 | 2011-05-11 19:56:11 +0200 | [diff] [blame] | 5202 | |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5203 | http_silent_debug(__LINE__, s); |
Willy Tarreau | f5c8bd6 | 2010-01-04 07:10:34 +0100 | [diff] [blame] | 5204 | return 0; |
| 5205 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5206 | return_bad_req: /* let's centralize all bad requests */ |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 5207 | s->fe->fe_counters.failed_req++; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5208 | if (s->listener->counters) |
| 5209 | s->listener->counters->failed_req++; |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 5210 | return_bad_req_stats_ok: |
| 5211 | txn->req.msg_state = HTTP_MSG_ERROR; |
| 5212 | if (txn->status) { |
| 5213 | /* Note: we don't send any error if some data were already sent */ |
| 5214 | stream_int_retnclose(req->prod, NULL); |
| 5215 | } else { |
| 5216 | txn->status = 400; |
Willy Tarreau | 783f258 | 2012-09-04 12:19:04 +0200 | [diff] [blame] | 5217 | stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_400)); |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 5218 | } |
| 5219 | req->analysers = 0; |
| 5220 | 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] | 5221 | |
| 5222 | if (!(s->flags & SN_ERR_MASK)) |
| 5223 | s->flags |= SN_ERR_PRXCOND; |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 5224 | if (!(s->flags & SN_FINST_MASK)) { |
| 5225 | if (txn->rsp.msg_state < HTTP_MSG_ERROR) |
| 5226 | s->flags |= SN_FINST_H; |
| 5227 | else |
| 5228 | s->flags |= SN_FINST_D; |
| 5229 | } |
| 5230 | return 0; |
| 5231 | |
| 5232 | aborted_xfer: |
| 5233 | txn->req.msg_state = HTTP_MSG_ERROR; |
| 5234 | if (txn->status) { |
| 5235 | /* Note: we don't send any error if some data were already sent */ |
| 5236 | stream_int_retnclose(req->prod, NULL); |
| 5237 | } else { |
| 5238 | txn->status = 502; |
Willy Tarreau | 783f258 | 2012-09-04 12:19:04 +0200 | [diff] [blame] | 5239 | stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_502)); |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 5240 | } |
| 5241 | req->analysers = 0; |
| 5242 | s->rep->analysers = 0; /* we're in data phase, we want to abort both directions */ |
| 5243 | |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 5244 | s->fe->fe_counters.srv_aborts++; |
| 5245 | s->be->be_counters.srv_aborts++; |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 5246 | if (objt_server(s->target)) |
| 5247 | objt_server(s->target)->counters.srv_aborts++; |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 5248 | |
| 5249 | if (!(s->flags & SN_ERR_MASK)) |
| 5250 | s->flags |= SN_ERR_SRVCL; |
| 5251 | if (!(s->flags & SN_FINST_MASK)) { |
| 5252 | if (txn->rsp.msg_state < HTTP_MSG_ERROR) |
| 5253 | s->flags |= SN_FINST_H; |
| 5254 | else |
| 5255 | s->flags |= SN_FINST_D; |
| 5256 | } |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5257 | return 0; |
| 5258 | } |
| 5259 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5260 | /* This stream analyser waits for a complete HTTP response. It returns 1 if the |
| 5261 | * processing can continue on next analysers, or zero if it either needs more |
| 5262 | * data or wants to immediately abort the response (eg: timeout, error, ...). It |
| 5263 | * is tied to AN_RES_WAIT_HTTP and may may remove itself from s->rep->analysers |
| 5264 | * when it has nothing left to do, and may remove any analyser when it wants to |
| 5265 | * abort. |
Willy Tarreau | c65a3ba | 2008-08-11 23:42:50 +0200 | [diff] [blame] | 5266 | */ |
Willy Tarreau | 7421efb | 2012-07-02 15:11:27 +0200 | [diff] [blame] | 5267 | 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] | 5268 | { |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5269 | struct http_txn *txn = &s->txn; |
| 5270 | struct http_msg *msg = &txn->rsp; |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 5271 | struct hdr_ctx ctx; |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 5272 | int use_close_only; |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5273 | int cur_idx; |
Krzysztof Piotr Oledzki | 5fb1882 | 2009-10-13 21:14:09 +0200 | [diff] [blame] | 5274 | int n; |
Willy Tarreau | adfb856 | 2008-08-11 15:24:42 +0200 | [diff] [blame] | 5275 | |
Willy Tarreau | 02d6cfc | 2012-03-01 18:19:58 +0100 | [diff] [blame] | 5276 | 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] | 5277 | now_ms, __FUNCTION__, |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5278 | s, |
Willy Tarreau | 3a16b2c | 2008-08-28 08:54:27 +0200 | [diff] [blame] | 5279 | rep, |
| 5280 | rep->rex, rep->wex, |
| 5281 | rep->flags, |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 5282 | rep->buf->i, |
Willy Tarreau | 3a16b2c | 2008-08-28 08:54:27 +0200 | [diff] [blame] | 5283 | rep->analysers); |
Willy Tarreau | 67f0eea | 2008-08-10 22:55:22 +0200 | [diff] [blame] | 5284 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5285 | /* |
| 5286 | * Now parse the partial (or complete) lines. |
| 5287 | * We will check the response syntax, and also join multi-line |
| 5288 | * headers. An index of all the lines will be elaborated while |
| 5289 | * parsing. |
| 5290 | * |
| 5291 | * For the parsing, we use a 28 states FSM. |
| 5292 | * |
| 5293 | * Here is the information we currently have : |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 5294 | * rep->buf->p = beginning of response |
| 5295 | * rep->buf->p + msg->eoh = end of processed headers / start of current one |
| 5296 | * rep->buf->p + rep->buf->i = end of input data |
Willy Tarreau | 2692736 | 2012-05-18 23:22:52 +0200 | [diff] [blame] | 5297 | * msg->eol = end of current header or line (LF or CRLF) |
| 5298 | * msg->next = first non-visited byte |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5299 | */ |
| 5300 | |
Willy Tarreau | 83e3af0 | 2009-12-28 17:39:57 +0100 | [diff] [blame] | 5301 | /* There's a protected area at the end of the buffer for rewriting |
| 5302 | * purposes. We don't want to start to parse the request if the |
| 5303 | * protected area is affected, because we may have to move processed |
| 5304 | * data later, which is much more complicated. |
| 5305 | */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 5306 | if (buffer_not_empty(rep->buf) && msg->msg_state < HTTP_MSG_ERROR) { |
Willy Tarreau | 379357a | 2013-06-08 12:55:46 +0200 | [diff] [blame] | 5307 | if (unlikely(!channel_reserved(rep))) { |
| 5308 | /* some data has still not left the buffer, wake us once that's done */ |
| 5309 | if (rep->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_WRITE_ERROR|CF_WRITE_TIMEOUT)) |
| 5310 | goto abort_response; |
| 5311 | channel_dont_close(rep); |
| 5312 | rep->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */ |
Willy Tarreau | d7ad9f5 | 2013-12-31 17:26:25 +0100 | [diff] [blame] | 5313 | rep->flags |= CF_WAKE_WRITE; |
Willy Tarreau | 379357a | 2013-06-08 12:55:46 +0200 | [diff] [blame] | 5314 | return 0; |
Willy Tarreau | 83e3af0 | 2009-12-28 17:39:57 +0100 | [diff] [blame] | 5315 | } |
| 5316 | |
Willy Tarreau | 379357a | 2013-06-08 12:55:46 +0200 | [diff] [blame] | 5317 | if (unlikely(bi_end(rep->buf) < b_ptr(rep->buf, msg->next) || |
| 5318 | bi_end(rep->buf) > rep->buf->data + rep->buf->size - global.tune.maxrewrite)) |
| 5319 | buffer_slow_realign(rep->buf); |
| 5320 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 5321 | if (likely(msg->next < rep->buf->i)) |
Willy Tarreau | a560c21 | 2012-03-09 13:50:57 +0100 | [diff] [blame] | 5322 | http_msg_analyzer(msg, &txn->hdr_idx); |
Willy Tarreau | 83e3af0 | 2009-12-28 17:39:57 +0100 | [diff] [blame] | 5323 | } |
| 5324 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5325 | /* 1: we might have to print this header in debug mode */ |
| 5326 | if (unlikely((global.mode & MODE_DEBUG) && |
| 5327 | (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) && |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 5328 | (msg->msg_state >= HTTP_MSG_BODY || msg->msg_state == HTTP_MSG_ERROR))) { |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5329 | char *eol, *sol; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5330 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 5331 | sol = rep->buf->p; |
| 5332 | 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] | 5333 | debug_hdr("srvrep", s, sol, eol); |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5334 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5335 | sol += hdr_idx_first_pos(&txn->hdr_idx); |
| 5336 | cur_idx = hdr_idx_first_idx(&txn->hdr_idx); |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5337 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5338 | while (cur_idx) { |
| 5339 | eol = sol + txn->hdr_idx.v[cur_idx].len; |
| 5340 | debug_hdr("srvhdr", s, sol, eol); |
| 5341 | sol = eol + txn->hdr_idx.v[cur_idx].cr + 1; |
| 5342 | cur_idx = txn->hdr_idx.v[cur_idx].next; |
| 5343 | } |
| 5344 | } |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5345 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5346 | /* |
| 5347 | * Now we quickly check if we have found a full valid response. |
| 5348 | * If not so, we check the FD and buffer states before leaving. |
| 5349 | * A full response is indicated by the fact that we have seen |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 5350 | * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5351 | * responses are checked first. |
| 5352 | * |
| 5353 | * Depending on whether the client is still there or not, we |
| 5354 | * may send an error response back or not. Note that normally |
| 5355 | * we should only check for HTTP status there, and check I/O |
| 5356 | * errors somewhere else. |
| 5357 | */ |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5358 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 5359 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) { |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5360 | /* Invalid response */ |
| 5361 | if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) { |
| 5362 | /* we detected a parsing error. We want to archive this response |
| 5363 | * in the dedicated proxy area for later troubleshooting. |
| 5364 | */ |
| 5365 | hdr_response_bad: |
| 5366 | if (msg->msg_state == HTTP_MSG_ERROR || msg->err_pos >= 0) |
Willy Tarreau | 8a0cef2 | 2012-03-09 13:39:23 +0100 | [diff] [blame] | 5367 | 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] | 5368 | |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 5369 | s->be->be_counters.failed_resp++; |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 5370 | if (objt_server(s->target)) { |
| 5371 | objt_server(s->target)->counters.failed_resp++; |
| 5372 | health_adjust(objt_server(s->target), HANA_STATUS_HTTP_HDRRSP); |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 5373 | } |
Willy Tarreau | 6464841 | 2010-03-05 10:41:54 +0100 | [diff] [blame] | 5374 | abort_response: |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 5375 | channel_auto_close(rep); |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5376 | rep->analysers = 0; |
| 5377 | txn->status = 502; |
Willy Tarreau | c88ea68 | 2009-12-29 14:56:36 +0100 | [diff] [blame] | 5378 | rep->prod->flags |= SI_FL_NOLINGER; |
Willy Tarreau | 9dab5fc | 2012-05-07 11:56:55 +0200 | [diff] [blame] | 5379 | bi_erase(rep); |
Willy Tarreau | 783f258 | 2012-09-04 12:19:04 +0200 | [diff] [blame] | 5380 | stream_int_retnclose(rep->cons, http_error_message(s, HTTP_ERR_502)); |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5381 | |
| 5382 | if (!(s->flags & SN_ERR_MASK)) |
| 5383 | s->flags |= SN_ERR_PRXCOND; |
| 5384 | if (!(s->flags & SN_FINST_MASK)) |
| 5385 | s->flags |= SN_FINST_H; |
| 5386 | |
| 5387 | return 0; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 5388 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 5389 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5390 | /* too large response does not fit in buffer. */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 5391 | else if (buffer_full(rep->buf, global.tune.maxrewrite)) { |
Willy Tarreau | fec4d89 | 2011-09-02 20:04:57 +0200 | [diff] [blame] | 5392 | if (msg->err_pos < 0) |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 5393 | msg->err_pos = rep->buf->i; |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5394 | goto hdr_response_bad; |
| 5395 | } |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5396 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5397 | /* read error */ |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 5398 | else if (rep->flags & CF_READ_ERROR) { |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5399 | if (msg->err_pos >= 0) |
Willy Tarreau | 8a0cef2 | 2012-03-09 13:39:23 +0100 | [diff] [blame] | 5400 | 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] | 5401 | else if (txn->flags & TX_NOT_FIRST) |
| 5402 | goto abort_keep_alive; |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 5403 | |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 5404 | s->be->be_counters.failed_resp++; |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 5405 | if (objt_server(s->target)) { |
| 5406 | objt_server(s->target)->counters.failed_resp++; |
| 5407 | health_adjust(objt_server(s->target), HANA_STATUS_HTTP_READ_ERROR); |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 5408 | } |
Willy Tarreau | 461f662 | 2008-08-15 23:43:19 +0200 | [diff] [blame] | 5409 | |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 5410 | channel_auto_close(rep); |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5411 | rep->analysers = 0; |
| 5412 | txn->status = 502; |
Willy Tarreau | c88ea68 | 2009-12-29 14:56:36 +0100 | [diff] [blame] | 5413 | rep->prod->flags |= SI_FL_NOLINGER; |
Willy Tarreau | 9dab5fc | 2012-05-07 11:56:55 +0200 | [diff] [blame] | 5414 | bi_erase(rep); |
Willy Tarreau | 783f258 | 2012-09-04 12:19:04 +0200 | [diff] [blame] | 5415 | stream_int_retnclose(rep->cons, http_error_message(s, HTTP_ERR_502)); |
Willy Tarreau | 816b979 | 2009-09-15 21:25:21 +0200 | [diff] [blame] | 5416 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5417 | if (!(s->flags & SN_ERR_MASK)) |
| 5418 | s->flags |= SN_ERR_SRVCL; |
| 5419 | if (!(s->flags & SN_FINST_MASK)) |
| 5420 | s->flags |= SN_FINST_H; |
Willy Tarreau | cebf57e | 2008-08-15 18:16:37 +0200 | [diff] [blame] | 5421 | return 0; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5422 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 5423 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5424 | /* read timeout : return a 504 to the client. */ |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 5425 | else if (rep->flags & CF_READ_TIMEOUT) { |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5426 | if (msg->err_pos >= 0) |
Willy Tarreau | 8a0cef2 | 2012-03-09 13:39:23 +0100 | [diff] [blame] | 5427 | 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] | 5428 | else if (txn->flags & TX_NOT_FIRST) |
| 5429 | goto abort_keep_alive; |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 5430 | |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 5431 | s->be->be_counters.failed_resp++; |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 5432 | if (objt_server(s->target)) { |
| 5433 | objt_server(s->target)->counters.failed_resp++; |
| 5434 | health_adjust(objt_server(s->target), HANA_STATUS_HTTP_READ_TIMEOUT); |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 5435 | } |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 5436 | |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 5437 | channel_auto_close(rep); |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5438 | rep->analysers = 0; |
| 5439 | txn->status = 504; |
Willy Tarreau | c88ea68 | 2009-12-29 14:56:36 +0100 | [diff] [blame] | 5440 | rep->prod->flags |= SI_FL_NOLINGER; |
Willy Tarreau | 9dab5fc | 2012-05-07 11:56:55 +0200 | [diff] [blame] | 5441 | bi_erase(rep); |
Willy Tarreau | 783f258 | 2012-09-04 12:19:04 +0200 | [diff] [blame] | 5442 | stream_int_retnclose(rep->cons, http_error_message(s, HTTP_ERR_504)); |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 5443 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5444 | if (!(s->flags & SN_ERR_MASK)) |
| 5445 | s->flags |= SN_ERR_SRVTO; |
| 5446 | if (!(s->flags & SN_FINST_MASK)) |
| 5447 | s->flags |= SN_FINST_H; |
| 5448 | return 0; |
| 5449 | } |
Willy Tarreau | a7c5276 | 2008-08-16 18:40:18 +0200 | [diff] [blame] | 5450 | |
Willy Tarreau | f003d37 | 2012-11-26 13:35:37 +0100 | [diff] [blame] | 5451 | /* client abort with an abortonclose */ |
| 5452 | else if ((rep->flags & CF_SHUTR) && ((s->req->flags & (CF_SHUTR|CF_SHUTW)) == (CF_SHUTR|CF_SHUTW))) { |
| 5453 | s->fe->fe_counters.cli_aborts++; |
| 5454 | s->be->be_counters.cli_aborts++; |
| 5455 | if (objt_server(s->target)) |
| 5456 | objt_server(s->target)->counters.cli_aborts++; |
| 5457 | |
| 5458 | rep->analysers = 0; |
| 5459 | channel_auto_close(rep); |
| 5460 | |
| 5461 | txn->status = 400; |
| 5462 | bi_erase(rep); |
| 5463 | stream_int_retnclose(rep->cons, http_error_message(s, HTTP_ERR_400)); |
| 5464 | |
| 5465 | if (!(s->flags & SN_ERR_MASK)) |
| 5466 | s->flags |= SN_ERR_CLICL; |
| 5467 | if (!(s->flags & SN_FINST_MASK)) |
| 5468 | s->flags |= SN_FINST_H; |
| 5469 | |
| 5470 | /* process_session() will take care of the error */ |
| 5471 | return 0; |
| 5472 | } |
| 5473 | |
Willy Tarreau | 3b8c08a | 2011-09-02 20:16:24 +0200 | [diff] [blame] | 5474 | /* 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] | 5475 | else if (rep->flags & CF_SHUTR) { |
Willy Tarreau | 3b8c08a | 2011-09-02 20:16:24 +0200 | [diff] [blame] | 5476 | if (msg->msg_state >= HTTP_MSG_RPVER || msg->err_pos >= 0) |
Willy Tarreau | 8a0cef2 | 2012-03-09 13:39:23 +0100 | [diff] [blame] | 5477 | 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] | 5478 | else if (txn->flags & TX_NOT_FIRST) |
| 5479 | goto abort_keep_alive; |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 5480 | |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 5481 | s->be->be_counters.failed_resp++; |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 5482 | if (objt_server(s->target)) { |
| 5483 | objt_server(s->target)->counters.failed_resp++; |
| 5484 | health_adjust(objt_server(s->target), HANA_STATUS_HTTP_BROKEN_PIPE); |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 5485 | } |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 5486 | |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 5487 | channel_auto_close(rep); |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5488 | rep->analysers = 0; |
| 5489 | txn->status = 502; |
Willy Tarreau | c88ea68 | 2009-12-29 14:56:36 +0100 | [diff] [blame] | 5490 | rep->prod->flags |= SI_FL_NOLINGER; |
Willy Tarreau | 9dab5fc | 2012-05-07 11:56:55 +0200 | [diff] [blame] | 5491 | bi_erase(rep); |
Willy Tarreau | 783f258 | 2012-09-04 12:19:04 +0200 | [diff] [blame] | 5492 | stream_int_retnclose(rep->cons, http_error_message(s, HTTP_ERR_502)); |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 5493 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5494 | if (!(s->flags & SN_ERR_MASK)) |
| 5495 | s->flags |= SN_ERR_SRVCL; |
| 5496 | if (!(s->flags & SN_FINST_MASK)) |
| 5497 | s->flags |= SN_FINST_H; |
| 5498 | return 0; |
| 5499 | } |
Krzysztof Piotr Oledzki | 5fb1882 | 2009-10-13 21:14:09 +0200 | [diff] [blame] | 5500 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5501 | /* write error to client (we don't send any message then) */ |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 5502 | else if (rep->flags & CF_WRITE_ERROR) { |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5503 | if (msg->err_pos >= 0) |
Willy Tarreau | 8a0cef2 | 2012-03-09 13:39:23 +0100 | [diff] [blame] | 5504 | 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] | 5505 | else if (txn->flags & TX_NOT_FIRST) |
| 5506 | goto abort_keep_alive; |
Krzysztof Piotr Oledzki | 5fb1882 | 2009-10-13 21:14:09 +0200 | [diff] [blame] | 5507 | |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 5508 | s->be->be_counters.failed_resp++; |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5509 | rep->analysers = 0; |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 5510 | channel_auto_close(rep); |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5511 | |
| 5512 | if (!(s->flags & SN_ERR_MASK)) |
| 5513 | s->flags |= SN_ERR_CLICL; |
| 5514 | if (!(s->flags & SN_FINST_MASK)) |
| 5515 | s->flags |= SN_FINST_H; |
| 5516 | |
| 5517 | /* process_session() will take care of the error */ |
| 5518 | return 0; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5519 | } |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 5520 | |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 5521 | channel_dont_close(rep); |
Willy Tarreau | 3f3997e | 2013-12-15 15:21:32 +0100 | [diff] [blame] | 5522 | rep->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */ |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5523 | return 0; |
| 5524 | } |
| 5525 | |
| 5526 | /* More interesting part now : we know that we have a complete |
| 5527 | * response which at least looks like HTTP. We have an indicator |
| 5528 | * of each header's length, so we can parse them quickly. |
| 5529 | */ |
| 5530 | |
| 5531 | if (unlikely(msg->err_pos >= 0)) |
Willy Tarreau | 8a0cef2 | 2012-03-09 13:39:23 +0100 | [diff] [blame] | 5532 | 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] | 5533 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5534 | /* |
| 5535 | * 1: get the status code |
| 5536 | */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 5537 | n = rep->buf->p[msg->sl.st.c] - '0'; |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5538 | if (n < 1 || n > 5) |
| 5539 | n = 0; |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 5540 | /* when the client triggers a 4xx from the server, it's most often due |
| 5541 | * to a missing object or permission. These events should be tracked |
| 5542 | * because if they happen often, it may indicate a brute force or a |
| 5543 | * vulnerability scan. |
| 5544 | */ |
| 5545 | if (n == 4) |
| 5546 | session_inc_http_err_ctr(s); |
| 5547 | |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 5548 | if (objt_server(s->target)) |
| 5549 | objt_server(s->target)->counters.p.http.rsp[n]++; |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5550 | |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 5551 | /* check if the response is HTTP/1.1 or above */ |
| 5552 | if ((msg->sl.st.v_l == 8) && |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 5553 | ((rep->buf->p[5] > '1') || |
| 5554 | ((rep->buf->p[5] == '1') && (rep->buf->p[7] >= '1')))) |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 5555 | msg->flags |= HTTP_MSGF_VER_11; |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 5556 | |
| 5557 | /* "connection" has not been parsed yet */ |
Willy Tarreau | 50fc777 | 2012-11-11 22:19:57 +0100 | [diff] [blame] | 5558 | 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] | 5559 | |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 5560 | /* transfer length unknown*/ |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 5561 | msg->flags &= ~HTTP_MSGF_XFER_LEN; |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 5562 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 5563 | 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] | 5564 | |
Willy Tarreau | 3965040 | 2010-03-15 19:44:39 +0100 | [diff] [blame] | 5565 | /* Adjust server's health based on status code. Note: status codes 501 |
| 5566 | * and 505 are triggered on demand by client request, so we must not |
| 5567 | * count them as server failures. |
| 5568 | */ |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 5569 | if (objt_server(s->target)) { |
Willy Tarreau | d45b3d5 | 2010-05-20 11:49:03 +0200 | [diff] [blame] | 5570 | 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] | 5571 | health_adjust(objt_server(s->target), HANA_STATUS_HTTP_OK); |
Willy Tarreau | d45b3d5 | 2010-05-20 11:49:03 +0200 | [diff] [blame] | 5572 | else |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 5573 | health_adjust(objt_server(s->target), HANA_STATUS_HTTP_STS); |
Willy Tarreau | d45b3d5 | 2010-05-20 11:49:03 +0200 | [diff] [blame] | 5574 | } |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 5575 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5576 | /* |
| 5577 | * 2: check for cacheability. |
| 5578 | */ |
| 5579 | |
| 5580 | switch (txn->status) { |
| 5581 | case 200: |
| 5582 | case 203: |
| 5583 | case 206: |
| 5584 | case 300: |
| 5585 | case 301: |
| 5586 | case 410: |
| 5587 | /* RFC2616 @13.4: |
| 5588 | * "A response received with a status code of |
| 5589 | * 200, 203, 206, 300, 301 or 410 MAY be stored |
| 5590 | * by a cache (...) unless a cache-control |
| 5591 | * directive prohibits caching." |
| 5592 | * |
| 5593 | * RFC2616 @9.5: POST method : |
| 5594 | * "Responses to this method are not cacheable, |
| 5595 | * unless the response includes appropriate |
| 5596 | * Cache-Control or Expires header fields." |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5597 | */ |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5598 | if (likely(txn->meth != HTTP_METH_POST) && |
Willy Tarreau | 6740213 | 2012-05-31 20:40:20 +0200 | [diff] [blame] | 5599 | ((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] | 5600 | txn->flags |= TX_CACHEABLE | TX_CACHE_COOK; |
| 5601 | break; |
| 5602 | default: |
| 5603 | break; |
| 5604 | } |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5605 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5606 | /* |
| 5607 | * 3: we may need to capture headers |
| 5608 | */ |
| 5609 | s->logs.logwait &= ~LW_RESP; |
Willy Tarreau | 42f7d89 | 2012-03-24 08:28:09 +0100 | [diff] [blame] | 5610 | if (unlikely((s->logs.logwait & LW_RSPHDR) && txn->rsp.cap)) |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 5611 | capture_headers(rep->buf->p, &txn->hdr_idx, |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5612 | txn->rsp.cap, s->fe->rsp_cap); |
| 5613 | |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 5614 | /* 4: determine the transfer-length. |
| 5615 | * According to RFC2616 #4.4, amended by the HTTPbis working group, |
| 5616 | * the presence of a message-body in a RESPONSE and its transfer length |
| 5617 | * must be determined that way : |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 5618 | * |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 5619 | * All responses to the HEAD request method MUST NOT include a |
| 5620 | * message-body, even though the presence of entity-header fields |
| 5621 | * might lead one to believe they do. All 1xx (informational), 204 |
| 5622 | * (No Content), and 304 (Not Modified) responses MUST NOT include a |
| 5623 | * message-body. All other responses do include a message-body, |
| 5624 | * although it MAY be of zero length. |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 5625 | * |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 5626 | * 1. Any response which "MUST NOT" include a message-body (such as the |
| 5627 | * 1xx, 204 and 304 responses and any response to a HEAD request) is |
| 5628 | * always terminated by the first empty line after the header fields, |
| 5629 | * regardless of the entity-header fields present in the message. |
| 5630 | * |
| 5631 | * 2. If a Transfer-Encoding header field (Section 9.7) is present and |
| 5632 | * the "chunked" transfer-coding (Section 6.2) is used, the |
| 5633 | * transfer-length is defined by the use of this transfer-coding. |
| 5634 | * If a Transfer-Encoding header field is present and the "chunked" |
| 5635 | * transfer-coding is not present, the transfer-length is defined by |
| 5636 | * the sender closing the connection. |
| 5637 | * |
| 5638 | * 3. If a Content-Length header field is present, its decimal value in |
| 5639 | * OCTETs represents both the entity-length and the transfer-length. |
| 5640 | * If a message is received with both a Transfer-Encoding header |
| 5641 | * field and a Content-Length header field, the latter MUST be ignored. |
| 5642 | * |
| 5643 | * 4. If the message uses the media type "multipart/byteranges", and |
| 5644 | * the transfer-length is not otherwise specified, then this self- |
| 5645 | * delimiting media type defines the transfer-length. This media |
| 5646 | * type MUST NOT be used unless the sender knows that the recipient |
| 5647 | * can parse it; the presence in a request of a Range header with |
| 5648 | * multiple byte-range specifiers from a 1.1 client implies that the |
| 5649 | * client can parse multipart/byteranges responses. |
| 5650 | * |
| 5651 | * 5. By the server closing the connection. |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 5652 | */ |
| 5653 | |
| 5654 | /* Skip parsing if no content length is possible. The response flags |
Willy Tarreau | 124d991 | 2011-03-01 20:30:48 +0100 | [diff] [blame] | 5655 | * 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] | 5656 | * 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] | 5657 | * FIXME: should we parse anyway and return an error on chunked encoding ? |
| 5658 | */ |
| 5659 | if (txn->meth == HTTP_METH_HEAD || |
| 5660 | (txn->status >= 100 && txn->status < 200) || |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 5661 | txn->status == 204 || txn->status == 304) { |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 5662 | msg->flags |= HTTP_MSGF_XFER_LEN; |
Willy Tarreau | 9101535 | 2012-11-27 07:31:33 +0100 | [diff] [blame] | 5663 | s->comp_algo = NULL; |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 5664 | goto skip_content_length; |
| 5665 | } |
| 5666 | |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 5667 | use_close_only = 0; |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 5668 | ctx.idx = 0; |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 5669 | while ((msg->flags & HTTP_MSGF_VER_11) && |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 5670 | 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] | 5671 | if (ctx.vlen == 7 && strncasecmp(ctx.line + ctx.val, "chunked", 7) == 0) |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 5672 | msg->flags |= (HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN); |
| 5673 | else if (msg->flags & HTTP_MSGF_TE_CHNK) { |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 5674 | /* bad transfer-encoding (chunked followed by something else) */ |
| 5675 | use_close_only = 1; |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 5676 | msg->flags &= ~(HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN); |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 5677 | break; |
| 5678 | } |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 5679 | } |
| 5680 | |
| 5681 | /* FIXME: below we should remove the content-length header(s) in case of chunked encoding */ |
| 5682 | ctx.idx = 0; |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 5683 | while (!(msg->flags & HTTP_MSGF_TE_CHNK) && !use_close_only && |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 5684 | 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] | 5685 | signed long long cl; |
| 5686 | |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 5687 | if (!ctx.vlen) { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 5688 | msg->err_pos = ctx.line + ctx.val - rep->buf->p; |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 5689 | goto hdr_response_bad; |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 5690 | } |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 5691 | |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 5692 | if (strl2llrc(ctx.line + ctx.val, ctx.vlen, &cl)) { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 5693 | msg->err_pos = ctx.line + ctx.val - rep->buf->p; |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 5694 | goto hdr_response_bad; /* parse failure */ |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 5695 | } |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 5696 | |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 5697 | if (cl < 0) { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 5698 | msg->err_pos = ctx.line + ctx.val - rep->buf->p; |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 5699 | goto hdr_response_bad; |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 5700 | } |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 5701 | |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 5702 | if ((msg->flags & HTTP_MSGF_CNT_LEN) && (msg->chunk_len != cl)) { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 5703 | msg->err_pos = ctx.line + ctx.val - rep->buf->p; |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 5704 | goto hdr_response_bad; /* already specified, was different */ |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 5705 | } |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 5706 | |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 5707 | msg->flags |= HTTP_MSGF_CNT_LEN | HTTP_MSGF_XFER_LEN; |
Willy Tarreau | 124d991 | 2011-03-01 20:30:48 +0100 | [diff] [blame] | 5708 | msg->body_len = msg->chunk_len = cl; |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 5709 | } |
| 5710 | |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 5711 | if (s->fe->comp || s->be->comp) |
| 5712 | select_compression_response_header(s, rep->buf); |
| 5713 | |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 5714 | /* FIXME: we should also implement the multipart/byterange method. |
| 5715 | * For now on, we resort to close mode in this case (unknown length). |
| 5716 | */ |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 5717 | skip_content_length: |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 5718 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5719 | /* end of job, return OK */ |
| 5720 | rep->analysers &= ~an_bit; |
| 5721 | rep->analyse_exp = TICK_ETERNITY; |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 5722 | channel_auto_close(rep); |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5723 | return 1; |
Willy Tarreau | 6b726ad | 2013-12-15 19:31:37 +0100 | [diff] [blame] | 5724 | |
| 5725 | abort_keep_alive: |
| 5726 | /* A keep-alive request to the server failed on a network error. |
| 5727 | * The client is required to retry. We need to close without returning |
| 5728 | * any other information so that the client retries. |
| 5729 | */ |
| 5730 | txn->status = 0; |
| 5731 | rep->analysers = 0; |
| 5732 | s->req->analysers = 0; |
| 5733 | channel_auto_close(rep); |
| 5734 | s->logs.logwait = 0; |
| 5735 | s->logs.level = 0; |
| 5736 | s->rep->flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */ |
| 5737 | bi_erase(rep); |
| 5738 | stream_int_retnclose(rep->cons, NULL); |
| 5739 | return 0; |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5740 | } |
| 5741 | |
| 5742 | /* This function performs all the processing enabled for the current response. |
Willy Tarreau | e3fa6e5 | 2010-01-04 22:57:43 +0100 | [diff] [blame] | 5743 | * It normally returns 1 unless it wants to break. It relies on buffers flags, |
| 5744 | * and updates t->rep->analysers. It might make sense to explode it into several |
| 5745 | * other functions. It works like process_request (see indications above). |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5746 | */ |
Willy Tarreau | 7421efb | 2012-07-02 15:11:27 +0200 | [diff] [blame] | 5747 | 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] | 5748 | { |
| 5749 | struct http_txn *txn = &t->txn; |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5750 | struct http_msg *msg = &txn->rsp; |
| 5751 | struct proxy *cur_proxy; |
Willy Tarreau | f4f0412 | 2010-01-28 18:10:50 +0100 | [diff] [blame] | 5752 | struct cond_wordlist *wl; |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 5753 | struct http_res_rule *http_res_last_rule = NULL; |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5754 | |
Willy Tarreau | 02d6cfc | 2012-03-01 18:19:58 +0100 | [diff] [blame] | 5755 | 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] | 5756 | now_ms, __FUNCTION__, |
| 5757 | t, |
| 5758 | rep, |
| 5759 | rep->rex, rep->wex, |
| 5760 | rep->flags, |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 5761 | rep->buf->i, |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5762 | rep->analysers); |
| 5763 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 5764 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) /* we need more data */ |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5765 | return 0; |
| 5766 | |
| 5767 | rep->analysers &= ~an_bit; |
| 5768 | rep->analyse_exp = TICK_ETERNITY; |
| 5769 | |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 5770 | /* Now we have to check if we need to modify the Connection header. |
| 5771 | * This is more difficult on the response than it is on the request, |
| 5772 | * because we can have two different HTTP versions and we don't know |
| 5773 | * how the client will interprete a response. For instance, let's say |
| 5774 | * that the client sends a keep-alive request in HTTP/1.0 and gets an |
| 5775 | * HTTP/1.1 response without any header. Maybe it will bound itself to |
| 5776 | * HTTP/1.0 because it only knows about it, and will consider the lack |
| 5777 | * of header as a close, or maybe it knows HTTP/1.1 and can consider |
| 5778 | * the lack of header as a keep-alive. Thus we will use two flags |
| 5779 | * indicating how a request MAY be understood by the client. In case |
| 5780 | * of multiple possibilities, we'll fix the header to be explicit. If |
| 5781 | * ambiguous cases such as both close and keepalive are seen, then we |
| 5782 | * will fall back to explicit close. Note that we won't take risks with |
| 5783 | * HTTP/1.0 clients which may not necessarily understand keep-alive. |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 5784 | * See doc/internals/connection-header.txt for the complete matrix. |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 5785 | */ |
| 5786 | |
Willy Tarreau | dc008c5 | 2010-02-01 16:20:08 +0100 | [diff] [blame] | 5787 | if (unlikely((txn->meth == HTTP_METH_CONNECT && txn->status == 200) || |
| 5788 | txn->status == 101)) { |
| 5789 | /* Either we've established an explicit tunnel, or we're |
| 5790 | * switching the protocol. In both cases, we're very unlikely |
Willy Tarreau | 5843d1a | 2010-02-01 15:13:32 +0100 | [diff] [blame] | 5791 | * to understand the next protocols. We have to switch to tunnel |
| 5792 | * mode, so that we transfer the request and responses then let |
| 5793 | * this protocol pass unmodified. When we later implement specific |
| 5794 | * parsers for such protocols, we'll want to check the Upgrade |
Willy Tarreau | dc008c5 | 2010-02-01 16:20:08 +0100 | [diff] [blame] | 5795 | * header which contains information about that protocol for |
| 5796 | * responses with status 101 (eg: see RFC2817 about TLS). |
Willy Tarreau | 5843d1a | 2010-02-01 15:13:32 +0100 | [diff] [blame] | 5797 | */ |
| 5798 | txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_TUN; |
| 5799 | } |
Willy Tarreau | dc008c5 | 2010-02-01 16:20:08 +0100 | [diff] [blame] | 5800 | else if ((txn->status >= 200) && !(txn->flags & TX_HDR_CONN_PRS) && |
| 5801 | ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN || |
Willy Tarreau | 02bce8b | 2014-01-30 00:15:28 +0100 | [diff] [blame] | 5802 | ((t->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL || |
| 5803 | (t->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL))) { |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 5804 | int to_del = 0; |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 5805 | |
Willy Tarreau | 70dffda | 2014-01-30 03:07:23 +0100 | [diff] [blame] | 5806 | /* this situation happens when combining pretend-keepalive with httpclose. */ |
| 5807 | if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL && |
| 5808 | ((t->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL || |
| 5809 | (t->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL)) |
| 5810 | txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO; |
| 5811 | |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 5812 | /* on unknown transfer length, we must close */ |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 5813 | if (!(msg->flags & HTTP_MSGF_XFER_LEN) && |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 5814 | (txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) |
| 5815 | txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO; |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 5816 | |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 5817 | /* now adjust header transformations depending on current state */ |
| 5818 | if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN || |
| 5819 | (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) { |
| 5820 | to_del |= 2; /* remove "keep-alive" on any response */ |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 5821 | if (!(msg->flags & HTTP_MSGF_VER_11)) |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 5822 | to_del |= 1; /* remove "close" for HTTP/1.0 responses */ |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 5823 | } |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 5824 | else { /* SCL / KAL */ |
| 5825 | to_del |= 1; /* remove "close" on any response */ |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 5826 | if (txn->req.flags & msg->flags & HTTP_MSGF_VER_11) |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 5827 | to_del |= 2; /* remove "keep-alive" on pure 1.1 responses */ |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 5828 | } |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 5829 | |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 5830 | /* Parse and remove some headers from the connection header */ |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 5831 | http_parse_connection_header(txn, msg, to_del); |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 5832 | |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 5833 | /* Some keep-alive responses are converted to Server-close if |
| 5834 | * the server wants to close. |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 5835 | */ |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 5836 | if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL) { |
| 5837 | if ((txn->flags & TX_HDR_CONN_CLO) || |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 5838 | (!(txn->flags & TX_HDR_CONN_KAL) && !(msg->flags & HTTP_MSGF_VER_11))) |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 5839 | txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_SCL; |
Willy Tarreau | b608feb | 2010-01-02 22:47:18 +0100 | [diff] [blame] | 5840 | } |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 5841 | } |
| 5842 | |
Willy Tarreau | 7959a55 | 2013-09-23 16:44:27 +0200 | [diff] [blame] | 5843 | /* we want to have the response time before we start processing it */ |
| 5844 | t->logs.t_data = tv_ms_elapsed(&t->logs.tv_accept, &now); |
| 5845 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5846 | if (1) { |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5847 | /* |
| 5848 | * 3: we will have to evaluate the filters. |
| 5849 | * As opposed to version 1.2, now they will be evaluated in the |
| 5850 | * filters order and not in the header order. This means that |
| 5851 | * each filter has to be validated among all headers. |
| 5852 | * |
| 5853 | * Filters are tried with ->be first, then with ->fe if it is |
| 5854 | * different from ->be. |
| 5855 | */ |
| 5856 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5857 | cur_proxy = t->be; |
| 5858 | while (1) { |
| 5859 | struct proxy *rule_set = cur_proxy; |
| 5860 | |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 5861 | /* evaluate http-response rules */ |
| 5862 | if (!http_res_last_rule) |
| 5863 | http_res_last_rule = http_res_get_intercept_rule(cur_proxy, &cur_proxy->http_res_rules, t, txn); |
| 5864 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5865 | /* try headers filters */ |
| 5866 | if (rule_set->rsp_exp != NULL) { |
Willy Tarreau | fdb563c | 2010-01-31 15:43:27 +0100 | [diff] [blame] | 5867 | if (apply_filters_to_response(t, rep, rule_set) < 0) { |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5868 | return_bad_resp: |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 5869 | if (objt_server(t->target)) { |
| 5870 | objt_server(t->target)->counters.failed_resp++; |
| 5871 | health_adjust(objt_server(t->target), HANA_STATUS_HTTP_RSP); |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 5872 | } |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 5873 | t->be->be_counters.failed_resp++; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5874 | return_srv_prx_502: |
Willy Tarreau | 2df28e8 | 2008-08-17 15:20:19 +0200 | [diff] [blame] | 5875 | rep->analysers = 0; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5876 | txn->status = 502; |
Willy Tarreau | 7959a55 | 2013-09-23 16:44:27 +0200 | [diff] [blame] | 5877 | t->logs.t_data = -1; /* was not a valid response */ |
Willy Tarreau | c88ea68 | 2009-12-29 14:56:36 +0100 | [diff] [blame] | 5878 | rep->prod->flags |= SI_FL_NOLINGER; |
Willy Tarreau | 9dab5fc | 2012-05-07 11:56:55 +0200 | [diff] [blame] | 5879 | bi_erase(rep); |
Willy Tarreau | 783f258 | 2012-09-04 12:19:04 +0200 | [diff] [blame] | 5880 | stream_int_retnclose(rep->cons, http_error_message(t, HTTP_ERR_502)); |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5881 | if (!(t->flags & SN_ERR_MASK)) |
| 5882 | t->flags |= SN_ERR_PRXCOND; |
| 5883 | if (!(t->flags & SN_FINST_MASK)) |
| 5884 | t->flags |= SN_FINST_H; |
Willy Tarreau | dafde43 | 2008-08-17 01:00:46 +0200 | [diff] [blame] | 5885 | return 0; |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 5886 | } |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5887 | } |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 5888 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5889 | /* has the response been denied ? */ |
| 5890 | if (txn->flags & TX_SVDENY) { |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 5891 | if (objt_server(t->target)) |
| 5892 | objt_server(t->target)->counters.failed_secu++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 5893 | |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 5894 | t->be->be_counters.denied_resp++; |
| 5895 | t->fe->fe_counters.denied_resp++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 5896 | if (t->listener->counters) |
| 5897 | t->listener->counters->denied_resp++; |
| 5898 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5899 | goto return_srv_prx_502; |
Willy Tarreau | 5140623 | 2008-03-10 22:04:20 +0100 | [diff] [blame] | 5900 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 5901 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5902 | /* add response headers from the rule sets in the same order */ |
Willy Tarreau | deb9ed8 | 2010-01-03 21:03:22 +0100 | [diff] [blame] | 5903 | list_for_each_entry(wl, &rule_set->rsp_add, list) { |
Willy Tarreau | 816b979 | 2009-09-15 21:25:21 +0200 | [diff] [blame] | 5904 | if (txn->status < 200) |
| 5905 | break; |
Willy Tarreau | fdb563c | 2010-01-31 15:43:27 +0100 | [diff] [blame] | 5906 | if (wl->cond) { |
Willy Tarreau | 32a6f2e | 2012-04-25 10:13:36 +0200 | [diff] [blame] | 5907 | 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] | 5908 | ret = acl_pass(ret); |
| 5909 | if (((struct acl_cond *)wl->cond)->pol == ACL_COND_UNLESS) |
| 5910 | ret = !ret; |
| 5911 | if (!ret) |
| 5912 | continue; |
| 5913 | } |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 5914 | 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] | 5915 | goto return_bad_resp; |
Willy Tarreau | 0bbc3cf | 2006-10-15 14:26:02 +0200 | [diff] [blame] | 5916 | } |
| 5917 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5918 | /* check whether we're already working on the frontend */ |
| 5919 | if (cur_proxy == t->fe) |
| 5920 | break; |
| 5921 | cur_proxy = t->fe; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 5922 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 5923 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5924 | /* |
Willy Tarreau | 5843d1a | 2010-02-01 15:13:32 +0100 | [diff] [blame] | 5925 | * We may be facing a 100-continue response, in which case this |
| 5926 | * is not the right response, and we're waiting for the next one. |
| 5927 | * 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] | 5928 | * next one. |
| 5929 | */ |
Willy Tarreau | 5843d1a | 2010-02-01 15:13:32 +0100 | [diff] [blame] | 5930 | if (unlikely(txn->status == 100)) { |
Willy Tarreau | 63c9e5f | 2009-12-22 16:01:27 +0100 | [diff] [blame] | 5931 | hdr_idx_init(&txn->hdr_idx); |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 5932 | msg->next -= channel_forward(rep, msg->next); |
Willy Tarreau | 63c9e5f | 2009-12-22 16:01:27 +0100 | [diff] [blame] | 5933 | msg->msg_state = HTTP_MSG_RPBEFORE; |
| 5934 | txn->status = 0; |
Willy Tarreau | 7959a55 | 2013-09-23 16:44:27 +0200 | [diff] [blame] | 5935 | t->logs.t_data = -1; /* was not a response yet */ |
Willy Tarreau | 63c9e5f | 2009-12-22 16:01:27 +0100 | [diff] [blame] | 5936 | rep->analysers |= AN_RES_WAIT_HTTP | an_bit; |
| 5937 | return 1; |
| 5938 | } |
Willy Tarreau | 5843d1a | 2010-02-01 15:13:32 +0100 | [diff] [blame] | 5939 | else if (unlikely(txn->status < 200)) |
| 5940 | goto skip_header_mangling; |
Willy Tarreau | 63c9e5f | 2009-12-22 16:01:27 +0100 | [diff] [blame] | 5941 | |
| 5942 | /* we don't have any 1xx status code now */ |
| 5943 | |
| 5944 | /* |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5945 | * 4: check for server cookie. |
| 5946 | */ |
Willy Tarreau | 63c9e5f | 2009-12-22 16:01:27 +0100 | [diff] [blame] | 5947 | if (t->be->cookie_name || t->be->appsession_name || t->fe->capture_name || |
| 5948 | (t->be->options & PR_O_CHK_CACHE)) |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5949 | manage_server_side_cookies(t, rep); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 5950 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 5951 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5952 | /* |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5953 | * 5: check for cache-control or pragma headers if required. |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5954 | */ |
Willy Tarreau | 6740213 | 2012-05-31 20:40:20 +0200 | [diff] [blame] | 5955 | 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] | 5956 | check_response_for_cacheability(t, rep); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5957 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5958 | /* |
| 5959 | * 6: add server cookie in the response if needed |
| 5960 | */ |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 5961 | if (objt_server(t->target) && (t->be->ck_opts & PR_CK_INS) && |
Willy Tarreau | 6740213 | 2012-05-31 20:40:20 +0200 | [diff] [blame] | 5962 | !((txn->flags & TX_SCK_FOUND) && (t->be->ck_opts & PR_CK_PSV)) && |
Willy Tarreau | ef4f391 | 2010-10-07 21:00:29 +0200 | [diff] [blame] | 5963 | (!(t->flags & SN_DIRECT) || |
| 5964 | ((t->be->cookie_maxidle || txn->cookie_last_date) && |
| 5965 | (!txn->cookie_last_date || (txn->cookie_last_date - date.tv_sec) < 0)) || |
| 5966 | (t->be->cookie_maxlife && !txn->cookie_first_date) || // set the first_date |
| 5967 | (!t->be->cookie_maxlife && txn->cookie_first_date)) && // remove the first_date |
Willy Tarreau | 6740213 | 2012-05-31 20:40:20 +0200 | [diff] [blame] | 5968 | (!(t->be->ck_opts & PR_CK_POST) || (txn->meth == HTTP_METH_POST)) && |
Cyril Bonté | 47fdd8e | 2010-04-25 00:00:51 +0200 | [diff] [blame] | 5969 | !(t->flags & SN_IGNORE_PRST)) { |
Willy Tarreau | ef4f391 | 2010-10-07 21:00:29 +0200 | [diff] [blame] | 5970 | /* the server is known, it's not the one the client requested, or the |
| 5971 | * cookie's last seen date needs to be refreshed. We have to |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5972 | * insert a set-cookie here, except if we want to insert only on POST |
| 5973 | * requests and this one isn't. Note that servers which don't have cookies |
| 5974 | * (eg: some backup servers) will return a full cookie removal request. |
| 5975 | */ |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 5976 | if (!objt_server(t->target)->cookie) { |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 5977 | chunk_printf(&trash, |
Willy Tarreau | ef4f391 | 2010-10-07 21:00:29 +0200 | [diff] [blame] | 5978 | "Set-Cookie: %s=; Expires=Thu, 01-Jan-1970 00:00:01 GMT; path=/", |
| 5979 | t->be->cookie_name); |
| 5980 | } |
| 5981 | else { |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 5982 | 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] | 5983 | |
| 5984 | if (t->be->cookie_maxidle || t->be->cookie_maxlife) { |
| 5985 | /* emit last_date, which is mandatory */ |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 5986 | trash.str[trash.len++] = COOKIE_DELIM_DATE; |
| 5987 | s30tob64((date.tv_sec+3) >> 2, trash.str + trash.len); |
| 5988 | trash.len += 5; |
| 5989 | |
Willy Tarreau | ef4f391 | 2010-10-07 21:00:29 +0200 | [diff] [blame] | 5990 | if (t->be->cookie_maxlife) { |
| 5991 | /* emit first_date, which is either the original one or |
| 5992 | * the current date. |
| 5993 | */ |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 5994 | trash.str[trash.len++] = COOKIE_DELIM_DATE; |
Willy Tarreau | ef4f391 | 2010-10-07 21:00:29 +0200 | [diff] [blame] | 5995 | s30tob64(txn->cookie_first_date ? |
| 5996 | txn->cookie_first_date >> 2 : |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 5997 | (date.tv_sec+3) >> 2, trash.str + trash.len); |
| 5998 | trash.len += 5; |
Willy Tarreau | ef4f391 | 2010-10-07 21:00:29 +0200 | [diff] [blame] | 5999 | } |
| 6000 | } |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 6001 | chunk_appendf(&trash, "; path=/"); |
Willy Tarreau | ef4f391 | 2010-10-07 21:00:29 +0200 | [diff] [blame] | 6002 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 6003 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 6004 | if (t->be->cookie_domain) |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 6005 | chunk_appendf(&trash, "; domain=%s", t->be->cookie_domain); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 6006 | |
Willy Tarreau | 4992dd2 | 2012-05-31 21:02:17 +0200 | [diff] [blame] | 6007 | if (t->be->ck_opts & PR_CK_HTTPONLY) |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 6008 | chunk_appendf(&trash, "; HttpOnly"); |
Willy Tarreau | 4992dd2 | 2012-05-31 21:02:17 +0200 | [diff] [blame] | 6009 | |
| 6010 | if (t->be->ck_opts & PR_CK_SECURE) |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 6011 | chunk_appendf(&trash, "; Secure"); |
Willy Tarreau | 4992dd2 | 2012-05-31 21:02:17 +0200 | [diff] [blame] | 6012 | |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 6013 | 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] | 6014 | goto return_bad_resp; |
Willy Tarreau | ef4f391 | 2010-10-07 21:00:29 +0200 | [diff] [blame] | 6015 | |
Willy Tarreau | f134831 | 2010-10-07 15:54:11 +0200 | [diff] [blame] | 6016 | txn->flags &= ~TX_SCK_MASK; |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 6017 | if (objt_server(t->target)->cookie && (t->flags & SN_DIRECT)) |
Willy Tarreau | ef4f391 | 2010-10-07 21:00:29 +0200 | [diff] [blame] | 6018 | /* the server did not change, only the date was updated */ |
| 6019 | txn->flags |= TX_SCK_UPDATED; |
| 6020 | else |
| 6021 | txn->flags |= TX_SCK_INSERTED; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 6022 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 6023 | /* Here, we will tell an eventual cache on the client side that we don't |
| 6024 | * want it to cache this reply because HTTP/1.0 caches also cache cookies ! |
| 6025 | * Some caches understand the correct form: 'no-cache="set-cookie"', but |
| 6026 | * others don't (eg: apache <= 1.3.26). So we use 'private' instead. |
| 6027 | */ |
Willy Tarreau | 6740213 | 2012-05-31 20:40:20 +0200 | [diff] [blame] | 6028 | if ((t->be->ck_opts & PR_CK_NOC) && (txn->flags & TX_CACHEABLE)) { |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 6029 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 6030 | txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK; |
| 6031 | |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 6032 | if (unlikely(http_header_add_tail2(&txn->rsp, &txn->hdr_idx, |
Willy Tarreau | 58cc872 | 2009-12-28 06:57:33 +0100 | [diff] [blame] | 6033 | "Cache-control: private", 22) < 0)) |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 6034 | goto return_bad_resp; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6035 | } |
| 6036 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 6037 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 6038 | /* |
| 6039 | * 7: check if result will be cacheable with a cookie. |
| 6040 | * We'll block the response if security checks have caught |
| 6041 | * nasty things such as a cacheable cookie. |
| 6042 | */ |
Willy Tarreau | f134831 | 2010-10-07 15:54:11 +0200 | [diff] [blame] | 6043 | if (((txn->flags & (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) == |
| 6044 | (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) && |
Willy Tarreau | 63c9e5f | 2009-12-22 16:01:27 +0100 | [diff] [blame] | 6045 | (t->be->options & PR_O_CHK_CACHE)) { |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 6046 | |
| 6047 | /* we're in presence of a cacheable response containing |
| 6048 | * a set-cookie header. We'll block it as requested by |
| 6049 | * the 'checkcache' option, and send an alert. |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6050 | */ |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 6051 | if (objt_server(t->target)) |
| 6052 | objt_server(t->target)->counters.failed_secu++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 6053 | |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 6054 | t->be->be_counters.denied_resp++; |
| 6055 | t->fe->fe_counters.denied_resp++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 6056 | if (t->listener->counters) |
| 6057 | t->listener->counters->denied_resp++; |
| 6058 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 6059 | Alert("Blocking cacheable cookie in response from instance %s, server %s.\n", |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 6060 | 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] | 6061 | send_log(t->be, LOG_ALERT, |
| 6062 | "Blocking cacheable cookie in response from instance %s, server %s.\n", |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 6063 | 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] | 6064 | goto return_srv_prx_502; |
| 6065 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6066 | |
| 6067 | /* |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 6068 | * 8: adjust "Connection: close" or "Connection: keep-alive" if needed. |
Willy Tarreau | 50fc777 | 2012-11-11 22:19:57 +0100 | [diff] [blame] | 6069 | * If an "Upgrade" token is found, the header is left untouched in order |
| 6070 | * not to have to deal with some client bugs : some of them fail an upgrade |
| 6071 | * if anything but "Upgrade" is present in the Connection header. |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6072 | */ |
Willy Tarreau | 50fc777 | 2012-11-11 22:19:57 +0100 | [diff] [blame] | 6073 | if (!(txn->flags & TX_HDR_CONN_UPG) && |
| 6074 | (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) || |
Willy Tarreau | 02bce8b | 2014-01-30 00:15:28 +0100 | [diff] [blame] | 6075 | ((t->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL || |
| 6076 | (t->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL))) { |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 6077 | unsigned int want_flags = 0; |
| 6078 | |
| 6079 | if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL || |
| 6080 | (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) { |
| 6081 | /* we want a keep-alive response here. Keep-alive header |
| 6082 | * required if either side is not 1.1. |
| 6083 | */ |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 6084 | if (!(txn->req.flags & msg->flags & HTTP_MSGF_VER_11)) |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 6085 | want_flags |= TX_CON_KAL_SET; |
| 6086 | } |
| 6087 | else { |
| 6088 | /* we want a close response here. Close header required if |
| 6089 | * the server is 1.1, regardless of the client. |
| 6090 | */ |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 6091 | if (msg->flags & HTTP_MSGF_VER_11) |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 6092 | want_flags |= TX_CON_CLO_SET; |
| 6093 | } |
| 6094 | |
| 6095 | 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] | 6096 | http_change_connection_header(txn, msg, want_flags); |
Willy Tarreau | b608feb | 2010-01-02 22:47:18 +0100 | [diff] [blame] | 6097 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6098 | |
Willy Tarreau | 5843d1a | 2010-02-01 15:13:32 +0100 | [diff] [blame] | 6099 | skip_header_mangling: |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 6100 | if ((msg->flags & HTTP_MSGF_XFER_LEN) || |
Willy Tarreau | dc008c5 | 2010-02-01 16:20:08 +0100 | [diff] [blame] | 6101 | (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN) |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 6102 | rep->analysers |= AN_RES_HTTP_XFER_BODY; |
Willy Tarreau | 0394594 | 2009-12-22 16:50:27 +0100 | [diff] [blame] | 6103 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 6104 | /************************************************************* |
| 6105 | * OK, that's finished for the headers. We have done what we * |
| 6106 | * could. Let's switch to the DATA state. * |
| 6107 | ************************************************************/ |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 6108 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 6109 | /* if the user wants to log as soon as possible, without counting |
| 6110 | * bytes from the server, then this is the right moment. We have |
| 6111 | * to temporarily assign bytes_out to log what we currently have. |
| 6112 | */ |
Willy Tarreau | d79a3b2 | 2012-12-28 09:40:16 +0100 | [diff] [blame] | 6113 | if (!LIST_ISEMPTY(&t->fe->logformat) && !(t->logs.logwait & LW_BYTES)) { |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 6114 | t->logs.t_close = t->logs.t_data; /* to get a valid end date */ |
| 6115 | t->logs.bytes_out = txn->rsp.eoh; |
Willy Tarreau | a5555ec | 2008-11-30 19:02:32 +0100 | [diff] [blame] | 6116 | t->do_log(t); |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 6117 | t->logs.bytes_out = 0; |
| 6118 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6119 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 6120 | /* Note: we must not try to cheat by jumping directly to DATA, |
| 6121 | * otherwise we would not let the client side wake up. |
| 6122 | */ |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6123 | |
Willy Tarreau | e3fa6e5 | 2010-01-04 22:57:43 +0100 | [diff] [blame] | 6124 | return 1; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 6125 | } |
Willy Tarreau | e3fa6e5 | 2010-01-04 22:57:43 +0100 | [diff] [blame] | 6126 | return 1; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 6127 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6128 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 6129 | /* This function is an analyser which forwards response body (including chunk |
| 6130 | * sizes if any). It is called as soon as we must forward, even if we forward |
| 6131 | * zero byte. The only situation where it must not be called is when we're in |
| 6132 | * tunnel mode and we want to forward till the close. It's used both to forward |
| 6133 | * remaining data and to resync after end of body. It expects the msg_state to |
| 6134 | * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to |
| 6135 | * 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] | 6136 | * 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] | 6137 | * bytes of pending data + the headers if not already done (between sol and sov). |
| 6138 | * 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] | 6139 | */ |
Willy Tarreau | 7421efb | 2012-07-02 15:11:27 +0200 | [diff] [blame] | 6140 | 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] | 6141 | { |
| 6142 | struct http_txn *txn = &s->txn; |
| 6143 | struct http_msg *msg = &s->txn.rsp; |
Willy Tarreau | ea95316 | 2012-05-18 23:41:28 +0200 | [diff] [blame] | 6144 | unsigned int bytes; |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 6145 | static struct buffer *tmpbuf = NULL; |
| 6146 | int compressing = 0; |
William Lallemand | bf3ae61 | 2012-11-19 12:35:37 +0100 | [diff] [blame] | 6147 | int consumed_data = 0; |
Willy Tarreau | d655ffe | 2013-04-02 01:48:58 +0200 | [diff] [blame] | 6148 | int ret; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 6149 | |
Willy Tarreau | f5c8bd6 | 2010-01-04 07:10:34 +0100 | [diff] [blame] | 6150 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) |
| 6151 | return 0; |
| 6152 | |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 6153 | 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] | 6154 | ((res->flags & CF_SHUTW) && (res->to_forward || res->buf->o)) || |
Willy Tarreau | 6c2cbe1 | 2010-01-03 17:07:49 +0100 | [diff] [blame] | 6155 | !s->req->analysers) { |
Willy Tarreau | 4fe4190 | 2010-06-07 22:27:41 +0200 | [diff] [blame] | 6156 | /* Output closed while we were sending data. We must abort and |
| 6157 | * wake the other side up. |
| 6158 | */ |
| 6159 | msg->msg_state = HTTP_MSG_ERROR; |
| 6160 | http_resync_states(s); |
Willy Tarreau | 082b01c | 2010-01-02 23:58:04 +0100 | [diff] [blame] | 6161 | return 1; |
| 6162 | } |
| 6163 | |
Willy Tarreau | 4fe4190 | 2010-06-07 22:27:41 +0200 | [diff] [blame] | 6164 | /* in most states, we should abort in case of early close */ |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 6165 | channel_auto_close(res); |
Willy Tarreau | b608feb | 2010-01-02 22:47:18 +0100 | [diff] [blame] | 6166 | |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 6167 | /* this is the first time we need the compression buffer */ |
| 6168 | if (s->comp_algo != NULL && tmpbuf == NULL) { |
| 6169 | if ((tmpbuf = pool_alloc2(pool2_buffer)) == NULL) |
| 6170 | goto aborted_xfer; /* no memory */ |
| 6171 | } |
| 6172 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 6173 | if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) { |
Willy Tarreau | fa4a03c | 2012-03-09 21:28:54 +0100 | [diff] [blame] | 6174 | /* 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] | 6175 | * rep->buf.p still points to the beginning of the message and msg->sol |
| 6176 | * is still null. We forward the headers, we don't need them. |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 6177 | */ |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 6178 | channel_forward(res, msg->sov); |
| 6179 | msg->next = 0; |
| 6180 | msg->sov = 0; |
Willy Tarreau | a458b67 | 2012-03-05 11:17:50 +0100 | [diff] [blame] | 6181 | |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 6182 | if (msg->flags & HTTP_MSGF_TE_CHNK) |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 6183 | msg->msg_state = HTTP_MSG_CHUNK_SIZE; |
Willy Tarreau | 54d23df | 2012-10-25 19:04:45 +0200 | [diff] [blame] | 6184 | else |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 6185 | msg->msg_state = HTTP_MSG_DATA; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 6186 | } |
| 6187 | |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 6188 | if (s->comp_algo != NULL) { |
Willy Tarreau | d655ffe | 2013-04-02 01:48:58 +0200 | [diff] [blame] | 6189 | ret = http_compression_buffer_init(s, res->buf, tmpbuf); /* init a buffer with headers */ |
Willy Tarreau | 4afd70a | 2014-01-25 02:26:39 +0100 | [diff] [blame] | 6190 | if (ret < 0) { |
| 6191 | res->flags |= CF_WAKE_WRITE; |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 6192 | goto missing_data; /* not enough spaces in buffers */ |
Willy Tarreau | 4afd70a | 2014-01-25 02:26:39 +0100 | [diff] [blame] | 6193 | } |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 6194 | compressing = 1; |
| 6195 | } |
| 6196 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 6197 | while (1) { |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 6198 | http_silent_debug(__LINE__, s); |
Willy Tarreau | ea95316 | 2012-05-18 23:41:28 +0200 | [diff] [blame] | 6199 | /* we may have some data pending between sol and sov */ |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 6200 | if (s->comp_algo == NULL) { |
| 6201 | bytes = msg->sov - msg->sol; |
| 6202 | if (msg->chunk_len || bytes) { |
| 6203 | msg->sol = msg->sov; |
| 6204 | msg->next -= bytes; /* will be forwarded */ |
| 6205 | msg->chunk_len += bytes; |
| 6206 | msg->chunk_len -= channel_forward(res, msg->chunk_len); |
| 6207 | } |
Willy Tarreau | 638cd02 | 2010-01-03 07:42:04 +0100 | [diff] [blame] | 6208 | } |
| 6209 | |
Willy Tarreau | d655ffe | 2013-04-02 01:48:58 +0200 | [diff] [blame] | 6210 | switch (msg->msg_state - HTTP_MSG_DATA) { |
| 6211 | case HTTP_MSG_DATA - HTTP_MSG_DATA: /* must still forward */ |
William Lallemand | bf3ae61 | 2012-11-19 12:35:37 +0100 | [diff] [blame] | 6212 | if (compressing) { |
| 6213 | consumed_data += ret = http_compression_buffer_add_data(s, res->buf, tmpbuf); |
| 6214 | if (ret < 0) |
| 6215 | goto aborted_xfer; |
| 6216 | } |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 6217 | |
Willy Tarreau | 4afd70a | 2014-01-25 02:26:39 +0100 | [diff] [blame] | 6218 | if (res->to_forward || msg->chunk_len) { |
| 6219 | res->flags |= CF_WAKE_WRITE; |
Willy Tarreau | f5c8bd6 | 2010-01-04 07:10:34 +0100 | [diff] [blame] | 6220 | goto missing_data; |
Willy Tarreau | 4afd70a | 2014-01-25 02:26:39 +0100 | [diff] [blame] | 6221 | } |
Willy Tarreau | caabe41 | 2010-01-03 23:08:28 +0100 | [diff] [blame] | 6222 | |
| 6223 | /* nothing left to forward */ |
William Lallemand | bf3ae61 | 2012-11-19 12:35:37 +0100 | [diff] [blame] | 6224 | if (msg->flags & HTTP_MSGF_TE_CHNK) { |
Willy Tarreau | 54d23df | 2012-10-25 19:04:45 +0200 | [diff] [blame] | 6225 | msg->msg_state = HTTP_MSG_CHUNK_CRLF; |
William Lallemand | bf3ae61 | 2012-11-19 12:35:37 +0100 | [diff] [blame] | 6226 | } else { |
Willy Tarreau | caabe41 | 2010-01-03 23:08:28 +0100 | [diff] [blame] | 6227 | msg->msg_state = HTTP_MSG_DONE; |
William Lallemand | bf3ae61 | 2012-11-19 12:35:37 +0100 | [diff] [blame] | 6228 | if (compressing && consumed_data) { |
| 6229 | http_compression_buffer_end(s, &res->buf, &tmpbuf, 1); |
| 6230 | compressing = 0; |
| 6231 | } |
Willy Tarreau | d655ffe | 2013-04-02 01:48:58 +0200 | [diff] [blame] | 6232 | break; |
William Lallemand | bf3ae61 | 2012-11-19 12:35:37 +0100 | [diff] [blame] | 6233 | } |
Willy Tarreau | d655ffe | 2013-04-02 01:48:58 +0200 | [diff] [blame] | 6234 | /* fall through for HTTP_MSG_CHUNK_CRLF */ |
| 6235 | |
| 6236 | case HTTP_MSG_CHUNK_CRLF - HTTP_MSG_DATA: |
| 6237 | /* we want the CRLF after the data */ |
| 6238 | |
| 6239 | ret = http_skip_chunk_crlf(msg); |
| 6240 | if (ret == 0) |
| 6241 | goto missing_data; |
| 6242 | else if (ret < 0) { |
| 6243 | if (msg->err_pos >= 0) |
| 6244 | http_capture_bad_message(&s->be->invalid_rep, s, msg, HTTP_MSG_CHUNK_CRLF, s->fe); |
| 6245 | goto return_bad_res; |
| 6246 | } |
| 6247 | /* skipping data in buffer for compression */ |
| 6248 | if (compressing) { |
| 6249 | b_adv(res->buf, msg->next); |
| 6250 | msg->next = 0; |
| 6251 | msg->sov = 0; |
| 6252 | msg->sol = 0; |
| 6253 | } |
| 6254 | /* we're in MSG_CHUNK_SIZE now, fall through */ |
| 6255 | |
| 6256 | case HTTP_MSG_CHUNK_SIZE - HTTP_MSG_DATA: |
Willy Tarreau | 124d991 | 2011-03-01 20:30:48 +0100 | [diff] [blame] | 6257 | /* read the chunk size and assign it to ->chunk_len, then |
Willy Tarreau | a458b67 | 2012-03-05 11:17:50 +0100 | [diff] [blame] | 6258 | * set ->sov and ->next to point to the body and switch to DATA or |
| 6259 | * TRAILERS state. |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 6260 | */ |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 6261 | |
Willy Tarreau | d655ffe | 2013-04-02 01:48:58 +0200 | [diff] [blame] | 6262 | ret = http_parse_chunk_size(msg); |
Willy Tarreau | 54d23df | 2012-10-25 19:04:45 +0200 | [diff] [blame] | 6263 | if (ret == 0) |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 6264 | goto missing_data; |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 6265 | else if (ret < 0) { |
| 6266 | if (msg->err_pos >= 0) |
Willy Tarreau | 8a0cef2 | 2012-03-09 13:39:23 +0100 | [diff] [blame] | 6267 | 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] | 6268 | goto return_bad_res; |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 6269 | } |
William Lallemand | bf3ae61 | 2012-11-19 12:35:37 +0100 | [diff] [blame] | 6270 | if (compressing) { |
| 6271 | if (likely(msg->chunk_len > 0)) { |
| 6272 | /* skipping data if we are in compression mode */ |
| 6273 | b_adv(res->buf, msg->next); |
| 6274 | msg->next = 0; |
| 6275 | msg->sov = 0; |
| 6276 | msg->sol = 0; |
| 6277 | } else { |
| 6278 | if (consumed_data) { |
| 6279 | http_compression_buffer_end(s, &res->buf, &tmpbuf, 1); |
| 6280 | compressing = 0; |
| 6281 | } |
| 6282 | } |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 6283 | } |
Willy Tarreau | 0161d62 | 2013-04-02 01:26:55 +0200 | [diff] [blame] | 6284 | /* otherwise we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state */ |
Willy Tarreau | d655ffe | 2013-04-02 01:48:58 +0200 | [diff] [blame] | 6285 | break; |
Willy Tarreau | 5523b32 | 2009-12-29 12:05:52 +0100 | [diff] [blame] | 6286 | |
Willy Tarreau | d655ffe | 2013-04-02 01:48:58 +0200 | [diff] [blame] | 6287 | case HTTP_MSG_TRAILERS - HTTP_MSG_DATA: |
| 6288 | ret = http_forward_trailers(msg); |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 6289 | if (ret == 0) |
| 6290 | goto missing_data; |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 6291 | else if (ret < 0) { |
| 6292 | if (msg->err_pos >= 0) |
Willy Tarreau | 8a0cef2 | 2012-03-09 13:39:23 +0100 | [diff] [blame] | 6293 | 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] | 6294 | goto return_bad_res; |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 6295 | } |
William Lallemand | 00bf1de | 2012-11-22 17:55:14 +0100 | [diff] [blame] | 6296 | if (s->comp_algo != NULL) { |
| 6297 | /* forwarding trailers */ |
| 6298 | channel_forward(res, msg->next); |
| 6299 | msg->next = 0; |
| 6300 | } |
Willy Tarreau | 2d43e18 | 2013-04-03 00:22:25 +0200 | [diff] [blame] | 6301 | /* we're in HTTP_MSG_DONE now, but we might still have |
| 6302 | * some data pending, so let's loop over once. |
| 6303 | */ |
| 6304 | break; |
Willy Tarreau | d655ffe | 2013-04-02 01:48:58 +0200 | [diff] [blame] | 6305 | |
| 6306 | default: |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 6307 | /* other states, DONE...TUNNEL */ |
Willy Tarreau | d655ffe | 2013-04-02 01:48:58 +0200 | [diff] [blame] | 6308 | |
| 6309 | ret = msg->msg_state; |
Willy Tarreau | 4fe4190 | 2010-06-07 22:27:41 +0200 | [diff] [blame] | 6310 | /* for keep-alive we don't want to forward closes on DONE */ |
Willy Tarreau | 92aa1fa | 2010-08-28 18:57:20 +0200 | [diff] [blame] | 6311 | if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL || |
| 6312 | (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 6313 | channel_dont_close(res); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 6314 | if (http_resync_states(s)) { |
| 6315 | http_silent_debug(__LINE__, s); |
| 6316 | /* some state changes occurred, maybe the analyser |
| 6317 | * was disabled too. |
Willy Tarreau | 5523b32 | 2009-12-29 12:05:52 +0100 | [diff] [blame] | 6318 | */ |
Willy Tarreau | 3fe693b | 2010-12-12 12:50:05 +0100 | [diff] [blame] | 6319 | if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) { |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 6320 | if (res->flags & CF_SHUTW) { |
Willy Tarreau | 3fe693b | 2010-12-12 12:50:05 +0100 | [diff] [blame] | 6321 | /* response errors are most likely due to |
| 6322 | * the client aborting the transfer. |
| 6323 | */ |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 6324 | goto aborted_xfer; |
Willy Tarreau | 3fe693b | 2010-12-12 12:50:05 +0100 | [diff] [blame] | 6325 | } |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 6326 | if (msg->err_pos >= 0) |
Willy Tarreau | d655ffe | 2013-04-02 01:48:58 +0200 | [diff] [blame] | 6327 | 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] | 6328 | goto return_bad_res; |
Willy Tarreau | 3fe693b | 2010-12-12 12:50:05 +0100 | [diff] [blame] | 6329 | } |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 6330 | return 1; |
Willy Tarreau | 5523b32 | 2009-12-29 12:05:52 +0100 | [diff] [blame] | 6331 | } |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 6332 | return 0; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 6333 | } |
| 6334 | } |
| 6335 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 6336 | missing_data: |
William Lallemand | bf3ae61 | 2012-11-19 12:35:37 +0100 | [diff] [blame] | 6337 | if (compressing && consumed_data) { |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 6338 | http_compression_buffer_end(s, &res->buf, &tmpbuf, 0); |
| 6339 | compressing = 0; |
| 6340 | } |
Willy Tarreau | f003d37 | 2012-11-26 13:35:37 +0100 | [diff] [blame] | 6341 | |
| 6342 | if (res->flags & CF_SHUTW) |
| 6343 | goto aborted_xfer; |
| 6344 | |
| 6345 | /* stop waiting for data if the input is closed before the end. If the |
| 6346 | * client side was already closed, it means that the client has aborted, |
| 6347 | * so we don't want to count this as a server abort. Otherwise it's a |
| 6348 | * server abort. |
| 6349 | */ |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 6350 | if (res->flags & CF_SHUTR) { |
Willy Tarreau | f003d37 | 2012-11-26 13:35:37 +0100 | [diff] [blame] | 6351 | if ((res->flags & CF_SHUTW_NOW) || (s->req->flags & CF_SHUTR)) |
| 6352 | goto aborted_xfer; |
Willy Tarreau | 40dba09 | 2010-03-04 18:14:51 +0100 | [diff] [blame] | 6353 | if (!(s->flags & SN_ERR_MASK)) |
| 6354 | s->flags |= SN_ERR_SRVCL; |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 6355 | s->be->be_counters.srv_aborts++; |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 6356 | if (objt_server(s->target)) |
| 6357 | objt_server(s->target)->counters.srv_aborts++; |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 6358 | goto return_bad_res_stats_ok; |
Willy Tarreau | 40dba09 | 2010-03-04 18:14:51 +0100 | [diff] [blame] | 6359 | } |
Willy Tarreau | f5c8bd6 | 2010-01-04 07:10:34 +0100 | [diff] [blame] | 6360 | |
Willy Tarreau | 40dba09 | 2010-03-04 18:14:51 +0100 | [diff] [blame] | 6361 | /* 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] | 6362 | if (!s->req->analysers) |
| 6363 | goto return_bad_res; |
| 6364 | |
Willy Tarreau | ea95316 | 2012-05-18 23:41:28 +0200 | [diff] [blame] | 6365 | /* forward any data pending between sol and sov */ |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 6366 | if (s->comp_algo == NULL) { |
| 6367 | bytes = msg->sov - msg->sol; |
| 6368 | if (msg->chunk_len || bytes) { |
| 6369 | msg->sol = msg->sov; |
| 6370 | msg->next -= bytes; /* will be forwarded */ |
| 6371 | msg->chunk_len += bytes; |
| 6372 | msg->chunk_len -= channel_forward(res, msg->chunk_len); |
| 6373 | } |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 6374 | } |
| 6375 | |
Willy Tarreau | 92aa1fa | 2010-08-28 18:57:20 +0200 | [diff] [blame] | 6376 | /* 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] | 6377 | * 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] | 6378 | * Similarly, with keep-alive on the client side, we don't want to forward a |
| 6379 | * close. |
| 6380 | */ |
Willy Tarreau | 08b4d79 | 2012-10-27 01:36:34 +0200 | [diff] [blame] | 6381 | if ((msg->flags & HTTP_MSGF_TE_CHNK) || s->comp_algo || |
Willy Tarreau | 92aa1fa | 2010-08-28 18:57:20 +0200 | [diff] [blame] | 6382 | (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL || |
| 6383 | (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 6384 | channel_dont_close(res); |
Willy Tarreau | 92aa1fa | 2010-08-28 18:57:20 +0200 | [diff] [blame] | 6385 | |
Willy Tarreau | 5c62092 | 2011-05-11 19:56:11 +0200 | [diff] [blame] | 6386 | /* 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] | 6387 | * 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] | 6388 | * 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] | 6389 | * modes are already handled by the stream sock layer. We must not do |
| 6390 | * this in content-length mode because it could present the MSG_MORE |
| 6391 | * flag with the last block of forwarded data, which would cause an |
| 6392 | * additional delay to be observed by the receiver. |
Willy Tarreau | 5c62092 | 2011-05-11 19:56:11 +0200 | [diff] [blame] | 6393 | */ |
Willy Tarreau | 08b4d79 | 2012-10-27 01:36:34 +0200 | [diff] [blame] | 6394 | if ((msg->flags & HTTP_MSGF_TE_CHNK) || s->comp_algo) |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 6395 | res->flags |= CF_EXPECT_MORE; |
Willy Tarreau | 5c62092 | 2011-05-11 19:56:11 +0200 | [diff] [blame] | 6396 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 6397 | /* the session handler will take care of timeouts and errors */ |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 6398 | http_silent_debug(__LINE__, s); |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 6399 | return 0; |
| 6400 | |
Willy Tarreau | 40dba09 | 2010-03-04 18:14:51 +0100 | [diff] [blame] | 6401 | return_bad_res: /* let's centralize all bad responses */ |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 6402 | s->be->be_counters.failed_resp++; |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 6403 | if (objt_server(s->target)) |
| 6404 | objt_server(s->target)->counters.failed_resp++; |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 6405 | |
| 6406 | return_bad_res_stats_ok: |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 6407 | txn->rsp.msg_state = HTTP_MSG_ERROR; |
Willy Tarreau | 148d099 | 2010-01-10 10:21:21 +0100 | [diff] [blame] | 6408 | /* don't send any error message as we're in the body */ |
| 6409 | stream_int_retnclose(res->cons, NULL); |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 6410 | res->analysers = 0; |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 6411 | 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] | 6412 | if (objt_server(s->target)) |
| 6413 | health_adjust(objt_server(s->target), HANA_STATUS_HTTP_HDRRSP); |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 6414 | |
| 6415 | if (!(s->flags & SN_ERR_MASK)) |
| 6416 | s->flags |= SN_ERR_PRXCOND; |
| 6417 | if (!(s->flags & SN_FINST_MASK)) |
Willy Tarreau | 40dba09 | 2010-03-04 18:14:51 +0100 | [diff] [blame] | 6418 | s->flags |= SN_FINST_D; |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 6419 | return 0; |
| 6420 | |
| 6421 | aborted_xfer: |
| 6422 | txn->rsp.msg_state = HTTP_MSG_ERROR; |
| 6423 | /* don't send any error message as we're in the body */ |
| 6424 | stream_int_retnclose(res->cons, NULL); |
| 6425 | res->analysers = 0; |
| 6426 | s->req->analysers = 0; /* we're in data phase, we want to abort both directions */ |
| 6427 | |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 6428 | s->fe->fe_counters.cli_aborts++; |
| 6429 | s->be->be_counters.cli_aborts++; |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 6430 | if (objt_server(s->target)) |
| 6431 | objt_server(s->target)->counters.cli_aborts++; |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 6432 | |
| 6433 | if (!(s->flags & SN_ERR_MASK)) |
| 6434 | s->flags |= SN_ERR_CLICL; |
| 6435 | if (!(s->flags & SN_FINST_MASK)) |
| 6436 | s->flags |= SN_FINST_D; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 6437 | return 0; |
| 6438 | } |
| 6439 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6440 | /* Iterate the same filter through all request headers. |
| 6441 | * Returns 1 if this filter can be stopped upon return, otherwise 0. |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6442 | * Since it can manage the switch to another backend, it updates the per-proxy |
| 6443 | * DENY stats. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6444 | */ |
Willy Tarreau | 7421efb | 2012-07-02 15:11:27 +0200 | [diff] [blame] | 6445 | 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] | 6446 | { |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6447 | char term; |
| 6448 | char *cur_ptr, *cur_end, *cur_next; |
| 6449 | int cur_idx, old_idx, last_hdr; |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 6450 | struct http_txn *txn = &t->txn; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6451 | struct hdr_idx_elem *cur_hdr; |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 6452 | int delta; |
Willy Tarreau | 0f7562b | 2007-01-07 15:46:13 +0100 | [diff] [blame] | 6453 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6454 | last_hdr = 0; |
| 6455 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 6456 | cur_next = req->buf->p + hdr_idx_first_pos(&txn->hdr_idx); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6457 | old_idx = 0; |
| 6458 | |
| 6459 | while (!last_hdr) { |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 6460 | if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT))) |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6461 | return 1; |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 6462 | else if (unlikely(txn->flags & TX_CLALLOW) && |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6463 | (exp->action == ACT_ALLOW || |
| 6464 | exp->action == ACT_DENY || |
| 6465 | exp->action == ACT_TARPIT)) |
| 6466 | return 0; |
| 6467 | |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 6468 | cur_idx = txn->hdr_idx.v[old_idx].next; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6469 | if (!cur_idx) |
| 6470 | break; |
| 6471 | |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 6472 | cur_hdr = &txn->hdr_idx.v[cur_idx]; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6473 | cur_ptr = cur_next; |
| 6474 | cur_end = cur_ptr + cur_hdr->len; |
| 6475 | cur_next = cur_end + cur_hdr->cr + 1; |
| 6476 | |
| 6477 | /* Now we have one header between cur_ptr and cur_end, |
| 6478 | * and the next header starts at cur_next. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6479 | */ |
| 6480 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6481 | /* The annoying part is that pattern matching needs |
| 6482 | * that we modify the contents to null-terminate all |
| 6483 | * strings before testing them. |
| 6484 | */ |
| 6485 | |
| 6486 | term = *cur_end; |
| 6487 | *cur_end = '\0'; |
| 6488 | |
| 6489 | if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) { |
| 6490 | switch (exp->action) { |
| 6491 | case ACT_SETBE: |
| 6492 | /* It is not possible to jump a second time. |
| 6493 | * FIXME: should we return an HTTP/500 here so that |
| 6494 | * the admin knows there's a problem ? |
| 6495 | */ |
| 6496 | if (t->be != t->fe) |
| 6497 | break; |
| 6498 | |
| 6499 | /* Swithing Proxy */ |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 6500 | session_set_backend(t, (struct proxy *)exp->replace); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6501 | last_hdr = 1; |
| 6502 | break; |
| 6503 | |
| 6504 | case ACT_ALLOW: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 6505 | txn->flags |= TX_CLALLOW; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6506 | last_hdr = 1; |
| 6507 | break; |
| 6508 | |
| 6509 | case ACT_DENY: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 6510 | txn->flags |= TX_CLDENY; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6511 | last_hdr = 1; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6512 | break; |
| 6513 | |
| 6514 | case ACT_TARPIT: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 6515 | txn->flags |= TX_CLTARPIT; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6516 | last_hdr = 1; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6517 | break; |
| 6518 | |
| 6519 | case ACT_REPLACE: |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 6520 | trash.len = exp_replace(trash.str, cur_ptr, exp->replace, pmatch); |
| 6521 | 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] | 6522 | /* FIXME: if the user adds a newline in the replacement, the |
| 6523 | * index will not be recalculated for now, and the new line |
| 6524 | * will not be counted as a new header. |
| 6525 | */ |
| 6526 | |
| 6527 | cur_end += delta; |
| 6528 | cur_next += delta; |
| 6529 | cur_hdr->len += delta; |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 6530 | http_msg_move_end(&txn->req, delta); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6531 | break; |
| 6532 | |
| 6533 | case ACT_REMOVE: |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 6534 | delta = buffer_replace2(req->buf, cur_ptr, cur_next, NULL, 0); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6535 | cur_next += delta; |
| 6536 | |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 6537 | http_msg_move_end(&txn->req, delta); |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 6538 | txn->hdr_idx.v[old_idx].next = cur_hdr->next; |
| 6539 | txn->hdr_idx.used--; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6540 | cur_hdr->len = 0; |
| 6541 | cur_end = NULL; /* null-term has been rewritten */ |
Willy Tarreau | 26db59e | 2010-11-28 06:57:24 +0100 | [diff] [blame] | 6542 | cur_idx = old_idx; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6543 | break; |
| 6544 | |
| 6545 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6546 | } |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6547 | if (cur_end) |
| 6548 | *cur_end = term; /* restore the string terminator */ |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6549 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6550 | /* keep the link from this header to next one in case of later |
| 6551 | * removal of next header. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6552 | */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6553 | old_idx = cur_idx; |
| 6554 | } |
| 6555 | return 0; |
| 6556 | } |
| 6557 | |
| 6558 | |
| 6559 | /* Apply the filter to the request line. |
| 6560 | * Returns 0 if nothing has been done, 1 if the filter has been applied, |
| 6561 | * or -1 if a replacement resulted in an invalid request line. |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6562 | * Since it can manage the switch to another backend, it updates the per-proxy |
| 6563 | * DENY stats. |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6564 | */ |
Willy Tarreau | 7421efb | 2012-07-02 15:11:27 +0200 | [diff] [blame] | 6565 | 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] | 6566 | { |
| 6567 | char term; |
| 6568 | char *cur_ptr, *cur_end; |
| 6569 | int done; |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 6570 | struct http_txn *txn = &t->txn; |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 6571 | int delta; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6572 | |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 6573 | if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT))) |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6574 | return 1; |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 6575 | else if (unlikely(txn->flags & TX_CLALLOW) && |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6576 | (exp->action == ACT_ALLOW || |
| 6577 | exp->action == ACT_DENY || |
| 6578 | exp->action == ACT_TARPIT)) |
| 6579 | return 0; |
| 6580 | else if (exp->action == ACT_REMOVE) |
| 6581 | return 0; |
| 6582 | |
| 6583 | done = 0; |
| 6584 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 6585 | cur_ptr = req->buf->p; |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 6586 | cur_end = cur_ptr + txn->req.sl.rq.l; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6587 | |
| 6588 | /* Now we have the request line between cur_ptr and cur_end */ |
| 6589 | |
| 6590 | /* The annoying part is that pattern matching needs |
| 6591 | * that we modify the contents to null-terminate all |
| 6592 | * strings before testing them. |
| 6593 | */ |
| 6594 | |
| 6595 | term = *cur_end; |
| 6596 | *cur_end = '\0'; |
| 6597 | |
| 6598 | if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) { |
| 6599 | switch (exp->action) { |
| 6600 | case ACT_SETBE: |
| 6601 | /* It is not possible to jump a second time. |
| 6602 | * FIXME: should we return an HTTP/500 here so that |
| 6603 | * the admin knows there's a problem ? |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6604 | */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6605 | if (t->be != t->fe) |
| 6606 | break; |
| 6607 | |
| 6608 | /* Swithing Proxy */ |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 6609 | session_set_backend(t, (struct proxy *)exp->replace); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6610 | done = 1; |
| 6611 | break; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6612 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6613 | case ACT_ALLOW: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 6614 | txn->flags |= TX_CLALLOW; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6615 | done = 1; |
| 6616 | break; |
Willy Tarreau | a496b60 | 2006-12-17 23:15:24 +0100 | [diff] [blame] | 6617 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6618 | case ACT_DENY: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 6619 | txn->flags |= TX_CLDENY; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6620 | done = 1; |
| 6621 | break; |
Willy Tarreau | a496b60 | 2006-12-17 23:15:24 +0100 | [diff] [blame] | 6622 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6623 | case ACT_TARPIT: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 6624 | txn->flags |= TX_CLTARPIT; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6625 | done = 1; |
| 6626 | break; |
Willy Tarreau | a496b60 | 2006-12-17 23:15:24 +0100 | [diff] [blame] | 6627 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6628 | case ACT_REPLACE: |
| 6629 | *cur_end = term; /* restore the string terminator */ |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 6630 | trash.len = exp_replace(trash.str, cur_ptr, exp->replace, pmatch); |
| 6631 | 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] | 6632 | /* FIXME: if the user adds a newline in the replacement, the |
| 6633 | * index will not be recalculated for now, and the new line |
| 6634 | * will not be counted as a new header. |
| 6635 | */ |
Willy Tarreau | a496b60 | 2006-12-17 23:15:24 +0100 | [diff] [blame] | 6636 | |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 6637 | http_msg_move_end(&txn->req, delta); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6638 | cur_end += delta; |
Willy Tarreau | 69d8c5d | 2012-05-08 09:44:41 +0200 | [diff] [blame] | 6639 | cur_end = (char *)http_parse_reqline(&txn->req, |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6640 | HTTP_MSG_RQMETH, |
| 6641 | cur_ptr, cur_end + 1, |
| 6642 | NULL, NULL); |
| 6643 | if (unlikely(!cur_end)) |
| 6644 | return -1; |
Willy Tarreau | a496b60 | 2006-12-17 23:15:24 +0100 | [diff] [blame] | 6645 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6646 | /* we have a full request and we know that we have either a CR |
| 6647 | * or an LF at <ptr>. |
| 6648 | */ |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 6649 | txn->meth = find_http_meth(cur_ptr, txn->req.sl.rq.m_l); |
| 6650 | 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] | 6651 | /* there is no point trying this regex on headers */ |
| 6652 | return 1; |
| 6653 | } |
| 6654 | } |
| 6655 | *cur_end = term; /* restore the string terminator */ |
| 6656 | return done; |
| 6657 | } |
Willy Tarreau | 97de624 | 2006-12-27 17:18:38 +0100 | [diff] [blame] | 6658 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6659 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6660 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6661 | /* |
Willy Tarreau | 6c123b1 | 2010-01-28 20:22:06 +0100 | [diff] [blame] | 6662 | * 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] | 6663 | * 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] | 6664 | * unparsable request. Since it can manage the switch to another backend, it |
| 6665 | * updates the per-proxy DENY stats. |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6666 | */ |
Willy Tarreau | 7421efb | 2012-07-02 15:11:27 +0200 | [diff] [blame] | 6667 | 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] | 6668 | { |
Willy Tarreau | 6c123b1 | 2010-01-28 20:22:06 +0100 | [diff] [blame] | 6669 | struct http_txn *txn = &s->txn; |
| 6670 | struct hdr_exp *exp; |
| 6671 | |
| 6672 | for (exp = px->req_exp; exp; exp = exp->next) { |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6673 | int ret; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6674 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6675 | /* |
| 6676 | * The interleaving of transformations and verdicts |
| 6677 | * makes it difficult to decide to continue or stop |
| 6678 | * the evaluation. |
| 6679 | */ |
| 6680 | |
Willy Tarreau | 6c123b1 | 2010-01-28 20:22:06 +0100 | [diff] [blame] | 6681 | if (txn->flags & (TX_CLDENY|TX_CLTARPIT)) |
| 6682 | break; |
| 6683 | |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 6684 | if ((txn->flags & TX_CLALLOW) && |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6685 | (exp->action == ACT_ALLOW || exp->action == ACT_DENY || |
Willy Tarreau | 6c123b1 | 2010-01-28 20:22:06 +0100 | [diff] [blame] | 6686 | exp->action == ACT_TARPIT || exp->action == ACT_PASS)) |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6687 | continue; |
Willy Tarreau | 6c123b1 | 2010-01-28 20:22:06 +0100 | [diff] [blame] | 6688 | |
| 6689 | /* if this filter had a condition, evaluate it now and skip to |
| 6690 | * next filter if the condition does not match. |
| 6691 | */ |
| 6692 | if (exp->cond) { |
Willy Tarreau | 32a6f2e | 2012-04-25 10:13:36 +0200 | [diff] [blame] | 6693 | 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] | 6694 | ret = acl_pass(ret); |
| 6695 | if (((struct acl_cond *)exp->cond)->pol == ACL_COND_UNLESS) |
| 6696 | ret = !ret; |
| 6697 | |
| 6698 | if (!ret) |
| 6699 | continue; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6700 | } |
| 6701 | |
| 6702 | /* Apply the filter to the request line. */ |
Willy Tarreau | 6c123b1 | 2010-01-28 20:22:06 +0100 | [diff] [blame] | 6703 | ret = apply_filter_to_req_line(s, req, exp); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6704 | if (unlikely(ret < 0)) |
| 6705 | return -1; |
| 6706 | |
| 6707 | if (likely(ret == 0)) { |
| 6708 | /* The filter did not match the request, it can be |
| 6709 | * iterated through all headers. |
| 6710 | */ |
Willy Tarreau | 6c123b1 | 2010-01-28 20:22:06 +0100 | [diff] [blame] | 6711 | apply_filter_to_req_headers(s, req, exp); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6712 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6713 | } |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6714 | return 0; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6715 | } |
| 6716 | |
| 6717 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6718 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6719 | /* |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 6720 | * Try to retrieve the server associated to the appsession. |
| 6721 | * If the server is found, it's assigned to the session. |
| 6722 | */ |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 6723 | 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] | 6724 | struct http_txn *txn = &t->txn; |
| 6725 | appsess *asession = NULL; |
| 6726 | char *sessid_temp = NULL; |
| 6727 | |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 6728 | if (len > t->be->appsession_len) { |
| 6729 | len = t->be->appsession_len; |
| 6730 | } |
| 6731 | |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 6732 | if (t->be->options2 & PR_O2_AS_REQL) { |
| 6733 | /* 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] | 6734 | if (txn->sessid != NULL) { |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 6735 | /* 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] | 6736 | pool_free2(apools.sessid, txn->sessid); |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 6737 | } |
| 6738 | |
Willy Tarreau | a3377ee | 2010-01-10 10:49:11 +0100 | [diff] [blame] | 6739 | if ((txn->sessid = pool_alloc2(apools.sessid)) == NULL) { |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 6740 | Alert("Not enough memory process_cli():asession->sessid:malloc().\n"); |
| 6741 | send_log(t->be, LOG_ALERT, "Not enough memory process_cli():asession->sessid:malloc().\n"); |
| 6742 | return; |
| 6743 | } |
| 6744 | |
Willy Tarreau | a3377ee | 2010-01-10 10:49:11 +0100 | [diff] [blame] | 6745 | memcpy(txn->sessid, buf, len); |
| 6746 | txn->sessid[len] = 0; |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 6747 | } |
| 6748 | |
| 6749 | if ((sessid_temp = pool_alloc2(apools.sessid)) == NULL) { |
| 6750 | Alert("Not enough memory process_cli():asession->sessid:malloc().\n"); |
| 6751 | send_log(t->be, LOG_ALERT, "Not enough memory process_cli():asession->sessid:malloc().\n"); |
| 6752 | return; |
| 6753 | } |
| 6754 | |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 6755 | memcpy(sessid_temp, buf, len); |
| 6756 | sessid_temp[len] = 0; |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 6757 | |
| 6758 | asession = appsession_hash_lookup(&(t->be->htbl_proxy), sessid_temp); |
| 6759 | /* free previously allocated memory */ |
| 6760 | pool_free2(apools.sessid, sessid_temp); |
| 6761 | |
| 6762 | if (asession != NULL) { |
| 6763 | asession->expire = tick_add_ifset(now_ms, t->be->timeout.appsession); |
| 6764 | if (!(t->be->options2 & PR_O2_AS_REQL)) |
| 6765 | asession->request_count++; |
| 6766 | |
| 6767 | if (asession->serverid != NULL) { |
| 6768 | struct server *srv = t->be->srv; |
Cyril Bonté | 47fdd8e | 2010-04-25 00:00:51 +0200 | [diff] [blame] | 6769 | |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 6770 | while (srv) { |
| 6771 | if (strcmp(srv->id, asession->serverid) == 0) { |
Willy Tarreau | 4de9149 | 2010-01-22 19:10:05 +0100 | [diff] [blame] | 6772 | if ((srv->state & SRV_RUNNING) || |
| 6773 | (t->be->options & PR_O_PERSIST) || |
| 6774 | (t->flags & SN_FORCE_PRST)) { |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 6775 | /* we found the server and it's usable */ |
| 6776 | txn->flags &= ~TX_CK_MASK; |
Willy Tarreau | 2a6d88d | 2010-01-24 13:10:43 +0100 | [diff] [blame] | 6777 | txn->flags |= (srv->state & SRV_RUNNING) ? TX_CK_VALID : TX_CK_DOWN; |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 6778 | t->flags |= SN_DIRECT | SN_ASSIGNED; |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 6779 | t->target = &srv->obj_type; |
Willy Tarreau | 664beb8 | 2011-03-10 11:38:29 +0100 | [diff] [blame] | 6780 | |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 6781 | break; |
| 6782 | } else { |
| 6783 | txn->flags &= ~TX_CK_MASK; |
| 6784 | txn->flags |= TX_CK_DOWN; |
| 6785 | } |
| 6786 | } |
| 6787 | srv = srv->next; |
| 6788 | } |
| 6789 | } |
| 6790 | } |
| 6791 | } |
| 6792 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6793 | /* Find the end of a cookie value contained between <s> and <e>. It works the |
| 6794 | * same way as with headers above except that the semi-colon also ends a token. |
| 6795 | * See RFC2965 for more information. Note that it requires a valid header to |
| 6796 | * return a valid result. |
| 6797 | */ |
| 6798 | char *find_cookie_value_end(char *s, const char *e) |
| 6799 | { |
| 6800 | int quoted, qdpair; |
| 6801 | |
| 6802 | quoted = qdpair = 0; |
| 6803 | for (; s < e; s++) { |
| 6804 | if (qdpair) qdpair = 0; |
| 6805 | else if (quoted) { |
| 6806 | if (*s == '\\') qdpair = 1; |
| 6807 | else if (*s == '"') quoted = 0; |
| 6808 | } |
| 6809 | else if (*s == '"') quoted = 1; |
| 6810 | else if (*s == ',' || *s == ';') return s; |
| 6811 | } |
| 6812 | return s; |
| 6813 | } |
| 6814 | |
| 6815 | /* Delete a value in a header between delimiters <from> and <next> in buffer |
| 6816 | * <buf>. The number of characters displaced is returned, and the pointer to |
| 6817 | * the first delimiter is updated if required. The function tries as much as |
| 6818 | * possible to respect the following principles : |
| 6819 | * - replace <from> delimiter by the <next> one unless <from> points to a |
| 6820 | * colon, in which case <next> is simply removed |
| 6821 | * - set exactly one space character after the new first delimiter, unless |
| 6822 | * there are not enough characters in the block being moved to do so. |
| 6823 | * - remove unneeded spaces before the previous delimiter and after the new |
| 6824 | * one. |
| 6825 | * |
| 6826 | * It is the caller's responsibility to ensure that : |
| 6827 | * - <from> points to a valid delimiter or the colon ; |
| 6828 | * - <next> points to a valid delimiter or the final CR/LF ; |
| 6829 | * - there are non-space chars before <from> ; |
| 6830 | * - there is a CR/LF at or after <next>. |
| 6831 | */ |
Willy Tarreau | af81935 | 2012-08-27 22:08:00 +0200 | [diff] [blame] | 6832 | int del_hdr_value(struct buffer *buf, char **from, char *next) |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6833 | { |
| 6834 | char *prev = *from; |
| 6835 | |
| 6836 | if (*prev == ':') { |
| 6837 | /* We're removing the first value, preserve the colon and add a |
| 6838 | * space if possible. |
| 6839 | */ |
| 6840 | if (!http_is_crlf[(unsigned char)*next]) |
| 6841 | next++; |
| 6842 | prev++; |
| 6843 | if (prev < next) |
| 6844 | *prev++ = ' '; |
| 6845 | |
| 6846 | while (http_is_spht[(unsigned char)*next]) |
| 6847 | next++; |
| 6848 | } else { |
| 6849 | /* Remove useless spaces before the old delimiter. */ |
| 6850 | while (http_is_spht[(unsigned char)*(prev-1)]) |
| 6851 | prev--; |
| 6852 | *from = prev; |
| 6853 | |
| 6854 | /* copy the delimiter and if possible a space if we're |
| 6855 | * not at the end of the line. |
| 6856 | */ |
| 6857 | if (!http_is_crlf[(unsigned char)*next]) { |
| 6858 | *prev++ = *next++; |
| 6859 | if (prev + 1 < next) |
| 6860 | *prev++ = ' '; |
| 6861 | while (http_is_spht[(unsigned char)*next]) |
| 6862 | next++; |
| 6863 | } |
| 6864 | } |
| 6865 | return buffer_replace2(buf, prev, next, NULL, 0); |
| 6866 | } |
| 6867 | |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 6868 | /* |
Willy Tarreau | 396d2c6 | 2007-11-04 19:30:00 +0100 | [diff] [blame] | 6869 | * 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] | 6870 | * desirable to call it only when needed. This code is quite complex because |
| 6871 | * of the multiple very crappy and ambiguous syntaxes we have to support. it |
| 6872 | * highly recommended not to touch this part without a good reason ! |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6873 | */ |
Willy Tarreau | 7421efb | 2012-07-02 15:11:27 +0200 | [diff] [blame] | 6874 | void manage_client_side_cookies(struct session *t, struct channel *req) |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6875 | { |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 6876 | struct http_txn *txn = &t->txn; |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6877 | int preserve_hdr; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6878 | int cur_idx, old_idx; |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6879 | char *hdr_beg, *hdr_end, *hdr_next, *del_from; |
| 6880 | char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6881 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6882 | /* Iterate through the headers, we start with the start line. */ |
Willy Tarreau | 83969f4 | 2007-01-22 08:55:47 +0100 | [diff] [blame] | 6883 | old_idx = 0; |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 6884 | hdr_next = req->buf->p + hdr_idx_first_pos(&txn->hdr_idx); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6885 | |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 6886 | while ((cur_idx = txn->hdr_idx.v[old_idx].next)) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6887 | struct hdr_idx_elem *cur_hdr; |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 6888 | int val; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6889 | |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 6890 | cur_hdr = &txn->hdr_idx.v[cur_idx]; |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6891 | hdr_beg = hdr_next; |
| 6892 | hdr_end = hdr_beg + cur_hdr->len; |
| 6893 | hdr_next = hdr_end + cur_hdr->cr + 1; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6894 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6895 | /* We have one full header between hdr_beg and hdr_end, and the |
| 6896 | * next header starts at hdr_next. We're only interested in |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6897 | * "Cookie:" headers. |
| 6898 | */ |
| 6899 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6900 | val = http_header_match2(hdr_beg, hdr_end, "Cookie", 6); |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 6901 | if (!val) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6902 | old_idx = cur_idx; |
| 6903 | continue; |
| 6904 | } |
| 6905 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6906 | del_from = NULL; /* nothing to be deleted */ |
| 6907 | preserve_hdr = 0; /* assume we may kill the whole header */ |
| 6908 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6909 | /* Now look for cookies. Conforming to RFC2109, we have to support |
| 6910 | * attributes whose name begin with a '$', and associate them with |
| 6911 | * the right cookie, if we want to delete this cookie. |
| 6912 | * So there are 3 cases for each cookie read : |
| 6913 | * 1) it's a special attribute, beginning with a '$' : ignore it. |
| 6914 | * 2) it's a server id cookie that we *MAY* want to delete : save |
| 6915 | * some pointers on it (last semi-colon, beginning of cookie...) |
| 6916 | * 3) it's an application cookie : we *MAY* have to delete a previous |
| 6917 | * "special" cookie. |
| 6918 | * At the end of loop, if a "special" cookie remains, we may have to |
| 6919 | * remove it. If no application cookie persists in the header, we |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6920 | * *MUST* delete it. |
| 6921 | * |
| 6922 | * Note: RFC2965 is unclear about the processing of spaces around |
| 6923 | * the equal sign in the ATTR=VALUE form. A careful inspection of |
| 6924 | * the RFC explicitly allows spaces before it, and not within the |
| 6925 | * tokens (attrs or values). An inspection of RFC2109 allows that |
| 6926 | * too but section 10.1.3 lets one think that spaces may be allowed |
| 6927 | * after the equal sign too, resulting in some (rare) buggy |
| 6928 | * implementations trying to do that. So let's do what servers do. |
| 6929 | * Latest ietf draft forbids spaces all around. Also, earlier RFCs |
| 6930 | * allowed quoted strings in values, with any possible character |
| 6931 | * after a backslash, including control chars and delimitors, which |
| 6932 | * causes parsing to become ambiguous. Browsers also allow spaces |
| 6933 | * within values even without quotes. |
| 6934 | * |
| 6935 | * We have to keep multiple pointers in order to support cookie |
| 6936 | * removal at the beginning, middle or end of header without |
| 6937 | * corrupting the header. All of these headers are valid : |
| 6938 | * |
| 6939 | * Cookie:NAME1=VALUE1;NAME2=VALUE2;NAME3=VALUE3\r\n |
| 6940 | * Cookie:NAME1=VALUE1;NAME2_ONLY ;NAME3=VALUE3\r\n |
| 6941 | * Cookie: NAME1 = VALUE 1 ; NAME2 = VALUE2 ; NAME3 = VALUE3\r\n |
| 6942 | * | | | | | | | | | |
| 6943 | * | | | | | | | | hdr_end <--+ |
| 6944 | * | | | | | | | +--> next |
| 6945 | * | | | | | | +----> val_end |
| 6946 | * | | | | | +-----------> val_beg |
| 6947 | * | | | | +--------------> equal |
| 6948 | * | | | +----------------> att_end |
| 6949 | * | | +---------------------> att_beg |
| 6950 | * | +--------------------------> prev |
| 6951 | * +--------------------------------> hdr_beg |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6952 | */ |
| 6953 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6954 | for (prev = hdr_beg + 6; prev < hdr_end; prev = next) { |
| 6955 | /* Iterate through all cookies on this line */ |
| 6956 | |
| 6957 | /* find att_beg */ |
| 6958 | att_beg = prev + 1; |
| 6959 | while (att_beg < hdr_end && http_is_spht[(unsigned char)*att_beg]) |
| 6960 | att_beg++; |
| 6961 | |
| 6962 | /* find att_end : this is the first character after the last non |
| 6963 | * space before the equal. It may be equal to hdr_end. |
| 6964 | */ |
| 6965 | equal = att_end = att_beg; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6966 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6967 | while (equal < hdr_end) { |
| 6968 | if (*equal == '=' || *equal == ',' || *equal == ';') |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6969 | break; |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6970 | if (http_is_spht[(unsigned char)*equal++]) |
| 6971 | continue; |
| 6972 | att_end = equal; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6973 | } |
| 6974 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6975 | /* here, <equal> points to '=', a delimitor or the end. <att_end> |
| 6976 | * is between <att_beg> and <equal>, both may be identical. |
| 6977 | */ |
| 6978 | |
| 6979 | /* look for end of cookie if there is an equal sign */ |
| 6980 | if (equal < hdr_end && *equal == '=') { |
| 6981 | /* look for the beginning of the value */ |
| 6982 | val_beg = equal + 1; |
| 6983 | while (val_beg < hdr_end && http_is_spht[(unsigned char)*val_beg]) |
| 6984 | val_beg++; |
| 6985 | |
| 6986 | /* find the end of the value, respecting quotes */ |
| 6987 | next = find_cookie_value_end(val_beg, hdr_end); |
| 6988 | |
| 6989 | /* make val_end point to the first white space or delimitor after the value */ |
| 6990 | val_end = next; |
| 6991 | while (val_end > val_beg && http_is_spht[(unsigned char)*(val_end - 1)]) |
| 6992 | val_end--; |
| 6993 | } else { |
| 6994 | val_beg = val_end = next = equal; |
Willy Tarreau | 305ae85 | 2010-01-03 19:45:54 +0100 | [diff] [blame] | 6995 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6996 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6997 | /* We have nothing to do with attributes beginning with '$'. However, |
| 6998 | * they will automatically be removed if a header before them is removed, |
| 6999 | * since they're supposed to be linked together. |
| 7000 | */ |
| 7001 | if (*att_beg == '$') |
| 7002 | continue; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7003 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7004 | /* Ignore cookies with no equal sign */ |
| 7005 | if (equal == next) { |
| 7006 | /* This is not our cookie, so we must preserve it. But if we already |
| 7007 | * scheduled another cookie for removal, we cannot remove the |
| 7008 | * complete header, but we can remove the previous block itself. |
| 7009 | */ |
| 7010 | preserve_hdr = 1; |
| 7011 | if (del_from != NULL) { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 7012 | int delta = del_hdr_value(req->buf, &del_from, prev); |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7013 | val_end += delta; |
| 7014 | next += delta; |
| 7015 | hdr_end += delta; |
| 7016 | hdr_next += delta; |
| 7017 | cur_hdr->len += delta; |
| 7018 | http_msg_move_end(&txn->req, delta); |
| 7019 | prev = del_from; |
| 7020 | del_from = NULL; |
| 7021 | } |
| 7022 | continue; |
Willy Tarreau | 305ae85 | 2010-01-03 19:45:54 +0100 | [diff] [blame] | 7023 | } |
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 | /* if there are spaces around the equal sign, we need to |
| 7026 | * strip them otherwise we'll get trouble for cookie captures, |
| 7027 | * or even for rewrites. Since this happens extremely rarely, |
| 7028 | * it does not hurt performance. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7029 | */ |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7030 | if (unlikely(att_end != equal || val_beg > equal + 1)) { |
| 7031 | int stripped_before = 0; |
| 7032 | int stripped_after = 0; |
| 7033 | |
| 7034 | if (att_end != equal) { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 7035 | stripped_before = buffer_replace2(req->buf, att_end, equal, NULL, 0); |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7036 | equal += stripped_before; |
| 7037 | val_beg += stripped_before; |
| 7038 | } |
| 7039 | |
| 7040 | if (val_beg > equal + 1) { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 7041 | stripped_after = buffer_replace2(req->buf, equal + 1, val_beg, NULL, 0); |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7042 | val_beg += stripped_after; |
| 7043 | stripped_before += stripped_after; |
| 7044 | } |
| 7045 | |
| 7046 | val_end += stripped_before; |
| 7047 | next += stripped_before; |
| 7048 | hdr_end += stripped_before; |
| 7049 | hdr_next += stripped_before; |
| 7050 | cur_hdr->len += stripped_before; |
| 7051 | http_msg_move_end(&txn->req, stripped_before); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7052 | } |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7053 | /* now everything is as on the diagram above */ |
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 | /* First, let's see if we want to capture this cookie. We check |
| 7056 | * that we don't already have a client side cookie, because we |
| 7057 | * can only capture one. Also as an optimisation, we ignore |
| 7058 | * cookies shorter than the declared name. |
| 7059 | */ |
| 7060 | if (t->fe->capture_name != NULL && txn->cli_cookie == NULL && |
| 7061 | (val_end - att_beg >= t->fe->capture_namelen) && |
| 7062 | memcmp(att_beg, t->fe->capture_name, t->fe->capture_namelen) == 0) { |
| 7063 | int log_len = val_end - att_beg; |
| 7064 | |
| 7065 | if ((txn->cli_cookie = pool_alloc2(pool2_capture)) == NULL) { |
| 7066 | Alert("HTTP logging : out of memory.\n"); |
| 7067 | } else { |
| 7068 | if (log_len > t->fe->capture_len) |
| 7069 | log_len = t->fe->capture_len; |
| 7070 | memcpy(txn->cli_cookie, att_beg, log_len); |
| 7071 | txn->cli_cookie[log_len] = 0; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7072 | } |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7073 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7074 | |
Willy Tarreau | bca9969 | 2010-10-06 19:25:55 +0200 | [diff] [blame] | 7075 | /* Persistence cookies in passive, rewrite or insert mode have the |
| 7076 | * following form : |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7077 | * |
Willy Tarreau | bca9969 | 2010-10-06 19:25:55 +0200 | [diff] [blame] | 7078 | * Cookie: NAME=SRV[|<lastseen>[|<firstseen>]] |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7079 | * |
Willy Tarreau | bca9969 | 2010-10-06 19:25:55 +0200 | [diff] [blame] | 7080 | * For cookies in prefix mode, the form is : |
| 7081 | * |
| 7082 | * Cookie: NAME=SRV~VALUE |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7083 | */ |
| 7084 | if ((att_end - att_beg == t->be->cookie_len) && (t->be->cookie_name != NULL) && |
| 7085 | (memcmp(att_beg, t->be->cookie_name, att_end - att_beg) == 0)) { |
| 7086 | struct server *srv = t->be->srv; |
| 7087 | char *delim; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7088 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7089 | /* if we're in cookie prefix mode, we'll search the delimitor so that we |
| 7090 | * have the server ID between val_beg and delim, and the original cookie between |
| 7091 | * delim+1 and val_end. Otherwise, delim==val_end : |
| 7092 | * |
| 7093 | * Cookie: NAME=SRV; # in all but prefix modes |
| 7094 | * Cookie: NAME=SRV~OPAQUE ; # in prefix mode |
| 7095 | * | || || | |+-> next |
| 7096 | * | || || | +--> val_end |
| 7097 | * | || || +---------> delim |
| 7098 | * | || |+------------> val_beg |
| 7099 | * | || +-------------> att_end = equal |
| 7100 | * | |+-----------------> att_beg |
| 7101 | * | +------------------> prev |
| 7102 | * +-------------------------> hdr_beg |
| 7103 | */ |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7104 | |
Willy Tarreau | 6740213 | 2012-05-31 20:40:20 +0200 | [diff] [blame] | 7105 | if (t->be->ck_opts & PR_CK_PFX) { |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7106 | for (delim = val_beg; delim < val_end; delim++) |
| 7107 | if (*delim == COOKIE_DELIM) |
| 7108 | break; |
Willy Tarreau | bca9969 | 2010-10-06 19:25:55 +0200 | [diff] [blame] | 7109 | } else { |
| 7110 | char *vbar1; |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7111 | delim = val_end; |
Willy Tarreau | bca9969 | 2010-10-06 19:25:55 +0200 | [diff] [blame] | 7112 | /* Now check if the cookie contains a date field, which would |
| 7113 | * appear after a vertical bar ('|') just after the server name |
| 7114 | * and before the delimiter. |
| 7115 | */ |
| 7116 | vbar1 = memchr(val_beg, COOKIE_DELIM_DATE, val_end - val_beg); |
| 7117 | if (vbar1) { |
| 7118 | /* OK, so left of the bar is the server's cookie and |
Willy Tarreau | f64d141 | 2010-10-07 20:06:11 +0200 | [diff] [blame] | 7119 | * right is the last seen date. It is a base64 encoded |
| 7120 | * 30-bit value representing the UNIX date since the |
| 7121 | * epoch in 4-second quantities. |
Willy Tarreau | bca9969 | 2010-10-06 19:25:55 +0200 | [diff] [blame] | 7122 | */ |
Willy Tarreau | f64d141 | 2010-10-07 20:06:11 +0200 | [diff] [blame] | 7123 | int val; |
Willy Tarreau | bca9969 | 2010-10-06 19:25:55 +0200 | [diff] [blame] | 7124 | delim = vbar1++; |
Willy Tarreau | f64d141 | 2010-10-07 20:06:11 +0200 | [diff] [blame] | 7125 | if (val_end - vbar1 >= 5) { |
| 7126 | val = b64tos30(vbar1); |
| 7127 | if (val > 0) |
| 7128 | txn->cookie_last_date = val << 2; |
| 7129 | } |
| 7130 | /* look for a second vertical bar */ |
| 7131 | vbar1 = memchr(vbar1, COOKIE_DELIM_DATE, val_end - vbar1); |
| 7132 | if (vbar1 && (val_end - vbar1 > 5)) { |
| 7133 | val = b64tos30(vbar1 + 1); |
| 7134 | if (val > 0) |
| 7135 | txn->cookie_first_date = val << 2; |
| 7136 | } |
Willy Tarreau | bca9969 | 2010-10-06 19:25:55 +0200 | [diff] [blame] | 7137 | } |
| 7138 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7139 | |
Willy Tarreau | f64d141 | 2010-10-07 20:06:11 +0200 | [diff] [blame] | 7140 | /* if the cookie has an expiration date and the proxy wants to check |
| 7141 | * it, then we do that now. We first check if the cookie is too old, |
| 7142 | * then only if it has expired. We detect strict overflow because the |
| 7143 | * time resolution here is not great (4 seconds). Cookies with dates |
| 7144 | * in the future are ignored if their offset is beyond one day. This |
| 7145 | * allows an admin to fix timezone issues without expiring everyone |
| 7146 | * and at the same time avoids keeping unwanted side effects for too |
| 7147 | * long. |
| 7148 | */ |
| 7149 | if (txn->cookie_first_date && t->be->cookie_maxlife && |
Willy Tarreau | ef4f391 | 2010-10-07 21:00:29 +0200 | [diff] [blame] | 7150 | (((signed)(date.tv_sec - txn->cookie_first_date) > (signed)t->be->cookie_maxlife) || |
| 7151 | ((signed)(txn->cookie_first_date - date.tv_sec) > 86400))) { |
Willy Tarreau | f64d141 | 2010-10-07 20:06:11 +0200 | [diff] [blame] | 7152 | txn->flags &= ~TX_CK_MASK; |
| 7153 | txn->flags |= TX_CK_OLD; |
| 7154 | delim = val_beg; // let's pretend we have not found the cookie |
| 7155 | txn->cookie_first_date = 0; |
| 7156 | txn->cookie_last_date = 0; |
| 7157 | } |
| 7158 | else if (txn->cookie_last_date && t->be->cookie_maxidle && |
Willy Tarreau | ef4f391 | 2010-10-07 21:00:29 +0200 | [diff] [blame] | 7159 | (((signed)(date.tv_sec - txn->cookie_last_date) > (signed)t->be->cookie_maxidle) || |
| 7160 | ((signed)(txn->cookie_last_date - date.tv_sec) > 86400))) { |
Willy Tarreau | f64d141 | 2010-10-07 20:06:11 +0200 | [diff] [blame] | 7161 | txn->flags &= ~TX_CK_MASK; |
| 7162 | txn->flags |= TX_CK_EXPIRED; |
| 7163 | delim = val_beg; // let's pretend we have not found the cookie |
| 7164 | txn->cookie_first_date = 0; |
| 7165 | txn->cookie_last_date = 0; |
| 7166 | } |
| 7167 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7168 | /* Here, we'll look for the first running server which supports the cookie. |
| 7169 | * This allows to share a same cookie between several servers, for example |
| 7170 | * to dedicate backup servers to specific servers only. |
| 7171 | * However, to prevent clients from sticking to cookie-less backup server |
| 7172 | * when they have incidentely learned an empty cookie, we simply ignore |
| 7173 | * empty cookies and mark them as invalid. |
| 7174 | * The same behaviour is applied when persistence must be ignored. |
| 7175 | */ |
Willy Tarreau | 4a5cade | 2012-04-05 21:09:48 +0200 | [diff] [blame] | 7176 | if ((delim == val_beg) || (t->flags & (SN_IGNORE_PRST | SN_ASSIGNED))) |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7177 | srv = NULL; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7178 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7179 | while (srv) { |
| 7180 | if (srv->cookie && (srv->cklen == delim - val_beg) && |
| 7181 | !memcmp(val_beg, srv->cookie, delim - val_beg)) { |
| 7182 | if ((srv->state & SRV_RUNNING) || |
| 7183 | (t->be->options & PR_O_PERSIST) || |
| 7184 | (t->flags & SN_FORCE_PRST)) { |
| 7185 | /* we found the server and we can use it */ |
| 7186 | txn->flags &= ~TX_CK_MASK; |
| 7187 | txn->flags |= (srv->state & SRV_RUNNING) ? TX_CK_VALID : TX_CK_DOWN; |
| 7188 | t->flags |= SN_DIRECT | SN_ASSIGNED; |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 7189 | t->target = &srv->obj_type; |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7190 | break; |
| 7191 | } else { |
| 7192 | /* we found a server, but it's down, |
| 7193 | * mark it as such and go on in case |
| 7194 | * another one is available. |
| 7195 | */ |
| 7196 | txn->flags &= ~TX_CK_MASK; |
| 7197 | txn->flags |= TX_CK_DOWN; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7198 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7199 | } |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7200 | srv = srv->next; |
| 7201 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7202 | |
Willy Tarreau | f64d141 | 2010-10-07 20:06:11 +0200 | [diff] [blame] | 7203 | 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] | 7204 | /* no server matched this cookie or we deliberately skipped it */ |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7205 | txn->flags &= ~TX_CK_MASK; |
Willy Tarreau | c89ccb6 | 2012-04-05 21:18:22 +0200 | [diff] [blame] | 7206 | if ((t->flags & (SN_IGNORE_PRST | SN_ASSIGNED))) |
| 7207 | txn->flags |= TX_CK_UNUSED; |
| 7208 | else |
| 7209 | txn->flags |= TX_CK_INVALID; |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7210 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7211 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7212 | /* depending on the cookie mode, we may have to either : |
| 7213 | * - delete the complete cookie if we're in insert+indirect mode, so that |
| 7214 | * the server never sees it ; |
| 7215 | * - remove the server id from the cookie value, and tag the cookie as an |
| 7216 | * application cookie so that it does not get accidentely removed later, |
| 7217 | * if we're in cookie prefix mode |
| 7218 | */ |
Willy Tarreau | 6740213 | 2012-05-31 20:40:20 +0200 | [diff] [blame] | 7219 | if ((t->be->ck_opts & PR_CK_PFX) && (delim != val_end)) { |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7220 | int delta; /* negative */ |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7221 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 7222 | delta = buffer_replace2(req->buf, val_beg, delim + 1, NULL, 0); |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7223 | val_end += delta; |
| 7224 | next += delta; |
| 7225 | hdr_end += delta; |
| 7226 | hdr_next += delta; |
| 7227 | cur_hdr->len += delta; |
| 7228 | http_msg_move_end(&txn->req, delta); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7229 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7230 | del_from = NULL; |
| 7231 | preserve_hdr = 1; /* we want to keep this cookie */ |
| 7232 | } |
| 7233 | else if (del_from == NULL && |
Willy Tarreau | 6740213 | 2012-05-31 20:40:20 +0200 | [diff] [blame] | 7234 | (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] | 7235 | del_from = prev; |
| 7236 | } |
| 7237 | } else { |
| 7238 | /* This is not our cookie, so we must preserve it. But if we already |
| 7239 | * scheduled another cookie for removal, we cannot remove the |
| 7240 | * complete header, but we can remove the previous block itself. |
| 7241 | */ |
| 7242 | preserve_hdr = 1; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7243 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7244 | if (del_from != NULL) { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 7245 | int delta = del_hdr_value(req->buf, &del_from, prev); |
Willy Tarreau | b810554 | 2010-11-24 18:31:28 +0100 | [diff] [blame] | 7246 | if (att_beg >= del_from) |
| 7247 | att_beg += delta; |
| 7248 | if (att_end >= del_from) |
| 7249 | att_end += delta; |
| 7250 | val_beg += delta; |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7251 | val_end += delta; |
| 7252 | next += delta; |
| 7253 | hdr_end += delta; |
| 7254 | hdr_next += delta; |
| 7255 | cur_hdr->len += delta; |
| 7256 | http_msg_move_end(&txn->req, delta); |
| 7257 | prev = del_from; |
| 7258 | del_from = NULL; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7259 | } |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7260 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7261 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7262 | /* Look for the appsession cookie unless persistence must be ignored */ |
| 7263 | if (!(t->flags & SN_IGNORE_PRST) && (t->be->appsession_name != NULL)) { |
| 7264 | int cmp_len, value_len; |
| 7265 | char *value_begin; |
Aleksandar Lazic | 697bbb0 | 2008-08-13 19:57:02 +0200 | [diff] [blame] | 7266 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7267 | if (t->be->options2 & PR_O2_AS_PFX) { |
| 7268 | cmp_len = MIN(val_end - att_beg, t->be->appsession_name_len); |
| 7269 | value_begin = att_beg + t->be->appsession_name_len; |
| 7270 | value_len = val_end - att_beg - t->be->appsession_name_len; |
| 7271 | } else { |
| 7272 | cmp_len = att_end - att_beg; |
| 7273 | value_begin = val_beg; |
| 7274 | value_len = val_end - val_beg; |
| 7275 | } |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 7276 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7277 | /* let's see if the cookie is our appcookie */ |
| 7278 | if (cmp_len == t->be->appsession_name_len && |
| 7279 | memcmp(att_beg, t->be->appsession_name, cmp_len) == 0) { |
| 7280 | manage_client_side_appsession(t, value_begin, value_len); |
| 7281 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7282 | } |
| 7283 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7284 | /* continue with next cookie on this header line */ |
| 7285 | att_beg = next; |
| 7286 | } /* for each cookie */ |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7287 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7288 | /* There are no more cookies on this line. |
| 7289 | * We may still have one (or several) marked for deletion at the |
| 7290 | * end of the line. We must do this now in two ways : |
| 7291 | * - if some cookies must be preserved, we only delete from the |
| 7292 | * mark to the end of line ; |
| 7293 | * - if nothing needs to be preserved, simply delete the whole header |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7294 | */ |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7295 | if (del_from) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7296 | int delta; |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7297 | if (preserve_hdr) { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 7298 | delta = del_hdr_value(req->buf, &del_from, hdr_end); |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7299 | hdr_end = del_from; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7300 | cur_hdr->len += delta; |
| 7301 | } else { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 7302 | delta = buffer_replace2(req->buf, hdr_beg, hdr_next, NULL, 0); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7303 | |
| 7304 | /* FIXME: this should be a separate function */ |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 7305 | txn->hdr_idx.v[old_idx].next = cur_hdr->next; |
| 7306 | txn->hdr_idx.used--; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7307 | cur_hdr->len = 0; |
Willy Tarreau | 26db59e | 2010-11-28 06:57:24 +0100 | [diff] [blame] | 7308 | cur_idx = old_idx; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7309 | } |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7310 | hdr_next += delta; |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 7311 | http_msg_move_end(&txn->req, delta); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7312 | } |
| 7313 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7314 | /* check next header */ |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7315 | old_idx = cur_idx; |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7316 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7317 | } |
| 7318 | |
| 7319 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7320 | /* Iterate the same filter through all response headers contained in <rtr>. |
| 7321 | * Returns 1 if this filter can be stopped upon return, otherwise 0. |
| 7322 | */ |
Willy Tarreau | 7421efb | 2012-07-02 15:11:27 +0200 | [diff] [blame] | 7323 | 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] | 7324 | { |
| 7325 | char term; |
| 7326 | char *cur_ptr, *cur_end, *cur_next; |
| 7327 | int cur_idx, old_idx, last_hdr; |
| 7328 | struct http_txn *txn = &t->txn; |
| 7329 | struct hdr_idx_elem *cur_hdr; |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 7330 | int delta; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7331 | |
| 7332 | last_hdr = 0; |
| 7333 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 7334 | cur_next = rtr->buf->p + hdr_idx_first_pos(&txn->hdr_idx); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7335 | old_idx = 0; |
| 7336 | |
| 7337 | while (!last_hdr) { |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 7338 | if (unlikely(txn->flags & TX_SVDENY)) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7339 | return 1; |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 7340 | else if (unlikely(txn->flags & TX_SVALLOW) && |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7341 | (exp->action == ACT_ALLOW || |
| 7342 | exp->action == ACT_DENY)) |
| 7343 | return 0; |
| 7344 | |
| 7345 | cur_idx = txn->hdr_idx.v[old_idx].next; |
| 7346 | if (!cur_idx) |
| 7347 | break; |
| 7348 | |
| 7349 | cur_hdr = &txn->hdr_idx.v[cur_idx]; |
| 7350 | cur_ptr = cur_next; |
| 7351 | cur_end = cur_ptr + cur_hdr->len; |
| 7352 | cur_next = cur_end + cur_hdr->cr + 1; |
| 7353 | |
| 7354 | /* Now we have one header between cur_ptr and cur_end, |
| 7355 | * and the next header starts at cur_next. |
| 7356 | */ |
| 7357 | |
| 7358 | /* The annoying part is that pattern matching needs |
| 7359 | * that we modify the contents to null-terminate all |
| 7360 | * strings before testing them. |
| 7361 | */ |
| 7362 | |
| 7363 | term = *cur_end; |
| 7364 | *cur_end = '\0'; |
| 7365 | |
| 7366 | if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) { |
| 7367 | switch (exp->action) { |
| 7368 | case ACT_ALLOW: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 7369 | txn->flags |= TX_SVALLOW; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7370 | last_hdr = 1; |
| 7371 | break; |
| 7372 | |
| 7373 | case ACT_DENY: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 7374 | txn->flags |= TX_SVDENY; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7375 | last_hdr = 1; |
| 7376 | break; |
| 7377 | |
| 7378 | case ACT_REPLACE: |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 7379 | trash.len = exp_replace(trash.str, cur_ptr, exp->replace, pmatch); |
| 7380 | 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] | 7381 | /* FIXME: if the user adds a newline in the replacement, the |
| 7382 | * index will not be recalculated for now, and the new line |
| 7383 | * will not be counted as a new header. |
| 7384 | */ |
| 7385 | |
| 7386 | cur_end += delta; |
| 7387 | cur_next += delta; |
| 7388 | cur_hdr->len += delta; |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 7389 | http_msg_move_end(&txn->rsp, delta); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7390 | break; |
| 7391 | |
| 7392 | case ACT_REMOVE: |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 7393 | delta = buffer_replace2(rtr->buf, cur_ptr, cur_next, NULL, 0); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7394 | cur_next += delta; |
| 7395 | |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 7396 | http_msg_move_end(&txn->rsp, delta); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7397 | txn->hdr_idx.v[old_idx].next = cur_hdr->next; |
| 7398 | txn->hdr_idx.used--; |
| 7399 | cur_hdr->len = 0; |
| 7400 | cur_end = NULL; /* null-term has been rewritten */ |
Willy Tarreau | 26db59e | 2010-11-28 06:57:24 +0100 | [diff] [blame] | 7401 | cur_idx = old_idx; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7402 | break; |
| 7403 | |
| 7404 | } |
| 7405 | } |
| 7406 | if (cur_end) |
| 7407 | *cur_end = term; /* restore the string terminator */ |
| 7408 | |
| 7409 | /* keep the link from this header to next one in case of later |
| 7410 | * removal of next header. |
| 7411 | */ |
| 7412 | old_idx = cur_idx; |
| 7413 | } |
| 7414 | return 0; |
| 7415 | } |
| 7416 | |
| 7417 | |
| 7418 | /* Apply the filter to the status line in the response buffer <rtr>. |
| 7419 | * Returns 0 if nothing has been done, 1 if the filter has been applied, |
| 7420 | * or -1 if a replacement resulted in an invalid status line. |
| 7421 | */ |
Willy Tarreau | 7421efb | 2012-07-02 15:11:27 +0200 | [diff] [blame] | 7422 | 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] | 7423 | { |
| 7424 | char term; |
| 7425 | char *cur_ptr, *cur_end; |
| 7426 | int done; |
| 7427 | struct http_txn *txn = &t->txn; |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 7428 | int delta; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7429 | |
| 7430 | |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 7431 | if (unlikely(txn->flags & TX_SVDENY)) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7432 | return 1; |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 7433 | else if (unlikely(txn->flags & TX_SVALLOW) && |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7434 | (exp->action == ACT_ALLOW || |
| 7435 | exp->action == ACT_DENY)) |
| 7436 | return 0; |
| 7437 | else if (exp->action == ACT_REMOVE) |
| 7438 | return 0; |
| 7439 | |
| 7440 | done = 0; |
| 7441 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 7442 | cur_ptr = rtr->buf->p; |
Willy Tarreau | 1ba0e5f | 2010-06-07 13:57:32 +0200 | [diff] [blame] | 7443 | cur_end = cur_ptr + txn->rsp.sl.st.l; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7444 | |
| 7445 | /* Now we have the status line between cur_ptr and cur_end */ |
| 7446 | |
| 7447 | /* The annoying part is that pattern matching needs |
| 7448 | * that we modify the contents to null-terminate all |
| 7449 | * strings before testing them. |
| 7450 | */ |
| 7451 | |
| 7452 | term = *cur_end; |
| 7453 | *cur_end = '\0'; |
| 7454 | |
| 7455 | if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) { |
| 7456 | switch (exp->action) { |
| 7457 | case ACT_ALLOW: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 7458 | txn->flags |= TX_SVALLOW; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7459 | done = 1; |
| 7460 | break; |
| 7461 | |
| 7462 | case ACT_DENY: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 7463 | txn->flags |= TX_SVDENY; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7464 | done = 1; |
| 7465 | break; |
| 7466 | |
| 7467 | case ACT_REPLACE: |
| 7468 | *cur_end = term; /* restore the string terminator */ |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 7469 | trash.len = exp_replace(trash.str, cur_ptr, exp->replace, pmatch); |
| 7470 | 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] | 7471 | /* FIXME: if the user adds a newline in the replacement, the |
| 7472 | * index will not be recalculated for now, and the new line |
| 7473 | * will not be counted as a new header. |
| 7474 | */ |
| 7475 | |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 7476 | http_msg_move_end(&txn->rsp, delta); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7477 | cur_end += delta; |
Willy Tarreau | 69d8c5d | 2012-05-08 09:44:41 +0200 | [diff] [blame] | 7478 | cur_end = (char *)http_parse_stsline(&txn->rsp, |
Willy Tarreau | 0278576 | 2007-04-03 14:45:44 +0200 | [diff] [blame] | 7479 | HTTP_MSG_RPVER, |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7480 | cur_ptr, cur_end + 1, |
| 7481 | NULL, NULL); |
| 7482 | if (unlikely(!cur_end)) |
| 7483 | return -1; |
| 7484 | |
| 7485 | /* we have a full respnse and we know that we have either a CR |
| 7486 | * or an LF at <ptr>. |
| 7487 | */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 7488 | 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] | 7489 | 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] | 7490 | /* there is no point trying this regex on headers */ |
| 7491 | return 1; |
| 7492 | } |
| 7493 | } |
| 7494 | *cur_end = term; /* restore the string terminator */ |
| 7495 | return done; |
| 7496 | } |
| 7497 | |
| 7498 | |
| 7499 | |
| 7500 | /* |
Willy Tarreau | fdb563c | 2010-01-31 15:43:27 +0100 | [diff] [blame] | 7501 | * 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] | 7502 | * Returns 0 if everything is alright, or -1 in case a replacement lead to an |
| 7503 | * unparsable response. |
| 7504 | */ |
Willy Tarreau | 7421efb | 2012-07-02 15:11:27 +0200 | [diff] [blame] | 7505 | 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] | 7506 | { |
Willy Tarreau | fdb563c | 2010-01-31 15:43:27 +0100 | [diff] [blame] | 7507 | struct http_txn *txn = &s->txn; |
| 7508 | struct hdr_exp *exp; |
| 7509 | |
| 7510 | for (exp = px->rsp_exp; exp; exp = exp->next) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7511 | int ret; |
| 7512 | |
| 7513 | /* |
| 7514 | * The interleaving of transformations and verdicts |
| 7515 | * makes it difficult to decide to continue or stop |
| 7516 | * the evaluation. |
| 7517 | */ |
| 7518 | |
Willy Tarreau | fdb563c | 2010-01-31 15:43:27 +0100 | [diff] [blame] | 7519 | if (txn->flags & TX_SVDENY) |
| 7520 | break; |
| 7521 | |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 7522 | if ((txn->flags & TX_SVALLOW) && |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7523 | (exp->action == ACT_ALLOW || exp->action == ACT_DENY || |
| 7524 | exp->action == ACT_PASS)) { |
| 7525 | exp = exp->next; |
| 7526 | continue; |
| 7527 | } |
| 7528 | |
Willy Tarreau | fdb563c | 2010-01-31 15:43:27 +0100 | [diff] [blame] | 7529 | /* if this filter had a condition, evaluate it now and skip to |
| 7530 | * next filter if the condition does not match. |
| 7531 | */ |
| 7532 | if (exp->cond) { |
Willy Tarreau | 32a6f2e | 2012-04-25 10:13:36 +0200 | [diff] [blame] | 7533 | 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] | 7534 | ret = acl_pass(ret); |
| 7535 | if (((struct acl_cond *)exp->cond)->pol == ACL_COND_UNLESS) |
| 7536 | ret = !ret; |
| 7537 | if (!ret) |
| 7538 | continue; |
| 7539 | } |
| 7540 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7541 | /* Apply the filter to the status line. */ |
Willy Tarreau | fdb563c | 2010-01-31 15:43:27 +0100 | [diff] [blame] | 7542 | ret = apply_filter_to_sts_line(s, rtr, exp); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7543 | if (unlikely(ret < 0)) |
| 7544 | return -1; |
| 7545 | |
| 7546 | if (likely(ret == 0)) { |
| 7547 | /* The filter did not match the response, it can be |
| 7548 | * iterated through all headers. |
| 7549 | */ |
Willy Tarreau | fdb563c | 2010-01-31 15:43:27 +0100 | [diff] [blame] | 7550 | apply_filter_to_resp_headers(s, rtr, exp); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7551 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7552 | } |
| 7553 | return 0; |
| 7554 | } |
| 7555 | |
| 7556 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7557 | /* |
Willy Tarreau | 396d2c6 | 2007-11-04 19:30:00 +0100 | [diff] [blame] | 7558 | * 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] | 7559 | * desirable to call it only when needed. This function is also used when we |
| 7560 | * 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] | 7561 | */ |
Willy Tarreau | 7421efb | 2012-07-02 15:11:27 +0200 | [diff] [blame] | 7562 | void manage_server_side_cookies(struct session *t, struct channel *res) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7563 | { |
| 7564 | struct http_txn *txn = &t->txn; |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 7565 | struct server *srv; |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7566 | int is_cookie2; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7567 | int cur_idx, old_idx, delta; |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7568 | char *hdr_beg, *hdr_end, *hdr_next; |
| 7569 | char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7570 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7571 | /* Iterate through the headers. |
| 7572 | * we start with the start line. |
| 7573 | */ |
| 7574 | old_idx = 0; |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 7575 | hdr_next = res->buf->p + hdr_idx_first_pos(&txn->hdr_idx); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7576 | |
| 7577 | while ((cur_idx = txn->hdr_idx.v[old_idx].next)) { |
| 7578 | struct hdr_idx_elem *cur_hdr; |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 7579 | int val; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7580 | |
| 7581 | cur_hdr = &txn->hdr_idx.v[cur_idx]; |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7582 | hdr_beg = hdr_next; |
| 7583 | hdr_end = hdr_beg + cur_hdr->len; |
| 7584 | hdr_next = hdr_end + cur_hdr->cr + 1; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7585 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7586 | /* We have one full header between hdr_beg and hdr_end, and the |
| 7587 | * next header starts at hdr_next. We're only interested in |
| 7588 | * "Set-Cookie" and "Set-Cookie2" headers. |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7589 | */ |
| 7590 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7591 | is_cookie2 = 0; |
| 7592 | prev = hdr_beg + 10; |
| 7593 | val = http_header_match2(hdr_beg, hdr_end, "Set-Cookie", 10); |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 7594 | if (!val) { |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7595 | val = http_header_match2(hdr_beg, hdr_end, "Set-Cookie2", 11); |
| 7596 | if (!val) { |
| 7597 | old_idx = cur_idx; |
| 7598 | continue; |
| 7599 | } |
| 7600 | is_cookie2 = 1; |
| 7601 | prev = hdr_beg + 11; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7602 | } |
| 7603 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7604 | /* OK, right now we know we have a Set-Cookie* at hdr_beg, and |
| 7605 | * <prev> points to the colon. |
| 7606 | */ |
Willy Tarreau | f134831 | 2010-10-07 15:54:11 +0200 | [diff] [blame] | 7607 | txn->flags |= TX_SCK_PRESENT; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7608 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7609 | /* Maybe we only wanted to see if there was a Set-Cookie (eg: |
| 7610 | * check-cache is enabled) and we are not interested in checking |
| 7611 | * them. Warning, the cookie capture is declared in the frontend. |
Willy Tarreau | fd39dda | 2008-10-17 12:01:58 +0200 | [diff] [blame] | 7612 | */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 7613 | if (t->be->cookie_name == NULL && |
| 7614 | t->be->appsession_name == NULL && |
Willy Tarreau | fd39dda | 2008-10-17 12:01:58 +0200 | [diff] [blame] | 7615 | t->fe->capture_name == NULL) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7616 | return; |
| 7617 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7618 | /* OK so now we know we have to process this response cookie. |
| 7619 | * The format of the Set-Cookie header is slightly different |
| 7620 | * from the format of the Cookie header in that it does not |
| 7621 | * support the comma as a cookie delimiter (thus the header |
| 7622 | * cannot be folded) because the Expires attribute described in |
| 7623 | * the original Netscape's spec may contain an unquoted date |
| 7624 | * with a comma inside. We have to live with this because |
| 7625 | * many browsers don't support Max-Age and some browsers don't |
| 7626 | * support quoted strings. However the Set-Cookie2 header is |
| 7627 | * clean. |
| 7628 | * |
| 7629 | * We have to keep multiple pointers in order to support cookie |
| 7630 | * removal at the beginning, middle or end of header without |
| 7631 | * corrupting the header (in case of set-cookie2). A special |
| 7632 | * pointer, <scav> points to the beginning of the set-cookie-av |
| 7633 | * fields after the first semi-colon. The <next> pointer points |
| 7634 | * either to the end of line (set-cookie) or next unquoted comma |
| 7635 | * (set-cookie2). All of these headers are valid : |
| 7636 | * |
| 7637 | * Set-Cookie: NAME1 = VALUE 1 ; Secure; Path="/"\r\n |
| 7638 | * Set-Cookie:NAME=VALUE; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT\r\n |
| 7639 | * Set-Cookie: NAME = VALUE ; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT\r\n |
| 7640 | * Set-Cookie2: NAME1 = VALUE 1 ; Max-Age=0, NAME2=VALUE2; Discard\r\n |
| 7641 | * | | | | | | | | | | |
| 7642 | * | | | | | | | | +-> next hdr_end <--+ |
| 7643 | * | | | | | | | +------------> scav |
| 7644 | * | | | | | | +--------------> val_end |
| 7645 | * | | | | | +--------------------> val_beg |
| 7646 | * | | | | +----------------------> equal |
| 7647 | * | | | +------------------------> att_end |
| 7648 | * | | +----------------------------> att_beg |
| 7649 | * | +------------------------------> prev |
| 7650 | * +-----------------------------------------> hdr_beg |
| 7651 | */ |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7652 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7653 | for (; prev < hdr_end; prev = next) { |
| 7654 | /* Iterate through all cookies on this line */ |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7655 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7656 | /* find att_beg */ |
| 7657 | att_beg = prev + 1; |
| 7658 | while (att_beg < hdr_end && http_is_spht[(unsigned char)*att_beg]) |
| 7659 | att_beg++; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7660 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7661 | /* find att_end : this is the first character after the last non |
| 7662 | * space before the equal. It may be equal to hdr_end. |
| 7663 | */ |
| 7664 | equal = att_end = att_beg; |
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 | while (equal < hdr_end) { |
| 7667 | if (*equal == '=' || *equal == ';' || (is_cookie2 && *equal == ',')) |
| 7668 | break; |
| 7669 | if (http_is_spht[(unsigned char)*equal++]) |
| 7670 | continue; |
| 7671 | att_end = equal; |
| 7672 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7673 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7674 | /* here, <equal> points to '=', a delimitor or the end. <att_end> |
| 7675 | * is between <att_beg> and <equal>, both may be identical. |
| 7676 | */ |
| 7677 | |
| 7678 | /* look for end of cookie if there is an equal sign */ |
| 7679 | if (equal < hdr_end && *equal == '=') { |
| 7680 | /* look for the beginning of the value */ |
| 7681 | val_beg = equal + 1; |
| 7682 | while (val_beg < hdr_end && http_is_spht[(unsigned char)*val_beg]) |
| 7683 | val_beg++; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7684 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7685 | /* find the end of the value, respecting quotes */ |
| 7686 | next = find_cookie_value_end(val_beg, hdr_end); |
| 7687 | |
| 7688 | /* make val_end point to the first white space or delimitor after the value */ |
| 7689 | val_end = next; |
| 7690 | while (val_end > val_beg && http_is_spht[(unsigned char)*(val_end - 1)]) |
| 7691 | val_end--; |
| 7692 | } else { |
| 7693 | /* <equal> points to next comma, semi-colon or EOL */ |
| 7694 | val_beg = val_end = next = equal; |
| 7695 | } |
| 7696 | |
| 7697 | if (next < hdr_end) { |
| 7698 | /* Set-Cookie2 supports multiple cookies, and <next> points to |
| 7699 | * a colon or semi-colon before the end. So skip all attr-value |
| 7700 | * pairs and look for the next comma. For Set-Cookie, since |
| 7701 | * commas are permitted in values, skip to the end. |
| 7702 | */ |
| 7703 | if (is_cookie2) |
| 7704 | next = find_hdr_value_end(next, hdr_end); |
| 7705 | else |
| 7706 | next = hdr_end; |
| 7707 | } |
| 7708 | |
| 7709 | /* Now everything is as on the diagram above */ |
| 7710 | |
| 7711 | /* Ignore cookies with no equal sign */ |
| 7712 | if (equal == val_end) |
| 7713 | continue; |
| 7714 | |
| 7715 | /* If there are spaces around the equal sign, we need to |
| 7716 | * strip them otherwise we'll get trouble for cookie captures, |
| 7717 | * or even for rewrites. Since this happens extremely rarely, |
| 7718 | * it does not hurt performance. |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7719 | */ |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7720 | if (unlikely(att_end != equal || val_beg > equal + 1)) { |
| 7721 | int stripped_before = 0; |
| 7722 | int stripped_after = 0; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7723 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7724 | if (att_end != equal) { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 7725 | stripped_before = buffer_replace2(res->buf, att_end, equal, NULL, 0); |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7726 | equal += stripped_before; |
| 7727 | val_beg += stripped_before; |
| 7728 | } |
| 7729 | |
| 7730 | if (val_beg > equal + 1) { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 7731 | stripped_after = buffer_replace2(res->buf, equal + 1, val_beg, NULL, 0); |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7732 | val_beg += stripped_after; |
| 7733 | stripped_before += stripped_after; |
| 7734 | } |
| 7735 | |
| 7736 | val_end += stripped_before; |
| 7737 | next += stripped_before; |
| 7738 | hdr_end += stripped_before; |
| 7739 | hdr_next += stripped_before; |
| 7740 | cur_hdr->len += stripped_before; |
Willy Tarreau | 1fc1f45 | 2011-04-07 22:35:37 +0200 | [diff] [blame] | 7741 | http_msg_move_end(&txn->rsp, stripped_before); |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7742 | } |
| 7743 | |
| 7744 | /* First, let's see if we want to capture this cookie. We check |
| 7745 | * that we don't already have a server side cookie, because we |
| 7746 | * can only capture one. Also as an optimisation, we ignore |
| 7747 | * cookies shorter than the declared name. |
| 7748 | */ |
Willy Tarreau | fd39dda | 2008-10-17 12:01:58 +0200 | [diff] [blame] | 7749 | if (t->fe->capture_name != NULL && |
Willy Tarreau | 3bac9ff | 2007-03-18 17:31:28 +0100 | [diff] [blame] | 7750 | txn->srv_cookie == NULL && |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7751 | (val_end - att_beg >= t->fe->capture_namelen) && |
| 7752 | memcmp(att_beg, t->fe->capture_name, t->fe->capture_namelen) == 0) { |
| 7753 | int log_len = val_end - att_beg; |
Willy Tarreau | 086b3b4 | 2007-05-13 21:45:51 +0200 | [diff] [blame] | 7754 | if ((txn->srv_cookie = pool_alloc2(pool2_capture)) == NULL) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7755 | Alert("HTTP logging : out of memory.\n"); |
| 7756 | } |
Willy Tarreau | f70fc75 | 2010-11-19 11:27:18 +0100 | [diff] [blame] | 7757 | else { |
| 7758 | if (log_len > t->fe->capture_len) |
| 7759 | log_len = t->fe->capture_len; |
| 7760 | memcpy(txn->srv_cookie, att_beg, log_len); |
| 7761 | txn->srv_cookie[log_len] = 0; |
| 7762 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7763 | } |
| 7764 | |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 7765 | srv = objt_server(t->target); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7766 | /* now check if we need to process it for persistence */ |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7767 | if (!(t->flags & SN_IGNORE_PRST) && |
| 7768 | (att_end - att_beg == t->be->cookie_len) && (t->be->cookie_name != NULL) && |
| 7769 | (memcmp(att_beg, t->be->cookie_name, att_end - att_beg) == 0)) { |
Willy Tarreau | f134831 | 2010-10-07 15:54:11 +0200 | [diff] [blame] | 7770 | /* assume passive cookie by default */ |
| 7771 | txn->flags &= ~TX_SCK_MASK; |
| 7772 | txn->flags |= TX_SCK_FOUND; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7773 | |
| 7774 | /* If the cookie is in insert mode on a known server, we'll delete |
| 7775 | * this occurrence because we'll insert another one later. |
| 7776 | * 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] | 7777 | * a direct access. |
| 7778 | */ |
Willy Tarreau | 6740213 | 2012-05-31 20:40:20 +0200 | [diff] [blame] | 7779 | if (t->be->ck_opts & PR_CK_PSV) { |
Willy Tarreau | ba4c5be | 2010-10-23 12:46:42 +0200 | [diff] [blame] | 7780 | /* The "preserve" flag was set, we don't want to touch the |
| 7781 | * server's cookie. |
| 7782 | */ |
| 7783 | } |
Willy Tarreau | 6740213 | 2012-05-31 20:40:20 +0200 | [diff] [blame] | 7784 | else if ((srv && (t->be->ck_opts & PR_CK_INS)) || |
| 7785 | ((t->flags & SN_DIRECT) && (t->be->ck_opts & PR_CK_IND))) { |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7786 | /* this cookie must be deleted */ |
| 7787 | if (*prev == ':' && next == hdr_end) { |
| 7788 | /* whole header */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 7789 | delta = buffer_replace2(res->buf, hdr_beg, hdr_next, NULL, 0); |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7790 | txn->hdr_idx.v[old_idx].next = cur_hdr->next; |
| 7791 | txn->hdr_idx.used--; |
| 7792 | cur_hdr->len = 0; |
Willy Tarreau | 26db59e | 2010-11-28 06:57:24 +0100 | [diff] [blame] | 7793 | cur_idx = old_idx; |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7794 | hdr_next += delta; |
| 7795 | http_msg_move_end(&txn->rsp, delta); |
| 7796 | /* note: while both invalid now, <next> and <hdr_end> |
| 7797 | * are still equal, so the for() will stop as expected. |
| 7798 | */ |
| 7799 | } else { |
| 7800 | /* just remove the value */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 7801 | int delta = del_hdr_value(res->buf, &prev, next); |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7802 | next = prev; |
| 7803 | hdr_end += delta; |
| 7804 | hdr_next += delta; |
| 7805 | cur_hdr->len += delta; |
| 7806 | http_msg_move_end(&txn->rsp, delta); |
| 7807 | } |
Willy Tarreau | f134831 | 2010-10-07 15:54:11 +0200 | [diff] [blame] | 7808 | txn->flags &= ~TX_SCK_MASK; |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 7809 | txn->flags |= TX_SCK_DELETED; |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7810 | /* and go on with next cookie */ |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7811 | } |
Willy Tarreau | 6740213 | 2012-05-31 20:40:20 +0200 | [diff] [blame] | 7812 | else if (srv && srv->cookie && (t->be->ck_opts & PR_CK_RW)) { |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7813 | /* replace bytes val_beg->val_end with the cookie name associated |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7814 | * with this server since we know it. |
| 7815 | */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 7816 | 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] | 7817 | next += delta; |
| 7818 | hdr_end += delta; |
| 7819 | hdr_next += delta; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7820 | cur_hdr->len += delta; |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 7821 | http_msg_move_end(&txn->rsp, delta); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7822 | |
Willy Tarreau | f134831 | 2010-10-07 15:54:11 +0200 | [diff] [blame] | 7823 | txn->flags &= ~TX_SCK_MASK; |
| 7824 | txn->flags |= TX_SCK_REPLACED; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7825 | } |
Willy Tarreau | a059031 | 2012-06-06 16:07:00 +0200 | [diff] [blame] | 7826 | else if (srv && srv->cookie && (t->be->ck_opts & PR_CK_PFX)) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7827 | /* insert the cookie name associated with this server |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7828 | * before existing cookie, and insert a delimiter between them.. |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7829 | */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 7830 | 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] | 7831 | next += delta; |
| 7832 | hdr_end += delta; |
| 7833 | hdr_next += delta; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7834 | cur_hdr->len += delta; |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 7835 | http_msg_move_end(&txn->rsp, delta); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7836 | |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 7837 | val_beg[srv->cklen] = COOKIE_DELIM; |
Willy Tarreau | f134831 | 2010-10-07 15:54:11 +0200 | [diff] [blame] | 7838 | txn->flags &= ~TX_SCK_MASK; |
| 7839 | txn->flags |= TX_SCK_REPLACED; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7840 | } |
| 7841 | } |
Cyril Bonté | 47fdd8e | 2010-04-25 00:00:51 +0200 | [diff] [blame] | 7842 | /* next, let's see if the cookie is our appcookie, unless persistence must be ignored */ |
| 7843 | else if (!(t->flags & SN_IGNORE_PRST) && (t->be->appsession_name != NULL)) { |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 7844 | int cmp_len, value_len; |
| 7845 | char *value_begin; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7846 | |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 7847 | if (t->be->options2 & PR_O2_AS_PFX) { |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7848 | cmp_len = MIN(val_end - att_beg, t->be->appsession_name_len); |
| 7849 | value_begin = att_beg + t->be->appsession_name_len; |
| 7850 | 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] | 7851 | } else { |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7852 | cmp_len = att_end - att_beg; |
| 7853 | value_begin = val_beg; |
| 7854 | value_len = MIN(t->be->appsession_len, val_end - val_beg); |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 7855 | } |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 7856 | |
Cyril Bonté | 17530c3 | 2010-04-06 21:11:10 +0200 | [diff] [blame] | 7857 | if ((cmp_len == t->be->appsession_name_len) && |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7858 | (memcmp(att_beg, t->be->appsession_name, t->be->appsession_name_len) == 0)) { |
| 7859 | /* free a possibly previously allocated memory */ |
| 7860 | pool_free2(apools.sessid, txn->sessid); |
| 7861 | |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 7862 | /* Store the sessid in the session for future use */ |
Willy Tarreau | a3377ee | 2010-01-10 10:49:11 +0100 | [diff] [blame] | 7863 | if ((txn->sessid = pool_alloc2(apools.sessid)) == NULL) { |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 7864 | Alert("Not enough Memory process_srv():asession->sessid:malloc().\n"); |
| 7865 | send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n"); |
| 7866 | return; |
| 7867 | } |
Willy Tarreau | a3377ee | 2010-01-10 10:49:11 +0100 | [diff] [blame] | 7868 | memcpy(txn->sessid, value_begin, value_len); |
| 7869 | txn->sessid[value_len] = 0; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7870 | } |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7871 | } |
| 7872 | /* that's done for this cookie, check the next one on the same |
| 7873 | * line when next != hdr_end (only if is_cookie2). |
| 7874 | */ |
| 7875 | } |
| 7876 | /* check next header */ |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7877 | old_idx = cur_idx; |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7878 | } |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 7879 | |
Willy Tarreau | a3377ee | 2010-01-10 10:49:11 +0100 | [diff] [blame] | 7880 | if (txn->sessid != NULL) { |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 7881 | appsess *asession = NULL; |
| 7882 | /* only do insert, if lookup fails */ |
Willy Tarreau | a3377ee | 2010-01-10 10:49:11 +0100 | [diff] [blame] | 7883 | asession = appsession_hash_lookup(&(t->be->htbl_proxy), txn->sessid); |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 7884 | if (asession == NULL) { |
Willy Tarreau | 1fac753 | 2010-01-09 19:23:06 +0100 | [diff] [blame] | 7885 | size_t server_id_len; |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 7886 | if ((asession = pool_alloc2(pool2_appsess)) == NULL) { |
| 7887 | Alert("Not enough Memory process_srv():asession:calloc().\n"); |
| 7888 | send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession:calloc().\n"); |
| 7889 | return; |
| 7890 | } |
Willy Tarreau | 77eb9b8 | 2010-11-19 11:29:06 +0100 | [diff] [blame] | 7891 | asession->serverid = NULL; /* to avoid a double free in case of allocation error */ |
| 7892 | |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 7893 | if ((asession->sessid = pool_alloc2(apools.sessid)) == NULL) { |
| 7894 | Alert("Not enough Memory process_srv():asession->sessid:malloc().\n"); |
| 7895 | 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] | 7896 | t->be->htbl_proxy.destroy(asession); |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 7897 | return; |
| 7898 | } |
Willy Tarreau | a3377ee | 2010-01-10 10:49:11 +0100 | [diff] [blame] | 7899 | memcpy(asession->sessid, txn->sessid, t->be->appsession_len); |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 7900 | asession->sessid[t->be->appsession_len] = 0; |
| 7901 | |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 7902 | server_id_len = strlen(objt_server(t->target)->id) + 1; |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 7903 | if ((asession->serverid = pool_alloc2(apools.serverid)) == NULL) { |
Willy Tarreau | 77eb9b8 | 2010-11-19 11:29:06 +0100 | [diff] [blame] | 7904 | Alert("Not enough Memory process_srv():asession->serverid:malloc().\n"); |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 7905 | 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] | 7906 | t->be->htbl_proxy.destroy(asession); |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 7907 | return; |
| 7908 | } |
| 7909 | asession->serverid[0] = '\0'; |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 7910 | memcpy(asession->serverid, objt_server(t->target)->id, server_id_len); |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 7911 | |
| 7912 | asession->request_count = 0; |
| 7913 | appsession_hash_insert(&(t->be->htbl_proxy), asession); |
| 7914 | } |
| 7915 | |
| 7916 | asession->expire = tick_add_ifset(now_ms, t->be->timeout.appsession); |
| 7917 | asession->request_count++; |
| 7918 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7919 | } |
| 7920 | |
| 7921 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7922 | /* |
| 7923 | * Check if response is cacheable or not. Updates t->flags. |
| 7924 | */ |
Willy Tarreau | 7421efb | 2012-07-02 15:11:27 +0200 | [diff] [blame] | 7925 | void check_response_for_cacheability(struct session *t, struct channel *rtr) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7926 | { |
| 7927 | struct http_txn *txn = &t->txn; |
| 7928 | char *p1, *p2; |
| 7929 | |
| 7930 | char *cur_ptr, *cur_end, *cur_next; |
| 7931 | int cur_idx; |
| 7932 | |
Willy Tarreau | 5df5187 | 2007-11-25 16:20:08 +0100 | [diff] [blame] | 7933 | if (!(txn->flags & TX_CACHEABLE)) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7934 | return; |
| 7935 | |
| 7936 | /* Iterate through the headers. |
| 7937 | * we start with the start line. |
| 7938 | */ |
| 7939 | cur_idx = 0; |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 7940 | cur_next = rtr->buf->p + hdr_idx_first_pos(&txn->hdr_idx); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7941 | |
| 7942 | while ((cur_idx = txn->hdr_idx.v[cur_idx].next)) { |
| 7943 | struct hdr_idx_elem *cur_hdr; |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 7944 | int val; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7945 | |
| 7946 | cur_hdr = &txn->hdr_idx.v[cur_idx]; |
| 7947 | cur_ptr = cur_next; |
| 7948 | cur_end = cur_ptr + cur_hdr->len; |
| 7949 | cur_next = cur_end + cur_hdr->cr + 1; |
| 7950 | |
| 7951 | /* We have one full header between cur_ptr and cur_end, and the |
| 7952 | * next header starts at cur_next. We're only interested in |
| 7953 | * "Cookie:" headers. |
| 7954 | */ |
| 7955 | |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 7956 | val = http_header_match2(cur_ptr, cur_end, "Pragma", 6); |
| 7957 | if (val) { |
| 7958 | if ((cur_end - (cur_ptr + val) >= 8) && |
| 7959 | strncasecmp(cur_ptr + val, "no-cache", 8) == 0) { |
| 7960 | txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK; |
| 7961 | return; |
| 7962 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7963 | } |
| 7964 | |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 7965 | val = http_header_match2(cur_ptr, cur_end, "Cache-control", 13); |
| 7966 | if (!val) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7967 | continue; |
| 7968 | |
| 7969 | /* OK, right now we know we have a cache-control header at cur_ptr */ |
| 7970 | |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 7971 | p1 = cur_ptr + val; /* first non-space char after 'cache-control:' */ |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7972 | |
| 7973 | if (p1 >= cur_end) /* no more info */ |
| 7974 | continue; |
| 7975 | |
| 7976 | /* p1 is at the beginning of the value */ |
| 7977 | p2 = p1; |
| 7978 | |
Willy Tarreau | 8f8e645 | 2007-06-17 21:51:38 +0200 | [diff] [blame] | 7979 | while (p2 < cur_end && *p2 != '=' && *p2 != ',' && !isspace((unsigned char)*p2)) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7980 | p2++; |
| 7981 | |
| 7982 | /* we have a complete value between p1 and p2 */ |
| 7983 | if (p2 < cur_end && *p2 == '=') { |
| 7984 | /* we have something of the form no-cache="set-cookie" */ |
| 7985 | if ((cur_end - p1 >= 21) && |
| 7986 | strncasecmp(p1, "no-cache=\"set-cookie", 20) == 0 |
| 7987 | && (p1[20] == '"' || p1[20] == ',')) |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 7988 | txn->flags &= ~TX_CACHE_COOK; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7989 | continue; |
| 7990 | } |
| 7991 | |
| 7992 | /* OK, so we know that either p2 points to the end of string or to a comma */ |
| 7993 | if (((p2 - p1 == 7) && strncasecmp(p1, "private", 7) == 0) || |
Willy Tarreau | 5b15f90 | 2013-07-04 12:46:56 +0200 | [diff] [blame] | 7994 | ((p2 - p1 == 8) && strncasecmp(p1, "no-cache", 8) == 0) || |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7995 | ((p2 - p1 == 8) && strncasecmp(p1, "no-store", 8) == 0) || |
| 7996 | ((p2 - p1 == 9) && strncasecmp(p1, "max-age=0", 9) == 0) || |
| 7997 | ((p2 - p1 == 10) && strncasecmp(p1, "s-maxage=0", 10) == 0)) { |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 7998 | txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7999 | return; |
| 8000 | } |
| 8001 | |
| 8002 | if ((p2 - p1 == 6) && strncasecmp(p1, "public", 6) == 0) { |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 8003 | txn->flags |= TX_CACHEABLE | TX_CACHE_COOK; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8004 | continue; |
| 8005 | } |
| 8006 | } |
| 8007 | } |
| 8008 | |
| 8009 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 8010 | /* |
| 8011 | * Try to retrieve a known appsession in the URI, then the associated server. |
| 8012 | * If the server is found, it's assigned to the session. |
| 8013 | */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 8014 | 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] | 8015 | { |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 8016 | char *end_params, *first_param, *cur_param, *next_param; |
| 8017 | char separator; |
| 8018 | int value_len; |
| 8019 | |
| 8020 | int mode = t->be->options2 & PR_O2_AS_M_ANY; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 8021 | |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 8022 | if (t->be->appsession_name == NULL || |
Cyril Bonté | 17530c3 | 2010-04-06 21:11:10 +0200 | [diff] [blame] | 8023 | (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] | 8024 | return; |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 8025 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 8026 | |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 8027 | first_param = NULL; |
| 8028 | switch (mode) { |
| 8029 | case PR_O2_AS_M_PP: |
| 8030 | first_param = memchr(begin, ';', len); |
| 8031 | break; |
| 8032 | case PR_O2_AS_M_QS: |
| 8033 | first_param = memchr(begin, '?', len); |
| 8034 | break; |
| 8035 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 8036 | |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 8037 | if (first_param == NULL) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 8038 | return; |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 8039 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 8040 | |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 8041 | switch (mode) { |
| 8042 | case PR_O2_AS_M_PP: |
| 8043 | if ((end_params = memchr(first_param, '?', len - (begin - first_param))) == NULL) { |
| 8044 | end_params = (char *) begin + len; |
| 8045 | } |
| 8046 | separator = ';'; |
| 8047 | break; |
| 8048 | case PR_O2_AS_M_QS: |
| 8049 | end_params = (char *) begin + len; |
| 8050 | separator = '&'; |
| 8051 | break; |
| 8052 | default: |
| 8053 | /* unknown mode, shouldn't happen */ |
| 8054 | return; |
| 8055 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 8056 | |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 8057 | cur_param = next_param = end_params; |
| 8058 | while (cur_param > first_param) { |
| 8059 | cur_param--; |
| 8060 | if ((cur_param[0] == separator) || (cur_param == first_param)) { |
| 8061 | /* let's see if this is the appsession parameter */ |
| 8062 | if ((cur_param + t->be->appsession_name_len + 1 < next_param) && |
| 8063 | ((t->be->options2 & PR_O2_AS_PFX) || cur_param[t->be->appsession_name_len + 1] == '=') && |
| 8064 | (strncasecmp(cur_param + 1, t->be->appsession_name, t->be->appsession_name_len) == 0)) { |
| 8065 | /* Cool... it's the right one */ |
| 8066 | cur_param += t->be->appsession_name_len + (t->be->options2 & PR_O2_AS_PFX ? 1 : 2); |
| 8067 | value_len = MIN(t->be->appsession_len, next_param - cur_param); |
| 8068 | if (value_len > 0) { |
| 8069 | manage_client_side_appsession(t, cur_param, value_len); |
| 8070 | } |
| 8071 | break; |
| 8072 | } |
| 8073 | next_param = cur_param; |
| 8074 | } |
| 8075 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 8076 | #if defined(DEBUG_HASH) |
Aleksandar Lazic | 697bbb0 | 2008-08-13 19:57:02 +0200 | [diff] [blame] | 8077 | Alert("get_srv_from_appsession\n"); |
Willy Tarreau | 51041c7 | 2007-09-09 21:56:53 +0200 | [diff] [blame] | 8078 | appsession_hash_dump(&(t->be->htbl_proxy)); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 8079 | #endif |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 8080 | } |
| 8081 | |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 8082 | /* |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 8083 | * 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] | 8084 | * for the current backend. |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 8085 | * |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 8086 | * 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] | 8087 | * uri_auth field is valid. |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 8088 | * |
Krzysztof Piotr Oledzki | 8c8bd45 | 2010-01-29 19:29:32 +0100 | [diff] [blame] | 8089 | * Returns 1 if stats should be provided, otherwise 0. |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 8090 | */ |
Willy Tarreau | 295a837 | 2011-03-10 11:25:07 +0100 | [diff] [blame] | 8091 | 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] | 8092 | { |
| 8093 | struct uri_auth *uri_auth = backend->uri_auth; |
Willy Tarreau | 3a215be | 2012-03-09 21:39:51 +0100 | [diff] [blame] | 8094 | struct http_msg *msg = &txn->req; |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 8095 | const char *uri = msg->chn->buf->p+ msg->sl.rq.u; |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 8096 | |
Krzysztof Piotr Oledzki | 8c8bd45 | 2010-01-29 19:29:32 +0100 | [diff] [blame] | 8097 | if (!uri_auth) |
| 8098 | return 0; |
| 8099 | |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 8100 | 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] | 8101 | return 0; |
| 8102 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 8103 | /* check URI size */ |
Willy Tarreau | 3a215be | 2012-03-09 21:39:51 +0100 | [diff] [blame] | 8104 | if (uri_auth->uri_len > msg->sl.rq.u_l) |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 8105 | return 0; |
| 8106 | |
Willy Tarreau | 414e9bb | 2013-11-23 00:30:38 +0100 | [diff] [blame] | 8107 | if (memcmp(uri, uri_auth->uri_prefix, uri_auth->uri_len) != 0) |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 8108 | return 0; |
| 8109 | |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 8110 | return 1; |
| 8111 | } |
| 8112 | |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 8113 | /* |
| 8114 | * 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] | 8115 | * By default it tries to report the error position as msg->err_pos. However if |
| 8116 | * this one is not set, it will then report msg->next, which is the last known |
| 8117 | * 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] | 8118 | * displays buffers as a contiguous area starting at buf->p. |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 8119 | */ |
| 8120 | void http_capture_bad_message(struct error_snapshot *es, struct session *s, |
Willy Tarreau | 8a0cef2 | 2012-03-09 13:39:23 +0100 | [diff] [blame] | 8121 | struct http_msg *msg, |
Willy Tarreau | 3770f23 | 2013-12-07 00:01:53 +0100 | [diff] [blame] | 8122 | enum ht_state state, struct proxy *other_end) |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 8123 | { |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 8124 | struct channel *chn = msg->chn; |
Willy Tarreau | 69d8c5d | 2012-05-08 09:44:41 +0200 | [diff] [blame] | 8125 | int len1, len2; |
Willy Tarreau | 8a0cef2 | 2012-03-09 13:39:23 +0100 | [diff] [blame] | 8126 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 8127 | es->len = MIN(chn->buf->i, sizeof(es->buf)); |
| 8128 | len1 = chn->buf->data + chn->buf->size - chn->buf->p; |
Willy Tarreau | 69d8c5d | 2012-05-08 09:44:41 +0200 | [diff] [blame] | 8129 | len1 = MIN(len1, es->len); |
| 8130 | len2 = es->len - len1; /* remaining data if buffer wraps */ |
| 8131 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 8132 | memcpy(es->buf, chn->buf->p, len1); |
Willy Tarreau | 69d8c5d | 2012-05-08 09:44:41 +0200 | [diff] [blame] | 8133 | if (len2) |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 8134 | memcpy(es->buf + len1, chn->buf->data, len2); |
Willy Tarreau | 81f2fb9 | 2010-12-12 13:09:08 +0100 | [diff] [blame] | 8135 | |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 8136 | if (msg->err_pos >= 0) |
Willy Tarreau | 69d8c5d | 2012-05-08 09:44:41 +0200 | [diff] [blame] | 8137 | es->pos = msg->err_pos; |
Willy Tarreau | 81f2fb9 | 2010-12-12 13:09:08 +0100 | [diff] [blame] | 8138 | else |
Willy Tarreau | 69d8c5d | 2012-05-08 09:44:41 +0200 | [diff] [blame] | 8139 | es->pos = msg->next; |
Willy Tarreau | 81f2fb9 | 2010-12-12 13:09:08 +0100 | [diff] [blame] | 8140 | |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 8141 | es->when = date; // user-visible date |
| 8142 | es->sid = s->uniq_id; |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 8143 | es->srv = objt_server(s->target); |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 8144 | es->oe = other_end; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 8145 | if (objt_conn(s->req->prod->end)) |
| 8146 | es->src = __objt_conn(s->req->prod->end)->addr.from; |
| 8147 | else |
| 8148 | memset(&es->src, 0, sizeof(es->src)); |
| 8149 | |
Willy Tarreau | 078272e | 2010-12-12 12:46:33 +0100 | [diff] [blame] | 8150 | es->state = state; |
Willy Tarreau | 10479e4 | 2010-12-12 14:00:34 +0100 | [diff] [blame] | 8151 | es->ev_id = error_snapshot_id++; |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 8152 | es->b_flags = chn->flags; |
Willy Tarreau | d04b1bc | 2012-05-08 11:03:10 +0200 | [diff] [blame] | 8153 | es->s_flags = s->flags; |
| 8154 | es->t_flags = s->txn.flags; |
| 8155 | es->m_flags = msg->flags; |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 8156 | es->b_out = chn->buf->o; |
| 8157 | es->b_wrap = chn->buf->data + chn->buf->size - chn->buf->p; |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 8158 | es->b_tot = chn->total; |
Willy Tarreau | d04b1bc | 2012-05-08 11:03:10 +0200 | [diff] [blame] | 8159 | es->m_clen = msg->chunk_len; |
| 8160 | es->m_blen = msg->body_len; |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 8161 | } |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 8162 | |
Willy Tarreau | 294c473 | 2011-12-16 21:35:50 +0100 | [diff] [blame] | 8163 | /* Return in <vptr> and <vlen> the pointer and length of occurrence <occ> of |
| 8164 | * header whose name is <hname> of length <hlen>. If <ctx> is null, lookup is |
| 8165 | * performed over the whole headers. Otherwise it must contain a valid header |
| 8166 | * context, initialised with ctx->idx=0 for the first lookup in a series. If |
| 8167 | * <occ> is positive or null, occurrence #occ from the beginning (or last ctx) |
| 8168 | * is returned. Occ #0 and #1 are equivalent. If <occ> is negative (and no less |
| 8169 | * 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] | 8170 | * -1. The value fetch stops at commas, so this function is suited for use with |
| 8171 | * list headers. |
Willy Tarreau | 294c473 | 2011-12-16 21:35:50 +0100 | [diff] [blame] | 8172 | * 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] | 8173 | */ |
Willy Tarreau | 185b5c4 | 2012-04-26 15:11:51 +0200 | [diff] [blame] | 8174 | 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] | 8175 | struct hdr_idx *idx, int occ, |
| 8176 | struct hdr_ctx *ctx, char **vptr, int *vlen) |
Willy Tarreau | bce7088 | 2009-09-07 11:51:47 +0200 | [diff] [blame] | 8177 | { |
Willy Tarreau | 294c473 | 2011-12-16 21:35:50 +0100 | [diff] [blame] | 8178 | struct hdr_ctx local_ctx; |
| 8179 | char *ptr_hist[MAX_HDR_HISTORY]; |
| 8180 | int len_hist[MAX_HDR_HISTORY]; |
Willy Tarreau | bce7088 | 2009-09-07 11:51:47 +0200 | [diff] [blame] | 8181 | unsigned int hist_ptr; |
Willy Tarreau | 294c473 | 2011-12-16 21:35:50 +0100 | [diff] [blame] | 8182 | int found; |
Willy Tarreau | bce7088 | 2009-09-07 11:51:47 +0200 | [diff] [blame] | 8183 | |
Willy Tarreau | 294c473 | 2011-12-16 21:35:50 +0100 | [diff] [blame] | 8184 | if (!ctx) { |
| 8185 | local_ctx.idx = 0; |
| 8186 | ctx = &local_ctx; |
| 8187 | } |
| 8188 | |
Willy Tarreau | bce7088 | 2009-09-07 11:51:47 +0200 | [diff] [blame] | 8189 | if (occ >= 0) { |
Willy Tarreau | 294c473 | 2011-12-16 21:35:50 +0100 | [diff] [blame] | 8190 | /* search from the beginning */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 8191 | while (http_find_header2(hname, hlen, msg->chn->buf->p, idx, ctx)) { |
Willy Tarreau | bce7088 | 2009-09-07 11:51:47 +0200 | [diff] [blame] | 8192 | occ--; |
| 8193 | if (occ <= 0) { |
Willy Tarreau | 294c473 | 2011-12-16 21:35:50 +0100 | [diff] [blame] | 8194 | *vptr = ctx->line + ctx->val; |
| 8195 | *vlen = ctx->vlen; |
| 8196 | return 1; |
Willy Tarreau | bce7088 | 2009-09-07 11:51:47 +0200 | [diff] [blame] | 8197 | } |
| 8198 | } |
Willy Tarreau | 294c473 | 2011-12-16 21:35:50 +0100 | [diff] [blame] | 8199 | return 0; |
Willy Tarreau | bce7088 | 2009-09-07 11:51:47 +0200 | [diff] [blame] | 8200 | } |
| 8201 | |
| 8202 | /* negative occurrence, we scan all the list then walk back */ |
| 8203 | if (-occ > MAX_HDR_HISTORY) |
| 8204 | return 0; |
| 8205 | |
Willy Tarreau | 294c473 | 2011-12-16 21:35:50 +0100 | [diff] [blame] | 8206 | found = hist_ptr = 0; |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 8207 | while (http_find_header2(hname, hlen, msg->chn->buf->p, idx, ctx)) { |
Willy Tarreau | 294c473 | 2011-12-16 21:35:50 +0100 | [diff] [blame] | 8208 | ptr_hist[hist_ptr] = ctx->line + ctx->val; |
| 8209 | len_hist[hist_ptr] = ctx->vlen; |
| 8210 | if (++hist_ptr >= MAX_HDR_HISTORY) |
Willy Tarreau | bce7088 | 2009-09-07 11:51:47 +0200 | [diff] [blame] | 8211 | hist_ptr = 0; |
| 8212 | found++; |
| 8213 | } |
| 8214 | if (-occ > found) |
| 8215 | return 0; |
| 8216 | /* 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] | 8217 | * find occurrence -occ. 0 <= hist_ptr < MAX_HDR_HISTORY, and we have |
| 8218 | * -10 <= occ <= -1. So we have to check [hist_ptr%MAX_HDR_HISTORY+occ] |
| 8219 | * to remain in the 0..9 range. |
Willy Tarreau | bce7088 | 2009-09-07 11:51:47 +0200 | [diff] [blame] | 8220 | */ |
Willy Tarreau | 67dad27 | 2013-06-12 22:27:44 +0200 | [diff] [blame] | 8221 | hist_ptr += occ + MAX_HDR_HISTORY; |
Willy Tarreau | bce7088 | 2009-09-07 11:51:47 +0200 | [diff] [blame] | 8222 | if (hist_ptr >= MAX_HDR_HISTORY) |
| 8223 | hist_ptr -= MAX_HDR_HISTORY; |
Willy Tarreau | 294c473 | 2011-12-16 21:35:50 +0100 | [diff] [blame] | 8224 | *vptr = ptr_hist[hist_ptr]; |
| 8225 | *vlen = len_hist[hist_ptr]; |
| 8226 | return 1; |
Willy Tarreau | bce7088 | 2009-09-07 11:51:47 +0200 | [diff] [blame] | 8227 | } |
| 8228 | |
Willy Tarreau | 04ff9f1 | 2013-06-10 18:39:42 +0200 | [diff] [blame] | 8229 | /* Return in <vptr> and <vlen> the pointer and length of occurrence <occ> of |
| 8230 | * header whose name is <hname> of length <hlen>. If <ctx> is null, lookup is |
| 8231 | * performed over the whole headers. Otherwise it must contain a valid header |
| 8232 | * context, initialised with ctx->idx=0 for the first lookup in a series. If |
| 8233 | * <occ> is positive or null, occurrence #occ from the beginning (or last ctx) |
| 8234 | * is returned. Occ #0 and #1 are equivalent. If <occ> is negative (and no less |
| 8235 | * than -MAX_HDR_HISTORY), the occurrence is counted from the last one which is |
| 8236 | * -1. This function differs from http_get_hdr() in that it only returns full |
| 8237 | * line header values and does not stop at commas. |
| 8238 | * The return value is 0 if nothing was found, or non-zero otherwise. |
| 8239 | */ |
| 8240 | unsigned int http_get_fhdr(const struct http_msg *msg, const char *hname, int hlen, |
| 8241 | struct hdr_idx *idx, int occ, |
| 8242 | struct hdr_ctx *ctx, char **vptr, int *vlen) |
| 8243 | { |
| 8244 | struct hdr_ctx local_ctx; |
| 8245 | char *ptr_hist[MAX_HDR_HISTORY]; |
| 8246 | int len_hist[MAX_HDR_HISTORY]; |
| 8247 | unsigned int hist_ptr; |
| 8248 | int found; |
| 8249 | |
| 8250 | if (!ctx) { |
| 8251 | local_ctx.idx = 0; |
| 8252 | ctx = &local_ctx; |
| 8253 | } |
| 8254 | |
| 8255 | if (occ >= 0) { |
| 8256 | /* search from the beginning */ |
| 8257 | while (http_find_full_header2(hname, hlen, msg->chn->buf->p, idx, ctx)) { |
| 8258 | occ--; |
| 8259 | if (occ <= 0) { |
| 8260 | *vptr = ctx->line + ctx->val; |
| 8261 | *vlen = ctx->vlen; |
| 8262 | return 1; |
| 8263 | } |
| 8264 | } |
| 8265 | return 0; |
| 8266 | } |
| 8267 | |
| 8268 | /* negative occurrence, we scan all the list then walk back */ |
| 8269 | if (-occ > MAX_HDR_HISTORY) |
| 8270 | return 0; |
| 8271 | |
| 8272 | found = hist_ptr = 0; |
| 8273 | while (http_find_full_header2(hname, hlen, msg->chn->buf->p, idx, ctx)) { |
| 8274 | ptr_hist[hist_ptr] = ctx->line + ctx->val; |
| 8275 | len_hist[hist_ptr] = ctx->vlen; |
| 8276 | if (++hist_ptr >= MAX_HDR_HISTORY) |
| 8277 | hist_ptr = 0; |
| 8278 | found++; |
| 8279 | } |
| 8280 | if (-occ > found) |
| 8281 | return 0; |
| 8282 | /* OK now we have the last occurrence in [hist_ptr-1], and we need to |
| 8283 | * find occurrence -occ, so we have to check [hist_ptr+occ]. |
| 8284 | */ |
| 8285 | hist_ptr += occ; |
| 8286 | if (hist_ptr >= MAX_HDR_HISTORY) |
| 8287 | hist_ptr -= MAX_HDR_HISTORY; |
| 8288 | *vptr = ptr_hist[hist_ptr]; |
| 8289 | *vlen = len_hist[hist_ptr]; |
| 8290 | return 1; |
| 8291 | } |
| 8292 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 8293 | /* |
Willy Tarreau | e92693a | 2012-09-24 21:13:39 +0200 | [diff] [blame] | 8294 | * Print a debug line with a header. Always stop at the first CR or LF char, |
| 8295 | * so it is safe to pass it a full buffer if needed. If <err> is not NULL, an |
| 8296 | * arrow is printed after the line which contains the pointer. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 8297 | */ |
| 8298 | void debug_hdr(const char *dir, struct session *t, const char *start, const char *end) |
| 8299 | { |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 8300 | int max; |
| 8301 | 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] | 8302 | dir, |
| 8303 | objt_conn(t->req->prod->end) ? (unsigned short)objt_conn(t->req->prod->end)->t.sock.fd : -1, |
| 8304 | 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] | 8305 | |
| 8306 | for (max = 0; start + max < end; max++) |
| 8307 | if (start[max] == '\r' || start[max] == '\n') |
| 8308 | break; |
| 8309 | |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 8310 | UBOUND(max, trash.size - trash.len - 3); |
| 8311 | trash.len += strlcpy2(trash.str + trash.len, start, max + 1); |
| 8312 | trash.str[trash.len++] = '\n'; |
Willy Tarreau | 89efaed | 2013-12-13 15:14:55 +0100 | [diff] [blame] | 8313 | shut_your_big_mouth_gcc(write(1, trash.str, trash.len)); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 8314 | } |
| 8315 | |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 8316 | /* |
| 8317 | * Initialize a new HTTP transaction for session <s>. It is assumed that all |
| 8318 | * the required fields are properly allocated and that we only need to (re)init |
| 8319 | * them. This should be used before processing any new request. |
| 8320 | */ |
| 8321 | void http_init_txn(struct session *s) |
| 8322 | { |
| 8323 | struct http_txn *txn = &s->txn; |
| 8324 | struct proxy *fe = s->fe; |
| 8325 | |
| 8326 | txn->flags = 0; |
| 8327 | txn->status = -1; |
| 8328 | |
Willy Tarreau | f64d141 | 2010-10-07 20:06:11 +0200 | [diff] [blame] | 8329 | txn->cookie_first_date = 0; |
| 8330 | txn->cookie_last_date = 0; |
| 8331 | |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 8332 | txn->req.flags = 0; |
Willy Tarreau | 2692736 | 2012-05-18 23:22:52 +0200 | [diff] [blame] | 8333 | 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] | 8334 | txn->req.next = 0; |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 8335 | txn->rsp.flags = 0; |
Willy Tarreau | 2692736 | 2012-05-18 23:22:52 +0200 | [diff] [blame] | 8336 | 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] | 8337 | txn->rsp.next = 0; |
Willy Tarreau | 124d991 | 2011-03-01 20:30:48 +0100 | [diff] [blame] | 8338 | txn->req.chunk_len = 0LL; |
| 8339 | txn->req.body_len = 0LL; |
| 8340 | txn->rsp.chunk_len = 0LL; |
| 8341 | txn->rsp.body_len = 0LL; |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 8342 | txn->req.msg_state = HTTP_MSG_RQBEFORE; /* at the very beginning of the request */ |
| 8343 | 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] | 8344 | txn->req.chn = s->req; |
| 8345 | txn->rsp.chn = s->rep; |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 8346 | |
| 8347 | txn->auth.method = HTTP_AUTH_UNKNOWN; |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 8348 | |
| 8349 | txn->req.err_pos = txn->rsp.err_pos = -2; /* block buggy requests/responses */ |
| 8350 | if (fe->options2 & PR_O2_REQBUG_OK) |
| 8351 | txn->req.err_pos = -1; /* let buggy requests pass */ |
| 8352 | |
Willy Tarreau | 4602363 | 2010-01-07 22:51:47 +0100 | [diff] [blame] | 8353 | if (txn->req.cap) |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 8354 | memset(txn->req.cap, 0, fe->nb_req_cap * sizeof(void *)); |
| 8355 | |
Willy Tarreau | 4602363 | 2010-01-07 22:51:47 +0100 | [diff] [blame] | 8356 | if (txn->rsp.cap) |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 8357 | memset(txn->rsp.cap, 0, fe->nb_rsp_cap * sizeof(void *)); |
| 8358 | |
| 8359 | if (txn->hdr_idx.v) |
| 8360 | hdr_idx_init(&txn->hdr_idx); |
| 8361 | } |
| 8362 | |
| 8363 | /* to be used at the end of a transaction */ |
| 8364 | void http_end_txn(struct session *s) |
| 8365 | { |
| 8366 | struct http_txn *txn = &s->txn; |
| 8367 | |
Willy Tarreau | 7519560 | 2014-03-11 15:48:55 +0100 | [diff] [blame] | 8368 | /* release any possible compression context */ |
| 8369 | if (s->flags & SN_COMP_READY) |
| 8370 | s->comp_algo->end(&s->comp_ctx); |
| 8371 | s->comp_algo = NULL; |
| 8372 | s->flags &= ~SN_COMP_READY; |
| 8373 | |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 8374 | /* these ones will have been dynamically allocated */ |
| 8375 | pool_free2(pool2_requri, txn->uri); |
| 8376 | pool_free2(pool2_capture, txn->cli_cookie); |
| 8377 | pool_free2(pool2_capture, txn->srv_cookie); |
Willy Tarreau | a3377ee | 2010-01-10 10:49:11 +0100 | [diff] [blame] | 8378 | pool_free2(apools.sessid, txn->sessid); |
William Lallemand | a73203e | 2012-03-12 12:48:57 +0100 | [diff] [blame] | 8379 | pool_free2(pool2_uniqueid, s->unique_id); |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 8380 | |
William Lallemand | a73203e | 2012-03-12 12:48:57 +0100 | [diff] [blame] | 8381 | s->unique_id = NULL; |
Willy Tarreau | a3377ee | 2010-01-10 10:49:11 +0100 | [diff] [blame] | 8382 | txn->sessid = NULL; |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 8383 | txn->uri = NULL; |
| 8384 | txn->srv_cookie = NULL; |
| 8385 | txn->cli_cookie = NULL; |
Willy Tarreau | 4602363 | 2010-01-07 22:51:47 +0100 | [diff] [blame] | 8386 | |
| 8387 | if (txn->req.cap) { |
| 8388 | struct cap_hdr *h; |
| 8389 | for (h = s->fe->req_cap; h; h = h->next) |
| 8390 | pool_free2(h->pool, txn->req.cap[h->index]); |
| 8391 | memset(txn->req.cap, 0, s->fe->nb_req_cap * sizeof(void *)); |
| 8392 | } |
| 8393 | |
| 8394 | if (txn->rsp.cap) { |
| 8395 | struct cap_hdr *h; |
| 8396 | for (h = s->fe->rsp_cap; h; h = h->next) |
| 8397 | pool_free2(h->pool, txn->rsp.cap[h->index]); |
| 8398 | memset(txn->rsp.cap, 0, s->fe->nb_rsp_cap * sizeof(void *)); |
| 8399 | } |
| 8400 | |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 8401 | } |
| 8402 | |
| 8403 | /* to be used at the end of a transaction to prepare a new one */ |
| 8404 | void http_reset_txn(struct session *s) |
| 8405 | { |
| 8406 | http_end_txn(s); |
| 8407 | http_init_txn(s); |
| 8408 | |
| 8409 | s->be = s->fe; |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 8410 | s->logs.logwait = s->fe->to_log; |
Willy Tarreau | abcd514 | 2013-06-11 17:18:02 +0200 | [diff] [blame] | 8411 | s->logs.level = 0; |
Simon Horman | af51495 | 2011-06-21 14:34:57 +0900 | [diff] [blame] | 8412 | session_del_srv_conn(s); |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 8413 | s->target = NULL; |
Emeric Brun | b982a3d | 2010-01-04 15:45:53 +0100 | [diff] [blame] | 8414 | /* re-init store persistence */ |
| 8415 | s->store_count = 0; |
Willy Tarreau | 1f0da24 | 2014-01-25 11:01:50 +0100 | [diff] [blame] | 8416 | s->uniq_id = global.req_count++; |
Emeric Brun | b982a3d | 2010-01-04 15:45:53 +0100 | [diff] [blame] | 8417 | |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 8418 | s->pend_pos = NULL; |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 8419 | |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 8420 | s->req->flags |= CF_READ_DONTWAIT; /* one read is usually enough */ |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 8421 | |
Willy Tarreau | 739cfba | 2010-01-25 23:11:14 +0100 | [diff] [blame] | 8422 | /* We must trim any excess data from the response buffer, because we |
| 8423 | * may have blocked an invalid response from a server that we don't |
| 8424 | * want to accidentely forward once we disable the analysers, nor do |
| 8425 | * we want those data to come along with next response. A typical |
| 8426 | * example of such data would be from a buggy server responding to |
| 8427 | * a HEAD with some data, or sending more than the advertised |
| 8428 | * content-length. |
| 8429 | */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 8430 | if (unlikely(s->rep->buf->i)) |
| 8431 | s->rep->buf->i = 0; |
Willy Tarreau | 739cfba | 2010-01-25 23:11:14 +0100 | [diff] [blame] | 8432 | |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 8433 | s->req->rto = s->fe->timeout.client; |
Willy Tarreau | d04e858 | 2010-05-31 12:31:35 +0200 | [diff] [blame] | 8434 | s->req->wto = TICK_ETERNITY; |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 8435 | |
Willy Tarreau | d04e858 | 2010-05-31 12:31:35 +0200 | [diff] [blame] | 8436 | s->rep->rto = TICK_ETERNITY; |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 8437 | s->rep->wto = s->fe->timeout.client; |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 8438 | |
| 8439 | s->req->rex = TICK_ETERNITY; |
| 8440 | s->req->wex = TICK_ETERNITY; |
| 8441 | s->req->analyse_exp = TICK_ETERNITY; |
| 8442 | s->rep->rex = TICK_ETERNITY; |
| 8443 | s->rep->wex = TICK_ETERNITY; |
| 8444 | s->rep->analyse_exp = TICK_ETERNITY; |
| 8445 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 8446 | |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 8447 | void free_http_req_rules(struct list *r) { |
| 8448 | struct http_req_rule *tr, *pr; |
| 8449 | |
| 8450 | list_for_each_entry_safe(pr, tr, r, list) { |
| 8451 | LIST_DEL(&pr->list); |
Willy Tarreau | 20b0de5 | 2012-12-24 15:45:22 +0100 | [diff] [blame] | 8452 | if (pr->action == HTTP_REQ_ACT_AUTH) |
Willy Tarreau | 5c2e198 | 2012-12-24 12:00:25 +0100 | [diff] [blame] | 8453 | free(pr->arg.auth.realm); |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 8454 | |
| 8455 | free(pr); |
| 8456 | } |
| 8457 | } |
| 8458 | |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 8459 | /* parse an "http-request" rule */ |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 8460 | struct http_req_rule *parse_http_req_cond(const char **args, const char *file, int linenum, struct proxy *proxy) |
| 8461 | { |
| 8462 | struct http_req_rule *rule; |
| 8463 | int cur_arg; |
| 8464 | |
| 8465 | rule = (struct http_req_rule*)calloc(1, sizeof(struct http_req_rule)); |
| 8466 | if (!rule) { |
| 8467 | Alert("parsing [%s:%d]: out of memory.\n", file, linenum); |
Willy Tarreau | 81499eb | 2012-12-27 12:19:02 +0100 | [diff] [blame] | 8468 | goto out_err; |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 8469 | } |
| 8470 | |
Willy Tarreau | 5c2e198 | 2012-12-24 12:00:25 +0100 | [diff] [blame] | 8471 | if (!strcmp(args[0], "allow")) { |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 8472 | rule->action = HTTP_REQ_ACT_ALLOW; |
| 8473 | cur_arg = 1; |
| 8474 | } else if (!strcmp(args[0], "deny")) { |
| 8475 | rule->action = HTTP_REQ_ACT_DENY; |
| 8476 | cur_arg = 1; |
Willy Tarreau | ccbcc37 | 2012-12-27 12:37:57 +0100 | [diff] [blame] | 8477 | } else if (!strcmp(args[0], "tarpit")) { |
| 8478 | rule->action = HTTP_REQ_ACT_TARPIT; |
| 8479 | cur_arg = 1; |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 8480 | } else if (!strcmp(args[0], "auth")) { |
Willy Tarreau | 20b0de5 | 2012-12-24 15:45:22 +0100 | [diff] [blame] | 8481 | rule->action = HTTP_REQ_ACT_AUTH; |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 8482 | cur_arg = 1; |
| 8483 | |
| 8484 | while(*args[cur_arg]) { |
| 8485 | if (!strcmp(args[cur_arg], "realm")) { |
Willy Tarreau | 5c2e198 | 2012-12-24 12:00:25 +0100 | [diff] [blame] | 8486 | rule->arg.auth.realm = strdup(args[cur_arg + 1]); |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 8487 | cur_arg+=2; |
| 8488 | continue; |
| 8489 | } else |
| 8490 | break; |
| 8491 | } |
Willy Tarreau | f4c43c1 | 2013-06-11 17:01:13 +0200 | [diff] [blame] | 8492 | } else if (!strcmp(args[0], "set-nice")) { |
| 8493 | rule->action = HTTP_REQ_ACT_SET_NICE; |
| 8494 | cur_arg = 1; |
| 8495 | |
| 8496 | if (!*args[cur_arg] || |
| 8497 | (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) { |
| 8498 | Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (integer value).\n", |
| 8499 | file, linenum, args[0]); |
| 8500 | goto out_err; |
| 8501 | } |
| 8502 | rule->arg.nice = atoi(args[cur_arg]); |
| 8503 | if (rule->arg.nice < -1024) |
| 8504 | rule->arg.nice = -1024; |
| 8505 | else if (rule->arg.nice > 1024) |
| 8506 | rule->arg.nice = 1024; |
| 8507 | cur_arg++; |
Willy Tarreau | 42cf39e | 2013-06-11 18:51:32 +0200 | [diff] [blame] | 8508 | } else if (!strcmp(args[0], "set-tos")) { |
| 8509 | #ifdef IP_TOS |
| 8510 | char *err; |
| 8511 | rule->action = HTTP_REQ_ACT_SET_TOS; |
| 8512 | cur_arg = 1; |
| 8513 | |
| 8514 | if (!*args[cur_arg] || |
| 8515 | (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) { |
| 8516 | Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (integer/hex value).\n", |
| 8517 | file, linenum, args[0]); |
| 8518 | goto out_err; |
| 8519 | } |
| 8520 | |
| 8521 | rule->arg.tos = strtol(args[cur_arg], &err, 0); |
| 8522 | if (err && *err != '\0') { |
| 8523 | Alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-request %s' (integer/hex value expected).\n", |
| 8524 | file, linenum, err, args[0]); |
| 8525 | goto out_err; |
| 8526 | } |
| 8527 | cur_arg++; |
| 8528 | #else |
| 8529 | Alert("parsing [%s:%d]: 'http-request %s' is not supported on this platform (IP_TOS undefined).\n", file, linenum, args[0]); |
| 8530 | goto out_err; |
| 8531 | #endif |
Willy Tarreau | 51347ed | 2013-06-11 19:34:13 +0200 | [diff] [blame] | 8532 | } else if (!strcmp(args[0], "set-mark")) { |
| 8533 | #ifdef SO_MARK |
| 8534 | char *err; |
| 8535 | rule->action = HTTP_REQ_ACT_SET_MARK; |
| 8536 | cur_arg = 1; |
| 8537 | |
| 8538 | if (!*args[cur_arg] || |
| 8539 | (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) { |
| 8540 | Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (integer/hex value).\n", |
| 8541 | file, linenum, args[0]); |
| 8542 | goto out_err; |
| 8543 | } |
| 8544 | |
| 8545 | rule->arg.mark = strtoul(args[cur_arg], &err, 0); |
| 8546 | if (err && *err != '\0') { |
| 8547 | Alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-request %s' (integer/hex value expected).\n", |
| 8548 | file, linenum, err, args[0]); |
| 8549 | goto out_err; |
| 8550 | } |
| 8551 | cur_arg++; |
| 8552 | global.last_checks |= LSTCHK_NETADM; |
| 8553 | #else |
| 8554 | Alert("parsing [%s:%d]: 'http-request %s' is not supported on this platform (SO_MARK undefined).\n", file, linenum, args[0]); |
| 8555 | goto out_err; |
| 8556 | #endif |
Willy Tarreau | 9a355ec | 2013-06-11 17:45:46 +0200 | [diff] [blame] | 8557 | } else if (!strcmp(args[0], "set-log-level")) { |
| 8558 | rule->action = HTTP_REQ_ACT_SET_LOGL; |
| 8559 | cur_arg = 1; |
| 8560 | |
| 8561 | if (!*args[cur_arg] || |
| 8562 | (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) { |
| 8563 | bad_log_level: |
| 8564 | Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (log level name or 'silent').\n", |
| 8565 | file, linenum, args[0]); |
| 8566 | goto out_err; |
| 8567 | } |
| 8568 | if (strcmp(args[cur_arg], "silent") == 0) |
| 8569 | rule->arg.loglevel = -1; |
| 8570 | else if ((rule->arg.loglevel = get_log_level(args[cur_arg]) + 1) == 0) |
| 8571 | goto bad_log_level; |
| 8572 | cur_arg++; |
Willy Tarreau | 20b0de5 | 2012-12-24 15:45:22 +0100 | [diff] [blame] | 8573 | } else if (strcmp(args[0], "add-header") == 0 || strcmp(args[0], "set-header") == 0) { |
| 8574 | rule->action = *args[0] == 'a' ? HTTP_REQ_ACT_ADD_HDR : HTTP_REQ_ACT_SET_HDR; |
| 8575 | cur_arg = 1; |
| 8576 | |
Willy Tarreau | 8d1c516 | 2013-04-03 14:13:58 +0200 | [diff] [blame] | 8577 | if (!*args[cur_arg] || !*args[cur_arg+1] || |
| 8578 | (*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] | 8579 | Alert("parsing [%s:%d]: 'http-request %s' expects exactly 2 arguments.\n", |
| 8580 | file, linenum, args[0]); |
Willy Tarreau | 81499eb | 2012-12-27 12:19:02 +0100 | [diff] [blame] | 8581 | goto out_err; |
Willy Tarreau | 20b0de5 | 2012-12-24 15:45:22 +0100 | [diff] [blame] | 8582 | } |
| 8583 | |
| 8584 | rule->arg.hdr_add.name = strdup(args[cur_arg]); |
| 8585 | rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name); |
| 8586 | LIST_INIT(&rule->arg.hdr_add.fmt); |
Willy Tarreau | a4312fa | 2013-04-02 16:34:32 +0200 | [diff] [blame] | 8587 | |
Thierry FOURNIER | 1c0054f | 2013-11-20 15:09:52 +0100 | [diff] [blame] | 8588 | proxy->conf.args.ctx = ARGC_HRQ; |
Thierry FOURNIER | d048d8b | 2014-03-13 16:46:18 +0100 | [diff] [blame] | 8589 | parse_logformat_string(args[cur_arg + 1], proxy, &rule->arg.hdr_add.fmt, LOG_OPT_HTTP, |
Thierry FOURNIER | eeaa951 | 2014-02-11 14:00:19 +0100 | [diff] [blame] | 8590 | (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR, |
| 8591 | file, linenum); |
Willy Tarreau | 59ad1a2 | 2014-01-29 14:39:58 +0100 | [diff] [blame] | 8592 | free(proxy->conf.lfs_file); |
| 8593 | proxy->conf.lfs_file = strdup(proxy->conf.args.file); |
| 8594 | proxy->conf.lfs_line = proxy->conf.args.line; |
Willy Tarreau | 20b0de5 | 2012-12-24 15:45:22 +0100 | [diff] [blame] | 8595 | cur_arg += 2; |
Thierry FOURNIER | dad3d1d | 2014-04-22 18:07:25 +0200 | [diff] [blame] | 8596 | } else if (strcmp(args[0], "del-header") == 0) { |
| 8597 | rule->action = HTTP_REQ_ACT_DEL_HDR; |
| 8598 | cur_arg = 1; |
| 8599 | |
| 8600 | if (!*args[cur_arg] || |
| 8601 | (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) { |
| 8602 | Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument.\n", |
| 8603 | file, linenum, args[0]); |
| 8604 | goto out_err; |
| 8605 | } |
| 8606 | |
| 8607 | rule->arg.hdr_add.name = strdup(args[cur_arg]); |
| 8608 | rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name); |
| 8609 | |
| 8610 | proxy->conf.args.ctx = ARGC_HRQ; |
| 8611 | free(proxy->conf.lfs_file); |
| 8612 | proxy->conf.lfs_file = strdup(proxy->conf.args.file); |
| 8613 | proxy->conf.lfs_line = proxy->conf.args.line; |
| 8614 | cur_arg += 1; |
Willy Tarreau | 81499eb | 2012-12-27 12:19:02 +0100 | [diff] [blame] | 8615 | } else if (strcmp(args[0], "redirect") == 0) { |
| 8616 | struct redirect_rule *redir; |
Willy Tarreau | 6d4890c | 2013-11-18 18:04:25 +0100 | [diff] [blame] | 8617 | char *errmsg = NULL; |
Willy Tarreau | 81499eb | 2012-12-27 12:19:02 +0100 | [diff] [blame] | 8618 | |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 8619 | 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] | 8620 | Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : %s.\n", |
| 8621 | file, linenum, proxy_type_str(proxy), proxy->id, args[0], errmsg); |
| 8622 | goto out_err; |
| 8623 | } |
| 8624 | |
| 8625 | /* this redirect rule might already contain a parsed condition which |
| 8626 | * we'll pass to the http-request rule. |
| 8627 | */ |
| 8628 | rule->action = HTTP_REQ_ACT_REDIR; |
| 8629 | rule->arg.redir = redir; |
| 8630 | rule->cond = redir->cond; |
| 8631 | redir->cond = NULL; |
| 8632 | cur_arg = 2; |
| 8633 | return rule; |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 8634 | } else { |
Willy Tarreau | 51347ed | 2013-06-11 19:34:13 +0200 | [diff] [blame] | 8635 | 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] | 8636 | file, linenum, args[0], *args[0] ? "" : " (missing argument)"); |
Willy Tarreau | 81499eb | 2012-12-27 12:19:02 +0100 | [diff] [blame] | 8637 | goto out_err; |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 8638 | } |
| 8639 | |
| 8640 | if (strcmp(args[cur_arg], "if") == 0 || strcmp(args[cur_arg], "unless") == 0) { |
| 8641 | struct acl_cond *cond; |
Willy Tarreau | b7451bb | 2012-04-27 12:38:15 +0200 | [diff] [blame] | 8642 | char *errmsg = NULL; |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 8643 | |
Willy Tarreau | b7451bb | 2012-04-27 12:38:15 +0200 | [diff] [blame] | 8644 | if ((cond = build_acl_cond(file, linenum, proxy, args+cur_arg, &errmsg)) == NULL) { |
| 8645 | Alert("parsing [%s:%d] : error detected while parsing an 'http-request %s' condition : %s.\n", |
| 8646 | file, linenum, args[0], errmsg); |
| 8647 | free(errmsg); |
Willy Tarreau | 81499eb | 2012-12-27 12:19:02 +0100 | [diff] [blame] | 8648 | goto out_err; |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 8649 | } |
| 8650 | rule->cond = cond; |
| 8651 | } |
| 8652 | else if (*args[cur_arg]) { |
| 8653 | Alert("parsing [%s:%d]: 'http-request %s' expects 'realm' for 'auth' or" |
| 8654 | " either 'if' or 'unless' followed by a condition but found '%s'.\n", |
| 8655 | file, linenum, args[0], args[cur_arg]); |
Willy Tarreau | 81499eb | 2012-12-27 12:19:02 +0100 | [diff] [blame] | 8656 | goto out_err; |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 8657 | } |
| 8658 | |
| 8659 | return rule; |
Willy Tarreau | 81499eb | 2012-12-27 12:19:02 +0100 | [diff] [blame] | 8660 | out_err: |
| 8661 | free(rule); |
| 8662 | return NULL; |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 8663 | } |
| 8664 | |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 8665 | /* parse an "http-respose" rule */ |
| 8666 | struct http_res_rule *parse_http_res_cond(const char **args, const char *file, int linenum, struct proxy *proxy) |
| 8667 | { |
| 8668 | struct http_res_rule *rule; |
| 8669 | int cur_arg; |
| 8670 | |
| 8671 | rule = calloc(1, sizeof(*rule)); |
| 8672 | if (!rule) { |
| 8673 | Alert("parsing [%s:%d]: out of memory.\n", file, linenum); |
| 8674 | goto out_err; |
| 8675 | } |
| 8676 | |
| 8677 | if (!strcmp(args[0], "allow")) { |
| 8678 | rule->action = HTTP_RES_ACT_ALLOW; |
| 8679 | cur_arg = 1; |
| 8680 | } else if (!strcmp(args[0], "deny")) { |
| 8681 | rule->action = HTTP_RES_ACT_DENY; |
| 8682 | cur_arg = 1; |
Willy Tarreau | f4c43c1 | 2013-06-11 17:01:13 +0200 | [diff] [blame] | 8683 | } else if (!strcmp(args[0], "set-nice")) { |
| 8684 | rule->action = HTTP_RES_ACT_SET_NICE; |
| 8685 | cur_arg = 1; |
| 8686 | |
| 8687 | if (!*args[cur_arg] || |
| 8688 | (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) { |
| 8689 | Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (integer value).\n", |
| 8690 | file, linenum, args[0]); |
| 8691 | goto out_err; |
| 8692 | } |
| 8693 | rule->arg.nice = atoi(args[cur_arg]); |
| 8694 | if (rule->arg.nice < -1024) |
| 8695 | rule->arg.nice = -1024; |
| 8696 | else if (rule->arg.nice > 1024) |
| 8697 | rule->arg.nice = 1024; |
| 8698 | cur_arg++; |
Willy Tarreau | 42cf39e | 2013-06-11 18:51:32 +0200 | [diff] [blame] | 8699 | } else if (!strcmp(args[0], "set-tos")) { |
| 8700 | #ifdef IP_TOS |
| 8701 | char *err; |
| 8702 | rule->action = HTTP_RES_ACT_SET_TOS; |
| 8703 | cur_arg = 1; |
| 8704 | |
| 8705 | if (!*args[cur_arg] || |
| 8706 | (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) { |
| 8707 | Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (integer/hex value).\n", |
| 8708 | file, linenum, args[0]); |
| 8709 | goto out_err; |
| 8710 | } |
| 8711 | |
| 8712 | rule->arg.tos = strtol(args[cur_arg], &err, 0); |
| 8713 | if (err && *err != '\0') { |
| 8714 | Alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-response %s' (integer/hex value expected).\n", |
| 8715 | file, linenum, err, args[0]); |
| 8716 | goto out_err; |
| 8717 | } |
| 8718 | cur_arg++; |
| 8719 | #else |
| 8720 | Alert("parsing [%s:%d]: 'http-response %s' is not supported on this platform (IP_TOS undefined).\n", file, linenum, args[0]); |
| 8721 | goto out_err; |
| 8722 | #endif |
Willy Tarreau | 51347ed | 2013-06-11 19:34:13 +0200 | [diff] [blame] | 8723 | } else if (!strcmp(args[0], "set-mark")) { |
| 8724 | #ifdef SO_MARK |
| 8725 | char *err; |
| 8726 | rule->action = HTTP_RES_ACT_SET_MARK; |
| 8727 | cur_arg = 1; |
| 8728 | |
| 8729 | if (!*args[cur_arg] || |
| 8730 | (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) { |
| 8731 | Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (integer/hex value).\n", |
| 8732 | file, linenum, args[0]); |
| 8733 | goto out_err; |
| 8734 | } |
| 8735 | |
| 8736 | rule->arg.mark = strtoul(args[cur_arg], &err, 0); |
| 8737 | if (err && *err != '\0') { |
| 8738 | Alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-response %s' (integer/hex value expected).\n", |
| 8739 | file, linenum, err, args[0]); |
| 8740 | goto out_err; |
| 8741 | } |
| 8742 | cur_arg++; |
| 8743 | global.last_checks |= LSTCHK_NETADM; |
| 8744 | #else |
| 8745 | Alert("parsing [%s:%d]: 'http-response %s' is not supported on this platform (SO_MARK undefined).\n", file, linenum, args[0]); |
| 8746 | goto out_err; |
| 8747 | #endif |
Willy Tarreau | 9a355ec | 2013-06-11 17:45:46 +0200 | [diff] [blame] | 8748 | } else if (!strcmp(args[0], "set-log-level")) { |
| 8749 | rule->action = HTTP_RES_ACT_SET_LOGL; |
| 8750 | cur_arg = 1; |
| 8751 | |
| 8752 | if (!*args[cur_arg] || |
| 8753 | (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) { |
| 8754 | bad_log_level: |
| 8755 | Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (log level name or 'silent').\n", |
| 8756 | file, linenum, args[0]); |
| 8757 | goto out_err; |
| 8758 | } |
| 8759 | if (strcmp(args[cur_arg], "silent") == 0) |
| 8760 | rule->arg.loglevel = -1; |
| 8761 | else if ((rule->arg.loglevel = get_log_level(args[cur_arg] + 1)) == 0) |
| 8762 | goto bad_log_level; |
| 8763 | cur_arg++; |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 8764 | } else if (strcmp(args[0], "add-header") == 0 || strcmp(args[0], "set-header") == 0) { |
| 8765 | rule->action = *args[0] == 'a' ? HTTP_RES_ACT_ADD_HDR : HTTP_RES_ACT_SET_HDR; |
| 8766 | cur_arg = 1; |
| 8767 | |
| 8768 | if (!*args[cur_arg] || !*args[cur_arg+1] || |
| 8769 | (*args[cur_arg+2] && strcmp(args[cur_arg+2], "if") != 0 && strcmp(args[cur_arg+2], "unless") != 0)) { |
| 8770 | Alert("parsing [%s:%d]: 'http-response %s' expects exactly 2 arguments.\n", |
| 8771 | file, linenum, args[0]); |
| 8772 | goto out_err; |
| 8773 | } |
| 8774 | |
| 8775 | rule->arg.hdr_add.name = strdup(args[cur_arg]); |
| 8776 | rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name); |
| 8777 | LIST_INIT(&rule->arg.hdr_add.fmt); |
| 8778 | |
Thierry FOURNIER | 1c0054f | 2013-11-20 15:09:52 +0100 | [diff] [blame] | 8779 | proxy->conf.args.ctx = ARGC_HRS; |
Thierry FOURNIER | d048d8b | 2014-03-13 16:46:18 +0100 | [diff] [blame] | 8780 | parse_logformat_string(args[cur_arg + 1], proxy, &rule->arg.hdr_add.fmt, LOG_OPT_HTTP, |
Thierry FOURNIER | eeaa951 | 2014-02-11 14:00:19 +0100 | [diff] [blame] | 8781 | (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR, |
| 8782 | file, linenum); |
Willy Tarreau | 59ad1a2 | 2014-01-29 14:39:58 +0100 | [diff] [blame] | 8783 | free(proxy->conf.lfs_file); |
| 8784 | proxy->conf.lfs_file = strdup(proxy->conf.args.file); |
| 8785 | proxy->conf.lfs_line = proxy->conf.args.line; |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 8786 | cur_arg += 2; |
Thierry FOURNIER | dad3d1d | 2014-04-22 18:07:25 +0200 | [diff] [blame] | 8787 | } else if (strcmp(args[0], "del-header") == 0) { |
| 8788 | rule->action = HTTP_RES_ACT_DEL_HDR; |
| 8789 | cur_arg = 1; |
| 8790 | |
| 8791 | if (!*args[cur_arg] || |
| 8792 | (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) { |
| 8793 | Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument.\n", |
| 8794 | file, linenum, args[0]); |
| 8795 | goto out_err; |
| 8796 | } |
| 8797 | |
| 8798 | rule->arg.hdr_add.name = strdup(args[cur_arg]); |
| 8799 | rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name); |
| 8800 | |
| 8801 | proxy->conf.args.ctx = ARGC_HRS; |
| 8802 | free(proxy->conf.lfs_file); |
| 8803 | proxy->conf.lfs_file = strdup(proxy->conf.args.file); |
| 8804 | proxy->conf.lfs_line = proxy->conf.args.line; |
| 8805 | cur_arg += 1; |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 8806 | } else { |
Willy Tarreau | 51347ed | 2013-06-11 19:34:13 +0200 | [diff] [blame] | 8807 | 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] | 8808 | file, linenum, args[0], *args[0] ? "" : " (missing argument)"); |
| 8809 | goto out_err; |
| 8810 | } |
| 8811 | |
| 8812 | if (strcmp(args[cur_arg], "if") == 0 || strcmp(args[cur_arg], "unless") == 0) { |
| 8813 | struct acl_cond *cond; |
| 8814 | char *errmsg = NULL; |
| 8815 | |
| 8816 | if ((cond = build_acl_cond(file, linenum, proxy, args+cur_arg, &errmsg)) == NULL) { |
| 8817 | Alert("parsing [%s:%d] : error detected while parsing an 'http-response %s' condition : %s.\n", |
| 8818 | file, linenum, args[0], errmsg); |
| 8819 | free(errmsg); |
| 8820 | goto out_err; |
| 8821 | } |
| 8822 | rule->cond = cond; |
| 8823 | } |
| 8824 | else if (*args[cur_arg]) { |
| 8825 | Alert("parsing [%s:%d]: 'http-response %s' expects" |
| 8826 | " either 'if' or 'unless' followed by a condition but found '%s'.\n", |
| 8827 | file, linenum, args[0], args[cur_arg]); |
| 8828 | goto out_err; |
| 8829 | } |
| 8830 | |
| 8831 | return rule; |
| 8832 | out_err: |
| 8833 | free(rule); |
| 8834 | return NULL; |
| 8835 | } |
| 8836 | |
Willy Tarreau | 4baae24 | 2012-12-27 12:00:31 +0100 | [diff] [blame] | 8837 | /* 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] | 8838 | * with <err> filled with the error message. If <use_fmt> is not null, builds a |
| 8839 | * dynamic log-format rule instead of a static string. |
Willy Tarreau | 4baae24 | 2012-12-27 12:00:31 +0100 | [diff] [blame] | 8840 | */ |
| 8841 | 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] | 8842 | const char **args, char **errmsg, int use_fmt) |
Willy Tarreau | 4baae24 | 2012-12-27 12:00:31 +0100 | [diff] [blame] | 8843 | { |
| 8844 | struct redirect_rule *rule; |
| 8845 | int cur_arg; |
| 8846 | int type = REDIRECT_TYPE_NONE; |
| 8847 | int code = 302; |
| 8848 | const char *destination = NULL; |
| 8849 | const char *cookie = NULL; |
| 8850 | int cookie_set = 0; |
| 8851 | unsigned int flags = REDIRECT_FLAG_NONE; |
| 8852 | struct acl_cond *cond = NULL; |
| 8853 | |
| 8854 | cur_arg = 0; |
| 8855 | while (*(args[cur_arg])) { |
| 8856 | if (strcmp(args[cur_arg], "location") == 0) { |
| 8857 | if (!*args[cur_arg + 1]) |
| 8858 | goto missing_arg; |
| 8859 | |
| 8860 | type = REDIRECT_TYPE_LOCATION; |
| 8861 | cur_arg++; |
| 8862 | destination = args[cur_arg]; |
| 8863 | } |
| 8864 | else if (strcmp(args[cur_arg], "prefix") == 0) { |
| 8865 | if (!*args[cur_arg + 1]) |
| 8866 | goto missing_arg; |
| 8867 | |
| 8868 | type = REDIRECT_TYPE_PREFIX; |
| 8869 | cur_arg++; |
| 8870 | destination = args[cur_arg]; |
| 8871 | } |
| 8872 | else if (strcmp(args[cur_arg], "scheme") == 0) { |
| 8873 | if (!*args[cur_arg + 1]) |
| 8874 | goto missing_arg; |
| 8875 | |
| 8876 | type = REDIRECT_TYPE_SCHEME; |
| 8877 | cur_arg++; |
| 8878 | destination = args[cur_arg]; |
| 8879 | } |
| 8880 | else if (strcmp(args[cur_arg], "set-cookie") == 0) { |
| 8881 | if (!*args[cur_arg + 1]) |
| 8882 | goto missing_arg; |
| 8883 | |
| 8884 | cur_arg++; |
| 8885 | cookie = args[cur_arg]; |
| 8886 | cookie_set = 1; |
| 8887 | } |
| 8888 | else if (strcmp(args[cur_arg], "clear-cookie") == 0) { |
| 8889 | if (!*args[cur_arg + 1]) |
| 8890 | goto missing_arg; |
| 8891 | |
| 8892 | cur_arg++; |
| 8893 | cookie = args[cur_arg]; |
| 8894 | cookie_set = 0; |
| 8895 | } |
| 8896 | else if (strcmp(args[cur_arg], "code") == 0) { |
| 8897 | if (!*args[cur_arg + 1]) |
| 8898 | goto missing_arg; |
| 8899 | |
| 8900 | cur_arg++; |
| 8901 | code = atol(args[cur_arg]); |
Yves Lafon | 3e8d1ae | 2013-03-11 11:06:05 -0400 | [diff] [blame] | 8902 | if (code < 301 || code > 308 || (code > 303 && code < 307)) { |
Willy Tarreau | 4baae24 | 2012-12-27 12:00:31 +0100 | [diff] [blame] | 8903 | memprintf(errmsg, |
Yves Lafon | 3e8d1ae | 2013-03-11 11:06:05 -0400 | [diff] [blame] | 8904 | "'%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] | 8905 | args[cur_arg - 1], args[cur_arg]); |
| 8906 | return NULL; |
| 8907 | } |
| 8908 | } |
| 8909 | else if (!strcmp(args[cur_arg],"drop-query")) { |
| 8910 | flags |= REDIRECT_FLAG_DROP_QS; |
| 8911 | } |
| 8912 | else if (!strcmp(args[cur_arg],"append-slash")) { |
| 8913 | flags |= REDIRECT_FLAG_APPEND_SLASH; |
| 8914 | } |
| 8915 | else if (strcmp(args[cur_arg], "if") == 0 || |
| 8916 | strcmp(args[cur_arg], "unless") == 0) { |
| 8917 | cond = build_acl_cond(file, linenum, curproxy, (const char **)args + cur_arg, errmsg); |
| 8918 | if (!cond) { |
| 8919 | memprintf(errmsg, "error in condition: %s", *errmsg); |
| 8920 | return NULL; |
| 8921 | } |
| 8922 | break; |
| 8923 | } |
| 8924 | else { |
| 8925 | memprintf(errmsg, |
| 8926 | "expects 'code', 'prefix', 'location', 'scheme', 'set-cookie', 'clear-cookie', 'drop-query' or 'append-slash' (was '%s')", |
| 8927 | args[cur_arg]); |
| 8928 | return NULL; |
| 8929 | } |
| 8930 | cur_arg++; |
| 8931 | } |
| 8932 | |
| 8933 | if (type == REDIRECT_TYPE_NONE) { |
| 8934 | memprintf(errmsg, "redirection type expected ('prefix', 'location', or 'scheme')"); |
| 8935 | return NULL; |
| 8936 | } |
| 8937 | |
| 8938 | rule = (struct redirect_rule *)calloc(1, sizeof(*rule)); |
| 8939 | rule->cond = cond; |
Willy Tarreau | 60e0838 | 2013-12-03 00:48:45 +0100 | [diff] [blame] | 8940 | LIST_INIT(&rule->rdr_fmt); |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 8941 | |
| 8942 | if (!use_fmt) { |
| 8943 | /* old-style static redirect rule */ |
| 8944 | rule->rdr_str = strdup(destination); |
| 8945 | rule->rdr_len = strlen(destination); |
| 8946 | } |
| 8947 | else { |
| 8948 | /* log-format based redirect rule */ |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 8949 | |
| 8950 | /* Parse destination. Note that in the REDIRECT_TYPE_PREFIX case, |
| 8951 | * if prefix == "/", we don't want to add anything, otherwise it |
| 8952 | * makes it hard for the user to configure a self-redirection. |
| 8953 | */ |
| 8954 | proxy->conf.args.ctx = ARGC_RDR; |
| 8955 | if (!(type == REDIRECT_TYPE_PREFIX && destination[0] == '/' && destination[1] == '\0')) { |
Thierry FOURNIER | d048d8b | 2014-03-13 16:46:18 +0100 | [diff] [blame] | 8956 | parse_logformat_string(destination, curproxy, &rule->rdr_fmt, LOG_OPT_HTTP, |
Thierry FOURNIER | eeaa951 | 2014-02-11 14:00:19 +0100 | [diff] [blame] | 8957 | (curproxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR, |
| 8958 | file, linenum); |
Willy Tarreau | 59ad1a2 | 2014-01-29 14:39:58 +0100 | [diff] [blame] | 8959 | free(curproxy->conf.lfs_file); |
| 8960 | curproxy->conf.lfs_file = strdup(curproxy->conf.args.file); |
| 8961 | curproxy->conf.lfs_line = curproxy->conf.args.line; |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 8962 | } |
| 8963 | } |
| 8964 | |
Willy Tarreau | 4baae24 | 2012-12-27 12:00:31 +0100 | [diff] [blame] | 8965 | if (cookie) { |
| 8966 | /* depending on cookie_set, either we want to set the cookie, or to clear it. |
| 8967 | * a clear consists in appending "; path=/; Max-Age=0;" at the end. |
| 8968 | */ |
| 8969 | rule->cookie_len = strlen(cookie); |
| 8970 | if (cookie_set) { |
| 8971 | rule->cookie_str = malloc(rule->cookie_len + 10); |
| 8972 | memcpy(rule->cookie_str, cookie, rule->cookie_len); |
| 8973 | memcpy(rule->cookie_str + rule->cookie_len, "; path=/;", 10); |
| 8974 | rule->cookie_len += 9; |
| 8975 | } else { |
| 8976 | rule->cookie_str = malloc(rule->cookie_len + 21); |
| 8977 | memcpy(rule->cookie_str, cookie, rule->cookie_len); |
| 8978 | memcpy(rule->cookie_str + rule->cookie_len, "; path=/; Max-Age=0;", 21); |
| 8979 | rule->cookie_len += 20; |
| 8980 | } |
| 8981 | } |
| 8982 | rule->type = type; |
| 8983 | rule->code = code; |
| 8984 | rule->flags = flags; |
| 8985 | LIST_INIT(&rule->list); |
| 8986 | return rule; |
| 8987 | |
| 8988 | missing_arg: |
| 8989 | memprintf(errmsg, "missing argument for '%s'", args[cur_arg]); |
| 8990 | return NULL; |
| 8991 | } |
| 8992 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 8993 | /************************************************************************/ |
| 8994 | /* The code below is dedicated to ACL parsing and matching */ |
| 8995 | /************************************************************************/ |
| 8996 | |
| 8997 | |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 8998 | /* This function ensures that the prerequisites for an L7 fetch are ready, |
| 8999 | * which means that a request or response is ready. If some data is missing, |
| 9000 | * 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] | 9001 | * to extract data from L7. If <req_vol> is non-null during a request prefetch, |
| 9002 | * another test is made to ensure the required information is not gone. |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 9003 | * |
| 9004 | * The function returns : |
Willy Tarreau | 506d050 | 2013-07-06 13:29:24 +0200 | [diff] [blame] | 9005 | * 0 with SMP_F_MAY_CHANGE in the sample flags if some data is missing to |
| 9006 | * decide whether or not an HTTP message is present ; |
| 9007 | * 0 if the requested data cannot be fetched or if it is certain that |
| 9008 | * we'll never have any HTTP message there ; |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 9009 | * 1 if an HTTP message is ready |
| 9010 | */ |
| 9011 | static int |
Willy Tarreau | 506d050 | 2013-07-06 13:29:24 +0200 | [diff] [blame] | 9012 | 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] | 9013 | const struct arg *args, struct sample *smp, int req_vol) |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 9014 | { |
| 9015 | struct http_txn *txn = l7; |
| 9016 | struct http_msg *msg = &txn->req; |
| 9017 | |
| 9018 | /* Note: hdr_idx.v cannot be NULL in this ACL because the ACL is tagged |
| 9019 | * as a layer7 ACL, which involves automatic allocation of hdr_idx. |
| 9020 | */ |
| 9021 | |
| 9022 | if (unlikely(!s || !txn)) |
| 9023 | return 0; |
| 9024 | |
| 9025 | /* Check for a dependency on a request */ |
Willy Tarreau | f853c46 | 2012-04-23 18:53:56 +0200 | [diff] [blame] | 9026 | smp->type = SMP_T_BOOL; |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 9027 | |
Willy Tarreau | 32a6f2e | 2012-04-25 10:13:36 +0200 | [diff] [blame] | 9028 | if ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) { |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 9029 | if (unlikely(!s->req)) |
| 9030 | return 0; |
| 9031 | |
Willy Tarreau | aae75e3 | 2013-03-29 12:31:49 +0100 | [diff] [blame] | 9032 | /* If the buffer does not leave enough free space at the end, |
| 9033 | * we must first realign it. |
| 9034 | */ |
| 9035 | if (s->req->buf->p > s->req->buf->data && |
| 9036 | s->req->buf->i + s->req->buf->p > s->req->buf->data + s->req->buf->size - global.tune.maxrewrite) |
| 9037 | buffer_slow_realign(s->req->buf); |
| 9038 | |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 9039 | if (unlikely(txn->req.msg_state < HTTP_MSG_BODY)) { |
Willy Tarreau | 472b1ee | 2013-10-14 22:41:30 +0200 | [diff] [blame] | 9040 | if (msg->msg_state == HTTP_MSG_ERROR) |
Willy Tarreau | 506d050 | 2013-07-06 13:29:24 +0200 | [diff] [blame] | 9041 | return 0; |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 9042 | |
| 9043 | /* Try to decode HTTP request */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 9044 | if (likely(msg->next < s->req->buf->i)) |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 9045 | http_msg_analyzer(msg, &txn->hdr_idx); |
| 9046 | |
| 9047 | /* Still no valid request ? */ |
| 9048 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) { |
Willy Tarreau | 3bf1b2b | 2012-08-27 20:46:07 +0200 | [diff] [blame] | 9049 | if ((msg->msg_state == HTTP_MSG_ERROR) || |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 9050 | buffer_full(s->req->buf, global.tune.maxrewrite)) { |
Willy Tarreau | 506d050 | 2013-07-06 13:29:24 +0200 | [diff] [blame] | 9051 | return 0; |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 9052 | } |
| 9053 | /* wait for final state */ |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 9054 | smp->flags |= SMP_F_MAY_CHANGE; |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 9055 | return 0; |
| 9056 | } |
| 9057 | |
| 9058 | /* OK we just got a valid HTTP request. We have some minor |
| 9059 | * preparation to perform so that further checks can rely |
| 9060 | * on HTTP tests. |
| 9061 | */ |
Willy Tarreau | aae75e3 | 2013-03-29 12:31:49 +0100 | [diff] [blame] | 9062 | |
| 9063 | /* If the request was parsed but was too large, we must absolutely |
| 9064 | * return an error so that it is not processed. At the moment this |
| 9065 | * cannot happen, but if the parsers are to change in the future, |
| 9066 | * we want this check to be maintained. |
| 9067 | */ |
| 9068 | if (unlikely(s->req->buf->i + s->req->buf->p > |
| 9069 | s->req->buf->data + s->req->buf->size - global.tune.maxrewrite)) { |
| 9070 | msg->msg_state = HTTP_MSG_ERROR; |
Willy Tarreau | 506d050 | 2013-07-06 13:29:24 +0200 | [diff] [blame] | 9071 | smp->data.uint = 1; |
Willy Tarreau | aae75e3 | 2013-03-29 12:31:49 +0100 | [diff] [blame] | 9072 | return 1; |
| 9073 | } |
| 9074 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 9075 | 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] | 9076 | if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD) |
| 9077 | s->flags |= SN_REDIRECTABLE; |
| 9078 | |
Willy Tarreau | 506d050 | 2013-07-06 13:29:24 +0200 | [diff] [blame] | 9079 | if (unlikely(msg->sl.rq.v_l == 0) && !http_upgrade_v09_to_v10(txn)) |
| 9080 | return 0; |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 9081 | } |
| 9082 | |
Willy Tarreau | 506d050 | 2013-07-06 13:29:24 +0200 | [diff] [blame] | 9083 | if (req_vol && txn->rsp.msg_state != HTTP_MSG_RPBEFORE) { |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 9084 | return 0; /* data might have moved and indexes changed */ |
Willy Tarreau | 506d050 | 2013-07-06 13:29:24 +0200 | [diff] [blame] | 9085 | } |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 9086 | |
| 9087 | /* otherwise everything's ready for the request */ |
| 9088 | } |
Willy Tarreau | 24e32d8 | 2012-04-23 23:55:44 +0200 | [diff] [blame] | 9089 | else { |
| 9090 | /* Check for a dependency on a response */ |
Willy Tarreau | 506d050 | 2013-07-06 13:29:24 +0200 | [diff] [blame] | 9091 | if (txn->rsp.msg_state < HTTP_MSG_BODY) { |
| 9092 | smp->flags |= SMP_F_MAY_CHANGE; |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 9093 | return 0; |
Willy Tarreau | 506d050 | 2013-07-06 13:29:24 +0200 | [diff] [blame] | 9094 | } |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 9095 | } |
| 9096 | |
| 9097 | /* everything's OK */ |
Willy Tarreau | 506d050 | 2013-07-06 13:29:24 +0200 | [diff] [blame] | 9098 | smp->data.uint = 1; |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 9099 | return 1; |
| 9100 | } |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 9101 | |
Willy Tarreau | c0239e0 | 2012-04-16 14:42:55 +0200 | [diff] [blame] | 9102 | #define CHECK_HTTP_MESSAGE_FIRST() \ |
Willy Tarreau | 506d050 | 2013-07-06 13:29:24 +0200 | [diff] [blame] | 9103 | 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] | 9104 | |
Willy Tarreau | 24e32d8 | 2012-04-23 23:55:44 +0200 | [diff] [blame] | 9105 | #define CHECK_HTTP_MESSAGE_FIRST_PERM() \ |
Willy Tarreau | 506d050 | 2013-07-06 13:29:24 +0200 | [diff] [blame] | 9106 | 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] | 9107 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 9108 | |
| 9109 | /* 1. Check on METHOD |
| 9110 | * We use the pre-parsed method if it is known, and store its number as an |
| 9111 | * integer. If it is unknown, we use the pointer and the length. |
| 9112 | */ |
Thierry FOURNIER | edc15c3 | 2013-12-13 15:36:59 +0100 | [diff] [blame] | 9113 | static int pat_parse_meth(const char *text, struct pattern *pattern, char **err) |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 9114 | { |
| 9115 | int len, meth; |
Thierry FOURNIER | 0b2fe4a | 2013-12-06 20:33:50 +0100 | [diff] [blame] | 9116 | struct chunk *trash; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 9117 | |
Thierry FOURNIER | 580c32c | 2014-01-24 10:58:12 +0100 | [diff] [blame] | 9118 | len = strlen(text); |
| 9119 | meth = find_http_meth(text, len); |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 9120 | |
| 9121 | pattern->val.i = meth; |
| 9122 | if (meth == HTTP_METH_OTHER) { |
Thierry FOURNIER | edc15c3 | 2013-12-13 15:36:59 +0100 | [diff] [blame] | 9123 | trash = get_trash_chunk(); |
| 9124 | if (trash->size < len) { |
| 9125 | memprintf(err, "no space avalaible in the buffer. expect %d, provides %d", |
| 9126 | len, trash->size); |
| 9127 | return 0; |
Thierry FOURNIER | 0b2fe4a | 2013-12-06 20:33:50 +0100 | [diff] [blame] | 9128 | } |
Thierry FOURNIER | edc15c3 | 2013-12-13 15:36:59 +0100 | [diff] [blame] | 9129 | pattern->ptr.str = trash->str; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 9130 | pattern->len = len; |
| 9131 | } |
Thierry FOURNIER | d437314 | 2013-12-17 01:10:10 +0100 | [diff] [blame] | 9132 | else { |
| 9133 | pattern->ptr.str = NULL; |
| 9134 | pattern->len = 0; |
Thierry FOURNIER | d437314 | 2013-12-17 01:10:10 +0100 | [diff] [blame] | 9135 | } |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 9136 | return 1; |
| 9137 | } |
| 9138 | |
Willy Tarreau | 8e5e955 | 2011-12-16 15:38:49 +0100 | [diff] [blame] | 9139 | /* This function fetches the method of current HTTP request and stores |
| 9140 | * it in the global pattern struct as a chunk. There are two possibilities : |
| 9141 | * - if the method is known (not HTTP_METH_OTHER), its identifier is stored |
| 9142 | * in <len> and <ptr> is NULL ; |
| 9143 | * - if the method is unknown (HTTP_METH_OTHER), <ptr> points to the text and |
| 9144 | * <len> to its length. |
Thierry FOURNIER | a65b343 | 2013-11-28 18:22:00 +0100 | [diff] [blame] | 9145 | * This is intended to be used with pat_match_meth() only. |
Willy Tarreau | 8e5e955 | 2011-12-16 15:38:49 +0100 | [diff] [blame] | 9146 | */ |
Willy Tarreau | d41f8d8 | 2007-06-10 10:06:18 +0200 | [diff] [blame] | 9147 | static int |
Willy Tarreau | 409bcde | 2013-01-08 00:31:00 +0100 | [diff] [blame] | 9148 | 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] | 9149 | const struct arg *args, struct sample *smp, const char *kw) |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 9150 | { |
| 9151 | int meth; |
| 9152 | struct http_txn *txn = l7; |
| 9153 | |
Willy Tarreau | 24e32d8 | 2012-04-23 23:55:44 +0200 | [diff] [blame] | 9154 | CHECK_HTTP_MESSAGE_FIRST_PERM(); |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 9155 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 9156 | meth = txn->meth; |
Thierry FOURNIER | d437314 | 2013-12-17 01:10:10 +0100 | [diff] [blame] | 9157 | smp->type = SMP_T_METH; |
| 9158 | smp->data.meth.meth = meth; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 9159 | if (meth == HTTP_METH_OTHER) { |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 9160 | if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE) |
| 9161 | /* ensure the indexes are not affected */ |
| 9162 | return 0; |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 9163 | smp->flags |= SMP_F_CONST; |
Thierry FOURNIER | d437314 | 2013-12-17 01:10:10 +0100 | [diff] [blame] | 9164 | smp->data.meth.str.len = txn->req.sl.rq.m_l; |
| 9165 | smp->data.meth.str.str = txn->req.chn->buf->p; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 9166 | } |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 9167 | smp->flags |= SMP_F_VOL_1ST; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 9168 | return 1; |
| 9169 | } |
| 9170 | |
Willy Tarreau | 8e5e955 | 2011-12-16 15:38:49 +0100 | [diff] [blame] | 9171 | /* See above how the method is stored in the global pattern */ |
Thierry FOURNIER | 5338eea | 2013-12-16 14:22:13 +0100 | [diff] [blame] | 9172 | static struct pattern *pat_match_meth(struct sample *smp, struct pattern_expr *expr, int fill) |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 9173 | { |
Willy Tarreau | c8d7c96 | 2007-06-17 08:20:33 +0200 | [diff] [blame] | 9174 | int icase; |
Thierry FOURNIER | 5338eea | 2013-12-16 14:22:13 +0100 | [diff] [blame] | 9175 | struct pattern_list *lst; |
| 9176 | struct pattern *pattern; |
Willy Tarreau | c8d7c96 | 2007-06-17 08:20:33 +0200 | [diff] [blame] | 9177 | |
Thierry FOURNIER | 5338eea | 2013-12-16 14:22:13 +0100 | [diff] [blame] | 9178 | list_for_each_entry(lst, &expr->patterns, list) { |
| 9179 | pattern = &lst->pat; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 9180 | |
Thierry FOURNIER | 5338eea | 2013-12-16 14:22:13 +0100 | [diff] [blame] | 9181 | /* well-known method */ |
| 9182 | if (pattern->val.i != HTTP_METH_OTHER) { |
| 9183 | if (smp->data.meth.meth == pattern->val.i) |
| 9184 | return pattern; |
| 9185 | else |
| 9186 | continue; |
| 9187 | } |
Willy Tarreau | c8d7c96 | 2007-06-17 08:20:33 +0200 | [diff] [blame] | 9188 | |
Thierry FOURNIER | 5338eea | 2013-12-16 14:22:13 +0100 | [diff] [blame] | 9189 | /* Other method, we must compare the strings */ |
| 9190 | if (pattern->len != smp->data.meth.str.len) |
| 9191 | continue; |
| 9192 | |
| 9193 | icase = pattern->flags & PAT_F_IGNORE_CASE; |
| 9194 | if ((icase && strncasecmp(pattern->ptr.str, smp->data.meth.str.str, smp->data.meth.str.len) != 0) || |
| 9195 | (!icase && strncmp(pattern->ptr.str, smp->data.meth.str.str, smp->data.meth.str.len) != 0)) |
| 9196 | return pattern; |
| 9197 | } |
| 9198 | return NULL; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 9199 | } |
| 9200 | |
Willy Tarreau | d41f8d8 | 2007-06-10 10:06:18 +0200 | [diff] [blame] | 9201 | static int |
Willy Tarreau | 409bcde | 2013-01-08 00:31:00 +0100 | [diff] [blame] | 9202 | 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] | 9203 | const struct arg *args, struct sample *smp, const char *kw) |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 9204 | { |
| 9205 | struct http_txn *txn = l7; |
| 9206 | char *ptr; |
| 9207 | int len; |
| 9208 | |
Willy Tarreau | c0239e0 | 2012-04-16 14:42:55 +0200 | [diff] [blame] | 9209 | CHECK_HTTP_MESSAGE_FIRST(); |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 9210 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 9211 | len = txn->req.sl.rq.v_l; |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 9212 | ptr = txn->req.chn->buf->p + txn->req.sl.rq.v; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 9213 | |
| 9214 | while ((len-- > 0) && (*ptr++ != '/')); |
| 9215 | if (len <= 0) |
| 9216 | return 0; |
| 9217 | |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 9218 | smp->type = SMP_T_STR; |
Willy Tarreau | f853c46 | 2012-04-23 18:53:56 +0200 | [diff] [blame] | 9219 | smp->data.str.str = ptr; |
| 9220 | smp->data.str.len = len; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 9221 | |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 9222 | smp->flags = SMP_F_VOL_1ST | SMP_F_CONST; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 9223 | return 1; |
| 9224 | } |
| 9225 | |
Willy Tarreau | d41f8d8 | 2007-06-10 10:06:18 +0200 | [diff] [blame] | 9226 | static int |
Willy Tarreau | 409bcde | 2013-01-08 00:31:00 +0100 | [diff] [blame] | 9227 | 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] | 9228 | const struct arg *args, struct sample *smp, const char *kw) |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 9229 | { |
| 9230 | struct http_txn *txn = l7; |
| 9231 | char *ptr; |
| 9232 | int len; |
| 9233 | |
Willy Tarreau | c0239e0 | 2012-04-16 14:42:55 +0200 | [diff] [blame] | 9234 | CHECK_HTTP_MESSAGE_FIRST(); |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 9235 | |
Willy Tarreau | f26b252 | 2012-12-14 08:33:14 +0100 | [diff] [blame] | 9236 | if (txn->rsp.msg_state < HTTP_MSG_BODY) |
| 9237 | return 0; |
| 9238 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 9239 | len = txn->rsp.sl.st.v_l; |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 9240 | ptr = txn->rsp.chn->buf->p; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 9241 | |
| 9242 | while ((len-- > 0) && (*ptr++ != '/')); |
| 9243 | if (len <= 0) |
| 9244 | return 0; |
| 9245 | |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 9246 | smp->type = SMP_T_STR; |
Willy Tarreau | f853c46 | 2012-04-23 18:53:56 +0200 | [diff] [blame] | 9247 | smp->data.str.str = ptr; |
| 9248 | smp->data.str.len = len; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 9249 | |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 9250 | smp->flags = SMP_F_VOL_1ST | SMP_F_CONST; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 9251 | return 1; |
| 9252 | } |
| 9253 | |
| 9254 | /* 3. Check on Status Code. We manipulate integers here. */ |
Willy Tarreau | d41f8d8 | 2007-06-10 10:06:18 +0200 | [diff] [blame] | 9255 | static int |
Willy Tarreau | 409bcde | 2013-01-08 00:31:00 +0100 | [diff] [blame] | 9256 | 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] | 9257 | const struct arg *args, struct sample *smp, const char *kw) |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 9258 | { |
| 9259 | struct http_txn *txn = l7; |
| 9260 | char *ptr; |
| 9261 | int len; |
| 9262 | |
Willy Tarreau | c0239e0 | 2012-04-16 14:42:55 +0200 | [diff] [blame] | 9263 | CHECK_HTTP_MESSAGE_FIRST(); |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 9264 | |
Willy Tarreau | f26b252 | 2012-12-14 08:33:14 +0100 | [diff] [blame] | 9265 | if (txn->rsp.msg_state < HTTP_MSG_BODY) |
| 9266 | return 0; |
| 9267 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 9268 | len = txn->rsp.sl.st.c_l; |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 9269 | ptr = txn->rsp.chn->buf->p + txn->rsp.sl.st.c; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 9270 | |
Willy Tarreau | f853c46 | 2012-04-23 18:53:56 +0200 | [diff] [blame] | 9271 | smp->type = SMP_T_UINT; |
| 9272 | smp->data.uint = __strl2ui(ptr, len); |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 9273 | smp->flags = SMP_F_VOL_1ST; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 9274 | return 1; |
| 9275 | } |
| 9276 | |
| 9277 | /* 4. Check on URL/URI. A pointer to the URI is stored. */ |
Willy Tarreau | d41f8d8 | 2007-06-10 10:06:18 +0200 | [diff] [blame] | 9278 | static int |
Willy Tarreau | 6812bcf | 2012-04-29 09:28:50 +0200 | [diff] [blame] | 9279 | 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] | 9280 | const struct arg *args, struct sample *smp, const char *kw) |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 9281 | { |
| 9282 | struct http_txn *txn = l7; |
| 9283 | |
Willy Tarreau | c0239e0 | 2012-04-16 14:42:55 +0200 | [diff] [blame] | 9284 | CHECK_HTTP_MESSAGE_FIRST(); |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 9285 | |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 9286 | smp->type = SMP_T_STR; |
Willy Tarreau | f853c46 | 2012-04-23 18:53:56 +0200 | [diff] [blame] | 9287 | smp->data.str.len = txn->req.sl.rq.u_l; |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 9288 | smp->data.str.str = txn->req.chn->buf->p + txn->req.sl.rq.u; |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 9289 | smp->flags = SMP_F_VOL_1ST | SMP_F_CONST; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 9290 | return 1; |
| 9291 | } |
| 9292 | |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 9293 | static int |
Willy Tarreau | 6812bcf | 2012-04-29 09:28:50 +0200 | [diff] [blame] | 9294 | 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] | 9295 | const struct arg *args, struct sample *smp, const char *kw) |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 9296 | { |
| 9297 | struct http_txn *txn = l7; |
Willy Tarreau | 4c804ec | 2013-09-30 14:37:14 +0200 | [diff] [blame] | 9298 | struct sockaddr_storage addr; |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 9299 | |
Willy Tarreau | c0239e0 | 2012-04-16 14:42:55 +0200 | [diff] [blame] | 9300 | CHECK_HTTP_MESSAGE_FIRST(); |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 9301 | |
Thierry FOURNIER | 9f95e40 | 2014-03-21 14:51:46 +0100 | [diff] [blame] | 9302 | url2sa(txn->req.chn->buf->p + txn->req.sl.rq.u, txn->req.sl.rq.u_l, &addr, NULL); |
Willy Tarreau | 4c804ec | 2013-09-30 14:37:14 +0200 | [diff] [blame] | 9303 | if (((struct sockaddr_in *)&addr)->sin_family != AF_INET) |
Willy Tarreau | f4362b3 | 2011-12-16 17:49:52 +0100 | [diff] [blame] | 9304 | return 0; |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 9305 | |
Willy Tarreau | 4c804ec | 2013-09-30 14:37:14 +0200 | [diff] [blame] | 9306 | smp->type = SMP_T_IPV4; |
| 9307 | smp->data.ipv4 = ((struct sockaddr_in *)&addr)->sin_addr; |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 9308 | smp->flags = 0; |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 9309 | return 1; |
| 9310 | } |
| 9311 | |
| 9312 | static int |
Willy Tarreau | 6812bcf | 2012-04-29 09:28:50 +0200 | [diff] [blame] | 9313 | 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] | 9314 | const struct arg *args, struct sample *smp, const char *kw) |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 9315 | { |
| 9316 | struct http_txn *txn = l7; |
Willy Tarreau | 4c804ec | 2013-09-30 14:37:14 +0200 | [diff] [blame] | 9317 | struct sockaddr_storage addr; |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 9318 | |
Willy Tarreau | c0239e0 | 2012-04-16 14:42:55 +0200 | [diff] [blame] | 9319 | CHECK_HTTP_MESSAGE_FIRST(); |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 9320 | |
Thierry FOURNIER | 9f95e40 | 2014-03-21 14:51:46 +0100 | [diff] [blame] | 9321 | url2sa(txn->req.chn->buf->p + txn->req.sl.rq.u, txn->req.sl.rq.u_l, &addr, NULL); |
Willy Tarreau | 4c804ec | 2013-09-30 14:37:14 +0200 | [diff] [blame] | 9322 | if (((struct sockaddr_in *)&addr)->sin_family != AF_INET) |
| 9323 | return 0; |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 9324 | |
Willy Tarreau | 4c804ec | 2013-09-30 14:37:14 +0200 | [diff] [blame] | 9325 | smp->type = SMP_T_UINT; |
| 9326 | smp->data.uint = ntohs(((struct sockaddr_in *)&addr)->sin_port); |
Willy Tarreau | 21e5b0e | 2012-04-23 19:25:44 +0200 | [diff] [blame] | 9327 | smp->flags = 0; |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 9328 | return 1; |
| 9329 | } |
| 9330 | |
Willy Tarreau | 185b5c4 | 2012-04-26 15:11:51 +0200 | [diff] [blame] | 9331 | /* Fetch an HTTP header. A pointer to the beginning of the value is returned. |
| 9332 | * Accepts an optional argument of type string containing the header field name, |
| 9333 | * and an optional argument of type signed or unsigned integer to request an |
| 9334 | * 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] | 9335 | * headers are considered from the first one. It does not stop on commas and |
| 9336 | * returns full lines instead (useful for User-Agent or Date for example). |
| 9337 | */ |
| 9338 | static int |
| 9339 | 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] | 9340 | const struct arg *args, struct sample *smp, const char *kw) |
Willy Tarreau | 04ff9f1 | 2013-06-10 18:39:42 +0200 | [diff] [blame] | 9341 | { |
| 9342 | struct http_txn *txn = l7; |
| 9343 | struct hdr_idx *idx = &txn->hdr_idx; |
| 9344 | struct hdr_ctx *ctx = smp->ctx.a[0]; |
| 9345 | const struct http_msg *msg = ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) ? &txn->req : &txn->rsp; |
| 9346 | int occ = 0; |
| 9347 | const char *name_str = NULL; |
| 9348 | int name_len = 0; |
| 9349 | |
| 9350 | if (!ctx) { |
| 9351 | /* first call */ |
| 9352 | ctx = &static_hdr_ctx; |
| 9353 | ctx->idx = 0; |
| 9354 | smp->ctx.a[0] = ctx; |
| 9355 | } |
| 9356 | |
| 9357 | if (args) { |
| 9358 | if (args[0].type != ARGT_STR) |
| 9359 | return 0; |
| 9360 | name_str = args[0].data.str.str; |
| 9361 | name_len = args[0].data.str.len; |
| 9362 | |
| 9363 | if (args[1].type == ARGT_UINT || args[1].type == ARGT_SINT) |
| 9364 | occ = args[1].data.uint; |
| 9365 | } |
| 9366 | |
| 9367 | CHECK_HTTP_MESSAGE_FIRST(); |
| 9368 | |
| 9369 | if (ctx && !(smp->flags & SMP_F_NOT_LAST)) |
| 9370 | /* search for header from the beginning */ |
| 9371 | ctx->idx = 0; |
| 9372 | |
| 9373 | if (!occ && !(opt & SMP_OPT_ITERATE)) |
| 9374 | /* no explicit occurrence and single fetch => last header by default */ |
| 9375 | occ = -1; |
| 9376 | |
| 9377 | if (!occ) |
| 9378 | /* prepare to report multiple occurrences for ACL fetches */ |
| 9379 | smp->flags |= SMP_F_NOT_LAST; |
| 9380 | |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 9381 | smp->type = SMP_T_STR; |
| 9382 | smp->flags |= SMP_F_VOL_HDR | SMP_F_CONST; |
Willy Tarreau | 04ff9f1 | 2013-06-10 18:39:42 +0200 | [diff] [blame] | 9383 | if (http_get_fhdr(msg, name_str, name_len, idx, occ, ctx, &smp->data.str.str, &smp->data.str.len)) |
| 9384 | return 1; |
| 9385 | |
| 9386 | smp->flags &= ~SMP_F_NOT_LAST; |
| 9387 | return 0; |
| 9388 | } |
| 9389 | |
| 9390 | /* 6. Check on HTTP header count. The number of occurrences is returned. |
| 9391 | * Accepts exactly 1 argument of type string. It does not stop on commas and |
| 9392 | * returns full lines instead (useful for User-Agent or Date for example). |
| 9393 | */ |
| 9394 | static int |
| 9395 | 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] | 9396 | const struct arg *args, struct sample *smp, const char *kw) |
Willy Tarreau | 04ff9f1 | 2013-06-10 18:39:42 +0200 | [diff] [blame] | 9397 | { |
| 9398 | struct http_txn *txn = l7; |
| 9399 | struct hdr_idx *idx = &txn->hdr_idx; |
| 9400 | struct hdr_ctx ctx; |
| 9401 | const struct http_msg *msg = ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) ? &txn->req : &txn->rsp; |
| 9402 | int cnt; |
| 9403 | |
| 9404 | if (!args || args->type != ARGT_STR) |
| 9405 | return 0; |
| 9406 | |
| 9407 | CHECK_HTTP_MESSAGE_FIRST(); |
| 9408 | |
| 9409 | ctx.idx = 0; |
| 9410 | cnt = 0; |
| 9411 | while (http_find_full_header2(args->data.str.str, args->data.str.len, msg->chn->buf->p, idx, &ctx)) |
| 9412 | cnt++; |
| 9413 | |
| 9414 | smp->type = SMP_T_UINT; |
| 9415 | smp->data.uint = cnt; |
| 9416 | smp->flags = SMP_F_VOL_HDR; |
| 9417 | return 1; |
| 9418 | } |
| 9419 | |
| 9420 | /* Fetch an HTTP header. A pointer to the beginning of the value is returned. |
| 9421 | * Accepts an optional argument of type string containing the header field name, |
| 9422 | * and an optional argument of type signed or unsigned integer to request an |
| 9423 | * 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] | 9424 | * headers are considered from the first one. |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 9425 | */ |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 9426 | static int |
Willy Tarreau | 185b5c4 | 2012-04-26 15:11:51 +0200 | [diff] [blame] | 9427 | 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] | 9428 | const struct arg *args, struct sample *smp, const char *kw) |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 9429 | { |
| 9430 | struct http_txn *txn = l7; |
| 9431 | struct hdr_idx *idx = &txn->hdr_idx; |
Willy Tarreau | a890d07 | 2013-04-02 12:01:06 +0200 | [diff] [blame] | 9432 | struct hdr_ctx *ctx = smp->ctx.a[0]; |
Willy Tarreau | 32a6f2e | 2012-04-25 10:13:36 +0200 | [diff] [blame] | 9433 | 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] | 9434 | int occ = 0; |
| 9435 | const char *name_str = NULL; |
| 9436 | int name_len = 0; |
Willy Tarreau | e333ec9 | 2012-04-16 16:26:40 +0200 | [diff] [blame] | 9437 | |
Willy Tarreau | a890d07 | 2013-04-02 12:01:06 +0200 | [diff] [blame] | 9438 | if (!ctx) { |
| 9439 | /* first call */ |
| 9440 | ctx = &static_hdr_ctx; |
| 9441 | ctx->idx = 0; |
Willy Tarreau | ffb6f08 | 2013-04-02 23:16:53 +0200 | [diff] [blame] | 9442 | smp->ctx.a[0] = ctx; |
Willy Tarreau | a890d07 | 2013-04-02 12:01:06 +0200 | [diff] [blame] | 9443 | } |
| 9444 | |
Willy Tarreau | 185b5c4 | 2012-04-26 15:11:51 +0200 | [diff] [blame] | 9445 | if (args) { |
| 9446 | if (args[0].type != ARGT_STR) |
| 9447 | return 0; |
| 9448 | name_str = args[0].data.str.str; |
| 9449 | name_len = args[0].data.str.len; |
| 9450 | |
| 9451 | if (args[1].type == ARGT_UINT || args[1].type == ARGT_SINT) |
| 9452 | occ = args[1].data.uint; |
| 9453 | } |
Willy Tarreau | 34db108 | 2012-04-19 17:16:54 +0200 | [diff] [blame] | 9454 | |
Willy Tarreau | e333ec9 | 2012-04-16 16:26:40 +0200 | [diff] [blame] | 9455 | CHECK_HTTP_MESSAGE_FIRST(); |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 9456 | |
Willy Tarreau | 185b5c4 | 2012-04-26 15:11:51 +0200 | [diff] [blame] | 9457 | if (ctx && !(smp->flags & SMP_F_NOT_LAST)) |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 9458 | /* search for header from the beginning */ |
| 9459 | ctx->idx = 0; |
| 9460 | |
Willy Tarreau | 185b5c4 | 2012-04-26 15:11:51 +0200 | [diff] [blame] | 9461 | if (!occ && !(opt & SMP_OPT_ITERATE)) |
| 9462 | /* no explicit occurrence and single fetch => last header by default */ |
| 9463 | occ = -1; |
| 9464 | |
| 9465 | if (!occ) |
| 9466 | /* prepare to report multiple occurrences for ACL fetches */ |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 9467 | smp->flags |= SMP_F_NOT_LAST; |
Willy Tarreau | 664092c | 2011-12-16 19:11:42 +0100 | [diff] [blame] | 9468 | |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 9469 | smp->type = SMP_T_STR; |
| 9470 | smp->flags |= SMP_F_VOL_HDR | SMP_F_CONST; |
Willy Tarreau | 185b5c4 | 2012-04-26 15:11:51 +0200 | [diff] [blame] | 9471 | 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] | 9472 | return 1; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 9473 | |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 9474 | smp->flags &= ~SMP_F_NOT_LAST; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 9475 | return 0; |
| 9476 | } |
| 9477 | |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 9478 | /* 6. Check on HTTP header count. The number of occurrences is returned. |
Willy Tarreau | 34db108 | 2012-04-19 17:16:54 +0200 | [diff] [blame] | 9479 | * Accepts exactly 1 argument of type string. |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 9480 | */ |
| 9481 | static int |
Willy Tarreau | 185b5c4 | 2012-04-26 15:11:51 +0200 | [diff] [blame] | 9482 | 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] | 9483 | const struct arg *args, struct sample *smp, const char *kw) |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 9484 | { |
| 9485 | struct http_txn *txn = l7; |
| 9486 | struct hdr_idx *idx = &txn->hdr_idx; |
| 9487 | struct hdr_ctx ctx; |
Willy Tarreau | 32a6f2e | 2012-04-25 10:13:36 +0200 | [diff] [blame] | 9488 | 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] | 9489 | int cnt; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 9490 | |
Willy Tarreau | 24e32d8 | 2012-04-23 23:55:44 +0200 | [diff] [blame] | 9491 | if (!args || args->type != ARGT_STR) |
Willy Tarreau | 34db108 | 2012-04-19 17:16:54 +0200 | [diff] [blame] | 9492 | return 0; |
| 9493 | |
Willy Tarreau | e333ec9 | 2012-04-16 16:26:40 +0200 | [diff] [blame] | 9494 | CHECK_HTTP_MESSAGE_FIRST(); |
| 9495 | |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 9496 | ctx.idx = 0; |
| 9497 | cnt = 0; |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 9498 | 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] | 9499 | cnt++; |
| 9500 | |
Willy Tarreau | f853c46 | 2012-04-23 18:53:56 +0200 | [diff] [blame] | 9501 | smp->type = SMP_T_UINT; |
| 9502 | smp->data.uint = cnt; |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 9503 | smp->flags = SMP_F_VOL_HDR; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 9504 | return 1; |
| 9505 | } |
| 9506 | |
Willy Tarreau | 185b5c4 | 2012-04-26 15:11:51 +0200 | [diff] [blame] | 9507 | /* Fetch an HTTP header's integer value. The integer value is returned. It |
| 9508 | * takes a mandatory argument of type string and an optional one of type int |
| 9509 | * to designate a specific occurrence. It returns an unsigned integer, which |
| 9510 | * may or may not be appropriate for everything. |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 9511 | */ |
| 9512 | static int |
Willy Tarreau | 185b5c4 | 2012-04-26 15:11:51 +0200 | [diff] [blame] | 9513 | 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] | 9514 | const struct arg *args, struct sample *smp, const char *kw) |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 9515 | { |
Willy Tarreau | ef38c39 | 2013-07-22 16:29:32 +0200 | [diff] [blame] | 9516 | int ret = smp_fetch_hdr(px, l4, l7, opt, args, smp, kw); |
Willy Tarreau | e333ec9 | 2012-04-16 16:26:40 +0200 | [diff] [blame] | 9517 | |
Willy Tarreau | f853c46 | 2012-04-23 18:53:56 +0200 | [diff] [blame] | 9518 | if (ret > 0) { |
| 9519 | smp->type = SMP_T_UINT; |
| 9520 | smp->data.uint = strl2ic(smp->data.str.str, smp->data.str.len); |
| 9521 | } |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 9522 | |
Willy Tarreau | d53e242 | 2012-04-16 17:21:11 +0200 | [diff] [blame] | 9523 | return ret; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 9524 | } |
| 9525 | |
Cyril Bonté | 69fa992 | 2012-10-25 00:01:06 +0200 | [diff] [blame] | 9526 | /* Fetch an HTTP header's IP value. takes a mandatory argument of type string |
| 9527 | * and an optional one of type int to designate a specific occurrence. |
| 9528 | * It returns an IPv4 or IPv6 address. |
Willy Tarreau | 106f979 | 2009-09-19 07:54:16 +0200 | [diff] [blame] | 9529 | */ |
| 9530 | static int |
Willy Tarreau | 185b5c4 | 2012-04-26 15:11:51 +0200 | [diff] [blame] | 9531 | 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] | 9532 | const struct arg *args, struct sample *smp, const char *kw) |
Willy Tarreau | 106f979 | 2009-09-19 07:54:16 +0200 | [diff] [blame] | 9533 | { |
Willy Tarreau | d53e242 | 2012-04-16 17:21:11 +0200 | [diff] [blame] | 9534 | int ret; |
Willy Tarreau | e333ec9 | 2012-04-16 16:26:40 +0200 | [diff] [blame] | 9535 | |
Willy Tarreau | ef38c39 | 2013-07-22 16:29:32 +0200 | [diff] [blame] | 9536 | 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] | 9537 | if (url2ipv4((char *)smp->data.str.str, &smp->data.ipv4)) { |
| 9538 | smp->type = SMP_T_IPV4; |
Willy Tarreau | d53e242 | 2012-04-16 17:21:11 +0200 | [diff] [blame] | 9539 | break; |
Cyril Bonté | 69fa992 | 2012-10-25 00:01:06 +0200 | [diff] [blame] | 9540 | } else { |
Willy Tarreau | 47ca545 | 2012-12-23 20:22:19 +0100 | [diff] [blame] | 9541 | struct chunk *temp = get_trash_chunk(); |
Cyril Bonté | 69fa992 | 2012-10-25 00:01:06 +0200 | [diff] [blame] | 9542 | if (smp->data.str.len < temp->size - 1) { |
| 9543 | memcpy(temp->str, smp->data.str.str, smp->data.str.len); |
| 9544 | temp->str[smp->data.str.len] = '\0'; |
| 9545 | if (inet_pton(AF_INET6, temp->str, &smp->data.ipv6)) { |
| 9546 | smp->type = SMP_T_IPV6; |
| 9547 | break; |
| 9548 | } |
| 9549 | } |
| 9550 | } |
| 9551 | |
Willy Tarreau | d53e242 | 2012-04-16 17:21:11 +0200 | [diff] [blame] | 9552 | /* if the header doesn't match an IP address, fetch next one */ |
Willy Tarreau | 185b5c4 | 2012-04-26 15:11:51 +0200 | [diff] [blame] | 9553 | if (!(smp->flags & SMP_F_NOT_LAST)) |
| 9554 | return 0; |
Willy Tarreau | 106f979 | 2009-09-19 07:54:16 +0200 | [diff] [blame] | 9555 | } |
Willy Tarreau | d53e242 | 2012-04-16 17:21:11 +0200 | [diff] [blame] | 9556 | return ret; |
Willy Tarreau | 106f979 | 2009-09-19 07:54:16 +0200 | [diff] [blame] | 9557 | } |
| 9558 | |
Willy Tarreau | 737b0c1 | 2007-06-10 21:28:46 +0200 | [diff] [blame] | 9559 | /* 8. Check on URI PATH. A pointer to the PATH is stored. The path starts at |
| 9560 | * the first '/' after the possible hostname, and ends before the possible '?'. |
| 9561 | */ |
| 9562 | static int |
Willy Tarreau | 6812bcf | 2012-04-29 09:28:50 +0200 | [diff] [blame] | 9563 | 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] | 9564 | const struct arg *args, struct sample *smp, const char *kw) |
Willy Tarreau | 737b0c1 | 2007-06-10 21:28:46 +0200 | [diff] [blame] | 9565 | { |
| 9566 | struct http_txn *txn = l7; |
| 9567 | char *ptr, *end; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 9568 | |
Willy Tarreau | c0239e0 | 2012-04-16 14:42:55 +0200 | [diff] [blame] | 9569 | CHECK_HTTP_MESSAGE_FIRST(); |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 9570 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 9571 | 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] | 9572 | ptr = http_get_path(txn); |
| 9573 | if (!ptr) |
Willy Tarreau | 737b0c1 | 2007-06-10 21:28:46 +0200 | [diff] [blame] | 9574 | return 0; |
| 9575 | |
| 9576 | /* OK, we got the '/' ! */ |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 9577 | smp->type = SMP_T_STR; |
Willy Tarreau | f853c46 | 2012-04-23 18:53:56 +0200 | [diff] [blame] | 9578 | smp->data.str.str = ptr; |
Willy Tarreau | 737b0c1 | 2007-06-10 21:28:46 +0200 | [diff] [blame] | 9579 | |
| 9580 | while (ptr < end && *ptr != '?') |
| 9581 | ptr++; |
| 9582 | |
Willy Tarreau | f853c46 | 2012-04-23 18:53:56 +0200 | [diff] [blame] | 9583 | smp->data.str.len = ptr - smp->data.str.str; |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 9584 | smp->flags = SMP_F_VOL_1ST | SMP_F_CONST; |
Willy Tarreau | 737b0c1 | 2007-06-10 21:28:46 +0200 | [diff] [blame] | 9585 | return 1; |
| 9586 | } |
| 9587 | |
Willy Tarreau | a7ad50c | 2012-04-29 15:39:40 +0200 | [diff] [blame] | 9588 | /* This produces a concatenation of the first occurrence of the Host header |
| 9589 | * followed by the path component if it begins with a slash ('/'). This means |
| 9590 | * that '*' will not be added, resulting in exactly the first Host entry. |
| 9591 | * If no Host header is found, then the path is returned as-is. The returned |
| 9592 | * 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] | 9593 | * The returned sample is of type string. |
Willy Tarreau | a7ad50c | 2012-04-29 15:39:40 +0200 | [diff] [blame] | 9594 | */ |
| 9595 | static int |
| 9596 | 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] | 9597 | const struct arg *args, struct sample *smp, const char *kw) |
Willy Tarreau | a7ad50c | 2012-04-29 15:39:40 +0200 | [diff] [blame] | 9598 | { |
| 9599 | struct http_txn *txn = l7; |
| 9600 | char *ptr, *end, *beg; |
| 9601 | struct hdr_ctx ctx; |
| 9602 | |
| 9603 | CHECK_HTTP_MESSAGE_FIRST(); |
| 9604 | |
| 9605 | ctx.idx = 0; |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 9606 | 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] | 9607 | !ctx.vlen) |
Willy Tarreau | ef38c39 | 2013-07-22 16:29:32 +0200 | [diff] [blame] | 9608 | return smp_fetch_path(px, l4, l7, opt, args, smp, kw); |
Willy Tarreau | a7ad50c | 2012-04-29 15:39:40 +0200 | [diff] [blame] | 9609 | |
| 9610 | /* 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] | 9611 | memcpy(trash.str, ctx.line + ctx.val, ctx.vlen); |
Willy Tarreau | a7ad50c | 2012-04-29 15:39:40 +0200 | [diff] [blame] | 9612 | smp->type = SMP_T_STR; |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 9613 | smp->data.str.str = trash.str; |
Willy Tarreau | a7ad50c | 2012-04-29 15:39:40 +0200 | [diff] [blame] | 9614 | smp->data.str.len = ctx.vlen; |
| 9615 | |
| 9616 | /* now retrieve the path */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 9617 | 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] | 9618 | beg = http_get_path(txn); |
| 9619 | if (!beg) |
| 9620 | beg = end; |
| 9621 | |
| 9622 | for (ptr = beg; ptr < end && *ptr != '?'; ptr++); |
| 9623 | |
| 9624 | if (beg < ptr && *beg == '/') { |
| 9625 | memcpy(smp->data.str.str + smp->data.str.len, beg, ptr - beg); |
| 9626 | smp->data.str.len += ptr - beg; |
| 9627 | } |
| 9628 | |
| 9629 | smp->flags = SMP_F_VOL_1ST; |
| 9630 | return 1; |
| 9631 | } |
| 9632 | |
Willy Tarreau | ab1f7b7 | 2012-12-09 13:38:54 +0100 | [diff] [blame] | 9633 | /* This produces a 32-bit hash of the concatenation of the first occurrence of |
| 9634 | * the Host header followed by the path component if it begins with a slash ('/'). |
| 9635 | * This means that '*' will not be added, resulting in exactly the first Host |
| 9636 | * 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] | 9637 | * is hashed using the path hash followed by a full avalanche hash and provides a |
| 9638 | * 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] | 9639 | * high-traffic sites without having to store whole paths. |
| 9640 | */ |
| 9641 | static int |
| 9642 | 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] | 9643 | const struct arg *args, struct sample *smp, const char *kw) |
Willy Tarreau | ab1f7b7 | 2012-12-09 13:38:54 +0100 | [diff] [blame] | 9644 | { |
| 9645 | struct http_txn *txn = l7; |
| 9646 | struct hdr_ctx ctx; |
| 9647 | unsigned int hash = 0; |
| 9648 | char *ptr, *beg, *end; |
| 9649 | int len; |
| 9650 | |
| 9651 | CHECK_HTTP_MESSAGE_FIRST(); |
| 9652 | |
| 9653 | ctx.idx = 0; |
| 9654 | if (http_find_header2("Host", 4, txn->req.chn->buf->p + txn->req.sol, &txn->hdr_idx, &ctx)) { |
| 9655 | /* OK we have the header value in ctx.line+ctx.val for ctx.vlen bytes */ |
| 9656 | ptr = ctx.line + ctx.val; |
| 9657 | len = ctx.vlen; |
| 9658 | while (len--) |
| 9659 | hash = *(ptr++) + (hash << 6) + (hash << 16) - hash; |
| 9660 | } |
| 9661 | |
| 9662 | /* now retrieve the path */ |
| 9663 | end = txn->req.chn->buf->p + txn->req.sol + txn->req.sl.rq.u + txn->req.sl.rq.u_l; |
| 9664 | beg = http_get_path(txn); |
| 9665 | if (!beg) |
| 9666 | beg = end; |
| 9667 | |
| 9668 | for (ptr = beg; ptr < end && *ptr != '?'; ptr++); |
| 9669 | |
| 9670 | if (beg < ptr && *beg == '/') { |
| 9671 | while (beg < ptr) |
| 9672 | hash = *(beg++) + (hash << 6) + (hash << 16) - hash; |
| 9673 | } |
| 9674 | hash = full_hash(hash); |
| 9675 | |
| 9676 | smp->type = SMP_T_UINT; |
| 9677 | smp->data.uint = hash; |
| 9678 | smp->flags = SMP_F_VOL_1ST; |
| 9679 | return 1; |
| 9680 | } |
| 9681 | |
Willy Tarreau | 4a55060 | 2012-12-09 14:53:32 +0100 | [diff] [blame] | 9682 | /* 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] | 9683 | * path as returned by smp_fetch_base32(). The idea is to have per-source and |
| 9684 | * per-path counters. The result is a binary block from 8 to 20 bytes depending |
| 9685 | * 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] | 9686 | * that in environments where IPv6 is insignificant, truncating the output to |
| 9687 | * 8 bytes would still work. |
| 9688 | */ |
| 9689 | static int |
| 9690 | 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] | 9691 | const struct arg *args, struct sample *smp, const char *kw) |
Willy Tarreau | 4a55060 | 2012-12-09 14:53:32 +0100 | [diff] [blame] | 9692 | { |
Willy Tarreau | 47ca545 | 2012-12-23 20:22:19 +0100 | [diff] [blame] | 9693 | struct chunk *temp; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 9694 | struct connection *cli_conn = objt_conn(l4->si[0].end); |
| 9695 | |
| 9696 | if (!cli_conn) |
| 9697 | return 0; |
Willy Tarreau | 4a55060 | 2012-12-09 14:53:32 +0100 | [diff] [blame] | 9698 | |
Willy Tarreau | ef38c39 | 2013-07-22 16:29:32 +0200 | [diff] [blame] | 9699 | if (!smp_fetch_base32(px, l4, l7, opt, args, smp, kw)) |
Willy Tarreau | 4a55060 | 2012-12-09 14:53:32 +0100 | [diff] [blame] | 9700 | return 0; |
| 9701 | |
Willy Tarreau | 47ca545 | 2012-12-23 20:22:19 +0100 | [diff] [blame] | 9702 | temp = get_trash_chunk(); |
Willy Tarreau | 4a55060 | 2012-12-09 14:53:32 +0100 | [diff] [blame] | 9703 | memcpy(temp->str + temp->len, &smp->data.uint, sizeof(smp->data.uint)); |
| 9704 | temp->len += sizeof(smp->data.uint); |
| 9705 | |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 9706 | switch (cli_conn->addr.from.ss_family) { |
Willy Tarreau | 4a55060 | 2012-12-09 14:53:32 +0100 | [diff] [blame] | 9707 | case AF_INET: |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 9708 | 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] | 9709 | temp->len += 4; |
| 9710 | break; |
| 9711 | case AF_INET6: |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 9712 | 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] | 9713 | temp->len += 16; |
| 9714 | break; |
| 9715 | default: |
| 9716 | return 0; |
| 9717 | } |
| 9718 | |
| 9719 | smp->data.str = *temp; |
| 9720 | smp->type = SMP_T_BIN; |
| 9721 | return 1; |
| 9722 | } |
| 9723 | |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 9724 | static int |
Willy Tarreau | 409bcde | 2013-01-08 00:31:00 +0100 | [diff] [blame] | 9725 | 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] | 9726 | const struct arg *args, struct sample *smp, const char *kw) |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 9727 | { |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 9728 | /* Note: hdr_idx.v cannot be NULL in this ACL because the ACL is tagged |
| 9729 | * as a layer7 ACL, which involves automatic allocation of hdr_idx. |
| 9730 | */ |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 9731 | |
Willy Tarreau | 24e32d8 | 2012-04-23 23:55:44 +0200 | [diff] [blame] | 9732 | CHECK_HTTP_MESSAGE_FIRST_PERM(); |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 9733 | |
Willy Tarreau | f853c46 | 2012-04-23 18:53:56 +0200 | [diff] [blame] | 9734 | smp->type = SMP_T_BOOL; |
Willy Tarreau | 197e10a | 2012-04-23 19:18:42 +0200 | [diff] [blame] | 9735 | smp->data.uint = 1; |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 9736 | return 1; |
| 9737 | } |
| 9738 | |
Willy Tarreau | 7f18e52 | 2010-10-22 20:04:13 +0200 | [diff] [blame] | 9739 | /* return a valid test if the current request is the first one on the connection */ |
| 9740 | static int |
Willy Tarreau | 409bcde | 2013-01-08 00:31:00 +0100 | [diff] [blame] | 9741 | 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] | 9742 | const struct arg *args, struct sample *smp, const char *kw) |
Willy Tarreau | 7f18e52 | 2010-10-22 20:04:13 +0200 | [diff] [blame] | 9743 | { |
| 9744 | if (!s) |
| 9745 | return 0; |
| 9746 | |
Willy Tarreau | f853c46 | 2012-04-23 18:53:56 +0200 | [diff] [blame] | 9747 | smp->type = SMP_T_BOOL; |
Willy Tarreau | 197e10a | 2012-04-23 19:18:42 +0200 | [diff] [blame] | 9748 | smp->data.uint = !(s->txn.flags & TX_NOT_FIRST); |
Willy Tarreau | 7f18e52 | 2010-10-22 20:04:13 +0200 | [diff] [blame] | 9749 | return 1; |
| 9750 | } |
| 9751 | |
Willy Tarreau | 34db108 | 2012-04-19 17:16:54 +0200 | [diff] [blame] | 9752 | /* Accepts exactly 1 argument of type userlist */ |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 9753 | static int |
Willy Tarreau | 409bcde | 2013-01-08 00:31:00 +0100 | [diff] [blame] | 9754 | 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] | 9755 | const struct arg *args, struct sample *smp, const char *kw) |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 9756 | { |
| 9757 | |
Willy Tarreau | 24e32d8 | 2012-04-23 23:55:44 +0200 | [diff] [blame] | 9758 | if (!args || args->type != ARGT_USR) |
Willy Tarreau | 34db108 | 2012-04-19 17:16:54 +0200 | [diff] [blame] | 9759 | return 0; |
| 9760 | |
Willy Tarreau | c0239e0 | 2012-04-16 14:42:55 +0200 | [diff] [blame] | 9761 | CHECK_HTTP_MESSAGE_FIRST(); |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 9762 | |
Willy Tarreau | c0239e0 | 2012-04-16 14:42:55 +0200 | [diff] [blame] | 9763 | if (!get_http_auth(l4)) |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 9764 | return 0; |
| 9765 | |
Willy Tarreau | f853c46 | 2012-04-23 18:53:56 +0200 | [diff] [blame] | 9766 | smp->type = SMP_T_BOOL; |
Thierry FOURNIER | 9eec0a6 | 2014-01-22 18:38:02 +0100 | [diff] [blame] | 9767 | smp->data.uint = check_user(args->data.usr, l4->txn.auth.user, l4->txn.auth.pass); |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 9768 | return 1; |
| 9769 | } |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 9770 | |
Willy Tarreau | 4a3fd4c | 2012-05-10 23:18:26 +0200 | [diff] [blame] | 9771 | /* Accepts exactly 1 argument of type userlist */ |
| 9772 | static int |
Willy Tarreau | 409bcde | 2013-01-08 00:31:00 +0100 | [diff] [blame] | 9773 | 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] | 9774 | const struct arg *args, struct sample *smp, const char *kw) |
Willy Tarreau | 4a3fd4c | 2012-05-10 23:18:26 +0200 | [diff] [blame] | 9775 | { |
| 9776 | |
| 9777 | if (!args || args->type != ARGT_USR) |
| 9778 | return 0; |
| 9779 | |
| 9780 | CHECK_HTTP_MESSAGE_FIRST(); |
| 9781 | |
| 9782 | if (!get_http_auth(l4)) |
| 9783 | return 0; |
| 9784 | |
Willy Tarreau | 4a3fd4c | 2012-05-10 23:18:26 +0200 | [diff] [blame] | 9785 | /* if the user does not belong to the userlist or has a wrong password, |
| 9786 | * report that it unconditionally does not match. Otherwise we return |
Thierry FOURNIER | 9eec0a6 | 2014-01-22 18:38:02 +0100 | [diff] [blame] | 9787 | * a string containing the username. |
Willy Tarreau | 4a3fd4c | 2012-05-10 23:18:26 +0200 | [diff] [blame] | 9788 | */ |
Thierry FOURNIER | 9eec0a6 | 2014-01-22 18:38:02 +0100 | [diff] [blame] | 9789 | if (!check_user(args->data.usr, l4->txn.auth.user, l4->txn.auth.pass)) |
| 9790 | return 0; |
| 9791 | |
| 9792 | /* pat_match_auth() will need the user list */ |
| 9793 | smp->ctx.a[0] = args->data.usr; |
| 9794 | |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 9795 | smp->type = SMP_T_STR; |
| 9796 | smp->flags = SMP_F_CONST; |
Thierry FOURNIER | 9eec0a6 | 2014-01-22 18:38:02 +0100 | [diff] [blame] | 9797 | smp->data.str.str = l4->txn.auth.user; |
| 9798 | smp->data.str.len = strlen(l4->txn.auth.user); |
Willy Tarreau | 4a3fd4c | 2012-05-10 23:18:26 +0200 | [diff] [blame] | 9799 | |
| 9800 | return 1; |
| 9801 | } |
| 9802 | |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 9803 | /* Try to find the next occurrence of a cookie name in a cookie header value. |
| 9804 | * The lookup begins at <hdr>. The pointer and size of the next occurrence of |
| 9805 | * the cookie value is returned into *value and *value_l, and the function |
| 9806 | * returns a pointer to the next pointer to search from if the value was found. |
| 9807 | * Otherwise if the cookie was not found, NULL is returned and neither value |
| 9808 | * nor value_l are touched. The input <hdr> string should first point to the |
| 9809 | * header's value, and the <hdr_end> pointer must point to the first character |
| 9810 | * not part of the value. <list> must be non-zero if value may represent a list |
| 9811 | * of values (cookie headers). This makes it faster to abort parsing when no |
| 9812 | * list is expected. |
| 9813 | */ |
| 9814 | static char * |
| 9815 | extract_cookie_value(char *hdr, const char *hdr_end, |
| 9816 | char *cookie_name, size_t cookie_name_l, int list, |
Willy Tarreau | 3fb818c | 2012-04-11 17:21:08 +0200 | [diff] [blame] | 9817 | char **value, int *value_l) |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 9818 | { |
| 9819 | char *equal, *att_end, *att_beg, *val_beg, *val_end; |
| 9820 | char *next; |
| 9821 | |
| 9822 | /* we search at least a cookie name followed by an equal, and more |
| 9823 | * generally something like this : |
| 9824 | * Cookie: NAME1 = VALUE 1 ; NAME2 = VALUE2 ; NAME3 = VALUE3\r\n |
| 9825 | */ |
| 9826 | for (att_beg = hdr; att_beg + cookie_name_l + 1 < hdr_end; att_beg = next + 1) { |
| 9827 | /* Iterate through all cookies on this line */ |
| 9828 | |
| 9829 | while (att_beg < hdr_end && http_is_spht[(unsigned char)*att_beg]) |
| 9830 | att_beg++; |
| 9831 | |
| 9832 | /* find att_end : this is the first character after the last non |
| 9833 | * space before the equal. It may be equal to hdr_end. |
| 9834 | */ |
| 9835 | equal = att_end = att_beg; |
| 9836 | |
| 9837 | while (equal < hdr_end) { |
| 9838 | if (*equal == '=' || *equal == ';' || (list && *equal == ',')) |
| 9839 | break; |
| 9840 | if (http_is_spht[(unsigned char)*equal++]) |
| 9841 | continue; |
| 9842 | att_end = equal; |
| 9843 | } |
| 9844 | |
| 9845 | /* here, <equal> points to '=', a delimitor or the end. <att_end> |
| 9846 | * is between <att_beg> and <equal>, both may be identical. |
| 9847 | */ |
| 9848 | |
| 9849 | /* look for end of cookie if there is an equal sign */ |
| 9850 | if (equal < hdr_end && *equal == '=') { |
| 9851 | /* look for the beginning of the value */ |
| 9852 | val_beg = equal + 1; |
| 9853 | while (val_beg < hdr_end && http_is_spht[(unsigned char)*val_beg]) |
| 9854 | val_beg++; |
| 9855 | |
| 9856 | /* find the end of the value, respecting quotes */ |
| 9857 | next = find_cookie_value_end(val_beg, hdr_end); |
| 9858 | |
| 9859 | /* make val_end point to the first white space or delimitor after the value */ |
| 9860 | val_end = next; |
| 9861 | while (val_end > val_beg && http_is_spht[(unsigned char)*(val_end - 1)]) |
| 9862 | val_end--; |
| 9863 | } else { |
| 9864 | val_beg = val_end = next = equal; |
| 9865 | } |
| 9866 | |
| 9867 | /* We have nothing to do with attributes beginning with '$'. However, |
| 9868 | * they will automatically be removed if a header before them is removed, |
| 9869 | * since they're supposed to be linked together. |
| 9870 | */ |
| 9871 | if (*att_beg == '$') |
| 9872 | continue; |
| 9873 | |
| 9874 | /* Ignore cookies with no equal sign */ |
| 9875 | if (equal == next) |
| 9876 | continue; |
| 9877 | |
| 9878 | /* Now we have the cookie name between att_beg and att_end, and |
| 9879 | * its value between val_beg and val_end. |
| 9880 | */ |
| 9881 | |
| 9882 | if (att_end - att_beg == cookie_name_l && |
| 9883 | memcmp(att_beg, cookie_name, cookie_name_l) == 0) { |
| 9884 | /* let's return this value and indicate where to go on from */ |
| 9885 | *value = val_beg; |
| 9886 | *value_l = val_end - val_beg; |
| 9887 | return next + 1; |
| 9888 | } |
| 9889 | |
| 9890 | /* Set-Cookie headers only have the name in the first attr=value part */ |
| 9891 | if (!list) |
| 9892 | break; |
| 9893 | } |
| 9894 | |
| 9895 | return NULL; |
| 9896 | } |
| 9897 | |
William Lallemand | a43ba4e | 2014-01-28 18:14:25 +0100 | [diff] [blame] | 9898 | /* Fetch a captured HTTP request header. The index is the position of |
| 9899 | * the "capture" option in the configuration file |
| 9900 | */ |
| 9901 | static int |
| 9902 | smp_fetch_capture_header_req(struct proxy *px, struct session *l4, void *l7, unsigned int opt, |
| 9903 | const struct arg *args, struct sample *smp, const char *kw) |
| 9904 | { |
| 9905 | struct proxy *fe = l4->fe; |
| 9906 | struct http_txn *txn = l7; |
| 9907 | int idx; |
| 9908 | |
| 9909 | if (!args || args->type != ARGT_UINT) |
| 9910 | return 0; |
| 9911 | |
| 9912 | idx = args->data.uint; |
| 9913 | |
| 9914 | if (idx > (fe->nb_req_cap - 1) || txn->req.cap == NULL || txn->req.cap[idx] == NULL) |
| 9915 | return 0; |
| 9916 | |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 9917 | smp->type = SMP_T_STR; |
| 9918 | smp->flags |= SMP_F_CONST; |
William Lallemand | a43ba4e | 2014-01-28 18:14:25 +0100 | [diff] [blame] | 9919 | smp->data.str.str = txn->req.cap[idx]; |
| 9920 | smp->data.str.len = strlen(txn->req.cap[idx]); |
| 9921 | |
| 9922 | return 1; |
| 9923 | } |
| 9924 | |
| 9925 | /* Fetch a captured HTTP response header. The index is the position of |
| 9926 | * the "capture" option in the configuration file |
| 9927 | */ |
| 9928 | static int |
| 9929 | smp_fetch_capture_header_res(struct proxy *px, struct session *l4, void *l7, unsigned int opt, |
| 9930 | const struct arg *args, struct sample *smp, const char *kw) |
| 9931 | { |
| 9932 | struct proxy *fe = l4->fe; |
| 9933 | struct http_txn *txn = l7; |
| 9934 | int idx; |
| 9935 | |
| 9936 | if (!args || args->type != ARGT_UINT) |
| 9937 | return 0; |
| 9938 | |
| 9939 | idx = args->data.uint; |
| 9940 | |
| 9941 | if (idx > (fe->nb_rsp_cap - 1) || txn->rsp.cap == NULL || txn->rsp.cap[idx] == NULL) |
| 9942 | return 0; |
| 9943 | |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 9944 | smp->type = SMP_T_STR; |
| 9945 | smp->flags |= SMP_F_CONST; |
William Lallemand | a43ba4e | 2014-01-28 18:14:25 +0100 | [diff] [blame] | 9946 | smp->data.str.str = txn->rsp.cap[idx]; |
| 9947 | smp->data.str.len = strlen(txn->rsp.cap[idx]); |
| 9948 | |
| 9949 | return 1; |
| 9950 | } |
| 9951 | |
William Lallemand | 65ad6e1 | 2014-01-31 15:08:02 +0100 | [diff] [blame] | 9952 | /* Extracts the METHOD in the HTTP request, the txn->uri should be filled before the call */ |
| 9953 | static int |
| 9954 | smp_fetch_capture_req_method(struct proxy *px, struct session *l4, void *l7, unsigned int opt, |
| 9955 | const struct arg *args, struct sample *smp, const char *kw) |
| 9956 | { |
| 9957 | struct chunk *temp; |
| 9958 | struct http_txn *txn = l7; |
William Lallemand | 96a7785 | 2014-02-05 00:30:02 +0100 | [diff] [blame] | 9959 | char *ptr; |
William Lallemand | 65ad6e1 | 2014-01-31 15:08:02 +0100 | [diff] [blame] | 9960 | |
| 9961 | if (!txn->uri) |
| 9962 | return 0; |
| 9963 | |
William Lallemand | 96a7785 | 2014-02-05 00:30:02 +0100 | [diff] [blame] | 9964 | ptr = txn->uri; |
William Lallemand | 65ad6e1 | 2014-01-31 15:08:02 +0100 | [diff] [blame] | 9965 | |
William Lallemand | 96a7785 | 2014-02-05 00:30:02 +0100 | [diff] [blame] | 9966 | while (*ptr != ' ' && *ptr != '\0') /* find first space */ |
| 9967 | ptr++; |
William Lallemand | 65ad6e1 | 2014-01-31 15:08:02 +0100 | [diff] [blame] | 9968 | |
William Lallemand | 96a7785 | 2014-02-05 00:30:02 +0100 | [diff] [blame] | 9969 | temp = get_trash_chunk(); |
| 9970 | temp->str = txn->uri; |
| 9971 | temp->len = ptr - txn->uri; |
William Lallemand | 65ad6e1 | 2014-01-31 15:08:02 +0100 | [diff] [blame] | 9972 | smp->data.str = *temp; |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 9973 | smp->type = SMP_T_STR; |
| 9974 | smp->flags = SMP_F_CONST; |
William Lallemand | 65ad6e1 | 2014-01-31 15:08:02 +0100 | [diff] [blame] | 9975 | |
| 9976 | return 1; |
| 9977 | |
| 9978 | } |
| 9979 | |
| 9980 | /* Extracts the path in the HTTP request, the txn->uri should be filled before the call */ |
| 9981 | static int |
| 9982 | smp_fetch_capture_req_uri(struct proxy *px, struct session *l4, void *l7, unsigned int opt, |
| 9983 | const struct arg *args, struct sample *smp, const char *kw) |
| 9984 | { |
| 9985 | struct chunk *temp; |
| 9986 | struct http_txn *txn = l7; |
| 9987 | char *ptr; |
William Lallemand | 65ad6e1 | 2014-01-31 15:08:02 +0100 | [diff] [blame] | 9988 | |
| 9989 | if (!txn->uri) |
| 9990 | return 0; |
William Lallemand | 96a7785 | 2014-02-05 00:30:02 +0100 | [diff] [blame] | 9991 | |
William Lallemand | 65ad6e1 | 2014-01-31 15:08:02 +0100 | [diff] [blame] | 9992 | ptr = txn->uri; |
| 9993 | |
| 9994 | while (*ptr != ' ' && *ptr != '\0') /* find first space */ |
| 9995 | ptr++; |
William Lallemand | 96a7785 | 2014-02-05 00:30:02 +0100 | [diff] [blame] | 9996 | |
William Lallemand | 65ad6e1 | 2014-01-31 15:08:02 +0100 | [diff] [blame] | 9997 | if (!*ptr) |
| 9998 | return 0; |
| 9999 | |
| 10000 | ptr++; /* skip the space */ |
| 10001 | |
| 10002 | temp = get_trash_chunk(); |
William Lallemand | 96a7785 | 2014-02-05 00:30:02 +0100 | [diff] [blame] | 10003 | ptr = temp->str = http_get_path_from_string(ptr); |
William Lallemand | 65ad6e1 | 2014-01-31 15:08:02 +0100 | [diff] [blame] | 10004 | if (!ptr) |
| 10005 | return 0; |
| 10006 | while (*ptr != ' ' && *ptr != '\0') /* find space after URI */ |
| 10007 | ptr++; |
William Lallemand | 65ad6e1 | 2014-01-31 15:08:02 +0100 | [diff] [blame] | 10008 | |
| 10009 | smp->data.str = *temp; |
William Lallemand | 96a7785 | 2014-02-05 00:30:02 +0100 | [diff] [blame] | 10010 | smp->data.str.len = ptr - temp->str; |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 10011 | smp->type = SMP_T_STR; |
| 10012 | smp->flags = SMP_F_CONST; |
William Lallemand | 65ad6e1 | 2014-01-31 15:08:02 +0100 | [diff] [blame] | 10013 | |
| 10014 | return 1; |
| 10015 | } |
| 10016 | |
| 10017 | |
Willy Tarreau | e333ec9 | 2012-04-16 16:26:40 +0200 | [diff] [blame] | 10018 | /* 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] | 10019 | * 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] | 10020 | * 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] | 10021 | * 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] | 10022 | * 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] | 10023 | * Accepts exactly 1 argument of type string. If the input options indicate |
| 10024 | * 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] | 10025 | * The returned sample is of type CSTR. |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 10026 | */ |
| 10027 | static int |
Willy Tarreau | 5153936 | 2012-05-08 12:46:28 +0200 | [diff] [blame] | 10028 | 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] | 10029 | const struct arg *args, struct sample *smp, const char *kw) |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 10030 | { |
| 10031 | struct http_txn *txn = l7; |
| 10032 | struct hdr_idx *idx = &txn->hdr_idx; |
Willy Tarreau | a890d07 | 2013-04-02 12:01:06 +0200 | [diff] [blame] | 10033 | struct hdr_ctx *ctx = smp->ctx.a[2]; |
Willy Tarreau | e333ec9 | 2012-04-16 16:26:40 +0200 | [diff] [blame] | 10034 | const struct http_msg *msg; |
| 10035 | const char *hdr_name; |
| 10036 | int hdr_name_len; |
| 10037 | char *sol; |
Willy Tarreau | 28376d6 | 2012-04-26 21:26:10 +0200 | [diff] [blame] | 10038 | int occ = 0; |
| 10039 | int found = 0; |
Willy Tarreau | e333ec9 | 2012-04-16 16:26:40 +0200 | [diff] [blame] | 10040 | |
Willy Tarreau | 24e32d8 | 2012-04-23 23:55:44 +0200 | [diff] [blame] | 10041 | if (!args || args->type != ARGT_STR) |
Willy Tarreau | 34db108 | 2012-04-19 17:16:54 +0200 | [diff] [blame] | 10042 | return 0; |
| 10043 | |
Willy Tarreau | a890d07 | 2013-04-02 12:01:06 +0200 | [diff] [blame] | 10044 | if (!ctx) { |
| 10045 | /* first call */ |
| 10046 | ctx = &static_hdr_ctx; |
| 10047 | ctx->idx = 0; |
| 10048 | smp->ctx.a[2] = ctx; |
| 10049 | } |
| 10050 | |
Willy Tarreau | e333ec9 | 2012-04-16 16:26:40 +0200 | [diff] [blame] | 10051 | CHECK_HTTP_MESSAGE_FIRST(); |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 10052 | |
Willy Tarreau | 32a6f2e | 2012-04-25 10:13:36 +0200 | [diff] [blame] | 10053 | if ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) { |
Willy Tarreau | e333ec9 | 2012-04-16 16:26:40 +0200 | [diff] [blame] | 10054 | msg = &txn->req; |
| 10055 | hdr_name = "Cookie"; |
| 10056 | hdr_name_len = 6; |
| 10057 | } else { |
| 10058 | msg = &txn->rsp; |
| 10059 | hdr_name = "Set-Cookie"; |
| 10060 | hdr_name_len = 10; |
| 10061 | } |
| 10062 | |
Willy Tarreau | 28376d6 | 2012-04-26 21:26:10 +0200 | [diff] [blame] | 10063 | if (!occ && !(opt & SMP_OPT_ITERATE)) |
| 10064 | /* no explicit occurrence and single fetch => last cookie by default */ |
| 10065 | occ = -1; |
| 10066 | |
| 10067 | /* OK so basically here, either we want only one value and it's the |
| 10068 | * last one, or we want to iterate over all of them and we fetch the |
| 10069 | * next one. |
| 10070 | */ |
| 10071 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 10072 | sol = msg->chn->buf->p; |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 10073 | if (!(smp->flags & SMP_F_NOT_LAST)) { |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 10074 | /* search for the header from the beginning, we must first initialize |
| 10075 | * the search parameters. |
| 10076 | */ |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 10077 | smp->ctx.a[0] = NULL; |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 10078 | ctx->idx = 0; |
| 10079 | } |
| 10080 | |
Willy Tarreau | 28376d6 | 2012-04-26 21:26:10 +0200 | [diff] [blame] | 10081 | smp->flags |= SMP_F_VOL_HDR; |
| 10082 | |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 10083 | while (1) { |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 10084 | /* Note: smp->ctx.a[0] == NULL every time we need to fetch a new header */ |
| 10085 | if (!smp->ctx.a[0]) { |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 10086 | if (!http_find_header2(hdr_name, hdr_name_len, sol, idx, ctx)) |
| 10087 | goto out; |
| 10088 | |
Willy Tarreau | 24e32d8 | 2012-04-23 23:55:44 +0200 | [diff] [blame] | 10089 | if (ctx->vlen < args->data.str.len + 1) |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 10090 | continue; |
| 10091 | |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 10092 | smp->ctx.a[0] = ctx->line + ctx->val; |
| 10093 | smp->ctx.a[1] = smp->ctx.a[0] + ctx->vlen; |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 10094 | } |
| 10095 | |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 10096 | smp->type = SMP_T_STR; |
| 10097 | smp->flags |= SMP_F_CONST; |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 10098 | 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] | 10099 | args->data.str.str, args->data.str.len, |
Willy Tarreau | 32a6f2e | 2012-04-25 10:13:36 +0200 | [diff] [blame] | 10100 | (opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ, |
Willy Tarreau | f853c46 | 2012-04-23 18:53:56 +0200 | [diff] [blame] | 10101 | &smp->data.str.str, |
| 10102 | &smp->data.str.len); |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 10103 | if (smp->ctx.a[0]) { |
Willy Tarreau | 28376d6 | 2012-04-26 21:26:10 +0200 | [diff] [blame] | 10104 | found = 1; |
| 10105 | if (occ >= 0) { |
| 10106 | /* one value was returned into smp->data.str.{str,len} */ |
| 10107 | smp->flags |= SMP_F_NOT_LAST; |
| 10108 | return 1; |
| 10109 | } |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 10110 | } |
Willy Tarreau | 28376d6 | 2012-04-26 21:26:10 +0200 | [diff] [blame] | 10111 | /* if we're looking for last occurrence, let's loop */ |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 10112 | } |
Willy Tarreau | 28376d6 | 2012-04-26 21:26:10 +0200 | [diff] [blame] | 10113 | /* all cookie headers and values were scanned. If we're looking for the |
| 10114 | * last occurrence, we may return it now. |
| 10115 | */ |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 10116 | out: |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 10117 | smp->flags &= ~SMP_F_NOT_LAST; |
Willy Tarreau | 28376d6 | 2012-04-26 21:26:10 +0200 | [diff] [blame] | 10118 | return found; |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 10119 | } |
| 10120 | |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 10121 | /* 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] | 10122 | * 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] | 10123 | * multiple cookies may be parsed on the same line. The returned sample is of |
| 10124 | * type UINT. Accepts exactly 1 argument of type string. |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 10125 | */ |
| 10126 | static int |
Willy Tarreau | 409bcde | 2013-01-08 00:31:00 +0100 | [diff] [blame] | 10127 | 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] | 10128 | const struct arg *args, struct sample *smp, const char *kw) |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 10129 | { |
| 10130 | struct http_txn *txn = l7; |
| 10131 | struct hdr_idx *idx = &txn->hdr_idx; |
| 10132 | struct hdr_ctx ctx; |
Willy Tarreau | e333ec9 | 2012-04-16 16:26:40 +0200 | [diff] [blame] | 10133 | const struct http_msg *msg; |
| 10134 | const char *hdr_name; |
| 10135 | int hdr_name_len; |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 10136 | int cnt; |
| 10137 | char *val_beg, *val_end; |
Willy Tarreau | e333ec9 | 2012-04-16 16:26:40 +0200 | [diff] [blame] | 10138 | char *sol; |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 10139 | |
Willy Tarreau | 24e32d8 | 2012-04-23 23:55:44 +0200 | [diff] [blame] | 10140 | if (!args || args->type != ARGT_STR) |
Willy Tarreau | 34db108 | 2012-04-19 17:16:54 +0200 | [diff] [blame] | 10141 | return 0; |
| 10142 | |
Willy Tarreau | e333ec9 | 2012-04-16 16:26:40 +0200 | [diff] [blame] | 10143 | CHECK_HTTP_MESSAGE_FIRST(); |
| 10144 | |
Willy Tarreau | 32a6f2e | 2012-04-25 10:13:36 +0200 | [diff] [blame] | 10145 | if ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) { |
Willy Tarreau | e333ec9 | 2012-04-16 16:26:40 +0200 | [diff] [blame] | 10146 | msg = &txn->req; |
| 10147 | hdr_name = "Cookie"; |
| 10148 | hdr_name_len = 6; |
| 10149 | } else { |
| 10150 | msg = &txn->rsp; |
| 10151 | hdr_name = "Set-Cookie"; |
| 10152 | hdr_name_len = 10; |
| 10153 | } |
| 10154 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 10155 | sol = msg->chn->buf->p; |
Willy Tarreau | 46787ed | 2012-04-11 17:28:40 +0200 | [diff] [blame] | 10156 | val_end = val_beg = NULL; |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 10157 | ctx.idx = 0; |
| 10158 | cnt = 0; |
| 10159 | |
| 10160 | while (1) { |
| 10161 | /* Note: val_beg == NULL every time we need to fetch a new header */ |
| 10162 | if (!val_beg) { |
| 10163 | if (!http_find_header2(hdr_name, hdr_name_len, sol, idx, &ctx)) |
| 10164 | break; |
| 10165 | |
Willy Tarreau | 24e32d8 | 2012-04-23 23:55:44 +0200 | [diff] [blame] | 10166 | if (ctx.vlen < args->data.str.len + 1) |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 10167 | continue; |
| 10168 | |
| 10169 | val_beg = ctx.line + ctx.val; |
| 10170 | val_end = val_beg + ctx.vlen; |
| 10171 | } |
| 10172 | |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 10173 | smp->type = SMP_T_STR; |
| 10174 | smp->flags |= SMP_F_CONST; |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 10175 | while ((val_beg = extract_cookie_value(val_beg, val_end, |
Willy Tarreau | 24e32d8 | 2012-04-23 23:55:44 +0200 | [diff] [blame] | 10176 | args->data.str.str, args->data.str.len, |
Willy Tarreau | 32a6f2e | 2012-04-25 10:13:36 +0200 | [diff] [blame] | 10177 | (opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ, |
Willy Tarreau | f853c46 | 2012-04-23 18:53:56 +0200 | [diff] [blame] | 10178 | &smp->data.str.str, |
| 10179 | &smp->data.str.len))) { |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 10180 | cnt++; |
| 10181 | } |
| 10182 | } |
| 10183 | |
Willy Tarreau | b169eba | 2013-12-16 15:14:43 +0100 | [diff] [blame] | 10184 | smp->type = SMP_T_UINT; |
Willy Tarreau | f853c46 | 2012-04-23 18:53:56 +0200 | [diff] [blame] | 10185 | smp->data.uint = cnt; |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 10186 | smp->flags |= SMP_F_VOL_HDR; |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 10187 | return 1; |
| 10188 | } |
| 10189 | |
Willy Tarreau | 5153936 | 2012-05-08 12:46:28 +0200 | [diff] [blame] | 10190 | /* Fetch an cookie's integer value. The integer value is returned. It |
| 10191 | * takes a mandatory argument of type string. It relies on smp_fetch_cookie(). |
| 10192 | */ |
| 10193 | static int |
| 10194 | 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] | 10195 | const struct arg *args, struct sample *smp, const char *kw) |
Willy Tarreau | 5153936 | 2012-05-08 12:46:28 +0200 | [diff] [blame] | 10196 | { |
Willy Tarreau | ef38c39 | 2013-07-22 16:29:32 +0200 | [diff] [blame] | 10197 | int ret = smp_fetch_cookie(px, l4, l7, opt, args, smp, kw); |
Willy Tarreau | 5153936 | 2012-05-08 12:46:28 +0200 | [diff] [blame] | 10198 | |
| 10199 | if (ret > 0) { |
| 10200 | smp->type = SMP_T_UINT; |
| 10201 | smp->data.uint = strl2ic(smp->data.str.str, smp->data.str.len); |
| 10202 | } |
| 10203 | |
| 10204 | return ret; |
| 10205 | } |
| 10206 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 10207 | /************************************************************************/ |
Willy Tarreau | 1278578 | 2012-04-27 21:37:17 +0200 | [diff] [blame] | 10208 | /* The code below is dedicated to sample fetches */ |
Willy Tarreau | 4a56897 | 2010-05-12 08:08:50 +0200 | [diff] [blame] | 10209 | /************************************************************************/ |
| 10210 | |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 10211 | /* |
| 10212 | * Given a path string and its length, find the position of beginning of the |
| 10213 | * query string. Returns NULL if no query string is found in the path. |
| 10214 | * |
| 10215 | * Example: if path = "/foo/bar/fubar?yo=mama;ye=daddy", and n = 22: |
| 10216 | * |
| 10217 | * find_query_string(path, n) points to "yo=mama;ye=daddy" string. |
| 10218 | */ |
bedis | 4c75cca | 2012-10-05 08:38:24 +0200 | [diff] [blame] | 10219 | 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] | 10220 | { |
| 10221 | char *p; |
Emeric Brun | 485479d | 2010-09-23 18:02:19 +0200 | [diff] [blame] | 10222 | |
bedis | 4c75cca | 2012-10-05 08:38:24 +0200 | [diff] [blame] | 10223 | p = memchr(path, delim, path_l); |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 10224 | return p ? p + 1 : NULL; |
| 10225 | } |
| 10226 | |
bedis | 4c75cca | 2012-10-05 08:38:24 +0200 | [diff] [blame] | 10227 | static inline int is_param_delimiter(char c, char delim) |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 10228 | { |
bedis | 4c75cca | 2012-10-05 08:38:24 +0200 | [diff] [blame] | 10229 | return c == '&' || c == ';' || c == delim; |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 10230 | } |
| 10231 | |
| 10232 | /* |
| 10233 | * Given a url parameter, find the starting position of the first occurence, |
| 10234 | * or NULL if the parameter is not found. |
| 10235 | * |
| 10236 | * Example: if query_string is "yo=mama;ye=daddy" and url_param_name is "ye", |
| 10237 | * the function will return query_string+8. |
| 10238 | */ |
| 10239 | static char* |
| 10240 | find_url_param_pos(char* query_string, size_t query_string_l, |
bedis | 4c75cca | 2012-10-05 08:38:24 +0200 | [diff] [blame] | 10241 | char* url_param_name, size_t url_param_name_l, |
| 10242 | char delim) |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 10243 | { |
| 10244 | char *pos, *last; |
| 10245 | |
| 10246 | pos = query_string; |
| 10247 | last = query_string + query_string_l - url_param_name_l - 1; |
| 10248 | |
| 10249 | while (pos <= last) { |
| 10250 | if (pos[url_param_name_l] == '=') { |
| 10251 | if (memcmp(pos, url_param_name, url_param_name_l) == 0) |
| 10252 | return pos; |
| 10253 | pos += url_param_name_l + 1; |
| 10254 | } |
bedis | 4c75cca | 2012-10-05 08:38:24 +0200 | [diff] [blame] | 10255 | while (pos <= last && !is_param_delimiter(*pos, delim)) |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 10256 | pos++; |
| 10257 | pos++; |
| 10258 | } |
| 10259 | return NULL; |
| 10260 | } |
| 10261 | |
| 10262 | /* |
| 10263 | * Given a url parameter name, returns its value and size into *value and |
| 10264 | * *value_l respectively, and returns non-zero. If the parameter is not found, |
| 10265 | * zero is returned and value/value_l are not touched. |
| 10266 | */ |
| 10267 | static int |
| 10268 | find_url_param_value(char* path, size_t path_l, |
| 10269 | char* url_param_name, size_t url_param_name_l, |
bedis | 4c75cca | 2012-10-05 08:38:24 +0200 | [diff] [blame] | 10270 | char** value, int* value_l, char delim) |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 10271 | { |
| 10272 | char *query_string, *qs_end; |
| 10273 | char *arg_start; |
| 10274 | char *value_start, *value_end; |
| 10275 | |
bedis | 4c75cca | 2012-10-05 08:38:24 +0200 | [diff] [blame] | 10276 | query_string = find_param_list(path, path_l, delim); |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 10277 | if (!query_string) |
| 10278 | return 0; |
| 10279 | |
| 10280 | qs_end = path + path_l; |
| 10281 | arg_start = find_url_param_pos(query_string, qs_end - query_string, |
bedis | 4c75cca | 2012-10-05 08:38:24 +0200 | [diff] [blame] | 10282 | url_param_name, url_param_name_l, |
| 10283 | delim); |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 10284 | if (!arg_start) |
| 10285 | return 0; |
| 10286 | |
| 10287 | value_start = arg_start + url_param_name_l + 1; |
| 10288 | value_end = value_start; |
| 10289 | |
bedis | 4c75cca | 2012-10-05 08:38:24 +0200 | [diff] [blame] | 10290 | while ((value_end < qs_end) && !is_param_delimiter(*value_end, delim)) |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 10291 | value_end++; |
| 10292 | |
| 10293 | *value = value_start; |
| 10294 | *value_l = value_end - value_start; |
Willy Tarreau | 0013433 | 2011-01-04 14:57:34 +0100 | [diff] [blame] | 10295 | return value_end != value_start; |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 10296 | } |
| 10297 | |
| 10298 | static int |
Willy Tarreau | 25c1ebc | 2012-04-25 16:21:44 +0200 | [diff] [blame] | 10299 | 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] | 10300 | const struct arg *args, struct sample *smp, const char *kw) |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 10301 | { |
bedis | 4c75cca | 2012-10-05 08:38:24 +0200 | [diff] [blame] | 10302 | char delim = '?'; |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 10303 | struct http_txn *txn = l7; |
| 10304 | struct http_msg *msg = &txn->req; |
Willy Tarreau | 25c1ebc | 2012-04-25 16:21:44 +0200 | [diff] [blame] | 10305 | |
bedis | 4c75cca | 2012-10-05 08:38:24 +0200 | [diff] [blame] | 10306 | if (!args || args[0].type != ARGT_STR || |
| 10307 | (args[1].type && args[1].type != ARGT_STR)) |
Willy Tarreau | 25c1ebc | 2012-04-25 16:21:44 +0200 | [diff] [blame] | 10308 | return 0; |
| 10309 | |
| 10310 | CHECK_HTTP_MESSAGE_FIRST(); |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 10311 | |
bedis | 4c75cca | 2012-10-05 08:38:24 +0200 | [diff] [blame] | 10312 | if (args[1].type) |
| 10313 | delim = *args[1].data.str.str; |
| 10314 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 10315 | 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] | 10316 | args->data.str.str, args->data.str.len, |
| 10317 | &smp->data.str.str, &smp->data.str.len, |
| 10318 | delim)) |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 10319 | return 0; |
| 10320 | |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 10321 | smp->type = SMP_T_STR; |
| 10322 | smp->flags = SMP_F_VOL_1ST | SMP_F_CONST; |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 10323 | return 1; |
| 10324 | } |
| 10325 | |
Willy Tarreau | a9fddca | 2012-07-31 07:51:48 +0200 | [diff] [blame] | 10326 | /* Return the signed integer value for the specified url parameter (see url_param |
| 10327 | * above). |
| 10328 | */ |
| 10329 | static int |
| 10330 | 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] | 10331 | const struct arg *args, struct sample *smp, const char *kw) |
Willy Tarreau | a9fddca | 2012-07-31 07:51:48 +0200 | [diff] [blame] | 10332 | { |
Willy Tarreau | ef38c39 | 2013-07-22 16:29:32 +0200 | [diff] [blame] | 10333 | 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] | 10334 | |
| 10335 | if (ret > 0) { |
| 10336 | smp->type = SMP_T_UINT; |
| 10337 | smp->data.uint = strl2ic(smp->data.str.str, smp->data.str.len); |
| 10338 | } |
| 10339 | |
| 10340 | return ret; |
| 10341 | } |
| 10342 | |
Neil - HAProxy List | 39c63c5 | 2013-11-04 13:48:42 +0000 | [diff] [blame] | 10343 | /* This produces a 32-bit hash of the concatenation of the first occurrence of |
| 10344 | * the Host header followed by the path component if it begins with a slash ('/'). |
| 10345 | * This means that '*' will not be added, resulting in exactly the first Host |
| 10346 | * entry. If no Host header is found, then the path is used. The resulting value |
| 10347 | * is hashed using the url hash followed by a full avalanche hash and provides a |
| 10348 | * 32-bit integer value. This fetch is useful for tracking per-URL activity on |
| 10349 | * high-traffic sites without having to store whole paths. |
| 10350 | * this differs from the base32 functions in that it includes the url parameters |
| 10351 | * as well as the path |
| 10352 | */ |
| 10353 | static int |
| 10354 | 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] | 10355 | const struct arg *args, struct sample *smp, const char *kw) |
Neil - HAProxy List | 39c63c5 | 2013-11-04 13:48:42 +0000 | [diff] [blame] | 10356 | { |
| 10357 | struct http_txn *txn = l7; |
| 10358 | struct hdr_ctx ctx; |
| 10359 | unsigned int hash = 0; |
| 10360 | char *ptr, *beg, *end; |
| 10361 | int len; |
| 10362 | |
| 10363 | CHECK_HTTP_MESSAGE_FIRST(); |
| 10364 | |
| 10365 | ctx.idx = 0; |
| 10366 | if (http_find_header2("Host", 4, txn->req.chn->buf->p + txn->req.sol, &txn->hdr_idx, &ctx)) { |
| 10367 | /* OK we have the header value in ctx.line+ctx.val for ctx.vlen bytes */ |
| 10368 | ptr = ctx.line + ctx.val; |
| 10369 | len = ctx.vlen; |
| 10370 | while (len--) |
| 10371 | hash = *(ptr++) + (hash << 6) + (hash << 16) - hash; |
| 10372 | } |
| 10373 | |
| 10374 | /* now retrieve the path */ |
| 10375 | end = txn->req.chn->buf->p + txn->req.sol + txn->req.sl.rq.u + txn->req.sl.rq.u_l; |
| 10376 | beg = http_get_path(txn); |
| 10377 | if (!beg) |
| 10378 | beg = end; |
| 10379 | |
| 10380 | for (ptr = beg; ptr < end ; ptr++); |
| 10381 | |
| 10382 | if (beg < ptr && *beg == '/') { |
| 10383 | while (beg < ptr) |
| 10384 | hash = *(beg++) + (hash << 6) + (hash << 16) - hash; |
| 10385 | } |
| 10386 | hash = full_hash(hash); |
| 10387 | |
| 10388 | smp->type = SMP_T_UINT; |
| 10389 | smp->data.uint = hash; |
| 10390 | smp->flags = SMP_F_VOL_1ST; |
| 10391 | return 1; |
| 10392 | } |
| 10393 | |
| 10394 | /* This concatenates the source address with the 32-bit hash of the Host and |
| 10395 | * URL as returned by smp_fetch_base32(). The idea is to have per-source and |
| 10396 | * per-url counters. The result is a binary block from 8 to 20 bytes depending |
| 10397 | * on the source address length. The URL hash is stored before the address so |
| 10398 | * that in environments where IPv6 is insignificant, truncating the output to |
| 10399 | * 8 bytes would still work. |
| 10400 | */ |
| 10401 | static int |
| 10402 | 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] | 10403 | const struct arg *args, struct sample *smp, const char *kw) |
Neil - HAProxy List | 39c63c5 | 2013-11-04 13:48:42 +0000 | [diff] [blame] | 10404 | { |
| 10405 | struct chunk *temp; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 10406 | struct connection *cli_conn = objt_conn(l4->si[0].end); |
Neil - HAProxy List | 39c63c5 | 2013-11-04 13:48:42 +0000 | [diff] [blame] | 10407 | |
Willy Tarreau | e155ec2 | 2013-11-18 18:33:22 +0100 | [diff] [blame] | 10408 | if (!smp_fetch_url32(px, l4, l7, opt, args, smp, kw)) |
Neil - HAProxy List | 39c63c5 | 2013-11-04 13:48:42 +0000 | [diff] [blame] | 10409 | return 0; |
| 10410 | |
| 10411 | temp = get_trash_chunk(); |
| 10412 | memcpy(temp->str + temp->len, &smp->data.uint, sizeof(smp->data.uint)); |
| 10413 | temp->len += sizeof(smp->data.uint); |
| 10414 | |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 10415 | switch (cli_conn->addr.from.ss_family) { |
Neil - HAProxy List | 39c63c5 | 2013-11-04 13:48:42 +0000 | [diff] [blame] | 10416 | case AF_INET: |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 10417 | 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] | 10418 | temp->len += 4; |
| 10419 | break; |
| 10420 | case AF_INET6: |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 10421 | 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] | 10422 | temp->len += 16; |
| 10423 | break; |
| 10424 | default: |
| 10425 | return 0; |
| 10426 | } |
| 10427 | |
| 10428 | smp->data.str = *temp; |
| 10429 | smp->type = SMP_T_BIN; |
| 10430 | return 1; |
| 10431 | } |
| 10432 | |
Willy Tarreau | 185b5c4 | 2012-04-26 15:11:51 +0200 | [diff] [blame] | 10433 | /* This function is used to validate the arguments passed to any "hdr" fetch |
| 10434 | * keyword. These keywords support an optional positive or negative occurrence |
| 10435 | * number. We must ensure that the number is greater than -MAX_HDR_HISTORY. It |
| 10436 | * is assumed that the types are already the correct ones. Returns 0 on error, |
| 10437 | * non-zero if OK. If <err> is not NULL, it will be filled with a pointer to an |
| 10438 | * error message in case of error, that the caller is responsible for freeing. |
| 10439 | * The initial location must either be freeable or NULL. |
| 10440 | */ |
| 10441 | static int val_hdr(struct arg *arg, char **err_msg) |
| 10442 | { |
| 10443 | 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] | 10444 | memprintf(err_msg, "header occurrence must be >= %d", -MAX_HDR_HISTORY); |
Willy Tarreau | 185b5c4 | 2012-04-26 15:11:51 +0200 | [diff] [blame] | 10445 | return 0; |
| 10446 | } |
| 10447 | return 1; |
| 10448 | } |
| 10449 | |
Willy Tarreau | 276fae9 | 2013-07-25 14:36:01 +0200 | [diff] [blame] | 10450 | /* takes an UINT value on input supposed to represent the time since EPOCH, |
| 10451 | * adds an optional offset found in args[0] and emits a string representing |
| 10452 | * the date in RFC-1123/5322 format. |
| 10453 | */ |
| 10454 | static int sample_conv_http_date(const struct arg *args, struct sample *smp) |
| 10455 | { |
| 10456 | const char day[7][4] = { "Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun" }; |
| 10457 | const char mon[12][4] = { "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" }; |
| 10458 | struct chunk *temp; |
| 10459 | struct tm *tm; |
| 10460 | time_t curr_date = smp->data.uint; |
| 10461 | |
| 10462 | /* add offset */ |
| 10463 | if (args && (args[0].type == ARGT_SINT || args[0].type == ARGT_UINT)) |
| 10464 | curr_date += args[0].data.sint; |
| 10465 | |
| 10466 | tm = gmtime(&curr_date); |
| 10467 | |
| 10468 | temp = get_trash_chunk(); |
| 10469 | temp->len = snprintf(temp->str, temp->size - temp->len, |
| 10470 | "%s, %02d %s %04d %02d:%02d:%02d GMT", |
| 10471 | day[tm->tm_wday], tm->tm_mday, mon[tm->tm_mon], 1900+tm->tm_year, |
| 10472 | tm->tm_hour, tm->tm_min, tm->tm_sec); |
| 10473 | |
| 10474 | smp->data.str = *temp; |
| 10475 | smp->type = SMP_T_STR; |
| 10476 | return 1; |
| 10477 | } |
| 10478 | |
Thierry FOURNIER | ad90351 | 2014-04-11 17:51:01 +0200 | [diff] [blame] | 10479 | /* Match language range with language tag. RFC2616 14.4: |
| 10480 | * |
| 10481 | * A language-range matches a language-tag if it exactly equals |
| 10482 | * the tag, or if it exactly equals a prefix of the tag such |
| 10483 | * that the first tag character following the prefix is "-". |
| 10484 | * |
| 10485 | * Return 1 if the strings match, else return 0. |
| 10486 | */ |
| 10487 | static inline int language_range_match(const char *range, int range_len, |
| 10488 | const char *tag, int tag_len) |
| 10489 | { |
| 10490 | const char *end = range + range_len; |
| 10491 | const char *tend = tag + tag_len; |
| 10492 | while (range < end) { |
| 10493 | if (*range == '-' && tag == tend) |
| 10494 | return 1; |
| 10495 | if (*range != *tag || tag == tend) |
| 10496 | return 0; |
| 10497 | range++; |
| 10498 | tag++; |
| 10499 | } |
| 10500 | /* Return true only if the last char of the tag is matched. */ |
| 10501 | return tag == tend; |
| 10502 | } |
| 10503 | |
| 10504 | /* Arguments: The list of expected value, the number of parts returned and the separator */ |
| 10505 | static int sample_conv_q_prefered(const struct arg *args, struct sample *smp) |
| 10506 | { |
| 10507 | const char *al = smp->data.str.str; |
| 10508 | const char *end = al + smp->data.str.len; |
| 10509 | const char *token; |
| 10510 | int toklen; |
| 10511 | int qvalue; |
| 10512 | const char *str; |
| 10513 | const char *w; |
| 10514 | int best_q = 0; |
| 10515 | |
| 10516 | /* Set the constant to the sample, because the output of the |
| 10517 | * function will be peek in the constant configuration string. |
| 10518 | */ |
| 10519 | smp->flags |= SMP_F_CONST; |
| 10520 | smp->data.str.size = 0; |
| 10521 | smp->data.str.str = ""; |
| 10522 | smp->data.str.len = 0; |
| 10523 | |
| 10524 | /* Parse the accept language */ |
| 10525 | while (1) { |
| 10526 | |
| 10527 | /* Jump spaces, quit if the end is detected. */ |
| 10528 | while (al < end && isspace(*al)) |
| 10529 | al++; |
| 10530 | if (al >= end) |
| 10531 | break; |
| 10532 | |
| 10533 | /* Start of the fisrt word. */ |
| 10534 | token = al; |
| 10535 | |
| 10536 | /* Look for separator: isspace(), ',' or ';'. Next value if 0 length word. */ |
| 10537 | while (al < end && *al != ';' && *al != ',' && !isspace(*al)) |
| 10538 | al++; |
| 10539 | if (al == token) |
| 10540 | goto expect_comma; |
| 10541 | |
| 10542 | /* Length of the token. */ |
| 10543 | toklen = al - token; |
| 10544 | qvalue = 1000; |
| 10545 | |
| 10546 | /* Check if the token exists in the list. If the token not exists, |
| 10547 | * jump to the next token. |
| 10548 | */ |
| 10549 | str = args[0].data.str.str; |
| 10550 | w = str; |
| 10551 | while (1) { |
| 10552 | if (*str == ';' || *str == '\0') { |
| 10553 | if (language_range_match(token, toklen, w, str-w)) |
| 10554 | goto look_for_q; |
| 10555 | if (*str == '\0') |
| 10556 | goto expect_comma; |
| 10557 | w = str + 1; |
| 10558 | } |
| 10559 | str++; |
| 10560 | } |
| 10561 | goto expect_comma; |
| 10562 | |
| 10563 | look_for_q: |
| 10564 | |
| 10565 | /* Jump spaces, quit if the end is detected. */ |
| 10566 | while (al < end && isspace(*al)) |
| 10567 | al++; |
| 10568 | if (al >= end) |
| 10569 | goto process_value; |
| 10570 | |
| 10571 | /* If ',' is found, process the result */ |
| 10572 | if (*al == ',') |
| 10573 | goto process_value; |
| 10574 | |
| 10575 | /* If the character is different from ';', look |
| 10576 | * for the end of the header part in best effort. |
| 10577 | */ |
| 10578 | if (*al != ';') |
| 10579 | goto expect_comma; |
| 10580 | |
| 10581 | /* Assumes that the char is ';', now expect "q=". */ |
| 10582 | al++; |
| 10583 | |
| 10584 | /* Jump spaces, process value if the end is detected. */ |
| 10585 | while (al < end && isspace(*al)) |
| 10586 | al++; |
| 10587 | if (al >= end) |
| 10588 | goto process_value; |
| 10589 | |
| 10590 | /* Expect 'q'. If no 'q', continue in best effort */ |
| 10591 | if (*al != 'q') |
| 10592 | goto process_value; |
| 10593 | al++; |
| 10594 | |
| 10595 | /* Jump spaces, process value if the end is detected. */ |
| 10596 | while (al < end && isspace(*al)) |
| 10597 | al++; |
| 10598 | if (al >= end) |
| 10599 | goto process_value; |
| 10600 | |
| 10601 | /* Expect '='. If no '=', continue in best effort */ |
| 10602 | if (*al != '=') |
| 10603 | goto process_value; |
| 10604 | al++; |
| 10605 | |
| 10606 | /* Jump spaces, process value if the end is detected. */ |
| 10607 | while (al < end && isspace(*al)) |
| 10608 | al++; |
| 10609 | if (al >= end) |
| 10610 | goto process_value; |
| 10611 | |
| 10612 | /* Parse the q value. */ |
| 10613 | qvalue = parse_qvalue(al, &al); |
| 10614 | |
| 10615 | process_value: |
| 10616 | |
| 10617 | /* If the new q value is the best q value, then store the associated |
| 10618 | * language in the response. If qvalue is the biggest value (1000), |
| 10619 | * break the process. |
| 10620 | */ |
| 10621 | if (qvalue > best_q) { |
| 10622 | smp->data.str.str = (char *)w; |
| 10623 | smp->data.str.len = str - w; |
| 10624 | if (qvalue >= 1000) |
| 10625 | break; |
| 10626 | best_q = qvalue; |
| 10627 | } |
| 10628 | |
| 10629 | expect_comma: |
| 10630 | |
| 10631 | /* Expect comma or end. If the end is detected, quit the loop. */ |
| 10632 | while (al < end && *al != ',') |
| 10633 | al++; |
| 10634 | if (al >= end) |
| 10635 | break; |
| 10636 | |
| 10637 | /* Comma is found, jump it and restart the analyzer. */ |
| 10638 | al++; |
| 10639 | } |
| 10640 | |
| 10641 | /* Set default value if required. */ |
| 10642 | if (smp->data.str.len == 0 && args[1].type == ARGT_STR) { |
| 10643 | smp->data.str.str = args[1].data.str.str; |
| 10644 | smp->data.str.len = args[1].data.str.len; |
| 10645 | } |
| 10646 | |
| 10647 | /* Return true only if a matching language was found. */ |
| 10648 | return smp->data.str.len != 0; |
| 10649 | } |
| 10650 | |
Willy Tarreau | 4a56897 | 2010-05-12 08:08:50 +0200 | [diff] [blame] | 10651 | /************************************************************************/ |
Willy Tarreau | 25c1ebc | 2012-04-25 16:21:44 +0200 | [diff] [blame] | 10652 | /* All supported ACL keywords must be declared here. */ |
| 10653 | /************************************************************************/ |
| 10654 | |
| 10655 | /* Note: must not be declared <const> as its list will be overwritten. |
| 10656 | * Please take care of keeping this list alphabetically sorted. |
| 10657 | */ |
Willy Tarreau | dc13c11 | 2013-06-21 23:16:39 +0200 | [diff] [blame] | 10658 | static struct acl_kw_list acl_kws = {ILH, { |
Thierry FOURNIER | c5a4e98 | 2014-03-05 16:07:08 +0100 | [diff] [blame] | 10659 | { "base", "base", PAT_MATCH_STR }, |
| 10660 | { "base_beg", "base", PAT_MATCH_BEG }, |
| 10661 | { "base_dir", "base", PAT_MATCH_DIR }, |
| 10662 | { "base_dom", "base", PAT_MATCH_DOM }, |
| 10663 | { "base_end", "base", PAT_MATCH_END }, |
| 10664 | { "base_len", "base", PAT_MATCH_LEN }, |
| 10665 | { "base_reg", "base", PAT_MATCH_REG }, |
| 10666 | { "base_sub", "base", PAT_MATCH_SUB }, |
Willy Tarreau | a7ad50c | 2012-04-29 15:39:40 +0200 | [diff] [blame] | 10667 | |
Thierry FOURNIER | c5a4e98 | 2014-03-05 16:07:08 +0100 | [diff] [blame] | 10668 | { "cook", "req.cook", PAT_MATCH_STR }, |
| 10669 | { "cook_beg", "req.cook", PAT_MATCH_BEG }, |
| 10670 | { "cook_dir", "req.cook", PAT_MATCH_DIR }, |
| 10671 | { "cook_dom", "req.cook", PAT_MATCH_DOM }, |
| 10672 | { "cook_end", "req.cook", PAT_MATCH_END }, |
| 10673 | { "cook_len", "req.cook", PAT_MATCH_LEN }, |
| 10674 | { "cook_reg", "req.cook", PAT_MATCH_REG }, |
| 10675 | { "cook_sub", "req.cook", PAT_MATCH_SUB }, |
Willy Tarreau | 25c1ebc | 2012-04-25 16:21:44 +0200 | [diff] [blame] | 10676 | |
Thierry FOURNIER | c5a4e98 | 2014-03-05 16:07:08 +0100 | [diff] [blame] | 10677 | { "hdr", "req.hdr", PAT_MATCH_STR }, |
| 10678 | { "hdr_beg", "req.hdr", PAT_MATCH_BEG }, |
| 10679 | { "hdr_dir", "req.hdr", PAT_MATCH_DIR }, |
| 10680 | { "hdr_dom", "req.hdr", PAT_MATCH_DOM }, |
| 10681 | { "hdr_end", "req.hdr", PAT_MATCH_END }, |
| 10682 | { "hdr_len", "req.hdr", PAT_MATCH_LEN }, |
| 10683 | { "hdr_reg", "req.hdr", PAT_MATCH_REG }, |
| 10684 | { "hdr_sub", "req.hdr", PAT_MATCH_SUB }, |
Willy Tarreau | 25c1ebc | 2012-04-25 16:21:44 +0200 | [diff] [blame] | 10685 | |
Thierry FOURNIER | c5a4e98 | 2014-03-05 16:07:08 +0100 | [diff] [blame] | 10686 | /* these two declarations uses strings with list storage (in place |
| 10687 | * of tree storage). The basic match is PAT_MATCH_STR, but the indexation |
| 10688 | * and delete functions are relative to the list management. The parse |
| 10689 | * and match method are related to the corresponding fetch methods. This |
| 10690 | * is very particular ACL declaration mode. |
| 10691 | */ |
| 10692 | { "http_auth_group", NULL, PAT_MATCH_STR, NULL, pat_idx_list_str, pat_del_list_ptr, NULL, pat_match_auth }, |
| 10693 | { "method", NULL, PAT_MATCH_STR, pat_parse_meth, pat_idx_list_str, pat_del_list_ptr, NULL, pat_match_meth }, |
Willy Tarreau | 25c1ebc | 2012-04-25 16:21:44 +0200 | [diff] [blame] | 10694 | |
Thierry FOURNIER | c5a4e98 | 2014-03-05 16:07:08 +0100 | [diff] [blame] | 10695 | { "path", "path", PAT_MATCH_STR }, |
| 10696 | { "path_beg", "path", PAT_MATCH_BEG }, |
| 10697 | { "path_dir", "path", PAT_MATCH_DIR }, |
| 10698 | { "path_dom", "path", PAT_MATCH_DOM }, |
| 10699 | { "path_end", "path", PAT_MATCH_END }, |
| 10700 | { "path_len", "path", PAT_MATCH_LEN }, |
| 10701 | { "path_reg", "path", PAT_MATCH_REG }, |
| 10702 | { "path_sub", "path", PAT_MATCH_SUB }, |
Willy Tarreau | 25c1ebc | 2012-04-25 16:21:44 +0200 | [diff] [blame] | 10703 | |
Thierry FOURNIER | c5a4e98 | 2014-03-05 16:07:08 +0100 | [diff] [blame] | 10704 | { "req_ver", "req.ver", PAT_MATCH_STR }, |
| 10705 | { "resp_ver", "res.ver", PAT_MATCH_STR }, |
Willy Tarreau | 25c1ebc | 2012-04-25 16:21:44 +0200 | [diff] [blame] | 10706 | |
Thierry FOURNIER | c5a4e98 | 2014-03-05 16:07:08 +0100 | [diff] [blame] | 10707 | { "scook", "res.cook", PAT_MATCH_STR }, |
| 10708 | { "scook_beg", "res.cook", PAT_MATCH_BEG }, |
| 10709 | { "scook_dir", "res.cook", PAT_MATCH_DIR }, |
| 10710 | { "scook_dom", "res.cook", PAT_MATCH_DOM }, |
| 10711 | { "scook_end", "res.cook", PAT_MATCH_END }, |
| 10712 | { "scook_len", "res.cook", PAT_MATCH_LEN }, |
| 10713 | { "scook_reg", "res.cook", PAT_MATCH_REG }, |
| 10714 | { "scook_sub", "res.cook", PAT_MATCH_SUB }, |
Willy Tarreau | 25c1ebc | 2012-04-25 16:21:44 +0200 | [diff] [blame] | 10715 | |
Thierry FOURNIER | c5a4e98 | 2014-03-05 16:07:08 +0100 | [diff] [blame] | 10716 | { "shdr", "res.hdr", PAT_MATCH_STR }, |
| 10717 | { "shdr_beg", "res.hdr", PAT_MATCH_BEG }, |
| 10718 | { "shdr_dir", "res.hdr", PAT_MATCH_DIR }, |
| 10719 | { "shdr_dom", "res.hdr", PAT_MATCH_DOM }, |
| 10720 | { "shdr_end", "res.hdr", PAT_MATCH_END }, |
| 10721 | { "shdr_len", "res.hdr", PAT_MATCH_LEN }, |
| 10722 | { "shdr_reg", "res.hdr", PAT_MATCH_REG }, |
| 10723 | { "shdr_sub", "res.hdr", PAT_MATCH_SUB }, |
Willy Tarreau | 25c1ebc | 2012-04-25 16:21:44 +0200 | [diff] [blame] | 10724 | |
Thierry FOURNIER | c5a4e98 | 2014-03-05 16:07:08 +0100 | [diff] [blame] | 10725 | { "url", "url", PAT_MATCH_STR }, |
| 10726 | { "url_beg", "url", PAT_MATCH_BEG }, |
| 10727 | { "url_dir", "url", PAT_MATCH_DIR }, |
| 10728 | { "url_dom", "url", PAT_MATCH_DOM }, |
| 10729 | { "url_end", "url", PAT_MATCH_END }, |
| 10730 | { "url_len", "url", PAT_MATCH_LEN }, |
| 10731 | { "url_reg", "url", PAT_MATCH_REG }, |
| 10732 | { "url_sub", "url", PAT_MATCH_SUB }, |
Willy Tarreau | 25c1ebc | 2012-04-25 16:21:44 +0200 | [diff] [blame] | 10733 | |
Thierry FOURNIER | c5a4e98 | 2014-03-05 16:07:08 +0100 | [diff] [blame] | 10734 | { "urlp", "urlp", PAT_MATCH_STR }, |
| 10735 | { "urlp_beg", "urlp", PAT_MATCH_BEG }, |
| 10736 | { "urlp_dir", "urlp", PAT_MATCH_DIR }, |
| 10737 | { "urlp_dom", "urlp", PAT_MATCH_DOM }, |
| 10738 | { "urlp_end", "urlp", PAT_MATCH_END }, |
| 10739 | { "urlp_len", "urlp", PAT_MATCH_LEN }, |
| 10740 | { "urlp_reg", "urlp", PAT_MATCH_REG }, |
| 10741 | { "urlp_sub", "urlp", PAT_MATCH_SUB }, |
Willy Tarreau | 25c1ebc | 2012-04-25 16:21:44 +0200 | [diff] [blame] | 10742 | |
Willy Tarreau | 8ed669b | 2013-01-11 15:49:37 +0100 | [diff] [blame] | 10743 | { /* END */ }, |
Willy Tarreau | 25c1ebc | 2012-04-25 16:21:44 +0200 | [diff] [blame] | 10744 | }}; |
| 10745 | |
| 10746 | /************************************************************************/ |
| 10747 | /* All supported pattern keywords must be declared here. */ |
Willy Tarreau | 4a56897 | 2010-05-12 08:08:50 +0200 | [diff] [blame] | 10748 | /************************************************************************/ |
| 10749 | /* Note: must not be declared <const> as its list will be overwritten */ |
Willy Tarreau | dc13c11 | 2013-06-21 23:16:39 +0200 | [diff] [blame] | 10750 | static struct sample_fetch_kw_list sample_fetch_keywords = {ILH, { |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 10751 | { "base", smp_fetch_base, 0, NULL, SMP_T_STR, SMP_USE_HRQHV }, |
Willy Tarreau | 409bcde | 2013-01-08 00:31:00 +0100 | [diff] [blame] | 10752 | { "base32", smp_fetch_base32, 0, NULL, SMP_T_UINT, SMP_USE_HRQHV }, |
| 10753 | { "base32+src", smp_fetch_base32_src, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV }, |
| 10754 | |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 10755 | { "capture.req.uri", smp_fetch_capture_req_uri, 0, NULL, SMP_T_STR, SMP_USE_HRQHP }, |
| 10756 | { "capture.req.method", smp_fetch_capture_req_method, 0, NULL, SMP_T_STR, SMP_USE_HRQHP }, |
William Lallemand | 65ad6e1 | 2014-01-31 15:08:02 +0100 | [diff] [blame] | 10757 | |
William Lallemand | a43ba4e | 2014-01-28 18:14:25 +0100 | [diff] [blame] | 10758 | /* capture are allocated and are permanent in the session */ |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 10759 | { "capture.req.hdr", smp_fetch_capture_header_req, ARG1(1, UINT), NULL, SMP_T_STR, SMP_USE_HRQHP }, |
| 10760 | { "capture.res.hdr", smp_fetch_capture_header_res, ARG1(1, UINT), NULL, SMP_T_STR, SMP_USE_HRSHP }, |
William Lallemand | a43ba4e | 2014-01-28 18:14:25 +0100 | [diff] [blame] | 10761 | |
Willy Tarreau | 409bcde | 2013-01-08 00:31:00 +0100 | [diff] [blame] | 10762 | /* cookie is valid in both directions (eg: for "stick ...") but cook* |
| 10763 | * are only here to match the ACL's name, are request-only and are used |
| 10764 | * for ACL compatibility only. |
| 10765 | */ |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 10766 | { "cook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRQHV }, |
| 10767 | { "cookie", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRQHV|SMP_USE_HRSHV }, |
Willy Tarreau | 409bcde | 2013-01-08 00:31:00 +0100 | [diff] [blame] | 10768 | { "cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV }, |
| 10769 | { "cook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV }, |
| 10770 | |
| 10771 | /* hdr is valid in both directions (eg: for "stick ...") but hdr_* are |
| 10772 | * only here to match the ACL's name, are request-only and are used for |
| 10773 | * ACL compatibility only. |
| 10774 | */ |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 10775 | { "hdr", smp_fetch_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRQHV|SMP_USE_HRSHV }, |
Willy Tarreau | 409bcde | 2013-01-08 00:31:00 +0100 | [diff] [blame] | 10776 | { "hdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV }, |
| 10777 | { "hdr_ip", smp_fetch_hdr_ip, ARG2(0,STR,SINT), val_hdr, SMP_T_IPV4, SMP_USE_HRQHV }, |
| 10778 | { "hdr_val", smp_fetch_hdr_val, ARG2(0,STR,SINT), val_hdr, SMP_T_UINT, SMP_USE_HRQHV }, |
| 10779 | |
Willy Tarreau | 0a0daec | 2013-04-02 22:44:58 +0200 | [diff] [blame] | 10780 | { "http_auth", smp_fetch_http_auth, ARG1(1,USR), NULL, SMP_T_BOOL, SMP_USE_HRQHV }, |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 10781 | { "http_auth_group", smp_fetch_http_auth_grp, ARG1(1,USR), NULL, SMP_T_STR, SMP_USE_HRQHV }, |
Willy Tarreau | 409bcde | 2013-01-08 00:31:00 +0100 | [diff] [blame] | 10782 | { "http_first_req", smp_fetch_http_first_req, 0, NULL, SMP_T_BOOL, SMP_USE_HRQHP }, |
Thierry FOURNIER | d437314 | 2013-12-17 01:10:10 +0100 | [diff] [blame] | 10783 | { "method", smp_fetch_meth, 0, NULL, SMP_T_METH, SMP_USE_HRQHP }, |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 10784 | { "path", smp_fetch_path, 0, NULL, SMP_T_STR, SMP_USE_HRQHV }, |
Willy Tarreau | 18ed256 | 2013-01-14 15:56:36 +0100 | [diff] [blame] | 10785 | |
| 10786 | /* HTTP protocol on the request path */ |
| 10787 | { "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] | 10788 | { "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] | 10789 | |
| 10790 | /* HTTP version on the request path */ |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 10791 | { "req.ver", smp_fetch_rqver, 0, NULL, SMP_T_STR, SMP_USE_HRQHV }, |
| 10792 | { "req_ver", smp_fetch_rqver, 0, NULL, SMP_T_STR, SMP_USE_HRQHV }, |
Willy Tarreau | 18ed256 | 2013-01-14 15:56:36 +0100 | [diff] [blame] | 10793 | |
| 10794 | /* HTTP version on the response path */ |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 10795 | { "res.ver", smp_fetch_stver, 0, NULL, SMP_T_STR, SMP_USE_HRSHV }, |
| 10796 | { "resp_ver", smp_fetch_stver, 0, NULL, SMP_T_STR, SMP_USE_HRSHV }, |
Willy Tarreau | 409bcde | 2013-01-08 00:31:00 +0100 | [diff] [blame] | 10797 | |
Willy Tarreau | 18ed256 | 2013-01-14 15:56:36 +0100 | [diff] [blame] | 10798 | /* explicit req.{cook,hdr} are used to force the fetch direction to be request-only */ |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 10799 | { "req.cook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRQHV }, |
Willy Tarreau | 18ed256 | 2013-01-14 15:56:36 +0100 | [diff] [blame] | 10800 | { "req.cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV }, |
| 10801 | { "req.cook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV }, |
| 10802 | |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 10803 | { "req.fhdr", smp_fetch_fhdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRQHV }, |
Willy Tarreau | 04ff9f1 | 2013-06-10 18:39:42 +0200 | [diff] [blame] | 10804 | { "req.fhdr_cnt", smp_fetch_fhdr_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV }, |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 10805 | { "req.hdr", smp_fetch_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRQHV }, |
Willy Tarreau | 18ed256 | 2013-01-14 15:56:36 +0100 | [diff] [blame] | 10806 | { "req.hdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV }, |
| 10807 | { "req.hdr_ip", smp_fetch_hdr_ip, ARG2(0,STR,SINT), val_hdr, SMP_T_IPV4, SMP_USE_HRQHV }, |
| 10808 | { "req.hdr_val", smp_fetch_hdr_val, ARG2(0,STR,SINT), val_hdr, SMP_T_UINT, SMP_USE_HRQHV }, |
| 10809 | |
| 10810 | /* explicit req.{cook,hdr} are used to force the fetch direction to be response-only */ |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 10811 | { "res.cook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRSHV }, |
Willy Tarreau | 18ed256 | 2013-01-14 15:56:36 +0100 | [diff] [blame] | 10812 | { "res.cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRSHV }, |
| 10813 | { "res.cook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRSHV }, |
| 10814 | |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 10815 | { "res.fhdr", smp_fetch_fhdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRSHV }, |
Willy Tarreau | 04ff9f1 | 2013-06-10 18:39:42 +0200 | [diff] [blame] | 10816 | { "res.fhdr_cnt", smp_fetch_fhdr_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRSHV }, |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 10817 | { "res.hdr", smp_fetch_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRSHV }, |
Willy Tarreau | 18ed256 | 2013-01-14 15:56:36 +0100 | [diff] [blame] | 10818 | { "res.hdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRSHV }, |
| 10819 | { "res.hdr_ip", smp_fetch_hdr_ip, ARG2(0,STR,SINT), val_hdr, SMP_T_IPV4, SMP_USE_HRSHV }, |
| 10820 | { "res.hdr_val", smp_fetch_hdr_val, ARG2(0,STR,SINT), val_hdr, SMP_T_UINT, SMP_USE_HRSHV }, |
| 10821 | |
Willy Tarreau | 409bcde | 2013-01-08 00:31:00 +0100 | [diff] [blame] | 10822 | /* scook is valid only on the response and is used for ACL compatibility */ |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 10823 | { "scook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRSHV }, |
Willy Tarreau | 409bcde | 2013-01-08 00:31:00 +0100 | [diff] [blame] | 10824 | { "scook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRSHV }, |
| 10825 | { "scook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRSHV }, |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 10826 | { "set-cookie", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRSHV }, /* deprecated */ |
Willy Tarreau | 409bcde | 2013-01-08 00:31:00 +0100 | [diff] [blame] | 10827 | |
| 10828 | /* shdr is valid only on the response and is used for ACL compatibility */ |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 10829 | { "shdr", smp_fetch_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRSHV }, |
Willy Tarreau | 409bcde | 2013-01-08 00:31:00 +0100 | [diff] [blame] | 10830 | { "shdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRSHV }, |
| 10831 | { "shdr_ip", smp_fetch_hdr_ip, ARG2(0,STR,SINT), val_hdr, SMP_T_IPV4, SMP_USE_HRSHV }, |
| 10832 | { "shdr_val", smp_fetch_hdr_val, ARG2(0,STR,SINT), val_hdr, SMP_T_UINT, SMP_USE_HRSHV }, |
| 10833 | |
| 10834 | { "status", smp_fetch_stcode, 0, NULL, SMP_T_UINT, SMP_USE_HRSHP }, |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 10835 | { "url", smp_fetch_url, 0, NULL, SMP_T_STR, SMP_USE_HRQHV }, |
Neil - HAProxy List | 39c63c5 | 2013-11-04 13:48:42 +0000 | [diff] [blame] | 10836 | { "url32", smp_fetch_url32, 0, NULL, SMP_T_UINT, SMP_USE_HRQHV }, |
| 10837 | { "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] | 10838 | { "url_ip", smp_fetch_url_ip, 0, NULL, SMP_T_IPV4, SMP_USE_HRQHV }, |
| 10839 | { "url_port", smp_fetch_url_port, 0, NULL, SMP_T_UINT, SMP_USE_HRQHV }, |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 10840 | { "url_param", smp_fetch_url_param, ARG2(1,STR,STR), NULL, SMP_T_STR, SMP_USE_HRQHV }, |
| 10841 | { "urlp" , smp_fetch_url_param, ARG2(1,STR,STR), NULL, SMP_T_STR, SMP_USE_HRQHV }, |
Willy Tarreau | 409bcde | 2013-01-08 00:31:00 +0100 | [diff] [blame] | 10842 | { "urlp_val", smp_fetch_url_param_val, ARG2(1,STR,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV }, |
| 10843 | { /* END */ }, |
Willy Tarreau | 4a56897 | 2010-05-12 08:08:50 +0200 | [diff] [blame] | 10844 | }}; |
| 10845 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 10846 | |
Willy Tarreau | 276fae9 | 2013-07-25 14:36:01 +0200 | [diff] [blame] | 10847 | /* Note: must not be declared <const> as its list will be overwritten */ |
| 10848 | static struct sample_conv_kw_list sample_conv_kws = {ILH, { |
Thierry FOURNIER | ad90351 | 2014-04-11 17:51:01 +0200 | [diff] [blame] | 10849 | { "http_date", sample_conv_http_date, ARG1(0,SINT), NULL, SMP_T_UINT, SMP_T_STR}, |
| 10850 | { "language", sample_conv_q_prefered, ARG2(1,STR,STR), NULL, SMP_T_STR, SMP_T_STR}, |
Willy Tarreau | 276fae9 | 2013-07-25 14:36:01 +0200 | [diff] [blame] | 10851 | { NULL, NULL, 0, 0, 0 }, |
| 10852 | }}; |
| 10853 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 10854 | __attribute__((constructor)) |
| 10855 | static void __http_protocol_init(void) |
| 10856 | { |
| 10857 | acl_register_keywords(&acl_kws); |
Willy Tarreau | 1278578 | 2012-04-27 21:37:17 +0200 | [diff] [blame] | 10858 | sample_register_fetches(&sample_fetch_keywords); |
Willy Tarreau | 276fae9 | 2013-07-25 14:36:01 +0200 | [diff] [blame] | 10859 | sample_register_convs(&sample_conv_kws); |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 10860 | } |
| 10861 | |
| 10862 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 10863 | /* |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 10864 | * Local variables: |
| 10865 | * c-indent-level: 8 |
| 10866 | * c-basic-offset: 8 |
| 10867 | * End: |
| 10868 | */ |