Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1 | /* |
| 2 | * HTTP protocol analyzer |
| 3 | * |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 4 | * Copyright 2000-2011 Willy Tarreau <w@1wt.eu> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 5 | * |
| 6 | * This program is free software; you can redistribute it and/or |
| 7 | * modify it under the terms of the GNU General Public License |
| 8 | * as published by the Free Software Foundation; either version |
| 9 | * 2 of the License, or (at your option) any later version. |
| 10 | * |
| 11 | */ |
| 12 | |
| 13 | #include <ctype.h> |
| 14 | #include <errno.h> |
| 15 | #include <fcntl.h> |
| 16 | #include <stdio.h> |
| 17 | #include <stdlib.h> |
| 18 | #include <string.h> |
| 19 | #include <syslog.h> |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 20 | #include <time.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 21 | |
| 22 | #include <sys/socket.h> |
| 23 | #include <sys/stat.h> |
| 24 | #include <sys/types.h> |
| 25 | |
Willy Tarreau | b05405a | 2012-01-23 15:35:52 +0100 | [diff] [blame] | 26 | #include <netinet/tcp.h> |
| 27 | |
Willy Tarreau | 2dd0d47 | 2006-06-29 17:53:05 +0200 | [diff] [blame] | 28 | #include <common/appsession.h> |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 29 | #include <common/base64.h> |
Willy Tarreau | c7e4238 | 2012-08-24 19:22:53 +0200 | [diff] [blame] | 30 | #include <common/chunk.h> |
Willy Tarreau | 2dd0d47 | 2006-06-29 17:53:05 +0200 | [diff] [blame] | 31 | #include <common/compat.h> |
| 32 | #include <common/config.h> |
Willy Tarreau | a4cd1f5 | 2006-12-16 19:57:26 +0100 | [diff] [blame] | 33 | #include <common/debug.h> |
Willy Tarreau | 2dd0d47 | 2006-06-29 17:53:05 +0200 | [diff] [blame] | 34 | #include <common/memory.h> |
| 35 | #include <common/mini-clist.h> |
| 36 | #include <common/standard.h> |
Willy Tarreau | 0c303ee | 2008-07-07 00:09:58 +0200 | [diff] [blame] | 37 | #include <common/ticks.h> |
Willy Tarreau | 2dd0d47 | 2006-06-29 17:53:05 +0200 | [diff] [blame] | 38 | #include <common/time.h> |
| 39 | #include <common/uri_auth.h> |
| 40 | #include <common/version.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 41 | |
| 42 | #include <types/capture.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 43 | #include <types/global.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 44 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 45 | #include <proto/acl.h> |
Willy Tarreau | 61612d4 | 2012-04-19 18:42:05 +0200 | [diff] [blame] | 46 | #include <proto/arg.h> |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 47 | #include <proto/auth.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 48 | #include <proto/backend.h> |
Willy Tarreau | c7e4238 | 2012-08-24 19:22:53 +0200 | [diff] [blame] | 49 | #include <proto/channel.h> |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 50 | #include <proto/checks.h> |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 51 | #include <proto/compression.h> |
Willy Tarreau | 9186126 | 2007-10-17 17:06:05 +0200 | [diff] [blame] | 52 | #include <proto/dumpstats.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 53 | #include <proto/fd.h> |
Willy Tarreau | 03fa5df | 2010-05-24 21:02:37 +0200 | [diff] [blame] | 54 | #include <proto/frontend.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 55 | #include <proto/log.h> |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 56 | #include <proto/hdr_idx.h> |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 57 | #include <proto/pattern.h> |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 58 | #include <proto/proto_tcp.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 59 | #include <proto/proto_http.h> |
Willy Tarreau | 7f062c4 | 2009-03-05 18:43:00 +0100 | [diff] [blame] | 60 | #include <proto/proxy.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 61 | #include <proto/queue.h> |
Willy Tarreau | cd3b094 | 2012-04-27 21:52:18 +0200 | [diff] [blame] | 62 | #include <proto/sample.h> |
Willy Tarreau | 7f062c4 | 2009-03-05 18:43:00 +0100 | [diff] [blame] | 63 | #include <proto/server.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 64 | #include <proto/session.h> |
Willy Tarreau | cff6411 | 2008-11-03 06:26:53 +0100 | [diff] [blame] | 65 | #include <proto/stream_interface.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 66 | #include <proto/task.h> |
| 67 | |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 68 | const char HTTP_100[] = |
| 69 | "HTTP/1.1 100 Continue\r\n\r\n"; |
| 70 | |
| 71 | const struct chunk http_100_chunk = { |
| 72 | .str = (char *)&HTTP_100, |
| 73 | .len = sizeof(HTTP_100)-1 |
| 74 | }; |
| 75 | |
Willy Tarreau | a9679ac | 2010-01-03 17:32:57 +0100 | [diff] [blame] | 76 | /* Warning: no "connection" header is provided with the 3xx messages below */ |
Willy Tarreau | b463dfb | 2008-06-07 23:08:56 +0200 | [diff] [blame] | 77 | const char *HTTP_301 = |
Willy Tarreau | bc5aa19 | 2010-01-03 15:09:36 +0100 | [diff] [blame] | 78 | "HTTP/1.1 301 Moved Permanently\r\n" |
Willy Tarreau | bc5aa19 | 2010-01-03 15:09:36 +0100 | [diff] [blame] | 79 | "Content-length: 0\r\n" |
Willy Tarreau | b463dfb | 2008-06-07 23:08:56 +0200 | [diff] [blame] | 80 | "Location: "; /* not terminated since it will be concatenated with the URL */ |
| 81 | |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 82 | const char *HTTP_302 = |
Willy Tarreau | bc5aa19 | 2010-01-03 15:09:36 +0100 | [diff] [blame] | 83 | "HTTP/1.1 302 Found\r\n" |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 84 | "Cache-Control: no-cache\r\n" |
Willy Tarreau | bc5aa19 | 2010-01-03 15:09:36 +0100 | [diff] [blame] | 85 | "Content-length: 0\r\n" |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 86 | "Location: "; /* not terminated since it will be concatenated with the URL */ |
| 87 | |
| 88 | /* same as 302 except that the browser MUST retry with the GET method */ |
| 89 | const char *HTTP_303 = |
Willy Tarreau | bc5aa19 | 2010-01-03 15:09:36 +0100 | [diff] [blame] | 90 | "HTTP/1.1 303 See Other\r\n" |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 91 | "Cache-Control: no-cache\r\n" |
Willy Tarreau | bc5aa19 | 2010-01-03 15:09:36 +0100 | [diff] [blame] | 92 | "Content-length: 0\r\n" |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 93 | "Location: "; /* not terminated since it will be concatenated with the URL */ |
| 94 | |
Yves Lafon | 3e8d1ae | 2013-03-11 11:06:05 -0400 | [diff] [blame] | 95 | |
| 96 | /* same as 302 except that the browser MUST retry with the same method */ |
| 97 | const char *HTTP_307 = |
| 98 | "HTTP/1.1 307 Temporary Redirect\r\n" |
| 99 | "Cache-Control: no-cache\r\n" |
| 100 | "Content-length: 0\r\n" |
| 101 | "Location: "; /* not terminated since it will be concatenated with the URL */ |
| 102 | |
| 103 | /* same as 301 except that the browser MUST retry with the same method */ |
| 104 | const char *HTTP_308 = |
| 105 | "HTTP/1.1 308 Permanent Redirect\r\n" |
| 106 | "Content-length: 0\r\n" |
| 107 | "Location: "; /* not terminated since it will be concatenated with the URL */ |
| 108 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 109 | /* Warning: this one is an sprintf() fmt string, with <realm> as its only argument */ |
| 110 | const char *HTTP_401_fmt = |
| 111 | "HTTP/1.0 401 Unauthorized\r\n" |
| 112 | "Cache-Control: no-cache\r\n" |
| 113 | "Connection: close\r\n" |
Willy Tarreau | 791d66d | 2006-07-08 16:53:38 +0200 | [diff] [blame] | 114 | "Content-Type: text/html\r\n" |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 115 | "WWW-Authenticate: Basic realm=\"%s\"\r\n" |
| 116 | "\r\n" |
| 117 | "<html><body><h1>401 Unauthorized</h1>\nYou need a valid user and password to access this content.\n</body></html>\n"; |
| 118 | |
Willy Tarreau | 844a7e7 | 2010-01-31 21:46:18 +0100 | [diff] [blame] | 119 | const char *HTTP_407_fmt = |
| 120 | "HTTP/1.0 407 Unauthorized\r\n" |
| 121 | "Cache-Control: no-cache\r\n" |
| 122 | "Connection: close\r\n" |
| 123 | "Content-Type: text/html\r\n" |
| 124 | "Proxy-Authenticate: Basic realm=\"%s\"\r\n" |
| 125 | "\r\n" |
| 126 | "<html><body><h1>401 Unauthorized</h1>\nYou need a valid user and password to access this content.\n</body></html>\n"; |
| 127 | |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 128 | |
| 129 | const int http_err_codes[HTTP_ERR_SIZE] = { |
Willy Tarreau | ae94d4d | 2011-05-11 16:28:49 +0200 | [diff] [blame] | 130 | [HTTP_ERR_200] = 200, /* used by "monitor-uri" */ |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 131 | [HTTP_ERR_400] = 400, |
| 132 | [HTTP_ERR_403] = 403, |
| 133 | [HTTP_ERR_408] = 408, |
| 134 | [HTTP_ERR_500] = 500, |
| 135 | [HTTP_ERR_502] = 502, |
| 136 | [HTTP_ERR_503] = 503, |
| 137 | [HTTP_ERR_504] = 504, |
| 138 | }; |
| 139 | |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 140 | static const char *http_err_msgs[HTTP_ERR_SIZE] = { |
Willy Tarreau | ae94d4d | 2011-05-11 16:28:49 +0200 | [diff] [blame] | 141 | [HTTP_ERR_200] = |
| 142 | "HTTP/1.0 200 OK\r\n" |
| 143 | "Cache-Control: no-cache\r\n" |
| 144 | "Connection: close\r\n" |
| 145 | "Content-Type: text/html\r\n" |
| 146 | "\r\n" |
| 147 | "<html><body><h1>200 OK</h1>\nService ready.\n</body></html>\n", |
| 148 | |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 149 | [HTTP_ERR_400] = |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 150 | "HTTP/1.0 400 Bad request\r\n" |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 151 | "Cache-Control: no-cache\r\n" |
| 152 | "Connection: close\r\n" |
| 153 | "Content-Type: text/html\r\n" |
| 154 | "\r\n" |
| 155 | "<html><body><h1>400 Bad request</h1>\nYour browser sent an invalid request.\n</body></html>\n", |
| 156 | |
| 157 | [HTTP_ERR_403] = |
| 158 | "HTTP/1.0 403 Forbidden\r\n" |
| 159 | "Cache-Control: no-cache\r\n" |
| 160 | "Connection: close\r\n" |
| 161 | "Content-Type: text/html\r\n" |
| 162 | "\r\n" |
| 163 | "<html><body><h1>403 Forbidden</h1>\nRequest forbidden by administrative rules.\n</body></html>\n", |
| 164 | |
| 165 | [HTTP_ERR_408] = |
| 166 | "HTTP/1.0 408 Request Time-out\r\n" |
| 167 | "Cache-Control: no-cache\r\n" |
| 168 | "Connection: close\r\n" |
| 169 | "Content-Type: text/html\r\n" |
| 170 | "\r\n" |
| 171 | "<html><body><h1>408 Request Time-out</h1>\nYour browser didn't send a complete request in time.\n</body></html>\n", |
| 172 | |
| 173 | [HTTP_ERR_500] = |
| 174 | "HTTP/1.0 500 Server Error\r\n" |
| 175 | "Cache-Control: no-cache\r\n" |
| 176 | "Connection: close\r\n" |
| 177 | "Content-Type: text/html\r\n" |
| 178 | "\r\n" |
| 179 | "<html><body><h1>500 Server Error</h1>\nAn internal server error occured.\n</body></html>\n", |
| 180 | |
| 181 | [HTTP_ERR_502] = |
| 182 | "HTTP/1.0 502 Bad Gateway\r\n" |
| 183 | "Cache-Control: no-cache\r\n" |
| 184 | "Connection: close\r\n" |
| 185 | "Content-Type: text/html\r\n" |
| 186 | "\r\n" |
| 187 | "<html><body><h1>502 Bad Gateway</h1>\nThe server returned an invalid or incomplete response.\n</body></html>\n", |
| 188 | |
| 189 | [HTTP_ERR_503] = |
| 190 | "HTTP/1.0 503 Service Unavailable\r\n" |
| 191 | "Cache-Control: no-cache\r\n" |
| 192 | "Connection: close\r\n" |
| 193 | "Content-Type: text/html\r\n" |
| 194 | "\r\n" |
| 195 | "<html><body><h1>503 Service Unavailable</h1>\nNo server is available to handle this request.\n</body></html>\n", |
| 196 | |
| 197 | [HTTP_ERR_504] = |
| 198 | "HTTP/1.0 504 Gateway Time-out\r\n" |
| 199 | "Cache-Control: no-cache\r\n" |
| 200 | "Connection: close\r\n" |
| 201 | "Content-Type: text/html\r\n" |
| 202 | "\r\n" |
| 203 | "<html><body><h1>504 Gateway Time-out</h1>\nThe server didn't respond in time.\n</body></html>\n", |
| 204 | |
| 205 | }; |
| 206 | |
Cyril Bonté | 19979e1 | 2012-04-04 12:57:21 +0200 | [diff] [blame] | 207 | /* status codes available for the stats admin page (strictly 4 chars length) */ |
| 208 | const char *stat_status_codes[STAT_STATUS_SIZE] = { |
| 209 | [STAT_STATUS_DENY] = "DENY", |
| 210 | [STAT_STATUS_DONE] = "DONE", |
| 211 | [STAT_STATUS_ERRP] = "ERRP", |
| 212 | [STAT_STATUS_EXCD] = "EXCD", |
| 213 | [STAT_STATUS_NONE] = "NONE", |
| 214 | [STAT_STATUS_PART] = "PART", |
| 215 | [STAT_STATUS_UNKN] = "UNKN", |
| 216 | }; |
| 217 | |
| 218 | |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 219 | /* We must put the messages here since GCC cannot initialize consts depending |
| 220 | * on strlen(). |
| 221 | */ |
| 222 | struct chunk http_err_chunks[HTTP_ERR_SIZE]; |
| 223 | |
Willy Tarreau | a890d07 | 2013-04-02 12:01:06 +0200 | [diff] [blame] | 224 | /* this struct is used between calls to smp_fetch_hdr() or smp_fetch_cookie() */ |
| 225 | static struct hdr_ctx static_hdr_ctx; |
| 226 | |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 227 | #define FD_SETS_ARE_BITFIELDS |
| 228 | #ifdef FD_SETS_ARE_BITFIELDS |
| 229 | /* |
| 230 | * This map is used with all the FD_* macros to check whether a particular bit |
| 231 | * is set or not. Each bit represents an ACSII code. FD_SET() sets those bytes |
| 232 | * which should be encoded. When FD_ISSET() returns non-zero, it means that the |
| 233 | * byte should be encoded. Be careful to always pass bytes from 0 to 255 |
| 234 | * exclusively to the macros. |
| 235 | */ |
| 236 | fd_set hdr_encode_map[(sizeof(fd_set) > (256/8)) ? 1 : ((256/8) / sizeof(fd_set))]; |
| 237 | fd_set url_encode_map[(sizeof(fd_set) > (256/8)) ? 1 : ((256/8) / sizeof(fd_set))]; |
| 238 | |
| 239 | #else |
| 240 | #error "Check if your OS uses bitfields for fd_sets" |
| 241 | #endif |
| 242 | |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 243 | void init_proto_http() |
| 244 | { |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 245 | int i; |
| 246 | char *tmp; |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 247 | int msg; |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 248 | |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 249 | for (msg = 0; msg < HTTP_ERR_SIZE; msg++) { |
| 250 | if (!http_err_msgs[msg]) { |
| 251 | Alert("Internal error: no message defined for HTTP return code %d. Aborting.\n", msg); |
| 252 | abort(); |
| 253 | } |
| 254 | |
| 255 | http_err_chunks[msg].str = (char *)http_err_msgs[msg]; |
| 256 | http_err_chunks[msg].len = strlen(http_err_msgs[msg]); |
| 257 | } |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 258 | |
| 259 | /* initialize the log header encoding map : '{|}"#' should be encoded with |
| 260 | * '#' as prefix, as well as non-printable characters ( <32 or >= 127 ). |
| 261 | * URL encoding only requires '"', '#' to be encoded as well as non- |
| 262 | * printable characters above. |
| 263 | */ |
| 264 | memset(hdr_encode_map, 0, sizeof(hdr_encode_map)); |
| 265 | memset(url_encode_map, 0, sizeof(url_encode_map)); |
| 266 | for (i = 0; i < 32; i++) { |
| 267 | FD_SET(i, hdr_encode_map); |
| 268 | FD_SET(i, url_encode_map); |
| 269 | } |
| 270 | for (i = 127; i < 256; i++) { |
| 271 | FD_SET(i, hdr_encode_map); |
| 272 | FD_SET(i, url_encode_map); |
| 273 | } |
| 274 | |
| 275 | tmp = "\"#{|}"; |
| 276 | while (*tmp) { |
| 277 | FD_SET(*tmp, hdr_encode_map); |
| 278 | tmp++; |
| 279 | } |
| 280 | |
| 281 | tmp = "\"#"; |
| 282 | while (*tmp) { |
| 283 | FD_SET(*tmp, url_encode_map); |
| 284 | tmp++; |
| 285 | } |
Willy Tarreau | 332f8bf | 2007-05-13 21:36:56 +0200 | [diff] [blame] | 286 | |
| 287 | /* memory allocations */ |
| 288 | pool2_requri = create_pool("requri", REQURI_LEN, MEM_F_SHARED); |
William Lallemand | a73203e | 2012-03-12 12:48:57 +0100 | [diff] [blame] | 289 | pool2_uniqueid = create_pool("uniqueid", UNIQUEID_LEN, MEM_F_SHARED); |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 290 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 291 | |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 292 | /* |
| 293 | * We have 26 list of methods (1 per first letter), each of which can have |
| 294 | * up to 3 entries (2 valid, 1 null). |
| 295 | */ |
| 296 | struct http_method_desc { |
Willy Tarreau | c8987b3 | 2013-12-06 23:43:17 +0100 | [diff] [blame] | 297 | enum http_meth_t meth; |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 298 | int len; |
| 299 | const char text[8]; |
| 300 | }; |
| 301 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 302 | const struct http_method_desc http_methods[26][3] = { |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 303 | ['C' - 'A'] = { |
| 304 | [0] = { .meth = HTTP_METH_CONNECT , .len=7, .text="CONNECT" }, |
| 305 | }, |
| 306 | ['D' - 'A'] = { |
| 307 | [0] = { .meth = HTTP_METH_DELETE , .len=6, .text="DELETE" }, |
| 308 | }, |
| 309 | ['G' - 'A'] = { |
| 310 | [0] = { .meth = HTTP_METH_GET , .len=3, .text="GET" }, |
| 311 | }, |
| 312 | ['H' - 'A'] = { |
| 313 | [0] = { .meth = HTTP_METH_HEAD , .len=4, .text="HEAD" }, |
| 314 | }, |
| 315 | ['P' - 'A'] = { |
| 316 | [0] = { .meth = HTTP_METH_POST , .len=4, .text="POST" }, |
| 317 | [1] = { .meth = HTTP_METH_PUT , .len=3, .text="PUT" }, |
| 318 | }, |
| 319 | ['T' - 'A'] = { |
| 320 | [0] = { .meth = HTTP_METH_TRACE , .len=5, .text="TRACE" }, |
| 321 | }, |
| 322 | /* rest is empty like this : |
| 323 | * [1] = { .meth = HTTP_METH_NONE , .len=0, .text="" }, |
| 324 | */ |
| 325 | }; |
| 326 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 327 | /* It is about twice as fast on recent architectures to lookup a byte in a |
matt.farnsworth@nokia.com | 1c2ab96 | 2008-04-14 20:47:37 +0200 | [diff] [blame] | 328 | * table than to perform a boolean AND or OR between two tests. Refer to |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 329 | * RFC2616 for those chars. |
| 330 | */ |
| 331 | |
| 332 | const char http_is_spht[256] = { |
| 333 | [' '] = 1, ['\t'] = 1, |
| 334 | }; |
| 335 | |
| 336 | const char http_is_crlf[256] = { |
| 337 | ['\r'] = 1, ['\n'] = 1, |
| 338 | }; |
| 339 | |
| 340 | const char http_is_lws[256] = { |
| 341 | [' '] = 1, ['\t'] = 1, |
| 342 | ['\r'] = 1, ['\n'] = 1, |
| 343 | }; |
| 344 | |
| 345 | const char http_is_sep[256] = { |
| 346 | ['('] = 1, [')'] = 1, ['<'] = 1, ['>'] = 1, |
| 347 | ['@'] = 1, [','] = 1, [';'] = 1, [':'] = 1, |
| 348 | ['"'] = 1, ['/'] = 1, ['['] = 1, [']'] = 1, |
| 349 | ['{'] = 1, ['}'] = 1, ['?'] = 1, ['='] = 1, |
| 350 | [' '] = 1, ['\t'] = 1, ['\\'] = 1, |
| 351 | }; |
| 352 | |
| 353 | const char http_is_ctl[256] = { |
| 354 | [0 ... 31] = 1, |
| 355 | [127] = 1, |
| 356 | }; |
| 357 | |
| 358 | /* |
| 359 | * A token is any ASCII char that is neither a separator nor a CTL char. |
| 360 | * Do not overwrite values in assignment since gcc-2.95 will not handle |
| 361 | * them correctly. Instead, define every non-CTL char's status. |
| 362 | */ |
| 363 | const char http_is_token[256] = { |
| 364 | [' '] = 0, ['!'] = 1, ['"'] = 0, ['#'] = 1, |
| 365 | ['$'] = 1, ['%'] = 1, ['&'] = 1, ['\''] = 1, |
| 366 | ['('] = 0, [')'] = 0, ['*'] = 1, ['+'] = 1, |
| 367 | [','] = 0, ['-'] = 1, ['.'] = 1, ['/'] = 0, |
| 368 | ['0'] = 1, ['1'] = 1, ['2'] = 1, ['3'] = 1, |
| 369 | ['4'] = 1, ['5'] = 1, ['6'] = 1, ['7'] = 1, |
| 370 | ['8'] = 1, ['9'] = 1, [':'] = 0, [';'] = 0, |
| 371 | ['<'] = 0, ['='] = 0, ['>'] = 0, ['?'] = 0, |
| 372 | ['@'] = 0, ['A'] = 1, ['B'] = 1, ['C'] = 1, |
| 373 | ['D'] = 1, ['E'] = 1, ['F'] = 1, ['G'] = 1, |
| 374 | ['H'] = 1, ['I'] = 1, ['J'] = 1, ['K'] = 1, |
| 375 | ['L'] = 1, ['M'] = 1, ['N'] = 1, ['O'] = 1, |
| 376 | ['P'] = 1, ['Q'] = 1, ['R'] = 1, ['S'] = 1, |
| 377 | ['T'] = 1, ['U'] = 1, ['V'] = 1, ['W'] = 1, |
| 378 | ['X'] = 1, ['Y'] = 1, ['Z'] = 1, ['['] = 0, |
| 379 | ['\\'] = 0, [']'] = 0, ['^'] = 1, ['_'] = 1, |
| 380 | ['`'] = 1, ['a'] = 1, ['b'] = 1, ['c'] = 1, |
| 381 | ['d'] = 1, ['e'] = 1, ['f'] = 1, ['g'] = 1, |
| 382 | ['h'] = 1, ['i'] = 1, ['j'] = 1, ['k'] = 1, |
| 383 | ['l'] = 1, ['m'] = 1, ['n'] = 1, ['o'] = 1, |
| 384 | ['p'] = 1, ['q'] = 1, ['r'] = 1, ['s'] = 1, |
| 385 | ['t'] = 1, ['u'] = 1, ['v'] = 1, ['w'] = 1, |
| 386 | ['x'] = 1, ['y'] = 1, ['z'] = 1, ['{'] = 0, |
| 387 | ['|'] = 1, ['}'] = 0, ['~'] = 1, |
| 388 | }; |
| 389 | |
| 390 | |
Willy Tarreau | 4b89ad4 | 2007-03-04 18:13:58 +0100 | [diff] [blame] | 391 | /* |
| 392 | * An http ver_token is any ASCII which can be found in an HTTP version, |
| 393 | * which includes 'H', 'T', 'P', '/', '.' and any digit. |
| 394 | */ |
| 395 | const char http_is_ver_token[256] = { |
| 396 | ['.'] = 1, ['/'] = 1, |
| 397 | ['0'] = 1, ['1'] = 1, ['2'] = 1, ['3'] = 1, ['4'] = 1, |
| 398 | ['5'] = 1, ['6'] = 1, ['7'] = 1, ['8'] = 1, ['9'] = 1, |
| 399 | ['H'] = 1, ['P'] = 1, ['T'] = 1, |
| 400 | }; |
| 401 | |
| 402 | |
Willy Tarreau | 4af6f3a | 2007-03-18 22:36:26 +0100 | [diff] [blame] | 403 | /* |
Willy Tarreau | e988a79 | 2010-01-04 21:13:14 +0100 | [diff] [blame] | 404 | * Silent debug that outputs only in strace, using fd #-1. Trash is modified. |
| 405 | */ |
| 406 | #if defined(DEBUG_FSM) |
| 407 | static void http_silent_debug(int line, struct session *s) |
| 408 | { |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 409 | chunk_printf(&trash, |
| 410 | "[%04d] req: p=%d(%d) s=%d bf=%08x an=%08x data=%p size=%d l=%d w=%p r=%p o=%p sm=%d fw=%ld tf=%08x\n", |
| 411 | line, |
| 412 | s->si[0].state, s->si[0].fd, s->txn.req.msg_state, s->req->flags, s->req->analysers, |
| 413 | s->req->buf->data, s->req->buf->size, s->req->l, s->req->w, s->req->r, s->req->buf->p, s->req->buf->o, s->req->to_forward, s->txn.flags); |
| 414 | write(-1, trash.str, trash.len); |
Willy Tarreau | e988a79 | 2010-01-04 21:13:14 +0100 | [diff] [blame] | 415 | |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 416 | chunk_printf(&trash, |
| 417 | " %04d rep: p=%d(%d) s=%d bf=%08x an=%08x data=%p size=%d l=%d w=%p r=%p o=%p sm=%d fw=%ld\n", |
| 418 | line, |
| 419 | s->si[1].state, s->si[1].fd, s->txn.rsp.msg_state, s->rep->flags, s->rep->analysers, |
| 420 | s->rep->buf->data, s->rep->buf->size, s->rep->l, s->rep->w, s->rep->r, s->rep->buf->p, s->rep->buf->o, s->rep->to_forward); |
| 421 | write(-1, trash.str, trash.len); |
Willy Tarreau | e988a79 | 2010-01-04 21:13:14 +0100 | [diff] [blame] | 422 | } |
| 423 | #else |
| 424 | #define http_silent_debug(l,s) do { } while (0) |
| 425 | #endif |
| 426 | |
| 427 | /* |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 428 | * Adds a header and its CRLF at the tail of the message's buffer, just before |
| 429 | * the last CRLF. Text length is measured first, so it cannot be NULL. |
Willy Tarreau | 4af6f3a | 2007-03-18 22:36:26 +0100 | [diff] [blame] | 430 | * The header is also automatically added to the index <hdr_idx>, and the end |
| 431 | * of headers is automatically adjusted. The number of bytes added is returned |
| 432 | * on success, otherwise <0 is returned indicating an error. |
| 433 | */ |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 434 | int http_header_add_tail(struct http_msg *msg, struct hdr_idx *hdr_idx, const char *text) |
Willy Tarreau | 4af6f3a | 2007-03-18 22:36:26 +0100 | [diff] [blame] | 435 | { |
| 436 | int bytes, len; |
| 437 | |
| 438 | len = strlen(text); |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 439 | bytes = buffer_insert_line2(msg->chn->buf, msg->chn->buf->p + msg->eoh, text, len); |
Willy Tarreau | 4af6f3a | 2007-03-18 22:36:26 +0100 | [diff] [blame] | 440 | if (!bytes) |
| 441 | return -1; |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 442 | http_msg_move_end(msg, bytes); |
Willy Tarreau | 4af6f3a | 2007-03-18 22:36:26 +0100 | [diff] [blame] | 443 | return hdr_idx_add(len, 1, hdr_idx, hdr_idx->tail); |
| 444 | } |
| 445 | |
| 446 | /* |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 447 | * Adds a header and its CRLF at the tail of the message's buffer, just before |
| 448 | * the last CRLF. <len> bytes are copied, not counting the CRLF. If <text> is NULL, then |
Willy Tarreau | 4af6f3a | 2007-03-18 22:36:26 +0100 | [diff] [blame] | 449 | * the buffer is only opened and the space reserved, but nothing is copied. |
| 450 | * The header is also automatically added to the index <hdr_idx>, and the end |
| 451 | * of headers is automatically adjusted. The number of bytes added is returned |
| 452 | * on success, otherwise <0 is returned indicating an error. |
| 453 | */ |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 454 | int http_header_add_tail2(struct http_msg *msg, |
| 455 | struct hdr_idx *hdr_idx, const char *text, int len) |
Willy Tarreau | 4af6f3a | 2007-03-18 22:36:26 +0100 | [diff] [blame] | 456 | { |
| 457 | int bytes; |
| 458 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 459 | bytes = buffer_insert_line2(msg->chn->buf, msg->chn->buf->p + msg->eoh, text, len); |
Willy Tarreau | 4af6f3a | 2007-03-18 22:36:26 +0100 | [diff] [blame] | 460 | if (!bytes) |
| 461 | return -1; |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 462 | http_msg_move_end(msg, bytes); |
Willy Tarreau | 4af6f3a | 2007-03-18 22:36:26 +0100 | [diff] [blame] | 463 | return hdr_idx_add(len, 1, hdr_idx, hdr_idx->tail); |
| 464 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 465 | |
| 466 | /* |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 467 | * Checks if <hdr> is exactly <name> for <len> chars, and ends with a colon. |
| 468 | * If so, returns the position of the first non-space character relative to |
| 469 | * <hdr>, or <end>-<hdr> if not found before. If no value is found, it tries |
| 470 | * to return a pointer to the place after the first space. Returns 0 if the |
| 471 | * header name does not match. Checks are case-insensitive. |
| 472 | */ |
| 473 | int http_header_match2(const char *hdr, const char *end, |
| 474 | const char *name, int len) |
| 475 | { |
| 476 | const char *val; |
| 477 | |
| 478 | if (hdr + len >= end) |
| 479 | return 0; |
| 480 | if (hdr[len] != ':') |
| 481 | return 0; |
| 482 | if (strncasecmp(hdr, name, len) != 0) |
| 483 | return 0; |
| 484 | val = hdr + len + 1; |
| 485 | while (val < end && HTTP_IS_SPHT(*val)) |
| 486 | val++; |
| 487 | if ((val >= end) && (len + 2 <= end - hdr)) |
| 488 | return len + 2; /* we may replace starting from second space */ |
| 489 | return val - hdr; |
| 490 | } |
| 491 | |
Willy Tarreau | 04ff9f1 | 2013-06-10 18:39:42 +0200 | [diff] [blame] | 492 | /* Find the first or next occurrence of header <name> in message buffer <sol> |
| 493 | * using headers index <idx>, and return it in the <ctx> structure. This |
| 494 | * structure holds everything necessary to use the header and find next |
| 495 | * occurrence. If its <idx> member is 0, the header is searched from the |
| 496 | * beginning. Otherwise, the next occurrence is returned. The function returns |
| 497 | * 1 when it finds a value, and 0 when there is no more. It is very similar to |
| 498 | * http_find_header2() except that it is designed to work with full-line headers |
| 499 | * whose comma is not a delimiter but is part of the syntax. As a special case, |
| 500 | * if ctx->val is NULL when searching for a new values of a header, the current |
| 501 | * header is rescanned. This allows rescanning after a header deletion. |
| 502 | */ |
| 503 | int http_find_full_header2(const char *name, int len, |
| 504 | char *sol, struct hdr_idx *idx, |
| 505 | struct hdr_ctx *ctx) |
| 506 | { |
| 507 | char *eol, *sov; |
| 508 | int cur_idx, old_idx; |
| 509 | |
| 510 | cur_idx = ctx->idx; |
| 511 | if (cur_idx) { |
| 512 | /* We have previously returned a header, let's search another one */ |
| 513 | sol = ctx->line; |
| 514 | eol = sol + idx->v[cur_idx].len; |
| 515 | goto next_hdr; |
| 516 | } |
| 517 | |
| 518 | /* first request for this header */ |
| 519 | sol += hdr_idx_first_pos(idx); |
| 520 | old_idx = 0; |
| 521 | cur_idx = hdr_idx_first_idx(idx); |
| 522 | while (cur_idx) { |
| 523 | eol = sol + idx->v[cur_idx].len; |
| 524 | |
| 525 | if (len == 0) { |
| 526 | /* No argument was passed, we want any header. |
| 527 | * To achieve this, we simply build a fake request. */ |
| 528 | while (sol + len < eol && sol[len] != ':') |
| 529 | len++; |
| 530 | name = sol; |
| 531 | } |
| 532 | |
| 533 | if ((len < eol - sol) && |
| 534 | (sol[len] == ':') && |
| 535 | (strncasecmp(sol, name, len) == 0)) { |
| 536 | ctx->del = len; |
| 537 | sov = sol + len + 1; |
| 538 | while (sov < eol && http_is_lws[(unsigned char)*sov]) |
| 539 | sov++; |
| 540 | |
| 541 | ctx->line = sol; |
| 542 | ctx->prev = old_idx; |
| 543 | ctx->idx = cur_idx; |
| 544 | ctx->val = sov - sol; |
| 545 | ctx->tws = 0; |
| 546 | while (eol > sov && http_is_lws[(unsigned char)*(eol - 1)]) { |
| 547 | eol--; |
| 548 | ctx->tws++; |
| 549 | } |
| 550 | ctx->vlen = eol - sov; |
| 551 | return 1; |
| 552 | } |
| 553 | next_hdr: |
| 554 | sol = eol + idx->v[cur_idx].cr + 1; |
| 555 | old_idx = cur_idx; |
| 556 | cur_idx = idx->v[cur_idx].next; |
| 557 | } |
| 558 | return 0; |
| 559 | } |
| 560 | |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 561 | /* Find the end of the header value contained between <s> and <e>. See RFC2616, |
| 562 | * par 2.2 for more information. Note that it requires a valid header to return |
| 563 | * a valid result. This works for headers defined as comma-separated lists. |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 564 | */ |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 565 | char *find_hdr_value_end(char *s, const char *e) |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 566 | { |
| 567 | int quoted, qdpair; |
| 568 | |
| 569 | quoted = qdpair = 0; |
| 570 | for (; s < e; s++) { |
| 571 | if (qdpair) qdpair = 0; |
Willy Tarreau | 0f7f51f | 2010-08-30 11:06:34 +0200 | [diff] [blame] | 572 | else if (quoted) { |
| 573 | if (*s == '\\') qdpair = 1; |
| 574 | else if (*s == '"') quoted = 0; |
| 575 | } |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 576 | else if (*s == '"') quoted = 1; |
| 577 | else if (*s == ',') return s; |
| 578 | } |
| 579 | return s; |
| 580 | } |
| 581 | |
| 582 | /* Find the first or next occurrence of header <name> in message buffer <sol> |
| 583 | * using headers index <idx>, and return it in the <ctx> structure. This |
| 584 | * structure holds everything necessary to use the header and find next |
| 585 | * occurrence. If its <idx> member is 0, the header is searched from the |
| 586 | * beginning. Otherwise, the next occurrence is returned. The function returns |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 587 | * 1 when it finds a value, and 0 when there is no more. It is designed to work |
| 588 | * with headers defined as comma-separated lists. As a special case, if ctx->val |
| 589 | * is NULL when searching for a new values of a header, the current header is |
| 590 | * rescanned. This allows rescanning after a header deletion. |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 591 | */ |
| 592 | int http_find_header2(const char *name, int len, |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 593 | char *sol, struct hdr_idx *idx, |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 594 | struct hdr_ctx *ctx) |
| 595 | { |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 596 | char *eol, *sov; |
| 597 | int cur_idx, old_idx; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 598 | |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 599 | cur_idx = ctx->idx; |
| 600 | if (cur_idx) { |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 601 | /* We have previously returned a value, let's search |
| 602 | * another one on the same line. |
| 603 | */ |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 604 | sol = ctx->line; |
Willy Tarreau | 588bd4f | 2011-09-01 22:22:28 +0200 | [diff] [blame] | 605 | ctx->del = ctx->val + ctx->vlen + ctx->tws; |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 606 | sov = sol + ctx->del; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 607 | eol = sol + idx->v[cur_idx].len; |
| 608 | |
| 609 | if (sov >= eol) |
| 610 | /* no more values in this header */ |
| 611 | goto next_hdr; |
| 612 | |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 613 | /* values remaining for this header, skip the comma but save it |
| 614 | * for later use (eg: for header deletion). |
| 615 | */ |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 616 | sov++; |
| 617 | while (sov < eol && http_is_lws[(unsigned char)*sov]) |
| 618 | sov++; |
| 619 | |
| 620 | goto return_hdr; |
| 621 | } |
| 622 | |
| 623 | /* first request for this header */ |
| 624 | sol += hdr_idx_first_pos(idx); |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 625 | old_idx = 0; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 626 | cur_idx = hdr_idx_first_idx(idx); |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 627 | while (cur_idx) { |
| 628 | eol = sol + idx->v[cur_idx].len; |
| 629 | |
Willy Tarreau | 1ad7c6d | 2007-06-10 21:42:55 +0200 | [diff] [blame] | 630 | if (len == 0) { |
| 631 | /* No argument was passed, we want any header. |
| 632 | * To achieve this, we simply build a fake request. */ |
| 633 | while (sol + len < eol && sol[len] != ':') |
| 634 | len++; |
| 635 | name = sol; |
| 636 | } |
| 637 | |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 638 | if ((len < eol - sol) && |
| 639 | (sol[len] == ':') && |
| 640 | (strncasecmp(sol, name, len) == 0)) { |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 641 | ctx->del = len; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 642 | sov = sol + len + 1; |
| 643 | while (sov < eol && http_is_lws[(unsigned char)*sov]) |
| 644 | sov++; |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 645 | |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 646 | ctx->line = sol; |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 647 | ctx->prev = old_idx; |
| 648 | return_hdr: |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 649 | ctx->idx = cur_idx; |
| 650 | ctx->val = sov - sol; |
| 651 | |
| 652 | eol = find_hdr_value_end(sov, eol); |
Willy Tarreau | 588bd4f | 2011-09-01 22:22:28 +0200 | [diff] [blame] | 653 | ctx->tws = 0; |
Willy Tarreau | 275600b | 2011-09-16 08:11:26 +0200 | [diff] [blame] | 654 | while (eol > sov && http_is_lws[(unsigned char)*(eol - 1)]) { |
Willy Tarreau | 588bd4f | 2011-09-01 22:22:28 +0200 | [diff] [blame] | 655 | eol--; |
| 656 | ctx->tws++; |
| 657 | } |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 658 | ctx->vlen = eol - sov; |
| 659 | return 1; |
| 660 | } |
| 661 | next_hdr: |
| 662 | sol = eol + idx->v[cur_idx].cr + 1; |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 663 | old_idx = cur_idx; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 664 | cur_idx = idx->v[cur_idx].next; |
| 665 | } |
| 666 | return 0; |
| 667 | } |
| 668 | |
| 669 | int http_find_header(const char *name, |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 670 | char *sol, struct hdr_idx *idx, |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 671 | struct hdr_ctx *ctx) |
| 672 | { |
| 673 | return http_find_header2(name, strlen(name), sol, idx, ctx); |
| 674 | } |
| 675 | |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 676 | /* Remove one value of a header. This only works on a <ctx> returned by one of |
| 677 | * the http_find_header functions. The value is removed, as well as surrounding |
| 678 | * commas if any. If the removed value was alone, the whole header is removed. |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 679 | * The ctx is always updated accordingly, as well as the buffer and HTTP |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 680 | * message <msg>. The new index is returned. If it is zero, it means there is |
| 681 | * no more header, so any processing may stop. The ctx is always left in a form |
| 682 | * that can be handled by http_find_header2() to find next occurrence. |
| 683 | */ |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 684 | int http_remove_header2(struct http_msg *msg, struct hdr_idx *idx, struct hdr_ctx *ctx) |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 685 | { |
| 686 | int cur_idx = ctx->idx; |
| 687 | char *sol = ctx->line; |
| 688 | struct hdr_idx_elem *hdr; |
| 689 | int delta, skip_comma; |
| 690 | |
| 691 | if (!cur_idx) |
| 692 | return 0; |
| 693 | |
| 694 | hdr = &idx->v[cur_idx]; |
Willy Tarreau | 588bd4f | 2011-09-01 22:22:28 +0200 | [diff] [blame] | 695 | if (sol[ctx->del] == ':' && ctx->val + ctx->vlen + ctx->tws == hdr->len) { |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 696 | /* This was the only value of the header, we must now remove it entirely. */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 697 | delta = buffer_replace2(msg->chn->buf, sol, sol + hdr->len + hdr->cr + 1, NULL, 0); |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 698 | http_msg_move_end(msg, delta); |
| 699 | idx->used--; |
| 700 | hdr->len = 0; /* unused entry */ |
| 701 | idx->v[ctx->prev].next = idx->v[ctx->idx].next; |
Willy Tarreau | 5c4784f | 2011-02-12 13:07:35 +0100 | [diff] [blame] | 702 | if (idx->tail == ctx->idx) |
| 703 | idx->tail = ctx->prev; |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 704 | ctx->idx = ctx->prev; /* walk back to the end of previous header */ |
| 705 | ctx->line -= idx->v[ctx->idx].len + idx->v[cur_idx].cr + 1; |
| 706 | ctx->val = idx->v[ctx->idx].len; /* point to end of previous header */ |
Willy Tarreau | 588bd4f | 2011-09-01 22:22:28 +0200 | [diff] [blame] | 707 | ctx->tws = ctx->vlen = 0; |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 708 | return ctx->idx; |
| 709 | } |
| 710 | |
| 711 | /* This was not the only value of this header. We have to remove between |
Willy Tarreau | 588bd4f | 2011-09-01 22:22:28 +0200 | [diff] [blame] | 712 | * ctx->del+1 and ctx->val+ctx->vlen+ctx->tws+1 included. If it is the |
| 713 | * last entry of the list, we remove the last separator. |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 714 | */ |
| 715 | |
Willy Tarreau | 588bd4f | 2011-09-01 22:22:28 +0200 | [diff] [blame] | 716 | skip_comma = (ctx->val + ctx->vlen + ctx->tws == hdr->len) ? 0 : 1; |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 717 | delta = buffer_replace2(msg->chn->buf, sol + ctx->del + skip_comma, |
Willy Tarreau | 588bd4f | 2011-09-01 22:22:28 +0200 | [diff] [blame] | 718 | sol + ctx->val + ctx->vlen + ctx->tws + skip_comma, |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 719 | NULL, 0); |
| 720 | hdr->len += delta; |
| 721 | http_msg_move_end(msg, delta); |
| 722 | ctx->val = ctx->del; |
Willy Tarreau | 588bd4f | 2011-09-01 22:22:28 +0200 | [diff] [blame] | 723 | ctx->tws = ctx->vlen = 0; |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 724 | return ctx->idx; |
| 725 | } |
| 726 | |
Willy Tarreau | 2d3d94c | 2008-11-30 20:20:08 +0100 | [diff] [blame] | 727 | /* This function handles a server error at the stream interface level. The |
| 728 | * stream interface is assumed to be already in a closed state. An optional |
| 729 | * message is copied into the input buffer, and an HTTP status code stored. |
| 730 | * The error flags are set to the values in arguments. Any pending request |
Willy Tarreau | 6f0aa47 | 2009-03-08 20:33:29 +0100 | [diff] [blame] | 731 | * in this buffer will be lost. |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 732 | */ |
Willy Tarreau | 2d3d94c | 2008-11-30 20:20:08 +0100 | [diff] [blame] | 733 | static void http_server_error(struct session *t, struct stream_interface *si, |
| 734 | int err, int finst, int status, const struct chunk *msg) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 735 | { |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 736 | channel_auto_read(si->ob); |
| 737 | channel_abort(si->ob); |
| 738 | channel_auto_close(si->ob); |
| 739 | channel_erase(si->ob); |
| 740 | channel_auto_close(si->ib); |
| 741 | channel_auto_read(si->ib); |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 742 | if (status > 0 && msg) { |
Willy Tarreau | 3bac9ff | 2007-03-18 17:31:28 +0100 | [diff] [blame] | 743 | t->txn.status = status; |
Willy Tarreau | 9dab5fc | 2012-05-07 11:56:55 +0200 | [diff] [blame] | 744 | bo_inject(si->ib, msg->str, msg->len); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 745 | } |
| 746 | if (!(t->flags & SN_ERR_MASK)) |
| 747 | t->flags |= err; |
| 748 | if (!(t->flags & SN_FINST_MASK)) |
| 749 | t->flags |= finst; |
| 750 | } |
| 751 | |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 752 | /* This function returns the appropriate error location for the given session |
| 753 | * and message. |
| 754 | */ |
| 755 | |
Willy Tarreau | 783f258 | 2012-09-04 12:19:04 +0200 | [diff] [blame] | 756 | struct chunk *http_error_message(struct session *s, int msgnum) |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 757 | { |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 758 | if (s->be->errmsg[msgnum].str) |
| 759 | return &s->be->errmsg[msgnum]; |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 760 | else if (s->fe->errmsg[msgnum].str) |
| 761 | return &s->fe->errmsg[msgnum]; |
| 762 | else |
| 763 | return &http_err_chunks[msgnum]; |
| 764 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 765 | |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 766 | /* |
| 767 | * returns HTTP_METH_NONE if there is nothing valid to read (empty or non-text |
| 768 | * string), HTTP_METH_OTHER for unknown methods, or the identified method. |
| 769 | */ |
Willy Tarreau | c8987b3 | 2013-12-06 23:43:17 +0100 | [diff] [blame] | 770 | static enum http_meth_t find_http_meth(const char *str, const int len) |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 771 | { |
| 772 | unsigned char m; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 773 | const struct http_method_desc *h; |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 774 | |
| 775 | m = ((unsigned)*str - 'A'); |
| 776 | |
| 777 | if (m < 26) { |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 778 | for (h = http_methods[m]; h->len > 0; h++) { |
| 779 | if (unlikely(h->len != len)) |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 780 | continue; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 781 | if (likely(memcmp(str, h->text, h->len) == 0)) |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 782 | return h->meth; |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 783 | }; |
| 784 | return HTTP_METH_OTHER; |
| 785 | } |
| 786 | return HTTP_METH_NONE; |
| 787 | |
| 788 | } |
| 789 | |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 790 | /* Parse the URI from the given transaction (which is assumed to be in request |
| 791 | * phase) and look for the "/" beginning the PATH. If not found, return NULL. |
| 792 | * It is returned otherwise. |
| 793 | */ |
| 794 | static char * |
| 795 | http_get_path(struct http_txn *txn) |
| 796 | { |
| 797 | char *ptr, *end; |
| 798 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 799 | ptr = txn->req.chn->buf->p + txn->req.sl.rq.u; |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 800 | end = ptr + txn->req.sl.rq.u_l; |
| 801 | |
| 802 | if (ptr >= end) |
| 803 | return NULL; |
| 804 | |
| 805 | /* RFC2616, par. 5.1.2 : |
| 806 | * Request-URI = "*" | absuri | abspath | authority |
| 807 | */ |
| 808 | |
| 809 | if (*ptr == '*') |
| 810 | return NULL; |
| 811 | |
| 812 | if (isalpha((unsigned char)*ptr)) { |
| 813 | /* this is a scheme as described by RFC3986, par. 3.1 */ |
| 814 | ptr++; |
| 815 | while (ptr < end && |
| 816 | (isalnum((unsigned char)*ptr) || *ptr == '+' || *ptr == '-' || *ptr == '.')) |
| 817 | ptr++; |
| 818 | /* skip '://' */ |
| 819 | if (ptr == end || *ptr++ != ':') |
| 820 | return NULL; |
| 821 | if (ptr == end || *ptr++ != '/') |
| 822 | return NULL; |
| 823 | if (ptr == end || *ptr++ != '/') |
| 824 | return NULL; |
| 825 | } |
| 826 | /* skip [user[:passwd]@]host[:[port]] */ |
| 827 | |
| 828 | while (ptr < end && *ptr != '/') |
| 829 | ptr++; |
| 830 | |
| 831 | if (ptr == end) |
| 832 | return NULL; |
| 833 | |
| 834 | /* OK, we got the '/' ! */ |
| 835 | return ptr; |
| 836 | } |
| 837 | |
William Lallemand | 65ad6e1 | 2014-01-31 15:08:02 +0100 | [diff] [blame] | 838 | /* Parse the URI from the given string and look for the "/" beginning the PATH. |
| 839 | * If not found, return NULL. It is returned otherwise. |
| 840 | */ |
| 841 | static char * |
| 842 | http_get_path_from_string(char *str) |
| 843 | { |
| 844 | char *ptr = str; |
| 845 | |
| 846 | /* RFC2616, par. 5.1.2 : |
| 847 | * Request-URI = "*" | absuri | abspath | authority |
| 848 | */ |
| 849 | |
| 850 | if (*ptr == '*') |
| 851 | return NULL; |
| 852 | |
| 853 | if (isalpha((unsigned char)*ptr)) { |
| 854 | /* this is a scheme as described by RFC3986, par. 3.1 */ |
| 855 | ptr++; |
| 856 | while (isalnum((unsigned char)*ptr) || *ptr == '+' || *ptr == '-' || *ptr == '.') |
| 857 | ptr++; |
| 858 | /* skip '://' */ |
| 859 | if (*ptr == '\0' || *ptr++ != ':') |
| 860 | return NULL; |
| 861 | if (*ptr == '\0' || *ptr++ != '/') |
| 862 | return NULL; |
| 863 | if (*ptr == '\0' || *ptr++ != '/') |
| 864 | return NULL; |
| 865 | } |
| 866 | /* skip [user[:passwd]@]host[:[port]] */ |
| 867 | |
| 868 | while (*ptr != '\0' && *ptr != ' ' && *ptr != '/') |
| 869 | ptr++; |
| 870 | |
| 871 | if (*ptr == '\0' || *ptr == ' ') |
| 872 | return NULL; |
| 873 | |
| 874 | /* OK, we got the '/' ! */ |
| 875 | return ptr; |
| 876 | } |
| 877 | |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 878 | /* Returns a 302 for a redirectable request that reaches a server working in |
| 879 | * in redirect mode. This may only be called just after the stream interface |
| 880 | * has moved to SI_ST_ASS. Unprocessable requests are left unchanged and will |
| 881 | * follow normal proxy processing. NOTE: this function is designed to support |
| 882 | * being called once data are scheduled for forwarding. |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 883 | */ |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 884 | void http_perform_server_redirect(struct session *s, struct stream_interface *si) |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 885 | { |
| 886 | struct http_txn *txn; |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 887 | struct server *srv; |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 888 | char *path; |
Willy Tarreau | cde18fc | 2012-05-30 07:59:54 +0200 | [diff] [blame] | 889 | int len, rewind; |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 890 | |
| 891 | /* 1: create the response header */ |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 892 | trash.len = strlen(HTTP_302); |
| 893 | memcpy(trash.str, HTTP_302, trash.len); |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 894 | |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 895 | srv = objt_server(s->target); |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 896 | |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 897 | /* 2: add the server's prefix */ |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 898 | if (trash.len + srv->rdr_len > trash.size) |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 899 | return; |
| 900 | |
Willy Tarreau | dcb75c4 | 2010-01-10 00:24:22 +0100 | [diff] [blame] | 901 | /* special prefix "/" means don't change URL */ |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 902 | if (srv->rdr_len != 1 || *srv->rdr_pfx != '/') { |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 903 | memcpy(trash.str + trash.len, srv->rdr_pfx, srv->rdr_len); |
| 904 | trash.len += srv->rdr_len; |
Willy Tarreau | dcb75c4 | 2010-01-10 00:24:22 +0100 | [diff] [blame] | 905 | } |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 906 | |
Willy Tarreau | cde18fc | 2012-05-30 07:59:54 +0200 | [diff] [blame] | 907 | /* 3: add the request URI. Since it was already forwarded, we need |
| 908 | * to temporarily rewind the buffer. |
| 909 | */ |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 910 | txn = &s->txn; |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 911 | b_rew(s->req->buf, rewind = s->req->buf->o); |
Willy Tarreau | cde18fc | 2012-05-30 07:59:54 +0200 | [diff] [blame] | 912 | |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 913 | path = http_get_path(txn); |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 914 | 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] | 915 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 916 | b_adv(s->req->buf, rewind); |
Willy Tarreau | cde18fc | 2012-05-30 07:59:54 +0200 | [diff] [blame] | 917 | |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 918 | if (!path) |
| 919 | return; |
| 920 | |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 921 | if (trash.len + len > trash.size - 4) /* 4 for CRLF-CRLF */ |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 922 | return; |
| 923 | |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 924 | memcpy(trash.str + trash.len, path, len); |
| 925 | trash.len += len; |
Willy Tarreau | 88d349d | 2010-01-25 12:15:43 +0100 | [diff] [blame] | 926 | |
| 927 | if (unlikely(txn->flags & TX_USE_PX_CONN)) { |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 928 | memcpy(trash.str + trash.len, "\r\nProxy-Connection: close\r\n\r\n", 29); |
| 929 | trash.len += 29; |
Willy Tarreau | 88d349d | 2010-01-25 12:15:43 +0100 | [diff] [blame] | 930 | } else { |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 931 | memcpy(trash.str + trash.len, "\r\nConnection: close\r\n\r\n", 23); |
| 932 | trash.len += 23; |
Willy Tarreau | 88d349d | 2010-01-25 12:15:43 +0100 | [diff] [blame] | 933 | } |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 934 | |
| 935 | /* prepare to return without error. */ |
Willy Tarreau | 73b013b | 2012-05-21 16:31:45 +0200 | [diff] [blame] | 936 | si_shutr(si); |
| 937 | si_shutw(si); |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 938 | si->err_type = SI_ET_NONE; |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 939 | si->state = SI_ST_CLO; |
| 940 | |
| 941 | /* send the message */ |
Willy Tarreau | 570f221 | 2013-06-10 16:42:09 +0200 | [diff] [blame] | 942 | 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] | 943 | |
| 944 | /* 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] | 945 | srv_inc_sess_ctr(srv); |
Bhaskar Maddala | a20cb85 | 2014-02-03 16:26:46 -0500 | [diff] [blame] | 946 | srv_set_sess_last(srv); |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 947 | } |
| 948 | |
Willy Tarreau | 0cac36f | 2008-11-30 20:44:17 +0100 | [diff] [blame] | 949 | /* Return the error message corresponding to si->err_type. It is assumed |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 950 | * that the server side is closed. Note that err_type is actually a |
| 951 | * bitmask, where almost only aborts may be cumulated with other |
| 952 | * values. We consider that aborted operations are more important |
| 953 | * than timeouts or errors due to the fact that nobody else in the |
| 954 | * logs might explain incomplete retries. All others should avoid |
| 955 | * being cumulated. It should normally not be possible to have multiple |
| 956 | * 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] | 957 | * Note that connection errors appearing on the second request of a keep-alive |
| 958 | * connection are not reported since this allows the client to retry. |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 959 | */ |
Willy Tarreau | 0cac36f | 2008-11-30 20:44:17 +0100 | [diff] [blame] | 960 | void http_return_srv_error(struct session *s, struct stream_interface *si) |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 961 | { |
Willy Tarreau | 0cac36f | 2008-11-30 20:44:17 +0100 | [diff] [blame] | 962 | int err_type = si->err_type; |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 963 | |
| 964 | if (err_type & SI_ET_QUEUE_ABRT) |
Willy Tarreau | 2d3d94c | 2008-11-30 20:20:08 +0100 | [diff] [blame] | 965 | http_server_error(s, si, SN_ERR_CLICL, SN_FINST_Q, |
Willy Tarreau | 783f258 | 2012-09-04 12:19:04 +0200 | [diff] [blame] | 966 | 503, http_error_message(s, HTTP_ERR_503)); |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 967 | else if (err_type & SI_ET_CONN_ABRT) |
Willy Tarreau | 2d3d94c | 2008-11-30 20:20:08 +0100 | [diff] [blame] | 968 | http_server_error(s, si, SN_ERR_CLICL, SN_FINST_C, |
Willy Tarreau | 6b726ad | 2013-12-15 19:31:37 +0100 | [diff] [blame] | 969 | 503, (s->txn.flags & TX_NOT_FIRST) ? NULL : |
| 970 | http_error_message(s, HTTP_ERR_503)); |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 971 | else if (err_type & SI_ET_QUEUE_TO) |
Willy Tarreau | 2d3d94c | 2008-11-30 20:20:08 +0100 | [diff] [blame] | 972 | http_server_error(s, si, SN_ERR_SRVTO, SN_FINST_Q, |
Willy Tarreau | 783f258 | 2012-09-04 12:19:04 +0200 | [diff] [blame] | 973 | 503, http_error_message(s, HTTP_ERR_503)); |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 974 | else if (err_type & SI_ET_QUEUE_ERR) |
Willy Tarreau | 2d3d94c | 2008-11-30 20:20:08 +0100 | [diff] [blame] | 975 | http_server_error(s, si, SN_ERR_SRVCL, SN_FINST_Q, |
Willy Tarreau | 783f258 | 2012-09-04 12:19:04 +0200 | [diff] [blame] | 976 | 503, http_error_message(s, HTTP_ERR_503)); |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 977 | else if (err_type & SI_ET_CONN_TO) |
Willy Tarreau | 2d3d94c | 2008-11-30 20:20:08 +0100 | [diff] [blame] | 978 | http_server_error(s, si, SN_ERR_SRVTO, SN_FINST_C, |
Willy Tarreau | 6b726ad | 2013-12-15 19:31:37 +0100 | [diff] [blame] | 979 | 503, (s->txn.flags & TX_NOT_FIRST) ? NULL : |
| 980 | http_error_message(s, HTTP_ERR_503)); |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 981 | else if (err_type & SI_ET_CONN_ERR) |
Willy Tarreau | 2d3d94c | 2008-11-30 20:20:08 +0100 | [diff] [blame] | 982 | http_server_error(s, si, SN_ERR_SRVCL, SN_FINST_C, |
Willy Tarreau | 3634624 | 2014-02-24 18:26:30 +0100 | [diff] [blame] | 983 | 503, (s->flags & SN_SRV_REUSED) ? NULL : |
Willy Tarreau | 6b726ad | 2013-12-15 19:31:37 +0100 | [diff] [blame] | 984 | http_error_message(s, HTTP_ERR_503)); |
Willy Tarreau | 2d400bb | 2012-05-14 12:11:47 +0200 | [diff] [blame] | 985 | else if (err_type & SI_ET_CONN_RES) |
| 986 | http_server_error(s, si, SN_ERR_RESOURCE, SN_FINST_C, |
Willy Tarreau | 6b726ad | 2013-12-15 19:31:37 +0100 | [diff] [blame] | 987 | 503, (s->txn.flags & TX_NOT_FIRST) ? NULL : |
| 988 | http_error_message(s, HTTP_ERR_503)); |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 989 | else /* SI_ET_CONN_OTHER and others */ |
Willy Tarreau | 2d3d94c | 2008-11-30 20:20:08 +0100 | [diff] [blame] | 990 | http_server_error(s, si, SN_ERR_INTERNAL, SN_FINST_C, |
Willy Tarreau | 783f258 | 2012-09-04 12:19:04 +0200 | [diff] [blame] | 991 | 500, http_error_message(s, HTTP_ERR_500)); |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 992 | } |
| 993 | |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 994 | extern const char sess_term_cond[8]; |
| 995 | extern const char sess_fin_state[8]; |
| 996 | extern const char *monthname[12]; |
Willy Tarreau | 332f8bf | 2007-05-13 21:36:56 +0200 | [diff] [blame] | 997 | struct pool_head *pool2_requri; |
Willy Tarreau | 193b8c6 | 2012-11-22 00:17:38 +0100 | [diff] [blame] | 998 | struct pool_head *pool2_capture = NULL; |
William Lallemand | a73203e | 2012-03-12 12:48:57 +0100 | [diff] [blame] | 999 | struct pool_head *pool2_uniqueid; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1000 | |
Willy Tarreau | 117f59e | 2007-03-04 18:17:17 +0100 | [diff] [blame] | 1001 | /* |
| 1002 | * Capture headers from message starting at <som> according to header list |
| 1003 | * <cap_hdr>, and fill the <idx> structure appropriately. |
| 1004 | */ |
| 1005 | void capture_headers(char *som, struct hdr_idx *idx, |
| 1006 | char **cap, struct cap_hdr *cap_hdr) |
| 1007 | { |
| 1008 | char *eol, *sol, *col, *sov; |
| 1009 | int cur_idx; |
| 1010 | struct cap_hdr *h; |
| 1011 | int len; |
| 1012 | |
| 1013 | sol = som + hdr_idx_first_pos(idx); |
| 1014 | cur_idx = hdr_idx_first_idx(idx); |
| 1015 | |
| 1016 | while (cur_idx) { |
| 1017 | eol = sol + idx->v[cur_idx].len; |
| 1018 | |
| 1019 | col = sol; |
| 1020 | while (col < eol && *col != ':') |
| 1021 | col++; |
| 1022 | |
| 1023 | sov = col + 1; |
| 1024 | while (sov < eol && http_is_lws[(unsigned char)*sov]) |
| 1025 | sov++; |
| 1026 | |
| 1027 | for (h = cap_hdr; h; h = h->next) { |
| 1028 | if ((h->namelen == col - sol) && |
| 1029 | (strncasecmp(sol, h->name, h->namelen) == 0)) { |
| 1030 | if (cap[h->index] == NULL) |
| 1031 | cap[h->index] = |
Willy Tarreau | cf7f320 | 2007-05-13 22:46:04 +0200 | [diff] [blame] | 1032 | pool_alloc2(h->pool); |
Willy Tarreau | 117f59e | 2007-03-04 18:17:17 +0100 | [diff] [blame] | 1033 | |
| 1034 | if (cap[h->index] == NULL) { |
| 1035 | Alert("HTTP capture : out of memory.\n"); |
| 1036 | continue; |
| 1037 | } |
| 1038 | |
| 1039 | len = eol - sov; |
| 1040 | if (len > h->len) |
| 1041 | len = h->len; |
| 1042 | |
| 1043 | memcpy(cap[h->index], sov, len); |
| 1044 | cap[h->index][len]=0; |
| 1045 | } |
| 1046 | } |
| 1047 | sol = eol + idx->v[cur_idx].cr + 1; |
| 1048 | cur_idx = idx->v[cur_idx].next; |
| 1049 | } |
| 1050 | } |
| 1051 | |
| 1052 | |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 1053 | /* either we find an LF at <ptr> or we jump to <bad>. |
| 1054 | */ |
| 1055 | #define EXPECT_LF_HERE(ptr, bad) do { if (unlikely(*(ptr) != '\n')) goto bad; } while (0) |
| 1056 | |
| 1057 | /* plays with variables <ptr>, <end> and <state>. Jumps to <good> if OK, |
| 1058 | * otherwise to <http_msg_ood> with <state> set to <st>. |
| 1059 | */ |
| 1060 | #define EAT_AND_JUMP_OR_RETURN(good, st) do { \ |
| 1061 | ptr++; \ |
| 1062 | if (likely(ptr < end)) \ |
| 1063 | goto good; \ |
| 1064 | else { \ |
| 1065 | state = (st); \ |
| 1066 | goto http_msg_ood; \ |
| 1067 | } \ |
| 1068 | } while (0) |
| 1069 | |
| 1070 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1071 | /* |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 1072 | * This function parses a status line between <ptr> and <end>, starting with |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1073 | * parser state <state>. Only states HTTP_MSG_RPVER, HTTP_MSG_RPVER_SP, |
| 1074 | * HTTP_MSG_RPCODE, HTTP_MSG_RPCODE_SP and HTTP_MSG_RPREASON are handled. Others |
| 1075 | * will give undefined results. |
| 1076 | * Note that it is upon the caller's responsibility to ensure that ptr < end, |
| 1077 | * and that msg->sol points to the beginning of the response. |
| 1078 | * If a complete line is found (which implies that at least one CR or LF is |
| 1079 | * found before <end>, the updated <ptr> is returned, otherwise NULL is |
| 1080 | * returned indicating an incomplete line (which does not mean that parts have |
| 1081 | * not been updated). In the incomplete case, if <ret_ptr> or <ret_state> are |
| 1082 | * non-NULL, they are fed with the new <ptr> and <state> values to be passed |
| 1083 | * upon next call. |
| 1084 | * |
Willy Tarreau | 9cdde23 | 2007-05-02 20:58:19 +0200 | [diff] [blame] | 1085 | * This function was intentionally designed to be called from |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1086 | * http_msg_analyzer() with the lowest overhead. It should integrate perfectly |
| 1087 | * 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] | 1088 | * labels and variable names. Note that msg->sol is left unchanged. |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1089 | */ |
Willy Tarreau | 69d8c5d | 2012-05-08 09:44:41 +0200 | [diff] [blame] | 1090 | const char *http_parse_stsline(struct http_msg *msg, |
Willy Tarreau | 3770f23 | 2013-12-07 00:01:53 +0100 | [diff] [blame] | 1091 | enum ht_state state, const char *ptr, const char *end, |
| 1092 | unsigned int *ret_ptr, enum ht_state *ret_state) |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1093 | { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 1094 | const char *msg_start = msg->chn->buf->p; |
Willy Tarreau | 62f791e | 2012-03-09 11:32:30 +0100 | [diff] [blame] | 1095 | |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1096 | switch (state) { |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1097 | case HTTP_MSG_RPVER: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1098 | http_msg_rpver: |
Willy Tarreau | 4b89ad4 | 2007-03-04 18:13:58 +0100 | [diff] [blame] | 1099 | if (likely(HTTP_IS_VER_TOKEN(*ptr))) |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1100 | EAT_AND_JUMP_OR_RETURN(http_msg_rpver, HTTP_MSG_RPVER); |
| 1101 | |
| 1102 | if (likely(HTTP_IS_SPHT(*ptr))) { |
Willy Tarreau | ea1175a | 2012-03-05 15:52:30 +0100 | [diff] [blame] | 1103 | msg->sl.st.v_l = ptr - msg_start; |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1104 | EAT_AND_JUMP_OR_RETURN(http_msg_rpver_sp, HTTP_MSG_RPVER_SP); |
| 1105 | } |
Willy Tarreau | 7552c03 | 2009-03-01 11:10:40 +0100 | [diff] [blame] | 1106 | state = HTTP_MSG_ERROR; |
| 1107 | break; |
| 1108 | |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1109 | case HTTP_MSG_RPVER_SP: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1110 | http_msg_rpver_sp: |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1111 | if (likely(!HTTP_IS_LWS(*ptr))) { |
Willy Tarreau | ea1175a | 2012-03-05 15:52:30 +0100 | [diff] [blame] | 1112 | msg->sl.st.c = ptr - msg_start; |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1113 | goto http_msg_rpcode; |
| 1114 | } |
| 1115 | if (likely(HTTP_IS_SPHT(*ptr))) |
| 1116 | EAT_AND_JUMP_OR_RETURN(http_msg_rpver_sp, HTTP_MSG_RPVER_SP); |
| 1117 | /* so it's a CR/LF, this is invalid */ |
Willy Tarreau | 7552c03 | 2009-03-01 11:10:40 +0100 | [diff] [blame] | 1118 | state = HTTP_MSG_ERROR; |
| 1119 | break; |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1120 | |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1121 | case HTTP_MSG_RPCODE: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1122 | http_msg_rpcode: |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1123 | if (likely(!HTTP_IS_LWS(*ptr))) |
| 1124 | EAT_AND_JUMP_OR_RETURN(http_msg_rpcode, HTTP_MSG_RPCODE); |
| 1125 | |
| 1126 | if (likely(HTTP_IS_SPHT(*ptr))) { |
Willy Tarreau | ea1175a | 2012-03-05 15:52:30 +0100 | [diff] [blame] | 1127 | msg->sl.st.c_l = ptr - msg_start - msg->sl.st.c; |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1128 | EAT_AND_JUMP_OR_RETURN(http_msg_rpcode_sp, HTTP_MSG_RPCODE_SP); |
| 1129 | } |
| 1130 | |
| 1131 | /* so it's a CR/LF, so there is no reason phrase */ |
Willy Tarreau | ea1175a | 2012-03-05 15:52:30 +0100 | [diff] [blame] | 1132 | msg->sl.st.c_l = ptr - msg_start - msg->sl.st.c; |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1133 | http_msg_rsp_reason: |
| 1134 | /* FIXME: should we support HTTP responses without any reason phrase ? */ |
Willy Tarreau | ea1175a | 2012-03-05 15:52:30 +0100 | [diff] [blame] | 1135 | msg->sl.st.r = ptr - msg_start; |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1136 | msg->sl.st.r_l = 0; |
| 1137 | goto http_msg_rpline_eol; |
| 1138 | |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1139 | case HTTP_MSG_RPCODE_SP: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1140 | http_msg_rpcode_sp: |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1141 | if (likely(!HTTP_IS_LWS(*ptr))) { |
Willy Tarreau | ea1175a | 2012-03-05 15:52:30 +0100 | [diff] [blame] | 1142 | msg->sl.st.r = ptr - msg_start; |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1143 | goto http_msg_rpreason; |
| 1144 | } |
| 1145 | if (likely(HTTP_IS_SPHT(*ptr))) |
| 1146 | EAT_AND_JUMP_OR_RETURN(http_msg_rpcode_sp, HTTP_MSG_RPCODE_SP); |
| 1147 | /* so it's a CR/LF, so there is no reason phrase */ |
| 1148 | goto http_msg_rsp_reason; |
| 1149 | |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1150 | case HTTP_MSG_RPREASON: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1151 | http_msg_rpreason: |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1152 | if (likely(!HTTP_IS_CRLF(*ptr))) |
| 1153 | EAT_AND_JUMP_OR_RETURN(http_msg_rpreason, HTTP_MSG_RPREASON); |
Willy Tarreau | ea1175a | 2012-03-05 15:52:30 +0100 | [diff] [blame] | 1154 | msg->sl.st.r_l = ptr - msg_start - msg->sl.st.r; |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1155 | http_msg_rpline_eol: |
| 1156 | /* We have seen the end of line. Note that we do not |
| 1157 | * necessarily have the \n yet, but at least we know that we |
| 1158 | * have EITHER \r OR \n, otherwise the response would not be |
| 1159 | * complete. We can then record the response length and return |
| 1160 | * to the caller which will be able to register it. |
| 1161 | */ |
Willy Tarreau | 3a215be | 2012-03-09 21:39:51 +0100 | [diff] [blame] | 1162 | msg->sl.st.l = ptr - msg_start - msg->sol; |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1163 | return ptr; |
| 1164 | |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1165 | default: |
Willy Tarreau | 3770f23 | 2013-12-07 00:01:53 +0100 | [diff] [blame] | 1166 | #ifdef DEBUG_FULL |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1167 | fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state); |
| 1168 | exit(1); |
| 1169 | #endif |
Willy Tarreau | 3770f23 | 2013-12-07 00:01:53 +0100 | [diff] [blame] | 1170 | ; |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1171 | } |
| 1172 | |
| 1173 | http_msg_ood: |
Willy Tarreau | 7552c03 | 2009-03-01 11:10:40 +0100 | [diff] [blame] | 1174 | /* out of valid data */ |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1175 | if (ret_state) |
| 1176 | *ret_state = state; |
| 1177 | if (ret_ptr) |
Willy Tarreau | a458b67 | 2012-03-05 11:17:50 +0100 | [diff] [blame] | 1178 | *ret_ptr = ptr - msg_start; |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1179 | return NULL; |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1180 | } |
| 1181 | |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1182 | /* |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1183 | * This function parses a request line between <ptr> and <end>, starting with |
| 1184 | * parser state <state>. Only states HTTP_MSG_RQMETH, HTTP_MSG_RQMETH_SP, |
| 1185 | * HTTP_MSG_RQURI, HTTP_MSG_RQURI_SP and HTTP_MSG_RQVER are handled. Others |
| 1186 | * will give undefined results. |
| 1187 | * Note that it is upon the caller's responsibility to ensure that ptr < end, |
| 1188 | * and that msg->sol points to the beginning of the request. |
| 1189 | * If a complete line is found (which implies that at least one CR or LF is |
| 1190 | * found before <end>, the updated <ptr> is returned, otherwise NULL is |
| 1191 | * returned indicating an incomplete line (which does not mean that parts have |
| 1192 | * not been updated). In the incomplete case, if <ret_ptr> or <ret_state> are |
| 1193 | * non-NULL, they are fed with the new <ptr> and <state> values to be passed |
| 1194 | * upon next call. |
| 1195 | * |
Willy Tarreau | 9cdde23 | 2007-05-02 20:58:19 +0200 | [diff] [blame] | 1196 | * This function was intentionally designed to be called from |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1197 | * http_msg_analyzer() with the lowest overhead. It should integrate perfectly |
| 1198 | * 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] | 1199 | * labels and variable names. Note that msg->sol is left unchanged. |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1200 | */ |
Willy Tarreau | 69d8c5d | 2012-05-08 09:44:41 +0200 | [diff] [blame] | 1201 | const char *http_parse_reqline(struct http_msg *msg, |
Willy Tarreau | 3770f23 | 2013-12-07 00:01:53 +0100 | [diff] [blame] | 1202 | enum ht_state state, const char *ptr, const char *end, |
| 1203 | unsigned int *ret_ptr, enum ht_state *ret_state) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1204 | { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 1205 | const char *msg_start = msg->chn->buf->p; |
Willy Tarreau | 62f791e | 2012-03-09 11:32:30 +0100 | [diff] [blame] | 1206 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1207 | switch (state) { |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1208 | case HTTP_MSG_RQMETH: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1209 | http_msg_rqmeth: |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1210 | if (likely(HTTP_IS_TOKEN(*ptr))) |
| 1211 | EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth, HTTP_MSG_RQMETH); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1212 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1213 | if (likely(HTTP_IS_SPHT(*ptr))) { |
Willy Tarreau | ea1175a | 2012-03-05 15:52:30 +0100 | [diff] [blame] | 1214 | msg->sl.rq.m_l = ptr - msg_start; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1215 | EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth_sp, HTTP_MSG_RQMETH_SP); |
| 1216 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1217 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1218 | if (likely(HTTP_IS_CRLF(*ptr))) { |
| 1219 | /* HTTP 0.9 request */ |
Willy Tarreau | ea1175a | 2012-03-05 15:52:30 +0100 | [diff] [blame] | 1220 | msg->sl.rq.m_l = ptr - msg_start; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1221 | http_msg_req09_uri: |
Willy Tarreau | ea1175a | 2012-03-05 15:52:30 +0100 | [diff] [blame] | 1222 | msg->sl.rq.u = ptr - msg_start; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1223 | http_msg_req09_uri_e: |
Willy Tarreau | ea1175a | 2012-03-05 15:52:30 +0100 | [diff] [blame] | 1224 | msg->sl.rq.u_l = ptr - msg_start - msg->sl.rq.u; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1225 | http_msg_req09_ver: |
Willy Tarreau | ea1175a | 2012-03-05 15:52:30 +0100 | [diff] [blame] | 1226 | msg->sl.rq.v = ptr - msg_start; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1227 | msg->sl.rq.v_l = 0; |
| 1228 | goto http_msg_rqline_eol; |
| 1229 | } |
Willy Tarreau | 7552c03 | 2009-03-01 11:10:40 +0100 | [diff] [blame] | 1230 | state = HTTP_MSG_ERROR; |
| 1231 | break; |
| 1232 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1233 | case HTTP_MSG_RQMETH_SP: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1234 | http_msg_rqmeth_sp: |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1235 | if (likely(!HTTP_IS_LWS(*ptr))) { |
Willy Tarreau | ea1175a | 2012-03-05 15:52:30 +0100 | [diff] [blame] | 1236 | msg->sl.rq.u = ptr - msg_start; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1237 | goto http_msg_rquri; |
| 1238 | } |
| 1239 | if (likely(HTTP_IS_SPHT(*ptr))) |
| 1240 | EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth_sp, HTTP_MSG_RQMETH_SP); |
| 1241 | /* so it's a CR/LF, meaning an HTTP 0.9 request */ |
| 1242 | goto http_msg_req09_uri; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1243 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1244 | case HTTP_MSG_RQURI: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1245 | http_msg_rquri: |
Willy Tarreau | 2e9506d | 2012-01-07 23:22:31 +0100 | [diff] [blame] | 1246 | if (likely((unsigned char)(*ptr - 33) <= 93)) /* 33 to 126 included */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1247 | EAT_AND_JUMP_OR_RETURN(http_msg_rquri, HTTP_MSG_RQURI); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1248 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1249 | if (likely(HTTP_IS_SPHT(*ptr))) { |
Willy Tarreau | ea1175a | 2012-03-05 15:52:30 +0100 | [diff] [blame] | 1250 | msg->sl.rq.u_l = ptr - msg_start - msg->sl.rq.u; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1251 | EAT_AND_JUMP_OR_RETURN(http_msg_rquri_sp, HTTP_MSG_RQURI_SP); |
| 1252 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1253 | |
Willy Tarreau | 2e9506d | 2012-01-07 23:22:31 +0100 | [diff] [blame] | 1254 | if (likely((unsigned char)*ptr >= 128)) { |
Willy Tarreau | 422246e | 2012-01-07 23:54:13 +0100 | [diff] [blame] | 1255 | /* non-ASCII chars are forbidden unless option |
| 1256 | * accept-invalid-http-request is enabled in the frontend. |
| 1257 | * In any case, we capture the faulty char. |
Willy Tarreau | 2e9506d | 2012-01-07 23:22:31 +0100 | [diff] [blame] | 1258 | */ |
Willy Tarreau | 422246e | 2012-01-07 23:54:13 +0100 | [diff] [blame] | 1259 | if (msg->err_pos < -1) |
| 1260 | goto invalid_char; |
| 1261 | if (msg->err_pos == -1) |
Willy Tarreau | 69d8c5d | 2012-05-08 09:44:41 +0200 | [diff] [blame] | 1262 | msg->err_pos = ptr - msg_start; |
Willy Tarreau | 2e9506d | 2012-01-07 23:22:31 +0100 | [diff] [blame] | 1263 | EAT_AND_JUMP_OR_RETURN(http_msg_rquri, HTTP_MSG_RQURI); |
| 1264 | } |
| 1265 | |
| 1266 | if (likely(HTTP_IS_CRLF(*ptr))) { |
| 1267 | /* so it's a CR/LF, meaning an HTTP 0.9 request */ |
| 1268 | goto http_msg_req09_uri_e; |
| 1269 | } |
| 1270 | |
| 1271 | /* OK forbidden chars, 0..31 or 127 */ |
Willy Tarreau | 422246e | 2012-01-07 23:54:13 +0100 | [diff] [blame] | 1272 | invalid_char: |
Willy Tarreau | 69d8c5d | 2012-05-08 09:44:41 +0200 | [diff] [blame] | 1273 | msg->err_pos = ptr - msg_start; |
Willy Tarreau | 2e9506d | 2012-01-07 23:22:31 +0100 | [diff] [blame] | 1274 | state = HTTP_MSG_ERROR; |
| 1275 | break; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1276 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1277 | case HTTP_MSG_RQURI_SP: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1278 | http_msg_rquri_sp: |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1279 | if (likely(!HTTP_IS_LWS(*ptr))) { |
Willy Tarreau | ea1175a | 2012-03-05 15:52:30 +0100 | [diff] [blame] | 1280 | msg->sl.rq.v = ptr - msg_start; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1281 | goto http_msg_rqver; |
| 1282 | } |
| 1283 | if (likely(HTTP_IS_SPHT(*ptr))) |
| 1284 | EAT_AND_JUMP_OR_RETURN(http_msg_rquri_sp, HTTP_MSG_RQURI_SP); |
| 1285 | /* so it's a CR/LF, meaning an HTTP 0.9 request */ |
| 1286 | goto http_msg_req09_ver; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1287 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1288 | case HTTP_MSG_RQVER: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1289 | http_msg_rqver: |
Willy Tarreau | 4b89ad4 | 2007-03-04 18:13:58 +0100 | [diff] [blame] | 1290 | if (likely(HTTP_IS_VER_TOKEN(*ptr))) |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1291 | EAT_AND_JUMP_OR_RETURN(http_msg_rqver, HTTP_MSG_RQVER); |
Willy Tarreau | 4b89ad4 | 2007-03-04 18:13:58 +0100 | [diff] [blame] | 1292 | |
| 1293 | if (likely(HTTP_IS_CRLF(*ptr))) { |
Willy Tarreau | ea1175a | 2012-03-05 15:52:30 +0100 | [diff] [blame] | 1294 | msg->sl.rq.v_l = ptr - msg_start - msg->sl.rq.v; |
Willy Tarreau | 4b89ad4 | 2007-03-04 18:13:58 +0100 | [diff] [blame] | 1295 | http_msg_rqline_eol: |
| 1296 | /* We have seen the end of line. Note that we do not |
| 1297 | * necessarily have the \n yet, but at least we know that we |
| 1298 | * have EITHER \r OR \n, otherwise the request would not be |
| 1299 | * complete. We can then record the request length and return |
| 1300 | * to the caller which will be able to register it. |
| 1301 | */ |
Willy Tarreau | 3a215be | 2012-03-09 21:39:51 +0100 | [diff] [blame] | 1302 | msg->sl.rq.l = ptr - msg_start - msg->sol; |
Willy Tarreau | 4b89ad4 | 2007-03-04 18:13:58 +0100 | [diff] [blame] | 1303 | return ptr; |
| 1304 | } |
| 1305 | |
| 1306 | /* neither an HTTP_VER token nor a CRLF */ |
Willy Tarreau | 7552c03 | 2009-03-01 11:10:40 +0100 | [diff] [blame] | 1307 | state = HTTP_MSG_ERROR; |
| 1308 | break; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1309 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1310 | default: |
Willy Tarreau | 3770f23 | 2013-12-07 00:01:53 +0100 | [diff] [blame] | 1311 | #ifdef DEBUG_FULL |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1312 | fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state); |
| 1313 | exit(1); |
| 1314 | #endif |
Willy Tarreau | 3770f23 | 2013-12-07 00:01:53 +0100 | [diff] [blame] | 1315 | ; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1316 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1317 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1318 | http_msg_ood: |
Willy Tarreau | 7552c03 | 2009-03-01 11:10:40 +0100 | [diff] [blame] | 1319 | /* out of valid data */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1320 | if (ret_state) |
| 1321 | *ret_state = state; |
| 1322 | if (ret_ptr) |
Willy Tarreau | a458b67 | 2012-03-05 11:17:50 +0100 | [diff] [blame] | 1323 | *ret_ptr = ptr - msg_start; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1324 | return NULL; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1325 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1326 | |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 1327 | /* |
| 1328 | * Returns the data from Authorization header. Function may be called more |
| 1329 | * than once so data is stored in txn->auth_data. When no header is found |
| 1330 | * 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] | 1331 | * searching again for something we are unable to find anyway. However, if |
| 1332 | * the result if valid, the cache is not reused because we would risk to |
| 1333 | * have the credentials overwritten by another session in parallel. |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 1334 | */ |
| 1335 | |
Willy Tarreau | 7e2c647 | 2012-10-29 20:44:36 +0100 | [diff] [blame] | 1336 | char *get_http_auth_buff; |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 1337 | |
| 1338 | int |
| 1339 | get_http_auth(struct session *s) |
| 1340 | { |
| 1341 | |
| 1342 | struct http_txn *txn = &s->txn; |
| 1343 | struct chunk auth_method; |
| 1344 | struct hdr_ctx ctx; |
| 1345 | char *h, *p; |
| 1346 | int len; |
| 1347 | |
| 1348 | #ifdef DEBUG_AUTH |
| 1349 | printf("Auth for session %p: %d\n", s, txn->auth.method); |
| 1350 | #endif |
| 1351 | |
| 1352 | if (txn->auth.method == HTTP_AUTH_WRONG) |
| 1353 | return 0; |
| 1354 | |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 1355 | txn->auth.method = HTTP_AUTH_WRONG; |
| 1356 | |
| 1357 | ctx.idx = 0; |
Willy Tarreau | 844a7e7 | 2010-01-31 21:46:18 +0100 | [diff] [blame] | 1358 | |
| 1359 | if (txn->flags & TX_USE_PX_CONN) { |
| 1360 | h = "Proxy-Authorization"; |
| 1361 | len = strlen(h); |
| 1362 | } else { |
| 1363 | h = "Authorization"; |
| 1364 | len = strlen(h); |
| 1365 | } |
| 1366 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 1367 | 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] | 1368 | return 0; |
| 1369 | |
| 1370 | h = ctx.line + ctx.val; |
| 1371 | |
| 1372 | p = memchr(h, ' ', ctx.vlen); |
| 1373 | if (!p || p == h) |
| 1374 | return 0; |
| 1375 | |
| 1376 | chunk_initlen(&auth_method, h, 0, p-h); |
| 1377 | chunk_initlen(&txn->auth.method_data, p+1, 0, ctx.vlen-(p-h)-1); |
| 1378 | |
| 1379 | if (!strncasecmp("Basic", auth_method.str, auth_method.len)) { |
| 1380 | |
| 1381 | len = base64dec(txn->auth.method_data.str, txn->auth.method_data.len, |
Willy Tarreau | 7e2c647 | 2012-10-29 20:44:36 +0100 | [diff] [blame] | 1382 | get_http_auth_buff, global.tune.bufsize - 1); |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 1383 | |
| 1384 | if (len < 0) |
| 1385 | return 0; |
| 1386 | |
| 1387 | |
| 1388 | get_http_auth_buff[len] = '\0'; |
| 1389 | |
| 1390 | p = strchr(get_http_auth_buff, ':'); |
| 1391 | |
| 1392 | if (!p) |
| 1393 | return 0; |
| 1394 | |
| 1395 | txn->auth.user = get_http_auth_buff; |
| 1396 | *p = '\0'; |
| 1397 | txn->auth.pass = p+1; |
| 1398 | |
| 1399 | txn->auth.method = HTTP_AUTH_BASIC; |
| 1400 | return 1; |
| 1401 | } |
| 1402 | |
| 1403 | return 0; |
| 1404 | } |
| 1405 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1406 | |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1407 | /* |
| 1408 | * This function parses an HTTP message, either a request or a response, |
Willy Tarreau | 8b1323e | 2012-03-09 14:46:19 +0100 | [diff] [blame] | 1409 | * depending on the initial msg->msg_state. The caller is responsible for |
| 1410 | * ensuring that the message does not wrap. The function can be preempted |
| 1411 | * everywhere when data are missing and recalled at the exact same location |
| 1412 | * with no information loss. The message may even be realigned between two |
| 1413 | * calls. The header index is re-initialized when switching from |
Willy Tarreau | 9cdde23 | 2007-05-02 20:58:19 +0200 | [diff] [blame] | 1414 | * 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] | 1415 | * fields. Note that msg->sol will be initialized after completing the first |
| 1416 | * state, so that none of the msg pointers has to be initialized prior to the |
| 1417 | * first call. |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1418 | */ |
Willy Tarreau | a560c21 | 2012-03-09 13:50:57 +0100 | [diff] [blame] | 1419 | void http_msg_analyzer(struct http_msg *msg, struct hdr_idx *idx) |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1420 | { |
Willy Tarreau | 3770f23 | 2013-12-07 00:01:53 +0100 | [diff] [blame] | 1421 | enum ht_state state; /* updated only when leaving the FSM */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1422 | register char *ptr, *end; /* request pointers, to avoid dereferences */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1423 | struct buffer *buf; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1424 | |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1425 | state = msg->msg_state; |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 1426 | buf = msg->chn->buf; |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1427 | ptr = buf->p + msg->next; |
| 1428 | end = buf->p + buf->i; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1429 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1430 | if (unlikely(ptr >= end)) |
| 1431 | goto http_msg_ood; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1432 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1433 | switch (state) { |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1434 | /* |
| 1435 | * First, states that are specific to the response only. |
| 1436 | * We check them first so that request and headers are |
| 1437 | * closer to each other (accessed more often). |
| 1438 | */ |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1439 | case HTTP_MSG_RPBEFORE: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1440 | http_msg_rpbefore: |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1441 | if (likely(HTTP_IS_TOKEN(*ptr))) { |
Willy Tarreau | 15de77e | 2010-01-02 21:59:16 +0100 | [diff] [blame] | 1442 | /* we have a start of message, but we have to check |
| 1443 | * first if we need to remove some CRLF. We can only |
Willy Tarreau | 2e046c6 | 2012-03-01 16:08:30 +0100 | [diff] [blame] | 1444 | * do this when o=0. |
Willy Tarreau | 15de77e | 2010-01-02 21:59:16 +0100 | [diff] [blame] | 1445 | */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1446 | if (unlikely(ptr != buf->p)) { |
| 1447 | if (buf->o) |
Willy Tarreau | 15de77e | 2010-01-02 21:59:16 +0100 | [diff] [blame] | 1448 | goto http_msg_ood; |
Willy Tarreau | 1d3bcce | 2009-12-27 15:50:06 +0100 | [diff] [blame] | 1449 | /* Remove empty leading lines, as recommended by RFC2616. */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1450 | bi_fast_delete(buf, ptr - buf->p); |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1451 | } |
Willy Tarreau | 2692736 | 2012-05-18 23:22:52 +0200 | [diff] [blame] | 1452 | msg->sol = 0; |
Willy Tarreau | e92693a | 2012-09-24 21:13:39 +0200 | [diff] [blame] | 1453 | msg->sl.st.l = 0; /* used in debug mode */ |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1454 | hdr_idx_init(idx); |
| 1455 | state = HTTP_MSG_RPVER; |
| 1456 | goto http_msg_rpver; |
| 1457 | } |
| 1458 | |
| 1459 | if (unlikely(!HTTP_IS_CRLF(*ptr))) |
| 1460 | goto http_msg_invalid; |
| 1461 | |
| 1462 | if (unlikely(*ptr == '\n')) |
| 1463 | EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore, HTTP_MSG_RPBEFORE); |
| 1464 | EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore_cr, HTTP_MSG_RPBEFORE_CR); |
| 1465 | /* stop here */ |
| 1466 | |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1467 | case HTTP_MSG_RPBEFORE_CR: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1468 | http_msg_rpbefore_cr: |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1469 | EXPECT_LF_HERE(ptr, http_msg_invalid); |
| 1470 | EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore, HTTP_MSG_RPBEFORE); |
| 1471 | /* stop here */ |
| 1472 | |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1473 | case HTTP_MSG_RPVER: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1474 | http_msg_rpver: |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1475 | case HTTP_MSG_RPVER_SP: |
| 1476 | case HTTP_MSG_RPCODE: |
| 1477 | case HTTP_MSG_RPCODE_SP: |
| 1478 | case HTTP_MSG_RPREASON: |
Willy Tarreau | 69d8c5d | 2012-05-08 09:44:41 +0200 | [diff] [blame] | 1479 | ptr = (char *)http_parse_stsline(msg, |
Willy Tarreau | a458b67 | 2012-03-05 11:17:50 +0100 | [diff] [blame] | 1480 | state, ptr, end, |
| 1481 | &msg->next, &msg->msg_state); |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1482 | if (unlikely(!ptr)) |
| 1483 | return; |
| 1484 | |
| 1485 | /* we have a full response and we know that we have either a CR |
| 1486 | * or an LF at <ptr>. |
| 1487 | */ |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1488 | hdr_idx_set_start(idx, msg->sl.st.l, *ptr == '\r'); |
| 1489 | |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1490 | msg->sol = ptr - buf->p; |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1491 | if (likely(*ptr == '\r')) |
| 1492 | EAT_AND_JUMP_OR_RETURN(http_msg_rpline_end, HTTP_MSG_RPLINE_END); |
| 1493 | goto http_msg_rpline_end; |
| 1494 | |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1495 | case HTTP_MSG_RPLINE_END: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1496 | http_msg_rpline_end: |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1497 | /* msg->sol must point to the first of CR or LF. */ |
| 1498 | EXPECT_LF_HERE(ptr, http_msg_invalid); |
| 1499 | EAT_AND_JUMP_OR_RETURN(http_msg_hdr_first, HTTP_MSG_HDR_FIRST); |
| 1500 | /* stop here */ |
| 1501 | |
| 1502 | /* |
| 1503 | * Second, states that are specific to the request only |
| 1504 | */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1505 | case HTTP_MSG_RQBEFORE: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1506 | http_msg_rqbefore: |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1507 | if (likely(HTTP_IS_TOKEN(*ptr))) { |
Willy Tarreau | 15de77e | 2010-01-02 21:59:16 +0100 | [diff] [blame] | 1508 | /* we have a start of message, but we have to check |
| 1509 | * first if we need to remove some CRLF. We can only |
Willy Tarreau | 2e046c6 | 2012-03-01 16:08:30 +0100 | [diff] [blame] | 1510 | * do this when o=0. |
Willy Tarreau | 15de77e | 2010-01-02 21:59:16 +0100 | [diff] [blame] | 1511 | */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1512 | if (likely(ptr != buf->p)) { |
| 1513 | if (buf->o) |
Willy Tarreau | 15de77e | 2010-01-02 21:59:16 +0100 | [diff] [blame] | 1514 | goto http_msg_ood; |
Willy Tarreau | 1d3bcce | 2009-12-27 15:50:06 +0100 | [diff] [blame] | 1515 | /* Remove empty leading lines, as recommended by RFC2616. */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1516 | bi_fast_delete(buf, ptr - buf->p); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1517 | } |
Willy Tarreau | 2692736 | 2012-05-18 23:22:52 +0200 | [diff] [blame] | 1518 | msg->sol = 0; |
Willy Tarreau | e92693a | 2012-09-24 21:13:39 +0200 | [diff] [blame] | 1519 | msg->sl.rq.l = 0; /* used in debug mode */ |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1520 | state = HTTP_MSG_RQMETH; |
| 1521 | goto http_msg_rqmeth; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1522 | } |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1523 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1524 | if (unlikely(!HTTP_IS_CRLF(*ptr))) |
| 1525 | goto http_msg_invalid; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1526 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1527 | if (unlikely(*ptr == '\n')) |
| 1528 | EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore, HTTP_MSG_RQBEFORE); |
| 1529 | 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] | 1530 | /* stop here */ |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1531 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1532 | case HTTP_MSG_RQBEFORE_CR: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1533 | http_msg_rqbefore_cr: |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1534 | EXPECT_LF_HERE(ptr, http_msg_invalid); |
| 1535 | EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore, HTTP_MSG_RQBEFORE); |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1536 | /* stop here */ |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1537 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1538 | case HTTP_MSG_RQMETH: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1539 | http_msg_rqmeth: |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1540 | case HTTP_MSG_RQMETH_SP: |
| 1541 | case HTTP_MSG_RQURI: |
| 1542 | case HTTP_MSG_RQURI_SP: |
| 1543 | case HTTP_MSG_RQVER: |
Willy Tarreau | 69d8c5d | 2012-05-08 09:44:41 +0200 | [diff] [blame] | 1544 | ptr = (char *)http_parse_reqline(msg, |
Willy Tarreau | a458b67 | 2012-03-05 11:17:50 +0100 | [diff] [blame] | 1545 | state, ptr, end, |
| 1546 | &msg->next, &msg->msg_state); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1547 | if (unlikely(!ptr)) |
| 1548 | return; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1549 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1550 | /* we have a full request and we know that we have either a CR |
| 1551 | * or an LF at <ptr>. |
| 1552 | */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1553 | hdr_idx_set_start(idx, msg->sl.rq.l, *ptr == '\r'); |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1554 | |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1555 | msg->sol = ptr - buf->p; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1556 | if (likely(*ptr == '\r')) |
| 1557 | 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] | 1558 | goto http_msg_rqline_end; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1559 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1560 | case HTTP_MSG_RQLINE_END: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1561 | http_msg_rqline_end: |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1562 | /* check for HTTP/0.9 request : no version information available. |
| 1563 | * msg->sol must point to the first of CR or LF. |
| 1564 | */ |
| 1565 | if (unlikely(msg->sl.rq.v_l == 0)) |
| 1566 | goto http_msg_last_lf; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1567 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1568 | EXPECT_LF_HERE(ptr, http_msg_invalid); |
| 1569 | 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] | 1570 | /* stop here */ |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1571 | |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1572 | /* |
| 1573 | * Common states below |
| 1574 | */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1575 | case HTTP_MSG_HDR_FIRST: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1576 | http_msg_hdr_first: |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1577 | msg->sol = ptr - buf->p; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1578 | if (likely(!HTTP_IS_CRLF(*ptr))) { |
| 1579 | goto http_msg_hdr_name; |
| 1580 | } |
| 1581 | |
| 1582 | if (likely(*ptr == '\r')) |
| 1583 | EAT_AND_JUMP_OR_RETURN(http_msg_last_lf, HTTP_MSG_LAST_LF); |
| 1584 | goto http_msg_last_lf; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1585 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1586 | case HTTP_MSG_HDR_NAME: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1587 | http_msg_hdr_name: |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1588 | /* assumes msg->sol points to the first char */ |
| 1589 | if (likely(HTTP_IS_TOKEN(*ptr))) |
| 1590 | 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] | 1591 | |
Willy Tarreau | fa4a03c | 2012-03-09 21:28:54 +0100 | [diff] [blame] | 1592 | if (likely(*ptr == ':')) |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1593 | 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] | 1594 | |
Willy Tarreau | 32a4ec0 | 2009-04-02 11:35:18 +0200 | [diff] [blame] | 1595 | if (likely(msg->err_pos < -1) || *ptr == '\n') |
| 1596 | goto http_msg_invalid; |
| 1597 | |
| 1598 | if (msg->err_pos == -1) /* capture error pointer */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1599 | msg->err_pos = ptr - buf->p; /* >= 0 now */ |
Willy Tarreau | 32a4ec0 | 2009-04-02 11:35:18 +0200 | [diff] [blame] | 1600 | |
| 1601 | /* and we still accept this non-token character */ |
| 1602 | 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] | 1603 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1604 | case HTTP_MSG_HDR_L1_SP: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1605 | http_msg_hdr_l1_sp: |
Willy Tarreau | fa4a03c | 2012-03-09 21:28:54 +0100 | [diff] [blame] | 1606 | /* assumes msg->sol points to the first char */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1607 | if (likely(HTTP_IS_SPHT(*ptr))) |
| 1608 | 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] | 1609 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1610 | /* header value can be basically anything except CR/LF */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1611 | msg->sov = ptr - buf->p; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1612 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1613 | if (likely(!HTTP_IS_CRLF(*ptr))) { |
| 1614 | goto http_msg_hdr_val; |
| 1615 | } |
| 1616 | |
| 1617 | if (likely(*ptr == '\r')) |
| 1618 | EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_lf, HTTP_MSG_HDR_L1_LF); |
| 1619 | goto http_msg_hdr_l1_lf; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1620 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1621 | case HTTP_MSG_HDR_L1_LF: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1622 | http_msg_hdr_l1_lf: |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1623 | EXPECT_LF_HERE(ptr, http_msg_invalid); |
| 1624 | 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] | 1625 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1626 | case HTTP_MSG_HDR_L1_LWS: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1627 | http_msg_hdr_l1_lws: |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1628 | if (likely(HTTP_IS_SPHT(*ptr))) { |
| 1629 | /* replace HT,CR,LF with spaces */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1630 | for (; buf->p + msg->sov < ptr; msg->sov++) |
| 1631 | buf->p[msg->sov] = ' '; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1632 | goto http_msg_hdr_l1_sp; |
| 1633 | } |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 1634 | /* we had a header consisting only in spaces ! */ |
Willy Tarreau | 12e48b3 | 2012-03-05 16:57:34 +0100 | [diff] [blame] | 1635 | msg->eol = msg->sov; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1636 | goto http_msg_complete_header; |
| 1637 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1638 | case HTTP_MSG_HDR_VAL: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1639 | http_msg_hdr_val: |
Willy Tarreau | fa4a03c | 2012-03-09 21:28:54 +0100 | [diff] [blame] | 1640 | /* assumes msg->sol points to the first char, and msg->sov |
| 1641 | * points to the first character of the value. |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1642 | */ |
| 1643 | if (likely(!HTTP_IS_CRLF(*ptr))) |
| 1644 | 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] | 1645 | |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1646 | msg->eol = ptr - buf->p; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1647 | /* Note: we could also copy eol into ->eoh so that we have the |
| 1648 | * real header end in case it ends with lots of LWS, but is this |
| 1649 | * really needed ? |
| 1650 | */ |
| 1651 | if (likely(*ptr == '\r')) |
| 1652 | EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l2_lf, HTTP_MSG_HDR_L2_LF); |
| 1653 | goto http_msg_hdr_l2_lf; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1654 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1655 | case HTTP_MSG_HDR_L2_LF: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1656 | http_msg_hdr_l2_lf: |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1657 | EXPECT_LF_HERE(ptr, http_msg_invalid); |
| 1658 | 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] | 1659 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1660 | case HTTP_MSG_HDR_L2_LWS: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1661 | http_msg_hdr_l2_lws: |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1662 | if (unlikely(HTTP_IS_SPHT(*ptr))) { |
| 1663 | /* LWS: replace HT,CR,LF with spaces */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1664 | for (; buf->p + msg->eol < ptr; msg->eol++) |
| 1665 | buf->p[msg->eol] = ' '; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1666 | goto http_msg_hdr_val; |
| 1667 | } |
| 1668 | http_msg_complete_header: |
| 1669 | /* |
| 1670 | * It was a new header, so the last one is finished. |
Willy Tarreau | fa4a03c | 2012-03-09 21:28:54 +0100 | [diff] [blame] | 1671 | * Assumes msg->sol points to the first char, msg->sov points |
| 1672 | * to the first character of the value and msg->eol to the |
| 1673 | * first CR or LF so we know how the line ends. We insert last |
| 1674 | * header into the index. |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1675 | */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1676 | 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] | 1677 | idx, idx->tail) < 0)) |
| 1678 | goto http_msg_invalid; |
Willy Tarreau | 230fd0b | 2006-12-17 12:05:00 +0100 | [diff] [blame] | 1679 | |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1680 | msg->sol = ptr - buf->p; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1681 | if (likely(!HTTP_IS_CRLF(*ptr))) { |
| 1682 | goto http_msg_hdr_name; |
| 1683 | } |
| 1684 | |
| 1685 | if (likely(*ptr == '\r')) |
| 1686 | EAT_AND_JUMP_OR_RETURN(http_msg_last_lf, HTTP_MSG_LAST_LF); |
| 1687 | goto http_msg_last_lf; |
Willy Tarreau | 230fd0b | 2006-12-17 12:05:00 +0100 | [diff] [blame] | 1688 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1689 | case HTTP_MSG_LAST_LF: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1690 | http_msg_last_lf: |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1691 | /* Assumes msg->sol points to the first of either CR or LF */ |
| 1692 | EXPECT_LF_HERE(ptr, http_msg_invalid); |
| 1693 | ptr++; |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1694 | msg->sov = msg->next = ptr - buf->p; |
Willy Tarreau | 3a215be | 2012-03-09 21:39:51 +0100 | [diff] [blame] | 1695 | msg->eoh = msg->sol; |
| 1696 | msg->sol = 0; |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1697 | msg->msg_state = HTTP_MSG_BODY; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1698 | return; |
Willy Tarreau | b56928a | 2012-04-16 14:51:55 +0200 | [diff] [blame] | 1699 | |
| 1700 | case HTTP_MSG_ERROR: |
| 1701 | /* this may only happen if we call http_msg_analyser() twice with an error */ |
| 1702 | break; |
| 1703 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1704 | default: |
Willy Tarreau | 3770f23 | 2013-12-07 00:01:53 +0100 | [diff] [blame] | 1705 | #ifdef DEBUG_FULL |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1706 | fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state); |
| 1707 | exit(1); |
Willy Tarreau | 230fd0b | 2006-12-17 12:05:00 +0100 | [diff] [blame] | 1708 | #endif |
Willy Tarreau | 3770f23 | 2013-12-07 00:01:53 +0100 | [diff] [blame] | 1709 | ; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1710 | } |
| 1711 | http_msg_ood: |
| 1712 | /* out of data */ |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1713 | msg->msg_state = state; |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1714 | msg->next = ptr - buf->p; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1715 | return; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1716 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1717 | http_msg_invalid: |
| 1718 | /* invalid message */ |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1719 | msg->msg_state = HTTP_MSG_ERROR; |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1720 | msg->next = ptr - buf->p; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1721 | return; |
| 1722 | } |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 1723 | |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 1724 | /* convert an HTTP/0.9 request into an HTTP/1.0 request. Returns 1 if the |
| 1725 | * conversion succeeded, 0 in case of error. If the request was already 1.X, |
| 1726 | * nothing is done and 1 is returned. |
| 1727 | */ |
Willy Tarreau | 418bfcc | 2012-03-09 13:56:20 +0100 | [diff] [blame] | 1728 | static int http_upgrade_v09_to_v10(struct http_txn *txn) |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 1729 | { |
| 1730 | int delta; |
| 1731 | char *cur_end; |
Willy Tarreau | 418bfcc | 2012-03-09 13:56:20 +0100 | [diff] [blame] | 1732 | struct http_msg *msg = &txn->req; |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 1733 | |
| 1734 | if (msg->sl.rq.v_l != 0) |
| 1735 | return 1; |
| 1736 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 1737 | cur_end = msg->chn->buf->p + msg->sl.rq.l; |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 1738 | delta = 0; |
| 1739 | |
| 1740 | if (msg->sl.rq.u_l == 0) { |
| 1741 | /* if no URI was set, add "/" */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 1742 | delta = buffer_replace2(msg->chn->buf, cur_end, cur_end, " /", 2); |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 1743 | cur_end += delta; |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 1744 | http_msg_move_end(msg, delta); |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 1745 | } |
| 1746 | /* add HTTP version */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 1747 | 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] | 1748 | http_msg_move_end(msg, delta); |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 1749 | cur_end += delta; |
Willy Tarreau | 69d8c5d | 2012-05-08 09:44:41 +0200 | [diff] [blame] | 1750 | cur_end = (char *)http_parse_reqline(msg, |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 1751 | HTTP_MSG_RQMETH, |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 1752 | msg->chn->buf->p, cur_end + 1, |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 1753 | NULL, NULL); |
| 1754 | if (unlikely(!cur_end)) |
| 1755 | return 0; |
| 1756 | |
| 1757 | /* we have a full HTTP/1.0 request now and we know that |
| 1758 | * we have either a CR or an LF at <ptr>. |
| 1759 | */ |
| 1760 | hdr_idx_set_start(&txn->hdr_idx, msg->sl.rq.l, *cur_end == '\r'); |
| 1761 | return 1; |
| 1762 | } |
| 1763 | |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 1764 | /* Parse the Connection: header of an HTTP request, looking for both "close" |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 1765 | * and "keep-alive" values. If we already know that some headers may safely |
| 1766 | * 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] | 1767 | * - bit 0 means remove "close" headers (in HTTP/1.0 requests/responses) |
| 1768 | * - 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] | 1769 | * Presence of the "Upgrade" token is also checked and reported. |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 1770 | * The TX_HDR_CONN_* flags are adjusted in txn->flags depending on what was |
| 1771 | * found, and TX_CON_*_SET is adjusted depending on what is left so only |
| 1772 | * harmless combinations may be removed. Do not call that after changes have |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 1773 | * been processed. |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 1774 | */ |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 1775 | 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] | 1776 | { |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 1777 | struct hdr_ctx ctx; |
Willy Tarreau | 88d349d | 2010-01-25 12:15:43 +0100 | [diff] [blame] | 1778 | const char *hdr_val = "Connection"; |
| 1779 | int hdr_len = 10; |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 1780 | |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 1781 | if (txn->flags & TX_HDR_CONN_PRS) |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 1782 | return; |
| 1783 | |
Willy Tarreau | 88d349d | 2010-01-25 12:15:43 +0100 | [diff] [blame] | 1784 | if (unlikely(txn->flags & TX_USE_PX_CONN)) { |
| 1785 | hdr_val = "Proxy-Connection"; |
| 1786 | hdr_len = 16; |
| 1787 | } |
| 1788 | |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 1789 | ctx.idx = 0; |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 1790 | txn->flags &= ~(TX_CON_KAL_SET|TX_CON_CLO_SET); |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 1791 | 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] | 1792 | if (ctx.vlen >= 10 && word_match(ctx.line + ctx.val, ctx.vlen, "keep-alive", 10)) { |
| 1793 | txn->flags |= TX_HDR_CONN_KAL; |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 1794 | if (to_del & 2) |
| 1795 | http_remove_header2(msg, &txn->hdr_idx, &ctx); |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 1796 | else |
| 1797 | txn->flags |= TX_CON_KAL_SET; |
| 1798 | } |
| 1799 | else if (ctx.vlen >= 5 && word_match(ctx.line + ctx.val, ctx.vlen, "close", 5)) { |
| 1800 | txn->flags |= TX_HDR_CONN_CLO; |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 1801 | if (to_del & 1) |
| 1802 | http_remove_header2(msg, &txn->hdr_idx, &ctx); |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 1803 | else |
| 1804 | txn->flags |= TX_CON_CLO_SET; |
| 1805 | } |
Willy Tarreau | 50fc777 | 2012-11-11 22:19:57 +0100 | [diff] [blame] | 1806 | else if (ctx.vlen >= 7 && word_match(ctx.line + ctx.val, ctx.vlen, "upgrade", 7)) { |
| 1807 | txn->flags |= TX_HDR_CONN_UPG; |
| 1808 | } |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 1809 | } |
| 1810 | |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 1811 | txn->flags |= TX_HDR_CONN_PRS; |
| 1812 | return; |
| 1813 | } |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 1814 | |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 1815 | /* Apply desired changes on the Connection: header. Values may be removed and/or |
| 1816 | * added depending on the <wanted> flags, which are exclusively composed of |
| 1817 | * TX_CON_CLO_SET and TX_CON_KAL_SET, depending on what flags are desired. The |
| 1818 | * TX_CON_*_SET flags are adjusted in txn->flags depending on what is left. |
| 1819 | */ |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 1820 | 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] | 1821 | { |
| 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 | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 1825 | |
| 1826 | ctx.idx = 0; |
| 1827 | |
Willy Tarreau | 88d349d | 2010-01-25 12:15:43 +0100 | [diff] [blame] | 1828 | |
| 1829 | if (unlikely(txn->flags & TX_USE_PX_CONN)) { |
| 1830 | hdr_val = "Proxy-Connection"; |
| 1831 | hdr_len = 16; |
| 1832 | } |
| 1833 | |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 1834 | txn->flags &= ~(TX_CON_CLO_SET | TX_CON_KAL_SET); |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 1835 | 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] | 1836 | if (ctx.vlen >= 10 && word_match(ctx.line + ctx.val, ctx.vlen, "keep-alive", 10)) { |
| 1837 | if (wanted & TX_CON_KAL_SET) |
| 1838 | txn->flags |= TX_CON_KAL_SET; |
| 1839 | else |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 1840 | http_remove_header2(msg, &txn->hdr_idx, &ctx); |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 1841 | } |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 1842 | else if (ctx.vlen >= 5 && word_match(ctx.line + ctx.val, ctx.vlen, "close", 5)) { |
| 1843 | if (wanted & TX_CON_CLO_SET) |
| 1844 | txn->flags |= TX_CON_CLO_SET; |
| 1845 | else |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 1846 | http_remove_header2(msg, &txn->hdr_idx, &ctx); |
Willy Tarreau | 0dfdf19 | 2010-01-05 11:33:11 +0100 | [diff] [blame] | 1847 | } |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 1848 | } |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 1849 | |
| 1850 | if (wanted == (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET))) |
| 1851 | return; |
| 1852 | |
| 1853 | if ((wanted & TX_CON_CLO_SET) && !(txn->flags & TX_CON_CLO_SET)) { |
| 1854 | txn->flags |= TX_CON_CLO_SET; |
Willy Tarreau | 88d349d | 2010-01-25 12:15:43 +0100 | [diff] [blame] | 1855 | hdr_val = "Connection: close"; |
| 1856 | hdr_len = 17; |
| 1857 | if (unlikely(txn->flags & TX_USE_PX_CONN)) { |
| 1858 | hdr_val = "Proxy-Connection: close"; |
| 1859 | hdr_len = 23; |
| 1860 | } |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 1861 | http_header_add_tail2(msg, &txn->hdr_idx, hdr_val, hdr_len); |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 1862 | } |
| 1863 | |
| 1864 | if ((wanted & TX_CON_KAL_SET) && !(txn->flags & TX_CON_KAL_SET)) { |
| 1865 | txn->flags |= TX_CON_KAL_SET; |
Willy Tarreau | 88d349d | 2010-01-25 12:15:43 +0100 | [diff] [blame] | 1866 | hdr_val = "Connection: keep-alive"; |
| 1867 | hdr_len = 22; |
| 1868 | if (unlikely(txn->flags & TX_USE_PX_CONN)) { |
| 1869 | hdr_val = "Proxy-Connection: keep-alive"; |
| 1870 | hdr_len = 28; |
| 1871 | } |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 1872 | http_header_add_tail2(msg, &txn->hdr_idx, hdr_val, hdr_len); |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 1873 | } |
| 1874 | return; |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 1875 | } |
| 1876 | |
Willy Tarreau | a458b67 | 2012-03-05 11:17:50 +0100 | [diff] [blame] | 1877 | /* 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] | 1878 | * 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] | 1879 | * so that we know we can forward between ->sol and ->sov. |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 1880 | * 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] | 1881 | * 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] | 1882 | */ |
Willy Tarreau | 24e6d97 | 2012-10-26 00:49:52 +0200 | [diff] [blame] | 1883 | static inline int http_parse_chunk_size(struct http_msg *msg) |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 1884 | { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 1885 | const struct buffer *buf = msg->chn->buf; |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1886 | const char *ptr = b_ptr(buf, msg->next); |
Willy Tarreau | 4baf44b | 2012-03-09 14:10:20 +0100 | [diff] [blame] | 1887 | const char *ptr_old = ptr; |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1888 | const char *end = buf->data + buf->size; |
| 1889 | const char *stop = bi_end(buf); |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 1890 | unsigned int chunk = 0; |
| 1891 | |
| 1892 | /* The chunk size is in the following form, though we are only |
| 1893 | * interested in the size and CRLF : |
| 1894 | * 1*HEXDIGIT *WSP *[ ';' extensions ] CRLF |
| 1895 | */ |
| 1896 | while (1) { |
| 1897 | int c; |
Willy Tarreau | 363a5bb | 2012-03-02 20:14:45 +0100 | [diff] [blame] | 1898 | if (ptr == stop) |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 1899 | return 0; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 1900 | c = hex2i(*ptr); |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 1901 | if (c < 0) /* not a hex digit anymore */ |
| 1902 | break; |
Willy Tarreau | 0161d62 | 2013-04-02 01:26:55 +0200 | [diff] [blame] | 1903 | if (unlikely(++ptr >= end)) |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1904 | ptr = buf->data; |
Willy Tarreau | 431946e | 2012-02-24 19:20:12 +0100 | [diff] [blame] | 1905 | if (chunk & 0xF8000000) /* integer overflow will occur if result >= 2GB */ |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 1906 | goto error; |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 1907 | chunk = (chunk << 4) + c; |
| 1908 | } |
| 1909 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 1910 | /* empty size not allowed */ |
Willy Tarreau | 0161d62 | 2013-04-02 01:26:55 +0200 | [diff] [blame] | 1911 | if (unlikely(ptr == ptr_old)) |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 1912 | goto error; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 1913 | |
| 1914 | while (http_is_spht[(unsigned char)*ptr]) { |
| 1915 | if (++ptr >= end) |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1916 | ptr = buf->data; |
Willy Tarreau | 0161d62 | 2013-04-02 01:26:55 +0200 | [diff] [blame] | 1917 | if (unlikely(ptr == stop)) |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 1918 | return 0; |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 1919 | } |
| 1920 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 1921 | /* Up to there, we know that at least one byte is present at *ptr. Check |
| 1922 | * for the end of chunk size. |
| 1923 | */ |
| 1924 | while (1) { |
| 1925 | if (likely(HTTP_IS_CRLF(*ptr))) { |
| 1926 | /* we now have a CR or an LF at ptr */ |
| 1927 | if (likely(*ptr == '\r')) { |
| 1928 | if (++ptr >= end) |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1929 | ptr = buf->data; |
Willy Tarreau | 363a5bb | 2012-03-02 20:14:45 +0100 | [diff] [blame] | 1930 | if (ptr == stop) |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 1931 | return 0; |
| 1932 | } |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 1933 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 1934 | if (*ptr != '\n') |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 1935 | goto error; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 1936 | if (++ptr >= end) |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1937 | ptr = buf->data; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 1938 | /* done */ |
| 1939 | break; |
| 1940 | } |
| 1941 | else if (*ptr == ';') { |
| 1942 | /* chunk extension, ends at next CRLF */ |
| 1943 | if (++ptr >= end) |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1944 | ptr = buf->data; |
Willy Tarreau | 363a5bb | 2012-03-02 20:14:45 +0100 | [diff] [blame] | 1945 | if (ptr == stop) |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 1946 | return 0; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 1947 | |
| 1948 | while (!HTTP_IS_CRLF(*ptr)) { |
| 1949 | if (++ptr >= end) |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1950 | ptr = buf->data; |
Willy Tarreau | 363a5bb | 2012-03-02 20:14:45 +0100 | [diff] [blame] | 1951 | if (ptr == stop) |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 1952 | return 0; |
| 1953 | } |
| 1954 | /* we have a CRLF now, loop above */ |
| 1955 | continue; |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 1956 | } |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 1957 | else |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 1958 | goto error; |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 1959 | } |
| 1960 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 1961 | /* 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] | 1962 | * 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] | 1963 | * ->sov. |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 1964 | */ |
Willy Tarreau | 0161d62 | 2013-04-02 01:26:55 +0200 | [diff] [blame] | 1965 | if (unlikely(ptr < ptr_old)) |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1966 | msg->sov += buf->size; |
Willy Tarreau | a458b67 | 2012-03-05 11:17:50 +0100 | [diff] [blame] | 1967 | msg->sov += ptr - ptr_old; |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1968 | msg->next = buffer_count(buf, buf->p, ptr); |
Willy Tarreau | 124d991 | 2011-03-01 20:30:48 +0100 | [diff] [blame] | 1969 | msg->chunk_len = chunk; |
| 1970 | msg->body_len += chunk; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 1971 | msg->msg_state = chunk ? HTTP_MSG_DATA : HTTP_MSG_TRAILERS; |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 1972 | return 1; |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 1973 | error: |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1974 | msg->err_pos = buffer_count(buf, buf->p, ptr); |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 1975 | return -1; |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 1976 | } |
| 1977 | |
Willy Tarreau | 4baf44b | 2012-03-09 14:10:20 +0100 | [diff] [blame] | 1978 | /* This function skips trailers in the buffer associated with HTTP |
Willy Tarreau | a458b67 | 2012-03-05 11:17:50 +0100 | [diff] [blame] | 1979 | * 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] | 1980 | * the trailers is found, it is automatically scheduled to be forwarded, |
| 1981 | * msg->msg_state switches to HTTP_MSG_DONE, and the function returns >0. |
| 1982 | * If not enough data are available, the function does not change anything |
Willy Tarreau | a458b67 | 2012-03-05 11:17:50 +0100 | [diff] [blame] | 1983 | * 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] | 1984 | * 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] | 1985 | * 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] | 1986 | * must already be in HTTP_MSG_TRAILERS state before calling this function, |
| 1987 | * which implies that all non-trailers data have already been scheduled for |
Willy Tarreau | 2692736 | 2012-05-18 23:22:52 +0200 | [diff] [blame] | 1988 | * forwarding, and that the difference between msg->sol and msg->sov exactly |
Willy Tarreau | 638cd02 | 2010-01-03 07:42:04 +0100 | [diff] [blame] | 1989 | * matches the length of trailers already parsed and not forwarded. It is also |
| 1990 | * important to note that this function is designed to be able to parse wrapped |
| 1991 | * headers at end of buffer. |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 1992 | */ |
Willy Tarreau | 24e6d97 | 2012-10-26 00:49:52 +0200 | [diff] [blame] | 1993 | static int http_forward_trailers(struct http_msg *msg) |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 1994 | { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 1995 | const struct buffer *buf = msg->chn->buf; |
Willy Tarreau | 4baf44b | 2012-03-09 14:10:20 +0100 | [diff] [blame] | 1996 | |
Willy Tarreau | a458b67 | 2012-03-05 11:17:50 +0100 | [diff] [blame] | 1997 | /* we have msg->next which points to next line. Look for CRLF. */ |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 1998 | while (1) { |
Willy Tarreau | 4baf44b | 2012-03-09 14:10:20 +0100 | [diff] [blame] | 1999 | const char *p1 = NULL, *p2 = NULL; |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 2000 | const char *ptr = b_ptr(buf, msg->next); |
| 2001 | const char *stop = bi_end(buf); |
Willy Tarreau | 638cd02 | 2010-01-03 07:42:04 +0100 | [diff] [blame] | 2002 | int bytes; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2003 | |
| 2004 | /* scan current line and stop at LF or CRLF */ |
| 2005 | while (1) { |
Willy Tarreau | 363a5bb | 2012-03-02 20:14:45 +0100 | [diff] [blame] | 2006 | if (ptr == stop) |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2007 | return 0; |
| 2008 | |
| 2009 | if (*ptr == '\n') { |
| 2010 | if (!p1) |
| 2011 | p1 = ptr; |
| 2012 | p2 = ptr; |
| 2013 | break; |
| 2014 | } |
| 2015 | |
| 2016 | if (*ptr == '\r') { |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 2017 | if (p1) { |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 2018 | msg->err_pos = buffer_count(buf, buf->p, ptr); |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2019 | return -1; |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 2020 | } |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2021 | p1 = ptr; |
| 2022 | } |
| 2023 | |
| 2024 | ptr++; |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 2025 | if (ptr >= buf->data + buf->size) |
| 2026 | ptr = buf->data; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2027 | } |
| 2028 | |
| 2029 | /* after LF; point to beginning of next line */ |
| 2030 | p2++; |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 2031 | if (p2 >= buf->data + buf->size) |
| 2032 | p2 = buf->data; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2033 | |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 2034 | bytes = p2 - b_ptr(buf, msg->next); |
Willy Tarreau | 638cd02 | 2010-01-03 07:42:04 +0100 | [diff] [blame] | 2035 | if (bytes < 0) |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 2036 | bytes += buf->size; |
Willy Tarreau | 638cd02 | 2010-01-03 07:42:04 +0100 | [diff] [blame] | 2037 | |
| 2038 | /* schedule this line for forwarding */ |
| 2039 | msg->sov += bytes; |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 2040 | if (msg->sov >= buf->size) |
| 2041 | msg->sov -= buf->size; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2042 | |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 2043 | if (p1 == b_ptr(buf, msg->next)) { |
Willy Tarreau | 638cd02 | 2010-01-03 07:42:04 +0100 | [diff] [blame] | 2044 | /* LF/CRLF at beginning of line => end of trailers at p2. |
| 2045 | * Everything was scheduled for forwarding, there's nothing |
| 2046 | * left from this message. |
Willy Tarreau | 5523b32 | 2009-12-29 12:05:52 +0100 | [diff] [blame] | 2047 | */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 2048 | msg->next = buffer_count(buf, buf->p, p2); |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2049 | msg->msg_state = HTTP_MSG_DONE; |
| 2050 | return 1; |
| 2051 | } |
| 2052 | /* OK, next line then */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 2053 | msg->next = buffer_count(buf, buf->p, p2); |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2054 | } |
| 2055 | } |
| 2056 | |
Willy Tarreau | 54d23df | 2012-10-25 19:04:45 +0200 | [diff] [blame] | 2057 | /* 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] | 2058 | * 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] | 2059 | * ->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] | 2060 | * 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] | 2061 | * It also sets msg_state to HTTP_MSG_CHUNK_SIZE and returns >0 on success. If |
| 2062 | * not enough data are available, the function does not change anything and |
| 2063 | * returns zero. If a parse error is encountered, the function returns < 0 and |
| 2064 | * does not change anything. Note: this function is designed to parse wrapped |
| 2065 | * CRLF at the end of the buffer. |
| 2066 | */ |
Willy Tarreau | 24e6d97 | 2012-10-26 00:49:52 +0200 | [diff] [blame] | 2067 | static inline int http_skip_chunk_crlf(struct http_msg *msg) |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2068 | { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2069 | const struct buffer *buf = msg->chn->buf; |
Willy Tarreau | 4baf44b | 2012-03-09 14:10:20 +0100 | [diff] [blame] | 2070 | const char *ptr; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2071 | int bytes; |
| 2072 | |
| 2073 | /* NB: we'll check data availabilty at the end. It's not a |
| 2074 | * problem because whatever we match first will be checked |
| 2075 | * against the correct length. |
| 2076 | */ |
| 2077 | bytes = 1; |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 2078 | ptr = buf->p; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2079 | if (*ptr == '\r') { |
| 2080 | bytes++; |
| 2081 | ptr++; |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 2082 | if (ptr >= buf->data + buf->size) |
| 2083 | ptr = buf->data; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2084 | } |
| 2085 | |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 2086 | if (bytes > buf->i) |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2087 | return 0; |
| 2088 | |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 2089 | if (*ptr != '\n') { |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 2090 | msg->err_pos = buffer_count(buf, buf->p, ptr); |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2091 | return -1; |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 2092 | } |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2093 | |
| 2094 | ptr++; |
Willy Tarreau | 0161d62 | 2013-04-02 01:26:55 +0200 | [diff] [blame] | 2095 | if (unlikely(ptr >= buf->data + buf->size)) |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 2096 | ptr = buf->data; |
Willy Tarreau | 2692736 | 2012-05-18 23:22:52 +0200 | [diff] [blame] | 2097 | /* prepare the CRLF to be forwarded (between ->sol and ->sov) */ |
| 2098 | msg->sol = 0; |
Willy Tarreau | ea1175a | 2012-03-05 15:52:30 +0100 | [diff] [blame] | 2099 | msg->sov = msg->next = bytes; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2100 | msg->msg_state = HTTP_MSG_CHUNK_SIZE; |
| 2101 | return 1; |
| 2102 | } |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 2103 | |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 2104 | |
| 2105 | /* |
| 2106 | * Selects a compression algorithm depending on the client request. |
Willy Tarreau | 05d8460 | 2012-10-26 02:11:25 +0200 | [diff] [blame] | 2107 | */ |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 2108 | int select_compression_request_header(struct session *s, struct buffer *req) |
| 2109 | { |
| 2110 | struct http_txn *txn = &s->txn; |
Willy Tarreau | 70737d1 | 2012-10-27 00:34:28 +0200 | [diff] [blame] | 2111 | struct http_msg *msg = &txn->req; |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 2112 | struct hdr_ctx ctx; |
| 2113 | struct comp_algo *comp_algo = NULL; |
Willy Tarreau | 3c7b97b | 2012-10-26 14:50:26 +0200 | [diff] [blame] | 2114 | struct comp_algo *comp_algo_back = NULL; |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 2115 | |
Finn Arne Gangstad | cbb9a4b | 2012-10-29 21:43:01 +0100 | [diff] [blame] | 2116 | /* Disable compression for older user agents announcing themselves as "Mozilla/4" |
| 2117 | * 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] | 2118 | * See http://zoompf.com/2012/02/lose-the-wait-http-compression for more details. |
| 2119 | */ |
| 2120 | ctx.idx = 0; |
| 2121 | if (http_find_header2("User-Agent", 10, req->p, &txn->hdr_idx, &ctx) && |
| 2122 | ctx.vlen >= 9 && |
Finn Arne Gangstad | cbb9a4b | 2012-10-29 21:43:01 +0100 | [diff] [blame] | 2123 | memcmp(ctx.line + ctx.val, "Mozilla/4", 9) == 0 && |
| 2124 | (ctx.vlen < 31 || |
| 2125 | memcmp(ctx.line + ctx.val + 25, "MSIE ", 5) != 0 || |
| 2126 | ctx.line[ctx.val + 30] < '6' || |
| 2127 | (ctx.line[ctx.val + 30] == '6' && |
| 2128 | (ctx.vlen < 54 || memcmp(ctx.line + 51, "SV1", 3) != 0)))) { |
| 2129 | s->comp_algo = NULL; |
| 2130 | return 0; |
Willy Tarreau | 05d8460 | 2012-10-26 02:11:25 +0200 | [diff] [blame] | 2131 | } |
| 2132 | |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 2133 | /* search for the algo in the backend in priority or the frontend */ |
Willy Tarreau | 3c7b97b | 2012-10-26 14:50:26 +0200 | [diff] [blame] | 2134 | if ((s->be->comp && (comp_algo_back = s->be->comp->algos)) || (s->fe->comp && (comp_algo_back = s->fe->comp->algos))) { |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 2135 | ctx.idx = 0; |
| 2136 | while (http_find_header2("Accept-Encoding", 15, req->p, &txn->hdr_idx, &ctx)) { |
Willy Tarreau | 3c7b97b | 2012-10-26 14:50:26 +0200 | [diff] [blame] | 2137 | 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] | 2138 | if (word_match(ctx.line + ctx.val, ctx.vlen, comp_algo->name, comp_algo->name_len)) { |
| 2139 | s->comp_algo = comp_algo; |
Willy Tarreau | 70737d1 | 2012-10-27 00:34:28 +0200 | [diff] [blame] | 2140 | |
| 2141 | /* remove all occurrences of the header when "compression offload" is set */ |
| 2142 | |
| 2143 | if ((s->be->comp && s->be->comp->offload) || |
| 2144 | (s->fe->comp && s->fe->comp->offload)) { |
| 2145 | http_remove_header2(msg, &txn->hdr_idx, &ctx); |
| 2146 | ctx.idx = 0; |
| 2147 | while (http_find_header2("Accept-Encoding", 15, req->p, &txn->hdr_idx, &ctx)) { |
| 2148 | http_remove_header2(msg, &txn->hdr_idx, &ctx); |
| 2149 | } |
| 2150 | } |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 2151 | return 1; |
| 2152 | } |
| 2153 | } |
| 2154 | } |
| 2155 | } |
| 2156 | |
| 2157 | /* identity is implicit does not require headers */ |
Willy Tarreau | 3c7b97b | 2012-10-26 14:50:26 +0200 | [diff] [blame] | 2158 | if ((s->be->comp && (comp_algo_back = s->be->comp->algos)) || (s->fe->comp && (comp_algo_back = s->fe->comp->algos))) { |
| 2159 | 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] | 2160 | if (comp_algo->add_data == identity_add_data) { |
| 2161 | s->comp_algo = comp_algo; |
| 2162 | return 1; |
| 2163 | } |
| 2164 | } |
| 2165 | } |
| 2166 | |
| 2167 | s->comp_algo = NULL; |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 2168 | return 0; |
| 2169 | } |
| 2170 | |
| 2171 | /* |
| 2172 | * Selects a comression algorithm depending of the server response. |
| 2173 | */ |
| 2174 | int select_compression_response_header(struct session *s, struct buffer *res) |
| 2175 | { |
| 2176 | struct http_txn *txn = &s->txn; |
| 2177 | struct http_msg *msg = &txn->rsp; |
| 2178 | struct hdr_ctx ctx; |
| 2179 | struct comp_type *comp_type; |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 2180 | |
| 2181 | /* no common compression algorithm was found in request header */ |
| 2182 | if (s->comp_algo == NULL) |
| 2183 | goto fail; |
| 2184 | |
| 2185 | /* HTTP < 1.1 should not be compressed */ |
Willy Tarreau | 7257550 | 2013-12-24 14:41:35 +0100 | [diff] [blame] | 2186 | 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] | 2187 | goto fail; |
| 2188 | |
William Lallemand | d300261 | 2012-11-26 14:34:47 +0100 | [diff] [blame] | 2189 | /* 200 only */ |
| 2190 | if (txn->status != 200) |
| 2191 | goto fail; |
| 2192 | |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 2193 | /* Content-Length is null */ |
| 2194 | if (!(msg->flags & HTTP_MSGF_TE_CHNK) && msg->body_len == 0) |
| 2195 | goto fail; |
| 2196 | |
Willy Tarreau | 667c2a3 | 2013-04-09 08:13:58 +0200 | [diff] [blame] | 2197 | /* TEMPORARY WORKAROUND: do not compress if response is chunked !!!!!! */ |
| 2198 | if (msg->flags & HTTP_MSGF_TE_CHNK) |
| 2199 | goto fail; |
| 2200 | |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 2201 | /* content is already compressed */ |
Willy Tarreau | 0a80a8d | 2012-11-26 16:33:37 +0100 | [diff] [blame] | 2202 | ctx.idx = 0; |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 2203 | if (http_find_header2("Content-Encoding", 16, res->p, &txn->hdr_idx, &ctx)) |
| 2204 | goto fail; |
| 2205 | |
Willy Tarreau | 56e9ffa | 2013-01-05 16:20:35 +0100 | [diff] [blame] | 2206 | /* no compression when Cache-Control: no-transform is present in the message */ |
| 2207 | ctx.idx = 0; |
| 2208 | while (http_find_header2("Cache-Control", 13, res->p, &txn->hdr_idx, &ctx)) { |
| 2209 | if (word_match(ctx.line + ctx.val, ctx.vlen, "no-transform", 12)) |
| 2210 | goto fail; |
| 2211 | } |
| 2212 | |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 2213 | comp_type = NULL; |
| 2214 | |
Willy Tarreau | 0a80a8d | 2012-11-26 16:33:37 +0100 | [diff] [blame] | 2215 | /* we don't want to compress multipart content-types, nor content-types that are |
| 2216 | * not listed in the "compression type" directive if any. If no content-type was |
| 2217 | * found but configuration requires one, we don't compress either. Backend has |
| 2218 | * the priority. |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 2219 | */ |
Willy Tarreau | 0a80a8d | 2012-11-26 16:33:37 +0100 | [diff] [blame] | 2220 | ctx.idx = 0; |
| 2221 | if (http_find_header2("Content-Type", 12, res->p, &txn->hdr_idx, &ctx)) { |
| 2222 | if (ctx.vlen >= 9 && strncasecmp("multipart", ctx.line+ctx.val, 9) == 0) |
| 2223 | goto fail; |
| 2224 | |
| 2225 | if ((s->be->comp && (comp_type = s->be->comp->types)) || |
| 2226 | (s->fe->comp && (comp_type = s->fe->comp->types))) { |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 2227 | for (; comp_type; comp_type = comp_type->next) { |
Willy Tarreau | 0a80a8d | 2012-11-26 16:33:37 +0100 | [diff] [blame] | 2228 | if (ctx.vlen >= comp_type->name_len && |
| 2229 | 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] | 2230 | /* this Content-Type should be compressed */ |
| 2231 | break; |
| 2232 | } |
Willy Tarreau | 0a80a8d | 2012-11-26 16:33:37 +0100 | [diff] [blame] | 2233 | /* this Content-Type should not be compressed */ |
| 2234 | if (comp_type == NULL) |
| 2235 | goto fail; |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 2236 | } |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 2237 | } |
Willy Tarreau | 0a80a8d | 2012-11-26 16:33:37 +0100 | [diff] [blame] | 2238 | else { /* no content-type header */ |
| 2239 | if ((s->be->comp && s->be->comp->types) || (s->fe->comp && s->fe->comp->types)) |
| 2240 | goto fail; /* a content-type was required */ |
William Lallemand | d300261 | 2012-11-26 14:34:47 +0100 | [diff] [blame] | 2241 | } |
| 2242 | |
William Lallemand | d85f917 | 2012-11-09 17:05:39 +0100 | [diff] [blame] | 2243 | /* limit compression rate */ |
| 2244 | if (global.comp_rate_lim > 0) |
| 2245 | if (read_freq_ctr(&global.comp_bps_in) > global.comp_rate_lim) |
| 2246 | goto fail; |
| 2247 | |
William Lallemand | 072a2bf | 2012-11-20 17:01:01 +0100 | [diff] [blame] | 2248 | /* limit cpu usage */ |
| 2249 | if (idle_pct < compress_min_idle) |
| 2250 | goto fail; |
| 2251 | |
William Lallemand | 4c49fae | 2012-11-07 15:00:23 +0100 | [diff] [blame] | 2252 | /* initialize compression */ |
William Lallemand | f374783 | 2012-11-09 12:33:10 +0100 | [diff] [blame] | 2253 | 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] | 2254 | goto fail; |
| 2255 | |
William Lallemand | ec3e389 | 2012-11-12 17:02:18 +0100 | [diff] [blame] | 2256 | s->flags |= SN_COMP_READY; |
| 2257 | |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 2258 | /* remove Content-Length header */ |
Willy Tarreau | 0a80a8d | 2012-11-26 16:33:37 +0100 | [diff] [blame] | 2259 | ctx.idx = 0; |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 2260 | if ((msg->flags & HTTP_MSGF_CNT_LEN) && http_find_header2("Content-Length", 14, res->p, &txn->hdr_idx, &ctx)) |
| 2261 | http_remove_header2(msg, &txn->hdr_idx, &ctx); |
| 2262 | |
| 2263 | /* add Transfer-Encoding header */ |
| 2264 | if (!(msg->flags & HTTP_MSGF_TE_CHNK)) |
| 2265 | http_header_add_tail2(&txn->rsp, &txn->hdr_idx, "Transfer-Encoding: chunked", 26); |
| 2266 | |
| 2267 | /* |
| 2268 | * Add Content-Encoding header when it's not identity encoding. |
| 2269 | * RFC 2616 : Identity encoding: This content-coding is used only in the |
| 2270 | * Accept-Encoding header, and SHOULD NOT be used in the Content-Encoding |
| 2271 | * header. |
| 2272 | */ |
| 2273 | if (s->comp_algo->add_data != identity_add_data) { |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 2274 | trash.len = 18; |
| 2275 | memcpy(trash.str, "Content-Encoding: ", trash.len); |
| 2276 | memcpy(trash.str + trash.len, s->comp_algo->name, s->comp_algo->name_len); |
| 2277 | trash.len += s->comp_algo->name_len; |
| 2278 | trash.str[trash.len] = '\0'; |
| 2279 | 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] | 2280 | } |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 2281 | return 1; |
| 2282 | |
| 2283 | fail: |
Willy Tarreau | b97b619 | 2012-11-19 14:55:02 +0100 | [diff] [blame] | 2284 | s->comp_algo = NULL; |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 2285 | return 0; |
| 2286 | } |
| 2287 | |
| 2288 | |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 2289 | /* This stream analyser waits for a complete HTTP request. It returns 1 if the |
| 2290 | * processing can continue on next analysers, or zero if it either needs more |
| 2291 | * data or wants to immediately abort the request (eg: timeout, error, ...). It |
| 2292 | * is tied to AN_REQ_WAIT_HTTP and may may remove itself from s->req->analysers |
| 2293 | * when it has nothing left to do, and may remove any analyser when it wants to |
| 2294 | * abort. |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2295 | */ |
Willy Tarreau | 7421efb | 2012-07-02 15:11:27 +0200 | [diff] [blame] | 2296 | 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] | 2297 | { |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2298 | /* |
| 2299 | * We will parse the partial (or complete) lines. |
| 2300 | * We will check the request syntax, and also join multi-line |
| 2301 | * headers. An index of all the lines will be elaborated while |
| 2302 | * parsing. |
| 2303 | * |
| 2304 | * For the parsing, we use a 28 states FSM. |
| 2305 | * |
| 2306 | * Here is the information we currently have : |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2307 | * req->buf->p = beginning of request |
| 2308 | * req->buf->p + msg->eoh = end of processed headers / start of current one |
| 2309 | * req->buf->p + req->buf->i = end of input data |
Willy Tarreau | 2692736 | 2012-05-18 23:22:52 +0200 | [diff] [blame] | 2310 | * msg->eol = end of current header or line (LF or CRLF) |
| 2311 | * msg->next = first non-visited byte |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 2312 | * |
| 2313 | * At end of parsing, we may perform a capture of the error (if any), and |
| 2314 | * we will set a few fields (msg->sol, txn->meth, sn->flags/SN_REDIRECTABLE). |
| 2315 | * We also check for monitor-uri, logging, HTTP/0.9 to 1.0 conversion, and |
| 2316 | * finally headers capture. |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2317 | */ |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 2318 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2319 | int cur_idx; |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 2320 | int use_close_only; |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2321 | struct http_txn *txn = &s->txn; |
| 2322 | struct http_msg *msg = &txn->req; |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 2323 | struct hdr_ctx ctx; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 2324 | |
Willy Tarreau | 02d6cfc | 2012-03-01 18:19:58 +0100 | [diff] [blame] | 2325 | 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] | 2326 | now_ms, __FUNCTION__, |
| 2327 | s, |
| 2328 | req, |
| 2329 | req->rex, req->wex, |
| 2330 | req->flags, |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2331 | req->buf->i, |
Willy Tarreau | 6bf1736 | 2009-02-24 10:48:35 +0100 | [diff] [blame] | 2332 | req->analysers); |
| 2333 | |
Willy Tarreau | 52a0c60 | 2009-08-16 22:45:38 +0200 | [diff] [blame] | 2334 | /* we're speaking HTTP here, so let's speak HTTP to the client */ |
| 2335 | s->srv_error = http_return_srv_error; |
| 2336 | |
Willy Tarreau | 83e3af0 | 2009-12-28 17:39:57 +0100 | [diff] [blame] | 2337 | /* There's a protected area at the end of the buffer for rewriting |
| 2338 | * purposes. We don't want to start to parse the request if the |
| 2339 | * protected area is affected, because we may have to move processed |
| 2340 | * data later, which is much more complicated. |
| 2341 | */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2342 | if (buffer_not_empty(req->buf) && msg->msg_state < HTTP_MSG_ERROR) { |
Willy Tarreau | 379357a | 2013-06-08 12:55:46 +0200 | [diff] [blame] | 2343 | if (txn->flags & TX_NOT_FIRST) { |
| 2344 | if (unlikely(!channel_reserved(req))) { |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 2345 | 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] | 2346 | goto failed_keep_alive; |
Willy Tarreau | 2ab6eb1 | 2010-01-02 22:04:45 +0100 | [diff] [blame] | 2347 | /* 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] | 2348 | channel_dont_connect(req); |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 2349 | req->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */ |
Willy Tarreau | d7ad9f5 | 2013-12-31 17:26:25 +0100 | [diff] [blame] | 2350 | req->flags |= CF_WAKE_WRITE; |
Willy Tarreau | 2ab6eb1 | 2010-01-02 22:04:45 +0100 | [diff] [blame] | 2351 | return 0; |
| 2352 | } |
Willy Tarreau | 379357a | 2013-06-08 12:55:46 +0200 | [diff] [blame] | 2353 | if (unlikely(bi_end(req->buf) < b_ptr(req->buf, msg->next) || |
| 2354 | bi_end(req->buf) > req->buf->data + req->buf->size - global.tune.maxrewrite)) |
| 2355 | buffer_slow_realign(req->buf); |
Willy Tarreau | 83e3af0 | 2009-12-28 17:39:57 +0100 | [diff] [blame] | 2356 | } |
| 2357 | |
Willy Tarreau | 065e833 | 2010-01-08 00:30:20 +0100 | [diff] [blame] | 2358 | /* Note that we have the same problem with the response ; we |
| 2359 | * may want to send a redirect, error or anything which requires |
| 2360 | * some spare space. So we'll ensure that we have at least |
| 2361 | * maxrewrite bytes available in the response buffer before |
| 2362 | * processing that one. This will only affect pipelined |
| 2363 | * keep-alive requests. |
| 2364 | */ |
| 2365 | if ((txn->flags & TX_NOT_FIRST) && |
Willy Tarreau | 379357a | 2013-06-08 12:55:46 +0200 | [diff] [blame] | 2366 | unlikely(!channel_reserved(s->rep) || |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2367 | bi_end(s->rep->buf) < b_ptr(s->rep->buf, txn->rsp.next) || |
| 2368 | bi_end(s->rep->buf) > s->rep->buf->data + s->rep->buf->size - global.tune.maxrewrite)) { |
| 2369 | if (s->rep->buf->o) { |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 2370 | 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] | 2371 | goto failed_keep_alive; |
Willy Tarreau | 065e833 | 2010-01-08 00:30:20 +0100 | [diff] [blame] | 2372 | /* don't let a connection request be initiated */ |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 2373 | channel_dont_connect(req); |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 2374 | s->rep->flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */ |
Willy Tarreau | d7ad9f5 | 2013-12-31 17:26:25 +0100 | [diff] [blame] | 2375 | s->rep->flags |= CF_WAKE_WRITE; |
Willy Tarreau | 0499e35 | 2010-12-17 07:13:42 +0100 | [diff] [blame] | 2376 | s->rep->analysers |= an_bit; /* wake us up once it changes */ |
Willy Tarreau | 065e833 | 2010-01-08 00:30:20 +0100 | [diff] [blame] | 2377 | return 0; |
| 2378 | } |
| 2379 | } |
| 2380 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2381 | if (likely(msg->next < req->buf->i)) /* some unparsed data are available */ |
Willy Tarreau | a560c21 | 2012-03-09 13:50:57 +0100 | [diff] [blame] | 2382 | http_msg_analyzer(msg, &txn->hdr_idx); |
Willy Tarreau | 83e3af0 | 2009-12-28 17:39:57 +0100 | [diff] [blame] | 2383 | } |
| 2384 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2385 | /* 1: we might have to print this header in debug mode */ |
| 2386 | if (unlikely((global.mode & MODE_DEBUG) && |
| 2387 | (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) && |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 2388 | (msg->msg_state >= HTTP_MSG_BODY || msg->msg_state == HTTP_MSG_ERROR))) { |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2389 | char *eol, *sol; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2390 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2391 | sol = req->buf->p; |
Willy Tarreau | e92693a | 2012-09-24 21:13:39 +0200 | [diff] [blame] | 2392 | /* this is a bit complex : in case of error on the request line, |
| 2393 | * we know that rq.l is still zero, so we display only the part |
| 2394 | * up to the end of the line (truncated by debug_hdr). |
| 2395 | */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2396 | 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] | 2397 | debug_hdr("clireq", s, sol, eol); |
Willy Tarreau | 45e73e3 | 2006-12-17 00:05:15 +0100 | [diff] [blame] | 2398 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2399 | sol += hdr_idx_first_pos(&txn->hdr_idx); |
| 2400 | cur_idx = hdr_idx_first_idx(&txn->hdr_idx); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2401 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2402 | while (cur_idx) { |
| 2403 | eol = sol + txn->hdr_idx.v[cur_idx].len; |
| 2404 | debug_hdr("clihdr", s, sol, eol); |
| 2405 | sol = eol + txn->hdr_idx.v[cur_idx].cr + 1; |
| 2406 | cur_idx = txn->hdr_idx.v[cur_idx].next; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2407 | } |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2408 | } |
| 2409 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2410 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2411 | /* |
| 2412 | * Now we quickly check if we have found a full valid request. |
| 2413 | * If not so, we check the FD and buffer states before leaving. |
| 2414 | * A full request is indicated by the fact that we have seen |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 2415 | * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid |
Willy Tarreau | d3c343f | 2010-01-16 10:26:19 +0100 | [diff] [blame] | 2416 | * requests are checked first. When waiting for a second request |
| 2417 | * on a keep-alive session, if we encounter and error, close, t/o, |
| 2418 | * we note the error in the session flags but don't set any state. |
| 2419 | * Since the error will be noted there, it will not be counted by |
| 2420 | * process_session() as a frontend error. |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 2421 | * Last, we may increase some tracked counters' http request errors on |
| 2422 | * the cases that are deliberately the client's fault. For instance, |
| 2423 | * a timeout or connection reset is not counted as an error. However |
| 2424 | * a bad request is. |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2425 | */ |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2426 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 2427 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2428 | /* |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2429 | * First, let's catch bad requests. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2430 | */ |
Willy Tarreau | 3e1b6d1 | 2010-03-04 23:02:38 +0100 | [diff] [blame] | 2431 | if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) { |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 2432 | session_inc_http_req_ctr(s); |
| 2433 | session_inc_http_err_ctr(s); |
Willy Tarreau | 3e1b6d1 | 2010-03-04 23:02:38 +0100 | [diff] [blame] | 2434 | proxy_inc_fe_req_ctr(s->fe); |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2435 | goto return_bad_req; |
Willy Tarreau | 3e1b6d1 | 2010-03-04 23:02:38 +0100 | [diff] [blame] | 2436 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2437 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2438 | /* 1: Since we are in header mode, if there's no space |
| 2439 | * left for headers, we won't be able to free more |
| 2440 | * later, so the session will never terminate. We |
| 2441 | * must terminate it now. |
| 2442 | */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2443 | if (unlikely(buffer_full(req->buf, global.tune.maxrewrite))) { |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2444 | /* FIXME: check if URI is set and return Status |
| 2445 | * 414 Request URI too long instead. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2446 | */ |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 2447 | session_inc_http_req_ctr(s); |
| 2448 | session_inc_http_err_ctr(s); |
Willy Tarreau | 3e1b6d1 | 2010-03-04 23:02:38 +0100 | [diff] [blame] | 2449 | proxy_inc_fe_req_ctr(s->fe); |
Willy Tarreau | fec4d89 | 2011-09-02 20:04:57 +0200 | [diff] [blame] | 2450 | if (msg->err_pos < 0) |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2451 | msg->err_pos = req->buf->i; |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2452 | goto return_bad_req; |
| 2453 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2454 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2455 | /* 2: have we encountered a read error ? */ |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 2456 | else if (req->flags & CF_READ_ERROR) { |
Willy Tarreau | d3c343f | 2010-01-16 10:26:19 +0100 | [diff] [blame] | 2457 | if (!(s->flags & SN_ERR_MASK)) |
| 2458 | s->flags |= SN_ERR_CLICL; |
| 2459 | |
Willy Tarreau | fcffa69 | 2010-01-10 14:21:19 +0100 | [diff] [blame] | 2460 | if (txn->flags & TX_WAIT_NEXT_RQ) |
Willy Tarreau | b608feb | 2010-01-02 22:47:18 +0100 | [diff] [blame] | 2461 | goto failed_keep_alive; |
| 2462 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2463 | /* we cannot return any message on error */ |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 2464 | if (msg->err_pos >= 0) { |
Willy Tarreau | 8a0cef2 | 2012-03-09 13:39:23 +0100 | [diff] [blame] | 2465 | 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] | 2466 | session_inc_http_err_ctr(s); |
| 2467 | } |
| 2468 | |
Willy Tarreau | dc979f2 | 2012-12-04 10:39:01 +0100 | [diff] [blame] | 2469 | txn->status = 400; |
| 2470 | stream_int_retnclose(req->prod, NULL); |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2471 | msg->msg_state = HTTP_MSG_ERROR; |
| 2472 | req->analysers = 0; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 2473 | |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 2474 | session_inc_http_req_ctr(s); |
Willy Tarreau | 3e1b6d1 | 2010-03-04 23:02:38 +0100 | [diff] [blame] | 2475 | proxy_inc_fe_req_ctr(s->fe); |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 2476 | s->fe->fe_counters.failed_req++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 2477 | if (s->listener->counters) |
| 2478 | s->listener->counters->failed_req++; |
| 2479 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2480 | if (!(s->flags & SN_FINST_MASK)) |
| 2481 | s->flags |= SN_FINST_R; |
| 2482 | return 0; |
| 2483 | } |
Willy Tarreau | f9839bd | 2008-08-27 23:57:16 +0200 | [diff] [blame] | 2484 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2485 | /* 3: has the read timeout expired ? */ |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 2486 | 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] | 2487 | if (!(s->flags & SN_ERR_MASK)) |
| 2488 | s->flags |= SN_ERR_CLITO; |
| 2489 | |
Willy Tarreau | fcffa69 | 2010-01-10 14:21:19 +0100 | [diff] [blame] | 2490 | if (txn->flags & TX_WAIT_NEXT_RQ) |
Willy Tarreau | b608feb | 2010-01-02 22:47:18 +0100 | [diff] [blame] | 2491 | goto failed_keep_alive; |
| 2492 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2493 | /* read timeout : give up with an error message. */ |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 2494 | if (msg->err_pos >= 0) { |
Willy Tarreau | 8a0cef2 | 2012-03-09 13:39:23 +0100 | [diff] [blame] | 2495 | 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] | 2496 | session_inc_http_err_ctr(s); |
| 2497 | } |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2498 | txn->status = 408; |
Willy Tarreau | 783f258 | 2012-09-04 12:19:04 +0200 | [diff] [blame] | 2499 | stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_408)); |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2500 | msg->msg_state = HTTP_MSG_ERROR; |
| 2501 | req->analysers = 0; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 2502 | |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 2503 | session_inc_http_req_ctr(s); |
Willy Tarreau | 3e1b6d1 | 2010-03-04 23:02:38 +0100 | [diff] [blame] | 2504 | proxy_inc_fe_req_ctr(s->fe); |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 2505 | s->fe->fe_counters.failed_req++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 2506 | if (s->listener->counters) |
| 2507 | s->listener->counters->failed_req++; |
| 2508 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2509 | if (!(s->flags & SN_FINST_MASK)) |
| 2510 | s->flags |= SN_FINST_R; |
| 2511 | return 0; |
| 2512 | } |
Willy Tarreau | c65a3ba | 2008-08-11 23:42:50 +0200 | [diff] [blame] | 2513 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2514 | /* 4: have we encountered a close ? */ |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 2515 | else if (req->flags & CF_SHUTR) { |
Willy Tarreau | d3c343f | 2010-01-16 10:26:19 +0100 | [diff] [blame] | 2516 | if (!(s->flags & SN_ERR_MASK)) |
| 2517 | s->flags |= SN_ERR_CLICL; |
| 2518 | |
Willy Tarreau | fcffa69 | 2010-01-10 14:21:19 +0100 | [diff] [blame] | 2519 | if (txn->flags & TX_WAIT_NEXT_RQ) |
Willy Tarreau | b608feb | 2010-01-02 22:47:18 +0100 | [diff] [blame] | 2520 | goto failed_keep_alive; |
| 2521 | |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 2522 | if (msg->err_pos >= 0) |
Willy Tarreau | 8a0cef2 | 2012-03-09 13:39:23 +0100 | [diff] [blame] | 2523 | 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] | 2524 | txn->status = 400; |
Willy Tarreau | 783f258 | 2012-09-04 12:19:04 +0200 | [diff] [blame] | 2525 | stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_400)); |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2526 | msg->msg_state = HTTP_MSG_ERROR; |
| 2527 | req->analysers = 0; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 2528 | |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 2529 | session_inc_http_err_ctr(s); |
| 2530 | session_inc_http_req_ctr(s); |
Willy Tarreau | 3e1b6d1 | 2010-03-04 23:02:38 +0100 | [diff] [blame] | 2531 | proxy_inc_fe_req_ctr(s->fe); |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 2532 | s->fe->fe_counters.failed_req++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 2533 | if (s->listener->counters) |
| 2534 | s->listener->counters->failed_req++; |
| 2535 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2536 | if (!(s->flags & SN_FINST_MASK)) |
| 2537 | s->flags |= SN_FINST_R; |
Willy Tarreau | dafde43 | 2008-08-17 01:00:46 +0200 | [diff] [blame] | 2538 | return 0; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2539 | } |
| 2540 | |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 2541 | channel_dont_connect(req); |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 2542 | req->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */ |
| 2543 | s->rep->flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */ |
Willy Tarreau | 5e20552 | 2011-12-17 16:34:27 +0100 | [diff] [blame] | 2544 | #ifdef TCP_QUICKACK |
Willy Tarreau | 3c72872 | 2014-01-23 13:50:42 +0100 | [diff] [blame] | 2545 | 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] | 2546 | /* We need more data, we have to re-enable quick-ack in case we |
| 2547 | * previously disabled it, otherwise we might cause the client |
| 2548 | * to delay next data. |
| 2549 | */ |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 2550 | 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] | 2551 | } |
| 2552 | #endif |
Willy Tarreau | 1b194fe | 2009-03-21 21:10:04 +0100 | [diff] [blame] | 2553 | |
Willy Tarreau | fcffa69 | 2010-01-10 14:21:19 +0100 | [diff] [blame] | 2554 | if ((msg->msg_state != HTTP_MSG_RQBEFORE) && (txn->flags & TX_WAIT_NEXT_RQ)) { |
| 2555 | /* If the client starts to talk, let's fall back to |
| 2556 | * request timeout processing. |
| 2557 | */ |
| 2558 | txn->flags &= ~TX_WAIT_NEXT_RQ; |
Willy Tarreau | b16a574 | 2010-01-10 14:46:16 +0100 | [diff] [blame] | 2559 | req->analyse_exp = TICK_ETERNITY; |
Willy Tarreau | fcffa69 | 2010-01-10 14:21:19 +0100 | [diff] [blame] | 2560 | } |
| 2561 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2562 | /* just set the request timeout once at the beginning of the request */ |
Willy Tarreau | b16a574 | 2010-01-10 14:46:16 +0100 | [diff] [blame] | 2563 | if (!tick_isset(req->analyse_exp)) { |
| 2564 | if ((msg->msg_state == HTTP_MSG_RQBEFORE) && |
| 2565 | (txn->flags & TX_WAIT_NEXT_RQ) && |
| 2566 | tick_isset(s->be->timeout.httpka)) |
| 2567 | req->analyse_exp = tick_add(now_ms, s->be->timeout.httpka); |
| 2568 | else |
| 2569 | req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.httpreq); |
| 2570 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2571 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2572 | /* we're not ready yet */ |
| 2573 | return 0; |
Willy Tarreau | b608feb | 2010-01-02 22:47:18 +0100 | [diff] [blame] | 2574 | |
| 2575 | failed_keep_alive: |
| 2576 | /* Here we process low-level errors for keep-alive requests. In |
| 2577 | * short, if the request is not the first one and it experiences |
| 2578 | * a timeout, read error or shutdown, we just silently close so |
| 2579 | * that the client can try again. |
| 2580 | */ |
| 2581 | txn->status = 0; |
| 2582 | msg->msg_state = HTTP_MSG_RQBEFORE; |
| 2583 | req->analysers = 0; |
| 2584 | s->logs.logwait = 0; |
Willy Tarreau | abcd514 | 2013-06-11 17:18:02 +0200 | [diff] [blame] | 2585 | s->logs.level = 0; |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 2586 | s->rep->flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */ |
Willy Tarreau | 148d099 | 2010-01-10 10:21:21 +0100 | [diff] [blame] | 2587 | stream_int_retnclose(req->prod, NULL); |
Willy Tarreau | b608feb | 2010-01-02 22:47:18 +0100 | [diff] [blame] | 2588 | return 0; |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2589 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2590 | |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 2591 | /* OK now we have a complete HTTP request with indexed headers. Let's |
| 2592 | * complete the request parsing by setting a few fields we will need |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2593 | * 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] | 2594 | * 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] | 2595 | * 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] | 2596 | * byte after the last LF. msg->sov points to the first byte of data. |
| 2597 | * msg->eol cannot be trusted because it may have been left uninitialized |
| 2598 | * (for instance in the absence of headers). |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 2599 | */ |
Willy Tarreau | 9cdde23 | 2007-05-02 20:58:19 +0200 | [diff] [blame] | 2600 | |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 2601 | session_inc_http_req_ctr(s); |
Willy Tarreau | d9b587f | 2010-02-26 10:05:55 +0100 | [diff] [blame] | 2602 | proxy_inc_fe_req_ctr(s->fe); /* one more valid request for this FE */ |
| 2603 | |
Willy Tarreau | b16a574 | 2010-01-10 14:46:16 +0100 | [diff] [blame] | 2604 | if (txn->flags & TX_WAIT_NEXT_RQ) { |
| 2605 | /* kill the pending keep-alive timeout */ |
| 2606 | txn->flags &= ~TX_WAIT_NEXT_RQ; |
| 2607 | req->analyse_exp = TICK_ETERNITY; |
| 2608 | } |
| 2609 | |
| 2610 | |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 2611 | /* Maybe we found in invalid header name while we were configured not |
| 2612 | * to block on that, so we have to capture it now. |
| 2613 | */ |
| 2614 | if (unlikely(msg->err_pos >= 0)) |
Willy Tarreau | 8a0cef2 | 2012-03-09 13:39:23 +0100 | [diff] [blame] | 2615 | 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] | 2616 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2617 | /* |
| 2618 | * 1: identify the method |
| 2619 | */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2620 | 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] | 2621 | |
| 2622 | /* we can make use of server redirect on GET and HEAD */ |
| 2623 | if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD) |
| 2624 | s->flags |= SN_REDIRECTABLE; |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 2625 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2626 | /* |
| 2627 | * 2: check if the URI matches the monitor_uri. |
| 2628 | * We have to do this for every request which gets in, because |
| 2629 | * the monitor-uri is defined by the frontend. |
| 2630 | */ |
| 2631 | if (unlikely((s->fe->monitor_uri_len != 0) && |
| 2632 | (s->fe->monitor_uri_len == msg->sl.rq.u_l) && |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2633 | !memcmp(req->buf->p + msg->sl.rq.u, |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2634 | s->fe->monitor_uri, |
| 2635 | s->fe->monitor_uri_len))) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2636 | /* |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2637 | * We have found the monitor URI |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2638 | */ |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2639 | struct acl_cond *cond; |
Willy Tarreau | b80c230 | 2007-11-30 20:51:32 +0100 | [diff] [blame] | 2640 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2641 | s->flags |= SN_MONITOR; |
Willy Tarreau | eabea07 | 2011-09-10 23:29:44 +0200 | [diff] [blame] | 2642 | s->fe->fe_counters.intercepted_req++; |
Willy Tarreau | b80c230 | 2007-11-30 20:51:32 +0100 | [diff] [blame] | 2643 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2644 | /* Check if we want to fail this monitor request or not */ |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 2645 | list_for_each_entry(cond, &s->fe->mon_fail_cond, list) { |
Willy Tarreau | 32a6f2e | 2012-04-25 10:13:36 +0200 | [diff] [blame] | 2646 | 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] | 2647 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2648 | ret = acl_pass(ret); |
| 2649 | if (cond->pol == ACL_COND_UNLESS) |
| 2650 | ret = !ret; |
Willy Tarreau | b80c230 | 2007-11-30 20:51:32 +0100 | [diff] [blame] | 2651 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2652 | if (ret) { |
| 2653 | /* we fail this request, let's return 503 service unavail */ |
| 2654 | txn->status = 503; |
Willy Tarreau | 783f258 | 2012-09-04 12:19:04 +0200 | [diff] [blame] | 2655 | stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_503)); |
Willy Tarreau | 570f221 | 2013-06-10 16:42:09 +0200 | [diff] [blame] | 2656 | if (!(s->flags & SN_ERR_MASK)) |
| 2657 | 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] | 2658 | goto return_prx_cond; |
Willy Tarreau | b80c230 | 2007-11-30 20:51:32 +0100 | [diff] [blame] | 2659 | } |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2660 | } |
Willy Tarreau | a5555ec | 2008-11-30 19:02:32 +0100 | [diff] [blame] | 2661 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2662 | /* nothing to fail, let's reply normaly */ |
| 2663 | txn->status = 200; |
Willy Tarreau | 783f258 | 2012-09-04 12:19:04 +0200 | [diff] [blame] | 2664 | stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_200)); |
Willy Tarreau | 570f221 | 2013-06-10 16:42:09 +0200 | [diff] [blame] | 2665 | if (!(s->flags & SN_ERR_MASK)) |
| 2666 | 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] | 2667 | goto return_prx_cond; |
| 2668 | } |
| 2669 | |
| 2670 | /* |
| 2671 | * 3: Maybe we have to copy the original REQURI for the logs ? |
| 2672 | * Note: we cannot log anymore if the request has been |
| 2673 | * classified as invalid. |
| 2674 | */ |
| 2675 | if (unlikely(s->logs.logwait & LW_REQ)) { |
| 2676 | /* we have a complete HTTP request that we must log */ |
| 2677 | if ((txn->uri = pool_alloc2(pool2_requri)) != NULL) { |
| 2678 | int urilen = msg->sl.rq.l; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2679 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2680 | if (urilen >= REQURI_LEN) |
| 2681 | urilen = REQURI_LEN - 1; |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2682 | memcpy(txn->uri, req->buf->p, urilen); |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2683 | txn->uri[urilen] = 0; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2684 | |
Willy Tarreau | d79a3b2 | 2012-12-28 09:40:16 +0100 | [diff] [blame] | 2685 | if (!(s->logs.logwait &= ~(LW_REQ|LW_INIT))) |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2686 | s->do_log(s); |
| 2687 | } else { |
| 2688 | Alert("HTTP logging : out of memory.\n"); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2689 | } |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2690 | } |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 2691 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2692 | /* 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] | 2693 | 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] | 2694 | goto return_bad_req; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2695 | |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 2696 | /* ... and check if the request is HTTP/1.1 or above */ |
| 2697 | if ((msg->sl.rq.v_l == 8) && |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2698 | ((req->buf->p[msg->sl.rq.v + 5] > '1') || |
| 2699 | ((req->buf->p[msg->sl.rq.v + 5] == '1') && |
| 2700 | (req->buf->p[msg->sl.rq.v + 7] >= '1')))) |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 2701 | msg->flags |= HTTP_MSGF_VER_11; |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 2702 | |
| 2703 | /* "connection" has not been parsed yet */ |
Willy Tarreau | 50fc777 | 2012-11-11 22:19:57 +0100 | [diff] [blame] | 2704 | 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] | 2705 | |
Willy Tarreau | 88d349d | 2010-01-25 12:15:43 +0100 | [diff] [blame] | 2706 | /* if the frontend has "option http-use-proxy-header", we'll check if |
| 2707 | * we have what looks like a proxied connection instead of a connection, |
| 2708 | * and in this case set the TX_USE_PX_CONN flag to use Proxy-connection. |
| 2709 | * Note that this is *not* RFC-compliant, however browsers and proxies |
| 2710 | * happen to do that despite being non-standard :-( |
| 2711 | * We consider that a request not beginning with either '/' or '*' is |
| 2712 | * a proxied connection, which covers both "scheme://location" and |
| 2713 | * CONNECT ip:port. |
| 2714 | */ |
| 2715 | if ((s->fe->options2 & PR_O2_USE_PXHDR) && |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2716 | 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] | 2717 | txn->flags |= TX_USE_PX_CONN; |
| 2718 | |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 2719 | /* transfer length unknown*/ |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 2720 | msg->flags &= ~HTTP_MSGF_XFER_LEN; |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 2721 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2722 | /* 5: we may need to capture headers */ |
Willy Tarreau | 42f7d89 | 2012-03-24 08:28:09 +0100 | [diff] [blame] | 2723 | if (unlikely((s->logs.logwait & LW_REQHDR) && txn->req.cap)) |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2724 | capture_headers(req->buf->p, &txn->hdr_idx, |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2725 | txn->req.cap, s->fe->req_cap); |
Willy Tarreau | 1138281 | 2008-07-09 16:18:21 +0200 | [diff] [blame] | 2726 | |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 2727 | /* 6: determine the transfer-length. |
| 2728 | * According to RFC2616 #4.4, amended by the HTTPbis working group, |
| 2729 | * the presence of a message-body in a REQUEST and its transfer length |
| 2730 | * must be determined that way (in order of precedence) : |
| 2731 | * 1. The presence of a message-body in a request is signaled by the |
| 2732 | * inclusion of a Content-Length or Transfer-Encoding header field |
| 2733 | * in the request's header fields. When a request message contains |
| 2734 | * both a message-body of non-zero length and a method that does |
| 2735 | * not define any semantics for that request message-body, then an |
| 2736 | * origin server SHOULD either ignore the message-body or respond |
| 2737 | * with an appropriate error message (e.g., 413). A proxy or |
| 2738 | * gateway, when presented the same request, SHOULD either forward |
| 2739 | * the request inbound with the message- body or ignore the |
| 2740 | * message-body when determining a response. |
| 2741 | * |
| 2742 | * 2. If a Transfer-Encoding header field (Section 9.7) is present |
| 2743 | * and the "chunked" transfer-coding (Section 6.2) is used, the |
| 2744 | * transfer-length is defined by the use of this transfer-coding. |
| 2745 | * If a Transfer-Encoding header field is present and the "chunked" |
| 2746 | * transfer-coding is not present, the transfer-length is defined |
| 2747 | * by the sender closing the connection. |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 2748 | * |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 2749 | * 3. If a Content-Length header field is present, its decimal value in |
| 2750 | * OCTETs represents both the entity-length and the transfer-length. |
| 2751 | * If a message is received with both a Transfer-Encoding header |
| 2752 | * field and a Content-Length header field, the latter MUST be ignored. |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 2753 | * |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 2754 | * 4. By the server closing the connection. (Closing the connection |
| 2755 | * cannot be used to indicate the end of a request body, since that |
| 2756 | * would leave no possibility for the server to send back a response.) |
| 2757 | * |
| 2758 | * Whenever a transfer-coding is applied to a message-body, the set of |
| 2759 | * transfer-codings MUST include "chunked", unless the message indicates |
| 2760 | * it is terminated by closing the connection. When the "chunked" |
| 2761 | * transfer-coding is used, it MUST be the last transfer-coding applied |
| 2762 | * to the message-body. |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 2763 | */ |
| 2764 | |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 2765 | use_close_only = 0; |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 2766 | ctx.idx = 0; |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 2767 | /* set TE_CHNK and XFER_LEN only if "chunked" is seen last */ |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 2768 | while ((msg->flags & HTTP_MSGF_VER_11) && |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2769 | 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] | 2770 | if (ctx.vlen == 7 && strncasecmp(ctx.line + ctx.val, "chunked", 7) == 0) |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 2771 | msg->flags |= (HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN); |
| 2772 | else if (msg->flags & HTTP_MSGF_TE_CHNK) { |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 2773 | /* bad transfer-encoding (chunked followed by something else) */ |
| 2774 | use_close_only = 1; |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 2775 | msg->flags &= ~(HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN); |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 2776 | break; |
| 2777 | } |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 2778 | } |
| 2779 | |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 2780 | ctx.idx = 0; |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 2781 | while (!(msg->flags & HTTP_MSGF_TE_CHNK) && !use_close_only && |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2782 | 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] | 2783 | signed long long cl; |
| 2784 | |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 2785 | if (!ctx.vlen) { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2786 | msg->err_pos = ctx.line + ctx.val - req->buf->p; |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 2787 | goto return_bad_req; |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 2788 | } |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 2789 | |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 2790 | if (strl2llrc(ctx.line + ctx.val, ctx.vlen, &cl)) { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2791 | msg->err_pos = ctx.line + ctx.val - req->buf->p; |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 2792 | goto return_bad_req; /* parse failure */ |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 2793 | } |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 2794 | |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 2795 | if (cl < 0) { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2796 | msg->err_pos = ctx.line + ctx.val - req->buf->p; |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 2797 | goto return_bad_req; |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 2798 | } |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 2799 | |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 2800 | if ((msg->flags & HTTP_MSGF_CNT_LEN) && (msg->chunk_len != cl)) { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2801 | msg->err_pos = ctx.line + ctx.val - req->buf->p; |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 2802 | goto return_bad_req; /* already specified, was different */ |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 2803 | } |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 2804 | |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 2805 | msg->flags |= HTTP_MSGF_CNT_LEN | HTTP_MSGF_XFER_LEN; |
Willy Tarreau | 124d991 | 2011-03-01 20:30:48 +0100 | [diff] [blame] | 2806 | msg->body_len = msg->chunk_len = cl; |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 2807 | } |
| 2808 | |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 2809 | /* bodyless requests have a known length */ |
| 2810 | if (!use_close_only) |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 2811 | msg->flags |= HTTP_MSGF_XFER_LEN; |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 2812 | |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 2813 | /* end of job, return OK */ |
Willy Tarreau | 3a81629 | 2009-07-07 10:55:49 +0200 | [diff] [blame] | 2814 | req->analysers &= ~an_bit; |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 2815 | req->analyse_exp = TICK_ETERNITY; |
| 2816 | return 1; |
| 2817 | |
| 2818 | return_bad_req: |
| 2819 | /* We centralize bad requests processing here */ |
| 2820 | if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) { |
| 2821 | /* we detected a parsing error. We want to archive this request |
| 2822 | * in the dedicated proxy area for later troubleshooting. |
| 2823 | */ |
Willy Tarreau | 8a0cef2 | 2012-03-09 13:39:23 +0100 | [diff] [blame] | 2824 | 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] | 2825 | } |
| 2826 | |
| 2827 | txn->req.msg_state = HTTP_MSG_ERROR; |
| 2828 | txn->status = 400; |
Willy Tarreau | 783f258 | 2012-09-04 12:19:04 +0200 | [diff] [blame] | 2829 | 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] | 2830 | |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 2831 | s->fe->fe_counters.failed_req++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 2832 | if (s->listener->counters) |
| 2833 | s->listener->counters->failed_req++; |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 2834 | |
| 2835 | return_prx_cond: |
| 2836 | if (!(s->flags & SN_ERR_MASK)) |
| 2837 | s->flags |= SN_ERR_PRXCOND; |
| 2838 | if (!(s->flags & SN_FINST_MASK)) |
| 2839 | s->flags |= SN_FINST_R; |
| 2840 | |
| 2841 | req->analysers = 0; |
| 2842 | req->analyse_exp = TICK_ETERNITY; |
| 2843 | return 0; |
| 2844 | } |
| 2845 | |
Willy Tarreau | 4f8a83c | 2012-06-04 00:26:23 +0200 | [diff] [blame] | 2846 | |
Willy Tarreau | 347a35d | 2013-11-22 17:51:09 +0100 | [diff] [blame] | 2847 | /* This function prepares an applet to handle the stats. It can deal with the |
| 2848 | * "100-continue" expectation, check that admin rules are met for POST requests, |
| 2849 | * and program a response message if something was unexpected. It cannot fail |
| 2850 | * 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] | 2851 | * 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] | 2852 | * s->target which is supposed to already point to the stats applet. The caller |
| 2853 | * is expected to have already assigned an appctx to the session. |
Willy Tarreau | 1facd6d | 2012-12-22 22:03:39 +0100 | [diff] [blame] | 2854 | */ |
| 2855 | int http_handle_stats(struct session *s, struct channel *req) |
| 2856 | { |
| 2857 | struct stats_admin_rule *stats_admin_rule; |
| 2858 | struct stream_interface *si = s->rep->prod; |
| 2859 | struct http_txn *txn = &s->txn; |
| 2860 | struct http_msg *msg = &txn->req; |
Willy Tarreau | 414e9bb | 2013-11-23 00:30:38 +0100 | [diff] [blame] | 2861 | struct uri_auth *uri_auth = s->be->uri_auth; |
| 2862 | const char *uri, *h, *lookup; |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 2863 | struct appctx *appctx; |
Willy Tarreau | 414e9bb | 2013-11-23 00:30:38 +0100 | [diff] [blame] | 2864 | |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 2865 | appctx = si_appctx(si); |
| 2866 | memset(&appctx->ctx.stats, 0, sizeof(appctx->ctx.stats)); |
| 2867 | appctx->st1 = appctx->st2 = 0; |
| 2868 | appctx->ctx.stats.st_code = STAT_STATUS_INIT; |
| 2869 | appctx->ctx.stats.flags |= STAT_FMT_HTML; /* assume HTML mode by default */ |
Willy Tarreau | 414e9bb | 2013-11-23 00:30:38 +0100 | [diff] [blame] | 2870 | |
| 2871 | uri = msg->chn->buf->p + msg->sl.rq.u; |
| 2872 | lookup = uri + uri_auth->uri_len; |
| 2873 | |
| 2874 | for (h = lookup; h <= uri + msg->sl.rq.u_l - 3; h++) { |
| 2875 | if (memcmp(h, ";up", 3) == 0) { |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 2876 | appctx->ctx.stats.flags |= STAT_HIDE_DOWN; |
Willy Tarreau | 414e9bb | 2013-11-23 00:30:38 +0100 | [diff] [blame] | 2877 | break; |
| 2878 | } |
| 2879 | } |
| 2880 | |
| 2881 | if (uri_auth->refresh) { |
| 2882 | for (h = lookup; h <= uri + msg->sl.rq.u_l - 10; h++) { |
| 2883 | if (memcmp(h, ";norefresh", 10) == 0) { |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 2884 | appctx->ctx.stats.flags |= STAT_NO_REFRESH; |
Willy Tarreau | 414e9bb | 2013-11-23 00:30:38 +0100 | [diff] [blame] | 2885 | break; |
| 2886 | } |
| 2887 | } |
| 2888 | } |
| 2889 | |
| 2890 | for (h = lookup; h <= uri + msg->sl.rq.u_l - 4; h++) { |
| 2891 | if (memcmp(h, ";csv", 4) == 0) { |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 2892 | appctx->ctx.stats.flags &= ~STAT_FMT_HTML; |
Willy Tarreau | 414e9bb | 2013-11-23 00:30:38 +0100 | [diff] [blame] | 2893 | break; |
| 2894 | } |
| 2895 | } |
| 2896 | |
| 2897 | for (h = lookup; h <= uri + msg->sl.rq.u_l - 8; h++) { |
| 2898 | if (memcmp(h, ";st=", 4) == 0) { |
| 2899 | int i; |
| 2900 | h += 4; |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 2901 | appctx->ctx.stats.st_code = STAT_STATUS_UNKN; |
Willy Tarreau | 414e9bb | 2013-11-23 00:30:38 +0100 | [diff] [blame] | 2902 | for (i = STAT_STATUS_INIT + 1; i < STAT_STATUS_SIZE; i++) { |
| 2903 | if (strncmp(stat_status_codes[i], h, 4) == 0) { |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 2904 | appctx->ctx.stats.st_code = i; |
Willy Tarreau | 414e9bb | 2013-11-23 00:30:38 +0100 | [diff] [blame] | 2905 | break; |
| 2906 | } |
| 2907 | } |
| 2908 | break; |
| 2909 | } |
| 2910 | } |
| 2911 | |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 2912 | appctx->ctx.stats.scope_str = 0; |
| 2913 | appctx->ctx.stats.scope_len = 0; |
Willy Tarreau | 414e9bb | 2013-11-23 00:30:38 +0100 | [diff] [blame] | 2914 | for (h = lookup; h <= uri + msg->sl.rq.u_l - 8; h++) { |
| 2915 | if (memcmp(h, STAT_SCOPE_INPUT_NAME "=", strlen(STAT_SCOPE_INPUT_NAME) + 1) == 0) { |
| 2916 | int itx = 0; |
| 2917 | const char *h2; |
| 2918 | char scope_txt[STAT_SCOPE_TXT_MAXLEN + 1]; |
| 2919 | const char *err; |
| 2920 | |
| 2921 | h += strlen(STAT_SCOPE_INPUT_NAME) + 1; |
| 2922 | h2 = h; |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 2923 | appctx->ctx.stats.scope_str = h2 - msg->chn->buf->p; |
Willy Tarreau | 414e9bb | 2013-11-23 00:30:38 +0100 | [diff] [blame] | 2924 | while (*h != ';' && *h != '\0' && *h != '&' && *h != ' ' && *h != '\n') { |
| 2925 | itx++; |
| 2926 | h++; |
| 2927 | } |
| 2928 | |
| 2929 | if (itx > STAT_SCOPE_TXT_MAXLEN) |
| 2930 | itx = STAT_SCOPE_TXT_MAXLEN; |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 2931 | appctx->ctx.stats.scope_len = itx; |
Willy Tarreau | 414e9bb | 2013-11-23 00:30:38 +0100 | [diff] [blame] | 2932 | |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 2933 | /* 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] | 2934 | memcpy(scope_txt, h2, itx); |
| 2935 | scope_txt[itx] = '\0'; |
| 2936 | err = invalid_char(scope_txt); |
| 2937 | if (err) { |
| 2938 | /* bad char in search text => clear scope */ |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 2939 | appctx->ctx.stats.scope_str = 0; |
| 2940 | appctx->ctx.stats.scope_len = 0; |
Willy Tarreau | 414e9bb | 2013-11-23 00:30:38 +0100 | [diff] [blame] | 2941 | } |
| 2942 | break; |
| 2943 | } |
| 2944 | } |
Willy Tarreau | 1facd6d | 2012-12-22 22:03:39 +0100 | [diff] [blame] | 2945 | |
| 2946 | /* now check whether we have some admin rules for this request */ |
Willy Tarreau | 414e9bb | 2013-11-23 00:30:38 +0100 | [diff] [blame] | 2947 | list_for_each_entry(stats_admin_rule, &uri_auth->admin_rules, list) { |
Willy Tarreau | 1facd6d | 2012-12-22 22:03:39 +0100 | [diff] [blame] | 2948 | int ret = 1; |
| 2949 | |
| 2950 | if (stats_admin_rule->cond) { |
| 2951 | ret = acl_exec_cond(stats_admin_rule->cond, s->be, s, &s->txn, SMP_OPT_DIR_REQ|SMP_OPT_FINAL); |
| 2952 | ret = acl_pass(ret); |
| 2953 | if (stats_admin_rule->cond->pol == ACL_COND_UNLESS) |
| 2954 | ret = !ret; |
| 2955 | } |
| 2956 | |
| 2957 | if (ret) { |
| 2958 | /* no rule, or the rule matches */ |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 2959 | appctx->ctx.stats.flags |= STAT_ADMIN; |
Willy Tarreau | 1facd6d | 2012-12-22 22:03:39 +0100 | [diff] [blame] | 2960 | break; |
| 2961 | } |
| 2962 | } |
| 2963 | |
| 2964 | /* Was the status page requested with a POST ? */ |
Willy Tarreau | 347a35d | 2013-11-22 17:51:09 +0100 | [diff] [blame] | 2965 | if (unlikely(txn->meth == HTTP_METH_POST && txn->req.body_len > 0)) { |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 2966 | if (appctx->ctx.stats.flags & STAT_ADMIN) { |
Willy Tarreau | 1facd6d | 2012-12-22 22:03:39 +0100 | [diff] [blame] | 2967 | if (msg->msg_state < HTTP_MSG_100_SENT) { |
| 2968 | /* If we have HTTP/1.1 and Expect: 100-continue, then we must |
| 2969 | * send an HTTP/1.1 100 Continue intermediate response. |
| 2970 | */ |
| 2971 | if (msg->flags & HTTP_MSGF_VER_11) { |
| 2972 | struct hdr_ctx ctx; |
| 2973 | ctx.idx = 0; |
| 2974 | /* Expect is allowed in 1.1, look for it */ |
| 2975 | if (http_find_header2("Expect", 6, req->buf->p, &txn->hdr_idx, &ctx) && |
| 2976 | unlikely(ctx.vlen == 12 && strncasecmp(ctx.line+ctx.val, "100-continue", 12) == 0)) { |
| 2977 | bo_inject(s->rep, http_100_chunk.str, http_100_chunk.len); |
| 2978 | } |
| 2979 | } |
| 2980 | msg->msg_state = HTTP_MSG_100_SENT; |
| 2981 | s->logs.tv_request = now; /* update the request timer to reflect full request */ |
| 2982 | } |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 2983 | appctx->st0 = STAT_HTTP_POST; |
Willy Tarreau | 1facd6d | 2012-12-22 22:03:39 +0100 | [diff] [blame] | 2984 | } |
Willy Tarreau | 347a35d | 2013-11-22 17:51:09 +0100 | [diff] [blame] | 2985 | else { |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 2986 | appctx->ctx.stats.st_code = STAT_STATUS_DENY; |
| 2987 | appctx->st0 = STAT_HTTP_LAST; |
de Lafond Guillaume | 88c278f | 2013-04-15 19:27:10 +0200 | [diff] [blame] | 2988 | } |
Willy Tarreau | 1facd6d | 2012-12-22 22:03:39 +0100 | [diff] [blame] | 2989 | } |
Willy Tarreau | 347a35d | 2013-11-22 17:51:09 +0100 | [diff] [blame] | 2990 | else { |
| 2991 | /* So it was another method (GET/HEAD) */ |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 2992 | appctx->st0 = STAT_HTTP_HEAD; |
Willy Tarreau | 1facd6d | 2012-12-22 22:03:39 +0100 | [diff] [blame] | 2993 | } |
| 2994 | |
Willy Tarreau | 1facd6d | 2012-12-22 22:03:39 +0100 | [diff] [blame] | 2995 | s->task->nice = -32; /* small boost for HTTP statistics */ |
Willy Tarreau | 1facd6d | 2012-12-22 22:03:39 +0100 | [diff] [blame] | 2996 | return 1; |
| 2997 | } |
| 2998 | |
Lukas Tribus | 67db8df | 2013-06-23 17:37:13 +0200 | [diff] [blame] | 2999 | /* Sets the TOS header in IPv4 and the traffic class header in IPv6 packets |
| 3000 | * (as per RFC3260 #4 and BCP37 #4.2 and #5.2). |
| 3001 | */ |
| 3002 | static inline void inet_set_tos(int fd, struct sockaddr_storage from, int tos) |
| 3003 | { |
| 3004 | #ifdef IP_TOS |
| 3005 | if (from.ss_family == AF_INET) |
| 3006 | setsockopt(fd, IPPROTO_IP, IP_TOS, &tos, sizeof(tos)); |
| 3007 | #endif |
| 3008 | #ifdef IPV6_TCLASS |
| 3009 | if (from.ss_family == AF_INET6) { |
| 3010 | if (IN6_IS_ADDR_V4MAPPED(&((struct sockaddr_in6 *)&from)->sin6_addr)) |
| 3011 | /* v4-mapped addresses need IP_TOS */ |
| 3012 | setsockopt(fd, IPPROTO_IP, IP_TOS, &tos, sizeof(tos)); |
| 3013 | else |
| 3014 | setsockopt(fd, IPPROTO_IPV6, IPV6_TCLASS, &tos, sizeof(tos)); |
| 3015 | } |
| 3016 | #endif |
| 3017 | } |
| 3018 | |
Willy Tarreau | 20b0de5 | 2012-12-24 15:45:22 +0100 | [diff] [blame] | 3019 | /* Executes the http-request rules <rules> for session <s>, proxy <px> and |
Willy Tarreau | 96257ec | 2012-12-27 10:46:37 +0100 | [diff] [blame] | 3020 | * transaction <txn>. Returns the first rule that prevents further processing |
| 3021 | * of the request (auth, deny, ...) or NULL if it executed all rules or stopped |
| 3022 | * on an allow. It may set the TX_CLDENY on txn->flags if it encounters a deny |
| 3023 | * rule. |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 3024 | */ |
Willy Tarreau | 20b0de5 | 2012-12-24 15:45:22 +0100 | [diff] [blame] | 3025 | static struct http_req_rule * |
Willy Tarreau | 96257ec | 2012-12-27 10:46:37 +0100 | [diff] [blame] | 3026 | 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] | 3027 | { |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 3028 | struct connection *cli_conn; |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 3029 | struct http_req_rule *rule; |
Willy Tarreau | 20b0de5 | 2012-12-24 15:45:22 +0100 | [diff] [blame] | 3030 | struct hdr_ctx ctx; |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 3031 | |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 3032 | list_for_each_entry(rule, rules, list) { |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 3033 | if (rule->action >= HTTP_REQ_ACT_MAX) |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 3034 | continue; |
| 3035 | |
Willy Tarreau | 96257ec | 2012-12-27 10:46:37 +0100 | [diff] [blame] | 3036 | /* check optional condition */ |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 3037 | if (rule->cond) { |
Willy Tarreau | 96257ec | 2012-12-27 10:46:37 +0100 | [diff] [blame] | 3038 | int ret; |
| 3039 | |
Willy Tarreau | 32a6f2e | 2012-04-25 10:13:36 +0200 | [diff] [blame] | 3040 | 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] | 3041 | ret = acl_pass(ret); |
| 3042 | |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 3043 | if (rule->cond->pol == ACL_COND_UNLESS) |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 3044 | ret = !ret; |
Willy Tarreau | 96257ec | 2012-12-27 10:46:37 +0100 | [diff] [blame] | 3045 | |
| 3046 | if (!ret) /* condition not matched */ |
| 3047 | continue; |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 3048 | } |
| 3049 | |
Willy Tarreau | 20b0de5 | 2012-12-24 15:45:22 +0100 | [diff] [blame] | 3050 | |
Willy Tarreau | 96257ec | 2012-12-27 10:46:37 +0100 | [diff] [blame] | 3051 | switch (rule->action) { |
| 3052 | case HTTP_REQ_ACT_ALLOW: |
| 3053 | return NULL; /* "allow" rules are OK */ |
| 3054 | |
| 3055 | case HTTP_REQ_ACT_DENY: |
| 3056 | txn->flags |= TX_CLDENY; |
| 3057 | return rule; |
| 3058 | |
Willy Tarreau | ccbcc37 | 2012-12-27 12:37:57 +0100 | [diff] [blame] | 3059 | case HTTP_REQ_ACT_TARPIT: |
| 3060 | txn->flags |= TX_CLTARPIT; |
| 3061 | return rule; |
| 3062 | |
Willy Tarreau | 96257ec | 2012-12-27 10:46:37 +0100 | [diff] [blame] | 3063 | case HTTP_REQ_ACT_AUTH: |
| 3064 | return rule; |
| 3065 | |
Willy Tarreau | 81499eb | 2012-12-27 12:19:02 +0100 | [diff] [blame] | 3066 | case HTTP_REQ_ACT_REDIR: |
| 3067 | return rule; |
| 3068 | |
Willy Tarreau | f4c43c1 | 2013-06-11 17:01:13 +0200 | [diff] [blame] | 3069 | case HTTP_REQ_ACT_SET_NICE: |
| 3070 | s->task->nice = rule->arg.nice; |
| 3071 | break; |
| 3072 | |
Willy Tarreau | 42cf39e | 2013-06-11 18:51:32 +0200 | [diff] [blame] | 3073 | case HTTP_REQ_ACT_SET_TOS: |
Willy Tarreau | 3c72872 | 2014-01-23 13:50:42 +0100 | [diff] [blame] | 3074 | 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] | 3075 | 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] | 3076 | break; |
| 3077 | |
Willy Tarreau | 51347ed | 2013-06-11 19:34:13 +0200 | [diff] [blame] | 3078 | case HTTP_REQ_ACT_SET_MARK: |
| 3079 | #ifdef SO_MARK |
Willy Tarreau | 3c72872 | 2014-01-23 13:50:42 +0100 | [diff] [blame] | 3080 | 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] | 3081 | 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] | 3082 | #endif |
| 3083 | break; |
| 3084 | |
Willy Tarreau | 9a355ec | 2013-06-11 17:45:46 +0200 | [diff] [blame] | 3085 | case HTTP_REQ_ACT_SET_LOGL: |
| 3086 | s->logs.level = rule->arg.loglevel; |
| 3087 | break; |
| 3088 | |
Willy Tarreau | 96257ec | 2012-12-27 10:46:37 +0100 | [diff] [blame] | 3089 | case HTTP_REQ_ACT_SET_HDR: |
| 3090 | ctx.idx = 0; |
| 3091 | /* remove all occurrences of the header */ |
| 3092 | while (http_find_header2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len, |
| 3093 | txn->req.chn->buf->p, &txn->hdr_idx, &ctx)) { |
| 3094 | http_remove_header2(&txn->req, &txn->hdr_idx, &ctx); |
Willy Tarreau | 20b0de5 | 2012-12-24 15:45:22 +0100 | [diff] [blame] | 3095 | } |
Willy Tarreau | 96257ec | 2012-12-27 10:46:37 +0100 | [diff] [blame] | 3096 | /* now fall through to header addition */ |
| 3097 | |
| 3098 | case HTTP_REQ_ACT_ADD_HDR: |
| 3099 | chunk_printf(&trash, "%s: ", rule->arg.hdr_add.name); |
| 3100 | memcpy(trash.str, rule->arg.hdr_add.name, rule->arg.hdr_add.name_len); |
| 3101 | trash.len = rule->arg.hdr_add.name_len; |
| 3102 | trash.str[trash.len++] = ':'; |
| 3103 | trash.str[trash.len++] = ' '; |
| 3104 | trash.len += build_logline(s, trash.str + trash.len, trash.size - trash.len, &rule->arg.hdr_add.fmt); |
| 3105 | http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, trash.len); |
| 3106 | break; |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 3107 | } |
| 3108 | } |
Willy Tarreau | 96257ec | 2012-12-27 10:46:37 +0100 | [diff] [blame] | 3109 | |
| 3110 | /* we reached the end of the rules, nothing to report */ |
Willy Tarreau | 418c1a0 | 2012-12-25 20:52:58 +0100 | [diff] [blame] | 3111 | return NULL; |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 3112 | } |
| 3113 | |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 3114 | |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 3115 | /* Executes the http-response rules <rules> for session <s>, proxy <px> and |
| 3116 | * transaction <txn>. Returns the first rule that prevents further processing |
| 3117 | * of the response (deny, ...) or NULL if it executed all rules or stopped |
| 3118 | * on an allow. It may set the TX_SVDENY on txn->flags if it encounters a deny |
| 3119 | * rule. |
| 3120 | */ |
| 3121 | static struct http_res_rule * |
| 3122 | http_res_get_intercept_rule(struct proxy *px, struct list *rules, struct session *s, struct http_txn *txn) |
| 3123 | { |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 3124 | struct connection *cli_conn; |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 3125 | struct http_res_rule *rule; |
| 3126 | struct hdr_ctx ctx; |
| 3127 | |
| 3128 | list_for_each_entry(rule, rules, list) { |
| 3129 | if (rule->action >= HTTP_RES_ACT_MAX) |
| 3130 | continue; |
| 3131 | |
| 3132 | /* check optional condition */ |
| 3133 | if (rule->cond) { |
| 3134 | int ret; |
| 3135 | |
| 3136 | ret = acl_exec_cond(rule->cond, px, s, txn, SMP_OPT_DIR_RES|SMP_OPT_FINAL); |
| 3137 | ret = acl_pass(ret); |
| 3138 | |
| 3139 | if (rule->cond->pol == ACL_COND_UNLESS) |
| 3140 | ret = !ret; |
| 3141 | |
| 3142 | if (!ret) /* condition not matched */ |
| 3143 | continue; |
| 3144 | } |
| 3145 | |
| 3146 | |
| 3147 | switch (rule->action) { |
| 3148 | case HTTP_RES_ACT_ALLOW: |
| 3149 | return NULL; /* "allow" rules are OK */ |
| 3150 | |
| 3151 | case HTTP_RES_ACT_DENY: |
| 3152 | txn->flags |= TX_SVDENY; |
| 3153 | return rule; |
| 3154 | |
Willy Tarreau | f4c43c1 | 2013-06-11 17:01:13 +0200 | [diff] [blame] | 3155 | case HTTP_RES_ACT_SET_NICE: |
| 3156 | s->task->nice = rule->arg.nice; |
| 3157 | break; |
| 3158 | |
Willy Tarreau | 42cf39e | 2013-06-11 18:51:32 +0200 | [diff] [blame] | 3159 | case HTTP_RES_ACT_SET_TOS: |
Willy Tarreau | 3c72872 | 2014-01-23 13:50:42 +0100 | [diff] [blame] | 3160 | 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] | 3161 | 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] | 3162 | break; |
| 3163 | |
Willy Tarreau | 51347ed | 2013-06-11 19:34:13 +0200 | [diff] [blame] | 3164 | case HTTP_RES_ACT_SET_MARK: |
| 3165 | #ifdef SO_MARK |
Willy Tarreau | 3c72872 | 2014-01-23 13:50:42 +0100 | [diff] [blame] | 3166 | 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] | 3167 | 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] | 3168 | #endif |
| 3169 | break; |
| 3170 | |
Willy Tarreau | 9a355ec | 2013-06-11 17:45:46 +0200 | [diff] [blame] | 3171 | case HTTP_RES_ACT_SET_LOGL: |
| 3172 | s->logs.level = rule->arg.loglevel; |
| 3173 | break; |
| 3174 | |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 3175 | case HTTP_RES_ACT_SET_HDR: |
| 3176 | ctx.idx = 0; |
| 3177 | /* remove all occurrences of the header */ |
| 3178 | while (http_find_header2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len, |
| 3179 | txn->rsp.chn->buf->p, &txn->hdr_idx, &ctx)) { |
| 3180 | http_remove_header2(&txn->rsp, &txn->hdr_idx, &ctx); |
| 3181 | } |
| 3182 | /* now fall through to header addition */ |
| 3183 | |
| 3184 | case HTTP_RES_ACT_ADD_HDR: |
| 3185 | chunk_printf(&trash, "%s: ", rule->arg.hdr_add.name); |
| 3186 | memcpy(trash.str, rule->arg.hdr_add.name, rule->arg.hdr_add.name_len); |
| 3187 | trash.len = rule->arg.hdr_add.name_len; |
| 3188 | trash.str[trash.len++] = ':'; |
| 3189 | trash.str[trash.len++] = ' '; |
| 3190 | trash.len += build_logline(s, trash.str + trash.len, trash.size - trash.len, &rule->arg.hdr_add.fmt); |
| 3191 | http_header_add_tail2(&txn->rsp, &txn->hdr_idx, trash.str, trash.len); |
| 3192 | break; |
| 3193 | } |
| 3194 | } |
| 3195 | |
| 3196 | /* we reached the end of the rules, nothing to report */ |
| 3197 | return NULL; |
| 3198 | } |
| 3199 | |
| 3200 | |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 3201 | /* Perform an HTTP redirect based on the information in <rule>. The function |
| 3202 | * returns non-zero on success, or zero in case of a, irrecoverable error such |
| 3203 | * as too large a request to build a valid response. |
| 3204 | */ |
| 3205 | static int http_apply_redirect_rule(struct redirect_rule *rule, struct session *s, struct http_txn *txn) |
| 3206 | { |
| 3207 | struct http_msg *msg = &txn->req; |
| 3208 | const char *msg_fmt; |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 3209 | const char *location; |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 3210 | |
| 3211 | /* build redirect message */ |
| 3212 | switch(rule->code) { |
Yves Lafon | 3e8d1ae | 2013-03-11 11:06:05 -0400 | [diff] [blame] | 3213 | case 308: |
| 3214 | msg_fmt = HTTP_308; |
| 3215 | break; |
| 3216 | case 307: |
| 3217 | msg_fmt = HTTP_307; |
| 3218 | break; |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 3219 | case 303: |
| 3220 | msg_fmt = HTTP_303; |
| 3221 | break; |
| 3222 | case 301: |
| 3223 | msg_fmt = HTTP_301; |
| 3224 | break; |
| 3225 | case 302: |
| 3226 | default: |
| 3227 | msg_fmt = HTTP_302; |
| 3228 | break; |
| 3229 | } |
| 3230 | |
| 3231 | if (unlikely(!chunk_strcpy(&trash, msg_fmt))) |
| 3232 | return 0; |
| 3233 | |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 3234 | location = trash.str + trash.len; |
| 3235 | |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 3236 | switch(rule->type) { |
| 3237 | case REDIRECT_TYPE_SCHEME: { |
| 3238 | const char *path; |
| 3239 | const char *host; |
| 3240 | struct hdr_ctx ctx; |
| 3241 | int pathlen; |
| 3242 | int hostlen; |
| 3243 | |
| 3244 | host = ""; |
| 3245 | hostlen = 0; |
| 3246 | ctx.idx = 0; |
| 3247 | if (http_find_header2("Host", 4, txn->req.chn->buf->p + txn->req.sol, &txn->hdr_idx, &ctx)) { |
| 3248 | host = ctx.line + ctx.val; |
| 3249 | hostlen = ctx.vlen; |
| 3250 | } |
| 3251 | |
| 3252 | path = http_get_path(txn); |
| 3253 | /* build message using path */ |
| 3254 | if (path) { |
| 3255 | pathlen = txn->req.sl.rq.u_l + (txn->req.chn->buf->p + txn->req.sl.rq.u) - path; |
| 3256 | if (rule->flags & REDIRECT_FLAG_DROP_QS) { |
| 3257 | int qs = 0; |
| 3258 | while (qs < pathlen) { |
| 3259 | if (path[qs] == '?') { |
| 3260 | pathlen = qs; |
| 3261 | break; |
| 3262 | } |
| 3263 | qs++; |
| 3264 | } |
| 3265 | } |
| 3266 | } else { |
| 3267 | path = "/"; |
| 3268 | pathlen = 1; |
| 3269 | } |
| 3270 | |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 3271 | if (rule->rdr_str) { /* this is an old "redirect" rule */ |
| 3272 | /* check if we can add scheme + "://" + host + path */ |
| 3273 | if (trash.len + rule->rdr_len + 3 + hostlen + pathlen > trash.size - 4) |
| 3274 | return 0; |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 3275 | |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 3276 | /* add scheme */ |
| 3277 | memcpy(trash.str + trash.len, rule->rdr_str, rule->rdr_len); |
| 3278 | trash.len += rule->rdr_len; |
| 3279 | } |
| 3280 | else { |
| 3281 | /* add scheme with executing log format */ |
| 3282 | 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] | 3283 | |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 3284 | /* check if we can add scheme + "://" + host + path */ |
| 3285 | if (trash.len + 3 + hostlen + pathlen > trash.size - 4) |
| 3286 | return 0; |
| 3287 | } |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 3288 | /* add "://" */ |
| 3289 | memcpy(trash.str + trash.len, "://", 3); |
| 3290 | trash.len += 3; |
| 3291 | |
| 3292 | /* add host */ |
| 3293 | memcpy(trash.str + trash.len, host, hostlen); |
| 3294 | trash.len += hostlen; |
| 3295 | |
| 3296 | /* add path */ |
| 3297 | memcpy(trash.str + trash.len, path, pathlen); |
| 3298 | trash.len += pathlen; |
| 3299 | |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 3300 | /* 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] | 3301 | if (trash.len && trash.str[trash.len - 1] != '/' && |
| 3302 | (rule->flags & REDIRECT_FLAG_APPEND_SLASH)) { |
| 3303 | if (trash.len > trash.size - 5) |
| 3304 | return 0; |
| 3305 | trash.str[trash.len] = '/'; |
| 3306 | trash.len++; |
| 3307 | } |
| 3308 | |
| 3309 | break; |
| 3310 | } |
| 3311 | case REDIRECT_TYPE_PREFIX: { |
| 3312 | const char *path; |
| 3313 | int pathlen; |
| 3314 | |
| 3315 | path = http_get_path(txn); |
| 3316 | /* build message using path */ |
| 3317 | if (path) { |
| 3318 | pathlen = txn->req.sl.rq.u_l + (txn->req.chn->buf->p + txn->req.sl.rq.u) - path; |
| 3319 | if (rule->flags & REDIRECT_FLAG_DROP_QS) { |
| 3320 | int qs = 0; |
| 3321 | while (qs < pathlen) { |
| 3322 | if (path[qs] == '?') { |
| 3323 | pathlen = qs; |
| 3324 | break; |
| 3325 | } |
| 3326 | qs++; |
| 3327 | } |
| 3328 | } |
| 3329 | } else { |
| 3330 | path = "/"; |
| 3331 | pathlen = 1; |
| 3332 | } |
| 3333 | |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 3334 | if (rule->rdr_str) { /* this is an old "redirect" rule */ |
| 3335 | if (trash.len + rule->rdr_len + pathlen > trash.size - 4) |
| 3336 | return 0; |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 3337 | |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 3338 | /* add prefix. Note that if prefix == "/", we don't want to |
| 3339 | * add anything, otherwise it makes it hard for the user to |
| 3340 | * configure a self-redirection. |
| 3341 | */ |
| 3342 | if (rule->rdr_len != 1 || *rule->rdr_str != '/') { |
| 3343 | memcpy(trash.str + trash.len, rule->rdr_str, rule->rdr_len); |
| 3344 | trash.len += rule->rdr_len; |
| 3345 | } |
| 3346 | } |
| 3347 | else { |
| 3348 | /* add prefix with executing log format */ |
| 3349 | trash.len += build_logline(s, trash.str + trash.len, trash.size - trash.len, &rule->rdr_fmt); |
| 3350 | |
| 3351 | /* Check length */ |
| 3352 | if (trash.len + pathlen > trash.size - 4) |
| 3353 | return 0; |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 3354 | } |
| 3355 | |
| 3356 | /* add path */ |
| 3357 | memcpy(trash.str + trash.len, path, pathlen); |
| 3358 | trash.len += pathlen; |
| 3359 | |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 3360 | /* 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] | 3361 | if (trash.len && trash.str[trash.len - 1] != '/' && |
| 3362 | (rule->flags & REDIRECT_FLAG_APPEND_SLASH)) { |
| 3363 | if (trash.len > trash.size - 5) |
| 3364 | return 0; |
| 3365 | trash.str[trash.len] = '/'; |
| 3366 | trash.len++; |
| 3367 | } |
| 3368 | |
| 3369 | break; |
| 3370 | } |
| 3371 | case REDIRECT_TYPE_LOCATION: |
| 3372 | default: |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 3373 | if (rule->rdr_str) { /* this is an old "redirect" rule */ |
| 3374 | if (trash.len + rule->rdr_len > trash.size - 4) |
| 3375 | return 0; |
| 3376 | |
| 3377 | /* add location */ |
| 3378 | memcpy(trash.str + trash.len, rule->rdr_str, rule->rdr_len); |
| 3379 | trash.len += rule->rdr_len; |
| 3380 | } |
| 3381 | else { |
| 3382 | /* add location with executing log format */ |
| 3383 | 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] | 3384 | |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 3385 | /* Check left length */ |
| 3386 | if (trash.len > trash.size - 4) |
| 3387 | return 0; |
| 3388 | } |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 3389 | break; |
| 3390 | } |
| 3391 | |
| 3392 | if (rule->cookie_len) { |
| 3393 | memcpy(trash.str + trash.len, "\r\nSet-Cookie: ", 14); |
| 3394 | trash.len += 14; |
| 3395 | memcpy(trash.str + trash.len, rule->cookie_str, rule->cookie_len); |
| 3396 | trash.len += rule->cookie_len; |
| 3397 | memcpy(trash.str + trash.len, "\r\n", 2); |
| 3398 | trash.len += 2; |
| 3399 | } |
| 3400 | |
| 3401 | /* add end of headers and the keep-alive/close status. |
| 3402 | * We may choose to set keep-alive if the Location begins |
| 3403 | * with a slash, because the client will come back to the |
| 3404 | * same server. |
| 3405 | */ |
| 3406 | txn->status = rule->code; |
| 3407 | /* let's log the request time */ |
| 3408 | s->logs.tv_request = now; |
| 3409 | |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 3410 | if (*location == '/' && |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 3411 | (msg->flags & HTTP_MSGF_XFER_LEN) && |
| 3412 | !(msg->flags & HTTP_MSGF_TE_CHNK) && !txn->req.body_len && |
| 3413 | ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL || |
| 3414 | (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL)) { |
| 3415 | /* keep-alive possible */ |
| 3416 | if (!(msg->flags & HTTP_MSGF_VER_11)) { |
| 3417 | if (unlikely(txn->flags & TX_USE_PX_CONN)) { |
| 3418 | memcpy(trash.str + trash.len, "\r\nProxy-Connection: keep-alive", 30); |
| 3419 | trash.len += 30; |
| 3420 | } else { |
| 3421 | memcpy(trash.str + trash.len, "\r\nConnection: keep-alive", 24); |
| 3422 | trash.len += 24; |
| 3423 | } |
| 3424 | } |
| 3425 | memcpy(trash.str + trash.len, "\r\n\r\n", 4); |
| 3426 | trash.len += 4; |
| 3427 | bo_inject(txn->rsp.chn, trash.str, trash.len); |
| 3428 | /* "eat" the request */ |
| 3429 | bi_fast_delete(txn->req.chn->buf, msg->sov); |
| 3430 | msg->sov = 0; |
| 3431 | txn->req.chn->analysers = AN_REQ_HTTP_XFER_BODY; |
| 3432 | s->rep->analysers = AN_RES_HTTP_XFER_BODY; |
| 3433 | txn->req.msg_state = HTTP_MSG_CLOSED; |
| 3434 | txn->rsp.msg_state = HTTP_MSG_DONE; |
| 3435 | } else { |
| 3436 | /* keep-alive not possible */ |
| 3437 | if (unlikely(txn->flags & TX_USE_PX_CONN)) { |
| 3438 | memcpy(trash.str + trash.len, "\r\nProxy-Connection: close\r\n\r\n", 29); |
| 3439 | trash.len += 29; |
| 3440 | } else { |
| 3441 | memcpy(trash.str + trash.len, "\r\nConnection: close\r\n\r\n", 23); |
| 3442 | trash.len += 23; |
| 3443 | } |
| 3444 | stream_int_retnclose(txn->req.chn->prod, &trash); |
| 3445 | txn->req.chn->analysers = 0; |
| 3446 | } |
| 3447 | |
| 3448 | if (!(s->flags & SN_ERR_MASK)) |
Willy Tarreau | 570f221 | 2013-06-10 16:42:09 +0200 | [diff] [blame] | 3449 | s->flags |= SN_ERR_LOCAL; |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 3450 | if (!(s->flags & SN_FINST_MASK)) |
| 3451 | s->flags |= SN_FINST_R; |
| 3452 | |
| 3453 | return 1; |
| 3454 | } |
| 3455 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3456 | /* This stream analyser runs all HTTP request processing which is common to |
| 3457 | * frontends and backends, which means blocking ACLs, filters, connection-close, |
| 3458 | * reqadd, stats and redirects. This is performed for the designated proxy. |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 3459 | * 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] | 3460 | * either needs more data or wants to immediately abort the request (eg: deny, |
| 3461 | * error, ...). |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 3462 | */ |
Willy Tarreau | 7421efb | 2012-07-02 15:11:27 +0200 | [diff] [blame] | 3463 | 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] | 3464 | { |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 3465 | struct http_txn *txn = &s->txn; |
| 3466 | struct http_msg *msg = &txn->req; |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3467 | struct acl_cond *cond; |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 3468 | struct http_req_rule *http_req_last_rule = NULL; |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3469 | struct redirect_rule *rule; |
Willy Tarreau | f4f0412 | 2010-01-28 18:10:50 +0100 | [diff] [blame] | 3470 | struct cond_wordlist *wl; |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 3471 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 3472 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) { |
Willy Tarreau | 51aecc7 | 2009-07-12 09:47:04 +0200 | [diff] [blame] | 3473 | /* we need more data */ |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 3474 | channel_dont_connect(req); |
Willy Tarreau | 51aecc7 | 2009-07-12 09:47:04 +0200 | [diff] [blame] | 3475 | return 0; |
| 3476 | } |
| 3477 | |
Willy Tarreau | 3a81629 | 2009-07-07 10:55:49 +0200 | [diff] [blame] | 3478 | req->analysers &= ~an_bit; |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 3479 | req->analyse_exp = TICK_ETERNITY; |
| 3480 | |
Willy Tarreau | 02d6cfc | 2012-03-01 18:19:58 +0100 | [diff] [blame] | 3481 | 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] | 3482 | now_ms, __FUNCTION__, |
| 3483 | s, |
| 3484 | req, |
| 3485 | req->rex, req->wex, |
| 3486 | req->flags, |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 3487 | req->buf->i, |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 3488 | req->analysers); |
| 3489 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3490 | /* first check whether we have some ACLs set to block this request */ |
| 3491 | list_for_each_entry(cond, &px->block_cond, list) { |
Willy Tarreau | 32a6f2e | 2012-04-25 10:13:36 +0200 | [diff] [blame] | 3492 | 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] | 3493 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3494 | ret = acl_pass(ret); |
| 3495 | if (cond->pol == ACL_COND_UNLESS) |
| 3496 | ret = !ret; |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 3497 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3498 | if (ret) { |
| 3499 | txn->status = 403; |
| 3500 | /* let's log the request time */ |
| 3501 | s->logs.tv_request = now; |
Willy Tarreau | 783f258 | 2012-09-04 12:19:04 +0200 | [diff] [blame] | 3502 | stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_403)); |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 3503 | session_inc_http_err_ctr(s); |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3504 | goto return_prx_cond; |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3505 | } |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3506 | } |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3507 | |
Willy Tarreau | 5d5b5d8 | 2012-12-09 12:00:04 +0100 | [diff] [blame] | 3508 | /* just in case we have some per-backend tracking */ |
| 3509 | session_inc_be_http_req_ctr(s); |
| 3510 | |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 3511 | /* evaluate http-request rules */ |
Willy Tarreau | 96257ec | 2012-12-27 10:46:37 +0100 | [diff] [blame] | 3512 | 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] | 3513 | |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 3514 | /* evaluate stats http-request rules only if http-request is OK */ |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 3515 | if (!http_req_last_rule) { |
Willy Tarreau | 414e9bb | 2013-11-23 00:30:38 +0100 | [diff] [blame] | 3516 | if (stats_check_uri(s->rep->prod, txn, px)) { |
| 3517 | s->target = &http_stats_applet.obj_type; |
Willy Tarreau | 1fbe1c9 | 2013-12-01 09:35:41 +0100 | [diff] [blame] | 3518 | if (unlikely(!stream_int_register_handler(s->rep->prod, objt_applet(s->target)))) { |
| 3519 | txn->status = 500; |
| 3520 | s->logs.tv_request = now; |
| 3521 | stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_500)); |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 3522 | |
Willy Tarreau | 1fbe1c9 | 2013-12-01 09:35:41 +0100 | [diff] [blame] | 3523 | if (!(s->flags & SN_ERR_MASK)) |
| 3524 | s->flags |= SN_ERR_RESOURCE; |
| 3525 | goto return_prx_cond; |
| 3526 | } |
Willy Tarreau | 414e9bb | 2013-11-23 00:30:38 +0100 | [diff] [blame] | 3527 | /* parse the whole stats request and extract the relevant information */ |
| 3528 | http_handle_stats(s, req); |
Willy Tarreau | 96257ec | 2012-12-27 10:46:37 +0100 | [diff] [blame] | 3529 | 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] | 3530 | } |
Krzysztof Piotr Oledzki | 59bb218 | 2010-01-29 17:58:21 +0100 | [diff] [blame] | 3531 | } |
| 3532 | |
Willy Tarreau | 3b44e72 | 2013-11-16 10:28:23 +0100 | [diff] [blame] | 3533 | /* only apply req{,i}{rep/deny/tarpit} if the request was not yet |
| 3534 | * blocked by an http-request rule. |
| 3535 | */ |
| 3536 | if (!(txn->flags & (TX_CLDENY|TX_CLTARPIT)) && (px->req_exp != NULL)) { |
Willy Tarreau | 6c123b1 | 2010-01-28 20:22:06 +0100 | [diff] [blame] | 3537 | if (apply_filters_to_request(s, req, px) < 0) |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3538 | goto return_bad_req; |
Willy Tarreau | 3b44e72 | 2013-11-16 10:28:23 +0100 | [diff] [blame] | 3539 | } |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 3540 | |
Willy Tarreau | 3b44e72 | 2013-11-16 10:28:23 +0100 | [diff] [blame] | 3541 | /* return a 403 if either rule has blocked */ |
| 3542 | if (txn->flags & (TX_CLDENY|TX_CLTARPIT)) { |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3543 | if (txn->flags & TX_CLDENY) { |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3544 | txn->status = 403; |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3545 | s->logs.tv_request = now; |
Willy Tarreau | 783f258 | 2012-09-04 12:19:04 +0200 | [diff] [blame] | 3546 | stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_403)); |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 3547 | session_inc_http_err_ctr(s); |
Willy Tarreau | 687ba13 | 2013-11-16 10:13:35 +0100 | [diff] [blame] | 3548 | s->fe->fe_counters.denied_req++; |
| 3549 | if (s->fe != s->be) |
| 3550 | s->be->be_counters.denied_req++; |
| 3551 | if (s->listener->counters) |
| 3552 | s->listener->counters->denied_req++; |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3553 | goto return_prx_cond; |
| 3554 | } |
Willy Tarreau | c465fd7 | 2009-08-31 00:17:18 +0200 | [diff] [blame] | 3555 | |
| 3556 | /* When a connection is tarpitted, we use the tarpit timeout, |
| 3557 | * which may be the same as the connect timeout if unspecified. |
| 3558 | * If unset, then set it to zero because we really want it to |
| 3559 | * eventually expire. We build the tarpit as an analyser. |
| 3560 | */ |
| 3561 | if (txn->flags & TX_CLTARPIT) { |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 3562 | channel_erase(s->req); |
Willy Tarreau | c465fd7 | 2009-08-31 00:17:18 +0200 | [diff] [blame] | 3563 | /* wipe the request out so that we can drop the connection early |
| 3564 | * if the client closes first. |
| 3565 | */ |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 3566 | channel_dont_connect(req); |
Willy Tarreau | c465fd7 | 2009-08-31 00:17:18 +0200 | [diff] [blame] | 3567 | req->analysers = 0; /* remove switching rules etc... */ |
| 3568 | req->analysers |= AN_REQ_HTTP_TARPIT; |
| 3569 | req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.tarpit); |
| 3570 | if (!req->analyse_exp) |
| 3571 | req->analyse_exp = tick_add(now_ms, 0); |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 3572 | session_inc_http_err_ctr(s); |
Willy Tarreau | 687ba13 | 2013-11-16 10:13:35 +0100 | [diff] [blame] | 3573 | s->fe->fe_counters.denied_req++; |
| 3574 | if (s->fe != s->be) |
| 3575 | s->be->be_counters.denied_req++; |
| 3576 | if (s->listener->counters) |
| 3577 | s->listener->counters->denied_req++; |
Willy Tarreau | c465fd7 | 2009-08-31 00:17:18 +0200 | [diff] [blame] | 3578 | return 1; |
| 3579 | } |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3580 | } |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 3581 | |
Willy Tarreau | 70dffda | 2014-01-30 03:07:23 +0100 | [diff] [blame] | 3582 | /* 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] | 3583 | * only change if both the request and the config reference something else. |
Willy Tarreau | 70dffda | 2014-01-30 03:07:23 +0100 | [diff] [blame] | 3584 | * Option httpclose by itself sets tunnel mode where headers are mangled. |
| 3585 | * However, if another mode is set, it will affect it (eg: server-close/ |
| 3586 | * keep-alive + httpclose = close). Note that we avoid to redo the same work |
| 3587 | * if FE and BE have the same settings (common). The method consists in |
| 3588 | * checking if options changed between the two calls (implying that either |
| 3589 | * one is non-null, or one of them is non-null and we are there for the first |
| 3590 | * time. |
Willy Tarreau | 4273664 | 2009-10-18 21:04:35 +0200 | [diff] [blame] | 3591 | */ |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 3592 | |
Willy Tarreau | 416ce61 | 2014-01-31 15:45:34 +0100 | [diff] [blame] | 3593 | if (!(txn->flags & TX_HDR_CONN_PRS) || |
Willy Tarreau | 02bce8b | 2014-01-30 00:15:28 +0100 | [diff] [blame] | 3594 | ((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] | 3595 | int tmp = TX_CON_WANT_KAL; |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 3596 | |
Willy Tarreau | 70dffda | 2014-01-30 03:07:23 +0100 | [diff] [blame] | 3597 | if (!((s->fe->options2|s->be->options2) & PR_O2_FAKE_KA)) { |
| 3598 | if ((s->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_TUN || |
| 3599 | (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_TUN) |
| 3600 | tmp = TX_CON_WANT_TUN; |
| 3601 | |
| 3602 | if ((s->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL || |
| 3603 | (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL) |
| 3604 | tmp = TX_CON_WANT_TUN; |
| 3605 | } |
Willy Tarreau | 02bce8b | 2014-01-30 00:15:28 +0100 | [diff] [blame] | 3606 | |
| 3607 | if ((s->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_SCL || |
Willy Tarreau | 70dffda | 2014-01-30 03:07:23 +0100 | [diff] [blame] | 3608 | (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_SCL) { |
| 3609 | /* option httpclose + server_close => forceclose */ |
| 3610 | if ((s->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL || |
| 3611 | (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL) |
| 3612 | tmp = TX_CON_WANT_CLO; |
| 3613 | else |
| 3614 | tmp = TX_CON_WANT_SCL; |
| 3615 | } |
Willy Tarreau | 02bce8b | 2014-01-30 00:15:28 +0100 | [diff] [blame] | 3616 | |
| 3617 | if ((s->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_FCL || |
| 3618 | (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_FCL) |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 3619 | tmp = TX_CON_WANT_CLO; |
| 3620 | |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 3621 | if ((txn->flags & TX_CON_WANT_MSK) < tmp) |
| 3622 | txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | tmp; |
Willy Tarreau | 0dfdf19 | 2010-01-05 11:33:11 +0100 | [diff] [blame] | 3623 | |
Willy Tarreau | 416ce61 | 2014-01-31 15:45:34 +0100 | [diff] [blame] | 3624 | if (!(txn->flags & TX_HDR_CONN_PRS) && |
| 3625 | (txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) { |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 3626 | /* parse the Connection header and possibly clean it */ |
| 3627 | int to_del = 0; |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 3628 | if ((msg->flags & HTTP_MSGF_VER_11) || |
Willy Tarreau | 8a8e1d9 | 2010-04-05 16:15:16 +0200 | [diff] [blame] | 3629 | ((txn->flags & TX_CON_WANT_MSK) >= TX_CON_WANT_SCL && |
| 3630 | !((s->fe->options2|s->be->options2) & PR_O2_FAKE_KA))) |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 3631 | to_del |= 2; /* remove "keep-alive" */ |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 3632 | if (!(msg->flags & HTTP_MSGF_VER_11)) |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 3633 | to_del |= 1; /* remove "close" */ |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 3634 | http_parse_connection_header(txn, msg, to_del); |
Willy Tarreau | 0dfdf19 | 2010-01-05 11:33:11 +0100 | [diff] [blame] | 3635 | } |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 3636 | |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 3637 | /* check if client or config asks for explicit close in KAL/SCL */ |
| 3638 | if (((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL || |
| 3639 | (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) && |
| 3640 | ((txn->flags & TX_HDR_CONN_CLO) || /* "connection: close" */ |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 3641 | (!(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] | 3642 | !(msg->flags & HTTP_MSGF_XFER_LEN) || /* no length known => close */ |
Willy Tarreau | c3e8b25 | 2010-01-28 15:01:20 +0100 | [diff] [blame] | 3643 | s->fe->state == PR_STSTOPPED)) /* frontend is stopping */ |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 3644 | txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO; |
| 3645 | } |
Willy Tarreau | 7859991 | 2009-10-17 20:12:21 +0200 | [diff] [blame] | 3646 | |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 3647 | /* we can be blocked here because the request needs to be authenticated, |
| 3648 | * either to pass or to access stats. |
| 3649 | */ |
Willy Tarreau | 20b0de5 | 2012-12-24 15:45:22 +0100 | [diff] [blame] | 3650 | 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] | 3651 | char *realm = http_req_last_rule->arg.auth.realm; |
Krzysztof Piotr Oledzki | 59bb218 | 2010-01-29 17:58:21 +0100 | [diff] [blame] | 3652 | |
Krzysztof Piotr Oledzki | 8c8bd45 | 2010-01-29 19:29:32 +0100 | [diff] [blame] | 3653 | if (!realm) |
Willy Tarreau | 414e9bb | 2013-11-23 00:30:38 +0100 | [diff] [blame] | 3654 | 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] | 3655 | |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 3656 | 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] | 3657 | txn->status = 401; |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 3658 | stream_int_retnclose(req->prod, &trash); |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 3659 | /* on 401 we still count one error, because normal browsing |
| 3660 | * won't significantly increase the counter but brute force |
| 3661 | * attempts will. |
| 3662 | */ |
| 3663 | session_inc_http_err_ctr(s); |
Krzysztof Piotr Oledzki | 59bb218 | 2010-01-29 17:58:21 +0100 | [diff] [blame] | 3664 | goto return_prx_cond; |
| 3665 | } |
| 3666 | |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 3667 | /* add request headers from the rule sets in the same order */ |
| 3668 | list_for_each_entry(wl, &px->req_add, list) { |
| 3669 | if (wl->cond) { |
Willy Tarreau | 32a6f2e | 2012-04-25 10:13:36 +0200 | [diff] [blame] | 3670 | 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] | 3671 | ret = acl_pass(ret); |
| 3672 | if (((struct acl_cond *)wl->cond)->pol == ACL_COND_UNLESS) |
| 3673 | ret = !ret; |
| 3674 | if (!ret) |
| 3675 | continue; |
| 3676 | } |
| 3677 | |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 3678 | 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] | 3679 | goto return_bad_req; |
Willy Tarreau | 81499eb | 2012-12-27 12:19:02 +0100 | [diff] [blame] | 3680 | } |
| 3681 | |
| 3682 | if (http_req_last_rule && http_req_last_rule->action == HTTP_REQ_ACT_REDIR) { |
| 3683 | if (!http_apply_redirect_rule(http_req_last_rule->arg.redir, s, txn)) |
| 3684 | goto return_bad_req; |
| 3685 | req->analyse_exp = TICK_ETERNITY; |
| 3686 | return 1; |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 3687 | } |
| 3688 | |
Willy Tarreau | 414e9bb | 2013-11-23 00:30:38 +0100 | [diff] [blame] | 3689 | if (unlikely(objt_applet(s->target) == &http_stats_applet)) { |
Willy Tarreau | 1facd6d | 2012-12-22 22:03:39 +0100 | [diff] [blame] | 3690 | /* process the stats request now */ |
Willy Tarreau | 347a35d | 2013-11-22 17:51:09 +0100 | [diff] [blame] | 3691 | if (s->fe == s->be) /* report it if the request was intercepted by the frontend */ |
| 3692 | s->fe->fe_counters.intercepted_req++; |
| 3693 | |
| 3694 | if (!(s->flags & SN_ERR_MASK)) // this is not really an error but it is |
| 3695 | s->flags |= SN_ERR_LOCAL; // to mark that it comes from the proxy |
| 3696 | if (!(s->flags & SN_FINST_MASK)) |
| 3697 | s->flags |= SN_FINST_R; |
| 3698 | |
| 3699 | req->analyse_exp = TICK_ETERNITY; |
Willy Tarreau | 51437d2 | 2013-12-29 00:43:40 +0100 | [diff] [blame] | 3700 | req->analysers = 0; |
Willy Tarreau | 1facd6d | 2012-12-22 22:03:39 +0100 | [diff] [blame] | 3701 | return 1; |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3702 | } |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 3703 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3704 | /* check whether we have some ACLs set to redirect this request */ |
| 3705 | list_for_each_entry(rule, &px->redirect_rules, list) { |
Willy Tarreau | f285f54 | 2010-01-03 20:03:03 +0100 | [diff] [blame] | 3706 | if (rule->cond) { |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 3707 | int ret; |
| 3708 | |
Willy Tarreau | 32a6f2e | 2012-04-25 10:13:36 +0200 | [diff] [blame] | 3709 | 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] | 3710 | ret = acl_pass(ret); |
| 3711 | if (rule->cond->pol == ACL_COND_UNLESS) |
| 3712 | ret = !ret; |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 3713 | if (!ret) |
| 3714 | continue; |
Willy Tarreau | f285f54 | 2010-01-03 20:03:03 +0100 | [diff] [blame] | 3715 | } |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 3716 | if (!http_apply_redirect_rule(rule, s, txn)) |
| 3717 | goto return_bad_req; |
Willy Tarreau | a9679ac | 2010-01-03 17:32:57 +0100 | [diff] [blame] | 3718 | |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 3719 | req->analyse_exp = TICK_ETERNITY; |
| 3720 | return 1; |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3721 | } |
Willy Tarreau | 55ea757 | 2007-06-17 19:56:27 +0200 | [diff] [blame] | 3722 | |
Willy Tarreau | 2be3939 | 2010-01-03 17:24:51 +0100 | [diff] [blame] | 3723 | /* POST requests may be accompanied with an "Expect: 100-Continue" header. |
| 3724 | * If this happens, then the data will not come immediately, so we must |
| 3725 | * send all what we have without waiting. Note that due to the small gain |
| 3726 | * 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] | 3727 | * 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] | 3728 | * itself once used. |
| 3729 | */ |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 3730 | req->flags |= CF_SEND_DONTWAIT; |
Willy Tarreau | 2be3939 | 2010-01-03 17:24:51 +0100 | [diff] [blame] | 3731 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3732 | /* that's OK for us now, let's move on to next analysers */ |
| 3733 | return 1; |
Willy Tarreau | 1138281 | 2008-07-09 16:18:21 +0200 | [diff] [blame] | 3734 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3735 | return_bad_req: |
| 3736 | /* We centralize bad requests processing here */ |
| 3737 | if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) { |
| 3738 | /* we detected a parsing error. We want to archive this request |
| 3739 | * in the dedicated proxy area for later troubleshooting. |
| 3740 | */ |
Willy Tarreau | 8a0cef2 | 2012-03-09 13:39:23 +0100 | [diff] [blame] | 3741 | 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] | 3742 | } |
Willy Tarreau | 55ea757 | 2007-06-17 19:56:27 +0200 | [diff] [blame] | 3743 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3744 | txn->req.msg_state = HTTP_MSG_ERROR; |
| 3745 | txn->status = 400; |
Willy Tarreau | 783f258 | 2012-09-04 12:19:04 +0200 | [diff] [blame] | 3746 | 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] | 3747 | |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 3748 | s->fe->fe_counters.failed_req++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 3749 | if (s->listener->counters) |
| 3750 | s->listener->counters->failed_req++; |
Willy Tarreau | 6e4261e | 2007-09-18 18:36:05 +0200 | [diff] [blame] | 3751 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3752 | return_prx_cond: |
| 3753 | if (!(s->flags & SN_ERR_MASK)) |
| 3754 | s->flags |= SN_ERR_PRXCOND; |
| 3755 | if (!(s->flags & SN_FINST_MASK)) |
| 3756 | s->flags |= SN_FINST_R; |
Willy Tarreau | f1221aa | 2006-12-17 22:14:12 +0100 | [diff] [blame] | 3757 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3758 | req->analysers = 0; |
| 3759 | req->analyse_exp = TICK_ETERNITY; |
| 3760 | return 0; |
| 3761 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3762 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3763 | /* This function performs all the processing enabled for the current request. |
| 3764 | * It returns 1 if the processing can continue on next analysers, or zero if it |
| 3765 | * needs more data, encounters an error, or wants to immediately abort the |
| 3766 | * request. It relies on buffers flags, and updates s->req->analysers. |
| 3767 | */ |
Willy Tarreau | 7421efb | 2012-07-02 15:11:27 +0200 | [diff] [blame] | 3768 | 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] | 3769 | { |
| 3770 | struct http_txn *txn = &s->txn; |
| 3771 | struct http_msg *msg = &txn->req; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 3772 | struct connection *cli_conn = objt_conn(req->prod->end); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3773 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 3774 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) { |
Willy Tarreau | 51aecc7 | 2009-07-12 09:47:04 +0200 | [diff] [blame] | 3775 | /* we need more data */ |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 3776 | channel_dont_connect(req); |
Willy Tarreau | 51aecc7 | 2009-07-12 09:47:04 +0200 | [diff] [blame] | 3777 | return 0; |
| 3778 | } |
| 3779 | |
Willy Tarreau | 02d6cfc | 2012-03-01 18:19:58 +0100 | [diff] [blame] | 3780 | 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] | 3781 | now_ms, __FUNCTION__, |
| 3782 | s, |
| 3783 | req, |
| 3784 | req->rex, req->wex, |
| 3785 | req->flags, |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 3786 | req->buf->i, |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3787 | req->analysers); |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 3788 | |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 3789 | if (s->fe->comp || s->be->comp) |
| 3790 | select_compression_request_header(s, req->buf); |
| 3791 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3792 | /* |
| 3793 | * Right now, we know that we have processed the entire headers |
| 3794 | * and that unwanted requests have been filtered out. We can do |
| 3795 | * whatever we want with the remaining request. Also, now we |
| 3796 | * may have separate values for ->fe, ->be. |
| 3797 | */ |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 3798 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3799 | /* |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 3800 | * If HTTP PROXY is set we simply get remote server address parsing |
| 3801 | * incoming request. Note that this requires that a connection is |
| 3802 | * allocated on the server side. |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3803 | */ |
| 3804 | 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] | 3805 | struct connection *conn; |
Willy Tarreau | e8df1e1 | 2013-12-16 14:30:55 +0100 | [diff] [blame] | 3806 | char *path; |
Willy Tarreau | 32e3c6a | 2013-10-11 19:34:20 +0200 | [diff] [blame] | 3807 | |
Willy Tarreau | 9471b8c | 2013-12-15 13:31:35 +0100 | [diff] [blame] | 3808 | /* Note that for now we don't reuse existing proxy connections */ |
| 3809 | if (unlikely((conn = si_alloc_conn(req->cons, 0)) == NULL)) { |
Willy Tarreau | 32e3c6a | 2013-10-11 19:34:20 +0200 | [diff] [blame] | 3810 | txn->req.msg_state = HTTP_MSG_ERROR; |
| 3811 | txn->status = 500; |
| 3812 | req->analysers = 0; |
| 3813 | stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_500)); |
| 3814 | |
| 3815 | if (!(s->flags & SN_ERR_MASK)) |
| 3816 | s->flags |= SN_ERR_RESOURCE; |
| 3817 | if (!(s->flags & SN_FINST_MASK)) |
| 3818 | s->flags |= SN_FINST_R; |
| 3819 | |
| 3820 | return 0; |
| 3821 | } |
Willy Tarreau | e8df1e1 | 2013-12-16 14:30:55 +0100 | [diff] [blame] | 3822 | |
| 3823 | path = http_get_path(txn); |
| 3824 | url2sa(req->buf->p + msg->sl.rq.u, |
| 3825 | path ? path - (req->buf->p + msg->sl.rq.u) : msg->sl.rq.u_l, |
| 3826 | &conn->addr.to); |
| 3827 | /* if the path was found, we have to remove everything between |
| 3828 | * req->buf->p + msg->sl.rq.u and path (excluded). If it was not |
| 3829 | * found, we need to replace from req->buf->p + msg->sl.rq.u for |
| 3830 | * u_l characters by a single "/". |
| 3831 | */ |
| 3832 | if (path) { |
| 3833 | char *cur_ptr = req->buf->p; |
| 3834 | char *cur_end = cur_ptr + txn->req.sl.rq.l; |
| 3835 | int delta; |
| 3836 | |
| 3837 | delta = buffer_replace2(req->buf, req->buf->p + msg->sl.rq.u, path, NULL, 0); |
| 3838 | http_msg_move_end(&txn->req, delta); |
| 3839 | cur_end += delta; |
| 3840 | if (http_parse_reqline(&txn->req, HTTP_MSG_RQMETH, cur_ptr, cur_end + 1, NULL, NULL) == NULL) |
| 3841 | goto return_bad_req; |
| 3842 | } |
| 3843 | else { |
| 3844 | char *cur_ptr = req->buf->p; |
| 3845 | char *cur_end = cur_ptr + txn->req.sl.rq.l; |
| 3846 | int delta; |
| 3847 | |
| 3848 | delta = buffer_replace2(req->buf, req->buf->p + msg->sl.rq.u, |
| 3849 | req->buf->p + msg->sl.rq.u + msg->sl.rq.u_l, "/", 1); |
| 3850 | http_msg_move_end(&txn->req, delta); |
| 3851 | cur_end += delta; |
| 3852 | if (http_parse_reqline(&txn->req, HTTP_MSG_RQMETH, cur_ptr, cur_end + 1, NULL, NULL) == NULL) |
| 3853 | goto return_bad_req; |
| 3854 | } |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3855 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3856 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3857 | /* |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 3858 | * 7: Now we can work with the cookies. |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3859 | * Note that doing so might move headers in the request, but |
| 3860 | * the fields will stay coherent and the URI will not move. |
| 3861 | * This should only be performed in the backend. |
| 3862 | */ |
Willy Tarreau | fd39dda | 2008-10-17 12:01:58 +0200 | [diff] [blame] | 3863 | 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] | 3864 | && !(txn->flags & (TX_CLDENY|TX_CLTARPIT))) |
| 3865 | manage_client_side_cookies(s, req); |
Willy Tarreau | 7ac51f6 | 2007-03-25 16:00:04 +0200 | [diff] [blame] | 3866 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3867 | /* |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 3868 | * 8: the appsession cookie was looked up very early in 1.2, |
| 3869 | * so let's do the same now. |
| 3870 | */ |
| 3871 | |
Cyril Bonté | 47fdd8e | 2010-04-25 00:00:51 +0200 | [diff] [blame] | 3872 | /* It needs to look into the URI unless persistence must be ignored */ |
| 3873 | 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] | 3874 | 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] | 3875 | } |
| 3876 | |
William Lallemand | a73203e | 2012-03-12 12:48:57 +0100 | [diff] [blame] | 3877 | /* add unique-id if "header-unique-id" is specified */ |
| 3878 | |
William Lallemand | 5b7ea3a | 2013-08-28 15:44:19 +0200 | [diff] [blame] | 3879 | if (!LIST_ISEMPTY(&s->fe->format_unique_id)) { |
| 3880 | if ((s->unique_id = pool_alloc2(pool2_uniqueid)) == NULL) |
| 3881 | goto return_bad_req; |
| 3882 | s->unique_id[0] = '\0'; |
William Lallemand | a73203e | 2012-03-12 12:48:57 +0100 | [diff] [blame] | 3883 | 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] | 3884 | } |
William Lallemand | a73203e | 2012-03-12 12:48:57 +0100 | [diff] [blame] | 3885 | |
| 3886 | if (s->fe->header_unique_id && s->unique_id) { |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 3887 | chunk_printf(&trash, "%s: %s", s->fe->header_unique_id, s->unique_id); |
| 3888 | if (trash.len < 0) |
William Lallemand | a73203e | 2012-03-12 12:48:57 +0100 | [diff] [blame] | 3889 | goto return_bad_req; |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 3890 | 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] | 3891 | goto return_bad_req; |
| 3892 | } |
| 3893 | |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 3894 | /* |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3895 | * 9: add X-Forwarded-For if either the frontend or the backend |
| 3896 | * asks for it. |
| 3897 | */ |
| 3898 | if ((s->fe->options | s->be->options) & PR_O_FWDFOR) { |
Willy Tarreau | 87cf514 | 2011-08-19 22:57:24 +0200 | [diff] [blame] | 3899 | struct hdr_ctx ctx = { .idx = 0 }; |
Willy Tarreau | 87cf514 | 2011-08-19 22:57:24 +0200 | [diff] [blame] | 3900 | if (!((s->fe->options | s->be->options) & PR_O_FF_ALWAYS) && |
Cyril Bonté | a32d275 | 2012-05-29 23:27:41 +0200 | [diff] [blame] | 3901 | http_find_header2(s->be->fwdfor_hdr_len ? s->be->fwdfor_hdr_name : s->fe->fwdfor_hdr_name, |
| 3902 | 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] | 3903 | req->buf->p, &txn->hdr_idx, &ctx)) { |
Willy Tarreau | 87cf514 | 2011-08-19 22:57:24 +0200 | [diff] [blame] | 3904 | /* The header is set to be added only if none is present |
| 3905 | * and we found it, so don't do anything. |
| 3906 | */ |
| 3907 | } |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 3908 | else if (cli_conn && cli_conn->addr.from.ss_family == AF_INET) { |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3909 | /* Add an X-Forwarded-For header unless the source IP is |
| 3910 | * in the 'except' network range. |
| 3911 | */ |
| 3912 | if ((!s->fe->except_mask.s_addr || |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 3913 | (((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] | 3914 | != s->fe->except_net.s_addr) && |
| 3915 | (!s->be->except_mask.s_addr || |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 3916 | (((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] | 3917 | != s->be->except_net.s_addr)) { |
Willy Tarreau | 2a32428 | 2006-12-05 00:05:46 +0100 | [diff] [blame] | 3918 | int len; |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3919 | unsigned char *pn; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 3920 | pn = (unsigned char *)&((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr; |
Ross West | af72a1d | 2008-08-03 10:51:45 +0200 | [diff] [blame] | 3921 | |
| 3922 | /* Note: we rely on the backend to get the header name to be used for |
| 3923 | * x-forwarded-for, because the header is really meant for the backends. |
| 3924 | * However, if the backend did not specify any option, we have to rely |
| 3925 | * on the frontend's header name. |
| 3926 | */ |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3927 | if (s->be->fwdfor_hdr_len) { |
| 3928 | len = s->be->fwdfor_hdr_len; |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 3929 | memcpy(trash.str, s->be->fwdfor_hdr_name, len); |
Ross West | af72a1d | 2008-08-03 10:51:45 +0200 | [diff] [blame] | 3930 | } else { |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3931 | len = s->fe->fwdfor_hdr_len; |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 3932 | memcpy(trash.str, s->fe->fwdfor_hdr_name, len); |
Willy Tarreau | b86db34 | 2009-11-30 11:50:16 +0100 | [diff] [blame] | 3933 | } |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 3934 | len += sprintf(trash.str + len, ": %d.%d.%d.%d", pn[0], pn[1], pn[2], pn[3]); |
Willy Tarreau | edcf668 | 2008-11-30 23:15:34 +0100 | [diff] [blame] | 3935 | |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 3936 | 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] | 3937 | goto return_bad_req; |
Willy Tarreau | 2a32428 | 2006-12-05 00:05:46 +0100 | [diff] [blame] | 3938 | } |
| 3939 | } |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 3940 | else if (cli_conn && cli_conn->addr.from.ss_family == AF_INET6) { |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3941 | /* FIXME: for the sake of completeness, we should also support |
| 3942 | * 'except' here, although it is mostly useless in this case. |
matt.farnsworth@nokia.com | 1c2ab96 | 2008-04-14 20:47:37 +0200 | [diff] [blame] | 3943 | */ |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3944 | int len; |
| 3945 | char pn[INET6_ADDRSTRLEN]; |
| 3946 | inet_ntop(AF_INET6, |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 3947 | (const void *)&((struct sockaddr_in6 *)(&cli_conn->addr.from))->sin6_addr, |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3948 | pn, sizeof(pn)); |
matt.farnsworth@nokia.com | 1c2ab96 | 2008-04-14 20:47:37 +0200 | [diff] [blame] | 3949 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3950 | /* Note: we rely on the backend to get the header name to be used for |
| 3951 | * x-forwarded-for, because the header is really meant for the backends. |
| 3952 | * However, if the backend did not specify any option, we have to rely |
| 3953 | * on the frontend's header name. |
matt.farnsworth@nokia.com | 1c2ab96 | 2008-04-14 20:47:37 +0200 | [diff] [blame] | 3954 | */ |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3955 | if (s->be->fwdfor_hdr_len) { |
| 3956 | len = s->be->fwdfor_hdr_len; |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 3957 | memcpy(trash.str, s->be->fwdfor_hdr_name, len); |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3958 | } else { |
| 3959 | len = s->fe->fwdfor_hdr_len; |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 3960 | memcpy(trash.str, s->fe->fwdfor_hdr_name, len); |
matt.farnsworth@nokia.com | 1c2ab96 | 2008-04-14 20:47:37 +0200 | [diff] [blame] | 3961 | } |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 3962 | len += sprintf(trash.str + len, ": %s", pn); |
Willy Tarreau | adfb856 | 2008-08-11 15:24:42 +0200 | [diff] [blame] | 3963 | |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 3964 | 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] | 3965 | goto return_bad_req; |
| 3966 | } |
| 3967 | } |
| 3968 | |
| 3969 | /* |
Maik Broemme | 2850cb4 | 2009-04-17 18:53:21 +0200 | [diff] [blame] | 3970 | * 10: add X-Original-To if either the frontend or the backend |
| 3971 | * asks for it. |
| 3972 | */ |
| 3973 | if ((s->fe->options | s->be->options) & PR_O_ORGTO) { |
| 3974 | |
| 3975 | /* FIXME: don't know if IPv6 can handle that case too. */ |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 3976 | if (cli_conn && cli_conn->addr.from.ss_family == AF_INET) { |
Maik Broemme | 2850cb4 | 2009-04-17 18:53:21 +0200 | [diff] [blame] | 3977 | /* Add an X-Original-To header unless the destination IP is |
| 3978 | * in the 'except' network range. |
| 3979 | */ |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 3980 | conn_get_to_addr(cli_conn); |
Maik Broemme | 2850cb4 | 2009-04-17 18:53:21 +0200 | [diff] [blame] | 3981 | |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 3982 | if (cli_conn->addr.to.ss_family == AF_INET && |
Emeric Brun | 5bd86a8 | 2010-10-22 17:23:04 +0200 | [diff] [blame] | 3983 | ((!s->fe->except_mask_to.s_addr || |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 3984 | (((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] | 3985 | != s->fe->except_to.s_addr) && |
| 3986 | (!s->be->except_mask_to.s_addr || |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 3987 | (((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] | 3988 | != s->be->except_to.s_addr))) { |
Maik Broemme | 2850cb4 | 2009-04-17 18:53:21 +0200 | [diff] [blame] | 3989 | int len; |
| 3990 | unsigned char *pn; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 3991 | pn = (unsigned char *)&((struct sockaddr_in *)&cli_conn->addr.to)->sin_addr; |
Maik Broemme | 2850cb4 | 2009-04-17 18:53:21 +0200 | [diff] [blame] | 3992 | |
| 3993 | /* Note: we rely on the backend to get the header name to be used for |
| 3994 | * x-original-to, because the header is really meant for the backends. |
| 3995 | * However, if the backend did not specify any option, we have to rely |
| 3996 | * on the frontend's header name. |
| 3997 | */ |
| 3998 | if (s->be->orgto_hdr_len) { |
| 3999 | len = s->be->orgto_hdr_len; |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 4000 | memcpy(trash.str, s->be->orgto_hdr_name, len); |
Maik Broemme | 2850cb4 | 2009-04-17 18:53:21 +0200 | [diff] [blame] | 4001 | } else { |
| 4002 | len = s->fe->orgto_hdr_len; |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 4003 | memcpy(trash.str, s->fe->orgto_hdr_name, len); |
Willy Tarreau | b86db34 | 2009-11-30 11:50:16 +0100 | [diff] [blame] | 4004 | } |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 4005 | len += sprintf(trash.str + len, ": %d.%d.%d.%d", pn[0], pn[1], pn[2], pn[3]); |
Maik Broemme | 2850cb4 | 2009-04-17 18:53:21 +0200 | [diff] [blame] | 4006 | |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 4007 | 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] | 4008 | goto return_bad_req; |
| 4009 | } |
| 4010 | } |
| 4011 | } |
| 4012 | |
Willy Tarreau | 50fc777 | 2012-11-11 22:19:57 +0100 | [diff] [blame] | 4013 | /* 11: add "Connection: close" or "Connection: keep-alive" if needed and not yet set. |
| 4014 | * If an "Upgrade" token is found, the header is left untouched in order not to have |
| 4015 | * to deal with some servers bugs : some of them fail an Upgrade if anything but |
| 4016 | * "Upgrade" is present in the Connection header. |
| 4017 | */ |
| 4018 | if (!(txn->flags & TX_HDR_CONN_UPG) && |
| 4019 | (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) || |
Willy Tarreau | 02bce8b | 2014-01-30 00:15:28 +0100 | [diff] [blame] | 4020 | ((s->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL || |
| 4021 | (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL))) { |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 4022 | unsigned int want_flags = 0; |
| 4023 | |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 4024 | if (msg->flags & HTTP_MSGF_VER_11) { |
Willy Tarreau | 22a9534 | 2010-09-29 14:31:41 +0200 | [diff] [blame] | 4025 | if (((txn->flags & TX_CON_WANT_MSK) >= TX_CON_WANT_SCL || |
Willy Tarreau | 02bce8b | 2014-01-30 00:15:28 +0100 | [diff] [blame] | 4026 | ((s->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL || |
| 4027 | (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL)) && |
Willy Tarreau | 22a9534 | 2010-09-29 14:31:41 +0200 | [diff] [blame] | 4028 | !((s->fe->options2|s->be->options2) & PR_O2_FAKE_KA)) |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 4029 | want_flags |= TX_CON_CLO_SET; |
| 4030 | } else { |
Willy Tarreau | 22a9534 | 2010-09-29 14:31:41 +0200 | [diff] [blame] | 4031 | if (((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL && |
Willy Tarreau | 02bce8b | 2014-01-30 00:15:28 +0100 | [diff] [blame] | 4032 | ((s->fe->options & PR_O_HTTP_MODE) != PR_O_HTTP_PCL && |
| 4033 | (s->be->options & PR_O_HTTP_MODE) != PR_O_HTTP_PCL)) || |
Willy Tarreau | 22a9534 | 2010-09-29 14:31:41 +0200 | [diff] [blame] | 4034 | ((s->fe->options2|s->be->options2) & PR_O2_FAKE_KA)) |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 4035 | want_flags |= TX_CON_KAL_SET; |
| 4036 | } |
| 4037 | |
| 4038 | 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] | 4039 | http_change_connection_header(txn, msg, want_flags); |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4040 | } |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 4041 | |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 4042 | |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 4043 | /* If we have no server assigned yet and we're balancing on url_param |
| 4044 | * with a POST request, we may be interested in checking the body for |
| 4045 | * that parameter. This will be done in another analyser. |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4046 | */ |
| 4047 | if (!(s->flags & (SN_ASSIGNED|SN_DIRECT)) && |
| 4048 | s->txn.meth == HTTP_METH_POST && s->be->url_param_name != NULL && |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 4049 | s->be->url_param_post_limit != 0 && |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 4050 | (msg->flags & (HTTP_MSGF_CNT_LEN|HTTP_MSGF_TE_CHNK))) { |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 4051 | channel_dont_connect(req); |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 4052 | req->analysers |= AN_REQ_HTTP_BODY; |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4053 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 4054 | |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 4055 | if (msg->flags & HTTP_MSGF_XFER_LEN) { |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4056 | req->analysers |= AN_REQ_HTTP_XFER_BODY; |
Willy Tarreau | 5e20552 | 2011-12-17 16:34:27 +0100 | [diff] [blame] | 4057 | #ifdef TCP_QUICKACK |
| 4058 | /* We expect some data from the client. Unless we know for sure |
| 4059 | * we already have a full request, we have to re-enable quick-ack |
| 4060 | * in case we previously disabled it, otherwise we might cause |
| 4061 | * the client to delay further data. |
| 4062 | */ |
| 4063 | if ((s->listener->options & LI_O_NOQUICKACK) && |
Willy Tarreau | 3c72872 | 2014-01-23 13:50:42 +0100 | [diff] [blame] | 4064 | cli_conn && conn_ctrl_ready(cli_conn) && |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 4065 | ((msg->flags & HTTP_MSGF_TE_CHNK) || |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 4066 | (msg->body_len > req->buf->i - txn->req.eoh - 2))) |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 4067 | 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] | 4068 | #endif |
| 4069 | } |
Willy Tarreau | 0394594 | 2009-12-22 16:50:27 +0100 | [diff] [blame] | 4070 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4071 | /************************************************************* |
| 4072 | * OK, that's finished for the headers. We have done what we * |
| 4073 | * could. Let's switch to the DATA state. * |
| 4074 | ************************************************************/ |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 4075 | req->analyse_exp = TICK_ETERNITY; |
| 4076 | req->analysers &= ~an_bit; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 4077 | |
Willy Tarreau | 7bb68ab | 2012-05-13 14:48:59 +0200 | [diff] [blame] | 4078 | /* if the server closes the connection, we want to immediately react |
| 4079 | * and close the socket to save packets and syscalls. |
| 4080 | */ |
Willy Tarreau | 40f151a | 2012-12-20 12:10:09 +0100 | [diff] [blame] | 4081 | if (!(req->analysers & AN_REQ_HTTP_XFER_BODY)) |
| 4082 | req->cons->flags |= SI_FL_NOHALF; |
Willy Tarreau | 7bb68ab | 2012-05-13 14:48:59 +0200 | [diff] [blame] | 4083 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4084 | s->logs.tv_request = now; |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4085 | /* OK let's go on with the BODY now */ |
| 4086 | return 1; |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 4087 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4088 | return_bad_req: /* let's centralize all bad requests */ |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 4089 | if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) { |
Willy Tarreau | f073a83 | 2009-03-01 23:21:47 +0100 | [diff] [blame] | 4090 | /* we detected a parsing error. We want to archive this request |
| 4091 | * in the dedicated proxy area for later troubleshooting. |
| 4092 | */ |
Willy Tarreau | 8a0cef2 | 2012-03-09 13:39:23 +0100 | [diff] [blame] | 4093 | 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] | 4094 | } |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 4095 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4096 | txn->req.msg_state = HTTP_MSG_ERROR; |
| 4097 | txn->status = 400; |
| 4098 | req->analysers = 0; |
Willy Tarreau | 783f258 | 2012-09-04 12:19:04 +0200 | [diff] [blame] | 4099 | 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] | 4100 | |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 4101 | s->fe->fe_counters.failed_req++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 4102 | if (s->listener->counters) |
| 4103 | s->listener->counters->failed_req++; |
Willy Tarreau | adfb856 | 2008-08-11 15:24:42 +0200 | [diff] [blame] | 4104 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4105 | if (!(s->flags & SN_ERR_MASK)) |
| 4106 | s->flags |= SN_ERR_PRXCOND; |
| 4107 | if (!(s->flags & SN_FINST_MASK)) |
| 4108 | s->flags |= SN_FINST_R; |
Willy Tarreau | dafde43 | 2008-08-17 01:00:46 +0200 | [diff] [blame] | 4109 | return 0; |
Willy Tarreau | c65a3ba | 2008-08-11 23:42:50 +0200 | [diff] [blame] | 4110 | } |
Willy Tarreau | adfb856 | 2008-08-11 15:24:42 +0200 | [diff] [blame] | 4111 | |
Willy Tarreau | 60b85b0 | 2008-11-30 23:28:40 +0100 | [diff] [blame] | 4112 | /* This function is an analyser which processes the HTTP tarpit. It always |
| 4113 | * returns zero, at the beginning because it prevents any other processing |
| 4114 | * from occurring, and at the end because it terminates the request. |
| 4115 | */ |
Willy Tarreau | 7421efb | 2012-07-02 15:11:27 +0200 | [diff] [blame] | 4116 | 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] | 4117 | { |
| 4118 | struct http_txn *txn = &s->txn; |
| 4119 | |
| 4120 | /* This connection is being tarpitted. The CLIENT side has |
| 4121 | * already set the connect expiration date to the right |
| 4122 | * timeout. We just have to check that the client is still |
| 4123 | * there and that the timeout has not expired. |
| 4124 | */ |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 4125 | channel_dont_connect(req); |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 4126 | if ((req->flags & (CF_SHUTR|CF_READ_ERROR)) == 0 && |
Willy Tarreau | 60b85b0 | 2008-11-30 23:28:40 +0100 | [diff] [blame] | 4127 | !tick_is_expired(req->analyse_exp, now_ms)) |
| 4128 | return 0; |
| 4129 | |
| 4130 | /* We will set the queue timer to the time spent, just for |
| 4131 | * logging purposes. We fake a 500 server error, so that the |
| 4132 | * attacker will not suspect his connection has been tarpitted. |
| 4133 | * It will not cause trouble to the logs because we can exclude |
| 4134 | * the tarpitted connections by filtering on the 'PT' status flags. |
| 4135 | */ |
Willy Tarreau | 60b85b0 | 2008-11-30 23:28:40 +0100 | [diff] [blame] | 4136 | s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now); |
| 4137 | |
| 4138 | txn->status = 500; |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 4139 | if (!(req->flags & CF_READ_ERROR)) |
Willy Tarreau | 783f258 | 2012-09-04 12:19:04 +0200 | [diff] [blame] | 4140 | stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_500)); |
Willy Tarreau | 60b85b0 | 2008-11-30 23:28:40 +0100 | [diff] [blame] | 4141 | |
| 4142 | req->analysers = 0; |
| 4143 | req->analyse_exp = TICK_ETERNITY; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 4144 | |
Willy Tarreau | 60b85b0 | 2008-11-30 23:28:40 +0100 | [diff] [blame] | 4145 | if (!(s->flags & SN_ERR_MASK)) |
| 4146 | s->flags |= SN_ERR_PRXCOND; |
| 4147 | if (!(s->flags & SN_FINST_MASK)) |
| 4148 | s->flags |= SN_FINST_T; |
| 4149 | return 0; |
| 4150 | } |
| 4151 | |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 4152 | /* This function is an analyser which processes the HTTP request body. It looks |
| 4153 | * for parameters to be used for the load balancing algorithm (url_param). It |
| 4154 | * must only be called after the standard HTTP request processing has occurred, |
| 4155 | * because it expects the request to be parsed. It returns zero if it needs to |
| 4156 | * read more data, or 1 once it has completed its analysis. |
| 4157 | */ |
Willy Tarreau | 7421efb | 2012-07-02 15:11:27 +0200 | [diff] [blame] | 4158 | int http_process_request_body(struct session *s, struct channel *req, int an_bit) |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 4159 | { |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 4160 | struct http_txn *txn = &s->txn; |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 4161 | struct http_msg *msg = &s->txn.req; |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 4162 | long long limit = s->be->url_param_post_limit; |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 4163 | |
| 4164 | /* We have to parse the HTTP request body to find any required data. |
| 4165 | * "balance url_param check_post" should have been the only way to get |
| 4166 | * into this. We were brought here after HTTP header analysis, so all |
| 4167 | * related structures are ready. |
| 4168 | */ |
| 4169 | |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 4170 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) |
| 4171 | goto missing_data; |
| 4172 | |
| 4173 | if (msg->msg_state < HTTP_MSG_100_SENT) { |
| 4174 | /* If we have HTTP/1.1 and Expect: 100-continue, then we must |
| 4175 | * send an HTTP/1.1 100 Continue intermediate response. |
| 4176 | */ |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 4177 | if (msg->flags & HTTP_MSGF_VER_11) { |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 4178 | struct hdr_ctx ctx; |
| 4179 | ctx.idx = 0; |
| 4180 | /* Expect is allowed in 1.1, look for it */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 4181 | 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] | 4182 | 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] | 4183 | bo_inject(s->rep, http_100_chunk.str, http_100_chunk.len); |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 4184 | } |
| 4185 | } |
| 4186 | msg->msg_state = HTTP_MSG_100_SENT; |
| 4187 | } |
| 4188 | |
| 4189 | if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) { |
Willy Tarreau | fa4a03c | 2012-03-09 21:28:54 +0100 | [diff] [blame] | 4190 | /* 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] | 4191 | * 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] | 4192 | * is still null. We must save the body in msg->next because it |
| 4193 | * survives buffer re-alignments. |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4194 | */ |
Willy Tarreau | ea1175a | 2012-03-05 15:52:30 +0100 | [diff] [blame] | 4195 | msg->next = msg->sov; |
Willy Tarreau | a458b67 | 2012-03-05 11:17:50 +0100 | [diff] [blame] | 4196 | |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 4197 | if (msg->flags & HTTP_MSGF_TE_CHNK) |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 4198 | msg->msg_state = HTTP_MSG_CHUNK_SIZE; |
| 4199 | else |
| 4200 | msg->msg_state = HTTP_MSG_DATA; |
| 4201 | } |
| 4202 | |
| 4203 | if (msg->msg_state == HTTP_MSG_CHUNK_SIZE) { |
Willy Tarreau | 124d991 | 2011-03-01 20:30:48 +0100 | [diff] [blame] | 4204 | /* read the chunk size and assign it to ->chunk_len, then |
Willy Tarreau | a458b67 | 2012-03-05 11:17:50 +0100 | [diff] [blame] | 4205 | * 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] | 4206 | * TRAILERS state. |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 4207 | */ |
Willy Tarreau | 4baf44b | 2012-03-09 14:10:20 +0100 | [diff] [blame] | 4208 | int ret = http_parse_chunk_size(msg); |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 4209 | |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 4210 | if (!ret) |
| 4211 | goto missing_data; |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 4212 | else if (ret < 0) { |
| 4213 | session_inc_http_err_ctr(s); |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 4214 | goto return_bad_req; |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 4215 | } |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 4216 | } |
| 4217 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4218 | /* Now we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state. |
Willy Tarreau | fa4a03c | 2012-03-09 21:28:54 +0100 | [diff] [blame] | 4219 | * We have the first data byte is in msg->sov. We're waiting for at |
| 4220 | * least <url_param_post_limit> bytes after msg->sov. |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 4221 | */ |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 4222 | |
Willy Tarreau | 124d991 | 2011-03-01 20:30:48 +0100 | [diff] [blame] | 4223 | if (msg->body_len < limit) |
| 4224 | limit = msg->body_len; |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 4225 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 4226 | if (req->buf->i - msg->sov >= limit) /* we have enough bytes now */ |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 4227 | goto http_end; |
| 4228 | |
| 4229 | missing_data: |
| 4230 | /* we get here if we need to wait for more data */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 4231 | if (buffer_full(req->buf, global.tune.maxrewrite)) { |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 4232 | session_inc_http_err_ctr(s); |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 4233 | goto return_bad_req; |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 4234 | } |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 4235 | |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 4236 | 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] | 4237 | txn->status = 408; |
Willy Tarreau | 783f258 | 2012-09-04 12:19:04 +0200 | [diff] [blame] | 4238 | stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_408)); |
Willy Tarreau | 79ebac6 | 2010-06-07 13:47:49 +0200 | [diff] [blame] | 4239 | |
| 4240 | if (!(s->flags & SN_ERR_MASK)) |
| 4241 | s->flags |= SN_ERR_CLITO; |
| 4242 | if (!(s->flags & SN_FINST_MASK)) |
| 4243 | s->flags |= SN_FINST_D; |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 4244 | goto return_err_msg; |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 4245 | } |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 4246 | |
| 4247 | /* we get here if we need to wait for more data */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 4248 | if (!(req->flags & (CF_SHUTR | CF_READ_ERROR)) && !buffer_full(req->buf, global.tune.maxrewrite)) { |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 4249 | /* Not enough data. We'll re-use the http-request |
| 4250 | * timeout here. Ideally, we should set the timeout |
| 4251 | * relative to the accept() date. We just set the |
| 4252 | * request timeout once at the beginning of the |
| 4253 | * request. |
| 4254 | */ |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 4255 | channel_dont_connect(req); |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 4256 | if (!tick_isset(req->analyse_exp)) |
Willy Tarreau | cd7afc0 | 2009-07-12 10:03:17 +0200 | [diff] [blame] | 4257 | req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.httpreq); |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 4258 | return 0; |
| 4259 | } |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 4260 | |
| 4261 | http_end: |
| 4262 | /* The situation will not evolve, so let's give up on the analysis. */ |
| 4263 | s->logs.tv_request = now; /* update the request timer to reflect full request */ |
| 4264 | req->analysers &= ~an_bit; |
| 4265 | req->analyse_exp = TICK_ETERNITY; |
| 4266 | return 1; |
| 4267 | |
| 4268 | return_bad_req: /* let's centralize all bad requests */ |
| 4269 | txn->req.msg_state = HTTP_MSG_ERROR; |
| 4270 | txn->status = 400; |
Willy Tarreau | 783f258 | 2012-09-04 12:19:04 +0200 | [diff] [blame] | 4271 | stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_400)); |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 4272 | |
Willy Tarreau | 79ebac6 | 2010-06-07 13:47:49 +0200 | [diff] [blame] | 4273 | if (!(s->flags & SN_ERR_MASK)) |
| 4274 | s->flags |= SN_ERR_PRXCOND; |
| 4275 | if (!(s->flags & SN_FINST_MASK)) |
| 4276 | s->flags |= SN_FINST_R; |
| 4277 | |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 4278 | return_err_msg: |
| 4279 | req->analysers = 0; |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 4280 | s->fe->fe_counters.failed_req++; |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 4281 | if (s->listener->counters) |
| 4282 | s->listener->counters->failed_req++; |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 4283 | return 0; |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 4284 | } |
| 4285 | |
Willy Tarreau | d1de8af | 2012-05-18 22:12:14 +0200 | [diff] [blame] | 4286 | /* send a server's name with an outgoing request over an established connection. |
| 4287 | * Note: this function is designed to be called once the request has been scheduled |
| 4288 | * for being forwarded. This is the reason why it rewinds the buffer before |
| 4289 | * proceeding. |
| 4290 | */ |
Willy Tarreau | 45c0d98 | 2012-03-09 12:11:57 +0100 | [diff] [blame] | 4291 | 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] | 4292 | |
| 4293 | struct hdr_ctx ctx; |
| 4294 | |
Mark Lamourine | c2247f0 | 2012-01-04 13:02:01 -0500 | [diff] [blame] | 4295 | char *hdr_name = be->server_id_hdr_name; |
| 4296 | int hdr_name_len = be->server_id_hdr_len; |
Willy Tarreau | 394db37 | 2012-10-12 22:40:39 +0200 | [diff] [blame] | 4297 | struct channel *chn = txn->req.chn; |
Mark Lamourine | c2247f0 | 2012-01-04 13:02:01 -0500 | [diff] [blame] | 4298 | char *hdr_val; |
Willy Tarreau | d1de8af | 2012-05-18 22:12:14 +0200 | [diff] [blame] | 4299 | unsigned int old_o, old_i; |
Mark Lamourine | c2247f0 | 2012-01-04 13:02:01 -0500 | [diff] [blame] | 4300 | |
William Lallemand | d9e9066 | 2012-01-30 17:27:17 +0100 | [diff] [blame] | 4301 | ctx.idx = 0; |
| 4302 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 4303 | old_o = chn->buf->o; |
Willy Tarreau | d1de8af | 2012-05-18 22:12:14 +0200 | [diff] [blame] | 4304 | if (old_o) { |
| 4305 | /* The request was already skipped, let's restore it */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 4306 | b_rew(chn->buf, old_o); |
Willy Tarreau | d1de8af | 2012-05-18 22:12:14 +0200 | [diff] [blame] | 4307 | } |
| 4308 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 4309 | old_i = chn->buf->i; |
| 4310 | 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] | 4311 | /* remove any existing values from the header */ |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 4312 | http_remove_header2(&txn->req, &txn->hdr_idx, &ctx); |
Mark Lamourine | c2247f0 | 2012-01-04 13:02:01 -0500 | [diff] [blame] | 4313 | } |
| 4314 | |
| 4315 | /* Add the new header requested with the server value */ |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 4316 | hdr_val = trash.str; |
Mark Lamourine | c2247f0 | 2012-01-04 13:02:01 -0500 | [diff] [blame] | 4317 | memcpy(hdr_val, hdr_name, hdr_name_len); |
| 4318 | hdr_val += hdr_name_len; |
| 4319 | *hdr_val++ = ':'; |
| 4320 | *hdr_val++ = ' '; |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 4321 | hdr_val += strlcpy2(hdr_val, srv_name, trash.str + trash.size - hdr_val); |
| 4322 | 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] | 4323 | |
Willy Tarreau | d1de8af | 2012-05-18 22:12:14 +0200 | [diff] [blame] | 4324 | if (old_o) { |
| 4325 | /* If this was a forwarded request, we must readjust the amount of |
| 4326 | * data to be forwarded in order to take into account the size |
Willy Tarreau | 2fef9b1 | 2013-03-26 01:08:21 +0100 | [diff] [blame] | 4327 | * variations. Note that if the request was already scheduled for |
| 4328 | * forwarding, it had its req->sol pointing to the body, which |
| 4329 | * must then be updated too. |
Willy Tarreau | d1de8af | 2012-05-18 22:12:14 +0200 | [diff] [blame] | 4330 | */ |
Willy Tarreau | 2fef9b1 | 2013-03-26 01:08:21 +0100 | [diff] [blame] | 4331 | txn->req.sol += chn->buf->i - old_i; |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 4332 | b_adv(chn->buf, old_o + chn->buf->i - old_i); |
Willy Tarreau | d1de8af | 2012-05-18 22:12:14 +0200 | [diff] [blame] | 4333 | } |
| 4334 | |
Mark Lamourine | c2247f0 | 2012-01-04 13:02:01 -0500 | [diff] [blame] | 4335 | return 0; |
| 4336 | } |
| 4337 | |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4338 | /* Terminate current transaction and prepare a new one. This is very tricky |
| 4339 | * right now but it works. |
| 4340 | */ |
| 4341 | void http_end_txn_clean_session(struct session *s) |
| 4342 | { |
Willy Tarreau | 068621e | 2013-12-23 15:11:25 +0100 | [diff] [blame] | 4343 | int prev_status = s->txn.status; |
| 4344 | |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4345 | /* FIXME: We need a more portable way of releasing a backend's and a |
| 4346 | * server's connections. We need a safer way to reinitialize buffer |
| 4347 | * flags. We also need a more accurate method for computing per-request |
| 4348 | * data. |
| 4349 | */ |
| 4350 | http_silent_debug(__LINE__, s); |
| 4351 | |
Willy Tarreau | 4213a11 | 2013-12-15 10:25:42 +0100 | [diff] [blame] | 4352 | /* unless we're doing keep-alive, we want to quickly close the connection |
| 4353 | * to the server. |
| 4354 | */ |
| 4355 | if (((s->txn.flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) || |
| 4356 | !si_conn_ready(s->req->cons)) { |
| 4357 | s->req->cons->flags |= SI_FL_NOLINGER | SI_FL_NOHALF; |
| 4358 | si_shutr(s->req->cons); |
| 4359 | si_shutw(s->req->cons); |
| 4360 | } |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4361 | |
| 4362 | http_silent_debug(__LINE__, s); |
| 4363 | |
Willy Tarreau | 2d5cd47 | 2012-03-01 23:34:37 +0100 | [diff] [blame] | 4364 | if (s->flags & SN_BE_ASSIGNED) { |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4365 | s->be->beconn--; |
Willy Tarreau | 2d5cd47 | 2012-03-01 23:34:37 +0100 | [diff] [blame] | 4366 | if (unlikely(s->srv_conn)) |
| 4367 | sess_change_server(s, NULL); |
| 4368 | } |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4369 | |
| 4370 | s->logs.t_close = tv_ms_elapsed(&s->logs.tv_accept, &now); |
| 4371 | session_process_counters(s); |
Willy Tarreau | f333834 | 2014-01-28 21:40:28 +0100 | [diff] [blame] | 4372 | session_stop_content_counters(s); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4373 | |
| 4374 | if (s->txn.status) { |
| 4375 | int n; |
| 4376 | |
| 4377 | n = s->txn.status / 100; |
| 4378 | if (n < 1 || n > 5) |
| 4379 | n = 0; |
| 4380 | |
Willy Tarreau | 5e16cbc | 2012-11-24 14:54:13 +0100 | [diff] [blame] | 4381 | if (s->fe->mode == PR_MODE_HTTP) { |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 4382 | s->fe->fe_counters.p.http.rsp[n]++; |
Willy Tarreau | 8139b99 | 2012-11-27 07:35:31 +0100 | [diff] [blame] | 4383 | if (s->comp_algo && (s->flags & SN_COMP_READY)) |
Willy Tarreau | 5e16cbc | 2012-11-24 14:54:13 +0100 | [diff] [blame] | 4384 | s->fe->fe_counters.p.http.comp_rsp++; |
| 4385 | } |
Willy Tarreau | 2465779 | 2010-02-26 10:30:28 +0100 | [diff] [blame] | 4386 | if ((s->flags & SN_BE_ASSIGNED) && |
Willy Tarreau | 5e16cbc | 2012-11-24 14:54:13 +0100 | [diff] [blame] | 4387 | (s->be->mode == PR_MODE_HTTP)) { |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 4388 | s->be->be_counters.p.http.rsp[n]++; |
Willy Tarreau | 5e16cbc | 2012-11-24 14:54:13 +0100 | [diff] [blame] | 4389 | s->be->be_counters.p.http.cum_req++; |
Willy Tarreau | 8139b99 | 2012-11-27 07:35:31 +0100 | [diff] [blame] | 4390 | if (s->comp_algo && (s->flags & SN_COMP_READY)) |
Willy Tarreau | 5e16cbc | 2012-11-24 14:54:13 +0100 | [diff] [blame] | 4391 | s->be->be_counters.p.http.comp_rsp++; |
| 4392 | } |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4393 | } |
| 4394 | |
| 4395 | /* don't count other requests' data */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 4396 | s->logs.bytes_in -= s->req->buf->i; |
| 4397 | s->logs.bytes_out -= s->rep->buf->i; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4398 | |
| 4399 | /* let's do a final log if we need it */ |
Willy Tarreau | d79a3b2 | 2012-12-28 09:40:16 +0100 | [diff] [blame] | 4400 | if (!LIST_ISEMPTY(&s->fe->logformat) && s->logs.logwait && |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4401 | !(s->flags & SN_MONITOR) && |
| 4402 | (!(s->fe->options & PR_O_NULLNOLOG) || s->req->total)) { |
| 4403 | s->do_log(s); |
| 4404 | } |
| 4405 | |
| 4406 | s->logs.accept_date = date; /* user-visible date for logging */ |
| 4407 | s->logs.tv_accept = now; /* corrected date for internal use */ |
| 4408 | tv_zero(&s->logs.tv_request); |
| 4409 | s->logs.t_queue = -1; |
| 4410 | s->logs.t_connect = -1; |
| 4411 | s->logs.t_data = -1; |
| 4412 | s->logs.t_close = 0; |
| 4413 | s->logs.prx_queue_size = 0; /* we get the number of pending conns before us */ |
| 4414 | s->logs.srv_queue_size = 0; /* we will get this number soon */ |
| 4415 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 4416 | s->logs.bytes_in = s->req->total = s->req->buf->i; |
| 4417 | s->logs.bytes_out = s->rep->total = s->rep->buf->i; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4418 | |
| 4419 | if (s->pend_pos) |
| 4420 | pendconn_free(s->pend_pos); |
| 4421 | |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 4422 | if (objt_server(s->target)) { |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4423 | if (s->flags & SN_CURR_SESS) { |
| 4424 | s->flags &= ~SN_CURR_SESS; |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 4425 | objt_server(s->target)->cur_sess--; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4426 | } |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 4427 | if (may_dequeue_tasks(objt_server(s->target), s->be)) |
| 4428 | process_srv_queue(objt_server(s->target)); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4429 | } |
| 4430 | |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 4431 | s->target = NULL; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4432 | |
Willy Tarreau | 4213a11 | 2013-12-15 10:25:42 +0100 | [diff] [blame] | 4433 | /* only release our endpoint if we don't intend to reuse the |
| 4434 | * connection. |
| 4435 | */ |
| 4436 | if (((s->txn.flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) || |
| 4437 | !si_conn_ready(s->req->cons)) { |
| 4438 | si_release_endpoint(s->req->cons); |
| 4439 | } |
| 4440 | |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4441 | s->req->cons->state = s->req->cons->prev_state = SI_ST_INI; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4442 | s->req->cons->err_type = SI_ET_NONE; |
Willy Tarreau | 0b3a411 | 2011-03-27 19:16:56 +0200 | [diff] [blame] | 4443 | s->req->cons->conn_retries = 0; /* used for logging too */ |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4444 | s->req->cons->exp = TICK_ETERNITY; |
Willy Tarreau | c920096 | 2013-12-31 23:03:09 +0100 | [diff] [blame] | 4445 | 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] | 4446 | s->req->flags &= ~(CF_SHUTW|CF_SHUTW_NOW|CF_AUTO_CONNECT|CF_WRITE_ERROR|CF_STREAMER|CF_STREAMER_FAST|CF_NEVER_WAIT); |
| 4447 | 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] | 4448 | 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] | 4449 | s->flags &= ~(SN_CURR_SESS|SN_REDIRECTABLE|SN_SRV_REUSED); |
Willy Tarreau | 543db62 | 2012-11-15 16:41:22 +0100 | [diff] [blame] | 4450 | |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4451 | s->txn.meth = 0; |
| 4452 | http_reset_txn(s); |
Willy Tarreau | fcffa69 | 2010-01-10 14:21:19 +0100 | [diff] [blame] | 4453 | s->txn.flags |= TX_NOT_FIRST | TX_WAIT_NEXT_RQ; |
Willy Tarreau | 068621e | 2013-12-23 15:11:25 +0100 | [diff] [blame] | 4454 | |
| 4455 | if (prev_status == 401 || prev_status == 407) { |
| 4456 | /* In HTTP keep-alive mode, if we receive a 401, we still have |
| 4457 | * a chance of being able to send the visitor again to the same |
| 4458 | * server over the same connection. This is required by some |
| 4459 | * broken protocols such as NTLM, and anyway whenever there is |
| 4460 | * an opportunity for sending the challenge to the proper place, |
| 4461 | * it's better to do it (at least it helps with debugging). |
| 4462 | */ |
| 4463 | s->txn.flags |= TX_PREFER_LAST; |
| 4464 | } |
| 4465 | |
Willy Tarreau | ee55dc0 | 2010-06-01 10:56:34 +0200 | [diff] [blame] | 4466 | if (s->fe->options2 & PR_O2_INDEPSTR) |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4467 | s->req->cons->flags |= SI_FL_INDEP_STR; |
| 4468 | |
Willy Tarreau | 96e3121 | 2011-05-30 18:10:30 +0200 | [diff] [blame] | 4469 | if (s->fe->options2 & PR_O2_NODELAY) { |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 4470 | s->req->flags |= CF_NEVER_WAIT; |
| 4471 | s->rep->flags |= CF_NEVER_WAIT; |
Willy Tarreau | 96e3121 | 2011-05-30 18:10:30 +0200 | [diff] [blame] | 4472 | } |
| 4473 | |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4474 | /* if the request buffer is not empty, it means we're |
| 4475 | * about to process another request, so send pending |
| 4476 | * data with MSG_MORE to merge TCP packets when possible. |
Willy Tarreau | 065e833 | 2010-01-08 00:30:20 +0100 | [diff] [blame] | 4477 | * Just don't do this if the buffer is close to be full, |
| 4478 | * because the request will wait for it to flush a little |
| 4479 | * bit before proceeding. |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4480 | */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 4481 | if (s->req->buf->i) { |
| 4482 | if (s->rep->buf->o && |
| 4483 | !buffer_full(s->rep->buf, global.tune.maxrewrite) && |
| 4484 | 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] | 4485 | s->rep->flags |= CF_EXPECT_MORE; |
Willy Tarreau | 065e833 | 2010-01-08 00:30:20 +0100 | [diff] [blame] | 4486 | } |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 4487 | |
| 4488 | /* we're removing the analysers, we MUST re-enable events detection */ |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 4489 | channel_auto_read(s->req); |
| 4490 | channel_auto_close(s->req); |
| 4491 | channel_auto_read(s->rep); |
| 4492 | channel_auto_close(s->rep); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4493 | |
Willy Tarreau | 2737562 | 2013-12-17 00:00:28 +0100 | [diff] [blame] | 4494 | /* we're in keep-alive with an idle connection, monitor it */ |
| 4495 | si_idle_conn(s->req->cons); |
| 4496 | |
Willy Tarreau | 342b11c | 2010-11-24 16:22:09 +0100 | [diff] [blame] | 4497 | s->req->analysers = s->listener->analysers; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4498 | s->rep->analysers = 0; |
| 4499 | |
| 4500 | http_silent_debug(__LINE__, s); |
| 4501 | } |
| 4502 | |
| 4503 | |
| 4504 | /* This function updates the request state machine according to the response |
| 4505 | * state machine and buffer flags. It returns 1 if it changes anything (flag |
| 4506 | * or state), otherwise zero. It ignores any state before HTTP_MSG_DONE, as |
| 4507 | * it is only used to find when a request/response couple is complete. Both |
| 4508 | * this function and its equivalent should loop until both return zero. It |
| 4509 | * can set its own state to DONE, CLOSING, CLOSED, TUNNEL, ERROR. |
| 4510 | */ |
| 4511 | int http_sync_req_state(struct session *s) |
| 4512 | { |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 4513 | struct channel *chn = s->req; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4514 | struct http_txn *txn = &s->txn; |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 4515 | unsigned int old_flags = chn->flags; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4516 | unsigned int old_state = txn->req.msg_state; |
| 4517 | |
| 4518 | http_silent_debug(__LINE__, s); |
| 4519 | if (unlikely(txn->req.msg_state < HTTP_MSG_BODY)) |
| 4520 | return 0; |
| 4521 | |
| 4522 | if (txn->req.msg_state == HTTP_MSG_DONE) { |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 4523 | /* No need to read anymore, the request was completely parsed. |
Willy Tarreau | 58bd8fd | 2010-09-28 14:16:41 +0200 | [diff] [blame] | 4524 | * We can shut the read side unless we want to abort_on_close, |
| 4525 | * or we have a POST request. The issue with POST requests is |
| 4526 | * that some browsers still send a CRLF after the request, and |
| 4527 | * this CRLF must be read so that it does not remain in the kernel |
| 4528 | * buffers, otherwise a close could cause an RST on some systems |
| 4529 | * (eg: Linux). |
Willy Tarreau | 3988d93 | 2013-12-27 23:03:08 +0100 | [diff] [blame] | 4530 | * Note that if we're using keep-alive on the client side, we'd |
| 4531 | * rather poll now and keep the polling enabled for the whole |
| 4532 | * session's life than enabling/disabling it between each |
| 4533 | * response and next request. |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 4534 | */ |
Willy Tarreau | 3988d93 | 2013-12-27 23:03:08 +0100 | [diff] [blame] | 4535 | if (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_SCL) && |
| 4536 | ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) && |
| 4537 | !(s->be->options & PR_O_ABRT_CLOSE) && |
| 4538 | txn->meth != HTTP_METH_POST) |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 4539 | channel_dont_read(chn); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4540 | |
Willy Tarreau | 40f151a | 2012-12-20 12:10:09 +0100 | [diff] [blame] | 4541 | /* if the server closes the connection, we want to immediately react |
| 4542 | * and close the socket to save packets and syscalls. |
| 4543 | */ |
| 4544 | chn->cons->flags |= SI_FL_NOHALF; |
| 4545 | |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4546 | if (txn->rsp.msg_state == HTTP_MSG_ERROR) |
| 4547 | goto wait_other_side; |
| 4548 | |
| 4549 | if (txn->rsp.msg_state < HTTP_MSG_DONE) { |
| 4550 | /* The server has not finished to respond, so we |
| 4551 | * don't want to move in order not to upset it. |
| 4552 | */ |
| 4553 | goto wait_other_side; |
| 4554 | } |
| 4555 | |
| 4556 | if (txn->rsp.msg_state == HTTP_MSG_TUNNEL) { |
| 4557 | /* if any side switches to tunnel mode, the other one does too */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 4558 | channel_auto_read(chn); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4559 | txn->req.msg_state = HTTP_MSG_TUNNEL; |
Willy Tarreau | fc47f91 | 2012-10-20 10:38:09 +0200 | [diff] [blame] | 4560 | chn->flags |= CF_NEVER_WAIT; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4561 | goto wait_other_side; |
| 4562 | } |
| 4563 | |
| 4564 | /* When we get here, it means that both the request and the |
| 4565 | * response have finished receiving. Depending on the connection |
| 4566 | * mode, we'll have to wait for the last bytes to leave in either |
| 4567 | * direction, and sometimes for a close to be effective. |
| 4568 | */ |
| 4569 | |
Willy Tarreau | cce7fa4 | 2010-01-16 23:19:39 +0100 | [diff] [blame] | 4570 | if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) { |
| 4571 | /* Server-close mode : queue a connection close to the server */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 4572 | if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW))) |
| 4573 | channel_shutw_now(chn); |
Willy Tarreau | cce7fa4 | 2010-01-16 23:19:39 +0100 | [diff] [blame] | 4574 | } |
| 4575 | else if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) { |
| 4576 | /* Option forceclose is set, or either side wants to close, |
| 4577 | * let's enforce it now that we're not expecting any new |
| 4578 | * data to come. The caller knows the session is complete |
| 4579 | * once both states are CLOSED. |
| 4580 | */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 4581 | if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW))) { |
| 4582 | channel_shutr_now(chn); |
| 4583 | channel_shutw_now(chn); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4584 | } |
Willy Tarreau | cce7fa4 | 2010-01-16 23:19:39 +0100 | [diff] [blame] | 4585 | } |
| 4586 | else { |
Willy Tarreau | 4213a11 | 2013-12-15 10:25:42 +0100 | [diff] [blame] | 4587 | /* The last possible modes are keep-alive and tunnel. Tunnel mode |
| 4588 | * will not have any analyser so it needs to poll for reads. |
Willy Tarreau | cce7fa4 | 2010-01-16 23:19:39 +0100 | [diff] [blame] | 4589 | */ |
Willy Tarreau | 4213a11 | 2013-12-15 10:25:42 +0100 | [diff] [blame] | 4590 | if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN) { |
| 4591 | channel_auto_read(chn); |
| 4592 | txn->req.msg_state = HTTP_MSG_TUNNEL; |
| 4593 | chn->flags |= CF_NEVER_WAIT; |
| 4594 | } |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4595 | } |
| 4596 | |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 4597 | if (chn->flags & (CF_SHUTW|CF_SHUTW_NOW)) { |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4598 | /* if we've just closed an output, let's switch */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 4599 | chn->cons->flags |= SI_FL_NOLINGER; /* we want to close ASAP */ |
Willy Tarreau | cce7fa4 | 2010-01-16 23:19:39 +0100 | [diff] [blame] | 4600 | |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 4601 | if (!channel_is_empty(chn)) { |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4602 | txn->req.msg_state = HTTP_MSG_CLOSING; |
| 4603 | goto http_msg_closing; |
| 4604 | } |
| 4605 | else { |
| 4606 | txn->req.msg_state = HTTP_MSG_CLOSED; |
| 4607 | goto http_msg_closed; |
| 4608 | } |
| 4609 | } |
Willy Tarreau | cce7fa4 | 2010-01-16 23:19:39 +0100 | [diff] [blame] | 4610 | goto wait_other_side; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4611 | } |
| 4612 | |
| 4613 | if (txn->req.msg_state == HTTP_MSG_CLOSING) { |
| 4614 | http_msg_closing: |
| 4615 | /* nothing else to forward, just waiting for the output buffer |
| 4616 | * to be empty and for the shutw_now to take effect. |
| 4617 | */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 4618 | if (channel_is_empty(chn)) { |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4619 | txn->req.msg_state = HTTP_MSG_CLOSED; |
| 4620 | goto http_msg_closed; |
| 4621 | } |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 4622 | else if (chn->flags & CF_SHUTW) { |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4623 | txn->req.msg_state = HTTP_MSG_ERROR; |
| 4624 | goto wait_other_side; |
| 4625 | } |
| 4626 | } |
| 4627 | |
| 4628 | if (txn->req.msg_state == HTTP_MSG_CLOSED) { |
| 4629 | http_msg_closed: |
Willy Tarreau | 3988d93 | 2013-12-27 23:03:08 +0100 | [diff] [blame] | 4630 | /* see above in MSG_DONE why we only do this in these states */ |
| 4631 | if (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_SCL) && |
| 4632 | ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) && |
| 4633 | !(s->be->options & PR_O_ABRT_CLOSE)) |
Willy Tarreau | 2e7a165 | 2013-12-15 15:32:10 +0100 | [diff] [blame] | 4634 | channel_dont_read(chn); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4635 | goto wait_other_side; |
| 4636 | } |
| 4637 | |
| 4638 | wait_other_side: |
| 4639 | http_silent_debug(__LINE__, s); |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 4640 | return txn->req.msg_state != old_state || chn->flags != old_flags; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4641 | } |
| 4642 | |
| 4643 | |
| 4644 | /* This function updates the response state machine according to the request |
| 4645 | * state machine and buffer flags. It returns 1 if it changes anything (flag |
| 4646 | * or state), otherwise zero. It ignores any state before HTTP_MSG_DONE, as |
| 4647 | * it is only used to find when a request/response couple is complete. Both |
| 4648 | * this function and its equivalent should loop until both return zero. It |
| 4649 | * can set its own state to DONE, CLOSING, CLOSED, TUNNEL, ERROR. |
| 4650 | */ |
| 4651 | int http_sync_res_state(struct session *s) |
| 4652 | { |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 4653 | struct channel *chn = s->rep; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4654 | struct http_txn *txn = &s->txn; |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 4655 | unsigned int old_flags = chn->flags; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4656 | unsigned int old_state = txn->rsp.msg_state; |
| 4657 | |
| 4658 | http_silent_debug(__LINE__, s); |
| 4659 | if (unlikely(txn->rsp.msg_state < HTTP_MSG_BODY)) |
| 4660 | return 0; |
| 4661 | |
| 4662 | if (txn->rsp.msg_state == HTTP_MSG_DONE) { |
| 4663 | /* In theory, we don't need to read anymore, but we must |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 4664 | * still monitor the server connection for a possible close |
| 4665 | * while the request is being uploaded, so we don't disable |
| 4666 | * reading. |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4667 | */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 4668 | /* channel_dont_read(chn); */ |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4669 | |
| 4670 | if (txn->req.msg_state == HTTP_MSG_ERROR) |
| 4671 | goto wait_other_side; |
| 4672 | |
| 4673 | if (txn->req.msg_state < HTTP_MSG_DONE) { |
| 4674 | /* The client seems to still be sending data, probably |
| 4675 | * because we got an error response during an upload. |
| 4676 | * We have the choice of either breaking the connection |
| 4677 | * or letting it pass through. Let's do the later. |
| 4678 | */ |
| 4679 | goto wait_other_side; |
| 4680 | } |
| 4681 | |
| 4682 | if (txn->req.msg_state == HTTP_MSG_TUNNEL) { |
| 4683 | /* if any side switches to tunnel mode, the other one does too */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 4684 | channel_auto_read(chn); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4685 | txn->rsp.msg_state = HTTP_MSG_TUNNEL; |
Willy Tarreau | fc47f91 | 2012-10-20 10:38:09 +0200 | [diff] [blame] | 4686 | chn->flags |= CF_NEVER_WAIT; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4687 | goto wait_other_side; |
| 4688 | } |
| 4689 | |
| 4690 | /* When we get here, it means that both the request and the |
| 4691 | * response have finished receiving. Depending on the connection |
| 4692 | * mode, we'll have to wait for the last bytes to leave in either |
| 4693 | * direction, and sometimes for a close to be effective. |
| 4694 | */ |
| 4695 | |
| 4696 | if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) { |
| 4697 | /* Server-close mode : shut read and wait for the request |
| 4698 | * side to close its output buffer. The caller will detect |
| 4699 | * when we're in DONE and the other is in CLOSED and will |
| 4700 | * catch that for the final cleanup. |
| 4701 | */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 4702 | if (!(chn->flags & (CF_SHUTR|CF_SHUTR_NOW))) |
| 4703 | channel_shutr_now(chn); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4704 | } |
Willy Tarreau | cce7fa4 | 2010-01-16 23:19:39 +0100 | [diff] [blame] | 4705 | else if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) { |
| 4706 | /* Option forceclose is set, or either side wants to close, |
| 4707 | * let's enforce it now that we're not expecting any new |
| 4708 | * data to come. The caller knows the session is complete |
| 4709 | * once both states are CLOSED. |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4710 | */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 4711 | if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW))) { |
| 4712 | channel_shutr_now(chn); |
| 4713 | channel_shutw_now(chn); |
Willy Tarreau | cce7fa4 | 2010-01-16 23:19:39 +0100 | [diff] [blame] | 4714 | } |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4715 | } |
| 4716 | else { |
Willy Tarreau | 4213a11 | 2013-12-15 10:25:42 +0100 | [diff] [blame] | 4717 | /* The last possible modes are keep-alive and tunnel. Tunnel will |
| 4718 | * need to forward remaining data. Keep-alive will need to monitor |
| 4719 | * for connection closing. |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4720 | */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 4721 | channel_auto_read(chn); |
Willy Tarreau | fc47f91 | 2012-10-20 10:38:09 +0200 | [diff] [blame] | 4722 | chn->flags |= CF_NEVER_WAIT; |
Willy Tarreau | 4213a11 | 2013-12-15 10:25:42 +0100 | [diff] [blame] | 4723 | if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN) |
| 4724 | txn->rsp.msg_state = HTTP_MSG_TUNNEL; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4725 | } |
| 4726 | |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 4727 | if (chn->flags & (CF_SHUTW|CF_SHUTW_NOW)) { |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4728 | /* if we've just closed an output, let's switch */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 4729 | if (!channel_is_empty(chn)) { |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4730 | txn->rsp.msg_state = HTTP_MSG_CLOSING; |
| 4731 | goto http_msg_closing; |
| 4732 | } |
| 4733 | else { |
| 4734 | txn->rsp.msg_state = HTTP_MSG_CLOSED; |
| 4735 | goto http_msg_closed; |
| 4736 | } |
| 4737 | } |
| 4738 | goto wait_other_side; |
| 4739 | } |
| 4740 | |
| 4741 | if (txn->rsp.msg_state == HTTP_MSG_CLOSING) { |
| 4742 | http_msg_closing: |
| 4743 | /* nothing else to forward, just waiting for the output buffer |
| 4744 | * to be empty and for the shutw_now to take effect. |
| 4745 | */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 4746 | if (channel_is_empty(chn)) { |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4747 | txn->rsp.msg_state = HTTP_MSG_CLOSED; |
| 4748 | goto http_msg_closed; |
| 4749 | } |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 4750 | else if (chn->flags & CF_SHUTW) { |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4751 | txn->rsp.msg_state = HTTP_MSG_ERROR; |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 4752 | s->be->be_counters.cli_aborts++; |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 4753 | if (objt_server(s->target)) |
| 4754 | objt_server(s->target)->counters.cli_aborts++; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4755 | goto wait_other_side; |
| 4756 | } |
| 4757 | } |
| 4758 | |
| 4759 | if (txn->rsp.msg_state == HTTP_MSG_CLOSED) { |
| 4760 | http_msg_closed: |
| 4761 | /* drop any pending data */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 4762 | bi_erase(chn); |
| 4763 | channel_auto_close(chn); |
| 4764 | channel_auto_read(chn); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4765 | goto wait_other_side; |
| 4766 | } |
| 4767 | |
| 4768 | wait_other_side: |
| 4769 | http_silent_debug(__LINE__, s); |
Willy Tarreau | fc47f91 | 2012-10-20 10:38:09 +0200 | [diff] [blame] | 4770 | /* We force the response to leave immediately if we're waiting for the |
| 4771 | * other side, since there is no pending shutdown to push it out. |
| 4772 | */ |
| 4773 | if (!channel_is_empty(chn)) |
| 4774 | chn->flags |= CF_SEND_DONTWAIT; |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 4775 | return txn->rsp.msg_state != old_state || chn->flags != old_flags; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4776 | } |
| 4777 | |
| 4778 | |
| 4779 | /* Resync the request and response state machines. Return 1 if either state |
| 4780 | * changes. |
| 4781 | */ |
| 4782 | int http_resync_states(struct session *s) |
| 4783 | { |
| 4784 | struct http_txn *txn = &s->txn; |
| 4785 | int old_req_state = txn->req.msg_state; |
| 4786 | int old_res_state = txn->rsp.msg_state; |
| 4787 | |
| 4788 | http_silent_debug(__LINE__, s); |
| 4789 | http_sync_req_state(s); |
| 4790 | while (1) { |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 4791 | http_silent_debug(__LINE__, s); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4792 | if (!http_sync_res_state(s)) |
| 4793 | break; |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 4794 | http_silent_debug(__LINE__, s); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4795 | if (!http_sync_req_state(s)) |
| 4796 | break; |
| 4797 | } |
| 4798 | http_silent_debug(__LINE__, s); |
| 4799 | /* OK, both state machines agree on a compatible state. |
| 4800 | * There are a few cases we're interested in : |
| 4801 | * - HTTP_MSG_TUNNEL on either means we have to disable both analysers |
| 4802 | * - HTTP_MSG_CLOSED on both sides means we've reached the end in both |
| 4803 | * directions, so let's simply disable both analysers. |
| 4804 | * - HTTP_MSG_CLOSED on the response only means we must abort the |
| 4805 | * request. |
| 4806 | * - HTTP_MSG_CLOSED on the request and HTTP_MSG_DONE on the response |
| 4807 | * with server-close mode means we've completed one request and we |
| 4808 | * must re-initialize the server connection. |
| 4809 | */ |
| 4810 | |
| 4811 | if (txn->req.msg_state == HTTP_MSG_TUNNEL || |
| 4812 | txn->rsp.msg_state == HTTP_MSG_TUNNEL || |
| 4813 | (txn->req.msg_state == HTTP_MSG_CLOSED && |
| 4814 | txn->rsp.msg_state == HTTP_MSG_CLOSED)) { |
| 4815 | s->req->analysers = 0; |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 4816 | channel_auto_close(s->req); |
| 4817 | channel_auto_read(s->req); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4818 | s->rep->analysers = 0; |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 4819 | channel_auto_close(s->rep); |
| 4820 | channel_auto_read(s->rep); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4821 | } |
Willy Tarreau | 40f151a | 2012-12-20 12:10:09 +0100 | [diff] [blame] | 4822 | else if ((txn->req.msg_state >= HTTP_MSG_DONE && |
| 4823 | (txn->rsp.msg_state == HTTP_MSG_CLOSED || (s->rep->flags & CF_SHUTW))) || |
Willy Tarreau | 2fa144c | 2010-01-04 23:13:26 +0100 | [diff] [blame] | 4824 | txn->rsp.msg_state == HTTP_MSG_ERROR || |
Willy Tarreau | 40f151a | 2012-12-20 12:10:09 +0100 | [diff] [blame] | 4825 | txn->req.msg_state == HTTP_MSG_ERROR) { |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 4826 | s->rep->analysers = 0; |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 4827 | channel_auto_close(s->rep); |
| 4828 | channel_auto_read(s->rep); |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 4829 | s->req->analysers = 0; |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 4830 | channel_abort(s->req); |
| 4831 | channel_auto_close(s->req); |
| 4832 | channel_auto_read(s->req); |
Willy Tarreau | 9dab5fc | 2012-05-07 11:56:55 +0200 | [diff] [blame] | 4833 | bi_erase(s->req); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4834 | } |
Willy Tarreau | 4213a11 | 2013-12-15 10:25:42 +0100 | [diff] [blame] | 4835 | else if ((txn->req.msg_state == HTTP_MSG_DONE || |
| 4836 | txn->req.msg_state == HTTP_MSG_CLOSED) && |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4837 | txn->rsp.msg_state == HTTP_MSG_DONE && |
Willy Tarreau | 4213a11 | 2013-12-15 10:25:42 +0100 | [diff] [blame] | 4838 | ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL || |
| 4839 | (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL)) { |
| 4840 | /* server-close/keep-alive: terminate this transaction, |
| 4841 | * possibly killing the server connection and reinitialize |
| 4842 | * a fresh-new transaction. |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4843 | */ |
| 4844 | http_end_txn_clean_session(s); |
| 4845 | } |
| 4846 | |
| 4847 | http_silent_debug(__LINE__, s); |
| 4848 | return txn->req.msg_state != old_req_state || |
| 4849 | txn->rsp.msg_state != old_res_state; |
| 4850 | } |
| 4851 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4852 | /* This function is an analyser which forwards request body (including chunk |
| 4853 | * sizes if any). It is called as soon as we must forward, even if we forward |
| 4854 | * zero byte. The only situation where it must not be called is when we're in |
| 4855 | * tunnel mode and we want to forward till the close. It's used both to forward |
| 4856 | * remaining data and to resync after end of body. It expects the msg_state to |
| 4857 | * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to |
| 4858 | * 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] | 4859 | * 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] | 4860 | * bytes of pending data + the headers if not already done (between sol and sov). |
| 4861 | * 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] | 4862 | */ |
Willy Tarreau | 7421efb | 2012-07-02 15:11:27 +0200 | [diff] [blame] | 4863 | 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] | 4864 | { |
| 4865 | struct http_txn *txn = &s->txn; |
| 4866 | struct http_msg *msg = &s->txn.req; |
| 4867 | |
Willy Tarreau | f5c8bd6 | 2010-01-04 07:10:34 +0100 | [diff] [blame] | 4868 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) |
| 4869 | return 0; |
| 4870 | |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 4871 | 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] | 4872 | ((req->flags & CF_SHUTW) && (req->to_forward || req->buf->o))) { |
Willy Tarreau | 4fe4190 | 2010-06-07 22:27:41 +0200 | [diff] [blame] | 4873 | /* Output closed while we were sending data. We must abort and |
| 4874 | * wake the other side up. |
| 4875 | */ |
| 4876 | msg->msg_state = HTTP_MSG_ERROR; |
| 4877 | http_resync_states(s); |
Willy Tarreau | 082b01c | 2010-01-02 23:58:04 +0100 | [diff] [blame] | 4878 | return 1; |
| 4879 | } |
| 4880 | |
Willy Tarreau | 80a92c0 | 2014-03-12 10:41:13 +0100 | [diff] [blame] | 4881 | /* Some post-connect processing might want us to refrain from starting to |
| 4882 | * forward data. Currently, the only reason for this is "balance url_param" |
| 4883 | * whichs need to parse/process the request after we've enabled forwarding. |
| 4884 | */ |
| 4885 | if (unlikely(msg->flags & HTTP_MSGF_WAIT_CONN)) { |
| 4886 | if (!(s->rep->flags & CF_READ_ATTACHED)) { |
| 4887 | channel_auto_connect(req); |
| 4888 | goto missing_data; |
| 4889 | } |
| 4890 | msg->flags &= ~HTTP_MSGF_WAIT_CONN; |
| 4891 | } |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4892 | |
| 4893 | /* Note that we don't have to send 100-continue back because we don't |
| 4894 | * need the data to complete our job, and it's up to the server to |
| 4895 | * decide whether to return 100, 417 or anything else in return of |
| 4896 | * an "Expect: 100-continue" header. |
| 4897 | */ |
| 4898 | |
| 4899 | if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) { |
Willy Tarreau | fa4a03c | 2012-03-09 21:28:54 +0100 | [diff] [blame] | 4900 | /* 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] | 4901 | * 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] | 4902 | * is still null. We must save the body in msg->next because it |
| 4903 | * survives buffer re-alignments. |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4904 | */ |
Willy Tarreau | ea1175a | 2012-03-05 15:52:30 +0100 | [diff] [blame] | 4905 | msg->next = msg->sov; |
Willy Tarreau | a458b67 | 2012-03-05 11:17:50 +0100 | [diff] [blame] | 4906 | |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 4907 | if (msg->flags & HTTP_MSGF_TE_CHNK) |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4908 | msg->msg_state = HTTP_MSG_CHUNK_SIZE; |
Willy Tarreau | 54d23df | 2012-10-25 19:04:45 +0200 | [diff] [blame] | 4909 | else |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4910 | msg->msg_state = HTTP_MSG_DATA; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4911 | } |
| 4912 | |
Willy Tarreau | 80a92c0 | 2014-03-12 10:41:13 +0100 | [diff] [blame] | 4913 | /* in most states, we should abort in case of early close */ |
| 4914 | channel_auto_close(req); |
| 4915 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4916 | while (1) { |
Willy Tarreau | ea95316 | 2012-05-18 23:41:28 +0200 | [diff] [blame] | 4917 | unsigned int bytes; |
Willy Tarreau | d8ee85a | 2011-03-28 16:06:28 +0200 | [diff] [blame] | 4918 | |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4919 | http_silent_debug(__LINE__, s); |
Willy Tarreau | ea95316 | 2012-05-18 23:41:28 +0200 | [diff] [blame] | 4920 | /* we may have some data pending between sol and sov */ |
Willy Tarreau | 2692736 | 2012-05-18 23:22:52 +0200 | [diff] [blame] | 4921 | bytes = msg->sov - msg->sol; |
Willy Tarreau | d8ee85a | 2011-03-28 16:06:28 +0200 | [diff] [blame] | 4922 | if (msg->chunk_len || bytes) { |
Willy Tarreau | 2692736 | 2012-05-18 23:22:52 +0200 | [diff] [blame] | 4923 | msg->sol = msg->sov; |
Willy Tarreau | a458b67 | 2012-03-05 11:17:50 +0100 | [diff] [blame] | 4924 | msg->next -= bytes; /* will be forwarded */ |
Willy Tarreau | ea95316 | 2012-05-18 23:41:28 +0200 | [diff] [blame] | 4925 | msg->chunk_len += bytes; |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 4926 | msg->chunk_len -= channel_forward(req, msg->chunk_len); |
Willy Tarreau | 638cd02 | 2010-01-03 07:42:04 +0100 | [diff] [blame] | 4927 | } |
Willy Tarreau | 5523b32 | 2009-12-29 12:05:52 +0100 | [diff] [blame] | 4928 | |
Willy Tarreau | caabe41 | 2010-01-03 23:08:28 +0100 | [diff] [blame] | 4929 | if (msg->msg_state == HTTP_MSG_DATA) { |
| 4930 | /* must still forward */ |
Willy Tarreau | 4afd70a | 2014-01-25 02:26:39 +0100 | [diff] [blame] | 4931 | if (req->to_forward) { |
| 4932 | req->flags |= CF_WAKE_WRITE; |
Willy Tarreau | f5c8bd6 | 2010-01-04 07:10:34 +0100 | [diff] [blame] | 4933 | goto missing_data; |
Willy Tarreau | 4afd70a | 2014-01-25 02:26:39 +0100 | [diff] [blame] | 4934 | } |
Willy Tarreau | caabe41 | 2010-01-03 23:08:28 +0100 | [diff] [blame] | 4935 | |
| 4936 | /* nothing left to forward */ |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 4937 | if (msg->flags & HTTP_MSGF_TE_CHNK) |
Willy Tarreau | 54d23df | 2012-10-25 19:04:45 +0200 | [diff] [blame] | 4938 | msg->msg_state = HTTP_MSG_CHUNK_CRLF; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4939 | else |
Willy Tarreau | caabe41 | 2010-01-03 23:08:28 +0100 | [diff] [blame] | 4940 | msg->msg_state = HTTP_MSG_DONE; |
Willy Tarreau | caabe41 | 2010-01-03 23:08:28 +0100 | [diff] [blame] | 4941 | } |
| 4942 | else if (msg->msg_state == HTTP_MSG_CHUNK_SIZE) { |
Willy Tarreau | 124d991 | 2011-03-01 20:30:48 +0100 | [diff] [blame] | 4943 | /* read the chunk size and assign it to ->chunk_len, then |
Willy Tarreau | a458b67 | 2012-03-05 11:17:50 +0100 | [diff] [blame] | 4944 | * 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] | 4945 | * TRAILERS state. |
| 4946 | */ |
Willy Tarreau | 4baf44b | 2012-03-09 14:10:20 +0100 | [diff] [blame] | 4947 | int ret = http_parse_chunk_size(msg); |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4948 | |
Willy Tarreau | 54d23df | 2012-10-25 19:04:45 +0200 | [diff] [blame] | 4949 | if (ret == 0) |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4950 | goto missing_data; |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 4951 | else if (ret < 0) { |
| 4952 | session_inc_http_err_ctr(s); |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 4953 | if (msg->err_pos >= 0) |
Willy Tarreau | 8a0cef2 | 2012-03-09 13:39:23 +0100 | [diff] [blame] | 4954 | 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] | 4955 | goto return_bad_req; |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 4956 | } |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4957 | /* otherwise we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state */ |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4958 | } |
Willy Tarreau | 54d23df | 2012-10-25 19:04:45 +0200 | [diff] [blame] | 4959 | else if (msg->msg_state == HTTP_MSG_CHUNK_CRLF) { |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4960 | /* we want the CRLF after the data */ |
Willy Tarreau | 54d23df | 2012-10-25 19:04:45 +0200 | [diff] [blame] | 4961 | int ret = http_skip_chunk_crlf(msg); |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4962 | |
| 4963 | if (ret == 0) |
| 4964 | goto missing_data; |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 4965 | else if (ret < 0) { |
| 4966 | session_inc_http_err_ctr(s); |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 4967 | if (msg->err_pos >= 0) |
Willy Tarreau | 54d23df | 2012-10-25 19:04:45 +0200 | [diff] [blame] | 4968 | 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] | 4969 | goto return_bad_req; |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 4970 | } |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4971 | /* we're in MSG_CHUNK_SIZE now */ |
| 4972 | } |
| 4973 | else if (msg->msg_state == HTTP_MSG_TRAILERS) { |
Willy Tarreau | 4baf44b | 2012-03-09 14:10:20 +0100 | [diff] [blame] | 4974 | int ret = http_forward_trailers(msg); |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4975 | |
| 4976 | if (ret == 0) |
| 4977 | goto missing_data; |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 4978 | else if (ret < 0) { |
| 4979 | session_inc_http_err_ctr(s); |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 4980 | if (msg->err_pos >= 0) |
Willy Tarreau | 8a0cef2 | 2012-03-09 13:39:23 +0100 | [diff] [blame] | 4981 | 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] | 4982 | goto return_bad_req; |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 4983 | } |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4984 | /* we're in HTTP_MSG_DONE now */ |
| 4985 | } |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4986 | else { |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 4987 | int old_state = msg->msg_state; |
| 4988 | |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4989 | /* other states, DONE...TUNNEL */ |
Willy Tarreau | 4fe4190 | 2010-06-07 22:27:41 +0200 | [diff] [blame] | 4990 | /* for keep-alive we don't want to forward closes on DONE */ |
Willy Tarreau | 92aa1fa | 2010-08-28 18:57:20 +0200 | [diff] [blame] | 4991 | if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL || |
| 4992 | (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 4993 | channel_dont_close(req); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4994 | if (http_resync_states(s)) { |
| 4995 | /* some state changes occurred, maybe the analyser |
| 4996 | * was disabled too. |
Willy Tarreau | face839 | 2010-01-03 11:37:54 +0100 | [diff] [blame] | 4997 | */ |
Willy Tarreau | 3fe693b | 2010-12-12 12:50:05 +0100 | [diff] [blame] | 4998 | if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) { |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 4999 | if (req->flags & CF_SHUTW) { |
Willy Tarreau | 3fe693b | 2010-12-12 12:50:05 +0100 | [diff] [blame] | 5000 | /* request errors are most likely due to |
| 5001 | * the server aborting the transfer. |
| 5002 | */ |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 5003 | goto aborted_xfer; |
Willy Tarreau | 3fe693b | 2010-12-12 12:50:05 +0100 | [diff] [blame] | 5004 | } |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 5005 | if (msg->err_pos >= 0) |
Willy Tarreau | 8a0cef2 | 2012-03-09 13:39:23 +0100 | [diff] [blame] | 5006 | 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] | 5007 | goto return_bad_req; |
Willy Tarreau | 3fe693b | 2010-12-12 12:50:05 +0100 | [diff] [blame] | 5008 | } |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5009 | return 1; |
Willy Tarreau | b608feb | 2010-01-02 22:47:18 +0100 | [diff] [blame] | 5010 | } |
Willy Tarreau | 5c54c71 | 2010-07-17 08:02:58 +0200 | [diff] [blame] | 5011 | |
| 5012 | /* If "option abortonclose" is set on the backend, we |
| 5013 | * want to monitor the client's connection and forward |
| 5014 | * any shutdown notification to the server, which will |
| 5015 | * decide whether to close or to go on processing the |
| 5016 | * request. |
| 5017 | */ |
| 5018 | if (s->be->options & PR_O_ABRT_CLOSE) { |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 5019 | channel_auto_read(req); |
| 5020 | channel_auto_close(req); |
Willy Tarreau | 5c54c71 | 2010-07-17 08:02:58 +0200 | [diff] [blame] | 5021 | } |
Willy Tarreau | 58bd8fd | 2010-09-28 14:16:41 +0200 | [diff] [blame] | 5022 | else if (s->txn.meth == HTTP_METH_POST) { |
| 5023 | /* POST requests may require to read extra CRLF |
| 5024 | * sent by broken browsers and which could cause |
| 5025 | * an RST to be sent upon close on some systems |
| 5026 | * (eg: Linux). |
| 5027 | */ |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 5028 | channel_auto_read(req); |
Willy Tarreau | 58bd8fd | 2010-09-28 14:16:41 +0200 | [diff] [blame] | 5029 | } |
Willy Tarreau | 5c54c71 | 2010-07-17 08:02:58 +0200 | [diff] [blame] | 5030 | |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5031 | return 0; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5032 | } |
| 5033 | } |
| 5034 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5035 | missing_data: |
Willy Tarreau | f5c8bd6 | 2010-01-04 07:10:34 +0100 | [diff] [blame] | 5036 | /* stop waiting for data if the input is closed before the end */ |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 5037 | if (req->flags & CF_SHUTR) { |
Willy Tarreau | 79ebac6 | 2010-06-07 13:47:49 +0200 | [diff] [blame] | 5038 | if (!(s->flags & SN_ERR_MASK)) |
| 5039 | s->flags |= SN_ERR_CLICL; |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 5040 | if (!(s->flags & SN_FINST_MASK)) { |
| 5041 | if (txn->rsp.msg_state < HTTP_MSG_ERROR) |
| 5042 | s->flags |= SN_FINST_H; |
| 5043 | else |
| 5044 | s->flags |= SN_FINST_D; |
| 5045 | } |
| 5046 | |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 5047 | s->fe->fe_counters.cli_aborts++; |
| 5048 | s->be->be_counters.cli_aborts++; |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 5049 | if (objt_server(s->target)) |
| 5050 | objt_server(s->target)->counters.cli_aborts++; |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 5051 | |
| 5052 | goto return_bad_req_stats_ok; |
Willy Tarreau | 79ebac6 | 2010-06-07 13:47:49 +0200 | [diff] [blame] | 5053 | } |
Willy Tarreau | f5c8bd6 | 2010-01-04 07:10:34 +0100 | [diff] [blame] | 5054 | |
Willy Tarreau | f5c8bd6 | 2010-01-04 07:10:34 +0100 | [diff] [blame] | 5055 | /* waiting for the last bits to leave the buffer */ |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 5056 | if (req->flags & CF_SHUTW) |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 5057 | goto aborted_xfer; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5058 | |
Willy Tarreau | 92aa1fa | 2010-08-28 18:57:20 +0200 | [diff] [blame] | 5059 | /* 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] | 5060 | * 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] | 5061 | */ |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 5062 | if (msg->flags & HTTP_MSGF_TE_CHNK) |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 5063 | channel_dont_close(req); |
Willy Tarreau | 92aa1fa | 2010-08-28 18:57:20 +0200 | [diff] [blame] | 5064 | |
Willy Tarreau | 5c62092 | 2011-05-11 19:56:11 +0200 | [diff] [blame] | 5065 | /* 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] | 5066 | * 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] | 5067 | * 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] | 5068 | * modes are already handled by the stream sock layer. We must not do |
| 5069 | * this in content-length mode because it could present the MSG_MORE |
| 5070 | * flag with the last block of forwarded data, which would cause an |
| 5071 | * additional delay to be observed by the receiver. |
Willy Tarreau | 5c62092 | 2011-05-11 19:56:11 +0200 | [diff] [blame] | 5072 | */ |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 5073 | if (msg->flags & HTTP_MSGF_TE_CHNK) |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 5074 | req->flags |= CF_EXPECT_MORE; |
Willy Tarreau | 5c62092 | 2011-05-11 19:56:11 +0200 | [diff] [blame] | 5075 | |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5076 | http_silent_debug(__LINE__, s); |
Willy Tarreau | f5c8bd6 | 2010-01-04 07:10:34 +0100 | [diff] [blame] | 5077 | return 0; |
| 5078 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5079 | return_bad_req: /* let's centralize all bad requests */ |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 5080 | s->fe->fe_counters.failed_req++; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5081 | if (s->listener->counters) |
| 5082 | s->listener->counters->failed_req++; |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 5083 | return_bad_req_stats_ok: |
| 5084 | txn->req.msg_state = HTTP_MSG_ERROR; |
| 5085 | if (txn->status) { |
| 5086 | /* Note: we don't send any error if some data were already sent */ |
| 5087 | stream_int_retnclose(req->prod, NULL); |
| 5088 | } else { |
| 5089 | txn->status = 400; |
Willy Tarreau | 783f258 | 2012-09-04 12:19:04 +0200 | [diff] [blame] | 5090 | stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_400)); |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 5091 | } |
| 5092 | req->analysers = 0; |
| 5093 | 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] | 5094 | |
| 5095 | if (!(s->flags & SN_ERR_MASK)) |
| 5096 | s->flags |= SN_ERR_PRXCOND; |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 5097 | if (!(s->flags & SN_FINST_MASK)) { |
| 5098 | if (txn->rsp.msg_state < HTTP_MSG_ERROR) |
| 5099 | s->flags |= SN_FINST_H; |
| 5100 | else |
| 5101 | s->flags |= SN_FINST_D; |
| 5102 | } |
| 5103 | return 0; |
| 5104 | |
| 5105 | aborted_xfer: |
| 5106 | txn->req.msg_state = HTTP_MSG_ERROR; |
| 5107 | if (txn->status) { |
| 5108 | /* Note: we don't send any error if some data were already sent */ |
| 5109 | stream_int_retnclose(req->prod, NULL); |
| 5110 | } else { |
| 5111 | txn->status = 502; |
Willy Tarreau | 783f258 | 2012-09-04 12:19:04 +0200 | [diff] [blame] | 5112 | stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_502)); |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 5113 | } |
| 5114 | req->analysers = 0; |
| 5115 | s->rep->analysers = 0; /* we're in data phase, we want to abort both directions */ |
| 5116 | |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 5117 | s->fe->fe_counters.srv_aborts++; |
| 5118 | s->be->be_counters.srv_aborts++; |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 5119 | if (objt_server(s->target)) |
| 5120 | objt_server(s->target)->counters.srv_aborts++; |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 5121 | |
| 5122 | if (!(s->flags & SN_ERR_MASK)) |
| 5123 | s->flags |= SN_ERR_SRVCL; |
| 5124 | if (!(s->flags & SN_FINST_MASK)) { |
| 5125 | if (txn->rsp.msg_state < HTTP_MSG_ERROR) |
| 5126 | s->flags |= SN_FINST_H; |
| 5127 | else |
| 5128 | s->flags |= SN_FINST_D; |
| 5129 | } |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5130 | return 0; |
| 5131 | } |
| 5132 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5133 | /* This stream analyser waits for a complete HTTP response. It returns 1 if the |
| 5134 | * processing can continue on next analysers, or zero if it either needs more |
| 5135 | * data or wants to immediately abort the response (eg: timeout, error, ...). It |
| 5136 | * is tied to AN_RES_WAIT_HTTP and may may remove itself from s->rep->analysers |
| 5137 | * when it has nothing left to do, and may remove any analyser when it wants to |
| 5138 | * abort. |
Willy Tarreau | c65a3ba | 2008-08-11 23:42:50 +0200 | [diff] [blame] | 5139 | */ |
Willy Tarreau | 7421efb | 2012-07-02 15:11:27 +0200 | [diff] [blame] | 5140 | 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] | 5141 | { |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5142 | struct http_txn *txn = &s->txn; |
| 5143 | struct http_msg *msg = &txn->rsp; |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 5144 | struct hdr_ctx ctx; |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 5145 | int use_close_only; |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5146 | int cur_idx; |
Krzysztof Piotr Oledzki | 5fb1882 | 2009-10-13 21:14:09 +0200 | [diff] [blame] | 5147 | int n; |
Willy Tarreau | adfb856 | 2008-08-11 15:24:42 +0200 | [diff] [blame] | 5148 | |
Willy Tarreau | 02d6cfc | 2012-03-01 18:19:58 +0100 | [diff] [blame] | 5149 | 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] | 5150 | now_ms, __FUNCTION__, |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5151 | s, |
Willy Tarreau | 3a16b2c | 2008-08-28 08:54:27 +0200 | [diff] [blame] | 5152 | rep, |
| 5153 | rep->rex, rep->wex, |
| 5154 | rep->flags, |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 5155 | rep->buf->i, |
Willy Tarreau | 3a16b2c | 2008-08-28 08:54:27 +0200 | [diff] [blame] | 5156 | rep->analysers); |
Willy Tarreau | 67f0eea | 2008-08-10 22:55:22 +0200 | [diff] [blame] | 5157 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5158 | /* |
| 5159 | * Now parse the partial (or complete) lines. |
| 5160 | * We will check the response syntax, and also join multi-line |
| 5161 | * headers. An index of all the lines will be elaborated while |
| 5162 | * parsing. |
| 5163 | * |
| 5164 | * For the parsing, we use a 28 states FSM. |
| 5165 | * |
| 5166 | * Here is the information we currently have : |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 5167 | * rep->buf->p = beginning of response |
| 5168 | * rep->buf->p + msg->eoh = end of processed headers / start of current one |
| 5169 | * rep->buf->p + rep->buf->i = end of input data |
Willy Tarreau | 2692736 | 2012-05-18 23:22:52 +0200 | [diff] [blame] | 5170 | * msg->eol = end of current header or line (LF or CRLF) |
| 5171 | * msg->next = first non-visited byte |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5172 | */ |
| 5173 | |
Willy Tarreau | 83e3af0 | 2009-12-28 17:39:57 +0100 | [diff] [blame] | 5174 | /* There's a protected area at the end of the buffer for rewriting |
| 5175 | * purposes. We don't want to start to parse the request if the |
| 5176 | * protected area is affected, because we may have to move processed |
| 5177 | * data later, which is much more complicated. |
| 5178 | */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 5179 | if (buffer_not_empty(rep->buf) && msg->msg_state < HTTP_MSG_ERROR) { |
Willy Tarreau | 379357a | 2013-06-08 12:55:46 +0200 | [diff] [blame] | 5180 | if (unlikely(!channel_reserved(rep))) { |
| 5181 | /* some data has still not left the buffer, wake us once that's done */ |
| 5182 | if (rep->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_WRITE_ERROR|CF_WRITE_TIMEOUT)) |
| 5183 | goto abort_response; |
| 5184 | channel_dont_close(rep); |
| 5185 | rep->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */ |
Willy Tarreau | d7ad9f5 | 2013-12-31 17:26:25 +0100 | [diff] [blame] | 5186 | rep->flags |= CF_WAKE_WRITE; |
Willy Tarreau | 379357a | 2013-06-08 12:55:46 +0200 | [diff] [blame] | 5187 | return 0; |
Willy Tarreau | 83e3af0 | 2009-12-28 17:39:57 +0100 | [diff] [blame] | 5188 | } |
| 5189 | |
Willy Tarreau | 379357a | 2013-06-08 12:55:46 +0200 | [diff] [blame] | 5190 | if (unlikely(bi_end(rep->buf) < b_ptr(rep->buf, msg->next) || |
| 5191 | bi_end(rep->buf) > rep->buf->data + rep->buf->size - global.tune.maxrewrite)) |
| 5192 | buffer_slow_realign(rep->buf); |
| 5193 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 5194 | if (likely(msg->next < rep->buf->i)) |
Willy Tarreau | a560c21 | 2012-03-09 13:50:57 +0100 | [diff] [blame] | 5195 | http_msg_analyzer(msg, &txn->hdr_idx); |
Willy Tarreau | 83e3af0 | 2009-12-28 17:39:57 +0100 | [diff] [blame] | 5196 | } |
| 5197 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5198 | /* 1: we might have to print this header in debug mode */ |
| 5199 | if (unlikely((global.mode & MODE_DEBUG) && |
| 5200 | (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) && |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 5201 | (msg->msg_state >= HTTP_MSG_BODY || msg->msg_state == HTTP_MSG_ERROR))) { |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5202 | char *eol, *sol; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5203 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 5204 | sol = rep->buf->p; |
| 5205 | 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] | 5206 | debug_hdr("srvrep", s, sol, eol); |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5207 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5208 | sol += hdr_idx_first_pos(&txn->hdr_idx); |
| 5209 | cur_idx = hdr_idx_first_idx(&txn->hdr_idx); |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5210 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5211 | while (cur_idx) { |
| 5212 | eol = sol + txn->hdr_idx.v[cur_idx].len; |
| 5213 | debug_hdr("srvhdr", s, sol, eol); |
| 5214 | sol = eol + txn->hdr_idx.v[cur_idx].cr + 1; |
| 5215 | cur_idx = txn->hdr_idx.v[cur_idx].next; |
| 5216 | } |
| 5217 | } |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5218 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5219 | /* |
| 5220 | * Now we quickly check if we have found a full valid response. |
| 5221 | * If not so, we check the FD and buffer states before leaving. |
| 5222 | * A full response is indicated by the fact that we have seen |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 5223 | * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5224 | * responses are checked first. |
| 5225 | * |
| 5226 | * Depending on whether the client is still there or not, we |
| 5227 | * may send an error response back or not. Note that normally |
| 5228 | * we should only check for HTTP status there, and check I/O |
| 5229 | * errors somewhere else. |
| 5230 | */ |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5231 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 5232 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) { |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5233 | /* Invalid response */ |
| 5234 | if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) { |
| 5235 | /* we detected a parsing error. We want to archive this response |
| 5236 | * in the dedicated proxy area for later troubleshooting. |
| 5237 | */ |
| 5238 | hdr_response_bad: |
| 5239 | if (msg->msg_state == HTTP_MSG_ERROR || msg->err_pos >= 0) |
Willy Tarreau | 8a0cef2 | 2012-03-09 13:39:23 +0100 | [diff] [blame] | 5240 | 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] | 5241 | |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 5242 | s->be->be_counters.failed_resp++; |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 5243 | if (objt_server(s->target)) { |
| 5244 | objt_server(s->target)->counters.failed_resp++; |
| 5245 | health_adjust(objt_server(s->target), HANA_STATUS_HTTP_HDRRSP); |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 5246 | } |
Willy Tarreau | 6464841 | 2010-03-05 10:41:54 +0100 | [diff] [blame] | 5247 | abort_response: |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 5248 | channel_auto_close(rep); |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5249 | rep->analysers = 0; |
| 5250 | txn->status = 502; |
Willy Tarreau | c88ea68 | 2009-12-29 14:56:36 +0100 | [diff] [blame] | 5251 | rep->prod->flags |= SI_FL_NOLINGER; |
Willy Tarreau | 9dab5fc | 2012-05-07 11:56:55 +0200 | [diff] [blame] | 5252 | bi_erase(rep); |
Willy Tarreau | 783f258 | 2012-09-04 12:19:04 +0200 | [diff] [blame] | 5253 | stream_int_retnclose(rep->cons, http_error_message(s, HTTP_ERR_502)); |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5254 | |
| 5255 | if (!(s->flags & SN_ERR_MASK)) |
| 5256 | s->flags |= SN_ERR_PRXCOND; |
| 5257 | if (!(s->flags & SN_FINST_MASK)) |
| 5258 | s->flags |= SN_FINST_H; |
| 5259 | |
| 5260 | return 0; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 5261 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 5262 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5263 | /* too large response does not fit in buffer. */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 5264 | else if (buffer_full(rep->buf, global.tune.maxrewrite)) { |
Willy Tarreau | fec4d89 | 2011-09-02 20:04:57 +0200 | [diff] [blame] | 5265 | if (msg->err_pos < 0) |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 5266 | msg->err_pos = rep->buf->i; |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5267 | goto hdr_response_bad; |
| 5268 | } |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5269 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5270 | /* read error */ |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 5271 | else if (rep->flags & CF_READ_ERROR) { |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5272 | if (msg->err_pos >= 0) |
Willy Tarreau | 8a0cef2 | 2012-03-09 13:39:23 +0100 | [diff] [blame] | 5273 | 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] | 5274 | else if (txn->flags & TX_NOT_FIRST) |
| 5275 | goto abort_keep_alive; |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 5276 | |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 5277 | s->be->be_counters.failed_resp++; |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 5278 | if (objt_server(s->target)) { |
| 5279 | objt_server(s->target)->counters.failed_resp++; |
| 5280 | health_adjust(objt_server(s->target), HANA_STATUS_HTTP_READ_ERROR); |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 5281 | } |
Willy Tarreau | 461f662 | 2008-08-15 23:43:19 +0200 | [diff] [blame] | 5282 | |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 5283 | channel_auto_close(rep); |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5284 | rep->analysers = 0; |
| 5285 | txn->status = 502; |
Willy Tarreau | c88ea68 | 2009-12-29 14:56:36 +0100 | [diff] [blame] | 5286 | rep->prod->flags |= SI_FL_NOLINGER; |
Willy Tarreau | 9dab5fc | 2012-05-07 11:56:55 +0200 | [diff] [blame] | 5287 | bi_erase(rep); |
Willy Tarreau | 783f258 | 2012-09-04 12:19:04 +0200 | [diff] [blame] | 5288 | stream_int_retnclose(rep->cons, http_error_message(s, HTTP_ERR_502)); |
Willy Tarreau | 816b979 | 2009-09-15 21:25:21 +0200 | [diff] [blame] | 5289 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5290 | if (!(s->flags & SN_ERR_MASK)) |
| 5291 | s->flags |= SN_ERR_SRVCL; |
| 5292 | if (!(s->flags & SN_FINST_MASK)) |
| 5293 | s->flags |= SN_FINST_H; |
Willy Tarreau | cebf57e | 2008-08-15 18:16:37 +0200 | [diff] [blame] | 5294 | return 0; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5295 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 5296 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5297 | /* read timeout : return a 504 to the client. */ |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 5298 | else if (rep->flags & CF_READ_TIMEOUT) { |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5299 | if (msg->err_pos >= 0) |
Willy Tarreau | 8a0cef2 | 2012-03-09 13:39:23 +0100 | [diff] [blame] | 5300 | 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] | 5301 | else if (txn->flags & TX_NOT_FIRST) |
| 5302 | goto abort_keep_alive; |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 5303 | |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 5304 | s->be->be_counters.failed_resp++; |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 5305 | if (objt_server(s->target)) { |
| 5306 | objt_server(s->target)->counters.failed_resp++; |
| 5307 | health_adjust(objt_server(s->target), HANA_STATUS_HTTP_READ_TIMEOUT); |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 5308 | } |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 5309 | |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 5310 | channel_auto_close(rep); |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5311 | rep->analysers = 0; |
| 5312 | txn->status = 504; |
Willy Tarreau | c88ea68 | 2009-12-29 14:56:36 +0100 | [diff] [blame] | 5313 | rep->prod->flags |= SI_FL_NOLINGER; |
Willy Tarreau | 9dab5fc | 2012-05-07 11:56:55 +0200 | [diff] [blame] | 5314 | bi_erase(rep); |
Willy Tarreau | 783f258 | 2012-09-04 12:19:04 +0200 | [diff] [blame] | 5315 | stream_int_retnclose(rep->cons, http_error_message(s, HTTP_ERR_504)); |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 5316 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5317 | if (!(s->flags & SN_ERR_MASK)) |
| 5318 | s->flags |= SN_ERR_SRVTO; |
| 5319 | if (!(s->flags & SN_FINST_MASK)) |
| 5320 | s->flags |= SN_FINST_H; |
| 5321 | return 0; |
| 5322 | } |
Willy Tarreau | a7c5276 | 2008-08-16 18:40:18 +0200 | [diff] [blame] | 5323 | |
Willy Tarreau | f003d37 | 2012-11-26 13:35:37 +0100 | [diff] [blame] | 5324 | /* client abort with an abortonclose */ |
| 5325 | else if ((rep->flags & CF_SHUTR) && ((s->req->flags & (CF_SHUTR|CF_SHUTW)) == (CF_SHUTR|CF_SHUTW))) { |
| 5326 | s->fe->fe_counters.cli_aborts++; |
| 5327 | s->be->be_counters.cli_aborts++; |
| 5328 | if (objt_server(s->target)) |
| 5329 | objt_server(s->target)->counters.cli_aborts++; |
| 5330 | |
| 5331 | rep->analysers = 0; |
| 5332 | channel_auto_close(rep); |
| 5333 | |
| 5334 | txn->status = 400; |
| 5335 | bi_erase(rep); |
| 5336 | stream_int_retnclose(rep->cons, http_error_message(s, HTTP_ERR_400)); |
| 5337 | |
| 5338 | if (!(s->flags & SN_ERR_MASK)) |
| 5339 | s->flags |= SN_ERR_CLICL; |
| 5340 | if (!(s->flags & SN_FINST_MASK)) |
| 5341 | s->flags |= SN_FINST_H; |
| 5342 | |
| 5343 | /* process_session() will take care of the error */ |
| 5344 | return 0; |
| 5345 | } |
| 5346 | |
Willy Tarreau | 3b8c08a | 2011-09-02 20:16:24 +0200 | [diff] [blame] | 5347 | /* 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] | 5348 | else if (rep->flags & CF_SHUTR) { |
Willy Tarreau | 3b8c08a | 2011-09-02 20:16:24 +0200 | [diff] [blame] | 5349 | if (msg->msg_state >= HTTP_MSG_RPVER || msg->err_pos >= 0) |
Willy Tarreau | 8a0cef2 | 2012-03-09 13:39:23 +0100 | [diff] [blame] | 5350 | 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] | 5351 | else if (txn->flags & TX_NOT_FIRST) |
| 5352 | goto abort_keep_alive; |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 5353 | |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 5354 | s->be->be_counters.failed_resp++; |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 5355 | if (objt_server(s->target)) { |
| 5356 | objt_server(s->target)->counters.failed_resp++; |
| 5357 | health_adjust(objt_server(s->target), HANA_STATUS_HTTP_BROKEN_PIPE); |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 5358 | } |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 5359 | |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 5360 | channel_auto_close(rep); |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5361 | rep->analysers = 0; |
| 5362 | txn->status = 502; |
Willy Tarreau | c88ea68 | 2009-12-29 14:56:36 +0100 | [diff] [blame] | 5363 | rep->prod->flags |= SI_FL_NOLINGER; |
Willy Tarreau | 9dab5fc | 2012-05-07 11:56:55 +0200 | [diff] [blame] | 5364 | bi_erase(rep); |
Willy Tarreau | 783f258 | 2012-09-04 12:19:04 +0200 | [diff] [blame] | 5365 | stream_int_retnclose(rep->cons, http_error_message(s, HTTP_ERR_502)); |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 5366 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5367 | if (!(s->flags & SN_ERR_MASK)) |
| 5368 | s->flags |= SN_ERR_SRVCL; |
| 5369 | if (!(s->flags & SN_FINST_MASK)) |
| 5370 | s->flags |= SN_FINST_H; |
| 5371 | return 0; |
| 5372 | } |
Krzysztof Piotr Oledzki | 5fb1882 | 2009-10-13 21:14:09 +0200 | [diff] [blame] | 5373 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5374 | /* write error to client (we don't send any message then) */ |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 5375 | else if (rep->flags & CF_WRITE_ERROR) { |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5376 | if (msg->err_pos >= 0) |
Willy Tarreau | 8a0cef2 | 2012-03-09 13:39:23 +0100 | [diff] [blame] | 5377 | 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] | 5378 | else if (txn->flags & TX_NOT_FIRST) |
| 5379 | goto abort_keep_alive; |
Krzysztof Piotr Oledzki | 5fb1882 | 2009-10-13 21:14:09 +0200 | [diff] [blame] | 5380 | |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 5381 | s->be->be_counters.failed_resp++; |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5382 | rep->analysers = 0; |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 5383 | channel_auto_close(rep); |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5384 | |
| 5385 | if (!(s->flags & SN_ERR_MASK)) |
| 5386 | s->flags |= SN_ERR_CLICL; |
| 5387 | if (!(s->flags & SN_FINST_MASK)) |
| 5388 | s->flags |= SN_FINST_H; |
| 5389 | |
| 5390 | /* process_session() will take care of the error */ |
| 5391 | return 0; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5392 | } |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 5393 | |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 5394 | channel_dont_close(rep); |
Willy Tarreau | 3f3997e | 2013-12-15 15:21:32 +0100 | [diff] [blame] | 5395 | rep->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */ |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5396 | return 0; |
| 5397 | } |
| 5398 | |
| 5399 | /* More interesting part now : we know that we have a complete |
| 5400 | * response which at least looks like HTTP. We have an indicator |
| 5401 | * of each header's length, so we can parse them quickly. |
| 5402 | */ |
| 5403 | |
| 5404 | if (unlikely(msg->err_pos >= 0)) |
Willy Tarreau | 8a0cef2 | 2012-03-09 13:39:23 +0100 | [diff] [blame] | 5405 | 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] | 5406 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5407 | /* |
| 5408 | * 1: get the status code |
| 5409 | */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 5410 | n = rep->buf->p[msg->sl.st.c] - '0'; |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5411 | if (n < 1 || n > 5) |
| 5412 | n = 0; |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 5413 | /* when the client triggers a 4xx from the server, it's most often due |
| 5414 | * to a missing object or permission. These events should be tracked |
| 5415 | * because if they happen often, it may indicate a brute force or a |
| 5416 | * vulnerability scan. |
| 5417 | */ |
| 5418 | if (n == 4) |
| 5419 | session_inc_http_err_ctr(s); |
| 5420 | |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 5421 | if (objt_server(s->target)) |
| 5422 | objt_server(s->target)->counters.p.http.rsp[n]++; |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5423 | |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 5424 | /* check if the response is HTTP/1.1 or above */ |
| 5425 | if ((msg->sl.st.v_l == 8) && |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 5426 | ((rep->buf->p[5] > '1') || |
| 5427 | ((rep->buf->p[5] == '1') && (rep->buf->p[7] >= '1')))) |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 5428 | msg->flags |= HTTP_MSGF_VER_11; |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 5429 | |
| 5430 | /* "connection" has not been parsed yet */ |
Willy Tarreau | 50fc777 | 2012-11-11 22:19:57 +0100 | [diff] [blame] | 5431 | 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] | 5432 | |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 5433 | /* transfer length unknown*/ |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 5434 | msg->flags &= ~HTTP_MSGF_XFER_LEN; |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 5435 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 5436 | 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] | 5437 | |
Willy Tarreau | 3965040 | 2010-03-15 19:44:39 +0100 | [diff] [blame] | 5438 | /* Adjust server's health based on status code. Note: status codes 501 |
| 5439 | * and 505 are triggered on demand by client request, so we must not |
| 5440 | * count them as server failures. |
| 5441 | */ |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 5442 | if (objt_server(s->target)) { |
Willy Tarreau | d45b3d5 | 2010-05-20 11:49:03 +0200 | [diff] [blame] | 5443 | 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] | 5444 | health_adjust(objt_server(s->target), HANA_STATUS_HTTP_OK); |
Willy Tarreau | d45b3d5 | 2010-05-20 11:49:03 +0200 | [diff] [blame] | 5445 | else |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 5446 | health_adjust(objt_server(s->target), HANA_STATUS_HTTP_STS); |
Willy Tarreau | d45b3d5 | 2010-05-20 11:49:03 +0200 | [diff] [blame] | 5447 | } |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 5448 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5449 | /* |
| 5450 | * 2: check for cacheability. |
| 5451 | */ |
| 5452 | |
| 5453 | switch (txn->status) { |
| 5454 | case 200: |
| 5455 | case 203: |
| 5456 | case 206: |
| 5457 | case 300: |
| 5458 | case 301: |
| 5459 | case 410: |
| 5460 | /* RFC2616 @13.4: |
| 5461 | * "A response received with a status code of |
| 5462 | * 200, 203, 206, 300, 301 or 410 MAY be stored |
| 5463 | * by a cache (...) unless a cache-control |
| 5464 | * directive prohibits caching." |
| 5465 | * |
| 5466 | * RFC2616 @9.5: POST method : |
| 5467 | * "Responses to this method are not cacheable, |
| 5468 | * unless the response includes appropriate |
| 5469 | * Cache-Control or Expires header fields." |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5470 | */ |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5471 | if (likely(txn->meth != HTTP_METH_POST) && |
Willy Tarreau | 6740213 | 2012-05-31 20:40:20 +0200 | [diff] [blame] | 5472 | ((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] | 5473 | txn->flags |= TX_CACHEABLE | TX_CACHE_COOK; |
| 5474 | break; |
| 5475 | default: |
| 5476 | break; |
| 5477 | } |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5478 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5479 | /* |
| 5480 | * 3: we may need to capture headers |
| 5481 | */ |
| 5482 | s->logs.logwait &= ~LW_RESP; |
Willy Tarreau | 42f7d89 | 2012-03-24 08:28:09 +0100 | [diff] [blame] | 5483 | if (unlikely((s->logs.logwait & LW_RSPHDR) && txn->rsp.cap)) |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 5484 | capture_headers(rep->buf->p, &txn->hdr_idx, |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5485 | txn->rsp.cap, s->fe->rsp_cap); |
| 5486 | |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 5487 | /* 4: determine the transfer-length. |
| 5488 | * According to RFC2616 #4.4, amended by the HTTPbis working group, |
| 5489 | * the presence of a message-body in a RESPONSE and its transfer length |
| 5490 | * must be determined that way : |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 5491 | * |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 5492 | * All responses to the HEAD request method MUST NOT include a |
| 5493 | * message-body, even though the presence of entity-header fields |
| 5494 | * might lead one to believe they do. All 1xx (informational), 204 |
| 5495 | * (No Content), and 304 (Not Modified) responses MUST NOT include a |
| 5496 | * message-body. All other responses do include a message-body, |
| 5497 | * although it MAY be of zero length. |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 5498 | * |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 5499 | * 1. Any response which "MUST NOT" include a message-body (such as the |
| 5500 | * 1xx, 204 and 304 responses and any response to a HEAD request) is |
| 5501 | * always terminated by the first empty line after the header fields, |
| 5502 | * regardless of the entity-header fields present in the message. |
| 5503 | * |
| 5504 | * 2. If a Transfer-Encoding header field (Section 9.7) is present and |
| 5505 | * the "chunked" transfer-coding (Section 6.2) is used, the |
| 5506 | * transfer-length is defined by the use of this transfer-coding. |
| 5507 | * If a Transfer-Encoding header field is present and the "chunked" |
| 5508 | * transfer-coding is not present, the transfer-length is defined by |
| 5509 | * the sender closing the connection. |
| 5510 | * |
| 5511 | * 3. If a Content-Length header field is present, its decimal value in |
| 5512 | * OCTETs represents both the entity-length and the transfer-length. |
| 5513 | * If a message is received with both a Transfer-Encoding header |
| 5514 | * field and a Content-Length header field, the latter MUST be ignored. |
| 5515 | * |
| 5516 | * 4. If the message uses the media type "multipart/byteranges", and |
| 5517 | * the transfer-length is not otherwise specified, then this self- |
| 5518 | * delimiting media type defines the transfer-length. This media |
| 5519 | * type MUST NOT be used unless the sender knows that the recipient |
| 5520 | * can parse it; the presence in a request of a Range header with |
| 5521 | * multiple byte-range specifiers from a 1.1 client implies that the |
| 5522 | * client can parse multipart/byteranges responses. |
| 5523 | * |
| 5524 | * 5. By the server closing the connection. |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 5525 | */ |
| 5526 | |
| 5527 | /* Skip parsing if no content length is possible. The response flags |
Willy Tarreau | 124d991 | 2011-03-01 20:30:48 +0100 | [diff] [blame] | 5528 | * 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] | 5529 | * 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] | 5530 | * FIXME: should we parse anyway and return an error on chunked encoding ? |
| 5531 | */ |
| 5532 | if (txn->meth == HTTP_METH_HEAD || |
| 5533 | (txn->status >= 100 && txn->status < 200) || |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 5534 | txn->status == 204 || txn->status == 304) { |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 5535 | msg->flags |= HTTP_MSGF_XFER_LEN; |
Willy Tarreau | 9101535 | 2012-11-27 07:31:33 +0100 | [diff] [blame] | 5536 | s->comp_algo = NULL; |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 5537 | goto skip_content_length; |
| 5538 | } |
| 5539 | |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 5540 | use_close_only = 0; |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 5541 | ctx.idx = 0; |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 5542 | while ((msg->flags & HTTP_MSGF_VER_11) && |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 5543 | 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] | 5544 | if (ctx.vlen == 7 && strncasecmp(ctx.line + ctx.val, "chunked", 7) == 0) |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 5545 | msg->flags |= (HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN); |
| 5546 | else if (msg->flags & HTTP_MSGF_TE_CHNK) { |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 5547 | /* bad transfer-encoding (chunked followed by something else) */ |
| 5548 | use_close_only = 1; |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 5549 | msg->flags &= ~(HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN); |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 5550 | break; |
| 5551 | } |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 5552 | } |
| 5553 | |
| 5554 | /* FIXME: below we should remove the content-length header(s) in case of chunked encoding */ |
| 5555 | ctx.idx = 0; |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 5556 | while (!(msg->flags & HTTP_MSGF_TE_CHNK) && !use_close_only && |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 5557 | 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] | 5558 | signed long long cl; |
| 5559 | |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 5560 | if (!ctx.vlen) { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 5561 | msg->err_pos = ctx.line + ctx.val - rep->buf->p; |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 5562 | goto hdr_response_bad; |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 5563 | } |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 5564 | |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 5565 | if (strl2llrc(ctx.line + ctx.val, ctx.vlen, &cl)) { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 5566 | msg->err_pos = ctx.line + ctx.val - rep->buf->p; |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 5567 | goto hdr_response_bad; /* parse failure */ |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 5568 | } |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 5569 | |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 5570 | if (cl < 0) { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 5571 | msg->err_pos = ctx.line + ctx.val - rep->buf->p; |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 5572 | goto hdr_response_bad; |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 5573 | } |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 5574 | |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 5575 | if ((msg->flags & HTTP_MSGF_CNT_LEN) && (msg->chunk_len != cl)) { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 5576 | msg->err_pos = ctx.line + ctx.val - rep->buf->p; |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 5577 | goto hdr_response_bad; /* already specified, was different */ |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 5578 | } |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 5579 | |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 5580 | msg->flags |= HTTP_MSGF_CNT_LEN | HTTP_MSGF_XFER_LEN; |
Willy Tarreau | 124d991 | 2011-03-01 20:30:48 +0100 | [diff] [blame] | 5581 | msg->body_len = msg->chunk_len = cl; |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 5582 | } |
| 5583 | |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 5584 | if (s->fe->comp || s->be->comp) |
| 5585 | select_compression_response_header(s, rep->buf); |
| 5586 | |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 5587 | /* FIXME: we should also implement the multipart/byterange method. |
| 5588 | * For now on, we resort to close mode in this case (unknown length). |
| 5589 | */ |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 5590 | skip_content_length: |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 5591 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5592 | /* end of job, return OK */ |
| 5593 | rep->analysers &= ~an_bit; |
| 5594 | rep->analyse_exp = TICK_ETERNITY; |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 5595 | channel_auto_close(rep); |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5596 | return 1; |
Willy Tarreau | 6b726ad | 2013-12-15 19:31:37 +0100 | [diff] [blame] | 5597 | |
| 5598 | abort_keep_alive: |
| 5599 | /* A keep-alive request to the server failed on a network error. |
| 5600 | * The client is required to retry. We need to close without returning |
| 5601 | * any other information so that the client retries. |
| 5602 | */ |
| 5603 | txn->status = 0; |
| 5604 | rep->analysers = 0; |
| 5605 | s->req->analysers = 0; |
| 5606 | channel_auto_close(rep); |
| 5607 | s->logs.logwait = 0; |
| 5608 | s->logs.level = 0; |
| 5609 | s->rep->flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */ |
| 5610 | bi_erase(rep); |
| 5611 | stream_int_retnclose(rep->cons, NULL); |
| 5612 | return 0; |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5613 | } |
| 5614 | |
| 5615 | /* This function performs all the processing enabled for the current response. |
Willy Tarreau | e3fa6e5 | 2010-01-04 22:57:43 +0100 | [diff] [blame] | 5616 | * It normally returns 1 unless it wants to break. It relies on buffers flags, |
| 5617 | * and updates t->rep->analysers. It might make sense to explode it into several |
| 5618 | * other functions. It works like process_request (see indications above). |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5619 | */ |
Willy Tarreau | 7421efb | 2012-07-02 15:11:27 +0200 | [diff] [blame] | 5620 | 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] | 5621 | { |
| 5622 | struct http_txn *txn = &t->txn; |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5623 | struct http_msg *msg = &txn->rsp; |
| 5624 | struct proxy *cur_proxy; |
Willy Tarreau | f4f0412 | 2010-01-28 18:10:50 +0100 | [diff] [blame] | 5625 | struct cond_wordlist *wl; |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 5626 | struct http_res_rule *http_res_last_rule = NULL; |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5627 | |
Willy Tarreau | 02d6cfc | 2012-03-01 18:19:58 +0100 | [diff] [blame] | 5628 | 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] | 5629 | now_ms, __FUNCTION__, |
| 5630 | t, |
| 5631 | rep, |
| 5632 | rep->rex, rep->wex, |
| 5633 | rep->flags, |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 5634 | rep->buf->i, |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5635 | rep->analysers); |
| 5636 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 5637 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) /* we need more data */ |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5638 | return 0; |
| 5639 | |
| 5640 | rep->analysers &= ~an_bit; |
| 5641 | rep->analyse_exp = TICK_ETERNITY; |
| 5642 | |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 5643 | /* Now we have to check if we need to modify the Connection header. |
| 5644 | * This is more difficult on the response than it is on the request, |
| 5645 | * because we can have two different HTTP versions and we don't know |
| 5646 | * how the client will interprete a response. For instance, let's say |
| 5647 | * that the client sends a keep-alive request in HTTP/1.0 and gets an |
| 5648 | * HTTP/1.1 response without any header. Maybe it will bound itself to |
| 5649 | * HTTP/1.0 because it only knows about it, and will consider the lack |
| 5650 | * of header as a close, or maybe it knows HTTP/1.1 and can consider |
| 5651 | * the lack of header as a keep-alive. Thus we will use two flags |
| 5652 | * indicating how a request MAY be understood by the client. In case |
| 5653 | * of multiple possibilities, we'll fix the header to be explicit. If |
| 5654 | * ambiguous cases such as both close and keepalive are seen, then we |
| 5655 | * will fall back to explicit close. Note that we won't take risks with |
| 5656 | * HTTP/1.0 clients which may not necessarily understand keep-alive. |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 5657 | * See doc/internals/connection-header.txt for the complete matrix. |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 5658 | */ |
| 5659 | |
Willy Tarreau | dc008c5 | 2010-02-01 16:20:08 +0100 | [diff] [blame] | 5660 | if (unlikely((txn->meth == HTTP_METH_CONNECT && txn->status == 200) || |
| 5661 | txn->status == 101)) { |
| 5662 | /* Either we've established an explicit tunnel, or we're |
| 5663 | * switching the protocol. In both cases, we're very unlikely |
Willy Tarreau | 5843d1a | 2010-02-01 15:13:32 +0100 | [diff] [blame] | 5664 | * to understand the next protocols. We have to switch to tunnel |
| 5665 | * mode, so that we transfer the request and responses then let |
| 5666 | * this protocol pass unmodified. When we later implement specific |
| 5667 | * parsers for such protocols, we'll want to check the Upgrade |
Willy Tarreau | dc008c5 | 2010-02-01 16:20:08 +0100 | [diff] [blame] | 5668 | * header which contains information about that protocol for |
| 5669 | * responses with status 101 (eg: see RFC2817 about TLS). |
Willy Tarreau | 5843d1a | 2010-02-01 15:13:32 +0100 | [diff] [blame] | 5670 | */ |
| 5671 | txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_TUN; |
| 5672 | } |
Willy Tarreau | dc008c5 | 2010-02-01 16:20:08 +0100 | [diff] [blame] | 5673 | else if ((txn->status >= 200) && !(txn->flags & TX_HDR_CONN_PRS) && |
| 5674 | ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN || |
Willy Tarreau | 02bce8b | 2014-01-30 00:15:28 +0100 | [diff] [blame] | 5675 | ((t->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL || |
| 5676 | (t->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL))) { |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 5677 | int to_del = 0; |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 5678 | |
Willy Tarreau | 70dffda | 2014-01-30 03:07:23 +0100 | [diff] [blame] | 5679 | /* this situation happens when combining pretend-keepalive with httpclose. */ |
| 5680 | if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL && |
| 5681 | ((t->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL || |
| 5682 | (t->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL)) |
| 5683 | txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO; |
| 5684 | |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 5685 | /* on unknown transfer length, we must close */ |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 5686 | if (!(msg->flags & HTTP_MSGF_XFER_LEN) && |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 5687 | (txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) |
| 5688 | txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO; |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 5689 | |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 5690 | /* now adjust header transformations depending on current state */ |
| 5691 | if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN || |
| 5692 | (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) { |
| 5693 | to_del |= 2; /* remove "keep-alive" on any response */ |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 5694 | if (!(msg->flags & HTTP_MSGF_VER_11)) |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 5695 | to_del |= 1; /* remove "close" for HTTP/1.0 responses */ |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 5696 | } |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 5697 | else { /* SCL / KAL */ |
| 5698 | to_del |= 1; /* remove "close" on any response */ |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 5699 | if (txn->req.flags & msg->flags & HTTP_MSGF_VER_11) |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 5700 | to_del |= 2; /* remove "keep-alive" on pure 1.1 responses */ |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 5701 | } |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 5702 | |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 5703 | /* Parse and remove some headers from the connection header */ |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 5704 | http_parse_connection_header(txn, msg, to_del); |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 5705 | |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 5706 | /* Some keep-alive responses are converted to Server-close if |
| 5707 | * the server wants to close. |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 5708 | */ |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 5709 | if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL) { |
| 5710 | if ((txn->flags & TX_HDR_CONN_CLO) || |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 5711 | (!(txn->flags & TX_HDR_CONN_KAL) && !(msg->flags & HTTP_MSGF_VER_11))) |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 5712 | txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_SCL; |
Willy Tarreau | b608feb | 2010-01-02 22:47:18 +0100 | [diff] [blame] | 5713 | } |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 5714 | } |
| 5715 | |
Willy Tarreau | 7959a55 | 2013-09-23 16:44:27 +0200 | [diff] [blame] | 5716 | /* we want to have the response time before we start processing it */ |
| 5717 | t->logs.t_data = tv_ms_elapsed(&t->logs.tv_accept, &now); |
| 5718 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5719 | if (1) { |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5720 | /* |
| 5721 | * 3: we will have to evaluate the filters. |
| 5722 | * As opposed to version 1.2, now they will be evaluated in the |
| 5723 | * filters order and not in the header order. This means that |
| 5724 | * each filter has to be validated among all headers. |
| 5725 | * |
| 5726 | * Filters are tried with ->be first, then with ->fe if it is |
| 5727 | * different from ->be. |
| 5728 | */ |
| 5729 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5730 | cur_proxy = t->be; |
| 5731 | while (1) { |
| 5732 | struct proxy *rule_set = cur_proxy; |
| 5733 | |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 5734 | /* evaluate http-response rules */ |
| 5735 | if (!http_res_last_rule) |
| 5736 | http_res_last_rule = http_res_get_intercept_rule(cur_proxy, &cur_proxy->http_res_rules, t, txn); |
| 5737 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5738 | /* try headers filters */ |
| 5739 | if (rule_set->rsp_exp != NULL) { |
Willy Tarreau | fdb563c | 2010-01-31 15:43:27 +0100 | [diff] [blame] | 5740 | if (apply_filters_to_response(t, rep, rule_set) < 0) { |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5741 | return_bad_resp: |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 5742 | if (objt_server(t->target)) { |
| 5743 | objt_server(t->target)->counters.failed_resp++; |
| 5744 | health_adjust(objt_server(t->target), HANA_STATUS_HTTP_RSP); |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 5745 | } |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 5746 | t->be->be_counters.failed_resp++; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5747 | return_srv_prx_502: |
Willy Tarreau | 2df28e8 | 2008-08-17 15:20:19 +0200 | [diff] [blame] | 5748 | rep->analysers = 0; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5749 | txn->status = 502; |
Willy Tarreau | 7959a55 | 2013-09-23 16:44:27 +0200 | [diff] [blame] | 5750 | t->logs.t_data = -1; /* was not a valid response */ |
Willy Tarreau | c88ea68 | 2009-12-29 14:56:36 +0100 | [diff] [blame] | 5751 | rep->prod->flags |= SI_FL_NOLINGER; |
Willy Tarreau | 9dab5fc | 2012-05-07 11:56:55 +0200 | [diff] [blame] | 5752 | bi_erase(rep); |
Willy Tarreau | 783f258 | 2012-09-04 12:19:04 +0200 | [diff] [blame] | 5753 | stream_int_retnclose(rep->cons, http_error_message(t, HTTP_ERR_502)); |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5754 | if (!(t->flags & SN_ERR_MASK)) |
| 5755 | t->flags |= SN_ERR_PRXCOND; |
| 5756 | if (!(t->flags & SN_FINST_MASK)) |
| 5757 | t->flags |= SN_FINST_H; |
Willy Tarreau | dafde43 | 2008-08-17 01:00:46 +0200 | [diff] [blame] | 5758 | return 0; |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 5759 | } |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5760 | } |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 5761 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5762 | /* has the response been denied ? */ |
| 5763 | if (txn->flags & TX_SVDENY) { |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 5764 | if (objt_server(t->target)) |
| 5765 | objt_server(t->target)->counters.failed_secu++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 5766 | |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 5767 | t->be->be_counters.denied_resp++; |
| 5768 | t->fe->fe_counters.denied_resp++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 5769 | if (t->listener->counters) |
| 5770 | t->listener->counters->denied_resp++; |
| 5771 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5772 | goto return_srv_prx_502; |
Willy Tarreau | 5140623 | 2008-03-10 22:04:20 +0100 | [diff] [blame] | 5773 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 5774 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5775 | /* add response headers from the rule sets in the same order */ |
Willy Tarreau | deb9ed8 | 2010-01-03 21:03:22 +0100 | [diff] [blame] | 5776 | list_for_each_entry(wl, &rule_set->rsp_add, list) { |
Willy Tarreau | 816b979 | 2009-09-15 21:25:21 +0200 | [diff] [blame] | 5777 | if (txn->status < 200) |
| 5778 | break; |
Willy Tarreau | fdb563c | 2010-01-31 15:43:27 +0100 | [diff] [blame] | 5779 | if (wl->cond) { |
Willy Tarreau | 32a6f2e | 2012-04-25 10:13:36 +0200 | [diff] [blame] | 5780 | 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] | 5781 | ret = acl_pass(ret); |
| 5782 | if (((struct acl_cond *)wl->cond)->pol == ACL_COND_UNLESS) |
| 5783 | ret = !ret; |
| 5784 | if (!ret) |
| 5785 | continue; |
| 5786 | } |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 5787 | 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] | 5788 | goto return_bad_resp; |
Willy Tarreau | 0bbc3cf | 2006-10-15 14:26:02 +0200 | [diff] [blame] | 5789 | } |
| 5790 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5791 | /* check whether we're already working on the frontend */ |
| 5792 | if (cur_proxy == t->fe) |
| 5793 | break; |
| 5794 | cur_proxy = t->fe; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 5795 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 5796 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5797 | /* |
Willy Tarreau | 5843d1a | 2010-02-01 15:13:32 +0100 | [diff] [blame] | 5798 | * We may be facing a 100-continue response, in which case this |
| 5799 | * is not the right response, and we're waiting for the next one. |
| 5800 | * 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] | 5801 | * next one. |
| 5802 | */ |
Willy Tarreau | 5843d1a | 2010-02-01 15:13:32 +0100 | [diff] [blame] | 5803 | if (unlikely(txn->status == 100)) { |
Willy Tarreau | 63c9e5f | 2009-12-22 16:01:27 +0100 | [diff] [blame] | 5804 | hdr_idx_init(&txn->hdr_idx); |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 5805 | msg->next -= channel_forward(rep, msg->next); |
Willy Tarreau | 63c9e5f | 2009-12-22 16:01:27 +0100 | [diff] [blame] | 5806 | msg->msg_state = HTTP_MSG_RPBEFORE; |
| 5807 | txn->status = 0; |
Willy Tarreau | 7959a55 | 2013-09-23 16:44:27 +0200 | [diff] [blame] | 5808 | t->logs.t_data = -1; /* was not a response yet */ |
Willy Tarreau | 63c9e5f | 2009-12-22 16:01:27 +0100 | [diff] [blame] | 5809 | rep->analysers |= AN_RES_WAIT_HTTP | an_bit; |
| 5810 | return 1; |
| 5811 | } |
Willy Tarreau | 5843d1a | 2010-02-01 15:13:32 +0100 | [diff] [blame] | 5812 | else if (unlikely(txn->status < 200)) |
| 5813 | goto skip_header_mangling; |
Willy Tarreau | 63c9e5f | 2009-12-22 16:01:27 +0100 | [diff] [blame] | 5814 | |
| 5815 | /* we don't have any 1xx status code now */ |
| 5816 | |
| 5817 | /* |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5818 | * 4: check for server cookie. |
| 5819 | */ |
Willy Tarreau | 63c9e5f | 2009-12-22 16:01:27 +0100 | [diff] [blame] | 5820 | if (t->be->cookie_name || t->be->appsession_name || t->fe->capture_name || |
| 5821 | (t->be->options & PR_O_CHK_CACHE)) |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5822 | manage_server_side_cookies(t, rep); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 5823 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 5824 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5825 | /* |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5826 | * 5: check for cache-control or pragma headers if required. |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5827 | */ |
Willy Tarreau | 6740213 | 2012-05-31 20:40:20 +0200 | [diff] [blame] | 5828 | 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] | 5829 | check_response_for_cacheability(t, rep); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5830 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5831 | /* |
| 5832 | * 6: add server cookie in the response if needed |
| 5833 | */ |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 5834 | if (objt_server(t->target) && (t->be->ck_opts & PR_CK_INS) && |
Willy Tarreau | 6740213 | 2012-05-31 20:40:20 +0200 | [diff] [blame] | 5835 | !((txn->flags & TX_SCK_FOUND) && (t->be->ck_opts & PR_CK_PSV)) && |
Willy Tarreau | ef4f391 | 2010-10-07 21:00:29 +0200 | [diff] [blame] | 5836 | (!(t->flags & SN_DIRECT) || |
| 5837 | ((t->be->cookie_maxidle || txn->cookie_last_date) && |
| 5838 | (!txn->cookie_last_date || (txn->cookie_last_date - date.tv_sec) < 0)) || |
| 5839 | (t->be->cookie_maxlife && !txn->cookie_first_date) || // set the first_date |
| 5840 | (!t->be->cookie_maxlife && txn->cookie_first_date)) && // remove the first_date |
Willy Tarreau | 6740213 | 2012-05-31 20:40:20 +0200 | [diff] [blame] | 5841 | (!(t->be->ck_opts & PR_CK_POST) || (txn->meth == HTTP_METH_POST)) && |
Cyril Bonté | 47fdd8e | 2010-04-25 00:00:51 +0200 | [diff] [blame] | 5842 | !(t->flags & SN_IGNORE_PRST)) { |
Willy Tarreau | ef4f391 | 2010-10-07 21:00:29 +0200 | [diff] [blame] | 5843 | /* the server is known, it's not the one the client requested, or the |
| 5844 | * cookie's last seen date needs to be refreshed. We have to |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5845 | * insert a set-cookie here, except if we want to insert only on POST |
| 5846 | * requests and this one isn't. Note that servers which don't have cookies |
| 5847 | * (eg: some backup servers) will return a full cookie removal request. |
| 5848 | */ |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 5849 | if (!objt_server(t->target)->cookie) { |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 5850 | chunk_printf(&trash, |
Willy Tarreau | ef4f391 | 2010-10-07 21:00:29 +0200 | [diff] [blame] | 5851 | "Set-Cookie: %s=; Expires=Thu, 01-Jan-1970 00:00:01 GMT; path=/", |
| 5852 | t->be->cookie_name); |
| 5853 | } |
| 5854 | else { |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 5855 | 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] | 5856 | |
| 5857 | if (t->be->cookie_maxidle || t->be->cookie_maxlife) { |
| 5858 | /* emit last_date, which is mandatory */ |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 5859 | trash.str[trash.len++] = COOKIE_DELIM_DATE; |
| 5860 | s30tob64((date.tv_sec+3) >> 2, trash.str + trash.len); |
| 5861 | trash.len += 5; |
| 5862 | |
Willy Tarreau | ef4f391 | 2010-10-07 21:00:29 +0200 | [diff] [blame] | 5863 | if (t->be->cookie_maxlife) { |
| 5864 | /* emit first_date, which is either the original one or |
| 5865 | * the current date. |
| 5866 | */ |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 5867 | trash.str[trash.len++] = COOKIE_DELIM_DATE; |
Willy Tarreau | ef4f391 | 2010-10-07 21:00:29 +0200 | [diff] [blame] | 5868 | s30tob64(txn->cookie_first_date ? |
| 5869 | txn->cookie_first_date >> 2 : |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 5870 | (date.tv_sec+3) >> 2, trash.str + trash.len); |
| 5871 | trash.len += 5; |
Willy Tarreau | ef4f391 | 2010-10-07 21:00:29 +0200 | [diff] [blame] | 5872 | } |
| 5873 | } |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 5874 | chunk_appendf(&trash, "; path=/"); |
Willy Tarreau | ef4f391 | 2010-10-07 21:00:29 +0200 | [diff] [blame] | 5875 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 5876 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5877 | if (t->be->cookie_domain) |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 5878 | chunk_appendf(&trash, "; domain=%s", t->be->cookie_domain); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 5879 | |
Willy Tarreau | 4992dd2 | 2012-05-31 21:02:17 +0200 | [diff] [blame] | 5880 | if (t->be->ck_opts & PR_CK_HTTPONLY) |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 5881 | chunk_appendf(&trash, "; HttpOnly"); |
Willy Tarreau | 4992dd2 | 2012-05-31 21:02:17 +0200 | [diff] [blame] | 5882 | |
| 5883 | if (t->be->ck_opts & PR_CK_SECURE) |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 5884 | chunk_appendf(&trash, "; Secure"); |
Willy Tarreau | 4992dd2 | 2012-05-31 21:02:17 +0200 | [diff] [blame] | 5885 | |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 5886 | 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] | 5887 | goto return_bad_resp; |
Willy Tarreau | ef4f391 | 2010-10-07 21:00:29 +0200 | [diff] [blame] | 5888 | |
Willy Tarreau | f134831 | 2010-10-07 15:54:11 +0200 | [diff] [blame] | 5889 | txn->flags &= ~TX_SCK_MASK; |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 5890 | if (objt_server(t->target)->cookie && (t->flags & SN_DIRECT)) |
Willy Tarreau | ef4f391 | 2010-10-07 21:00:29 +0200 | [diff] [blame] | 5891 | /* the server did not change, only the date was updated */ |
| 5892 | txn->flags |= TX_SCK_UPDATED; |
| 5893 | else |
| 5894 | txn->flags |= TX_SCK_INSERTED; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 5895 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5896 | /* Here, we will tell an eventual cache on the client side that we don't |
| 5897 | * want it to cache this reply because HTTP/1.0 caches also cache cookies ! |
| 5898 | * Some caches understand the correct form: 'no-cache="set-cookie"', but |
| 5899 | * others don't (eg: apache <= 1.3.26). So we use 'private' instead. |
| 5900 | */ |
Willy Tarreau | 6740213 | 2012-05-31 20:40:20 +0200 | [diff] [blame] | 5901 | if ((t->be->ck_opts & PR_CK_NOC) && (txn->flags & TX_CACHEABLE)) { |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 5902 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5903 | txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK; |
| 5904 | |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 5905 | if (unlikely(http_header_add_tail2(&txn->rsp, &txn->hdr_idx, |
Willy Tarreau | 58cc872 | 2009-12-28 06:57:33 +0100 | [diff] [blame] | 5906 | "Cache-control: private", 22) < 0)) |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5907 | goto return_bad_resp; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5908 | } |
| 5909 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 5910 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5911 | /* |
| 5912 | * 7: check if result will be cacheable with a cookie. |
| 5913 | * We'll block the response if security checks have caught |
| 5914 | * nasty things such as a cacheable cookie. |
| 5915 | */ |
Willy Tarreau | f134831 | 2010-10-07 15:54:11 +0200 | [diff] [blame] | 5916 | if (((txn->flags & (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) == |
| 5917 | (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) && |
Willy Tarreau | 63c9e5f | 2009-12-22 16:01:27 +0100 | [diff] [blame] | 5918 | (t->be->options & PR_O_CHK_CACHE)) { |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5919 | |
| 5920 | /* we're in presence of a cacheable response containing |
| 5921 | * a set-cookie header. We'll block it as requested by |
| 5922 | * the 'checkcache' option, and send an alert. |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5923 | */ |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 5924 | if (objt_server(t->target)) |
| 5925 | objt_server(t->target)->counters.failed_secu++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 5926 | |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 5927 | t->be->be_counters.denied_resp++; |
| 5928 | t->fe->fe_counters.denied_resp++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 5929 | if (t->listener->counters) |
| 5930 | t->listener->counters->denied_resp++; |
| 5931 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5932 | Alert("Blocking cacheable cookie in response from instance %s, server %s.\n", |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 5933 | 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] | 5934 | send_log(t->be, LOG_ALERT, |
| 5935 | "Blocking cacheable cookie in response from instance %s, server %s.\n", |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 5936 | 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] | 5937 | goto return_srv_prx_502; |
| 5938 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5939 | |
| 5940 | /* |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 5941 | * 8: adjust "Connection: close" or "Connection: keep-alive" if needed. |
Willy Tarreau | 50fc777 | 2012-11-11 22:19:57 +0100 | [diff] [blame] | 5942 | * If an "Upgrade" token is found, the header is left untouched in order |
| 5943 | * not to have to deal with some client bugs : some of them fail an upgrade |
| 5944 | * if anything but "Upgrade" is present in the Connection header. |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5945 | */ |
Willy Tarreau | 50fc777 | 2012-11-11 22:19:57 +0100 | [diff] [blame] | 5946 | if (!(txn->flags & TX_HDR_CONN_UPG) && |
| 5947 | (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) || |
Willy Tarreau | 02bce8b | 2014-01-30 00:15:28 +0100 | [diff] [blame] | 5948 | ((t->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL || |
| 5949 | (t->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL))) { |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 5950 | unsigned int want_flags = 0; |
| 5951 | |
| 5952 | if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL || |
| 5953 | (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) { |
| 5954 | /* we want a keep-alive response here. Keep-alive header |
| 5955 | * required if either side is not 1.1. |
| 5956 | */ |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 5957 | if (!(txn->req.flags & msg->flags & HTTP_MSGF_VER_11)) |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 5958 | want_flags |= TX_CON_KAL_SET; |
| 5959 | } |
| 5960 | else { |
| 5961 | /* we want a close response here. Close header required if |
| 5962 | * the server is 1.1, regardless of the client. |
| 5963 | */ |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 5964 | if (msg->flags & HTTP_MSGF_VER_11) |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 5965 | want_flags |= TX_CON_CLO_SET; |
| 5966 | } |
| 5967 | |
| 5968 | 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] | 5969 | http_change_connection_header(txn, msg, want_flags); |
Willy Tarreau | b608feb | 2010-01-02 22:47:18 +0100 | [diff] [blame] | 5970 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5971 | |
Willy Tarreau | 5843d1a | 2010-02-01 15:13:32 +0100 | [diff] [blame] | 5972 | skip_header_mangling: |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 5973 | if ((msg->flags & HTTP_MSGF_XFER_LEN) || |
Willy Tarreau | dc008c5 | 2010-02-01 16:20:08 +0100 | [diff] [blame] | 5974 | (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN) |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5975 | rep->analysers |= AN_RES_HTTP_XFER_BODY; |
Willy Tarreau | 0394594 | 2009-12-22 16:50:27 +0100 | [diff] [blame] | 5976 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5977 | /************************************************************* |
| 5978 | * OK, that's finished for the headers. We have done what we * |
| 5979 | * could. Let's switch to the DATA state. * |
| 5980 | ************************************************************/ |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 5981 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5982 | /* if the user wants to log as soon as possible, without counting |
| 5983 | * bytes from the server, then this is the right moment. We have |
| 5984 | * to temporarily assign bytes_out to log what we currently have. |
| 5985 | */ |
Willy Tarreau | d79a3b2 | 2012-12-28 09:40:16 +0100 | [diff] [blame] | 5986 | if (!LIST_ISEMPTY(&t->fe->logformat) && !(t->logs.logwait & LW_BYTES)) { |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5987 | t->logs.t_close = t->logs.t_data; /* to get a valid end date */ |
| 5988 | t->logs.bytes_out = txn->rsp.eoh; |
Willy Tarreau | a5555ec | 2008-11-30 19:02:32 +0100 | [diff] [blame] | 5989 | t->do_log(t); |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5990 | t->logs.bytes_out = 0; |
| 5991 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5992 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5993 | /* Note: we must not try to cheat by jumping directly to DATA, |
| 5994 | * otherwise we would not let the client side wake up. |
| 5995 | */ |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5996 | |
Willy Tarreau | e3fa6e5 | 2010-01-04 22:57:43 +0100 | [diff] [blame] | 5997 | return 1; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5998 | } |
Willy Tarreau | e3fa6e5 | 2010-01-04 22:57:43 +0100 | [diff] [blame] | 5999 | return 1; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 6000 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6001 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 6002 | /* This function is an analyser which forwards response body (including chunk |
| 6003 | * sizes if any). It is called as soon as we must forward, even if we forward |
| 6004 | * zero byte. The only situation where it must not be called is when we're in |
| 6005 | * tunnel mode and we want to forward till the close. It's used both to forward |
| 6006 | * remaining data and to resync after end of body. It expects the msg_state to |
| 6007 | * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to |
| 6008 | * 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] | 6009 | * 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] | 6010 | * bytes of pending data + the headers if not already done (between sol and sov). |
| 6011 | * 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] | 6012 | */ |
Willy Tarreau | 7421efb | 2012-07-02 15:11:27 +0200 | [diff] [blame] | 6013 | 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] | 6014 | { |
| 6015 | struct http_txn *txn = &s->txn; |
| 6016 | struct http_msg *msg = &s->txn.rsp; |
Willy Tarreau | ea95316 | 2012-05-18 23:41:28 +0200 | [diff] [blame] | 6017 | unsigned int bytes; |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 6018 | static struct buffer *tmpbuf = NULL; |
| 6019 | int compressing = 0; |
William Lallemand | bf3ae61 | 2012-11-19 12:35:37 +0100 | [diff] [blame] | 6020 | int consumed_data = 0; |
Willy Tarreau | d655ffe | 2013-04-02 01:48:58 +0200 | [diff] [blame] | 6021 | int ret; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 6022 | |
Willy Tarreau | f5c8bd6 | 2010-01-04 07:10:34 +0100 | [diff] [blame] | 6023 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) |
| 6024 | return 0; |
| 6025 | |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 6026 | 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] | 6027 | ((res->flags & CF_SHUTW) && (res->to_forward || res->buf->o)) || |
Willy Tarreau | 6c2cbe1 | 2010-01-03 17:07:49 +0100 | [diff] [blame] | 6028 | !s->req->analysers) { |
Willy Tarreau | 4fe4190 | 2010-06-07 22:27:41 +0200 | [diff] [blame] | 6029 | /* Output closed while we were sending data. We must abort and |
| 6030 | * wake the other side up. |
| 6031 | */ |
| 6032 | msg->msg_state = HTTP_MSG_ERROR; |
| 6033 | http_resync_states(s); |
Willy Tarreau | 082b01c | 2010-01-02 23:58:04 +0100 | [diff] [blame] | 6034 | return 1; |
| 6035 | } |
| 6036 | |
Willy Tarreau | 4fe4190 | 2010-06-07 22:27:41 +0200 | [diff] [blame] | 6037 | /* in most states, we should abort in case of early close */ |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 6038 | channel_auto_close(res); |
Willy Tarreau | b608feb | 2010-01-02 22:47:18 +0100 | [diff] [blame] | 6039 | |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 6040 | /* this is the first time we need the compression buffer */ |
| 6041 | if (s->comp_algo != NULL && tmpbuf == NULL) { |
| 6042 | if ((tmpbuf = pool_alloc2(pool2_buffer)) == NULL) |
| 6043 | goto aborted_xfer; /* no memory */ |
| 6044 | } |
| 6045 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 6046 | if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) { |
Willy Tarreau | fa4a03c | 2012-03-09 21:28:54 +0100 | [diff] [blame] | 6047 | /* 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] | 6048 | * rep->buf.p still points to the beginning of the message and msg->sol |
| 6049 | * is still null. We forward the headers, we don't need them. |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 6050 | */ |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 6051 | channel_forward(res, msg->sov); |
| 6052 | msg->next = 0; |
| 6053 | msg->sov = 0; |
Willy Tarreau | a458b67 | 2012-03-05 11:17:50 +0100 | [diff] [blame] | 6054 | |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 6055 | if (msg->flags & HTTP_MSGF_TE_CHNK) |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 6056 | msg->msg_state = HTTP_MSG_CHUNK_SIZE; |
Willy Tarreau | 54d23df | 2012-10-25 19:04:45 +0200 | [diff] [blame] | 6057 | else |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 6058 | msg->msg_state = HTTP_MSG_DATA; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 6059 | } |
| 6060 | |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 6061 | if (s->comp_algo != NULL) { |
Willy Tarreau | d655ffe | 2013-04-02 01:48:58 +0200 | [diff] [blame] | 6062 | 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] | 6063 | if (ret < 0) { |
| 6064 | res->flags |= CF_WAKE_WRITE; |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 6065 | goto missing_data; /* not enough spaces in buffers */ |
Willy Tarreau | 4afd70a | 2014-01-25 02:26:39 +0100 | [diff] [blame] | 6066 | } |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 6067 | compressing = 1; |
| 6068 | } |
| 6069 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 6070 | while (1) { |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 6071 | http_silent_debug(__LINE__, s); |
Willy Tarreau | ea95316 | 2012-05-18 23:41:28 +0200 | [diff] [blame] | 6072 | /* we may have some data pending between sol and sov */ |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 6073 | if (s->comp_algo == NULL) { |
| 6074 | bytes = msg->sov - msg->sol; |
| 6075 | if (msg->chunk_len || bytes) { |
| 6076 | msg->sol = msg->sov; |
| 6077 | msg->next -= bytes; /* will be forwarded */ |
| 6078 | msg->chunk_len += bytes; |
| 6079 | msg->chunk_len -= channel_forward(res, msg->chunk_len); |
| 6080 | } |
Willy Tarreau | 638cd02 | 2010-01-03 07:42:04 +0100 | [diff] [blame] | 6081 | } |
| 6082 | |
Willy Tarreau | d655ffe | 2013-04-02 01:48:58 +0200 | [diff] [blame] | 6083 | switch (msg->msg_state - HTTP_MSG_DATA) { |
| 6084 | case HTTP_MSG_DATA - HTTP_MSG_DATA: /* must still forward */ |
William Lallemand | bf3ae61 | 2012-11-19 12:35:37 +0100 | [diff] [blame] | 6085 | if (compressing) { |
| 6086 | consumed_data += ret = http_compression_buffer_add_data(s, res->buf, tmpbuf); |
| 6087 | if (ret < 0) |
| 6088 | goto aborted_xfer; |
| 6089 | } |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 6090 | |
Willy Tarreau | 4afd70a | 2014-01-25 02:26:39 +0100 | [diff] [blame] | 6091 | if (res->to_forward || msg->chunk_len) { |
| 6092 | res->flags |= CF_WAKE_WRITE; |
Willy Tarreau | f5c8bd6 | 2010-01-04 07:10:34 +0100 | [diff] [blame] | 6093 | goto missing_data; |
Willy Tarreau | 4afd70a | 2014-01-25 02:26:39 +0100 | [diff] [blame] | 6094 | } |
Willy Tarreau | caabe41 | 2010-01-03 23:08:28 +0100 | [diff] [blame] | 6095 | |
| 6096 | /* nothing left to forward */ |
William Lallemand | bf3ae61 | 2012-11-19 12:35:37 +0100 | [diff] [blame] | 6097 | if (msg->flags & HTTP_MSGF_TE_CHNK) { |
Willy Tarreau | 54d23df | 2012-10-25 19:04:45 +0200 | [diff] [blame] | 6098 | msg->msg_state = HTTP_MSG_CHUNK_CRLF; |
William Lallemand | bf3ae61 | 2012-11-19 12:35:37 +0100 | [diff] [blame] | 6099 | } else { |
Willy Tarreau | caabe41 | 2010-01-03 23:08:28 +0100 | [diff] [blame] | 6100 | msg->msg_state = HTTP_MSG_DONE; |
William Lallemand | bf3ae61 | 2012-11-19 12:35:37 +0100 | [diff] [blame] | 6101 | if (compressing && consumed_data) { |
| 6102 | http_compression_buffer_end(s, &res->buf, &tmpbuf, 1); |
| 6103 | compressing = 0; |
| 6104 | } |
Willy Tarreau | d655ffe | 2013-04-02 01:48:58 +0200 | [diff] [blame] | 6105 | break; |
William Lallemand | bf3ae61 | 2012-11-19 12:35:37 +0100 | [diff] [blame] | 6106 | } |
Willy Tarreau | d655ffe | 2013-04-02 01:48:58 +0200 | [diff] [blame] | 6107 | /* fall through for HTTP_MSG_CHUNK_CRLF */ |
| 6108 | |
| 6109 | case HTTP_MSG_CHUNK_CRLF - HTTP_MSG_DATA: |
| 6110 | /* we want the CRLF after the data */ |
| 6111 | |
| 6112 | ret = http_skip_chunk_crlf(msg); |
| 6113 | if (ret == 0) |
| 6114 | goto missing_data; |
| 6115 | else if (ret < 0) { |
| 6116 | if (msg->err_pos >= 0) |
| 6117 | http_capture_bad_message(&s->be->invalid_rep, s, msg, HTTP_MSG_CHUNK_CRLF, s->fe); |
| 6118 | goto return_bad_res; |
| 6119 | } |
| 6120 | /* skipping data in buffer for compression */ |
| 6121 | if (compressing) { |
| 6122 | b_adv(res->buf, msg->next); |
| 6123 | msg->next = 0; |
| 6124 | msg->sov = 0; |
| 6125 | msg->sol = 0; |
| 6126 | } |
| 6127 | /* we're in MSG_CHUNK_SIZE now, fall through */ |
| 6128 | |
| 6129 | case HTTP_MSG_CHUNK_SIZE - HTTP_MSG_DATA: |
Willy Tarreau | 124d991 | 2011-03-01 20:30:48 +0100 | [diff] [blame] | 6130 | /* read the chunk size and assign it to ->chunk_len, then |
Willy Tarreau | a458b67 | 2012-03-05 11:17:50 +0100 | [diff] [blame] | 6131 | * set ->sov and ->next to point to the body and switch to DATA or |
| 6132 | * TRAILERS state. |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 6133 | */ |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 6134 | |
Willy Tarreau | d655ffe | 2013-04-02 01:48:58 +0200 | [diff] [blame] | 6135 | ret = http_parse_chunk_size(msg); |
Willy Tarreau | 54d23df | 2012-10-25 19:04:45 +0200 | [diff] [blame] | 6136 | if (ret == 0) |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 6137 | goto missing_data; |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 6138 | else if (ret < 0) { |
| 6139 | if (msg->err_pos >= 0) |
Willy Tarreau | 8a0cef2 | 2012-03-09 13:39:23 +0100 | [diff] [blame] | 6140 | 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] | 6141 | goto return_bad_res; |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 6142 | } |
William Lallemand | bf3ae61 | 2012-11-19 12:35:37 +0100 | [diff] [blame] | 6143 | if (compressing) { |
| 6144 | if (likely(msg->chunk_len > 0)) { |
| 6145 | /* skipping data if we are in compression mode */ |
| 6146 | b_adv(res->buf, msg->next); |
| 6147 | msg->next = 0; |
| 6148 | msg->sov = 0; |
| 6149 | msg->sol = 0; |
| 6150 | } else { |
| 6151 | if (consumed_data) { |
| 6152 | http_compression_buffer_end(s, &res->buf, &tmpbuf, 1); |
| 6153 | compressing = 0; |
| 6154 | } |
| 6155 | } |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 6156 | } |
Willy Tarreau | 0161d62 | 2013-04-02 01:26:55 +0200 | [diff] [blame] | 6157 | /* otherwise we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state */ |
Willy Tarreau | d655ffe | 2013-04-02 01:48:58 +0200 | [diff] [blame] | 6158 | break; |
Willy Tarreau | 5523b32 | 2009-12-29 12:05:52 +0100 | [diff] [blame] | 6159 | |
Willy Tarreau | d655ffe | 2013-04-02 01:48:58 +0200 | [diff] [blame] | 6160 | case HTTP_MSG_TRAILERS - HTTP_MSG_DATA: |
| 6161 | ret = http_forward_trailers(msg); |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 6162 | if (ret == 0) |
| 6163 | goto missing_data; |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 6164 | else if (ret < 0) { |
| 6165 | if (msg->err_pos >= 0) |
Willy Tarreau | 8a0cef2 | 2012-03-09 13:39:23 +0100 | [diff] [blame] | 6166 | 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] | 6167 | goto return_bad_res; |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 6168 | } |
William Lallemand | 00bf1de | 2012-11-22 17:55:14 +0100 | [diff] [blame] | 6169 | if (s->comp_algo != NULL) { |
| 6170 | /* forwarding trailers */ |
| 6171 | channel_forward(res, msg->next); |
| 6172 | msg->next = 0; |
| 6173 | } |
Willy Tarreau | 2d43e18 | 2013-04-03 00:22:25 +0200 | [diff] [blame] | 6174 | /* we're in HTTP_MSG_DONE now, but we might still have |
| 6175 | * some data pending, so let's loop over once. |
| 6176 | */ |
| 6177 | break; |
Willy Tarreau | d655ffe | 2013-04-02 01:48:58 +0200 | [diff] [blame] | 6178 | |
| 6179 | default: |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 6180 | /* other states, DONE...TUNNEL */ |
Willy Tarreau | d655ffe | 2013-04-02 01:48:58 +0200 | [diff] [blame] | 6181 | |
| 6182 | ret = msg->msg_state; |
Willy Tarreau | 4fe4190 | 2010-06-07 22:27:41 +0200 | [diff] [blame] | 6183 | /* for keep-alive we don't want to forward closes on DONE */ |
Willy Tarreau | 92aa1fa | 2010-08-28 18:57:20 +0200 | [diff] [blame] | 6184 | if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL || |
| 6185 | (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 6186 | channel_dont_close(res); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 6187 | if (http_resync_states(s)) { |
| 6188 | http_silent_debug(__LINE__, s); |
| 6189 | /* some state changes occurred, maybe the analyser |
| 6190 | * was disabled too. |
Willy Tarreau | 5523b32 | 2009-12-29 12:05:52 +0100 | [diff] [blame] | 6191 | */ |
Willy Tarreau | 3fe693b | 2010-12-12 12:50:05 +0100 | [diff] [blame] | 6192 | if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) { |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 6193 | if (res->flags & CF_SHUTW) { |
Willy Tarreau | 3fe693b | 2010-12-12 12:50:05 +0100 | [diff] [blame] | 6194 | /* response errors are most likely due to |
| 6195 | * the client aborting the transfer. |
| 6196 | */ |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 6197 | goto aborted_xfer; |
Willy Tarreau | 3fe693b | 2010-12-12 12:50:05 +0100 | [diff] [blame] | 6198 | } |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 6199 | if (msg->err_pos >= 0) |
Willy Tarreau | d655ffe | 2013-04-02 01:48:58 +0200 | [diff] [blame] | 6200 | 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] | 6201 | goto return_bad_res; |
Willy Tarreau | 3fe693b | 2010-12-12 12:50:05 +0100 | [diff] [blame] | 6202 | } |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 6203 | return 1; |
Willy Tarreau | 5523b32 | 2009-12-29 12:05:52 +0100 | [diff] [blame] | 6204 | } |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 6205 | return 0; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 6206 | } |
| 6207 | } |
| 6208 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 6209 | missing_data: |
William Lallemand | bf3ae61 | 2012-11-19 12:35:37 +0100 | [diff] [blame] | 6210 | if (compressing && consumed_data) { |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 6211 | http_compression_buffer_end(s, &res->buf, &tmpbuf, 0); |
| 6212 | compressing = 0; |
| 6213 | } |
Willy Tarreau | f003d37 | 2012-11-26 13:35:37 +0100 | [diff] [blame] | 6214 | |
| 6215 | if (res->flags & CF_SHUTW) |
| 6216 | goto aborted_xfer; |
| 6217 | |
| 6218 | /* stop waiting for data if the input is closed before the end. If the |
| 6219 | * client side was already closed, it means that the client has aborted, |
| 6220 | * so we don't want to count this as a server abort. Otherwise it's a |
| 6221 | * server abort. |
| 6222 | */ |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 6223 | if (res->flags & CF_SHUTR) { |
Willy Tarreau | f003d37 | 2012-11-26 13:35:37 +0100 | [diff] [blame] | 6224 | if ((res->flags & CF_SHUTW_NOW) || (s->req->flags & CF_SHUTR)) |
| 6225 | goto aborted_xfer; |
Willy Tarreau | 40dba09 | 2010-03-04 18:14:51 +0100 | [diff] [blame] | 6226 | if (!(s->flags & SN_ERR_MASK)) |
| 6227 | s->flags |= SN_ERR_SRVCL; |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 6228 | s->be->be_counters.srv_aborts++; |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 6229 | if (objt_server(s->target)) |
| 6230 | objt_server(s->target)->counters.srv_aborts++; |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 6231 | goto return_bad_res_stats_ok; |
Willy Tarreau | 40dba09 | 2010-03-04 18:14:51 +0100 | [diff] [blame] | 6232 | } |
Willy Tarreau | f5c8bd6 | 2010-01-04 07:10:34 +0100 | [diff] [blame] | 6233 | |
Willy Tarreau | 40dba09 | 2010-03-04 18:14:51 +0100 | [diff] [blame] | 6234 | /* 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] | 6235 | if (!s->req->analysers) |
| 6236 | goto return_bad_res; |
| 6237 | |
Willy Tarreau | ea95316 | 2012-05-18 23:41:28 +0200 | [diff] [blame] | 6238 | /* forward any data pending between sol and sov */ |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 6239 | if (s->comp_algo == NULL) { |
| 6240 | bytes = msg->sov - msg->sol; |
| 6241 | if (msg->chunk_len || bytes) { |
| 6242 | msg->sol = msg->sov; |
| 6243 | msg->next -= bytes; /* will be forwarded */ |
| 6244 | msg->chunk_len += bytes; |
| 6245 | msg->chunk_len -= channel_forward(res, msg->chunk_len); |
| 6246 | } |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 6247 | } |
| 6248 | |
Willy Tarreau | 92aa1fa | 2010-08-28 18:57:20 +0200 | [diff] [blame] | 6249 | /* 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] | 6250 | * 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] | 6251 | * Similarly, with keep-alive on the client side, we don't want to forward a |
| 6252 | * close. |
| 6253 | */ |
Willy Tarreau | 08b4d79 | 2012-10-27 01:36:34 +0200 | [diff] [blame] | 6254 | if ((msg->flags & HTTP_MSGF_TE_CHNK) || s->comp_algo || |
Willy Tarreau | 92aa1fa | 2010-08-28 18:57:20 +0200 | [diff] [blame] | 6255 | (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL || |
| 6256 | (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 6257 | channel_dont_close(res); |
Willy Tarreau | 92aa1fa | 2010-08-28 18:57:20 +0200 | [diff] [blame] | 6258 | |
Willy Tarreau | 5c62092 | 2011-05-11 19:56:11 +0200 | [diff] [blame] | 6259 | /* 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] | 6260 | * 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] | 6261 | * 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] | 6262 | * modes are already handled by the stream sock layer. We must not do |
| 6263 | * this in content-length mode because it could present the MSG_MORE |
| 6264 | * flag with the last block of forwarded data, which would cause an |
| 6265 | * additional delay to be observed by the receiver. |
Willy Tarreau | 5c62092 | 2011-05-11 19:56:11 +0200 | [diff] [blame] | 6266 | */ |
Willy Tarreau | 08b4d79 | 2012-10-27 01:36:34 +0200 | [diff] [blame] | 6267 | if ((msg->flags & HTTP_MSGF_TE_CHNK) || s->comp_algo) |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 6268 | res->flags |= CF_EXPECT_MORE; |
Willy Tarreau | 5c62092 | 2011-05-11 19:56:11 +0200 | [diff] [blame] | 6269 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 6270 | /* the session handler will take care of timeouts and errors */ |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 6271 | http_silent_debug(__LINE__, s); |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 6272 | return 0; |
| 6273 | |
Willy Tarreau | 40dba09 | 2010-03-04 18:14:51 +0100 | [diff] [blame] | 6274 | return_bad_res: /* let's centralize all bad responses */ |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 6275 | s->be->be_counters.failed_resp++; |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 6276 | if (objt_server(s->target)) |
| 6277 | objt_server(s->target)->counters.failed_resp++; |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 6278 | |
| 6279 | return_bad_res_stats_ok: |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 6280 | txn->rsp.msg_state = HTTP_MSG_ERROR; |
Willy Tarreau | 148d099 | 2010-01-10 10:21:21 +0100 | [diff] [blame] | 6281 | /* don't send any error message as we're in the body */ |
| 6282 | stream_int_retnclose(res->cons, NULL); |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 6283 | res->analysers = 0; |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 6284 | 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] | 6285 | if (objt_server(s->target)) |
| 6286 | health_adjust(objt_server(s->target), HANA_STATUS_HTTP_HDRRSP); |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 6287 | |
| 6288 | if (!(s->flags & SN_ERR_MASK)) |
| 6289 | s->flags |= SN_ERR_PRXCOND; |
| 6290 | if (!(s->flags & SN_FINST_MASK)) |
Willy Tarreau | 40dba09 | 2010-03-04 18:14:51 +0100 | [diff] [blame] | 6291 | s->flags |= SN_FINST_D; |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 6292 | return 0; |
| 6293 | |
| 6294 | aborted_xfer: |
| 6295 | txn->rsp.msg_state = HTTP_MSG_ERROR; |
| 6296 | /* don't send any error message as we're in the body */ |
| 6297 | stream_int_retnclose(res->cons, NULL); |
| 6298 | res->analysers = 0; |
| 6299 | s->req->analysers = 0; /* we're in data phase, we want to abort both directions */ |
| 6300 | |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 6301 | s->fe->fe_counters.cli_aborts++; |
| 6302 | s->be->be_counters.cli_aborts++; |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 6303 | if (objt_server(s->target)) |
| 6304 | objt_server(s->target)->counters.cli_aborts++; |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 6305 | |
| 6306 | if (!(s->flags & SN_ERR_MASK)) |
| 6307 | s->flags |= SN_ERR_CLICL; |
| 6308 | if (!(s->flags & SN_FINST_MASK)) |
| 6309 | s->flags |= SN_FINST_D; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 6310 | return 0; |
| 6311 | } |
| 6312 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6313 | /* Iterate the same filter through all request headers. |
| 6314 | * Returns 1 if this filter can be stopped upon return, otherwise 0. |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6315 | * Since it can manage the switch to another backend, it updates the per-proxy |
| 6316 | * DENY stats. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6317 | */ |
Willy Tarreau | 7421efb | 2012-07-02 15:11:27 +0200 | [diff] [blame] | 6318 | 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] | 6319 | { |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6320 | char term; |
| 6321 | char *cur_ptr, *cur_end, *cur_next; |
| 6322 | int cur_idx, old_idx, last_hdr; |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 6323 | struct http_txn *txn = &t->txn; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6324 | struct hdr_idx_elem *cur_hdr; |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 6325 | int delta; |
Willy Tarreau | 0f7562b | 2007-01-07 15:46:13 +0100 | [diff] [blame] | 6326 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6327 | last_hdr = 0; |
| 6328 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 6329 | cur_next = req->buf->p + hdr_idx_first_pos(&txn->hdr_idx); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6330 | old_idx = 0; |
| 6331 | |
| 6332 | while (!last_hdr) { |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 6333 | if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT))) |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6334 | return 1; |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 6335 | else if (unlikely(txn->flags & TX_CLALLOW) && |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6336 | (exp->action == ACT_ALLOW || |
| 6337 | exp->action == ACT_DENY || |
| 6338 | exp->action == ACT_TARPIT)) |
| 6339 | return 0; |
| 6340 | |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 6341 | cur_idx = txn->hdr_idx.v[old_idx].next; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6342 | if (!cur_idx) |
| 6343 | break; |
| 6344 | |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 6345 | cur_hdr = &txn->hdr_idx.v[cur_idx]; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6346 | cur_ptr = cur_next; |
| 6347 | cur_end = cur_ptr + cur_hdr->len; |
| 6348 | cur_next = cur_end + cur_hdr->cr + 1; |
| 6349 | |
| 6350 | /* Now we have one header between cur_ptr and cur_end, |
| 6351 | * and the next header starts at cur_next. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6352 | */ |
| 6353 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6354 | /* The annoying part is that pattern matching needs |
| 6355 | * that we modify the contents to null-terminate all |
| 6356 | * strings before testing them. |
| 6357 | */ |
| 6358 | |
| 6359 | term = *cur_end; |
| 6360 | *cur_end = '\0'; |
| 6361 | |
| 6362 | if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) { |
| 6363 | switch (exp->action) { |
| 6364 | case ACT_SETBE: |
| 6365 | /* It is not possible to jump a second time. |
| 6366 | * FIXME: should we return an HTTP/500 here so that |
| 6367 | * the admin knows there's a problem ? |
| 6368 | */ |
| 6369 | if (t->be != t->fe) |
| 6370 | break; |
| 6371 | |
| 6372 | /* Swithing Proxy */ |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 6373 | session_set_backend(t, (struct proxy *)exp->replace); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6374 | last_hdr = 1; |
| 6375 | break; |
| 6376 | |
| 6377 | case ACT_ALLOW: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 6378 | txn->flags |= TX_CLALLOW; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6379 | last_hdr = 1; |
| 6380 | break; |
| 6381 | |
| 6382 | case ACT_DENY: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 6383 | txn->flags |= TX_CLDENY; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6384 | last_hdr = 1; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6385 | break; |
| 6386 | |
| 6387 | case ACT_TARPIT: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 6388 | txn->flags |= TX_CLTARPIT; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6389 | last_hdr = 1; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6390 | break; |
| 6391 | |
| 6392 | case ACT_REPLACE: |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 6393 | trash.len = exp_replace(trash.str, cur_ptr, exp->replace, pmatch); |
| 6394 | 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] | 6395 | /* FIXME: if the user adds a newline in the replacement, the |
| 6396 | * index will not be recalculated for now, and the new line |
| 6397 | * will not be counted as a new header. |
| 6398 | */ |
| 6399 | |
| 6400 | cur_end += delta; |
| 6401 | cur_next += delta; |
| 6402 | cur_hdr->len += delta; |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 6403 | http_msg_move_end(&txn->req, delta); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6404 | break; |
| 6405 | |
| 6406 | case ACT_REMOVE: |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 6407 | delta = buffer_replace2(req->buf, cur_ptr, cur_next, NULL, 0); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6408 | cur_next += delta; |
| 6409 | |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 6410 | http_msg_move_end(&txn->req, delta); |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 6411 | txn->hdr_idx.v[old_idx].next = cur_hdr->next; |
| 6412 | txn->hdr_idx.used--; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6413 | cur_hdr->len = 0; |
| 6414 | cur_end = NULL; /* null-term has been rewritten */ |
Willy Tarreau | 26db59e | 2010-11-28 06:57:24 +0100 | [diff] [blame] | 6415 | cur_idx = old_idx; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6416 | break; |
| 6417 | |
| 6418 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6419 | } |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6420 | if (cur_end) |
| 6421 | *cur_end = term; /* restore the string terminator */ |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6422 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6423 | /* keep the link from this header to next one in case of later |
| 6424 | * removal of next header. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6425 | */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6426 | old_idx = cur_idx; |
| 6427 | } |
| 6428 | return 0; |
| 6429 | } |
| 6430 | |
| 6431 | |
| 6432 | /* Apply the filter to the request line. |
| 6433 | * Returns 0 if nothing has been done, 1 if the filter has been applied, |
| 6434 | * or -1 if a replacement resulted in an invalid request line. |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6435 | * Since it can manage the switch to another backend, it updates the per-proxy |
| 6436 | * DENY stats. |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6437 | */ |
Willy Tarreau | 7421efb | 2012-07-02 15:11:27 +0200 | [diff] [blame] | 6438 | 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] | 6439 | { |
| 6440 | char term; |
| 6441 | char *cur_ptr, *cur_end; |
| 6442 | int done; |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 6443 | struct http_txn *txn = &t->txn; |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 6444 | int delta; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6445 | |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 6446 | if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT))) |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6447 | return 1; |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 6448 | else if (unlikely(txn->flags & TX_CLALLOW) && |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6449 | (exp->action == ACT_ALLOW || |
| 6450 | exp->action == ACT_DENY || |
| 6451 | exp->action == ACT_TARPIT)) |
| 6452 | return 0; |
| 6453 | else if (exp->action == ACT_REMOVE) |
| 6454 | return 0; |
| 6455 | |
| 6456 | done = 0; |
| 6457 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 6458 | cur_ptr = req->buf->p; |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 6459 | cur_end = cur_ptr + txn->req.sl.rq.l; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6460 | |
| 6461 | /* Now we have the request line between cur_ptr and cur_end */ |
| 6462 | |
| 6463 | /* The annoying part is that pattern matching needs |
| 6464 | * that we modify the contents to null-terminate all |
| 6465 | * strings before testing them. |
| 6466 | */ |
| 6467 | |
| 6468 | term = *cur_end; |
| 6469 | *cur_end = '\0'; |
| 6470 | |
| 6471 | if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) { |
| 6472 | switch (exp->action) { |
| 6473 | case ACT_SETBE: |
| 6474 | /* It is not possible to jump a second time. |
| 6475 | * FIXME: should we return an HTTP/500 here so that |
| 6476 | * the admin knows there's a problem ? |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6477 | */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6478 | if (t->be != t->fe) |
| 6479 | break; |
| 6480 | |
| 6481 | /* Swithing Proxy */ |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 6482 | session_set_backend(t, (struct proxy *)exp->replace); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6483 | done = 1; |
| 6484 | break; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6485 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6486 | case ACT_ALLOW: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 6487 | txn->flags |= TX_CLALLOW; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6488 | done = 1; |
| 6489 | break; |
Willy Tarreau | a496b60 | 2006-12-17 23:15:24 +0100 | [diff] [blame] | 6490 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6491 | case ACT_DENY: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 6492 | txn->flags |= TX_CLDENY; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6493 | done = 1; |
| 6494 | break; |
Willy Tarreau | a496b60 | 2006-12-17 23:15:24 +0100 | [diff] [blame] | 6495 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6496 | case ACT_TARPIT: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 6497 | txn->flags |= TX_CLTARPIT; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6498 | done = 1; |
| 6499 | break; |
Willy Tarreau | a496b60 | 2006-12-17 23:15:24 +0100 | [diff] [blame] | 6500 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6501 | case ACT_REPLACE: |
| 6502 | *cur_end = term; /* restore the string terminator */ |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 6503 | trash.len = exp_replace(trash.str, cur_ptr, exp->replace, pmatch); |
| 6504 | 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] | 6505 | /* FIXME: if the user adds a newline in the replacement, the |
| 6506 | * index will not be recalculated for now, and the new line |
| 6507 | * will not be counted as a new header. |
| 6508 | */ |
Willy Tarreau | a496b60 | 2006-12-17 23:15:24 +0100 | [diff] [blame] | 6509 | |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 6510 | http_msg_move_end(&txn->req, delta); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6511 | cur_end += delta; |
Willy Tarreau | 69d8c5d | 2012-05-08 09:44:41 +0200 | [diff] [blame] | 6512 | cur_end = (char *)http_parse_reqline(&txn->req, |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6513 | HTTP_MSG_RQMETH, |
| 6514 | cur_ptr, cur_end + 1, |
| 6515 | NULL, NULL); |
| 6516 | if (unlikely(!cur_end)) |
| 6517 | return -1; |
Willy Tarreau | a496b60 | 2006-12-17 23:15:24 +0100 | [diff] [blame] | 6518 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6519 | /* we have a full request and we know that we have either a CR |
| 6520 | * or an LF at <ptr>. |
| 6521 | */ |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 6522 | txn->meth = find_http_meth(cur_ptr, txn->req.sl.rq.m_l); |
| 6523 | 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] | 6524 | /* there is no point trying this regex on headers */ |
| 6525 | return 1; |
| 6526 | } |
| 6527 | } |
| 6528 | *cur_end = term; /* restore the string terminator */ |
| 6529 | return done; |
| 6530 | } |
Willy Tarreau | 97de624 | 2006-12-27 17:18:38 +0100 | [diff] [blame] | 6531 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6532 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6533 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6534 | /* |
Willy Tarreau | 6c123b1 | 2010-01-28 20:22:06 +0100 | [diff] [blame] | 6535 | * 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] | 6536 | * 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] | 6537 | * unparsable request. Since it can manage the switch to another backend, it |
| 6538 | * updates the per-proxy DENY stats. |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6539 | */ |
Willy Tarreau | 7421efb | 2012-07-02 15:11:27 +0200 | [diff] [blame] | 6540 | 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] | 6541 | { |
Willy Tarreau | 6c123b1 | 2010-01-28 20:22:06 +0100 | [diff] [blame] | 6542 | struct http_txn *txn = &s->txn; |
| 6543 | struct hdr_exp *exp; |
| 6544 | |
| 6545 | for (exp = px->req_exp; exp; exp = exp->next) { |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6546 | int ret; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6547 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6548 | /* |
| 6549 | * The interleaving of transformations and verdicts |
| 6550 | * makes it difficult to decide to continue or stop |
| 6551 | * the evaluation. |
| 6552 | */ |
| 6553 | |
Willy Tarreau | 6c123b1 | 2010-01-28 20:22:06 +0100 | [diff] [blame] | 6554 | if (txn->flags & (TX_CLDENY|TX_CLTARPIT)) |
| 6555 | break; |
| 6556 | |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 6557 | if ((txn->flags & TX_CLALLOW) && |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6558 | (exp->action == ACT_ALLOW || exp->action == ACT_DENY || |
Willy Tarreau | 6c123b1 | 2010-01-28 20:22:06 +0100 | [diff] [blame] | 6559 | exp->action == ACT_TARPIT || exp->action == ACT_PASS)) |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6560 | continue; |
Willy Tarreau | 6c123b1 | 2010-01-28 20:22:06 +0100 | [diff] [blame] | 6561 | |
| 6562 | /* if this filter had a condition, evaluate it now and skip to |
| 6563 | * next filter if the condition does not match. |
| 6564 | */ |
| 6565 | if (exp->cond) { |
Willy Tarreau | 32a6f2e | 2012-04-25 10:13:36 +0200 | [diff] [blame] | 6566 | 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] | 6567 | ret = acl_pass(ret); |
| 6568 | if (((struct acl_cond *)exp->cond)->pol == ACL_COND_UNLESS) |
| 6569 | ret = !ret; |
| 6570 | |
| 6571 | if (!ret) |
| 6572 | continue; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6573 | } |
| 6574 | |
| 6575 | /* Apply the filter to the request line. */ |
Willy Tarreau | 6c123b1 | 2010-01-28 20:22:06 +0100 | [diff] [blame] | 6576 | ret = apply_filter_to_req_line(s, req, exp); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6577 | if (unlikely(ret < 0)) |
| 6578 | return -1; |
| 6579 | |
| 6580 | if (likely(ret == 0)) { |
| 6581 | /* The filter did not match the request, it can be |
| 6582 | * iterated through all headers. |
| 6583 | */ |
Willy Tarreau | 6c123b1 | 2010-01-28 20:22:06 +0100 | [diff] [blame] | 6584 | apply_filter_to_req_headers(s, req, exp); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6585 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6586 | } |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6587 | return 0; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6588 | } |
| 6589 | |
| 6590 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6591 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6592 | /* |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 6593 | * Try to retrieve the server associated to the appsession. |
| 6594 | * If the server is found, it's assigned to the session. |
| 6595 | */ |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 6596 | 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] | 6597 | struct http_txn *txn = &t->txn; |
| 6598 | appsess *asession = NULL; |
| 6599 | char *sessid_temp = NULL; |
| 6600 | |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 6601 | if (len > t->be->appsession_len) { |
| 6602 | len = t->be->appsession_len; |
| 6603 | } |
| 6604 | |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 6605 | if (t->be->options2 & PR_O2_AS_REQL) { |
| 6606 | /* 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] | 6607 | if (txn->sessid != NULL) { |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 6608 | /* 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] | 6609 | pool_free2(apools.sessid, txn->sessid); |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 6610 | } |
| 6611 | |
Willy Tarreau | a3377ee | 2010-01-10 10:49:11 +0100 | [diff] [blame] | 6612 | if ((txn->sessid = pool_alloc2(apools.sessid)) == NULL) { |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 6613 | Alert("Not enough memory process_cli():asession->sessid:malloc().\n"); |
| 6614 | send_log(t->be, LOG_ALERT, "Not enough memory process_cli():asession->sessid:malloc().\n"); |
| 6615 | return; |
| 6616 | } |
| 6617 | |
Willy Tarreau | a3377ee | 2010-01-10 10:49:11 +0100 | [diff] [blame] | 6618 | memcpy(txn->sessid, buf, len); |
| 6619 | txn->sessid[len] = 0; |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 6620 | } |
| 6621 | |
| 6622 | if ((sessid_temp = pool_alloc2(apools.sessid)) == NULL) { |
| 6623 | Alert("Not enough memory process_cli():asession->sessid:malloc().\n"); |
| 6624 | send_log(t->be, LOG_ALERT, "Not enough memory process_cli():asession->sessid:malloc().\n"); |
| 6625 | return; |
| 6626 | } |
| 6627 | |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 6628 | memcpy(sessid_temp, buf, len); |
| 6629 | sessid_temp[len] = 0; |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 6630 | |
| 6631 | asession = appsession_hash_lookup(&(t->be->htbl_proxy), sessid_temp); |
| 6632 | /* free previously allocated memory */ |
| 6633 | pool_free2(apools.sessid, sessid_temp); |
| 6634 | |
| 6635 | if (asession != NULL) { |
| 6636 | asession->expire = tick_add_ifset(now_ms, t->be->timeout.appsession); |
| 6637 | if (!(t->be->options2 & PR_O2_AS_REQL)) |
| 6638 | asession->request_count++; |
| 6639 | |
| 6640 | if (asession->serverid != NULL) { |
| 6641 | struct server *srv = t->be->srv; |
Cyril Bonté | 47fdd8e | 2010-04-25 00:00:51 +0200 | [diff] [blame] | 6642 | |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 6643 | while (srv) { |
| 6644 | if (strcmp(srv->id, asession->serverid) == 0) { |
Willy Tarreau | 4de9149 | 2010-01-22 19:10:05 +0100 | [diff] [blame] | 6645 | if ((srv->state & SRV_RUNNING) || |
| 6646 | (t->be->options & PR_O_PERSIST) || |
| 6647 | (t->flags & SN_FORCE_PRST)) { |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 6648 | /* we found the server and it's usable */ |
| 6649 | txn->flags &= ~TX_CK_MASK; |
Willy Tarreau | 2a6d88d | 2010-01-24 13:10:43 +0100 | [diff] [blame] | 6650 | txn->flags |= (srv->state & SRV_RUNNING) ? TX_CK_VALID : TX_CK_DOWN; |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 6651 | t->flags |= SN_DIRECT | SN_ASSIGNED; |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 6652 | t->target = &srv->obj_type; |
Willy Tarreau | 664beb8 | 2011-03-10 11:38:29 +0100 | [diff] [blame] | 6653 | |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 6654 | break; |
| 6655 | } else { |
| 6656 | txn->flags &= ~TX_CK_MASK; |
| 6657 | txn->flags |= TX_CK_DOWN; |
| 6658 | } |
| 6659 | } |
| 6660 | srv = srv->next; |
| 6661 | } |
| 6662 | } |
| 6663 | } |
| 6664 | } |
| 6665 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6666 | /* Find the end of a cookie value contained between <s> and <e>. It works the |
| 6667 | * same way as with headers above except that the semi-colon also ends a token. |
| 6668 | * See RFC2965 for more information. Note that it requires a valid header to |
| 6669 | * return a valid result. |
| 6670 | */ |
| 6671 | char *find_cookie_value_end(char *s, const char *e) |
| 6672 | { |
| 6673 | int quoted, qdpair; |
| 6674 | |
| 6675 | quoted = qdpair = 0; |
| 6676 | for (; s < e; s++) { |
| 6677 | if (qdpair) qdpair = 0; |
| 6678 | else if (quoted) { |
| 6679 | if (*s == '\\') qdpair = 1; |
| 6680 | else if (*s == '"') quoted = 0; |
| 6681 | } |
| 6682 | else if (*s == '"') quoted = 1; |
| 6683 | else if (*s == ',' || *s == ';') return s; |
| 6684 | } |
| 6685 | return s; |
| 6686 | } |
| 6687 | |
| 6688 | /* Delete a value in a header between delimiters <from> and <next> in buffer |
| 6689 | * <buf>. The number of characters displaced is returned, and the pointer to |
| 6690 | * the first delimiter is updated if required. The function tries as much as |
| 6691 | * possible to respect the following principles : |
| 6692 | * - replace <from> delimiter by the <next> one unless <from> points to a |
| 6693 | * colon, in which case <next> is simply removed |
| 6694 | * - set exactly one space character after the new first delimiter, unless |
| 6695 | * there are not enough characters in the block being moved to do so. |
| 6696 | * - remove unneeded spaces before the previous delimiter and after the new |
| 6697 | * one. |
| 6698 | * |
| 6699 | * It is the caller's responsibility to ensure that : |
| 6700 | * - <from> points to a valid delimiter or the colon ; |
| 6701 | * - <next> points to a valid delimiter or the final CR/LF ; |
| 6702 | * - there are non-space chars before <from> ; |
| 6703 | * - there is a CR/LF at or after <next>. |
| 6704 | */ |
Willy Tarreau | af81935 | 2012-08-27 22:08:00 +0200 | [diff] [blame] | 6705 | int del_hdr_value(struct buffer *buf, char **from, char *next) |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6706 | { |
| 6707 | char *prev = *from; |
| 6708 | |
| 6709 | if (*prev == ':') { |
| 6710 | /* We're removing the first value, preserve the colon and add a |
| 6711 | * space if possible. |
| 6712 | */ |
| 6713 | if (!http_is_crlf[(unsigned char)*next]) |
| 6714 | next++; |
| 6715 | prev++; |
| 6716 | if (prev < next) |
| 6717 | *prev++ = ' '; |
| 6718 | |
| 6719 | while (http_is_spht[(unsigned char)*next]) |
| 6720 | next++; |
| 6721 | } else { |
| 6722 | /* Remove useless spaces before the old delimiter. */ |
| 6723 | while (http_is_spht[(unsigned char)*(prev-1)]) |
| 6724 | prev--; |
| 6725 | *from = prev; |
| 6726 | |
| 6727 | /* copy the delimiter and if possible a space if we're |
| 6728 | * not at the end of the line. |
| 6729 | */ |
| 6730 | if (!http_is_crlf[(unsigned char)*next]) { |
| 6731 | *prev++ = *next++; |
| 6732 | if (prev + 1 < next) |
| 6733 | *prev++ = ' '; |
| 6734 | while (http_is_spht[(unsigned char)*next]) |
| 6735 | next++; |
| 6736 | } |
| 6737 | } |
| 6738 | return buffer_replace2(buf, prev, next, NULL, 0); |
| 6739 | } |
| 6740 | |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 6741 | /* |
Willy Tarreau | 396d2c6 | 2007-11-04 19:30:00 +0100 | [diff] [blame] | 6742 | * 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] | 6743 | * desirable to call it only when needed. This code is quite complex because |
| 6744 | * of the multiple very crappy and ambiguous syntaxes we have to support. it |
| 6745 | * highly recommended not to touch this part without a good reason ! |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6746 | */ |
Willy Tarreau | 7421efb | 2012-07-02 15:11:27 +0200 | [diff] [blame] | 6747 | void manage_client_side_cookies(struct session *t, struct channel *req) |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6748 | { |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 6749 | struct http_txn *txn = &t->txn; |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6750 | int preserve_hdr; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6751 | int cur_idx, old_idx; |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6752 | char *hdr_beg, *hdr_end, *hdr_next, *del_from; |
| 6753 | char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6754 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6755 | /* Iterate through the headers, we start with the start line. */ |
Willy Tarreau | 83969f4 | 2007-01-22 08:55:47 +0100 | [diff] [blame] | 6756 | old_idx = 0; |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 6757 | hdr_next = req->buf->p + hdr_idx_first_pos(&txn->hdr_idx); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6758 | |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 6759 | while ((cur_idx = txn->hdr_idx.v[old_idx].next)) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6760 | struct hdr_idx_elem *cur_hdr; |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 6761 | int val; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6762 | |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 6763 | cur_hdr = &txn->hdr_idx.v[cur_idx]; |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6764 | hdr_beg = hdr_next; |
| 6765 | hdr_end = hdr_beg + cur_hdr->len; |
| 6766 | hdr_next = hdr_end + cur_hdr->cr + 1; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6767 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6768 | /* We have one full header between hdr_beg and hdr_end, and the |
| 6769 | * next header starts at hdr_next. We're only interested in |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6770 | * "Cookie:" headers. |
| 6771 | */ |
| 6772 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6773 | val = http_header_match2(hdr_beg, hdr_end, "Cookie", 6); |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 6774 | if (!val) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6775 | old_idx = cur_idx; |
| 6776 | continue; |
| 6777 | } |
| 6778 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6779 | del_from = NULL; /* nothing to be deleted */ |
| 6780 | preserve_hdr = 0; /* assume we may kill the whole header */ |
| 6781 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6782 | /* Now look for cookies. Conforming to RFC2109, we have to support |
| 6783 | * attributes whose name begin with a '$', and associate them with |
| 6784 | * the right cookie, if we want to delete this cookie. |
| 6785 | * So there are 3 cases for each cookie read : |
| 6786 | * 1) it's a special attribute, beginning with a '$' : ignore it. |
| 6787 | * 2) it's a server id cookie that we *MAY* want to delete : save |
| 6788 | * some pointers on it (last semi-colon, beginning of cookie...) |
| 6789 | * 3) it's an application cookie : we *MAY* have to delete a previous |
| 6790 | * "special" cookie. |
| 6791 | * At the end of loop, if a "special" cookie remains, we may have to |
| 6792 | * remove it. If no application cookie persists in the header, we |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6793 | * *MUST* delete it. |
| 6794 | * |
| 6795 | * Note: RFC2965 is unclear about the processing of spaces around |
| 6796 | * the equal sign in the ATTR=VALUE form. A careful inspection of |
| 6797 | * the RFC explicitly allows spaces before it, and not within the |
| 6798 | * tokens (attrs or values). An inspection of RFC2109 allows that |
| 6799 | * too but section 10.1.3 lets one think that spaces may be allowed |
| 6800 | * after the equal sign too, resulting in some (rare) buggy |
| 6801 | * implementations trying to do that. So let's do what servers do. |
| 6802 | * Latest ietf draft forbids spaces all around. Also, earlier RFCs |
| 6803 | * allowed quoted strings in values, with any possible character |
| 6804 | * after a backslash, including control chars and delimitors, which |
| 6805 | * causes parsing to become ambiguous. Browsers also allow spaces |
| 6806 | * within values even without quotes. |
| 6807 | * |
| 6808 | * We have to keep multiple pointers in order to support cookie |
| 6809 | * removal at the beginning, middle or end of header without |
| 6810 | * corrupting the header. All of these headers are valid : |
| 6811 | * |
| 6812 | * Cookie:NAME1=VALUE1;NAME2=VALUE2;NAME3=VALUE3\r\n |
| 6813 | * Cookie:NAME1=VALUE1;NAME2_ONLY ;NAME3=VALUE3\r\n |
| 6814 | * Cookie: NAME1 = VALUE 1 ; NAME2 = VALUE2 ; NAME3 = VALUE3\r\n |
| 6815 | * | | | | | | | | | |
| 6816 | * | | | | | | | | hdr_end <--+ |
| 6817 | * | | | | | | | +--> next |
| 6818 | * | | | | | | +----> val_end |
| 6819 | * | | | | | +-----------> val_beg |
| 6820 | * | | | | +--------------> equal |
| 6821 | * | | | +----------------> att_end |
| 6822 | * | | +---------------------> att_beg |
| 6823 | * | +--------------------------> prev |
| 6824 | * +--------------------------------> hdr_beg |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6825 | */ |
| 6826 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6827 | for (prev = hdr_beg + 6; prev < hdr_end; prev = next) { |
| 6828 | /* Iterate through all cookies on this line */ |
| 6829 | |
| 6830 | /* find att_beg */ |
| 6831 | att_beg = prev + 1; |
| 6832 | while (att_beg < hdr_end && http_is_spht[(unsigned char)*att_beg]) |
| 6833 | att_beg++; |
| 6834 | |
| 6835 | /* find att_end : this is the first character after the last non |
| 6836 | * space before the equal. It may be equal to hdr_end. |
| 6837 | */ |
| 6838 | equal = att_end = att_beg; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6839 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6840 | while (equal < hdr_end) { |
| 6841 | if (*equal == '=' || *equal == ',' || *equal == ';') |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6842 | break; |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6843 | if (http_is_spht[(unsigned char)*equal++]) |
| 6844 | continue; |
| 6845 | att_end = equal; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6846 | } |
| 6847 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6848 | /* here, <equal> points to '=', a delimitor or the end. <att_end> |
| 6849 | * is between <att_beg> and <equal>, both may be identical. |
| 6850 | */ |
| 6851 | |
| 6852 | /* look for end of cookie if there is an equal sign */ |
| 6853 | if (equal < hdr_end && *equal == '=') { |
| 6854 | /* look for the beginning of the value */ |
| 6855 | val_beg = equal + 1; |
| 6856 | while (val_beg < hdr_end && http_is_spht[(unsigned char)*val_beg]) |
| 6857 | val_beg++; |
| 6858 | |
| 6859 | /* find the end of the value, respecting quotes */ |
| 6860 | next = find_cookie_value_end(val_beg, hdr_end); |
| 6861 | |
| 6862 | /* make val_end point to the first white space or delimitor after the value */ |
| 6863 | val_end = next; |
| 6864 | while (val_end > val_beg && http_is_spht[(unsigned char)*(val_end - 1)]) |
| 6865 | val_end--; |
| 6866 | } else { |
| 6867 | val_beg = val_end = next = equal; |
Willy Tarreau | 305ae85 | 2010-01-03 19:45:54 +0100 | [diff] [blame] | 6868 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6869 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6870 | /* We have nothing to do with attributes beginning with '$'. However, |
| 6871 | * they will automatically be removed if a header before them is removed, |
| 6872 | * since they're supposed to be linked together. |
| 6873 | */ |
| 6874 | if (*att_beg == '$') |
| 6875 | continue; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6876 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6877 | /* Ignore cookies with no equal sign */ |
| 6878 | if (equal == next) { |
| 6879 | /* This is not our cookie, so we must preserve it. But if we already |
| 6880 | * scheduled another cookie for removal, we cannot remove the |
| 6881 | * complete header, but we can remove the previous block itself. |
| 6882 | */ |
| 6883 | preserve_hdr = 1; |
| 6884 | if (del_from != NULL) { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 6885 | int delta = del_hdr_value(req->buf, &del_from, prev); |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6886 | val_end += delta; |
| 6887 | next += delta; |
| 6888 | hdr_end += delta; |
| 6889 | hdr_next += delta; |
| 6890 | cur_hdr->len += delta; |
| 6891 | http_msg_move_end(&txn->req, delta); |
| 6892 | prev = del_from; |
| 6893 | del_from = NULL; |
| 6894 | } |
| 6895 | continue; |
Willy Tarreau | 305ae85 | 2010-01-03 19:45:54 +0100 | [diff] [blame] | 6896 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6897 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6898 | /* if there are spaces around the equal sign, we need to |
| 6899 | * strip them otherwise we'll get trouble for cookie captures, |
| 6900 | * or even for rewrites. Since this happens extremely rarely, |
| 6901 | * it does not hurt performance. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6902 | */ |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6903 | if (unlikely(att_end != equal || val_beg > equal + 1)) { |
| 6904 | int stripped_before = 0; |
| 6905 | int stripped_after = 0; |
| 6906 | |
| 6907 | if (att_end != equal) { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 6908 | stripped_before = buffer_replace2(req->buf, att_end, equal, NULL, 0); |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6909 | equal += stripped_before; |
| 6910 | val_beg += stripped_before; |
| 6911 | } |
| 6912 | |
| 6913 | if (val_beg > equal + 1) { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 6914 | stripped_after = buffer_replace2(req->buf, equal + 1, val_beg, NULL, 0); |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6915 | val_beg += stripped_after; |
| 6916 | stripped_before += stripped_after; |
| 6917 | } |
| 6918 | |
| 6919 | val_end += stripped_before; |
| 6920 | next += stripped_before; |
| 6921 | hdr_end += stripped_before; |
| 6922 | hdr_next += stripped_before; |
| 6923 | cur_hdr->len += stripped_before; |
| 6924 | http_msg_move_end(&txn->req, stripped_before); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6925 | } |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6926 | /* now everything is as on the diagram above */ |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6927 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6928 | /* First, let's see if we want to capture this cookie. We check |
| 6929 | * that we don't already have a client side cookie, because we |
| 6930 | * can only capture one. Also as an optimisation, we ignore |
| 6931 | * cookies shorter than the declared name. |
| 6932 | */ |
| 6933 | if (t->fe->capture_name != NULL && txn->cli_cookie == NULL && |
| 6934 | (val_end - att_beg >= t->fe->capture_namelen) && |
| 6935 | memcmp(att_beg, t->fe->capture_name, t->fe->capture_namelen) == 0) { |
| 6936 | int log_len = val_end - att_beg; |
| 6937 | |
| 6938 | if ((txn->cli_cookie = pool_alloc2(pool2_capture)) == NULL) { |
| 6939 | Alert("HTTP logging : out of memory.\n"); |
| 6940 | } else { |
| 6941 | if (log_len > t->fe->capture_len) |
| 6942 | log_len = t->fe->capture_len; |
| 6943 | memcpy(txn->cli_cookie, att_beg, log_len); |
| 6944 | txn->cli_cookie[log_len] = 0; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6945 | } |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6946 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6947 | |
Willy Tarreau | bca9969 | 2010-10-06 19:25:55 +0200 | [diff] [blame] | 6948 | /* Persistence cookies in passive, rewrite or insert mode have the |
| 6949 | * following form : |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6950 | * |
Willy Tarreau | bca9969 | 2010-10-06 19:25:55 +0200 | [diff] [blame] | 6951 | * Cookie: NAME=SRV[|<lastseen>[|<firstseen>]] |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6952 | * |
Willy Tarreau | bca9969 | 2010-10-06 19:25:55 +0200 | [diff] [blame] | 6953 | * For cookies in prefix mode, the form is : |
| 6954 | * |
| 6955 | * Cookie: NAME=SRV~VALUE |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6956 | */ |
| 6957 | if ((att_end - att_beg == t->be->cookie_len) && (t->be->cookie_name != NULL) && |
| 6958 | (memcmp(att_beg, t->be->cookie_name, att_end - att_beg) == 0)) { |
| 6959 | struct server *srv = t->be->srv; |
| 6960 | char *delim; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6961 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6962 | /* if we're in cookie prefix mode, we'll search the delimitor so that we |
| 6963 | * have the server ID between val_beg and delim, and the original cookie between |
| 6964 | * delim+1 and val_end. Otherwise, delim==val_end : |
| 6965 | * |
| 6966 | * Cookie: NAME=SRV; # in all but prefix modes |
| 6967 | * Cookie: NAME=SRV~OPAQUE ; # in prefix mode |
| 6968 | * | || || | |+-> next |
| 6969 | * | || || | +--> val_end |
| 6970 | * | || || +---------> delim |
| 6971 | * | || |+------------> val_beg |
| 6972 | * | || +-------------> att_end = equal |
| 6973 | * | |+-----------------> att_beg |
| 6974 | * | +------------------> prev |
| 6975 | * +-------------------------> hdr_beg |
| 6976 | */ |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6977 | |
Willy Tarreau | 6740213 | 2012-05-31 20:40:20 +0200 | [diff] [blame] | 6978 | if (t->be->ck_opts & PR_CK_PFX) { |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6979 | for (delim = val_beg; delim < val_end; delim++) |
| 6980 | if (*delim == COOKIE_DELIM) |
| 6981 | break; |
Willy Tarreau | bca9969 | 2010-10-06 19:25:55 +0200 | [diff] [blame] | 6982 | } else { |
| 6983 | char *vbar1; |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6984 | delim = val_end; |
Willy Tarreau | bca9969 | 2010-10-06 19:25:55 +0200 | [diff] [blame] | 6985 | /* Now check if the cookie contains a date field, which would |
| 6986 | * appear after a vertical bar ('|') just after the server name |
| 6987 | * and before the delimiter. |
| 6988 | */ |
| 6989 | vbar1 = memchr(val_beg, COOKIE_DELIM_DATE, val_end - val_beg); |
| 6990 | if (vbar1) { |
| 6991 | /* OK, so left of the bar is the server's cookie and |
Willy Tarreau | f64d141 | 2010-10-07 20:06:11 +0200 | [diff] [blame] | 6992 | * right is the last seen date. It is a base64 encoded |
| 6993 | * 30-bit value representing the UNIX date since the |
| 6994 | * epoch in 4-second quantities. |
Willy Tarreau | bca9969 | 2010-10-06 19:25:55 +0200 | [diff] [blame] | 6995 | */ |
Willy Tarreau | f64d141 | 2010-10-07 20:06:11 +0200 | [diff] [blame] | 6996 | int val; |
Willy Tarreau | bca9969 | 2010-10-06 19:25:55 +0200 | [diff] [blame] | 6997 | delim = vbar1++; |
Willy Tarreau | f64d141 | 2010-10-07 20:06:11 +0200 | [diff] [blame] | 6998 | if (val_end - vbar1 >= 5) { |
| 6999 | val = b64tos30(vbar1); |
| 7000 | if (val > 0) |
| 7001 | txn->cookie_last_date = val << 2; |
| 7002 | } |
| 7003 | /* look for a second vertical bar */ |
| 7004 | vbar1 = memchr(vbar1, COOKIE_DELIM_DATE, val_end - vbar1); |
| 7005 | if (vbar1 && (val_end - vbar1 > 5)) { |
| 7006 | val = b64tos30(vbar1 + 1); |
| 7007 | if (val > 0) |
| 7008 | txn->cookie_first_date = val << 2; |
| 7009 | } |
Willy Tarreau | bca9969 | 2010-10-06 19:25:55 +0200 | [diff] [blame] | 7010 | } |
| 7011 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7012 | |
Willy Tarreau | f64d141 | 2010-10-07 20:06:11 +0200 | [diff] [blame] | 7013 | /* if the cookie has an expiration date and the proxy wants to check |
| 7014 | * it, then we do that now. We first check if the cookie is too old, |
| 7015 | * then only if it has expired. We detect strict overflow because the |
| 7016 | * time resolution here is not great (4 seconds). Cookies with dates |
| 7017 | * in the future are ignored if their offset is beyond one day. This |
| 7018 | * allows an admin to fix timezone issues without expiring everyone |
| 7019 | * and at the same time avoids keeping unwanted side effects for too |
| 7020 | * long. |
| 7021 | */ |
| 7022 | if (txn->cookie_first_date && t->be->cookie_maxlife && |
Willy Tarreau | ef4f391 | 2010-10-07 21:00:29 +0200 | [diff] [blame] | 7023 | (((signed)(date.tv_sec - txn->cookie_first_date) > (signed)t->be->cookie_maxlife) || |
| 7024 | ((signed)(txn->cookie_first_date - date.tv_sec) > 86400))) { |
Willy Tarreau | f64d141 | 2010-10-07 20:06:11 +0200 | [diff] [blame] | 7025 | txn->flags &= ~TX_CK_MASK; |
| 7026 | txn->flags |= TX_CK_OLD; |
| 7027 | delim = val_beg; // let's pretend we have not found the cookie |
| 7028 | txn->cookie_first_date = 0; |
| 7029 | txn->cookie_last_date = 0; |
| 7030 | } |
| 7031 | else if (txn->cookie_last_date && t->be->cookie_maxidle && |
Willy Tarreau | ef4f391 | 2010-10-07 21:00:29 +0200 | [diff] [blame] | 7032 | (((signed)(date.tv_sec - txn->cookie_last_date) > (signed)t->be->cookie_maxidle) || |
| 7033 | ((signed)(txn->cookie_last_date - date.tv_sec) > 86400))) { |
Willy Tarreau | f64d141 | 2010-10-07 20:06:11 +0200 | [diff] [blame] | 7034 | txn->flags &= ~TX_CK_MASK; |
| 7035 | txn->flags |= TX_CK_EXPIRED; |
| 7036 | delim = val_beg; // let's pretend we have not found the cookie |
| 7037 | txn->cookie_first_date = 0; |
| 7038 | txn->cookie_last_date = 0; |
| 7039 | } |
| 7040 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7041 | /* Here, we'll look for the first running server which supports the cookie. |
| 7042 | * This allows to share a same cookie between several servers, for example |
| 7043 | * to dedicate backup servers to specific servers only. |
| 7044 | * However, to prevent clients from sticking to cookie-less backup server |
| 7045 | * when they have incidentely learned an empty cookie, we simply ignore |
| 7046 | * empty cookies and mark them as invalid. |
| 7047 | * The same behaviour is applied when persistence must be ignored. |
| 7048 | */ |
Willy Tarreau | 4a5cade | 2012-04-05 21:09:48 +0200 | [diff] [blame] | 7049 | if ((delim == val_beg) || (t->flags & (SN_IGNORE_PRST | SN_ASSIGNED))) |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7050 | srv = NULL; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7051 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7052 | while (srv) { |
| 7053 | if (srv->cookie && (srv->cklen == delim - val_beg) && |
| 7054 | !memcmp(val_beg, srv->cookie, delim - val_beg)) { |
| 7055 | if ((srv->state & SRV_RUNNING) || |
| 7056 | (t->be->options & PR_O_PERSIST) || |
| 7057 | (t->flags & SN_FORCE_PRST)) { |
| 7058 | /* we found the server and we can use it */ |
| 7059 | txn->flags &= ~TX_CK_MASK; |
| 7060 | txn->flags |= (srv->state & SRV_RUNNING) ? TX_CK_VALID : TX_CK_DOWN; |
| 7061 | t->flags |= SN_DIRECT | SN_ASSIGNED; |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 7062 | t->target = &srv->obj_type; |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7063 | break; |
| 7064 | } else { |
| 7065 | /* we found a server, but it's down, |
| 7066 | * mark it as such and go on in case |
| 7067 | * another one is available. |
| 7068 | */ |
| 7069 | txn->flags &= ~TX_CK_MASK; |
| 7070 | txn->flags |= TX_CK_DOWN; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7071 | } |
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 | srv = srv->next; |
| 7074 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7075 | |
Willy Tarreau | f64d141 | 2010-10-07 20:06:11 +0200 | [diff] [blame] | 7076 | 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] | 7077 | /* no server matched this cookie or we deliberately skipped it */ |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7078 | txn->flags &= ~TX_CK_MASK; |
Willy Tarreau | c89ccb6 | 2012-04-05 21:18:22 +0200 | [diff] [blame] | 7079 | if ((t->flags & (SN_IGNORE_PRST | SN_ASSIGNED))) |
| 7080 | txn->flags |= TX_CK_UNUSED; |
| 7081 | else |
| 7082 | txn->flags |= TX_CK_INVALID; |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7083 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7084 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7085 | /* depending on the cookie mode, we may have to either : |
| 7086 | * - delete the complete cookie if we're in insert+indirect mode, so that |
| 7087 | * the server never sees it ; |
| 7088 | * - remove the server id from the cookie value, and tag the cookie as an |
| 7089 | * application cookie so that it does not get accidentely removed later, |
| 7090 | * if we're in cookie prefix mode |
| 7091 | */ |
Willy Tarreau | 6740213 | 2012-05-31 20:40:20 +0200 | [diff] [blame] | 7092 | if ((t->be->ck_opts & PR_CK_PFX) && (delim != val_end)) { |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7093 | int delta; /* negative */ |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7094 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 7095 | delta = buffer_replace2(req->buf, val_beg, delim + 1, NULL, 0); |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7096 | val_end += delta; |
| 7097 | next += delta; |
| 7098 | hdr_end += delta; |
| 7099 | hdr_next += delta; |
| 7100 | cur_hdr->len += delta; |
| 7101 | http_msg_move_end(&txn->req, delta); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7102 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7103 | del_from = NULL; |
| 7104 | preserve_hdr = 1; /* we want to keep this cookie */ |
| 7105 | } |
| 7106 | else if (del_from == NULL && |
Willy Tarreau | 6740213 | 2012-05-31 20:40:20 +0200 | [diff] [blame] | 7107 | (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] | 7108 | del_from = prev; |
| 7109 | } |
| 7110 | } else { |
| 7111 | /* This is not our cookie, so we must preserve it. But if we already |
| 7112 | * scheduled another cookie for removal, we cannot remove the |
| 7113 | * complete header, but we can remove the previous block itself. |
| 7114 | */ |
| 7115 | preserve_hdr = 1; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7116 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7117 | if (del_from != NULL) { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 7118 | int delta = del_hdr_value(req->buf, &del_from, prev); |
Willy Tarreau | b810554 | 2010-11-24 18:31:28 +0100 | [diff] [blame] | 7119 | if (att_beg >= del_from) |
| 7120 | att_beg += delta; |
| 7121 | if (att_end >= del_from) |
| 7122 | att_end += delta; |
| 7123 | val_beg += delta; |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7124 | val_end += delta; |
| 7125 | next += delta; |
| 7126 | hdr_end += delta; |
| 7127 | hdr_next += delta; |
| 7128 | cur_hdr->len += delta; |
| 7129 | http_msg_move_end(&txn->req, delta); |
| 7130 | prev = del_from; |
| 7131 | del_from = NULL; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7132 | } |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7133 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7134 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7135 | /* Look for the appsession cookie unless persistence must be ignored */ |
| 7136 | if (!(t->flags & SN_IGNORE_PRST) && (t->be->appsession_name != NULL)) { |
| 7137 | int cmp_len, value_len; |
| 7138 | char *value_begin; |
Aleksandar Lazic | 697bbb0 | 2008-08-13 19:57:02 +0200 | [diff] [blame] | 7139 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7140 | if (t->be->options2 & PR_O2_AS_PFX) { |
| 7141 | cmp_len = MIN(val_end - att_beg, t->be->appsession_name_len); |
| 7142 | value_begin = att_beg + t->be->appsession_name_len; |
| 7143 | value_len = val_end - att_beg - t->be->appsession_name_len; |
| 7144 | } else { |
| 7145 | cmp_len = att_end - att_beg; |
| 7146 | value_begin = val_beg; |
| 7147 | value_len = val_end - val_beg; |
| 7148 | } |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 7149 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7150 | /* let's see if the cookie is our appcookie */ |
| 7151 | if (cmp_len == t->be->appsession_name_len && |
| 7152 | memcmp(att_beg, t->be->appsession_name, cmp_len) == 0) { |
| 7153 | manage_client_side_appsession(t, value_begin, value_len); |
| 7154 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7155 | } |
| 7156 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7157 | /* continue with next cookie on this header line */ |
| 7158 | att_beg = next; |
| 7159 | } /* for each cookie */ |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7160 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7161 | /* There are no more cookies on this line. |
| 7162 | * We may still have one (or several) marked for deletion at the |
| 7163 | * end of the line. We must do this now in two ways : |
| 7164 | * - if some cookies must be preserved, we only delete from the |
| 7165 | * mark to the end of line ; |
| 7166 | * - if nothing needs to be preserved, simply delete the whole header |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7167 | */ |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7168 | if (del_from) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7169 | int delta; |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7170 | if (preserve_hdr) { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 7171 | delta = del_hdr_value(req->buf, &del_from, hdr_end); |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7172 | hdr_end = del_from; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7173 | cur_hdr->len += delta; |
| 7174 | } else { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 7175 | delta = buffer_replace2(req->buf, hdr_beg, hdr_next, NULL, 0); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7176 | |
| 7177 | /* FIXME: this should be a separate function */ |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 7178 | txn->hdr_idx.v[old_idx].next = cur_hdr->next; |
| 7179 | txn->hdr_idx.used--; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7180 | cur_hdr->len = 0; |
Willy Tarreau | 26db59e | 2010-11-28 06:57:24 +0100 | [diff] [blame] | 7181 | cur_idx = old_idx; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7182 | } |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7183 | hdr_next += delta; |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 7184 | http_msg_move_end(&txn->req, delta); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7185 | } |
| 7186 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7187 | /* check next header */ |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7188 | old_idx = cur_idx; |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7189 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7190 | } |
| 7191 | |
| 7192 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7193 | /* Iterate the same filter through all response headers contained in <rtr>. |
| 7194 | * Returns 1 if this filter can be stopped upon return, otherwise 0. |
| 7195 | */ |
Willy Tarreau | 7421efb | 2012-07-02 15:11:27 +0200 | [diff] [blame] | 7196 | 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] | 7197 | { |
| 7198 | char term; |
| 7199 | char *cur_ptr, *cur_end, *cur_next; |
| 7200 | int cur_idx, old_idx, last_hdr; |
| 7201 | struct http_txn *txn = &t->txn; |
| 7202 | struct hdr_idx_elem *cur_hdr; |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 7203 | int delta; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7204 | |
| 7205 | last_hdr = 0; |
| 7206 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 7207 | cur_next = rtr->buf->p + hdr_idx_first_pos(&txn->hdr_idx); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7208 | old_idx = 0; |
| 7209 | |
| 7210 | while (!last_hdr) { |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 7211 | if (unlikely(txn->flags & TX_SVDENY)) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7212 | return 1; |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 7213 | else if (unlikely(txn->flags & TX_SVALLOW) && |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7214 | (exp->action == ACT_ALLOW || |
| 7215 | exp->action == ACT_DENY)) |
| 7216 | return 0; |
| 7217 | |
| 7218 | cur_idx = txn->hdr_idx.v[old_idx].next; |
| 7219 | if (!cur_idx) |
| 7220 | break; |
| 7221 | |
| 7222 | cur_hdr = &txn->hdr_idx.v[cur_idx]; |
| 7223 | cur_ptr = cur_next; |
| 7224 | cur_end = cur_ptr + cur_hdr->len; |
| 7225 | cur_next = cur_end + cur_hdr->cr + 1; |
| 7226 | |
| 7227 | /* Now we have one header between cur_ptr and cur_end, |
| 7228 | * and the next header starts at cur_next. |
| 7229 | */ |
| 7230 | |
| 7231 | /* The annoying part is that pattern matching needs |
| 7232 | * that we modify the contents to null-terminate all |
| 7233 | * strings before testing them. |
| 7234 | */ |
| 7235 | |
| 7236 | term = *cur_end; |
| 7237 | *cur_end = '\0'; |
| 7238 | |
| 7239 | if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) { |
| 7240 | switch (exp->action) { |
| 7241 | case ACT_ALLOW: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 7242 | txn->flags |= TX_SVALLOW; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7243 | last_hdr = 1; |
| 7244 | break; |
| 7245 | |
| 7246 | case ACT_DENY: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 7247 | txn->flags |= TX_SVDENY; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7248 | last_hdr = 1; |
| 7249 | break; |
| 7250 | |
| 7251 | case ACT_REPLACE: |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 7252 | trash.len = exp_replace(trash.str, cur_ptr, exp->replace, pmatch); |
| 7253 | 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] | 7254 | /* FIXME: if the user adds a newline in the replacement, the |
| 7255 | * index will not be recalculated for now, and the new line |
| 7256 | * will not be counted as a new header. |
| 7257 | */ |
| 7258 | |
| 7259 | cur_end += delta; |
| 7260 | cur_next += delta; |
| 7261 | cur_hdr->len += delta; |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 7262 | http_msg_move_end(&txn->rsp, delta); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7263 | break; |
| 7264 | |
| 7265 | case ACT_REMOVE: |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 7266 | delta = buffer_replace2(rtr->buf, cur_ptr, cur_next, NULL, 0); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7267 | cur_next += delta; |
| 7268 | |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 7269 | http_msg_move_end(&txn->rsp, delta); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7270 | txn->hdr_idx.v[old_idx].next = cur_hdr->next; |
| 7271 | txn->hdr_idx.used--; |
| 7272 | cur_hdr->len = 0; |
| 7273 | cur_end = NULL; /* null-term has been rewritten */ |
Willy Tarreau | 26db59e | 2010-11-28 06:57:24 +0100 | [diff] [blame] | 7274 | cur_idx = old_idx; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7275 | break; |
| 7276 | |
| 7277 | } |
| 7278 | } |
| 7279 | if (cur_end) |
| 7280 | *cur_end = term; /* restore the string terminator */ |
| 7281 | |
| 7282 | /* keep the link from this header to next one in case of later |
| 7283 | * removal of next header. |
| 7284 | */ |
| 7285 | old_idx = cur_idx; |
| 7286 | } |
| 7287 | return 0; |
| 7288 | } |
| 7289 | |
| 7290 | |
| 7291 | /* Apply the filter to the status line in the response buffer <rtr>. |
| 7292 | * Returns 0 if nothing has been done, 1 if the filter has been applied, |
| 7293 | * or -1 if a replacement resulted in an invalid status line. |
| 7294 | */ |
Willy Tarreau | 7421efb | 2012-07-02 15:11:27 +0200 | [diff] [blame] | 7295 | 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] | 7296 | { |
| 7297 | char term; |
| 7298 | char *cur_ptr, *cur_end; |
| 7299 | int done; |
| 7300 | struct http_txn *txn = &t->txn; |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 7301 | int delta; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7302 | |
| 7303 | |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 7304 | if (unlikely(txn->flags & TX_SVDENY)) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7305 | return 1; |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 7306 | else if (unlikely(txn->flags & TX_SVALLOW) && |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7307 | (exp->action == ACT_ALLOW || |
| 7308 | exp->action == ACT_DENY)) |
| 7309 | return 0; |
| 7310 | else if (exp->action == ACT_REMOVE) |
| 7311 | return 0; |
| 7312 | |
| 7313 | done = 0; |
| 7314 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 7315 | cur_ptr = rtr->buf->p; |
Willy Tarreau | 1ba0e5f | 2010-06-07 13:57:32 +0200 | [diff] [blame] | 7316 | cur_end = cur_ptr + txn->rsp.sl.st.l; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7317 | |
| 7318 | /* Now we have the status line between cur_ptr and cur_end */ |
| 7319 | |
| 7320 | /* The annoying part is that pattern matching needs |
| 7321 | * that we modify the contents to null-terminate all |
| 7322 | * strings before testing them. |
| 7323 | */ |
| 7324 | |
| 7325 | term = *cur_end; |
| 7326 | *cur_end = '\0'; |
| 7327 | |
| 7328 | if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) { |
| 7329 | switch (exp->action) { |
| 7330 | case ACT_ALLOW: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 7331 | txn->flags |= TX_SVALLOW; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7332 | done = 1; |
| 7333 | break; |
| 7334 | |
| 7335 | case ACT_DENY: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 7336 | txn->flags |= TX_SVDENY; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7337 | done = 1; |
| 7338 | break; |
| 7339 | |
| 7340 | case ACT_REPLACE: |
| 7341 | *cur_end = term; /* restore the string terminator */ |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 7342 | trash.len = exp_replace(trash.str, cur_ptr, exp->replace, pmatch); |
| 7343 | 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] | 7344 | /* FIXME: if the user adds a newline in the replacement, the |
| 7345 | * index will not be recalculated for now, and the new line |
| 7346 | * will not be counted as a new header. |
| 7347 | */ |
| 7348 | |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 7349 | http_msg_move_end(&txn->rsp, delta); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7350 | cur_end += delta; |
Willy Tarreau | 69d8c5d | 2012-05-08 09:44:41 +0200 | [diff] [blame] | 7351 | cur_end = (char *)http_parse_stsline(&txn->rsp, |
Willy Tarreau | 0278576 | 2007-04-03 14:45:44 +0200 | [diff] [blame] | 7352 | HTTP_MSG_RPVER, |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7353 | cur_ptr, cur_end + 1, |
| 7354 | NULL, NULL); |
| 7355 | if (unlikely(!cur_end)) |
| 7356 | return -1; |
| 7357 | |
| 7358 | /* we have a full respnse and we know that we have either a CR |
| 7359 | * or an LF at <ptr>. |
| 7360 | */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 7361 | 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] | 7362 | 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] | 7363 | /* there is no point trying this regex on headers */ |
| 7364 | return 1; |
| 7365 | } |
| 7366 | } |
| 7367 | *cur_end = term; /* restore the string terminator */ |
| 7368 | return done; |
| 7369 | } |
| 7370 | |
| 7371 | |
| 7372 | |
| 7373 | /* |
Willy Tarreau | fdb563c | 2010-01-31 15:43:27 +0100 | [diff] [blame] | 7374 | * 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] | 7375 | * Returns 0 if everything is alright, or -1 in case a replacement lead to an |
| 7376 | * unparsable response. |
| 7377 | */ |
Willy Tarreau | 7421efb | 2012-07-02 15:11:27 +0200 | [diff] [blame] | 7378 | 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] | 7379 | { |
Willy Tarreau | fdb563c | 2010-01-31 15:43:27 +0100 | [diff] [blame] | 7380 | struct http_txn *txn = &s->txn; |
| 7381 | struct hdr_exp *exp; |
| 7382 | |
| 7383 | for (exp = px->rsp_exp; exp; exp = exp->next) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7384 | int ret; |
| 7385 | |
| 7386 | /* |
| 7387 | * The interleaving of transformations and verdicts |
| 7388 | * makes it difficult to decide to continue or stop |
| 7389 | * the evaluation. |
| 7390 | */ |
| 7391 | |
Willy Tarreau | fdb563c | 2010-01-31 15:43:27 +0100 | [diff] [blame] | 7392 | if (txn->flags & TX_SVDENY) |
| 7393 | break; |
| 7394 | |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 7395 | if ((txn->flags & TX_SVALLOW) && |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7396 | (exp->action == ACT_ALLOW || exp->action == ACT_DENY || |
| 7397 | exp->action == ACT_PASS)) { |
| 7398 | exp = exp->next; |
| 7399 | continue; |
| 7400 | } |
| 7401 | |
Willy Tarreau | fdb563c | 2010-01-31 15:43:27 +0100 | [diff] [blame] | 7402 | /* if this filter had a condition, evaluate it now and skip to |
| 7403 | * next filter if the condition does not match. |
| 7404 | */ |
| 7405 | if (exp->cond) { |
Willy Tarreau | 32a6f2e | 2012-04-25 10:13:36 +0200 | [diff] [blame] | 7406 | 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] | 7407 | ret = acl_pass(ret); |
| 7408 | if (((struct acl_cond *)exp->cond)->pol == ACL_COND_UNLESS) |
| 7409 | ret = !ret; |
| 7410 | if (!ret) |
| 7411 | continue; |
| 7412 | } |
| 7413 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7414 | /* Apply the filter to the status line. */ |
Willy Tarreau | fdb563c | 2010-01-31 15:43:27 +0100 | [diff] [blame] | 7415 | ret = apply_filter_to_sts_line(s, rtr, exp); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7416 | if (unlikely(ret < 0)) |
| 7417 | return -1; |
| 7418 | |
| 7419 | if (likely(ret == 0)) { |
| 7420 | /* The filter did not match the response, it can be |
| 7421 | * iterated through all headers. |
| 7422 | */ |
Willy Tarreau | fdb563c | 2010-01-31 15:43:27 +0100 | [diff] [blame] | 7423 | apply_filter_to_resp_headers(s, rtr, exp); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7424 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7425 | } |
| 7426 | return 0; |
| 7427 | } |
| 7428 | |
| 7429 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7430 | /* |
Willy Tarreau | 396d2c6 | 2007-11-04 19:30:00 +0100 | [diff] [blame] | 7431 | * 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] | 7432 | * desirable to call it only when needed. This function is also used when we |
| 7433 | * 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] | 7434 | */ |
Willy Tarreau | 7421efb | 2012-07-02 15:11:27 +0200 | [diff] [blame] | 7435 | void manage_server_side_cookies(struct session *t, struct channel *res) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7436 | { |
| 7437 | struct http_txn *txn = &t->txn; |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 7438 | struct server *srv; |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7439 | int is_cookie2; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7440 | int cur_idx, old_idx, delta; |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7441 | char *hdr_beg, *hdr_end, *hdr_next; |
| 7442 | char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7443 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7444 | /* Iterate through the headers. |
| 7445 | * we start with the start line. |
| 7446 | */ |
| 7447 | old_idx = 0; |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 7448 | hdr_next = res->buf->p + hdr_idx_first_pos(&txn->hdr_idx); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7449 | |
| 7450 | while ((cur_idx = txn->hdr_idx.v[old_idx].next)) { |
| 7451 | struct hdr_idx_elem *cur_hdr; |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 7452 | int val; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7453 | |
| 7454 | cur_hdr = &txn->hdr_idx.v[cur_idx]; |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7455 | hdr_beg = hdr_next; |
| 7456 | hdr_end = hdr_beg + cur_hdr->len; |
| 7457 | hdr_next = hdr_end + cur_hdr->cr + 1; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7458 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7459 | /* We have one full header between hdr_beg and hdr_end, and the |
| 7460 | * next header starts at hdr_next. We're only interested in |
| 7461 | * "Set-Cookie" and "Set-Cookie2" headers. |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7462 | */ |
| 7463 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7464 | is_cookie2 = 0; |
| 7465 | prev = hdr_beg + 10; |
| 7466 | val = http_header_match2(hdr_beg, hdr_end, "Set-Cookie", 10); |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 7467 | if (!val) { |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7468 | val = http_header_match2(hdr_beg, hdr_end, "Set-Cookie2", 11); |
| 7469 | if (!val) { |
| 7470 | old_idx = cur_idx; |
| 7471 | continue; |
| 7472 | } |
| 7473 | is_cookie2 = 1; |
| 7474 | prev = hdr_beg + 11; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7475 | } |
| 7476 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7477 | /* OK, right now we know we have a Set-Cookie* at hdr_beg, and |
| 7478 | * <prev> points to the colon. |
| 7479 | */ |
Willy Tarreau | f134831 | 2010-10-07 15:54:11 +0200 | [diff] [blame] | 7480 | txn->flags |= TX_SCK_PRESENT; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7481 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7482 | /* Maybe we only wanted to see if there was a Set-Cookie (eg: |
| 7483 | * check-cache is enabled) and we are not interested in checking |
| 7484 | * them. Warning, the cookie capture is declared in the frontend. |
Willy Tarreau | fd39dda | 2008-10-17 12:01:58 +0200 | [diff] [blame] | 7485 | */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 7486 | if (t->be->cookie_name == NULL && |
| 7487 | t->be->appsession_name == NULL && |
Willy Tarreau | fd39dda | 2008-10-17 12:01:58 +0200 | [diff] [blame] | 7488 | t->fe->capture_name == NULL) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7489 | return; |
| 7490 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7491 | /* OK so now we know we have to process this response cookie. |
| 7492 | * The format of the Set-Cookie header is slightly different |
| 7493 | * from the format of the Cookie header in that it does not |
| 7494 | * support the comma as a cookie delimiter (thus the header |
| 7495 | * cannot be folded) because the Expires attribute described in |
| 7496 | * the original Netscape's spec may contain an unquoted date |
| 7497 | * with a comma inside. We have to live with this because |
| 7498 | * many browsers don't support Max-Age and some browsers don't |
| 7499 | * support quoted strings. However the Set-Cookie2 header is |
| 7500 | * clean. |
| 7501 | * |
| 7502 | * We have to keep multiple pointers in order to support cookie |
| 7503 | * removal at the beginning, middle or end of header without |
| 7504 | * corrupting the header (in case of set-cookie2). A special |
| 7505 | * pointer, <scav> points to the beginning of the set-cookie-av |
| 7506 | * fields after the first semi-colon. The <next> pointer points |
| 7507 | * either to the end of line (set-cookie) or next unquoted comma |
| 7508 | * (set-cookie2). All of these headers are valid : |
| 7509 | * |
| 7510 | * Set-Cookie: NAME1 = VALUE 1 ; Secure; Path="/"\r\n |
| 7511 | * Set-Cookie:NAME=VALUE; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT\r\n |
| 7512 | * Set-Cookie: NAME = VALUE ; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT\r\n |
| 7513 | * Set-Cookie2: NAME1 = VALUE 1 ; Max-Age=0, NAME2=VALUE2; Discard\r\n |
| 7514 | * | | | | | | | | | | |
| 7515 | * | | | | | | | | +-> next hdr_end <--+ |
| 7516 | * | | | | | | | +------------> scav |
| 7517 | * | | | | | | +--------------> val_end |
| 7518 | * | | | | | +--------------------> val_beg |
| 7519 | * | | | | +----------------------> equal |
| 7520 | * | | | +------------------------> att_end |
| 7521 | * | | +----------------------------> att_beg |
| 7522 | * | +------------------------------> prev |
| 7523 | * +-----------------------------------------> hdr_beg |
| 7524 | */ |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7525 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7526 | for (; prev < hdr_end; prev = next) { |
| 7527 | /* Iterate through all cookies on this line */ |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7528 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7529 | /* find att_beg */ |
| 7530 | att_beg = prev + 1; |
| 7531 | while (att_beg < hdr_end && http_is_spht[(unsigned char)*att_beg]) |
| 7532 | att_beg++; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7533 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7534 | /* find att_end : this is the first character after the last non |
| 7535 | * space before the equal. It may be equal to hdr_end. |
| 7536 | */ |
| 7537 | equal = att_end = att_beg; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7538 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7539 | while (equal < hdr_end) { |
| 7540 | if (*equal == '=' || *equal == ';' || (is_cookie2 && *equal == ',')) |
| 7541 | break; |
| 7542 | if (http_is_spht[(unsigned char)*equal++]) |
| 7543 | continue; |
| 7544 | att_end = equal; |
| 7545 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7546 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7547 | /* here, <equal> points to '=', a delimitor or the end. <att_end> |
| 7548 | * is between <att_beg> and <equal>, both may be identical. |
| 7549 | */ |
| 7550 | |
| 7551 | /* look for end of cookie if there is an equal sign */ |
| 7552 | if (equal < hdr_end && *equal == '=') { |
| 7553 | /* look for the beginning of the value */ |
| 7554 | val_beg = equal + 1; |
| 7555 | while (val_beg < hdr_end && http_is_spht[(unsigned char)*val_beg]) |
| 7556 | val_beg++; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7557 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7558 | /* find the end of the value, respecting quotes */ |
| 7559 | next = find_cookie_value_end(val_beg, hdr_end); |
| 7560 | |
| 7561 | /* make val_end point to the first white space or delimitor after the value */ |
| 7562 | val_end = next; |
| 7563 | while (val_end > val_beg && http_is_spht[(unsigned char)*(val_end - 1)]) |
| 7564 | val_end--; |
| 7565 | } else { |
| 7566 | /* <equal> points to next comma, semi-colon or EOL */ |
| 7567 | val_beg = val_end = next = equal; |
| 7568 | } |
| 7569 | |
| 7570 | if (next < hdr_end) { |
| 7571 | /* Set-Cookie2 supports multiple cookies, and <next> points to |
| 7572 | * a colon or semi-colon before the end. So skip all attr-value |
| 7573 | * pairs and look for the next comma. For Set-Cookie, since |
| 7574 | * commas are permitted in values, skip to the end. |
| 7575 | */ |
| 7576 | if (is_cookie2) |
| 7577 | next = find_hdr_value_end(next, hdr_end); |
| 7578 | else |
| 7579 | next = hdr_end; |
| 7580 | } |
| 7581 | |
| 7582 | /* Now everything is as on the diagram above */ |
| 7583 | |
| 7584 | /* Ignore cookies with no equal sign */ |
| 7585 | if (equal == val_end) |
| 7586 | continue; |
| 7587 | |
| 7588 | /* If there are spaces around the equal sign, we need to |
| 7589 | * strip them otherwise we'll get trouble for cookie captures, |
| 7590 | * or even for rewrites. Since this happens extremely rarely, |
| 7591 | * it does not hurt performance. |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7592 | */ |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7593 | if (unlikely(att_end != equal || val_beg > equal + 1)) { |
| 7594 | int stripped_before = 0; |
| 7595 | int stripped_after = 0; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7596 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7597 | if (att_end != equal) { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 7598 | stripped_before = buffer_replace2(res->buf, att_end, equal, NULL, 0); |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7599 | equal += stripped_before; |
| 7600 | val_beg += stripped_before; |
| 7601 | } |
| 7602 | |
| 7603 | if (val_beg > equal + 1) { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 7604 | stripped_after = buffer_replace2(res->buf, equal + 1, val_beg, NULL, 0); |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7605 | val_beg += stripped_after; |
| 7606 | stripped_before += stripped_after; |
| 7607 | } |
| 7608 | |
| 7609 | val_end += stripped_before; |
| 7610 | next += stripped_before; |
| 7611 | hdr_end += stripped_before; |
| 7612 | hdr_next += stripped_before; |
| 7613 | cur_hdr->len += stripped_before; |
Willy Tarreau | 1fc1f45 | 2011-04-07 22:35:37 +0200 | [diff] [blame] | 7614 | http_msg_move_end(&txn->rsp, stripped_before); |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7615 | } |
| 7616 | |
| 7617 | /* First, let's see if we want to capture this cookie. We check |
| 7618 | * that we don't already have a server side cookie, because we |
| 7619 | * can only capture one. Also as an optimisation, we ignore |
| 7620 | * cookies shorter than the declared name. |
| 7621 | */ |
Willy Tarreau | fd39dda | 2008-10-17 12:01:58 +0200 | [diff] [blame] | 7622 | if (t->fe->capture_name != NULL && |
Willy Tarreau | 3bac9ff | 2007-03-18 17:31:28 +0100 | [diff] [blame] | 7623 | txn->srv_cookie == NULL && |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7624 | (val_end - att_beg >= t->fe->capture_namelen) && |
| 7625 | memcmp(att_beg, t->fe->capture_name, t->fe->capture_namelen) == 0) { |
| 7626 | int log_len = val_end - att_beg; |
Willy Tarreau | 086b3b4 | 2007-05-13 21:45:51 +0200 | [diff] [blame] | 7627 | if ((txn->srv_cookie = pool_alloc2(pool2_capture)) == NULL) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7628 | Alert("HTTP logging : out of memory.\n"); |
| 7629 | } |
Willy Tarreau | f70fc75 | 2010-11-19 11:27:18 +0100 | [diff] [blame] | 7630 | else { |
| 7631 | if (log_len > t->fe->capture_len) |
| 7632 | log_len = t->fe->capture_len; |
| 7633 | memcpy(txn->srv_cookie, att_beg, log_len); |
| 7634 | txn->srv_cookie[log_len] = 0; |
| 7635 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7636 | } |
| 7637 | |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 7638 | srv = objt_server(t->target); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7639 | /* now check if we need to process it for persistence */ |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7640 | if (!(t->flags & SN_IGNORE_PRST) && |
| 7641 | (att_end - att_beg == t->be->cookie_len) && (t->be->cookie_name != NULL) && |
| 7642 | (memcmp(att_beg, t->be->cookie_name, att_end - att_beg) == 0)) { |
Willy Tarreau | f134831 | 2010-10-07 15:54:11 +0200 | [diff] [blame] | 7643 | /* assume passive cookie by default */ |
| 7644 | txn->flags &= ~TX_SCK_MASK; |
| 7645 | txn->flags |= TX_SCK_FOUND; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7646 | |
| 7647 | /* If the cookie is in insert mode on a known server, we'll delete |
| 7648 | * this occurrence because we'll insert another one later. |
| 7649 | * 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] | 7650 | * a direct access. |
| 7651 | */ |
Willy Tarreau | 6740213 | 2012-05-31 20:40:20 +0200 | [diff] [blame] | 7652 | if (t->be->ck_opts & PR_CK_PSV) { |
Willy Tarreau | ba4c5be | 2010-10-23 12:46:42 +0200 | [diff] [blame] | 7653 | /* The "preserve" flag was set, we don't want to touch the |
| 7654 | * server's cookie. |
| 7655 | */ |
| 7656 | } |
Willy Tarreau | 6740213 | 2012-05-31 20:40:20 +0200 | [diff] [blame] | 7657 | else if ((srv && (t->be->ck_opts & PR_CK_INS)) || |
| 7658 | ((t->flags & SN_DIRECT) && (t->be->ck_opts & PR_CK_IND))) { |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7659 | /* this cookie must be deleted */ |
| 7660 | if (*prev == ':' && next == hdr_end) { |
| 7661 | /* whole header */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 7662 | delta = buffer_replace2(res->buf, hdr_beg, hdr_next, NULL, 0); |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7663 | txn->hdr_idx.v[old_idx].next = cur_hdr->next; |
| 7664 | txn->hdr_idx.used--; |
| 7665 | cur_hdr->len = 0; |
Willy Tarreau | 26db59e | 2010-11-28 06:57:24 +0100 | [diff] [blame] | 7666 | cur_idx = old_idx; |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7667 | hdr_next += delta; |
| 7668 | http_msg_move_end(&txn->rsp, delta); |
| 7669 | /* note: while both invalid now, <next> and <hdr_end> |
| 7670 | * are still equal, so the for() will stop as expected. |
| 7671 | */ |
| 7672 | } else { |
| 7673 | /* just remove the value */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 7674 | int delta = del_hdr_value(res->buf, &prev, next); |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7675 | next = prev; |
| 7676 | hdr_end += delta; |
| 7677 | hdr_next += delta; |
| 7678 | cur_hdr->len += delta; |
| 7679 | http_msg_move_end(&txn->rsp, delta); |
| 7680 | } |
Willy Tarreau | f134831 | 2010-10-07 15:54:11 +0200 | [diff] [blame] | 7681 | txn->flags &= ~TX_SCK_MASK; |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 7682 | txn->flags |= TX_SCK_DELETED; |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7683 | /* and go on with next cookie */ |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7684 | } |
Willy Tarreau | 6740213 | 2012-05-31 20:40:20 +0200 | [diff] [blame] | 7685 | else if (srv && srv->cookie && (t->be->ck_opts & PR_CK_RW)) { |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7686 | /* replace bytes val_beg->val_end with the cookie name associated |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7687 | * with this server since we know it. |
| 7688 | */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 7689 | 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] | 7690 | next += delta; |
| 7691 | hdr_end += delta; |
| 7692 | hdr_next += delta; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7693 | cur_hdr->len += delta; |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 7694 | http_msg_move_end(&txn->rsp, delta); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7695 | |
Willy Tarreau | f134831 | 2010-10-07 15:54:11 +0200 | [diff] [blame] | 7696 | txn->flags &= ~TX_SCK_MASK; |
| 7697 | txn->flags |= TX_SCK_REPLACED; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7698 | } |
Willy Tarreau | a059031 | 2012-06-06 16:07:00 +0200 | [diff] [blame] | 7699 | else if (srv && srv->cookie && (t->be->ck_opts & PR_CK_PFX)) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7700 | /* insert the cookie name associated with this server |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7701 | * before existing cookie, and insert a delimiter between them.. |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7702 | */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 7703 | 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] | 7704 | next += delta; |
| 7705 | hdr_end += delta; |
| 7706 | hdr_next += delta; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7707 | cur_hdr->len += delta; |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 7708 | http_msg_move_end(&txn->rsp, delta); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7709 | |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 7710 | val_beg[srv->cklen] = COOKIE_DELIM; |
Willy Tarreau | f134831 | 2010-10-07 15:54:11 +0200 | [diff] [blame] | 7711 | txn->flags &= ~TX_SCK_MASK; |
| 7712 | txn->flags |= TX_SCK_REPLACED; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7713 | } |
| 7714 | } |
Cyril Bonté | 47fdd8e | 2010-04-25 00:00:51 +0200 | [diff] [blame] | 7715 | /* next, let's see if the cookie is our appcookie, unless persistence must be ignored */ |
| 7716 | else if (!(t->flags & SN_IGNORE_PRST) && (t->be->appsession_name != NULL)) { |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 7717 | int cmp_len, value_len; |
| 7718 | char *value_begin; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7719 | |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 7720 | if (t->be->options2 & PR_O2_AS_PFX) { |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7721 | cmp_len = MIN(val_end - att_beg, t->be->appsession_name_len); |
| 7722 | value_begin = att_beg + t->be->appsession_name_len; |
| 7723 | 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] | 7724 | } else { |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7725 | cmp_len = att_end - att_beg; |
| 7726 | value_begin = val_beg; |
| 7727 | value_len = MIN(t->be->appsession_len, val_end - val_beg); |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 7728 | } |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 7729 | |
Cyril Bonté | 17530c3 | 2010-04-06 21:11:10 +0200 | [diff] [blame] | 7730 | if ((cmp_len == t->be->appsession_name_len) && |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7731 | (memcmp(att_beg, t->be->appsession_name, t->be->appsession_name_len) == 0)) { |
| 7732 | /* free a possibly previously allocated memory */ |
| 7733 | pool_free2(apools.sessid, txn->sessid); |
| 7734 | |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 7735 | /* Store the sessid in the session for future use */ |
Willy Tarreau | a3377ee | 2010-01-10 10:49:11 +0100 | [diff] [blame] | 7736 | if ((txn->sessid = pool_alloc2(apools.sessid)) == NULL) { |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 7737 | Alert("Not enough Memory process_srv():asession->sessid:malloc().\n"); |
| 7738 | send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n"); |
| 7739 | return; |
| 7740 | } |
Willy Tarreau | a3377ee | 2010-01-10 10:49:11 +0100 | [diff] [blame] | 7741 | memcpy(txn->sessid, value_begin, value_len); |
| 7742 | txn->sessid[value_len] = 0; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7743 | } |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7744 | } |
| 7745 | /* that's done for this cookie, check the next one on the same |
| 7746 | * line when next != hdr_end (only if is_cookie2). |
| 7747 | */ |
| 7748 | } |
| 7749 | /* check next header */ |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7750 | old_idx = cur_idx; |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7751 | } |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 7752 | |
Willy Tarreau | a3377ee | 2010-01-10 10:49:11 +0100 | [diff] [blame] | 7753 | if (txn->sessid != NULL) { |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 7754 | appsess *asession = NULL; |
| 7755 | /* only do insert, if lookup fails */ |
Willy Tarreau | a3377ee | 2010-01-10 10:49:11 +0100 | [diff] [blame] | 7756 | asession = appsession_hash_lookup(&(t->be->htbl_proxy), txn->sessid); |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 7757 | if (asession == NULL) { |
Willy Tarreau | 1fac753 | 2010-01-09 19:23:06 +0100 | [diff] [blame] | 7758 | size_t server_id_len; |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 7759 | if ((asession = pool_alloc2(pool2_appsess)) == NULL) { |
| 7760 | Alert("Not enough Memory process_srv():asession:calloc().\n"); |
| 7761 | send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession:calloc().\n"); |
| 7762 | return; |
| 7763 | } |
Willy Tarreau | 77eb9b8 | 2010-11-19 11:29:06 +0100 | [diff] [blame] | 7764 | asession->serverid = NULL; /* to avoid a double free in case of allocation error */ |
| 7765 | |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 7766 | if ((asession->sessid = pool_alloc2(apools.sessid)) == NULL) { |
| 7767 | Alert("Not enough Memory process_srv():asession->sessid:malloc().\n"); |
| 7768 | 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] | 7769 | t->be->htbl_proxy.destroy(asession); |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 7770 | return; |
| 7771 | } |
Willy Tarreau | a3377ee | 2010-01-10 10:49:11 +0100 | [diff] [blame] | 7772 | memcpy(asession->sessid, txn->sessid, t->be->appsession_len); |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 7773 | asession->sessid[t->be->appsession_len] = 0; |
| 7774 | |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 7775 | server_id_len = strlen(objt_server(t->target)->id) + 1; |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 7776 | if ((asession->serverid = pool_alloc2(apools.serverid)) == NULL) { |
Willy Tarreau | 77eb9b8 | 2010-11-19 11:29:06 +0100 | [diff] [blame] | 7777 | Alert("Not enough Memory process_srv():asession->serverid:malloc().\n"); |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 7778 | 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] | 7779 | t->be->htbl_proxy.destroy(asession); |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 7780 | return; |
| 7781 | } |
| 7782 | asession->serverid[0] = '\0'; |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 7783 | memcpy(asession->serverid, objt_server(t->target)->id, server_id_len); |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 7784 | |
| 7785 | asession->request_count = 0; |
| 7786 | appsession_hash_insert(&(t->be->htbl_proxy), asession); |
| 7787 | } |
| 7788 | |
| 7789 | asession->expire = tick_add_ifset(now_ms, t->be->timeout.appsession); |
| 7790 | asession->request_count++; |
| 7791 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7792 | } |
| 7793 | |
| 7794 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7795 | /* |
| 7796 | * Check if response is cacheable or not. Updates t->flags. |
| 7797 | */ |
Willy Tarreau | 7421efb | 2012-07-02 15:11:27 +0200 | [diff] [blame] | 7798 | void check_response_for_cacheability(struct session *t, struct channel *rtr) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7799 | { |
| 7800 | struct http_txn *txn = &t->txn; |
| 7801 | char *p1, *p2; |
| 7802 | |
| 7803 | char *cur_ptr, *cur_end, *cur_next; |
| 7804 | int cur_idx; |
| 7805 | |
Willy Tarreau | 5df5187 | 2007-11-25 16:20:08 +0100 | [diff] [blame] | 7806 | if (!(txn->flags & TX_CACHEABLE)) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7807 | return; |
| 7808 | |
| 7809 | /* Iterate through the headers. |
| 7810 | * we start with the start line. |
| 7811 | */ |
| 7812 | cur_idx = 0; |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 7813 | cur_next = rtr->buf->p + hdr_idx_first_pos(&txn->hdr_idx); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7814 | |
| 7815 | while ((cur_idx = txn->hdr_idx.v[cur_idx].next)) { |
| 7816 | struct hdr_idx_elem *cur_hdr; |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 7817 | int val; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7818 | |
| 7819 | cur_hdr = &txn->hdr_idx.v[cur_idx]; |
| 7820 | cur_ptr = cur_next; |
| 7821 | cur_end = cur_ptr + cur_hdr->len; |
| 7822 | cur_next = cur_end + cur_hdr->cr + 1; |
| 7823 | |
| 7824 | /* We have one full header between cur_ptr and cur_end, and the |
| 7825 | * next header starts at cur_next. We're only interested in |
| 7826 | * "Cookie:" headers. |
| 7827 | */ |
| 7828 | |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 7829 | val = http_header_match2(cur_ptr, cur_end, "Pragma", 6); |
| 7830 | if (val) { |
| 7831 | if ((cur_end - (cur_ptr + val) >= 8) && |
| 7832 | strncasecmp(cur_ptr + val, "no-cache", 8) == 0) { |
| 7833 | txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK; |
| 7834 | return; |
| 7835 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7836 | } |
| 7837 | |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 7838 | val = http_header_match2(cur_ptr, cur_end, "Cache-control", 13); |
| 7839 | if (!val) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7840 | continue; |
| 7841 | |
| 7842 | /* OK, right now we know we have a cache-control header at cur_ptr */ |
| 7843 | |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 7844 | p1 = cur_ptr + val; /* first non-space char after 'cache-control:' */ |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7845 | |
| 7846 | if (p1 >= cur_end) /* no more info */ |
| 7847 | continue; |
| 7848 | |
| 7849 | /* p1 is at the beginning of the value */ |
| 7850 | p2 = p1; |
| 7851 | |
Willy Tarreau | 8f8e645 | 2007-06-17 21:51:38 +0200 | [diff] [blame] | 7852 | while (p2 < cur_end && *p2 != '=' && *p2 != ',' && !isspace((unsigned char)*p2)) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7853 | p2++; |
| 7854 | |
| 7855 | /* we have a complete value between p1 and p2 */ |
| 7856 | if (p2 < cur_end && *p2 == '=') { |
| 7857 | /* we have something of the form no-cache="set-cookie" */ |
| 7858 | if ((cur_end - p1 >= 21) && |
| 7859 | strncasecmp(p1, "no-cache=\"set-cookie", 20) == 0 |
| 7860 | && (p1[20] == '"' || p1[20] == ',')) |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 7861 | txn->flags &= ~TX_CACHE_COOK; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7862 | continue; |
| 7863 | } |
| 7864 | |
| 7865 | /* OK, so we know that either p2 points to the end of string or to a comma */ |
| 7866 | if (((p2 - p1 == 7) && strncasecmp(p1, "private", 7) == 0) || |
Willy Tarreau | 5b15f90 | 2013-07-04 12:46:56 +0200 | [diff] [blame] | 7867 | ((p2 - p1 == 8) && strncasecmp(p1, "no-cache", 8) == 0) || |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7868 | ((p2 - p1 == 8) && strncasecmp(p1, "no-store", 8) == 0) || |
| 7869 | ((p2 - p1 == 9) && strncasecmp(p1, "max-age=0", 9) == 0) || |
| 7870 | ((p2 - p1 == 10) && strncasecmp(p1, "s-maxage=0", 10) == 0)) { |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 7871 | txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7872 | return; |
| 7873 | } |
| 7874 | |
| 7875 | if ((p2 - p1 == 6) && strncasecmp(p1, "public", 6) == 0) { |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 7876 | txn->flags |= TX_CACHEABLE | TX_CACHE_COOK; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7877 | continue; |
| 7878 | } |
| 7879 | } |
| 7880 | } |
| 7881 | |
| 7882 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7883 | /* |
| 7884 | * Try to retrieve a known appsession in the URI, then the associated server. |
| 7885 | * If the server is found, it's assigned to the session. |
| 7886 | */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7887 | 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] | 7888 | { |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 7889 | char *end_params, *first_param, *cur_param, *next_param; |
| 7890 | char separator; |
| 7891 | int value_len; |
| 7892 | |
| 7893 | int mode = t->be->options2 & PR_O2_AS_M_ANY; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7894 | |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 7895 | if (t->be->appsession_name == NULL || |
Cyril Bonté | 17530c3 | 2010-04-06 21:11:10 +0200 | [diff] [blame] | 7896 | (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] | 7897 | return; |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 7898 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7899 | |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 7900 | first_param = NULL; |
| 7901 | switch (mode) { |
| 7902 | case PR_O2_AS_M_PP: |
| 7903 | first_param = memchr(begin, ';', len); |
| 7904 | break; |
| 7905 | case PR_O2_AS_M_QS: |
| 7906 | first_param = memchr(begin, '?', len); |
| 7907 | break; |
| 7908 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7909 | |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 7910 | if (first_param == NULL) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7911 | return; |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 7912 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7913 | |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 7914 | switch (mode) { |
| 7915 | case PR_O2_AS_M_PP: |
| 7916 | if ((end_params = memchr(first_param, '?', len - (begin - first_param))) == NULL) { |
| 7917 | end_params = (char *) begin + len; |
| 7918 | } |
| 7919 | separator = ';'; |
| 7920 | break; |
| 7921 | case PR_O2_AS_M_QS: |
| 7922 | end_params = (char *) begin + len; |
| 7923 | separator = '&'; |
| 7924 | break; |
| 7925 | default: |
| 7926 | /* unknown mode, shouldn't happen */ |
| 7927 | return; |
| 7928 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7929 | |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 7930 | cur_param = next_param = end_params; |
| 7931 | while (cur_param > first_param) { |
| 7932 | cur_param--; |
| 7933 | if ((cur_param[0] == separator) || (cur_param == first_param)) { |
| 7934 | /* let's see if this is the appsession parameter */ |
| 7935 | if ((cur_param + t->be->appsession_name_len + 1 < next_param) && |
| 7936 | ((t->be->options2 & PR_O2_AS_PFX) || cur_param[t->be->appsession_name_len + 1] == '=') && |
| 7937 | (strncasecmp(cur_param + 1, t->be->appsession_name, t->be->appsession_name_len) == 0)) { |
| 7938 | /* Cool... it's the right one */ |
| 7939 | cur_param += t->be->appsession_name_len + (t->be->options2 & PR_O2_AS_PFX ? 1 : 2); |
| 7940 | value_len = MIN(t->be->appsession_len, next_param - cur_param); |
| 7941 | if (value_len > 0) { |
| 7942 | manage_client_side_appsession(t, cur_param, value_len); |
| 7943 | } |
| 7944 | break; |
| 7945 | } |
| 7946 | next_param = cur_param; |
| 7947 | } |
| 7948 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7949 | #if defined(DEBUG_HASH) |
Aleksandar Lazic | 697bbb0 | 2008-08-13 19:57:02 +0200 | [diff] [blame] | 7950 | Alert("get_srv_from_appsession\n"); |
Willy Tarreau | 51041c7 | 2007-09-09 21:56:53 +0200 | [diff] [blame] | 7951 | appsession_hash_dump(&(t->be->htbl_proxy)); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7952 | #endif |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7953 | } |
| 7954 | |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 7955 | /* |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 7956 | * 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] | 7957 | * for the current backend. |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 7958 | * |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 7959 | * 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] | 7960 | * uri_auth field is valid. |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 7961 | * |
Krzysztof Piotr Oledzki | 8c8bd45 | 2010-01-29 19:29:32 +0100 | [diff] [blame] | 7962 | * Returns 1 if stats should be provided, otherwise 0. |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 7963 | */ |
Willy Tarreau | 295a837 | 2011-03-10 11:25:07 +0100 | [diff] [blame] | 7964 | 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] | 7965 | { |
| 7966 | struct uri_auth *uri_auth = backend->uri_auth; |
Willy Tarreau | 3a215be | 2012-03-09 21:39:51 +0100 | [diff] [blame] | 7967 | struct http_msg *msg = &txn->req; |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 7968 | const char *uri = msg->chn->buf->p+ msg->sl.rq.u; |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 7969 | |
Krzysztof Piotr Oledzki | 8c8bd45 | 2010-01-29 19:29:32 +0100 | [diff] [blame] | 7970 | if (!uri_auth) |
| 7971 | return 0; |
| 7972 | |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 7973 | 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] | 7974 | return 0; |
| 7975 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7976 | /* check URI size */ |
Willy Tarreau | 3a215be | 2012-03-09 21:39:51 +0100 | [diff] [blame] | 7977 | if (uri_auth->uri_len > msg->sl.rq.u_l) |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 7978 | return 0; |
| 7979 | |
Willy Tarreau | 414e9bb | 2013-11-23 00:30:38 +0100 | [diff] [blame] | 7980 | if (memcmp(uri, uri_auth->uri_prefix, uri_auth->uri_len) != 0) |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 7981 | return 0; |
| 7982 | |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 7983 | return 1; |
| 7984 | } |
| 7985 | |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 7986 | /* |
| 7987 | * 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] | 7988 | * By default it tries to report the error position as msg->err_pos. However if |
| 7989 | * this one is not set, it will then report msg->next, which is the last known |
| 7990 | * 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] | 7991 | * displays buffers as a contiguous area starting at buf->p. |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 7992 | */ |
| 7993 | void http_capture_bad_message(struct error_snapshot *es, struct session *s, |
Willy Tarreau | 8a0cef2 | 2012-03-09 13:39:23 +0100 | [diff] [blame] | 7994 | struct http_msg *msg, |
Willy Tarreau | 3770f23 | 2013-12-07 00:01:53 +0100 | [diff] [blame] | 7995 | enum ht_state state, struct proxy *other_end) |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 7996 | { |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 7997 | struct channel *chn = msg->chn; |
Willy Tarreau | 69d8c5d | 2012-05-08 09:44:41 +0200 | [diff] [blame] | 7998 | int len1, len2; |
Willy Tarreau | 8a0cef2 | 2012-03-09 13:39:23 +0100 | [diff] [blame] | 7999 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 8000 | es->len = MIN(chn->buf->i, sizeof(es->buf)); |
| 8001 | len1 = chn->buf->data + chn->buf->size - chn->buf->p; |
Willy Tarreau | 69d8c5d | 2012-05-08 09:44:41 +0200 | [diff] [blame] | 8002 | len1 = MIN(len1, es->len); |
| 8003 | len2 = es->len - len1; /* remaining data if buffer wraps */ |
| 8004 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 8005 | memcpy(es->buf, chn->buf->p, len1); |
Willy Tarreau | 69d8c5d | 2012-05-08 09:44:41 +0200 | [diff] [blame] | 8006 | if (len2) |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 8007 | memcpy(es->buf + len1, chn->buf->data, len2); |
Willy Tarreau | 81f2fb9 | 2010-12-12 13:09:08 +0100 | [diff] [blame] | 8008 | |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 8009 | if (msg->err_pos >= 0) |
Willy Tarreau | 69d8c5d | 2012-05-08 09:44:41 +0200 | [diff] [blame] | 8010 | es->pos = msg->err_pos; |
Willy Tarreau | 81f2fb9 | 2010-12-12 13:09:08 +0100 | [diff] [blame] | 8011 | else |
Willy Tarreau | 69d8c5d | 2012-05-08 09:44:41 +0200 | [diff] [blame] | 8012 | es->pos = msg->next; |
Willy Tarreau | 81f2fb9 | 2010-12-12 13:09:08 +0100 | [diff] [blame] | 8013 | |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 8014 | es->when = date; // user-visible date |
| 8015 | es->sid = s->uniq_id; |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 8016 | es->srv = objt_server(s->target); |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 8017 | es->oe = other_end; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 8018 | if (objt_conn(s->req->prod->end)) |
| 8019 | es->src = __objt_conn(s->req->prod->end)->addr.from; |
| 8020 | else |
| 8021 | memset(&es->src, 0, sizeof(es->src)); |
| 8022 | |
Willy Tarreau | 078272e | 2010-12-12 12:46:33 +0100 | [diff] [blame] | 8023 | es->state = state; |
Willy Tarreau | 10479e4 | 2010-12-12 14:00:34 +0100 | [diff] [blame] | 8024 | es->ev_id = error_snapshot_id++; |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 8025 | es->b_flags = chn->flags; |
Willy Tarreau | d04b1bc | 2012-05-08 11:03:10 +0200 | [diff] [blame] | 8026 | es->s_flags = s->flags; |
| 8027 | es->t_flags = s->txn.flags; |
| 8028 | es->m_flags = msg->flags; |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 8029 | es->b_out = chn->buf->o; |
| 8030 | es->b_wrap = chn->buf->data + chn->buf->size - chn->buf->p; |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 8031 | es->b_tot = chn->total; |
Willy Tarreau | d04b1bc | 2012-05-08 11:03:10 +0200 | [diff] [blame] | 8032 | es->m_clen = msg->chunk_len; |
| 8033 | es->m_blen = msg->body_len; |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 8034 | } |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 8035 | |
Willy Tarreau | 294c473 | 2011-12-16 21:35:50 +0100 | [diff] [blame] | 8036 | /* Return in <vptr> and <vlen> the pointer and length of occurrence <occ> of |
| 8037 | * header whose name is <hname> of length <hlen>. If <ctx> is null, lookup is |
| 8038 | * performed over the whole headers. Otherwise it must contain a valid header |
| 8039 | * context, initialised with ctx->idx=0 for the first lookup in a series. If |
| 8040 | * <occ> is positive or null, occurrence #occ from the beginning (or last ctx) |
| 8041 | * is returned. Occ #0 and #1 are equivalent. If <occ> is negative (and no less |
| 8042 | * 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] | 8043 | * -1. The value fetch stops at commas, so this function is suited for use with |
| 8044 | * list headers. |
Willy Tarreau | 294c473 | 2011-12-16 21:35:50 +0100 | [diff] [blame] | 8045 | * 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] | 8046 | */ |
Willy Tarreau | 185b5c4 | 2012-04-26 15:11:51 +0200 | [diff] [blame] | 8047 | 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] | 8048 | struct hdr_idx *idx, int occ, |
| 8049 | struct hdr_ctx *ctx, char **vptr, int *vlen) |
Willy Tarreau | bce7088 | 2009-09-07 11:51:47 +0200 | [diff] [blame] | 8050 | { |
Willy Tarreau | 294c473 | 2011-12-16 21:35:50 +0100 | [diff] [blame] | 8051 | struct hdr_ctx local_ctx; |
| 8052 | char *ptr_hist[MAX_HDR_HISTORY]; |
| 8053 | int len_hist[MAX_HDR_HISTORY]; |
Willy Tarreau | bce7088 | 2009-09-07 11:51:47 +0200 | [diff] [blame] | 8054 | unsigned int hist_ptr; |
Willy Tarreau | 294c473 | 2011-12-16 21:35:50 +0100 | [diff] [blame] | 8055 | int found; |
Willy Tarreau | bce7088 | 2009-09-07 11:51:47 +0200 | [diff] [blame] | 8056 | |
Willy Tarreau | 294c473 | 2011-12-16 21:35:50 +0100 | [diff] [blame] | 8057 | if (!ctx) { |
| 8058 | local_ctx.idx = 0; |
| 8059 | ctx = &local_ctx; |
| 8060 | } |
| 8061 | |
Willy Tarreau | bce7088 | 2009-09-07 11:51:47 +0200 | [diff] [blame] | 8062 | if (occ >= 0) { |
Willy Tarreau | 294c473 | 2011-12-16 21:35:50 +0100 | [diff] [blame] | 8063 | /* search from the beginning */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 8064 | while (http_find_header2(hname, hlen, msg->chn->buf->p, idx, ctx)) { |
Willy Tarreau | bce7088 | 2009-09-07 11:51:47 +0200 | [diff] [blame] | 8065 | occ--; |
| 8066 | if (occ <= 0) { |
Willy Tarreau | 294c473 | 2011-12-16 21:35:50 +0100 | [diff] [blame] | 8067 | *vptr = ctx->line + ctx->val; |
| 8068 | *vlen = ctx->vlen; |
| 8069 | return 1; |
Willy Tarreau | bce7088 | 2009-09-07 11:51:47 +0200 | [diff] [blame] | 8070 | } |
| 8071 | } |
Willy Tarreau | 294c473 | 2011-12-16 21:35:50 +0100 | [diff] [blame] | 8072 | return 0; |
Willy Tarreau | bce7088 | 2009-09-07 11:51:47 +0200 | [diff] [blame] | 8073 | } |
| 8074 | |
| 8075 | /* negative occurrence, we scan all the list then walk back */ |
| 8076 | if (-occ > MAX_HDR_HISTORY) |
| 8077 | return 0; |
| 8078 | |
Willy Tarreau | 294c473 | 2011-12-16 21:35:50 +0100 | [diff] [blame] | 8079 | found = hist_ptr = 0; |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 8080 | while (http_find_header2(hname, hlen, msg->chn->buf->p, idx, ctx)) { |
Willy Tarreau | 294c473 | 2011-12-16 21:35:50 +0100 | [diff] [blame] | 8081 | ptr_hist[hist_ptr] = ctx->line + ctx->val; |
| 8082 | len_hist[hist_ptr] = ctx->vlen; |
| 8083 | if (++hist_ptr >= MAX_HDR_HISTORY) |
Willy Tarreau | bce7088 | 2009-09-07 11:51:47 +0200 | [diff] [blame] | 8084 | hist_ptr = 0; |
| 8085 | found++; |
| 8086 | } |
| 8087 | if (-occ > found) |
| 8088 | return 0; |
| 8089 | /* 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] | 8090 | * find occurrence -occ. 0 <= hist_ptr < MAX_HDR_HISTORY, and we have |
| 8091 | * -10 <= occ <= -1. So we have to check [hist_ptr%MAX_HDR_HISTORY+occ] |
| 8092 | * to remain in the 0..9 range. |
Willy Tarreau | bce7088 | 2009-09-07 11:51:47 +0200 | [diff] [blame] | 8093 | */ |
Willy Tarreau | 67dad27 | 2013-06-12 22:27:44 +0200 | [diff] [blame] | 8094 | hist_ptr += occ + MAX_HDR_HISTORY; |
Willy Tarreau | bce7088 | 2009-09-07 11:51:47 +0200 | [diff] [blame] | 8095 | if (hist_ptr >= MAX_HDR_HISTORY) |
| 8096 | hist_ptr -= MAX_HDR_HISTORY; |
Willy Tarreau | 294c473 | 2011-12-16 21:35:50 +0100 | [diff] [blame] | 8097 | *vptr = ptr_hist[hist_ptr]; |
| 8098 | *vlen = len_hist[hist_ptr]; |
| 8099 | return 1; |
Willy Tarreau | bce7088 | 2009-09-07 11:51:47 +0200 | [diff] [blame] | 8100 | } |
| 8101 | |
Willy Tarreau | 04ff9f1 | 2013-06-10 18:39:42 +0200 | [diff] [blame] | 8102 | /* Return in <vptr> and <vlen> the pointer and length of occurrence <occ> of |
| 8103 | * header whose name is <hname> of length <hlen>. If <ctx> is null, lookup is |
| 8104 | * performed over the whole headers. Otherwise it must contain a valid header |
| 8105 | * context, initialised with ctx->idx=0 for the first lookup in a series. If |
| 8106 | * <occ> is positive or null, occurrence #occ from the beginning (or last ctx) |
| 8107 | * is returned. Occ #0 and #1 are equivalent. If <occ> is negative (and no less |
| 8108 | * than -MAX_HDR_HISTORY), the occurrence is counted from the last one which is |
| 8109 | * -1. This function differs from http_get_hdr() in that it only returns full |
| 8110 | * line header values and does not stop at commas. |
| 8111 | * The return value is 0 if nothing was found, or non-zero otherwise. |
| 8112 | */ |
| 8113 | unsigned int http_get_fhdr(const struct http_msg *msg, const char *hname, int hlen, |
| 8114 | struct hdr_idx *idx, int occ, |
| 8115 | struct hdr_ctx *ctx, char **vptr, int *vlen) |
| 8116 | { |
| 8117 | struct hdr_ctx local_ctx; |
| 8118 | char *ptr_hist[MAX_HDR_HISTORY]; |
| 8119 | int len_hist[MAX_HDR_HISTORY]; |
| 8120 | unsigned int hist_ptr; |
| 8121 | int found; |
| 8122 | |
| 8123 | if (!ctx) { |
| 8124 | local_ctx.idx = 0; |
| 8125 | ctx = &local_ctx; |
| 8126 | } |
| 8127 | |
| 8128 | if (occ >= 0) { |
| 8129 | /* search from the beginning */ |
| 8130 | while (http_find_full_header2(hname, hlen, msg->chn->buf->p, idx, ctx)) { |
| 8131 | occ--; |
| 8132 | if (occ <= 0) { |
| 8133 | *vptr = ctx->line + ctx->val; |
| 8134 | *vlen = ctx->vlen; |
| 8135 | return 1; |
| 8136 | } |
| 8137 | } |
| 8138 | return 0; |
| 8139 | } |
| 8140 | |
| 8141 | /* negative occurrence, we scan all the list then walk back */ |
| 8142 | if (-occ > MAX_HDR_HISTORY) |
| 8143 | return 0; |
| 8144 | |
| 8145 | found = hist_ptr = 0; |
| 8146 | while (http_find_full_header2(hname, hlen, msg->chn->buf->p, idx, ctx)) { |
| 8147 | ptr_hist[hist_ptr] = ctx->line + ctx->val; |
| 8148 | len_hist[hist_ptr] = ctx->vlen; |
| 8149 | if (++hist_ptr >= MAX_HDR_HISTORY) |
| 8150 | hist_ptr = 0; |
| 8151 | found++; |
| 8152 | } |
| 8153 | if (-occ > found) |
| 8154 | return 0; |
| 8155 | /* OK now we have the last occurrence in [hist_ptr-1], and we need to |
| 8156 | * find occurrence -occ, so we have to check [hist_ptr+occ]. |
| 8157 | */ |
| 8158 | hist_ptr += occ; |
| 8159 | if (hist_ptr >= MAX_HDR_HISTORY) |
| 8160 | hist_ptr -= MAX_HDR_HISTORY; |
| 8161 | *vptr = ptr_hist[hist_ptr]; |
| 8162 | *vlen = len_hist[hist_ptr]; |
| 8163 | return 1; |
| 8164 | } |
| 8165 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 8166 | /* |
Willy Tarreau | e92693a | 2012-09-24 21:13:39 +0200 | [diff] [blame] | 8167 | * Print a debug line with a header. Always stop at the first CR or LF char, |
| 8168 | * so it is safe to pass it a full buffer if needed. If <err> is not NULL, an |
| 8169 | * arrow is printed after the line which contains the pointer. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 8170 | */ |
| 8171 | void debug_hdr(const char *dir, struct session *t, const char *start, const char *end) |
| 8172 | { |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 8173 | int max; |
| 8174 | 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] | 8175 | dir, |
| 8176 | objt_conn(t->req->prod->end) ? (unsigned short)objt_conn(t->req->prod->end)->t.sock.fd : -1, |
| 8177 | 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] | 8178 | |
| 8179 | for (max = 0; start + max < end; max++) |
| 8180 | if (start[max] == '\r' || start[max] == '\n') |
| 8181 | break; |
| 8182 | |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 8183 | UBOUND(max, trash.size - trash.len - 3); |
| 8184 | trash.len += strlcpy2(trash.str + trash.len, start, max + 1); |
| 8185 | trash.str[trash.len++] = '\n'; |
Willy Tarreau | 89efaed | 2013-12-13 15:14:55 +0100 | [diff] [blame] | 8186 | shut_your_big_mouth_gcc(write(1, trash.str, trash.len)); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 8187 | } |
| 8188 | |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 8189 | /* |
| 8190 | * Initialize a new HTTP transaction for session <s>. It is assumed that all |
| 8191 | * the required fields are properly allocated and that we only need to (re)init |
| 8192 | * them. This should be used before processing any new request. |
| 8193 | */ |
| 8194 | void http_init_txn(struct session *s) |
| 8195 | { |
| 8196 | struct http_txn *txn = &s->txn; |
| 8197 | struct proxy *fe = s->fe; |
| 8198 | |
| 8199 | txn->flags = 0; |
| 8200 | txn->status = -1; |
| 8201 | |
Willy Tarreau | f64d141 | 2010-10-07 20:06:11 +0200 | [diff] [blame] | 8202 | txn->cookie_first_date = 0; |
| 8203 | txn->cookie_last_date = 0; |
| 8204 | |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 8205 | txn->req.flags = 0; |
Willy Tarreau | 2692736 | 2012-05-18 23:22:52 +0200 | [diff] [blame] | 8206 | 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] | 8207 | txn->req.next = 0; |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 8208 | txn->rsp.flags = 0; |
Willy Tarreau | 2692736 | 2012-05-18 23:22:52 +0200 | [diff] [blame] | 8209 | 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] | 8210 | txn->rsp.next = 0; |
Willy Tarreau | 124d991 | 2011-03-01 20:30:48 +0100 | [diff] [blame] | 8211 | txn->req.chunk_len = 0LL; |
| 8212 | txn->req.body_len = 0LL; |
| 8213 | txn->rsp.chunk_len = 0LL; |
| 8214 | txn->rsp.body_len = 0LL; |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 8215 | txn->req.msg_state = HTTP_MSG_RQBEFORE; /* at the very beginning of the request */ |
| 8216 | 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] | 8217 | txn->req.chn = s->req; |
| 8218 | txn->rsp.chn = s->rep; |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 8219 | |
| 8220 | txn->auth.method = HTTP_AUTH_UNKNOWN; |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 8221 | |
| 8222 | txn->req.err_pos = txn->rsp.err_pos = -2; /* block buggy requests/responses */ |
| 8223 | if (fe->options2 & PR_O2_REQBUG_OK) |
| 8224 | txn->req.err_pos = -1; /* let buggy requests pass */ |
| 8225 | |
Willy Tarreau | 4602363 | 2010-01-07 22:51:47 +0100 | [diff] [blame] | 8226 | if (txn->req.cap) |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 8227 | memset(txn->req.cap, 0, fe->nb_req_cap * sizeof(void *)); |
| 8228 | |
Willy Tarreau | 4602363 | 2010-01-07 22:51:47 +0100 | [diff] [blame] | 8229 | if (txn->rsp.cap) |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 8230 | memset(txn->rsp.cap, 0, fe->nb_rsp_cap * sizeof(void *)); |
| 8231 | |
| 8232 | if (txn->hdr_idx.v) |
| 8233 | hdr_idx_init(&txn->hdr_idx); |
| 8234 | } |
| 8235 | |
| 8236 | /* to be used at the end of a transaction */ |
| 8237 | void http_end_txn(struct session *s) |
| 8238 | { |
| 8239 | struct http_txn *txn = &s->txn; |
| 8240 | |
Willy Tarreau | 7519560 | 2014-03-11 15:48:55 +0100 | [diff] [blame] | 8241 | /* release any possible compression context */ |
| 8242 | if (s->flags & SN_COMP_READY) |
| 8243 | s->comp_algo->end(&s->comp_ctx); |
| 8244 | s->comp_algo = NULL; |
| 8245 | s->flags &= ~SN_COMP_READY; |
| 8246 | |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 8247 | /* these ones will have been dynamically allocated */ |
| 8248 | pool_free2(pool2_requri, txn->uri); |
| 8249 | pool_free2(pool2_capture, txn->cli_cookie); |
| 8250 | pool_free2(pool2_capture, txn->srv_cookie); |
Willy Tarreau | a3377ee | 2010-01-10 10:49:11 +0100 | [diff] [blame] | 8251 | pool_free2(apools.sessid, txn->sessid); |
William Lallemand | a73203e | 2012-03-12 12:48:57 +0100 | [diff] [blame] | 8252 | pool_free2(pool2_uniqueid, s->unique_id); |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 8253 | |
William Lallemand | a73203e | 2012-03-12 12:48:57 +0100 | [diff] [blame] | 8254 | s->unique_id = NULL; |
Willy Tarreau | a3377ee | 2010-01-10 10:49:11 +0100 | [diff] [blame] | 8255 | txn->sessid = NULL; |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 8256 | txn->uri = NULL; |
| 8257 | txn->srv_cookie = NULL; |
| 8258 | txn->cli_cookie = NULL; |
Willy Tarreau | 4602363 | 2010-01-07 22:51:47 +0100 | [diff] [blame] | 8259 | |
| 8260 | if (txn->req.cap) { |
| 8261 | struct cap_hdr *h; |
| 8262 | for (h = s->fe->req_cap; h; h = h->next) |
| 8263 | pool_free2(h->pool, txn->req.cap[h->index]); |
| 8264 | memset(txn->req.cap, 0, s->fe->nb_req_cap * sizeof(void *)); |
| 8265 | } |
| 8266 | |
| 8267 | if (txn->rsp.cap) { |
| 8268 | struct cap_hdr *h; |
| 8269 | for (h = s->fe->rsp_cap; h; h = h->next) |
| 8270 | pool_free2(h->pool, txn->rsp.cap[h->index]); |
| 8271 | memset(txn->rsp.cap, 0, s->fe->nb_rsp_cap * sizeof(void *)); |
| 8272 | } |
| 8273 | |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 8274 | } |
| 8275 | |
| 8276 | /* to be used at the end of a transaction to prepare a new one */ |
| 8277 | void http_reset_txn(struct session *s) |
| 8278 | { |
| 8279 | http_end_txn(s); |
| 8280 | http_init_txn(s); |
| 8281 | |
| 8282 | s->be = s->fe; |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 8283 | s->logs.logwait = s->fe->to_log; |
Willy Tarreau | abcd514 | 2013-06-11 17:18:02 +0200 | [diff] [blame] | 8284 | s->logs.level = 0; |
Simon Horman | af51495 | 2011-06-21 14:34:57 +0900 | [diff] [blame] | 8285 | session_del_srv_conn(s); |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 8286 | s->target = NULL; |
Emeric Brun | b982a3d | 2010-01-04 15:45:53 +0100 | [diff] [blame] | 8287 | /* re-init store persistence */ |
| 8288 | s->store_count = 0; |
Willy Tarreau | 1f0da24 | 2014-01-25 11:01:50 +0100 | [diff] [blame] | 8289 | s->uniq_id = global.req_count++; |
Emeric Brun | b982a3d | 2010-01-04 15:45:53 +0100 | [diff] [blame] | 8290 | |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 8291 | s->pend_pos = NULL; |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 8292 | |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 8293 | s->req->flags |= CF_READ_DONTWAIT; /* one read is usually enough */ |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 8294 | |
Willy Tarreau | 739cfba | 2010-01-25 23:11:14 +0100 | [diff] [blame] | 8295 | /* We must trim any excess data from the response buffer, because we |
| 8296 | * may have blocked an invalid response from a server that we don't |
| 8297 | * want to accidentely forward once we disable the analysers, nor do |
| 8298 | * we want those data to come along with next response. A typical |
| 8299 | * example of such data would be from a buggy server responding to |
| 8300 | * a HEAD with some data, or sending more than the advertised |
| 8301 | * content-length. |
| 8302 | */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 8303 | if (unlikely(s->rep->buf->i)) |
| 8304 | s->rep->buf->i = 0; |
Willy Tarreau | 739cfba | 2010-01-25 23:11:14 +0100 | [diff] [blame] | 8305 | |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 8306 | s->req->rto = s->fe->timeout.client; |
Willy Tarreau | d04e858 | 2010-05-31 12:31:35 +0200 | [diff] [blame] | 8307 | s->req->wto = TICK_ETERNITY; |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 8308 | |
Willy Tarreau | d04e858 | 2010-05-31 12:31:35 +0200 | [diff] [blame] | 8309 | s->rep->rto = TICK_ETERNITY; |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 8310 | s->rep->wto = s->fe->timeout.client; |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 8311 | |
| 8312 | s->req->rex = TICK_ETERNITY; |
| 8313 | s->req->wex = TICK_ETERNITY; |
| 8314 | s->req->analyse_exp = TICK_ETERNITY; |
| 8315 | s->rep->rex = TICK_ETERNITY; |
| 8316 | s->rep->wex = TICK_ETERNITY; |
| 8317 | s->rep->analyse_exp = TICK_ETERNITY; |
| 8318 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 8319 | |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 8320 | void free_http_req_rules(struct list *r) { |
| 8321 | struct http_req_rule *tr, *pr; |
| 8322 | |
| 8323 | list_for_each_entry_safe(pr, tr, r, list) { |
| 8324 | LIST_DEL(&pr->list); |
Willy Tarreau | 20b0de5 | 2012-12-24 15:45:22 +0100 | [diff] [blame] | 8325 | if (pr->action == HTTP_REQ_ACT_AUTH) |
Willy Tarreau | 5c2e198 | 2012-12-24 12:00:25 +0100 | [diff] [blame] | 8326 | free(pr->arg.auth.realm); |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 8327 | |
| 8328 | free(pr); |
| 8329 | } |
| 8330 | } |
| 8331 | |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 8332 | /* parse an "http-request" rule */ |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 8333 | struct http_req_rule *parse_http_req_cond(const char **args, const char *file, int linenum, struct proxy *proxy) |
| 8334 | { |
| 8335 | struct http_req_rule *rule; |
| 8336 | int cur_arg; |
| 8337 | |
| 8338 | rule = (struct http_req_rule*)calloc(1, sizeof(struct http_req_rule)); |
| 8339 | if (!rule) { |
| 8340 | Alert("parsing [%s:%d]: out of memory.\n", file, linenum); |
Willy Tarreau | 81499eb | 2012-12-27 12:19:02 +0100 | [diff] [blame] | 8341 | goto out_err; |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 8342 | } |
| 8343 | |
Willy Tarreau | 5c2e198 | 2012-12-24 12:00:25 +0100 | [diff] [blame] | 8344 | if (!strcmp(args[0], "allow")) { |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 8345 | rule->action = HTTP_REQ_ACT_ALLOW; |
| 8346 | cur_arg = 1; |
| 8347 | } else if (!strcmp(args[0], "deny")) { |
| 8348 | rule->action = HTTP_REQ_ACT_DENY; |
| 8349 | cur_arg = 1; |
Willy Tarreau | ccbcc37 | 2012-12-27 12:37:57 +0100 | [diff] [blame] | 8350 | } else if (!strcmp(args[0], "tarpit")) { |
| 8351 | rule->action = HTTP_REQ_ACT_TARPIT; |
| 8352 | cur_arg = 1; |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 8353 | } else if (!strcmp(args[0], "auth")) { |
Willy Tarreau | 20b0de5 | 2012-12-24 15:45:22 +0100 | [diff] [blame] | 8354 | rule->action = HTTP_REQ_ACT_AUTH; |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 8355 | cur_arg = 1; |
| 8356 | |
| 8357 | while(*args[cur_arg]) { |
| 8358 | if (!strcmp(args[cur_arg], "realm")) { |
Willy Tarreau | 5c2e198 | 2012-12-24 12:00:25 +0100 | [diff] [blame] | 8359 | rule->arg.auth.realm = strdup(args[cur_arg + 1]); |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 8360 | cur_arg+=2; |
| 8361 | continue; |
| 8362 | } else |
| 8363 | break; |
| 8364 | } |
Willy Tarreau | f4c43c1 | 2013-06-11 17:01:13 +0200 | [diff] [blame] | 8365 | } else if (!strcmp(args[0], "set-nice")) { |
| 8366 | rule->action = HTTP_REQ_ACT_SET_NICE; |
| 8367 | cur_arg = 1; |
| 8368 | |
| 8369 | if (!*args[cur_arg] || |
| 8370 | (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) { |
| 8371 | Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (integer value).\n", |
| 8372 | file, linenum, args[0]); |
| 8373 | goto out_err; |
| 8374 | } |
| 8375 | rule->arg.nice = atoi(args[cur_arg]); |
| 8376 | if (rule->arg.nice < -1024) |
| 8377 | rule->arg.nice = -1024; |
| 8378 | else if (rule->arg.nice > 1024) |
| 8379 | rule->arg.nice = 1024; |
| 8380 | cur_arg++; |
Willy Tarreau | 42cf39e | 2013-06-11 18:51:32 +0200 | [diff] [blame] | 8381 | } else if (!strcmp(args[0], "set-tos")) { |
| 8382 | #ifdef IP_TOS |
| 8383 | char *err; |
| 8384 | rule->action = HTTP_REQ_ACT_SET_TOS; |
| 8385 | cur_arg = 1; |
| 8386 | |
| 8387 | if (!*args[cur_arg] || |
| 8388 | (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) { |
| 8389 | Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (integer/hex value).\n", |
| 8390 | file, linenum, args[0]); |
| 8391 | goto out_err; |
| 8392 | } |
| 8393 | |
| 8394 | rule->arg.tos = strtol(args[cur_arg], &err, 0); |
| 8395 | if (err && *err != '\0') { |
| 8396 | Alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-request %s' (integer/hex value expected).\n", |
| 8397 | file, linenum, err, args[0]); |
| 8398 | goto out_err; |
| 8399 | } |
| 8400 | cur_arg++; |
| 8401 | #else |
| 8402 | Alert("parsing [%s:%d]: 'http-request %s' is not supported on this platform (IP_TOS undefined).\n", file, linenum, args[0]); |
| 8403 | goto out_err; |
| 8404 | #endif |
Willy Tarreau | 51347ed | 2013-06-11 19:34:13 +0200 | [diff] [blame] | 8405 | } else if (!strcmp(args[0], "set-mark")) { |
| 8406 | #ifdef SO_MARK |
| 8407 | char *err; |
| 8408 | rule->action = HTTP_REQ_ACT_SET_MARK; |
| 8409 | cur_arg = 1; |
| 8410 | |
| 8411 | if (!*args[cur_arg] || |
| 8412 | (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) { |
| 8413 | Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (integer/hex value).\n", |
| 8414 | file, linenum, args[0]); |
| 8415 | goto out_err; |
| 8416 | } |
| 8417 | |
| 8418 | rule->arg.mark = strtoul(args[cur_arg], &err, 0); |
| 8419 | if (err && *err != '\0') { |
| 8420 | Alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-request %s' (integer/hex value expected).\n", |
| 8421 | file, linenum, err, args[0]); |
| 8422 | goto out_err; |
| 8423 | } |
| 8424 | cur_arg++; |
| 8425 | global.last_checks |= LSTCHK_NETADM; |
| 8426 | #else |
| 8427 | Alert("parsing [%s:%d]: 'http-request %s' is not supported on this platform (SO_MARK undefined).\n", file, linenum, args[0]); |
| 8428 | goto out_err; |
| 8429 | #endif |
Willy Tarreau | 9a355ec | 2013-06-11 17:45:46 +0200 | [diff] [blame] | 8430 | } else if (!strcmp(args[0], "set-log-level")) { |
| 8431 | rule->action = HTTP_REQ_ACT_SET_LOGL; |
| 8432 | cur_arg = 1; |
| 8433 | |
| 8434 | if (!*args[cur_arg] || |
| 8435 | (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) { |
| 8436 | bad_log_level: |
| 8437 | Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (log level name or 'silent').\n", |
| 8438 | file, linenum, args[0]); |
| 8439 | goto out_err; |
| 8440 | } |
| 8441 | if (strcmp(args[cur_arg], "silent") == 0) |
| 8442 | rule->arg.loglevel = -1; |
| 8443 | else if ((rule->arg.loglevel = get_log_level(args[cur_arg]) + 1) == 0) |
| 8444 | goto bad_log_level; |
| 8445 | cur_arg++; |
Willy Tarreau | 20b0de5 | 2012-12-24 15:45:22 +0100 | [diff] [blame] | 8446 | } else if (strcmp(args[0], "add-header") == 0 || strcmp(args[0], "set-header") == 0) { |
| 8447 | rule->action = *args[0] == 'a' ? HTTP_REQ_ACT_ADD_HDR : HTTP_REQ_ACT_SET_HDR; |
| 8448 | cur_arg = 1; |
| 8449 | |
Willy Tarreau | 8d1c516 | 2013-04-03 14:13:58 +0200 | [diff] [blame] | 8450 | if (!*args[cur_arg] || !*args[cur_arg+1] || |
| 8451 | (*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] | 8452 | Alert("parsing [%s:%d]: 'http-request %s' expects exactly 2 arguments.\n", |
| 8453 | file, linenum, args[0]); |
Willy Tarreau | 81499eb | 2012-12-27 12:19:02 +0100 | [diff] [blame] | 8454 | goto out_err; |
Willy Tarreau | 20b0de5 | 2012-12-24 15:45:22 +0100 | [diff] [blame] | 8455 | } |
| 8456 | |
| 8457 | rule->arg.hdr_add.name = strdup(args[cur_arg]); |
| 8458 | rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name); |
| 8459 | LIST_INIT(&rule->arg.hdr_add.fmt); |
Willy Tarreau | a4312fa | 2013-04-02 16:34:32 +0200 | [diff] [blame] | 8460 | |
Thierry FOURNIER | 1c0054f | 2013-11-20 15:09:52 +0100 | [diff] [blame] | 8461 | proxy->conf.args.ctx = ARGC_HRQ; |
Willy Tarreau | 434c57c | 2013-01-08 01:10:24 +0100 | [diff] [blame] | 8462 | parse_logformat_string(args[cur_arg + 1], proxy, &rule->arg.hdr_add.fmt, 0, |
| 8463 | (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR); |
Willy Tarreau | 59ad1a2 | 2014-01-29 14:39:58 +0100 | [diff] [blame] | 8464 | free(proxy->conf.lfs_file); |
| 8465 | proxy->conf.lfs_file = strdup(proxy->conf.args.file); |
| 8466 | proxy->conf.lfs_line = proxy->conf.args.line; |
Willy Tarreau | 20b0de5 | 2012-12-24 15:45:22 +0100 | [diff] [blame] | 8467 | cur_arg += 2; |
Willy Tarreau | 81499eb | 2012-12-27 12:19:02 +0100 | [diff] [blame] | 8468 | } else if (strcmp(args[0], "redirect") == 0) { |
| 8469 | struct redirect_rule *redir; |
Willy Tarreau | 6d4890c | 2013-11-18 18:04:25 +0100 | [diff] [blame] | 8470 | char *errmsg = NULL; |
Willy Tarreau | 81499eb | 2012-12-27 12:19:02 +0100 | [diff] [blame] | 8471 | |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 8472 | 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] | 8473 | Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : %s.\n", |
| 8474 | file, linenum, proxy_type_str(proxy), proxy->id, args[0], errmsg); |
| 8475 | goto out_err; |
| 8476 | } |
| 8477 | |
| 8478 | /* this redirect rule might already contain a parsed condition which |
| 8479 | * we'll pass to the http-request rule. |
| 8480 | */ |
| 8481 | rule->action = HTTP_REQ_ACT_REDIR; |
| 8482 | rule->arg.redir = redir; |
| 8483 | rule->cond = redir->cond; |
| 8484 | redir->cond = NULL; |
| 8485 | cur_arg = 2; |
| 8486 | return rule; |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 8487 | } else { |
Willy Tarreau | 51347ed | 2013-06-11 19:34:13 +0200 | [diff] [blame] | 8488 | 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] | 8489 | file, linenum, args[0], *args[0] ? "" : " (missing argument)"); |
Willy Tarreau | 81499eb | 2012-12-27 12:19:02 +0100 | [diff] [blame] | 8490 | goto out_err; |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 8491 | } |
| 8492 | |
| 8493 | if (strcmp(args[cur_arg], "if") == 0 || strcmp(args[cur_arg], "unless") == 0) { |
| 8494 | struct acl_cond *cond; |
Willy Tarreau | b7451bb | 2012-04-27 12:38:15 +0200 | [diff] [blame] | 8495 | char *errmsg = NULL; |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 8496 | |
Willy Tarreau | b7451bb | 2012-04-27 12:38:15 +0200 | [diff] [blame] | 8497 | if ((cond = build_acl_cond(file, linenum, proxy, args+cur_arg, &errmsg)) == NULL) { |
| 8498 | Alert("parsing [%s:%d] : error detected while parsing an 'http-request %s' condition : %s.\n", |
| 8499 | file, linenum, args[0], errmsg); |
| 8500 | free(errmsg); |
Willy Tarreau | 81499eb | 2012-12-27 12:19:02 +0100 | [diff] [blame] | 8501 | goto out_err; |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 8502 | } |
| 8503 | rule->cond = cond; |
| 8504 | } |
| 8505 | else if (*args[cur_arg]) { |
| 8506 | Alert("parsing [%s:%d]: 'http-request %s' expects 'realm' for 'auth' or" |
| 8507 | " either 'if' or 'unless' followed by a condition but found '%s'.\n", |
| 8508 | file, linenum, args[0], args[cur_arg]); |
Willy Tarreau | 81499eb | 2012-12-27 12:19:02 +0100 | [diff] [blame] | 8509 | goto out_err; |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 8510 | } |
| 8511 | |
| 8512 | return rule; |
Willy Tarreau | 81499eb | 2012-12-27 12:19:02 +0100 | [diff] [blame] | 8513 | out_err: |
| 8514 | free(rule); |
| 8515 | return NULL; |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 8516 | } |
| 8517 | |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 8518 | /* parse an "http-respose" rule */ |
| 8519 | struct http_res_rule *parse_http_res_cond(const char **args, const char *file, int linenum, struct proxy *proxy) |
| 8520 | { |
| 8521 | struct http_res_rule *rule; |
| 8522 | int cur_arg; |
| 8523 | |
| 8524 | rule = calloc(1, sizeof(*rule)); |
| 8525 | if (!rule) { |
| 8526 | Alert("parsing [%s:%d]: out of memory.\n", file, linenum); |
| 8527 | goto out_err; |
| 8528 | } |
| 8529 | |
| 8530 | if (!strcmp(args[0], "allow")) { |
| 8531 | rule->action = HTTP_RES_ACT_ALLOW; |
| 8532 | cur_arg = 1; |
| 8533 | } else if (!strcmp(args[0], "deny")) { |
| 8534 | rule->action = HTTP_RES_ACT_DENY; |
| 8535 | cur_arg = 1; |
Willy Tarreau | f4c43c1 | 2013-06-11 17:01:13 +0200 | [diff] [blame] | 8536 | } else if (!strcmp(args[0], "set-nice")) { |
| 8537 | rule->action = HTTP_RES_ACT_SET_NICE; |
| 8538 | cur_arg = 1; |
| 8539 | |
| 8540 | if (!*args[cur_arg] || |
| 8541 | (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) { |
| 8542 | Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (integer value).\n", |
| 8543 | file, linenum, args[0]); |
| 8544 | goto out_err; |
| 8545 | } |
| 8546 | rule->arg.nice = atoi(args[cur_arg]); |
| 8547 | if (rule->arg.nice < -1024) |
| 8548 | rule->arg.nice = -1024; |
| 8549 | else if (rule->arg.nice > 1024) |
| 8550 | rule->arg.nice = 1024; |
| 8551 | cur_arg++; |
Willy Tarreau | 42cf39e | 2013-06-11 18:51:32 +0200 | [diff] [blame] | 8552 | } else if (!strcmp(args[0], "set-tos")) { |
| 8553 | #ifdef IP_TOS |
| 8554 | char *err; |
| 8555 | rule->action = HTTP_RES_ACT_SET_TOS; |
| 8556 | cur_arg = 1; |
| 8557 | |
| 8558 | if (!*args[cur_arg] || |
| 8559 | (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) { |
| 8560 | Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (integer/hex value).\n", |
| 8561 | file, linenum, args[0]); |
| 8562 | goto out_err; |
| 8563 | } |
| 8564 | |
| 8565 | rule->arg.tos = strtol(args[cur_arg], &err, 0); |
| 8566 | if (err && *err != '\0') { |
| 8567 | Alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-response %s' (integer/hex value expected).\n", |
| 8568 | file, linenum, err, args[0]); |
| 8569 | goto out_err; |
| 8570 | } |
| 8571 | cur_arg++; |
| 8572 | #else |
| 8573 | Alert("parsing [%s:%d]: 'http-response %s' is not supported on this platform (IP_TOS undefined).\n", file, linenum, args[0]); |
| 8574 | goto out_err; |
| 8575 | #endif |
Willy Tarreau | 51347ed | 2013-06-11 19:34:13 +0200 | [diff] [blame] | 8576 | } else if (!strcmp(args[0], "set-mark")) { |
| 8577 | #ifdef SO_MARK |
| 8578 | char *err; |
| 8579 | rule->action = HTTP_RES_ACT_SET_MARK; |
| 8580 | cur_arg = 1; |
| 8581 | |
| 8582 | if (!*args[cur_arg] || |
| 8583 | (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) { |
| 8584 | Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (integer/hex value).\n", |
| 8585 | file, linenum, args[0]); |
| 8586 | goto out_err; |
| 8587 | } |
| 8588 | |
| 8589 | rule->arg.mark = strtoul(args[cur_arg], &err, 0); |
| 8590 | if (err && *err != '\0') { |
| 8591 | Alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-response %s' (integer/hex value expected).\n", |
| 8592 | file, linenum, err, args[0]); |
| 8593 | goto out_err; |
| 8594 | } |
| 8595 | cur_arg++; |
| 8596 | global.last_checks |= LSTCHK_NETADM; |
| 8597 | #else |
| 8598 | Alert("parsing [%s:%d]: 'http-response %s' is not supported on this platform (SO_MARK undefined).\n", file, linenum, args[0]); |
| 8599 | goto out_err; |
| 8600 | #endif |
Willy Tarreau | 9a355ec | 2013-06-11 17:45:46 +0200 | [diff] [blame] | 8601 | } else if (!strcmp(args[0], "set-log-level")) { |
| 8602 | rule->action = HTTP_RES_ACT_SET_LOGL; |
| 8603 | cur_arg = 1; |
| 8604 | |
| 8605 | if (!*args[cur_arg] || |
| 8606 | (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) { |
| 8607 | bad_log_level: |
| 8608 | Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (log level name or 'silent').\n", |
| 8609 | file, linenum, args[0]); |
| 8610 | goto out_err; |
| 8611 | } |
| 8612 | if (strcmp(args[cur_arg], "silent") == 0) |
| 8613 | rule->arg.loglevel = -1; |
| 8614 | else if ((rule->arg.loglevel = get_log_level(args[cur_arg] + 1)) == 0) |
| 8615 | goto bad_log_level; |
| 8616 | cur_arg++; |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 8617 | } else if (strcmp(args[0], "add-header") == 0 || strcmp(args[0], "set-header") == 0) { |
| 8618 | rule->action = *args[0] == 'a' ? HTTP_RES_ACT_ADD_HDR : HTTP_RES_ACT_SET_HDR; |
| 8619 | cur_arg = 1; |
| 8620 | |
| 8621 | if (!*args[cur_arg] || !*args[cur_arg+1] || |
| 8622 | (*args[cur_arg+2] && strcmp(args[cur_arg+2], "if") != 0 && strcmp(args[cur_arg+2], "unless") != 0)) { |
| 8623 | Alert("parsing [%s:%d]: 'http-response %s' expects exactly 2 arguments.\n", |
| 8624 | file, linenum, args[0]); |
| 8625 | goto out_err; |
| 8626 | } |
| 8627 | |
| 8628 | rule->arg.hdr_add.name = strdup(args[cur_arg]); |
| 8629 | rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name); |
| 8630 | LIST_INIT(&rule->arg.hdr_add.fmt); |
| 8631 | |
Thierry FOURNIER | 1c0054f | 2013-11-20 15:09:52 +0100 | [diff] [blame] | 8632 | proxy->conf.args.ctx = ARGC_HRS; |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 8633 | parse_logformat_string(args[cur_arg + 1], proxy, &rule->arg.hdr_add.fmt, 0, |
| 8634 | (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR); |
Willy Tarreau | 59ad1a2 | 2014-01-29 14:39:58 +0100 | [diff] [blame] | 8635 | free(proxy->conf.lfs_file); |
| 8636 | proxy->conf.lfs_file = strdup(proxy->conf.args.file); |
| 8637 | proxy->conf.lfs_line = proxy->conf.args.line; |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 8638 | cur_arg += 2; |
| 8639 | } else { |
Willy Tarreau | 51347ed | 2013-06-11 19:34:13 +0200 | [diff] [blame] | 8640 | 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] | 8641 | file, linenum, args[0], *args[0] ? "" : " (missing argument)"); |
| 8642 | goto out_err; |
| 8643 | } |
| 8644 | |
| 8645 | if (strcmp(args[cur_arg], "if") == 0 || strcmp(args[cur_arg], "unless") == 0) { |
| 8646 | struct acl_cond *cond; |
| 8647 | char *errmsg = NULL; |
| 8648 | |
| 8649 | if ((cond = build_acl_cond(file, linenum, proxy, args+cur_arg, &errmsg)) == NULL) { |
| 8650 | Alert("parsing [%s:%d] : error detected while parsing an 'http-response %s' condition : %s.\n", |
| 8651 | file, linenum, args[0], errmsg); |
| 8652 | free(errmsg); |
| 8653 | goto out_err; |
| 8654 | } |
| 8655 | rule->cond = cond; |
| 8656 | } |
| 8657 | else if (*args[cur_arg]) { |
| 8658 | Alert("parsing [%s:%d]: 'http-response %s' expects" |
| 8659 | " either 'if' or 'unless' followed by a condition but found '%s'.\n", |
| 8660 | file, linenum, args[0], args[cur_arg]); |
| 8661 | goto out_err; |
| 8662 | } |
| 8663 | |
| 8664 | return rule; |
| 8665 | out_err: |
| 8666 | free(rule); |
| 8667 | return NULL; |
| 8668 | } |
| 8669 | |
Willy Tarreau | 4baae24 | 2012-12-27 12:00:31 +0100 | [diff] [blame] | 8670 | /* 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] | 8671 | * with <err> filled with the error message. If <use_fmt> is not null, builds a |
| 8672 | * dynamic log-format rule instead of a static string. |
Willy Tarreau | 4baae24 | 2012-12-27 12:00:31 +0100 | [diff] [blame] | 8673 | */ |
| 8674 | 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] | 8675 | const char **args, char **errmsg, int use_fmt) |
Willy Tarreau | 4baae24 | 2012-12-27 12:00:31 +0100 | [diff] [blame] | 8676 | { |
| 8677 | struct redirect_rule *rule; |
| 8678 | int cur_arg; |
| 8679 | int type = REDIRECT_TYPE_NONE; |
| 8680 | int code = 302; |
| 8681 | const char *destination = NULL; |
| 8682 | const char *cookie = NULL; |
| 8683 | int cookie_set = 0; |
| 8684 | unsigned int flags = REDIRECT_FLAG_NONE; |
| 8685 | struct acl_cond *cond = NULL; |
| 8686 | |
| 8687 | cur_arg = 0; |
| 8688 | while (*(args[cur_arg])) { |
| 8689 | if (strcmp(args[cur_arg], "location") == 0) { |
| 8690 | if (!*args[cur_arg + 1]) |
| 8691 | goto missing_arg; |
| 8692 | |
| 8693 | type = REDIRECT_TYPE_LOCATION; |
| 8694 | cur_arg++; |
| 8695 | destination = args[cur_arg]; |
| 8696 | } |
| 8697 | else if (strcmp(args[cur_arg], "prefix") == 0) { |
| 8698 | if (!*args[cur_arg + 1]) |
| 8699 | goto missing_arg; |
| 8700 | |
| 8701 | type = REDIRECT_TYPE_PREFIX; |
| 8702 | cur_arg++; |
| 8703 | destination = args[cur_arg]; |
| 8704 | } |
| 8705 | else if (strcmp(args[cur_arg], "scheme") == 0) { |
| 8706 | if (!*args[cur_arg + 1]) |
| 8707 | goto missing_arg; |
| 8708 | |
| 8709 | type = REDIRECT_TYPE_SCHEME; |
| 8710 | cur_arg++; |
| 8711 | destination = args[cur_arg]; |
| 8712 | } |
| 8713 | else if (strcmp(args[cur_arg], "set-cookie") == 0) { |
| 8714 | if (!*args[cur_arg + 1]) |
| 8715 | goto missing_arg; |
| 8716 | |
| 8717 | cur_arg++; |
| 8718 | cookie = args[cur_arg]; |
| 8719 | cookie_set = 1; |
| 8720 | } |
| 8721 | else if (strcmp(args[cur_arg], "clear-cookie") == 0) { |
| 8722 | if (!*args[cur_arg + 1]) |
| 8723 | goto missing_arg; |
| 8724 | |
| 8725 | cur_arg++; |
| 8726 | cookie = args[cur_arg]; |
| 8727 | cookie_set = 0; |
| 8728 | } |
| 8729 | else if (strcmp(args[cur_arg], "code") == 0) { |
| 8730 | if (!*args[cur_arg + 1]) |
| 8731 | goto missing_arg; |
| 8732 | |
| 8733 | cur_arg++; |
| 8734 | code = atol(args[cur_arg]); |
Yves Lafon | 3e8d1ae | 2013-03-11 11:06:05 -0400 | [diff] [blame] | 8735 | if (code < 301 || code > 308 || (code > 303 && code < 307)) { |
Willy Tarreau | 4baae24 | 2012-12-27 12:00:31 +0100 | [diff] [blame] | 8736 | memprintf(errmsg, |
Yves Lafon | 3e8d1ae | 2013-03-11 11:06:05 -0400 | [diff] [blame] | 8737 | "'%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] | 8738 | args[cur_arg - 1], args[cur_arg]); |
| 8739 | return NULL; |
| 8740 | } |
| 8741 | } |
| 8742 | else if (!strcmp(args[cur_arg],"drop-query")) { |
| 8743 | flags |= REDIRECT_FLAG_DROP_QS; |
| 8744 | } |
| 8745 | else if (!strcmp(args[cur_arg],"append-slash")) { |
| 8746 | flags |= REDIRECT_FLAG_APPEND_SLASH; |
| 8747 | } |
| 8748 | else if (strcmp(args[cur_arg], "if") == 0 || |
| 8749 | strcmp(args[cur_arg], "unless") == 0) { |
| 8750 | cond = build_acl_cond(file, linenum, curproxy, (const char **)args + cur_arg, errmsg); |
| 8751 | if (!cond) { |
| 8752 | memprintf(errmsg, "error in condition: %s", *errmsg); |
| 8753 | return NULL; |
| 8754 | } |
| 8755 | break; |
| 8756 | } |
| 8757 | else { |
| 8758 | memprintf(errmsg, |
| 8759 | "expects 'code', 'prefix', 'location', 'scheme', 'set-cookie', 'clear-cookie', 'drop-query' or 'append-slash' (was '%s')", |
| 8760 | args[cur_arg]); |
| 8761 | return NULL; |
| 8762 | } |
| 8763 | cur_arg++; |
| 8764 | } |
| 8765 | |
| 8766 | if (type == REDIRECT_TYPE_NONE) { |
| 8767 | memprintf(errmsg, "redirection type expected ('prefix', 'location', or 'scheme')"); |
| 8768 | return NULL; |
| 8769 | } |
| 8770 | |
| 8771 | rule = (struct redirect_rule *)calloc(1, sizeof(*rule)); |
| 8772 | rule->cond = cond; |
Willy Tarreau | 60e0838 | 2013-12-03 00:48:45 +0100 | [diff] [blame] | 8773 | LIST_INIT(&rule->rdr_fmt); |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 8774 | |
| 8775 | if (!use_fmt) { |
| 8776 | /* old-style static redirect rule */ |
| 8777 | rule->rdr_str = strdup(destination); |
| 8778 | rule->rdr_len = strlen(destination); |
| 8779 | } |
| 8780 | else { |
| 8781 | /* log-format based redirect rule */ |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 8782 | |
| 8783 | /* Parse destination. Note that in the REDIRECT_TYPE_PREFIX case, |
| 8784 | * if prefix == "/", we don't want to add anything, otherwise it |
| 8785 | * makes it hard for the user to configure a self-redirection. |
| 8786 | */ |
| 8787 | proxy->conf.args.ctx = ARGC_RDR; |
| 8788 | if (!(type == REDIRECT_TYPE_PREFIX && destination[0] == '/' && destination[1] == '\0')) { |
| 8789 | parse_logformat_string(destination, curproxy, &rule->rdr_fmt, 0, |
| 8790 | (curproxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR); |
Willy Tarreau | 59ad1a2 | 2014-01-29 14:39:58 +0100 | [diff] [blame] | 8791 | free(curproxy->conf.lfs_file); |
| 8792 | curproxy->conf.lfs_file = strdup(curproxy->conf.args.file); |
| 8793 | curproxy->conf.lfs_line = curproxy->conf.args.line; |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 8794 | } |
| 8795 | } |
| 8796 | |
Willy Tarreau | 4baae24 | 2012-12-27 12:00:31 +0100 | [diff] [blame] | 8797 | if (cookie) { |
| 8798 | /* depending on cookie_set, either we want to set the cookie, or to clear it. |
| 8799 | * a clear consists in appending "; path=/; Max-Age=0;" at the end. |
| 8800 | */ |
| 8801 | rule->cookie_len = strlen(cookie); |
| 8802 | if (cookie_set) { |
| 8803 | rule->cookie_str = malloc(rule->cookie_len + 10); |
| 8804 | memcpy(rule->cookie_str, cookie, rule->cookie_len); |
| 8805 | memcpy(rule->cookie_str + rule->cookie_len, "; path=/;", 10); |
| 8806 | rule->cookie_len += 9; |
| 8807 | } else { |
| 8808 | rule->cookie_str = malloc(rule->cookie_len + 21); |
| 8809 | memcpy(rule->cookie_str, cookie, rule->cookie_len); |
| 8810 | memcpy(rule->cookie_str + rule->cookie_len, "; path=/; Max-Age=0;", 21); |
| 8811 | rule->cookie_len += 20; |
| 8812 | } |
| 8813 | } |
| 8814 | rule->type = type; |
| 8815 | rule->code = code; |
| 8816 | rule->flags = flags; |
| 8817 | LIST_INIT(&rule->list); |
| 8818 | return rule; |
| 8819 | |
| 8820 | missing_arg: |
| 8821 | memprintf(errmsg, "missing argument for '%s'", args[cur_arg]); |
| 8822 | return NULL; |
| 8823 | } |
| 8824 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 8825 | /************************************************************************/ |
| 8826 | /* The code below is dedicated to ACL parsing and matching */ |
| 8827 | /************************************************************************/ |
| 8828 | |
| 8829 | |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 8830 | /* This function ensures that the prerequisites for an L7 fetch are ready, |
| 8831 | * which means that a request or response is ready. If some data is missing, |
| 8832 | * 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] | 8833 | * to extract data from L7. If <req_vol> is non-null during a request prefetch, |
| 8834 | * another test is made to ensure the required information is not gone. |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 8835 | * |
| 8836 | * The function returns : |
Willy Tarreau | 506d050 | 2013-07-06 13:29:24 +0200 | [diff] [blame] | 8837 | * 0 with SMP_F_MAY_CHANGE in the sample flags if some data is missing to |
| 8838 | * decide whether or not an HTTP message is present ; |
| 8839 | * 0 if the requested data cannot be fetched or if it is certain that |
| 8840 | * we'll never have any HTTP message there ; |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 8841 | * 1 if an HTTP message is ready |
| 8842 | */ |
| 8843 | static int |
Willy Tarreau | 506d050 | 2013-07-06 13:29:24 +0200 | [diff] [blame] | 8844 | 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] | 8845 | const struct arg *args, struct sample *smp, int req_vol) |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 8846 | { |
| 8847 | struct http_txn *txn = l7; |
| 8848 | struct http_msg *msg = &txn->req; |
| 8849 | |
| 8850 | /* Note: hdr_idx.v cannot be NULL in this ACL because the ACL is tagged |
| 8851 | * as a layer7 ACL, which involves automatic allocation of hdr_idx. |
| 8852 | */ |
| 8853 | |
| 8854 | if (unlikely(!s || !txn)) |
| 8855 | return 0; |
| 8856 | |
| 8857 | /* Check for a dependency on a request */ |
Willy Tarreau | f853c46 | 2012-04-23 18:53:56 +0200 | [diff] [blame] | 8858 | smp->type = SMP_T_BOOL; |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 8859 | |
Willy Tarreau | 32a6f2e | 2012-04-25 10:13:36 +0200 | [diff] [blame] | 8860 | if ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) { |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 8861 | if (unlikely(!s->req)) |
| 8862 | return 0; |
| 8863 | |
Willy Tarreau | aae75e3 | 2013-03-29 12:31:49 +0100 | [diff] [blame] | 8864 | /* If the buffer does not leave enough free space at the end, |
| 8865 | * we must first realign it. |
| 8866 | */ |
| 8867 | if (s->req->buf->p > s->req->buf->data && |
| 8868 | s->req->buf->i + s->req->buf->p > s->req->buf->data + s->req->buf->size - global.tune.maxrewrite) |
| 8869 | buffer_slow_realign(s->req->buf); |
| 8870 | |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 8871 | if (unlikely(txn->req.msg_state < HTTP_MSG_BODY)) { |
Willy Tarreau | 472b1ee | 2013-10-14 22:41:30 +0200 | [diff] [blame] | 8872 | if (msg->msg_state == HTTP_MSG_ERROR) |
Willy Tarreau | 506d050 | 2013-07-06 13:29:24 +0200 | [diff] [blame] | 8873 | return 0; |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 8874 | |
| 8875 | /* Try to decode HTTP request */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 8876 | if (likely(msg->next < s->req->buf->i)) |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 8877 | http_msg_analyzer(msg, &txn->hdr_idx); |
| 8878 | |
| 8879 | /* Still no valid request ? */ |
| 8880 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) { |
Willy Tarreau | 3bf1b2b | 2012-08-27 20:46:07 +0200 | [diff] [blame] | 8881 | if ((msg->msg_state == HTTP_MSG_ERROR) || |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 8882 | buffer_full(s->req->buf, global.tune.maxrewrite)) { |
Willy Tarreau | 506d050 | 2013-07-06 13:29:24 +0200 | [diff] [blame] | 8883 | return 0; |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 8884 | } |
| 8885 | /* wait for final state */ |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 8886 | smp->flags |= SMP_F_MAY_CHANGE; |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 8887 | return 0; |
| 8888 | } |
| 8889 | |
| 8890 | /* OK we just got a valid HTTP request. We have some minor |
| 8891 | * preparation to perform so that further checks can rely |
| 8892 | * on HTTP tests. |
| 8893 | */ |
Willy Tarreau | aae75e3 | 2013-03-29 12:31:49 +0100 | [diff] [blame] | 8894 | |
| 8895 | /* If the request was parsed but was too large, we must absolutely |
| 8896 | * return an error so that it is not processed. At the moment this |
| 8897 | * cannot happen, but if the parsers are to change in the future, |
| 8898 | * we want this check to be maintained. |
| 8899 | */ |
| 8900 | if (unlikely(s->req->buf->i + s->req->buf->p > |
| 8901 | s->req->buf->data + s->req->buf->size - global.tune.maxrewrite)) { |
| 8902 | msg->msg_state = HTTP_MSG_ERROR; |
Willy Tarreau | 506d050 | 2013-07-06 13:29:24 +0200 | [diff] [blame] | 8903 | smp->data.uint = 1; |
Willy Tarreau | aae75e3 | 2013-03-29 12:31:49 +0100 | [diff] [blame] | 8904 | return 1; |
| 8905 | } |
| 8906 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 8907 | 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] | 8908 | if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD) |
| 8909 | s->flags |= SN_REDIRECTABLE; |
| 8910 | |
Willy Tarreau | 506d050 | 2013-07-06 13:29:24 +0200 | [diff] [blame] | 8911 | if (unlikely(msg->sl.rq.v_l == 0) && !http_upgrade_v09_to_v10(txn)) |
| 8912 | return 0; |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 8913 | } |
| 8914 | |
Willy Tarreau | 506d050 | 2013-07-06 13:29:24 +0200 | [diff] [blame] | 8915 | if (req_vol && txn->rsp.msg_state != HTTP_MSG_RPBEFORE) { |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 8916 | return 0; /* data might have moved and indexes changed */ |
Willy Tarreau | 506d050 | 2013-07-06 13:29:24 +0200 | [diff] [blame] | 8917 | } |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 8918 | |
| 8919 | /* otherwise everything's ready for the request */ |
| 8920 | } |
Willy Tarreau | 24e32d8 | 2012-04-23 23:55:44 +0200 | [diff] [blame] | 8921 | else { |
| 8922 | /* Check for a dependency on a response */ |
Willy Tarreau | 506d050 | 2013-07-06 13:29:24 +0200 | [diff] [blame] | 8923 | if (txn->rsp.msg_state < HTTP_MSG_BODY) { |
| 8924 | smp->flags |= SMP_F_MAY_CHANGE; |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 8925 | return 0; |
Willy Tarreau | 506d050 | 2013-07-06 13:29:24 +0200 | [diff] [blame] | 8926 | } |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 8927 | } |
| 8928 | |
| 8929 | /* everything's OK */ |
Willy Tarreau | 506d050 | 2013-07-06 13:29:24 +0200 | [diff] [blame] | 8930 | smp->data.uint = 1; |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 8931 | return 1; |
| 8932 | } |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 8933 | |
Willy Tarreau | c0239e0 | 2012-04-16 14:42:55 +0200 | [diff] [blame] | 8934 | #define CHECK_HTTP_MESSAGE_FIRST() \ |
Willy Tarreau | 506d050 | 2013-07-06 13:29:24 +0200 | [diff] [blame] | 8935 | 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] | 8936 | |
Willy Tarreau | 24e32d8 | 2012-04-23 23:55:44 +0200 | [diff] [blame] | 8937 | #define CHECK_HTTP_MESSAGE_FIRST_PERM() \ |
Willy Tarreau | 506d050 | 2013-07-06 13:29:24 +0200 | [diff] [blame] | 8938 | 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] | 8939 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 8940 | |
| 8941 | /* 1. Check on METHOD |
| 8942 | * We use the pre-parsed method if it is known, and store its number as an |
| 8943 | * integer. If it is unknown, we use the pointer and the length. |
| 8944 | */ |
Thierry FOURNIER | 0b2fe4a | 2013-12-06 20:33:50 +0100 | [diff] [blame] | 8945 | static int pat_parse_meth(const char **text, struct pattern *pattern, enum pat_usage usage, int *opaque, char **err) |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 8946 | { |
| 8947 | int len, meth; |
Thierry FOURNIER | 0b2fe4a | 2013-12-06 20:33:50 +0100 | [diff] [blame] | 8948 | struct chunk *trash; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 8949 | |
Willy Tarreau | ae8b796 | 2007-06-09 23:10:04 +0200 | [diff] [blame] | 8950 | len = strlen(*text); |
| 8951 | meth = find_http_meth(*text, len); |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 8952 | |
| 8953 | pattern->val.i = meth; |
| 8954 | if (meth == HTTP_METH_OTHER) { |
Thierry FOURNIER | 0b2fe4a | 2013-12-06 20:33:50 +0100 | [diff] [blame] | 8955 | if (usage == PAT_U_COMPILE) { |
| 8956 | pattern->ptr.str = strdup(*text); |
| 8957 | if (!pattern->ptr.str) { |
| 8958 | memprintf(err, "out of memory while loading pattern"); |
| 8959 | return 0; |
| 8960 | } |
Willy Tarreau | 7dcb648 | 2012-04-27 17:52:25 +0200 | [diff] [blame] | 8961 | } |
Thierry FOURNIER | 0b2fe4a | 2013-12-06 20:33:50 +0100 | [diff] [blame] | 8962 | else { |
| 8963 | trash = get_trash_chunk(); |
| 8964 | if (trash->size < len) { |
| 8965 | memprintf(err, "no space avalaible in the buffer. expect %d, provides %d", |
| 8966 | len, trash->size); |
| 8967 | return 0; |
| 8968 | } |
| 8969 | pattern->ptr.str = trash->str; |
| 8970 | } |
| 8971 | pattern->expect_type = SMP_T_CSTR; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 8972 | pattern->len = len; |
| 8973 | } |
Thierry FOURNIER | cc0e0b3 | 2013-12-06 16:56:40 +0100 | [diff] [blame] | 8974 | else |
| 8975 | pattern->expect_type = SMP_T_UINT; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 8976 | return 1; |
| 8977 | } |
| 8978 | |
Willy Tarreau | 8e5e955 | 2011-12-16 15:38:49 +0100 | [diff] [blame] | 8979 | /* This function fetches the method of current HTTP request and stores |
| 8980 | * it in the global pattern struct as a chunk. There are two possibilities : |
| 8981 | * - if the method is known (not HTTP_METH_OTHER), its identifier is stored |
| 8982 | * in <len> and <ptr> is NULL ; |
| 8983 | * - if the method is unknown (HTTP_METH_OTHER), <ptr> points to the text and |
| 8984 | * <len> to its length. |
Thierry FOURNIER | a65b343 | 2013-11-28 18:22:00 +0100 | [diff] [blame] | 8985 | * This is intended to be used with pat_match_meth() only. |
Willy Tarreau | 8e5e955 | 2011-12-16 15:38:49 +0100 | [diff] [blame] | 8986 | */ |
Willy Tarreau | d41f8d8 | 2007-06-10 10:06:18 +0200 | [diff] [blame] | 8987 | static int |
Willy Tarreau | 409bcde | 2013-01-08 00:31:00 +0100 | [diff] [blame] | 8988 | 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] | 8989 | const struct arg *args, struct sample *smp, const char *kw) |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 8990 | { |
| 8991 | int meth; |
| 8992 | struct http_txn *txn = l7; |
| 8993 | |
Willy Tarreau | 24e32d8 | 2012-04-23 23:55:44 +0200 | [diff] [blame] | 8994 | CHECK_HTTP_MESSAGE_FIRST_PERM(); |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 8995 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 8996 | meth = txn->meth; |
Willy Tarreau | f853c46 | 2012-04-23 18:53:56 +0200 | [diff] [blame] | 8997 | smp->type = SMP_T_UINT; |
| 8998 | smp->data.uint = meth; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 8999 | if (meth == HTTP_METH_OTHER) { |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 9000 | if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE) |
| 9001 | /* ensure the indexes are not affected */ |
| 9002 | return 0; |
Willy Tarreau | f853c46 | 2012-04-23 18:53:56 +0200 | [diff] [blame] | 9003 | smp->type = SMP_T_CSTR; |
| 9004 | smp->data.str.len = txn->req.sl.rq.m_l; |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 9005 | smp->data.str.str = txn->req.chn->buf->p; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 9006 | } |
Willy Tarreau | 21e5b0e | 2012-04-23 19:25:44 +0200 | [diff] [blame] | 9007 | smp->flags = SMP_F_VOL_1ST; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 9008 | return 1; |
| 9009 | } |
| 9010 | |
Willy Tarreau | 8e5e955 | 2011-12-16 15:38:49 +0100 | [diff] [blame] | 9011 | /* See above how the method is stored in the global pattern */ |
Willy Tarreau | 0cba607 | 2013-11-28 22:21:02 +0100 | [diff] [blame] | 9012 | static enum pat_match_res pat_match_meth(struct sample *smp, struct pattern *pattern) |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 9013 | { |
Willy Tarreau | c8d7c96 | 2007-06-17 08:20:33 +0200 | [diff] [blame] | 9014 | int icase; |
| 9015 | |
Willy Tarreau | 8e5e955 | 2011-12-16 15:38:49 +0100 | [diff] [blame] | 9016 | |
Willy Tarreau | f853c46 | 2012-04-23 18:53:56 +0200 | [diff] [blame] | 9017 | if (smp->type == SMP_T_UINT) { |
Willy Tarreau | 8e5e955 | 2011-12-16 15:38:49 +0100 | [diff] [blame] | 9018 | /* well-known method */ |
Willy Tarreau | f853c46 | 2012-04-23 18:53:56 +0200 | [diff] [blame] | 9019 | if (smp->data.uint == pattern->val.i) |
Thierry FOURNIER | a65b343 | 2013-11-28 18:22:00 +0100 | [diff] [blame] | 9020 | return PAT_MATCH; |
| 9021 | return PAT_NOMATCH; |
Willy Tarreau | 8e5e955 | 2011-12-16 15:38:49 +0100 | [diff] [blame] | 9022 | } |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 9023 | |
Willy Tarreau | 8e5e955 | 2011-12-16 15:38:49 +0100 | [diff] [blame] | 9024 | /* Uncommon method, only HTTP_METH_OTHER is accepted now */ |
| 9025 | if (pattern->val.i != HTTP_METH_OTHER) |
Thierry FOURNIER | a65b343 | 2013-11-28 18:22:00 +0100 | [diff] [blame] | 9026 | return PAT_NOMATCH; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 9027 | |
| 9028 | /* Other method, we must compare the strings */ |
Willy Tarreau | f853c46 | 2012-04-23 18:53:56 +0200 | [diff] [blame] | 9029 | if (pattern->len != smp->data.str.len) |
Thierry FOURNIER | a65b343 | 2013-11-28 18:22:00 +0100 | [diff] [blame] | 9030 | return PAT_NOMATCH; |
Willy Tarreau | c8d7c96 | 2007-06-17 08:20:33 +0200 | [diff] [blame] | 9031 | |
Thierry FOURNIER | a65b343 | 2013-11-28 18:22:00 +0100 | [diff] [blame] | 9032 | icase = pattern->flags & PAT_F_IGNORE_CASE; |
Willy Tarreau | f853c46 | 2012-04-23 18:53:56 +0200 | [diff] [blame] | 9033 | if ((icase && strncasecmp(pattern->ptr.str, smp->data.str.str, smp->data.str.len) != 0) || |
| 9034 | (!icase && strncmp(pattern->ptr.str, smp->data.str.str, smp->data.str.len) != 0)) |
Thierry FOURNIER | a65b343 | 2013-11-28 18:22:00 +0100 | [diff] [blame] | 9035 | return PAT_NOMATCH; |
| 9036 | return PAT_MATCH; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 9037 | } |
| 9038 | |
Willy Tarreau | d41f8d8 | 2007-06-10 10:06:18 +0200 | [diff] [blame] | 9039 | static int |
Willy Tarreau | 409bcde | 2013-01-08 00:31:00 +0100 | [diff] [blame] | 9040 | 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] | 9041 | const struct arg *args, struct sample *smp, const char *kw) |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 9042 | { |
| 9043 | struct http_txn *txn = l7; |
| 9044 | char *ptr; |
| 9045 | int len; |
| 9046 | |
Willy Tarreau | c0239e0 | 2012-04-16 14:42:55 +0200 | [diff] [blame] | 9047 | CHECK_HTTP_MESSAGE_FIRST(); |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 9048 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 9049 | len = txn->req.sl.rq.v_l; |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 9050 | ptr = txn->req.chn->buf->p + txn->req.sl.rq.v; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 9051 | |
| 9052 | while ((len-- > 0) && (*ptr++ != '/')); |
| 9053 | if (len <= 0) |
| 9054 | return 0; |
| 9055 | |
Willy Tarreau | f853c46 | 2012-04-23 18:53:56 +0200 | [diff] [blame] | 9056 | smp->type = SMP_T_CSTR; |
| 9057 | smp->data.str.str = ptr; |
| 9058 | smp->data.str.len = len; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 9059 | |
Willy Tarreau | 21e5b0e | 2012-04-23 19:25:44 +0200 | [diff] [blame] | 9060 | smp->flags = SMP_F_VOL_1ST; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 9061 | return 1; |
| 9062 | } |
| 9063 | |
Willy Tarreau | d41f8d8 | 2007-06-10 10:06:18 +0200 | [diff] [blame] | 9064 | static int |
Willy Tarreau | 409bcde | 2013-01-08 00:31:00 +0100 | [diff] [blame] | 9065 | 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] | 9066 | const struct arg *args, struct sample *smp, const char *kw) |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 9067 | { |
| 9068 | struct http_txn *txn = l7; |
| 9069 | char *ptr; |
| 9070 | int len; |
| 9071 | |
Willy Tarreau | c0239e0 | 2012-04-16 14:42:55 +0200 | [diff] [blame] | 9072 | CHECK_HTTP_MESSAGE_FIRST(); |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 9073 | |
Willy Tarreau | f26b252 | 2012-12-14 08:33:14 +0100 | [diff] [blame] | 9074 | if (txn->rsp.msg_state < HTTP_MSG_BODY) |
| 9075 | return 0; |
| 9076 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 9077 | len = txn->rsp.sl.st.v_l; |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 9078 | ptr = txn->rsp.chn->buf->p; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 9079 | |
| 9080 | while ((len-- > 0) && (*ptr++ != '/')); |
| 9081 | if (len <= 0) |
| 9082 | return 0; |
| 9083 | |
Willy Tarreau | f853c46 | 2012-04-23 18:53:56 +0200 | [diff] [blame] | 9084 | smp->type = SMP_T_CSTR; |
| 9085 | smp->data.str.str = ptr; |
| 9086 | smp->data.str.len = len; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 9087 | |
Willy Tarreau | 21e5b0e | 2012-04-23 19:25:44 +0200 | [diff] [blame] | 9088 | smp->flags = SMP_F_VOL_1ST; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 9089 | return 1; |
| 9090 | } |
| 9091 | |
| 9092 | /* 3. Check on Status Code. We manipulate integers here. */ |
Willy Tarreau | d41f8d8 | 2007-06-10 10:06:18 +0200 | [diff] [blame] | 9093 | static int |
Willy Tarreau | 409bcde | 2013-01-08 00:31:00 +0100 | [diff] [blame] | 9094 | 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] | 9095 | const struct arg *args, struct sample *smp, const char *kw) |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 9096 | { |
| 9097 | struct http_txn *txn = l7; |
| 9098 | char *ptr; |
| 9099 | int len; |
| 9100 | |
Willy Tarreau | c0239e0 | 2012-04-16 14:42:55 +0200 | [diff] [blame] | 9101 | CHECK_HTTP_MESSAGE_FIRST(); |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 9102 | |
Willy Tarreau | f26b252 | 2012-12-14 08:33:14 +0100 | [diff] [blame] | 9103 | if (txn->rsp.msg_state < HTTP_MSG_BODY) |
| 9104 | return 0; |
| 9105 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 9106 | len = txn->rsp.sl.st.c_l; |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 9107 | ptr = txn->rsp.chn->buf->p + txn->rsp.sl.st.c; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 9108 | |
Willy Tarreau | f853c46 | 2012-04-23 18:53:56 +0200 | [diff] [blame] | 9109 | smp->type = SMP_T_UINT; |
| 9110 | smp->data.uint = __strl2ui(ptr, len); |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 9111 | smp->flags = SMP_F_VOL_1ST; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 9112 | return 1; |
| 9113 | } |
| 9114 | |
| 9115 | /* 4. Check on URL/URI. A pointer to the URI is stored. */ |
Willy Tarreau | d41f8d8 | 2007-06-10 10:06:18 +0200 | [diff] [blame] | 9116 | static int |
Willy Tarreau | 6812bcf | 2012-04-29 09:28:50 +0200 | [diff] [blame] | 9117 | 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] | 9118 | const struct arg *args, struct sample *smp, const char *kw) |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 9119 | { |
| 9120 | struct http_txn *txn = l7; |
| 9121 | |
Willy Tarreau | c0239e0 | 2012-04-16 14:42:55 +0200 | [diff] [blame] | 9122 | CHECK_HTTP_MESSAGE_FIRST(); |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 9123 | |
Willy Tarreau | f853c46 | 2012-04-23 18:53:56 +0200 | [diff] [blame] | 9124 | smp->type = SMP_T_CSTR; |
| 9125 | smp->data.str.len = txn->req.sl.rq.u_l; |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 9126 | smp->data.str.str = txn->req.chn->buf->p + txn->req.sl.rq.u; |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 9127 | smp->flags = SMP_F_VOL_1ST; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 9128 | return 1; |
| 9129 | } |
| 9130 | |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 9131 | static int |
Willy Tarreau | 6812bcf | 2012-04-29 09:28:50 +0200 | [diff] [blame] | 9132 | 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] | 9133 | const struct arg *args, struct sample *smp, const char *kw) |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 9134 | { |
| 9135 | struct http_txn *txn = l7; |
Willy Tarreau | 4c804ec | 2013-09-30 14:37:14 +0200 | [diff] [blame] | 9136 | struct sockaddr_storage addr; |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 9137 | |
Willy Tarreau | c0239e0 | 2012-04-16 14:42:55 +0200 | [diff] [blame] | 9138 | CHECK_HTTP_MESSAGE_FIRST(); |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 9139 | |
Willy Tarreau | 4c804ec | 2013-09-30 14:37:14 +0200 | [diff] [blame] | 9140 | url2sa(txn->req.chn->buf->p + txn->req.sl.rq.u, txn->req.sl.rq.u_l, &addr); |
| 9141 | if (((struct sockaddr_in *)&addr)->sin_family != AF_INET) |
Willy Tarreau | f4362b3 | 2011-12-16 17:49:52 +0100 | [diff] [blame] | 9142 | return 0; |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 9143 | |
Willy Tarreau | 4c804ec | 2013-09-30 14:37:14 +0200 | [diff] [blame] | 9144 | smp->type = SMP_T_IPV4; |
| 9145 | smp->data.ipv4 = ((struct sockaddr_in *)&addr)->sin_addr; |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 9146 | smp->flags = 0; |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 9147 | return 1; |
| 9148 | } |
| 9149 | |
| 9150 | static int |
Willy Tarreau | 6812bcf | 2012-04-29 09:28:50 +0200 | [diff] [blame] | 9151 | 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] | 9152 | const struct arg *args, struct sample *smp, const char *kw) |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 9153 | { |
| 9154 | struct http_txn *txn = l7; |
Willy Tarreau | 4c804ec | 2013-09-30 14:37:14 +0200 | [diff] [blame] | 9155 | struct sockaddr_storage addr; |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 9156 | |
Willy Tarreau | c0239e0 | 2012-04-16 14:42:55 +0200 | [diff] [blame] | 9157 | CHECK_HTTP_MESSAGE_FIRST(); |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 9158 | |
Willy Tarreau | 4c804ec | 2013-09-30 14:37:14 +0200 | [diff] [blame] | 9159 | url2sa(txn->req.chn->buf->p + txn->req.sl.rq.u, txn->req.sl.rq.u_l, &addr); |
| 9160 | if (((struct sockaddr_in *)&addr)->sin_family != AF_INET) |
| 9161 | return 0; |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 9162 | |
Willy Tarreau | 4c804ec | 2013-09-30 14:37:14 +0200 | [diff] [blame] | 9163 | smp->type = SMP_T_UINT; |
| 9164 | smp->data.uint = ntohs(((struct sockaddr_in *)&addr)->sin_port); |
Willy Tarreau | 21e5b0e | 2012-04-23 19:25:44 +0200 | [diff] [blame] | 9165 | smp->flags = 0; |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 9166 | return 1; |
| 9167 | } |
| 9168 | |
Willy Tarreau | 185b5c4 | 2012-04-26 15:11:51 +0200 | [diff] [blame] | 9169 | /* Fetch an HTTP header. A pointer to the beginning of the value is returned. |
| 9170 | * Accepts an optional argument of type string containing the header field name, |
| 9171 | * and an optional argument of type signed or unsigned integer to request an |
| 9172 | * 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] | 9173 | * headers are considered from the first one. It does not stop on commas and |
| 9174 | * returns full lines instead (useful for User-Agent or Date for example). |
| 9175 | */ |
| 9176 | static int |
| 9177 | 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] | 9178 | const struct arg *args, struct sample *smp, const char *kw) |
Willy Tarreau | 04ff9f1 | 2013-06-10 18:39:42 +0200 | [diff] [blame] | 9179 | { |
| 9180 | struct http_txn *txn = l7; |
| 9181 | struct hdr_idx *idx = &txn->hdr_idx; |
| 9182 | struct hdr_ctx *ctx = smp->ctx.a[0]; |
| 9183 | const struct http_msg *msg = ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) ? &txn->req : &txn->rsp; |
| 9184 | int occ = 0; |
| 9185 | const char *name_str = NULL; |
| 9186 | int name_len = 0; |
| 9187 | |
| 9188 | if (!ctx) { |
| 9189 | /* first call */ |
| 9190 | ctx = &static_hdr_ctx; |
| 9191 | ctx->idx = 0; |
| 9192 | smp->ctx.a[0] = ctx; |
| 9193 | } |
| 9194 | |
| 9195 | if (args) { |
| 9196 | if (args[0].type != ARGT_STR) |
| 9197 | return 0; |
| 9198 | name_str = args[0].data.str.str; |
| 9199 | name_len = args[0].data.str.len; |
| 9200 | |
| 9201 | if (args[1].type == ARGT_UINT || args[1].type == ARGT_SINT) |
| 9202 | occ = args[1].data.uint; |
| 9203 | } |
| 9204 | |
| 9205 | CHECK_HTTP_MESSAGE_FIRST(); |
| 9206 | |
| 9207 | if (ctx && !(smp->flags & SMP_F_NOT_LAST)) |
| 9208 | /* search for header from the beginning */ |
| 9209 | ctx->idx = 0; |
| 9210 | |
| 9211 | if (!occ && !(opt & SMP_OPT_ITERATE)) |
| 9212 | /* no explicit occurrence and single fetch => last header by default */ |
| 9213 | occ = -1; |
| 9214 | |
| 9215 | if (!occ) |
| 9216 | /* prepare to report multiple occurrences for ACL fetches */ |
| 9217 | smp->flags |= SMP_F_NOT_LAST; |
| 9218 | |
| 9219 | smp->type = SMP_T_CSTR; |
| 9220 | smp->flags |= SMP_F_VOL_HDR; |
| 9221 | if (http_get_fhdr(msg, name_str, name_len, idx, occ, ctx, &smp->data.str.str, &smp->data.str.len)) |
| 9222 | return 1; |
| 9223 | |
| 9224 | smp->flags &= ~SMP_F_NOT_LAST; |
| 9225 | return 0; |
| 9226 | } |
| 9227 | |
| 9228 | /* 6. Check on HTTP header count. The number of occurrences is returned. |
| 9229 | * Accepts exactly 1 argument of type string. It does not stop on commas and |
| 9230 | * returns full lines instead (useful for User-Agent or Date for example). |
| 9231 | */ |
| 9232 | static int |
| 9233 | 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] | 9234 | const struct arg *args, struct sample *smp, const char *kw) |
Willy Tarreau | 04ff9f1 | 2013-06-10 18:39:42 +0200 | [diff] [blame] | 9235 | { |
| 9236 | struct http_txn *txn = l7; |
| 9237 | struct hdr_idx *idx = &txn->hdr_idx; |
| 9238 | struct hdr_ctx ctx; |
| 9239 | const struct http_msg *msg = ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) ? &txn->req : &txn->rsp; |
| 9240 | int cnt; |
| 9241 | |
| 9242 | if (!args || args->type != ARGT_STR) |
| 9243 | return 0; |
| 9244 | |
| 9245 | CHECK_HTTP_MESSAGE_FIRST(); |
| 9246 | |
| 9247 | ctx.idx = 0; |
| 9248 | cnt = 0; |
| 9249 | while (http_find_full_header2(args->data.str.str, args->data.str.len, msg->chn->buf->p, idx, &ctx)) |
| 9250 | cnt++; |
| 9251 | |
| 9252 | smp->type = SMP_T_UINT; |
| 9253 | smp->data.uint = cnt; |
| 9254 | smp->flags = SMP_F_VOL_HDR; |
| 9255 | return 1; |
| 9256 | } |
| 9257 | |
| 9258 | /* Fetch an HTTP header. A pointer to the beginning of the value is returned. |
| 9259 | * Accepts an optional argument of type string containing the header field name, |
| 9260 | * and an optional argument of type signed or unsigned integer to request an |
| 9261 | * 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] | 9262 | * headers are considered from the first one. |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 9263 | */ |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 9264 | static int |
Willy Tarreau | 185b5c4 | 2012-04-26 15:11:51 +0200 | [diff] [blame] | 9265 | 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] | 9266 | const struct arg *args, struct sample *smp, const char *kw) |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 9267 | { |
| 9268 | struct http_txn *txn = l7; |
| 9269 | struct hdr_idx *idx = &txn->hdr_idx; |
Willy Tarreau | a890d07 | 2013-04-02 12:01:06 +0200 | [diff] [blame] | 9270 | struct hdr_ctx *ctx = smp->ctx.a[0]; |
Willy Tarreau | 32a6f2e | 2012-04-25 10:13:36 +0200 | [diff] [blame] | 9271 | 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] | 9272 | int occ = 0; |
| 9273 | const char *name_str = NULL; |
| 9274 | int name_len = 0; |
Willy Tarreau | e333ec9 | 2012-04-16 16:26:40 +0200 | [diff] [blame] | 9275 | |
Willy Tarreau | a890d07 | 2013-04-02 12:01:06 +0200 | [diff] [blame] | 9276 | if (!ctx) { |
| 9277 | /* first call */ |
| 9278 | ctx = &static_hdr_ctx; |
| 9279 | ctx->idx = 0; |
Willy Tarreau | ffb6f08 | 2013-04-02 23:16:53 +0200 | [diff] [blame] | 9280 | smp->ctx.a[0] = ctx; |
Willy Tarreau | a890d07 | 2013-04-02 12:01:06 +0200 | [diff] [blame] | 9281 | } |
| 9282 | |
Willy Tarreau | 185b5c4 | 2012-04-26 15:11:51 +0200 | [diff] [blame] | 9283 | if (args) { |
| 9284 | if (args[0].type != ARGT_STR) |
| 9285 | return 0; |
| 9286 | name_str = args[0].data.str.str; |
| 9287 | name_len = args[0].data.str.len; |
| 9288 | |
| 9289 | if (args[1].type == ARGT_UINT || args[1].type == ARGT_SINT) |
| 9290 | occ = args[1].data.uint; |
| 9291 | } |
Willy Tarreau | 34db108 | 2012-04-19 17:16:54 +0200 | [diff] [blame] | 9292 | |
Willy Tarreau | e333ec9 | 2012-04-16 16:26:40 +0200 | [diff] [blame] | 9293 | CHECK_HTTP_MESSAGE_FIRST(); |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 9294 | |
Willy Tarreau | 185b5c4 | 2012-04-26 15:11:51 +0200 | [diff] [blame] | 9295 | if (ctx && !(smp->flags & SMP_F_NOT_LAST)) |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 9296 | /* search for header from the beginning */ |
| 9297 | ctx->idx = 0; |
| 9298 | |
Willy Tarreau | 185b5c4 | 2012-04-26 15:11:51 +0200 | [diff] [blame] | 9299 | if (!occ && !(opt & SMP_OPT_ITERATE)) |
| 9300 | /* no explicit occurrence and single fetch => last header by default */ |
| 9301 | occ = -1; |
| 9302 | |
| 9303 | if (!occ) |
| 9304 | /* prepare to report multiple occurrences for ACL fetches */ |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 9305 | smp->flags |= SMP_F_NOT_LAST; |
Willy Tarreau | 664092c | 2011-12-16 19:11:42 +0100 | [diff] [blame] | 9306 | |
Willy Tarreau | 185b5c4 | 2012-04-26 15:11:51 +0200 | [diff] [blame] | 9307 | smp->type = SMP_T_CSTR; |
| 9308 | smp->flags |= SMP_F_VOL_HDR; |
| 9309 | 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] | 9310 | return 1; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 9311 | |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 9312 | smp->flags &= ~SMP_F_NOT_LAST; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 9313 | return 0; |
| 9314 | } |
| 9315 | |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 9316 | /* 6. Check on HTTP header count. The number of occurrences is returned. |
Willy Tarreau | 34db108 | 2012-04-19 17:16:54 +0200 | [diff] [blame] | 9317 | * Accepts exactly 1 argument of type string. |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 9318 | */ |
| 9319 | static int |
Willy Tarreau | 185b5c4 | 2012-04-26 15:11:51 +0200 | [diff] [blame] | 9320 | 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] | 9321 | const struct arg *args, struct sample *smp, const char *kw) |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 9322 | { |
| 9323 | struct http_txn *txn = l7; |
| 9324 | struct hdr_idx *idx = &txn->hdr_idx; |
| 9325 | struct hdr_ctx ctx; |
Willy Tarreau | 32a6f2e | 2012-04-25 10:13:36 +0200 | [diff] [blame] | 9326 | 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] | 9327 | int cnt; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 9328 | |
Willy Tarreau | 24e32d8 | 2012-04-23 23:55:44 +0200 | [diff] [blame] | 9329 | if (!args || args->type != ARGT_STR) |
Willy Tarreau | 34db108 | 2012-04-19 17:16:54 +0200 | [diff] [blame] | 9330 | return 0; |
| 9331 | |
Willy Tarreau | e333ec9 | 2012-04-16 16:26:40 +0200 | [diff] [blame] | 9332 | CHECK_HTTP_MESSAGE_FIRST(); |
| 9333 | |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 9334 | ctx.idx = 0; |
| 9335 | cnt = 0; |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 9336 | 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] | 9337 | cnt++; |
| 9338 | |
Willy Tarreau | f853c46 | 2012-04-23 18:53:56 +0200 | [diff] [blame] | 9339 | smp->type = SMP_T_UINT; |
| 9340 | smp->data.uint = cnt; |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 9341 | smp->flags = SMP_F_VOL_HDR; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 9342 | return 1; |
| 9343 | } |
| 9344 | |
Willy Tarreau | 185b5c4 | 2012-04-26 15:11:51 +0200 | [diff] [blame] | 9345 | /* Fetch an HTTP header's integer value. The integer value is returned. It |
| 9346 | * takes a mandatory argument of type string and an optional one of type int |
| 9347 | * to designate a specific occurrence. It returns an unsigned integer, which |
| 9348 | * may or may not be appropriate for everything. |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 9349 | */ |
| 9350 | static int |
Willy Tarreau | 185b5c4 | 2012-04-26 15:11:51 +0200 | [diff] [blame] | 9351 | 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] | 9352 | const struct arg *args, struct sample *smp, const char *kw) |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 9353 | { |
Willy Tarreau | ef38c39 | 2013-07-22 16:29:32 +0200 | [diff] [blame] | 9354 | int ret = smp_fetch_hdr(px, l4, l7, opt, args, smp, kw); |
Willy Tarreau | e333ec9 | 2012-04-16 16:26:40 +0200 | [diff] [blame] | 9355 | |
Willy Tarreau | f853c46 | 2012-04-23 18:53:56 +0200 | [diff] [blame] | 9356 | if (ret > 0) { |
| 9357 | smp->type = SMP_T_UINT; |
| 9358 | smp->data.uint = strl2ic(smp->data.str.str, smp->data.str.len); |
| 9359 | } |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 9360 | |
Willy Tarreau | d53e242 | 2012-04-16 17:21:11 +0200 | [diff] [blame] | 9361 | return ret; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 9362 | } |
| 9363 | |
Cyril Bonté | 69fa992 | 2012-10-25 00:01:06 +0200 | [diff] [blame] | 9364 | /* Fetch an HTTP header's IP value. takes a mandatory argument of type string |
| 9365 | * and an optional one of type int to designate a specific occurrence. |
| 9366 | * It returns an IPv4 or IPv6 address. |
Willy Tarreau | 106f979 | 2009-09-19 07:54:16 +0200 | [diff] [blame] | 9367 | */ |
| 9368 | static int |
Willy Tarreau | 185b5c4 | 2012-04-26 15:11:51 +0200 | [diff] [blame] | 9369 | 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] | 9370 | const struct arg *args, struct sample *smp, const char *kw) |
Willy Tarreau | 106f979 | 2009-09-19 07:54:16 +0200 | [diff] [blame] | 9371 | { |
Willy Tarreau | d53e242 | 2012-04-16 17:21:11 +0200 | [diff] [blame] | 9372 | int ret; |
Willy Tarreau | e333ec9 | 2012-04-16 16:26:40 +0200 | [diff] [blame] | 9373 | |
Willy Tarreau | ef38c39 | 2013-07-22 16:29:32 +0200 | [diff] [blame] | 9374 | 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] | 9375 | if (url2ipv4((char *)smp->data.str.str, &smp->data.ipv4)) { |
| 9376 | smp->type = SMP_T_IPV4; |
Willy Tarreau | d53e242 | 2012-04-16 17:21:11 +0200 | [diff] [blame] | 9377 | break; |
Cyril Bonté | 69fa992 | 2012-10-25 00:01:06 +0200 | [diff] [blame] | 9378 | } else { |
Willy Tarreau | 47ca545 | 2012-12-23 20:22:19 +0100 | [diff] [blame] | 9379 | struct chunk *temp = get_trash_chunk(); |
Cyril Bonté | 69fa992 | 2012-10-25 00:01:06 +0200 | [diff] [blame] | 9380 | if (smp->data.str.len < temp->size - 1) { |
| 9381 | memcpy(temp->str, smp->data.str.str, smp->data.str.len); |
| 9382 | temp->str[smp->data.str.len] = '\0'; |
| 9383 | if (inet_pton(AF_INET6, temp->str, &smp->data.ipv6)) { |
| 9384 | smp->type = SMP_T_IPV6; |
| 9385 | break; |
| 9386 | } |
| 9387 | } |
| 9388 | } |
| 9389 | |
Willy Tarreau | d53e242 | 2012-04-16 17:21:11 +0200 | [diff] [blame] | 9390 | /* if the header doesn't match an IP address, fetch next one */ |
Willy Tarreau | 185b5c4 | 2012-04-26 15:11:51 +0200 | [diff] [blame] | 9391 | if (!(smp->flags & SMP_F_NOT_LAST)) |
| 9392 | return 0; |
Willy Tarreau | 106f979 | 2009-09-19 07:54:16 +0200 | [diff] [blame] | 9393 | } |
Willy Tarreau | d53e242 | 2012-04-16 17:21:11 +0200 | [diff] [blame] | 9394 | return ret; |
Willy Tarreau | 106f979 | 2009-09-19 07:54:16 +0200 | [diff] [blame] | 9395 | } |
| 9396 | |
Willy Tarreau | 737b0c1 | 2007-06-10 21:28:46 +0200 | [diff] [blame] | 9397 | /* 8. Check on URI PATH. A pointer to the PATH is stored. The path starts at |
| 9398 | * the first '/' after the possible hostname, and ends before the possible '?'. |
| 9399 | */ |
| 9400 | static int |
Willy Tarreau | 6812bcf | 2012-04-29 09:28:50 +0200 | [diff] [blame] | 9401 | 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] | 9402 | const struct arg *args, struct sample *smp, const char *kw) |
Willy Tarreau | 737b0c1 | 2007-06-10 21:28:46 +0200 | [diff] [blame] | 9403 | { |
| 9404 | struct http_txn *txn = l7; |
| 9405 | char *ptr, *end; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 9406 | |
Willy Tarreau | c0239e0 | 2012-04-16 14:42:55 +0200 | [diff] [blame] | 9407 | CHECK_HTTP_MESSAGE_FIRST(); |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 9408 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 9409 | 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] | 9410 | ptr = http_get_path(txn); |
| 9411 | if (!ptr) |
Willy Tarreau | 737b0c1 | 2007-06-10 21:28:46 +0200 | [diff] [blame] | 9412 | return 0; |
| 9413 | |
| 9414 | /* OK, we got the '/' ! */ |
Willy Tarreau | f853c46 | 2012-04-23 18:53:56 +0200 | [diff] [blame] | 9415 | smp->type = SMP_T_CSTR; |
| 9416 | smp->data.str.str = ptr; |
Willy Tarreau | 737b0c1 | 2007-06-10 21:28:46 +0200 | [diff] [blame] | 9417 | |
| 9418 | while (ptr < end && *ptr != '?') |
| 9419 | ptr++; |
| 9420 | |
Willy Tarreau | f853c46 | 2012-04-23 18:53:56 +0200 | [diff] [blame] | 9421 | smp->data.str.len = ptr - smp->data.str.str; |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 9422 | smp->flags = SMP_F_VOL_1ST; |
Willy Tarreau | 737b0c1 | 2007-06-10 21:28:46 +0200 | [diff] [blame] | 9423 | return 1; |
| 9424 | } |
| 9425 | |
Willy Tarreau | a7ad50c | 2012-04-29 15:39:40 +0200 | [diff] [blame] | 9426 | /* This produces a concatenation of the first occurrence of the Host header |
| 9427 | * followed by the path component if it begins with a slash ('/'). This means |
| 9428 | * that '*' will not be added, resulting in exactly the first Host entry. |
| 9429 | * If no Host header is found, then the path is returned as-is. The returned |
| 9430 | * 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] | 9431 | * The returned sample is of type string. |
Willy Tarreau | a7ad50c | 2012-04-29 15:39:40 +0200 | [diff] [blame] | 9432 | */ |
| 9433 | static int |
| 9434 | 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] | 9435 | const struct arg *args, struct sample *smp, const char *kw) |
Willy Tarreau | a7ad50c | 2012-04-29 15:39:40 +0200 | [diff] [blame] | 9436 | { |
| 9437 | struct http_txn *txn = l7; |
| 9438 | char *ptr, *end, *beg; |
| 9439 | struct hdr_ctx ctx; |
| 9440 | |
| 9441 | CHECK_HTTP_MESSAGE_FIRST(); |
| 9442 | |
| 9443 | ctx.idx = 0; |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 9444 | 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] | 9445 | !ctx.vlen) |
Willy Tarreau | ef38c39 | 2013-07-22 16:29:32 +0200 | [diff] [blame] | 9446 | return smp_fetch_path(px, l4, l7, opt, args, smp, kw); |
Willy Tarreau | a7ad50c | 2012-04-29 15:39:40 +0200 | [diff] [blame] | 9447 | |
| 9448 | /* 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] | 9449 | memcpy(trash.str, ctx.line + ctx.val, ctx.vlen); |
Willy Tarreau | a7ad50c | 2012-04-29 15:39:40 +0200 | [diff] [blame] | 9450 | smp->type = SMP_T_STR; |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 9451 | smp->data.str.str = trash.str; |
Willy Tarreau | a7ad50c | 2012-04-29 15:39:40 +0200 | [diff] [blame] | 9452 | smp->data.str.len = ctx.vlen; |
| 9453 | |
| 9454 | /* now retrieve the path */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 9455 | 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] | 9456 | beg = http_get_path(txn); |
| 9457 | if (!beg) |
| 9458 | beg = end; |
| 9459 | |
| 9460 | for (ptr = beg; ptr < end && *ptr != '?'; ptr++); |
| 9461 | |
| 9462 | if (beg < ptr && *beg == '/') { |
| 9463 | memcpy(smp->data.str.str + smp->data.str.len, beg, ptr - beg); |
| 9464 | smp->data.str.len += ptr - beg; |
| 9465 | } |
| 9466 | |
| 9467 | smp->flags = SMP_F_VOL_1ST; |
| 9468 | return 1; |
| 9469 | } |
| 9470 | |
Willy Tarreau | ab1f7b7 | 2012-12-09 13:38:54 +0100 | [diff] [blame] | 9471 | /* This produces a 32-bit hash of the concatenation of the first occurrence of |
| 9472 | * the Host header followed by the path component if it begins with a slash ('/'). |
| 9473 | * This means that '*' will not be added, resulting in exactly the first Host |
| 9474 | * 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] | 9475 | * is hashed using the path hash followed by a full avalanche hash and provides a |
| 9476 | * 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] | 9477 | * high-traffic sites without having to store whole paths. |
| 9478 | */ |
| 9479 | static int |
| 9480 | 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] | 9481 | const struct arg *args, struct sample *smp, const char *kw) |
Willy Tarreau | ab1f7b7 | 2012-12-09 13:38:54 +0100 | [diff] [blame] | 9482 | { |
| 9483 | struct http_txn *txn = l7; |
| 9484 | struct hdr_ctx ctx; |
| 9485 | unsigned int hash = 0; |
| 9486 | char *ptr, *beg, *end; |
| 9487 | int len; |
| 9488 | |
| 9489 | CHECK_HTTP_MESSAGE_FIRST(); |
| 9490 | |
| 9491 | ctx.idx = 0; |
| 9492 | if (http_find_header2("Host", 4, txn->req.chn->buf->p + txn->req.sol, &txn->hdr_idx, &ctx)) { |
| 9493 | /* OK we have the header value in ctx.line+ctx.val for ctx.vlen bytes */ |
| 9494 | ptr = ctx.line + ctx.val; |
| 9495 | len = ctx.vlen; |
| 9496 | while (len--) |
| 9497 | hash = *(ptr++) + (hash << 6) + (hash << 16) - hash; |
| 9498 | } |
| 9499 | |
| 9500 | /* now retrieve the path */ |
| 9501 | end = txn->req.chn->buf->p + txn->req.sol + txn->req.sl.rq.u + txn->req.sl.rq.u_l; |
| 9502 | beg = http_get_path(txn); |
| 9503 | if (!beg) |
| 9504 | beg = end; |
| 9505 | |
| 9506 | for (ptr = beg; ptr < end && *ptr != '?'; ptr++); |
| 9507 | |
| 9508 | if (beg < ptr && *beg == '/') { |
| 9509 | while (beg < ptr) |
| 9510 | hash = *(beg++) + (hash << 6) + (hash << 16) - hash; |
| 9511 | } |
| 9512 | hash = full_hash(hash); |
| 9513 | |
| 9514 | smp->type = SMP_T_UINT; |
| 9515 | smp->data.uint = hash; |
| 9516 | smp->flags = SMP_F_VOL_1ST; |
| 9517 | return 1; |
| 9518 | } |
| 9519 | |
Willy Tarreau | 4a55060 | 2012-12-09 14:53:32 +0100 | [diff] [blame] | 9520 | /* 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] | 9521 | * path as returned by smp_fetch_base32(). The idea is to have per-source and |
| 9522 | * per-path counters. The result is a binary block from 8 to 20 bytes depending |
| 9523 | * 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] | 9524 | * that in environments where IPv6 is insignificant, truncating the output to |
| 9525 | * 8 bytes would still work. |
| 9526 | */ |
| 9527 | static int |
| 9528 | 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] | 9529 | const struct arg *args, struct sample *smp, const char *kw) |
Willy Tarreau | 4a55060 | 2012-12-09 14:53:32 +0100 | [diff] [blame] | 9530 | { |
Willy Tarreau | 47ca545 | 2012-12-23 20:22:19 +0100 | [diff] [blame] | 9531 | struct chunk *temp; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 9532 | struct connection *cli_conn = objt_conn(l4->si[0].end); |
| 9533 | |
| 9534 | if (!cli_conn) |
| 9535 | return 0; |
Willy Tarreau | 4a55060 | 2012-12-09 14:53:32 +0100 | [diff] [blame] | 9536 | |
Willy Tarreau | ef38c39 | 2013-07-22 16:29:32 +0200 | [diff] [blame] | 9537 | if (!smp_fetch_base32(px, l4, l7, opt, args, smp, kw)) |
Willy Tarreau | 4a55060 | 2012-12-09 14:53:32 +0100 | [diff] [blame] | 9538 | return 0; |
| 9539 | |
Willy Tarreau | 47ca545 | 2012-12-23 20:22:19 +0100 | [diff] [blame] | 9540 | temp = get_trash_chunk(); |
Willy Tarreau | 4a55060 | 2012-12-09 14:53:32 +0100 | [diff] [blame] | 9541 | memcpy(temp->str + temp->len, &smp->data.uint, sizeof(smp->data.uint)); |
| 9542 | temp->len += sizeof(smp->data.uint); |
| 9543 | |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 9544 | switch (cli_conn->addr.from.ss_family) { |
Willy Tarreau | 4a55060 | 2012-12-09 14:53:32 +0100 | [diff] [blame] | 9545 | case AF_INET: |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 9546 | 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] | 9547 | temp->len += 4; |
| 9548 | break; |
| 9549 | case AF_INET6: |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 9550 | 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] | 9551 | temp->len += 16; |
| 9552 | break; |
| 9553 | default: |
| 9554 | return 0; |
| 9555 | } |
| 9556 | |
| 9557 | smp->data.str = *temp; |
| 9558 | smp->type = SMP_T_BIN; |
| 9559 | return 1; |
| 9560 | } |
| 9561 | |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 9562 | static int |
Willy Tarreau | 409bcde | 2013-01-08 00:31:00 +0100 | [diff] [blame] | 9563 | 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] | 9564 | const struct arg *args, struct sample *smp, const char *kw) |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 9565 | { |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 9566 | /* Note: hdr_idx.v cannot be NULL in this ACL because the ACL is tagged |
| 9567 | * as a layer7 ACL, which involves automatic allocation of hdr_idx. |
| 9568 | */ |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 9569 | |
Willy Tarreau | 24e32d8 | 2012-04-23 23:55:44 +0200 | [diff] [blame] | 9570 | CHECK_HTTP_MESSAGE_FIRST_PERM(); |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 9571 | |
Willy Tarreau | f853c46 | 2012-04-23 18:53:56 +0200 | [diff] [blame] | 9572 | smp->type = SMP_T_BOOL; |
Willy Tarreau | 197e10a | 2012-04-23 19:18:42 +0200 | [diff] [blame] | 9573 | smp->data.uint = 1; |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 9574 | return 1; |
| 9575 | } |
| 9576 | |
Willy Tarreau | 7f18e52 | 2010-10-22 20:04:13 +0200 | [diff] [blame] | 9577 | /* return a valid test if the current request is the first one on the connection */ |
| 9578 | static int |
Willy Tarreau | 409bcde | 2013-01-08 00:31:00 +0100 | [diff] [blame] | 9579 | 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] | 9580 | const struct arg *args, struct sample *smp, const char *kw) |
Willy Tarreau | 7f18e52 | 2010-10-22 20:04:13 +0200 | [diff] [blame] | 9581 | { |
| 9582 | if (!s) |
| 9583 | return 0; |
| 9584 | |
Willy Tarreau | f853c46 | 2012-04-23 18:53:56 +0200 | [diff] [blame] | 9585 | smp->type = SMP_T_BOOL; |
Willy Tarreau | 197e10a | 2012-04-23 19:18:42 +0200 | [diff] [blame] | 9586 | smp->data.uint = !(s->txn.flags & TX_NOT_FIRST); |
Willy Tarreau | 7f18e52 | 2010-10-22 20:04:13 +0200 | [diff] [blame] | 9587 | return 1; |
| 9588 | } |
| 9589 | |
Willy Tarreau | 34db108 | 2012-04-19 17:16:54 +0200 | [diff] [blame] | 9590 | /* Accepts exactly 1 argument of type userlist */ |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 9591 | static int |
Willy Tarreau | 409bcde | 2013-01-08 00:31:00 +0100 | [diff] [blame] | 9592 | 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] | 9593 | const struct arg *args, struct sample *smp, const char *kw) |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 9594 | { |
| 9595 | |
Willy Tarreau | 24e32d8 | 2012-04-23 23:55:44 +0200 | [diff] [blame] | 9596 | if (!args || args->type != ARGT_USR) |
Willy Tarreau | 34db108 | 2012-04-19 17:16:54 +0200 | [diff] [blame] | 9597 | return 0; |
| 9598 | |
Willy Tarreau | c0239e0 | 2012-04-16 14:42:55 +0200 | [diff] [blame] | 9599 | CHECK_HTTP_MESSAGE_FIRST(); |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 9600 | |
Willy Tarreau | c0239e0 | 2012-04-16 14:42:55 +0200 | [diff] [blame] | 9601 | if (!get_http_auth(l4)) |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 9602 | return 0; |
| 9603 | |
Willy Tarreau | f853c46 | 2012-04-23 18:53:56 +0200 | [diff] [blame] | 9604 | smp->type = SMP_T_BOOL; |
Willy Tarreau | 24e32d8 | 2012-04-23 23:55:44 +0200 | [diff] [blame] | 9605 | smp->data.uint = check_user(args->data.usr, 0, l4->txn.auth.user, l4->txn.auth.pass); |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 9606 | return 1; |
| 9607 | } |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 9608 | |
Willy Tarreau | 4a3fd4c | 2012-05-10 23:18:26 +0200 | [diff] [blame] | 9609 | /* Accepts exactly 1 argument of type userlist */ |
| 9610 | static int |
Willy Tarreau | 409bcde | 2013-01-08 00:31:00 +0100 | [diff] [blame] | 9611 | 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] | 9612 | const struct arg *args, struct sample *smp, const char *kw) |
Willy Tarreau | 4a3fd4c | 2012-05-10 23:18:26 +0200 | [diff] [blame] | 9613 | { |
| 9614 | |
| 9615 | if (!args || args->type != ARGT_USR) |
| 9616 | return 0; |
| 9617 | |
| 9618 | CHECK_HTTP_MESSAGE_FIRST(); |
| 9619 | |
| 9620 | if (!get_http_auth(l4)) |
| 9621 | return 0; |
| 9622 | |
Thierry FOURNIER | a65b343 | 2013-11-28 18:22:00 +0100 | [diff] [blame] | 9623 | /* pat_match_auth() will need several information at once */ |
Willy Tarreau | 4a3fd4c | 2012-05-10 23:18:26 +0200 | [diff] [blame] | 9624 | smp->ctx.a[0] = args->data.usr; /* user list */ |
| 9625 | smp->ctx.a[1] = l4->txn.auth.user; /* user name */ |
| 9626 | smp->ctx.a[2] = l4->txn.auth.pass; /* password */ |
| 9627 | |
| 9628 | /* if the user does not belong to the userlist or has a wrong password, |
| 9629 | * report that it unconditionally does not match. Otherwise we return |
| 9630 | * a non-zero integer which will be ignored anyway since all the params |
Thierry FOURNIER | a65b343 | 2013-11-28 18:22:00 +0100 | [diff] [blame] | 9631 | * that pat_match_auth() will use are in test->ctx.a[0,1,2]. |
Willy Tarreau | 4a3fd4c | 2012-05-10 23:18:26 +0200 | [diff] [blame] | 9632 | */ |
| 9633 | smp->type = SMP_T_BOOL; |
| 9634 | smp->data.uint = check_user(args->data.usr, 0, l4->txn.auth.user, l4->txn.auth.pass); |
| 9635 | if (smp->data.uint) |
| 9636 | smp->type = SMP_T_UINT; |
| 9637 | |
| 9638 | return 1; |
| 9639 | } |
| 9640 | |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 9641 | /* Try to find the next occurrence of a cookie name in a cookie header value. |
| 9642 | * The lookup begins at <hdr>. The pointer and size of the next occurrence of |
| 9643 | * the cookie value is returned into *value and *value_l, and the function |
| 9644 | * returns a pointer to the next pointer to search from if the value was found. |
| 9645 | * Otherwise if the cookie was not found, NULL is returned and neither value |
| 9646 | * nor value_l are touched. The input <hdr> string should first point to the |
| 9647 | * header's value, and the <hdr_end> pointer must point to the first character |
| 9648 | * not part of the value. <list> must be non-zero if value may represent a list |
| 9649 | * of values (cookie headers). This makes it faster to abort parsing when no |
| 9650 | * list is expected. |
| 9651 | */ |
| 9652 | static char * |
| 9653 | extract_cookie_value(char *hdr, const char *hdr_end, |
| 9654 | char *cookie_name, size_t cookie_name_l, int list, |
Willy Tarreau | 3fb818c | 2012-04-11 17:21:08 +0200 | [diff] [blame] | 9655 | char **value, int *value_l) |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 9656 | { |
| 9657 | char *equal, *att_end, *att_beg, *val_beg, *val_end; |
| 9658 | char *next; |
| 9659 | |
| 9660 | /* we search at least a cookie name followed by an equal, and more |
| 9661 | * generally something like this : |
| 9662 | * Cookie: NAME1 = VALUE 1 ; NAME2 = VALUE2 ; NAME3 = VALUE3\r\n |
| 9663 | */ |
| 9664 | for (att_beg = hdr; att_beg + cookie_name_l + 1 < hdr_end; att_beg = next + 1) { |
| 9665 | /* Iterate through all cookies on this line */ |
| 9666 | |
| 9667 | while (att_beg < hdr_end && http_is_spht[(unsigned char)*att_beg]) |
| 9668 | att_beg++; |
| 9669 | |
| 9670 | /* find att_end : this is the first character after the last non |
| 9671 | * space before the equal. It may be equal to hdr_end. |
| 9672 | */ |
| 9673 | equal = att_end = att_beg; |
| 9674 | |
| 9675 | while (equal < hdr_end) { |
| 9676 | if (*equal == '=' || *equal == ';' || (list && *equal == ',')) |
| 9677 | break; |
| 9678 | if (http_is_spht[(unsigned char)*equal++]) |
| 9679 | continue; |
| 9680 | att_end = equal; |
| 9681 | } |
| 9682 | |
| 9683 | /* here, <equal> points to '=', a delimitor or the end. <att_end> |
| 9684 | * is between <att_beg> and <equal>, both may be identical. |
| 9685 | */ |
| 9686 | |
| 9687 | /* look for end of cookie if there is an equal sign */ |
| 9688 | if (equal < hdr_end && *equal == '=') { |
| 9689 | /* look for the beginning of the value */ |
| 9690 | val_beg = equal + 1; |
| 9691 | while (val_beg < hdr_end && http_is_spht[(unsigned char)*val_beg]) |
| 9692 | val_beg++; |
| 9693 | |
| 9694 | /* find the end of the value, respecting quotes */ |
| 9695 | next = find_cookie_value_end(val_beg, hdr_end); |
| 9696 | |
| 9697 | /* make val_end point to the first white space or delimitor after the value */ |
| 9698 | val_end = next; |
| 9699 | while (val_end > val_beg && http_is_spht[(unsigned char)*(val_end - 1)]) |
| 9700 | val_end--; |
| 9701 | } else { |
| 9702 | val_beg = val_end = next = equal; |
| 9703 | } |
| 9704 | |
| 9705 | /* We have nothing to do with attributes beginning with '$'. However, |
| 9706 | * they will automatically be removed if a header before them is removed, |
| 9707 | * since they're supposed to be linked together. |
| 9708 | */ |
| 9709 | if (*att_beg == '$') |
| 9710 | continue; |
| 9711 | |
| 9712 | /* Ignore cookies with no equal sign */ |
| 9713 | if (equal == next) |
| 9714 | continue; |
| 9715 | |
| 9716 | /* Now we have the cookie name between att_beg and att_end, and |
| 9717 | * its value between val_beg and val_end. |
| 9718 | */ |
| 9719 | |
| 9720 | if (att_end - att_beg == cookie_name_l && |
| 9721 | memcmp(att_beg, cookie_name, cookie_name_l) == 0) { |
| 9722 | /* let's return this value and indicate where to go on from */ |
| 9723 | *value = val_beg; |
| 9724 | *value_l = val_end - val_beg; |
| 9725 | return next + 1; |
| 9726 | } |
| 9727 | |
| 9728 | /* Set-Cookie headers only have the name in the first attr=value part */ |
| 9729 | if (!list) |
| 9730 | break; |
| 9731 | } |
| 9732 | |
| 9733 | return NULL; |
| 9734 | } |
| 9735 | |
William Lallemand | a43ba4e | 2014-01-28 18:14:25 +0100 | [diff] [blame] | 9736 | /* Fetch a captured HTTP request header. The index is the position of |
| 9737 | * the "capture" option in the configuration file |
| 9738 | */ |
| 9739 | static int |
| 9740 | smp_fetch_capture_header_req(struct proxy *px, struct session *l4, void *l7, unsigned int opt, |
| 9741 | const struct arg *args, struct sample *smp, const char *kw) |
| 9742 | { |
| 9743 | struct proxy *fe = l4->fe; |
| 9744 | struct http_txn *txn = l7; |
| 9745 | int idx; |
| 9746 | |
| 9747 | if (!args || args->type != ARGT_UINT) |
| 9748 | return 0; |
| 9749 | |
| 9750 | idx = args->data.uint; |
| 9751 | |
| 9752 | if (idx > (fe->nb_req_cap - 1) || txn->req.cap == NULL || txn->req.cap[idx] == NULL) |
| 9753 | return 0; |
| 9754 | |
| 9755 | smp->type = SMP_T_CSTR; |
| 9756 | smp->data.str.str = txn->req.cap[idx]; |
| 9757 | smp->data.str.len = strlen(txn->req.cap[idx]); |
| 9758 | |
| 9759 | return 1; |
| 9760 | } |
| 9761 | |
| 9762 | /* Fetch a captured HTTP response header. The index is the position of |
| 9763 | * the "capture" option in the configuration file |
| 9764 | */ |
| 9765 | static int |
| 9766 | smp_fetch_capture_header_res(struct proxy *px, struct session *l4, void *l7, unsigned int opt, |
| 9767 | const struct arg *args, struct sample *smp, const char *kw) |
| 9768 | { |
| 9769 | struct proxy *fe = l4->fe; |
| 9770 | struct http_txn *txn = l7; |
| 9771 | int idx; |
| 9772 | |
| 9773 | if (!args || args->type != ARGT_UINT) |
| 9774 | return 0; |
| 9775 | |
| 9776 | idx = args->data.uint; |
| 9777 | |
| 9778 | if (idx > (fe->nb_rsp_cap - 1) || txn->rsp.cap == NULL || txn->rsp.cap[idx] == NULL) |
| 9779 | return 0; |
| 9780 | |
| 9781 | smp->type = SMP_T_CSTR; |
| 9782 | smp->data.str.str = txn->rsp.cap[idx]; |
| 9783 | smp->data.str.len = strlen(txn->rsp.cap[idx]); |
| 9784 | |
| 9785 | return 1; |
| 9786 | } |
| 9787 | |
William Lallemand | 65ad6e1 | 2014-01-31 15:08:02 +0100 | [diff] [blame] | 9788 | /* Extracts the METHOD in the HTTP request, the txn->uri should be filled before the call */ |
| 9789 | static int |
| 9790 | smp_fetch_capture_req_method(struct proxy *px, struct session *l4, void *l7, unsigned int opt, |
| 9791 | const struct arg *args, struct sample *smp, const char *kw) |
| 9792 | { |
| 9793 | struct chunk *temp; |
| 9794 | struct http_txn *txn = l7; |
William Lallemand | 96a7785 | 2014-02-05 00:30:02 +0100 | [diff] [blame] | 9795 | char *ptr; |
William Lallemand | 65ad6e1 | 2014-01-31 15:08:02 +0100 | [diff] [blame] | 9796 | |
| 9797 | if (!txn->uri) |
| 9798 | return 0; |
| 9799 | |
William Lallemand | 96a7785 | 2014-02-05 00:30:02 +0100 | [diff] [blame] | 9800 | ptr = txn->uri; |
William Lallemand | 65ad6e1 | 2014-01-31 15:08:02 +0100 | [diff] [blame] | 9801 | |
William Lallemand | 96a7785 | 2014-02-05 00:30:02 +0100 | [diff] [blame] | 9802 | while (*ptr != ' ' && *ptr != '\0') /* find first space */ |
| 9803 | ptr++; |
William Lallemand | 65ad6e1 | 2014-01-31 15:08:02 +0100 | [diff] [blame] | 9804 | |
William Lallemand | 96a7785 | 2014-02-05 00:30:02 +0100 | [diff] [blame] | 9805 | temp = get_trash_chunk(); |
| 9806 | temp->str = txn->uri; |
| 9807 | temp->len = ptr - txn->uri; |
William Lallemand | 65ad6e1 | 2014-01-31 15:08:02 +0100 | [diff] [blame] | 9808 | smp->data.str = *temp; |
William Lallemand | 96a7785 | 2014-02-05 00:30:02 +0100 | [diff] [blame] | 9809 | smp->type = SMP_T_CSTR; |
William Lallemand | 65ad6e1 | 2014-01-31 15:08:02 +0100 | [diff] [blame] | 9810 | |
| 9811 | return 1; |
| 9812 | |
| 9813 | } |
| 9814 | |
| 9815 | /* Extracts the path in the HTTP request, the txn->uri should be filled before the call */ |
| 9816 | static int |
| 9817 | smp_fetch_capture_req_uri(struct proxy *px, struct session *l4, void *l7, unsigned int opt, |
| 9818 | const struct arg *args, struct sample *smp, const char *kw) |
| 9819 | { |
| 9820 | struct chunk *temp; |
| 9821 | struct http_txn *txn = l7; |
| 9822 | char *ptr; |
William Lallemand | 65ad6e1 | 2014-01-31 15:08:02 +0100 | [diff] [blame] | 9823 | |
| 9824 | if (!txn->uri) |
| 9825 | return 0; |
William Lallemand | 96a7785 | 2014-02-05 00:30:02 +0100 | [diff] [blame] | 9826 | |
William Lallemand | 65ad6e1 | 2014-01-31 15:08:02 +0100 | [diff] [blame] | 9827 | ptr = txn->uri; |
| 9828 | |
| 9829 | while (*ptr != ' ' && *ptr != '\0') /* find first space */ |
| 9830 | ptr++; |
William Lallemand | 96a7785 | 2014-02-05 00:30:02 +0100 | [diff] [blame] | 9831 | |
William Lallemand | 65ad6e1 | 2014-01-31 15:08:02 +0100 | [diff] [blame] | 9832 | if (!*ptr) |
| 9833 | return 0; |
| 9834 | |
| 9835 | ptr++; /* skip the space */ |
| 9836 | |
| 9837 | temp = get_trash_chunk(); |
William Lallemand | 96a7785 | 2014-02-05 00:30:02 +0100 | [diff] [blame] | 9838 | ptr = temp->str = http_get_path_from_string(ptr); |
William Lallemand | 65ad6e1 | 2014-01-31 15:08:02 +0100 | [diff] [blame] | 9839 | if (!ptr) |
| 9840 | return 0; |
| 9841 | while (*ptr != ' ' && *ptr != '\0') /* find space after URI */ |
| 9842 | ptr++; |
William Lallemand | 65ad6e1 | 2014-01-31 15:08:02 +0100 | [diff] [blame] | 9843 | |
| 9844 | smp->data.str = *temp; |
William Lallemand | 96a7785 | 2014-02-05 00:30:02 +0100 | [diff] [blame] | 9845 | smp->data.str.len = ptr - temp->str; |
| 9846 | smp->type = SMP_T_CSTR; |
William Lallemand | 65ad6e1 | 2014-01-31 15:08:02 +0100 | [diff] [blame] | 9847 | |
| 9848 | return 1; |
| 9849 | } |
| 9850 | |
| 9851 | |
Willy Tarreau | e333ec9 | 2012-04-16 16:26:40 +0200 | [diff] [blame] | 9852 | /* 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] | 9853 | * 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] | 9854 | * 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] | 9855 | * 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] | 9856 | * 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] | 9857 | * Accepts exactly 1 argument of type string. If the input options indicate |
| 9858 | * 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] | 9859 | * The returned sample is of type CSTR. |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 9860 | */ |
| 9861 | static int |
Willy Tarreau | 5153936 | 2012-05-08 12:46:28 +0200 | [diff] [blame] | 9862 | 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] | 9863 | const struct arg *args, struct sample *smp, const char *kw) |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 9864 | { |
| 9865 | struct http_txn *txn = l7; |
| 9866 | struct hdr_idx *idx = &txn->hdr_idx; |
Willy Tarreau | a890d07 | 2013-04-02 12:01:06 +0200 | [diff] [blame] | 9867 | struct hdr_ctx *ctx = smp->ctx.a[2]; |
Willy Tarreau | e333ec9 | 2012-04-16 16:26:40 +0200 | [diff] [blame] | 9868 | const struct http_msg *msg; |
| 9869 | const char *hdr_name; |
| 9870 | int hdr_name_len; |
| 9871 | char *sol; |
Willy Tarreau | 28376d6 | 2012-04-26 21:26:10 +0200 | [diff] [blame] | 9872 | int occ = 0; |
| 9873 | int found = 0; |
Willy Tarreau | e333ec9 | 2012-04-16 16:26:40 +0200 | [diff] [blame] | 9874 | |
Willy Tarreau | 24e32d8 | 2012-04-23 23:55:44 +0200 | [diff] [blame] | 9875 | if (!args || args->type != ARGT_STR) |
Willy Tarreau | 34db108 | 2012-04-19 17:16:54 +0200 | [diff] [blame] | 9876 | return 0; |
| 9877 | |
Willy Tarreau | a890d07 | 2013-04-02 12:01:06 +0200 | [diff] [blame] | 9878 | if (!ctx) { |
| 9879 | /* first call */ |
| 9880 | ctx = &static_hdr_ctx; |
| 9881 | ctx->idx = 0; |
| 9882 | smp->ctx.a[2] = ctx; |
| 9883 | } |
| 9884 | |
Willy Tarreau | e333ec9 | 2012-04-16 16:26:40 +0200 | [diff] [blame] | 9885 | CHECK_HTTP_MESSAGE_FIRST(); |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 9886 | |
Willy Tarreau | 32a6f2e | 2012-04-25 10:13:36 +0200 | [diff] [blame] | 9887 | if ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) { |
Willy Tarreau | e333ec9 | 2012-04-16 16:26:40 +0200 | [diff] [blame] | 9888 | msg = &txn->req; |
| 9889 | hdr_name = "Cookie"; |
| 9890 | hdr_name_len = 6; |
| 9891 | } else { |
| 9892 | msg = &txn->rsp; |
| 9893 | hdr_name = "Set-Cookie"; |
| 9894 | hdr_name_len = 10; |
| 9895 | } |
| 9896 | |
Willy Tarreau | 28376d6 | 2012-04-26 21:26:10 +0200 | [diff] [blame] | 9897 | if (!occ && !(opt & SMP_OPT_ITERATE)) |
| 9898 | /* no explicit occurrence and single fetch => last cookie by default */ |
| 9899 | occ = -1; |
| 9900 | |
| 9901 | /* OK so basically here, either we want only one value and it's the |
| 9902 | * last one, or we want to iterate over all of them and we fetch the |
| 9903 | * next one. |
| 9904 | */ |
| 9905 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 9906 | sol = msg->chn->buf->p; |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 9907 | if (!(smp->flags & SMP_F_NOT_LAST)) { |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 9908 | /* search for the header from the beginning, we must first initialize |
| 9909 | * the search parameters. |
| 9910 | */ |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 9911 | smp->ctx.a[0] = NULL; |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 9912 | ctx->idx = 0; |
| 9913 | } |
| 9914 | |
Willy Tarreau | 28376d6 | 2012-04-26 21:26:10 +0200 | [diff] [blame] | 9915 | smp->flags |= SMP_F_VOL_HDR; |
| 9916 | |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 9917 | while (1) { |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 9918 | /* Note: smp->ctx.a[0] == NULL every time we need to fetch a new header */ |
| 9919 | if (!smp->ctx.a[0]) { |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 9920 | if (!http_find_header2(hdr_name, hdr_name_len, sol, idx, ctx)) |
| 9921 | goto out; |
| 9922 | |
Willy Tarreau | 24e32d8 | 2012-04-23 23:55:44 +0200 | [diff] [blame] | 9923 | if (ctx->vlen < args->data.str.len + 1) |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 9924 | continue; |
| 9925 | |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 9926 | smp->ctx.a[0] = ctx->line + ctx->val; |
| 9927 | smp->ctx.a[1] = smp->ctx.a[0] + ctx->vlen; |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 9928 | } |
| 9929 | |
Willy Tarreau | f853c46 | 2012-04-23 18:53:56 +0200 | [diff] [blame] | 9930 | smp->type = SMP_T_CSTR; |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 9931 | 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] | 9932 | args->data.str.str, args->data.str.len, |
Willy Tarreau | 32a6f2e | 2012-04-25 10:13:36 +0200 | [diff] [blame] | 9933 | (opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ, |
Willy Tarreau | f853c46 | 2012-04-23 18:53:56 +0200 | [diff] [blame] | 9934 | &smp->data.str.str, |
| 9935 | &smp->data.str.len); |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 9936 | if (smp->ctx.a[0]) { |
Willy Tarreau | 28376d6 | 2012-04-26 21:26:10 +0200 | [diff] [blame] | 9937 | found = 1; |
| 9938 | if (occ >= 0) { |
| 9939 | /* one value was returned into smp->data.str.{str,len} */ |
| 9940 | smp->flags |= SMP_F_NOT_LAST; |
| 9941 | return 1; |
| 9942 | } |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 9943 | } |
Willy Tarreau | 28376d6 | 2012-04-26 21:26:10 +0200 | [diff] [blame] | 9944 | /* if we're looking for last occurrence, let's loop */ |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 9945 | } |
Willy Tarreau | 28376d6 | 2012-04-26 21:26:10 +0200 | [diff] [blame] | 9946 | /* all cookie headers and values were scanned. If we're looking for the |
| 9947 | * last occurrence, we may return it now. |
| 9948 | */ |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 9949 | out: |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 9950 | smp->flags &= ~SMP_F_NOT_LAST; |
Willy Tarreau | 28376d6 | 2012-04-26 21:26:10 +0200 | [diff] [blame] | 9951 | return found; |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 9952 | } |
| 9953 | |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 9954 | /* 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] | 9955 | * 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] | 9956 | * multiple cookies may be parsed on the same line. The returned sample is of |
| 9957 | * type UINT. Accepts exactly 1 argument of type string. |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 9958 | */ |
| 9959 | static int |
Willy Tarreau | 409bcde | 2013-01-08 00:31:00 +0100 | [diff] [blame] | 9960 | 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] | 9961 | const struct arg *args, struct sample *smp, const char *kw) |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 9962 | { |
| 9963 | struct http_txn *txn = l7; |
| 9964 | struct hdr_idx *idx = &txn->hdr_idx; |
| 9965 | struct hdr_ctx ctx; |
Willy Tarreau | e333ec9 | 2012-04-16 16:26:40 +0200 | [diff] [blame] | 9966 | const struct http_msg *msg; |
| 9967 | const char *hdr_name; |
| 9968 | int hdr_name_len; |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 9969 | int cnt; |
| 9970 | char *val_beg, *val_end; |
Willy Tarreau | e333ec9 | 2012-04-16 16:26:40 +0200 | [diff] [blame] | 9971 | char *sol; |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 9972 | |
Willy Tarreau | 24e32d8 | 2012-04-23 23:55:44 +0200 | [diff] [blame] | 9973 | if (!args || args->type != ARGT_STR) |
Willy Tarreau | 34db108 | 2012-04-19 17:16:54 +0200 | [diff] [blame] | 9974 | return 0; |
| 9975 | |
Willy Tarreau | e333ec9 | 2012-04-16 16:26:40 +0200 | [diff] [blame] | 9976 | CHECK_HTTP_MESSAGE_FIRST(); |
| 9977 | |
Willy Tarreau | 32a6f2e | 2012-04-25 10:13:36 +0200 | [diff] [blame] | 9978 | if ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) { |
Willy Tarreau | e333ec9 | 2012-04-16 16:26:40 +0200 | [diff] [blame] | 9979 | msg = &txn->req; |
| 9980 | hdr_name = "Cookie"; |
| 9981 | hdr_name_len = 6; |
| 9982 | } else { |
| 9983 | msg = &txn->rsp; |
| 9984 | hdr_name = "Set-Cookie"; |
| 9985 | hdr_name_len = 10; |
| 9986 | } |
| 9987 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 9988 | sol = msg->chn->buf->p; |
Willy Tarreau | 46787ed | 2012-04-11 17:28:40 +0200 | [diff] [blame] | 9989 | val_end = val_beg = NULL; |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 9990 | ctx.idx = 0; |
| 9991 | cnt = 0; |
| 9992 | |
| 9993 | while (1) { |
| 9994 | /* Note: val_beg == NULL every time we need to fetch a new header */ |
| 9995 | if (!val_beg) { |
| 9996 | if (!http_find_header2(hdr_name, hdr_name_len, sol, idx, &ctx)) |
| 9997 | break; |
| 9998 | |
Willy Tarreau | 24e32d8 | 2012-04-23 23:55:44 +0200 | [diff] [blame] | 9999 | if (ctx.vlen < args->data.str.len + 1) |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 10000 | continue; |
| 10001 | |
| 10002 | val_beg = ctx.line + ctx.val; |
| 10003 | val_end = val_beg + ctx.vlen; |
| 10004 | } |
| 10005 | |
Willy Tarreau | f853c46 | 2012-04-23 18:53:56 +0200 | [diff] [blame] | 10006 | smp->type = SMP_T_CSTR; |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 10007 | while ((val_beg = extract_cookie_value(val_beg, val_end, |
Willy Tarreau | 24e32d8 | 2012-04-23 23:55:44 +0200 | [diff] [blame] | 10008 | args->data.str.str, args->data.str.len, |
Willy Tarreau | 32a6f2e | 2012-04-25 10:13:36 +0200 | [diff] [blame] | 10009 | (opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ, |
Willy Tarreau | f853c46 | 2012-04-23 18:53:56 +0200 | [diff] [blame] | 10010 | &smp->data.str.str, |
| 10011 | &smp->data.str.len))) { |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 10012 | cnt++; |
| 10013 | } |
| 10014 | } |
| 10015 | |
Willy Tarreau | b169eba | 2013-12-16 15:14:43 +0100 | [diff] [blame] | 10016 | smp->type = SMP_T_UINT; |
Willy Tarreau | f853c46 | 2012-04-23 18:53:56 +0200 | [diff] [blame] | 10017 | smp->data.uint = cnt; |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 10018 | smp->flags |= SMP_F_VOL_HDR; |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 10019 | return 1; |
| 10020 | } |
| 10021 | |
Willy Tarreau | 5153936 | 2012-05-08 12:46:28 +0200 | [diff] [blame] | 10022 | /* Fetch an cookie's integer value. The integer value is returned. It |
| 10023 | * takes a mandatory argument of type string. It relies on smp_fetch_cookie(). |
| 10024 | */ |
| 10025 | static int |
| 10026 | 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] | 10027 | const struct arg *args, struct sample *smp, const char *kw) |
Willy Tarreau | 5153936 | 2012-05-08 12:46:28 +0200 | [diff] [blame] | 10028 | { |
Willy Tarreau | ef38c39 | 2013-07-22 16:29:32 +0200 | [diff] [blame] | 10029 | int ret = smp_fetch_cookie(px, l4, l7, opt, args, smp, kw); |
Willy Tarreau | 5153936 | 2012-05-08 12:46:28 +0200 | [diff] [blame] | 10030 | |
| 10031 | if (ret > 0) { |
| 10032 | smp->type = SMP_T_UINT; |
| 10033 | smp->data.uint = strl2ic(smp->data.str.str, smp->data.str.len); |
| 10034 | } |
| 10035 | |
| 10036 | return ret; |
| 10037 | } |
| 10038 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 10039 | /************************************************************************/ |
Willy Tarreau | 1278578 | 2012-04-27 21:37:17 +0200 | [diff] [blame] | 10040 | /* The code below is dedicated to sample fetches */ |
Willy Tarreau | 4a56897 | 2010-05-12 08:08:50 +0200 | [diff] [blame] | 10041 | /************************************************************************/ |
| 10042 | |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 10043 | /* |
| 10044 | * Given a path string and its length, find the position of beginning of the |
| 10045 | * query string. Returns NULL if no query string is found in the path. |
| 10046 | * |
| 10047 | * Example: if path = "/foo/bar/fubar?yo=mama;ye=daddy", and n = 22: |
| 10048 | * |
| 10049 | * find_query_string(path, n) points to "yo=mama;ye=daddy" string. |
| 10050 | */ |
bedis | 4c75cca | 2012-10-05 08:38:24 +0200 | [diff] [blame] | 10051 | 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] | 10052 | { |
| 10053 | char *p; |
Emeric Brun | 485479d | 2010-09-23 18:02:19 +0200 | [diff] [blame] | 10054 | |
bedis | 4c75cca | 2012-10-05 08:38:24 +0200 | [diff] [blame] | 10055 | p = memchr(path, delim, path_l); |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 10056 | return p ? p + 1 : NULL; |
| 10057 | } |
| 10058 | |
bedis | 4c75cca | 2012-10-05 08:38:24 +0200 | [diff] [blame] | 10059 | static inline int is_param_delimiter(char c, char delim) |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 10060 | { |
bedis | 4c75cca | 2012-10-05 08:38:24 +0200 | [diff] [blame] | 10061 | return c == '&' || c == ';' || c == delim; |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 10062 | } |
| 10063 | |
| 10064 | /* |
| 10065 | * Given a url parameter, find the starting position of the first occurence, |
| 10066 | * or NULL if the parameter is not found. |
| 10067 | * |
| 10068 | * Example: if query_string is "yo=mama;ye=daddy" and url_param_name is "ye", |
| 10069 | * the function will return query_string+8. |
| 10070 | */ |
| 10071 | static char* |
| 10072 | find_url_param_pos(char* query_string, size_t query_string_l, |
bedis | 4c75cca | 2012-10-05 08:38:24 +0200 | [diff] [blame] | 10073 | char* url_param_name, size_t url_param_name_l, |
| 10074 | char delim) |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 10075 | { |
| 10076 | char *pos, *last; |
| 10077 | |
| 10078 | pos = query_string; |
| 10079 | last = query_string + query_string_l - url_param_name_l - 1; |
| 10080 | |
| 10081 | while (pos <= last) { |
| 10082 | if (pos[url_param_name_l] == '=') { |
| 10083 | if (memcmp(pos, url_param_name, url_param_name_l) == 0) |
| 10084 | return pos; |
| 10085 | pos += url_param_name_l + 1; |
| 10086 | } |
bedis | 4c75cca | 2012-10-05 08:38:24 +0200 | [diff] [blame] | 10087 | while (pos <= last && !is_param_delimiter(*pos, delim)) |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 10088 | pos++; |
| 10089 | pos++; |
| 10090 | } |
| 10091 | return NULL; |
| 10092 | } |
| 10093 | |
| 10094 | /* |
| 10095 | * Given a url parameter name, returns its value and size into *value and |
| 10096 | * *value_l respectively, and returns non-zero. If the parameter is not found, |
| 10097 | * zero is returned and value/value_l are not touched. |
| 10098 | */ |
| 10099 | static int |
| 10100 | find_url_param_value(char* path, size_t path_l, |
| 10101 | char* url_param_name, size_t url_param_name_l, |
bedis | 4c75cca | 2012-10-05 08:38:24 +0200 | [diff] [blame] | 10102 | char** value, int* value_l, char delim) |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 10103 | { |
| 10104 | char *query_string, *qs_end; |
| 10105 | char *arg_start; |
| 10106 | char *value_start, *value_end; |
| 10107 | |
bedis | 4c75cca | 2012-10-05 08:38:24 +0200 | [diff] [blame] | 10108 | query_string = find_param_list(path, path_l, delim); |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 10109 | if (!query_string) |
| 10110 | return 0; |
| 10111 | |
| 10112 | qs_end = path + path_l; |
| 10113 | arg_start = find_url_param_pos(query_string, qs_end - query_string, |
bedis | 4c75cca | 2012-10-05 08:38:24 +0200 | [diff] [blame] | 10114 | url_param_name, url_param_name_l, |
| 10115 | delim); |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 10116 | if (!arg_start) |
| 10117 | return 0; |
| 10118 | |
| 10119 | value_start = arg_start + url_param_name_l + 1; |
| 10120 | value_end = value_start; |
| 10121 | |
bedis | 4c75cca | 2012-10-05 08:38:24 +0200 | [diff] [blame] | 10122 | while ((value_end < qs_end) && !is_param_delimiter(*value_end, delim)) |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 10123 | value_end++; |
| 10124 | |
| 10125 | *value = value_start; |
| 10126 | *value_l = value_end - value_start; |
Willy Tarreau | 0013433 | 2011-01-04 14:57:34 +0100 | [diff] [blame] | 10127 | return value_end != value_start; |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 10128 | } |
| 10129 | |
| 10130 | static int |
Willy Tarreau | 25c1ebc | 2012-04-25 16:21:44 +0200 | [diff] [blame] | 10131 | 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] | 10132 | const struct arg *args, struct sample *smp, const char *kw) |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 10133 | { |
bedis | 4c75cca | 2012-10-05 08:38:24 +0200 | [diff] [blame] | 10134 | char delim = '?'; |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 10135 | struct http_txn *txn = l7; |
| 10136 | struct http_msg *msg = &txn->req; |
Willy Tarreau | 25c1ebc | 2012-04-25 16:21:44 +0200 | [diff] [blame] | 10137 | |
bedis | 4c75cca | 2012-10-05 08:38:24 +0200 | [diff] [blame] | 10138 | if (!args || args[0].type != ARGT_STR || |
| 10139 | (args[1].type && args[1].type != ARGT_STR)) |
Willy Tarreau | 25c1ebc | 2012-04-25 16:21:44 +0200 | [diff] [blame] | 10140 | return 0; |
| 10141 | |
| 10142 | CHECK_HTTP_MESSAGE_FIRST(); |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 10143 | |
bedis | 4c75cca | 2012-10-05 08:38:24 +0200 | [diff] [blame] | 10144 | if (args[1].type) |
| 10145 | delim = *args[1].data.str.str; |
| 10146 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 10147 | 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] | 10148 | args->data.str.str, args->data.str.len, |
| 10149 | &smp->data.str.str, &smp->data.str.len, |
| 10150 | delim)) |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 10151 | return 0; |
| 10152 | |
Willy Tarreau | b8c8f1f | 2012-04-23 22:38:26 +0200 | [diff] [blame] | 10153 | smp->type = SMP_T_CSTR; |
Willy Tarreau | 25c1ebc | 2012-04-25 16:21:44 +0200 | [diff] [blame] | 10154 | smp->flags = SMP_F_VOL_1ST; |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 10155 | return 1; |
| 10156 | } |
| 10157 | |
Willy Tarreau | a9fddca | 2012-07-31 07:51:48 +0200 | [diff] [blame] | 10158 | /* Return the signed integer value for the specified url parameter (see url_param |
| 10159 | * above). |
| 10160 | */ |
| 10161 | static int |
| 10162 | 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] | 10163 | const struct arg *args, struct sample *smp, const char *kw) |
Willy Tarreau | a9fddca | 2012-07-31 07:51:48 +0200 | [diff] [blame] | 10164 | { |
Willy Tarreau | ef38c39 | 2013-07-22 16:29:32 +0200 | [diff] [blame] | 10165 | 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] | 10166 | |
| 10167 | if (ret > 0) { |
| 10168 | smp->type = SMP_T_UINT; |
| 10169 | smp->data.uint = strl2ic(smp->data.str.str, smp->data.str.len); |
| 10170 | } |
| 10171 | |
| 10172 | return ret; |
| 10173 | } |
| 10174 | |
Neil - HAProxy List | 39c63c5 | 2013-11-04 13:48:42 +0000 | [diff] [blame] | 10175 | /* This produces a 32-bit hash of the concatenation of the first occurrence of |
| 10176 | * the Host header followed by the path component if it begins with a slash ('/'). |
| 10177 | * This means that '*' will not be added, resulting in exactly the first Host |
| 10178 | * entry. If no Host header is found, then the path is used. The resulting value |
| 10179 | * is hashed using the url hash followed by a full avalanche hash and provides a |
| 10180 | * 32-bit integer value. This fetch is useful for tracking per-URL activity on |
| 10181 | * high-traffic sites without having to store whole paths. |
| 10182 | * this differs from the base32 functions in that it includes the url parameters |
| 10183 | * as well as the path |
| 10184 | */ |
| 10185 | static int |
| 10186 | 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] | 10187 | const struct arg *args, struct sample *smp, const char *kw) |
Neil - HAProxy List | 39c63c5 | 2013-11-04 13:48:42 +0000 | [diff] [blame] | 10188 | { |
| 10189 | struct http_txn *txn = l7; |
| 10190 | struct hdr_ctx ctx; |
| 10191 | unsigned int hash = 0; |
| 10192 | char *ptr, *beg, *end; |
| 10193 | int len; |
| 10194 | |
| 10195 | CHECK_HTTP_MESSAGE_FIRST(); |
| 10196 | |
| 10197 | ctx.idx = 0; |
| 10198 | if (http_find_header2("Host", 4, txn->req.chn->buf->p + txn->req.sol, &txn->hdr_idx, &ctx)) { |
| 10199 | /* OK we have the header value in ctx.line+ctx.val for ctx.vlen bytes */ |
| 10200 | ptr = ctx.line + ctx.val; |
| 10201 | len = ctx.vlen; |
| 10202 | while (len--) |
| 10203 | hash = *(ptr++) + (hash << 6) + (hash << 16) - hash; |
| 10204 | } |
| 10205 | |
| 10206 | /* now retrieve the path */ |
| 10207 | end = txn->req.chn->buf->p + txn->req.sol + txn->req.sl.rq.u + txn->req.sl.rq.u_l; |
| 10208 | beg = http_get_path(txn); |
| 10209 | if (!beg) |
| 10210 | beg = end; |
| 10211 | |
| 10212 | for (ptr = beg; ptr < end ; ptr++); |
| 10213 | |
| 10214 | if (beg < ptr && *beg == '/') { |
| 10215 | while (beg < ptr) |
| 10216 | hash = *(beg++) + (hash << 6) + (hash << 16) - hash; |
| 10217 | } |
| 10218 | hash = full_hash(hash); |
| 10219 | |
| 10220 | smp->type = SMP_T_UINT; |
| 10221 | smp->data.uint = hash; |
| 10222 | smp->flags = SMP_F_VOL_1ST; |
| 10223 | return 1; |
| 10224 | } |
| 10225 | |
| 10226 | /* This concatenates the source address with the 32-bit hash of the Host and |
| 10227 | * URL as returned by smp_fetch_base32(). The idea is to have per-source and |
| 10228 | * per-url counters. The result is a binary block from 8 to 20 bytes depending |
| 10229 | * on the source address length. The URL hash is stored before the address so |
| 10230 | * that in environments where IPv6 is insignificant, truncating the output to |
| 10231 | * 8 bytes would still work. |
| 10232 | */ |
| 10233 | static int |
| 10234 | 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] | 10235 | const struct arg *args, struct sample *smp, const char *kw) |
Neil - HAProxy List | 39c63c5 | 2013-11-04 13:48:42 +0000 | [diff] [blame] | 10236 | { |
| 10237 | struct chunk *temp; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 10238 | struct connection *cli_conn = objt_conn(l4->si[0].end); |
Neil - HAProxy List | 39c63c5 | 2013-11-04 13:48:42 +0000 | [diff] [blame] | 10239 | |
Willy Tarreau | e155ec2 | 2013-11-18 18:33:22 +0100 | [diff] [blame] | 10240 | if (!smp_fetch_url32(px, l4, l7, opt, args, smp, kw)) |
Neil - HAProxy List | 39c63c5 | 2013-11-04 13:48:42 +0000 | [diff] [blame] | 10241 | return 0; |
| 10242 | |
| 10243 | temp = get_trash_chunk(); |
| 10244 | memcpy(temp->str + temp->len, &smp->data.uint, sizeof(smp->data.uint)); |
| 10245 | temp->len += sizeof(smp->data.uint); |
| 10246 | |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 10247 | switch (cli_conn->addr.from.ss_family) { |
Neil - HAProxy List | 39c63c5 | 2013-11-04 13:48:42 +0000 | [diff] [blame] | 10248 | case AF_INET: |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 10249 | 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] | 10250 | temp->len += 4; |
| 10251 | break; |
| 10252 | case AF_INET6: |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 10253 | 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] | 10254 | temp->len += 16; |
| 10255 | break; |
| 10256 | default: |
| 10257 | return 0; |
| 10258 | } |
| 10259 | |
| 10260 | smp->data.str = *temp; |
| 10261 | smp->type = SMP_T_BIN; |
| 10262 | return 1; |
| 10263 | } |
| 10264 | |
Willy Tarreau | 185b5c4 | 2012-04-26 15:11:51 +0200 | [diff] [blame] | 10265 | /* This function is used to validate the arguments passed to any "hdr" fetch |
| 10266 | * keyword. These keywords support an optional positive or negative occurrence |
| 10267 | * number. We must ensure that the number is greater than -MAX_HDR_HISTORY. It |
| 10268 | * is assumed that the types are already the correct ones. Returns 0 on error, |
| 10269 | * non-zero if OK. If <err> is not NULL, it will be filled with a pointer to an |
| 10270 | * error message in case of error, that the caller is responsible for freeing. |
| 10271 | * The initial location must either be freeable or NULL. |
| 10272 | */ |
| 10273 | static int val_hdr(struct arg *arg, char **err_msg) |
| 10274 | { |
| 10275 | 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] | 10276 | memprintf(err_msg, "header occurrence must be >= %d", -MAX_HDR_HISTORY); |
Willy Tarreau | 185b5c4 | 2012-04-26 15:11:51 +0200 | [diff] [blame] | 10277 | return 0; |
| 10278 | } |
| 10279 | return 1; |
| 10280 | } |
| 10281 | |
Willy Tarreau | 276fae9 | 2013-07-25 14:36:01 +0200 | [diff] [blame] | 10282 | /* takes an UINT value on input supposed to represent the time since EPOCH, |
| 10283 | * adds an optional offset found in args[0] and emits a string representing |
| 10284 | * the date in RFC-1123/5322 format. |
| 10285 | */ |
| 10286 | static int sample_conv_http_date(const struct arg *args, struct sample *smp) |
| 10287 | { |
| 10288 | const char day[7][4] = { "Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun" }; |
| 10289 | const char mon[12][4] = { "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" }; |
| 10290 | struct chunk *temp; |
| 10291 | struct tm *tm; |
| 10292 | time_t curr_date = smp->data.uint; |
| 10293 | |
| 10294 | /* add offset */ |
| 10295 | if (args && (args[0].type == ARGT_SINT || args[0].type == ARGT_UINT)) |
| 10296 | curr_date += args[0].data.sint; |
| 10297 | |
| 10298 | tm = gmtime(&curr_date); |
| 10299 | |
| 10300 | temp = get_trash_chunk(); |
| 10301 | temp->len = snprintf(temp->str, temp->size - temp->len, |
| 10302 | "%s, %02d %s %04d %02d:%02d:%02d GMT", |
| 10303 | day[tm->tm_wday], tm->tm_mday, mon[tm->tm_mon], 1900+tm->tm_year, |
| 10304 | tm->tm_hour, tm->tm_min, tm->tm_sec); |
| 10305 | |
| 10306 | smp->data.str = *temp; |
| 10307 | smp->type = SMP_T_STR; |
| 10308 | return 1; |
| 10309 | } |
| 10310 | |
Willy Tarreau | 4a56897 | 2010-05-12 08:08:50 +0200 | [diff] [blame] | 10311 | /************************************************************************/ |
Willy Tarreau | 25c1ebc | 2012-04-25 16:21:44 +0200 | [diff] [blame] | 10312 | /* All supported ACL keywords must be declared here. */ |
| 10313 | /************************************************************************/ |
| 10314 | |
| 10315 | /* Note: must not be declared <const> as its list will be overwritten. |
| 10316 | * Please take care of keeping this list alphabetically sorted. |
| 10317 | */ |
Willy Tarreau | dc13c11 | 2013-06-21 23:16:39 +0200 | [diff] [blame] | 10318 | static struct acl_kw_list acl_kws = {ILH, { |
Thierry FOURNIER | a65b343 | 2013-11-28 18:22:00 +0100 | [diff] [blame] | 10319 | { "base", "base", pat_parse_str, pat_match_str }, |
| 10320 | { "base_beg", "base", pat_parse_str, pat_match_beg }, |
| 10321 | { "base_dir", "base", pat_parse_str, pat_match_dir }, |
| 10322 | { "base_dom", "base", pat_parse_str, pat_match_dom }, |
| 10323 | { "base_end", "base", pat_parse_str, pat_match_end }, |
| 10324 | { "base_len", "base", pat_parse_int, pat_match_len }, |
| 10325 | { "base_reg", "base", pat_parse_reg, pat_match_reg }, |
| 10326 | { "base_sub", "base", pat_parse_str, pat_match_sub }, |
Willy Tarreau | a7ad50c | 2012-04-29 15:39:40 +0200 | [diff] [blame] | 10327 | |
Thierry FOURNIER | a65b343 | 2013-11-28 18:22:00 +0100 | [diff] [blame] | 10328 | { "cook", "req.cook", pat_parse_str, pat_match_str }, |
| 10329 | { "cook_beg", "req.cook", pat_parse_str, pat_match_beg }, |
| 10330 | { "cook_dir", "req.cook", pat_parse_str, pat_match_dir }, |
| 10331 | { "cook_dom", "req.cook", pat_parse_str, pat_match_dom }, |
| 10332 | { "cook_end", "req.cook", pat_parse_str, pat_match_end }, |
| 10333 | { "cook_len", "req.cook", pat_parse_int, pat_match_len }, |
| 10334 | { "cook_reg", "req.cook", pat_parse_reg, pat_match_reg }, |
| 10335 | { "cook_sub", "req.cook", pat_parse_str, pat_match_sub }, |
Willy Tarreau | 25c1ebc | 2012-04-25 16:21:44 +0200 | [diff] [blame] | 10336 | |
Thierry FOURNIER | a65b343 | 2013-11-28 18:22:00 +0100 | [diff] [blame] | 10337 | { "hdr", "req.hdr", pat_parse_str, pat_match_str }, |
| 10338 | { "hdr_beg", "req.hdr", pat_parse_str, pat_match_beg }, |
| 10339 | { "hdr_dir", "req.hdr", pat_parse_str, pat_match_dir }, |
| 10340 | { "hdr_dom", "req.hdr", pat_parse_str, pat_match_dom }, |
| 10341 | { "hdr_end", "req.hdr", pat_parse_str, pat_match_end }, |
| 10342 | { "hdr_len", "req.hdr", pat_parse_int, pat_match_len }, |
| 10343 | { "hdr_reg", "req.hdr", pat_parse_reg, pat_match_reg }, |
| 10344 | { "hdr_sub", "req.hdr", pat_parse_str, pat_match_sub }, |
Willy Tarreau | 25c1ebc | 2012-04-25 16:21:44 +0200 | [diff] [blame] | 10345 | |
Thierry FOURNIER | a65b343 | 2013-11-28 18:22:00 +0100 | [diff] [blame] | 10346 | { "http_auth_group", NULL, pat_parse_strcat, pat_match_auth }, |
Willy Tarreau | 25c1ebc | 2012-04-25 16:21:44 +0200 | [diff] [blame] | 10347 | |
Thierry FOURNIER | a65b343 | 2013-11-28 18:22:00 +0100 | [diff] [blame] | 10348 | { "method", NULL, pat_parse_meth, pat_match_meth }, |
Willy Tarreau | 25c1ebc | 2012-04-25 16:21:44 +0200 | [diff] [blame] | 10349 | |
Thierry FOURNIER | a65b343 | 2013-11-28 18:22:00 +0100 | [diff] [blame] | 10350 | { "path", "path", pat_parse_str, pat_match_str }, |
| 10351 | { "path_beg", "path", pat_parse_str, pat_match_beg }, |
| 10352 | { "path_dir", "path", pat_parse_str, pat_match_dir }, |
| 10353 | { "path_dom", "path", pat_parse_str, pat_match_dom }, |
| 10354 | { "path_end", "path", pat_parse_str, pat_match_end }, |
| 10355 | { "path_len", "path", pat_parse_int, pat_match_len }, |
| 10356 | { "path_reg", "path", pat_parse_reg, pat_match_reg }, |
| 10357 | { "path_sub", "path", pat_parse_str, pat_match_sub }, |
Willy Tarreau | 25c1ebc | 2012-04-25 16:21:44 +0200 | [diff] [blame] | 10358 | |
Thierry FOURNIER | a65b343 | 2013-11-28 18:22:00 +0100 | [diff] [blame] | 10359 | { "req_ver", "req.ver", pat_parse_str, pat_match_str }, |
| 10360 | { "resp_ver", "res.ver", pat_parse_str, pat_match_str }, |
Willy Tarreau | 25c1ebc | 2012-04-25 16:21:44 +0200 | [diff] [blame] | 10361 | |
Thierry FOURNIER | a65b343 | 2013-11-28 18:22:00 +0100 | [diff] [blame] | 10362 | { "scook", "res.cook", pat_parse_str, pat_match_str }, |
| 10363 | { "scook_beg", "res.cook", pat_parse_str, pat_match_beg }, |
| 10364 | { "scook_dir", "res.cook", pat_parse_str, pat_match_dir }, |
| 10365 | { "scook_dom", "res.cook", pat_parse_str, pat_match_dom }, |
| 10366 | { "scook_end", "res.cook", pat_parse_str, pat_match_end }, |
| 10367 | { "scook_len", "res.cook", pat_parse_int, pat_match_len }, |
| 10368 | { "scook_reg", "res.cook", pat_parse_reg, pat_match_reg }, |
| 10369 | { "scook_sub", "res.cook", pat_parse_str, pat_match_sub }, |
Willy Tarreau | 25c1ebc | 2012-04-25 16:21:44 +0200 | [diff] [blame] | 10370 | |
Thierry FOURNIER | a65b343 | 2013-11-28 18:22:00 +0100 | [diff] [blame] | 10371 | { "shdr", "res.hdr", pat_parse_str, pat_match_str }, |
| 10372 | { "shdr_beg", "res.hdr", pat_parse_str, pat_match_beg }, |
| 10373 | { "shdr_dir", "res.hdr", pat_parse_str, pat_match_dir }, |
| 10374 | { "shdr_dom", "res.hdr", pat_parse_str, pat_match_dom }, |
| 10375 | { "shdr_end", "res.hdr", pat_parse_str, pat_match_end }, |
| 10376 | { "shdr_len", "res.hdr", pat_parse_int, pat_match_len }, |
| 10377 | { "shdr_reg", "res.hdr", pat_parse_reg, pat_match_reg }, |
| 10378 | { "shdr_sub", "res.hdr", pat_parse_str, pat_match_sub }, |
Willy Tarreau | 25c1ebc | 2012-04-25 16:21:44 +0200 | [diff] [blame] | 10379 | |
Thierry FOURNIER | a65b343 | 2013-11-28 18:22:00 +0100 | [diff] [blame] | 10380 | { "url", "url", pat_parse_str, pat_match_str }, |
| 10381 | { "url_beg", "url", pat_parse_str, pat_match_beg }, |
| 10382 | { "url_dir", "url", pat_parse_str, pat_match_dir }, |
| 10383 | { "url_dom", "url", pat_parse_str, pat_match_dom }, |
| 10384 | { "url_end", "url", pat_parse_str, pat_match_end }, |
| 10385 | { "url_len", "url", pat_parse_int, pat_match_len }, |
| 10386 | { "url_reg", "url", pat_parse_reg, pat_match_reg }, |
| 10387 | { "url_sub", "url", pat_parse_str, pat_match_sub }, |
Willy Tarreau | 25c1ebc | 2012-04-25 16:21:44 +0200 | [diff] [blame] | 10388 | |
Thierry FOURNIER | a65b343 | 2013-11-28 18:22:00 +0100 | [diff] [blame] | 10389 | { "urlp", "urlp", pat_parse_str, pat_match_str }, |
| 10390 | { "urlp_beg", "urlp", pat_parse_str, pat_match_beg }, |
| 10391 | { "urlp_dir", "urlp", pat_parse_str, pat_match_dir }, |
| 10392 | { "urlp_dom", "urlp", pat_parse_str, pat_match_dom }, |
| 10393 | { "urlp_end", "urlp", pat_parse_str, pat_match_end }, |
| 10394 | { "urlp_len", "urlp", pat_parse_int, pat_match_len }, |
| 10395 | { "urlp_reg", "urlp", pat_parse_reg, pat_match_reg }, |
| 10396 | { "urlp_sub", "urlp", pat_parse_str, pat_match_sub }, |
Willy Tarreau | 25c1ebc | 2012-04-25 16:21:44 +0200 | [diff] [blame] | 10397 | |
Willy Tarreau | 8ed669b | 2013-01-11 15:49:37 +0100 | [diff] [blame] | 10398 | { /* END */ }, |
Willy Tarreau | 25c1ebc | 2012-04-25 16:21:44 +0200 | [diff] [blame] | 10399 | }}; |
| 10400 | |
| 10401 | /************************************************************************/ |
| 10402 | /* All supported pattern keywords must be declared here. */ |
Willy Tarreau | 4a56897 | 2010-05-12 08:08:50 +0200 | [diff] [blame] | 10403 | /************************************************************************/ |
| 10404 | /* Note: must not be declared <const> as its list will be overwritten */ |
Willy Tarreau | dc13c11 | 2013-06-21 23:16:39 +0200 | [diff] [blame] | 10405 | static struct sample_fetch_kw_list sample_fetch_keywords = {ILH, { |
Willy Tarreau | 409bcde | 2013-01-08 00:31:00 +0100 | [diff] [blame] | 10406 | { "base", smp_fetch_base, 0, NULL, SMP_T_CSTR, SMP_USE_HRQHV }, |
| 10407 | { "base32", smp_fetch_base32, 0, NULL, SMP_T_UINT, SMP_USE_HRQHV }, |
| 10408 | { "base32+src", smp_fetch_base32_src, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV }, |
| 10409 | |
William Lallemand | 65ad6e1 | 2014-01-31 15:08:02 +0100 | [diff] [blame] | 10410 | { "capture.req.uri", smp_fetch_capture_req_uri, 0, NULL, SMP_T_CSTR, SMP_USE_HRQHP }, |
| 10411 | { "capture.req.method", smp_fetch_capture_req_method, 0, NULL, SMP_T_CSTR, SMP_USE_HRQHP }, |
| 10412 | |
William Lallemand | a43ba4e | 2014-01-28 18:14:25 +0100 | [diff] [blame] | 10413 | /* capture are allocated and are permanent in the session */ |
| 10414 | { "capture.req.hdr", smp_fetch_capture_header_req, ARG1(1, UINT), NULL, SMP_T_CSTR, SMP_USE_HRQHP }, |
| 10415 | { "capture.res.hdr", smp_fetch_capture_header_res, ARG1(1, UINT), NULL, SMP_T_CSTR, SMP_USE_HRSHP }, |
| 10416 | |
Willy Tarreau | 409bcde | 2013-01-08 00:31:00 +0100 | [diff] [blame] | 10417 | /* cookie is valid in both directions (eg: for "stick ...") but cook* |
| 10418 | * are only here to match the ACL's name, are request-only and are used |
| 10419 | * for ACL compatibility only. |
| 10420 | */ |
| 10421 | { "cook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_CSTR, SMP_USE_HRQHV }, |
| 10422 | { "cookie", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_CSTR, SMP_USE_HRQHV|SMP_USE_HRSHV }, |
| 10423 | { "cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV }, |
| 10424 | { "cook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV }, |
| 10425 | |
| 10426 | /* hdr is valid in both directions (eg: for "stick ...") but hdr_* are |
| 10427 | * only here to match the ACL's name, are request-only and are used for |
| 10428 | * ACL compatibility only. |
| 10429 | */ |
| 10430 | { "hdr", smp_fetch_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_CSTR, SMP_USE_HRQHV|SMP_USE_HRSHV }, |
| 10431 | { "hdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV }, |
| 10432 | { "hdr_ip", smp_fetch_hdr_ip, ARG2(0,STR,SINT), val_hdr, SMP_T_IPV4, SMP_USE_HRQHV }, |
| 10433 | { "hdr_val", smp_fetch_hdr_val, ARG2(0,STR,SINT), val_hdr, SMP_T_UINT, SMP_USE_HRQHV }, |
| 10434 | |
Willy Tarreau | 0a0daec | 2013-04-02 22:44:58 +0200 | [diff] [blame] | 10435 | { "http_auth", smp_fetch_http_auth, ARG1(1,USR), NULL, SMP_T_BOOL, SMP_USE_HRQHV }, |
| 10436 | { "http_auth_group", smp_fetch_http_auth_grp, ARG1(1,USR), NULL, SMP_T_BOOL, SMP_USE_HRQHV }, |
Willy Tarreau | 409bcde | 2013-01-08 00:31:00 +0100 | [diff] [blame] | 10437 | { "http_first_req", smp_fetch_http_first_req, 0, NULL, SMP_T_BOOL, SMP_USE_HRQHP }, |
| 10438 | { "method", smp_fetch_meth, 0, NULL, SMP_T_UINT, SMP_USE_HRQHP }, |
| 10439 | { "path", smp_fetch_path, 0, NULL, SMP_T_CSTR, SMP_USE_HRQHV }, |
Willy Tarreau | 18ed256 | 2013-01-14 15:56:36 +0100 | [diff] [blame] | 10440 | |
| 10441 | /* HTTP protocol on the request path */ |
| 10442 | { "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] | 10443 | { "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] | 10444 | |
| 10445 | /* HTTP version on the request path */ |
| 10446 | { "req.ver", smp_fetch_rqver, 0, NULL, SMP_T_CSTR, SMP_USE_HRQHV }, |
Willy Tarreau | 409bcde | 2013-01-08 00:31:00 +0100 | [diff] [blame] | 10447 | { "req_ver", smp_fetch_rqver, 0, NULL, SMP_T_CSTR, SMP_USE_HRQHV }, |
Willy Tarreau | 18ed256 | 2013-01-14 15:56:36 +0100 | [diff] [blame] | 10448 | |
| 10449 | /* HTTP version on the response path */ |
| 10450 | { "res.ver", smp_fetch_stver, 0, NULL, SMP_T_CSTR, SMP_USE_HRSHV }, |
Willy Tarreau | 409bcde | 2013-01-08 00:31:00 +0100 | [diff] [blame] | 10451 | { "resp_ver", smp_fetch_stver, 0, NULL, SMP_T_CSTR, SMP_USE_HRSHV }, |
| 10452 | |
Willy Tarreau | 18ed256 | 2013-01-14 15:56:36 +0100 | [diff] [blame] | 10453 | /* explicit req.{cook,hdr} are used to force the fetch direction to be request-only */ |
| 10454 | { "req.cook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_CSTR, SMP_USE_HRQHV }, |
| 10455 | { "req.cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV }, |
| 10456 | { "req.cook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV }, |
| 10457 | |
Willy Tarreau | 04ff9f1 | 2013-06-10 18:39:42 +0200 | [diff] [blame] | 10458 | { "req.fhdr", smp_fetch_fhdr, ARG2(0,STR,SINT), val_hdr, SMP_T_CSTR, SMP_USE_HRQHV }, |
| 10459 | { "req.fhdr_cnt", smp_fetch_fhdr_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV }, |
Willy Tarreau | 18ed256 | 2013-01-14 15:56:36 +0100 | [diff] [blame] | 10460 | { "req.hdr", smp_fetch_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_CSTR, SMP_USE_HRQHV }, |
| 10461 | { "req.hdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV }, |
| 10462 | { "req.hdr_ip", smp_fetch_hdr_ip, ARG2(0,STR,SINT), val_hdr, SMP_T_IPV4, SMP_USE_HRQHV }, |
| 10463 | { "req.hdr_val", smp_fetch_hdr_val, ARG2(0,STR,SINT), val_hdr, SMP_T_UINT, SMP_USE_HRQHV }, |
| 10464 | |
| 10465 | /* explicit req.{cook,hdr} are used to force the fetch direction to be response-only */ |
| 10466 | { "res.cook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_CSTR, SMP_USE_HRSHV }, |
| 10467 | { "res.cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRSHV }, |
| 10468 | { "res.cook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRSHV }, |
| 10469 | |
Willy Tarreau | 04ff9f1 | 2013-06-10 18:39:42 +0200 | [diff] [blame] | 10470 | { "res.fhdr", smp_fetch_fhdr, ARG2(0,STR,SINT), val_hdr, SMP_T_CSTR, SMP_USE_HRSHV }, |
| 10471 | { "res.fhdr_cnt", smp_fetch_fhdr_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRSHV }, |
Willy Tarreau | 18ed256 | 2013-01-14 15:56:36 +0100 | [diff] [blame] | 10472 | { "res.hdr", smp_fetch_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_CSTR, SMP_USE_HRSHV }, |
| 10473 | { "res.hdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRSHV }, |
| 10474 | { "res.hdr_ip", smp_fetch_hdr_ip, ARG2(0,STR,SINT), val_hdr, SMP_T_IPV4, SMP_USE_HRSHV }, |
| 10475 | { "res.hdr_val", smp_fetch_hdr_val, ARG2(0,STR,SINT), val_hdr, SMP_T_UINT, SMP_USE_HRSHV }, |
| 10476 | |
Willy Tarreau | 409bcde | 2013-01-08 00:31:00 +0100 | [diff] [blame] | 10477 | /* scook is valid only on the response and is used for ACL compatibility */ |
| 10478 | { "scook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_CSTR, SMP_USE_HRSHV }, |
| 10479 | { "scook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRSHV }, |
| 10480 | { "scook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRSHV }, |
| 10481 | { "set-cookie", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_CSTR, SMP_USE_HRSHV }, /* deprecated */ |
| 10482 | |
| 10483 | /* shdr is valid only on the response and is used for ACL compatibility */ |
| 10484 | { "shdr", smp_fetch_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_CSTR, SMP_USE_HRSHV }, |
| 10485 | { "shdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRSHV }, |
| 10486 | { "shdr_ip", smp_fetch_hdr_ip, ARG2(0,STR,SINT), val_hdr, SMP_T_IPV4, SMP_USE_HRSHV }, |
| 10487 | { "shdr_val", smp_fetch_hdr_val, ARG2(0,STR,SINT), val_hdr, SMP_T_UINT, SMP_USE_HRSHV }, |
| 10488 | |
| 10489 | { "status", smp_fetch_stcode, 0, NULL, SMP_T_UINT, SMP_USE_HRSHP }, |
| 10490 | { "url", smp_fetch_url, 0, NULL, SMP_T_CSTR, SMP_USE_HRQHV }, |
Neil - HAProxy List | 39c63c5 | 2013-11-04 13:48:42 +0000 | [diff] [blame] | 10491 | { "url32", smp_fetch_url32, 0, NULL, SMP_T_UINT, SMP_USE_HRQHV }, |
| 10492 | { "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] | 10493 | { "url_ip", smp_fetch_url_ip, 0, NULL, SMP_T_IPV4, SMP_USE_HRQHV }, |
| 10494 | { "url_port", smp_fetch_url_port, 0, NULL, SMP_T_UINT, SMP_USE_HRQHV }, |
| 10495 | { "url_param", smp_fetch_url_param, ARG2(1,STR,STR), NULL, SMP_T_CSTR, SMP_USE_HRQHV }, |
| 10496 | { "urlp" , smp_fetch_url_param, ARG2(1,STR,STR), NULL, SMP_T_CSTR, SMP_USE_HRQHV }, |
| 10497 | { "urlp_val", smp_fetch_url_param_val, ARG2(1,STR,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV }, |
| 10498 | { /* END */ }, |
Willy Tarreau | 4a56897 | 2010-05-12 08:08:50 +0200 | [diff] [blame] | 10499 | }}; |
| 10500 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 10501 | |
Willy Tarreau | 276fae9 | 2013-07-25 14:36:01 +0200 | [diff] [blame] | 10502 | /* Note: must not be declared <const> as its list will be overwritten */ |
| 10503 | static struct sample_conv_kw_list sample_conv_kws = {ILH, { |
| 10504 | { "http_date", sample_conv_http_date, ARG1(0,SINT), NULL, SMP_T_UINT, SMP_T_STR }, |
| 10505 | { NULL, NULL, 0, 0, 0 }, |
| 10506 | }}; |
| 10507 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 10508 | __attribute__((constructor)) |
| 10509 | static void __http_protocol_init(void) |
| 10510 | { |
| 10511 | acl_register_keywords(&acl_kws); |
Willy Tarreau | 1278578 | 2012-04-27 21:37:17 +0200 | [diff] [blame] | 10512 | sample_register_fetches(&sample_fetch_keywords); |
Willy Tarreau | 276fae9 | 2013-07-25 14:36:01 +0200 | [diff] [blame] | 10513 | sample_register_convs(&sample_conv_kws); |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 10514 | } |
| 10515 | |
| 10516 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 10517 | /* |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 10518 | * Local variables: |
| 10519 | * c-indent-level: 8 |
| 10520 | * c-basic-offset: 8 |
| 10521 | * End: |
| 10522 | */ |